polyglot-compiler / JLang

JLang: Ahead-of-time compilation of Java programs to LLVM
http://polyglot-compiler.github.io/JLang/
Other
284 stars 29 forks source link

Fix static field offset bug #41

Closed guoyiteng closed 5 years ago

guoyiteng commented 5 years ago

Previously we used the absolute address to access field offset. However, the address was larger than 32bit and the method sun.misc.Unsafe.fieldOffset(field) truncated it to 32 bit. When a static variable had address larger than 32 bit, it would lead to a segfault.

Now, we use the relative address instead, and the base pointer is the fieldObj. We compute the offset in Java_sun_misc_Unsafe_objectFieldOffset and return fieldObj in Java_sun_misc_Unsafe_staticFieldBase. We make an assumption that the static variable is within 32bit range of the fieldObj. It should be safe since fieldObj is allocated on the heap and static variable is on the .bss section.

dz333 commented 5 years ago

This isn't a perfect fix, since we don't actually know how far away the FieldObj pointer and the static field pointer will be from each other in memory. However, its a reasonable hotfix and can't make anything MORE broken.