undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
|
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4894899
|
||||
* @summary Test various cases of passing java.nio.ByteBuffers
|
||||
* to defineClass().
|
||||
*
|
||||
* @build DefineClassByteBuffer TestClass
|
||||
* @run main DefineClassByteBuffer
|
||||
*/
|
||||
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
import java.io.*;
|
||||
|
||||
public class DefineClassByteBuffer {
|
||||
|
||||
static void test(ClassLoader cl) throws Exception {
|
||||
Class<?> c = Class.forName("TestClass", true, cl);
|
||||
if (!"TestClass".equals(c.getName())) {
|
||||
throw new RuntimeException("Got wrong class: " + c);
|
||||
}
|
||||
if (c.getClassLoader() != cl) {
|
||||
throw new RuntimeException("TestClass defined by wrong classloader: " + c.getClassLoader());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String arg[]) throws Exception {
|
||||
DummyClassLoader[] cls = new DummyClassLoader[DummyClassLoader.MAX_TYPE];
|
||||
for (int i = 0; i < cls.length; i++) {
|
||||
cls[i] = new DummyClassLoader(i);
|
||||
}
|
||||
|
||||
/* Create several instances of the class using different classloaders,
|
||||
which are using different types of ByteBuffer. */
|
||||
for (int i = 0; i < cls.length; i++) {
|
||||
test(cls[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/** Always loads the same class, using various types of ByteBuffers */
|
||||
public static class DummyClassLoader extends ClassLoader {
|
||||
|
||||
public static final String CLASS_NAME = "TestClass";
|
||||
|
||||
public static final int MAPPED_BUFFER = 0;
|
||||
public static final int DIRECT_BUFFER = 1;
|
||||
public static final int ARRAY_BUFFER = 2;
|
||||
public static final int WRAPPED_BUFFER = 3;
|
||||
public static final int READ_ONLY_ARRAY_BUFFER = 4;
|
||||
public static final int READ_ONLY_DIRECT_BUFFER = 5;
|
||||
public static final int DUP_ARRAY_BUFFER = 6;
|
||||
public static final int DUP_DIRECT_BUFFER = 7;
|
||||
public static final int MAX_TYPE = 7;
|
||||
|
||||
int loaderType;
|
||||
|
||||
DummyClassLoader(int loaderType) {
|
||||
this.loaderType = loaderType;
|
||||
}
|
||||
|
||||
static ByteBuffer[] buffers = new ByteBuffer[MAX_TYPE + 1];
|
||||
|
||||
static ByteBuffer readClassFile(String name) {
|
||||
try {
|
||||
File f = new File(System.getProperty("test.classes", "."),
|
||||
name);
|
||||
FileInputStream fin = new FileInputStream(f);
|
||||
FileChannel fc = fin.getChannel();
|
||||
return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException("Can't open file: " + name, e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Can't open file: " + name, e);
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
/* create a bunch of different ByteBuffers, starting with a mapped
|
||||
buffer from a class file, and create various duplicate and wrapped
|
||||
buffers. */
|
||||
buffers[MAPPED_BUFFER] = readClassFile(CLASS_NAME + ".class");
|
||||
byte[] array = new byte[buffers[MAPPED_BUFFER].limit()];
|
||||
buffers[MAPPED_BUFFER].get(array).flip();
|
||||
|
||||
buffers[DIRECT_BUFFER] = ByteBuffer.allocateDirect(array.length);
|
||||
buffers[DIRECT_BUFFER].put(array).flip();
|
||||
|
||||
buffers[ARRAY_BUFFER] = ByteBuffer.allocate(array.length);
|
||||
buffers[ARRAY_BUFFER].put(array).flip();
|
||||
|
||||
buffers[WRAPPED_BUFFER] = ByteBuffer.wrap(array);
|
||||
|
||||
buffers[READ_ONLY_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].asReadOnlyBuffer();
|
||||
|
||||
buffers[READ_ONLY_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].asReadOnlyBuffer();
|
||||
|
||||
buffers[DUP_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].duplicate();
|
||||
|
||||
buffers[DUP_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].duplicate();
|
||||
}
|
||||
|
||||
protected Class<?> loadClass(String name, boolean resolve)
|
||||
throws ClassNotFoundException
|
||||
{
|
||||
Class<?> c;
|
||||
if (!"TestClass".equals(name)) {
|
||||
c = super.loadClass(name, resolve);
|
||||
} else {
|
||||
// should not delegate to the system class loader
|
||||
c = findClass(name);
|
||||
if (resolve) {
|
||||
resolveClass(c);
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
protected Class<?> findClass(String name)
|
||||
throws ClassNotFoundException
|
||||
{
|
||||
if (!"TestClass".equals(name)) {
|
||||
throw new ClassNotFoundException("Unexpected class: " + name);
|
||||
}
|
||||
return defineClass(name, buffers[loaderType], null);
|
||||
}
|
||||
} /* DummyClassLoader */
|
||||
|
||||
} /* DefineClassByteBuffer */
|
||||
|
|
@ -0,0 +1,297 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Test various cases of passing java.nio.ByteBuffers to defineClass().
|
||||
* @bug 8365588
|
||||
*
|
||||
* @build DefineClassDirectByteBuffer
|
||||
* @run junit/othervm --add-opens java.base/java.lang=ALL-UNNAMED -Dmode=Direct DefineClassDirectByteBuffer
|
||||
* @run junit/othervm --add-opens java.base/java.lang=ALL-UNNAMED -Dmode=Heap DefineClassDirectByteBuffer
|
||||
*/
|
||||
|
||||
import java.lang.foreign.Arena;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.security.ProtectionDomain;
|
||||
import java.util.HexFormat;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class DefineClassDirectByteBuffer {
|
||||
// this is for the trusted/biltin classloader
|
||||
private static final String mode = System.getProperty("mode", "Direct");
|
||||
private static final int CLASSBYTES_OFFSET = 16;
|
||||
|
||||
// -------- untrusted path (custom loader) --------
|
||||
static Stream<ByteBuffer> bytebuffers() throws Exception {
|
||||
byte[] classBytes = getTestClassBytes();
|
||||
byte[] classBytesAtOffset = new byte[classBytes.length + CLASSBYTES_OFFSET];
|
||||
System.arraycopy(classBytes, 0, classBytesAtOffset, CLASSBYTES_OFFSET, classBytes.length);
|
||||
|
||||
return Stream.of(
|
||||
// WRAPPED_BUFFER
|
||||
ByteBuffer.wrap(classBytes),
|
||||
ByteBuffer.wrap(classBytes)
|
||||
.asReadOnlyBuffer(),
|
||||
ByteBuffer.wrap(classBytesAtOffset)
|
||||
.position(CLASSBYTES_OFFSET),
|
||||
ByteBuffer.wrap(classBytesAtOffset)
|
||||
.position(CLASSBYTES_OFFSET)
|
||||
.asReadOnlyBuffer(),
|
||||
// ARRAY_BUFFER
|
||||
ByteBuffer.allocate(classBytes.length)
|
||||
.put(classBytes)
|
||||
.flip(),
|
||||
ByteBuffer.allocate(classBytes.length)
|
||||
.put(classBytes)
|
||||
.flip()
|
||||
.asReadOnlyBuffer(),
|
||||
ByteBuffer.allocate(classBytesAtOffset.length)
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET),
|
||||
ByteBuffer.allocate(classBytesAtOffset.length)
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET)
|
||||
.asReadOnlyBuffer(),
|
||||
// DIRECT_BUFFER
|
||||
ByteBuffer.allocateDirect(classBytes.length)
|
||||
.put(classBytes)
|
||||
.flip(),
|
||||
ByteBuffer.allocateDirect(classBytes.length)
|
||||
.put(classBytes)
|
||||
.flip()
|
||||
.asReadOnlyBuffer(),
|
||||
ByteBuffer.allocateDirect(classBytesAtOffset.length)
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET),
|
||||
ByteBuffer.allocateDirect(classBytesAtOffset.length)
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET)
|
||||
.asReadOnlyBuffer(),
|
||||
// FOREIGN_AUTO_BUFFER
|
||||
Arena.ofAuto()
|
||||
.allocate(classBytes.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytes)
|
||||
.flip(),
|
||||
Arena.ofAuto()
|
||||
.allocate(classBytes.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytes)
|
||||
.flip()
|
||||
.asReadOnlyBuffer(),
|
||||
Arena.ofAuto()
|
||||
.allocate(classBytesAtOffset.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET),
|
||||
Arena.ofAuto()
|
||||
.allocate(classBytesAtOffset.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET)
|
||||
.asReadOnlyBuffer(),
|
||||
// FOREIGN_CONFINED_BUFFER
|
||||
Arena.ofConfined()
|
||||
.allocate(classBytes.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytes)
|
||||
.flip(),
|
||||
Arena.ofConfined()
|
||||
.allocate(classBytes.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytes)
|
||||
.flip()
|
||||
.asReadOnlyBuffer(),
|
||||
Arena.ofConfined()
|
||||
.allocate(classBytesAtOffset.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET),
|
||||
Arena.ofConfined()
|
||||
.allocate(classBytesAtOffset.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET)
|
||||
.asReadOnlyBuffer(),
|
||||
// FOREIGN_GLOBAL_BUFFER
|
||||
Arena.global()
|
||||
.allocate(classBytes.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytes)
|
||||
.flip(),
|
||||
Arena.global()
|
||||
.allocate(classBytes.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytes)
|
||||
.flip()
|
||||
.asReadOnlyBuffer(),
|
||||
Arena.global()
|
||||
.allocate(classBytesAtOffset.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET),
|
||||
Arena.global()
|
||||
.allocate(classBytesAtOffset.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET)
|
||||
.asReadOnlyBuffer(),
|
||||
// FOREIGN_SHARED_BUFFER
|
||||
Arena.ofShared()
|
||||
.allocate(classBytes.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytes)
|
||||
.flip(),
|
||||
Arena.ofShared()
|
||||
.allocate(classBytes.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytes)
|
||||
.flip()
|
||||
.asReadOnlyBuffer(),
|
||||
Arena.ofShared()
|
||||
.allocate(classBytesAtOffset.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET),
|
||||
Arena.ofShared()
|
||||
.allocate(classBytesAtOffset.length)
|
||||
.asByteBuffer()
|
||||
.put(classBytesAtOffset)
|
||||
.flip()
|
||||
.position(CLASSBYTES_OFFSET)
|
||||
.asReadOnlyBuffer(),
|
||||
// MAPPED_BUFFER: MapMode.READ_ONLY from READ fc, the bb is readonly
|
||||
getMappedByteBuffer(classBytes, 0),
|
||||
getMappedByteBuffer(classBytesAtOffset, CLASSBYTES_OFFSET)
|
||||
);
|
||||
}
|
||||
|
||||
static ByteBuffer getMappedByteBuffer(byte[] classBytes, int offset) throws Exception {
|
||||
Path tempDir = Paths.get(System.getProperty("test.classes", "."));
|
||||
Files.createDirectories(tempDir);
|
||||
Path tempClassFile = tempDir.resolve(
|
||||
String.format("DefineClassDirectByteBuffer_Greeting_%d.class", offset));
|
||||
Files.write(tempClassFile, classBytes);
|
||||
try (FileChannel fc = FileChannel.open(tempClassFile, StandardOpenOption.READ)) {
|
||||
return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).position(offset);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest()
|
||||
@MethodSource("bytebuffers")
|
||||
void testDefineClassWithCustomLoaderByteBuffer(ByteBuffer bb) throws Exception {
|
||||
int originalPos = bb.position();
|
||||
int originalLimit = bb.limit();
|
||||
CustomClassLoader loader = new CustomClassLoader();
|
||||
Class<?> clazz = loader.defineClassFromByteBuffer(bb);
|
||||
assertInvocating(clazz);
|
||||
// long-standing (and undocumented) behavior, see JDK-8352583
|
||||
if (bb.isDirect() || bb.hasArray()) {
|
||||
assertEquals(originalPos, bb.position());
|
||||
} else {
|
||||
assertEquals(originalLimit, bb.position());
|
||||
}
|
||||
}
|
||||
|
||||
// -------- trusted path (BuiltinClassLoader) --------
|
||||
@Test
|
||||
void testDefineClassWithBuiltinLoaderByteBuffer() throws Exception {
|
||||
var classBytes = getTestClassBytes();
|
||||
var builtin = ClassLoader.getPlatformClassLoader();
|
||||
var bb = mode.equals("Direct")
|
||||
? ByteBuffer.allocateDirect(classBytes.length).put(classBytes).flip()
|
||||
: ByteBuffer.allocate(classBytes.length).put(classBytes).flip();
|
||||
var originalPos = bb.position();
|
||||
// reflectively call protected defineClass(String, ByteBuffer, ProtectionDomain)
|
||||
Method m = ClassLoader.class.getDeclaredMethod(
|
||||
"defineClass", String.class, ByteBuffer.class, ProtectionDomain.class
|
||||
);
|
||||
m.setAccessible(true);
|
||||
Class<?> clazz = (Class<?>) m.invoke(builtin, null, bb, null);
|
||||
assertInvocating(clazz);
|
||||
assertEquals(originalPos, bb.position());
|
||||
}
|
||||
|
||||
// -------- shared helpers --------
|
||||
private static void assertInvocating(Class<?> clazz) throws Exception {
|
||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||
var m = clazz.getMethod("hello");
|
||||
assertEquals("Hello", m.invoke(instance));
|
||||
}
|
||||
|
||||
private static class CustomClassLoader extends ClassLoader {
|
||||
Class<?> defineClassFromByteBuffer(ByteBuffer bb) throws Exception {
|
||||
return defineClass(null, bb, null);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] getTestClassBytes() throws Exception {
|
||||
final String source = """
|
||||
public class Greeting {
|
||||
public String hello() {
|
||||
return "Hello";
|
||||
}
|
||||
}
|
||||
""";
|
||||
// (externally) compiled content of the above source, represented as hex
|
||||
final String classBytesHex = """
|
||||
cafebabe0000004600110a000200030700040c000500060100106a617661
|
||||
2f6c616e672f4f626a6563740100063c696e69743e010003282956080008
|
||||
01000548656c6c6f07000a0100084772656574696e67010004436f646501
|
||||
000f4c696e654e756d6265725461626c6501000568656c6c6f0100142829
|
||||
4c6a6176612f6c616e672f537472696e673b01000a536f7572636546696c
|
||||
6501000d4772656574696e672e6a61766100210009000200000000000200
|
||||
01000500060001000b0000001d00010001000000052ab70001b100000001
|
||||
000c000000060001000000010001000d000e0001000b0000001b00010001
|
||||
000000031207b000000001000c000000060001000000030001000f000000
|
||||
020010
|
||||
""";
|
||||
return HexFormat.of().parseHex(classBytesHex.replaceAll("\n", ""));
|
||||
}
|
||||
}
|
||||
28
test/jdk/java/lang/ClassLoader/defineClass/TestClass.java
Normal file
28
test/jdk/java/lang/ClassLoader/defineClass/TestClass.java
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
public class TestClass {
|
||||
public static String foo() {
|
||||
return "OK";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8365203
|
||||
* @summary Tests guarding of ByteBuffers in ClassLoader::defineClass
|
||||
* @run junit TestGuardByteBuffer
|
||||
*/
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.foreign.Arena;
|
||||
import java.util.HexFormat;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class TestGuardByteBuffer {
|
||||
|
||||
@Test
|
||||
void guardCrash() {
|
||||
final byte[] classBytes = getClassBytes(); // get bytes of a valid class
|
||||
final var cl = new ClassLoader() {
|
||||
void tryCrash() {
|
||||
var arena = Arena.ofConfined();
|
||||
var byteBuffer = arena.allocate(classBytes.length).asByteBuffer();
|
||||
// Close the arena underneath
|
||||
arena.close();
|
||||
// expected to always fail because the arena
|
||||
// from which the ByteBuffer was constructed
|
||||
// has been closed
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> defineClass(null, byteBuffer, null));
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < 10_000; i++) {
|
||||
cl.tryCrash();
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] getClassBytes() {
|
||||
// unused. this is here just for reference
|
||||
final String source = """
|
||||
public class NoOp {}
|
||||
""";
|
||||
// (externally) compiled content of the above "source", represented as hex
|
||||
final String classBytesHex = """
|
||||
cafebabe00000044000d0a000200030700040c000500060100106a6176612f
|
||||
6c616e672f4f626a6563740100063c696e69743e0100032829560700080100
|
||||
044e6f4f70010004436f646501000f4c696e654e756d6265725461626c6501
|
||||
000a536f7572636546696c650100094e6f4f702e6a61766100210007000200
|
||||
0000000001000100050006000100090000001d00010001000000052ab70001
|
||||
b100000001000a000000060001000000010001000b00000002000c
|
||||
""";
|
||||
|
||||
return HexFormat.of().parseHex(classBytesHex.replaceAll("\n", ""));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue