RFR: 8139457: Array bases are aligned at HeapWord granularity [v52]

Roman Kennke rkennke at openjdk.org
Wed Aug 23 09:39:48 UTC 2023


On Tue, 22 Aug 2023 11:52:33 GMT, Roman Kennke <rkennke at openjdk.org> wrote:

>> See [JDK-8139457](https://bugs.openjdk.org/browse/JDK-8139457) for details.
>> 
>> Basically, when running with -XX:-UseCompressedClassPointers, arrays will have a gap between the length field and the first array element, because array elements will only start at word-aligned offsets. This is not necessary for smaller-than-word elements.
>> 
>> Also, while it is not very important now, it will become very important with Lilliput, which eliminates the Klass field and would always put the length field at offset 8, and leave a gap between offset 12 and 16.
>> 
>> Testing:
>>  - [x] runtime/FieldLayout/ArrayBaseOffsets.java (x86_64, x86_32, aarch64, arm, riscv, s390)
>>  - [x] bootcycle (x86_64, x86_32, aarch64, arm, riscv, s390)
>>  - [x] tier1 (x86_64, x86_32, aarch64, riscv)
>>  - [x] tier2 (x86_64, aarch64, riscv)
>>  - [x] tier3 (x86_64, riscv)
>
> Roman Kennke has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Move away arrayOopDesc::header_size()

> Note that this can break existing Java applications:
> 
> ```java
> import java.lang.invoke.*;
> import java.util.*;
> java.nio.*;
> 
> class Aligned {
>         public static void main(String[] args) throws Throwable {
>                 var handle = MethodHandles.byteArrayViewVarHandle(long[].class, ByteOrder.nativeOrder());
>                 byte[] array = new byte[64];
>                 handle.compareAndSet(array, 0, 0, 1234567890L);
>                 System.out.println(Arrays.toString(array));
>         }
> }
> ```
> 
> This code currently runs fine but fails with this patch and running with `-XX:-UseCompressedClassPointers` due to the misaligned access. While the Javadocs make no guarantee about the alignment of byte arrays and even suggests a way to calculate the alignment, this should be considered here I think.

Thanks for pointing that out! The [spec](https://docs.oracle.com/javase%2F9%2Fdocs%2Fapi%2F%2F/java/lang/invoke/MethodHandles.html#byteArrayViewVarHandle-java.lang.Class-java.nio.ByteOrder-) says this:

"Access of bytes at an index may be aligned or misaligned for T, with respect to the underlying memory address, A say, associated with the array and index. If access is misaligned then access for anything other than the get and set access modes will result in an IllegalStateException. In such cases atomic access is only guaranteed with respect to the largest power of two that divides the GCD of A and the size (in bytes) of T."

This reads to me as if all primitive arrays are required, by that spec, to be 8-byte aligned, because how else could the above be guaranteed? If so, then this PR would need to be dropped, or at least be reworked to only change object arrays. WDYT?

-------------

PR Comment: https://git.openjdk.org/jdk/pull/11044#issuecomment-1689625577


More information about the hotspot-dev mailing list