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(jboolean, WB_IsIntrinsicAvailableForMethod(JNIEnv* env, jobject o, jobject method, jobject compilation_context, jint compLevel))
 532   if (compLevel < CompLevel_none || compLevel > CompLevel_highest_tier) {
 533     return false; // Intrinsic is not available on a non-existent compilation level.
 534   }
 535   jmethodID method_id, compilation_context_id;
 536   method_id = reflected_method_to_jmid(thread, env, method);
 537   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 538   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(method_id));
 539   if (compilation_context != NULL) {
 540     compilation_context_id = reflected_method_to_jmid(thread, env, compilation_context);
 541     CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 542     methodHandle cch(THREAD, Method::checked_resolve_jmethod_id(compilation_context_id));
 543     return CompileBroker::compiler(compLevel)->is_intrinsic_available(mh, cch);
 544   } else {
 545     return CompileBroker::compiler(compLevel)->is_intrinsic_available(mh);
 546   }
 547 WB_END
 548 
 549 WB_ENTRY(jint, WB_GetMethodCompilationLevel(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 550   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 551   CHECK_JNI_EXCEPTION_(env, CompLevel_none);
 552   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 553   nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 554   return (code != NULL ? code->comp_level() : CompLevel_none);
 555 WB_END
 556 
 557 WB_ENTRY(void, WB_MakeMethodNotCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 558   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 559   CHECK_JNI_EXCEPTION(env);
 560   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 561   if (is_osr) {
 562     mh->set_not_osr_compilable(comp_level, true /* report */, "WhiteBox");
 563   } else {
 564     mh->set_not_compilable(comp_level, true /* report */, "WhiteBox");
 565   }
 566 WB_END
 567 
 568 WB_ENTRY(jint, WB_GetMethodEntryBci(JNIEnv* env, jobject o, jobject method))
 569   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 570   CHECK_JNI_EXCEPTION_(env, InvocationEntryBci);
 571   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 572   nmethod* code = mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false);
 573   return (code != NULL && code->is_osr_method() ? code->osr_entry_bci() : InvocationEntryBci);
 574 WB_END
 575 
 576 WB_ENTRY(jboolean, WB_TestSetDontInlineMethod(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->dont_inline();
 581   mh->set_dont_inline(value == JNI_TRUE);
 582   return result;
 583 WB_END
 584 
 585 WB_ENTRY(jint, WB_GetCompileQueueSize(JNIEnv* env, jobject o, jint comp_level))
 586   if (comp_level == CompLevel_any) {
 587     return CompileBroker::queue_size(CompLevel_full_optimization) /* C2 */ +
 588         CompileBroker::queue_size(CompLevel_full_profile) /* C1 */;
 589   } else {
 590     return CompileBroker::queue_size(comp_level);
 591   }
 592 WB_END
 593 
 594 WB_ENTRY(jboolean, WB_TestSetForceInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
 595   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 596   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 597   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 598   bool result = mh->force_inline();
 599   mh->set_force_inline(value == JNI_TRUE);
 600   return result;
 601 WB_END
 602 
 603 WB_ENTRY(jboolean, WB_EnqueueMethodForCompilation(JNIEnv* env, jobject o, jobject method, jint comp_level, jint bci))
 604   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 605   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 606   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 607   nmethod* nm = CompileBroker::compile_method(mh, bci, comp_level, mh, mh->invocation_count(), "WhiteBox", THREAD);
 608   MutexLockerEx mu(Compile_lock);
 609   return (mh->queued_for_compilation() || nm != NULL);
 610 WB_END
 611 
 612 class AlwaysFalseClosure : public BoolObjectClosure {
 613  public:
 614   bool do_object_b(oop p) { return false; }
 615 };
 616 
 617 static AlwaysFalseClosure always_false;
 618 
 619 WB_ENTRY(void, WB_ClearMethodState(JNIEnv* env, jobject o, jobject method))
 620   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 621   CHECK_JNI_EXCEPTION(env);
 622   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 623   MutexLockerEx mu(Compile_lock);
 624   MethodData* mdo = mh->method_data();
 625   MethodCounters* mcs = mh->method_counters();
 626 
 627   if (mdo != NULL) {
 628     mdo->init();
 629     ResourceMark rm;
 630     int arg_count = mdo->method()->size_of_parameters();
 631     for (int i = 0; i < arg_count; i++) {
 632       mdo->set_arg_modified(i, 0);
 633     }
 634     MutexLockerEx mu(mdo->extra_data_lock());
 635     mdo->clean_method_data(&always_false);
 636   }
 637 
 638   mh->clear_not_c1_compilable();
 639   mh->clear_not_c2_compilable();
 640   mh->clear_not_c2_osr_compilable();
 641   NOT_PRODUCT(mh->set_compiled_invocation_count(0));
 642   if (mcs != NULL) {
 643     mcs->backedge_counter()->init();
 644     mcs->invocation_counter()->init();
 645     mcs->set_interpreter_invocation_count(0);
 646     mcs->set_interpreter_throwout_count(0);
 647 
 648 #ifdef TIERED
 649     mcs->set_rate(0.0F);
 650     mh->set_prev_event_count(0);
 651     mh->set_prev_time(0);
 652 #endif
 653   }
 654 WB_END
 655 
 656 template <typename T>
 657 static bool GetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value, Flag::Error (*TAt)(const char*, T*, bool, bool)) {
 658   if (name == NULL) {
 659     return false;
 660   }
 661   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 662   const char* flag_name = env->GetStringUTFChars(name, NULL);
 663   Flag::Error result = (*TAt)(flag_name, value, true, true);
 664   env->ReleaseStringUTFChars(name, flag_name);
 665   return (result == Flag::SUCCESS);
 666 }
 667 
 668 template <typename T>
 669 static bool SetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value, Flag::Error (*TAtPut)(const char*, T*, Flag::Flags)) {
 670   if (name == NULL) {
 671     return false;
 672   }
 673   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 674   const char* flag_name = env->GetStringUTFChars(name, NULL);
 675   Flag::Error result = (*TAtPut)(flag_name, value, Flag::INTERNAL);
 676   env->ReleaseStringUTFChars(name, flag_name);
 677   return (result == Flag::SUCCESS);
 678 }
 679 
 680 template <typename T>
 681 static jobject box(JavaThread* thread, JNIEnv* env, Symbol* name, Symbol* sig, T value) {
 682   ResourceMark rm(thread);
 683   jclass clazz = env->FindClass(name->as_C_string());
 684   CHECK_JNI_EXCEPTION_(env, NULL);
 685   jmethodID methodID = env->GetStaticMethodID(clazz,
 686         vmSymbols::valueOf_name()->as_C_string(),
 687         sig->as_C_string());
 688   CHECK_JNI_EXCEPTION_(env, NULL);
 689   jobject result = env->CallStaticObjectMethod(clazz, methodID, value);
 690   CHECK_JNI_EXCEPTION_(env, NULL);
 691   return result;
 692 }
 693 
 694 static jobject booleanBox(JavaThread* thread, JNIEnv* env, jboolean value) {
 695   return box(thread, env, vmSymbols::java_lang_Boolean(), vmSymbols::Boolean_valueOf_signature(), value);
 696 }
 697 static jobject integerBox(JavaThread* thread, JNIEnv* env, jint value) {
 698   return box(thread, env, vmSymbols::java_lang_Integer(), vmSymbols::Integer_valueOf_signature(), value);
 699 }
 700 static jobject longBox(JavaThread* thread, JNIEnv* env, jlong value) {
 701   return box(thread, env, vmSymbols::java_lang_Long(), vmSymbols::Long_valueOf_signature(), value);
 702 }
 703 /* static jobject floatBox(JavaThread* thread, JNIEnv* env, jfloat value) {
 704   return box(thread, env, vmSymbols::java_lang_Float(), vmSymbols::Float_valueOf_signature(), value);
 705 }*/
 706 static jobject doubleBox(JavaThread* thread, JNIEnv* env, jdouble value) {
 707   return box(thread, env, vmSymbols::java_lang_Double(), vmSymbols::Double_valueOf_signature(), value);
 708 }
 709 
 710 static Flag* getVMFlag(JavaThread* thread, JNIEnv* env, jstring name) {
 711   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 712   const char* flag_name = env->GetStringUTFChars(name, NULL);
 713   Flag* result = Flag::find_flag(flag_name, strlen(flag_name), true, true);
 714   env->ReleaseStringUTFChars(name, flag_name);
 715   return result;
 716 }
 717 
 718 WB_ENTRY(jboolean, WB_IsConstantVMFlag(JNIEnv* env, jobject o, jstring name))
 719   Flag* flag = getVMFlag(thread, env, name);
 720   return (flag != NULL) && flag->is_constant_in_binary();
 721 WB_END
 722 
 723 WB_ENTRY(jboolean, WB_IsLockedVMFlag(JNIEnv* env, jobject o, jstring name))
 724   Flag* flag = getVMFlag(thread, env, name);
 725   return (flag != NULL) && !(flag->is_unlocked() || flag->is_unlocker());
 726 WB_END
 727 
 728 WB_ENTRY(jobject, WB_GetBooleanVMFlag(JNIEnv* env, jobject o, jstring name))
 729   bool result;
 730   if (GetVMFlag <bool> (thread, env, name, &result, &CommandLineFlags::boolAt)) {
 731     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 732     return booleanBox(thread, env, result);
 733   }
 734   return NULL;
 735 WB_END
 736 
 737 WB_ENTRY(jobject, WB_GetIntVMFlag(JNIEnv* env, jobject o, jstring name))
 738   int result;
 739   if (GetVMFlag <int> (thread, env, name, &result, &CommandLineFlags::intAt)) {
 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_GetUintVMFlag(JNIEnv* env, jobject o, jstring name))
 747   uint result;
 748   if (GetVMFlag <uint> (thread, env, name, &result, &CommandLineFlags::uintAt)) {
 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_GetIntxVMFlag(JNIEnv* env, jobject o, jstring name))
 756   intx result;
 757   if (GetVMFlag <intx> (thread, env, name, &result, &CommandLineFlags::intxAt)) {
 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_GetUintxVMFlag(JNIEnv* env, jobject o, jstring name))
 765   uintx result;
 766   if (GetVMFlag <uintx> (thread, env, name, &result, &CommandLineFlags::uintxAt)) {
 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_GetUint64VMFlag(JNIEnv* env, jobject o, jstring name))
 774   uint64_t result;
 775   if (GetVMFlag <uint64_t> (thread, env, name, &result, &CommandLineFlags::uint64_tAt)) {
 776     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 777     return longBox(thread, env, result);
 778   }
 779   return NULL;
 780 WB_END
 781 
 782 WB_ENTRY(jobject, WB_GetSizeTVMFlag(JNIEnv* env, jobject o, jstring name))
 783   uintx result;
 784   if (GetVMFlag <size_t> (thread, env, name, &result, &CommandLineFlags::size_tAt)) {
 785     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 786     return longBox(thread, env, result);
 787   }
 788   return NULL;
 789 WB_END
 790 
 791 WB_ENTRY(jobject, WB_GetDoubleVMFlag(JNIEnv* env, jobject o, jstring name))
 792   double result;
 793   if (GetVMFlag <double> (thread, env, name, &result, &CommandLineFlags::doubleAt)) {
 794     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 795     return doubleBox(thread, env, result);
 796   }
 797   return NULL;
 798 WB_END
 799 
 800 WB_ENTRY(jstring, WB_GetStringVMFlag(JNIEnv* env, jobject o, jstring name))
 801   ccstr ccstrResult;
 802   if (GetVMFlag <ccstr> (thread, env, name, &ccstrResult, &CommandLineFlags::ccstrAt)) {
 803     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 804     jstring result = env->NewStringUTF(ccstrResult);
 805     CHECK_JNI_EXCEPTION_(env, NULL);
 806     return result;
 807   }
 808   return NULL;
 809 WB_END
 810 
 811 WB_ENTRY(void, WB_SetBooleanVMFlag(JNIEnv* env, jobject o, jstring name, jboolean value))
 812   bool result = value == JNI_TRUE ? true : false;
 813   SetVMFlag <bool> (thread, env, name, &result, &CommandLineFlags::boolAtPut);
 814 WB_END
 815 
 816 WB_ENTRY(void, WB_SetIntVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
 817   int result = value;
 818   SetVMFlag <int> (thread, env, name, &result, &CommandLineFlags::intAtPut);
 819 WB_END
 820 
 821 WB_ENTRY(void, WB_SetUintVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
 822   uint result = value;
 823   SetVMFlag <uint> (thread, env, name, &result, &CommandLineFlags::uintAtPut);
 824 WB_END
 825 
 826 WB_ENTRY(void, WB_SetIntxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
 827   intx result = value;
 828   SetVMFlag <intx> (thread, env, name, &result, &CommandLineFlags::intxAtPut);
 829 WB_END
 830 
 831 WB_ENTRY(void, WB_SetUintxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
 832   uintx result = value;
 833   SetVMFlag <uintx> (thread, env, name, &result, &CommandLineFlags::uintxAtPut);
 834 WB_END
 835 
 836 WB_ENTRY(void, WB_SetUint64VMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
 837   uint64_t result = value;
 838   SetVMFlag <uint64_t> (thread, env, name, &result, &CommandLineFlags::uint64_tAtPut);
 839 WB_END
 840 
 841 WB_ENTRY(void, WB_SetSizeTVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
 842   size_t result = value;
 843   SetVMFlag <size_t> (thread, env, name, &result, &CommandLineFlags::size_tAtPut);
 844 WB_END
 845 
 846 WB_ENTRY(void, WB_SetDoubleVMFlag(JNIEnv* env, jobject o, jstring name, jdouble value))
 847   double result = value;
 848   SetVMFlag <double> (thread, env, name, &result, &CommandLineFlags::doubleAtPut);
 849 WB_END
 850 
 851 WB_ENTRY(void, WB_SetStringVMFlag(JNIEnv* env, jobject o, jstring name, jstring value))
 852   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 853   const char* ccstrValue = (value == NULL) ? NULL : env->GetStringUTFChars(value, NULL);
 854   ccstr ccstrResult = ccstrValue;
 855   bool needFree;
 856   {
 857     ThreadInVMfromNative ttvfn(thread); // back to VM
 858     needFree = SetVMFlag <ccstr> (thread, env, name, &ccstrResult, &CommandLineFlags::ccstrAtPut);
 859   }
 860   if (value != NULL) {
 861     env->ReleaseStringUTFChars(value, ccstrValue);
 862   }
 863   if (needFree) {
 864     FREE_C_HEAP_ARRAY(char, ccstrResult);
 865   }
 866 WB_END
 867 
 868 WB_ENTRY(void, WB_LockCompilation(JNIEnv* env, jobject o, jlong timeout))
 869   WhiteBox::compilation_locked = true;
 870 WB_END
 871 
 872 WB_ENTRY(void, WB_UnlockCompilation(JNIEnv* env, jobject o))
 873   MonitorLockerEx mo(Compilation_lock, Mutex::_no_safepoint_check_flag);
 874   WhiteBox::compilation_locked = false;
 875   mo.notify_all();
 876 WB_END
 877 
 878 WB_ENTRY(void, WB_ForceNMethodSweep(JNIEnv* env, jobject o))
 879   // Force a code cache sweep and block until it finished
 880   NMethodSweeper::force_sweep();
 881 WB_END
 882 
 883 WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString))
 884   ResourceMark rm(THREAD);
 885   int len;
 886   jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len, CHECK_false);
 887   return (StringTable::lookup(name, len) != NULL);
 888 WB_END
 889 
 890 WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o))
 891   Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(true);
 892   Universe::heap()->collect(GCCause::_last_ditch_collection);
 893 #if INCLUDE_ALL_GCS
 894   if (UseG1GC) {
 895     // Needs to be cleared explicitly for G1
 896     Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(false);
 897   }
 898 #endif // INCLUDE_ALL_GCS
 899 WB_END
 900 
 901 WB_ENTRY(void, WB_YoungGC(JNIEnv* env, jobject o))
 902   Universe::heap()->collect(GCCause::_wb_young_gc);
 903 WB_END
 904 
 905 WB_ENTRY(void, WB_ReadReservedMemory(JNIEnv* env, jobject o))
 906   // static+volatile in order to force the read to happen
 907   // (not be eliminated by the compiler)
 908   static char c;
 909   static volatile char* p;
 910 
 911   p = os::reserve_memory(os::vm_allocation_granularity(), NULL, 0);
 912   if (p == NULL) {
 913     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Failed to reserve memory");
 914   }
 915 
 916   c = *p;
 917 WB_END
 918 
 919 WB_ENTRY(jstring, WB_GetCPUFeatures(JNIEnv* env, jobject o))
 920   const char* cpu_features = VM_Version::cpu_features();
 921   ThreadToNativeFromVM ttn(thread);
 922   jstring features_string = env->NewStringUTF(cpu_features);
 923 
 924   CHECK_JNI_EXCEPTION_(env, NULL);
 925 
 926   return features_string;
 927 WB_END
 928 
 929 int WhiteBox::get_blob_type(const CodeBlob* code) {
 930   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
 931   return CodeCache::get_code_heap(code)->code_blob_type();
 932 }
 933 
 934 CodeHeap* WhiteBox::get_code_heap(int blob_type) {
 935   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
 936   return CodeCache::get_code_heap(blob_type);
 937 }
 938 
 939 struct CodeBlobStub {
 940   CodeBlobStub(const CodeBlob* blob) :
 941       name(os::strdup(blob->name())),
 942       size(blob->size()),
 943       blob_type(WhiteBox::get_blob_type(blob)) { }
 944   ~CodeBlobStub() { os::free((void*) name); }
 945   const char* const name;
 946   const int         size;
 947   const int         blob_type;
 948 };
 949 
 950 static jobjectArray codeBlob2objectArray(JavaThread* thread, JNIEnv* env, CodeBlobStub* cb) {
 951   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
 952   CHECK_JNI_EXCEPTION_(env, NULL);
 953   jobjectArray result = env->NewObjectArray(3, clazz, NULL);
 954 
 955   jstring name = env->NewStringUTF(cb->name);
 956   CHECK_JNI_EXCEPTION_(env, NULL);
 957   env->SetObjectArrayElement(result, 0, name);
 958 
 959   jobject obj = integerBox(thread, env, cb->size);
 960   CHECK_JNI_EXCEPTION_(env, NULL);
 961   env->SetObjectArrayElement(result, 1, obj);
 962 
 963   obj = integerBox(thread, env, cb->blob_type);
 964   CHECK_JNI_EXCEPTION_(env, NULL);
 965   env->SetObjectArrayElement(result, 2, obj);
 966 
 967   return result;
 968 }
 969 
 970 WB_ENTRY(jobjectArray, WB_GetNMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 971   ResourceMark rm(THREAD);
 972   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 973   CHECK_JNI_EXCEPTION_(env, NULL);
 974   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 975   nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 976   jobjectArray result = NULL;
 977   if (code == NULL) {
 978     return result;
 979   }
 980   int insts_size = code->insts_size();
 981 
 982   ThreadToNativeFromVM ttn(thread);
 983   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
 984   CHECK_JNI_EXCEPTION_(env, NULL);
 985   result = env->NewObjectArray(4, clazz, NULL);
 986   if (result == NULL) {
 987     return result;
 988   }
 989 
 990   CodeBlobStub stub(code);
 991   jobjectArray codeBlob = codeBlob2objectArray(thread, env, &stub);
 992   env->SetObjectArrayElement(result, 0, codeBlob);
 993 
 994   jobject level = integerBox(thread, env, code->comp_level());
 995   CHECK_JNI_EXCEPTION_(env, NULL);
 996   env->SetObjectArrayElement(result, 1, level);
 997 
 998   jbyteArray insts = env->NewByteArray(insts_size);
 999   CHECK_JNI_EXCEPTION_(env, NULL);
1000   env->SetByteArrayRegion(insts, 0, insts_size, (jbyte*) code->insts_begin());
1001   env->SetObjectArrayElement(result, 2, insts);
1002 
1003   jobject id = integerBox(thread, env, code->compile_id());
1004   CHECK_JNI_EXCEPTION_(env, NULL);
1005   env->SetObjectArrayElement(result, 3, id);
1006 
1007   return result;
1008 WB_END
1009 
1010 CodeBlob* WhiteBox::allocate_code_blob(int size, int blob_type) {
1011   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1012   BufferBlob* blob;
1013   int full_size = CodeBlob::align_code_offset(sizeof(BufferBlob));
1014   if (full_size < size) {
1015     full_size += round_to(size - full_size, oopSize);
1016   }
1017   {
1018     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1019     blob = (BufferBlob*) CodeCache::allocate(full_size, blob_type);
1020     ::new (blob) BufferBlob("WB::DummyBlob", full_size);
1021   }
1022   // Track memory usage statistic after releasing CodeCache_lock
1023   MemoryService::track_code_cache_memory_usage();
1024   return blob;
1025 }
1026 
1027 WB_ENTRY(jlong, WB_AllocateCodeBlob(JNIEnv* env, jobject o, jint size, jint blob_type))
1028     return (jlong) WhiteBox::allocate_code_blob(size, blob_type);
1029 WB_END
1030 
1031 WB_ENTRY(void, WB_FreeCodeBlob(JNIEnv* env, jobject o, jlong addr))
1032     BufferBlob::free((BufferBlob*) addr);
1033 WB_END
1034 
1035 WB_ENTRY(jobjectArray, WB_GetCodeHeapEntries(JNIEnv* env, jobject o, jint blob_type))
1036   ResourceMark rm;
1037   GrowableArray<CodeBlobStub*> blobs;
1038   {
1039     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1040     CodeHeap* heap = WhiteBox::get_code_heap(blob_type);
1041     if (heap == NULL) {
1042       return NULL;
1043     }
1044     for (CodeBlob* cb = (CodeBlob*) heap->first();
1045          cb != NULL; cb = (CodeBlob*) heap->next(cb)) {
1046       CodeBlobStub* stub = NEW_RESOURCE_OBJ(CodeBlobStub);
1047       new (stub) CodeBlobStub(cb);
1048       blobs.append(stub);
1049     }
1050   }
1051   if (blobs.length() == 0) {
1052     return NULL;
1053   }
1054   ThreadToNativeFromVM ttn(thread);
1055   jobjectArray result = NULL;
1056   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1057   CHECK_JNI_EXCEPTION_(env, NULL);
1058   result = env->NewObjectArray(blobs.length(), clazz, NULL);
1059   if (result == NULL) {
1060     return result;
1061   }
1062   int i = 0;
1063   for (GrowableArrayIterator<CodeBlobStub*> it = blobs.begin();
1064        it != blobs.end(); ++it) {
1065     jobjectArray obj = codeBlob2objectArray(thread, env, *it);
1066     env->SetObjectArrayElement(result, i, obj);
1067     ++i;
1068   }
1069   return result;
1070 WB_END
1071 
1072 WB_ENTRY(jint, WB_GetCompilationActivityMode(JNIEnv* env, jobject o))
1073   return CompileBroker::get_compilation_activity_mode();
1074 WB_END
1075 
1076 WB_ENTRY(jobjectArray, WB_GetCodeBlob(JNIEnv* env, jobject o, jlong addr))
1077     ThreadToNativeFromVM ttn(thread);
1078     CodeBlobStub stub((CodeBlob*) addr);
1079     return codeBlob2objectArray(thread, env, &stub);
1080 WB_END
1081 
1082 WB_ENTRY(jlong, WB_GetThreadStackSize(JNIEnv* env, jobject o))
1083   return (jlong) Thread::current()->stack_size();
1084 WB_END
1085 
1086 WB_ENTRY(jlong, WB_GetThreadRemainingStackSize(JNIEnv* env, jobject o))
1087   JavaThread* t = JavaThread::current();
1088   return (jlong) t->stack_available(os::current_stack_pointer()) - (jlong) StackShadowPages * os::vm_page_size();
1089 WB_END
1090 
1091 int WhiteBox::array_bytes_to_length(size_t bytes) {
1092   return Array<u1>::bytes_to_length(bytes);
1093 }
1094 
1095 WB_ENTRY(jlong, WB_AllocateMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong size))
1096   if (size < 0) {
1097     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1098         err_msg("WB_AllocateMetaspace: size is negative: " JLONG_FORMAT, size));
1099   }
1100 
1101   oop class_loader_oop = JNIHandles::resolve(class_loader);
1102   ClassLoaderData* cld = class_loader_oop != NULL
1103       ? java_lang_ClassLoader::loader_data(class_loader_oop)
1104       : ClassLoaderData::the_null_class_loader_data();
1105 
1106   void* metadata = MetadataFactory::new_writeable_array<u1>(cld, WhiteBox::array_bytes_to_length((size_t)size), thread);
1107 
1108   return (jlong)(uintptr_t)metadata;
1109 WB_END
1110 
1111 WB_ENTRY(void, WB_FreeMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong addr, jlong size))
1112   oop class_loader_oop = JNIHandles::resolve(class_loader);
1113   ClassLoaderData* cld = class_loader_oop != NULL
1114       ? java_lang_ClassLoader::loader_data(class_loader_oop)
1115       : ClassLoaderData::the_null_class_loader_data();
1116 
1117   MetadataFactory::free_array(cld, (Array<u1>*)(uintptr_t)addr);
1118 WB_END
1119 
1120 WB_ENTRY(jlong, WB_IncMetaspaceCapacityUntilGC(JNIEnv* env, jobject wb, jlong inc))
1121   if (inc < 0) {
1122     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1123         err_msg("WB_IncMetaspaceCapacityUntilGC: inc is negative: " JLONG_FORMAT, inc));
1124   }
1125 
1126   jlong max_size_t = (jlong) ((size_t) -1);
1127   if (inc > max_size_t) {
1128     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1129         err_msg("WB_IncMetaspaceCapacityUntilGC: inc does not fit in size_t: " JLONG_FORMAT, inc));
1130   }
1131 
1132   size_t new_cap_until_GC = 0;
1133   size_t aligned_inc = align_size_down((size_t) inc, Metaspace::commit_alignment());
1134   bool success = MetaspaceGC::inc_capacity_until_GC(aligned_inc, &new_cap_until_GC);
1135   if (!success) {
1136     THROW_MSG_0(vmSymbols::java_lang_IllegalStateException(),
1137                 "WB_IncMetaspaceCapacityUntilGC: could not increase capacity until GC "
1138                 "due to contention with another thread");
1139   }
1140   return (jlong) new_cap_until_GC;
1141 WB_END
1142 
1143 WB_ENTRY(jlong, WB_MetaspaceCapacityUntilGC(JNIEnv* env, jobject wb))
1144   return (jlong) MetaspaceGC::capacity_until_GC();
1145 WB_END
1146 
1147 WB_ENTRY(jboolean, WB_ReadImageFile(JNIEnv* env, jobject wb, jstring imagefile))
1148   const char* filename = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(imagefile));
1149   return ImageFileReader::open(filename) != NULL;
1150 WB_END
1151 
1152 WB_ENTRY(jlong, WB_imageOpenImage(JNIEnv *env, jobject wb, jstring path, jboolean big_endian))
1153   ThreadToNativeFromVM ttn(thread);
1154   const char *nativePath = env->GetStringUTFChars(path, NULL);
1155   jlong ret = JVM_ImageOpen(env, nativePath, big_endian);
1156 
1157   env->ReleaseStringUTFChars(path, nativePath);
1158   return ret;
1159 WB_END
1160 
1161 WB_ENTRY(void, WB_imageCloseImage(JNIEnv *env, jobject wb, jlong id))
1162   ThreadToNativeFromVM ttn(thread);
1163   JVM_ImageClose(env, id);
1164 WB_END
1165 
1166 WB_ENTRY(jlong, WB_imageGetIndexAddress(JNIEnv *env, jobject wb, jlong id))
1167   ThreadToNativeFromVM ttn(thread);
1168   return JVM_ImageGetIndexAddress(env, id);
1169 WB_END
1170 
1171 WB_ENTRY(jlong, WB_imageGetDataAddress(JNIEnv *env, jobject wb, jlong id))
1172   ThreadToNativeFromVM ttn(thread);
1173   return JVM_ImageGetDataAddress(env, id);
1174 WB_END
1175 
1176 WB_ENTRY(jboolean, WB_imageRead(JNIEnv *env, jobject wb, jlong id, jlong offset, jobject uncompressedBuffer, jlong uncompressed_size))
1177   ThreadToNativeFromVM ttn(thread);
1178   if (uncompressedBuffer == NULL) {
1179     return JNI_FALSE;
1180   }
1181   unsigned char* uncompressedAddress =
1182           (unsigned char*) env->GetDirectBufferAddress(uncompressedBuffer);
1183   return JVM_ImageRead(env, id, offset, uncompressedAddress, uncompressed_size);
1184 WB_END
1185 
1186 WB_ENTRY(jboolean, WB_imageReadCompressed(JNIEnv *env, jobject wb, jlong id, jlong offset, jobject compressedBuffer, jlong compressed_size, jobject uncompressedBuffer, jlong uncompressed_size))
1187   ThreadToNativeFromVM ttn(thread);
1188   if (uncompressedBuffer == NULL || compressedBuffer == NULL) {
1189     return false;
1190   }
1191   // Get address of read direct buffer.
1192   unsigned char* compressedAddress =
1193         (unsigned char*) env->GetDirectBufferAddress(compressedBuffer);
1194   // Get address of decompression direct buffer.
1195   unsigned char* uncompressedAddress =
1196         (unsigned char*) env->GetDirectBufferAddress(uncompressedBuffer);
1197   return JVM_ImageReadCompressed(env, id, offset, compressedAddress, compressed_size, uncompressedAddress, uncompressed_size);
1198 WB_END
1199 
1200 WB_ENTRY(jbyteArray, WB_imageGetStringBytes(JNIEnv *env, jobject wb, jlong id, jlong offset))
1201   ThreadToNativeFromVM ttn(thread);
1202   const char* data = JVM_ImageGetStringBytes(env, id, offset);
1203   // Determine String length.
1204   size_t size = strlen(data);
1205   // Allocate byte array.
1206   jbyteArray byteArray = env->NewByteArray((jsize) size);
1207   // Get array base address.
1208   jbyte* rawBytes = env->GetByteArrayElements(byteArray, NULL);
1209   // Copy bytes from image string table.
1210   memcpy(rawBytes, data, size);
1211   // Release byte array base address.
1212   env->ReleaseByteArrayElements(byteArray, rawBytes, 0);
1213   return byteArray;
1214 WB_END
1215 
1216 WB_ENTRY(jlong, WB_imageGetStringsSize(JNIEnv *env, jobject wb, jlong id))
1217   ImageFileReader* reader = ImageFileReader::idToReader(id);
1218   return reader? reader->strings_size() : 0L;
1219 WB_END
1220 
1221 WB_ENTRY(jlongArray, WB_imageGetAttributes(JNIEnv *env, jobject wb, jlong id, jint offset))
1222   ThreadToNativeFromVM ttn(thread);
1223   // Allocate a jlong large enough for all location attributes.
1224   jlongArray attributes = env->NewLongArray(JVM_ImageGetAttributesCount(env));
1225   // Get base address for jlong array.
1226   jlong* rawAttributes = env->GetLongArrayElements(attributes, NULL);
1227   jlong* ret = JVM_ImageGetAttributes(env, rawAttributes, id, offset);
1228   // Release jlong array base address.
1229   env->ReleaseLongArrayElements(attributes, rawAttributes, 0);
1230     return ret == NULL ? NULL : attributes;
1231 WB_END
1232 
1233 WB_ENTRY(jlongArray, WB_imageFindAttributes(JNIEnv *env, jobject wb, jlong id, jbyteArray utf8))
1234   ThreadToNativeFromVM ttn(thread);
1235   // Allocate a jlong large enough for all location attributes.
1236   jlongArray attributes = env->NewLongArray(JVM_ImageGetAttributesCount(env));
1237   // Get base address for jlong array.
1238   jlong* rawAttributes = env->GetLongArrayElements(attributes, NULL);
1239   jsize size = env->GetArrayLength(utf8);
1240   jbyte* rawBytes = env->GetByteArrayElements(utf8, NULL);
1241   jlong* ret = JVM_ImageFindAttributes(env, rawAttributes, rawBytes, size, id);
1242   env->ReleaseByteArrayElements(utf8, rawBytes, 0);
1243   env->ReleaseLongArrayElements(attributes, rawAttributes, 0);
1244   return ret == NULL ? NULL : attributes;
1245 WB_END
1246 
1247 WB_ENTRY(jintArray, WB_imageAttributeOffsets(JNIEnv *env, jobject wb, jlong id))
1248   ThreadToNativeFromVM ttn(thread);
1249   unsigned int length = JVM_ImageAttributeOffsetsLength(env, id);
1250   if (length == 0) {
1251     return NULL;
1252   }
1253   jintArray offsets = env->NewIntArray(length);
1254   // Get base address of result.
1255   jint* rawOffsets = env->GetIntArrayElements(offsets, NULL);
1256   jint* ret = JVM_ImageAttributeOffsets(env, rawOffsets, length, id);
1257   // Release result base address.
1258   env->ReleaseIntArrayElements(offsets, rawOffsets, 0);
1259   return ret == NULL ? NULL : offsets;
1260 WB_END
1261 
1262 WB_ENTRY(jint, WB_imageGetIntAtAddress(JNIEnv *env, jobject wb, jlong address, jint offset, jboolean big_endian))
1263   unsigned char* arr = (unsigned char*) address + offset;
1264   jint uraw;
1265   if (big_endian) {
1266      uraw = arr[0] << 24 | arr[1]<<16 | (arr[2]<<8) | arr[3];
1267   } else {
1268       uraw = arr[0] | arr[1]<<8 | (arr[2]<<16) | arr[3]<<24;
1269   }
1270   return uraw;
1271 WB_END
1272 
1273 WB_ENTRY(void, WB_AssertMatchingSafepointCalls(JNIEnv* env, jobject o, jboolean mutexSafepointValue, jboolean attemptedNoSafepointValue))
1274   Monitor::SafepointCheckRequired sfpt_check_required = mutexSafepointValue ?
1275                                            Monitor::_safepoint_check_always :
1276                                            Monitor::_safepoint_check_never;
1277   MutexLockerEx ml(new Mutex(Mutex::leaf, "SFPT_Test_lock", true, sfpt_check_required),
1278                    attemptedNoSafepointValue == JNI_TRUE);
1279 WB_END
1280 
1281 WB_ENTRY(jboolean, WB_IsMonitorInflated(JNIEnv* env, jobject wb, jobject obj))
1282   oop obj_oop = JNIHandles::resolve(obj);
1283   return (jboolean) obj_oop->mark()->has_monitor();
1284 WB_END
1285 
1286 WB_ENTRY(void, WB_ForceSafepoint(JNIEnv* env, jobject wb))
1287   VM_ForceSafepoint force_safepoint_op;
1288   VMThread::execute(&force_safepoint_op);
1289 WB_END
1290 
1291 template <typename T>
1292 static bool GetMethodOption(JavaThread* thread, JNIEnv* env, jobject method, jstring name, T* value) {
1293   assert(value != NULL, "sanity");
1294   if (method == NULL || name == NULL) {
1295     return false;
1296   }
1297   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1298   CHECK_JNI_EXCEPTION_(env, false);
1299   methodHandle mh(thread, Method::checked_resolve_jmethod_id(jmid));
1300   // can't be in VM when we call JNI
1301   ThreadToNativeFromVM ttnfv(thread);
1302   const char* flag_name = env->GetStringUTFChars(name, NULL);
1303   bool result =  CompilerOracle::has_option_value(mh, flag_name, *value);
1304   env->ReleaseStringUTFChars(name, flag_name);
1305   return result;
1306 }
1307 
1308 WB_ENTRY(jobject, WB_GetMethodBooleaneOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1309   bool result;
1310   if (GetMethodOption<bool> (thread, env, method, name, &result)) {
1311     // can't be in VM when we call JNI
1312     ThreadToNativeFromVM ttnfv(thread);
1313     return booleanBox(thread, env, result);
1314   }
1315   return NULL;
1316 WB_END
1317 
1318 WB_ENTRY(jobject, WB_GetMethodIntxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1319   intx result;
1320   if (GetMethodOption <intx> (thread, env, method, name, &result)) {
1321     // can't be in VM when we call JNI
1322     ThreadToNativeFromVM ttnfv(thread);
1323     return longBox(thread, env, result);
1324   }
1325   return NULL;
1326 WB_END
1327 
1328 WB_ENTRY(jobject, WB_GetMethodUintxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1329   uintx result;
1330   if (GetMethodOption <uintx> (thread, env, method, name, &result)) {
1331     // can't be in VM when we call JNI
1332     ThreadToNativeFromVM ttnfv(thread);
1333     return longBox(thread, env, result);
1334   }
1335   return NULL;
1336 WB_END
1337 
1338 WB_ENTRY(jobject, WB_GetMethodDoubleOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1339   double result;
1340   if (GetMethodOption <double> (thread, env, method, name, &result)) {
1341     // can't be in VM when we call JNI
1342     ThreadToNativeFromVM ttnfv(thread);
1343     return doubleBox(thread, env, result);
1344   }
1345   return NULL;
1346 WB_END
1347 
1348 WB_ENTRY(jobject, WB_GetMethodStringOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1349   ccstr ccstrResult;
1350   if (GetMethodOption <ccstr> (thread, env, method, name, &ccstrResult)) {
1351     // can't be in VM when we call JNI
1352     ThreadToNativeFromVM ttnfv(thread);
1353     jstring result = env->NewStringUTF(ccstrResult);
1354     CHECK_JNI_EXCEPTION_(env, NULL);
1355     return result;
1356   }
1357   return NULL;
1358 WB_END
1359 
1360 WB_ENTRY(jboolean, WB_IsShared(JNIEnv* env, jobject wb, jobject obj))
1361   oop obj_oop = JNIHandles::resolve(obj);
1362   return MetaspaceShared::is_in_shared_space((void*)obj_oop);
1363 WB_END
1364 
1365 WB_ENTRY(jboolean, WB_AreSharedStringsIgnored(JNIEnv* env))
1366   return StringTable::shared_string_ignored();
1367 WB_END
1368 
1369 //Some convenience methods to deal with objects from java
1370 int WhiteBox::offset_for_field(const char* field_name, oop object,
1371     Symbol* signature_symbol) {
1372   assert(field_name != NULL && strlen(field_name) > 0, "Field name not valid");
1373   Thread* THREAD = Thread::current();
1374 
1375   //Get the class of our object
1376   Klass* arg_klass = object->klass();
1377   //Turn it into an instance-klass
1378   InstanceKlass* ik = InstanceKlass::cast(arg_klass);
1379 
1380   //Create symbols to look for in the class
1381   TempNewSymbol name_symbol = SymbolTable::lookup(field_name, (int) strlen(field_name),
1382       THREAD);
1383 
1384   //To be filled in with an offset of the field we're looking for
1385   fieldDescriptor fd;
1386 
1387   Klass* res = ik->find_field(name_symbol, signature_symbol, &fd);
1388   if (res == NULL) {
1389     tty->print_cr("Invalid layout of %s at %s", ik->external_name(),
1390         name_symbol->as_C_string());
1391     vm_exit_during_initialization("Invalid layout of preloaded class: use -XX:+TraceClassLoading to see the origin of the problem class");
1392   }
1393 
1394   //fetch the field at the offset we've found
1395   int dest_offset = fd.offset();
1396 
1397   return dest_offset;
1398 }
1399 
1400 
1401 const char* WhiteBox::lookup_jstring(const char* field_name, oop object) {
1402   int offset = offset_for_field(field_name, object,
1403       vmSymbols::string_signature());
1404   oop string = object->obj_field(offset);
1405   if (string == NULL) {
1406     return NULL;
1407   }
1408   const char* ret = java_lang_String::as_utf8_string(string);
1409   return ret;
1410 }
1411 
1412 bool WhiteBox::lookup_bool(const char* field_name, oop object) {
1413   int offset =
1414       offset_for_field(field_name, object, vmSymbols::bool_signature());
1415   bool ret = (object->bool_field(offset) == JNI_TRUE);
1416   return ret;
1417 }
1418 
1419 void WhiteBox::register_methods(JNIEnv* env, jclass wbclass, JavaThread* thread, JNINativeMethod* method_array, int method_count) {
1420   ResourceMark rm;
1421   ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1422 
1423   //  one by one registration natives for exception catching
1424   jclass no_such_method_error_klass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string());
1425   CHECK_JNI_EXCEPTION(env);
1426   for (int i = 0, n = method_count; i < n; ++i) {
1427     // Skip dummy entries
1428     if (method_array[i].fnPtr == NULL) continue;
1429     if (env->RegisterNatives(wbclass, &method_array[i], 1) != 0) {
1430       jthrowable throwable_obj = env->ExceptionOccurred();
1431       if (throwable_obj != NULL) {
1432         env->ExceptionClear();
1433         if (env->IsInstanceOf(throwable_obj, no_such_method_error_klass)) {
1434           // NoSuchMethodError is thrown when a method can't be found or a method is not native.
1435           // Ignoring the exception since it is not preventing use of other WhiteBox methods.
1436           tty->print_cr("Warning: 'NoSuchMethodError' on register of sun.hotspot.WhiteBox::%s%s",
1437               method_array[i].name, method_array[i].signature);
1438         }
1439       } else {
1440         // Registration failed unexpectedly.
1441         tty->print_cr("Warning: unexpected error on register of sun.hotspot.WhiteBox::%s%s. All methods will be unregistered",
1442             method_array[i].name, method_array[i].signature);
1443         env->UnregisterNatives(wbclass);
1444         break;
1445       }
1446     }
1447   }
1448 }
1449 
1450 #define CC (char*)
1451 
1452 static JNINativeMethod methods[] = {
1453   {CC"getObjectAddress0",                CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress  },
1454   {CC"getObjectSize0",                   CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectSize     },
1455   {CC"isObjectInOldGen0",                CC"(Ljava/lang/Object;)Z", (void*)&WB_isObjectInOldGen  },
1456   {CC"getHeapOopSize",                   CC"()I",                   (void*)&WB_GetHeapOopSize    },
1457   {CC"getVMPageSize",                    CC"()I",                   (void*)&WB_GetVMPageSize     },
1458   {CC"getVMAllocationGranularity",       CC"()J",                   (void*)&WB_GetVMAllocationGranularity },
1459   {CC"getVMLargePageSize",               CC"()J",                   (void*)&WB_GetVMLargePageSize},
1460   {CC"isClassAlive0",                    CC"(Ljava/lang/String;)Z", (void*)&WB_IsClassAlive      },
1461   {CC"parseCommandLine0",
1462       CC"(Ljava/lang/String;C[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;",
1463       (void*) &WB_ParseCommandLine
1464   },
1465   {CC"addToBootstrapClassLoaderSearch0", CC"(Ljava/lang/String;)V",
1466                                                       (void*)&WB_AddToBootstrapClassLoaderSearch},
1467   {CC"addToSystemClassLoaderSearch0",    CC"(Ljava/lang/String;)V",
1468                                                       (void*)&WB_AddToSystemClassLoaderSearch},
1469   {CC"getCompressedOopsMaxHeapSize", CC"()J",
1470       (void*)&WB_GetCompressedOopsMaxHeapSize},
1471   {CC"printHeapSizes",     CC"()V",                   (void*)&WB_PrintHeapSizes    },
1472   {CC"runMemoryUnitTests", CC"()V",                   (void*)&WB_RunMemoryUnitTests},
1473   {CC"readFromNoaccessArea",CC"()V",                  (void*)&WB_ReadFromNoaccessArea},
1474   {CC"stressVirtualSpaceResize",CC"(JJJ)I",           (void*)&WB_StressVirtualSpaceResize},
1475 #if INCLUDE_ALL_GCS
1476   {CC"g1InConcurrentMark", CC"()Z",                   (void*)&WB_G1InConcurrentMark},
1477   {CC"g1IsHumongous0",      CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous     },
1478   {CC"g1NumMaxRegions",    CC"()J",                   (void*)&WB_G1NumMaxRegions  },
1479   {CC"g1NumFreeRegions",   CC"()J",                   (void*)&WB_G1NumFreeRegions  },
1480   {CC"g1RegionSize",       CC"()I",                   (void*)&WB_G1RegionSize      },
1481   {CC"g1StartConcMarkCycle",       CC"()Z",           (void*)&WB_G1StartMarkCycle  },
1482   {CC"g1AuxiliaryMemoryUsage", CC"()Ljava/lang/management/MemoryUsage;",
1483                                                       (void*)&WB_G1AuxiliaryMemoryUsage  },
1484 #endif // INCLUDE_ALL_GCS
1485 #if INCLUDE_NMT
1486   {CC"NMTMalloc",           CC"(J)J",                 (void*)&WB_NMTMalloc          },
1487   {CC"NMTMallocWithPseudoStack", CC"(JI)J",           (void*)&WB_NMTMallocWithPseudoStack},
1488   {CC"NMTFree",             CC"(J)V",                 (void*)&WB_NMTFree            },
1489   {CC"NMTReserveMemory",    CC"(J)J",                 (void*)&WB_NMTReserveMemory   },
1490   {CC"NMTCommitMemory",     CC"(JJ)V",                (void*)&WB_NMTCommitMemory    },
1491   {CC"NMTUncommitMemory",   CC"(JJ)V",                (void*)&WB_NMTUncommitMemory  },
1492   {CC"NMTReleaseMemory",    CC"(JJ)V",                (void*)&WB_NMTReleaseMemory   },
1493   {CC"NMTChangeTrackingLevel", CC"()Z",               (void*)&WB_NMTChangeTrackingLevel},
1494   {CC"NMTGetHashSize",      CC"()I",                  (void*)&WB_NMTGetHashSize     },
1495 #endif // INCLUDE_NMT
1496   {CC"deoptimizeFrames",   CC"(Z)I",                  (void*)&WB_DeoptimizeFrames  },
1497   {CC"deoptimizeAll",      CC"()V",                   (void*)&WB_DeoptimizeAll     },
1498     {CC"deoptimizeMethod0",   CC"(Ljava/lang/reflect/Executable;Z)I",
1499                                                         (void*)&WB_DeoptimizeMethod  },
1500   {CC"isMethodCompiled0",   CC"(Ljava/lang/reflect/Executable;Z)Z",
1501                                                       (void*)&WB_IsMethodCompiled  },
1502   {CC"isMethodCompilable0", CC"(Ljava/lang/reflect/Executable;IZ)Z",
1503                                                       (void*)&WB_IsMethodCompilable},
1504   {CC"isMethodQueuedForCompilation0",
1505       CC"(Ljava/lang/reflect/Executable;)Z",          (void*)&WB_IsMethodQueuedForCompilation},
1506   {CC"isIntrinsicAvailableForMethod0",
1507       CC"(Ljava/lang/reflect/Executable;Ljava/lang/reflect/Executable;I)Z",
1508                                                       (void*)&WB_IsIntrinsicAvailableForMethod},
1509   {CC"makeMethodNotCompilable0",
1510       CC"(Ljava/lang/reflect/Executable;IZ)V",        (void*)&WB_MakeMethodNotCompilable},
1511   {CC"testSetDontInlineMethod0",
1512       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetDontInlineMethod},
1513   {CC"getMethodCompilationLevel0",
1514       CC"(Ljava/lang/reflect/Executable;Z)I",         (void*)&WB_GetMethodCompilationLevel},
1515   {CC"getMethodEntryBci0",
1516       CC"(Ljava/lang/reflect/Executable;)I",          (void*)&WB_GetMethodEntryBci},
1517   {CC"getCompileQueueSize",
1518       CC"(I)I",                                       (void*)&WB_GetCompileQueueSize},
1519   {CC"testSetForceInlineMethod0",
1520       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetForceInlineMethod},
1521   {CC"enqueueMethodForCompilation0",
1522       CC"(Ljava/lang/reflect/Executable;II)Z",        (void*)&WB_EnqueueMethodForCompilation},
1523   {CC"clearMethodState0",
1524       CC"(Ljava/lang/reflect/Executable;)V",          (void*)&WB_ClearMethodState},
1525   {CC"lockCompilation",    CC"()V",                   (void*)&WB_LockCompilation},
1526   {CC"unlockCompilation",  CC"()V",                   (void*)&WB_UnlockCompilation},
1527   {CC"isConstantVMFlag",   CC"(Ljava/lang/String;)Z", (void*)&WB_IsConstantVMFlag},
1528   {CC"isLockedVMFlag",     CC"(Ljava/lang/String;)Z", (void*)&WB_IsLockedVMFlag},
1529   {CC"setBooleanVMFlag",   CC"(Ljava/lang/String;Z)V",(void*)&WB_SetBooleanVMFlag},
1530   {CC"setIntVMFlag",       CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntVMFlag},
1531   {CC"setUintVMFlag",      CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintVMFlag},
1532   {CC"setIntxVMFlag",      CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntxVMFlag},
1533   {CC"setUintxVMFlag",     CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintxVMFlag},
1534   {CC"setUint64VMFlag",    CC"(Ljava/lang/String;J)V",(void*)&WB_SetUint64VMFlag},
1535   {CC"setSizeTVMFlag",     CC"(Ljava/lang/String;J)V",(void*)&WB_SetSizeTVMFlag},
1536   {CC"setDoubleVMFlag",    CC"(Ljava/lang/String;D)V",(void*)&WB_SetDoubleVMFlag},
1537   {CC"setStringVMFlag",    CC"(Ljava/lang/String;Ljava/lang/String;)V",
1538                                                       (void*)&WB_SetStringVMFlag},
1539   {CC"getBooleanVMFlag",   CC"(Ljava/lang/String;)Ljava/lang/Boolean;",
1540                                                       (void*)&WB_GetBooleanVMFlag},
1541   {CC"getIntVMFlag",       CC"(Ljava/lang/String;)Ljava/lang/Long;",
1542                                                       (void*)&WB_GetIntVMFlag},
1543   {CC"getUintVMFlag",      CC"(Ljava/lang/String;)Ljava/lang/Long;",
1544                                                       (void*)&WB_GetUintVMFlag},
1545   {CC"getIntxVMFlag",      CC"(Ljava/lang/String;)Ljava/lang/Long;",
1546                                                       (void*)&WB_GetIntxVMFlag},
1547   {CC"getUintxVMFlag",     CC"(Ljava/lang/String;)Ljava/lang/Long;",
1548                                                       (void*)&WB_GetUintxVMFlag},
1549   {CC"getUint64VMFlag",    CC"(Ljava/lang/String;)Ljava/lang/Long;",
1550                                                       (void*)&WB_GetUint64VMFlag},
1551   {CC"getSizeTVMFlag",     CC"(Ljava/lang/String;)Ljava/lang/Long;",
1552                                                       (void*)&WB_GetSizeTVMFlag},
1553   {CC"getDoubleVMFlag",    CC"(Ljava/lang/String;)Ljava/lang/Double;",
1554                                                       (void*)&WB_GetDoubleVMFlag},
1555   {CC"getStringVMFlag",    CC"(Ljava/lang/String;)Ljava/lang/String;",
1556                                                       (void*)&WB_GetStringVMFlag},
1557   {CC"isInStringTable",    CC"(Ljava/lang/String;)Z", (void*)&WB_IsInStringTable  },
1558   {CC"fullGC",   CC"()V",                             (void*)&WB_FullGC },
1559   {CC"youngGC",  CC"()V",                             (void*)&WB_YoungGC },
1560   {CC"readReservedMemory", CC"()V",                   (void*)&WB_ReadReservedMemory },
1561   {CC"allocateMetaspace",
1562      CC"(Ljava/lang/ClassLoader;J)J",                 (void*)&WB_AllocateMetaspace },
1563   {CC"freeMetaspace",
1564      CC"(Ljava/lang/ClassLoader;JJ)V",                (void*)&WB_FreeMetaspace },
1565   {CC"incMetaspaceCapacityUntilGC", CC"(J)J",         (void*)&WB_IncMetaspaceCapacityUntilGC },
1566   {CC"metaspaceCapacityUntilGC", CC"()J",             (void*)&WB_MetaspaceCapacityUntilGC },
1567   {CC"getCPUFeatures",     CC"()Ljava/lang/String;",  (void*)&WB_GetCPUFeatures     },
1568   {CC"getNMethod0",         CC"(Ljava/lang/reflect/Executable;Z)[Ljava/lang/Object;",
1569                                                       (void*)&WB_GetNMethod         },
1570   {CC"forceNMethodSweep",  CC"()V",                   (void*)&WB_ForceNMethodSweep  },
1571   {CC"allocateCodeBlob",   CC"(II)J",                 (void*)&WB_AllocateCodeBlob   },
1572   {CC"freeCodeBlob",       CC"(J)V",                  (void*)&WB_FreeCodeBlob       },
1573   {CC"getCodeHeapEntries", CC"(I)[Ljava/lang/Object;",(void*)&WB_GetCodeHeapEntries },
1574   {CC"getCompilationActivityMode",
1575                            CC"()I",                   (void*)&WB_GetCompilationActivityMode},
1576   {CC"getCodeBlob",        CC"(J)[Ljava/lang/Object;",(void*)&WB_GetCodeBlob        },
1577   {CC"getThreadStackSize", CC"()J",                   (void*)&WB_GetThreadStackSize },
1578   {CC"getThreadRemainingStackSize", CC"()J",          (void*)&WB_GetThreadRemainingStackSize },
1579   {CC"readImageFile",      CC"(Ljava/lang/String;)Z", (void*)&WB_ReadImageFile },
1580   {CC"imageOpenImage",     CC"(Ljava/lang/String;Z)J",(void*)&WB_imageOpenImage },
1581   {CC"imageCloseImage",    CC"(J)V",                  (void*)&WB_imageCloseImage },
1582   {CC"imageGetIndexAddress",CC"(J)J",                 (void*)&WB_imageGetIndexAddress},
1583   {CC"imageGetDataAddress",CC"(J)J",                  (void*)&WB_imageGetDataAddress},
1584   {CC"imageRead",          CC"(JJLjava/nio/ByteBuffer;J)Z",
1585                                                       (void*)&WB_imageRead    },
1586   {CC"imageReadCompressed",CC"(JJLjava/nio/ByteBuffer;JLjava/nio/ByteBuffer;J)Z",
1587                                                       (void*)&WB_imageReadCompressed},
1588   {CC"imageGetStringBytes",CC"(JI)[B",                (void*)&WB_imageGetStringBytes},
1589   {CC"imageGetStringsSize",CC"(J)J",                  (void*)&WB_imageGetStringsSize},
1590   {CC"imageGetAttributes", CC"(JI)[J",                (void*)&WB_imageGetAttributes},
1591   {CC"imageFindAttributes",CC"(J[B)[J",               (void*)&WB_imageFindAttributes},
1592   {CC"imageAttributeOffsets",CC"(J)[I",               (void*)&WB_imageAttributeOffsets},
1593   {CC"imageGetIntAtAddress",CC"(JIZ)I",                (void*)&WB_imageGetIntAtAddress},
1594   {CC"assertMatchingSafepointCalls", CC"(ZZ)V",       (void*)&WB_AssertMatchingSafepointCalls },
1595   {CC"isMonitorInflated0", CC"(Ljava/lang/Object;)Z", (void*)&WB_IsMonitorInflated  },
1596   {CC"forceSafepoint",     CC"()V",                   (void*)&WB_ForceSafepoint     },
1597   {CC"getMethodBooleanOption",
1598       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Boolean;",
1599                                                       (void*)&WB_GetMethodBooleaneOption},
1600   {CC"getMethodIntxOption",
1601       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
1602                                                       (void*)&WB_GetMethodIntxOption},
1603   {CC"getMethodUintxOption",
1604       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
1605                                                       (void*)&WB_GetMethodUintxOption},
1606   {CC"getMethodDoubleOption",
1607       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Double;",
1608                                                       (void*)&WB_GetMethodDoubleOption},
1609   {CC"getMethodStringOption",
1610       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/String;",
1611                                                       (void*)&WB_GetMethodStringOption},
1612   {CC"isShared",           CC"(Ljava/lang/Object;)Z", (void*)&WB_IsShared },
1613   {CC"areSharedStringsIgnored",           CC"()Z",    (void*)&WB_AreSharedStringsIgnored },
1614 };
1615 
1616 #undef CC
1617 
1618 JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass))
1619   {
1620     if (WhiteBoxAPI) {
1621       // Make sure that wbclass is loaded by the null classloader
1622       instanceKlassHandle ikh = instanceKlassHandle(JNIHandles::resolve(wbclass)->klass());
1623       Handle loader(ikh->class_loader());
1624       if (loader.is_null()) {
1625         WhiteBox::register_methods(env, wbclass, thread, methods, sizeof(methods) / sizeof(methods[0]));
1626         WhiteBox::register_extended(env, wbclass, thread);
1627         WhiteBox::set_used();
1628       }
1629     }
1630   }
1631 JVM_END