1 /*
2 * Copyright (c) 2024, 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 8336829
27 * @library /test/lib
28 * @summary Test that redefinition of a value class containing Throwable refs does not leak constant pool
29 * @enablePreview
30 * @modules java.base/jdk.internal.misc
31 * @modules java.instrument
32 * java.compiler
33 * @run main RedefineClassHelper
34 * @run main/othervm/timeout=6000 --enable-preview -Xlog:class+load,gc+metaspace+freelist+oom:rt.log -javaagent:redefineagent.jar -XX:MetaspaceSize=23m -XX:MaxMetaspaceSize=23m RedefineLeakThrowableValue
35 */
36
37 import jdk.test.lib.compiler.InMemoryJavaCompiler;
38
39 value class Tester {
40 void test() {
41 try {
42 int i = 42;
43 } catch (Throwable t) {
44 t.printStackTrace();
45 }
46 }
47 }
48
49 public class RedefineLeakThrowableValue {
50
51 static final String NEW_TESTER =
52 "value class Tester {" +
53 " void test() {" +
54 " try {" +
55 " int i = 42;" +
56 " } catch (Throwable t) {" +
57 " t.printStackTrace();" +
58 " }" +
59 " }" +
60 "}";
61
62
63 public static void main(String argv[]) throws Exception {
64 String java_version = System.getProperty("java.specification.version");
65 // Load InMemoryJavaCompiler outside the redefinition loop to prevent random lambdas that load and fill up metaspace.
66 byte[] bytecodes = InMemoryJavaCompiler.compile("Tester", NEW_TESTER,
67 "-source", java_version, "--enable-preview",
68 "--add-exports", "java.base/jdk.internal.vm.annotation=ALL-UNNAMED");
69 for (int i = 0; i < 700; i++) {
70 RedefineClassHelper.redefineClass(Tester.class, bytecodes);
71 }
72 }
73 }