-
Bug
-
Resolution: Fixed
-
P3
-
None
-
repo-panama
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().
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().
- duplicates
-
JDK-8301239 test/jdk/java/foreign/CallGeneratorHelper.java padding not always correct
-
- Closed
-