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

Consolidate size and alignment checks in LayoutPath

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P3 P3
    • 23
    • None
    • core-libs
    • None

      As described in https://github.com/openjdk/jdk/pull/19124, when creating a nested memory access var handle, we ensure that the segment is accessed at the correct alignment for the root layout being accessed. But we do not ensure that the segment has at least the size of the accessed root layout. If we verified that, it would be possible for nested var handles to just piggy back on the root layout size check (since a nested var handle is guaranteed, by construction, to only access parts of the root layout). This could improve usability, as certain errors will currently go undetected. Consider a layout like this:

      ```
      MemoryLayout LAYOUT = sequenceLayout(2, structLayout(JAVA_INT.withName("x"), JAVA_INT.withName("y")));
      VarHandle X_HANDLE = LAYOUT.varHandle(PathElement.sequenceElement(), PathElement.groupElement("x"));
      ```

      If I have a memory segment whose size is 12, I can successfully call X_HANDLE on it with index 1, like so:

      ```
      MemorySegment segment = Arena.ofAuto().allocate(12);
      int x = (int)X_HANDLE.get(segment, 0, 1);
      ```

      This seems incorrect: after all, the provided segment doesn't have enough space to fit _two_ elements of the nested structs.





            mcimadamore Maurizio Cimadamore
            pminborg Per-Ake Minborg
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: