|
View:
New views
10 Messages
—
Rating Filter:
Alert me
|
|
|
java memory allcation with gcjDear All,
I am interested in studying how "new" allocates memory when running as native binary. For this I wrote a simple Java class which just create an int array. Then I generate the assembly with gcj -S option. There I spotted the call to _Jv_NewPrimArray. Since the result of the call is stored in eax register I check the value of the eax register after this call. But it is giving a small number. I tried the same thing with C. There I spotted the call to malloc and check the eax after the malloc call and it has the same value as the &variable_name has. So my objective is to identify the are of memory allocated by "new". Any help/advice on this regard is greatly appreciated. thanks and regards, Isuru |
|
|
Re: java memory allocation with gcjisuru herath wrote:
> I am interested in studying how "new" allocates memory when running > as native binary. For this I wrote a simple Java class which just > create an int array. Then I generate the assembly with gcj -S > option. There I spotted the call to _Jv_NewPrimArray. Since the > result of the call is stored in eax register I check the value of > the eax register after this call. But it is giving a small number. I > tried the same thing with C. There I spotted the call to malloc and > check the eax after the malloc call and it has the same value as the > &variable_name has. _Jv_NewPrimArray calls the memory allocator (actually part of the Boehm garbage collector) which calls mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, ..) to map the memory. Andrew. |
|
|
|
|
|
|
|
|
|
|
|
Re: java memory allocation with gcjisuru herath wrote:
> Thanks for the reply. I tried in x $eax gdb after the new being executed an > it was giving me the following. > 0x804a0e0 <_ZN13test_java_new6class$E>: 0xb7a4afe8 > > When converted to decimal it was 134521056 which seems close to that of C. > Could you please tell me what the other value represents here. > > Seems like the way I was doing is wrong. But I couldn't find another way. > > This is my Java class. > > class test_java_new > { > public static void main(String[]args) > { > int isuru[]=new int[2]; > isuru[0] = 12345; > } > } > > Then I compiled this with gcj -S. Then I open the test_java_new.s file. > There I add following lines after the > call _Jv_NewPrimArray line. > > The lines I am adding are > pushl %ecx #;for start location > movl %eax, %ecx #;copy value in eax to ecx > push %eax #;the memory tag > movl $5, %eax #;set eax to HEAP tag > xchg %bx, %bx #;magic instruction > popl %eax > popl %ecx This looks right. > Why I am doing this is, when the xchg %bx, %bx is executed my > simulator (Simics) triggers an event and my hardware module can get > the control of the system. So that I can pass information from the > user program to my hardware module. But when I check the eax > register at my hardware module after xchg %bx, %bx , I got that > small number which is 166816. But when I add the same assembly code > soon after the call malloc in a gcc generated assembly of a C > program, my hardware module gets the correct address. Therefore I > was thinking it should be the same here. It certainly should be. We know that when you print out $eax in gdb, you get the correct value. > Some other thing I noticed is that when I invoke x $eax after the > isuru[0] = 12345; I got 0X28bc0 which is quite similar to 166848. I > don't know this helps you to help me to clarify my problem. Let's see the assembly code of the whole function, before and after your changes. Andrew. |
|
|
Re: java memory allocation with gcjHi Andrew,
Thanks a lot for the reply. Here is the code. before change globl _ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE .type _ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE, @function _ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE: .LFB2: pushl %ebp .LCFI0: movl %esp, %ebp .LCFI1: subl $40, %esp .LCFI2: movl $_ZN13test_java_new6class$E, (%esp) call _Jv_InitClass .LBB2: movl $2, 4(%esp) movl $_Jv_intClass, (%esp) call _Jv_NewPrimArray movl %eax, -4(%ebp) movl -4(%ebp), %eax movl %eax, -20(%ebp) movl -20(%ebp), %edx movl 4(%edx), %eax testl %eax, %eax jne .L2 movl $0, (%esp) call _Jv_ThrowBadArrayIndex .L2: movl $0, %eax movl -20(%ebp), %edx movl $12345, 8(%edx,%eax,4) .LBE2: leave ret after change .globl _ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE .type _ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE, @function _ZN13test_java_new4mainEJvP6JArrayIPN4java4lang6StringEE: .LFB2: pushl %ebp .LCFI0: movl %esp, %ebp .LCFI1: subl $40, %esp .LCFI2: movl $_ZN13test_java_new6class$E, (%esp) call _Jv_InitClass .LBB2: movl $2, 4(%esp) movl $_Jv_intClass, (%esp) call _Jv_NewPrimArray #;added by isuru pushl %ecx #;for start location movl %eax, %ecx #;copy value in eax to ecx push %eax #;the memory tag movl $5, %eax #;set eax to HEAP tag xchg %bx, %bx #;magic instruction popl %eax popl %ecx #;added by isuru movl %eax, -4(%ebp) movl -4(%ebp), %eax movl %eax, -20(%ebp) movl -20(%ebp), %edx movl 4(%edx), %eax testl %eax, %eax jne .L2 movl $0, (%esp) call _Jv_ThrowBadArrayIndex .L2: movl $0, %eax movl -20(%ebp), %edx movl $12345, 8(%edx,%eax,4) .LBE2: leave ret the steps I follwed. 1. gcj -S test_java_new.java 2. edit the test_java_new.s file 3. as test_java_new.s -o test_java_new.o 4. gcj --main=test_java_new -o test_java_new test_java_new.o Is there anything wrong or do I need to do anything extra. your help on this regard is greatly appreciated. sincerely, isuru --- On Fri, 11/6/09, Andrew Haley <aph@...> wrote: > From: Andrew Haley <aph@...> > Subject: Re: java memory allocation with gcj > To: "isuru herath" <isuru81@...> > Cc: java@... > Date: Friday, November 6, 2009, 1:54 AM > isuru herath wrote: > > > Thanks for the reply. I tried in x $eax gdb after the new being executed an it was giving me the following. > > 0x804a0e0 <_ZN13test_java_new6class$E>: 0xb7a4afe8 > > > > When converted to decimal it was 134521056 which seems close to that of C. > > Could you please tell me what the other value represents here. > > > > Seems like the way I was doing is wrong. But I couldn't find another way. > > > > This is my Java class. > > > > class test_java_new > > { > > public static > void main(String[]args) > > { > > > int isuru[]=new int[2]; > > > isuru[0] = 12345; > > } > > } > > > > Then I compiled this with gcj -S. Then I open the > test_java_new.s file. > > There I add following lines after the > > call _Jv_NewPrimArray line. > > > > The lines I am adding are > > > pushl %ecx > #;for start location > > movl > %eax, %ecx #;copy value in eax to > ecx > > push > %eax #;the > memory tag > > movl > $5, %eax #;set eax to HEAP > tag > > xchg > %bx, %bx #;magic > instruction > > popl > %eax > > popl > %ecx > > This looks right. > > > Why I am doing this is, when the xchg %bx, %bx is > executed my > > simulator (Simics) triggers an event and my hardware > module can get > > the control of the system. So that I can pass > information from the > > user program to my hardware module. But when I check > the eax > > register at my hardware module after xchg %bx, %bx , I > got that > > small number which is 166816. But when I add the same > assembly code > > soon after the call malloc in a gcc generated assembly > of a C > > program, my hardware module gets the correct address. > Therefore I > > was thinking it should be the same here. > > It certainly should be. We know that when you print > out $eax in gdb, > you get the correct value. > > > Some other thing I noticed is that when I invoke x > $eax after the > > isuru[0] = 12345; I got 0X28bc0 which is quite similar > to 166848. I > > don't know this helps you to help me to clarify my > problem. > > Let's see the assembly code of the whole function, before > and after > your changes. > > Andrew. > > |
|
|
Re: java memory allocation with gcjHi Andrew,
You were right. The memory address I am getting was correct eventhough it is a small number compared to the one I got in C. The problem I had was, when I read that location it was not giving me the correct data I stored there. The problem was the memroy_read function of the simulator was expecting the physical address where as I was giving the logical address. When obtained the physical address corresponds to this logical address and give it to the memory_read function it gives me the correct value. Thanks a lot for your help to clarify this. regards, Isuru |
|
|
Re: java memory allocation with gcjOn Fri, Nov 6, 2009 at 4:02 PM, isuru herath <isuru81@...> wrote:
> You were right. The memory address I am getting was correct eventhough it is > a small number compared to the one I got in C. The problem I had was, when I > read that location it was not giving me the correct data I stored there. The > problem was the memroy_read function of the simulator was expecting the > physical address where as I was giving the logical address. When obtained the > physical address corresponds to this logical address and give it to the > memory_read function it gives me the correct value. Also be aware that Java arrays are different to C arrays. Unlike C, in Java the pointer returned from the allocator function will not correspond to the address of the first element stored in the array. This is due to the class identifier and array size being stored as well. Bryce |
|
|
Re: java memory allocation with gcjHi Bryce,
Thanks a lot for the mail. Ya I noticed that too. When I tried to store data in the first location of the array, in the assembly it was shown as the (base+8)th location which seems like 8 byte space for the header info. Thanks for the clarification. regards, Isuru --- On Fri, 11/6/09, Bryce McKinlay <bmckinlay@...> wrote: > From: Bryce McKinlay <bmckinlay@...> > Subject: Re: java memory allocation with gcj > To: "isuru herath" <isuru81@...> > Cc: "Andrew Haley" <aph@...>, java@... > Date: Friday, November 6, 2009, 8:43 AM > On Fri, Nov 6, 2009 at 4:02 PM, isuru > herath <isuru81@...> > wrote: > > > You were right. The memory address I am getting was > correct eventhough it is > > a small number compared to the one I got in C. The > problem I had was, when I > > read that location it was not giving me the correct > data I stored there. The > > problem was the memroy_read function of the simulator > was expecting the > > physical address where as I was giving the logical > address. When obtained the > > physical address corresponds to this logical address > and give it to the > > memory_read function it gives me the correct value. > > Also be aware that Java arrays are different to C arrays. > Unlike C, in > Java the pointer returned from the allocator function will > not > correspond to the address of the first element stored in > the array. > This is due to the class identifier and array size being > stored as > well. > > Bryce > |
| Free embeddable forum powered by Nabble | Forum Help |