Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Jtreg]Fix the issue with addVarg/nextVarg of VaList on Power #514

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ private Object readArg(MemoryLayout argLayout, SegmentAllocator allocator) {
argument = argHandle.get(segment);
}
case STRUCT -> {
/* Copy the struct argument with the aligned size from the va_list buffer to allocated memory */
argument = allocator.allocate(argByteSize).copyFrom(segment.asSlice(0, argByteSize));
/* With the smaller size of the allocated struct segment and the corresponding layout,
* it ensures the struct value is copied correctly from the va_list segment to the
* returned struct argument.
*/
MemorySegment structSegment = allocator.allocate(argLayout);
long structByteSize = getSmallerStructArgSize(structSegment, argLayout);
argument = structSegment.copyFrom(segment.asSlice(0, structByteSize));
}
default -> throw new IllegalStateException("Unsupported TypeClass: " + typeClass);
}
Expand Down Expand Up @@ -155,6 +160,11 @@ private void checkNextArgument(MemoryLayout argLayout, long argByteSize) {
}
}

private static long getSmallerStructArgSize(MemorySegment structSegment, MemoryLayout structArgLayout) {
return (structSegment.byteSize() > structArgLayout.byteSize()) ?
structArgLayout.byteSize() : structSegment.byteSize();
}

@Override
public void skip(MemoryLayout... layouts) {
Objects.requireNonNull(layouts);
Expand Down Expand Up @@ -257,20 +267,29 @@ public VaList build() {
MemorySegment cursorSegment = segment;

for (SimpleVaArg arg : stackArgs) {
Object argValue = arg.value;
MemoryLayout argLayout = arg.layout;
long argByteSize = getAlignedArgSize(argLayout);
TypeClass typeClass = TypeClass.classifyLayout(argLayout);

switch (typeClass) {
case PRIMITIVE, POINTER -> {
VarHandle argHandle = TypeClass.classifyVarHandle((ValueLayout)argLayout);
argHandle.set(cursorSegment, arg.value);
argHandle.set(cursorSegment, argValue);
}
case STRUCT -> {
cursorSegment.copyFrom((MemorySegment)(arg.value));
/* With the smaller size of the struct argument and the corresponding layout,
* it ensures the struct value is copied correctly from the struct argument
* to the va_list.
*/
MemorySegment structSegment = (MemorySegment)argValue;
long structByteSize = getSmallerStructArgSize(structSegment, argLayout);
cursorSegment.copyFrom(structSegment.asSlice(0, structByteSize));
}
default -> throw new IllegalStateException("Unsupported TypeClass: " + typeClass);
}
/* Move to the next argument by the aligned size of the current argument */
cursorSegment = cursorSegment.asSlice(getAlignedArgSize(argLayout));
cursorSegment = cursorSegment.asSlice(argByteSize);
}
return new AixPPC64VaList(segment, session);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ private Object readArg(MemoryLayout argLayout, SegmentAllocator allocator) {
argument = argHandle.get(segment);
}
case STRUCT -> {
/* Copy the struct argument with the aligned size from the va_list buffer to allocated memory */
argument = allocator.allocate(argByteSize).copyFrom(segment.asSlice(0, argByteSize));
/* With the smaller size of the allocated struct segment and the corresponding layout,
* it ensures the struct value is copied correctly from the va_list segment to the
* returned struct argument.
*/
MemorySegment structSegment = allocator.allocate(argLayout);
long structByteSize = getSmallerStructArgSize(structSegment, argLayout);
argument = structSegment.copyFrom(segment.asSlice(0, structByteSize));
}
default -> throw new IllegalStateException("Unsupported TypeClass: " + typeClass);
}
Expand Down Expand Up @@ -155,6 +160,11 @@ private void checkNextArgument(MemoryLayout argLayout, long argByteSize) {
}
}

private static long getSmallerStructArgSize(MemorySegment structSegment, MemoryLayout structArgLayout) {
return (structSegment.byteSize() > structArgLayout.byteSize()) ?
structArgLayout.byteSize() : structSegment.byteSize();
Comment on lines +164 to +165
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something or could this just be this?

    return Math.min(structSegment.byteSize(), structArgLayout.byteSize());

Copy link
Author

@ChengJin01 ChengJin01 Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect to get the smaller size suitable for the struct to allocate, in such case Math.min() should work. I will create a PR for this minor adjustment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRs are created at #521 and ibmruntimes/openj9-openjdk-jdk19#49.

}

@Override
public void skip(MemoryLayout... layouts) {
Objects.requireNonNull(layouts);
Expand Down Expand Up @@ -257,20 +267,29 @@ public VaList build() {
MemorySegment cursorSegment = segment;

for (SimpleVaArg arg : stackArgs) {
Object argValue = arg.value;
MemoryLayout argLayout = arg.layout;
long argByteSize = getAlignedArgSize(argLayout);
TypeClass typeClass = TypeClass.classifyLayout(argLayout);

switch (typeClass) {
case PRIMITIVE, POINTER -> {
VarHandle argHandle = TypeClass.classifyVarHandle((ValueLayout)argLayout);
argHandle.set(cursorSegment, arg.value);
argHandle.set(cursorSegment, argValue);
}
case STRUCT -> {
cursorSegment.copyFrom((MemorySegment)(arg.value));
/* With the smaller size of the struct argument and the corresponding layout,
* it ensures the struct value is copied correctly from the struct argument
* to the va_list.
*/
MemorySegment structSegment = (MemorySegment)argValue;
long structByteSize = getSmallerStructArgSize(structSegment, argLayout);
cursorSegment.copyFrom(structSegment.asSlice(0, structByteSize));
}
default -> throw new IllegalStateException("Unsupported TypeClass: " + typeClass);
}
/* Move to the next argument by the aligned size of the current argument */
cursorSegment = cursorSegment.asSlice(getAlignedArgSize(argLayout));
cursorSegment = cursorSegment.asSlice(argByteSize);
}
return new SysVPPC64leVaList(segment, session);
}
Expand Down