1 /*
  2  * Copyright (c) 2020, 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  *   TestStackWalk
 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  *   TestStackWalk
 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  *   TestStackWalk
 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  *   TestStackWalk
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 import java.lang.ref.Reference;
111 
112 import jdk.test.whitebox.WhiteBox;
113 
114 import static java.lang.invoke.MethodHandles.lookup;
115 
116 public class TestStackWalk extends NativeTestHelper {
117     static final WhiteBox WB = WhiteBox.getWhiteBox();
118 
119     static final Linker linker = Linker.nativeLinker();
120 
121     static final MethodHandle MH_foo;
122     static final MethodHandle MH_m;
123 
124     static {
125         try {
126             System.loadLibrary("StackWalk");
127             MH_foo = linker.downcallHandle(
128                     findNativeOrThrow("foo"),
129                     FunctionDescriptor.ofVoid(C_POINTER));
130             MH_m = lookup().findStatic(TestStackWalk.class, "m", MethodType.methodType(void.class));
131         } catch (ReflectiveOperationException e) {
132             throw new RuntimeException(e);
133         }
134     }
135 
136     static boolean armed;
137 
138     public static void main(String[] args) throws Throwable {
139         try (Arena arena = Arena.ofConfined()) {
140             MemorySegment stub = linker.upcallStub(MH_m, FunctionDescriptor.ofVoid(), arena);
141             armed = false;
142             for (int i = 0; i < 20_000; i++) {
143                 payload(stub); // warmup
144             }
145 
146             armed = true;
147             payload(stub); // test
148         }
149     }
150 
151     static void payload(MemorySegment cb) throws Throwable {
152         MH_foo.invoke(cb);
153         Reference.reachabilityFence(cb); // keep oop alive across call
154     }
155 
156     static void m() {
157         if (armed) {
158             WB.verifyFrames(/*log=*/true, /*updateRegisterMap=*/true);
159             WB.verifyFrames(/*log=*/true, /*updateRegisterMap=*/false); // triggers different code paths
160         }
161     }
162 
163 }