Thu, 12 Sep 2019 15:15:22 -0400
8178870: instrumentation.retransformClasses cause coredump
Summary: Don't double-free cached class bytes on redefinition loading failure.
Reviewed-by: sspitsyn, jiangli
1 /*
2 * Copyright (c) 2017, 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 */
24 /*
25 * @test
26 * @bug 8178870
27 * @summary Redefine class with CFLH twice to test deleting the cached_class_file
28 */
30 public class RedefineDoubleDelete {
32 // Class gets a redefinition error because it adds a data member
33 public static String newB =
34 "class RedefineDoubleDelete$B {" +
35 " int count1 = 0;" +
36 "}";
38 public static String newerB =
39 "class RedefineDoubleDelete$B { " +
40 " int faa() { System.out.println(\"baa\"); return 2; }" +
41 "}";
43 // The ClassFileLoadHook for this class turns foo into faa and prints out faa.
44 static class B {
45 int faa() { System.out.println("foo"); return 1; }
46 }
48 public static void main(String args[]) throws Exception {
50 B b = new B();
51 int val = b.faa();
52 if (val != 1) {
53 throw new RuntimeException("return value wrong " + val);
54 }
56 // Redefine B twice to get cached_class_file in both B scratch classes
57 try {
58 RedefineClassHelper.redefineClass(B.class, newB);
59 } catch (java.lang.UnsupportedOperationException e) {
60 // this is expected
61 }
62 try {
63 RedefineClassHelper.redefineClass(B.class, newB);
64 } catch (java.lang.UnsupportedOperationException e) {
65 // this is expected
66 }
68 // Do a full GC.
69 System.gc();
71 // Redefine with a compatible class
72 RedefineClassHelper.redefineClass(B.class, newerB);
73 val = b.faa();
74 if (val != 2) {
75 throw new RuntimeException("return value wrong " + val);
76 }
78 // Do another full GC to clean things up.
79 System.gc();
80 }
81 }