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