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

Hannes Greule hgreule at openjdk.org
Wed Aug 23 09:17:26 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:


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.

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

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


More information about the hotspot-dev mailing list