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.hpp"
 27 #include "jvmtifiles/jvmtiEnv.hpp"
 28 #include "logging/log.hpp"
 29 #include "prims/jvmtiExport.hpp"
 30 #include "prims/jvmtiManageCapabilities.hpp"
 31 
 32 static const jint CAPA_SIZE = (JVMTI_INTERNAL_CAPABILITY_COUNT + 7) / 8;
 33 
 34   // capabilities which are always potentially available
 35 jvmtiCapabilities JvmtiManageCapabilities::always_capabilities;
 36 
 37   // capabilities which are potentially available during OnLoad
 38 jvmtiCapabilities JvmtiManageCapabilities::onload_capabilities;
 39 
 40   // capabilities which are always potentially available
 41   // but to only one environment
 42 jvmtiCapabilities JvmtiManageCapabilities::always_solo_capabilities;
 43 
 44   // capabilities which are potentially available during OnLoad
 45   // but to only one environment
 46 jvmtiCapabilities JvmtiManageCapabilities::onload_solo_capabilities;
 47 
 48   // remaining capabilities which are always potentially available
 49   // but to only one environment
 50 jvmtiCapabilities JvmtiManageCapabilities::always_solo_remaining_capabilities;
 51 
 52   // remaining capabilities which are potentially available during OnLoad
 53   // but to only one environment
 54 jvmtiCapabilities JvmtiManageCapabilities::onload_solo_remaining_capabilities;
 55 
 56   // all capabilities ever acquired
 57 jvmtiCapabilities JvmtiManageCapabilities::acquired_capabilities;
 58 
 59 void JvmtiManageCapabilities::initialize() {
 60   always_capabilities = init_always_capabilities();
 61   onload_capabilities = init_onload_capabilities();
 62   always_solo_capabilities = init_always_solo_capabilities();
 63   onload_solo_capabilities = init_onload_solo_capabilities();
 64   always_solo_remaining_capabilities = init_always_solo_capabilities();
 65   onload_solo_remaining_capabilities = init_onload_solo_capabilities();
 66   memset(&acquired_capabilities, 0, sizeof(acquired_capabilities));
 67 }
 68 
 69 
 70 // corresponding init functions
 71 jvmtiCapabilities JvmtiManageCapabilities::init_always_capabilities() {
 72   jvmtiCapabilities jc;
 73 
 74   memset(&jc, 0, sizeof(jc));
 75   jc.can_get_bytecodes = 1;
 76   jc.can_signal_thread = 1;
 77   jc.can_get_source_file_name = 1;
 78   jc.can_get_line_numbers = 1;
 79   jc.can_get_synthetic_attribute = 1;
 80   jc.can_get_monitor_info = 1;
 81   jc.can_get_constant_pool = 1;
 82   jc.can_generate_all_class_hook_events = 1;
 83   jc.can_generate_monitor_events = 1;
 84   jc.can_generate_garbage_collection_events = 1;
 85   jc.can_generate_compiled_method_load_events = 1;
 86   jc.can_generate_native_method_bind_events = 1;
 87   jc.can_generate_vm_object_alloc_events = 1;
 88   if (os::is_thread_cpu_time_supported()) {
 89     jc.can_get_current_thread_cpu_time = 1;
 90     jc.can_get_thread_cpu_time = 1;
 91   }
 92   jc.can_redefine_classes = 1;
 93   jc.can_redefine_any_class = 1;
 94   jc.can_retransform_classes = 1;
 95   jc.can_retransform_any_class = 1;
 96   jc.can_set_native_method_prefix = 1;
 97   jc.can_tag_objects = 1;
 98   jc.can_generate_object_free_events = 1;
 99   jc.can_generate_resource_exhaustion_heap_events = 1;
100   jc.can_generate_resource_exhaustion_threads_events = 1;
101   return jc;
102 }
103 
104 jvmtiCapabilities JvmtiManageCapabilities::init_onload_capabilities() {
105   jvmtiCapabilities jc;
106 
107   memset(&jc, 0, sizeof(jc));
108 #ifndef ZERO
109   jc.can_pop_frame = 1;
110   jc.can_force_early_return = 1;
111   // Workaround for 8195635:
112   // disable pop_frame and force_early_return capabilities with Graal
113 #if INCLUDE_JVMCI
114   if (UseJVMCICompiler) {
115     jc.can_pop_frame = 0;
116     jc.can_force_early_return = 0;
117   }
118 #endif // INCLUDE_JVMCI
119 #endif // !ZERO
120   jc.can_get_source_debug_extension = 1;
121   jc.can_access_local_variables = 1;
122   jc.can_maintain_original_method_order = 1;
123   jc.can_generate_single_step_events = 1;
124   jc.can_generate_exception_events = 1;
125   jc.can_generate_frame_pop_events = 1;
126   jc.can_generate_method_entry_events = 1;
127   jc.can_generate_method_exit_events = 1;
128   jc.can_get_owned_monitor_info = 1;
129   jc.can_get_owned_monitor_stack_depth_info = 1;
130   jc.can_get_current_contended_monitor = 1;
131   jc.can_generate_early_vmstart = 1;
132   jc.can_generate_early_class_hook_events = 1;
133   jc.can_support_virtual_threads = 1;
134   return jc;
135 }
136 
137 
138 jvmtiCapabilities JvmtiManageCapabilities::init_always_solo_capabilities() {
139   jvmtiCapabilities jc;
140 
141   memset(&jc, 0, sizeof(jc));
142   jc.can_suspend = 1;
143   jc.can_generate_sampled_object_alloc_events = 1;
144   return jc;
145 }
146 
147 
148 jvmtiCapabilities JvmtiManageCapabilities::init_onload_solo_capabilities() {
149   jvmtiCapabilities jc;
150 
151   memset(&jc, 0, sizeof(jc));
152   jc.can_generate_field_modification_events = 1;
153   jc.can_generate_field_access_events = 1;
154   jc.can_generate_breakpoint_events = 1;
155   return jc;
156 }
157 
158 
159 jvmtiCapabilities *JvmtiManageCapabilities::either(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
160                                                    jvmtiCapabilities *result) {
161   char *ap = (char *)a;
162   char *bp = (char *)b;
163   char *resultp = (char *)result;
164 
165   for (int i = 0; i < CAPA_SIZE; ++i) {
166     *resultp++ = *ap++ | *bp++;
167   }
168 
169   return result;
170 }
171 
172 
173 jvmtiCapabilities *JvmtiManageCapabilities::both(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
174                                                     jvmtiCapabilities *result) {
175   char *ap = (char *)a;
176   char *bp = (char *)b;
177   char *resultp = (char *)result;
178 
179   for (int i = 0; i < CAPA_SIZE; ++i) {
180     *resultp++ = *ap++ & *bp++;
181   }
182 
183   return result;
184 }
185 
186 
187 jvmtiCapabilities *JvmtiManageCapabilities::exclude(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
188                                                     jvmtiCapabilities *result) {
189   char *ap = (char *)a;
190   char *bp = (char *)b;
191   char *resultp = (char *)result;
192 
193   for (int i = 0; i < CAPA_SIZE; ++i) {
194     *resultp++ = *ap++ & ~*bp++;
195   }
196 
197   return result;
198 }
199 
200 
201 bool JvmtiManageCapabilities::has_some(const jvmtiCapabilities *a) {
202   char *ap = (char *)a;
203 
204   for (int i = 0; i < CAPA_SIZE; ++i) {
205     if (*ap++ != 0) {
206       return true;
207     }
208   }
209 
210   return false;
211 }
212 
213 
214 void JvmtiManageCapabilities::copy_capabilities(const jvmtiCapabilities *from, jvmtiCapabilities *to) {
215   char *ap = (char *)from;
216   char *resultp = (char *)to;
217 
218   for (int i = 0; i < CAPA_SIZE; ++i) {
219     *resultp++ = *ap++;
220   }
221 }
222 
223 
224 void JvmtiManageCapabilities::get_potential_capabilities(const jvmtiCapabilities *current,
225                                                          const jvmtiCapabilities *prohibited,
226                                                          jvmtiCapabilities *result) {
227   // exclude prohibited capabilities, must be before adding current
228   exclude(&always_capabilities, prohibited, result);
229 
230   // must include current since it may possess solo capabilities and now prohibited
231   either(result, current, result);
232 
233   // add other remaining
234   either(result, &always_solo_remaining_capabilities, result);
235 
236   // if this is during OnLoad more capabilities are available
237   if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
238     either(result, &onload_capabilities, result);
239     either(result, &onload_solo_remaining_capabilities, result);
240   }
241 }
242 
243 jvmtiError JvmtiManageCapabilities::add_capabilities(const jvmtiCapabilities *current,
244                                                      const jvmtiCapabilities *prohibited,
245                                                      const jvmtiCapabilities *desired,
246                                                      jvmtiCapabilities *result) {
247   // check that the capabilities being added are potential capabilities
248   jvmtiCapabilities temp;
249   get_potential_capabilities(current, prohibited, &temp);
250   if (has_some(exclude(desired, &temp, &temp))) {
251     return JVMTI_ERROR_NOT_AVAILABLE;
252   }
253 
254   // add to the set of ever acquired capabilities
255   either(&acquired_capabilities, desired, &acquired_capabilities);
256 
257   // onload capabilities that got added are now permanent - so, also remove from onload
258   both(&onload_capabilities, desired, &temp);
259   either(&always_capabilities, &temp, &always_capabilities);
260   exclude(&onload_capabilities, &temp, &onload_capabilities);
261 
262   // same for solo capabilities (transferred capabilities in the remaining sets handled as part of standard grab - below)
263   both(&onload_solo_capabilities, desired, &temp);
264   either(&always_solo_capabilities, &temp, &always_solo_capabilities);
265   exclude(&onload_solo_capabilities, &temp, &onload_solo_capabilities);
266 
267   // remove solo capabilities that are now taken
268   exclude(&always_solo_remaining_capabilities, desired, &always_solo_remaining_capabilities);
269   exclude(&onload_solo_remaining_capabilities, desired, &onload_solo_remaining_capabilities);
270 
271   // return the result
272   either(current, desired, result);
273 
274   // special case for virtual thread events
275   // TBD: There can be a performance impact after check for can_support_virtual_threads has been removed.
276   java_lang_VirtualThread::set_notify_jvmti_events(true);
277 
278   update();
279 
280   return JVMTI_ERROR_NONE;
281 }
282 
283 
284 void JvmtiManageCapabilities::relinquish_capabilities(const jvmtiCapabilities *current,
285                                                       const jvmtiCapabilities *unwanted,
286                                                       jvmtiCapabilities *result) {
287   jvmtiCapabilities to_trash;
288   jvmtiCapabilities temp;
289 
290   // can't give up what you don't have
291   both(current, unwanted, &to_trash);
292 
293   // restore solo capabilities but only those that belong
294   either(&always_solo_remaining_capabilities, both(&always_solo_capabilities, &to_trash, &temp),
295          &always_solo_remaining_capabilities);
296   either(&onload_solo_remaining_capabilities, both(&onload_solo_capabilities, &to_trash, &temp),
297          &onload_solo_remaining_capabilities);
298 
299   update();
300 
301   // return the result
302   exclude(current, unwanted, result);
303 }
304 
305 
306 void JvmtiManageCapabilities::update() {
307   jvmtiCapabilities avail;
308 
309   // all capabilities
310   either(&always_capabilities, &always_solo_capabilities, &avail);
311 
312   bool interp_events =
313     avail.can_generate_field_access_events ||
314     avail.can_generate_field_modification_events ||
315     avail.can_generate_single_step_events ||
316     avail.can_generate_frame_pop_events ||
317     avail.can_generate_method_entry_events ||
318     avail.can_generate_method_exit_events;
319 #ifdef ZERO
320   bool enter_all_methods =
321     interp_events ||
322     avail.can_generate_breakpoint_events;
323   if (enter_all_methods) {
324     // Disable these when tracking the bytecodes
325     UseFastEmptyMethods = false;
326     UseFastAccessorMethods = false;
327   }
328 #endif // ZERO
329 
330   if (avail.can_generate_breakpoint_events
331        || avail.can_generate_field_access_events
332        || avail.can_generate_field_modification_events)
333   {
334     RewriteFrequentPairs = false;
335   }
336 
337   // If can_redefine_classes is enabled in the onload phase then we know that the
338   // dependency information recorded by the compiler is complete.
339   if ((avail.can_redefine_classes || avail.can_retransform_classes) &&
340       JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
341     JvmtiExport::set_all_dependencies_are_recorded(true);
342   }
343 
344   JvmtiExport::set_can_get_source_debug_extension(avail.can_get_source_debug_extension);
345   JvmtiExport::set_can_maintain_original_method_order(avail.can_maintain_original_method_order);
346   JvmtiExport::set_can_post_interpreter_events(interp_events);
347   JvmtiExport::set_can_hotswap_or_post_breakpoint(
348     avail.can_generate_breakpoint_events ||
349     avail.can_redefine_classes ||
350     avail.can_retransform_classes);
351   JvmtiExport::set_can_modify_any_class(
352     avail.can_generate_breakpoint_events ||
353     avail.can_generate_all_class_hook_events);
354   JvmtiExport::set_can_walk_any_space(
355     avail.can_tag_objects);   // disable sharing in onload phase
356   // This controls whether the compilers keep extra locals live to
357   // improve the debugging experience so only set them if the selected
358   // capabilities look like a debugger.
359   JvmtiExport::set_can_access_local_variables(
360     avail.can_access_local_variables ||
361     avail.can_generate_breakpoint_events ||
362     avail.can_generate_frame_pop_events);
363   JvmtiExport::set_can_post_on_exceptions(
364     avail.can_generate_exception_events ||
365     avail.can_generate_frame_pop_events ||
366     avail.can_generate_method_exit_events);
367   JvmtiExport::set_can_post_breakpoint(avail.can_generate_breakpoint_events);
368   JvmtiExport::set_can_post_field_access(avail.can_generate_field_access_events);
369   JvmtiExport::set_can_post_field_modification(avail.can_generate_field_modification_events);
370   JvmtiExport::set_can_post_method_entry(avail.can_generate_method_entry_events);
371   JvmtiExport::set_can_post_method_exit(avail.can_generate_method_exit_events ||
372                                         avail.can_generate_frame_pop_events);
373   JvmtiExport::set_can_post_frame_pop(avail.can_generate_frame_pop_events);
374   JvmtiExport::set_can_pop_frame(avail.can_pop_frame);
375   JvmtiExport::set_can_force_early_return(avail.can_force_early_return);
376   JvmtiExport::set_can_support_virtual_threads(avail.can_support_virtual_threads);
377   JvmtiExport::set_should_clean_up_heap_objects(avail.can_generate_breakpoint_events);
378   JvmtiExport::set_can_get_owned_monitor_info(avail.can_get_owned_monitor_info ||
379                                               avail.can_get_owned_monitor_stack_depth_info);
380 }
381 
382 #ifndef PRODUCT
383 
384 void JvmtiManageCapabilities:: print(const jvmtiCapabilities* cap) {
385   log_trace(jvmti)("----- capabilities -----");
386   if (cap->can_tag_objects)
387     log_trace(jvmti)("can_tag_objects");
388   if (cap->can_generate_field_modification_events)
389     log_trace(jvmti)("can_generate_field_modification_events");
390   if (cap->can_generate_field_access_events)
391     log_trace(jvmti)("can_generate_field_access_events");
392   if (cap->can_get_bytecodes)
393     log_trace(jvmti)("can_get_bytecodes");
394   if (cap->can_get_synthetic_attribute)
395     log_trace(jvmti)("can_get_synthetic_attribute");
396   if (cap->can_get_owned_monitor_info)
397     log_trace(jvmti)("can_get_owned_monitor_info");
398   if (cap->can_get_current_contended_monitor)
399     log_trace(jvmti)("can_get_current_contended_monitor");
400   if (cap->can_get_monitor_info)
401     log_trace(jvmti)("can_get_monitor_info");
402   if (cap->can_get_constant_pool)
403     log_trace(jvmti)("can_get_constant_pool");
404   if (cap->can_pop_frame)
405     log_trace(jvmti)("can_pop_frame");
406   if (cap->can_force_early_return)
407     log_trace(jvmti)("can_force_early_return");
408   if (cap->can_redefine_classes)
409     log_trace(jvmti)("can_redefine_classes");
410   if (cap->can_retransform_classes)
411     log_trace(jvmti)("can_retransform_classes");
412   if (cap->can_signal_thread)
413     log_trace(jvmti)("can_signal_thread");
414   if (cap->can_get_source_file_name)
415     log_trace(jvmti)("can_get_source_file_name");
416   if (cap->can_get_line_numbers)
417     log_trace(jvmti)("can_get_line_numbers");
418   if (cap->can_get_source_debug_extension)
419     log_trace(jvmti)("can_get_source_debug_extension");
420   if (cap->can_access_local_variables)
421     log_trace(jvmti)("can_access_local_variables");
422   if (cap->can_maintain_original_method_order)
423     log_trace(jvmti)("can_maintain_original_method_order");
424   if (cap->can_generate_single_step_events)
425     log_trace(jvmti)("can_generate_single_step_events");
426   if (cap->can_generate_exception_events)
427     log_trace(jvmti)("can_generate_exception_events");
428   if (cap->can_generate_frame_pop_events)
429     log_trace(jvmti)("can_generate_frame_pop_events");
430   if (cap->can_generate_breakpoint_events)
431     log_trace(jvmti)("can_generate_breakpoint_events");
432   if (cap->can_generate_sampled_object_alloc_events)
433     log_trace(jvmti)("can_generate_sampled_object_alloc_events");
434   if (cap->can_suspend)
435     log_trace(jvmti)("can_suspend");
436   if (cap->can_redefine_any_class )
437     log_trace(jvmti)("can_redefine_any_class");
438   if (cap->can_retransform_any_class )
439     log_trace(jvmti)("can_retransform_any_class");
440   if (cap->can_get_current_thread_cpu_time)
441     log_trace(jvmti)("can_get_current_thread_cpu_time");
442   if (cap->can_get_thread_cpu_time)
443     log_trace(jvmti)("can_get_thread_cpu_time");
444   if (cap->can_generate_method_entry_events)
445     log_trace(jvmti)("can_generate_method_entry_events");
446   if (cap->can_generate_method_exit_events)
447     log_trace(jvmti)("can_generate_method_exit_events");
448   if (cap->can_generate_all_class_hook_events)
449     log_trace(jvmti)("can_generate_all_class_hook_events");
450   if (cap->can_generate_compiled_method_load_events)
451     log_trace(jvmti)("can_generate_compiled_method_load_events");
452   if (cap->can_generate_monitor_events)
453     log_trace(jvmti)("can_generate_monitor_events");
454   if (cap->can_generate_vm_object_alloc_events)
455     log_trace(jvmti)("can_generate_vm_object_alloc_events");
456   if (cap->can_generate_native_method_bind_events)
457     log_trace(jvmti)("can_generate_native_method_bind_events");
458   if (cap->can_generate_garbage_collection_events)
459     log_trace(jvmti)("can_generate_garbage_collection_events");
460   if (cap->can_generate_object_free_events)
461     log_trace(jvmti)("can_generate_object_free_events");
462   if (cap->can_generate_resource_exhaustion_heap_events)
463     log_trace(jvmti)("can_generate_resource_exhaustion_heap_events");
464   if (cap->can_generate_resource_exhaustion_threads_events)
465     log_trace(jvmti)("can_generate_resource_exhaustion_threads_events");
466   if (cap->can_generate_early_vmstart)
467     log_trace(jvmti)("can_generate_early_vmstart");
468   if (cap->can_generate_early_class_hook_events)
469     log_trace(jvmti)("can_generate_early_class_hook_events");
470   if (cap->can_support_virtual_threads)
471     log_trace(jvmti)("can_support_virtual_threads");
472 }
473 
474 #endif