Skip to content

Commit

Permalink
8256466: MemoryLayout factories do not guard against null layouts
Browse files Browse the repository at this point in the history
Reviewed-by: jvernee, chegar
  • Loading branch information
mcimadamore committed Nov 20, 2020
1 parent fa0bfce commit cbdce31
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 328 deletions.
Expand Up @@ -171,6 +171,8 @@ private MemoryHandles() {
* @throws NullPointerException if {@code carrier == null}, or {@code byteOrder == null}.
*/
public static VarHandle varHandle(Class<?> carrier, ByteOrder byteOrder) {
Objects.requireNonNull(carrier);
Objects.requireNonNull(byteOrder);
return varHandle(carrier,
carrierSize(carrier),
byteOrder);
Expand Down
Expand Up @@ -625,8 +625,9 @@ static MemoryLayout ofPaddingBits(long size) {
* @throws NullPointerException if {@code order == null}.
*/
static ValueLayout ofValueBits(long size, ByteOrder order) {
Objects.requireNonNull(order);
AbstractLayout.checkSize(size);
return new ValueLayout(Objects.requireNonNull(order), size);
return new ValueLayout(order, size);
}

/**
Expand Down
70 changes: 0 additions & 70 deletions test/jdk/java/foreign/TestAdaptVarHandles.java
Expand Up @@ -153,21 +153,6 @@ public void testFilterValueLoose() throws Throwable {
assertEquals(value, "42");
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadFilterNullTarget() {
MemoryHandles.filterValue(null, S2I, I2S);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadFilterNullUnbox() {
MemoryHandles.filterValue(intHandle, null, I2S);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadFilterNullBox() {
MemoryHandles.filterValue(intHandle, S2I, null);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testBadFilterCarrier() {
MemoryHandles.filterValue(floatHandle, S2I, I2S);
Expand Down Expand Up @@ -235,16 +220,6 @@ public void testFilterCoordinates() throws Throwable {
assertEquals(value, 42);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadFilterCoordinatesNullTarget() {
MemoryHandles.filterCoordinates(null, 0, S2I);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadFilterCoordinatesNullFilters() {
MemoryHandles.filterCoordinates(intHandle, 0, null);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testBadFilterCoordinatesNegativePos() {
MemoryHandles.filterCoordinates(intHandle, -1, SUM_OFFSETS);
Expand Down Expand Up @@ -288,16 +263,6 @@ public void testInsertCoordinates() throws Throwable {
assertEquals(value, 42);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadInsertCoordinatesNullTarget() {
MemoryHandles.insertCoordinates(null, 0, 42);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadInsertCoordinatesNullValues() {
MemoryHandles.insertCoordinates(intHandle, 0, null);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testBadInsertCoordinatesNegativePos() {
MemoryHandles.insertCoordinates(intHandle, -1, 42);
Expand Down Expand Up @@ -337,21 +302,6 @@ public void testPermuteCoordinates() throws Throwable {
assertEquals(value, 42);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadPermuteCoordinatesNullTarget() {
MemoryHandles.permuteCoordinates(null, List.of());
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadPermuteCoordinatesNullCoordinates() {
MemoryHandles.permuteCoordinates(intHandle, null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadPermuteCoordinatesNullReorder() {
MemoryHandles.permuteCoordinates(intHandle, List.of(int.class), null);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testBadPermuteCoordinatesTooManyCoordinates() {
MemoryHandles.permuteCoordinates(intHandle, List.of(int.class, int.class), new int[2]);
Expand Down Expand Up @@ -390,16 +340,6 @@ public void testCollectCoordinates() throws Throwable {
assertEquals(value, 42);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadCollectCoordinatesNullTarget() {
MemoryHandles.collectCoordinates(null, 0, SUM_OFFSETS);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadCollectCoordinatesNullFilters() {
MemoryHandles.collectCoordinates(intHandle, 0, null);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testBadCollectCoordinatesNegativePos() {
MemoryHandles.collectCoordinates(intHandle, -1, SUM_OFFSETS);
Expand Down Expand Up @@ -453,16 +393,6 @@ public void testBadDropCoordinatesPosTooBig() {
MemoryHandles.dropCoordinates(intHandle, 2);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadDropCoordinatesNullValueTypes() {
MemoryHandles.dropCoordinates(intHandle, 1, null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadDropCoordinatesNullTarget() {
MemoryHandles.dropCoordinates(null, 1);
}

//helper methods

static int stringToInt(String s) {
Expand Down
5 changes: 0 additions & 5 deletions test/jdk/java/foreign/TestAddressHandle.java
Expand Up @@ -85,11 +85,6 @@ public void testNull(VarHandle addrHandle, int byteSize) {
}
}

@Test(expectedExceptions = NullPointerException.class)
public void testBadAdaptNull() {
MemoryHandles.asAddressVarHandle(null);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testBadAdaptFloat() {
VarHandle floatHandle = MemoryLayouts.JAVA_FLOAT.varHandle(float.class);
Expand Down
6 changes: 0 additions & 6 deletions test/jdk/java/foreign/TestCleaner.java
Expand Up @@ -123,12 +123,6 @@ public void testBadDoubleRegister(Supplier<Cleaner> cleanerFactory, SegmentFunct
}
}

@Test(expectedExceptions = NullPointerException.class)
public void testNullCleaner() {
MemorySegment segment = MemorySegment.ofArray(new byte[10]);
segment.registerCleaner(null);
}

enum SegmentFunction implements Function<MemorySegment, MemorySegment> {
IDENTITY(Function.identity()),
CLOSE(s -> { s.close(); return s; }),
Expand Down
15 changes: 0 additions & 15 deletions test/jdk/java/foreign/TestLayoutAttributes.java
Expand Up @@ -79,19 +79,4 @@ public void testAttributesStream() {
assertTrue(attribs.contains("MyAttribute"));
assertTrue(attribs.contains(MemoryLayout.LAYOUT_NAME));
}

@Test(expectedExceptions = NullPointerException.class)
public void testWithNameNull() {
MemoryLayouts.BITS_8_BE.withName(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testFetchAttributeNull() {
MemoryLayouts.BITS_8_BE.attribute(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testPutAttributeNull() {
MemoryLayouts.BITS_8_BE.withAttribute(null, "Hello");
}
}
100 changes: 0 additions & 100 deletions test/jdk/java/foreign/TestLayouts.java
Expand Up @@ -223,106 +223,6 @@ public void testAlignmentString(MemoryLayout layout, long bitAlign) {
}
}

@Test(expectedExceptions = NullPointerException.class)
public void testValueLayoutNullOrder() {
MemoryLayout.ofValueBits(10, null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testValueLayoutWithNullOrder() {
MemoryLayout.ofValueBits(10, ByteOrder.BIG_ENDIAN).withOrder(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testUnboundSequenceLayoutNullElement() {
MemoryLayout.ofSequence(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBoundSequenceLayoutNullElement() {
MemoryLayout.ofSequence(10, null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testStructNullElements() {
MemoryLayout.ofStruct(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testUnionNullElements() {
MemoryLayout.ofUnion(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testStructWithNullElement() {
MemoryLayout.ofStruct(new MemoryLayout[] { null });
}

@Test(expectedExceptions = NullPointerException.class)
public void testUnionWithNullElement() {
MemoryLayout.ofUnion(new MemoryLayout[] { null });
}

@Test(expectedExceptions = NullPointerException.class)
public void testMapWithNullPathElement() {
MemoryLayouts.BITS_8_BE.map(UnaryOperator.identity(), new MemoryLayout.PathElement[] { null });
}

@Test(expectedExceptions = NullPointerException.class)
public void testMapWithNullElements() {
MemoryLayouts.BITS_8_BE.map(UnaryOperator.identity(), null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testMapWithNullOp() {
MemoryLayouts.BITS_8_BE.map(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testBitOffsetWithNullPathElement() {
MemoryLayouts.BITS_8_BE.bitOffset(new MemoryLayout.PathElement[] { null });
}

@Test(expectedExceptions = NullPointerException.class)
public void testBitOffsetWithNullElements() {
MemoryLayouts.BITS_8_BE.bitOffset(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testByteOffsetWithNullPathElement() {
MemoryLayouts.BITS_8_BE.byteOffset(new MemoryLayout.PathElement[] { null });
}

@Test(expectedExceptions = NullPointerException.class)
public void testByteOffsetWithNullElements() {
MemoryLayouts.BITS_8_BE.byteOffset(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testSelectWithNullPathElement() {
MemoryLayouts.BITS_8_BE.select(new MemoryLayout.PathElement[] { null });
}

@Test(expectedExceptions = NullPointerException.class)
public void testSelectWithNullElements() {
MemoryLayouts.BITS_8_BE.select(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testHandleWithNullPathElement() {
MemoryLayouts.BITS_8_BE.varHandle(int.class, new MemoryLayout.PathElement[] { null });
}

@Test(expectedExceptions = NullPointerException.class)
public void testhandleWithNullElements() {
MemoryLayouts.BITS_8_BE.varHandle(null);
}

@Test(expectedExceptions = NullPointerException.class)
public void testHandleWithNullCarrier() {
MemoryLayouts.BITS_8_BE.varHandle(null);
}

@DataProvider(name = "badLayoutSizes")
public Object[][] factoriesAndSizes() {
return new Object[][] {
Expand Down
41 changes: 0 additions & 41 deletions test/jdk/java/foreign/TestMemoryAccessStatics.java
Expand Up @@ -97,47 +97,6 @@ public void testMemoryAccess(String testName, Accessor<?> accessor) {
accessor.test();
}

@Test
public void testNulls() {
for (Method m : MemoryAccess.class.getMethods()) {
if ((m.getModifiers() & Modifier.STATIC) == 0) continue;
Object[] args = new Object[m.getParameterCount()];
for (int i = 0 ; i < args.length ; i++) {
args[i] = defaultValue(m.getParameterTypes()[i]);
}
try {
m.invoke(null, args);
fail();
} catch (InvocationTargetException ex) {
assertEquals(ex.getCause().getClass(), NullPointerException.class);
assertTrue(ex.getCause().getStackTrace()[1].getClassName().contains("MemoryAccess"));
} catch (Throwable ex) {
fail();
}
}
}

static Object defaultValue(Class<?> carrier) {
if (carrier == char.class) {
return (char)0;
} else if (carrier == byte.class) {
return (byte)0;
} else if (carrier == short.class) {
return (short)0;
} else if (carrier == int.class) {
return 0;
} else if (carrier == long.class) {
return 0L;
} else if (carrier == float.class) {
return 0f;
} else if (carrier == double.class) {
return 0d;
} else {
// reference type!
return null;
}
}

static final ByteOrder BE = ByteOrder.BIG_ENDIAN;
static final ByteOrder LE = ByteOrder.LITTLE_ENDIAN;
static final ByteOrder NE = ByteOrder.nativeOrder();
Expand Down
6 changes: 0 additions & 6 deletions test/jdk/java/foreign/TestMemoryCopy.java
Expand Up @@ -61,12 +61,6 @@ public void testCopy(SegmentSlice s1, SegmentSlice s2) {
}
}

@Test(expectedExceptions = NullPointerException.class)
public void testNull() {
var segment = MemorySegment.ofArray(new byte[4]);
segment.copyFrom(null);
}

static class SegmentSlice {

enum Kind {
Expand Down
6 changes: 0 additions & 6 deletions test/jdk/java/foreign/TestMismatch.java
Expand Up @@ -169,12 +169,6 @@ public void testInsufficientAccessModes() {
assertThrows(UOE, () -> s1WithoutRead.mismatch(s2WithoutRead));
}

@Test(expectedExceptions = NullPointerException.class)
public void testNull() {
var segment = MemorySegment.ofArray(new byte[4]);
segment.mismatch(null);
}

@Test
public void testThreadAccess() throws Exception {
var segment = MemorySegment.ofArray(new byte[4]);
Expand Down

0 comments on commit cbdce31

Please sign in to comment.