1 /*
  2  * Copyright (c) 2003, 2021, 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 
 33 static jvmtiEnv *jvmti = NULL;
 34 static jvmtiEventCallbacks callbacks;
 35 static jboolean wasFramePop = JNI_FALSE;
 36 static jmethodID mid_checkPoint, mid_chain4;
 37 static jbyteArray classBytes;
 38 static frame_info expected_platform_frames[] = {
 39     {"Lgetstacktr08$TestThread;", "checkPoint", "()V"},
 40     {"Lgetstacktr08$TestThread;", "chain5", "()V"},
 41     {"Lgetstacktr08$TestThread;", "chain4", "()V"},
 42     {"Lgetstacktr08;", "nativeChain", "(Ljava/lang/Class;)V"},
 43     {"Lgetstacktr08$TestThread;", "chain3", "()V"},
 44     {"Lgetstacktr08$TestThread;", "chain2", "()V"},
 45     {"Lgetstacktr08$TestThread;", "chain1", "()V"},
 46     {"Lgetstacktr08$TestThread;", "run", "()V"},
 47     {"Ljava/lang/Thread;", "run", "()V"},
 48 };
 49 
 50 static frame_info expected_virtual_frames[] = {
 51     {"Lgetstacktr08$TestThread;", "checkPoint", "()V"},
 52     {"Lgetstacktr08$TestThread;", "chain5", "()V"},
 53     {"Lgetstacktr08$TestThread;", "chain4", "()V"},
 54     {"Lgetstacktr08;", "nativeChain", "(Ljava/lang/Class;)V"},
 55     {"Lgetstacktr08$TestThread;", "chain3", "()V"},
 56     {"Lgetstacktr08$TestThread;", "chain2", "()V"},
 57     {"Lgetstacktr08$TestThread;", "chain1", "()V"},
 58     {"Lgetstacktr08$TestThread;", "run", "()V"},
 59     {"Ljava/lang/VirtualThread;", "run", "(Ljava/lang/Runnable;)V"},
 60     {"Ljava/lang/VirtualThread$VThreadContinuation;", "lambda$new$0", "(Ljava/lang/VirtualThread;Ljava/lang/Runnable;)V"},
 61     {"Ljava/lang/VirtualThread$VThreadContinuation$$Lambda;", "run", "()V"},
 62     {"Ljdk/internal/vm/Continuation;", "enter0", "()V"},
 63     {"Ljdk/internal/vm/Continuation;", "enter", "(Ljdk/internal/vm/Continuation;Z)V"},
 64 };
 65 
 66 int compare_stack_trace(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread, int offset = 0) {
 67   frame_info *expected_frames = jni->IsVirtualThread(thread)
 68       ? expected_virtual_frames
 69       : expected_platform_frames;
 70   int expected_number_of_stack_frames = jni->IsVirtualThread(thread)
 71       ? ((int) (sizeof(expected_virtual_frames) / sizeof(frame_info)))
 72       : ((int) (sizeof(expected_platform_frames) / sizeof(frame_info)));
 73   return compare_stack_trace(jvmti, jni, thread, expected_frames, expected_number_of_stack_frames, offset);
 74 }
 75 
 76 void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *jni, jthread thread, jmethodID method, jlocation location) {
 77   if (mid_checkPoint != method) {
 78     jni->FatalError("ERROR: don't know where we get called from");
 79   }
 80   check_jvmti_status(jni, jvmti_env->ClearBreakpoint(mid_checkPoint, 0), "ClearBreakpoint failed.");
 81 
 82   if (!compare_stack_trace(jvmti_env, jni,  thread)) {
 83     jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), "Stacktrace differs from expected.");
 84     return;
 85   }
 86 
 87   set_event_notification_mode(jvmti_env, jni, JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, thread);
 88   LOG(">>> stepping ...\n");
 89 }
 90 
 91 void JNICALL SingleStep(jvmtiEnv *jvmti_env, JNIEnv *jni,
 92                         jthread thread, jmethodID method, jlocation location) {
 93   jclass klass;
 94   jvmtiClassDefinition classDef;
 95   LOG(">>> In SingleStep ...\n");
 96   print_stack_trace(jvmti_env, jni, thread);
 97 
 98   if (wasFramePop == JNI_FALSE) {
 99 
100     if (!compare_stack_trace(jvmti_env, jni, thread, 1)) {
101       // Disable single-stepping to don't cause stackoverflow
102       set_event_notification_mode(jvmti_env, jni, JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, thread);
103       jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), "Stacktrace differs from expected.");
104     }
105 
106     LOG(">>> popping frame ...\n");
107 
108     check_jvmti_status(jni, jvmti_env->PopFrame(thread), "PopFrame failed.");
109     wasFramePop = JNI_TRUE;
110   } else {
111     set_event_notification_mode(jvmti_env, jni, JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, thread);
112     if (!compare_stack_trace(jvmti_env, jni, thread, 2)) {
113       jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), "Stacktrace differs from expected.");
114     }
115 
116 
117     if (classBytes == NULL) {
118       jni->FatalError("ERROR: don't have any bytes");
119     }
120 
121     check_jvmti_status(jni, jvmti_env->GetMethodDeclaringClass(method, &klass), "GetMethodDeclaringClass failed.");
122     LOG(">>> redefining class ...\n");
123 
124     classDef.klass = klass;
125     classDef.class_byte_count = jni->GetArrayLength(classBytes);
126     classDef.class_bytes = (unsigned char *) jni->GetByteArrayElements(classBytes, NULL);
127     check_jvmti_status(jni, jvmti_env->RedefineClasses(1, &classDef), "RedefineClasses failed.");
128 
129     jni->DeleteGlobalRef(classBytes);
130     classBytes = NULL;
131     if (!compare_stack_trace(jvmti_env, jni, thread, 2)) {
132       jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), "Stacktrace differs from expected.");
133     }
134   }
135 }
136 
137 jint Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
138   jvmtiError err;
139   jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
140   if (res != JNI_OK || jvmti == NULL) {
141     LOG("Wrong result of a valid call to GetEnv!\n");
142     return JNI_ERR;
143   }
144 
145   jvmtiCapabilities caps;
146   memset(&caps, 0, sizeof(caps));
147   caps.can_generate_breakpoint_events = 1;
148   caps.can_generate_single_step_events = 1;
149   caps.can_pop_frame = 1;
150   caps.can_redefine_classes = 1;
151 
152   err = jvmti->AddCapabilities(&caps);
153   if (err != JVMTI_ERROR_NONE) {
154    LOG("(AddCapabilities) unexpected error: %s (%d)\n", TranslateError(err), err);
155     return JNI_ERR;
156   }
157 
158   callbacks.Breakpoint = &Breakpoint;
159   callbacks.SingleStep = &SingleStep;
160   err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
161   if (err != JVMTI_ERROR_NONE) {
162     LOG("(SetEventCallbacks) unexpected error: %s (%d)\n", TranslateError(err), err);
163     return JNI_ERR;
164   }
165   return JNI_OK;
166 }
167 
168 JNIEXPORT void JNICALL
169 Java_getstacktr08_getReady(JNIEnv *jni, jclass cls, jclass clazz, jbyteArray bytes) {
170   classBytes = (jbyteArray) jni->NewGlobalRef(bytes);
171   wasFramePop = JNI_FALSE;
172   mid_checkPoint = jni->GetStaticMethodID(clazz, "checkPoint", "()V");
173   mid_chain4 = jni->GetStaticMethodID(clazz, "chain4", "()V");
174 
175   check_jvmti_status(jni, jvmti->SetBreakpoint(mid_checkPoint, 0), "SetBreakpoint failed.");
176   set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL);
177 }
178 
179 JNIEXPORT void JNICALL
180 Java_getstacktr08_nativeChain(JNIEnv *jni, jclass cls, jclass clazz) {
181   if (mid_chain4 != NULL) {
182     jni->CallStaticVoidMethod(clazz, mid_chain4);
183   }
184   if (!compare_stack_trace(jvmti, jni, get_current_thread(jvmti, jni), 3)) {
185     jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), "Stacktrace differs from expected.");
186   }
187 }
188 
189 
190 }