1 /*
2 * Copyright (c) 2012, 2024, 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 "cds.h"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/cdsConstants.hpp"
29 #include "cds/filemap.hpp"
30 #include "cds/heapShared.hpp"
31 #include "cds/metaspaceShared.hpp"
32 #include "classfile/classLoader.hpp"
33 #include "classfile/classLoaderDataGraph.hpp"
34 #include "classfile/classLoaderStats.hpp"
35 #include "classfile/classPrinter.hpp"
36 #include "classfile/javaClasses.inline.hpp"
37 #include "classfile/modules.hpp"
38 #include "classfile/protectionDomainCache.hpp"
39 #include "classfile/stringTable.hpp"
40 #include "classfile/symbolTable.hpp"
41 #include "classfile/systemDictionary.hpp"
42 #include "classfile/vmSymbols.hpp"
43 #include "code/codeCache.hpp"
44 #include "compiler/compilationPolicy.hpp"
45 #include "compiler/compilerOracle.hpp"
46 #include "compiler/directivesParser.hpp"
47 #include "compiler/methodMatcher.hpp"
48 #include "gc/shared/concurrentGCBreakpoints.hpp"
49 #include "gc/shared/gcConfig.hpp"
50 #include "gc/shared/gcLocker.inline.hpp"
51 #include "gc/shared/genArguments.hpp"
52 #include "jvmtifiles/jvmtiEnv.hpp"
53 #include "logging/log.hpp"
54 #include "memory/iterator.hpp"
55 #include "memory/metadataFactory.hpp"
56 #include "memory/metaspace/testHelpers.hpp"
57 #include "memory/metaspaceUtils.hpp"
58 #include "memory/oopFactory.hpp"
59 #include "memory/resourceArea.hpp"
60 #include "memory/universe.hpp"
61 #include "nmt/mallocSiteTable.hpp"
62 #include "nmt/memTracker.hpp"
63 #include "oops/array.hpp"
64 #include "oops/compressedOops.hpp"
65 #include "oops/constantPool.inline.hpp"
66 #include "oops/klass.inline.hpp"
67 #include "oops/method.inline.hpp"
68 #include "oops/methodData.inline.hpp"
69 #include "oops/objArrayKlass.hpp"
70 #include "oops/objArrayOop.inline.hpp"
71 #include "oops/oop.inline.hpp"
72 #include "oops/typeArrayOop.inline.hpp"
73 #include "prims/jvmtiEnvBase.hpp"
74 #include "prims/resolvedMethodTable.hpp"
75 #include "prims/wbtestmethods/parserTests.hpp"
76 #include "prims/whitebox.inline.hpp"
77 #include "runtime/arguments.hpp"
78 #include "runtime/atomic.hpp"
79 #include "runtime/deoptimization.hpp"
80 #include "runtime/fieldDescriptor.inline.hpp"
81 #include "runtime/flags/jvmFlag.hpp"
82 #include "runtime/flags/jvmFlagAccess.hpp"
83 #include "runtime/frame.inline.hpp"
84 #include "runtime/handles.inline.hpp"
85 #include "runtime/handshake.hpp"
86 #include "runtime/interfaceSupport.inline.hpp"
87 #include "runtime/javaCalls.hpp"
88 #include "runtime/javaThread.inline.hpp"
89 #include "runtime/jniHandles.inline.hpp"
90 #include "runtime/lockStack.hpp"
91 #include "runtime/os.hpp"
92 #include "runtime/stackFrameStream.inline.hpp"
93 #include "runtime/synchronizer.hpp"
94 #include "runtime/threadSMR.hpp"
95 #include "runtime/vframe.hpp"
96 #include "runtime/vm_version.hpp"
97 #include "services/memoryService.hpp"
98 #include "utilities/align.hpp"
99 #include "utilities/checkedCast.hpp"
100 #include "utilities/debug.hpp"
101 #include "utilities/elfFile.hpp"
102 #include "utilities/exceptions.hpp"
103 #include "utilities/macros.hpp"
104 #include "utilities/nativeCallStack.hpp"
105 #include "utilities/ostream.hpp"
106 #if INCLUDE_G1GC
107 #include "gc/g1/g1Arguments.hpp"
108 #include "gc/g1/g1CollectedHeap.inline.hpp"
109 #include "gc/g1/g1ConcurrentMark.hpp"
110 #include "gc/g1/g1ConcurrentMarkThread.inline.hpp"
111 #include "gc/g1/g1HeapRegionManager.hpp"
112 #include "gc/g1/g1HeapRegionRemSet.inline.hpp"
113 #endif // INCLUDE_G1GC
114 #if INCLUDE_PARALLELGC
115 #include "gc/parallel/parallelScavengeHeap.inline.hpp"
116 #endif // INCLUDE_PARALLELGC
117 #if INCLUDE_SERIALGC
118 #include "gc/serial/serialHeap.hpp"
119 #endif // INCLUDE_SERIALGC
120 #if INCLUDE_ZGC
121 #include "gc/z/zAddress.inline.hpp"
122 #endif // INCLUDE_ZGC
123 #if INCLUDE_JVMCI
124 #include "jvmci/jvmciEnv.hpp"
125 #include "jvmci/jvmciRuntime.hpp"
126 #endif
127 #ifdef LINUX
128 #include "cgroupSubsystem_linux.hpp"
129 #include "osContainer_linux.hpp"
130 #include "os_linux.hpp"
131 #endif
132
133 #define CHECK_JNI_EXCEPTION_(env, value) \
134 do { \
135 JavaThread* THREAD = JavaThread::thread_from_jni_environment(env); \
136 THREAD->clear_pending_jni_exception_check(); \
137 if (HAS_PENDING_EXCEPTION) { \
138 return(value); \
139 } \
140 } while (0)
141
142 #define CHECK_JNI_EXCEPTION(env) \
143 do { \
144 JavaThread* THREAD = JavaThread::thread_from_jni_environment(env); \
145 THREAD->clear_pending_jni_exception_check(); \
146 if (HAS_PENDING_EXCEPTION) { \
147 return; \
148 } \
149 } while (0)
150
151 bool WhiteBox::_used = false;
152 volatile bool WhiteBox::compilation_locked = false;
153
154 class VM_WhiteBoxOperation : public VM_Operation {
155 public:
156 VM_WhiteBoxOperation() { }
157 VMOp_Type type() const { return VMOp_WhiteBoxOperation; }
158 bool allow_nested_vm_operations() const { return true; }
159 };
160
161
162 WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
163 return (jlong)(void*)JNIHandles::resolve(obj);
164 WB_END
165
166 WB_ENTRY(jint, WB_GetHeapOopSize(JNIEnv* env, jobject o))
167 return heapOopSize;
168 WB_END
169
170 WB_ENTRY(jint, WB_GetVMPageSize(JNIEnv* env, jobject o))
171 return (jint)os::vm_page_size();
172 WB_END
173
174 WB_ENTRY(jlong, WB_GetVMAllocationGranularity(JNIEnv* env, jobject o))
175 return os::vm_allocation_granularity();
176 WB_END
177
178 WB_ENTRY(jlong, WB_GetVMLargePageSize(JNIEnv* env, jobject o))
179 return os::large_page_size();
180 WB_END
181
182 WB_ENTRY(jstring, WB_PrintString(JNIEnv* env, jobject wb, jstring str, jint max_length))
183 ResourceMark rm(THREAD);
184 stringStream sb;
185 java_lang_String::print(JNIHandles::resolve(str), &sb, max_length);
186 oop result = java_lang_String::create_oop_from_str(sb.as_string(), THREAD);
187 return (jstring) JNIHandles::make_local(THREAD, result);
188 WB_END
189
190 class WBIsKlassAliveClosure : public LockedClassesDo {
191 Symbol* _name;
192 int _count;
193 public:
194 WBIsKlassAliveClosure(Symbol* name) : _name(name), _count(0) {}
195
196 void do_klass(Klass* k) {
197 Symbol* ksym = k->name();
198 if (ksym->fast_compare(_name) == 0) {
199 _count++;
200 } else if (k->is_instance_klass()) {
201 // Need special handling for hidden classes because the JVM
202 // appends "+<hex-address>" to hidden class names.
203 InstanceKlass *ik = InstanceKlass::cast(k);
204 if (ik->is_hidden()) {
205 ResourceMark rm;
206 char* k_name = ksym->as_C_string();
207 // Find the first '+' char and truncate the string at that point.
208 // NOTE: This will not work correctly if the original hidden class
209 // name contains a '+'.
210 char* plus_char = strchr(k_name, '+');
211 if (plus_char != nullptr) {
212 *plus_char = 0;
213 char* c_name = _name->as_C_string();
214 if (strcmp(c_name, k_name) == 0) {
215 _count++;
216 }
217 }
218 }
219 }
220 }
221
222 int count() const {
223 return _count;
224 }
225 };
226
227 WB_ENTRY(jint, WB_CountAliveClasses(JNIEnv* env, jobject target, jstring name))
228 oop h_name = JNIHandles::resolve(name);
229 if (h_name == nullptr) {
230 return 0;
231 }
232 Symbol* sym = java_lang_String::as_symbol(h_name);
233 TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
234
235 WBIsKlassAliveClosure closure(sym);
236 ClassLoaderDataGraph::classes_do(&closure);
237
238 // Return the count of alive classes with this name.
239 return closure.count();
240 WB_END
241
242 WB_ENTRY(jint, WB_GetSymbolRefcount(JNIEnv* env, jobject unused, jstring name))
243 oop h_name = JNIHandles::resolve(name);
244 if (h_name == nullptr) {
245 return 0;
246 }
247 Symbol* sym = java_lang_String::as_symbol(h_name);
248 TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
249 return (jint)sym->refcount();
250 WB_END
251
252
253 WB_ENTRY(void, WB_AddToBootstrapClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) {
254 #if INCLUDE_JVMTI
255 ResourceMark rm;
256 const char* seg = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(segment));
257 JvmtiEnv* jvmti_env = JvmtiEnv::create_a_jvmti(JVMTI_VERSION);
258 jvmtiError err = jvmti_env->AddToBootstrapClassLoaderSearch(seg);
259 assert(err == JVMTI_ERROR_NONE, "must not fail");
260 #endif
261 }
262 WB_END
263
264 WB_ENTRY(void, WB_AddToSystemClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) {
265 #if INCLUDE_JVMTI
266 ResourceMark rm;
267 const char* seg = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(segment));
268 JvmtiEnv* jvmti_env = JvmtiEnv::create_a_jvmti(JVMTI_VERSION);
269 jvmtiError err = jvmti_env->AddToSystemClassLoaderSearch(seg);
270 assert(err == JVMTI_ERROR_NONE, "must not fail");
271 #endif
272 }
273 WB_END
274
275
276 WB_ENTRY(jlong, WB_GetCompressedOopsMaxHeapSize(JNIEnv* env, jobject o)) {
277 return (jlong)Arguments::max_heap_for_compressed_oops();
278 }
279 WB_END
280
281 WB_ENTRY(void, WB_PrintHeapSizes(JNIEnv* env, jobject o)) {
282 tty->print_cr("Minimum heap " SIZE_FORMAT " Initial heap " SIZE_FORMAT " "
283 "Maximum heap " SIZE_FORMAT " Space alignment " SIZE_FORMAT " Heap alignment " SIZE_FORMAT,
284 MinHeapSize,
285 InitialHeapSize,
286 MaxHeapSize,
287 SpaceAlignment,
288 HeapAlignment);
289 }
290 WB_END
291
292 WB_ENTRY(void, WB_ReadFromNoaccessArea(JNIEnv* env, jobject o))
293 size_t granularity = os::vm_allocation_granularity();
294 ReservedHeapSpace rhs(100 * granularity, granularity, os::vm_page_size());
295 VirtualSpace vs;
296 vs.initialize(rhs, 50 * granularity);
297
298 // Check if constraints are complied
299 if (!( UseCompressedOops && rhs.base() != nullptr &&
300 CompressedOops::base() != nullptr &&
301 CompressedOops::use_implicit_null_checks() )) {
302 tty->print_cr("WB_ReadFromNoaccessArea method is useless:\n "
303 "\tUseCompressedOops is %d\n"
304 "\trhs.base() is " PTR_FORMAT "\n"
305 "\tCompressedOops::base() is " PTR_FORMAT "\n"
306 "\tCompressedOops::use_implicit_null_checks() is %d",
307 UseCompressedOops,
308 p2i(rhs.base()),
309 p2i(CompressedOops::base()),
310 CompressedOops::use_implicit_null_checks());
311 return;
312 }
313 tty->print_cr("Reading from no access area... ");
314 tty->print_cr("*(vs.low_boundary() - rhs.noaccess_prefix() / 2 ) = %c",
315 *(vs.low_boundary() - rhs.noaccess_prefix() / 2 ));
316 WB_END
317
318 static jint wb_stress_virtual_space_resize(size_t reserved_space_size,
319 size_t magnitude, size_t iterations) {
320 size_t granularity = os::vm_allocation_granularity();
321 ReservedHeapSpace rhs(reserved_space_size * granularity, granularity, os::vm_page_size());
322 VirtualSpace vs;
323 if (!vs.initialize(rhs, 0)) {
324 tty->print_cr("Failed to initialize VirtualSpace. Can't proceed.");
325 return 3;
326 }
327
328 int seed = os::random();
329 tty->print_cr("Random seed is %d", seed);
330
331 for (size_t i = 0; i < iterations; i++) {
332
333 // Whether we will shrink or grow
334 bool shrink = os::random() % 2L == 0;
335
336 // Get random delta to resize virtual space
337 size_t delta = (size_t)os::random() % magnitude;
338
339 // If we are about to shrink virtual space below zero, then expand instead
340 if (shrink && vs.committed_size() < delta) {
341 shrink = false;
342 }
343
344 // Resizing by delta
345 if (shrink) {
346 vs.shrink_by(delta);
347 } else {
348 // If expanding fails expand_by will silently return false
349 vs.expand_by(delta, true);
350 }
351 }
352 return 0;
353 }
354
355 WB_ENTRY(jint, WB_StressVirtualSpaceResize(JNIEnv* env, jobject o,
356 jlong reserved_space_size, jlong magnitude, jlong iterations))
357 tty->print_cr("reservedSpaceSize=" JLONG_FORMAT ", magnitude=" JLONG_FORMAT ", "
358 "iterations=" JLONG_FORMAT "\n", reserved_space_size, magnitude,
359 iterations);
360 if (reserved_space_size < 0 || magnitude < 0 || iterations < 0) {
361 tty->print_cr("One of variables printed above is negative. Can't proceed.\n");
362 return 1;
363 }
364
365 // sizeof(size_t) depends on whether OS is 32bit or 64bit. sizeof(jlong) is
366 // always 8 byte. That's why we should avoid overflow in case of 32bit platform.
367 if (sizeof(size_t) < sizeof(jlong)) {
368 jlong size_t_max_value = (jlong)SIZE_MAX;
369 if (reserved_space_size > size_t_max_value || magnitude > size_t_max_value
370 || iterations > size_t_max_value) {
371 tty->print_cr("One of variables printed above overflows size_t. Can't proceed.\n");
372 return 2;
373 }
374 }
375
376 return wb_stress_virtual_space_resize((size_t) reserved_space_size,
377 (size_t) magnitude, (size_t) iterations);
378 WB_END
379
380 WB_ENTRY(jboolean, WB_IsGCSupported(JNIEnv* env, jobject o, jint name))
381 return GCConfig::is_gc_supported((CollectedHeap::Name)name);
382 WB_END
383
384 WB_ENTRY(jboolean, WB_HasLibgraal(JNIEnv* env, jobject o))
385 #if INCLUDE_JVMCI
386 return JVMCI::shared_library_exists();
387 #endif
388 return false;
389 WB_END
390
391 WB_ENTRY(jboolean, WB_IsGCSupportedByJVMCICompiler(JNIEnv* env, jobject o, jint name))
392 #if INCLUDE_JVMCI
393 if (EnableJVMCI) {
394 // Enter the JVMCI env that will be used by the CompileBroker.
395 JVMCIEnv jvmciEnv(thread, __FILE__, __LINE__);
396 return jvmciEnv.init_error() == JNI_OK && jvmciEnv.runtime()->is_gc_supported(&jvmciEnv, (CollectedHeap::Name)name);
397 }
398 #endif
399 return false;
400 WB_END
401
402 WB_ENTRY(jboolean, WB_IsGCSelected(JNIEnv* env, jobject o, jint name))
403 return GCConfig::is_gc_selected((CollectedHeap::Name)name);
404 WB_END
405
406 WB_ENTRY(jboolean, WB_IsGCSelectedErgonomically(JNIEnv* env, jobject o))
407 return GCConfig::is_gc_selected_ergonomically();
408 WB_END
409
410 WB_ENTRY(jboolean, WB_isObjectInOldGen(JNIEnv* env, jobject o, jobject obj))
411 oop p = JNIHandles::resolve(obj);
412 #if INCLUDE_G1GC
413 if (UseG1GC) {
414 G1CollectedHeap* g1h = G1CollectedHeap::heap();
415 const G1HeapRegion* hr = g1h->heap_region_containing(p);
416 return hr->is_old_or_humongous();
417 }
418 #endif
419 #if INCLUDE_PARALLELGC
420 if (UseParallelGC) {
421 ParallelScavengeHeap* psh = ParallelScavengeHeap::heap();
422 return !psh->is_in_young(p);
423 }
424 #endif
425 #if INCLUDE_ZGC
426 if (UseZGC) {
427 if (ZGenerational) {
428 return ZHeap::heap()->is_old(to_zaddress(p));
429 } else {
430 return Universe::heap()->is_in(p);
431 }
432 }
433 #endif
434 #if INCLUDE_SHENANDOAHGC
435 if (UseShenandoahGC) {
436 return Universe::heap()->is_in(p);
437 }
438 #endif
439 #if INCLUDE_SERIALGC
440 if (UseSerialGC) {
441 return !SerialHeap::heap()->is_in_young(p);
442 }
443 #endif
444 ShouldNotReachHere();
445 return false;
446 WB_END
447
448 WB_ENTRY(jlong, WB_GetObjectSize(JNIEnv* env, jobject o, jobject obj))
449 oop p = JNIHandles::resolve(obj);
450 return p->size() * HeapWordSize;
451 WB_END
452
453 WB_ENTRY(jlong, WB_GetHeapSpaceAlignment(JNIEnv* env, jobject o))
454 return (jlong)SpaceAlignment;
455 WB_END
456
457 WB_ENTRY(jlong, WB_GetHeapAlignment(JNIEnv* env, jobject o))
458 return (jlong)HeapAlignment;
459 WB_END
460
461 WB_ENTRY(jboolean, WB_SupportsConcurrentGCBreakpoints(JNIEnv* env, jobject o))
462 return Universe::heap()->supports_concurrent_gc_breakpoints();
463 WB_END
464
465 WB_ENTRY(void, WB_ConcurrentGCAcquireControl(JNIEnv* env, jobject o))
466 ConcurrentGCBreakpoints::acquire_control();
467 WB_END
468
469 WB_ENTRY(void, WB_ConcurrentGCReleaseControl(JNIEnv* env, jobject o))
470 ConcurrentGCBreakpoints::release_control();
471 WB_END
472
473 WB_ENTRY(void, WB_ConcurrentGCRunToIdle(JNIEnv* env, jobject o))
474 ConcurrentGCBreakpoints::run_to_idle();
475 WB_END
476
477 WB_ENTRY(jboolean, WB_ConcurrentGCRunTo(JNIEnv* env, jobject o, jobject at))
478 Handle h_name(THREAD, JNIHandles::resolve(at));
479 ResourceMark rm;
480 const char* c_name = java_lang_String::as_utf8_string(h_name());
481 return ConcurrentGCBreakpoints::run_to(c_name);
482 WB_END
483
484 #if INCLUDE_G1GC
485
486 WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj))
487 if (UseG1GC) {
488 G1CollectedHeap* g1h = G1CollectedHeap::heap();
489 oop result = JNIHandles::resolve(obj);
490 const G1HeapRegion* hr = g1h->heap_region_containing(result);
491 return hr->is_humongous();
492 }
493 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1IsHumongous: G1 GC is not enabled");
494 WB_END
495
496 WB_ENTRY(jboolean, WB_G1BelongsToHumongousRegion(JNIEnv* env, jobject o, jlong addr))
497 if (UseG1GC) {
498 G1CollectedHeap* g1h = G1CollectedHeap::heap();
499 const G1HeapRegion* hr = g1h->heap_region_containing((void*) addr);
500 return hr->is_humongous();
501 }
502 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1BelongsToHumongousRegion: G1 GC is not enabled");
503 WB_END
504
505 WB_ENTRY(jboolean, WB_G1BelongsToFreeRegion(JNIEnv* env, jobject o, jlong addr))
506 if (UseG1GC) {
507 G1CollectedHeap* g1h = G1CollectedHeap::heap();
508 const G1HeapRegion* hr = g1h->heap_region_containing((void*) addr);
509 return hr->is_free();
510 }
511 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1BelongsToFreeRegion: G1 GC is not enabled");
512 WB_END
513
514 WB_ENTRY(jlong, WB_G1NumMaxRegions(JNIEnv* env, jobject o))
515 if (UseG1GC) {
516 G1CollectedHeap* g1h = G1CollectedHeap::heap();
517 size_t nr = g1h->max_regions();
518 return (jlong)nr;
519 }
520 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1NumMaxRegions: G1 GC is not enabled");
521 WB_END
522
523 WB_ENTRY(jlong, WB_G1NumFreeRegions(JNIEnv* env, jobject o))
524 if (UseG1GC) {
525 G1CollectedHeap* g1h = G1CollectedHeap::heap();
526 size_t nr = g1h->num_free_regions();
527 return (jlong)nr;
528 }
529 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1NumFreeRegions: G1 GC is not enabled");
530 WB_END
531
532 WB_ENTRY(jboolean, WB_G1InConcurrentMark(JNIEnv* env, jobject o))
533 if (UseG1GC) {
534 G1CollectedHeap* g1h = G1CollectedHeap::heap();
535 return g1h->concurrent_mark()->cm_thread()->in_progress();
536 }
537 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1InConcurrentMark: G1 GC is not enabled");
538 WB_END
539
540 WB_ENTRY(jint, WB_G1CompletedConcurrentMarkCycles(JNIEnv* env, jobject o))
541 if (UseG1GC) {
542 G1CollectedHeap* g1h = G1CollectedHeap::heap();
543 G1ConcurrentMark* cm = g1h->concurrent_mark();
544 return cm->completed_mark_cycles();
545 }
546 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1CompletedConcurrentMarkCycles: G1 GC is not enabled");
547 WB_END
548
549 WB_ENTRY(jint, WB_G1RegionSize(JNIEnv* env, jobject o))
550 if (UseG1GC) {
551 return (jint)G1HeapRegion::GrainBytes;
552 }
553 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1RegionSize: G1 GC is not enabled");
554 WB_END
555
556 WB_ENTRY(jboolean, WB_G1HasRegionsToUncommit(JNIEnv* env, jobject o))
557 if (UseG1GC) {
558 return G1CollectedHeap::heap()->has_uncommittable_regions();
559 }
560 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1HasRegionsToUncommit: G1 GC is not enabled");
561 WB_END
562
563 #endif // INCLUDE_G1GC
564
565 #if INCLUDE_PARALLELGC
566
567 WB_ENTRY(jlong, WB_PSVirtualSpaceAlignment(JNIEnv* env, jobject o))
568 if (UseParallelGC) {
569 return GenAlignment;
570 }
571 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_PSVirtualSpaceAlignment: Parallel GC is not enabled");
572 WB_END
573
574 WB_ENTRY(jlong, WB_PSHeapGenerationAlignment(JNIEnv* env, jobject o))
575 if (UseParallelGC) {
576 return GenAlignment;
577 }
578 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_PSHeapGenerationAlignment: Parallel GC is not enabled");
579 WB_END
580
581 #endif // INCLUDE_PARALLELGC
582
583 #if INCLUDE_G1GC
584
585 WB_ENTRY(jobject, WB_G1AuxiliaryMemoryUsage(JNIEnv* env))
586 if (UseG1GC) {
587 ResourceMark rm(THREAD);
588 G1CollectedHeap* g1h = G1CollectedHeap::heap();
589 MemoryUsage usage = g1h->get_auxiliary_data_memory_usage();
590 Handle h = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL);
591 return JNIHandles::make_local(THREAD, h());
592 }
593 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1AuxiliaryMemoryUsage: G1 GC is not enabled");
594 WB_END
595
596 WB_ENTRY(jint, WB_G1ActiveMemoryNodeCount(JNIEnv* env, jobject o))
597 if (UseG1GC) {
598 G1NUMA* numa = G1NUMA::numa();
599 return (jint)numa->num_active_nodes();
600 }
601 THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1ActiveMemoryNodeCount: G1 GC is not enabled");
602 WB_END
603
604 WB_ENTRY(jintArray, WB_G1MemoryNodeIds(JNIEnv* env, jobject o))
605 if (UseG1GC) {
606 G1NUMA* numa = G1NUMA::numa();
607 int num_node_ids = checked_cast<int>(numa->num_active_nodes());
608 const uint* node_ids = numa->node_ids();
609
610 typeArrayOop result = oopFactory::new_intArray(num_node_ids, CHECK_NULL);
611 for (int i = 0; i < num_node_ids; i++) {
612 result->int_at_put(i, checked_cast<jint>(node_ids[i]));
613 }
614 return (jintArray) JNIHandles::make_local(THREAD, result);
615 }
616 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1MemoryNodeIds: G1 GC is not enabled");
617 WB_END
618
619 class OldRegionsLivenessClosure: public G1HeapRegionClosure {
620
621 private:
622 const int _liveness;
623 size_t _total_count;
624 size_t _total_memory;
625 size_t _total_memory_to_free;
626
627 public:
628 OldRegionsLivenessClosure(int liveness) :
629 _liveness(liveness),
630 _total_count(0),
631 _total_memory(0),
632 _total_memory_to_free(0) { }
633
634 size_t total_count() { return _total_count; }
635 size_t total_memory() { return _total_memory; }
636 size_t total_memory_to_free() { return _total_memory_to_free; }
637
638 bool do_heap_region(G1HeapRegion* r) {
639 if (r->is_old()) {
640 size_t live = r->live_bytes();
641 size_t size = r->used();
642 size_t reg_size = G1HeapRegion::GrainBytes;
643 if (size > 0 && ((int)(live * 100 / size) < _liveness)) {
644 _total_memory += size;
645 ++_total_count;
646 if (size == reg_size) {
647 // We don't include non-full regions since they are unlikely included in mixed gc
648 // for testing purposes it's enough to have lowest estimation of total memory that is expected to be freed
649 _total_memory_to_free += size - live;
650 }
651 }
652 }
653 return false;
654 }
655 };
656
657
658 WB_ENTRY(jlongArray, WB_G1GetMixedGCInfo(JNIEnv* env, jobject o, jint liveness))
659 if (!UseG1GC) {
660 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1GetMixedGCInfo: G1 GC is not enabled");
661 }
662 if (liveness < 0) {
663 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "liveness value should be non-negative");
664 }
665
666 G1CollectedHeap* g1h = G1CollectedHeap::heap();
667 OldRegionsLivenessClosure rli(liveness);
668 g1h->heap_region_iterate(&rli);
669
670 typeArrayOop result = oopFactory::new_longArray(3, CHECK_NULL);
671 result->long_at_put(0, rli.total_count());
672 result->long_at_put(1, rli.total_memory());
673 result->long_at_put(2, rli.total_memory_to_free());
674 return (jlongArray) JNIHandles::make_local(THREAD, result);
675 WB_END
676
677 #endif // INCLUDE_G1GC
678
679 // Alloc memory using the test memory tag so that we can use that to see if
680 // NMT picks it up correctly
681 WB_ENTRY(jlong, WB_NMTMalloc(JNIEnv* env, jobject o, jlong size))
682 jlong addr = 0;
683 addr = (jlong)(uintptr_t)os::malloc(size, mtTest);
684 return addr;
685 WB_END
686
687 // Alloc memory with pseudo call stack. The test can create pseudo malloc
688 // allocation site to stress the malloc tracking.
689 WB_ENTRY(jlong, WB_NMTMallocWithPseudoStack(JNIEnv* env, jobject o, jlong size, jint pseudo_stack))
690 address pc = (address)(size_t)pseudo_stack;
691 NativeCallStack stack(&pc, 1);
692 return (jlong)(uintptr_t)os::malloc(size, mtTest, stack);
693 WB_END
694
695 // Alloc memory with pseudo call stack and specific memory tag.
696 WB_ENTRY(jlong, WB_NMTMallocWithPseudoStackAndType(JNIEnv* env, jobject o, jlong size, jint pseudo_stack, jint mem_tag))
697 address pc = (address)(size_t)pseudo_stack;
698 NativeCallStack stack(&pc, 1);
699 return (jlong)(uintptr_t)os::malloc(size, (MemTag)mem_tag, stack);
700 WB_END
701
702 // Free the memory allocated by NMTAllocTest
703 WB_ENTRY(void, WB_NMTFree(JNIEnv* env, jobject o, jlong mem))
704 os::free((void*)(uintptr_t)mem);
705 WB_END
706
707 WB_ENTRY(jlong, WB_NMTReserveMemory(JNIEnv* env, jobject o, jlong size))
708 jlong addr = 0;
709
710 addr = (jlong)(uintptr_t)os::reserve_memory(size);
711 MemTracker::record_virtual_memory_tag((address)addr, mtTest);
712
713 return addr;
714 WB_END
715
716 WB_ENTRY(jlong, WB_NMTAttemptReserveMemoryAt(JNIEnv* env, jobject o, jlong addr, jlong size))
717 addr = (jlong)(uintptr_t)os::attempt_reserve_memory_at((char*)(uintptr_t)addr, (size_t)size);
718 MemTracker::record_virtual_memory_tag((address)addr, mtTest);
719
720 return addr;
721 WB_END
722
723 WB_ENTRY(void, WB_NMTCommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
724 os::commit_memory((char *)(uintptr_t)addr, size, !ExecMem);
725 MemTracker::record_virtual_memory_tag((address)(uintptr_t)addr, mtTest);
726 WB_END
727
728 WB_ENTRY(void, WB_NMTUncommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
729 os::uncommit_memory((char *)(uintptr_t)addr, size);
730 WB_END
731
732 WB_ENTRY(void, WB_NMTReleaseMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
733 os::release_memory((char *)(uintptr_t)addr, size);
734 WB_END
735
736 WB_ENTRY(jint, WB_NMTGetHashSize(JNIEnv* env, jobject o))
737 int hash_size = MallocSiteTable::hash_buckets();
738 assert(hash_size > 0, "NMT hash_size should be > 0");
739 return (jint)hash_size;
740 WB_END
741
742 WB_ENTRY(jlong, WB_NMTNewArena(JNIEnv* env, jobject o, jlong init_size))
743 Arena* arena = new (mtTest) Arena(mtTest, Arena::Tag::tag_other, size_t(init_size));
744 return (jlong)arena;
745 WB_END
746
747 WB_ENTRY(void, WB_NMTFreeArena(JNIEnv* env, jobject o, jlong arena))
748 Arena* a = (Arena*)arena;
749 delete a;
750 WB_END
751
752 WB_ENTRY(void, WB_NMTArenaMalloc(JNIEnv* env, jobject o, jlong arena, jlong size))
753 Arena* a = (Arena*)arena;
754 a->Amalloc(size_t(size));
755 WB_END
756
757 static jmethodID reflected_method_to_jmid(JavaThread* thread, JNIEnv* env, jobject method) {
758 assert(method != nullptr, "method should not be null");
759 ThreadToNativeFromVM ttn(thread);
760 return env->FromReflectedMethod(method);
761 }
762
763 // Deoptimizes all compiled frames and makes nmethods not entrant if it's requested
764 class VM_WhiteBoxDeoptimizeFrames : public VM_WhiteBoxOperation {
765 private:
766 int _result;
767 const bool _make_not_entrant;
768 public:
769 VM_WhiteBoxDeoptimizeFrames(bool make_not_entrant) :
770 _result(0), _make_not_entrant(make_not_entrant) { }
771 int result() const { return _result; }
772
773 void doit() {
774 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
775 if (t->has_last_Java_frame()) {
776 for (StackFrameStream fst(t, false /* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
777 frame* f = fst.current();
778 if (f->can_be_deoptimized() && !f->is_deoptimized_frame()) {
779 Deoptimization::deoptimize(t, *f);
780 if (_make_not_entrant) {
781 nmethod* nm = CodeCache::find_nmethod(f->pc());
782 assert(nm != nullptr, "did not find nmethod");
783 nm->make_not_entrant();
784 }
785 ++_result;
786 }
787 }
788 }
789 }
790 }
791 };
792
793 WB_ENTRY(jint, WB_DeoptimizeFrames(JNIEnv* env, jobject o, jboolean make_not_entrant))
794 VM_WhiteBoxDeoptimizeFrames op(make_not_entrant == JNI_TRUE);
795 VMThread::execute(&op);
796 return op.result();
797 WB_END
798
799 WB_ENTRY(jboolean, WB_IsFrameDeoptimized(JNIEnv* env, jobject o, jint depth))
800 bool result = false;
801 if (thread->has_last_Java_frame()) {
802 ResourceMark rm(THREAD);
803 RegisterMap reg_map(thread,
804 RegisterMap::UpdateMap::include,
805 RegisterMap::ProcessFrames::include,
806 RegisterMap::WalkContinuation::skip);
807 javaVFrame *jvf = thread->last_java_vframe(®_map);
808 for (jint d = 0; d < depth && jvf != nullptr; d++) {
809 jvf = jvf->java_sender();
810 }
811 result = jvf != nullptr && jvf->fr().is_deoptimized_frame();
812 }
813 return result;
814 WB_END
815
816 WB_ENTRY(void, WB_DeoptimizeAll(JNIEnv* env, jobject o))
817 DeoptimizationScope deopt_scope;
818 CodeCache::mark_all_nmethods_for_deoptimization(&deopt_scope);
819 deopt_scope.deoptimize_marked();
820 WB_END
821
822 WB_ENTRY(jint, WB_DeoptimizeMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
823 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
824 int result = 0;
825 CHECK_JNI_EXCEPTION_(env, result);
826
827 DeoptimizationScope deopt_scope;
828 {
829 MutexLocker mu(Compile_lock);
830 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
831 if (is_osr) {
832 result += mh->method_holder()->mark_osr_nmethods(&deopt_scope, mh());
833 } else {
834 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
835 if (mh->code() != nullptr) {
836 deopt_scope.mark(mh->code());
837 ++result;
838 }
839 }
840 CodeCache::mark_for_deoptimization(&deopt_scope, mh());
841 }
842
843 deopt_scope.deoptimize_marked();
844
845 return result;
846 WB_END
847
848 WB_ENTRY(jboolean, WB_IsMethodCompiled(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
849 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
850 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
851 MutexLocker mu(Compile_lock);
852 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
853 nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
854 if (code == nullptr) {
855 return JNI_FALSE;
856 }
857 return !code->is_marked_for_deoptimization();
858 WB_END
859
860 static bool is_excluded_for_compiler(AbstractCompiler* comp, methodHandle& mh) {
861 if (comp == nullptr) {
862 return true;
863 }
864 DirectiveSet* directive = DirectivesStack::getMatchingDirective(mh, comp);
865 bool exclude = directive->ExcludeOption;
866 DirectivesStack::release(directive);
867 return exclude;
868 }
869
870 static bool can_be_compiled_at_level(methodHandle& mh, jboolean is_osr, int level) {
871 if (is_osr) {
872 return CompilationPolicy::can_be_osr_compiled(mh, level);
873 } else {
874 return CompilationPolicy::can_be_compiled(mh, level);
875 }
876 }
877
878 WB_ENTRY(jboolean, WB_IsMethodCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
879 if (method == nullptr || comp_level > CompilationPolicy::highest_compile_level()) {
880 return false;
881 }
882 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
883 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
884 MutexLocker mu(Compile_lock);
885 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
886
887 // The ExcludeOption directive is evaluated lazily upon compilation attempt. If a method was not tried to be compiled by
888 // a compiler, yet, the method object is not set to be not compilable by that compiler. Thus, evaluate the compiler directive
889 // to exclude a compilation of 'method'.
890 if (comp_level == CompLevel_any) {
891 // Both compilers could have ExcludeOption set. Check all combinations.
892 bool excluded_c1 = is_excluded_for_compiler(CompileBroker::compiler1(), mh);
893 bool excluded_c2 = is_excluded_for_compiler(CompileBroker::compiler2(), mh);
894 if (excluded_c1 && excluded_c2) {
895 // Compilation of 'method' excluded by both compilers.
896 return false;
897 }
898
899 if (excluded_c1) {
900 // C1 only has ExcludeOption set: Check if compilable with C2.
901 return can_be_compiled_at_level(mh, is_osr, CompLevel_full_optimization);
902 } else if (excluded_c2) {
903 // C2 only has ExcludeOption set: Check if compilable with C1.
904 return can_be_compiled_at_level(mh, is_osr, CompLevel_simple);
905 }
906 } else if (comp_level > CompLevel_none && is_excluded_for_compiler(CompileBroker::compiler((int)comp_level), mh)) {
907 // Compilation of 'method' excluded by compiler used for 'comp_level'.
908 return false;
909 }
910
911 return can_be_compiled_at_level(mh, is_osr, (int)comp_level);
912 WB_END
913
914 WB_ENTRY(jboolean, WB_IsMethodQueuedForCompilation(JNIEnv* env, jobject o, jobject method))
915 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
916 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
917 MutexLocker mu(Compile_lock);
918 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
919 return mh->queued_for_compilation();
920 WB_END
921
922 WB_ENTRY(jboolean, WB_IsIntrinsicAvailable(JNIEnv* env, jobject o, jobject method, jobject compilation_context, jint compLevel))
923 if (compLevel < CompLevel_none || compLevel > CompilationPolicy::highest_compile_level()) {
924 return false; // Intrinsic is not available on a non-existent compilation level.
925 }
926 jmethodID method_id, compilation_context_id;
927 method_id = reflected_method_to_jmid(thread, env, method);
928 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
929 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(method_id));
930
931 DirectiveSet* directive;
932 AbstractCompiler* comp = CompileBroker::compiler((int)compLevel);
933 assert(comp != nullptr, "compiler not available");
934 if (compilation_context != nullptr) {
935 compilation_context_id = reflected_method_to_jmid(thread, env, compilation_context);
936 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
937 methodHandle cch(THREAD, Method::checked_resolve_jmethod_id(compilation_context_id));
938 directive = DirectivesStack::getMatchingDirective(cch, comp);
939 } else {
940 // Calling with null matches default directive
941 directive = DirectivesStack::getDefaultDirective(comp);
942 }
943 bool result = comp->is_intrinsic_available(mh, directive);
944 DirectivesStack::release(directive);
945 return result;
946 WB_END
947
948 WB_ENTRY(jint, WB_GetMethodCompilationLevel(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
949 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
950 CHECK_JNI_EXCEPTION_(env, CompLevel_none);
951 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
952 nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
953 return (code != nullptr ? code->comp_level() : CompLevel_none);
954 WB_END
955
956 WB_ENTRY(void, WB_MakeMethodNotCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
957 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
958 CHECK_JNI_EXCEPTION(env);
959 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
960 if (is_osr) {
961 mh->set_not_osr_compilable("WhiteBox", comp_level);
962 } else {
963 mh->set_not_compilable("WhiteBox", comp_level);
964 }
965 WB_END
966
967 WB_ENTRY(jint, WB_GetMethodDecompileCount(JNIEnv* env, jobject o, jobject method))
968 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
969 CHECK_JNI_EXCEPTION_(env, 0);
970 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
971 uint cnt = 0;
972 MethodData* mdo = mh->method_data();
973 if (mdo != nullptr) {
974 cnt = mdo->decompile_count();
975 }
976 return cnt;
977 WB_END
978
979 // Get the trap count of a method for a specific reason. If the trap count for
980 // that reason did overflow, this includes the overflow trap count of the method.
981 // If 'reason' is null, the sum of the traps for all reasons will be returned.
982 // This number includes the overflow trap count if the trap count for any reason
983 // did overflow.
984 WB_ENTRY(jint, WB_GetMethodTrapCount(JNIEnv* env, jobject o, jobject method, jstring reason_obj))
985 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
986 CHECK_JNI_EXCEPTION_(env, 0);
987 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
988 uint cnt = 0;
989 MethodData* mdo = mh->method_data();
990 if (mdo != nullptr) {
991 ResourceMark rm(THREAD);
992 char* reason_str = (reason_obj == nullptr) ?
993 nullptr : java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(reason_obj));
994 bool overflow = false;
995 for (uint reason = 0; reason < mdo->trap_reason_limit(); reason++) {
996 if (reason_str != nullptr && !strcmp(reason_str, Deoptimization::trap_reason_name(reason))) {
997 cnt = mdo->trap_count(reason);
998 // Count in the overflow trap count on overflow
999 if (cnt == (uint)-1) {
1000 cnt = mdo->trap_count_limit() + mdo->overflow_trap_count();
1001 }
1002 break;
1003 } else if (reason_str == nullptr) {
1004 uint c = mdo->trap_count(reason);
1005 if (c == (uint)-1) {
1006 c = mdo->trap_count_limit();
1007 if (!overflow) {
1008 // Count overflow trap count just once
1009 overflow = true;
1010 c += mdo->overflow_trap_count();
1011 }
1012 }
1013 cnt += c;
1014 }
1015 }
1016 }
1017 return cnt;
1018 WB_END
1019
1020 WB_ENTRY(jint, WB_GetDeoptCount(JNIEnv* env, jobject o, jstring reason_obj, jstring action_obj))
1021 if (reason_obj == nullptr && action_obj == nullptr) {
1022 return Deoptimization::total_deoptimization_count();
1023 }
1024 ResourceMark rm(THREAD);
1025 const char *reason_str = (reason_obj == nullptr) ?
1026 nullptr : java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(reason_obj));
1027 const char *action_str = (action_obj == nullptr) ?
1028 nullptr : java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(action_obj));
1029
1030 return Deoptimization::deoptimization_count(reason_str, action_str);
1031 WB_END
1032
1033 WB_ENTRY(jint, WB_GetMethodEntryBci(JNIEnv* env, jobject o, jobject method))
1034 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1035 CHECK_JNI_EXCEPTION_(env, InvocationEntryBci);
1036 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1037 nmethod* code = mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false);
1038 return (code != nullptr && code->is_osr_method() ? code->osr_entry_bci() : InvocationEntryBci);
1039 WB_END
1040
1041 WB_ENTRY(jboolean, WB_TestSetDontInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
1042 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1043 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1044 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1045 bool result = mh->dont_inline();
1046 mh->set_dont_inline(value == JNI_TRUE);
1047 return result;
1048 WB_END
1049
1050 WB_ENTRY(jint, WB_GetCompileQueueSize(JNIEnv* env, jobject o, jint comp_level))
1051 if (comp_level == CompLevel_any) {
1052 return CompileBroker::queue_size(CompLevel_full_optimization) /* C2 */ +
1053 CompileBroker::queue_size(CompLevel_full_profile) /* C1 */;
1054 } else {
1055 return CompileBroker::queue_size(comp_level);
1056 }
1057 WB_END
1058
1059 WB_ENTRY(jboolean, WB_TestSetForceInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
1060 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1061 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1062 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1063 bool result = mh->force_inline();
1064 mh->set_force_inline(value == JNI_TRUE);
1065 return result;
1066 WB_END
1067
1068 #ifdef LINUX
1069 bool WhiteBox::validate_cgroup(const char* proc_cgroups,
1070 const char* proc_self_cgroup,
1071 const char* proc_self_mountinfo,
1072 u1* cg_flags) {
1073 CgroupInfo cg_infos[CG_INFO_LENGTH];
1074 return CgroupSubsystemFactory::determine_type(cg_infos, proc_cgroups,
1075 proc_self_cgroup,
1076 proc_self_mountinfo, cg_flags);
1077 }
1078 #endif
1079
1080 bool WhiteBox::compile_method(Method* method, int comp_level, int bci, JavaThread* THREAD) {
1081 // Screen for unavailable/bad comp level or null method
1082 AbstractCompiler* comp = CompileBroker::compiler(comp_level);
1083 if (method == nullptr) {
1084 tty->print_cr("WB error: request to compile null method");
1085 return false;
1086 }
1087 if (comp_level > CompilationPolicy::highest_compile_level()) {
1088 tty->print_cr("WB error: invalid compilation level %d", comp_level);
1089 return false;
1090 }
1091 if (comp == nullptr) {
1092 tty->print_cr("WB error: no compiler for requested compilation level %d", comp_level);
1093 return false;
1094 }
1095
1096 // Check if compilation is blocking
1097 methodHandle mh(THREAD, method);
1098 DirectiveSet* directive = DirectivesStack::getMatchingDirective(mh, comp);
1099 bool is_blocking = !directive->BackgroundCompilationOption;
1100 DirectivesStack::release(directive);
1101
1102 // Compile method and check result
1103 nmethod* nm = CompileBroker::compile_method(mh, bci, comp_level, mh, mh->invocation_count(), CompileTask::Reason_Whitebox, CHECK_false);
1104 MutexLocker mu(THREAD, Compile_lock);
1105 bool is_queued = mh->queued_for_compilation();
1106 if ((!is_blocking && is_queued) || nm != nullptr) {
1107 return true;
1108 }
1109 // Check code again because compilation may be finished before Compile_lock is acquired.
1110 if (bci == InvocationEntryBci) {
1111 nmethod* code = mh->code();
1112 if (code != nullptr) {
1113 return true;
1114 }
1115 } else if (mh->lookup_osr_nmethod_for(bci, comp_level, false) != nullptr) {
1116 return true;
1117 }
1118 tty->print("WB error: failed to %s compile at level %d method ", is_blocking ? "blocking" : "", comp_level);
1119 mh->print_short_name(tty);
1120 tty->cr();
1121 if (is_blocking && is_queued) {
1122 tty->print_cr("WB error: blocking compilation is still in queue!");
1123 }
1124 return false;
1125 }
1126
1127 size_t WhiteBox::get_in_use_monitor_count() {
1128 return ObjectSynchronizer::_in_use_list.count();
1129 }
1130
1131 WB_ENTRY(jboolean, WB_EnqueueMethodForCompilation(JNIEnv* env, jobject o, jobject method, jint comp_level, jint bci))
1132 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1133 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1134 return WhiteBox::compile_method(Method::checked_resolve_jmethod_id(jmid), comp_level, bci, THREAD);
1135 WB_END
1136
1137 WB_ENTRY(jboolean, WB_EnqueueInitializerForCompilation(JNIEnv* env, jobject o, jclass klass, jint comp_level))
1138 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1139 Method* clinit = ik->class_initializer();
1140 if (clinit == nullptr || clinit->method_holder()->is_not_initialized()) {
1141 return false;
1142 }
1143 return WhiteBox::compile_method(clinit, comp_level, InvocationEntryBci, THREAD);
1144 WB_END
1145
1146 WB_ENTRY(jboolean, WB_ShouldPrintAssembly(JNIEnv* env, jobject o, jobject method, jint comp_level))
1147 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1148 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1149
1150 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1151 DirectiveSet* directive = DirectivesStack::getMatchingDirective(mh, CompileBroker::compiler(comp_level));
1152 bool result = directive->PrintAssemblyOption;
1153 DirectivesStack::release(directive);
1154
1155 return result;
1156 WB_END
1157
1158 WB_ENTRY(jint, WB_MatchesInline(JNIEnv* env, jobject o, jobject method, jstring pattern))
1159 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1160 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1161
1162 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1163
1164 ResourceMark rm(THREAD);
1165 const char* error_msg = nullptr;
1166 char* method_str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(pattern));
1167 InlineMatcher* m = InlineMatcher::parse_inline_pattern(method_str, error_msg);
1168
1169 if (m == nullptr) {
1170 assert(error_msg != nullptr, "Always have an error message");
1171 tty->print_cr("Got error: %s", error_msg);
1172 return -1; // Pattern failed
1173 }
1174
1175 // Pattern works - now check if it matches
1176 int result;
1177 if (m->match(mh, InlineMatcher::force_inline)) {
1178 result = 2; // Force inline match
1179 } else if (m->match(mh, InlineMatcher::dont_inline)) {
1180 result = 1; // Dont inline match
1181 } else {
1182 result = 0; // No match
1183 }
1184 delete m;
1185 return result;
1186 WB_END
1187
1188 WB_ENTRY(jint, WB_MatchesMethod(JNIEnv* env, jobject o, jobject method, jstring pattern))
1189 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1190 CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1191
1192 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1193
1194 ResourceMark rm;
1195 char* method_str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(pattern));
1196
1197 const char* error_msg = nullptr;
1198
1199 BasicMatcher* m = BasicMatcher::parse_method_pattern(method_str, error_msg, false);
1200 if (m == nullptr) {
1201 assert(error_msg != nullptr, "Must have error_msg");
1202 tty->print_cr("Got error: %s", error_msg);
1203 return -1;
1204 }
1205
1206 // Pattern works - now check if it matches
1207 int result = m->matches(mh);
1208 delete m;
1209 assert(result == 0 || result == 1, "Result out of range");
1210 return result;
1211 WB_END
1212
1213 WB_ENTRY(void, WB_MarkMethodProfiled(JNIEnv* env, jobject o, jobject method))
1214 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1215 CHECK_JNI_EXCEPTION(env);
1216 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1217
1218 MethodData* mdo = mh->method_data();
1219 if (mdo == nullptr) {
1220 Method::build_profiling_method_data(mh, CHECK_AND_CLEAR);
1221 mdo = mh->method_data();
1222 }
1223 mdo->init();
1224 InvocationCounter* icnt = mdo->invocation_counter();
1225 InvocationCounter* bcnt = mdo->backedge_counter();
1226 // set i-counter according to CompilationPolicy::is_method_profiled
1227 icnt->set(Tier4MinInvocationThreshold);
1228 bcnt->set(Tier4CompileThreshold);
1229 WB_END
1230
1231 WB_ENTRY(void, WB_ClearMethodState(JNIEnv* env, jobject o, jobject method))
1232 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1233 CHECK_JNI_EXCEPTION(env);
1234 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1235 MutexLocker mu(THREAD, Compile_lock);
1236 MethodData* mdo = mh->method_data();
1237 MethodCounters* mcs = mh->method_counters();
1238
1239 if (mdo != nullptr) {
1240 mdo->init();
1241 ResourceMark rm(THREAD);
1242 int arg_count = mdo->method()->size_of_parameters();
1243 for (int i = 0; i < arg_count; i++) {
1244 mdo->set_arg_modified(i, 0);
1245 }
1246 mdo->clean_method_data(/*always_clean*/true);
1247 }
1248
1249 mh->clear_is_not_c1_compilable();
1250 mh->clear_is_not_c2_compilable();
1251 mh->clear_is_not_c2_osr_compilable();
1252 NOT_PRODUCT(mh->set_compiled_invocation_count(0));
1253 if (mcs != nullptr) {
1254 mcs->clear_counters();
1255 }
1256 WB_END
1257
1258 template <typename T, int type_enum>
1259 static bool GetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value) {
1260 if (name == nullptr) {
1261 return false;
1262 }
1263 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1264 const char* flag_name = env->GetStringUTFChars(name, nullptr);
1265 CHECK_JNI_EXCEPTION_(env, false);
1266 const JVMFlag* flag = JVMFlag::find_declared_flag(flag_name);
1267 JVMFlag::Error result = JVMFlagAccess::get<T, type_enum>(flag, value);
1268 env->ReleaseStringUTFChars(name, flag_name);
1269 return (result == JVMFlag::SUCCESS);
1270 }
1271
1272 template <typename T, int type_enum>
1273 static bool SetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value) {
1274 if (name == nullptr) {
1275 return false;
1276 }
1277 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1278 const char* flag_name = env->GetStringUTFChars(name, nullptr);
1279 CHECK_JNI_EXCEPTION_(env, false);
1280 JVMFlag* flag = JVMFlag::find_flag(flag_name);
1281 JVMFlag::Error result = JVMFlagAccess::set<T, type_enum>(flag, value, JVMFlagOrigin::INTERNAL);
1282 env->ReleaseStringUTFChars(name, flag_name);
1283 return (result == JVMFlag::SUCCESS);
1284 }
1285
1286 template <typename T>
1287 static jobject box(JavaThread* thread, JNIEnv* env, Symbol* name, Symbol* sig, T value) {
1288 ResourceMark rm(thread);
1289 jclass clazz = env->FindClass(name->as_C_string());
1290 CHECK_JNI_EXCEPTION_(env, nullptr);
1291 jmethodID methodID = env->GetStaticMethodID(clazz,
1292 vmSymbols::valueOf_name()->as_C_string(),
1293 sig->as_C_string());
1294 CHECK_JNI_EXCEPTION_(env, nullptr);
1295 jobject result = env->CallStaticObjectMethod(clazz, methodID, value);
1296 CHECK_JNI_EXCEPTION_(env, nullptr);
1297 return result;
1298 }
1299
1300 static jobject booleanBox(JavaThread* thread, JNIEnv* env, jboolean value) {
1301 return box(thread, env, vmSymbols::java_lang_Boolean(), vmSymbols::Boolean_valueOf_signature(), value);
1302 }
1303 static jobject integerBox(JavaThread* thread, JNIEnv* env, jint value) {
1304 return box(thread, env, vmSymbols::java_lang_Integer(), vmSymbols::Integer_valueOf_signature(), value);
1305 }
1306 static jobject longBox(JavaThread* thread, JNIEnv* env, jlong value) {
1307 return box(thread, env, vmSymbols::java_lang_Long(), vmSymbols::Long_valueOf_signature(), value);
1308 }
1309 /* static jobject floatBox(JavaThread* thread, JNIEnv* env, jfloat value) {
1310 return box(thread, env, vmSymbols::java_lang_Float(), vmSymbols::Float_valueOf_signature(), value);
1311 }*/
1312 static jobject doubleBox(JavaThread* thread, JNIEnv* env, jdouble value) {
1313 return box(thread, env, vmSymbols::java_lang_Double(), vmSymbols::Double_valueOf_signature(), value);
1314 }
1315
1316 static const JVMFlag* getVMFlag(JavaThread* thread, JNIEnv* env, jstring name) {
1317 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1318 const char* flag_name = env->GetStringUTFChars(name, nullptr);
1319 CHECK_JNI_EXCEPTION_(env, nullptr);
1320 const JVMFlag* result = JVMFlag::find_declared_flag(flag_name);
1321 env->ReleaseStringUTFChars(name, flag_name);
1322 return result;
1323 }
1324
1325 WB_ENTRY(jboolean, WB_IsConstantVMFlag(JNIEnv* env, jobject o, jstring name))
1326 const JVMFlag* flag = getVMFlag(thread, env, name);
1327 return (flag != nullptr) && flag->is_constant_in_binary();
1328 WB_END
1329
1330 WB_ENTRY(jboolean, WB_IsDefaultVMFlag(JNIEnv* env, jobject o, jstring name))
1331 const JVMFlag* flag = getVMFlag(thread, env, name);
1332 return (flag != nullptr) && flag->is_default();
1333 WB_END
1334
1335 WB_ENTRY(jboolean, WB_IsLockedVMFlag(JNIEnv* env, jobject o, jstring name))
1336 const JVMFlag* flag = getVMFlag(thread, env, name);
1337 return (flag != nullptr) && !(flag->is_unlocked() || flag->is_unlocker());
1338 WB_END
1339
1340 WB_ENTRY(jobject, WB_GetBooleanVMFlag(JNIEnv* env, jobject o, jstring name))
1341 bool result;
1342 if (GetVMFlag <JVM_FLAG_TYPE(bool)> (thread, env, name, &result)) {
1343 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1344 return booleanBox(thread, env, result);
1345 }
1346 return nullptr;
1347 WB_END
1348
1349 template <typename T, int type_enum>
1350 jobject GetVMFlag_longBox(JNIEnv* env, JavaThread* thread, jstring name) {
1351 T result;
1352 if (GetVMFlag <T, type_enum> (thread, env, name, &result)) {
1353 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1354 return longBox(thread, env, result);
1355 }
1356 return nullptr;
1357 }
1358
1359 WB_ENTRY(jobject, WB_GetIntVMFlag(JNIEnv* env, jobject o, jstring name))
1360 return GetVMFlag_longBox<JVM_FLAG_TYPE(int)>(env, thread, name);
1361 WB_END
1362
1363 WB_ENTRY(jobject, WB_GetUintVMFlag(JNIEnv* env, jobject o, jstring name))
1364 return GetVMFlag_longBox<JVM_FLAG_TYPE(uint)>(env, thread, name);
1365 WB_END
1366
1367 WB_ENTRY(jobject, WB_GetIntxVMFlag(JNIEnv* env, jobject o, jstring name))
1368 return GetVMFlag_longBox<JVM_FLAG_TYPE(intx)>(env, thread, name);
1369 WB_END
1370
1371 WB_ENTRY(jobject, WB_GetUintxVMFlag(JNIEnv* env, jobject o, jstring name))
1372 return GetVMFlag_longBox<JVM_FLAG_TYPE(uintx)>(env, thread, name);
1373 WB_END
1374
1375 WB_ENTRY(jobject, WB_GetUint64VMFlag(JNIEnv* env, jobject o, jstring name))
1376 return GetVMFlag_longBox<JVM_FLAG_TYPE(uint64_t)>(env, thread, name);
1377 WB_END
1378
1379 WB_ENTRY(jobject, WB_GetSizeTVMFlag(JNIEnv* env, jobject o, jstring name))
1380 return GetVMFlag_longBox<JVM_FLAG_TYPE(size_t)>(env, thread, name);
1381 WB_END
1382
1383 WB_ENTRY(jobject, WB_GetDoubleVMFlag(JNIEnv* env, jobject o, jstring name))
1384 double result;
1385 if (GetVMFlag <JVM_FLAG_TYPE(double)> (thread, env, name, &result)) {
1386 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1387 return doubleBox(thread, env, result);
1388 }
1389 return nullptr;
1390 WB_END
1391
1392 WB_ENTRY(jstring, WB_GetStringVMFlag(JNIEnv* env, jobject o, jstring name))
1393 ccstr ccstrResult;
1394 if (GetVMFlag <JVM_FLAG_TYPE(ccstr)> (thread, env, name, &ccstrResult)) {
1395 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1396 jstring result = env->NewStringUTF(ccstrResult);
1397 CHECK_JNI_EXCEPTION_(env, nullptr);
1398 return result;
1399 }
1400 return nullptr;
1401 WB_END
1402
1403 WB_ENTRY(void, WB_SetBooleanVMFlag(JNIEnv* env, jobject o, jstring name, jboolean value))
1404 bool result = value == JNI_TRUE ? true : false;
1405 SetVMFlag <JVM_FLAG_TYPE(bool)> (thread, env, name, &result);
1406 WB_END
1407
1408 WB_ENTRY(void, WB_SetIntVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1409 int result = checked_cast<int>(value);
1410 SetVMFlag <JVM_FLAG_TYPE(int)> (thread, env, name, &result);
1411 WB_END
1412
1413 WB_ENTRY(void, WB_SetUintVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1414 uint result = checked_cast<uint>(value);
1415 SetVMFlag <JVM_FLAG_TYPE(uint)> (thread, env, name, &result);
1416 WB_END
1417
1418 WB_ENTRY(void, WB_SetIntxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1419 intx result = value;
1420 SetVMFlag <JVM_FLAG_TYPE(intx)> (thread, env, name, &result);
1421 WB_END
1422
1423 WB_ENTRY(void, WB_SetUintxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1424 uintx result = value;
1425 SetVMFlag <JVM_FLAG_TYPE(uintx)> (thread, env, name, &result);
1426 WB_END
1427
1428 WB_ENTRY(void, WB_SetUint64VMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1429 uint64_t result = value;
1430 SetVMFlag <JVM_FLAG_TYPE(uint64_t)> (thread, env, name, &result);
1431 WB_END
1432
1433 WB_ENTRY(void, WB_SetSizeTVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1434 size_t result = value;
1435 SetVMFlag <JVM_FLAG_TYPE(size_t)> (thread, env, name, &result);
1436 WB_END
1437
1438 WB_ENTRY(void, WB_SetDoubleVMFlag(JNIEnv* env, jobject o, jstring name, jdouble value))
1439 double result = value;
1440 SetVMFlag <JVM_FLAG_TYPE(double)> (thread, env, name, &result);
1441 WB_END
1442
1443 WB_ENTRY(void, WB_SetStringVMFlag(JNIEnv* env, jobject o, jstring name, jstring value))
1444 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1445 const char* ccstrValue;
1446 if (value == nullptr) {
1447 ccstrValue = nullptr;
1448 }
1449 else {
1450 ccstrValue = env->GetStringUTFChars(value, nullptr);
1451 CHECK_JNI_EXCEPTION(env);
1452 }
1453 {
1454 ccstr param = ccstrValue;
1455 ThreadInVMfromNative ttvfn(thread); // back to VM
1456 if (SetVMFlag <JVM_FLAG_TYPE(ccstr)> (thread, env, name, ¶m)) {
1457 assert(param == nullptr, "old value is freed automatically and not returned");
1458 }
1459 }
1460 if (value != nullptr) {
1461 env->ReleaseStringUTFChars(value, ccstrValue);
1462 }
1463 WB_END
1464
1465 WB_ENTRY(void, WB_LockCompilation(JNIEnv* env, jobject o, jlong timeout))
1466 WhiteBox::compilation_locked = true;
1467 WB_END
1468
1469 WB_ENTRY(void, WB_UnlockCompilation(JNIEnv* env, jobject o))
1470 MonitorLocker mo(Compilation_lock, Mutex::_no_safepoint_check_flag);
1471 WhiteBox::compilation_locked = false;
1472 mo.notify_all();
1473 WB_END
1474
1475 WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString))
1476 ResourceMark rm(THREAD);
1477 int len;
1478 jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len, CHECK_false);
1479 return (StringTable::lookup(name, len) != nullptr);
1480 WB_END
1481
1482 WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o))
1483 Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(true);
1484 Universe::heap()->collect(GCCause::_wb_full_gc);
1485 #if INCLUDE_G1GC || INCLUDE_SERIALGC
1486 if (UseG1GC || UseSerialGC) {
1487 // Needs to be cleared explicitly for G1 and Serial GC.
1488 Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(false);
1489 }
1490 #endif // INCLUDE_G1GC || INCLUDE_SERIALGC
1491 WB_END
1492
1493 WB_ENTRY(void, WB_YoungGC(JNIEnv* env, jobject o))
1494 Universe::heap()->collect(GCCause::_wb_young_gc);
1495 WB_END
1496
1497 WB_ENTRY(void, WB_ReadReservedMemory(JNIEnv* env, jobject o))
1498 // static+volatile in order to force the read to happen
1499 // (not be eliminated by the compiler)
1500 static char c;
1501 static volatile char* p;
1502
1503 p = os::reserve_memory(os::vm_allocation_granularity());
1504 if (p == nullptr) {
1505 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Failed to reserve memory");
1506 }
1507
1508 c = *p;
1509 WB_END
1510
1511 WB_ENTRY(jstring, WB_GetCPUFeatures(JNIEnv* env, jobject o))
1512 const char* features = VM_Version::features_string();
1513 ThreadToNativeFromVM ttn(thread);
1514 jstring features_string = env->NewStringUTF(features);
1515
1516 CHECK_JNI_EXCEPTION_(env, nullptr);
1517
1518 return features_string;
1519 WB_END
1520
1521 CodeBlobType WhiteBox::get_blob_type(const CodeBlob* code) {
1522 guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1523 return CodeCache::get_code_heap(code)->code_blob_type();
1524 }
1525
1526 CodeHeap* WhiteBox::get_code_heap(CodeBlobType blob_type) {
1527 guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1528 return CodeCache::get_code_heap(blob_type);
1529 }
1530
1531 struct CodeBlobStub {
1532 CodeBlobStub(const CodeBlob* blob) :
1533 name(os::strdup(blob->name())),
1534 size(blob->size()),
1535 blob_type(static_cast<jint>(WhiteBox::get_blob_type(blob))),
1536 address((jlong) blob) { }
1537 ~CodeBlobStub() { os::free((void*) name); }
1538 const char* const name;
1539 const jint size;
1540 const jint blob_type;
1541 const jlong address;
1542 };
1543
1544 static jobjectArray codeBlob2objectArray(JavaThread* thread, JNIEnv* env, CodeBlobStub* cb) {
1545 ResourceMark rm;
1546 jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1547 CHECK_JNI_EXCEPTION_(env, nullptr);
1548 jobjectArray result = env->NewObjectArray(4, clazz, nullptr);
1549
1550 jstring name = env->NewStringUTF(cb->name);
1551 CHECK_JNI_EXCEPTION_(env, nullptr);
1552 env->SetObjectArrayElement(result, 0, name);
1553
1554 jobject obj = integerBox(thread, env, cb->size);
1555 CHECK_JNI_EXCEPTION_(env, nullptr);
1556 env->SetObjectArrayElement(result, 1, obj);
1557
1558 obj = integerBox(thread, env, cb->blob_type);
1559 CHECK_JNI_EXCEPTION_(env, nullptr);
1560 env->SetObjectArrayElement(result, 2, obj);
1561
1562 obj = longBox(thread, env, cb->address);
1563 CHECK_JNI_EXCEPTION_(env, nullptr);
1564 env->SetObjectArrayElement(result, 3, obj);
1565
1566 return result;
1567 }
1568
1569 WB_ENTRY(jobjectArray, WB_GetNMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
1570 ResourceMark rm(THREAD);
1571 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1572 CHECK_JNI_EXCEPTION_(env, nullptr);
1573 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1574 nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
1575 jobjectArray result = nullptr;
1576 if (code == nullptr) {
1577 return result;
1578 }
1579 int comp_level = code->comp_level();
1580 int insts_size = code->insts_size();
1581
1582 ThreadToNativeFromVM ttn(thread);
1583 jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1584 CHECK_JNI_EXCEPTION_(env, nullptr);
1585 result = env->NewObjectArray(5, clazz, nullptr);
1586 if (result == nullptr) {
1587 return result;
1588 }
1589
1590 CodeBlobStub stub(code);
1591 jobjectArray codeBlob = codeBlob2objectArray(thread, env, &stub);
1592 CHECK_JNI_EXCEPTION_(env, nullptr);
1593 env->SetObjectArrayElement(result, 0, codeBlob);
1594
1595 jobject level = integerBox(thread, env, comp_level);
1596 CHECK_JNI_EXCEPTION_(env, nullptr);
1597 env->SetObjectArrayElement(result, 1, level);
1598
1599 jbyteArray insts = env->NewByteArray(insts_size);
1600 CHECK_JNI_EXCEPTION_(env, nullptr);
1601 env->SetByteArrayRegion(insts, 0, insts_size, (jbyte*) code->insts_begin());
1602 env->SetObjectArrayElement(result, 2, insts);
1603
1604 jobject id = integerBox(thread, env, code->compile_id());
1605 CHECK_JNI_EXCEPTION_(env, nullptr);
1606 env->SetObjectArrayElement(result, 3, id);
1607
1608 jobject entry_point = longBox(thread, env, (jlong) code->entry_point());
1609 CHECK_JNI_EXCEPTION_(env, nullptr);
1610 env->SetObjectArrayElement(result, 4, entry_point);
1611
1612 return result;
1613 WB_END
1614
1615 CodeBlob* WhiteBox::allocate_code_blob(int size, CodeBlobType blob_type) {
1616 guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1617 BufferBlob* blob;
1618 int full_size = CodeBlob::align_code_offset(sizeof(BufferBlob));
1619 if (full_size < size) {
1620 full_size += align_up(size - full_size, oopSize);
1621 }
1622 {
1623 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1624 blob = (BufferBlob*) CodeCache::allocate(full_size, blob_type);
1625 if (blob != nullptr) {
1626 ::new (blob) BufferBlob("WB::DummyBlob", CodeBlobKind::Buffer, full_size);
1627 }
1628 }
1629 // Track memory usage statistic after releasing CodeCache_lock
1630 MemoryService::track_code_cache_memory_usage();
1631 return blob;
1632 }
1633
1634 WB_ENTRY(jlong, WB_AllocateCodeBlob(JNIEnv* env, jobject o, jint size, jint blob_type))
1635 if (size < 0) {
1636 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1637 err_msg("WB_AllocateCodeBlob: size is negative: " INT32_FORMAT, size));
1638 }
1639 return (jlong) WhiteBox::allocate_code_blob(size, static_cast<CodeBlobType>(blob_type));
1640 WB_END
1641
1642 WB_ENTRY(void, WB_FreeCodeBlob(JNIEnv* env, jobject o, jlong addr))
1643 if (addr == 0) {
1644 return;
1645 }
1646 BufferBlob::free((BufferBlob*) addr);
1647 WB_END
1648
1649 WB_ENTRY(jobjectArray, WB_GetCodeHeapEntries(JNIEnv* env, jobject o, jint blob_type))
1650 ResourceMark rm;
1651 GrowableArray<CodeBlobStub*> blobs;
1652 {
1653 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1654 CodeHeap* heap = WhiteBox::get_code_heap(static_cast<CodeBlobType>(blob_type));
1655 if (heap == nullptr) {
1656 return nullptr;
1657 }
1658 for (CodeBlob* cb = (CodeBlob*) heap->first();
1659 cb != nullptr; cb = (CodeBlob*) heap->next(cb)) {
1660 CodeBlobStub* stub = NEW_RESOURCE_OBJ(CodeBlobStub);
1661 new (stub) CodeBlobStub(cb);
1662 blobs.append(stub);
1663 }
1664 }
1665 ThreadToNativeFromVM ttn(thread);
1666 jobjectArray result = nullptr;
1667 jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1668 CHECK_JNI_EXCEPTION_(env, nullptr);
1669 result = env->NewObjectArray(blobs.length(), clazz, nullptr);
1670 CHECK_JNI_EXCEPTION_(env, nullptr);
1671 if (result == nullptr) {
1672 return result;
1673 }
1674 int i = 0;
1675 for (GrowableArrayIterator<CodeBlobStub*> it = blobs.begin();
1676 it != blobs.end(); ++it) {
1677 jobjectArray obj = codeBlob2objectArray(thread, env, *it);
1678 CHECK_JNI_EXCEPTION_(env, nullptr);
1679 env->SetObjectArrayElement(result, i, obj);
1680 CHECK_JNI_EXCEPTION_(env, nullptr);
1681 ++i;
1682 }
1683 return result;
1684 WB_END
1685
1686 WB_ENTRY(jint, WB_GetCompilationActivityMode(JNIEnv* env, jobject o))
1687 return CompileBroker::get_compilation_activity_mode();
1688 WB_END
1689
1690 WB_ENTRY(jobjectArray, WB_GetCodeBlob(JNIEnv* env, jobject o, jlong addr))
1691 if (addr == 0) {
1692 THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(),
1693 "WB_GetCodeBlob: addr is null");
1694 }
1695 ThreadToNativeFromVM ttn(thread);
1696 CodeBlobStub stub((CodeBlob*) addr);
1697 return codeBlob2objectArray(thread, env, &stub);
1698 WB_END
1699
1700 WB_ENTRY(jlong, WB_GetMethodData(JNIEnv* env, jobject wv, jobject method))
1701 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1702 CHECK_JNI_EXCEPTION_(env, 0);
1703 methodHandle mh(thread, Method::checked_resolve_jmethod_id(jmid));
1704 return (jlong) mh->method_data();
1705 WB_END
1706
1707 WB_ENTRY(jlong, WB_GetThreadStackSize(JNIEnv* env, jobject o))
1708 return (jlong) thread->stack_size();
1709 WB_END
1710
1711 WB_ENTRY(jlong, WB_GetThreadRemainingStackSize(JNIEnv* env, jobject o))
1712 return (jlong) thread->stack_overflow_state()->stack_available(
1713 os::current_stack_pointer()) - (jlong)StackOverflow::stack_shadow_zone_size();
1714 WB_END
1715
1716
1717 int WhiteBox::array_bytes_to_length(size_t bytes) {
1718 return Array<u1>::bytes_to_length(bytes);
1719 }
1720
1721 ///////////////
1722 // MetaspaceTestContext and MetaspaceTestArena
1723 WB_ENTRY(jlong, WB_CreateMetaspaceTestContext(JNIEnv* env, jobject wb, jlong commit_limit, jlong reserve_limit))
1724 metaspace::MetaspaceTestContext* context =
1725 new metaspace::MetaspaceTestContext("whitebox-metaspace-context", (size_t) commit_limit, (size_t) reserve_limit);
1726 return (jlong)p2i(context);
1727 WB_END
1728
1729 WB_ENTRY(void, WB_DestroyMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1730 delete (metaspace::MetaspaceTestContext*) context;
1731 WB_END
1732
1733 WB_ENTRY(void, WB_PurgeMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1734 metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1735 context0->purge_area();
1736 WB_END
1737
1738 WB_ENTRY(void, WB_PrintMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1739 metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1740 context0->print_on(tty);
1741 WB_END
1742
1743 WB_ENTRY(jlong, WB_GetTotalCommittedWordsInMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1744 metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1745 return context0->committed_words();
1746 WB_END
1747
1748 WB_ENTRY(jlong, WB_GetTotalUsedWordsInMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1749 metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1750 return context0->used_words();
1751 WB_END
1752
1753 WB_ENTRY(jlong, WB_CreateArenaInTestContext(JNIEnv* env, jobject wb, jlong context, jboolean is_micro))
1754 const Metaspace::MetaspaceType type = is_micro ? Metaspace::ReflectionMetaspaceType : Metaspace::StandardMetaspaceType;
1755 metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1756 return (jlong)p2i(context0->create_arena(type));
1757 WB_END
1758
1759 WB_ENTRY(void, WB_DestroyMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena))
1760 delete (metaspace::MetaspaceTestArena*) arena;
1761 WB_END
1762
1763 WB_ENTRY(jlong, WB_AllocateFromMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena, jlong word_size))
1764 metaspace::MetaspaceTestArena* arena0 = (metaspace::MetaspaceTestArena*) arena;
1765 MetaWord* p = arena0->allocate((size_t) word_size);
1766 return (jlong)p2i(p);
1767 WB_END
1768
1769 WB_ENTRY(void, WB_DeallocateToMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena, jlong p, jlong word_size))
1770 metaspace::MetaspaceTestArena* arena0 = (metaspace::MetaspaceTestArena*) arena;
1771 arena0->deallocate((MetaWord*)p, (size_t) word_size);
1772 WB_END
1773
1774 WB_ENTRY(jlong, WB_GetMaxMetaspaceAllocationSize(JNIEnv* env, jobject wb))
1775 return (jlong) Metaspace::max_allocation_word_size() * BytesPerWord;
1776 WB_END
1777
1778 //////////////
1779
1780 WB_ENTRY(jlong, WB_AllocateMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong size))
1781 if (size < 0) {
1782 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1783 err_msg("WB_AllocateMetaspace: size is negative: " JLONG_FORMAT, size));
1784 }
1785
1786 oop class_loader_oop = JNIHandles::resolve(class_loader);
1787 ClassLoaderData* cld = class_loader_oop != nullptr
1788 ? java_lang_ClassLoader::loader_data_acquire(class_loader_oop)
1789 : ClassLoaderData::the_null_class_loader_data();
1790
1791 void* metadata = MetadataFactory::new_array<u1>(cld, WhiteBox::array_bytes_to_length((size_t)size), thread);
1792
1793 return (jlong)(uintptr_t)metadata;
1794 WB_END
1795
1796 WB_ENTRY(void, WB_DefineModule(JNIEnv* env, jobject o, jobject module, jboolean is_open,
1797 jstring version, jstring location, jobjectArray packages))
1798 Handle h_module (THREAD, JNIHandles::resolve(module));
1799 Modules::define_module(h_module, is_open, version, location, packages, CHECK);
1800 WB_END
1801
1802 WB_ENTRY(void, WB_AddModuleExports(JNIEnv* env, jobject o, jobject from_module, jstring package, jobject to_module))
1803 Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1804 Handle h_to_module (THREAD, JNIHandles::resolve(to_module));
1805 Modules::add_module_exports_qualified(h_from_module, package, h_to_module, CHECK);
1806 WB_END
1807
1808 WB_ENTRY(void, WB_AddModuleExportsToAllUnnamed(JNIEnv* env, jobject o, jclass module, jstring package))
1809 Handle h_module (THREAD, JNIHandles::resolve(module));
1810 Modules::add_module_exports_to_all_unnamed(h_module, package, CHECK);
1811 WB_END
1812
1813 WB_ENTRY(void, WB_AddModuleExportsToAll(JNIEnv* env, jobject o, jclass module, jstring package))
1814 Handle h_module (THREAD, JNIHandles::resolve(module));
1815 Modules::add_module_exports(h_module, package, Handle(), CHECK);
1816 WB_END
1817
1818 WB_ENTRY(void, WB_AddReadsModule(JNIEnv* env, jobject o, jobject from_module, jobject source_module))
1819 Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1820 Handle h_source_module (THREAD, JNIHandles::resolve(source_module));
1821 Modules::add_reads_module(h_from_module, h_source_module, CHECK);
1822 WB_END
1823
1824 WB_ENTRY(jlong, WB_IncMetaspaceCapacityUntilGC(JNIEnv* env, jobject wb, jlong inc))
1825 if (inc < 0) {
1826 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1827 err_msg("WB_IncMetaspaceCapacityUntilGC: inc is negative: " JLONG_FORMAT, inc));
1828 }
1829
1830 jlong max_size_t = (jlong) ((size_t) -1);
1831 if (inc > max_size_t) {
1832 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1833 err_msg("WB_IncMetaspaceCapacityUntilGC: inc does not fit in size_t: " JLONG_FORMAT, inc));
1834 }
1835
1836 size_t new_cap_until_GC = 0;
1837 size_t aligned_inc = align_down((size_t) inc, Metaspace::commit_alignment());
1838 bool success = MetaspaceGC::inc_capacity_until_GC(aligned_inc, &new_cap_until_GC);
1839 if (!success) {
1840 THROW_MSG_0(vmSymbols::java_lang_IllegalStateException(),
1841 "WB_IncMetaspaceCapacityUntilGC: could not increase capacity until GC "
1842 "due to contention with another thread");
1843 }
1844 return (jlong) new_cap_until_GC;
1845 WB_END
1846
1847 WB_ENTRY(jlong, WB_MetaspaceCapacityUntilGC(JNIEnv* env, jobject wb))
1848 return (jlong) MetaspaceGC::capacity_until_GC();
1849 WB_END
1850
1851 // The function is only valid when CDS is available.
1852 WB_ENTRY(jlong, WB_MetaspaceSharedRegionAlignment(JNIEnv* env, jobject wb))
1853 #if INCLUDE_CDS
1854 return (jlong)MetaspaceShared::core_region_alignment();
1855 #else
1856 ShouldNotReachHere();
1857 return 0L;
1858 #endif
1859 WB_END
1860
1861 WB_ENTRY(jboolean, WB_IsMonitorInflated(JNIEnv* env, jobject wb, jobject obj))
1862 oop obj_oop = JNIHandles::resolve(obj);
1863 return (jboolean) obj_oop->mark().has_monitor();
1864 WB_END
1865
1866 WB_ENTRY(jlong, WB_getInUseMonitorCount(JNIEnv* env, jobject wb))
1867 return (jlong) WhiteBox::get_in_use_monitor_count();
1868 WB_END
1869
1870 WB_ENTRY(jint, WB_getLockStackCapacity(JNIEnv* env))
1871 return (jint) LockStack::CAPACITY;
1872 WB_END
1873
1874 WB_ENTRY(jboolean, WB_supportsRecursiveLightweightLocking(JNIEnv* env))
1875 return (jboolean) VM_Version::supports_recursive_lightweight_locking();
1876 WB_END
1877
1878 WB_ENTRY(jboolean, WB_DeflateIdleMonitors(JNIEnv* env, jobject wb))
1879 log_info(monitorinflation)("WhiteBox initiated DeflateIdleMonitors");
1880 return ObjectSynchronizer::request_deflate_idle_monitors_from_wb();
1881 WB_END
1882
1883 WB_ENTRY(void, WB_ForceSafepoint(JNIEnv* env, jobject wb))
1884 VM_ForceSafepoint force_safepoint_op;
1885 VMThread::execute(&force_safepoint_op);
1886 WB_END
1887
1888 WB_ENTRY(void, WB_ForceClassLoaderStatsSafepoint(JNIEnv* env, jobject wb))
1889 nullStream dev_null;
1890 ClassLoaderStatsVMOperation force_op(&dev_null);
1891 VMThread::execute(&force_op);
1892 WB_END
1893
1894 WB_ENTRY(jlong, WB_GetConstantPool(JNIEnv* env, jobject wb, jclass klass))
1895 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1896 return (jlong) ik->constants();
1897 WB_END
1898
1899 WB_ENTRY(jobjectArray, WB_GetResolvedReferences(JNIEnv* env, jobject wb, jclass klass))
1900 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1901 objArrayOop resolved_refs= ik->constants()->resolved_references();
1902 return (jobjectArray)JNIHandles::make_local(THREAD, resolved_refs);
1903 WB_END
1904
1905 WB_ENTRY(jint, WB_getFieldEntriesLength(JNIEnv* env, jobject wb, jclass klass))
1906 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1907 ConstantPool* cp = ik->constants();
1908 if (cp->cache() == nullptr) {
1909 return -1;
1910 }
1911 return cp->resolved_field_entries_length();
1912 WB_END
1913
1914 WB_ENTRY(jint, WB_getFieldCPIndex(JNIEnv* env, jobject wb, jclass klass, jint index))
1915 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1916 ConstantPool* cp = ik->constants();
1917 if (cp->cache() == nullptr) {
1918 return -1;
1919 }
1920 return cp->resolved_field_entry_at(index)->constant_pool_index();
1921 WB_END
1922
1923 WB_ENTRY(jint, WB_getMethodEntriesLength(JNIEnv* env, jobject wb, jclass klass))
1924 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1925 ConstantPool* cp = ik->constants();
1926 if (cp->cache() == nullptr) {
1927 return -1;
1928 }
1929 return cp->resolved_method_entries_length();
1930 WB_END
1931
1932 WB_ENTRY(jint, WB_getMethodCPIndex(JNIEnv* env, jobject wb, jclass klass, jint index))
1933 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1934 ConstantPool* cp = ik->constants();
1935 if (cp->cache() == nullptr) {
1936 return -1;
1937 }
1938 return cp->resolved_method_entry_at(index)->constant_pool_index();
1939 WB_END
1940
1941 WB_ENTRY(jint, WB_getIndyInfoLength(JNIEnv* env, jobject wb, jclass klass))
1942 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1943 ConstantPool* cp = ik->constants();
1944 if (cp->cache() == nullptr) {
1945 return -1;
1946 }
1947 return cp->resolved_indy_entries_length();
1948 WB_END
1949
1950 WB_ENTRY(jint, WB_getIndyCPIndex(JNIEnv* env, jobject wb, jclass klass, jint index))
1951 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1952 ConstantPool* cp = ik->constants();
1953 if (cp->cache() == nullptr) {
1954 return -1;
1955 }
1956 return cp->resolved_indy_entry_at(index)->constant_pool_index();
1957 WB_END
1958
1959 WB_ENTRY(jobject, WB_printClasses(JNIEnv* env, jobject wb, jstring class_name_pattern, jint flags))
1960 ThreadToNativeFromVM ttnfv(thread);
1961 const char* c = env->GetStringUTFChars(class_name_pattern, nullptr);
1962 ResourceMark rm;
1963 stringStream st;
1964 {
1965 ThreadInVMfromNative ttvfn(thread); // back to VM
1966 ClassPrinter::print_classes(c, flags, &st);
1967 }
1968 jstring result = env->NewStringUTF(st.freeze());
1969 CHECK_JNI_EXCEPTION_(env, nullptr);
1970 return result;
1971 WB_END
1972
1973 WB_ENTRY(jobject, WB_printMethods(JNIEnv* env, jobject wb, jstring class_name_pattern, jstring method_pattern, jint flags))
1974 ThreadToNativeFromVM ttnfv(thread);
1975 const char* c = env->GetStringUTFChars(class_name_pattern, nullptr);
1976 const char* m = env->GetStringUTFChars(method_pattern, nullptr);
1977 ResourceMark rm;
1978 stringStream st;
1979 {
1980 ThreadInVMfromNative ttvfn(thread); // back to VM
1981 ClassPrinter::print_methods(c, m, flags, &st);
1982 }
1983 jstring result = env->NewStringUTF(st.freeze());
1984 CHECK_JNI_EXCEPTION_(env, nullptr);
1985 return result;
1986 WB_END
1987
1988 WB_ENTRY(void, WB_ClearInlineCaches(JNIEnv* env, jobject wb, jboolean preserve_static_stubs))
1989 VM_ClearICs clear_ics(preserve_static_stubs == JNI_TRUE);
1990 VMThread::execute(&clear_ics);
1991 WB_END
1992
1993 template <typename T>
1994 static bool GetMethodOption(JavaThread* thread, JNIEnv* env, jobject method, jstring name, T* value) {
1995 assert(value != nullptr, "sanity");
1996 if (method == nullptr || name == nullptr) {
1997 return false;
1998 }
1999 jmethodID jmid = reflected_method_to_jmid(thread, env, method);
2000 CHECK_JNI_EXCEPTION_(env, false);
2001 methodHandle mh(thread, Method::checked_resolve_jmethod_id(jmid));
2002 // can't be in VM when we call JNI
2003 ThreadToNativeFromVM ttnfv(thread);
2004 const char* flag_name = env->GetStringUTFChars(name, nullptr);
2005 CHECK_JNI_EXCEPTION_(env, false);
2006 CompileCommandEnum option = CompilerOracle::string_to_option(flag_name);
2007 env->ReleaseStringUTFChars(name, flag_name);
2008 if (option == CompileCommandEnum::Unknown) {
2009 return false;
2010 }
2011 if (!CompilerOracle::option_matches_type(option, *value)) {
2012 return false;
2013 }
2014 return CompilerOracle::has_option_value(mh, option, *value);
2015 }
2016
2017 WB_ENTRY(jobject, WB_GetMethodBooleaneOption(JNIEnv* env, jobject wb, jobject method, jstring name))
2018 bool result;
2019 if (GetMethodOption<bool> (thread, env, method, name, &result)) {
2020 // can't be in VM when we call JNI
2021 ThreadToNativeFromVM ttnfv(thread);
2022 return booleanBox(thread, env, result);
2023 }
2024 return nullptr;
2025 WB_END
2026
2027 WB_ENTRY(jobject, WB_GetMethodIntxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
2028 intx result;
2029 if (GetMethodOption <intx> (thread, env, method, name, &result)) {
2030 // can't be in VM when we call JNI
2031 ThreadToNativeFromVM ttnfv(thread);
2032 return longBox(thread, env, result);
2033 }
2034 return nullptr;
2035 WB_END
2036
2037 WB_ENTRY(jobject, WB_GetMethodUintxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
2038 uintx result;
2039 if (GetMethodOption <uintx> (thread, env, method, name, &result)) {
2040 // can't be in VM when we call JNI
2041 ThreadToNativeFromVM ttnfv(thread);
2042 return longBox(thread, env, result);
2043 }
2044 return nullptr;
2045 WB_END
2046
2047 WB_ENTRY(jobject, WB_GetMethodDoubleOption(JNIEnv* env, jobject wb, jobject method, jstring name))
2048 double result;
2049 if (GetMethodOption <double> (thread, env, method, name, &result)) {
2050 // can't be in VM when we call JNI
2051 ThreadToNativeFromVM ttnfv(thread);
2052 return doubleBox(thread, env, result);
2053 }
2054 return nullptr;
2055 WB_END
2056
2057 WB_ENTRY(jobject, WB_GetMethodStringOption(JNIEnv* env, jobject wb, jobject method, jstring name))
2058 ccstr ccstrResult;
2059 if (GetMethodOption <ccstr> (thread, env, method, name, &ccstrResult)) {
2060 // can't be in VM when we call JNI
2061 ThreadToNativeFromVM ttnfv(thread);
2062 jstring result = env->NewStringUTF(ccstrResult);
2063 CHECK_JNI_EXCEPTION_(env, nullptr);
2064 return result;
2065 }
2066 return nullptr;
2067 WB_END
2068
2069 WB_ENTRY(jobject, WB_GetDefaultArchivePath(JNIEnv* env, jobject wb))
2070 const char* p = CDSConfig::default_archive_path();
2071 ThreadToNativeFromVM ttn(thread);
2072 jstring path_string = env->NewStringUTF(p);
2073
2074 CHECK_JNI_EXCEPTION_(env, nullptr);
2075
2076 return path_string;
2077 WB_END
2078
2079 WB_ENTRY(jboolean, WB_IsSharingEnabled(JNIEnv* env, jobject wb))
2080 return CDSConfig::is_using_archive();
2081 WB_END
2082
2083 WB_ENTRY(jint, WB_GetCDSGenericHeaderMinVersion(JNIEnv* env, jobject wb))
2084 #if INCLUDE_CDS
2085 return (jint)CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION;
2086 #else
2087 ShouldNotReachHere();
2088 return (jint)-1;
2089 #endif
2090 WB_END
2091
2092 WB_ENTRY(jint, WB_GetCDSCurrentVersion(JNIEnv* env, jobject wb))
2093 #if INCLUDE_CDS
2094 return (jint)CURRENT_CDS_ARCHIVE_VERSION;
2095 #else
2096 ShouldNotReachHere();
2097 return (jint)-1;
2098 #endif
2099 WB_END
2100
2101 WB_ENTRY(jboolean, WB_CDSMemoryMappingFailed(JNIEnv* env, jobject wb))
2102 return FileMapInfo::memory_mapping_failed();
2103 WB_END
2104
2105 WB_ENTRY(jboolean, WB_IsSharedInternedString(JNIEnv* env, jobject wb, jobject str))
2106 ResourceMark rm(THREAD);
2107 oop str_oop = JNIHandles::resolve(str);
2108 int length;
2109 jchar* chars = java_lang_String::as_unicode_string(str_oop, length, CHECK_(false));
2110 return StringTable::lookup_shared(chars, length) == str_oop;
2111 WB_END
2112
2113 WB_ENTRY(jboolean, WB_IsSharedClass(JNIEnv* env, jobject wb, jclass clazz))
2114 return (jboolean)MetaspaceShared::is_in_shared_metaspace(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
2115 WB_END
2116
2117 WB_ENTRY(jboolean, WB_AreSharedStringsMapped(JNIEnv* env))
2118 return ArchiveHeapLoader::is_mapped();
2119 WB_END
2120
2121 WB_ENTRY(void, WB_LinkClass(JNIEnv* env, jobject wb, jclass clazz))
2122 Klass *k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2123 if (!k->is_instance_klass()) {
2124 return;
2125 }
2126 InstanceKlass *ik = InstanceKlass::cast(k);
2127 ik->link_class(THREAD); // may throw verification error
2128 WB_END
2129
2130 WB_ENTRY(jboolean, WB_AreOpenArchiveHeapObjectsMapped(JNIEnv* env))
2131 return ArchiveHeapLoader::is_mapped();
2132 WB_END
2133
2134 WB_ENTRY(jboolean, WB_IsCDSIncluded(JNIEnv* env))
2135 #if INCLUDE_CDS
2136 // An exploded build inhibits use of CDS. Therefore, for the
2137 // purpose of testing, the JVM can be treated as not having CDS
2138 // built in at all.
2139 return ClassLoader::has_jrt_entry();
2140 #else
2141 return false;
2142 #endif // INCLUDE_CDS
2143 WB_END
2144
2145 WB_ENTRY(jboolean, WB_isC2OrJVMCIIncluded(JNIEnv* env))
2146 #if COMPILER2_OR_JVMCI
2147 return true;
2148 #else
2149 return false;
2150 #endif
2151 WB_END
2152
2153 WB_ENTRY(jboolean, WB_IsJVMCISupportedByGC(JNIEnv* env))
2154 #if INCLUDE_JVMCI
2155 return JVMCIGlobals::gc_supports_jvmci();
2156 #else
2157 return false;
2158 #endif
2159 WB_END
2160
2161 WB_ENTRY(jboolean, WB_CanWriteJavaHeapArchive(JNIEnv* env))
2162 return HeapShared::can_write()
2163 && ArchiveHeapLoader::can_use(); // work-around JDK-8341371
2164 WB_END
2165
2166
2167 WB_ENTRY(jboolean, WB_IsJFRIncluded(JNIEnv* env))
2168 #if INCLUDE_JFR
2169 return true;
2170 #else
2171 return false;
2172 #endif // INCLUDE_JFR
2173 WB_END
2174
2175 WB_ENTRY(jboolean, WB_IsDTraceIncluded(JNIEnv* env))
2176 #if defined(DTRACE_ENABLED)
2177 return true;
2178 #else
2179 return false;
2180 #endif // DTRACE_ENABLED
2181 WB_END
2182
2183 #if INCLUDE_CDS
2184
2185 WB_ENTRY(jint, WB_GetCDSOffsetForName(JNIEnv* env, jobject o, jstring name))
2186 ResourceMark rm;
2187 char* c_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
2188 jint result = (jint)CDSConstants::get_cds_offset(c_name);
2189 return result;
2190 WB_END
2191
2192 WB_ENTRY(jint, WB_GetCDSConstantForName(JNIEnv* env, jobject o, jstring name))
2193 ResourceMark rm;
2194 char* c_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
2195 jint result = (jint)CDSConstants::get_cds_constant(c_name);
2196 return result;
2197 WB_END
2198
2199 #endif // INCLUDE_CDS
2200
2201 WB_ENTRY(jboolean, WB_HandshakeReadMonitors(JNIEnv* env, jobject wb, jobject thread_handle))
2202 class ReadMonitorsClosure : public HandshakeClosure {
2203 jboolean _executed;
2204
2205 void do_thread(Thread* th) {
2206 JavaThread* jt = JavaThread::cast(th);
2207 ResourceMark rm;
2208
2209 GrowableArray<MonitorInfo*>* info = new GrowableArray<MonitorInfo*>();
2210
2211 if (!jt->has_last_Java_frame()) {
2212 return;
2213 }
2214 RegisterMap rmap(jt,
2215 RegisterMap::UpdateMap::include,
2216 RegisterMap::ProcessFrames::include,
2217 RegisterMap::WalkContinuation::skip);
2218 for (javaVFrame* vf = jt->last_java_vframe(&rmap); vf != nullptr; vf = vf->java_sender()) {
2219 GrowableArray<MonitorInfo*> *monitors = vf->monitors();
2220 if (monitors != nullptr) {
2221 int len = monitors->length();
2222 // Walk monitors youngest to oldest
2223 for (int i = len - 1; i >= 0; i--) {
2224 MonitorInfo* mon_info = monitors->at(i);
2225 if (mon_info->eliminated()) continue;
2226 oop owner = mon_info->owner();
2227 if (owner != nullptr) {
2228 info->append(mon_info);
2229 }
2230 }
2231 }
2232 }
2233 _executed = true;
2234 }
2235
2236 public:
2237 ReadMonitorsClosure() : HandshakeClosure("WB_HandshakeReadMonitors"), _executed(false) {}
2238 jboolean executed() const { return _executed; }
2239 };
2240
2241 ReadMonitorsClosure rmc;
2242 if (thread_handle != nullptr) {
2243 ThreadsListHandle tlh;
2244 JavaThread* target = nullptr;
2245 bool is_alive = tlh.cv_internal_thread_to_JavaThread(thread_handle, &target, nullptr);
2246 if (is_alive) {
2247 Handshake::execute(&rmc, &tlh, target);
2248 }
2249 }
2250 return rmc.executed();
2251 WB_END
2252
2253 WB_ENTRY(jint, WB_HandshakeWalkStack(JNIEnv* env, jobject wb, jobject thread_handle, jboolean all_threads))
2254 class TraceSelfClosure : public HandshakeClosure {
2255 jint _num_threads_completed;
2256
2257 void do_thread(Thread* th) {
2258 JavaThread* jt = JavaThread::cast(th);
2259 ResourceMark rm;
2260
2261 jt->print_on(tty);
2262 jt->print_stack_on(tty);
2263 tty->cr();
2264 Atomic::inc(&_num_threads_completed);
2265 }
2266
2267 public:
2268 TraceSelfClosure(Thread* thread) : HandshakeClosure("WB_TraceSelf"), _num_threads_completed(0) {}
2269
2270 jint num_threads_completed() const { return _num_threads_completed; }
2271 };
2272 TraceSelfClosure tsc(Thread::current());
2273
2274 if (all_threads) {
2275 Handshake::execute(&tsc);
2276 } else if (thread_handle != nullptr) {
2277 ThreadsListHandle tlh;
2278 JavaThread* target = nullptr;
2279 bool is_alive = tlh.cv_internal_thread_to_JavaThread(thread_handle, &target, nullptr);
2280 if (is_alive) {
2281 Handshake::execute(&tsc, &tlh, target);
2282 }
2283 }
2284 return tsc.num_threads_completed();
2285 WB_END
2286
2287 WB_ENTRY(void, WB_AsyncHandshakeWalkStack(JNIEnv* env, jobject wb, jobject thread_handle))
2288 class TraceSelfClosure : public AsyncHandshakeClosure {
2289 JavaThread* _self;
2290 void do_thread(Thread* th) {
2291 assert(th->is_Java_thread(), "sanity");
2292 // AsynchHandshake handshakes are only executed by target.
2293 assert(_self == th, "Must be");
2294 assert(Thread::current() == th, "Must be");
2295 JavaThread* jt = JavaThread::cast(th);
2296 ResourceMark rm;
2297 jt->print_on(tty);
2298 jt->print_stack_on(tty);
2299 tty->cr();
2300 }
2301
2302 public:
2303 TraceSelfClosure(JavaThread* self_target) : AsyncHandshakeClosure("WB_TraceSelf"), _self(self_target) {}
2304 };
2305 if (thread_handle != nullptr) {
2306 ThreadsListHandle tlh;
2307 JavaThread* target = nullptr;
2308 bool is_alive = tlh.cv_internal_thread_to_JavaThread(thread_handle, &target, nullptr);
2309 if (is_alive) {
2310 TraceSelfClosure* tsc = new TraceSelfClosure(target);
2311 Handshake::execute(tsc, target);
2312 }
2313 }
2314 WB_END
2315
2316 static volatile int _emulated_lock = 0;
2317
2318 WB_ENTRY(void, WB_LockAndBlock(JNIEnv* env, jobject wb, jboolean suspender))
2319 JavaThread* self = JavaThread::current();
2320
2321 {
2322 // Before trying to acquire the lock transition into a safepoint safe state.
2323 // Otherwise if either suspender or suspendee blocks for a safepoint
2324 // in ~ThreadBlockInVM the other one could loop forever trying to acquire
2325 // the lock without allowing the safepoint to progress.
2326 ThreadBlockInVM tbivm(self);
2327
2328 // We will deadlock here if we are 'suspender' and 'suspendee'
2329 // suspended in ~ThreadBlockInVM. This verifies we only suspend
2330 // at the right place.
2331 while (Atomic::cmpxchg(&_emulated_lock, 0, 1) != 0) {}
2332 assert(_emulated_lock == 1, "Must be locked");
2333
2334 // Sleep much longer in suspendee to force situation where
2335 // 'suspender' is waiting above to acquire lock.
2336 os::naked_short_sleep(suspender ? 1 : 10);
2337 }
2338 Atomic::store(&_emulated_lock, 0);
2339 WB_END
2340
2341 // Some convenience methods to deal with objects from java
2342 int WhiteBox::offset_for_field(const char* field_name, oop object,
2343 Symbol* signature_symbol) {
2344 assert(field_name != nullptr && strlen(field_name) > 0, "Field name not valid");
2345
2346 //Get the class of our object
2347 Klass* arg_klass = object->klass();
2348 //Turn it into an instance-klass
2349 InstanceKlass* ik = InstanceKlass::cast(arg_klass);
2350
2351 //Create symbols to look for in the class
2352 TempNewSymbol name_symbol = SymbolTable::new_symbol(field_name);
2353
2354 //To be filled in with an offset of the field we're looking for
2355 fieldDescriptor fd;
2356
2357 Klass* res = ik->find_field(name_symbol, signature_symbol, &fd);
2358 if (res == nullptr) {
2359 tty->print_cr("Invalid layout of %s at %s", ik->external_name(),
2360 name_symbol->as_C_string());
2361 vm_exit_during_initialization("Invalid layout of preloaded class: use -Xlog:class+load=info to see the origin of the problem class");
2362 }
2363
2364 //fetch the field at the offset we've found
2365 int dest_offset = fd.offset();
2366
2367 return dest_offset;
2368 }
2369
2370
2371 const char* WhiteBox::lookup_jstring(const char* field_name, oop object) {
2372 int offset = offset_for_field(field_name, object,
2373 vmSymbols::string_signature());
2374 oop string = object->obj_field(offset);
2375 if (string == nullptr) {
2376 return nullptr;
2377 }
2378 const char* ret = java_lang_String::as_utf8_string(string);
2379 return ret;
2380 }
2381
2382 bool WhiteBox::lookup_bool(const char* field_name, oop object) {
2383 int offset =
2384 offset_for_field(field_name, object, vmSymbols::bool_signature());
2385 bool ret = (object->bool_field(offset) == JNI_TRUE);
2386 return ret;
2387 }
2388
2389 void WhiteBox::register_methods(JNIEnv* env, jclass wbclass, JavaThread* thread, JNINativeMethod* method_array, int method_count) {
2390 ResourceMark rm;
2391 Klass* klass = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(wbclass));
2392 const char* klass_name = klass->external_name();
2393
2394 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
2395
2396 // one by one registration natives for exception catching
2397 jclass no_such_method_error_klass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string());
2398 CHECK_JNI_EXCEPTION(env);
2399 for (int i = 0, n = method_count; i < n; ++i) {
2400 // Skip dummy entries
2401 if (method_array[i].fnPtr == nullptr) continue;
2402 if (env->RegisterNatives(wbclass, &method_array[i], 1) != 0) {
2403 jthrowable throwable_obj = env->ExceptionOccurred();
2404 if (throwable_obj != nullptr) {
2405 env->ExceptionClear();
2406 if (env->IsInstanceOf(throwable_obj, no_such_method_error_klass)) {
2407 // NoSuchMethodError is thrown when a method can't be found or a method is not native.
2408 // Ignoring the exception since it is not preventing use of other WhiteBox methods.
2409 tty->print_cr("Warning: 'NoSuchMethodError' on register of %s::%s%s",
2410 klass_name, method_array[i].name, method_array[i].signature);
2411 }
2412 } else {
2413 // Registration failed unexpectedly.
2414 tty->print_cr("Warning: unexpected error on register of %s::%s%s. All methods will be unregistered",
2415 klass_name, method_array[i].name, method_array[i].signature);
2416 env->UnregisterNatives(wbclass);
2417 break;
2418 }
2419 }
2420 }
2421 }
2422
2423 WB_ENTRY(jint, WB_AddCompilerDirective(JNIEnv* env, jobject o, jstring compDirect))
2424 // can't be in VM when we call JNI
2425 ThreadToNativeFromVM ttnfv(thread);
2426 const char* dir = env->GetStringUTFChars(compDirect, nullptr);
2427 CHECK_JNI_EXCEPTION_(env, 0);
2428 int ret;
2429 {
2430 ThreadInVMfromNative ttvfn(thread); // back to VM
2431 ret = DirectivesParser::parse_string(dir, tty);
2432 }
2433 env->ReleaseStringUTFChars(compDirect, dir);
2434 // -1 for error parsing directive. Return 0 as number of directives added.
2435 if (ret == -1) {
2436 ret = 0;
2437 }
2438 return (jint) ret;
2439 WB_END
2440
2441 WB_ENTRY(void, WB_RemoveCompilerDirective(JNIEnv* env, jobject o, jint count))
2442 DirectivesStack::pop(count);
2443 WB_END
2444
2445 // Checks that the library libfile has the noexecstack bit set.
2446 WB_ENTRY(jboolean, WB_CheckLibSpecifiesNoexecstack(JNIEnv* env, jobject o, jstring libfile))
2447 jboolean ret = false;
2448 #ifdef LINUX
2449 // Can't be in VM when we call JNI.
2450 ThreadToNativeFromVM ttnfv(thread);
2451 const char* lf = env->GetStringUTFChars(libfile, nullptr);
2452 CHECK_JNI_EXCEPTION_(env, 0);
2453 ret = (jboolean) ElfFile::specifies_noexecstack(lf);
2454 env->ReleaseStringUTFChars(libfile, lf);
2455 #endif
2456 return ret;
2457 WB_END
2458
2459 WB_ENTRY(jboolean, WB_IsContainerized(JNIEnv* env, jobject o))
2460 LINUX_ONLY(return OSContainer::is_containerized();)
2461 return false;
2462 WB_END
2463
2464 // Physical memory of the host machine (including containers)
2465 WB_ENTRY(jlong, WB_HostPhysicalMemory(JNIEnv* env, jobject o))
2466 LINUX_ONLY(return os::Linux::physical_memory();)
2467 return os::physical_memory();
2468 WB_END
2469
2470 // Physical swap of the host machine (including containers), Linux only.
2471 WB_ENTRY(jlong, WB_HostPhysicalSwap(JNIEnv* env, jobject o))
2472 LINUX_ONLY(return (jlong)os::Linux::host_swap();)
2473 return -1; // Not used/implemented on other platforms
2474 WB_END
2475
2476 WB_ENTRY(jint, WB_ValidateCgroup(JNIEnv* env,
2477 jobject o,
2478 jstring proc_cgroups,
2479 jstring proc_self_cgroup,
2480 jstring proc_self_mountinfo))
2481 jint ret = 0;
2482 #ifdef LINUX
2483 ThreadToNativeFromVM ttnfv(thread);
2484 const char* p_cgroups = env->GetStringUTFChars(proc_cgroups, nullptr);
2485 CHECK_JNI_EXCEPTION_(env, 0);
2486 const char* p_s_cgroup = env->GetStringUTFChars(proc_self_cgroup, nullptr);
2487 CHECK_JNI_EXCEPTION_(env, 0);
2488 const char* p_s_mountinfo = env->GetStringUTFChars(proc_self_mountinfo, nullptr);
2489 CHECK_JNI_EXCEPTION_(env, 0);
2490 u1 cg_type_flags = 0;
2491 // This sets cg_type_flags
2492 WhiteBox::validate_cgroup(p_cgroups, p_s_cgroup, p_s_mountinfo, &cg_type_flags);
2493 ret = (jint)cg_type_flags;
2494 env->ReleaseStringUTFChars(proc_cgroups, p_cgroups);
2495 env->ReleaseStringUTFChars(proc_self_cgroup, p_s_cgroup);
2496 env->ReleaseStringUTFChars(proc_self_mountinfo, p_s_mountinfo);
2497 #endif
2498 return ret;
2499 WB_END
2500
2501 // Available cpus of the host machine, Linux only.
2502 // Used in container testing.
2503 WB_ENTRY(jint, WB_HostCPUs(JNIEnv* env, jobject o))
2504 LINUX_ONLY(return os::Linux::active_processor_count();)
2505 return -1; // Not used/implemented on other platforms
2506 WB_END
2507
2508 WB_ENTRY(void, WB_PrintOsInfo(JNIEnv* env, jobject o))
2509 os::print_os_info(tty);
2510 WB_END
2511
2512 // Elf decoder
2513 WB_ENTRY(void, WB_DisableElfSectionCache(JNIEnv* env))
2514 #if !defined(_WINDOWS) && !defined(__APPLE__) && !defined(_AIX)
2515 ElfFile::_do_not_cache_elf_section = true;
2516 #endif
2517 WB_END
2518
2519 WB_ENTRY(jlong, WB_ResolvedMethodItemsCount(JNIEnv* env, jobject o))
2520 return (jlong) ResolvedMethodTable::items_count();
2521 WB_END
2522
2523 WB_ENTRY(jint, WB_ProtectionDomainRemovedCount(JNIEnv* env, jobject o))
2524 return (jint) ProtectionDomainCacheTable::removed_entries_count();
2525 WB_END
2526
2527 WB_ENTRY(jint, WB_GetKlassMetadataSize(JNIEnv* env, jobject wb, jclass mirror))
2528 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(mirror));
2529 // Return size in bytes.
2530 return k->size() * wordSize;
2531 WB_END
2532
2533 // See test/hotspot/jtreg/runtime/Thread/ThreadObjAccessAtExit.java.
2534 // It explains how the thread's priority field is used for test state coordination.
2535 //
2536 WB_ENTRY(void, WB_CheckThreadObjOfTerminatingThread(JNIEnv* env, jobject wb, jobject target_handle))
2537 oop target_oop = JNIHandles::resolve_non_null(target_handle);
2538 jlong tid = java_lang_Thread::thread_id(target_oop);
2539 JavaThread* target = java_lang_Thread::thread(target_oop);
2540
2541 // Grab a ThreadsListHandle to protect the target thread whilst terminating
2542 ThreadsListHandle tlh;
2543
2544 // Look up the target thread by tid to ensure it is present
2545 JavaThread* t = tlh.list()->find_JavaThread_from_java_tid(tid);
2546 if (t == nullptr) {
2547 THROW_MSG(vmSymbols::java_lang_RuntimeException(), "Target thread not found in ThreadsList!");
2548 }
2549
2550 tty->print_cr("WB_CheckThreadObjOfTerminatingThread: target thread is protected");
2551 // Allow target to terminate by boosting priority
2552 java_lang_Thread::set_priority(t->threadObj(), ThreadPriority(NormPriority + 1));
2553
2554 // Now wait for the target to terminate
2555 while (!target->is_terminated()) {
2556 ThreadBlockInVM tbivm(thread); // just in case target is involved in a safepoint
2557 os::naked_short_sleep(0);
2558 }
2559
2560 tty->print_cr("WB_CheckThreadObjOfTerminatingThread: target thread is terminated");
2561
2562 // Now release the GC inducing thread - we have to re-resolve the external oop that
2563 // was passed in as GC may have occurred and we don't know if we can trust t->threadObj() now.
2564 oop original = JNIHandles::resolve_non_null(target_handle);
2565 java_lang_Thread::set_priority(original, ThreadPriority(NormPriority + 2));
2566
2567 tty->print_cr("WB_CheckThreadObjOfTerminatingThread: GC has been initiated - checking threadObj:");
2568
2569 // The Java code should be creating garbage and triggering GC, which would potentially move
2570 // the threadObj oop. If the exiting thread is properly protected then its threadObj should
2571 // remain valid and equal to our initial target_handle. Loop a few times to give GC a chance to
2572 // kick in.
2573 for (int i = 0; i < 5; i++) {
2574 oop original = JNIHandles::resolve_non_null(target_handle);
2575 oop current = t->threadObj();
2576 if (original != current) {
2577 tty->print_cr("WB_CheckThreadObjOfTerminatingThread: failed comparison on iteration %d", i);
2578 THROW_MSG(vmSymbols::java_lang_RuntimeException(), "Target thread oop has changed!");
2579 } else {
2580 tty->print_cr("WB_CheckThreadObjOfTerminatingThread: successful comparison on iteration %d", i);
2581 ThreadBlockInVM tbivm(thread);
2582 os::naked_short_sleep(50);
2583 }
2584 }
2585 WB_END
2586
2587 WB_ENTRY(void, WB_VerifyFrames(JNIEnv* env, jobject wb, jboolean log, jboolean update_map))
2588 ResourceMark rm; // for verify
2589 stringStream st;
2590 for (StackFrameStream fst(JavaThread::current(), update_map, true); !fst.is_done(); fst.next()) {
2591 frame* current_frame = fst.current();
2592 if (log) {
2593 current_frame->print_value_on(&st);
2594 }
2595 current_frame->verify(fst.register_map());
2596 }
2597 if (log) {
2598 tty->print_cr("[WhiteBox::VerifyFrames] Walking Frames");
2599 tty->print_raw(st.freeze());
2600 tty->print_cr("[WhiteBox::VerifyFrames] Done");
2601 }
2602 WB_END
2603
2604 WB_ENTRY(jboolean, WB_IsJVMTIIncluded(JNIEnv* env, jobject wb))
2605 #if INCLUDE_JVMTI
2606 return JNI_TRUE;
2607 #else
2608 return JNI_FALSE;
2609 #endif
2610 WB_END
2611
2612 WB_ENTRY(void, WB_WaitUnsafe(JNIEnv* env, jobject wb, jint time))
2613 os::naked_short_sleep(time);
2614 WB_END
2615
2616 WB_ENTRY(jstring, WB_GetLibcName(JNIEnv* env, jobject o))
2617 ThreadToNativeFromVM ttn(thread);
2618 jstring info_string = env->NewStringUTF(XSTR(LIBC));
2619 CHECK_JNI_EXCEPTION_(env, nullptr);
2620 return info_string;
2621 WB_END
2622
2623 WB_ENTRY(void, WB_LockCritical(JNIEnv* env, jobject wb))
2624 GCLocker::lock_critical(thread);
2625 WB_END
2626
2627 WB_ENTRY(void, WB_UnlockCritical(JNIEnv* env, jobject wb))
2628 GCLocker::unlock_critical(thread);
2629 WB_END
2630
2631 WB_ENTRY(void, WB_PinObject(JNIEnv* env, jobject wb, jobject o))
2632 #if INCLUDE_G1GC
2633 if (!UseG1GC) {
2634 ShouldNotReachHere();
2635 return;
2636 }
2637 oop obj = JNIHandles::resolve(o);
2638 G1CollectedHeap::heap()->pin_object(thread, obj);
2639 #else
2640 ShouldNotReachHere();
2641 #endif // INCLUDE_G1GC
2642 WB_END
2643
2644 WB_ENTRY(void, WB_UnpinObject(JNIEnv* env, jobject wb, jobject o))
2645 #if INCLUDE_G1GC
2646 if (!UseG1GC) {
2647 ShouldNotReachHere();
2648 return;
2649 }
2650 oop obj = JNIHandles::resolve(o);
2651 G1CollectedHeap::heap()->unpin_object(thread, obj);
2652 #else
2653 ShouldNotReachHere();
2654 #endif // INCLUDE_G1GC
2655 WB_END
2656
2657 WB_ENTRY(jboolean, WB_SetVirtualThreadsNotifyJvmtiMode(JNIEnv* env, jobject wb, jboolean enable))
2658 if (!Continuations::enabled()) {
2659 tty->print_cr("WB error: must be Continuations::enabled()!");
2660 return JNI_FALSE;
2661 }
2662 jboolean result = false;
2663 #if INCLUDE_JVMTI
2664 if (enable) {
2665 result = JvmtiEnvBase::enable_virtual_threads_notify_jvmti();
2666 } else {
2667 result = JvmtiEnvBase::disable_virtual_threads_notify_jvmti();
2668 }
2669 #endif
2670 return result;
2671 WB_END
2672
2673 WB_ENTRY(void, WB_PreTouchMemory(JNIEnv* env, jobject wb, jlong addr, jlong size))
2674 void* const from = (void*)addr;
2675 void* const to = (void*)(addr + size);
2676 if (from > to) {
2677 os::pretouch_memory(from, to, os::vm_page_size());
2678 }
2679 WB_END
2680
2681 WB_ENTRY(void, WB_CleanMetaspaces(JNIEnv* env, jobject target))
2682 ClassLoaderDataGraph::safepoint_and_clean_metaspaces();
2683 WB_END
2684
2685 // Reports resident set size (RSS) in bytes
2686 WB_ENTRY(jlong, WB_Rss(JNIEnv* env, jobject o))
2687 return os::rss();
2688 WB_END
2689
2690 #define CC (char*)
2691
2692 static JNINativeMethod methods[] = {
2693 {CC"getObjectAddress0", CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress },
2694 {CC"getObjectSize0", CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectSize },
2695 {CC"isObjectInOldGen0", CC"(Ljava/lang/Object;)Z", (void*)&WB_isObjectInOldGen },
2696 {CC"getHeapOopSize", CC"()I", (void*)&WB_GetHeapOopSize },
2697 {CC"getVMPageSize", CC"()I", (void*)&WB_GetVMPageSize },
2698 {CC"getVMAllocationGranularity", CC"()J", (void*)&WB_GetVMAllocationGranularity },
2699 {CC"getVMLargePageSize", CC"()J", (void*)&WB_GetVMLargePageSize},
2700 {CC"getHeapSpaceAlignment", CC"()J", (void*)&WB_GetHeapSpaceAlignment},
2701 {CC"getHeapAlignment", CC"()J", (void*)&WB_GetHeapAlignment},
2702 {CC"countAliveClasses0", CC"(Ljava/lang/String;)I", (void*)&WB_CountAliveClasses },
2703 {CC"getSymbolRefcount", CC"(Ljava/lang/String;)I", (void*)&WB_GetSymbolRefcount },
2704 {CC"parseCommandLine0",
2705 CC"(Ljava/lang/String;C[Ljdk/test/whitebox/parser/DiagnosticCommand;)[Ljava/lang/Object;",
2706 (void*) &WB_ParseCommandLine
2707 },
2708 {CC"addToBootstrapClassLoaderSearch0", CC"(Ljava/lang/String;)V",
2709 (void*)&WB_AddToBootstrapClassLoaderSearch},
2710 {CC"addToSystemClassLoaderSearch0", CC"(Ljava/lang/String;)V",
2711 (void*)&WB_AddToSystemClassLoaderSearch},
2712 {CC"getCompressedOopsMaxHeapSize", CC"()J",
2713 (void*)&WB_GetCompressedOopsMaxHeapSize},
2714 {CC"printHeapSizes", CC"()V", (void*)&WB_PrintHeapSizes },
2715 {CC"readFromNoaccessArea",CC"()V", (void*)&WB_ReadFromNoaccessArea},
2716 {CC"stressVirtualSpaceResize",CC"(JJJ)I", (void*)&WB_StressVirtualSpaceResize},
2717 #if INCLUDE_CDS
2718 {CC"getCDSOffsetForName0", CC"(Ljava/lang/String;)I", (void*)&WB_GetCDSOffsetForName},
2719 {CC"getCDSConstantForName0", CC"(Ljava/lang/String;)I", (void*)&WB_GetCDSConstantForName},
2720 #endif
2721 #if INCLUDE_G1GC
2722 {CC"g1InConcurrentMark", CC"()Z", (void*)&WB_G1InConcurrentMark},
2723 {CC"g1CompletedConcurrentMarkCycles", CC"()I", (void*)&WB_G1CompletedConcurrentMarkCycles},
2724 {CC"g1IsHumongous0", CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous },
2725 {CC"g1BelongsToHumongousRegion0", CC"(J)Z", (void*)&WB_G1BelongsToHumongousRegion},
2726 {CC"g1BelongsToFreeRegion0", CC"(J)Z", (void*)&WB_G1BelongsToFreeRegion},
2727 {CC"g1NumMaxRegions", CC"()J", (void*)&WB_G1NumMaxRegions },
2728 {CC"g1NumFreeRegions", CC"()J", (void*)&WB_G1NumFreeRegions },
2729 {CC"g1RegionSize", CC"()I", (void*)&WB_G1RegionSize },
2730 {CC"g1HasRegionsToUncommit", CC"()Z", (void*)&WB_G1HasRegionsToUncommit},
2731 {CC"g1AuxiliaryMemoryUsage", CC"()Ljava/lang/management/MemoryUsage;",
2732 (void*)&WB_G1AuxiliaryMemoryUsage },
2733 {CC"g1ActiveMemoryNodeCount", CC"()I", (void*)&WB_G1ActiveMemoryNodeCount },
2734 {CC"g1MemoryNodeIds", CC"()[I", (void*)&WB_G1MemoryNodeIds },
2735 {CC"g1GetMixedGCInfo", CC"(I)[J", (void*)&WB_G1GetMixedGCInfo },
2736 #endif // INCLUDE_G1GC
2737 #if INCLUDE_PARALLELGC
2738 {CC"psVirtualSpaceAlignment",CC"()J", (void*)&WB_PSVirtualSpaceAlignment},
2739 {CC"psHeapGenerationAlignment",CC"()J", (void*)&WB_PSHeapGenerationAlignment},
2740 #endif
2741 {CC"NMTMalloc", CC"(J)J", (void*)&WB_NMTMalloc },
2742 {CC"NMTMallocWithPseudoStack", CC"(JI)J", (void*)&WB_NMTMallocWithPseudoStack},
2743 {CC"NMTMallocWithPseudoStackAndType", CC"(JII)J", (void*)&WB_NMTMallocWithPseudoStackAndType},
2744 {CC"NMTFree", CC"(J)V", (void*)&WB_NMTFree },
2745 {CC"NMTReserveMemory", CC"(J)J", (void*)&WB_NMTReserveMemory },
2746 {CC"NMTAttemptReserveMemoryAt", CC"(JJ)J", (void*)&WB_NMTAttemptReserveMemoryAt },
2747 {CC"NMTCommitMemory", CC"(JJ)V", (void*)&WB_NMTCommitMemory },
2748 {CC"NMTUncommitMemory", CC"(JJ)V", (void*)&WB_NMTUncommitMemory },
2749 {CC"NMTReleaseMemory", CC"(JJ)V", (void*)&WB_NMTReleaseMemory },
2750 {CC"NMTGetHashSize", CC"()I", (void*)&WB_NMTGetHashSize },
2751 {CC"NMTNewArena", CC"(J)J", (void*)&WB_NMTNewArena },
2752 {CC"NMTFreeArena", CC"(J)V", (void*)&WB_NMTFreeArena },
2753 {CC"NMTArenaMalloc", CC"(JJ)V", (void*)&WB_NMTArenaMalloc },
2754 {CC"deoptimizeFrames", CC"(Z)I", (void*)&WB_DeoptimizeFrames },
2755 {CC"isFrameDeoptimized", CC"(I)Z", (void*)&WB_IsFrameDeoptimized},
2756 {CC"deoptimizeAll", CC"()V", (void*)&WB_DeoptimizeAll },
2757 {CC"deoptimizeMethod0", CC"(Ljava/lang/reflect/Executable;Z)I",
2758 (void*)&WB_DeoptimizeMethod },
2759 {CC"isMethodCompiled0", CC"(Ljava/lang/reflect/Executable;Z)Z",
2760 (void*)&WB_IsMethodCompiled },
2761 {CC"isMethodCompilable0", CC"(Ljava/lang/reflect/Executable;IZ)Z",
2762 (void*)&WB_IsMethodCompilable},
2763 {CC"isMethodQueuedForCompilation0",
2764 CC"(Ljava/lang/reflect/Executable;)Z", (void*)&WB_IsMethodQueuedForCompilation},
2765 {CC"isIntrinsicAvailable0",
2766 CC"(Ljava/lang/reflect/Executable;Ljava/lang/reflect/Executable;I)Z",
2767 (void*)&WB_IsIntrinsicAvailable},
2768 {CC"makeMethodNotCompilable0",
2769 CC"(Ljava/lang/reflect/Executable;IZ)V", (void*)&WB_MakeMethodNotCompilable},
2770 {CC"testSetDontInlineMethod0",
2771 CC"(Ljava/lang/reflect/Executable;Z)Z", (void*)&WB_TestSetDontInlineMethod},
2772 {CC"getMethodCompilationLevel0",
2773 CC"(Ljava/lang/reflect/Executable;Z)I", (void*)&WB_GetMethodCompilationLevel},
2774 {CC"getMethodDecompileCount0",
2775 CC"(Ljava/lang/reflect/Executable;)I", (void*)&WB_GetMethodDecompileCount},
2776 {CC"getMethodTrapCount0",
2777 CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)I",
2778 (void*)&WB_GetMethodTrapCount},
2779 {CC"getDeoptCount0",
2780 CC"(Ljava/lang/String;Ljava/lang/String;)I", (void*)&WB_GetDeoptCount},
2781 {CC"getMethodEntryBci0",
2782 CC"(Ljava/lang/reflect/Executable;)I", (void*)&WB_GetMethodEntryBci},
2783 {CC"getCompileQueueSize",
2784 CC"(I)I", (void*)&WB_GetCompileQueueSize},
2785 {CC"testSetForceInlineMethod0",
2786 CC"(Ljava/lang/reflect/Executable;Z)Z", (void*)&WB_TestSetForceInlineMethod},
2787 {CC"enqueueMethodForCompilation0",
2788 CC"(Ljava/lang/reflect/Executable;II)Z", (void*)&WB_EnqueueMethodForCompilation},
2789 {CC"enqueueInitializerForCompilation0",
2790 CC"(Ljava/lang/Class;I)Z", (void*)&WB_EnqueueInitializerForCompilation},
2791 {CC"markMethodProfiled",
2792 CC"(Ljava/lang/reflect/Executable;)V", (void*)&WB_MarkMethodProfiled},
2793 {CC"clearMethodState0",
2794 CC"(Ljava/lang/reflect/Executable;)V", (void*)&WB_ClearMethodState},
2795 {CC"lockCompilation", CC"()V", (void*)&WB_LockCompilation},
2796 {CC"unlockCompilation", CC"()V", (void*)&WB_UnlockCompilation},
2797 {CC"matchesMethod",
2798 CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)I",
2799 (void*)&WB_MatchesMethod},
2800 {CC"matchesInline",
2801 CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)I",
2802 (void*)&WB_MatchesInline},
2803 {CC"shouldPrintAssembly",
2804 CC"(Ljava/lang/reflect/Executable;I)Z",
2805 (void*)&WB_ShouldPrintAssembly},
2806
2807 {CC"isConstantVMFlag", CC"(Ljava/lang/String;)Z", (void*)&WB_IsConstantVMFlag},
2808 {CC"isDefaultVMFlag", CC"(Ljava/lang/String;)Z", (void*)&WB_IsDefaultVMFlag},
2809 {CC"isLockedVMFlag", CC"(Ljava/lang/String;)Z", (void*)&WB_IsLockedVMFlag},
2810 {CC"setBooleanVMFlag", CC"(Ljava/lang/String;Z)V",(void*)&WB_SetBooleanVMFlag},
2811 {CC"setIntVMFlag", CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntVMFlag},
2812 {CC"setUintVMFlag", CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintVMFlag},
2813 {CC"setIntxVMFlag", CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntxVMFlag},
2814 {CC"setUintxVMFlag", CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintxVMFlag},
2815 {CC"setUint64VMFlag", CC"(Ljava/lang/String;J)V",(void*)&WB_SetUint64VMFlag},
2816 {CC"setSizeTVMFlag", CC"(Ljava/lang/String;J)V",(void*)&WB_SetSizeTVMFlag},
2817 {CC"setDoubleVMFlag", CC"(Ljava/lang/String;D)V",(void*)&WB_SetDoubleVMFlag},
2818 {CC"setStringVMFlag", CC"(Ljava/lang/String;Ljava/lang/String;)V",
2819 (void*)&WB_SetStringVMFlag},
2820 {CC"getBooleanVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Boolean;",
2821 (void*)&WB_GetBooleanVMFlag},
2822 {CC"getIntVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Long;",
2823 (void*)&WB_GetIntVMFlag},
2824 {CC"getUintVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Long;",
2825 (void*)&WB_GetUintVMFlag},
2826 {CC"getIntxVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Long;",
2827 (void*)&WB_GetIntxVMFlag},
2828 {CC"getUintxVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Long;",
2829 (void*)&WB_GetUintxVMFlag},
2830 {CC"getUint64VMFlag", CC"(Ljava/lang/String;)Ljava/lang/Long;",
2831 (void*)&WB_GetUint64VMFlag},
2832 {CC"getSizeTVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Long;",
2833 (void*)&WB_GetSizeTVMFlag},
2834 {CC"getDoubleVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Double;",
2835 (void*)&WB_GetDoubleVMFlag},
2836 {CC"getStringVMFlag", CC"(Ljava/lang/String;)Ljava/lang/String;",
2837 (void*)&WB_GetStringVMFlag},
2838 {CC"isInStringTable", CC"(Ljava/lang/String;)Z", (void*)&WB_IsInStringTable },
2839 {CC"fullGC", CC"()V", (void*)&WB_FullGC },
2840 {CC"youngGC", CC"()V", (void*)&WB_YoungGC },
2841 {CC"readReservedMemory", CC"()V", (void*)&WB_ReadReservedMemory },
2842 {CC"allocateMetaspace",
2843 CC"(Ljava/lang/ClassLoader;J)J", (void*)&WB_AllocateMetaspace },
2844 {CC"incMetaspaceCapacityUntilGC", CC"(J)J", (void*)&WB_IncMetaspaceCapacityUntilGC },
2845 {CC"metaspaceCapacityUntilGC", CC"()J", (void*)&WB_MetaspaceCapacityUntilGC },
2846 {CC"metaspaceSharedRegionAlignment", CC"()J", (void*)&WB_MetaspaceSharedRegionAlignment },
2847 {CC"getCPUFeatures", CC"()Ljava/lang/String;", (void*)&WB_GetCPUFeatures },
2848 {CC"getNMethod0", CC"(Ljava/lang/reflect/Executable;Z)[Ljava/lang/Object;",
2849 (void*)&WB_GetNMethod },
2850 {CC"allocateCodeBlob", CC"(II)J", (void*)&WB_AllocateCodeBlob },
2851 {CC"freeCodeBlob", CC"(J)V", (void*)&WB_FreeCodeBlob },
2852 {CC"getCodeHeapEntries", CC"(I)[Ljava/lang/Object;",(void*)&WB_GetCodeHeapEntries },
2853 {CC"getCompilationActivityMode",
2854 CC"()I", (void*)&WB_GetCompilationActivityMode},
2855 {CC"getMethodData0", CC"(Ljava/lang/reflect/Executable;)J",
2856 (void*)&WB_GetMethodData },
2857 {CC"getCodeBlob", CC"(J)[Ljava/lang/Object;",(void*)&WB_GetCodeBlob },
2858 {CC"getThreadStackSize", CC"()J", (void*)&WB_GetThreadStackSize },
2859 {CC"getThreadRemainingStackSize", CC"()J", (void*)&WB_GetThreadRemainingStackSize },
2860 {CC"DefineModule", CC"(Ljava/lang/Object;ZLjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V",
2861 (void*)&WB_DefineModule },
2862 {CC"AddModuleExports", CC"(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V",
2863 (void*)&WB_AddModuleExports },
2864 {CC"AddReadsModule", CC"(Ljava/lang/Object;Ljava/lang/Object;)V",
2865 (void*)&WB_AddReadsModule },
2866 {CC"AddModuleExportsToAllUnnamed", CC"(Ljava/lang/Object;Ljava/lang/String;)V",
2867 (void*)&WB_AddModuleExportsToAllUnnamed },
2868 {CC"AddModuleExportsToAll", CC"(Ljava/lang/Object;Ljava/lang/String;)V",
2869 (void*)&WB_AddModuleExportsToAll },
2870 {CC"deflateIdleMonitors", CC"()Z", (void*)&WB_DeflateIdleMonitors },
2871 {CC"isMonitorInflated0", CC"(Ljava/lang/Object;)Z", (void*)&WB_IsMonitorInflated },
2872 {CC"getInUseMonitorCount", CC"()J", (void*)&WB_getInUseMonitorCount },
2873 {CC"getLockStackCapacity", CC"()I", (void*)&WB_getLockStackCapacity },
2874 {CC"supportsRecursiveLightweightLocking", CC"()Z", (void*)&WB_supportsRecursiveLightweightLocking },
2875 {CC"forceSafepoint", CC"()V", (void*)&WB_ForceSafepoint },
2876 {CC"forceClassLoaderStatsSafepoint", CC"()V", (void*)&WB_ForceClassLoaderStatsSafepoint },
2877 {CC"getConstantPool0", CC"(Ljava/lang/Class;)J", (void*)&WB_GetConstantPool },
2878 {CC"getResolvedReferences0", CC"(Ljava/lang/Class;)[Ljava/lang/Object;", (void*)&WB_GetResolvedReferences},
2879 {CC"getFieldEntriesLength0", CC"(Ljava/lang/Class;)I", (void*)&WB_getFieldEntriesLength},
2880 {CC"getFieldCPIndex0", CC"(Ljava/lang/Class;I)I", (void*)&WB_getFieldCPIndex},
2881 {CC"getMethodEntriesLength0", CC"(Ljava/lang/Class;)I", (void*)&WB_getMethodEntriesLength},
2882 {CC"getMethodCPIndex0", CC"(Ljava/lang/Class;I)I", (void*)&WB_getMethodCPIndex},
2883 {CC"getIndyInfoLength0", CC"(Ljava/lang/Class;)I", (void*)&WB_getIndyInfoLength},
2884 {CC"getIndyCPIndex0", CC"(Ljava/lang/Class;I)I", (void*)&WB_getIndyCPIndex},
2885 {CC"printClasses0", CC"(Ljava/lang/String;I)Ljava/lang/String;", (void*)&WB_printClasses},
2886 {CC"printMethods0", CC"(Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/String;", (void*)&WB_printMethods},
2887 {CC"getMethodBooleanOption",
2888 CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Boolean;",
2889 (void*)&WB_GetMethodBooleaneOption},
2890 {CC"getMethodIntxOption",
2891 CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
2892 (void*)&WB_GetMethodIntxOption},
2893 {CC"getMethodUintxOption",
2894 CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
2895 (void*)&WB_GetMethodUintxOption},
2896 {CC"getMethodDoubleOption",
2897 CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Double;",
2898 (void*)&WB_GetMethodDoubleOption},
2899 {CC"getMethodStringOption",
2900 CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/String;",
2901 (void*)&WB_GetMethodStringOption},
2902 {CC"getDefaultArchivePath", CC"()Ljava/lang/String;",
2903 (void*)&WB_GetDefaultArchivePath},
2904 {CC"getCDSGenericHeaderMinVersion", CC"()I", (void*)&WB_GetCDSGenericHeaderMinVersion},
2905 {CC"getCurrentCDSVersion", CC"()I", (void*)&WB_GetCDSCurrentVersion},
2906 {CC"isSharingEnabled", CC"()Z", (void*)&WB_IsSharingEnabled},
2907 {CC"isSharedInternedString", CC"(Ljava/lang/String;)Z", (void*)&WB_IsSharedInternedString },
2908 {CC"isSharedClass", CC"(Ljava/lang/Class;)Z", (void*)&WB_IsSharedClass },
2909 {CC"areSharedStringsMapped", CC"()Z", (void*)&WB_AreSharedStringsMapped },
2910 {CC"linkClass", CC"(Ljava/lang/Class;)V", (void*)&WB_LinkClass},
2911 {CC"areOpenArchiveHeapObjectsMapped", CC"()Z", (void*)&WB_AreOpenArchiveHeapObjectsMapped},
2912 {CC"isCDSIncluded", CC"()Z", (void*)&WB_IsCDSIncluded },
2913 {CC"isJFRIncluded", CC"()Z", (void*)&WB_IsJFRIncluded },
2914 {CC"isDTraceIncluded", CC"()Z", (void*)&WB_IsDTraceIncluded },
2915 {CC"hasLibgraal", CC"()Z", (void*)&WB_HasLibgraal },
2916 {CC"isC2OrJVMCIIncluded", CC"()Z", (void*)&WB_isC2OrJVMCIIncluded },
2917 {CC"isJVMCISupportedByGC", CC"()Z", (void*)&WB_IsJVMCISupportedByGC},
2918 {CC"canWriteJavaHeapArchive", CC"()Z", (void*)&WB_CanWriteJavaHeapArchive },
2919 {CC"cdsMemoryMappingFailed", CC"()Z", (void*)&WB_CDSMemoryMappingFailed },
2920
2921 {CC"clearInlineCaches0", CC"(Z)V", (void*)&WB_ClearInlineCaches },
2922 {CC"handshakeReadMonitors", CC"(Ljava/lang/Thread;)Z", (void*)&WB_HandshakeReadMonitors },
2923 {CC"handshakeWalkStack", CC"(Ljava/lang/Thread;Z)I", (void*)&WB_HandshakeWalkStack },
2924 {CC"asyncHandshakeWalkStack", CC"(Ljava/lang/Thread;)V", (void*)&WB_AsyncHandshakeWalkStack },
2925 {CC"lockAndBlock", CC"(Z)V", (void*)&WB_LockAndBlock},
2926 {CC"checkThreadObjOfTerminatingThread", CC"(Ljava/lang/Thread;)V", (void*)&WB_CheckThreadObjOfTerminatingThread },
2927 {CC"verifyFrames", CC"(ZZ)V", (void*)&WB_VerifyFrames },
2928 {CC"addCompilerDirective", CC"(Ljava/lang/String;)I",
2929 (void*)&WB_AddCompilerDirective },
2930 {CC"removeCompilerDirective", CC"(I)V", (void*)&WB_RemoveCompilerDirective },
2931 {CC"isGCSupported", CC"(I)Z", (void*)&WB_IsGCSupported},
2932 {CC"isGCSupportedByJVMCICompiler", CC"(I)Z", (void*)&WB_IsGCSupportedByJVMCICompiler},
2933 {CC"isGCSelected", CC"(I)Z", (void*)&WB_IsGCSelected},
2934 {CC"isGCSelectedErgonomically", CC"()Z", (void*)&WB_IsGCSelectedErgonomically},
2935 {CC"supportsConcurrentGCBreakpoints", CC"()Z", (void*)&WB_SupportsConcurrentGCBreakpoints},
2936 {CC"concurrentGCAcquireControl0", CC"()V", (void*)&WB_ConcurrentGCAcquireControl},
2937 {CC"concurrentGCReleaseControl0", CC"()V", (void*)&WB_ConcurrentGCReleaseControl},
2938 {CC"concurrentGCRunToIdle0", CC"()V", (void*)&WB_ConcurrentGCRunToIdle},
2939 {CC"concurrentGCRunTo0", CC"(Ljava/lang/String;)Z",
2940 (void*)&WB_ConcurrentGCRunTo},
2941 {CC"checkLibSpecifiesNoexecstack", CC"(Ljava/lang/String;)Z",
2942 (void*)&WB_CheckLibSpecifiesNoexecstack},
2943 {CC"isContainerized", CC"()Z", (void*)&WB_IsContainerized },
2944 {CC"validateCgroup",
2945 CC"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
2946 (void*)&WB_ValidateCgroup },
2947 {CC"hostPhysicalMemory", CC"()J", (void*)&WB_HostPhysicalMemory },
2948 {CC"hostPhysicalSwap", CC"()J", (void*)&WB_HostPhysicalSwap },
2949 {CC"hostCPUs", CC"()I", (void*)&WB_HostCPUs },
2950 {CC"printOsInfo", CC"()V", (void*)&WB_PrintOsInfo },
2951 {CC"disableElfSectionCache", CC"()V", (void*)&WB_DisableElfSectionCache },
2952 {CC"resolvedMethodItemsCount", CC"()J", (void*)&WB_ResolvedMethodItemsCount },
2953 {CC"protectionDomainRemovedCount", CC"()I", (void*)&WB_ProtectionDomainRemovedCount },
2954 {CC"getKlassMetadataSize", CC"(Ljava/lang/Class;)I",(void*)&WB_GetKlassMetadataSize},
2955
2956 {CC"createMetaspaceTestContext", CC"(JJ)J", (void*)&WB_CreateMetaspaceTestContext},
2957 {CC"destroyMetaspaceTestContext", CC"(J)V", (void*)&WB_DestroyMetaspaceTestContext},
2958 {CC"purgeMetaspaceTestContext", CC"(J)V", (void*)&WB_PurgeMetaspaceTestContext},
2959 {CC"printMetaspaceTestContext", CC"(J)V", (void*)&WB_PrintMetaspaceTestContext},
2960 {CC"getTotalCommittedWordsInMetaspaceTestContext", CC"(J)J",(void*)&WB_GetTotalCommittedWordsInMetaspaceTestContext},
2961 {CC"getTotalUsedWordsInMetaspaceTestContext", CC"(J)J", (void*)&WB_GetTotalUsedWordsInMetaspaceTestContext},
2962 {CC"createArenaInTestContext", CC"(JZ)J", (void*)&WB_CreateArenaInTestContext},
2963 {CC"destroyMetaspaceTestArena", CC"(J)V", (void*)&WB_DestroyMetaspaceTestArena},
2964 {CC"allocateFromMetaspaceTestArena", CC"(JJ)J", (void*)&WB_AllocateFromMetaspaceTestArena},
2965 {CC"deallocateToMetaspaceTestArena", CC"(JJJ)V", (void*)&WB_DeallocateToMetaspaceTestArena},
2966 {CC"maxMetaspaceAllocationSize", CC"()J", (void*)&WB_GetMaxMetaspaceAllocationSize},
2967
2968 {CC"isJVMTIIncluded", CC"()Z", (void*)&WB_IsJVMTIIncluded},
2969 {CC"waitUnsafe", CC"(I)V", (void*)&WB_WaitUnsafe},
2970 {CC"getLibcName", CC"()Ljava/lang/String;", (void*)&WB_GetLibcName},
2971
2972 {CC"lockCritical", CC"()V", (void*)&WB_LockCritical},
2973 {CC"unlockCritical", CC"()V", (void*)&WB_UnlockCritical},
2974 {CC"pinObject", CC"(Ljava/lang/Object;)V", (void*)&WB_PinObject},
2975 {CC"unpinObject", CC"(Ljava/lang/Object;)V", (void*)&WB_UnpinObject},
2976 {CC"setVirtualThreadsNotifyJvmtiMode", CC"(Z)Z", (void*)&WB_SetVirtualThreadsNotifyJvmtiMode},
2977 {CC"preTouchMemory", CC"(JJ)V", (void*)&WB_PreTouchMemory},
2978 {CC"cleanMetaspaces", CC"()V", (void*)&WB_CleanMetaspaces},
2979 {CC"rss", CC"()J", (void*)&WB_Rss},
2980 {CC"printString", CC"(Ljava/lang/String;I)Ljava/lang/String;", (void*)&WB_PrintString},
2981 };
2982
2983
2984 #undef CC
2985
2986 JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass))
2987 {
2988 if (WhiteBoxAPI) {
2989 // Make sure that wbclass is loaded by the null classloader
2990 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(wbclass)));
2991 Handle loader(THREAD, ik->class_loader());
2992 if (loader.is_null()) {
2993 WhiteBox::register_methods(env, wbclass, thread, methods, sizeof(methods) / sizeof(methods[0]));
2994 WhiteBox::set_used();
2995 }
2996 }
2997 }
2998 JVM_END
--- EOF ---