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