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

Padding computed by CallGeneratorHelper can be incorrect

XMLWordPrintable

      CallGeneratorHelper (in the tests) computes layouts of struct with the following code:

                      long offset = 0L;
                      List<MemoryLayout> layouts = new ArrayList<>();
                      long align = 0;
                      for (StructFieldType field : fields) {
                          MemoryLayout l = field.layout();
                          long padding = offset % l.bitAlignment();
                          if (padding != 0) {
                              layouts.add(MemoryLayout.paddingLayout(padding));
                              offset += padding;
                          }
                          layouts.add(l.withName("field" + offset));
                          align = Math.max(align, l.bitAlignment());
                          offset += l.bitSize();
                      }
                      long padding = offset % align;
                      if (padding != 0) {
                          layouts.add(MemoryLayout.paddingLayout(padding));
                      }
                      return MemoryLayout.structLayout(layouts.toArray(new MemoryLayout[0]));

      However, the padding computation `offset % l.bitAlignment()` is incorrect.

      Suppose we have a struct as follows:

          struct {
              char x;
              void* y;
          }

      The offset after processing the first field is 8, and the alignment of the second field is 64. So the computed padding is 8 % 64 = 8 which is incorrect, as it should be 56, i.e. (64 - (8 % 64)) % 64, i.e. (l.bitAlignment() - (offset % l.bitAlignment())) % l.bitAlignment().

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

              Created:
              Updated:
              Resolved: