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