openj9
openj9 copied to clipboard
Handling 0-length arrays in JNI Array Critical in GC
The representation of 0-length array <class><0><0>
is technically Discontiguous. The criteria for Discontiguous "size in elements" field contains 0 and it happen for 0-length array naturally due size 0.
According the spec JNI Array Critical section has to have Contiguous representation. If JVM stores an array in any discontiguous form internally it's data part should be copied to the memory in contiguous form.
Currently GC handles 0-length array naturally as Discontiguous. It means at "Get" side we malloc memory (8 bytes VM header word + 0 bytes reserved for array body, so 8 bytes total) and copy 0 bytes to it. Malloc'ed memory is wrapped with Memory Tags for header and footer of course. At "Release" size we copy 0 bytes from malloc'ed memory back to the heap and free malloc'ed memory. These actions are not necessary for 0-length arrays and can be avoided. We can handle 0-length arrays as Contiguous.
The motivation for this change is not only code simplification but also preparing for coming off-heap changes.
The length of data part for 0-length array is 0 obviously. Despite API returns starting address for Critical Section (points to malloc'ed memory after VM header word) nothing should be written to it. So, currently by returning an address in malloc'ed memory we have control that nothing is written to the memory - footer memory tag should not to be corrupted. By switching to contiguous path the returned address is going to be from object heap range. We can get less noticeable heap corruption potentially. I think this is only a factor we can take to consideration to not switch to contiguous array handling path.