Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #42 from ChengJin01/jep424_fix_valist_struct_jtreg…
Browse files Browse the repository at this point in the history
…_tests_jdk19

[Jtreg]Fix the issue with addVarg/nextVarg of VaList on Power
  • Loading branch information
tajila authored Oct 26, 2022
2 parents ac249fd + 72743a5 commit 0deb85c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
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();
}

@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

0 comments on commit 0deb85c

Please sign in to comment.