1 /*
 2  * Copyright (c) 2023, 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 8308762
27  * @library /test/lib
28  * @summary Test that redefinition of class containing Throwable refs does not leak constant pool
29  * @requires vm.jvmti
30  * @requires vm.flagless
31  * @modules java.base/jdk.internal.misc
32  * @modules java.instrument
33  *          java.compiler
34  * @run main RedefineClassHelper
35  * @run main/othervm/timeout=6000 -javaagent:redefineagent.jar -XX:MetaspaceSize=23m -XX:MaxMetaspaceSize=23m RedefineLeakThrowable
36  */
37 
38 
39 // MaxMetaspaceSize=23m allows InMemoryJavaCompiler to load even if CDS is off.
40 
41 class Tester {
42     void test() {
43         try {
44             int i = 42;
45         } catch (Throwable t) {
46             t.printStackTrace();
47         }
48     }
49 }
50 
51 public class RedefineLeakThrowable {
52 
53     static final String NEW_TESTER =
54         "class Tester {" +
55         "   void test() {" +
56         "        try {" +
57         "            int i = 42;" +
58         "        } catch (Throwable t) {" +
59         "            t.printStackTrace();" +
60         "        }" +
61         "    }" +
62         "}";
63 
64 
65     public static void main(String argv[]) throws Exception {
66         for (int i = 0; i < 700; i++) {
67             RedefineClassHelper.redefineClass(Tester.class, NEW_TESTER);
68         }
69     }
70 }