0

I'm trying to add a new procedure to Java native methods. To do this, I download the OpenJDK 7 source code to build a new JVM that contains my procedure in Windows XP (32 bits).

My problem is how to calculate the address of an element of array object Java in the heap?

Suppose that I create an array T of 5 elements, each element is composed of 2 integer element (int x, int y) [8 bytes (4 byte for x and 4 bytes for y)].

To get T[0], we need to get the address containing in T[0] and load it content from memory.

I've implemented this method in Microsoft VS2010 and it works very well:

Elmt : is an object of 2 integer Elmt(int x,int y)

static void test(Elmt* from, Elmt* to, size_t count) {

    from += count - 1;
    to   += count - 1;
    while (count-- > 0) {
             update(from);
                     *to-- = *from--;
                        }
                 }           


static void update (Elmt* p){

        __asm{
             mov         ecx, p 
                 mov         [ecx], 7
             }

                 }

The procedure update change the element as follow: x=7 For example: T[0] = (1, 1) after update will be T[0] = (7, 1).

Now, when I add this method to OpenJDK 7, I have a problem with the address of elements in JVM. I could not calculate the correct address of the elements from the addresses in array T.

The code in OpenJDK is as follows, with OOP the ordinary object used by the JVM

static void test(oop* from, oop * to, size_t count) {

    from += count - 1;
    to   += count - 1;
    while (count-- > 0) {
             update(from);
                     *to-- = *from--;
                        }
                 }           


static void update (oop * p){
        __asm{
             mov         ecx, p 
                 mov         [ecx], 7
                 }

                 }

My question is: How to calculate in Assembly the correct address of element of the array T in JVM.

In C++ it works very well but in Java? I know that the addressing memory mode of object in C++ is different from Java: http://www.javamex.com/tutorials/memory/object_memory_usage.shtml

Note: I did not used a CompressedOops https://wikis.oracle.com/display/HotSpotInternals/CompressedOops

  • Not sure, but did yoou take into account the additional data which might be associated with a data structure? You seem to assume that the data is exactly where you calculate it, based on the size of a member, but this assumption can be wrong. Don't know about Java interfacing, but are you sure that the relevant data starts at the start of the structure? – Devolus Dec 14 '13 at 14:03
  • you are right, java add additional data to the object, for example the header of object take 8 bytes. i tried ** mov [ecx+8], 7 ** to avoids the header bytes, but it not works !! – user3102197 Dec 14 '13 at 14:30
  • Why do you think it should be ecx **+8** and not something else? – Devolus Dec 14 '13 at 14:44
  • I'm not sure, I just tried to avoid the 8 bytes of the header (ecx +8 ) to get the content of the element. – user3102197 Dec 14 '13 at 14:53
  • I would have epxected that you answer with something like `I looked into the source and from that I thought it should be`. Obviously you must look into the SDK, how the structure looks like when compiled and only then can know the correct offset for a particular implementation. – Devolus Dec 14 '13 at 14:55
  • inside openjdk7 code source, the offset is not a problem, it is given by the methods JVM as follow : void objArrayKlass :: test1 (arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) { oop * src = objArrayOop (s) -> obj_at_addr (src_pos); oop * dst = (d) -> obj_at_addr (dst_pos);     Copy :: test (src, dst, length); } – user3102197 Dec 14 '13 at 15:09
  • I think the element is encapsulated in oop – user3102197 Dec 14 '13 at 15:13

0 Answers0