Uploaded image for project: 'Code Tools'
  1. Code Tools
  2. CODETOOLS-7903670

jextract generates wrong packed struct layout

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • None
    • None
    • tools
    • None

      Consider this:

      ```
      typedef union epoll_data
      {
        void *ptr;
        int fd;
        int u32;
        long u64;
      } epoll_data_t;

      #pragma pack(1)
      struct epoll_event
      {
        int events;
        epoll_data_t data;
      } __EPOLL_PACKED;
      ```

      Jextract generates the following layout for "epoll_event":

      ```
      private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
              foo_h.C_INT.withByteAlignment(1).withName("events"),
              epoll_data.layout().withName("data")
          ).withName("epoll_event");
      ```

      As can be seen, the "epoll_data" struct field is missing the "withByteAlignment" suffix. This results in a runtime exception when the layout is accessed:

      Exception in thread "main" java.lang.ExceptionInInitializerError
      at TestGensrc.main(TestGensrc.java:14)
      Caused by: java.lang.IllegalArgumentException: Invalid alignment constraint for member layout: [a8(ptr):[*:b1]|i4(fd)|i4(u32)|j8(u64)](data)
      at java.base/jdk.internal.foreign.layout.StructLayoutImpl.of(StructLayoutImpl.java:49)
      at java.base/java.lang.foreign.MemoryLayout.lambda$structLayout$1(MemoryLayout.java:1063)
      at java.base/jdk.internal.foreign.Utils.wrapOverflow(Utils.java:266)
      at java.base/java.lang.foreign.MemoryLayout.structLayout(MemoryLayout.java:1062)
      at test.epoll_event.<clinit>(epoll_event.java:29)

            mcimadamore Maurizio Cimadamore
            mcimadamore Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: