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