1 /*
   2  * Copyright (c) 2012, 2022, 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 #include <new>
  27 #include "cds.h"
  28 #include "cds/cdsConstants.hpp"
  29 #include "cds/filemap.hpp"
  30 #include "cds/heapShared.inline.hpp"
  31 #include "cds/metaspaceShared.hpp"
  32 #include "classfile/classLoaderDataGraph.hpp"
  33 #include "classfile/javaClasses.inline.hpp"
  34 #include "classfile/modules.hpp"
  35 #include "classfile/protectionDomainCache.hpp"
  36 #include "classfile/stringTable.hpp"
  37 #include "classfile/symbolTable.hpp"
  38 #include "classfile/systemDictionary.hpp"
  39 #include "classfile/vmSymbols.hpp"
  40 #include "code/codeCache.hpp"
  41 #include "compiler/compilationPolicy.hpp"
  42 #include "compiler/directivesParser.hpp"
  43 #include "compiler/methodMatcher.hpp"
  44 #include "gc/shared/concurrentGCBreakpoints.hpp"
  45 #include "gc/shared/gcConfig.hpp"
  46 #include "gc/shared/gcLocker.inline.hpp"
  47 #include "gc/shared/genArguments.hpp"
  48 #include "gc/shared/genCollectedHeap.hpp"
  49 #include "jvmtifiles/jvmtiEnv.hpp"
  50 #include "logging/log.hpp"
  51 #include "memory/iterator.hpp"
  52 #include "memory/metadataFactory.hpp"
  53 #include "memory/metaspace/testHelpers.hpp"
  54 #include "memory/metaspaceUtils.hpp"
  55 #include "memory/oopFactory.hpp"
  56 #include "memory/resourceArea.hpp"
  57 #include "memory/universe.hpp"
  58 #include "oops/array.hpp"
  59 #include "oops/compressedOops.hpp"
  60 #include "oops/constantPool.inline.hpp"
  61 #include "oops/klass.inline.hpp"
  62 #include "oops/method.inline.hpp"
  63 #include "oops/objArrayKlass.hpp"
  64 #include "oops/objArrayOop.inline.hpp"
  65 #include "oops/oop.inline.hpp"
  66 #include "oops/typeArrayOop.inline.hpp"
  67 #include "prims/resolvedMethodTable.hpp"
  68 #include "prims/wbtestmethods/parserTests.hpp"
  69 #include "prims/whitebox.inline.hpp"
  70 #include "runtime/arguments.hpp"
  71 #include "runtime/atomic.hpp"
  72 #include "runtime/deoptimization.hpp"
  73 #include "runtime/fieldDescriptor.inline.hpp"
  74 #include "runtime/flags/jvmFlag.hpp"
  75 #include "runtime/flags/jvmFlagAccess.hpp"
  76 #include "runtime/frame.inline.hpp"
  77 #include "runtime/handles.inline.hpp"
  78 #include "runtime/handshake.hpp"
  79 #include "runtime/interfaceSupport.inline.hpp"
  80 #include "runtime/javaCalls.hpp"
  81 #include "runtime/jniHandles.inline.hpp"
  82 #include "runtime/os.hpp"
  83 #include "runtime/stackFrameStream.inline.hpp"
  84 #include "runtime/sweeper.hpp"
  85 #include "runtime/synchronizer.hpp"
  86 #include "runtime/thread.hpp"
  87 #include "runtime/threadSMR.hpp"
  88 #include "runtime/vframe.hpp"
  89 #include "runtime/vm_version.hpp"
  90 #include "services/mallocSiteTable.hpp"
  91 #include "services/memoryService.hpp"
  92 #include "services/memTracker.hpp"
  93 #include "utilities/align.hpp"
  94 #include "utilities/debug.hpp"
  95 #include "utilities/elfFile.hpp"
  96 #include "utilities/exceptions.hpp"
  97 #include "utilities/macros.hpp"
  98 #include "utilities/nativeCallStack.hpp"
  99 #include "utilities/ostream.hpp"
 100 #if INCLUDE_G1GC
 101 #include "gc/g1/g1Arguments.hpp"
 102 #include "gc/g1/g1CollectedHeap.inline.hpp"
 103 #include "gc/g1/g1ConcurrentMark.hpp"
 104 #include "gc/g1/g1ConcurrentMarkThread.inline.hpp"
 105 #include "gc/g1/heapRegionManager.hpp"
 106 #include "gc/g1/heapRegionRemSet.inline.hpp"
 107 #endif // INCLUDE_G1GC
 108 #if INCLUDE_PARALLELGC
 109 #include "gc/parallel/parallelScavengeHeap.inline.hpp"
 110 #endif // INCLUDE_PARALLELGC
 111 #if INCLUDE_JVMCI
 112 #include "jvmci/jvmciEnv.hpp"
 113 #include "jvmci/jvmciRuntime.hpp"
 114 #endif
 115 
 116 #ifdef LINUX
 117 #include "osContainer_linux.hpp"
 118 #include "cgroupSubsystem_linux.hpp"
 119 #endif
 120 
 121 #define SIZE_T_MAX_VALUE ((size_t) -1)
 122 
 123 #define CHECK_JNI_EXCEPTION_(env, value)                               \
 124   do {                                                                 \
 125     JavaThread* THREAD = JavaThread::thread_from_jni_environment(env); \
 126     THREAD->clear_pending_jni_exception_check();                       \
 127     if (HAS_PENDING_EXCEPTION) {                                       \
 128       return(value);                                                   \
 129     }                                                                  \
 130   } while (0)
 131 
 132 #define CHECK_JNI_EXCEPTION(env)                                       \
 133   do {                                                                 \
 134     JavaThread* THREAD = JavaThread::thread_from_jni_environment(env); \
 135     THREAD->clear_pending_jni_exception_check();                       \
 136     if (HAS_PENDING_EXCEPTION) {                                       \
 137       return;                                                          \
 138     }                                                                  \
 139   } while (0)
 140 
 141 bool WhiteBox::_used = false;
 142 volatile bool WhiteBox::compilation_locked = false;
 143 
 144 class VM_WhiteBoxOperation : public VM_Operation {
 145  public:
 146   VM_WhiteBoxOperation()                         { }
 147   VMOp_Type type()                  const        { return VMOp_WhiteBoxOperation; }
 148   bool allow_nested_vm_operations() const        { return true; }
 149 };
 150 
 151 
 152 WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
 153   return (jlong)(void*)JNIHandles::resolve(obj);
 154 WB_END
 155 
 156 WB_ENTRY(jint, WB_GetHeapOopSize(JNIEnv* env, jobject o))
 157   return heapOopSize;
 158 WB_END
 159 
 160 WB_ENTRY(jint, WB_GetVMPageSize(JNIEnv* env, jobject o))
 161   return os::vm_page_size();
 162 WB_END
 163 
 164 WB_ENTRY(jlong, WB_GetVMAllocationGranularity(JNIEnv* env, jobject o))
 165   return os::vm_allocation_granularity();
 166 WB_END
 167 
 168 WB_ENTRY(jlong, WB_GetVMLargePageSize(JNIEnv* env, jobject o))
 169   return os::large_page_size();
 170 WB_END
 171 
 172 class WBIsKlassAliveClosure : public LockedClassesDo {
 173     Symbol* _name;
 174     int _count;
 175 public:
 176     WBIsKlassAliveClosure(Symbol* name) : _name(name), _count(0) {}
 177 
 178     void do_klass(Klass* k) {
 179       Symbol* ksym = k->name();
 180       if (ksym->fast_compare(_name) == 0) {
 181         _count++;
 182       } else if (k->is_instance_klass()) {
 183         // Need special handling for hidden classes because the JVM
 184         // appends "+<hex-address>" to hidden class names.
 185         InstanceKlass *ik = InstanceKlass::cast(k);
 186         if (ik->is_hidden()) {
 187           ResourceMark rm;
 188           char* k_name = ksym->as_C_string();
 189           // Find the first '+' char and truncate the string at that point.
 190           // NOTE: This will not work correctly if the original hidden class
 191           // name contains a '+'.
 192           char* plus_char = strchr(k_name, '+');
 193           if (plus_char != NULL) {
 194             *plus_char = 0;
 195             char* c_name = _name->as_C_string();
 196             if (strcmp(c_name, k_name) == 0) {
 197               _count++;
 198             }
 199           }
 200         }
 201       }
 202     }
 203 
 204     int count() const {
 205         return _count;
 206     }
 207 };
 208 
 209 WB_ENTRY(jint, WB_CountAliveClasses(JNIEnv* env, jobject target, jstring name))
 210   oop h_name = JNIHandles::resolve(name);
 211   if (h_name == NULL) return false;
 212   Symbol* sym = java_lang_String::as_symbol(h_name);
 213   TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
 214 
 215   WBIsKlassAliveClosure closure(sym);
 216   ClassLoaderDataGraph::classes_do(&closure);
 217 
 218   // Return the count of alive classes with this name.
 219   return closure.count();
 220 WB_END
 221 
 222 WB_ENTRY(jint, WB_GetSymbolRefcount(JNIEnv* env, jobject unused, jstring name))
 223   oop h_name = JNIHandles::resolve(name);
 224   if (h_name == NULL) return false;
 225   Symbol* sym = java_lang_String::as_symbol(h_name);
 226   TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
 227   return (jint)sym->refcount();
 228 WB_END
 229 
 230 
 231 WB_ENTRY(void, WB_AddToBootstrapClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) {
 232 #if INCLUDE_JVMTI
 233   ResourceMark rm;
 234   const char* seg = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(segment));
 235   JvmtiEnv* jvmti_env = JvmtiEnv::create_a_jvmti(JVMTI_VERSION);
 236   jvmtiError err = jvmti_env->AddToBootstrapClassLoaderSearch(seg);
 237   assert(err == JVMTI_ERROR_NONE, "must not fail");
 238 #endif
 239 }
 240 WB_END
 241 
 242 WB_ENTRY(void, WB_AddToSystemClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) {
 243 #if INCLUDE_JVMTI
 244   ResourceMark rm;
 245   const char* seg = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(segment));
 246   JvmtiEnv* jvmti_env = JvmtiEnv::create_a_jvmti(JVMTI_VERSION);
 247   jvmtiError err = jvmti_env->AddToSystemClassLoaderSearch(seg);
 248   assert(err == JVMTI_ERROR_NONE, "must not fail");
 249 #endif
 250 }
 251 WB_END
 252 
 253 
 254 WB_ENTRY(jlong, WB_GetCompressedOopsMaxHeapSize(JNIEnv* env, jobject o)) {
 255   return (jlong)Arguments::max_heap_for_compressed_oops();
 256 }
 257 WB_END
 258 
 259 WB_ENTRY(void, WB_PrintHeapSizes(JNIEnv* env, jobject o)) {
 260   tty->print_cr("Minimum heap " SIZE_FORMAT " Initial heap " SIZE_FORMAT " "
 261                 "Maximum heap " SIZE_FORMAT " Space alignment " SIZE_FORMAT " Heap alignment " SIZE_FORMAT,
 262                 MinHeapSize,
 263                 InitialHeapSize,
 264                 MaxHeapSize,
 265                 SpaceAlignment,
 266                 HeapAlignment);
 267 }
 268 WB_END
 269 
 270 WB_ENTRY(void, WB_ReadFromNoaccessArea(JNIEnv* env, jobject o))
 271   size_t granularity = os::vm_allocation_granularity();
 272   ReservedHeapSpace rhs(100 * granularity, granularity, os::vm_page_size());
 273   VirtualSpace vs;
 274   vs.initialize(rhs, 50 * granularity);
 275 
 276   // Check if constraints are complied
 277   if (!( UseCompressedOops && rhs.base() != NULL &&
 278          CompressedOops::base() != NULL &&
 279          CompressedOops::use_implicit_null_checks() )) {
 280     tty->print_cr("WB_ReadFromNoaccessArea method is useless:\n "
 281                   "\tUseCompressedOops is %d\n"
 282                   "\trhs.base() is " PTR_FORMAT "\n"
 283                   "\tCompressedOops::base() is " PTR_FORMAT "\n"
 284                   "\tCompressedOops::use_implicit_null_checks() is %d",
 285                   UseCompressedOops,
 286                   p2i(rhs.base()),
 287                   p2i(CompressedOops::base()),
 288                   CompressedOops::use_implicit_null_checks());
 289     return;
 290   }
 291   tty->print_cr("Reading from no access area... ");
 292   tty->print_cr("*(vs.low_boundary() - rhs.noaccess_prefix() / 2 ) = %c",
 293                 *(vs.low_boundary() - rhs.noaccess_prefix() / 2 ));
 294 WB_END
 295 
 296 static jint wb_stress_virtual_space_resize(size_t reserved_space_size,
 297                                            size_t magnitude, size_t iterations) {
 298   size_t granularity = os::vm_allocation_granularity();
 299   ReservedHeapSpace rhs(reserved_space_size * granularity, granularity, os::vm_page_size());
 300   VirtualSpace vs;
 301   if (!vs.initialize(rhs, 0)) {
 302     tty->print_cr("Failed to initialize VirtualSpace. Can't proceed.");
 303     return 3;
 304   }
 305 
 306   int seed = os::random();
 307   tty->print_cr("Random seed is %d", seed);
 308 
 309   for (size_t i = 0; i < iterations; i++) {
 310 
 311     // Whether we will shrink or grow
 312     bool shrink = os::random() % 2L == 0;
 313 
 314     // Get random delta to resize virtual space
 315     size_t delta = (size_t)os::random() % magnitude;
 316 
 317     // If we are about to shrink virtual space below zero, then expand instead
 318     if (shrink && vs.committed_size() < delta) {
 319       shrink = false;
 320     }
 321 
 322     // Resizing by delta
 323     if (shrink) {
 324       vs.shrink_by(delta);
 325     } else {
 326       // If expanding fails expand_by will silently return false
 327       vs.expand_by(delta, true);
 328     }
 329   }
 330   return 0;
 331 }
 332 
 333 WB_ENTRY(jint, WB_StressVirtualSpaceResize(JNIEnv* env, jobject o,
 334         jlong reserved_space_size, jlong magnitude, jlong iterations))
 335   tty->print_cr("reservedSpaceSize=" JLONG_FORMAT ", magnitude=" JLONG_FORMAT ", "
 336                 "iterations=" JLONG_FORMAT "\n", reserved_space_size, magnitude,
 337                 iterations);
 338   if (reserved_space_size < 0 || magnitude < 0 || iterations < 0) {
 339     tty->print_cr("One of variables printed above is negative. Can't proceed.\n");
 340     return 1;
 341   }
 342 
 343   // sizeof(size_t) depends on whether OS is 32bit or 64bit. sizeof(jlong) is
 344   // always 8 byte. That's why we should avoid overflow in case of 32bit platform.
 345   if (sizeof(size_t) < sizeof(jlong)) {
 346     jlong size_t_max_value = (jlong) SIZE_T_MAX_VALUE;
 347     if (reserved_space_size > size_t_max_value || magnitude > size_t_max_value
 348         || iterations > size_t_max_value) {
 349       tty->print_cr("One of variables printed above overflows size_t. Can't proceed.\n");
 350       return 2;
 351     }
 352   }
 353 
 354   return wb_stress_virtual_space_resize((size_t) reserved_space_size,
 355                                         (size_t) magnitude, (size_t) iterations);
 356 WB_END
 357 
 358 WB_ENTRY(jboolean, WB_IsGCSupported(JNIEnv* env, jobject o, jint name))
 359   return GCConfig::is_gc_supported((CollectedHeap::Name)name);
 360 WB_END
 361 
 362 WB_ENTRY(jboolean, WB_IsGCSupportedByJVMCICompiler(JNIEnv* env, jobject o, jint name))
 363 #if INCLUDE_JVMCI
 364   if (EnableJVMCI) {
 365     JVMCIEnv jvmciEnv(thread, env, __FILE__, __LINE__);
 366     return jvmciEnv.runtime()->is_gc_supported(&jvmciEnv, (CollectedHeap::Name)name);
 367   }
 368 #endif
 369   return false;
 370 WB_END
 371 
 372 WB_ENTRY(jboolean, WB_IsGCSelected(JNIEnv* env, jobject o, jint name))
 373   return GCConfig::is_gc_selected((CollectedHeap::Name)name);
 374 WB_END
 375 
 376 WB_ENTRY(jboolean, WB_IsGCSelectedErgonomically(JNIEnv* env, jobject o))
 377   return GCConfig::is_gc_selected_ergonomically();
 378 WB_END
 379 
 380 WB_ENTRY(jboolean, WB_isObjectInOldGen(JNIEnv* env, jobject o, jobject obj))
 381   oop p = JNIHandles::resolve(obj);
 382 #if INCLUDE_G1GC
 383   if (UseG1GC) {
 384     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 385     const HeapRegion* hr = g1h->heap_region_containing(p);
 386     if (hr == NULL) {
 387       return false;
 388     }
 389     return !(hr->is_young());
 390   }
 391 #endif
 392 #if INCLUDE_PARALLELGC
 393   if (UseParallelGC) {
 394     ParallelScavengeHeap* psh = ParallelScavengeHeap::heap();
 395     return !psh->is_in_young(p);
 396   }
 397 #endif
 398 #if INCLUDE_ZGC
 399   if (UseZGC) {
 400     return Universe::heap()->is_in(p);
 401   }
 402 #endif
 403 #if INCLUDE_SHENANDOAHGC
 404   if (UseShenandoahGC) {
 405     return Universe::heap()->is_in(p);
 406   }
 407 #endif
 408   GenCollectedHeap* gch = GenCollectedHeap::heap();
 409   return !gch->is_in_young(p);
 410 WB_END
 411 
 412 WB_ENTRY(jlong, WB_GetObjectSize(JNIEnv* env, jobject o, jobject obj))
 413   oop p = JNIHandles::resolve(obj);
 414   return p->size() * HeapWordSize;
 415 WB_END
 416 
 417 WB_ENTRY(jlong, WB_GetHeapSpaceAlignment(JNIEnv* env, jobject o))
 418   return (jlong)SpaceAlignment;
 419 WB_END
 420 
 421 WB_ENTRY(jlong, WB_GetHeapAlignment(JNIEnv* env, jobject o))
 422   return (jlong)HeapAlignment;
 423 WB_END
 424 
 425 WB_ENTRY(jboolean, WB_SupportsConcurrentGCBreakpoints(JNIEnv* env, jobject o))
 426   return Universe::heap()->supports_concurrent_gc_breakpoints();
 427 WB_END
 428 
 429 WB_ENTRY(void, WB_ConcurrentGCAcquireControl(JNIEnv* env, jobject o))
 430   ConcurrentGCBreakpoints::acquire_control();
 431 WB_END
 432 
 433 WB_ENTRY(void, WB_ConcurrentGCReleaseControl(JNIEnv* env, jobject o))
 434   ConcurrentGCBreakpoints::release_control();
 435 WB_END
 436 
 437 WB_ENTRY(void, WB_ConcurrentGCRunToIdle(JNIEnv* env, jobject o))
 438   ConcurrentGCBreakpoints::run_to_idle();
 439 WB_END
 440 
 441 WB_ENTRY(jboolean, WB_ConcurrentGCRunTo(JNIEnv* env, jobject o, jobject at))
 442   Handle h_name(THREAD, JNIHandles::resolve(at));
 443   ResourceMark rm;
 444   const char* c_name = java_lang_String::as_utf8_string(h_name());
 445   return ConcurrentGCBreakpoints::run_to(c_name);
 446 WB_END
 447 
 448 #if INCLUDE_G1GC
 449 
 450 WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj))
 451   if (UseG1GC) {
 452     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 453     oop result = JNIHandles::resolve(obj);
 454     const HeapRegion* hr = g1h->heap_region_containing(result);
 455     return hr->is_humongous();
 456   }
 457   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1IsHumongous: G1 GC is not enabled");
 458 WB_END
 459 
 460 WB_ENTRY(jboolean, WB_G1BelongsToHumongousRegion(JNIEnv* env, jobject o, jlong addr))
 461   if (UseG1GC) {
 462     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 463     const HeapRegion* hr = g1h->heap_region_containing((void*) addr);
 464     return hr->is_humongous();
 465   }
 466   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1BelongsToHumongousRegion: G1 GC is not enabled");
 467 WB_END
 468 
 469 WB_ENTRY(jboolean, WB_G1BelongsToFreeRegion(JNIEnv* env, jobject o, jlong addr))
 470   if (UseG1GC) {
 471     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 472     const HeapRegion* hr = g1h->heap_region_containing((void*) addr);
 473     return hr->is_free();
 474   }
 475   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1BelongsToFreeRegion: G1 GC is not enabled");
 476 WB_END
 477 
 478 WB_ENTRY(jlong, WB_G1NumMaxRegions(JNIEnv* env, jobject o))
 479   if (UseG1GC) {
 480     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 481     size_t nr = g1h->max_regions();
 482     return (jlong)nr;
 483   }
 484   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1NumMaxRegions: G1 GC is not enabled");
 485 WB_END
 486 
 487 WB_ENTRY(jlong, WB_G1NumFreeRegions(JNIEnv* env, jobject o))
 488   if (UseG1GC) {
 489     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 490     size_t nr = g1h->num_free_regions();
 491     return (jlong)nr;
 492   }
 493   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1NumFreeRegions: G1 GC is not enabled");
 494 WB_END
 495 
 496 WB_ENTRY(jboolean, WB_G1InConcurrentMark(JNIEnv* env, jobject o))
 497   if (UseG1GC) {
 498     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 499     return g1h->concurrent_mark()->cm_thread()->in_progress();
 500   }
 501   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1InConcurrentMark: G1 GC is not enabled");
 502 WB_END
 503 
 504 WB_ENTRY(jboolean, WB_G1StartMarkCycle(JNIEnv* env, jobject o))
 505   if (UseG1GC) {
 506     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 507     if (!g1h->concurrent_mark()->cm_thread()->in_progress()) {
 508       g1h->collect(GCCause::_wb_conc_mark);
 509       return true;
 510     }
 511     return false;
 512   }
 513   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1StartMarkCycle: G1 GC is not enabled");
 514 WB_END
 515 
 516 WB_ENTRY(jint, WB_G1RegionSize(JNIEnv* env, jobject o))
 517   if (UseG1GC) {
 518     return (jint)HeapRegion::GrainBytes;
 519   }
 520   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1RegionSize: G1 GC is not enabled");
 521 WB_END
 522 
 523 WB_ENTRY(jboolean, WB_G1HasRegionsToUncommit(JNIEnv* env, jobject o))
 524   if (UseG1GC) {
 525     return G1CollectedHeap::heap()->has_uncommittable_regions();
 526   }
 527   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1HasRegionsToUncommit: G1 GC is not enabled");
 528 WB_END
 529 
 530 #endif // INCLUDE_G1GC
 531 
 532 #if INCLUDE_PARALLELGC
 533 
 534 WB_ENTRY(jlong, WB_PSVirtualSpaceAlignment(JNIEnv* env, jobject o))
 535   if (UseParallelGC) {
 536     return GenAlignment;
 537   }
 538   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_PSVirtualSpaceAlignment: Parallel GC is not enabled");
 539 WB_END
 540 
 541 WB_ENTRY(jlong, WB_PSHeapGenerationAlignment(JNIEnv* env, jobject o))
 542   if (UseParallelGC) {
 543     return GenAlignment;
 544   }
 545   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_PSHeapGenerationAlignment: Parallel GC is not enabled");
 546 WB_END
 547 
 548 #endif // INCLUDE_PARALLELGC
 549 
 550 #if INCLUDE_G1GC
 551 
 552 WB_ENTRY(jobject, WB_G1AuxiliaryMemoryUsage(JNIEnv* env))
 553   if (UseG1GC) {
 554     ResourceMark rm(THREAD);
 555     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 556     MemoryUsage usage = g1h->get_auxiliary_data_memory_usage();
 557     Handle h = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL);
 558     return JNIHandles::make_local(THREAD, h());
 559   }
 560   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1AuxiliaryMemoryUsage: G1 GC is not enabled");
 561 WB_END
 562 
 563 WB_ENTRY(jint, WB_G1ActiveMemoryNodeCount(JNIEnv* env, jobject o))
 564   if (UseG1GC) {
 565     G1NUMA* numa = G1NUMA::numa();
 566     return (jint)numa->num_active_nodes();
 567   }
 568   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1ActiveMemoryNodeCount: G1 GC is not enabled");
 569 WB_END
 570 
 571 WB_ENTRY(jintArray, WB_G1MemoryNodeIds(JNIEnv* env, jobject o))
 572   if (UseG1GC) {
 573     G1NUMA* numa = G1NUMA::numa();
 574     int num_node_ids = (int)numa->num_active_nodes();
 575     const int* node_ids = numa->node_ids();
 576 
 577     typeArrayOop result = oopFactory::new_intArray(num_node_ids, CHECK_NULL);
 578     for (int i = 0; i < num_node_ids; i++) {
 579       result->int_at_put(i, (jint)node_ids[i]);
 580     }
 581     return (jintArray) JNIHandles::make_local(THREAD, result);
 582   }
 583   THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1MemoryNodeIds: G1 GC is not enabled");
 584 WB_END
 585 
 586 class OldRegionsLivenessClosure: public HeapRegionClosure {
 587 
 588  private:
 589   const int _liveness;
 590   size_t _total_count;
 591   size_t _total_memory;
 592   size_t _total_memory_to_free;
 593 
 594  public:
 595   OldRegionsLivenessClosure(int liveness) :
 596     _liveness(liveness),
 597     _total_count(0),
 598     _total_memory(0),
 599     _total_memory_to_free(0) { }
 600 
 601     size_t total_count() { return _total_count; }
 602     size_t total_memory() { return _total_memory; }
 603     size_t total_memory_to_free() { return _total_memory_to_free; }
 604 
 605   bool do_heap_region(HeapRegion* r) {
 606     if (r->is_old()) {
 607       size_t prev_live = r->marked_bytes();
 608       size_t live = r->live_bytes();
 609       size_t size = r->used();
 610       size_t reg_size = HeapRegion::GrainBytes;
 611       if (size > 0 && ((int)(live * 100 / size) < _liveness)) {
 612         _total_memory += size;
 613         ++_total_count;
 614         if (size == reg_size) {
 615         // we don't include non-full regions since they are unlikely included in mixed gc
 616         // for testing purposes it's enough to have lowest estimation of total memory that is expected to be freed
 617           _total_memory_to_free += size - prev_live;
 618         }
 619       }
 620     }
 621     return false;
 622   }
 623 };
 624 
 625 
 626 WB_ENTRY(jlongArray, WB_G1GetMixedGCInfo(JNIEnv* env, jobject o, jint liveness))
 627   if (!UseG1GC) {
 628     THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1GetMixedGCInfo: G1 GC is not enabled");
 629   }
 630   if (liveness < 0) {
 631     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "liveness value should be non-negative");
 632   }
 633 
 634   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 635   OldRegionsLivenessClosure rli(liveness);
 636   g1h->heap_region_iterate(&rli);
 637 
 638   typeArrayOop result = oopFactory::new_longArray(3, CHECK_NULL);
 639   result->long_at_put(0, rli.total_count());
 640   result->long_at_put(1, rli.total_memory());
 641   result->long_at_put(2, rli.total_memory_to_free());
 642   return (jlongArray) JNIHandles::make_local(THREAD, result);
 643 WB_END
 644 
 645 #endif // INCLUDE_G1GC
 646 
 647 // Alloc memory using the test memory type so that we can use that to see if
 648 // NMT picks it up correctly
 649 WB_ENTRY(jlong, WB_NMTMalloc(JNIEnv* env, jobject o, jlong size))
 650   jlong addr = 0;
 651   addr = (jlong)(uintptr_t)os::malloc(size, mtTest);
 652   return addr;
 653 WB_END
 654 
 655 // Alloc memory with pseudo call stack. The test can create psudo malloc
 656 // allocation site to stress the malloc tracking.
 657 WB_ENTRY(jlong, WB_NMTMallocWithPseudoStack(JNIEnv* env, jobject o, jlong size, jint pseudo_stack))
 658   address pc = (address)(size_t)pseudo_stack;
 659   NativeCallStack stack(&pc, 1);
 660   return (jlong)(uintptr_t)os::malloc(size, mtTest, stack);
 661 WB_END
 662 
 663 // Alloc memory with pseudo call stack and specific memory type.
 664 WB_ENTRY(jlong, WB_NMTMallocWithPseudoStackAndType(JNIEnv* env, jobject o, jlong size, jint pseudo_stack, jint type))
 665   address pc = (address)(size_t)pseudo_stack;
 666   NativeCallStack stack(&pc, 1);
 667   return (jlong)(uintptr_t)os::malloc(size, (MEMFLAGS)type, stack);
 668 WB_END
 669 
 670 // Free the memory allocated by NMTAllocTest
 671 WB_ENTRY(void, WB_NMTFree(JNIEnv* env, jobject o, jlong mem))
 672   os::free((void*)(uintptr_t)mem);
 673 WB_END
 674 
 675 WB_ENTRY(jlong, WB_NMTReserveMemory(JNIEnv* env, jobject o, jlong size))
 676   jlong addr = 0;
 677 
 678   addr = (jlong)(uintptr_t)os::reserve_memory(size);
 679   MemTracker::record_virtual_memory_type((address)addr, mtTest);
 680 
 681   return addr;
 682 WB_END
 683 
 684 WB_ENTRY(jlong, WB_NMTAttemptReserveMemoryAt(JNIEnv* env, jobject o, jlong addr, jlong size))
 685   addr = (jlong)(uintptr_t)os::attempt_reserve_memory_at((char*)(uintptr_t)addr, (size_t)size);
 686   MemTracker::record_virtual_memory_type((address)addr, mtTest);
 687 
 688   return addr;
 689 WB_END
 690 
 691 WB_ENTRY(void, WB_NMTCommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 692   os::commit_memory((char *)(uintptr_t)addr, size, !ExecMem);
 693   MemTracker::record_virtual_memory_type((address)(uintptr_t)addr, mtTest);
 694 WB_END
 695 
 696 WB_ENTRY(void, WB_NMTUncommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 697   os::uncommit_memory((char *)(uintptr_t)addr, size);
 698 WB_END
 699 
 700 WB_ENTRY(void, WB_NMTReleaseMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 701   os::release_memory((char *)(uintptr_t)addr, size);
 702 WB_END
 703 
 704 WB_ENTRY(jint, WB_NMTGetHashSize(JNIEnv* env, jobject o))
 705   int hash_size = MallocSiteTable::hash_buckets();
 706   assert(hash_size > 0, "NMT hash_size should be > 0");
 707   return (jint)hash_size;
 708 WB_END
 709 
 710 WB_ENTRY(jlong, WB_NMTNewArena(JNIEnv* env, jobject o, jlong init_size))
 711   Arena* arena =  new (mtTest) Arena(mtTest, size_t(init_size));
 712   return (jlong)arena;
 713 WB_END
 714 
 715 WB_ENTRY(void, WB_NMTFreeArena(JNIEnv* env, jobject o, jlong arena))
 716   Arena* a = (Arena*)arena;
 717   delete a;
 718 WB_END
 719 
 720 WB_ENTRY(void, WB_NMTArenaMalloc(JNIEnv* env, jobject o, jlong arena, jlong size))
 721   Arena* a = (Arena*)arena;
 722   a->Amalloc(size_t(size));
 723 WB_END
 724 
 725 static jmethodID reflected_method_to_jmid(JavaThread* thread, JNIEnv* env, jobject method) {
 726   assert(method != NULL, "method should not be null");
 727   ThreadToNativeFromVM ttn(thread);
 728   return env->FromReflectedMethod(method);
 729 }
 730 
 731 // Deoptimizes all compiled frames and makes nmethods not entrant if it's requested
 732 class VM_WhiteBoxDeoptimizeFrames : public VM_WhiteBoxOperation {
 733  private:
 734   int _result;
 735   const bool _make_not_entrant;
 736  public:
 737   VM_WhiteBoxDeoptimizeFrames(bool make_not_entrant) :
 738         _result(0), _make_not_entrant(make_not_entrant) { }
 739   int  result() const { return _result; }
 740 
 741   void doit() {
 742     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 743       if (t->has_last_Java_frame()) {
 744         for (StackFrameStream fst(t, false /* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
 745           frame* f = fst.current();
 746           if (f->can_be_deoptimized() && !f->is_deoptimized_frame()) {
 747             Deoptimization::deoptimize(t, *f);
 748             if (_make_not_entrant) {
 749                 CompiledMethod* cm = CodeCache::find_compiled(f->pc());
 750                 assert(cm != NULL, "sanity check");
 751                 cm->make_not_entrant();
 752             }
 753             ++_result;
 754           }
 755         }
 756       }
 757     }
 758   }
 759 };
 760 
 761 WB_ENTRY(jint, WB_DeoptimizeFrames(JNIEnv* env, jobject o, jboolean make_not_entrant))
 762   VM_WhiteBoxDeoptimizeFrames op(make_not_entrant == JNI_TRUE);
 763   VMThread::execute(&op);
 764   return op.result();
 765 WB_END
 766 
 767 WB_ENTRY(jboolean, WB_IsFrameDeoptimized(JNIEnv* env, jobject o, jint depth))
 768   bool result = false;
 769   if (thread->has_last_Java_frame()) {
 770     RegisterMap reg_map(thread);
 771     javaVFrame *jvf = thread->last_java_vframe(&reg_map);
 772     for (jint d = 0; d < depth && jvf != NULL; d++) {
 773       jvf = jvf->java_sender();
 774     }
 775     result = jvf != NULL && jvf->fr().is_deoptimized_frame();
 776   }
 777   return result;
 778 WB_END
 779 
 780 WB_ENTRY(void, WB_DeoptimizeAll(JNIEnv* env, jobject o))
 781   CodeCache::mark_all_nmethods_for_deoptimization();
 782   Deoptimization::deoptimize_all_marked();
 783 WB_END
 784 
 785 WB_ENTRY(jint, WB_DeoptimizeMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 786   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 787   int result = 0;
 788   CHECK_JNI_EXCEPTION_(env, result);
 789   MutexLocker mu(Compile_lock);
 790   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 791   if (is_osr) {
 792     result += mh->mark_osr_nmethods();
 793   } else if (mh->code() != NULL) {
 794     mh->code()->mark_for_deoptimization();
 795     ++result;
 796   }
 797   result += CodeCache::mark_for_deoptimization(mh());
 798   if (result > 0) {
 799     Deoptimization::deoptimize_all_marked();
 800   }
 801   return result;
 802 WB_END
 803 
 804 WB_ENTRY(jboolean, WB_IsMethodCompiled(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 805   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 806   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 807   MutexLocker mu(Compile_lock);
 808   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 809   CompiledMethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 810   if (code == NULL) {
 811     return JNI_FALSE;
 812   }
 813   return (code->is_alive() && !code->is_marked_for_deoptimization());
 814 WB_END
 815 
 816 static bool is_excluded_for_compiler(AbstractCompiler* comp, methodHandle& mh) {
 817   if (comp == NULL) {
 818     return true;
 819   }
 820   DirectiveSet* directive = DirectivesStack::getMatchingDirective(mh, comp);
 821   if (directive->ExcludeOption) {
 822     return true;
 823   }
 824   return false;
 825 }
 826 
 827 static bool can_be_compiled_at_level(methodHandle& mh, jboolean is_osr, int level) {
 828   if (is_osr) {
 829     return CompilationPolicy::can_be_osr_compiled(mh, level);
 830   } else {
 831     return CompilationPolicy::can_be_compiled(mh, level);
 832   }
 833 }
 834 
 835 WB_ENTRY(jboolean, WB_IsMethodCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 836   if (method == NULL || comp_level > CompilationPolicy::highest_compile_level()) {
 837     return false;
 838   }
 839   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 840   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 841   MutexLocker mu(Compile_lock);
 842   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 843 
 844   // The ExcludeOption directive is evaluated lazily upon compilation attempt. If a method was not tried to be compiled by
 845   // a compiler, yet, the method object is not set to be not compilable by that compiler. Thus, evaluate the compiler directive
 846   // to exclude a compilation of 'method'.
 847   if (comp_level == CompLevel_any) {
 848     // Both compilers could have ExcludeOption set. Check all combinations.
 849     bool excluded_c1 = is_excluded_for_compiler(CompileBroker::compiler1(), mh);
 850     bool excluded_c2 = is_excluded_for_compiler(CompileBroker::compiler2(), mh);
 851     if (excluded_c1 && excluded_c2) {
 852       // Compilation of 'method' excluded by both compilers.
 853       return false;
 854     }
 855 
 856     if (excluded_c1) {
 857       // C1 only has ExcludeOption set: Check if compilable with C2.
 858       return can_be_compiled_at_level(mh, is_osr, CompLevel_full_optimization);
 859     } else if (excluded_c2) {
 860       // C2 only has ExcludeOption set: Check if compilable with C1.
 861       return can_be_compiled_at_level(mh, is_osr, CompLevel_simple);
 862     }
 863   } else if (comp_level > CompLevel_none && is_excluded_for_compiler(CompileBroker::compiler((int)comp_level), mh)) {
 864     // Compilation of 'method' excluded by compiler used for 'comp_level'.
 865     return false;
 866   }
 867 
 868   return can_be_compiled_at_level(mh, is_osr, (int)comp_level);
 869 WB_END
 870 
 871 WB_ENTRY(jboolean, WB_IsMethodQueuedForCompilation(JNIEnv* env, jobject o, jobject method))
 872   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 873   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 874   MutexLocker mu(Compile_lock);
 875   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 876   return mh->queued_for_compilation();
 877 WB_END
 878 
 879 WB_ENTRY(jboolean, WB_IsIntrinsicAvailable(JNIEnv* env, jobject o, jobject method, jobject compilation_context, jint compLevel))
 880   if (compLevel < CompLevel_none || compLevel > CompilationPolicy::highest_compile_level()) {
 881     return false; // Intrinsic is not available on a non-existent compilation level.
 882   }
 883   jmethodID method_id, compilation_context_id;
 884   method_id = reflected_method_to_jmid(thread, env, method);
 885   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 886   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(method_id));
 887 
 888   DirectiveSet* directive;
 889   AbstractCompiler* comp = CompileBroker::compiler((int)compLevel);
 890   assert(comp != NULL, "compiler not available");
 891   if (compilation_context != NULL) {
 892     compilation_context_id = reflected_method_to_jmid(thread, env, compilation_context);
 893     CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 894     methodHandle cch(THREAD, Method::checked_resolve_jmethod_id(compilation_context_id));
 895     directive = DirectivesStack::getMatchingDirective(cch, comp);
 896   } else {
 897     // Calling with NULL matches default directive
 898     directive = DirectivesStack::getDefaultDirective(comp);
 899   }
 900   bool result = comp->is_intrinsic_available(mh, directive);
 901   DirectivesStack::release(directive);
 902   return result;
 903 WB_END
 904 
 905 WB_ENTRY(jint, WB_GetMethodCompilationLevel(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 906   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 907   CHECK_JNI_EXCEPTION_(env, CompLevel_none);
 908   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 909   CompiledMethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 910   return (code != NULL ? code->comp_level() : CompLevel_none);
 911 WB_END
 912 
 913 WB_ENTRY(void, WB_MakeMethodNotCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 914   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 915   CHECK_JNI_EXCEPTION(env);
 916   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 917   if (is_osr) {
 918     mh->set_not_osr_compilable("WhiteBox", comp_level);
 919   } else {
 920     mh->set_not_compilable("WhiteBox", comp_level);
 921   }
 922 WB_END
 923 
 924 WB_ENTRY(jint, WB_GetMethodDecompileCount(JNIEnv* env, jobject o, jobject method))
 925   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 926   CHECK_JNI_EXCEPTION_(env, 0);
 927   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 928   uint cnt = 0;
 929   MethodData* mdo = mh->method_data();
 930   if (mdo != NULL) {
 931     cnt = mdo->decompile_count();
 932   }
 933   return cnt;
 934 WB_END
 935 
 936 // Get the trap count of a method for a specific reason. If the trap count for
 937 // that reason did overflow, this includes the overflow trap count of the method.
 938 // If 'reason' is NULL, the sum of the traps for all reasons will be returned.
 939 // This number includes the overflow trap count if the trap count for any reason
 940 // did overflow.
 941 WB_ENTRY(jint, WB_GetMethodTrapCount(JNIEnv* env, jobject o, jobject method, jstring reason_obj))
 942   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 943   CHECK_JNI_EXCEPTION_(env, 0);
 944   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 945   uint cnt = 0;
 946   MethodData* mdo = mh->method_data();
 947   if (mdo != NULL) {
 948     ResourceMark rm(THREAD);
 949     char* reason_str = (reason_obj == NULL) ?
 950       NULL : java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(reason_obj));
 951     bool overflow = false;
 952     for (uint reason = 0; reason < mdo->trap_reason_limit(); reason++) {
 953       if (reason_str != NULL && !strcmp(reason_str, Deoptimization::trap_reason_name(reason))) {
 954         cnt = mdo->trap_count(reason);
 955         // Count in the overflow trap count on overflow
 956         if (cnt == (uint)-1) {
 957           cnt = mdo->trap_count_limit() + mdo->overflow_trap_count();
 958         }
 959         break;
 960       } else if (reason_str == NULL) {
 961         uint c = mdo->trap_count(reason);
 962         if (c == (uint)-1) {
 963           c = mdo->trap_count_limit();
 964           if (!overflow) {
 965             // Count overflow trap count just once
 966             overflow = true;
 967             c += mdo->overflow_trap_count();
 968           }
 969         }
 970         cnt += c;
 971       }
 972     }
 973   }
 974   return cnt;
 975 WB_END
 976 
 977 WB_ENTRY(jint, WB_GetDeoptCount(JNIEnv* env, jobject o, jstring reason_obj, jstring action_obj))
 978   if (reason_obj == NULL && action_obj == NULL) {
 979     return Deoptimization::total_deoptimization_count();
 980   }
 981   ResourceMark rm(THREAD);
 982   const char *reason_str = (reason_obj == NULL) ?
 983     NULL : java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(reason_obj));
 984   const char *action_str = (action_obj == NULL) ?
 985     NULL : java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(action_obj));
 986 
 987   return Deoptimization::deoptimization_count(reason_str, action_str);
 988 WB_END
 989 
 990 WB_ENTRY(jint, WB_GetMethodEntryBci(JNIEnv* env, jobject o, jobject method))
 991   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 992   CHECK_JNI_EXCEPTION_(env, InvocationEntryBci);
 993   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 994   CompiledMethod* code = mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false);
 995   return (code != NULL && code->is_osr_method() ? code->osr_entry_bci() : InvocationEntryBci);
 996 WB_END
 997 
 998 WB_ENTRY(jboolean, WB_TestSetDontInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
 999   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1000   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1001   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1002   bool result = mh->dont_inline();
1003   mh->set_dont_inline(value == JNI_TRUE);
1004   return result;
1005 WB_END
1006 
1007 WB_ENTRY(jint, WB_GetCompileQueueSize(JNIEnv* env, jobject o, jint comp_level))
1008   if (comp_level == CompLevel_any) {
1009     return CompileBroker::queue_size(CompLevel_full_optimization) /* C2 */ +
1010         CompileBroker::queue_size(CompLevel_full_profile) /* C1 */;
1011   } else {
1012     return CompileBroker::queue_size(comp_level);
1013   }
1014 WB_END
1015 
1016 WB_ENTRY(jboolean, WB_TestSetForceInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
1017   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1018   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1019   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1020   bool result = mh->force_inline();
1021   mh->set_force_inline(value == JNI_TRUE);
1022   return result;
1023 WB_END
1024 
1025 #ifdef LINUX
1026 bool WhiteBox::validate_cgroup(const char* proc_cgroups,
1027                                const char* proc_self_cgroup,
1028                                const char* proc_self_mountinfo,
1029                                u1* cg_flags) {
1030   CgroupInfo cg_infos[CG_INFO_LENGTH];
1031   return CgroupSubsystemFactory::determine_type(cg_infos, proc_cgroups,
1032                                                     proc_self_cgroup,
1033                                                     proc_self_mountinfo, cg_flags);
1034 }
1035 #endif
1036 
1037 bool WhiteBox::compile_method(Method* method, int comp_level, int bci, JavaThread* THREAD) {
1038   // Screen for unavailable/bad comp level or null method
1039   AbstractCompiler* comp = CompileBroker::compiler(comp_level);
1040   if (method == NULL) {
1041     tty->print_cr("WB error: request to compile NULL method");
1042     return false;
1043   }
1044   if (comp_level > CompilationPolicy::highest_compile_level()) {
1045     tty->print_cr("WB error: invalid compilation level %d", comp_level);
1046     return false;
1047   }
1048   if (comp == NULL) {
1049     tty->print_cr("WB error: no compiler for requested compilation level %d", comp_level);
1050     return false;
1051   }
1052 
1053   // Check if compilation is blocking
1054   methodHandle mh(THREAD, method);
1055   DirectiveSet* directive = DirectivesStack::getMatchingDirective(mh, comp);
1056   bool is_blocking = !directive->BackgroundCompilationOption;
1057   DirectivesStack::release(directive);
1058 
1059   // Compile method and check result
1060   nmethod* nm = CompileBroker::compile_method(mh, bci, comp_level, mh, mh->invocation_count(), CompileTask::Reason_Whitebox, CHECK_false);
1061   MutexLocker mu(THREAD, Compile_lock);
1062   bool is_queued = mh->queued_for_compilation();
1063   if ((!is_blocking && is_queued) || nm != NULL) {
1064     return true;
1065   }
1066   // Check code again because compilation may be finished before Compile_lock is acquired.
1067   if (bci == InvocationEntryBci) {
1068     CompiledMethod* code = mh->code();
1069     if (code != NULL && code->as_nmethod_or_null() != NULL) {
1070       return true;
1071     }
1072   } else if (mh->lookup_osr_nmethod_for(bci, comp_level, false) != NULL) {
1073     return true;
1074   }
1075   tty->print("WB error: failed to %s compile at level %d method ", is_blocking ? "blocking" : "", comp_level);
1076   mh->print_short_name(tty);
1077   tty->cr();
1078   if (is_blocking && is_queued) {
1079     tty->print_cr("WB error: blocking compilation is still in queue!");
1080   }
1081   return false;
1082 }
1083 
1084 WB_ENTRY(jboolean, WB_EnqueueMethodForCompilation(JNIEnv* env, jobject o, jobject method, jint comp_level, jint bci))
1085   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1086   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1087   return WhiteBox::compile_method(Method::checked_resolve_jmethod_id(jmid), comp_level, bci, THREAD);
1088 WB_END
1089 
1090 WB_ENTRY(jboolean, WB_EnqueueInitializerForCompilation(JNIEnv* env, jobject o, jclass klass, jint comp_level))
1091   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1092   Method* clinit = ik->class_initializer();
1093   if (clinit == NULL || clinit->method_holder()->is_not_initialized()) {
1094     return false;
1095   }
1096   return WhiteBox::compile_method(clinit, comp_level, InvocationEntryBci, THREAD);
1097 WB_END
1098 
1099 WB_ENTRY(jboolean, WB_ShouldPrintAssembly(JNIEnv* env, jobject o, jobject method, jint comp_level))
1100   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1101   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1102 
1103   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1104   DirectiveSet* directive = DirectivesStack::getMatchingDirective(mh, CompileBroker::compiler(comp_level));
1105   bool result = directive->PrintAssemblyOption;
1106   DirectivesStack::release(directive);
1107 
1108   return result;
1109 WB_END
1110 
1111 WB_ENTRY(jint, WB_MatchesInline(JNIEnv* env, jobject o, jobject method, jstring pattern))
1112   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1113   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1114 
1115   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1116 
1117   ResourceMark rm(THREAD);
1118   const char* error_msg = NULL;
1119   char* method_str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(pattern));
1120   InlineMatcher* m = InlineMatcher::parse_inline_pattern(method_str, error_msg);
1121 
1122   if (m == NULL) {
1123     assert(error_msg != NULL, "Always have an error message");
1124     tty->print_cr("Got error: %s", error_msg);
1125     return -1; // Pattern failed
1126   }
1127 
1128   // Pattern works - now check if it matches
1129   int result;
1130   if (m->match(mh, InlineMatcher::force_inline)) {
1131     result = 2; // Force inline match
1132   } else if (m->match(mh, InlineMatcher::dont_inline)) {
1133     result = 1; // Dont inline match
1134   } else {
1135     result = 0; // No match
1136   }
1137   delete m;
1138   return result;
1139 WB_END
1140 
1141 WB_ENTRY(jint, WB_MatchesMethod(JNIEnv* env, jobject o, jobject method, jstring pattern))
1142   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1143   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
1144 
1145   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1146 
1147   ResourceMark rm;
1148   char* method_str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(pattern));
1149 
1150   const char* error_msg = NULL;
1151 
1152   BasicMatcher* m = BasicMatcher::parse_method_pattern(method_str, error_msg, false);
1153   if (m == NULL) {
1154     assert(error_msg != NULL, "Must have error_msg");
1155     tty->print_cr("Got error: %s", error_msg);
1156     return -1;
1157   }
1158 
1159   // Pattern works - now check if it matches
1160   int result = m->matches(mh);
1161   delete m;
1162   assert(result == 0 || result == 1, "Result out of range");
1163   return result;
1164 WB_END
1165 
1166 WB_ENTRY(void, WB_MarkMethodProfiled(JNIEnv* env, jobject o, jobject method))
1167   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1168   CHECK_JNI_EXCEPTION(env);
1169   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1170 
1171   MethodData* mdo = mh->method_data();
1172   if (mdo == NULL) {
1173     Method::build_interpreter_method_data(mh, CHECK_AND_CLEAR);
1174     mdo = mh->method_data();
1175   }
1176   mdo->init();
1177   InvocationCounter* icnt = mdo->invocation_counter();
1178   InvocationCounter* bcnt = mdo->backedge_counter();
1179   // set i-counter according to CompilationPolicy::is_method_profiled
1180   icnt->set(Tier4MinInvocationThreshold);
1181   bcnt->set(Tier4CompileThreshold);
1182 WB_END
1183 
1184 WB_ENTRY(void, WB_ClearMethodState(JNIEnv* env, jobject o, jobject method))
1185   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1186   CHECK_JNI_EXCEPTION(env);
1187   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1188   MutexLocker mu(THREAD, Compile_lock);
1189   MethodData* mdo = mh->method_data();
1190   MethodCounters* mcs = mh->method_counters();
1191 
1192   if (mdo != NULL) {
1193     mdo->init();
1194     ResourceMark rm(THREAD);
1195     int arg_count = mdo->method()->size_of_parameters();
1196     for (int i = 0; i < arg_count; i++) {
1197       mdo->set_arg_modified(i, 0);
1198     }
1199     MutexLocker mu(THREAD, mdo->extra_data_lock());
1200     mdo->clean_method_data(/*always_clean*/true);
1201   }
1202 
1203   mh->clear_not_c1_compilable();
1204   mh->clear_not_c2_compilable();
1205   mh->clear_not_c2_osr_compilable();
1206   NOT_PRODUCT(mh->set_compiled_invocation_count(0));
1207   if (mcs != NULL) {
1208     mcs->clear_counters();
1209   }
1210 WB_END
1211 
1212 template <typename T, int type_enum>
1213 static bool GetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value) {
1214   if (name == NULL) {
1215     return false;
1216   }
1217   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1218   const char* flag_name = env->GetStringUTFChars(name, NULL);
1219   CHECK_JNI_EXCEPTION_(env, false);
1220   const JVMFlag* flag = JVMFlag::find_declared_flag(flag_name);
1221   JVMFlag::Error result = JVMFlagAccess::get<T, type_enum>(flag, value);
1222   env->ReleaseStringUTFChars(name, flag_name);
1223   return (result == JVMFlag::SUCCESS);
1224 }
1225 
1226 template <typename T, int type_enum>
1227 static bool SetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value) {
1228   if (name == NULL) {
1229     return false;
1230   }
1231   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1232   const char* flag_name = env->GetStringUTFChars(name, NULL);
1233   CHECK_JNI_EXCEPTION_(env, false);
1234   JVMFlag* flag = JVMFlag::find_flag(flag_name);
1235   JVMFlag::Error result = JVMFlagAccess::set<T, type_enum>(flag, value, JVMFlagOrigin::INTERNAL);
1236   env->ReleaseStringUTFChars(name, flag_name);
1237   return (result == JVMFlag::SUCCESS);
1238 }
1239 
1240 template <typename T>
1241 static jobject box(JavaThread* thread, JNIEnv* env, Symbol* name, Symbol* sig, T value) {
1242   ResourceMark rm(thread);
1243   jclass clazz = env->FindClass(name->as_C_string());
1244   CHECK_JNI_EXCEPTION_(env, NULL);
1245   jmethodID methodID = env->GetStaticMethodID(clazz,
1246         vmSymbols::valueOf_name()->as_C_string(),
1247         sig->as_C_string());
1248   CHECK_JNI_EXCEPTION_(env, NULL);
1249   jobject result = env->CallStaticObjectMethod(clazz, methodID, value);
1250   CHECK_JNI_EXCEPTION_(env, NULL);
1251   return result;
1252 }
1253 
1254 static jobject booleanBox(JavaThread* thread, JNIEnv* env, jboolean value) {
1255   return box(thread, env, vmSymbols::java_lang_Boolean(), vmSymbols::Boolean_valueOf_signature(), value);
1256 }
1257 static jobject integerBox(JavaThread* thread, JNIEnv* env, jint value) {
1258   return box(thread, env, vmSymbols::java_lang_Integer(), vmSymbols::Integer_valueOf_signature(), value);
1259 }
1260 static jobject longBox(JavaThread* thread, JNIEnv* env, jlong value) {
1261   return box(thread, env, vmSymbols::java_lang_Long(), vmSymbols::Long_valueOf_signature(), value);
1262 }
1263 /* static jobject floatBox(JavaThread* thread, JNIEnv* env, jfloat value) {
1264   return box(thread, env, vmSymbols::java_lang_Float(), vmSymbols::Float_valueOf_signature(), value);
1265 }*/
1266 static jobject doubleBox(JavaThread* thread, JNIEnv* env, jdouble value) {
1267   return box(thread, env, vmSymbols::java_lang_Double(), vmSymbols::Double_valueOf_signature(), value);
1268 }
1269 
1270 static const JVMFlag* getVMFlag(JavaThread* thread, JNIEnv* env, jstring name) {
1271   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1272   const char* flag_name = env->GetStringUTFChars(name, NULL);
1273   CHECK_JNI_EXCEPTION_(env, NULL);
1274   const JVMFlag* result = JVMFlag::find_declared_flag(flag_name);
1275   env->ReleaseStringUTFChars(name, flag_name);
1276   return result;
1277 }
1278 
1279 WB_ENTRY(jboolean, WB_IsConstantVMFlag(JNIEnv* env, jobject o, jstring name))
1280   const JVMFlag* flag = getVMFlag(thread, env, name);
1281   return (flag != NULL) && flag->is_constant_in_binary();
1282 WB_END
1283 
1284 WB_ENTRY(jboolean, WB_IsLockedVMFlag(JNIEnv* env, jobject o, jstring name))
1285   const JVMFlag* flag = getVMFlag(thread, env, name);
1286   return (flag != NULL) && !(flag->is_unlocked() || flag->is_unlocker());
1287 WB_END
1288 
1289 WB_ENTRY(jobject, WB_GetBooleanVMFlag(JNIEnv* env, jobject o, jstring name))
1290   bool result;
1291   if (GetVMFlag <JVM_FLAG_TYPE(bool)> (thread, env, name, &result)) {
1292     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1293     return booleanBox(thread, env, result);
1294   }
1295   return NULL;
1296 WB_END
1297 
1298 template <typename T, int type_enum>
1299 jobject GetVMFlag_longBox(JNIEnv* env, JavaThread* thread, jstring name) {
1300   T result;
1301   if (GetVMFlag <T, type_enum> (thread, env, name, &result)) {
1302     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1303     return longBox(thread, env, result);
1304   }
1305   return NULL;
1306 }
1307 
1308 WB_ENTRY(jobject, WB_GetIntVMFlag(JNIEnv* env, jobject o, jstring name))
1309   return GetVMFlag_longBox<JVM_FLAG_TYPE(int)>(env, thread, name);
1310 WB_END
1311 
1312 WB_ENTRY(jobject, WB_GetUintVMFlag(JNIEnv* env, jobject o, jstring name))
1313   return GetVMFlag_longBox<JVM_FLAG_TYPE(uint)>(env, thread, name);
1314 WB_END
1315 
1316 WB_ENTRY(jobject, WB_GetIntxVMFlag(JNIEnv* env, jobject o, jstring name))
1317   return GetVMFlag_longBox<JVM_FLAG_TYPE(intx)>(env, thread, name);
1318 WB_END
1319 
1320 WB_ENTRY(jobject, WB_GetUintxVMFlag(JNIEnv* env, jobject o, jstring name))
1321   return GetVMFlag_longBox<JVM_FLAG_TYPE(uintx)>(env, thread, name);
1322 WB_END
1323 
1324 WB_ENTRY(jobject, WB_GetUint64VMFlag(JNIEnv* env, jobject o, jstring name))
1325   return GetVMFlag_longBox<JVM_FLAG_TYPE(uint64_t)>(env, thread, name);
1326 WB_END
1327 
1328 WB_ENTRY(jobject, WB_GetSizeTVMFlag(JNIEnv* env, jobject o, jstring name))
1329   return GetVMFlag_longBox<JVM_FLAG_TYPE(size_t)>(env, thread, name);
1330 WB_END
1331 
1332 WB_ENTRY(jobject, WB_GetDoubleVMFlag(JNIEnv* env, jobject o, jstring name))
1333   double result;
1334   if (GetVMFlag <JVM_FLAG_TYPE(double)> (thread, env, name, &result)) {
1335     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1336     return doubleBox(thread, env, result);
1337   }
1338   return NULL;
1339 WB_END
1340 
1341 WB_ENTRY(jstring, WB_GetStringVMFlag(JNIEnv* env, jobject o, jstring name))
1342   ccstr ccstrResult;
1343   if (GetVMFlag <JVM_FLAG_TYPE(ccstr)> (thread, env, name, &ccstrResult)) {
1344     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1345     jstring result = env->NewStringUTF(ccstrResult);
1346     CHECK_JNI_EXCEPTION_(env, NULL);
1347     return result;
1348   }
1349   return NULL;
1350 WB_END
1351 
1352 WB_ENTRY(void, WB_SetBooleanVMFlag(JNIEnv* env, jobject o, jstring name, jboolean value))
1353   bool result = value == JNI_TRUE ? true : false;
1354   SetVMFlag <JVM_FLAG_TYPE(bool)> (thread, env, name, &result);
1355 WB_END
1356 
1357 WB_ENTRY(void, WB_SetIntVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1358   int result = value;
1359   SetVMFlag <JVM_FLAG_TYPE(int)> (thread, env, name, &result);
1360 WB_END
1361 
1362 WB_ENTRY(void, WB_SetUintVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1363   uint result = value;
1364   SetVMFlag <JVM_FLAG_TYPE(uint)> (thread, env, name, &result);
1365 WB_END
1366 
1367 WB_ENTRY(void, WB_SetIntxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1368   intx result = value;
1369   SetVMFlag <JVM_FLAG_TYPE(intx)> (thread, env, name, &result);
1370 WB_END
1371 
1372 WB_ENTRY(void, WB_SetUintxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1373   uintx result = value;
1374   SetVMFlag <JVM_FLAG_TYPE(uintx)> (thread, env, name, &result);
1375 WB_END
1376 
1377 WB_ENTRY(void, WB_SetUint64VMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1378   uint64_t result = value;
1379   SetVMFlag <JVM_FLAG_TYPE(uint64_t)> (thread, env, name, &result);
1380 WB_END
1381 
1382 WB_ENTRY(void, WB_SetSizeTVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1383   size_t result = value;
1384   SetVMFlag <JVM_FLAG_TYPE(size_t)> (thread, env, name, &result);
1385 WB_END
1386 
1387 WB_ENTRY(void, WB_SetDoubleVMFlag(JNIEnv* env, jobject o, jstring name, jdouble value))
1388   double result = value;
1389   SetVMFlag <JVM_FLAG_TYPE(double)> (thread, env, name, &result);
1390 WB_END
1391 
1392 WB_ENTRY(void, WB_SetStringVMFlag(JNIEnv* env, jobject o, jstring name, jstring value))
1393   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1394   const char* ccstrValue;
1395   if (value == NULL) {
1396     ccstrValue = NULL;
1397   }
1398   else {
1399     ccstrValue = env->GetStringUTFChars(value, NULL);
1400     CHECK_JNI_EXCEPTION(env);
1401   }
1402   {
1403     ccstr param = ccstrValue;
1404     ThreadInVMfromNative ttvfn(thread); // back to VM
1405     if (SetVMFlag <JVM_FLAG_TYPE(ccstr)> (thread, env, name, &param)) {
1406       assert(param == NULL, "old value is freed automatically and not returned");
1407     }
1408   }
1409   if (value != NULL) {
1410     env->ReleaseStringUTFChars(value, ccstrValue);
1411   }
1412 WB_END
1413 
1414 WB_ENTRY(void, WB_LockCompilation(JNIEnv* env, jobject o, jlong timeout))
1415   WhiteBox::compilation_locked = true;
1416 WB_END
1417 
1418 WB_ENTRY(void, WB_UnlockCompilation(JNIEnv* env, jobject o))
1419   MonitorLocker mo(Compilation_lock, Mutex::_no_safepoint_check_flag);
1420   WhiteBox::compilation_locked = false;
1421   mo.notify_all();
1422 WB_END
1423 
1424 WB_ENTRY(void, WB_ForceNMethodSweep(JNIEnv* env, jobject o))
1425   // Force a code cache sweep and block until it finished
1426   NMethodSweeper::force_sweep();
1427 WB_END
1428 
1429 WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString))
1430   ResourceMark rm(THREAD);
1431   int len;
1432   jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len, CHECK_false);
1433   return (StringTable::lookup(name, len) != NULL);
1434 WB_END
1435 
1436 WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o))
1437   Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(true);
1438   Universe::heap()->collect(GCCause::_wb_full_gc);
1439 #if INCLUDE_G1GC
1440   if (UseG1GC) {
1441     // Needs to be cleared explicitly for G1
1442     Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(false);
1443   }
1444 #endif // INCLUDE_G1GC
1445 WB_END
1446 
1447 WB_ENTRY(void, WB_YoungGC(JNIEnv* env, jobject o))
1448   Universe::heap()->collect(GCCause::_wb_young_gc);
1449 WB_END
1450 
1451 WB_ENTRY(void, WB_ReadReservedMemory(JNIEnv* env, jobject o))
1452   // static+volatile in order to force the read to happen
1453   // (not be eliminated by the compiler)
1454   static char c;
1455   static volatile char* p;
1456 
1457   p = os::reserve_memory(os::vm_allocation_granularity());
1458   if (p == NULL) {
1459     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Failed to reserve memory");
1460   }
1461 
1462   c = *p;
1463 WB_END
1464 
1465 WB_ENTRY(jstring, WB_GetCPUFeatures(JNIEnv* env, jobject o))
1466   const char* features = VM_Version::features_string();
1467   ThreadToNativeFromVM ttn(thread);
1468   jstring features_string = env->NewStringUTF(features);
1469 
1470   CHECK_JNI_EXCEPTION_(env, NULL);
1471 
1472   return features_string;
1473 WB_END
1474 
1475 int WhiteBox::get_blob_type(const CodeBlob* code) {
1476   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1477   return CodeCache::get_code_heap(code)->code_blob_type();
1478 }
1479 
1480 CodeHeap* WhiteBox::get_code_heap(int blob_type) {
1481   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1482   return CodeCache::get_code_heap(blob_type);
1483 }
1484 
1485 struct CodeBlobStub {
1486   CodeBlobStub(const CodeBlob* blob) :
1487       name(os::strdup(blob->name())),
1488       size(blob->size()),
1489       blob_type(WhiteBox::get_blob_type(blob)),
1490       address((jlong) blob) { }
1491   ~CodeBlobStub() { os::free((void*) name); }
1492   const char* const name;
1493   const jint        size;
1494   const jint        blob_type;
1495   const jlong       address;
1496 };
1497 
1498 static jobjectArray codeBlob2objectArray(JavaThread* thread, JNIEnv* env, CodeBlobStub* cb) {
1499   ResourceMark rm;
1500   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1501   CHECK_JNI_EXCEPTION_(env, NULL);
1502   jobjectArray result = env->NewObjectArray(4, clazz, NULL);
1503 
1504   jstring name = env->NewStringUTF(cb->name);
1505   CHECK_JNI_EXCEPTION_(env, NULL);
1506   env->SetObjectArrayElement(result, 0, name);
1507 
1508   jobject obj = integerBox(thread, env, cb->size);
1509   CHECK_JNI_EXCEPTION_(env, NULL);
1510   env->SetObjectArrayElement(result, 1, obj);
1511 
1512   obj = integerBox(thread, env, cb->blob_type);
1513   CHECK_JNI_EXCEPTION_(env, NULL);
1514   env->SetObjectArrayElement(result, 2, obj);
1515 
1516   obj = longBox(thread, env, cb->address);
1517   CHECK_JNI_EXCEPTION_(env, NULL);
1518   env->SetObjectArrayElement(result, 3, obj);
1519 
1520   return result;
1521 }
1522 
1523 WB_ENTRY(jobjectArray, WB_GetNMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
1524   ResourceMark rm(THREAD);
1525   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1526   CHECK_JNI_EXCEPTION_(env, NULL);
1527   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1528   CompiledMethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
1529   jobjectArray result = NULL;
1530   if (code == NULL) {
1531     return result;
1532   }
1533   int comp_level = code->comp_level();
1534   int insts_size = code->insts_size();
1535 
1536   ThreadToNativeFromVM ttn(thread);
1537   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1538   CHECK_JNI_EXCEPTION_(env, NULL);
1539   result = env->NewObjectArray(5, clazz, NULL);
1540   if (result == NULL) {
1541     return result;
1542   }
1543 
1544   CodeBlobStub stub(code);
1545   jobjectArray codeBlob = codeBlob2objectArray(thread, env, &stub);
1546   CHECK_JNI_EXCEPTION_(env, NULL);
1547   env->SetObjectArrayElement(result, 0, codeBlob);
1548 
1549   jobject level = integerBox(thread, env, comp_level);
1550   CHECK_JNI_EXCEPTION_(env, NULL);
1551   env->SetObjectArrayElement(result, 1, level);
1552 
1553   jbyteArray insts = env->NewByteArray(insts_size);
1554   CHECK_JNI_EXCEPTION_(env, NULL);
1555   env->SetByteArrayRegion(insts, 0, insts_size, (jbyte*) code->insts_begin());
1556   env->SetObjectArrayElement(result, 2, insts);
1557 
1558   jobject id = integerBox(thread, env, code->compile_id());
1559   CHECK_JNI_EXCEPTION_(env, NULL);
1560   env->SetObjectArrayElement(result, 3, id);
1561 
1562   jobject entry_point = longBox(thread, env, (jlong) code->entry_point());
1563   CHECK_JNI_EXCEPTION_(env, NULL);
1564   env->SetObjectArrayElement(result, 4, entry_point);
1565 
1566   return result;
1567 WB_END
1568 
1569 CodeBlob* WhiteBox::allocate_code_blob(int size, int blob_type) {
1570   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1571   BufferBlob* blob;
1572   int full_size = CodeBlob::align_code_offset(sizeof(BufferBlob));
1573   if (full_size < size) {
1574     full_size += align_up(size - full_size, oopSize);
1575   }
1576   {
1577     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1578     blob = (BufferBlob*) CodeCache::allocate(full_size, blob_type);
1579     if (blob != NULL) {
1580       ::new (blob) BufferBlob("WB::DummyBlob", full_size);
1581     }
1582   }
1583   // Track memory usage statistic after releasing CodeCache_lock
1584   MemoryService::track_code_cache_memory_usage();
1585   return blob;
1586 }
1587 
1588 WB_ENTRY(jlong, WB_AllocateCodeBlob(JNIEnv* env, jobject o, jint size, jint blob_type))
1589   if (size < 0) {
1590     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1591       err_msg("WB_AllocateCodeBlob: size is negative: " INT32_FORMAT, size));
1592   }
1593   return (jlong) WhiteBox::allocate_code_blob(size, blob_type);
1594 WB_END
1595 
1596 WB_ENTRY(void, WB_FreeCodeBlob(JNIEnv* env, jobject o, jlong addr))
1597   if (addr == 0) {
1598     return;
1599   }
1600   BufferBlob::free((BufferBlob*) addr);
1601 WB_END
1602 
1603 WB_ENTRY(jobjectArray, WB_GetCodeHeapEntries(JNIEnv* env, jobject o, jint blob_type))
1604   ResourceMark rm;
1605   GrowableArray<CodeBlobStub*> blobs;
1606   {
1607     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1608     CodeHeap* heap = WhiteBox::get_code_heap(blob_type);
1609     if (heap == NULL) {
1610       return NULL;
1611     }
1612     for (CodeBlob* cb = (CodeBlob*) heap->first();
1613          cb != NULL; cb = (CodeBlob*) heap->next(cb)) {
1614       CodeBlobStub* stub = NEW_RESOURCE_OBJ(CodeBlobStub);
1615       new (stub) CodeBlobStub(cb);
1616       blobs.append(stub);
1617     }
1618   }
1619   ThreadToNativeFromVM ttn(thread);
1620   jobjectArray result = NULL;
1621   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1622   CHECK_JNI_EXCEPTION_(env, NULL);
1623   result = env->NewObjectArray(blobs.length(), clazz, NULL);
1624   CHECK_JNI_EXCEPTION_(env, NULL);
1625   if (result == NULL) {
1626     return result;
1627   }
1628   int i = 0;
1629   for (GrowableArrayIterator<CodeBlobStub*> it = blobs.begin();
1630        it != blobs.end(); ++it) {
1631     jobjectArray obj = codeBlob2objectArray(thread, env, *it);
1632     CHECK_JNI_EXCEPTION_(env, NULL);
1633     env->SetObjectArrayElement(result, i, obj);
1634     CHECK_JNI_EXCEPTION_(env, NULL);
1635     ++i;
1636   }
1637   return result;
1638 WB_END
1639 
1640 WB_ENTRY(jint, WB_GetCompilationActivityMode(JNIEnv* env, jobject o))
1641   return CompileBroker::get_compilation_activity_mode();
1642 WB_END
1643 
1644 WB_ENTRY(jobjectArray, WB_GetCodeBlob(JNIEnv* env, jobject o, jlong addr))
1645   if (addr == 0) {
1646     THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(),
1647       "WB_GetCodeBlob: addr is null");
1648   }
1649   ThreadToNativeFromVM ttn(thread);
1650   CodeBlobStub stub((CodeBlob*) addr);
1651   return codeBlob2objectArray(thread, env, &stub);
1652 WB_END
1653 
1654 WB_ENTRY(jlong, WB_GetMethodData(JNIEnv* env, jobject wv, jobject method))
1655   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1656   CHECK_JNI_EXCEPTION_(env, 0);
1657   methodHandle mh(thread, Method::checked_resolve_jmethod_id(jmid));
1658   return (jlong) mh->method_data();
1659 WB_END
1660 
1661 WB_ENTRY(jlong, WB_GetThreadStackSize(JNIEnv* env, jobject o))
1662   return (jlong) thread->stack_size();
1663 WB_END
1664 
1665 WB_ENTRY(jlong, WB_GetThreadRemainingStackSize(JNIEnv* env, jobject o))
1666   return (jlong) thread->stack_overflow_state()->stack_available(
1667                    os::current_stack_pointer()) - (jlong)StackOverflow::stack_shadow_zone_size();
1668 WB_END
1669 
1670 
1671 int WhiteBox::array_bytes_to_length(size_t bytes) {
1672   return Array<u1>::bytes_to_length(bytes);
1673 }
1674 
1675 ///////////////
1676 // MetaspaceTestContext and MetaspaceTestArena
1677 WB_ENTRY(jlong, WB_CreateMetaspaceTestContext(JNIEnv* env, jobject wb, jlong commit_limit, jlong reserve_limit))
1678   metaspace::MetaspaceTestContext* context =
1679       new metaspace::MetaspaceTestContext("whitebox-metaspace-context", (size_t) commit_limit, (size_t) reserve_limit);
1680   return (jlong)p2i(context);
1681 WB_END
1682 
1683 WB_ENTRY(void, WB_DestroyMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1684   delete (metaspace::MetaspaceTestContext*) context;
1685 WB_END
1686 
1687 WB_ENTRY(void, WB_PurgeMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1688   metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1689   context0->purge_area();
1690 WB_END
1691 
1692 WB_ENTRY(void, WB_PrintMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1693   metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1694   context0->print_on(tty);
1695 WB_END
1696 
1697 WB_ENTRY(jlong, WB_GetTotalCommittedWordsInMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1698   metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1699   return context0->committed_words();
1700 WB_END
1701 
1702 WB_ENTRY(jlong, WB_GetTotalUsedWordsInMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1703   metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1704   return context0->used_words();
1705 WB_END
1706 
1707 WB_ENTRY(jlong, WB_CreateArenaInTestContext(JNIEnv* env, jobject wb, jlong context, jboolean is_micro))
1708   const Metaspace::MetaspaceType type = is_micro ? Metaspace::ReflectionMetaspaceType : Metaspace::StandardMetaspaceType;
1709   metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1710   return (jlong)p2i(context0->create_arena(type));
1711 WB_END
1712 
1713 WB_ENTRY(void, WB_DestroyMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena))
1714   delete (metaspace::MetaspaceTestArena*) arena;
1715 WB_END
1716 
1717 WB_ENTRY(jlong, WB_AllocateFromMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena, jlong word_size))
1718   metaspace::MetaspaceTestArena* arena0 = (metaspace::MetaspaceTestArena*) arena;
1719   MetaWord* p = arena0->allocate((size_t) word_size);
1720   return (jlong)p2i(p);
1721 WB_END
1722 
1723 WB_ENTRY(void, WB_DeallocateToMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena, jlong p, jlong word_size))
1724   metaspace::MetaspaceTestArena* arena0 = (metaspace::MetaspaceTestArena*) arena;
1725   arena0->deallocate((MetaWord*)p, (size_t) word_size);
1726 WB_END
1727 
1728 WB_ENTRY(jlong, WB_GetMaxMetaspaceAllocationSize(JNIEnv* env, jobject wb))
1729   return (jlong) Metaspace::max_allocation_word_size() * BytesPerWord;
1730 WB_END
1731 
1732 //////////////
1733 
1734 WB_ENTRY(jlong, WB_AllocateMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong size))
1735   if (size < 0) {
1736     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1737         err_msg("WB_AllocateMetaspace: size is negative: " JLONG_FORMAT, size));
1738   }
1739 
1740   oop class_loader_oop = JNIHandles::resolve(class_loader);
1741   ClassLoaderData* cld = class_loader_oop != NULL
1742       ? java_lang_ClassLoader::loader_data_acquire(class_loader_oop)
1743       : ClassLoaderData::the_null_class_loader_data();
1744 
1745   void* metadata = MetadataFactory::new_array<u1>(cld, WhiteBox::array_bytes_to_length((size_t)size), thread);
1746 
1747   return (jlong)(uintptr_t)metadata;
1748 WB_END
1749 
1750 WB_ENTRY(void, WB_DefineModule(JNIEnv* env, jobject o, jobject module, jboolean is_open,
1751                                 jstring version, jstring location, jobjectArray packages))
1752   Handle h_module (THREAD, JNIHandles::resolve(module));
1753   Modules::define_module(h_module, is_open, version, location, packages, CHECK);
1754 WB_END
1755 
1756 WB_ENTRY(void, WB_AddModuleExports(JNIEnv* env, jobject o, jobject from_module, jstring package, jobject to_module))
1757   Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1758   Handle h_to_module (THREAD, JNIHandles::resolve(to_module));
1759   Modules::add_module_exports_qualified(h_from_module, package, h_to_module, CHECK);
1760 WB_END
1761 
1762 WB_ENTRY(void, WB_AddModuleExportsToAllUnnamed(JNIEnv* env, jobject o, jclass module, jstring package))
1763   Handle h_module (THREAD, JNIHandles::resolve(module));
1764   Modules::add_module_exports_to_all_unnamed(h_module, package, CHECK);
1765 WB_END
1766 
1767 WB_ENTRY(void, WB_AddModuleExportsToAll(JNIEnv* env, jobject o, jclass module, jstring package))
1768   Handle h_module (THREAD, JNIHandles::resolve(module));
1769   Modules::add_module_exports(h_module, package, Handle(), CHECK);
1770 WB_END
1771 
1772 WB_ENTRY(void, WB_AddReadsModule(JNIEnv* env, jobject o, jobject from_module, jobject source_module))
1773   Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1774   Handle h_source_module (THREAD, JNIHandles::resolve(source_module));
1775   Modules::add_reads_module(h_from_module, h_source_module, CHECK);
1776 WB_END
1777 
1778 WB_ENTRY(jlong, WB_IncMetaspaceCapacityUntilGC(JNIEnv* env, jobject wb, jlong inc))
1779   if (inc < 0) {
1780     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1781         err_msg("WB_IncMetaspaceCapacityUntilGC: inc is negative: " JLONG_FORMAT, inc));
1782   }
1783 
1784   jlong max_size_t = (jlong) ((size_t) -1);
1785   if (inc > max_size_t) {
1786     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1787         err_msg("WB_IncMetaspaceCapacityUntilGC: inc does not fit in size_t: " JLONG_FORMAT, inc));
1788   }
1789 
1790   size_t new_cap_until_GC = 0;
1791   size_t aligned_inc = align_down((size_t) inc, Metaspace::commit_alignment());
1792   bool success = MetaspaceGC::inc_capacity_until_GC(aligned_inc, &new_cap_until_GC);
1793   if (!success) {
1794     THROW_MSG_0(vmSymbols::java_lang_IllegalStateException(),
1795                 "WB_IncMetaspaceCapacityUntilGC: could not increase capacity until GC "
1796                 "due to contention with another thread");
1797   }
1798   return (jlong) new_cap_until_GC;
1799 WB_END
1800 
1801 WB_ENTRY(jlong, WB_MetaspaceCapacityUntilGC(JNIEnv* env, jobject wb))
1802   return (jlong) MetaspaceGC::capacity_until_GC();
1803 WB_END
1804 
1805 // The function is only valid when CDS is available.
1806 WB_ENTRY(jlong, WB_MetaspaceSharedRegionAlignment(JNIEnv* env, jobject wb))
1807 #if INCLUDE_CDS
1808   return (jlong)MetaspaceShared::core_region_alignment();
1809 #else
1810   ShouldNotReachHere();
1811   return 0L;
1812 #endif
1813 WB_END
1814 
1815 WB_ENTRY(jboolean, WB_IsMonitorInflated(JNIEnv* env, jobject wb, jobject obj))
1816   oop obj_oop = JNIHandles::resolve(obj);
1817   return (jboolean) obj_oop->mark().has_monitor();
1818 WB_END
1819 
1820 WB_ENTRY(jboolean, WB_DeflateIdleMonitors(JNIEnv* env, jobject wb))
1821   log_info(monitorinflation)("WhiteBox initiated DeflateIdleMonitors");
1822   return ObjectSynchronizer::request_deflate_idle_monitors();
1823 WB_END
1824 
1825 WB_ENTRY(void, WB_ForceSafepoint(JNIEnv* env, jobject wb))
1826   VM_ForceSafepoint force_safepoint_op;
1827   VMThread::execute(&force_safepoint_op);
1828 WB_END
1829 
1830 WB_ENTRY(jlong, WB_GetConstantPool(JNIEnv* env, jobject wb, jclass klass))
1831   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1832   return (jlong) ik->constants();
1833 WB_END
1834 
1835 WB_ENTRY(jint, WB_GetConstantPoolCacheIndexTag(JNIEnv* env, jobject wb))
1836   return ConstantPool::CPCACHE_INDEX_TAG;
1837 WB_END
1838 
1839 WB_ENTRY(jint, WB_GetConstantPoolCacheLength(JNIEnv* env, jobject wb, jclass klass))
1840   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1841   ConstantPool* cp = ik->constants();
1842   if (cp->cache() == NULL) {
1843       return -1;
1844   }
1845   return cp->cache()->length();
1846 WB_END
1847 
1848 WB_ENTRY(jint, WB_ConstantPoolRemapInstructionOperandFromCache(JNIEnv* env, jobject wb, jclass klass, jint index))
1849   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1850   ConstantPool* cp = ik->constants();
1851   if (cp->cache() == NULL) {
1852     THROW_MSG_0(vmSymbols::java_lang_IllegalStateException(), "Constant pool does not have a cache");
1853   }
1854   jint cpci = index;
1855   jint cpciTag = ConstantPool::CPCACHE_INDEX_TAG;
1856   if (cpciTag > cpci || cpci >= cp->cache()->length() + cpciTag) {
1857     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool cache index is out of range");
1858   }
1859   jint cpi = cp->remap_instruction_operand_from_cache(cpci);
1860   return cpi;
1861 WB_END
1862 
1863 WB_ENTRY(jint, WB_ConstantPoolEncodeIndyIndex(JNIEnv* env, jobject wb, jint index))
1864   return ConstantPool::encode_invokedynamic_index(index);
1865 WB_END
1866 
1867 WB_ENTRY(void, WB_ClearInlineCaches(JNIEnv* env, jobject wb, jboolean preserve_static_stubs))
1868   VM_ClearICs clear_ics(preserve_static_stubs == JNI_TRUE);
1869   VMThread::execute(&clear_ics);
1870 WB_END
1871 
1872 template <typename T>
1873 static bool GetMethodOption(JavaThread* thread, JNIEnv* env, jobject method, jstring name, T* value) {
1874   assert(value != NULL, "sanity");
1875   if (method == NULL || name == NULL) {
1876     return false;
1877   }
1878   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1879   CHECK_JNI_EXCEPTION_(env, false);
1880   methodHandle mh(thread, Method::checked_resolve_jmethod_id(jmid));
1881   // can't be in VM when we call JNI
1882   ThreadToNativeFromVM ttnfv(thread);
1883   const char* flag_name = env->GetStringUTFChars(name, NULL);
1884   CHECK_JNI_EXCEPTION_(env, false);
1885   enum CompileCommand option = CompilerOracle::string_to_option(flag_name);
1886   env->ReleaseStringUTFChars(name, flag_name);
1887   if (option == CompileCommand::Unknown) {
1888     return false;
1889   }
1890   if (!CompilerOracle::option_matches_type(option, *value)) {
1891     return false;
1892   }
1893   return CompilerOracle::has_option_value(mh, option, *value);
1894 }
1895 
1896 WB_ENTRY(jobject, WB_GetMethodBooleaneOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1897   bool result;
1898   if (GetMethodOption<bool> (thread, env, method, name, &result)) {
1899     // can't be in VM when we call JNI
1900     ThreadToNativeFromVM ttnfv(thread);
1901     return booleanBox(thread, env, result);
1902   }
1903   return NULL;
1904 WB_END
1905 
1906 WB_ENTRY(jobject, WB_GetMethodIntxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1907   intx result;
1908   if (GetMethodOption <intx> (thread, env, method, name, &result)) {
1909     // can't be in VM when we call JNI
1910     ThreadToNativeFromVM ttnfv(thread);
1911     return longBox(thread, env, result);
1912   }
1913   return NULL;
1914 WB_END
1915 
1916 WB_ENTRY(jobject, WB_GetMethodUintxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1917   uintx result;
1918   if (GetMethodOption <uintx> (thread, env, method, name, &result)) {
1919     // can't be in VM when we call JNI
1920     ThreadToNativeFromVM ttnfv(thread);
1921     return longBox(thread, env, result);
1922   }
1923   return NULL;
1924 WB_END
1925 
1926 WB_ENTRY(jobject, WB_GetMethodDoubleOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1927   double result;
1928   if (GetMethodOption <double> (thread, env, method, name, &result)) {
1929     // can't be in VM when we call JNI
1930     ThreadToNativeFromVM ttnfv(thread);
1931     return doubleBox(thread, env, result);
1932   }
1933   return NULL;
1934 WB_END
1935 
1936 WB_ENTRY(jobject, WB_GetMethodStringOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1937   ccstr ccstrResult;
1938   if (GetMethodOption <ccstr> (thread, env, method, name, &ccstrResult)) {
1939     // can't be in VM when we call JNI
1940     ThreadToNativeFromVM ttnfv(thread);
1941     jstring result = env->NewStringUTF(ccstrResult);
1942     CHECK_JNI_EXCEPTION_(env, NULL);
1943     return result;
1944   }
1945   return NULL;
1946 WB_END
1947 
1948 WB_ENTRY(jobject, WB_GetDefaultArchivePath(JNIEnv* env, jobject wb))
1949   const char* p = Arguments::get_default_shared_archive_path();
1950   ThreadToNativeFromVM ttn(thread);
1951   jstring path_string = env->NewStringUTF(p);
1952 
1953   CHECK_JNI_EXCEPTION_(env, NULL);
1954 
1955   return path_string;
1956 WB_END
1957 
1958 WB_ENTRY(jboolean, WB_IsSharingEnabled(JNIEnv* env, jobject wb))
1959   return UseSharedSpaces;
1960 WB_END
1961 
1962 WB_ENTRY(jint, WB_GetCDSGenericHeaderMinVersion(JNIEnv* env, jobject wb))
1963 #if INCLUDE_CDS
1964   return (jint)CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION;
1965 #else
1966   ShouldNotReachHere();
1967   return (jint)-1;
1968 #endif
1969 WB_END
1970 
1971 WB_ENTRY(jint, WB_GetCDSCurrentVersion(JNIEnv* env, jobject wb))
1972 #if INCLUDE_CDS
1973   return (jint)CURRENT_CDS_ARCHIVE_VERSION;
1974 #else
1975   ShouldNotReachHere();
1976   return (jint)-1;
1977 #endif
1978 WB_END
1979 
1980 WB_ENTRY(jboolean, WB_CDSMemoryMappingFailed(JNIEnv* env, jobject wb))
1981   return FileMapInfo::memory_mapping_failed();
1982 WB_END
1983 
1984 WB_ENTRY(jboolean, WB_IsShared(JNIEnv* env, jobject wb, jobject obj))
1985   oop obj_oop = JNIHandles::resolve(obj);
1986   return Universe::heap()->is_archived_object(obj_oop);
1987 WB_END
1988 
1989 WB_ENTRY(jboolean, WB_IsSharedInternedString(JNIEnv* env, jobject wb, jobject str))
1990   ResourceMark rm(THREAD);
1991   oop str_oop = JNIHandles::resolve(str);
1992   int length;
1993   jchar* chars = java_lang_String::as_unicode_string(str_oop, length, CHECK_(false));
1994   return StringTable::lookup_shared(chars, length) == str_oop;
1995 WB_END
1996 
1997 WB_ENTRY(jboolean, WB_IsSharedClass(JNIEnv* env, jobject wb, jclass clazz))
1998   return (jboolean)MetaspaceShared::is_in_shared_metaspace(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
1999 WB_END
2000 
2001 WB_ENTRY(jboolean, WB_AreSharedStringsMapped(JNIEnv* env))
2002   return HeapShared::closed_regions_mapped();
2003 WB_END
2004 
2005 WB_ENTRY(jobject, WB_GetResolvedReferences(JNIEnv* env, jobject wb, jclass clazz))
2006   Klass *k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2007   if (k->is_instance_klass()) {
2008     InstanceKlass *ik = InstanceKlass::cast(k);
2009     ConstantPool *cp = ik->constants();
2010     objArrayOop refs =  cp->resolved_references();
2011     return (jobject)JNIHandles::make_local(THREAD, refs);
2012   } else {
2013     return NULL;
2014   }
2015 WB_END
2016 
2017 WB_ENTRY(void, WB_LinkClass(JNIEnv* env, jobject wb, jclass clazz))
2018   Klass *k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2019   if (!k->is_instance_klass()) {
2020     return;
2021   }
2022   InstanceKlass *ik = InstanceKlass::cast(k);
2023   ik->link_class(THREAD); // may throw verification error
2024 WB_END
2025 
2026 WB_ENTRY(jboolean, WB_AreOpenArchiveHeapObjectsMapped(JNIEnv* env))
2027   return HeapShared::open_regions_mapped();
2028 WB_END
2029 
2030 WB_ENTRY(jboolean, WB_IsCDSIncluded(JNIEnv* env))
2031 #if INCLUDE_CDS
2032   return true;
2033 #else
2034   return false;
2035 #endif // INCLUDE_CDS
2036 WB_END
2037 
2038 WB_ENTRY(jboolean, WB_isC2OrJVMCIIncluded(JNIEnv* env))
2039 #if COMPILER2_OR_JVMCI
2040   return true;
2041 #else
2042   return false;
2043 #endif
2044 WB_END
2045 
2046 WB_ENTRY(jboolean, WB_IsJVMCISupportedByGC(JNIEnv* env))
2047 #if INCLUDE_JVMCI
2048   return JVMCIGlobals::gc_supports_jvmci();
2049 #else
2050   return false;
2051 #endif
2052 WB_END
2053 
2054 WB_ENTRY(jboolean, WB_CanWriteJavaHeapArchive(JNIEnv* env))
2055   return HeapShared::can_write();
2056 WB_END
2057 
2058 
2059 WB_ENTRY(jboolean, WB_IsJFRIncluded(JNIEnv* env))
2060 #if INCLUDE_JFR
2061   return true;
2062 #else
2063   return false;
2064 #endif // INCLUDE_JFR
2065 WB_END
2066 
2067 WB_ENTRY(jboolean, WB_IsDTraceIncluded(JNIEnv* env))
2068 #if defined(DTRACE_ENABLED)
2069   return true;
2070 #else
2071   return false;
2072 #endif // DTRACE_ENABLED
2073 WB_END
2074 
2075 #if INCLUDE_CDS
2076 
2077 WB_ENTRY(jint, WB_GetCDSOffsetForName(JNIEnv* env, jobject o, jstring name))
2078   ResourceMark rm;
2079   char* c_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
2080   jint result = (jint)CDSConstants::get_cds_offset(c_name);
2081   return result;
2082 WB_END
2083 
2084 WB_ENTRY(jint, WB_GetCDSConstantForName(JNIEnv* env, jobject o, jstring name))
2085   ResourceMark rm;
2086   char* c_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
2087   jint result = (jint)CDSConstants::get_cds_constant(c_name);
2088   return result;
2089 WB_END
2090 
2091 #endif // INCLUDE_CDS
2092 
2093 WB_ENTRY(jboolean, WB_HandshakeReadMonitors(JNIEnv* env, jobject wb, jobject thread_handle))
2094   class ReadMonitorsClosure : public HandshakeClosure {
2095     jboolean _executed;
2096 
2097     void do_thread(Thread* th) {
2098       JavaThread* jt = JavaThread::cast(th);
2099       ResourceMark rm;
2100 
2101       GrowableArray<MonitorInfo*>* info = new GrowableArray<MonitorInfo*>();
2102 
2103       if (!jt->has_last_Java_frame()) {
2104         return;
2105       }
2106       RegisterMap rmap(jt);
2107       for (javaVFrame* vf = jt->last_java_vframe(&rmap); vf != NULL; vf = vf->java_sender()) {
2108         GrowableArray<MonitorInfo*> *monitors = vf->monitors();
2109         if (monitors != NULL) {
2110           int len = monitors->length();
2111           // Walk monitors youngest to oldest
2112           for (int i = len - 1; i >= 0; i--) {
2113             MonitorInfo* mon_info = monitors->at(i);
2114             if (mon_info->eliminated()) continue;
2115             oop owner = mon_info->owner();
2116             if (owner != NULL) {
2117               info->append(mon_info);
2118             }
2119           }
2120         }
2121       }
2122       _executed = true;
2123     }
2124 
2125    public:
2126     ReadMonitorsClosure() : HandshakeClosure("WB_HandshakeReadMonitors"), _executed(false) {}
2127     jboolean executed() const { return _executed; }
2128   };
2129 
2130   ReadMonitorsClosure rmc;
2131   if (thread_handle != NULL) {
2132     ThreadsListHandle tlh;
2133     JavaThread* target = nullptr;
2134     bool is_alive = tlh.cv_internal_thread_to_JavaThread(thread_handle, &target, NULL);
2135     if (is_alive) {
2136       Handshake::execute(&rmc, &tlh, target);
2137     }
2138   }
2139   return rmc.executed();
2140 WB_END
2141 
2142 WB_ENTRY(jint, WB_HandshakeWalkStack(JNIEnv* env, jobject wb, jobject thread_handle, jboolean all_threads))
2143   class TraceSelfClosure : public HandshakeClosure {
2144     jint _num_threads_completed;
2145 
2146     void do_thread(Thread* th) {
2147       JavaThread* jt = JavaThread::cast(th);
2148       ResourceMark rm;
2149 
2150       jt->print_on(tty);
2151       jt->print_stack_on(tty);
2152       tty->cr();
2153       Atomic::inc(&_num_threads_completed);
2154     }
2155 
2156   public:
2157     TraceSelfClosure(Thread* thread) : HandshakeClosure("WB_TraceSelf"), _num_threads_completed(0) {}
2158 
2159     jint num_threads_completed() const { return _num_threads_completed; }
2160   };
2161   TraceSelfClosure tsc(Thread::current());
2162 
2163   if (all_threads) {
2164     Handshake::execute(&tsc);
2165   } else if (thread_handle != NULL) {
2166     ThreadsListHandle tlh;
2167     JavaThread* target = nullptr;
2168     bool is_alive = tlh.cv_internal_thread_to_JavaThread(thread_handle, &target, NULL);
2169     if (is_alive) {
2170       Handshake::execute(&tsc, &tlh, target);
2171     }
2172   }
2173   return tsc.num_threads_completed();
2174 WB_END
2175 
2176 WB_ENTRY(void, WB_AsyncHandshakeWalkStack(JNIEnv* env, jobject wb, jobject thread_handle))
2177   class TraceSelfClosure : public AsyncHandshakeClosure {
2178     JavaThread* _self;
2179     void do_thread(Thread* th) {
2180       assert(th->is_Java_thread(), "sanity");
2181       // AsynchHandshake handshakes are only executed by target.
2182       assert(_self == th, "Must be");
2183       assert(Thread::current() == th, "Must be");
2184       JavaThread* jt = JavaThread::cast(th);
2185       ResourceMark rm;
2186       jt->print_on(tty);
2187       jt->print_stack_on(tty);
2188       tty->cr();
2189     }
2190 
2191   public:
2192     TraceSelfClosure(JavaThread* self_target) : AsyncHandshakeClosure("WB_TraceSelf"), _self(self_target) {}
2193   };
2194   if (thread_handle != NULL) {
2195     ThreadsListHandle tlh;
2196     JavaThread* target = nullptr;
2197     bool is_alive = tlh.cv_internal_thread_to_JavaThread(thread_handle, &target, NULL);
2198     if (is_alive) {
2199       TraceSelfClosure* tsc = new TraceSelfClosure(target);
2200       Handshake::execute(tsc, target);
2201     }
2202   }
2203 WB_END
2204 
2205 static volatile int _emulated_lock = 0;
2206 
2207 WB_ENTRY(void, WB_LockAndBlock(JNIEnv* env, jobject wb, jboolean suspender))
2208   JavaThread* self = JavaThread::current();
2209 
2210   {
2211     // Before trying to acquire the lock transition into a safepoint safe state.
2212     // Otherwise if either suspender or suspendee blocks for a safepoint
2213     // in ~ThreadBlockInVM the other one could loop forever trying to acquire
2214     // the lock without allowing the safepoint to progress.
2215     ThreadBlockInVM tbivm(self);
2216 
2217     // We will deadlock here if we are 'suspender' and 'suspendee'
2218     // suspended in ~ThreadBlockInVM. This verifies we only suspend
2219     // at the right place.
2220     while (Atomic::cmpxchg(&_emulated_lock, 0, 1) != 0) {}
2221     assert(_emulated_lock == 1, "Must be locked");
2222 
2223     // Sleep much longer in suspendee to force situation where
2224     // 'suspender' is waiting above to acquire lock.
2225     os::naked_short_sleep(suspender ? 1 : 10);
2226   }
2227   Atomic::store(&_emulated_lock, 0);
2228 WB_END
2229 
2230 // Some convenience methods to deal with objects from java
2231 int WhiteBox::offset_for_field(const char* field_name, oop object,
2232     Symbol* signature_symbol) {
2233   assert(field_name != NULL && strlen(field_name) > 0, "Field name not valid");
2234 
2235   //Get the class of our object
2236   Klass* arg_klass = object->klass();
2237   //Turn it into an instance-klass
2238   InstanceKlass* ik = InstanceKlass::cast(arg_klass);
2239 
2240   //Create symbols to look for in the class
2241   TempNewSymbol name_symbol = SymbolTable::new_symbol(field_name);
2242 
2243   //To be filled in with an offset of the field we're looking for
2244   fieldDescriptor fd;
2245 
2246   Klass* res = ik->find_field(name_symbol, signature_symbol, &fd);
2247   if (res == NULL) {
2248     tty->print_cr("Invalid layout of %s at %s", ik->external_name(),
2249         name_symbol->as_C_string());
2250     vm_exit_during_initialization("Invalid layout of preloaded class: use -Xlog:class+load=info to see the origin of the problem class");
2251   }
2252 
2253   //fetch the field at the offset we've found
2254   int dest_offset = fd.offset();
2255 
2256   return dest_offset;
2257 }
2258 
2259 
2260 const char* WhiteBox::lookup_jstring(const char* field_name, oop object) {
2261   int offset = offset_for_field(field_name, object,
2262       vmSymbols::string_signature());
2263   oop string = object->obj_field(offset);
2264   if (string == NULL) {
2265     return NULL;
2266   }
2267   const char* ret = java_lang_String::as_utf8_string(string);
2268   return ret;
2269 }
2270 
2271 bool WhiteBox::lookup_bool(const char* field_name, oop object) {
2272   int offset =
2273       offset_for_field(field_name, object, vmSymbols::bool_signature());
2274   bool ret = (object->bool_field(offset) == JNI_TRUE);
2275   return ret;
2276 }
2277 
2278 void WhiteBox::register_methods(JNIEnv* env, jclass wbclass, JavaThread* thread, JNINativeMethod* method_array, int method_count) {
2279   ResourceMark rm;
2280   Klass* klass = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(wbclass));
2281   const char* klass_name = klass->external_name();
2282 
2283   ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
2284 
2285   //  one by one registration natives for exception catching
2286   jclass no_such_method_error_klass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string());
2287   CHECK_JNI_EXCEPTION(env);
2288   for (int i = 0, n = method_count; i < n; ++i) {
2289     // Skip dummy entries
2290     if (method_array[i].fnPtr == NULL) continue;
2291     if (env->RegisterNatives(wbclass, &method_array[i], 1) != 0) {
2292       jthrowable throwable_obj = env->ExceptionOccurred();
2293       if (throwable_obj != NULL) {
2294         env->ExceptionClear();
2295         if (env->IsInstanceOf(throwable_obj, no_such_method_error_klass)) {
2296           // NoSuchMethodError is thrown when a method can't be found or a method is not native.
2297           // Ignoring the exception since it is not preventing use of other WhiteBox methods.
2298           tty->print_cr("Warning: 'NoSuchMethodError' on register of %s::%s%s",
2299               klass_name, method_array[i].name, method_array[i].signature);
2300         }
2301       } else {
2302         // Registration failed unexpectedly.
2303         tty->print_cr("Warning: unexpected error on register of %s::%s%s. All methods will be unregistered",
2304             klass_name, method_array[i].name, method_array[i].signature);
2305         env->UnregisterNatives(wbclass);
2306         break;
2307       }
2308     }
2309   }
2310 }
2311 
2312 WB_ENTRY(jint, WB_AddCompilerDirective(JNIEnv* env, jobject o, jstring compDirect))
2313   // can't be in VM when we call JNI
2314   ThreadToNativeFromVM ttnfv(thread);
2315   const char* dir = env->GetStringUTFChars(compDirect, NULL);
2316   CHECK_JNI_EXCEPTION_(env, 0);
2317   int ret;
2318   {
2319     ThreadInVMfromNative ttvfn(thread); // back to VM
2320     ret = DirectivesParser::parse_string(dir, tty);
2321   }
2322   env->ReleaseStringUTFChars(compDirect, dir);
2323   // -1 for error parsing directive. Return 0 as number of directives added.
2324   if (ret == -1) {
2325     ret = 0;
2326   }
2327   return (jint) ret;
2328 WB_END
2329 
2330 WB_ENTRY(void, WB_RemoveCompilerDirective(JNIEnv* env, jobject o, jint count))
2331   DirectivesStack::pop(count);
2332 WB_END
2333 
2334 // Checks that the library libfile has the noexecstack bit set.
2335 WB_ENTRY(jboolean, WB_CheckLibSpecifiesNoexecstack(JNIEnv* env, jobject o, jstring libfile))
2336   jboolean ret = false;
2337 #ifdef LINUX
2338   // Can't be in VM when we call JNI.
2339   ThreadToNativeFromVM ttnfv(thread);
2340   const char* lf = env->GetStringUTFChars(libfile, NULL);
2341   CHECK_JNI_EXCEPTION_(env, 0);
2342   ret = (jboolean) ElfFile::specifies_noexecstack(lf);
2343   env->ReleaseStringUTFChars(libfile, lf);
2344 #endif
2345   return ret;
2346 WB_END
2347 
2348 WB_ENTRY(jboolean, WB_IsContainerized(JNIEnv* env, jobject o))
2349   LINUX_ONLY(return OSContainer::is_containerized();)
2350   return false;
2351 WB_END
2352 
2353 WB_ENTRY(jint, WB_ValidateCgroup(JNIEnv* env,
2354                                     jobject o,
2355                                     jstring proc_cgroups,
2356                                     jstring proc_self_cgroup,
2357                                     jstring proc_self_mountinfo))
2358   jint ret = 0;
2359 #ifdef LINUX
2360   ThreadToNativeFromVM ttnfv(thread);
2361   const char* p_cgroups = env->GetStringUTFChars(proc_cgroups, NULL);
2362   CHECK_JNI_EXCEPTION_(env, 0);
2363   const char* p_s_cgroup = env->GetStringUTFChars(proc_self_cgroup, NULL);
2364   CHECK_JNI_EXCEPTION_(env, 0);
2365   const char* p_s_mountinfo = env->GetStringUTFChars(proc_self_mountinfo, NULL);
2366   CHECK_JNI_EXCEPTION_(env, 0);
2367   u1 cg_type_flags = 0;
2368   // This sets cg_type_flags
2369   WhiteBox::validate_cgroup(p_cgroups, p_s_cgroup, p_s_mountinfo, &cg_type_flags);
2370   ret = (jint)cg_type_flags;
2371   env->ReleaseStringUTFChars(proc_cgroups, p_cgroups);
2372   env->ReleaseStringUTFChars(proc_self_cgroup, p_s_cgroup);
2373   env->ReleaseStringUTFChars(proc_self_mountinfo, p_s_mountinfo);
2374 #endif
2375   return ret;
2376 WB_END
2377 
2378 WB_ENTRY(void, WB_PrintOsInfo(JNIEnv* env, jobject o))
2379   os::print_os_info(tty);
2380 WB_END
2381 
2382 // Elf decoder
2383 WB_ENTRY(void, WB_DisableElfSectionCache(JNIEnv* env))
2384 #if !defined(_WINDOWS) && !defined(__APPLE__) && !defined(_AIX)
2385   ElfFile::_do_not_cache_elf_section = true;
2386 #endif
2387 WB_END
2388 
2389 WB_ENTRY(jlong, WB_ResolvedMethodItemsCount(JNIEnv* env, jobject o))
2390   return (jlong) ResolvedMethodTable::items_count();
2391 WB_END
2392 
2393 WB_ENTRY(jint, WB_ProtectionDomainRemovedCount(JNIEnv* env, jobject o))
2394   return (jint) SystemDictionary::pd_cache_table()->removed_entries_count();
2395 WB_END
2396 
2397 WB_ENTRY(jint, WB_GetKlassMetadataSize(JNIEnv* env, jobject wb, jclass mirror))
2398   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(mirror));
2399   // Return size in bytes.
2400   return k->size() * wordSize;
2401 WB_END
2402 
2403 // See test/hotspot/jtreg/runtime/Thread/ThreadObjAccessAtExit.java.
2404 // It explains how the thread's priority field is used for test state coordination.
2405 //
2406 WB_ENTRY(void, WB_CheckThreadObjOfTerminatingThread(JNIEnv* env, jobject wb, jobject target_handle))
2407   oop target_oop = JNIHandles::resolve_non_null(target_handle);
2408   jlong tid = java_lang_Thread::thread_id(target_oop);
2409   JavaThread* target = java_lang_Thread::thread(target_oop);
2410 
2411   // Grab a ThreadsListHandle to protect the target thread whilst terminating
2412   ThreadsListHandle tlh;
2413 
2414   // Look up the target thread by tid to ensure it is present
2415   JavaThread* t = tlh.list()->find_JavaThread_from_java_tid(tid);
2416   if (t == NULL) {
2417     THROW_MSG(vmSymbols::java_lang_RuntimeException(), "Target thread not found in ThreadsList!");
2418   }
2419 
2420   tty->print_cr("WB_CheckThreadObjOfTerminatingThread: target thread is protected");
2421   // Allow target to terminate by boosting priority
2422   java_lang_Thread::set_priority(t->threadObj(), ThreadPriority(NormPriority + 1));
2423 
2424   // Now wait for the target to terminate
2425   while (!target->is_terminated()) {
2426     ThreadBlockInVM tbivm(thread);  // just in case target is involved in a safepoint
2427     os::naked_short_sleep(0);
2428   }
2429 
2430   tty->print_cr("WB_CheckThreadObjOfTerminatingThread: target thread is terminated");
2431 
2432   // Now release the GC inducing thread - we have to re-resolve the external oop that
2433   // was passed in as GC may have occurred and we don't know if we can trust t->threadObj() now.
2434   oop original = JNIHandles::resolve_non_null(target_handle);
2435   java_lang_Thread::set_priority(original, ThreadPriority(NormPriority + 2));
2436 
2437   tty->print_cr("WB_CheckThreadObjOfTerminatingThread: GC has been initiated - checking threadObj:");
2438 
2439   // The Java code should be creating garbage and triggering GC, which would potentially move
2440   // the threadObj oop. If the exiting thread is properly protected then its threadObj should
2441   // remain valid and equal to our initial target_handle. Loop a few times to give GC a chance to
2442   // kick in.
2443   for (int i = 0; i < 5; i++) {
2444     oop original = JNIHandles::resolve_non_null(target_handle);
2445     oop current = t->threadObj();
2446     if (original != current) {
2447       tty->print_cr("WB_CheckThreadObjOfTerminatingThread: failed comparison on iteration %d", i);
2448       THROW_MSG(vmSymbols::java_lang_RuntimeException(), "Target thread oop has changed!");
2449     } else {
2450       tty->print_cr("WB_CheckThreadObjOfTerminatingThread: successful comparison on iteration %d", i);
2451       ThreadBlockInVM tbivm(thread);
2452       os::naked_short_sleep(50);
2453     }
2454   }
2455 WB_END
2456 
2457 WB_ENTRY(void, WB_VerifyFrames(JNIEnv* env, jobject wb, jboolean log, jboolean update_map))
2458   ResourceMark rm; // for verify
2459   stringStream st;
2460   for (StackFrameStream fst(JavaThread::current(), update_map, true); !fst.is_done(); fst.next()) {
2461     frame* current_frame = fst.current();
2462     if (log) {
2463       current_frame->print_value_on(&st, NULL);
2464     }
2465     current_frame->verify(fst.register_map());
2466   }
2467   if (log) {
2468     tty->print_cr("[WhiteBox::VerifyFrames] Walking Frames");
2469     tty->print_raw(st.as_string());
2470     tty->print_cr("[WhiteBox::VerifyFrames] Done");
2471   }
2472 WB_END
2473 
2474 WB_ENTRY(jboolean, WB_IsJVMTIIncluded(JNIEnv* env, jobject wb))
2475 #if INCLUDE_JVMTI
2476   return JNI_TRUE;
2477 #else
2478   return JNI_FALSE;
2479 #endif
2480 WB_END
2481 
2482 WB_ENTRY(void, WB_WaitUnsafe(JNIEnv* env, jobject wb, jint time))
2483     os::naked_short_sleep(time);
2484 WB_END
2485 
2486 WB_ENTRY(jstring, WB_GetLibcName(JNIEnv* env, jobject o))
2487   ThreadToNativeFromVM ttn(thread);
2488   jstring info_string = env->NewStringUTF(XSTR(LIBC));
2489   CHECK_JNI_EXCEPTION_(env, NULL);
2490   return info_string;
2491 WB_END
2492 
2493 WB_ENTRY(void, WB_LockCritical(JNIEnv* env, jobject wb))
2494   GCLocker::lock_critical(thread);
2495 WB_END
2496 
2497 WB_ENTRY(void, WB_UnlockCritical(JNIEnv* env, jobject wb))
2498   GCLocker::unlock_critical(thread);
2499 WB_END
2500 
2501 #define CC (char*)
2502 
2503 static JNINativeMethod methods[] = {
2504   {CC"getObjectAddress0",                CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress  },
2505   {CC"getObjectSize0",                   CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectSize     },
2506   {CC"isObjectInOldGen0",                CC"(Ljava/lang/Object;)Z", (void*)&WB_isObjectInOldGen  },
2507   {CC"getHeapOopSize",                   CC"()I",                   (void*)&WB_GetHeapOopSize    },
2508   {CC"getVMPageSize",                    CC"()I",                   (void*)&WB_GetVMPageSize     },
2509   {CC"getVMAllocationGranularity",       CC"()J",                   (void*)&WB_GetVMAllocationGranularity },
2510   {CC"getVMLargePageSize",               CC"()J",                   (void*)&WB_GetVMLargePageSize},
2511   {CC"getHeapSpaceAlignment",            CC"()J",                   (void*)&WB_GetHeapSpaceAlignment},
2512   {CC"getHeapAlignment",                 CC"()J",                   (void*)&WB_GetHeapAlignment},
2513   {CC"countAliveClasses0",               CC"(Ljava/lang/String;)I", (void*)&WB_CountAliveClasses },
2514   {CC"getSymbolRefcount",                CC"(Ljava/lang/String;)I", (void*)&WB_GetSymbolRefcount },
2515   {CC"parseCommandLine0",
2516       CC"(Ljava/lang/String;C[Ljdk/test/whitebox/parser/DiagnosticCommand;)[Ljava/lang/Object;",
2517       (void*) &WB_ParseCommandLine
2518   },
2519   {CC"addToBootstrapClassLoaderSearch0", CC"(Ljava/lang/String;)V",
2520                                                       (void*)&WB_AddToBootstrapClassLoaderSearch},
2521   {CC"addToSystemClassLoaderSearch0",    CC"(Ljava/lang/String;)V",
2522                                                       (void*)&WB_AddToSystemClassLoaderSearch},
2523   {CC"getCompressedOopsMaxHeapSize", CC"()J",
2524       (void*)&WB_GetCompressedOopsMaxHeapSize},
2525   {CC"printHeapSizes",     CC"()V",                   (void*)&WB_PrintHeapSizes    },
2526   {CC"readFromNoaccessArea",CC"()V",                  (void*)&WB_ReadFromNoaccessArea},
2527   {CC"stressVirtualSpaceResize",CC"(JJJ)I",           (void*)&WB_StressVirtualSpaceResize},
2528 #if INCLUDE_CDS
2529   {CC"getCDSOffsetForName0", CC"(Ljava/lang/String;)I",  (void*)&WB_GetCDSOffsetForName},
2530   {CC"getCDSConstantForName0", CC"(Ljava/lang/String;)I",  (void*)&WB_GetCDSConstantForName},
2531 #endif
2532 #if INCLUDE_G1GC
2533   {CC"g1InConcurrentMark", CC"()Z",                   (void*)&WB_G1InConcurrentMark},
2534   {CC"g1IsHumongous0",      CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous     },
2535   {CC"g1BelongsToHumongousRegion0", CC"(J)Z",         (void*)&WB_G1BelongsToHumongousRegion},
2536   {CC"g1BelongsToFreeRegion0", CC"(J)Z",              (void*)&WB_G1BelongsToFreeRegion},
2537   {CC"g1NumMaxRegions",    CC"()J",                   (void*)&WB_G1NumMaxRegions  },
2538   {CC"g1NumFreeRegions",   CC"()J",                   (void*)&WB_G1NumFreeRegions  },
2539   {CC"g1RegionSize",       CC"()I",                   (void*)&WB_G1RegionSize      },
2540   {CC"g1StartConcMarkCycle",       CC"()Z",           (void*)&WB_G1StartMarkCycle  },
2541   {CC"g1HasRegionsToUncommit",  CC"()Z",              (void*)&WB_G1HasRegionsToUncommit},
2542   {CC"g1AuxiliaryMemoryUsage", CC"()Ljava/lang/management/MemoryUsage;",
2543                                                       (void*)&WB_G1AuxiliaryMemoryUsage  },
2544   {CC"g1ActiveMemoryNodeCount", CC"()I",              (void*)&WB_G1ActiveMemoryNodeCount },
2545   {CC"g1MemoryNodeIds",    CC"()[I",                  (void*)&WB_G1MemoryNodeIds },
2546   {CC"g1GetMixedGCInfo",   CC"(I)[J",                 (void*)&WB_G1GetMixedGCInfo },
2547 #endif // INCLUDE_G1GC
2548 #if INCLUDE_PARALLELGC
2549   {CC"psVirtualSpaceAlignment",CC"()J",               (void*)&WB_PSVirtualSpaceAlignment},
2550   {CC"psHeapGenerationAlignment",CC"()J",             (void*)&WB_PSHeapGenerationAlignment},
2551 #endif
2552   {CC"NMTMalloc",           CC"(J)J",                 (void*)&WB_NMTMalloc          },
2553   {CC"NMTMallocWithPseudoStack", CC"(JI)J",           (void*)&WB_NMTMallocWithPseudoStack},
2554   {CC"NMTMallocWithPseudoStackAndType", CC"(JII)J",   (void*)&WB_NMTMallocWithPseudoStackAndType},
2555   {CC"NMTFree",             CC"(J)V",                 (void*)&WB_NMTFree            },
2556   {CC"NMTReserveMemory",    CC"(J)J",                 (void*)&WB_NMTReserveMemory   },
2557   {CC"NMTAttemptReserveMemoryAt",    CC"(JJ)J",       (void*)&WB_NMTAttemptReserveMemoryAt },
2558   {CC"NMTCommitMemory",     CC"(JJ)V",                (void*)&WB_NMTCommitMemory    },
2559   {CC"NMTUncommitMemory",   CC"(JJ)V",                (void*)&WB_NMTUncommitMemory  },
2560   {CC"NMTReleaseMemory",    CC"(JJ)V",                (void*)&WB_NMTReleaseMemory   },
2561   {CC"NMTGetHashSize",      CC"()I",                  (void*)&WB_NMTGetHashSize     },
2562   {CC"NMTNewArena",         CC"(J)J",                 (void*)&WB_NMTNewArena        },
2563   {CC"NMTFreeArena",        CC"(J)V",                 (void*)&WB_NMTFreeArena       },
2564   {CC"NMTArenaMalloc",      CC"(JJ)V",                (void*)&WB_NMTArenaMalloc     },
2565   {CC"deoptimizeFrames",   CC"(Z)I",                  (void*)&WB_DeoptimizeFrames  },
2566   {CC"isFrameDeoptimized", CC"(I)Z",                  (void*)&WB_IsFrameDeoptimized},
2567   {CC"deoptimizeAll",      CC"()V",                   (void*)&WB_DeoptimizeAll     },
2568   {CC"deoptimizeMethod0",   CC"(Ljava/lang/reflect/Executable;Z)I",
2569                                                       (void*)&WB_DeoptimizeMethod  },
2570   {CC"isMethodCompiled0",   CC"(Ljava/lang/reflect/Executable;Z)Z",
2571                                                       (void*)&WB_IsMethodCompiled  },
2572   {CC"isMethodCompilable0", CC"(Ljava/lang/reflect/Executable;IZ)Z",
2573                                                       (void*)&WB_IsMethodCompilable},
2574   {CC"isMethodQueuedForCompilation0",
2575       CC"(Ljava/lang/reflect/Executable;)Z",          (void*)&WB_IsMethodQueuedForCompilation},
2576   {CC"isIntrinsicAvailable0",
2577       CC"(Ljava/lang/reflect/Executable;Ljava/lang/reflect/Executable;I)Z",
2578                                                       (void*)&WB_IsIntrinsicAvailable},
2579   {CC"makeMethodNotCompilable0",
2580       CC"(Ljava/lang/reflect/Executable;IZ)V",        (void*)&WB_MakeMethodNotCompilable},
2581   {CC"testSetDontInlineMethod0",
2582       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetDontInlineMethod},
2583   {CC"getMethodCompilationLevel0",
2584       CC"(Ljava/lang/reflect/Executable;Z)I",         (void*)&WB_GetMethodCompilationLevel},
2585   {CC"getMethodDecompileCount0",
2586       CC"(Ljava/lang/reflect/Executable;)I",          (void*)&WB_GetMethodDecompileCount},
2587   {CC"getMethodTrapCount0",
2588       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)I",
2589                                                       (void*)&WB_GetMethodTrapCount},
2590   {CC"getDeoptCount0",
2591       CC"(Ljava/lang/String;Ljava/lang/String;)I",    (void*)&WB_GetDeoptCount},
2592   {CC"getMethodEntryBci0",
2593       CC"(Ljava/lang/reflect/Executable;)I",          (void*)&WB_GetMethodEntryBci},
2594   {CC"getCompileQueueSize",
2595       CC"(I)I",                                       (void*)&WB_GetCompileQueueSize},
2596   {CC"testSetForceInlineMethod0",
2597       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetForceInlineMethod},
2598   {CC"enqueueMethodForCompilation0",
2599       CC"(Ljava/lang/reflect/Executable;II)Z",        (void*)&WB_EnqueueMethodForCompilation},
2600   {CC"enqueueInitializerForCompilation0",
2601       CC"(Ljava/lang/Class;I)Z",                      (void*)&WB_EnqueueInitializerForCompilation},
2602   {CC"markMethodProfiled",
2603       CC"(Ljava/lang/reflect/Executable;)V",          (void*)&WB_MarkMethodProfiled},
2604   {CC"clearMethodState0",
2605       CC"(Ljava/lang/reflect/Executable;)V",          (void*)&WB_ClearMethodState},
2606   {CC"lockCompilation",    CC"()V",                   (void*)&WB_LockCompilation},
2607   {CC"unlockCompilation",  CC"()V",                   (void*)&WB_UnlockCompilation},
2608   {CC"matchesMethod",
2609       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)I",
2610                                                       (void*)&WB_MatchesMethod},
2611   {CC"matchesInline",
2612       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)I",
2613                                                       (void*)&WB_MatchesInline},
2614   {CC"shouldPrintAssembly",
2615         CC"(Ljava/lang/reflect/Executable;I)Z",
2616                                                         (void*)&WB_ShouldPrintAssembly},
2617 
2618   {CC"isConstantVMFlag",   CC"(Ljava/lang/String;)Z", (void*)&WB_IsConstantVMFlag},
2619   {CC"isLockedVMFlag",     CC"(Ljava/lang/String;)Z", (void*)&WB_IsLockedVMFlag},
2620   {CC"setBooleanVMFlag",   CC"(Ljava/lang/String;Z)V",(void*)&WB_SetBooleanVMFlag},
2621   {CC"setIntVMFlag",       CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntVMFlag},
2622   {CC"setUintVMFlag",      CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintVMFlag},
2623   {CC"setIntxVMFlag",      CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntxVMFlag},
2624   {CC"setUintxVMFlag",     CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintxVMFlag},
2625   {CC"setUint64VMFlag",    CC"(Ljava/lang/String;J)V",(void*)&WB_SetUint64VMFlag},
2626   {CC"setSizeTVMFlag",     CC"(Ljava/lang/String;J)V",(void*)&WB_SetSizeTVMFlag},
2627   {CC"setDoubleVMFlag",    CC"(Ljava/lang/String;D)V",(void*)&WB_SetDoubleVMFlag},
2628   {CC"setStringVMFlag",    CC"(Ljava/lang/String;Ljava/lang/String;)V",
2629                                                       (void*)&WB_SetStringVMFlag},
2630   {CC"getBooleanVMFlag",   CC"(Ljava/lang/String;)Ljava/lang/Boolean;",
2631                                                       (void*)&WB_GetBooleanVMFlag},
2632   {CC"getIntVMFlag",       CC"(Ljava/lang/String;)Ljava/lang/Long;",
2633                                                       (void*)&WB_GetIntVMFlag},
2634   {CC"getUintVMFlag",      CC"(Ljava/lang/String;)Ljava/lang/Long;",
2635                                                       (void*)&WB_GetUintVMFlag},
2636   {CC"getIntxVMFlag",      CC"(Ljava/lang/String;)Ljava/lang/Long;",
2637                                                       (void*)&WB_GetIntxVMFlag},
2638   {CC"getUintxVMFlag",     CC"(Ljava/lang/String;)Ljava/lang/Long;",
2639                                                       (void*)&WB_GetUintxVMFlag},
2640   {CC"getUint64VMFlag",    CC"(Ljava/lang/String;)Ljava/lang/Long;",
2641                                                       (void*)&WB_GetUint64VMFlag},
2642   {CC"getSizeTVMFlag",     CC"(Ljava/lang/String;)Ljava/lang/Long;",
2643                                                       (void*)&WB_GetSizeTVMFlag},
2644   {CC"getDoubleVMFlag",    CC"(Ljava/lang/String;)Ljava/lang/Double;",
2645                                                       (void*)&WB_GetDoubleVMFlag},
2646   {CC"getStringVMFlag",    CC"(Ljava/lang/String;)Ljava/lang/String;",
2647                                                       (void*)&WB_GetStringVMFlag},
2648   {CC"isInStringTable",    CC"(Ljava/lang/String;)Z", (void*)&WB_IsInStringTable  },
2649   {CC"fullGC",   CC"()V",                             (void*)&WB_FullGC },
2650   {CC"youngGC",  CC"()V",                             (void*)&WB_YoungGC },
2651   {CC"readReservedMemory", CC"()V",                   (void*)&WB_ReadReservedMemory },
2652   {CC"allocateMetaspace",
2653      CC"(Ljava/lang/ClassLoader;J)J",                 (void*)&WB_AllocateMetaspace },
2654   {CC"incMetaspaceCapacityUntilGC", CC"(J)J",         (void*)&WB_IncMetaspaceCapacityUntilGC },
2655   {CC"metaspaceCapacityUntilGC", CC"()J",             (void*)&WB_MetaspaceCapacityUntilGC },
2656   {CC"metaspaceSharedRegionAlignment", CC"()J",       (void*)&WB_MetaspaceSharedRegionAlignment },
2657   {CC"getCPUFeatures",     CC"()Ljava/lang/String;",  (void*)&WB_GetCPUFeatures     },
2658   {CC"getNMethod0",         CC"(Ljava/lang/reflect/Executable;Z)[Ljava/lang/Object;",
2659                                                       (void*)&WB_GetNMethod         },
2660   {CC"forceNMethodSweep",  CC"()V",                   (void*)&WB_ForceNMethodSweep  },
2661   {CC"allocateCodeBlob",   CC"(II)J",                 (void*)&WB_AllocateCodeBlob   },
2662   {CC"freeCodeBlob",       CC"(J)V",                  (void*)&WB_FreeCodeBlob       },
2663   {CC"getCodeHeapEntries", CC"(I)[Ljava/lang/Object;",(void*)&WB_GetCodeHeapEntries },
2664   {CC"getCompilationActivityMode",
2665                            CC"()I",                   (void*)&WB_GetCompilationActivityMode},
2666   {CC"getMethodData0",     CC"(Ljava/lang/reflect/Executable;)J",
2667                                                       (void*)&WB_GetMethodData      },
2668   {CC"getCodeBlob",        CC"(J)[Ljava/lang/Object;",(void*)&WB_GetCodeBlob        },
2669   {CC"getThreadStackSize", CC"()J",                   (void*)&WB_GetThreadStackSize },
2670   {CC"getThreadRemainingStackSize", CC"()J",          (void*)&WB_GetThreadRemainingStackSize },
2671   {CC"DefineModule",       CC"(Ljava/lang/Object;ZLjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V",
2672                                                       (void*)&WB_DefineModule },
2673   {CC"AddModuleExports",   CC"(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V",
2674                                                       (void*)&WB_AddModuleExports },
2675   {CC"AddReadsModule",     CC"(Ljava/lang/Object;Ljava/lang/Object;)V",
2676                                                       (void*)&WB_AddReadsModule },
2677   {CC"AddModuleExportsToAllUnnamed", CC"(Ljava/lang/Object;Ljava/lang/String;)V",
2678                                                       (void*)&WB_AddModuleExportsToAllUnnamed },
2679   {CC"AddModuleExportsToAll", CC"(Ljava/lang/Object;Ljava/lang/String;)V",
2680                                                       (void*)&WB_AddModuleExportsToAll },
2681   {CC"deflateIdleMonitors", CC"()Z",                  (void*)&WB_DeflateIdleMonitors },
2682   {CC"isMonitorInflated0", CC"(Ljava/lang/Object;)Z", (void*)&WB_IsMonitorInflated  },
2683   {CC"forceSafepoint",     CC"()V",                   (void*)&WB_ForceSafepoint     },
2684   {CC"getConstantPool0",   CC"(Ljava/lang/Class;)J",  (void*)&WB_GetConstantPool    },
2685   {CC"getConstantPoolCacheIndexTag0", CC"()I",  (void*)&WB_GetConstantPoolCacheIndexTag},
2686   {CC"getConstantPoolCacheLength0", CC"(Ljava/lang/Class;)I",  (void*)&WB_GetConstantPoolCacheLength},
2687   {CC"remapInstructionOperandFromCPCache0",
2688       CC"(Ljava/lang/Class;I)I",                      (void*)&WB_ConstantPoolRemapInstructionOperandFromCache},
2689   {CC"encodeConstantPoolIndyIndex0",
2690       CC"(I)I",                      (void*)&WB_ConstantPoolEncodeIndyIndex},
2691   {CC"getMethodBooleanOption",
2692       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Boolean;",
2693                                                       (void*)&WB_GetMethodBooleaneOption},
2694   {CC"getMethodIntxOption",
2695       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
2696                                                       (void*)&WB_GetMethodIntxOption},
2697   {CC"getMethodUintxOption",
2698       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
2699                                                       (void*)&WB_GetMethodUintxOption},
2700   {CC"getMethodDoubleOption",
2701       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Double;",
2702                                                       (void*)&WB_GetMethodDoubleOption},
2703   {CC"getMethodStringOption",
2704       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/String;",
2705                                                       (void*)&WB_GetMethodStringOption},
2706   {CC"getDefaultArchivePath",             CC"()Ljava/lang/String;",
2707                                                       (void*)&WB_GetDefaultArchivePath},
2708   {CC"getCDSGenericHeaderMinVersion",     CC"()I",    (void*)&WB_GetCDSGenericHeaderMinVersion},
2709   {CC"getCurrentCDSVersion",              CC"()I",    (void*)&WB_GetCDSCurrentVersion},
2710   {CC"isSharingEnabled",   CC"()Z",                   (void*)&WB_IsSharingEnabled},
2711   {CC"isShared",           CC"(Ljava/lang/Object;)Z", (void*)&WB_IsShared },
2712   {CC"isSharedInternedString", CC"(Ljava/lang/String;)Z", (void*)&WB_IsSharedInternedString },
2713   {CC"isSharedClass",      CC"(Ljava/lang/Class;)Z",  (void*)&WB_IsSharedClass },
2714   {CC"areSharedStringsMapped",            CC"()Z",    (void*)&WB_AreSharedStringsMapped },
2715   {CC"getResolvedReferences", CC"(Ljava/lang/Class;)Ljava/lang/Object;", (void*)&WB_GetResolvedReferences},
2716   {CC"linkClass",          CC"(Ljava/lang/Class;)V",  (void*)&WB_LinkClass},
2717   {CC"areOpenArchiveHeapObjectsMapped",   CC"()Z",    (void*)&WB_AreOpenArchiveHeapObjectsMapped},
2718   {CC"isCDSIncluded",                     CC"()Z",    (void*)&WB_IsCDSIncluded },
2719   {CC"isJFRIncluded",                     CC"()Z",    (void*)&WB_IsJFRIncluded },
2720   {CC"isDTraceIncluded",                  CC"()Z",    (void*)&WB_IsDTraceIncluded },
2721   {CC"isC2OrJVMCIIncluded",               CC"()Z",    (void*)&WB_isC2OrJVMCIIncluded },
2722   {CC"isJVMCISupportedByGC",              CC"()Z",    (void*)&WB_IsJVMCISupportedByGC},
2723   {CC"canWriteJavaHeapArchive",           CC"()Z",    (void*)&WB_CanWriteJavaHeapArchive },
2724   {CC"cdsMemoryMappingFailed",            CC"()Z",    (void*)&WB_CDSMemoryMappingFailed },
2725 
2726   {CC"clearInlineCaches0",  CC"(Z)V",                 (void*)&WB_ClearInlineCaches },
2727   {CC"handshakeReadMonitors", CC"(Ljava/lang/Thread;)Z", (void*)&WB_HandshakeReadMonitors },
2728   {CC"handshakeWalkStack", CC"(Ljava/lang/Thread;Z)I", (void*)&WB_HandshakeWalkStack },
2729   {CC"asyncHandshakeWalkStack", CC"(Ljava/lang/Thread;)V", (void*)&WB_AsyncHandshakeWalkStack },
2730   {CC"lockAndBlock", CC"(Z)V",                        (void*)&WB_LockAndBlock},
2731   {CC"checkThreadObjOfTerminatingThread", CC"(Ljava/lang/Thread;)V", (void*)&WB_CheckThreadObjOfTerminatingThread },
2732   {CC"verifyFrames",                CC"(ZZ)V",            (void*)&WB_VerifyFrames },
2733   {CC"addCompilerDirective",    CC"(Ljava/lang/String;)I",
2734                                                       (void*)&WB_AddCompilerDirective },
2735   {CC"removeCompilerDirective",   CC"(I)V",           (void*)&WB_RemoveCompilerDirective },
2736   {CC"isGCSupported",             CC"(I)Z",           (void*)&WB_IsGCSupported},
2737   {CC"isGCSupportedByJVMCICompiler", CC"(I)Z",        (void*)&WB_IsGCSupportedByJVMCICompiler},
2738   {CC"isGCSelected",              CC"(I)Z",           (void*)&WB_IsGCSelected},
2739   {CC"isGCSelectedErgonomically", CC"()Z",            (void*)&WB_IsGCSelectedErgonomically},
2740   {CC"supportsConcurrentGCBreakpoints", CC"()Z",      (void*)&WB_SupportsConcurrentGCBreakpoints},
2741   {CC"concurrentGCAcquireControl0", CC"()V",          (void*)&WB_ConcurrentGCAcquireControl},
2742   {CC"concurrentGCReleaseControl0", CC"()V",          (void*)&WB_ConcurrentGCReleaseControl},
2743   {CC"concurrentGCRunToIdle0",    CC"()V",            (void*)&WB_ConcurrentGCRunToIdle},
2744   {CC"concurrentGCRunTo0",        CC"(Ljava/lang/String;)Z",
2745                                                       (void*)&WB_ConcurrentGCRunTo},
2746   {CC"checkLibSpecifiesNoexecstack", CC"(Ljava/lang/String;)Z",
2747                                                       (void*)&WB_CheckLibSpecifiesNoexecstack},
2748   {CC"isContainerized",           CC"()Z",            (void*)&WB_IsContainerized },
2749   {CC"validateCgroup",
2750       CC"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
2751                                                       (void*)&WB_ValidateCgroup },
2752   {CC"printOsInfo",               CC"()V",            (void*)&WB_PrintOsInfo },
2753   {CC"disableElfSectionCache",    CC"()V",            (void*)&WB_DisableElfSectionCache },
2754   {CC"resolvedMethodItemsCount",  CC"()J",            (void*)&WB_ResolvedMethodItemsCount },
2755   {CC"protectionDomainRemovedCount",   CC"()I",       (void*)&WB_ProtectionDomainRemovedCount },
2756   {CC"getKlassMetadataSize", CC"(Ljava/lang/Class;)I",(void*)&WB_GetKlassMetadataSize},
2757 
2758   {CC"createMetaspaceTestContext", CC"(JJ)J",         (void*)&WB_CreateMetaspaceTestContext},
2759   {CC"destroyMetaspaceTestContext", CC"(J)V",         (void*)&WB_DestroyMetaspaceTestContext},
2760   {CC"purgeMetaspaceTestContext", CC"(J)V",           (void*)&WB_PurgeMetaspaceTestContext},
2761   {CC"printMetaspaceTestContext", CC"(J)V",           (void*)&WB_PrintMetaspaceTestContext},
2762   {CC"getTotalCommittedWordsInMetaspaceTestContext", CC"(J)J",(void*)&WB_GetTotalCommittedWordsInMetaspaceTestContext},
2763   {CC"getTotalUsedWordsInMetaspaceTestContext", CC"(J)J", (void*)&WB_GetTotalUsedWordsInMetaspaceTestContext},
2764   {CC"createArenaInTestContext", CC"(JZ)J",           (void*)&WB_CreateArenaInTestContext},
2765   {CC"destroyMetaspaceTestArena", CC"(J)V",           (void*)&WB_DestroyMetaspaceTestArena},
2766   {CC"allocateFromMetaspaceTestArena", CC"(JJ)J",     (void*)&WB_AllocateFromMetaspaceTestArena},
2767   {CC"deallocateToMetaspaceTestArena", CC"(JJJ)V",    (void*)&WB_DeallocateToMetaspaceTestArena},
2768   {CC"maxMetaspaceAllocationSize", CC"()J",           (void*)&WB_GetMaxMetaspaceAllocationSize},
2769 
2770   {CC"isJVMTIIncluded", CC"()Z",                      (void*)&WB_IsJVMTIIncluded},
2771   {CC"waitUnsafe", CC"(I)V",                          (void*)&WB_WaitUnsafe},
2772   {CC"getLibcName",     CC"()Ljava/lang/String;",     (void*)&WB_GetLibcName},
2773 
2774   {CC"lockCritical",    CC"()V",                      (void*)&WB_LockCritical},
2775   {CC"unlockCritical",  CC"()V",                      (void*)&WB_UnlockCritical},
2776 };
2777 
2778 
2779 #undef CC
2780 
2781 JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass))
2782   {
2783     if (WhiteBoxAPI) {
2784       // Make sure that wbclass is loaded by the null classloader
2785       InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(wbclass)));
2786       Handle loader(THREAD, ik->class_loader());
2787       if (loader.is_null()) {
2788         WhiteBox::register_methods(env, wbclass, thread, methods, sizeof(methods) / sizeof(methods[0]));
2789         WhiteBox::set_used();
2790       }
2791     }
2792   }
2793 JVM_END