test/runtime/RedefineTests/RedefineDoubleDelete.java

Thu, 12 Sep 2019 15:15:22 -0400

author
zgu
date
Thu, 12 Sep 2019 15:15:22 -0400
changeset 9748
628176d22495
child 9754
4170228e11e6
permissions
-rw-r--r--

8178870: instrumentation.retransformClasses cause coredump
Summary: Don't double-free cached class bytes on redefinition loading failure.
Reviewed-by: sspitsyn, jiangli

zgu@9748 1 /*
zgu@9748 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
zgu@9748 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
zgu@9748 4 *
zgu@9748 5 * This code is free software; you can redistribute it and/or modify it
zgu@9748 6 * under the terms of the GNU General Public License version 2 only, as
zgu@9748 7 * published by the Free Software Foundation.
zgu@9748 8 *
zgu@9748 9 * This code is distributed in the hope that it will be useful, but WITHOUT
zgu@9748 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
zgu@9748 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
zgu@9748 12 * version 2 for more details (a copy is included in the LICENSE file that
zgu@9748 13 * accompanied this code).
zgu@9748 14 *
zgu@9748 15 * You should have received a copy of the GNU General Public License version
zgu@9748 16 * 2 along with this work; if not, write to the Free Software Foundation,
zgu@9748 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
zgu@9748 18 *
zgu@9748 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
zgu@9748 20 * or visit www.oracle.com if you need additional information or have any
zgu@9748 21 * questions.
zgu@9748 22 */
zgu@9748 23
zgu@9748 24 /*
zgu@9748 25 * @test
zgu@9748 26 * @bug 8178870
zgu@9748 27 * @summary Redefine class with CFLH twice to test deleting the cached_class_file
zgu@9748 28 */
zgu@9748 29
zgu@9748 30 public class RedefineDoubleDelete {
zgu@9748 31
zgu@9748 32 // Class gets a redefinition error because it adds a data member
zgu@9748 33 public static String newB =
zgu@9748 34 "class RedefineDoubleDelete$B {" +
zgu@9748 35 " int count1 = 0;" +
zgu@9748 36 "}";
zgu@9748 37
zgu@9748 38 public static String newerB =
zgu@9748 39 "class RedefineDoubleDelete$B { " +
zgu@9748 40 " int faa() { System.out.println(\"baa\"); return 2; }" +
zgu@9748 41 "}";
zgu@9748 42
zgu@9748 43 // The ClassFileLoadHook for this class turns foo into faa and prints out faa.
zgu@9748 44 static class B {
zgu@9748 45 int faa() { System.out.println("foo"); return 1; }
zgu@9748 46 }
zgu@9748 47
zgu@9748 48 public static void main(String args[]) throws Exception {
zgu@9748 49
zgu@9748 50 B b = new B();
zgu@9748 51 int val = b.faa();
zgu@9748 52 if (val != 1) {
zgu@9748 53 throw new RuntimeException("return value wrong " + val);
zgu@9748 54 }
zgu@9748 55
zgu@9748 56 // Redefine B twice to get cached_class_file in both B scratch classes
zgu@9748 57 try {
zgu@9748 58 RedefineClassHelper.redefineClass(B.class, newB);
zgu@9748 59 } catch (java.lang.UnsupportedOperationException e) {
zgu@9748 60 // this is expected
zgu@9748 61 }
zgu@9748 62 try {
zgu@9748 63 RedefineClassHelper.redefineClass(B.class, newB);
zgu@9748 64 } catch (java.lang.UnsupportedOperationException e) {
zgu@9748 65 // this is expected
zgu@9748 66 }
zgu@9748 67
zgu@9748 68 // Do a full GC.
zgu@9748 69 System.gc();
zgu@9748 70
zgu@9748 71 // Redefine with a compatible class
zgu@9748 72 RedefineClassHelper.redefineClass(B.class, newerB);
zgu@9748 73 val = b.faa();
zgu@9748 74 if (val != 2) {
zgu@9748 75 throw new RuntimeException("return value wrong " + val);
zgu@9748 76 }
zgu@9748 77
zgu@9748 78 // Do another full GC to clean things up.
zgu@9748 79 System.gc();
zgu@9748 80 }
zgu@9748 81 }

mercurial