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

Allocations larger than MAX_ALIGN can fail to be sliced to proper size.

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: repo-panama
    • Fix Version/s: None
    • Component/s: tools

      Description

      We have been seeing some infra failures in the test java/foreign/TestAllocationScope.java with the message: Exception: java.lang.AssertionError: expected [4096] but found [8191]

      It seems that the code in NativeMemorySegmentImpl that handles the allocation:

          public static MemorySegment makeNativeSegment(long bytesSize, long alignmentBytes) {
              long alignedSize = bytesSize;

              if (alignmentBytes > MAX_ALIGN) {
                  alignedSize = bytesSize + (alignmentBytes - 1);
              }

              long buf = unsafe.allocateMemory(alignedSize);
              if (!skipZeroMemory) {
                  unsafe.setMemory(buf, alignedSize, (byte)0);
              }
              long alignedBuf = Utils.alignUp(buf, alignmentBytes);
              MemoryScope scope = new MemoryScope(null, () -> unsafe.freeMemory(buf));
              MemorySegment segment = new NativeMemorySegmentImpl(buf, alignedSize, defaultAccessModes(alignedSize),
                      Thread.currentThread(), scope);
              if (alignedBuf != buf) {
                  long delta = alignedBuf - buf;
                  segment = segment.asSlice(delta, bytesSize);
              }
              return segment;
          }

      Particularly the path that deals with manually aligning allocations, fails to slice the returned segment when the allocated buffer is already aligned incidentally, since it uses:

          alignedBuf != buf

      This check could be changed to:

          alignedSize != byteSize

      To make sure the resulting segment is always sliced to the right size when there is a size mismatch.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              jvernee Jorn Vernee
              Reporter:
              jvernee Jorn Vernee
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: