Skip to content

Commit

Permalink
Add tests for nested layout size check
Browse files Browse the repository at this point in the history
  • Loading branch information
mcimadamore committed May 15, 2024
1 parent 749a6ca commit cae116d
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/jdk/java/foreign/TestLayoutPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.lang.invoke.MethodHandle;
import java.lang.invoke.VarHandle;
import java.lang.invoke.VarHandle.AccessMode;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -331,6 +332,57 @@ public void testOffsetHandleOOBIndex(MemoryLayout layout, PathElement[] pathElem
}
}

@Test(dataProvider = "testLayouts")
public void testVarHandleBadSegment(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
long expectedByteOffset) throws Throwable {
MemoryLayout seqLayout = MemoryLayout.sequenceLayout(10, layout);
PathElement[] seqPathElements = new PathElement[pathElements.length + 1];
long[] seqIndexes = new long[indexes.length + 1];
System.arraycopy(pathElements, 0, seqPathElements, 1, pathElements.length);
System.arraycopy(indexes, 0, seqIndexes, 1, indexes.length);
seqPathElements[0] = PathElement.sequenceElement();
seqIndexes[0] = 0;
MethodHandle getter_handle = seqLayout.varHandle(seqPathElements)
.toMethodHandle(AccessMode.GET)
.asSpreader(long[].class, seqIndexes.length);
MemorySegment segment = Arena.ofAuto().allocate(layout);
assertThrows(IndexOutOfBoundsException.class, () -> getter_handle.invoke(segment, 0L, seqIndexes));
}

@Test(dataProvider = "testLayouts")
public void testSliceHandleBadSegment(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
long expectedByteOffset) throws Throwable {
MemoryLayout seqLayout = MemoryLayout.sequenceLayout(10, layout);
PathElement[] seqPathElements = new PathElement[pathElements.length + 1];
long[] seqIndexes = new long[indexes.length + 1];
System.arraycopy(pathElements, 0, seqPathElements, 1, pathElements.length);
System.arraycopy(indexes, 0, seqIndexes, 1, indexes.length);
seqPathElements[0] = PathElement.sequenceElement();
seqIndexes[0] = 0;
MethodHandle getter_handle = seqLayout.sliceHandle(seqPathElements)
.asSpreader(long[].class, seqIndexes.length);
MemorySegment segment = Arena.ofAuto().allocate(layout);
assertThrows(IndexOutOfBoundsException.class, () -> getter_handle.invoke(segment, 0L, seqIndexes));
}

@Test(dataProvider = "testLayouts")
public void testArrayElementVarHandleBadSegment(MemoryLayout layout, PathElement[] pathElements, long[] indexes,
long expectedByteOffset) throws Throwable {
MemoryLayout seqLayout = MemoryLayout.sequenceLayout(10, layout);
PathElement[] seqPathElements = new PathElement[pathElements.length + 1];
long[] seqIndexes = new long[indexes.length + 2];
System.arraycopy(pathElements, 0, seqPathElements, 1, pathElements.length);
System.arraycopy(indexes, 0, seqIndexes, 2, indexes.length);
seqPathElements[0] = PathElement.sequenceElement();
seqIndexes[0] = 0;
seqIndexes[1] = 0;
MethodHandle getter_handle = seqLayout.arrayElementVarHandle(seqPathElements)
.toMethodHandle(AccessMode.GET)
.asSpreader(long[].class, seqIndexes.length);
MemorySegment segment = Arena.ofAuto().allocate(layout);
assertThrows(IndexOutOfBoundsException.class, () -> getter_handle.invoke(segment, 0L, seqIndexes));
}

@Test
public void testHashCodeCollision() {
PathElement sequenceElement = PathElement.sequenceElement();
Expand Down

0 comments on commit cae116d

Please sign in to comment.