From cae116dda71d649ea201e3e88b5dd06d3c0066b8 Mon Sep 17 00:00:00 2001 From: Maurizio Cimadamore Date: Wed, 15 May 2024 16:56:56 +0100 Subject: [PATCH] Add tests for nested layout size check --- test/jdk/java/foreign/TestLayoutPaths.java | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/jdk/java/foreign/TestLayoutPaths.java b/test/jdk/java/foreign/TestLayoutPaths.java index 484e8f86ab5f9..414eb4117ced7 100644 --- a/test/jdk/java/foreign/TestLayoutPaths.java +++ b/test/jdk/java/foreign/TestLayoutPaths.java @@ -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; @@ -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();