1 /* 2 * Copyright (c) 2021, 2023, 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 * @test id=default_gc 26 * @bug 8277602 27 * @library /test/lib 28 * @library ../ 29 * @build jdk.test.whitebox.WhiteBox 30 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox 31 * 32 * @run main/othervm 33 * -Xbootclasspath/a:. 34 * -XX:+UnlockDiagnosticVMOptions 35 * -XX:+WhiteBoxAPI 36 * --enable-native-access=ALL-UNNAMED 37 * -Xbatch 38 * TestUpcallDeopt 39 */ 40 41 import java.lang.foreign.Arena; 42 import java.lang.foreign.Linker; 43 import java.lang.foreign.FunctionDescriptor; 44 import java.lang.foreign.MemorySegment; 45 46 import java.lang.invoke.MethodHandle; 47 import java.lang.invoke.MethodType; 48 import java.lang.ref.Reference; 49 50 import jdk.test.whitebox.WhiteBox; 51 52 import static java.lang.invoke.MethodHandles.lookup; 53 54 public class TestUpcallDeopt extends NativeTestHelper { 55 static final WhiteBox WB = WhiteBox.getWhiteBox(); 56 57 static final Linker linker = Linker.nativeLinker(); 58 59 static final MethodHandle MH_foo; 60 static final MethodHandle MH_m; 61 62 static { 63 try { 64 System.loadLibrary("UpcallDeopt"); 65 MH_foo = linker.downcallHandle( 66 findNativeOrThrow("foo"), 67 FunctionDescriptor.ofVoid(C_POINTER, C_INT, C_INT, C_INT, C_INT)); 68 MH_m = lookup().findStatic(TestUpcallDeopt.class, "m", 69 MethodType.methodType(void.class, int.class, int.class, int.class, int.class)); 70 } catch (ReflectiveOperationException e) { 71 throw new RuntimeException(e); 72 } 73 } 74 75 static boolean armed; 76 77 // we need to deoptimize through an uncommon trap in the callee of the optimized upcall stub 78 // that is created when calling upcallStub below 79 public static void main(String[] args) throws Throwable { 80 try (Arena arena = Arena.ofConfined()) { 81 MemorySegment stub = linker.upcallStub(MH_m, FunctionDescriptor.ofVoid(C_INT, C_INT, C_INT, C_INT), arena); 82 armed = false; 83 for (int i = 0; i < 20_000; i++) { 84 payload(stub); // warmup 85 } 86 87 armed = true; 88 payload(stub); // test 89 } 90 } 91 92 static void payload(MemorySegment cb) throws Throwable { 93 MH_foo.invokeExact(cb, 0, 1, 2, 3); 94 Reference.reachabilityFence(cb); // keep oop alive across call 95 } 96 97 // Takes a bunch of arguments, even though unused, to test 98 // if the caller's frame is extended enough to spill these arguments. 99 static void m(int a0, int a1, int a2, int a3) { 100 if (armed) { 101 // Trigger uncommon trap from this frame 102 WB.verifyFrames(/*log=*/true, /*updateRegisterMap=*/true); 103 WB.verifyFrames(/*log=*/true, /*updateRegisterMap=*/false); // triggers different code paths 104 } 105 } 106 107 }