test/hotspot/jtreg/runtime/appcds/cacheObject/RedefineClassApp.java
changeset 48138 78b2ecdd3c4b
child 49329 04ed29f9ef33
equal deleted inserted replaced
48137:0afc5f9eafef 48138:78b2ecdd3c4b
       
     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  *
       
    23  */
       
    24 
       
    25 import java.lang.instrument.ClassDefinition;
       
    26 import java.lang.instrument.Instrumentation;
       
    27 import java.lang.instrument.UnmodifiableClassException;
       
    28 import java.net.URL;
       
    29 import java.net.URLClassLoader;
       
    30 import java.io.File;
       
    31 import java.security.CodeSigner;
       
    32 import java.security.CodeSource;
       
    33 import java.security.ProtectionDomain;
       
    34 import sun.hotspot.WhiteBox;
       
    35 
       
    36 public class RedefineClassApp {
       
    37     static WhiteBox wb = WhiteBox.getWhiteBox();
       
    38 
       
    39     public static interface Intf {            // Loaded from Boot class loader (-Xbootclasspath/a).
       
    40         public String get();
       
    41     }
       
    42     public static class Bar implements Intf { // Loaded from Boot class loader.
       
    43         public String get() {
       
    44             return "buzz";
       
    45         }
       
    46     }
       
    47     public static class Foo implements Intf { // Loaded from AppClassLoader
       
    48         public String get() {
       
    49             return "buzz";
       
    50         }
       
    51     }
       
    52 
       
    53     static int numTests = 0;
       
    54     static int failed = 0;
       
    55     static Instrumentation instrumentation;
       
    56 
       
    57     public static void main(String args[]) throws Throwable {
       
    58         if (wb.areSharedStringsIgnored()) {
       
    59           System.out.println("Shared strings are ignored.");
       
    60           return;
       
    61         }
       
    62 
       
    63         File bootJar = new File(args[0]);
       
    64         File appJar  = new File(args[1]);
       
    65 
       
    66         instrumentation = InstrumentationRegisterClassFileTransformer.getInstrumentation();
       
    67         System.out.println("INFO: instrumentation = " + instrumentation);
       
    68 
       
    69         testBootstrapCDS("Bootstrap Loader", bootJar);
       
    70         testAppCDSv1("Application Loader", appJar);
       
    71 
       
    72         if (failed > 0) {
       
    73             throw new RuntimeException("FINAL RESULT: " + failed + " out of " + numTests + " test case(s) have failed");
       
    74         } else {
       
    75             System.out.println("FINAL RESULT: All " + numTests + " test case(s) have passed!");
       
    76         }
       
    77 
       
    78         // Full GC. The cached objects in adjustable archive heap regions are
       
    79         // scanned. The archive regions are verified. No error should be
       
    80         // reported.
       
    81         wb.fullGC();
       
    82     }
       
    83 
       
    84     static void testBootstrapCDS(String group, File jar) throws Throwable {
       
    85         doTest(group, new Bar(), jar);
       
    86     }
       
    87 
       
    88     static void testAppCDSv1(String group, File jar) throws Throwable {
       
    89         doTest(group, new Foo(), jar);
       
    90     }
       
    91 
       
    92     static void doTest(String group, Intf object, File jar) throws Throwable {
       
    93         numTests ++;
       
    94 
       
    95         Class klass = object.getClass();
       
    96         System.out.println();
       
    97         System.out.println("++++++++++++++++++++++++++");
       
    98         System.out.println("Test group: " + group);
       
    99         System.out.println("Testing with classloader = " + klass.getClassLoader());
       
   100         System.out.println("Testing with class       = " + klass);
       
   101         System.out.println("Test is shared           = " + wb.isSharedClass(klass));
       
   102         System.out.println("++++++++++++++++++++++++++");
       
   103 
       
   104         // Call get() before redefine. All strings in archived classes are shared.
       
   105         String res = object.get();
       
   106         System.out.println("get() returns " + res);
       
   107         if (res.equals("buzz") && wb.isShared(res)) {
       
   108             System.out.println("get() returns " + res + ", string is shared");
       
   109         } else {
       
   110             if (!res.equals("buzz")) {
       
   111                 System.out.println("FAILED. buzz is expected but got " + res);
       
   112             } else {
       
   113                 System.out.println("FAILED. " + res + " is not shared");
       
   114             }
       
   115             failed ++;
       
   116             return;
       
   117         }
       
   118         res = null; // release the local reference to the string
       
   119 
       
   120         // Run GC
       
   121         System.gc();
       
   122         System.gc();
       
   123         System.gc();
       
   124 
       
   125         // Redefine the shared class
       
   126         byte[] buff = Util.getClassFileFromJar(jar, klass.getName());
       
   127         Util.replace(buff, "buzz", "huzz");
       
   128         String f = "(failed)";
       
   129         try {
       
   130             instrumentation.redefineClasses(new ClassDefinition(klass, buff));
       
   131             f = object.get();
       
   132         } catch (UnmodifiableClassException|UnsupportedOperationException e) {
       
   133             e.printStackTrace();
       
   134         }
       
   135         if (f.equals("huzz")) {
       
   136             System.out.println("PASSED: object.get() after redefinition returns " + f);
       
   137         } else {
       
   138             System.out.println("FAILED: object.get() after redefinition returns " + f);
       
   139             failed ++;
       
   140         }
       
   141 
       
   142         // Run GC. Should not crash.
       
   143         System.gc();
       
   144         System.gc();
       
   145         System.gc();
       
   146 
       
   147         System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++ (done)\n\n");
       
   148     }
       
   149 }