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

Reject packed structs from linker

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P2
    • 21
    • core-libs
    • None
    • behavioral
    • minimal
    • Preview API. We more eagerly reject memory layouts that are not supported, rather then attempting to link with them, which would likely result in broken behavior (e.g. crashes, value/memory corruption).
    • Java API

    Description

      Summary

      Detect and reject unsupported memory layouts that are passed to the foreign linker.

      Problem

      The various linker implementations do not reliably support certain memory layouts, such as packed structs, layouts that do not have natural alignment, or layouts that do not have the same byte order set as the underlying platform. This is largely due to insufficient specification of the underlying ABI, which prevents us from consistently implementing support for these layouts across platforms.

      Solution

      Detect and reject these layouts eagerly during linking, and specify exactly which restrictions apply to the layouts that can be used to link with. (dubbed 'canonical layouts').

      At the time of writing, it is believed that these layouts are uncommon enough that not supporting them won't be a problem in practice. They have never really been supported before either.

      Specification

      diff --git a/src/java.base/share/classes/java/lang/foreign/Linker.java b/src/java.base/share/classes/java/lang/foreign/Linker.java
      index eabe887881f6..082f562da15f 100644
      --- a/src/java.base/share/classes/java/lang/foreign/Linker.java
      +++ b/src/java.base/share/classes/java/lang/foreign/Linker.java
      @@ -195,6 +196,17 @@
        *     <td style="text-align:center;">{@link MemorySegment}</td>
        * </tbody>
        * </table></blockquote>
      + * <p>
      + * All the native linker implementations limit the function descriptors that they support to those that contain
      + * only so-called <em>canonical</em> layouts. A canonical layout has the following characteristics:
      + * <ol>
      + * <li>Its alignment constraint is set to its <a href="MemoryLayout.html#layout-align">natural alignment</a></li>
      + * <li>If it is a {@linkplain ValueLayout value layout}, its {@linkplain ValueLayout#order() byte order} is
      + * the {@linkplain ByteOrder#nativeOrder() native byte order}.
      + * <li>If it is a {@linkplain GroupLayout group layout}, its size is a multiple of its alignment constraint, and</li>
      + * <li>It does not contain padding other than what is strictly required to align its non-padding layout elements,
      + * or to satisfy constraint 3</li>
      + * </ol>
        *
        * <h3 id="function-pointers">Function pointers</h3>
        *

      Attachments

        Issue Links

          Activity

            People

              jvernee Jorn Vernee
              jvernee Jorn Vernee
              Maurizio Cimadamore
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: