1 /*
 2  * Copyright (c) 2003, 2022, 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 #include <stdio.h>
25 #include <string.h>
26 #include "jvmti.h"
27 #include "jvmti_common.h"
28 #include "../get_stack_trace.h"
29 
30 extern "C" {
31 
32 static jvmtiEnv *jvmti = NULL;
33 static frame_info expected_platform_frames[] = {
34     {"Ljava/lang/Object;", "wait", "()V"},
35     {"Lgetstacktr03;", "dummy", "()V"},
36     {"Lgetstacktr03;", "chain", "()V"},
37     {"Lgetstacktr03$Task;", "run", "()V"},
38     {"Ljava/lang/Thread;", "run", "()V"}
39 };
40 
41 static frame_info expected_virtual_frames[] = {
42     {"Ljava/lang/Object;", "wait", "()V"},
43     {"Lgetstacktr03;", "dummy", "()V"},
44     {"Lgetstacktr03;", "chain", "()V"},
45     {"Lgetstacktr03$Task;", "run", "()V"},
46     {"Ljava/lang/VirtualThread;", "run", "(Ljava/lang/Runnable;)V"},
47     {"Ljava/lang/VirtualThread$VThreadContinuation;", "lambda$new$0", "(Ljava/lang/VirtualThread;Ljava/lang/Runnable;)V"},
48     {"Ljava/lang/VirtualThread$VThreadContinuation$$Lambda$31.0x0000000800098810;", "run", "()V"},
49     {"Ljdk/internal/vm/Continuation;", "enter0", "()V"},
50     {"Ljdk/internal/vm/Continuation;", "enter", "(Ljdk/internal/vm/Continuation;Z)V"}
51 };
52 
53 JNIEXPORT jint JNICALL
54 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
55   jvmtiError err;
56   jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
57   if (res != JNI_OK || jvmti == NULL) {
58     LOG("Wrong result of a valid call to GetEnv!\n");
59     return JNI_ERR;
60   }
61 
62   jvmtiCapabilities caps;
63   memset(&caps, 0, sizeof(caps));
64   caps.can_suspend = 1;
65 
66   err = jvmti->AddCapabilities(&caps);
67   if (err != JVMTI_ERROR_NONE) {
68    LOG("(AddCapabilities) unexpected error: %s (%d)\n", TranslateError(err), err);
69     return JNI_ERR;
70   }
71 
72   return JNI_OK;
73 }
74 
75 JNIEXPORT void JNICALL
76 Java_getstacktr03_chain(JNIEnv *env, jclass cls) {
77   jmethodID mid = env->GetStaticMethodID(cls, "dummy", "()V");
78   env->CallStaticVoidMethod(cls, mid);
79 }
80 
81 JNIEXPORT int JNICALL
82 Java_getstacktr03_check(JNIEnv *jni, jclass cls, jthread thread) {
83   suspend_thread(jvmti, jni, thread);
84   frame_info *expected_frames = jni->IsVirtualThread(thread)
85       ? expected_virtual_frames
86       : expected_platform_frames;
87   int expected_number_of_stack_frames = jni->IsVirtualThread(thread)
88       ? ((int) (sizeof(expected_virtual_frames) / sizeof(frame_info)))
89       : ((int) (sizeof(expected_platform_frames) / sizeof(frame_info)));
90 
91   if (!compare_stack_trace(jvmti, jni, thread, expected_frames, expected_number_of_stack_frames)) {
92     jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), "Stacktrace differs from expected.");
93   }
94   resume_thread(jvmti, jni, thread);
95   return 0;
96 }
97 
98 }