Skip to content
Closed
Changes from 1 commit
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 @@ -141,11 +141,12 @@ static class ArrayConstants {
}

@Test
public void readAfterLastArrayElementTest() {
public void readOnePastLastArrayElementTest() {
for (ConstantValue cv : readConstants(ArrayConstants.class)) {
if (cv.boxed != null && cv.boxed.getClass().isArray()) {
JavaKind kind = metaAccess.lookupJavaType(cv.value).getComponentType().getJavaKind();
long offset = metaAccess.getArrayBaseOffset(kind) + (long) metaAccess.getArrayIndexScale(kind) * Array.getLength(cv.boxed);
Copy link
Member

@dougxc dougxc Apr 14, 2025

Choose a reason for hiding this comment

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

If I understand correctly, this tests a read of an element one past the end of the array.
Can you please also add a test for a read that is partially out-of-bounds:

long offset = 1 + metaAccess.getArrayBaseOffset(kind) + (long) metaAccess.getArrayIndexScale(kind) * (Array.getLength(cv.boxed) - 1);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a test for a long read from array[array.index - 1] because adding + 1 would make the read unaligned (which is also not allowed). Please check it out.

// read array[array.length]
assertThrows(IllegalArgumentException.class, () -> {
if (kind == JavaKind.Object) {
constantReflection.getMemoryAccessProvider().readObjectConstant(cv.value, offset);
Expand All @@ -156,4 +157,19 @@ public void readAfterLastArrayElementTest() {
}
}
}

static class IntArrayConstants {
static final int[] INT_ARRAY_CONST = new int[]{0};
}

@Test
public void readPartiallyOutOfBoundsTest() {
for (ConstantValue cv : readConstants(IntArrayConstants.class)) {
JavaKind kind = metaAccess.lookupJavaType(cv.value).getComponentType().getJavaKind();
long offset = metaAccess.getArrayBaseOffset(kind) + (long) metaAccess.getArrayIndexScale(kind) * (Array.getLength(cv.boxed) - 1);
// read a long from array[array.length - 1], which is partially out of bounds
JavaKind accessKind = JavaKind.Long;
assertThrows(IllegalArgumentException.class, () -> constantReflection.getMemoryAccessProvider().readPrimitiveConstant(accessKind, cv.value, offset, accessKind.getBitCount()));
}
}
}