- 
    Bug 
- 
    Resolution: Unresolved
- 
     P3 P3
- 
    22, 23, 24
- 
    None
                    There should be an up-front check of the alignment when constructing a VarHandle via the MemoryLayout::arrayElementVarHandle method.
When we create a strided var handle, we should check that the layout has a size multiple of its alignment (as the layout is going to be used as an element layout).
Reproduce:
jshell> import java.lang.foreign.*;
jshell> import java.lang.invoke.*;
jshell> var HYPER_INT = ValueLayout.JAVA_INT.withByteAlignment(8);
HYPER_INT ==> 8%i4
jshell> var HYPER_HANDLE = HYPER_INT.arrayElementVarHandle();
HYPER_HANDLE ==> VarHandle[varType=int, coord=[interface java.lang ... emorySegment, long, long]]
jshell> var segment = Arena.ofAuto().allocate(100);
segment ==> MemorySegment{ address: 0x7ff500273040, byteSize: 100 }
jshell> HYPER_HANDLE.get(segment, 0, 0);
$6 ==> 0
jshell> HYPER_HANDLE.get(segment, 0, 1);
| Exception java.lang.IllegalArgumentException: Target offset 4 is incompatible with alignment constraint 8 (of 8%i4) for segment MemorySegment{ address: 0x7ff500273040, byteSize: 100 }
| at AbstractMemorySegmentImpl.checkEnclosingLayout (AbstractMemorySegmentImpl.java:338)
| at VarHandleSegmentAsInts.checkSegment (VarHandleSegmentAsInts.java:83)
| at VarHandleSegmentAsInts.get (VarHandleSegmentAsInts.java:104)
| at (#7:1)
            
When we create a strided var handle, we should check that the layout has a size multiple of its alignment (as the layout is going to be used as an element layout).
Reproduce:
jshell> import java.lang.foreign.*;
jshell> import java.lang.invoke.*;
jshell> var HYPER_INT = ValueLayout.JAVA_INT.withByteAlignment(8);
HYPER_INT ==> 8%i4
jshell> var HYPER_HANDLE = HYPER_INT.arrayElementVarHandle();
HYPER_HANDLE ==> VarHandle[varType=int, coord=[interface java.lang ... emorySegment, long, long]]
jshell> var segment = Arena.ofAuto().allocate(100);
segment ==> MemorySegment{ address: 0x7ff500273040, byteSize: 100 }
jshell> HYPER_HANDLE.get(segment, 0, 0);
$6 ==> 0
jshell> HYPER_HANDLE.get(segment, 0, 1);
| Exception java.lang.IllegalArgumentException: Target offset 4 is incompatible with alignment constraint 8 (of 8%i4) for segment MemorySegment{ address: 0x7ff500273040, byteSize: 100 }
| at AbstractMemorySegmentImpl.checkEnclosingLayout (AbstractMemorySegmentImpl.java:338)
| at VarHandleSegmentAsInts.checkSegment (VarHandleSegmentAsInts.java:83)
| at VarHandleSegmentAsInts.get (VarHandleSegmentAsInts.java:104)
| at (#7:1)
- relates to
- 
                    JDK-8346651 Optimize the MemoryLayout::arrayElementVarHandle -           
- Open
 
-