1 /*
  2  * Copyright (c) 2003, 2019, 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 #include "precompiled.hpp"
 26 #include "classfile/javaClasses.inline.hpp"
 27 #include "prims/jvmtiExport.hpp"
 28 #include "prims/jvmtiExtensions.hpp"
 29 #include "prims/jvmtiThreadState.inline.hpp"
 30 #include "runtime/interfaceSupport.inline.hpp"
 31 #include "runtime/handles.inline.hpp"
 32 #include "runtime/jniHandles.inline.hpp"
 33 
 34 // the list of extension functions
 35 GrowableArray<jvmtiExtensionFunctionInfo*>* JvmtiExtensions::_ext_functions;
 36 
 37 // the list of extension events
 38 GrowableArray<jvmtiExtensionEventInfo*>* JvmtiExtensions::_ext_events;
 39 
 40 
 41 /*
 42  * Extension Functions
 43  */
 44 static jvmtiError JNICALL IsClassUnloadingEnabled(const jvmtiEnv* env, ...) {
 45   jboolean* enabled = NULL;
 46   va_list ap;
 47 
 48   va_start(ap, env);
 49   enabled = va_arg(ap, jboolean *);
 50   va_end(ap);
 51 
 52   if (enabled == NULL) {
 53     return JVMTI_ERROR_NULL_POINTER;
 54   }
 55   *enabled = (jboolean)ClassUnloading;
 56   return JVMTI_ERROR_NONE;
 57 }
 58 
 59 // Parameters: (jthread thread, jthread* vthread_ptr)
 60 static jvmtiError JNICALL GetVirtualThread(const jvmtiEnv* env, ...) {
 61   JvmtiEnv* jvmti_env = JvmtiEnv::JvmtiEnv_from_jvmti_env((jvmtiEnv*)env);
 62   if (jvmti_env->get_capabilities()->can_support_virtual_threads == 0) {
 63     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 64   }
 65 
 66   JavaThread* current_thread = JavaThread::current();
 67   ResourceMark rm(current_thread);
 68   jthread thread = NULL;
 69   jthread* vthread_ptr = NULL;
 70   JavaThread* java_thread = NULL;
 71   oop thread_oop = NULL;
 72   va_list ap;
 73 
 74   va_start(ap, env);
 75   thread = va_arg(ap, jthread);
 76   vthread_ptr = va_arg(ap, jthread*);
 77   va_end(ap);
 78 
 79   ThreadInVMfromNative tiv(current_thread);
 80   JvmtiVTMTDisabler vtmt_disabler;
 81   ThreadsListHandle tlh(current_thread);
 82 
 83   jvmtiError err;
 84 
 85   *vthread_ptr = NULL;
 86   if (thread == NULL) {
 87     java_thread = current_thread;
 88   } else {
 89     err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);
 90     if (err != JVMTI_ERROR_NONE) {
 91       return err;
 92     }
 93   }
 94   if (vthread_ptr == NULL) {
 95     return JVMTI_ERROR_NULL_POINTER;
 96   }
 97 
 98   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
 99   if (state == NULL) {
100     return JVMTI_ERROR_THREAD_NOT_ALIVE;
101   }
102   oop vthread_oop = java_thread->vthread();
103   if (!java_lang_VirtualThread::is_instance(vthread_oop)) { // not a virtual thread
104     vthread_oop = NULL;
105   }
106   *vthread_ptr = (jthread)JNIHandles::make_local(current_thread, vthread_oop);
107   return JVMTI_ERROR_NONE;
108 }
109 
110 // Parameters: (jthread vthread, jthread* thread_ptr)
111 static jvmtiError JNICALL GetCarrierThread(const jvmtiEnv* env, ...) {
112   JvmtiEnv* jvmti_env = JvmtiEnv::JvmtiEnv_from_jvmti_env((jvmtiEnv*)env);
113   if (jvmti_env->get_capabilities()->can_support_virtual_threads == 0) {
114     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
115   }
116 
117   JavaThread* current_thread = JavaThread::current();
118   HandleMark hm(current_thread);
119   jthread vthread = NULL;
120   jthread* thread_ptr = NULL;
121   va_list ap;
122 
123   va_start(ap, env);
124   vthread = va_arg(ap, jthread);
125   thread_ptr = va_arg(ap, jthread*);
126   va_end(ap);
127 
128   ThreadInVMfromNative tiv(current_thread);
129   JvmtiVTMTDisabler vtmt_disabler;
130 
131   oop vthread_obj = JNIHandles::resolve_external_guard(vthread);
132 
133   if (!java_lang_VirtualThread::is_instance(vthread_obj)) {
134     return JVMTI_ERROR_INVALID_THREAD;
135   }
136   if (thread_ptr == NULL) {
137       return JVMTI_ERROR_NULL_POINTER;
138   }
139   VThreadGetThreadClosure op(Handle(current_thread, vthread_obj), thread_ptr);
140   Handshake::execute(&op, current_thread);
141   return op.result();
142 }
143 
144 // register extension functions and events. In this implementation we
145 // have a single extension function (to prove the API) that tests if class
146 // unloading is enabled or disabled. We also have a single extension event
147 // EXT_EVENT_CLASS_UNLOAD which is used to provide the JVMDI_EVENT_CLASS_UNLOAD
148 // event. The function and the event are registered here.
149 //
150 void JvmtiExtensions::register_extensions() {
151   _ext_functions = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiExtensionFunctionInfo*>(1, mtServiceability);
152   _ext_events = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiExtensionEventInfo*>(1, mtServiceability);
153 
154   // register our extension functions
155   static jvmtiParamInfo func_params0[] = {
156     { (char*)"IsClassUnloadingEnabled", JVMTI_KIND_OUT, JVMTI_TYPE_JBOOLEAN, JNI_FALSE }
157   };
158   static jvmtiParamInfo func_params1[] = {
159     { (char*)"GetVirtualThread", JVMTI_KIND_IN, JVMTI_TYPE_JTHREAD, JNI_FALSE },
160     { (char*)"GetVirtualThread", JVMTI_KIND_OUT, JVMTI_TYPE_JTHREAD, JNI_FALSE }
161   };
162   static jvmtiParamInfo func_params2[] = {
163     { (char*)"GetVirtualThread", JVMTI_KIND_IN, JVMTI_TYPE_JTHREAD, JNI_FALSE },
164     { (char*)"GetCarrierThread", JVMTI_KIND_OUT, JVMTI_TYPE_JTHREAD, JNI_FALSE }
165   };
166 
167   static jvmtiError errors[] = {
168     JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
169     JVMTI_ERROR_INVALID_THREAD
170   };
171 
172   static jvmtiExtensionFunctionInfo ext_func0 = {
173     (jvmtiExtensionFunction)IsClassUnloadingEnabled,
174     (char*)"com.sun.hotspot.functions.IsClassUnloadingEnabled",
175     (char*)"Tell if class unloading is enabled (-noclassgc)",
176     sizeof(func_params0)/sizeof(func_params0[0]),
177     func_params0,
178     0,              // no non-universal errors
179     NULL
180   };
181 
182   static jvmtiExtensionFunctionInfo ext_func1 = {
183     (jvmtiExtensionFunction)GetVirtualThread,
184     (char*)"com.sun.hotspot.functions.GetVirtualThread",
185     (char*)"Get virtual thread executed on carrier thread",
186     sizeof(func_params1)/sizeof(func_params1[0]),
187     func_params1,
188     sizeof(errors)/sizeof(jvmtiError),   // non-universal errors
189     errors
190   };
191 
192   static jvmtiExtensionFunctionInfo ext_func2 = {
193     (jvmtiExtensionFunction)GetCarrierThread,
194     (char*)"com.sun.hotspot.functions.GetCarrierThread",
195     (char*)"Get carrier thread executing virtual thread",
196     sizeof(func_params2)/sizeof(func_params2[0]),
197     func_params2,
198     sizeof(errors)/sizeof(jvmtiError),   // non-universal errors
199     errors
200   };
201 
202   _ext_functions->append(&ext_func0);
203   _ext_functions->append(&ext_func1);
204   _ext_functions->append(&ext_func2);
205 
206   // register our extension event
207 
208   static jvmtiParamInfo class_unload_event_params[] = {
209     { (char*)"JNI Environment", JVMTI_KIND_IN_PTR, JVMTI_TYPE_JNIENV, JNI_FALSE },
210     { (char*)"Class", JVMTI_KIND_IN_PTR, JVMTI_TYPE_CCHAR, JNI_FALSE }
211   };
212   static jvmtiParamInfo virtual_thread_event_params[] = {
213     { (char*)"JNI Environment", JVMTI_KIND_IN_PTR, JVMTI_TYPE_JNIENV, JNI_FALSE },
214     { (char*)"Virtual Thread", JVMTI_KIND_IN, JVMTI_TYPE_JTHREAD, JNI_FALSE }
215   };
216 
217   static jvmtiExtensionEventInfo class_unload_ext_event = {
218     EXT_EVENT_CLASS_UNLOAD,
219     (char*)"com.sun.hotspot.events.ClassUnload",
220     (char*)"CLASS_UNLOAD event",
221     sizeof(class_unload_event_params)/sizeof(class_unload_event_params[0]),
222     class_unload_event_params
223   };
224   static jvmtiExtensionEventInfo virtual_thread_mount_ext_event = {
225     EXT_EVENT_VIRTUAL_THREAD_MOUNT,
226     (char*)"com.sun.hotspot.events.VirtualThreadMount",
227     (char*)"VIRTUAL_THREAD_MOUNT event",
228     sizeof(virtual_thread_event_params)/sizeof(virtual_thread_event_params[0]),
229     virtual_thread_event_params
230   };
231   static jvmtiExtensionEventInfo virtual_thread_unmount_ext_event = {
232     EXT_EVENT_VIRTUAL_THREAD_UNMOUNT,
233     (char*)"com.sun.hotspot.events.VirtualThreadUnmount",
234     (char*)"VIRTUAL_THREAD_UNMOUNT event",
235     sizeof(virtual_thread_event_params)/sizeof(virtual_thread_event_params[0]),
236     virtual_thread_event_params
237   };
238 
239   _ext_events->append(&class_unload_ext_event);
240   _ext_events->append(&virtual_thread_mount_ext_event);
241   _ext_events->append(&virtual_thread_unmount_ext_event);
242 }
243 
244 
245 // return the list of extension functions
246 
247 jvmtiError JvmtiExtensions::get_functions(JvmtiEnv* env,
248                                           jint* extension_count_ptr,
249                                           jvmtiExtensionFunctionInfo** extensions)
250 {
251   guarantee(_ext_functions != NULL, "registration not done");
252 
253   ResourceTracker rt(env);
254 
255   jvmtiExtensionFunctionInfo* ext_funcs;
256   jvmtiError err = rt.allocate(_ext_functions->length() *
257                                sizeof(jvmtiExtensionFunctionInfo),
258                                (unsigned char**)&ext_funcs);
259   if (err != JVMTI_ERROR_NONE) {
260     return err;
261   }
262 
263   for (int i=0; i<_ext_functions->length(); i++ ) {
264     ext_funcs[i].func = _ext_functions->at(i)->func;
265 
266     char *id = _ext_functions->at(i)->id;
267     err = rt.allocate(strlen(id)+1, (unsigned char**)&(ext_funcs[i].id));
268     if (err != JVMTI_ERROR_NONE) {
269       return err;
270     }
271     strcpy(ext_funcs[i].id, id);
272 
273     char *desc = _ext_functions->at(i)->short_description;
274     err = rt.allocate(strlen(desc)+1,
275                       (unsigned char**)&(ext_funcs[i].short_description));
276     if (err != JVMTI_ERROR_NONE) {
277       return err;
278     }
279     strcpy(ext_funcs[i].short_description, desc);
280 
281     // params
282 
283     jint param_count = _ext_functions->at(i)->param_count;
284 
285     ext_funcs[i].param_count = param_count;
286     if (param_count == 0) {
287       ext_funcs[i].params = NULL;
288     } else {
289       err = rt.allocate(param_count*sizeof(jvmtiParamInfo),
290                         (unsigned char**)&(ext_funcs[i].params));
291       if (err != JVMTI_ERROR_NONE) {
292         return err;
293       }
294       jvmtiParamInfo* src_params = _ext_functions->at(i)->params;
295       jvmtiParamInfo* dst_params = ext_funcs[i].params;
296 
297       for (int j=0; j<param_count; j++) {
298         err = rt.allocate(strlen(src_params[j].name)+1,
299                           (unsigned char**)&(dst_params[j].name));
300         if (err != JVMTI_ERROR_NONE) {
301           return err;
302         }
303         strcpy(dst_params[j].name, src_params[j].name);
304 
305         dst_params[j].kind = src_params[j].kind;
306         dst_params[j].base_type = src_params[j].base_type;
307         dst_params[j].null_ok = src_params[j].null_ok;
308       }
309     }
310 
311     // errors
312 
313     jint error_count = _ext_functions->at(i)->error_count;
314     ext_funcs[i].error_count = error_count;
315     if (error_count == 0) {
316       ext_funcs[i].errors = NULL;
317     } else {
318       err = rt.allocate(error_count*sizeof(jvmtiError),
319                         (unsigned char**)&(ext_funcs[i].errors));
320       if (err != JVMTI_ERROR_NONE) {
321         return err;
322       }
323       memcpy(ext_funcs[i].errors, _ext_functions->at(i)->errors,
324              error_count*sizeof(jvmtiError));
325     }
326   }
327 
328   *extension_count_ptr = _ext_functions->length();
329   *extensions = ext_funcs;
330   return JVMTI_ERROR_NONE;
331 }
332 
333 
334 // return the list of extension events
335 
336 jvmtiError JvmtiExtensions::get_events(JvmtiEnv* env,
337                                        jint* extension_count_ptr,
338                                        jvmtiExtensionEventInfo** extensions)
339 {
340   guarantee(_ext_events != NULL, "registration not done");
341 
342   ResourceTracker rt(env);
343 
344   jvmtiExtensionEventInfo* ext_events;
345   jvmtiError err = rt.allocate(_ext_events->length() * sizeof(jvmtiExtensionEventInfo),
346                                (unsigned char**)&ext_events);
347   if (err != JVMTI_ERROR_NONE) {
348     return err;
349   }
350 
351   for (int i=0; i<_ext_events->length(); i++ ) {
352     ext_events[i].extension_event_index = _ext_events->at(i)->extension_event_index;
353 
354     char *id = _ext_events->at(i)->id;
355     err = rt.allocate(strlen(id)+1, (unsigned char**)&(ext_events[i].id));
356     if (err != JVMTI_ERROR_NONE) {
357       return err;
358     }
359     strcpy(ext_events[i].id, id);
360 
361     char *desc = _ext_events->at(i)->short_description;
362     err = rt.allocate(strlen(desc)+1,
363                       (unsigned char**)&(ext_events[i].short_description));
364     if (err != JVMTI_ERROR_NONE) {
365       return err;
366     }
367     strcpy(ext_events[i].short_description, desc);
368 
369     // params
370 
371     jint param_count = _ext_events->at(i)->param_count;
372 
373     ext_events[i].param_count = param_count;
374     if (param_count == 0) {
375       ext_events[i].params = NULL;
376     } else {
377       err = rt.allocate(param_count*sizeof(jvmtiParamInfo),
378                         (unsigned char**)&(ext_events[i].params));
379       if (err != JVMTI_ERROR_NONE) {
380         return err;
381       }
382       jvmtiParamInfo* src_params = _ext_events->at(i)->params;
383       jvmtiParamInfo* dst_params = ext_events[i].params;
384 
385       for (int j=0; j<param_count; j++) {
386         err = rt.allocate(strlen(src_params[j].name)+1,
387                           (unsigned char**)&(dst_params[j].name));
388         if (err != JVMTI_ERROR_NONE) {
389           return err;
390         }
391         strcpy(dst_params[j].name, src_params[j].name);
392 
393         dst_params[j].kind = src_params[j].kind;
394         dst_params[j].base_type = src_params[j].base_type;
395         dst_params[j].null_ok = src_params[j].null_ok;
396       }
397     }
398   }
399 
400   *extension_count_ptr = _ext_events->length();
401   *extensions = ext_events;
402   return JVMTI_ERROR_NONE;
403 }
404 
405 // set callback for an extension event and enable/disable it.
406 
407 jvmtiError JvmtiExtensions::set_event_callback(JvmtiEnv* env,
408                                                jint extension_event_index,
409                                                jvmtiExtensionEvent callback)
410 {
411   guarantee(_ext_events != NULL, "registration not done");
412 
413   jvmtiExtensionEventInfo* event = NULL;
414 
415   // if there are extension events registered then validate that the
416   // extension_event_index matches one of the registered events.
417   if (_ext_events != NULL) {
418     for (int i=0; i<_ext_events->length(); i++ ) {
419       if (_ext_events->at(i)->extension_event_index == extension_event_index) {
420          event = _ext_events->at(i);
421          break;
422       }
423     }
424   }
425 
426   // invalid event index
427   if (event == NULL) {
428     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
429   }
430 
431   JvmtiEventController::set_extension_event_callback(env, extension_event_index,
432                                                      callback);
433 
434   return JVMTI_ERROR_NONE;
435 }