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