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