Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8346623

Missing alignment check in MemoryLayout::arrayElementVarHandle

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • None
    • 22, 23, 24
    • core-libs
    • 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)

            pminborg Per-Ake Minborg
            pminborg Per-Ake Minborg
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: