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  * @requires vm.gc != "Z"
 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  *   TestAsyncStackWalk
 39  */
 40 
 41 /*
 42  * @test id=ZSinglegen
 43  * @requires vm.gc.ZSinglegen
 44  * @library /test/lib
 45  * @library ../
 46  * @build jdk.test.whitebox.WhiteBox
 47  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 48  *
 49  * @run main/othervm
 50  *   -Xbootclasspath/a:.
 51  *   -XX:+UnlockDiagnosticVMOptions
 52  *   -XX:+WhiteBoxAPI
 53  *   --enable-native-access=ALL-UNNAMED
 54  *   -Xbatch
 55  *   -XX:+UseZGC -XX:-ZGenerational
 56  *   TestAsyncStackWalk
 57  */
 58 
 59 /*
 60  * @test id=ZGenerational
 61  * @requires vm.gc.ZGenerational
 62  * @library /test/lib
 63  * @library ../
 64  * @build jdk.test.whitebox.WhiteBox
 65  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 66  *
 67  * @run main/othervm
 68  *   -Xbootclasspath/a:.
 69  *   -XX:+UnlockDiagnosticVMOptions
 70  *   -XX:+WhiteBoxAPI
 71  *   --enable-native-access=ALL-UNNAMED
 72  *   -Xbatch
 73  *   -XX:+UseZGC -XX:+ZGenerational
 74  *   TestAsyncStackWalk
 75  */
 76 
 77 /*
 78  * @test id=shenandoah
 79  * @requires vm.gc.Shenandoah
 80  * @library /test/lib
 81  * @library ../
 82  * @build jdk.test.whitebox.WhiteBox
 83  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 84  *
 85  * @run main/othervm
 86  *   -Xbootclasspath/a:.
 87  *   -XX:+UnlockDiagnosticVMOptions
 88  *   -XX:+WhiteBoxAPI
 89  *   --enable-native-access=ALL-UNNAMED
 90  *   -Xbatch
 91  *   -XX:+UseShenandoahGC
 92  *   TestAsyncStackWalk
 93  */
 94 
 95 import java.lang.foreign.Arena;
 96 import java.lang.foreign.Linker;
 97 import java.lang.foreign.FunctionDescriptor;
 98 import java.lang.foreign.MemorySegment;
 99 
100 import java.lang.invoke.MethodHandle;
101 import java.lang.invoke.MethodType;
102 
103 import jdk.test.whitebox.WhiteBox;
104 
105 import static java.lang.invoke.MethodHandles.lookup;
106 import static jdk.test.lib.Asserts.assertTrue;
107 
108 public class TestAsyncStackWalk extends NativeTestHelper {
109     static final WhiteBox WB = WhiteBox.getWhiteBox();
110 
111     static final Linker linker = Linker.nativeLinker();
112 
113     static final MethodHandle MH_asyncStackWalk;
114     static final MethodHandle MH_m;
115 
116     static {
117         try {
118             System.loadLibrary("AsyncStackWalk");
119             MH_asyncStackWalk = linker.downcallHandle(
120                     findNativeOrThrow("asyncStackWalk"),
121                     FunctionDescriptor.ofVoid(C_POINTER));
122             MH_m = lookup().findStatic(TestAsyncStackWalk.class, "m", MethodType.methodType(void.class));
123         } catch (ReflectiveOperationException e) {
124             throw new RuntimeException(e);
125         }
126     }
127 
128     static int invocations;
129     static boolean didStackWalk;
130 
131     public static void main(String[] args) throws Throwable {
132         try (Arena arena = Arena.ofConfined()) {
133             MemorySegment stub = linker.upcallStub(MH_m, FunctionDescriptor.ofVoid(), arena);
134             invocations = 0;
135             didStackWalk = false;
136             payload(stub);
137             assertTrue(didStackWalk);
138         }
139     }
140 
141     static void payload(MemorySegment cb) throws Throwable {
142         MH_asyncStackWalk.invoke(cb);
143     }
144 
145     static void m() {
146         if (invocations++ >= 20000) { // warmup
147             didStackWalk = true;
148             WB.verifyFrames(/*log=*/true, /*updateRegisterMap=*/true);
149             WB.verifyFrames(/*log=*/true, /*updateRegisterMap=*/false); // triggers different code paths
150         }
151     }
152 
153 }