test/hotspot/jtreg/runtime/appcds/jvmti/ClassFileLoadHook.java
branchJDK-8200758-branch
changeset 57588 dac8f245de8e
parent 57587 16c4975e9e09
parent 57586 f459f98aa30d
child 57591 6805e0ef7453
equal deleted inserted replaced
57587:16c4975e9e09 57588:dac8f245de8e
     1 /*
       
     2  * Copyright (c) 2016, 2019, 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 sun.hotspot.WhiteBox;
       
    26 
       
    27 class LoadMe {
       
    28     static String getValue() {
       
    29         return "beforeHook";
       
    30     }
       
    31     static String getOtherValue() {
       
    32         return "abc-beforeHook-xyz";
       
    33     }
       
    34 }
       
    35 
       
    36 public class ClassFileLoadHook {
       
    37     public enum TestCaseId {
       
    38         SHARING_OFF_CFLH_ON,   // test case to establish a baseline
       
    39         SHARING_ON_CFLH_OFF,
       
    40         SHARING_AUTO_CFLH_ON,
       
    41         SHARING_ON_CFLH_ON
       
    42     }
       
    43 
       
    44     public static void main(String args[]) throws Exception {
       
    45         TestCaseId testCase = TestCaseId.valueOf(args[0]);
       
    46         test1(testCase);
       
    47         test2(testCase);
       
    48     }
       
    49 
       
    50     // Test rewriting the classfile data using CFLH
       
    51     static void test1(TestCaseId testCase) {
       
    52         WhiteBox wb = WhiteBox.getWhiteBox();
       
    53 
       
    54         System.out.println("====== ClassFileLoadHook.main():testCase = " + testCase);
       
    55         System.out.println("getValue():" + LoadMe.getValue());
       
    56         System.out.println("getOtherValue():" + LoadMe.getOtherValue());
       
    57 
       
    58         switch (testCase) {
       
    59             case SHARING_OFF_CFLH_ON:
       
    60                 assertTrue("after_Hook".equals(LoadMe.getValue()) &&
       
    61                            "abc-after_Hook-xyz".equals(LoadMe.getOtherValue()),
       
    62                            "Not sharing, this test should replace beforeHook " +
       
    63                            "with after_Hook");
       
    64             break;
       
    65 
       
    66             case SHARING_ON_CFLH_OFF:
       
    67                 assertTrue(wb.isSharedClass(LoadMe.class),
       
    68                     "LoadMe should be shared, but is not");
       
    69                 assertTrue("beforeHook".equals(LoadMe.getValue()) &&
       
    70                            "abc-beforeHook-xyz".equals(LoadMe.getOtherValue()),
       
    71                            "CFLH off, bug values are redefined");
       
    72             break;
       
    73 
       
    74             case SHARING_AUTO_CFLH_ON:
       
    75             case SHARING_ON_CFLH_ON:
       
    76                 // LoadMe is rewritten on CFLH
       
    77                 assertFalse(wb.isSharedClass(LoadMe.class),
       
    78                     "LoadMe should not be shared if CFLH has modified the class");
       
    79                 assertFalse("beforeHook".equals(LoadMe.getValue()) &&
       
    80                            "abc-beforeHook-xyz".equals(LoadMe.getOtherValue()),
       
    81                            "Class contents should be changed if CFLH is enabled");
       
    82              break;
       
    83 
       
    84              default:
       
    85                  throw new RuntimeException("Invalid testcase");
       
    86 
       
    87         }
       
    88     }
       
    89 
       
    90     // Test the loading of classfile data for non-boot shared classes from jrt:/xxx.
       
    91     // See JDK-8221351.
       
    92     static void test2(TestCaseId testCase) throws Exception {
       
    93         WhiteBox wb = WhiteBox.getWhiteBox();
       
    94         Class c = Class.forName("java.sql.SQLException"); // defined by platform class loader.
       
    95 
       
    96         switch (testCase) {
       
    97             case SHARING_ON_CFLH_OFF:
       
    98             case SHARING_ON_CFLH_ON:
       
    99                 assertTrue(wb.isSharedClass(c), "must be shared");
       
   100                 break;
       
   101             case SHARING_AUTO_CFLH_ON:
       
   102                 // With -Xshare:auto, the test continues to run with mapping failure.
       
   103                 // In case of mapping failure, java/lang/Object and the app class
       
   104                 // won't be loaded from the archive.
       
   105                 Class objClass = Class.forName("java.lang.Object");
       
   106                 if (wb.isSharedClass(objClass)) {
       
   107                     assertTrue(wb.isSharedClass(c), "must be shared");
       
   108                 } else {
       
   109                     assertFalse(wb.isSharedClass(c), "must not be shared");
       
   110                 }
       
   111                 break;
       
   112             default:
       
   113                 // this test is not applicable to -Xshare:off
       
   114                 if (testCase != TestCaseId.SHARING_OFF_CFLH_ON) {
       
   115                     throw new RuntimeException("Invalid testcase");
       
   116                 }
       
   117         }
       
   118     }
       
   119 
       
   120     private static void assertTrue(boolean expr, String msg) {
       
   121         if (!expr)
       
   122             throw new RuntimeException(msg);
       
   123     }
       
   124 
       
   125     private static void assertFalse(boolean expr, String msg) {
       
   126         if (expr)
       
   127             throw new RuntimeException(msg);
       
   128     }
       
   129 }