|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8365203 |
| 27 | + * @summary Tests guarding of ByteBuffers in ClassLoader::defineClass |
| 28 | + * @run junit TestGuardByteBuffer |
| 29 | + */ |
| 30 | + |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +import java.lang.foreign.Arena; |
| 34 | +import java.util.HexFormat; |
| 35 | + |
| 36 | +import static org.junit.jupiter.api.Assertions.*; |
| 37 | + |
| 38 | +final class TestGuardByteBuffer { |
| 39 | + |
| 40 | + @Test |
| 41 | + void guardCrash() { |
| 42 | + final byte[] classBytes = getClassBytes(); // get bytes of a valid class |
| 43 | + final var cl = new ClassLoader() { |
| 44 | + void tryCrash() { |
| 45 | + var arena = Arena.ofConfined(); |
| 46 | + var byteBuffer = arena.allocate(classBytes.length).asByteBuffer(); |
| 47 | + // Close the arena underneath |
| 48 | + arena.close(); |
| 49 | + // expected to always fail because the arena |
| 50 | + // from which the ByteBuffer was constructed |
| 51 | + // has been closed |
| 52 | + assertThrows(IllegalStateException.class, |
| 53 | + () -> defineClass(null, byteBuffer, null)); |
| 54 | + } |
| 55 | + }; |
| 56 | + for (int i = 0; i < 10_000; i++) { |
| 57 | + cl.tryCrash(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static byte[] getClassBytes() { |
| 62 | + // unused. this is here just for reference |
| 63 | + final String source = """ |
| 64 | + public class NoOp {} |
| 65 | + """; |
| 66 | + // (externally) compiled content of the above "source", represented as hex |
| 67 | + final String classBytesHex = """ |
| 68 | + cafebabe00000044000d0a000200030700040c000500060100106a6176612f |
| 69 | + 6c616e672f4f626a6563740100063c696e69743e0100032829560700080100 |
| 70 | + 044e6f4f70010004436f646501000f4c696e654e756d6265725461626c6501 |
| 71 | + 000a536f7572636546696c650100094e6f4f702e6a61766100210007000200 |
| 72 | + 0000000001000100050006000100090000001d00010001000000052ab70001 |
| 73 | + b100000001000a000000060001000000010001000b00000002000c |
| 74 | + """; |
| 75 | + |
| 76 | + return HexFormat.of().parseHex(classBytesHex.replaceAll("\n", "")); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments