1 /*
   2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 
  27 #include <new>
  28 
  29 #include "classfile/classLoaderDataGraph.hpp"
  30 #include "classfile/modules.hpp"
  31 #include "classfile/protectionDomainCache.hpp"
  32 #include "classfile/stringTable.hpp"
  33 #include "code/codeCache.hpp"
  34 #include "compiler/methodMatcher.hpp"
  35 #include "compiler/directivesParser.hpp"
  36 #include "gc/shared/gcConfig.hpp"
  37 #include "gc/shared/genCollectedHeap.hpp"
  38 #include "jvmtifiles/jvmtiEnv.hpp"
  39 #include "memory/heapShared.inline.hpp"
  40 #include "memory/metaspaceShared.hpp"
  41 #include "memory/metadataFactory.hpp"
  42 #include "memory/iterator.hpp"
  43 #include "memory/resourceArea.hpp"
  44 #include "memory/universe.hpp"
  45 #include "memory/oopFactory.hpp"
  46 #include "oops/array.hpp"
  47 #include "oops/constantPool.inline.hpp"
  48 #include "oops/method.inline.hpp"
  49 #include "oops/objArrayKlass.hpp"
  50 #include "oops/objArrayOop.inline.hpp"
  51 #include "oops/oop.inline.hpp"
  52 #include "oops/typeArrayOop.inline.hpp"
  53 #include "prims/resolvedMethodTable.hpp"
  54 #include "prims/wbtestmethods/parserTests.hpp"
  55 #include "prims/whitebox.inline.hpp"
  56 #include "runtime/arguments.hpp"
  57 #include "runtime/compilationPolicy.hpp"
  58 #include "runtime/deoptimization.hpp"
  59 #include "runtime/fieldDescriptor.inline.hpp"
  60 #include "runtime/flags/jvmFlag.hpp"
  61 #include "runtime/frame.inline.hpp"
  62 #include "runtime/handles.inline.hpp"
  63 #include "runtime/handshake.hpp"
  64 #include "runtime/interfaceSupport.inline.hpp"
  65 #include "runtime/javaCalls.hpp"
  66 #include "runtime/jniHandles.inline.hpp"
  67 #include "runtime/os.hpp"
  68 #include "runtime/sweeper.hpp"
  69 #include "runtime/thread.hpp"
  70 #include "runtime/threadSMR.hpp"
  71 #include "runtime/vm_version.hpp"
  72 #include "services/memoryService.hpp"
  73 #include "utilities/align.hpp"
  74 #include "utilities/debug.hpp"
  75 #include "utilities/elfFile.hpp"
  76 #include "utilities/exceptions.hpp"
  77 #include "utilities/macros.hpp"
  78 #if INCLUDE_CDS
  79 #include "prims/cdsoffsets.hpp"
  80 #endif // INCLUDE_CDS
  81 #if INCLUDE_G1GC
  82 #include "gc/g1/g1CollectedHeap.inline.hpp"
  83 #include "gc/g1/g1ConcurrentMark.hpp"
  84 #include "gc/g1/g1ConcurrentMarkThread.hpp"
  85 #include "gc/g1/heapRegionRemSet.hpp"
  86 #include "gc/g1/heterogeneousHeapRegionManager.hpp"
  87 #endif // INCLUDE_G1GC
  88 #if INCLUDE_PARALLELGC
  89 #include "gc/parallel/parallelScavengeHeap.inline.hpp"
  90 #include "gc/parallel/adjoiningGenerations.hpp"
  91 #endif // INCLUDE_PARALLELGC
  92 #if INCLUDE_NMT
  93 #include "services/mallocSiteTable.hpp"
  94 #include "services/memTracker.hpp"
  95 #include "utilities/nativeCallStack.hpp"
  96 #endif // INCLUDE_NMT
  97 #if INCLUDE_AOT
  98 #include "aot/aotLoader.hpp"
  99 #endif // INCLUDE_AOT
 100 
 101 #ifdef LINUX
 102 #include "osContainer_linux.hpp"
 103 #endif
 104 
 105 #define SIZE_T_MAX_VALUE ((size_t) -1)
 106 
 107 #define CHECK_JNI_EXCEPTION_(env, value)                               \
 108   do {                                                                 \
 109     JavaThread* THREAD = JavaThread::thread_from_jni_environment(env); \
 110     THREAD->clear_pending_jni_exception_check();                       \
 111     if (HAS_PENDING_EXCEPTION) {                                       \
 112       return(value);                                                   \
 113     }                                                                  \
 114   } while (0)
 115 
 116 #define CHECK_JNI_EXCEPTION(env)                                       \
 117   do {                                                                 \
 118     JavaThread* THREAD = JavaThread::thread_from_jni_environment(env); \
 119     THREAD->clear_pending_jni_exception_check();                       \
 120     if (HAS_PENDING_EXCEPTION) {                                       \
 121       return;                                                          \
 122     }                                                                  \
 123   } while (0)
 124 
 125 bool WhiteBox::_used = false;
 126 volatile bool WhiteBox::compilation_locked = false;
 127 
 128 class VM_WhiteBoxOperation : public VM_Operation {
 129  public:
 130   VM_WhiteBoxOperation()                         { }
 131   VMOp_Type type()                  const        { return VMOp_WhiteBoxOperation; }
 132   bool allow_nested_vm_operations() const        { return true; }
 133 };
 134 
 135 
 136 WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
 137   return (jlong)(void*)JNIHandles::resolve(obj);
 138 WB_END
 139 
 140 WB_ENTRY(jint, WB_GetHeapOopSize(JNIEnv* env, jobject o))
 141   return heapOopSize;
 142 WB_END
 143 
 144 WB_ENTRY(jint, WB_GetVMPageSize(JNIEnv* env, jobject o))
 145   return os::vm_page_size();
 146 WB_END
 147 
 148 WB_ENTRY(jlong, WB_GetVMAllocationGranularity(JNIEnv* env, jobject o))
 149   return os::vm_allocation_granularity();
 150 WB_END
 151 
 152 WB_ENTRY(jlong, WB_GetVMLargePageSize(JNIEnv* env, jobject o))
 153   return os::large_page_size();
 154 WB_END
 155 
 156 class WBIsKlassAliveClosure : public LockedClassesDo {
 157     Symbol* _name;
 158     bool _found;
 159 public:
 160     WBIsKlassAliveClosure(Symbol* name) : _name(name), _found(false) {}
 161 
 162     void do_klass(Klass* k) {
 163       if (_found) return;
 164       Symbol* ksym = k->name();
 165       if (ksym->fast_compare(_name) == 0) {
 166         _found = true;
 167       }
 168     }
 169 
 170     bool found() const {
 171         return _found;
 172     }
 173 };
 174 
 175 WB_ENTRY(jboolean, WB_IsClassAlive(JNIEnv* env, jobject target, jstring name))
 176   oop h_name = JNIHandles::resolve(name);
 177   if (h_name == NULL) return false;
 178   Symbol* sym = java_lang_String::as_symbol(h_name, CHECK_false);
 179   TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
 180 
 181   WBIsKlassAliveClosure closure(sym);
 182   ClassLoaderDataGraph::classes_do(&closure);
 183 
 184   return closure.found();
 185 WB_END
 186 
 187 WB_ENTRY(jint, WB_GetSymbolRefcount(JNIEnv* env, jobject unused, jstring name))
 188   oop h_name = JNIHandles::resolve(name);
 189   if (h_name == NULL) return false;
 190   Symbol* sym = java_lang_String::as_symbol(h_name, CHECK_0);
 191   TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
 192   return (jint)sym->refcount();
 193 WB_END
 194 
 195 
 196 WB_ENTRY(void, WB_AddToBootstrapClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) {
 197 #if INCLUDE_JVMTI
 198   ResourceMark rm;
 199   const char* seg = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(segment));
 200   JvmtiEnv* jvmti_env = JvmtiEnv::create_a_jvmti(JVMTI_VERSION);
 201   jvmtiError err = jvmti_env->AddToBootstrapClassLoaderSearch(seg);
 202   assert(err == JVMTI_ERROR_NONE, "must not fail");
 203 #endif
 204 }
 205 WB_END
 206 
 207 WB_ENTRY(void, WB_AddToSystemClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) {
 208 #if INCLUDE_JVMTI
 209   ResourceMark rm;
 210   const char* seg = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(segment));
 211   JvmtiEnv* jvmti_env = JvmtiEnv::create_a_jvmti(JVMTI_VERSION);
 212   jvmtiError err = jvmti_env->AddToSystemClassLoaderSearch(seg);
 213   assert(err == JVMTI_ERROR_NONE, "must not fail");
 214 #endif
 215 }
 216 WB_END
 217 
 218 
 219 WB_ENTRY(jlong, WB_GetCompressedOopsMaxHeapSize(JNIEnv* env, jobject o)) {
 220   return (jlong)Arguments::max_heap_for_compressed_oops();
 221 }
 222 WB_END
 223 
 224 WB_ENTRY(void, WB_PrintHeapSizes(JNIEnv* env, jobject o)) {
 225   CollectorPolicy * p = Universe::heap()->collector_policy();
 226   tty->print_cr("Minimum heap " SIZE_FORMAT " Initial heap "
 227     SIZE_FORMAT " Maximum heap " SIZE_FORMAT " Space alignment " SIZE_FORMAT " Heap alignment " SIZE_FORMAT,
 228     p->min_heap_byte_size(), p->initial_heap_byte_size(), p->max_heap_byte_size(),
 229     p->space_alignment(), p->heap_alignment());
 230 }
 231 WB_END
 232 
 233 #ifndef PRODUCT
 234 // Forward declaration
 235 void TestReservedSpace_test();
 236 void TestReserveMemorySpecial_test();
 237 void TestVirtualSpace_test();
 238 void TestMetaspaceUtils_test();
 239 #endif
 240 
 241 WB_ENTRY(void, WB_RunMemoryUnitTests(JNIEnv* env, jobject o))
 242 #ifndef PRODUCT
 243   TestReservedSpace_test();
 244   TestReserveMemorySpecial_test();
 245   TestVirtualSpace_test();
 246   TestMetaspaceUtils_test();
 247 #endif
 248 WB_END
 249 
 250 WB_ENTRY(void, WB_ReadFromNoaccessArea(JNIEnv* env, jobject o))
 251   size_t granularity = os::vm_allocation_granularity();
 252   ReservedHeapSpace rhs(100 * granularity, granularity, false);
 253   VirtualSpace vs;
 254   vs.initialize(rhs, 50 * granularity);
 255 
 256   // Check if constraints are complied
 257   if (!( UseCompressedOops && rhs.base() != NULL &&
 258          Universe::narrow_oop_base() != NULL &&
 259          Universe::narrow_oop_use_implicit_null_checks() )) {
 260     tty->print_cr("WB_ReadFromNoaccessArea method is useless:\n "
 261                   "\tUseCompressedOops is %d\n"
 262                   "\trhs.base() is " PTR_FORMAT "\n"
 263                   "\tUniverse::narrow_oop_base() is " PTR_FORMAT "\n"
 264                   "\tUniverse::narrow_oop_use_implicit_null_checks() is %d",
 265                   UseCompressedOops,
 266                   p2i(rhs.base()),
 267                   p2i(Universe::narrow_oop_base()),
 268                   Universe::narrow_oop_use_implicit_null_checks());
 269     return;
 270   }
 271   tty->print_cr("Reading from no access area... ");
 272   tty->print_cr("*(vs.low_boundary() - rhs.noaccess_prefix() / 2 ) = %c",
 273                 *(vs.low_boundary() - rhs.noaccess_prefix() / 2 ));
 274 WB_END
 275 
 276 static jint wb_stress_virtual_space_resize(size_t reserved_space_size,
 277                                            size_t magnitude, size_t iterations) {
 278   size_t granularity = os::vm_allocation_granularity();
 279   ReservedHeapSpace rhs(reserved_space_size * granularity, granularity, false);
 280   VirtualSpace vs;
 281   if (!vs.initialize(rhs, 0)) {
 282     tty->print_cr("Failed to initialize VirtualSpace. Can't proceed.");
 283     return 3;
 284   }
 285 
 286   int seed = os::random();
 287   tty->print_cr("Random seed is %d", seed);
 288   os::init_random(seed);
 289 
 290   for (size_t i = 0; i < iterations; i++) {
 291 
 292     // Whether we will shrink or grow
 293     bool shrink = os::random() % 2L == 0;
 294 
 295     // Get random delta to resize virtual space
 296     size_t delta = (size_t)os::random() % magnitude;
 297 
 298     // If we are about to shrink virtual space below zero, then expand instead
 299     if (shrink && vs.committed_size() < delta) {
 300       shrink = false;
 301     }
 302 
 303     // Resizing by delta
 304     if (shrink) {
 305       vs.shrink_by(delta);
 306     } else {
 307       // If expanding fails expand_by will silently return false
 308       vs.expand_by(delta, true);
 309     }
 310   }
 311   return 0;
 312 }
 313 
 314 WB_ENTRY(jint, WB_StressVirtualSpaceResize(JNIEnv* env, jobject o,
 315         jlong reserved_space_size, jlong magnitude, jlong iterations))
 316   tty->print_cr("reservedSpaceSize=" JLONG_FORMAT ", magnitude=" JLONG_FORMAT ", "
 317                 "iterations=" JLONG_FORMAT "\n", reserved_space_size, magnitude,
 318                 iterations);
 319   if (reserved_space_size < 0 || magnitude < 0 || iterations < 0) {
 320     tty->print_cr("One of variables printed above is negative. Can't proceed.\n");
 321     return 1;
 322   }
 323 
 324   // sizeof(size_t) depends on whether OS is 32bit or 64bit. sizeof(jlong) is
 325   // always 8 byte. That's why we should avoid overflow in case of 32bit platform.
 326   if (sizeof(size_t) < sizeof(jlong)) {
 327     jlong size_t_max_value = (jlong) SIZE_T_MAX_VALUE;
 328     if (reserved_space_size > size_t_max_value || magnitude > size_t_max_value
 329         || iterations > size_t_max_value) {
 330       tty->print_cr("One of variables printed above overflows size_t. Can't proceed.\n");
 331       return 2;
 332     }
 333   }
 334 
 335   return wb_stress_virtual_space_resize((size_t) reserved_space_size,
 336                                         (size_t) magnitude, (size_t) iterations);
 337 WB_END
 338 
 339 WB_ENTRY(jboolean, WB_IsGCSupported(JNIEnv* env, jobject o, jint name))
 340   return GCConfig::is_gc_supported((CollectedHeap::Name)name);
 341 WB_END
 342 
 343 WB_ENTRY(jboolean, WB_IsGCSelected(JNIEnv* env, jobject o, jint name))
 344   return GCConfig::is_gc_selected((CollectedHeap::Name)name);
 345 WB_END
 346 
 347 WB_ENTRY(jboolean, WB_IsGCSelectedErgonomically(JNIEnv* env, jobject o))
 348   return GCConfig::is_gc_selected_ergonomically();
 349 WB_END
 350 
 351 WB_ENTRY(jboolean, WB_isObjectInOldGen(JNIEnv* env, jobject o, jobject obj))
 352   oop p = JNIHandles::resolve(obj);
 353 #if INCLUDE_G1GC
 354   if (UseG1GC) {
 355     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 356     const HeapRegion* hr = g1h->heap_region_containing(p);
 357     if (hr == NULL) {
 358       return false;
 359     }
 360     return !(hr->is_young());
 361   }
 362 #endif
 363 #if INCLUDE_PARALLELGC
 364   if (UseParallelGC) {
 365     ParallelScavengeHeap* psh = ParallelScavengeHeap::heap();
 366     return !psh->is_in_young(p);
 367   }
 368 #endif
 369 #if INCLUDE_ZGC
 370   if (UseZGC) {
 371     return Universe::heap()->is_in(p);
 372   }
 373 #endif
 374   GenCollectedHeap* gch = GenCollectedHeap::heap();
 375   return !gch->is_in_young(p);
 376 WB_END
 377 
 378 WB_ENTRY(jlong, WB_GetObjectSize(JNIEnv* env, jobject o, jobject obj))
 379   oop p = JNIHandles::resolve(obj);
 380   return Universe::heap()->obj_size(p) * HeapWordSize;
 381 WB_END
 382 
 383 WB_ENTRY(jlong, WB_GetHeapSpaceAlignment(JNIEnv* env, jobject o))
 384   size_t alignment = Universe::heap()->collector_policy()->space_alignment();
 385   return (jlong)alignment;
 386 WB_END
 387 
 388 WB_ENTRY(jlong, WB_GetHeapAlignment(JNIEnv* env, jobject o))
 389   size_t alignment = Universe::heap()->collector_policy()->heap_alignment();
 390   return (jlong)alignment;
 391 WB_END
 392 
 393 WB_ENTRY(jboolean, WB_SupportsConcurrentGCPhaseControl(JNIEnv* env, jobject o))
 394   return Universe::heap()->supports_concurrent_phase_control();
 395 WB_END
 396 
 397 WB_ENTRY(jboolean, WB_RequestConcurrentGCPhase(JNIEnv* env, jobject o, jstring name))
 398   Handle h_name(THREAD, JNIHandles::resolve(name));
 399   ResourceMark rm;
 400   const char* c_name = java_lang_String::as_utf8_string(h_name());
 401   return Universe::heap()->request_concurrent_phase(c_name);
 402 WB_END
 403 
 404 #if INCLUDE_G1GC
 405 
 406 WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj))
 407   if (UseG1GC) {
 408     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 409     oop result = JNIHandles::resolve(obj);
 410     const HeapRegion* hr = g1h->heap_region_containing(result);
 411     return hr->is_humongous();
 412   }
 413   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1IsHumongous: G1 GC is not enabled");
 414 WB_END
 415 
 416 WB_ENTRY(jboolean, WB_G1BelongsToHumongousRegion(JNIEnv* env, jobject o, jlong addr))
 417   if (UseG1GC) {
 418     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 419     const HeapRegion* hr = g1h->heap_region_containing((void*) addr);
 420     return hr->is_humongous();
 421   }
 422   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1BelongsToHumongousRegion: G1 GC is not enabled");
 423 WB_END
 424 
 425 WB_ENTRY(jboolean, WB_G1BelongsToFreeRegion(JNIEnv* env, jobject o, jlong addr))
 426   if (UseG1GC) {
 427     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 428     const HeapRegion* hr = g1h->heap_region_containing((void*) addr);
 429     return hr->is_free();
 430   }
 431   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1BelongsToFreeRegion: G1 GC is not enabled");
 432 WB_END
 433 
 434 WB_ENTRY(jlong, WB_G1NumMaxRegions(JNIEnv* env, jobject o))
 435   if (UseG1GC) {
 436     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 437     size_t nr = g1h->max_regions();
 438     return (jlong)nr;
 439   }
 440   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1NumMaxRegions: G1 GC is not enabled");
 441 WB_END
 442 
 443 WB_ENTRY(jlong, WB_G1NumFreeRegions(JNIEnv* env, jobject o))
 444   if (UseG1GC) {
 445     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 446     size_t nr = g1h->num_free_regions();
 447     return (jlong)nr;
 448   }
 449   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1NumFreeRegions: G1 GC is not enabled");
 450 WB_END
 451 
 452 WB_ENTRY(jboolean, WB_G1InConcurrentMark(JNIEnv* env, jobject o))
 453   if (UseG1GC) {
 454     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 455     return g1h->concurrent_mark()->cm_thread()->during_cycle();
 456   }
 457   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1InConcurrentMark: G1 GC is not enabled");
 458 WB_END
 459 
 460 WB_ENTRY(jboolean, WB_G1StartMarkCycle(JNIEnv* env, jobject o))
 461   if (UseG1GC) {
 462     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 463     if (!g1h->concurrent_mark()->cm_thread()->during_cycle()) {
 464       g1h->collect(GCCause::_wb_conc_mark);
 465       return true;
 466     }
 467     return false;
 468   }
 469   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1StartMarkCycle: G1 GC is not enabled");
 470 WB_END
 471 
 472 WB_ENTRY(jint, WB_G1RegionSize(JNIEnv* env, jobject o))
 473   if (UseG1GC) {
 474     return (jint)HeapRegion::GrainBytes;
 475   }
 476   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1RegionSize: G1 GC is not enabled");
 477 WB_END
 478 
 479 #endif // INCLUDE_G1GC
 480 
 481 #if INCLUDE_G1GC || INCLUDE_PARALLELGC
 482 WB_ENTRY(jlong, WB_DramReservedStart(JNIEnv* env, jobject o))
 483 #if INCLUDE_G1GC
 484   if (UseG1GC) {
 485     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 486     if (g1h->is_heterogeneous_heap()) {
 487       uint start_region = HeterogeneousHeapRegionManager::manager()->start_index_of_dram();
 488       return (jlong)(g1h->base() + start_region * HeapRegion::GrainBytes);
 489     } else {
 490       return (jlong)g1h->base();
 491     }
 492   }
 493 #endif // INCLUDE_G1GC
 494 #if INCLUDE_PARALLELGC
 495   if (UseParallelGC) {
 496     ParallelScavengeHeap* ps_heap = ParallelScavengeHeap::heap();
 497     if (AllocateOldGenAt != NULL) {
 498       MemRegion reserved = ps_heap->young_gen()->reserved();
 499       return (jlong)reserved.start();
 500     } else {
 501       return (jlong)ps_heap->base();
 502     }
 503   }
 504 #endif // INCLUDE_PARALLELGC
 505   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_DramReservedStart: enabled only for G1 and Parallel GC");
 506 WB_END
 507 
 508 WB_ENTRY(jlong, WB_DramReservedEnd(JNIEnv* env, jobject o))
 509 #if INCLUDE_G1GC
 510   if (UseG1GC) {
 511     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 512     if (g1h->is_heterogeneous_heap()) {
 513       uint end_region = HeterogeneousHeapRegionManager::manager()->end_index_of_dram();
 514       return (jlong)(g1h->base() + (end_region + 1) * HeapRegion::GrainBytes - 1);
 515     } else {
 516       return (jlong)g1h->base() + g1h->collector_policy()->max_heap_byte_size();
 517     }
 518   }
 519 #endif // INCLUDE_G1GC
 520 #if INCLUDE_PARALLELGC
 521   if (UseParallelGC) {
 522     ParallelScavengeHeap* ps_heap = ParallelScavengeHeap::heap();
 523     if (AllocateOldGenAt != NULL) {
 524       MemRegion reserved = ps_heap->young_gen()->reserved();
 525       return (jlong)reserved.end();
 526     } else {
 527       return (jlong)ps_heap->reserved_region().end();
 528     }
 529   }
 530 #endif // INCLUDE_PARALLELGC
 531   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_DramReservedEnd: enabled only for G1 and Parallel GC");
 532 WB_END
 533 
 534 WB_ENTRY(jlong, WB_NvdimmReservedStart(JNIEnv* env, jobject o))
 535 #if INCLUDE_G1GC
 536   if (UseG1GC) {
 537     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 538     if (g1h->is_heterogeneous_heap()) {
 539       uint start_region = HeterogeneousHeapRegionManager::manager()->start_index_of_nvdimm();
 540       return (jlong)(g1h->base() + start_region * HeapRegion::GrainBytes);
 541     } else {
 542       THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_NvdimmReservedStart: Old gen is not allocated on NV-DIMM using AllocateOldGenAt flag");
 543     }
 544   }
 545 #endif // INCLUDE_G1GC
 546 #if INCLUDE_PARALLELGC
 547   if (UseParallelGC) {
 548     ParallelScavengeHeap* ps_heap = ParallelScavengeHeap::heap();
 549     if (AllocateOldGenAt != NULL) {
 550       MemRegion reserved = ps_heap->old_gen()->reserved();
 551       return (jlong)reserved.start();
 552     } else {
 553       THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_NvdimmReservedStart: Old gen is not allocated on NV-DIMM using AllocateOldGenAt flag");
 554     }
 555   }
 556 #endif // INCLUDE_PARALLELGC
 557   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_NvdimmReservedStart: enabled only for G1 and Parallel GC");
 558 WB_END
 559 
 560 WB_ENTRY(jlong, WB_NvdimmReservedEnd(JNIEnv* env, jobject o))
 561 #if INCLUDE_G1GC
 562   if (UseG1GC) {
 563     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 564     if (g1h->is_heterogeneous_heap()) {
 565       uint end_region = HeterogeneousHeapRegionManager::manager()->start_index_of_nvdimm();
 566       return (jlong)(g1h->base() + (end_region + 1) * HeapRegion::GrainBytes - 1);
 567     } else {
 568       THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_NvdimmReservedEnd: Old gen is not allocated on NV-DIMM using AllocateOldGenAt flag");
 569     }
 570   }
 571 #endif // INCLUDE_G1GC
 572 #if INCLUDE_PARALLELGC
 573   if (UseParallelGC) {
 574     ParallelScavengeHeap* ps_heap = ParallelScavengeHeap::heap();
 575     if (AllocateOldGenAt != NULL) {
 576       MemRegion reserved = ps_heap->old_gen()->reserved();
 577       return (jlong)reserved.end();
 578       } else {
 579       THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_NvdimmReservedEnd: Old gen is not allocated on NV-DIMM using AllocateOldGenAt flag");
 580     }
 581   }
 582 #endif // INCLUDE_PARALLELGC
 583   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_NvdimmReservedEnd: enabled only for G1 and Parallel GC");
 584 WB_END
 585 
 586 #endif // INCLUDE_G1GC || INCLUDE_PARALLELGC
 587 
 588 #if INCLUDE_PARALLELGC
 589 
 590 WB_ENTRY(jlong, WB_PSVirtualSpaceAlignment(JNIEnv* env, jobject o))
 591   if (UseParallelGC) {
 592     return ParallelScavengeHeap::heap()->gens()->virtual_spaces()->alignment();
 593   }
 594   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_PSVirtualSpaceAlignment: Parallel GC is not enabled");
 595 WB_END
 596 
 597 WB_ENTRY(jlong, WB_PSHeapGenerationAlignment(JNIEnv* env, jobject o))
 598   if (UseParallelGC) {
 599     return ParallelScavengeHeap::heap()->generation_alignment();
 600   }
 601   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_PSHeapGenerationAlignment: Parallel GC is not enabled");
 602 WB_END
 603 
 604 #endif // INCLUDE_PARALLELGC
 605 
 606 #if INCLUDE_G1GC
 607 
 608 WB_ENTRY(jobject, WB_G1AuxiliaryMemoryUsage(JNIEnv* env))
 609   if (UseG1GC) {
 610     ResourceMark rm(THREAD);
 611     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 612     MemoryUsage usage = g1h->get_auxiliary_data_memory_usage();
 613     Handle h = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL);
 614     return JNIHandles::make_local(env, h());
 615   }
 616   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1AuxiliaryMemoryUsage: G1 GC is not enabled");
 617 WB_END
 618 
 619 class OldRegionsLivenessClosure: public HeapRegionClosure {
 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(HeapRegion* r) {
 639     if (r->is_old()) {
 640       size_t prev_live = r->marked_bytes();
 641       size_t live = r->live_bytes();
 642       size_t size = r->used();
 643       size_t reg_size = HeapRegion::GrainBytes;
 644       if (size > 0 && ((int)(live * 100 / size) < _liveness)) {
 645         _total_memory += size;
 646         ++_total_count;
 647         if (size == reg_size) {
 648         // we don't include non-full regions since they are unlikely included in mixed gc
 649         // for testing purposes it's enough to have lowest estimation of total memory that is expected to be freed
 650           _total_memory_to_free += size - prev_live;
 651         }
 652       }
 653     }
 654     return false;
 655   }
 656 };
 657 
 658 
 659 WB_ENTRY(jlongArray, WB_G1GetMixedGCInfo(JNIEnv* env, jobject o, jint liveness))
 660   if (!UseG1GC) {
 661     THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1GetMixedGCInfo: G1 GC is not enabled");
 662   }
 663   if (liveness < 0) {
 664     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "liveness value should be non-negative");
 665   }
 666 
 667   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 668   OldRegionsLivenessClosure rli(liveness);
 669   g1h->heap_region_iterate(&rli);
 670 
 671   typeArrayOop result = oopFactory::new_longArray(3, CHECK_NULL);
 672   result->long_at_put(0, rli.total_count());
 673   result->long_at_put(1, rli.total_memory());
 674   result->long_at_put(2, rli.total_memory_to_free());
 675   return (jlongArray) JNIHandles::make_local(env, result);
 676 WB_END
 677 
 678 #endif // INCLUDE_G1GC
 679 
 680 #if INCLUDE_NMT
 681 // Alloc memory using the test memory type so that we can use that to see if
 682 // NMT picks it up correctly
 683 WB_ENTRY(jlong, WB_NMTMalloc(JNIEnv* env, jobject o, jlong size))
 684   jlong addr = 0;
 685   addr = (jlong)(uintptr_t)os::malloc(size, mtTest);
 686   return addr;
 687 WB_END
 688 
 689 // Alloc memory with pseudo call stack. The test can create psudo malloc
 690 // allocation site to stress the malloc tracking.
 691 WB_ENTRY(jlong, WB_NMTMallocWithPseudoStack(JNIEnv* env, jobject o, jlong size, jint pseudo_stack))
 692   address pc = (address)(size_t)pseudo_stack;
 693   NativeCallStack stack(&pc, 1);
 694   return (jlong)(uintptr_t)os::malloc(size, mtTest, stack);
 695 WB_END
 696 
 697 // Alloc memory with pseudo call stack and specific memory type.
 698 WB_ENTRY(jlong, WB_NMTMallocWithPseudoStackAndType(JNIEnv* env, jobject o, jlong size, jint pseudo_stack, jint type))
 699   address pc = (address)(size_t)pseudo_stack;
 700   NativeCallStack stack(&pc, 1);
 701   return (jlong)(uintptr_t)os::malloc(size, (MEMFLAGS)type, stack);
 702 WB_END
 703 
 704 // Free the memory allocated by NMTAllocTest
 705 WB_ENTRY(void, WB_NMTFree(JNIEnv* env, jobject o, jlong mem))
 706   os::free((void*)(uintptr_t)mem);
 707 WB_END
 708 
 709 WB_ENTRY(jlong, WB_NMTReserveMemory(JNIEnv* env, jobject o, jlong size))
 710   jlong addr = 0;
 711 
 712   addr = (jlong)(uintptr_t)os::reserve_memory(size);
 713   MemTracker::record_virtual_memory_type((address)addr, mtTest);
 714 
 715   return addr;
 716 WB_END
 717 
 718 WB_ENTRY(jlong, WB_NMTAttemptReserveMemoryAt(JNIEnv* env, jobject o, jlong addr, jlong size))
 719   addr = (jlong)(uintptr_t)os::attempt_reserve_memory_at((size_t)size, (char*)(uintptr_t)addr);
 720   MemTracker::record_virtual_memory_type((address)addr, mtTest);
 721 
 722   return addr;
 723 WB_END
 724 
 725 WB_ENTRY(void, WB_NMTCommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 726   os::commit_memory((char *)(uintptr_t)addr, size, !ExecMem);
 727   MemTracker::record_virtual_memory_type((address)(uintptr_t)addr, mtTest);
 728 WB_END
 729 
 730 WB_ENTRY(void, WB_NMTUncommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 731   os::uncommit_memory((char *)(uintptr_t)addr, size);
 732 WB_END
 733 
 734 WB_ENTRY(void, WB_NMTReleaseMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 735   os::release_memory((char *)(uintptr_t)addr, size);
 736 WB_END
 737 
 738 WB_ENTRY(jboolean, WB_NMTChangeTrackingLevel(JNIEnv* env))
 739   // Test that we can downgrade NMT levels but not upgrade them.
 740   if (MemTracker::tracking_level() == NMT_off) {
 741     MemTracker::transition_to(NMT_off);
 742     return MemTracker::tracking_level() == NMT_off;
 743   } else {
 744     assert(MemTracker::tracking_level() == NMT_detail, "Should start out as detail tracking");
 745     MemTracker::transition_to(NMT_summary);
 746     assert(MemTracker::tracking_level() == NMT_summary, "Should be summary now");
 747 
 748     // Can't go to detail once NMT is set to summary.
 749     MemTracker::transition_to(NMT_detail);
 750     assert(MemTracker::tracking_level() == NMT_summary, "Should still be summary now");
 751 
 752     // Shutdown sets tracking level to minimal.
 753     MemTracker::shutdown();
 754     assert(MemTracker::tracking_level() == NMT_minimal, "Should be minimal now");
 755 
 756     // Once the tracking level is minimal, we cannot increase to summary.
 757     // The code ignores this request instead of asserting because if the malloc site
 758     // table overflows in another thread, it tries to change the code to summary.
 759     MemTracker::transition_to(NMT_summary);
 760     assert(MemTracker::tracking_level() == NMT_minimal, "Should still be minimal now");
 761 
 762     // Really can never go up to detail, verify that the code would never do this.
 763     MemTracker::transition_to(NMT_detail);
 764     assert(MemTracker::tracking_level() == NMT_minimal, "Should still be minimal now");
 765     return MemTracker::tracking_level() == NMT_minimal;
 766   }
 767 WB_END
 768 
 769 WB_ENTRY(jint, WB_NMTGetHashSize(JNIEnv* env, jobject o))
 770   int hash_size = MallocSiteTable::hash_buckets();
 771   assert(hash_size > 0, "NMT hash_size should be > 0");
 772   return (jint)hash_size;
 773 WB_END
 774 #endif // INCLUDE_NMT
 775 
 776 static jmethodID reflected_method_to_jmid(JavaThread* thread, JNIEnv* env, jobject method) {
 777   assert(method != NULL, "method should not be null");
 778   ThreadToNativeFromVM ttn(thread);
 779   return env->FromReflectedMethod(method);
 780 }
 781 
 782 // Deoptimizes all compiled frames and makes nmethods not entrant if it's requested
 783 class VM_WhiteBoxDeoptimizeFrames : public VM_WhiteBoxOperation {
 784  private:
 785   int _result;
 786   const bool _make_not_entrant;
 787  public:
 788   VM_WhiteBoxDeoptimizeFrames(bool make_not_entrant) :
 789         _result(0), _make_not_entrant(make_not_entrant) { }
 790   int  result() const { return _result; }
 791 
 792   void doit() {
 793     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 794       if (t->has_last_Java_frame()) {
 795         for (StackFrameStream fst(t, UseBiasedLocking); !fst.is_done(); fst.next()) {
 796           frame* f = fst.current();
 797           if (f->can_be_deoptimized() && !f->is_deoptimized_frame()) {
 798             RegisterMap* reg_map = fst.register_map();
 799             Deoptimization::deoptimize(t, *f, reg_map);
 800             if (_make_not_entrant) {
 801                 CompiledMethod* cm = CodeCache::find_compiled(f->pc());
 802                 assert(cm != NULL, "sanity check");
 803                 cm->make_not_entrant();
 804             }
 805             ++_result;
 806           }
 807         }
 808       }
 809     }
 810   }
 811 };
 812 
 813 WB_ENTRY(jint, WB_DeoptimizeFrames(JNIEnv* env, jobject o, jboolean make_not_entrant))
 814   VM_WhiteBoxDeoptimizeFrames op(make_not_entrant == JNI_TRUE);
 815   VMThread::execute(&op);
 816   return op.result();
 817 WB_END
 818 
 819 WB_ENTRY(void, WB_DeoptimizeAll(JNIEnv* env, jobject o))
 820   MutexLockerEx mu(Compile_lock);
 821   CodeCache::mark_all_nmethods_for_deoptimization();
 822   Deoptimization::deoptimize_all_marked();
 823 WB_END
 824 
 825 WB_ENTRY(jint, WB_DeoptimizeMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 826   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 827   int result = 0;
 828   CHECK_JNI_EXCEPTION_(env, result);
 829   MutexLockerEx mu(Compile_lock);
 830   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 831   if (is_osr) {
 832     result += mh->mark_osr_nmethods();
 833   } else if (mh->code() != NULL) {
 834     mh->code()->mark_for_deoptimization();
 835     ++result;
 836   }
 837   result += CodeCache::mark_for_deoptimization(mh());
 838   if (result > 0) {
 839     Deoptimization::deoptimize_all_marked();
 840   }
 841   return result;
 842 WB_END
 843 
 844 WB_ENTRY(jboolean, WB_IsMethodCompiled(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 845   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 846   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 847   MutexLockerEx mu(Compile_lock);
 848   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 849   CompiledMethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 850   if (code == NULL) {
 851     return JNI_FALSE;
 852   }
 853   return (code->is_alive() && !code->is_marked_for_deoptimization());
 854 WB_END
 855 
 856 WB_ENTRY(jboolean, WB_IsMethodCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 857   if (method == NULL || comp_level > MIN2((CompLevel) TieredStopAtLevel, CompLevel_highest_tier)) {
 858     return false;
 859   }
 860   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 861   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 862   MutexLockerEx mu(Compile_lock);
 863   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 864   if (is_osr) {
 865     return CompilationPolicy::can_be_osr_compiled(mh, comp_level);
 866   } else {
 867     return CompilationPolicy::can_be_compiled(mh, comp_level);
 868   }
 869 WB_END
 870 
 871 WB_ENTRY(jboolean, WB_IsMethodQueuedForCompilation(JNIEnv* env, jobject o, jobject method))
 872   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 873   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 874   MutexLockerEx mu(Compile_lock);
 875   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 876   return mh->queued_for_compilation();
 877 WB_END
 878 
 879 WB_ENTRY(jboolean, WB_IsIntrinsicAvailable(JNIEnv* env, jobject o, jobject method, jobject compilation_context, jint compLevel))
 880   if (compLevel < CompLevel_none || compLevel > MIN2((CompLevel) TieredStopAtLevel, CompLevel_highest_tier)) {
 881     return false; // Intrinsic is not available on a non-existent compilation level.
 882   }
 883   jmethodID method_id, compilation_context_id;
 884   method_id = reflected_method_to_jmid(thread, env, method);
 885   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 886   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(method_id));
 887 
 888   DirectiveSet* directive;
 889   AbstractCompiler* comp = CompileBroker::compiler((int)compLevel);
 890   assert(comp != NULL, "compiler not available");
 891   if (compilation_context != NULL) {
 892     compilation_context_id = reflected_method_to_jmid(thread, env, compilation_context);
 893     CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 894     methodHandle cch(THREAD, Method::checked_resolve_jmethod_id(compilation_context_id));
 895     directive = DirectivesStack::getMatchingDirective(cch, comp);
 896   } else {
 897     // Calling with NULL matches default directive
 898     directive = DirectivesStack::getDefaultDirective(comp);
 899   }
 900   bool result = comp->is_intrinsic_available(mh, directive);
 901   DirectivesStack::release(directive);
 902   return result;
 903 WB_END
 904 
 905 WB_ENTRY(jint, WB_GetMethodCompilationLevel(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 906   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 907   CHECK_JNI_EXCEPTION_(env, CompLevel_none);
 908   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 909   CompiledMethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 910   return (code != NULL ? code->comp_level() : CompLevel_none);
 911 WB_END
 912 
 913 WB_ENTRY(void, WB_MakeMethodNotCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 914   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 915   CHECK_JNI_EXCEPTION(env);
 916   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 917   if (is_osr) {
 918     mh->set_not_osr_compilable(comp_level, true /* report */, "WhiteBox");
 919   } else {
 920     mh->set_not_compilable(comp_level, true /* report */, "WhiteBox");
 921   }
 922 WB_END
 923 
 924 WB_ENTRY(jint, WB_GetMethodEntryBci(JNIEnv* env, jobject o, jobject method))
 925   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 926   CHECK_JNI_EXCEPTION_(env, InvocationEntryBci);
 927   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 928   CompiledMethod* code = mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false);
 929   return (code != NULL && code->is_osr_method() ? code->osr_entry_bci() : InvocationEntryBci);
 930 WB_END
 931 
 932 WB_ENTRY(jboolean, WB_TestSetDontInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
 933   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 934   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 935   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 936   bool result = mh->dont_inline();
 937   mh->set_dont_inline(value == JNI_TRUE);
 938   return result;
 939 WB_END
 940 
 941 WB_ENTRY(jint, WB_GetCompileQueueSize(JNIEnv* env, jobject o, jint comp_level))
 942   if (comp_level == CompLevel_any) {
 943     return CompileBroker::queue_size(CompLevel_full_optimization) /* C2 */ +
 944         CompileBroker::queue_size(CompLevel_full_profile) /* C1 */;
 945   } else {
 946     return CompileBroker::queue_size(comp_level);
 947   }
 948 WB_END
 949 
 950 WB_ENTRY(jboolean, WB_TestSetForceInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
 951   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 952   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 953   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 954   bool result = mh->force_inline();
 955   mh->set_force_inline(value == JNI_TRUE);
 956   return result;
 957 WB_END
 958 
 959 bool WhiteBox::compile_method(Method* method, int comp_level, int bci, Thread* THREAD) {
 960   // Screen for unavailable/bad comp level or null method
 961   AbstractCompiler* comp = CompileBroker::compiler(comp_level);
 962   if (method == NULL) {
 963     tty->print_cr("WB error: request to compile NULL method");
 964     return false;
 965   }
 966   if (comp_level > MIN2((CompLevel) TieredStopAtLevel, CompLevel_highest_tier)) {
 967     tty->print_cr("WB error: invalid compilation level %d", comp_level);
 968     return false;
 969   }
 970   if (comp == NULL) {
 971     tty->print_cr("WB error: no compiler for requested compilation level %d", comp_level);
 972     return false;
 973   }
 974 
 975   // Check if compilation is blocking
 976   methodHandle mh(THREAD, method);
 977   DirectiveSet* directive = DirectivesStack::getMatchingDirective(mh, comp);
 978   bool is_blocking = !directive->BackgroundCompilationOption;
 979   DirectivesStack::release(directive);
 980 
 981   // Compile method and check result
 982   nmethod* nm = CompileBroker::compile_method(mh, bci, comp_level, mh, mh->invocation_count(), CompileTask::Reason_Whitebox, THREAD);
 983   MutexLockerEx mu(Compile_lock);
 984   bool is_queued = mh->queued_for_compilation();
 985   if ((!is_blocking && is_queued) || nm != NULL) {
 986     return true;
 987   }
 988   tty->print("WB error: failed to %s compile at level %d method ", is_blocking ? "blocking" : "", comp_level);
 989   mh->print_short_name(tty);
 990   tty->cr();
 991   if (is_blocking && is_queued) {
 992     tty->print_cr("WB error: blocking compilation is still in queue!");
 993   }
 994   return false;
 995 }
 996 
 997 WB_ENTRY(jboolean, WB_EnqueueMethodForCompilation(JNIEnv* env, jobject o, jobject method, jint comp_level, jint bci))
 998   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 999   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1000   return WhiteBox::compile_method(Method::checked_resolve_jmethod_id(jmid), comp_level, bci, THREAD);
1001 WB_END
1002 
1003 WB_ENTRY(jboolean, WB_EnqueueInitializerForCompilation(JNIEnv* env, jobject o, jclass klass, jint comp_level))
1004   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1005   Method* clinit = ik->class_initializer();
1006   if (clinit == NULL) {
1007     return false;
1008   }
1009   return WhiteBox::compile_method(clinit, comp_level, InvocationEntryBci, THREAD);
1010 WB_END
1011 
1012 WB_ENTRY(jboolean, WB_ShouldPrintAssembly(JNIEnv* env, jobject o, jobject method, jint comp_level))
1013   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1014   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1015 
1016   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1017   DirectiveSet* directive = DirectivesStack::getMatchingDirective(mh, CompileBroker::compiler(comp_level));
1018   bool result = directive->PrintAssemblyOption;
1019   DirectivesStack::release(directive);
1020 
1021   return result;
1022 WB_END
1023 
1024 WB_ENTRY(jint, WB_MatchesInline(JNIEnv* env, jobject o, jobject method, jstring pattern))
1025   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1026   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1027 
1028   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1029 
1030   ResourceMark rm;
1031   const char* error_msg = NULL;
1032   char* method_str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(pattern));
1033   InlineMatcher* m = InlineMatcher::parse_inline_pattern(method_str, error_msg);
1034 
1035   if (m == NULL) {
1036     assert(error_msg != NULL, "Always have an error message");
1037     tty->print_cr("Got error: %s", error_msg);
1038     return -1; // Pattern failed
1039   }
1040 
1041   // Pattern works - now check if it matches
1042   int result;
1043   if (m->match(mh, InlineMatcher::force_inline)) {
1044     result = 2; // Force inline match
1045   } else if (m->match(mh, InlineMatcher::dont_inline)) {
1046     result = 1; // Dont inline match
1047   } else {
1048     result = 0; // No match
1049   }
1050   delete m;
1051   return result;
1052 WB_END
1053 
1054 WB_ENTRY(jint, WB_MatchesMethod(JNIEnv* env, jobject o, jobject method, jstring pattern))
1055   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1056   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1057 
1058   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1059 
1060   ResourceMark rm;
1061   char* method_str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(pattern));
1062 
1063   const char* error_msg = NULL;
1064 
1065   BasicMatcher* m = BasicMatcher::parse_method_pattern(method_str, error_msg);
1066   if (m == NULL) {
1067     assert(error_msg != NULL, "Must have error_msg");
1068     tty->print_cr("Got error: %s", error_msg);
1069     return -1;
1070   }
1071 
1072   // Pattern works - now check if it matches
1073   int result = m->matches(mh);
1074   delete m;
1075   assert(result == 0 || result == 1, "Result out of range");
1076   return result;
1077 WB_END
1078 
1079 WB_ENTRY(void, WB_ClearMethodState(JNIEnv* env, jobject o, jobject method))
1080   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1081   CHECK_JNI_EXCEPTION(env);
1082   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1083   MutexLockerEx mu(Compile_lock);
1084   MethodData* mdo = mh->method_data();
1085   MethodCounters* mcs = mh->method_counters();
1086 
1087   if (mdo != NULL) {
1088     mdo->init();
1089     ResourceMark rm;
1090     int arg_count = mdo->method()->size_of_parameters();
1091     for (int i = 0; i < arg_count; i++) {
1092       mdo->set_arg_modified(i, 0);
1093     }
1094     MutexLockerEx mu(mdo->extra_data_lock());
1095     mdo->clean_method_data(/*always_clean*/true);
1096   }
1097 
1098   mh->clear_not_c1_compilable();
1099   mh->clear_not_c2_compilable();
1100   mh->clear_not_c2_osr_compilable();
1101   NOT_PRODUCT(mh->set_compiled_invocation_count(0));
1102   if (mcs != NULL) {
1103     mcs->backedge_counter()->init();
1104     mcs->invocation_counter()->init();
1105     mcs->set_interpreter_invocation_count(0);
1106     mcs->set_interpreter_throwout_count(0);
1107 
1108 #ifdef TIERED
1109     mcs->set_rate(0.0F);
1110     mh->set_prev_event_count(0);
1111     mh->set_prev_time(0);
1112 #endif
1113   }
1114 WB_END
1115 
1116 template <typename T>
1117 static bool GetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value, JVMFlag::Error (*TAt)(const char*, T*, bool, bool)) {
1118   if (name == NULL) {
1119     return false;
1120   }
1121   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1122   const char* flag_name = env->GetStringUTFChars(name, NULL);
1123   CHECK_JNI_EXCEPTION_(env, false);
1124   JVMFlag::Error result = (*TAt)(flag_name, value, true, true);
1125   env->ReleaseStringUTFChars(name, flag_name);
1126   return (result == JVMFlag::SUCCESS);
1127 }
1128 
1129 template <typename T>
1130 static bool SetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value, JVMFlag::Error (*TAtPut)(const char*, T*, JVMFlag::Flags)) {
1131   if (name == NULL) {
1132     return false;
1133   }
1134   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1135   const char* flag_name = env->GetStringUTFChars(name, NULL);
1136   CHECK_JNI_EXCEPTION_(env, false);
1137   JVMFlag::Error result = (*TAtPut)(flag_name, value, JVMFlag::INTERNAL);
1138   env->ReleaseStringUTFChars(name, flag_name);
1139   return (result == JVMFlag::SUCCESS);
1140 }
1141 
1142 template <typename T>
1143 static jobject box(JavaThread* thread, JNIEnv* env, Symbol* name, Symbol* sig, T value) {
1144   ResourceMark rm(thread);
1145   jclass clazz = env->FindClass(name->as_C_string());
1146   CHECK_JNI_EXCEPTION_(env, NULL);
1147   jmethodID methodID = env->GetStaticMethodID(clazz,
1148         vmSymbols::valueOf_name()->as_C_string(),
1149         sig->as_C_string());
1150   CHECK_JNI_EXCEPTION_(env, NULL);
1151   jobject result = env->CallStaticObjectMethod(clazz, methodID, value);
1152   CHECK_JNI_EXCEPTION_(env, NULL);
1153   return result;
1154 }
1155 
1156 static jobject booleanBox(JavaThread* thread, JNIEnv* env, jboolean value) {
1157   return box(thread, env, vmSymbols::java_lang_Boolean(), vmSymbols::Boolean_valueOf_signature(), value);
1158 }
1159 static jobject integerBox(JavaThread* thread, JNIEnv* env, jint value) {
1160   return box(thread, env, vmSymbols::java_lang_Integer(), vmSymbols::Integer_valueOf_signature(), value);
1161 }
1162 static jobject longBox(JavaThread* thread, JNIEnv* env, jlong value) {
1163   return box(thread, env, vmSymbols::java_lang_Long(), vmSymbols::Long_valueOf_signature(), value);
1164 }
1165 /* static jobject floatBox(JavaThread* thread, JNIEnv* env, jfloat value) {
1166   return box(thread, env, vmSymbols::java_lang_Float(), vmSymbols::Float_valueOf_signature(), value);
1167 }*/
1168 static jobject doubleBox(JavaThread* thread, JNIEnv* env, jdouble value) {
1169   return box(thread, env, vmSymbols::java_lang_Double(), vmSymbols::Double_valueOf_signature(), value);
1170 }
1171 
1172 static JVMFlag* getVMFlag(JavaThread* thread, JNIEnv* env, jstring name) {
1173   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1174   const char* flag_name = env->GetStringUTFChars(name, NULL);
1175   CHECK_JNI_EXCEPTION_(env, NULL);
1176   JVMFlag* result = JVMFlag::find_flag(flag_name, strlen(flag_name), true, true);
1177   env->ReleaseStringUTFChars(name, flag_name);
1178   return result;
1179 }
1180 
1181 WB_ENTRY(jboolean, WB_IsConstantVMFlag(JNIEnv* env, jobject o, jstring name))
1182   JVMFlag* flag = getVMFlag(thread, env, name);
1183   return (flag != NULL) && flag->is_constant_in_binary();
1184 WB_END
1185 
1186 WB_ENTRY(jboolean, WB_IsLockedVMFlag(JNIEnv* env, jobject o, jstring name))
1187   JVMFlag* flag = getVMFlag(thread, env, name);
1188   return (flag != NULL) && !(flag->is_unlocked() || flag->is_unlocker());
1189 WB_END
1190 
1191 WB_ENTRY(jobject, WB_GetBooleanVMFlag(JNIEnv* env, jobject o, jstring name))
1192   bool result;
1193   if (GetVMFlag <bool> (thread, env, name, &result, &JVMFlag::boolAt)) {
1194     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1195     return booleanBox(thread, env, result);
1196   }
1197   return NULL;
1198 WB_END
1199 
1200 WB_ENTRY(jobject, WB_GetIntVMFlag(JNIEnv* env, jobject o, jstring name))
1201   int result;
1202   if (GetVMFlag <int> (thread, env, name, &result, &JVMFlag::intAt)) {
1203     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1204     return longBox(thread, env, result);
1205   }
1206   return NULL;
1207 WB_END
1208 
1209 WB_ENTRY(jobject, WB_GetUintVMFlag(JNIEnv* env, jobject o, jstring name))
1210   uint result;
1211   if (GetVMFlag <uint> (thread, env, name, &result, &JVMFlag::uintAt)) {
1212     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1213     return longBox(thread, env, result);
1214   }
1215   return NULL;
1216 WB_END
1217 
1218 WB_ENTRY(jobject, WB_GetIntxVMFlag(JNIEnv* env, jobject o, jstring name))
1219   intx result;
1220   if (GetVMFlag <intx> (thread, env, name, &result, &JVMFlag::intxAt)) {
1221     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1222     return longBox(thread, env, result);
1223   }
1224   return NULL;
1225 WB_END
1226 
1227 WB_ENTRY(jobject, WB_GetUintxVMFlag(JNIEnv* env, jobject o, jstring name))
1228   uintx result;
1229   if (GetVMFlag <uintx> (thread, env, name, &result, &JVMFlag::uintxAt)) {
1230     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1231     return longBox(thread, env, result);
1232   }
1233   return NULL;
1234 WB_END
1235 
1236 WB_ENTRY(jobject, WB_GetUint64VMFlag(JNIEnv* env, jobject o, jstring name))
1237   uint64_t result;
1238   if (GetVMFlag <uint64_t> (thread, env, name, &result, &JVMFlag::uint64_tAt)) {
1239     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1240     return longBox(thread, env, result);
1241   }
1242   return NULL;
1243 WB_END
1244 
1245 WB_ENTRY(jobject, WB_GetSizeTVMFlag(JNIEnv* env, jobject o, jstring name))
1246   size_t result;
1247   if (GetVMFlag <size_t> (thread, env, name, &result, &JVMFlag::size_tAt)) {
1248     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1249     return longBox(thread, env, result);
1250   }
1251   return NULL;
1252 WB_END
1253 
1254 WB_ENTRY(jobject, WB_GetDoubleVMFlag(JNIEnv* env, jobject o, jstring name))
1255   double result;
1256   if (GetVMFlag <double> (thread, env, name, &result, &JVMFlag::doubleAt)) {
1257     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1258     return doubleBox(thread, env, result);
1259   }
1260   return NULL;
1261 WB_END
1262 
1263 WB_ENTRY(jstring, WB_GetStringVMFlag(JNIEnv* env, jobject o, jstring name))
1264   ccstr ccstrResult;
1265   if (GetVMFlag <ccstr> (thread, env, name, &ccstrResult, &JVMFlag::ccstrAt)) {
1266     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1267     jstring result = env->NewStringUTF(ccstrResult);
1268     CHECK_JNI_EXCEPTION_(env, NULL);
1269     return result;
1270   }
1271   return NULL;
1272 WB_END
1273 
1274 WB_ENTRY(void, WB_SetBooleanVMFlag(JNIEnv* env, jobject o, jstring name, jboolean value))
1275   bool result = value == JNI_TRUE ? true : false;
1276   SetVMFlag <bool> (thread, env, name, &result, &JVMFlag::boolAtPut);
1277 WB_END
1278 
1279 WB_ENTRY(void, WB_SetIntVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1280   int result = value;
1281   SetVMFlag <int> (thread, env, name, &result, &JVMFlag::intAtPut);
1282 WB_END
1283 
1284 WB_ENTRY(void, WB_SetUintVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1285   uint result = value;
1286   SetVMFlag <uint> (thread, env, name, &result, &JVMFlag::uintAtPut);
1287 WB_END
1288 
1289 WB_ENTRY(void, WB_SetIntxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1290   intx result = value;
1291   SetVMFlag <intx> (thread, env, name, &result, &JVMFlag::intxAtPut);
1292 WB_END
1293 
1294 WB_ENTRY(void, WB_SetUintxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1295   uintx result = value;
1296   SetVMFlag <uintx> (thread, env, name, &result, &JVMFlag::uintxAtPut);
1297 WB_END
1298 
1299 WB_ENTRY(void, WB_SetUint64VMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1300   uint64_t result = value;
1301   SetVMFlag <uint64_t> (thread, env, name, &result, &JVMFlag::uint64_tAtPut);
1302 WB_END
1303 
1304 WB_ENTRY(void, WB_SetSizeTVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1305   size_t result = value;
1306   SetVMFlag <size_t> (thread, env, name, &result, &JVMFlag::size_tAtPut);
1307 WB_END
1308 
1309 WB_ENTRY(void, WB_SetDoubleVMFlag(JNIEnv* env, jobject o, jstring name, jdouble value))
1310   double result = value;
1311   SetVMFlag <double> (thread, env, name, &result, &JVMFlag::doubleAtPut);
1312 WB_END
1313 
1314 WB_ENTRY(void, WB_SetStringVMFlag(JNIEnv* env, jobject o, jstring name, jstring value))
1315   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1316   const char* ccstrValue;
1317   if (value == NULL) {
1318     ccstrValue = NULL;
1319   }
1320   else {
1321     ccstrValue = env->GetStringUTFChars(value, NULL);
1322     CHECK_JNI_EXCEPTION(env);
1323   }
1324   ccstr ccstrResult = ccstrValue;
1325   bool needFree;
1326   {
1327     ThreadInVMfromNative ttvfn(thread); // back to VM
1328     needFree = SetVMFlag <ccstr> (thread, env, name, &ccstrResult, &JVMFlag::ccstrAtPut);
1329   }
1330   if (value != NULL) {
1331     env->ReleaseStringUTFChars(value, ccstrValue);
1332   }
1333   if (needFree) {
1334     FREE_C_HEAP_ARRAY(char, ccstrResult);
1335   }
1336 WB_END
1337 
1338 WB_ENTRY(void, WB_LockCompilation(JNIEnv* env, jobject o, jlong timeout))
1339   WhiteBox::compilation_locked = true;
1340 WB_END
1341 
1342 WB_ENTRY(void, WB_UnlockCompilation(JNIEnv* env, jobject o))
1343   MonitorLockerEx mo(Compilation_lock, Mutex::_no_safepoint_check_flag);
1344   WhiteBox::compilation_locked = false;
1345   mo.notify_all();
1346 WB_END
1347 
1348 WB_ENTRY(void, WB_ForceNMethodSweep(JNIEnv* env, jobject o))
1349   // Force a code cache sweep and block until it finished
1350   NMethodSweeper::force_sweep();
1351 WB_END
1352 
1353 WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString))
1354   ResourceMark rm(THREAD);
1355   int len;
1356   jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len, CHECK_false);
1357   return (StringTable::lookup(name, len) != NULL);
1358 WB_END
1359 
1360 WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o))
1361   Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(true);
1362   Universe::heap()->collect(GCCause::_wb_full_gc);
1363 #if INCLUDE_G1GC
1364   if (UseG1GC) {
1365     // Needs to be cleared explicitly for G1
1366     Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(false);
1367   }
1368 #endif // INCLUDE_G1GC
1369 WB_END
1370 
1371 WB_ENTRY(void, WB_YoungGC(JNIEnv* env, jobject o))
1372   Universe::heap()->collect(GCCause::_wb_young_gc);
1373 WB_END
1374 
1375 WB_ENTRY(void, WB_ReadReservedMemory(JNIEnv* env, jobject o))
1376   // static+volatile in order to force the read to happen
1377   // (not be eliminated by the compiler)
1378   static char c;
1379   static volatile char* p;
1380 
1381   p = os::reserve_memory(os::vm_allocation_granularity(), NULL, 0);
1382   if (p == NULL) {
1383     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Failed to reserve memory");
1384   }
1385 
1386   c = *p;
1387 WB_END
1388 
1389 WB_ENTRY(jstring, WB_GetCPUFeatures(JNIEnv* env, jobject o))
1390   const char* features = VM_Version::features_string();
1391   ThreadToNativeFromVM ttn(thread);
1392   jstring features_string = env->NewStringUTF(features);
1393 
1394   CHECK_JNI_EXCEPTION_(env, NULL);
1395 
1396   return features_string;
1397 WB_END
1398 
1399 int WhiteBox::get_blob_type(const CodeBlob* code) {
1400   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1401   if (code->is_aot()) {
1402     return -1;
1403   }
1404   return CodeCache::get_code_heap(code)->code_blob_type();
1405 }
1406 
1407 CodeHeap* WhiteBox::get_code_heap(int blob_type) {
1408   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1409   return CodeCache::get_code_heap(blob_type);
1410 }
1411 
1412 struct CodeBlobStub {
1413   CodeBlobStub(const CodeBlob* blob) :
1414       name(os::strdup(blob->name())),
1415       size(blob->size()),
1416       blob_type(WhiteBox::get_blob_type(blob)),
1417       address((jlong) blob) { }
1418   ~CodeBlobStub() { os::free((void*) name); }
1419   const char* const name;
1420   const jint        size;
1421   const jint        blob_type;
1422   const jlong       address;
1423 };
1424 
1425 static jobjectArray codeBlob2objectArray(JavaThread* thread, JNIEnv* env, CodeBlobStub* cb) {
1426   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1427   CHECK_JNI_EXCEPTION_(env, NULL);
1428   jobjectArray result = env->NewObjectArray(4, clazz, NULL);
1429 
1430   jstring name = env->NewStringUTF(cb->name);
1431   CHECK_JNI_EXCEPTION_(env, NULL);
1432   env->SetObjectArrayElement(result, 0, name);
1433 
1434   jobject obj = integerBox(thread, env, cb->size);
1435   CHECK_JNI_EXCEPTION_(env, NULL);
1436   env->SetObjectArrayElement(result, 1, obj);
1437 
1438   obj = integerBox(thread, env, cb->blob_type);
1439   CHECK_JNI_EXCEPTION_(env, NULL);
1440   env->SetObjectArrayElement(result, 2, obj);
1441 
1442   obj = longBox(thread, env, cb->address);
1443   CHECK_JNI_EXCEPTION_(env, NULL);
1444   env->SetObjectArrayElement(result, 3, obj);
1445 
1446   return result;
1447 }
1448 
1449 WB_ENTRY(jobjectArray, WB_GetNMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
1450   ResourceMark rm(THREAD);
1451   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1452   CHECK_JNI_EXCEPTION_(env, NULL);
1453   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1454   CompiledMethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
1455   jobjectArray result = NULL;
1456   if (code == NULL) {
1457     return result;
1458   }
1459   int comp_level = code->comp_level();
1460   int insts_size = comp_level == CompLevel_aot ? code->code_end() - code->code_begin() : code->insts_size();
1461 
1462   ThreadToNativeFromVM ttn(thread);
1463   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1464   CHECK_JNI_EXCEPTION_(env, NULL);
1465   result = env->NewObjectArray(5, clazz, NULL);
1466   if (result == NULL) {
1467     return result;
1468   }
1469 
1470   CodeBlobStub stub(code);
1471   jobjectArray codeBlob = codeBlob2objectArray(thread, env, &stub);
1472   CHECK_JNI_EXCEPTION_(env, NULL);
1473   env->SetObjectArrayElement(result, 0, codeBlob);
1474 
1475   jobject level = integerBox(thread, env, comp_level);
1476   CHECK_JNI_EXCEPTION_(env, NULL);
1477   env->SetObjectArrayElement(result, 1, level);
1478 
1479   jbyteArray insts = env->NewByteArray(insts_size);
1480   CHECK_JNI_EXCEPTION_(env, NULL);
1481   env->SetByteArrayRegion(insts, 0, insts_size, (jbyte*) code->insts_begin());
1482   env->SetObjectArrayElement(result, 2, insts);
1483 
1484   jobject id = integerBox(thread, env, code->compile_id());
1485   CHECK_JNI_EXCEPTION_(env, NULL);
1486   env->SetObjectArrayElement(result, 3, id);
1487 
1488   jobject entry_point = longBox(thread, env, (jlong) code->entry_point());
1489   CHECK_JNI_EXCEPTION_(env, NULL);
1490   env->SetObjectArrayElement(result, 4, entry_point);
1491 
1492   return result;
1493 WB_END
1494 
1495 CodeBlob* WhiteBox::allocate_code_blob(int size, int blob_type) {
1496   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1497   BufferBlob* blob;
1498   int full_size = CodeBlob::align_code_offset(sizeof(BufferBlob));
1499   if (full_size < size) {
1500     full_size += align_up(size - full_size, oopSize);
1501   }
1502   {
1503     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1504     blob = (BufferBlob*) CodeCache::allocate(full_size, blob_type);
1505     if (blob != NULL) {
1506       ::new (blob) BufferBlob("WB::DummyBlob", full_size);
1507     }
1508   }
1509   // Track memory usage statistic after releasing CodeCache_lock
1510   MemoryService::track_code_cache_memory_usage();
1511   return blob;
1512 }
1513 
1514 WB_ENTRY(jlong, WB_AllocateCodeBlob(JNIEnv* env, jobject o, jint size, jint blob_type))
1515   if (size < 0) {
1516     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1517       err_msg("WB_AllocateCodeBlob: size is negative: " INT32_FORMAT, size));
1518   }
1519   return (jlong) WhiteBox::allocate_code_blob(size, blob_type);
1520 WB_END
1521 
1522 WB_ENTRY(void, WB_FreeCodeBlob(JNIEnv* env, jobject o, jlong addr))
1523   if (addr == 0) {
1524     return;
1525   }
1526   BufferBlob::free((BufferBlob*) addr);
1527 WB_END
1528 
1529 WB_ENTRY(jobjectArray, WB_GetCodeHeapEntries(JNIEnv* env, jobject o, jint blob_type))
1530   ResourceMark rm;
1531   GrowableArray<CodeBlobStub*> blobs;
1532   {
1533     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1534     CodeHeap* heap = WhiteBox::get_code_heap(blob_type);
1535     if (heap == NULL) {
1536       return NULL;
1537     }
1538     for (CodeBlob* cb = (CodeBlob*) heap->first();
1539          cb != NULL; cb = (CodeBlob*) heap->next(cb)) {
1540       CodeBlobStub* stub = NEW_RESOURCE_OBJ(CodeBlobStub);
1541       new (stub) CodeBlobStub(cb);
1542       blobs.append(stub);
1543     }
1544   }
1545   ThreadToNativeFromVM ttn(thread);
1546   jobjectArray result = NULL;
1547   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1548   CHECK_JNI_EXCEPTION_(env, NULL);
1549   result = env->NewObjectArray(blobs.length(), clazz, NULL);
1550   CHECK_JNI_EXCEPTION_(env, NULL);
1551   if (result == NULL) {
1552     return result;
1553   }
1554   int i = 0;
1555   for (GrowableArrayIterator<CodeBlobStub*> it = blobs.begin();
1556        it != blobs.end(); ++it) {
1557     jobjectArray obj = codeBlob2objectArray(thread, env, *it);
1558     CHECK_JNI_EXCEPTION_(env, NULL);
1559     env->SetObjectArrayElement(result, i, obj);
1560     CHECK_JNI_EXCEPTION_(env, NULL);
1561     ++i;
1562   }
1563   return result;
1564 WB_END
1565 
1566 WB_ENTRY(jint, WB_GetCompilationActivityMode(JNIEnv* env, jobject o))
1567   return CompileBroker::get_compilation_activity_mode();
1568 WB_END
1569 
1570 WB_ENTRY(jobjectArray, WB_GetCodeBlob(JNIEnv* env, jobject o, jlong addr))
1571   if (addr == 0) {
1572     THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(),
1573       "WB_GetCodeBlob: addr is null");
1574   }
1575   ThreadToNativeFromVM ttn(thread);
1576   CodeBlobStub stub((CodeBlob*) addr);
1577   return codeBlob2objectArray(thread, env, &stub);
1578 WB_END
1579 
1580 WB_ENTRY(jlong, WB_GetMethodData(JNIEnv* env, jobject wv, jobject method))
1581   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1582   CHECK_JNI_EXCEPTION_(env, 0);
1583   methodHandle mh(thread, Method::checked_resolve_jmethod_id(jmid));
1584   return (jlong) mh->method_data();
1585 WB_END
1586 
1587 WB_ENTRY(jlong, WB_GetThreadStackSize(JNIEnv* env, jobject o))
1588   return (jlong) Thread::current()->stack_size();
1589 WB_END
1590 
1591 WB_ENTRY(jlong, WB_GetThreadRemainingStackSize(JNIEnv* env, jobject o))
1592   JavaThread* t = JavaThread::current();
1593   return (jlong) t->stack_available(os::current_stack_pointer()) - (jlong)JavaThread::stack_shadow_zone_size();
1594 WB_END
1595 
1596 
1597 int WhiteBox::array_bytes_to_length(size_t bytes) {
1598   return Array<u1>::bytes_to_length(bytes);
1599 }
1600 
1601 WB_ENTRY(jlong, WB_AllocateMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong size))
1602   if (size < 0) {
1603     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1604         err_msg("WB_AllocateMetaspace: size is negative: " JLONG_FORMAT, size));
1605   }
1606 
1607   oop class_loader_oop = JNIHandles::resolve(class_loader);
1608   ClassLoaderData* cld = class_loader_oop != NULL
1609       ? java_lang_ClassLoader::loader_data_acquire(class_loader_oop)
1610       : ClassLoaderData::the_null_class_loader_data();
1611 
1612   void* metadata = MetadataFactory::new_array<u1>(cld, WhiteBox::array_bytes_to_length((size_t)size), thread);
1613 
1614   return (jlong)(uintptr_t)metadata;
1615 WB_END
1616 
1617 WB_ENTRY(void, WB_FreeMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong addr, jlong size))
1618   oop class_loader_oop = JNIHandles::resolve(class_loader);
1619   ClassLoaderData* cld = class_loader_oop != NULL
1620       ? java_lang_ClassLoader::loader_data_acquire(class_loader_oop)
1621       : ClassLoaderData::the_null_class_loader_data();
1622 
1623   MetadataFactory::free_array(cld, (Array<u1>*)(uintptr_t)addr);
1624 WB_END
1625 
1626 WB_ENTRY(void, WB_DefineModule(JNIEnv* env, jobject o, jobject module, jboolean is_open,
1627                                 jstring version, jstring location, jobjectArray packages))
1628   ResourceMark rm(THREAD);
1629 
1630   objArrayOop packages_oop = objArrayOop(JNIHandles::resolve(packages));
1631   objArrayHandle packages_h(THREAD, packages_oop);
1632   int num_packages = (packages_h == NULL ? 0 : packages_h->length());
1633 
1634   char** pkgs = NULL;
1635   if (num_packages > 0) {
1636     pkgs = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char*, num_packages);
1637     for (int x = 0; x < num_packages; x++) {
1638       oop pkg_str = packages_h->obj_at(x);
1639       if (pkg_str == NULL || !pkg_str->is_a(SystemDictionary::String_klass())) {
1640         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1641                   err_msg("Bad package name"));
1642       }
1643       pkgs[x] = java_lang_String::as_utf8_string(pkg_str);
1644     }
1645   }
1646   Modules::define_module(module, is_open, version, location, (const char* const*)pkgs, num_packages, CHECK);
1647 WB_END
1648 
1649 WB_ENTRY(void, WB_AddModuleExports(JNIEnv* env, jobject o, jobject from_module, jstring package, jobject to_module))
1650   ResourceMark rm(THREAD);
1651   char* package_name = NULL;
1652   if (package != NULL) {
1653       package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
1654   }
1655   Modules::add_module_exports_qualified(from_module, package_name, to_module, CHECK);
1656 WB_END
1657 
1658 WB_ENTRY(void, WB_AddModuleExportsToAllUnnamed(JNIEnv* env, jobject o, jclass module, jstring package))
1659   ResourceMark rm(THREAD);
1660   char* package_name = NULL;
1661   if (package != NULL) {
1662       package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
1663   }
1664   Modules::add_module_exports_to_all_unnamed(module, package_name, CHECK);
1665 WB_END
1666 
1667 WB_ENTRY(void, WB_AddModuleExportsToAll(JNIEnv* env, jobject o, jclass module, jstring package))
1668   ResourceMark rm(THREAD);
1669   char* package_name = NULL;
1670   if (package != NULL) {
1671       package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
1672   }
1673   Modules::add_module_exports(module, package_name, NULL, CHECK);
1674 WB_END
1675 
1676 WB_ENTRY(void, WB_AddReadsModule(JNIEnv* env, jobject o, jobject from_module, jobject source_module))
1677   Modules::add_reads_module(from_module, source_module, CHECK);
1678 WB_END
1679 
1680 WB_ENTRY(jlong, WB_IncMetaspaceCapacityUntilGC(JNIEnv* env, jobject wb, jlong inc))
1681   if (inc < 0) {
1682     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1683         err_msg("WB_IncMetaspaceCapacityUntilGC: inc is negative: " JLONG_FORMAT, inc));
1684   }
1685 
1686   jlong max_size_t = (jlong) ((size_t) -1);
1687   if (inc > max_size_t) {
1688     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1689         err_msg("WB_IncMetaspaceCapacityUntilGC: inc does not fit in size_t: " JLONG_FORMAT, inc));
1690   }
1691 
1692   size_t new_cap_until_GC = 0;
1693   size_t aligned_inc = align_down((size_t) inc, Metaspace::commit_alignment());
1694   bool success = MetaspaceGC::inc_capacity_until_GC(aligned_inc, &new_cap_until_GC);
1695   if (!success) {
1696     THROW_MSG_0(vmSymbols::java_lang_IllegalStateException(),
1697                 "WB_IncMetaspaceCapacityUntilGC: could not increase capacity until GC "
1698                 "due to contention with another thread");
1699   }
1700   return (jlong) new_cap_until_GC;
1701 WB_END
1702 
1703 WB_ENTRY(jlong, WB_MetaspaceCapacityUntilGC(JNIEnv* env, jobject wb))
1704   return (jlong) MetaspaceGC::capacity_until_GC();
1705 WB_END
1706 
1707 WB_ENTRY(jboolean, WB_MetaspaceShouldConcurrentCollect(JNIEnv* env, jobject wb))
1708   return MetaspaceGC::should_concurrent_collect();
1709 WB_END
1710 
1711 WB_ENTRY(jlong, WB_MetaspaceReserveAlignment(JNIEnv* env, jobject wb))
1712   return (jlong)Metaspace::reserve_alignment();
1713 WB_END
1714 
1715 WB_ENTRY(void, WB_AssertMatchingSafepointCalls(JNIEnv* env, jobject o, jboolean mutexSafepointValue, jboolean attemptedNoSafepointValue))
1716   Monitor::SafepointCheckRequired sfpt_check_required = mutexSafepointValue ?
1717                                            Monitor::_safepoint_check_always :
1718                                            Monitor::_safepoint_check_never;
1719   MutexLockerEx ml(new Mutex(Mutex::leaf, "SFPT_Test_lock", true, sfpt_check_required),
1720                    attemptedNoSafepointValue == JNI_TRUE);
1721 WB_END
1722 
1723 WB_ENTRY(jboolean, WB_IsMonitorInflated(JNIEnv* env, jobject wb, jobject obj))
1724   oop obj_oop = JNIHandles::resolve(obj);
1725   return (jboolean) obj_oop->mark()->has_monitor();
1726 WB_END
1727 
1728 WB_ENTRY(void, WB_ForceSafepoint(JNIEnv* env, jobject wb))
1729   VM_ForceSafepoint force_safepoint_op;
1730   VMThread::execute(&force_safepoint_op);
1731 WB_END
1732 
1733 WB_ENTRY(jlong, WB_GetConstantPool(JNIEnv* env, jobject wb, jclass klass))
1734   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1735   return (jlong) ik->constants();
1736 WB_END
1737 
1738 WB_ENTRY(jint, WB_GetConstantPoolCacheIndexTag(JNIEnv* env, jobject wb))
1739   return ConstantPool::CPCACHE_INDEX_TAG;
1740 WB_END
1741 
1742 WB_ENTRY(jint, WB_GetConstantPoolCacheLength(JNIEnv* env, jobject wb, jclass klass))
1743   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1744   ConstantPool* cp = ik->constants();
1745   if (cp->cache() == NULL) {
1746       return -1;
1747   }
1748   return cp->cache()->length();
1749 WB_END
1750 
1751 WB_ENTRY(jint, WB_ConstantPoolRemapInstructionOperandFromCache(JNIEnv* env, jobject wb, jclass klass, jint index))
1752   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1753   ConstantPool* cp = ik->constants();
1754   if (cp->cache() == NULL) {
1755     THROW_MSG_0(vmSymbols::java_lang_IllegalStateException(), "Constant pool does not have a cache");
1756   }
1757   jint cpci = index;
1758   jint cpciTag = ConstantPool::CPCACHE_INDEX_TAG;
1759   if (cpciTag > cpci || cpci >= cp->cache()->length() + cpciTag) {
1760     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool cache index is out of range");
1761   }
1762   jint cpi = cp->remap_instruction_operand_from_cache(cpci);
1763   return cpi;
1764 WB_END
1765 
1766 WB_ENTRY(jint, WB_ConstantPoolEncodeIndyIndex(JNIEnv* env, jobject wb, jint index))
1767   return ConstantPool::encode_invokedynamic_index(index);
1768 WB_END
1769 
1770 WB_ENTRY(void, WB_ClearInlineCaches(JNIEnv* env, jobject wb, jboolean preserve_static_stubs))
1771   VM_ClearICs clear_ics(preserve_static_stubs == JNI_TRUE);
1772   VMThread::execute(&clear_ics);
1773 WB_END
1774 
1775 template <typename T>
1776 static bool GetMethodOption(JavaThread* thread, JNIEnv* env, jobject method, jstring name, T* value) {
1777   assert(value != NULL, "sanity");
1778   if (method == NULL || name == NULL) {
1779     return false;
1780   }
1781   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1782   CHECK_JNI_EXCEPTION_(env, false);
1783   methodHandle mh(thread, Method::checked_resolve_jmethod_id(jmid));
1784   // can't be in VM when we call JNI
1785   ThreadToNativeFromVM ttnfv(thread);
1786   const char* flag_name = env->GetStringUTFChars(name, NULL);
1787   CHECK_JNI_EXCEPTION_(env, false);
1788   bool result =  CompilerOracle::has_option_value(mh, flag_name, *value);
1789   env->ReleaseStringUTFChars(name, flag_name);
1790   return result;
1791 }
1792 
1793 WB_ENTRY(jobject, WB_GetMethodBooleaneOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1794   bool result;
1795   if (GetMethodOption<bool> (thread, env, method, name, &result)) {
1796     // can't be in VM when we call JNI
1797     ThreadToNativeFromVM ttnfv(thread);
1798     return booleanBox(thread, env, result);
1799   }
1800   return NULL;
1801 WB_END
1802 
1803 WB_ENTRY(jobject, WB_GetMethodIntxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1804   intx result;
1805   if (GetMethodOption <intx> (thread, env, method, name, &result)) {
1806     // can't be in VM when we call JNI
1807     ThreadToNativeFromVM ttnfv(thread);
1808     return longBox(thread, env, result);
1809   }
1810   return NULL;
1811 WB_END
1812 
1813 WB_ENTRY(jobject, WB_GetMethodUintxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1814   uintx result;
1815   if (GetMethodOption <uintx> (thread, env, method, name, &result)) {
1816     // can't be in VM when we call JNI
1817     ThreadToNativeFromVM ttnfv(thread);
1818     return longBox(thread, env, result);
1819   }
1820   return NULL;
1821 WB_END
1822 
1823 WB_ENTRY(jobject, WB_GetMethodDoubleOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1824   double result;
1825   if (GetMethodOption <double> (thread, env, method, name, &result)) {
1826     // can't be in VM when we call JNI
1827     ThreadToNativeFromVM ttnfv(thread);
1828     return doubleBox(thread, env, result);
1829   }
1830   return NULL;
1831 WB_END
1832 
1833 WB_ENTRY(jobject, WB_GetMethodStringOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1834   ccstr ccstrResult;
1835   if (GetMethodOption <ccstr> (thread, env, method, name, &ccstrResult)) {
1836     // can't be in VM when we call JNI
1837     ThreadToNativeFromVM ttnfv(thread);
1838     jstring result = env->NewStringUTF(ccstrResult);
1839     CHECK_JNI_EXCEPTION_(env, NULL);
1840     return result;
1841   }
1842   return NULL;
1843 WB_END
1844 
1845 WB_ENTRY(jobject, WB_GetDefaultArchivePath(JNIEnv* env, jobject wb))
1846   const char* p = Arguments::get_default_shared_archive_path();
1847   ThreadToNativeFromVM ttn(thread);
1848   jstring path_string = env->NewStringUTF(p);
1849 
1850   CHECK_JNI_EXCEPTION_(env, NULL);
1851 
1852   return path_string;
1853 WB_END
1854 
1855 WB_ENTRY(jboolean, WB_IsSharingEnabled(JNIEnv* env, jobject wb))
1856   return UseSharedSpaces;
1857 WB_END
1858 
1859 WB_ENTRY(jboolean, WB_IsShared(JNIEnv* env, jobject wb, jobject obj))
1860   oop obj_oop = JNIHandles::resolve(obj);
1861   return HeapShared::is_archived_object(obj_oop);
1862 WB_END
1863 
1864 WB_ENTRY(jboolean, WB_IsSharedClass(JNIEnv* env, jobject wb, jclass clazz))
1865   return (jboolean)MetaspaceShared::is_in_shared_metaspace(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
1866 WB_END
1867 
1868 WB_ENTRY(jboolean, WB_AreSharedStringsIgnored(JNIEnv* env))
1869   return !HeapShared::closed_archive_heap_region_mapped();
1870 WB_END
1871 
1872 WB_ENTRY(jobject, WB_GetResolvedReferences(JNIEnv* env, jobject wb, jclass clazz))
1873   Klass *k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
1874   if (k->is_instance_klass()) {
1875     InstanceKlass *ik = InstanceKlass::cast(k);
1876     ConstantPool *cp = ik->constants();
1877     objArrayOop refs =  cp->resolved_references();
1878     return (jobject)JNIHandles::make_local(env, refs);
1879   } else {
1880     return NULL;
1881   }
1882 WB_END
1883 
1884 WB_ENTRY(jboolean, WB_AreOpenArchiveHeapObjectsMapped(JNIEnv* env))
1885   return HeapShared::open_archive_heap_region_mapped();
1886 WB_END
1887 
1888 WB_ENTRY(jboolean, WB_IsCDSIncludedInVmBuild(JNIEnv* env))
1889 #if INCLUDE_CDS
1890 # ifdef _LP64
1891     if (!UseCompressedOops || !UseCompressedClassPointers) {
1892       // On 64-bit VMs, CDS is supported only with compressed oops/pointers
1893       return false;
1894     }
1895 # endif // _LP64
1896   return true;
1897 #else
1898   return false;
1899 #endif // INCLUDE_CDS
1900 WB_END
1901 
1902 WB_ENTRY(jboolean, WB_IsJavaHeapArchiveSupported(JNIEnv* env))
1903   return HeapShared::is_heap_object_archiving_allowed();
1904 WB_END
1905 
1906 
1907 WB_ENTRY(jboolean, WB_IsJFRIncludedInVmBuild(JNIEnv* env))
1908 #if INCLUDE_JFR
1909   return true;
1910 #else
1911   return false;
1912 #endif // INCLUDE_JFR
1913 WB_END
1914 
1915 #if INCLUDE_CDS
1916 
1917 WB_ENTRY(jint, WB_GetOffsetForName(JNIEnv* env, jobject o, jstring name))
1918   ResourceMark rm;
1919   char* c_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
1920   int result = CDSOffsets::find_offset(c_name);
1921   return (jint)result;
1922 WB_END
1923 
1924 #endif // INCLUDE_CDS
1925 
1926 WB_ENTRY(jint, WB_HandshakeWalkStack(JNIEnv* env, jobject wb, jobject thread_handle, jboolean all_threads))
1927   class TraceSelfClosure : public ThreadClosure {
1928     jint _num_threads_completed;
1929 
1930     void do_thread(Thread* th) {
1931       assert(th->is_Java_thread(), "sanity");
1932       JavaThread* jt = (JavaThread*)th;
1933       ResourceMark rm;
1934 
1935       jt->print_on(tty);
1936       jt->print_stack_on(tty);
1937       tty->cr();
1938       Atomic::inc(&_num_threads_completed);
1939     }
1940 
1941   public:
1942     TraceSelfClosure() : _num_threads_completed(0) {}
1943 
1944     jint num_threads_completed() const { return _num_threads_completed; }
1945   };
1946   TraceSelfClosure tsc;
1947 
1948   if (all_threads) {
1949     Handshake::execute(&tsc);
1950   } else {
1951     oop thread_oop = JNIHandles::resolve(thread_handle);
1952     if (thread_oop != NULL) {
1953       JavaThread* target = java_lang_Thread::thread(thread_oop);
1954       Handshake::execute(&tsc, target);
1955     }
1956   }
1957   return tsc.num_threads_completed();
1958 WB_END
1959 
1960 //Some convenience methods to deal with objects from java
1961 int WhiteBox::offset_for_field(const char* field_name, oop object,
1962     Symbol* signature_symbol) {
1963   assert(field_name != NULL && strlen(field_name) > 0, "Field name not valid");
1964   Thread* THREAD = Thread::current();
1965 
1966   //Get the class of our object
1967   Klass* arg_klass = object->klass();
1968   //Turn it into an instance-klass
1969   InstanceKlass* ik = InstanceKlass::cast(arg_klass);
1970 
1971   //Create symbols to look for in the class
1972   TempNewSymbol name_symbol = SymbolTable::lookup(field_name, (int) strlen(field_name),
1973       THREAD);
1974 
1975   //To be filled in with an offset of the field we're looking for
1976   fieldDescriptor fd;
1977 
1978   Klass* res = ik->find_field(name_symbol, signature_symbol, &fd);
1979   if (res == NULL) {
1980     tty->print_cr("Invalid layout of %s at %s", ik->external_name(),
1981         name_symbol->as_C_string());
1982     vm_exit_during_initialization("Invalid layout of preloaded class: use -Xlog:class+load=info to see the origin of the problem class");
1983   }
1984 
1985   //fetch the field at the offset we've found
1986   int dest_offset = fd.offset();
1987 
1988   return dest_offset;
1989 }
1990 
1991 
1992 const char* WhiteBox::lookup_jstring(const char* field_name, oop object) {
1993   int offset = offset_for_field(field_name, object,
1994       vmSymbols::string_signature());
1995   oop string = object->obj_field(offset);
1996   if (string == NULL) {
1997     return NULL;
1998   }
1999   const char* ret = java_lang_String::as_utf8_string(string);
2000   return ret;
2001 }
2002 
2003 bool WhiteBox::lookup_bool(const char* field_name, oop object) {
2004   int offset =
2005       offset_for_field(field_name, object, vmSymbols::bool_signature());
2006   bool ret = (object->bool_field(offset) == JNI_TRUE);
2007   return ret;
2008 }
2009 
2010 void WhiteBox::register_methods(JNIEnv* env, jclass wbclass, JavaThread* thread, JNINativeMethod* method_array, int method_count) {
2011   ResourceMark rm;
2012   ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
2013 
2014   //  one by one registration natives for exception catching
2015   jclass no_such_method_error_klass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string());
2016   CHECK_JNI_EXCEPTION(env);
2017   for (int i = 0, n = method_count; i < n; ++i) {
2018     // Skip dummy entries
2019     if (method_array[i].fnPtr == NULL) continue;
2020     if (env->RegisterNatives(wbclass, &method_array[i], 1) != 0) {
2021       jthrowable throwable_obj = env->ExceptionOccurred();
2022       if (throwable_obj != NULL) {
2023         env->ExceptionClear();
2024         if (env->IsInstanceOf(throwable_obj, no_such_method_error_klass)) {
2025           // NoSuchMethodError is thrown when a method can't be found or a method is not native.
2026           // Ignoring the exception since it is not preventing use of other WhiteBox methods.
2027           tty->print_cr("Warning: 'NoSuchMethodError' on register of sun.hotspot.WhiteBox::%s%s",
2028               method_array[i].name, method_array[i].signature);
2029         }
2030       } else {
2031         // Registration failed unexpectedly.
2032         tty->print_cr("Warning: unexpected error on register of sun.hotspot.WhiteBox::%s%s. All methods will be unregistered",
2033             method_array[i].name, method_array[i].signature);
2034         env->UnregisterNatives(wbclass);
2035         break;
2036       }
2037     }
2038   }
2039 }
2040 
2041 WB_ENTRY(jint, WB_AddCompilerDirective(JNIEnv* env, jobject o, jstring compDirect))
2042   // can't be in VM when we call JNI
2043   ThreadToNativeFromVM ttnfv(thread);
2044   const char* dir = env->GetStringUTFChars(compDirect, NULL);
2045   CHECK_JNI_EXCEPTION_(env, 0);
2046   int ret;
2047   {
2048     ThreadInVMfromNative ttvfn(thread); // back to VM
2049     ret = DirectivesParser::parse_string(dir, tty);
2050   }
2051   env->ReleaseStringUTFChars(compDirect, dir);
2052   // -1 for error parsing directive. Return 0 as number of directives added.
2053   if (ret == -1) {
2054     ret = 0;
2055   }
2056   return (jint) ret;
2057 WB_END
2058 
2059 WB_ENTRY(void, WB_RemoveCompilerDirective(JNIEnv* env, jobject o, jint count))
2060   DirectivesStack::pop(count);
2061 WB_END
2062 
2063 // Checks that the library libfile has the noexecstack bit set.
2064 WB_ENTRY(jboolean, WB_CheckLibSpecifiesNoexecstack(JNIEnv* env, jobject o, jstring libfile))
2065   jboolean ret = false;
2066 #ifdef LINUX
2067   // Can't be in VM when we call JNI.
2068   ThreadToNativeFromVM ttnfv(thread);
2069   const char* lf = env->GetStringUTFChars(libfile, NULL);
2070   CHECK_JNI_EXCEPTION_(env, 0);
2071   ret = (jboolean) ElfFile::specifies_noexecstack(lf);
2072   env->ReleaseStringUTFChars(libfile, lf);
2073 #endif
2074   return ret;
2075 WB_END
2076 
2077 WB_ENTRY(jboolean, WB_IsContainerized(JNIEnv* env, jobject o))
2078   LINUX_ONLY(return OSContainer::is_containerized();)
2079   return false;
2080 WB_END
2081 
2082 WB_ENTRY(void, WB_PrintOsInfo(JNIEnv* env, jobject o))
2083   os::print_os_info(tty);
2084 WB_END
2085 
2086 // Elf decoder
2087 WB_ENTRY(void, WB_DisableElfSectionCache(JNIEnv* env))
2088 #if !defined(_WINDOWS) && !defined(__APPLE__) && !defined(_AIX)
2089   ElfFile::_do_not_cache_elf_section = true;
2090 #endif
2091 WB_END
2092 
2093 WB_ENTRY(jlong, WB_ResolvedMethodItemsCount(JNIEnv* env, jobject o))
2094   return (jlong) ResolvedMethodTable::items_count();
2095 WB_END
2096 
2097 WB_ENTRY(jint, WB_ProtectionDomainRemovedCount(JNIEnv* env, jobject o))
2098   return (jint) SystemDictionary::pd_cache_table()->removed_entries_count();
2099 WB_END
2100 
2101 WB_ENTRY(jint, WB_AotLibrariesCount(JNIEnv* env, jobject o))
2102   jint result = 0;
2103 #if INCLUDE_AOT
2104   result = (jint) AOTLoader::heaps_count();
2105 #endif
2106   return result;
2107 WB_END
2108 
2109 #define CC (char*)
2110 
2111 static JNINativeMethod methods[] = {
2112   {CC"getObjectAddress0",                CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress  },
2113   {CC"getObjectSize0",                   CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectSize     },
2114   {CC"isObjectInOldGen0",                CC"(Ljava/lang/Object;)Z", (void*)&WB_isObjectInOldGen  },
2115   {CC"getHeapOopSize",                   CC"()I",                   (void*)&WB_GetHeapOopSize    },
2116   {CC"getVMPageSize",                    CC"()I",                   (void*)&WB_GetVMPageSize     },
2117   {CC"getVMAllocationGranularity",       CC"()J",                   (void*)&WB_GetVMAllocationGranularity },
2118   {CC"getVMLargePageSize",               CC"()J",                   (void*)&WB_GetVMLargePageSize},
2119   {CC"getHeapSpaceAlignment",            CC"()J",                   (void*)&WB_GetHeapSpaceAlignment},
2120   {CC"getHeapAlignment",                 CC"()J",                   (void*)&WB_GetHeapAlignment},
2121   {CC"isClassAlive0",                    CC"(Ljava/lang/String;)Z", (void*)&WB_IsClassAlive      },
2122   {CC"getSymbolRefcount",                CC"(Ljava/lang/String;)I", (void*)&WB_GetSymbolRefcount },
2123   {CC"parseCommandLine0",
2124       CC"(Ljava/lang/String;C[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;",
2125       (void*) &WB_ParseCommandLine
2126   },
2127   {CC"addToBootstrapClassLoaderSearch0", CC"(Ljava/lang/String;)V",
2128                                                       (void*)&WB_AddToBootstrapClassLoaderSearch},
2129   {CC"addToSystemClassLoaderSearch0",    CC"(Ljava/lang/String;)V",
2130                                                       (void*)&WB_AddToSystemClassLoaderSearch},
2131   {CC"getCompressedOopsMaxHeapSize", CC"()J",
2132       (void*)&WB_GetCompressedOopsMaxHeapSize},
2133   {CC"printHeapSizes",     CC"()V",                   (void*)&WB_PrintHeapSizes    },
2134   {CC"runMemoryUnitTests", CC"()V",                   (void*)&WB_RunMemoryUnitTests},
2135   {CC"readFromNoaccessArea",CC"()V",                  (void*)&WB_ReadFromNoaccessArea},
2136   {CC"stressVirtualSpaceResize",CC"(JJJ)I",           (void*)&WB_StressVirtualSpaceResize},
2137 #if INCLUDE_CDS
2138   {CC"getOffsetForName0", CC"(Ljava/lang/String;)I",  (void*)&WB_GetOffsetForName},
2139 #endif
2140 #if INCLUDE_G1GC
2141   {CC"g1InConcurrentMark", CC"()Z",                   (void*)&WB_G1InConcurrentMark},
2142   {CC"g1IsHumongous0",      CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous     },
2143   {CC"g1BelongsToHumongousRegion0", CC"(J)Z",         (void*)&WB_G1BelongsToHumongousRegion},
2144   {CC"g1BelongsToFreeRegion0", CC"(J)Z",              (void*)&WB_G1BelongsToFreeRegion},
2145   {CC"g1NumMaxRegions",    CC"()J",                   (void*)&WB_G1NumMaxRegions  },
2146   {CC"g1NumFreeRegions",   CC"()J",                   (void*)&WB_G1NumFreeRegions  },
2147   {CC"g1RegionSize",       CC"()I",                   (void*)&WB_G1RegionSize      },
2148   {CC"g1StartConcMarkCycle",       CC"()Z",           (void*)&WB_G1StartMarkCycle  },
2149   {CC"g1AuxiliaryMemoryUsage", CC"()Ljava/lang/management/MemoryUsage;",
2150                                                       (void*)&WB_G1AuxiliaryMemoryUsage  },
2151   {CC"g1GetMixedGCInfo",   CC"(I)[J",                 (void*)&WB_G1GetMixedGCInfo },
2152 #endif // INCLUDE_G1GC
2153 #if INCLUDE_G1GC || INCLUDE_PARALLELGC
2154   {CC"dramReservedStart",   CC"()J",                  (void*)&WB_DramReservedStart },
2155   {CC"dramReservedEnd",     CC"()J",                  (void*)&WB_DramReservedEnd },
2156   {CC"nvdimmReservedStart", CC"()J",                  (void*)&WB_NvdimmReservedStart },
2157   {CC"nvdimmReservedEnd",   CC"()J",                  (void*)&WB_NvdimmReservedEnd },
2158 #endif // INCLUDE_G1GC || INCLUDE_PARALLELGC
2159 #if INCLUDE_PARALLELGC
2160   {CC"psVirtualSpaceAlignment",CC"()J",               (void*)&WB_PSVirtualSpaceAlignment},
2161   {CC"psHeapGenerationAlignment",CC"()J",             (void*)&WB_PSHeapGenerationAlignment},
2162 #endif
2163 #if INCLUDE_NMT
2164   {CC"NMTMalloc",           CC"(J)J",                 (void*)&WB_NMTMalloc          },
2165   {CC"NMTMallocWithPseudoStack", CC"(JI)J",           (void*)&WB_NMTMallocWithPseudoStack},
2166   {CC"NMTMallocWithPseudoStackAndType", CC"(JII)J",   (void*)&WB_NMTMallocWithPseudoStackAndType},
2167   {CC"NMTFree",             CC"(J)V",                 (void*)&WB_NMTFree            },
2168   {CC"NMTReserveMemory",    CC"(J)J",                 (void*)&WB_NMTReserveMemory   },
2169   {CC"NMTAttemptReserveMemoryAt",    CC"(JJ)J",       (void*)&WB_NMTAttemptReserveMemoryAt },
2170   {CC"NMTCommitMemory",     CC"(JJ)V",                (void*)&WB_NMTCommitMemory    },
2171   {CC"NMTUncommitMemory",   CC"(JJ)V",                (void*)&WB_NMTUncommitMemory  },
2172   {CC"NMTReleaseMemory",    CC"(JJ)V",                (void*)&WB_NMTReleaseMemory   },
2173   {CC"NMTChangeTrackingLevel", CC"()Z",               (void*)&WB_NMTChangeTrackingLevel},
2174   {CC"NMTGetHashSize",      CC"()I",                  (void*)&WB_NMTGetHashSize     },
2175 #endif // INCLUDE_NMT
2176   {CC"deoptimizeFrames",   CC"(Z)I",                  (void*)&WB_DeoptimizeFrames  },
2177   {CC"deoptimizeAll",      CC"()V",                   (void*)&WB_DeoptimizeAll     },
2178   {CC"deoptimizeMethod0",   CC"(Ljava/lang/reflect/Executable;Z)I",
2179                                                       (void*)&WB_DeoptimizeMethod  },
2180   {CC"isMethodCompiled0",   CC"(Ljava/lang/reflect/Executable;Z)Z",
2181                                                       (void*)&WB_IsMethodCompiled  },
2182   {CC"isMethodCompilable0", CC"(Ljava/lang/reflect/Executable;IZ)Z",
2183                                                       (void*)&WB_IsMethodCompilable},
2184   {CC"isMethodQueuedForCompilation0",
2185       CC"(Ljava/lang/reflect/Executable;)Z",          (void*)&WB_IsMethodQueuedForCompilation},
2186   {CC"isIntrinsicAvailable0",
2187       CC"(Ljava/lang/reflect/Executable;Ljava/lang/reflect/Executable;I)Z",
2188                                                       (void*)&WB_IsIntrinsicAvailable},
2189   {CC"makeMethodNotCompilable0",
2190       CC"(Ljava/lang/reflect/Executable;IZ)V",        (void*)&WB_MakeMethodNotCompilable},
2191   {CC"testSetDontInlineMethod0",
2192       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetDontInlineMethod},
2193   {CC"getMethodCompilationLevel0",
2194       CC"(Ljava/lang/reflect/Executable;Z)I",         (void*)&WB_GetMethodCompilationLevel},
2195   {CC"getMethodEntryBci0",
2196       CC"(Ljava/lang/reflect/Executable;)I",          (void*)&WB_GetMethodEntryBci},
2197   {CC"getCompileQueueSize",
2198       CC"(I)I",                                       (void*)&WB_GetCompileQueueSize},
2199   {CC"testSetForceInlineMethod0",
2200       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetForceInlineMethod},
2201   {CC"enqueueMethodForCompilation0",
2202       CC"(Ljava/lang/reflect/Executable;II)Z",        (void*)&WB_EnqueueMethodForCompilation},
2203   {CC"enqueueInitializerForCompilation0",
2204       CC"(Ljava/lang/Class;I)Z",                      (void*)&WB_EnqueueInitializerForCompilation},
2205   {CC"clearMethodState0",
2206       CC"(Ljava/lang/reflect/Executable;)V",          (void*)&WB_ClearMethodState},
2207   {CC"lockCompilation",    CC"()V",                   (void*)&WB_LockCompilation},
2208   {CC"unlockCompilation",  CC"()V",                   (void*)&WB_UnlockCompilation},
2209   {CC"matchesMethod",
2210       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)I",
2211                                                       (void*)&WB_MatchesMethod},
2212   {CC"matchesInline",
2213       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)I",
2214                                                       (void*)&WB_MatchesInline},
2215   {CC"shouldPrintAssembly",
2216         CC"(Ljava/lang/reflect/Executable;I)Z",
2217                                                         (void*)&WB_ShouldPrintAssembly},
2218 
2219   {CC"isConstantVMFlag",   CC"(Ljava/lang/String;)Z", (void*)&WB_IsConstantVMFlag},
2220   {CC"isLockedVMFlag",     CC"(Ljava/lang/String;)Z", (void*)&WB_IsLockedVMFlag},
2221   {CC"setBooleanVMFlag",   CC"(Ljava/lang/String;Z)V",(void*)&WB_SetBooleanVMFlag},
2222   {CC"setIntVMFlag",       CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntVMFlag},
2223   {CC"setUintVMFlag",      CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintVMFlag},
2224   {CC"setIntxVMFlag",      CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntxVMFlag},
2225   {CC"setUintxVMFlag",     CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintxVMFlag},
2226   {CC"setUint64VMFlag",    CC"(Ljava/lang/String;J)V",(void*)&WB_SetUint64VMFlag},
2227   {CC"setSizeTVMFlag",     CC"(Ljava/lang/String;J)V",(void*)&WB_SetSizeTVMFlag},
2228   {CC"setDoubleVMFlag",    CC"(Ljava/lang/String;D)V",(void*)&WB_SetDoubleVMFlag},
2229   {CC"setStringVMFlag",    CC"(Ljava/lang/String;Ljava/lang/String;)V",
2230                                                       (void*)&WB_SetStringVMFlag},
2231   {CC"getBooleanVMFlag",   CC"(Ljava/lang/String;)Ljava/lang/Boolean;",
2232                                                       (void*)&WB_GetBooleanVMFlag},
2233   {CC"getIntVMFlag",       CC"(Ljava/lang/String;)Ljava/lang/Long;",
2234                                                       (void*)&WB_GetIntVMFlag},
2235   {CC"getUintVMFlag",      CC"(Ljava/lang/String;)Ljava/lang/Long;",
2236                                                       (void*)&WB_GetUintVMFlag},
2237   {CC"getIntxVMFlag",      CC"(Ljava/lang/String;)Ljava/lang/Long;",
2238                                                       (void*)&WB_GetIntxVMFlag},
2239   {CC"getUintxVMFlag",     CC"(Ljava/lang/String;)Ljava/lang/Long;",
2240                                                       (void*)&WB_GetUintxVMFlag},
2241   {CC"getUint64VMFlag",    CC"(Ljava/lang/String;)Ljava/lang/Long;",
2242                                                       (void*)&WB_GetUint64VMFlag},
2243   {CC"getSizeTVMFlag",     CC"(Ljava/lang/String;)Ljava/lang/Long;",
2244                                                       (void*)&WB_GetSizeTVMFlag},
2245   {CC"getDoubleVMFlag",    CC"(Ljava/lang/String;)Ljava/lang/Double;",
2246                                                       (void*)&WB_GetDoubleVMFlag},
2247   {CC"getStringVMFlag",    CC"(Ljava/lang/String;)Ljava/lang/String;",
2248                                                       (void*)&WB_GetStringVMFlag},
2249   {CC"isInStringTable",    CC"(Ljava/lang/String;)Z", (void*)&WB_IsInStringTable  },
2250   {CC"fullGC",   CC"()V",                             (void*)&WB_FullGC },
2251   {CC"youngGC",  CC"()V",                             (void*)&WB_YoungGC },
2252   {CC"readReservedMemory", CC"()V",                   (void*)&WB_ReadReservedMemory },
2253   {CC"allocateMetaspace",
2254      CC"(Ljava/lang/ClassLoader;J)J",                 (void*)&WB_AllocateMetaspace },
2255   {CC"freeMetaspace",
2256      CC"(Ljava/lang/ClassLoader;JJ)V",                (void*)&WB_FreeMetaspace },
2257   {CC"incMetaspaceCapacityUntilGC", CC"(J)J",         (void*)&WB_IncMetaspaceCapacityUntilGC },
2258   {CC"metaspaceCapacityUntilGC", CC"()J",             (void*)&WB_MetaspaceCapacityUntilGC },
2259   {CC"metaspaceShouldConcurrentCollect", CC"()Z",     (void*)&WB_MetaspaceShouldConcurrentCollect },
2260   {CC"metaspaceReserveAlignment", CC"()J",            (void*)&WB_MetaspaceReserveAlignment },
2261   {CC"getCPUFeatures",     CC"()Ljava/lang/String;",  (void*)&WB_GetCPUFeatures     },
2262   {CC"getNMethod0",         CC"(Ljava/lang/reflect/Executable;Z)[Ljava/lang/Object;",
2263                                                       (void*)&WB_GetNMethod         },
2264   {CC"forceNMethodSweep",  CC"()V",                   (void*)&WB_ForceNMethodSweep  },
2265   {CC"allocateCodeBlob",   CC"(II)J",                 (void*)&WB_AllocateCodeBlob   },
2266   {CC"freeCodeBlob",       CC"(J)V",                  (void*)&WB_FreeCodeBlob       },
2267   {CC"getCodeHeapEntries", CC"(I)[Ljava/lang/Object;",(void*)&WB_GetCodeHeapEntries },
2268   {CC"getCompilationActivityMode",
2269                            CC"()I",                   (void*)&WB_GetCompilationActivityMode},
2270   {CC"getMethodData0",     CC"(Ljava/lang/reflect/Executable;)J",
2271                                                       (void*)&WB_GetMethodData      },
2272   {CC"getCodeBlob",        CC"(J)[Ljava/lang/Object;",(void*)&WB_GetCodeBlob        },
2273   {CC"getThreadStackSize", CC"()J",                   (void*)&WB_GetThreadStackSize },
2274   {CC"getThreadRemainingStackSize", CC"()J",          (void*)&WB_GetThreadRemainingStackSize },
2275   {CC"DefineModule",       CC"(Ljava/lang/Object;ZLjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V",
2276                                                       (void*)&WB_DefineModule },
2277   {CC"AddModuleExports",   CC"(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V",
2278                                                       (void*)&WB_AddModuleExports },
2279   {CC"AddReadsModule",     CC"(Ljava/lang/Object;Ljava/lang/Object;)V",
2280                                                       (void*)&WB_AddReadsModule },
2281   {CC"AddModuleExportsToAllUnnamed", CC"(Ljava/lang/Object;Ljava/lang/String;)V",
2282                                                       (void*)&WB_AddModuleExportsToAllUnnamed },
2283   {CC"AddModuleExportsToAll", CC"(Ljava/lang/Object;Ljava/lang/String;)V",
2284                                                       (void*)&WB_AddModuleExportsToAll },
2285   {CC"assertMatchingSafepointCalls", CC"(ZZ)V",       (void*)&WB_AssertMatchingSafepointCalls },
2286   {CC"isMonitorInflated0", CC"(Ljava/lang/Object;)Z", (void*)&WB_IsMonitorInflated  },
2287   {CC"forceSafepoint",     CC"()V",                   (void*)&WB_ForceSafepoint     },
2288   {CC"getConstantPool0",   CC"(Ljava/lang/Class;)J",  (void*)&WB_GetConstantPool    },
2289   {CC"getConstantPoolCacheIndexTag0", CC"()I",  (void*)&WB_GetConstantPoolCacheIndexTag},
2290   {CC"getConstantPoolCacheLength0", CC"(Ljava/lang/Class;)I",  (void*)&WB_GetConstantPoolCacheLength},
2291   {CC"remapInstructionOperandFromCPCache0",
2292       CC"(Ljava/lang/Class;I)I",                      (void*)&WB_ConstantPoolRemapInstructionOperandFromCache},
2293   {CC"encodeConstantPoolIndyIndex0",
2294       CC"(I)I",                      (void*)&WB_ConstantPoolEncodeIndyIndex},
2295   {CC"getMethodBooleanOption",
2296       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Boolean;",
2297                                                       (void*)&WB_GetMethodBooleaneOption},
2298   {CC"getMethodIntxOption",
2299       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
2300                                                       (void*)&WB_GetMethodIntxOption},
2301   {CC"getMethodUintxOption",
2302       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
2303                                                       (void*)&WB_GetMethodUintxOption},
2304   {CC"getMethodDoubleOption",
2305       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Double;",
2306                                                       (void*)&WB_GetMethodDoubleOption},
2307   {CC"getMethodStringOption",
2308       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/String;",
2309                                                       (void*)&WB_GetMethodStringOption},
2310   {CC"getDefaultArchivePath",             CC"()Ljava/lang/String;",
2311                                                       (void*)&WB_GetDefaultArchivePath},
2312   {CC"isSharingEnabled",   CC"()Z",                   (void*)&WB_IsSharingEnabled},
2313   {CC"isShared",           CC"(Ljava/lang/Object;)Z", (void*)&WB_IsShared },
2314   {CC"isSharedClass",      CC"(Ljava/lang/Class;)Z",  (void*)&WB_IsSharedClass },
2315   {CC"areSharedStringsIgnored",           CC"()Z",    (void*)&WB_AreSharedStringsIgnored },
2316   {CC"getResolvedReferences", CC"(Ljava/lang/Class;)Ljava/lang/Object;", (void*)&WB_GetResolvedReferences},
2317   {CC"areOpenArchiveHeapObjectsMapped",   CC"()Z",    (void*)&WB_AreOpenArchiveHeapObjectsMapped},
2318   {CC"isCDSIncludedInVmBuild",            CC"()Z",    (void*)&WB_IsCDSIncludedInVmBuild },
2319   {CC"isJFRIncludedInVmBuild",            CC"()Z",    (void*)&WB_IsJFRIncludedInVmBuild },
2320   {CC"isJavaHeapArchiveSupported",      CC"()Z",      (void*)&WB_IsJavaHeapArchiveSupported },
2321 
2322   {CC"clearInlineCaches0",  CC"(Z)V",                 (void*)&WB_ClearInlineCaches },
2323   {CC"handshakeWalkStack", CC"(Ljava/lang/Thread;Z)I", (void*)&WB_HandshakeWalkStack },
2324   {CC"addCompilerDirective",    CC"(Ljava/lang/String;)I",
2325                                                       (void*)&WB_AddCompilerDirective },
2326   {CC"removeCompilerDirective",   CC"(I)V",           (void*)&WB_RemoveCompilerDirective },
2327   {CC"isGCSupported",             CC"(I)Z",           (void*)&WB_IsGCSupported},
2328   {CC"isGCSelected",              CC"(I)Z",           (void*)&WB_IsGCSelected},
2329   {CC"isGCSelectedErgonomically", CC"()Z",            (void*)&WB_IsGCSelectedErgonomically},
2330   {CC"supportsConcurrentGCPhaseControl", CC"()Z",     (void*)&WB_SupportsConcurrentGCPhaseControl},
2331   {CC"requestConcurrentGCPhase0", CC"(Ljava/lang/String;)Z",
2332                                                       (void*)&WB_RequestConcurrentGCPhase},
2333   {CC"checkLibSpecifiesNoexecstack", CC"(Ljava/lang/String;)Z",
2334                                                       (void*)&WB_CheckLibSpecifiesNoexecstack},
2335   {CC"isContainerized",           CC"()Z",            (void*)&WB_IsContainerized },
2336   {CC"printOsInfo",               CC"()V",            (void*)&WB_PrintOsInfo },
2337   {CC"disableElfSectionCache",    CC"()V",            (void*)&WB_DisableElfSectionCache },
2338   {CC"resolvedMethodItemsCount",  CC"()J",            (void*)&WB_ResolvedMethodItemsCount },
2339   {CC"protectionDomainRemovedCount",   CC"()I",       (void*)&WB_ProtectionDomainRemovedCount },
2340   {CC"aotLibrariesCount", CC"()I",                    (void*)&WB_AotLibrariesCount },
2341 };
2342 
2343 
2344 #undef CC
2345 
2346 JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass))
2347   {
2348     if (WhiteBoxAPI) {
2349       // Make sure that wbclass is loaded by the null classloader
2350       InstanceKlass* ik = InstanceKlass::cast(JNIHandles::resolve(wbclass)->klass());
2351       Handle loader(THREAD, ik->class_loader());
2352       if (loader.is_null()) {
2353         WhiteBox::register_methods(env, wbclass, thread, methods, sizeof(methods) / sizeof(methods[0]));
2354         WhiteBox::set_used();
2355       }
2356     }
2357   }
2358 JVM_END