1 /*
  2  * Copyright (c) 2018, 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 <string.h>
 25 #include "jvmti.h"
 26 #include "jvmti_common.h"
 27 #include "jvmti_thread.h"
 28 
 29 #define MAX_FRAME_COUNT 80
 30 
 31 static const char CONTINUATION_CLASS_NAME[] = "jdk/internal/vm/Continuation";
 32 static const char CONTINUATION_METHOD_NAME[] = "enter";
 33 
 34 static jrawMonitorID event_mon = NULL;
 35 
 36 static void
 37 test_stack_trace(jvmtiEnv *jvmti, JNIEnv *jni, jthread vthread) {
 38   jvmtiFrameInfo frames[MAX_FRAME_COUNT];
 39   jint count = -1;
 40   jmethodID method = NULL;
 41   jvmtiError err;
 42 
 43   err = jvmti->GetStackTrace(vthread, 0, MAX_FRAME_COUNT, frames, &count);
 44   check_jvmti_status(jni, err, "Error in GetStackTrace");
 45 
 46   if (count <= 0) {
 47     LOG("Stacktrace in virtual thread is incorrect.\n");
 48     print_thread_info(jvmti, jni, vthread);
 49     print_stack_trace_frames(jvmti, jni, count, frames);
 50     LOG("Incorrect frame count %d\n", count);
 51     fatal(jni, "Incorrect frame count: count <= 0");
 52   }
 53   method = frames[count -1].method;
 54   const char* class_name = get_method_class_name(jvmti, jni, method);
 55   const char* method_name = get_method_name(jvmti, jni, method);
 56 
 57   if (strcmp(CONTINUATION_CLASS_NAME, class_name) !=0 || strcmp(CONTINUATION_METHOD_NAME, method_name) != 0) {
 58     LOG("Stacktrace in virtual thread is incorrect (doesn't start from enter(...):\n");
 59     print_stack_trace_frames(jvmti, jni, count, frames);
 60 
 61     fatal(jni, "incorrect stacktrace.");
 62   }
 63 
 64   jint frame_count = -1;
 65   check_jvmti_status(jni, jvmti->GetFrameCount(vthread, &frame_count), "Error in GetFrameCount");
 66   if (frame_count != count) {
 67     LOG("Incorrect frame count %d while %d expected\n", frame_count, count);
 68 
 69     LOG("Suspended vthread 1st stack trace:\n");
 70     print_stack_trace_frames(jvmti, jni, count, frames);
 71 
 72     LOG("Suspended vthread 2nd stack trace:\n");
 73     print_stack_trace(jvmti, jni, vthread);
 74 
 75     fatal(jni, "Incorrect frame count: frame_count != count");
 76   }
 77 }
 78 
 79 void
 80 check_link_consistency(jvmtiEnv *jvmti, JNIEnv *jni, jthread vthread) {
 81   jthread cthread = get_carrier_thread(jvmti, jni, vthread);
 82   jint vstate = get_thread_state(jvmti, jni, vthread);
 83   jint cstate = get_thread_state(jvmti, jni, cthread);
 84 
 85   if ((vstate & JVMTI_THREAD_STATE_SUSPENDED) == 0) {
 86     print_thread_info(jvmti, jni, vthread);
 87     print_stack_trace(jvmti, jni, vthread);
 88     fatal(jni, "Virtual thread IS expected to be suspended");
 89   }
 90   if ((cstate & JVMTI_THREAD_STATE_SUSPENDED) != 0) {
 91     print_thread_info(jvmti, jni, cthread);
 92     print_stack_trace(jvmti, jni, cthread);
 93     fatal(jni, "Carrier thread is NOT expected to be suspended");
 94   }
 95 
 96   if (cthread != NULL) {
 97     jthread cthread_to_vthread = get_virtual_thread(jvmti, jni, cthread);
 98 
 99     if (!jni->IsSameObject(vthread, cthread_to_vthread)) {
100       LOG("\nCarrier: ");
101       print_thread_info(jvmti, jni, cthread);
102       LOG("Expected: ");
103       print_thread_info(jvmti, jni, vthread);
104       LOG("Resulted: ");
105       print_thread_info(jvmti, jni, cthread_to_vthread);
106       fatal(jni, "GetVirtualThread(GetCarrierThread(vthread)) != vthread");
107     }
108   }
109 }
110 
111 static void
112 check_vthread_consistency_suspended(jvmtiEnv *jvmti, JNIEnv *jni, jthread vthread) {
113   jvmtiError err;
114   jboolean is_virtual = jni->IsVirtualThread(vthread);
115   //const char* name = get_thread_name(jvmti, jni, vthread);
116 
117   if (!is_virtual) {
118     jni->FatalError("Agent: check_vthread_consistency_suspended: vthread is expected to be virtual");
119   }
120   jthread cthread = get_carrier_thread(jvmti, jni, vthread);
121   //const char* cname = (cthread == NULL) ? "<no cthread>" : get_thread_name(jvmti, jni, cthread);
122 
123   err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, vthread);
124   check_jvmti_status(jni, err, "Error in JVMTI SetEventNotificationMode: enable SINGLE_STEP");
125 
126   if (cthread != NULL) { // pre-condition for reliable testing
127     test_stack_trace(jvmti, jni, vthread);
128     check_link_consistency(jvmti, jni, vthread);
129   }
130 
131   err = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, vthread);
132   check_jvmti_status(jni, err, "Error in JVMTI SetEventNotificationMode: disable SINGLE_STEP");
133 }
134 
135 #if 1
136 static int i = 0;
137 #endif
138 
139 static void JNICALL
140 SingleStep(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread,
141            jmethodID method, jlocation location) {
142   RawMonitorLocker rml(jvmti, jni, event_mon);
143 
144   LOG("Agent: Got SingleStep event:\n");
145   print_stack_trace(jvmti, jni, thread);
146 
147   jthread cthread = get_carrier_thread(jvmti, jni, thread);
148   if (cthread != NULL) {
149     print_stack_trace(jvmti, jni, cthread);
150   }
151 
152 #if 1
153   i = 1 / (*(&i)); // NOT expected Singlestep event: crash to get full stack trace
154 #endif
155   jni->FatalError("SingleStep event is NOT expected");
156 }
157 
158 /** Agent algorithm. */
159 static void JNICALL
160 agentProc(jvmtiEnv * jvmti, JNIEnv * jni, void * arg) {
161 
162   static jlong timeout = 0;
163   LOG("Agent: waiting to start\n");
164   if (!nsk_jvmti_waitForSync(timeout))
165     return;
166   if (!nsk_jvmti_resumeSync())
167     return;
168 
169   LOG("Agent: started\n");
170 
171   int iter = 0;
172   while (true) {
173     jthread *threads = NULL;
174     jint count = 0;
175     jvmtiError err;
176 
177     err = jvmti->GetAllThreads(&count, &threads);
178     if (err == JVMTI_ERROR_WRONG_PHASE) {
179       return;
180     }
181     check_jvmti_status(jni, err,  "Error in GetAllThreads");
182 
183     for (int i = 0; i < count; i++) {
184       jthread cthread = threads[i];
185       jthread vthread = NULL;
186 
187       err = GetVirtualThread(jvmti, jni, cthread, &vthread);
188       if (err == JVMTI_ERROR_THREAD_NOT_ALIVE) {
189         continue;
190       }
191       if (err == JVMTI_ERROR_WRONG_PHASE) {
192         return;
193       }
194       check_jvmti_status(jni, err,  "Error in GetVirtualThread");
195       if (iter > 50 && vthread != NULL) {
196         // char* cname = get_thread_name(jvmti, jni, cthread);
197         // char* vname = get_thread_name(jvmti, jni, vthread);
198 
199         check_jvmti_status(jni, jvmti->SuspendThread(vthread), "Error in SuspendThread");
200         // LOG("Agent: suspended vt: %s ct: %s\n", vname, cname);
201 
202         check_vthread_consistency_suspended(jvmti, jni, vthread);
203 
204         check_jvmti_status(jni, jvmti->ResumeThread(vthread), "Error in ResumeThread");
205         // LOG("Agent: resumed vt: %s ct: %s\n", vname, cname);
206       }
207     }
208     check_jvmti_status(jni, jvmti->Deallocate((unsigned char *) threads), "Error in Deallocate");
209   //check_jvmti_status(jni, jvmti->Deallocate((unsigned char *) cname), "Error in Deallocate");
210   //check_jvmti_status(jni, jvmti->Deallocate((unsigned char *) vname), "Error in Deallocate");
211 
212     iter++;
213     millisleep(20);
214   }
215   LOG("Agent: finished\n");
216 }
217 
218 
219 extern JNIEXPORT jint JNICALL
220 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
221   jvmtiEventCallbacks callbacks;
222   jvmtiCapabilities caps;
223   jvmtiError err;
224   jvmtiEnv* jvmti;
225 
226   LOG("Agent_OnLoad started\n");
227   if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {
228     return JNI_ERR;
229   }
230 
231   memset(&caps, 0, sizeof(caps));
232   caps.can_generate_single_step_events = 1;
233   caps.can_support_virtual_threads = 1;
234   caps.can_suspend = 1;
235 
236   err = jvmti->AddCapabilities(&caps);
237   if (err != JVMTI_ERROR_NONE) {
238     LOG("error in JVMTI AddCapabilities: %d\n", err);
239   }
240 
241   memset(&callbacks, 0, sizeof(callbacks));
242   callbacks.SingleStep  = &SingleStep;
243   err = jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks));
244   if (err != JVMTI_ERROR_NONE) {
245     LOG("Agent_OnLoad: Error in JVMTI SetEventCallbacks: %d\n", err);
246   }
247 
248   event_mon = create_raw_monitor(jvmti, "Events Monitor");
249 
250   err = init_agent_data(jvmti, &agent_data);
251   if (err != JVMTI_ERROR_NONE) {
252     return JNI_ERR;
253   }
254 
255   if (nsk_jvmti_setAgentProc(agentProc, NULL) != NSK_TRUE) {
256     return JNI_ERR;
257   }
258 
259   LOG("Agent_OnLoad finished\n");
260   return 0;
261 }