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