1 /*
  2  * Copyright Amazon.com Inc. 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 8316694
 27  * @summary Verify that nmethod relocation posts the correct JVMTI events
 28  * @requires vm.jvmti &
 29  *           vm.gc != "Epsilon" &
 30  *           vm.flavor == "server" &
 31  *           (vm.opt.TieredStopAtLevel == null | vm.opt.TieredStopAtLevel == 4) &
 32  *           vm.compMode == "Xmixed"
 33  * @library /test/lib /test/hotspot/jtreg
 34  * @build jdk.test.whitebox.WhiteBox
 35  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 36  * @run main/othervm/native -agentlib:NMethodRelocationTest
 37  *                          --enable-native-access=ALL-UNNAMED
 38  *                          -Xbootclasspath/a:.
 39  *                          -Xbatch
 40  *                          -XX:+UnlockDiagnosticVMOptions
 41  *                          -XX:+WhiteBoxAPI
 42  *                          -XX:+SegmentedCodeCache
 43  *                          -XX:-TieredCompilation
 44  *                          -XX:+UnlockExperimentalVMOptions
 45  *                          -XX:+NMethodRelocation
 46  *                          NMethodRelocationTest
 47  */
 48 
 49 import java.lang.reflect.Executable;
 50 
 51 import jdk.test.lib.Asserts;
 52 import jdk.test.lib.process.OutputAnalyzer;
 53 import jdk.test.lib.process.ProcessTools;
 54 import jdk.test.whitebox.WhiteBox;
 55 import jdk.test.whitebox.code.BlobType;
 56 import jdk.test.whitebox.code.NMethod;
 57 
 58 import static compiler.whitebox.CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION;
 59 
 60 public class NMethodRelocationTest {
 61 
 62     /** Load native library if required. */
 63     static {
 64         try {
 65             System.loadLibrary("NMethodRelocationTest");
 66         } catch (UnsatisfiedLinkError ule) {
 67             System.err.println("Could not load NMethodRelocationTest library");
 68             System.err.println("java.library.path: "
 69                 + System.getProperty("java.library.path"));
 70             throw ule;
 71         }
 72     }
 73 
 74     protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
 75 
 76     native static boolean shouldExit();
 77 
 78     public static void main(String[] argv) throws Exception {
 79         Executable method = NMethodRelocationTest.class.getDeclaredMethod("compiledMethod");
 80         WHITE_BOX.testSetDontInlineMethod(method, true);
 81 
 82         WHITE_BOX.enqueueMethodForCompilation(method, COMP_LEVEL_FULL_OPTIMIZATION);
 83 
 84         if (!WHITE_BOX.isMethodCompiled(method)) {
 85             throw new AssertionError("Method not compiled");
 86         }
 87 
 88         NMethod originalNMethod = NMethod.get(method, false);
 89         if (originalNMethod == null) {
 90             throw new AssertionError("Could not find original nmethod");
 91         }
 92 
 93         WHITE_BOX.relocateNMethodFromMethod(method, BlobType.MethodNonProfiled.id);
 94 
 95         NMethod relocatedNMethod = NMethod.get(method, false);
 96         if (relocatedNMethod == null) {
 97             throw new AssertionError("Could not find relocated nmethod");
 98         }
 99 
100         if (originalNMethod.address == relocatedNMethod.address) {
101             throw new AssertionError("Relocated nmethod same as original");
102         }
103 
104         WHITE_BOX.deoptimizeAll();
105 
106         while (!shouldExit()) {
107             WHITE_BOX.fullGC();
108         }
109     }
110 
111     public static long compiledMethod() {
112         return 0;
113     }
114 }