1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 27 #include "memory/universe.hpp" 28 #include "oops/oop.inline.hpp" 29 30 #include "classfile/symbolTable.hpp" 31 #include "classfile/classLoaderData.hpp" 32 33 #include "prims/whitebox.hpp" 34 #include "prims/wbtestmethods/parserTests.hpp" 35 36 #include "runtime/thread.hpp" 37 #include "runtime/arguments.hpp" 38 #include "runtime/interfaceSupport.hpp" 39 #include "runtime/os.hpp" 40 41 #include "utilities/debug.hpp" 42 #include "utilities/macros.hpp" 43 #include "utilities/exceptions.hpp" 44 45 #if INCLUDE_ALL_GCS 46 #include "gc_implementation/g1/concurrentMark.hpp" 47 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" 48 #include "gc_implementation/g1/heapRegionRemSet.hpp" 49 #endif // INCLUDE_ALL_GCS 50 51 #ifdef INCLUDE_NMT 52 #include "services/memTracker.hpp" 53 #endif // INCLUDE_NMT 54 55 #include "compiler/compileBroker.hpp" 56 #include "runtime/compilationPolicy.hpp" 57 58 #define SIZE_T_MAX_VALUE ((size_t) -1) 59 60 bool WhiteBox::_used = false; 61 62 WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj)) 63 return (jlong)(void*)JNIHandles::resolve(obj); 64 WB_END 65 66 WB_ENTRY(jint, WB_GetHeapOopSize(JNIEnv* env, jobject o)) 67 return heapOopSize; 68 WB_END 69 70 71 class WBIsKlassAliveClosure : public KlassClosure { 72 Symbol* _name; 73 bool _found; 74 public: 75 WBIsKlassAliveClosure(Symbol* name) : _name(name), _found(false) {} 76 77 void do_klass(Klass* k) { 78 if (_found) return; 79 Symbol* ksym = k->name(); 80 if (ksym->fast_compare(_name) == 0) { 81 _found = true; 82 } 83 } 84 85 bool found() const { 86 return _found; 87 } 88 }; 89 90 WB_ENTRY(jboolean, WB_IsClassAlive(JNIEnv* env, jobject target, jstring name)) 91 Handle h_name = JNIHandles::resolve(name); 92 if (h_name.is_null()) return false; 93 Symbol* sym = java_lang_String::as_symbol(h_name, CHECK_false); 94 TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return 95 96 WBIsKlassAliveClosure closure(sym); 97 ClassLoaderDataGraph::classes_do(&closure); 98 99 return closure.found(); 100 WB_END 101 102 WB_ENTRY(jlong, WB_GetCompressedOopsMaxHeapSize(JNIEnv* env, jobject o)) { 103 return (jlong)Arguments::max_heap_for_compressed_oops(); 104 } 105 WB_END 106 107 WB_ENTRY(void, WB_PrintHeapSizes(JNIEnv* env, jobject o)) { 108 CollectorPolicy * p = Universe::heap()->collector_policy(); 109 gclog_or_tty->print_cr("Minimum heap "SIZE_FORMAT" Initial heap " 110 SIZE_FORMAT" Maximum heap "SIZE_FORMAT" Space alignment "SIZE_FORMAT" Heap alignment "SIZE_FORMAT, 111 p->min_heap_byte_size(), p->initial_heap_byte_size(), p->max_heap_byte_size(), 112 p->space_alignment(), p->heap_alignment()); 113 } 114 WB_END 115 116 #ifndef PRODUCT 117 // Forward declaration 118 void TestReservedSpace_test(); 119 void TestReserveMemorySpecial_test(); 120 void TestVirtualSpace_test(); 121 void TestMetaspaceAux_test(); 122 #endif 123 124 WB_ENTRY(void, WB_RunMemoryUnitTests(JNIEnv* env, jobject o)) 125 #ifndef PRODUCT 126 TestReservedSpace_test(); 127 TestReserveMemorySpecial_test(); 128 TestVirtualSpace_test(); 129 TestMetaspaceAux_test(); 130 #endif 131 WB_END 132 133 WB_ENTRY(void, WB_ReadFromNoaccessArea(JNIEnv* env, jobject o)) 134 size_t granularity = os::vm_allocation_granularity(); 135 ReservedHeapSpace rhs(100 * granularity, granularity, false, NULL); 136 VirtualSpace vs; 137 vs.initialize(rhs, 50 * granularity); 138 139 //Check if constraints are complied 140 if (!( UseCompressedOops && rhs.base() != NULL && 141 Universe::narrow_oop_base() != NULL && 142 Universe::narrow_oop_use_implicit_null_checks() )) { 143 tty->print_cr("WB_ReadFromNoaccessArea method is useless:\n " 144 "\tUseCompressedOops is %d\n" 145 "\trhs.base() is "PTR_FORMAT"\n" 146 "\tUniverse::narrow_oop_base() is "PTR_FORMAT"\n" 147 "\tUniverse::narrow_oop_use_implicit_null_checks() is %d", 148 UseCompressedOops, 149 rhs.base(), 150 Universe::narrow_oop_base(), 151 Universe::narrow_oop_use_implicit_null_checks()); 152 return; 153 } 154 tty->print_cr("Reading from no access area... "); 155 tty->print_cr("*(vs.low_boundary() - rhs.noaccess_prefix() / 2 ) = %c", 156 *(vs.low_boundary() - rhs.noaccess_prefix() / 2 )); 157 WB_END 158 159 static jint wb_stress_virtual_space_resize(size_t reserved_space_size, 160 size_t magnitude, size_t iterations) { 161 size_t granularity = os::vm_allocation_granularity(); 162 ReservedHeapSpace rhs(reserved_space_size * granularity, granularity, false, NULL); 163 VirtualSpace vs; 164 if (!vs.initialize(rhs, 0)) { 165 tty->print_cr("Failed to initialize VirtualSpace. Can't proceed."); 166 return 3; 167 } 168 169 long seed = os::random(); 170 tty->print_cr("Random seed is %ld", seed); 171 os::init_random(seed); 172 173 for (size_t i = 0; i < iterations; i++) { 174 175 // Whether we will shrink or grow 176 bool shrink = os::random() % 2L == 0; 177 178 // Get random delta to resize virtual space 179 size_t delta = (size_t)os::random() % magnitude; 180 181 // If we are about to shrink virtual space below zero, then expand instead 182 if (shrink && vs.committed_size() < delta) { 183 shrink = false; 184 } 185 186 // Resizing by delta 187 if (shrink) { 188 vs.shrink_by(delta); 189 } else { 190 // If expanding fails expand_by will silently return false 191 vs.expand_by(delta, true); 192 } 193 } 194 return 0; 195 } 196 197 WB_ENTRY(jint, WB_StressVirtualSpaceResize(JNIEnv* env, jobject o, 198 jlong reserved_space_size, jlong magnitude, jlong iterations)) 199 tty->print_cr("reservedSpaceSize="JLONG_FORMAT", magnitude="JLONG_FORMAT", " 200 "iterations="JLONG_FORMAT"\n", reserved_space_size, magnitude, 201 iterations); 202 if (reserved_space_size < 0 || magnitude < 0 || iterations < 0) { 203 tty->print_cr("One of variables printed above is negative. Can't proceed.\n"); 204 return 1; 205 } 206 207 // sizeof(size_t) depends on whether OS is 32bit or 64bit. sizeof(jlong) is 208 // always 8 byte. That's why we should avoid overflow in case of 32bit platform. 209 if (sizeof(size_t) < sizeof(jlong)) { 210 jlong size_t_max_value = (jlong) SIZE_T_MAX_VALUE; 211 if (reserved_space_size > size_t_max_value || magnitude > size_t_max_value 212 || iterations > size_t_max_value) { 213 tty->print_cr("One of variables printed above overflows size_t. Can't proceed.\n"); 214 return 2; 215 } 216 } 217 218 return wb_stress_virtual_space_resize((size_t) reserved_space_size, 219 (size_t) magnitude, (size_t) iterations); 220 WB_END 221 222 #if INCLUDE_ALL_GCS 223 WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj)) 224 G1CollectedHeap* g1 = G1CollectedHeap::heap(); 225 oop result = JNIHandles::resolve(obj); 226 const HeapRegion* hr = g1->heap_region_containing(result); 227 return hr->isHumongous(); 228 WB_END 229 230 WB_ENTRY(jlong, WB_G1NumFreeRegions(JNIEnv* env, jobject o)) 231 G1CollectedHeap* g1 = G1CollectedHeap::heap(); 232 size_t nr = g1->free_regions(); 233 return (jlong)nr; 234 WB_END 235 236 WB_ENTRY(jboolean, WB_G1InConcurrentMark(JNIEnv* env, jobject o)) 237 G1CollectedHeap* g1 = G1CollectedHeap::heap(); 238 ConcurrentMark* cm = g1->concurrent_mark(); 239 return cm->concurrent_marking_in_progress(); 240 WB_END 241 242 WB_ENTRY(jint, WB_G1RegionSize(JNIEnv* env, jobject o)) 243 return (jint)HeapRegion::GrainBytes; 244 WB_END 245 #endif // INCLUDE_ALL_GCS 246 247 #if INCLUDE_NMT 248 // Alloc memory using the test memory type so that we can use that to see if 249 // NMT picks it up correctly 250 WB_ENTRY(jlong, WB_NMTMalloc(JNIEnv* env, jobject o, jlong size)) 251 jlong addr = 0; 252 253 if (MemTracker::is_on() && !MemTracker::shutdown_in_progress()) { 254 addr = (jlong)(uintptr_t)os::malloc(size, mtTest); 255 } 256 257 return addr; 258 WB_END 259 260 // Free the memory allocated by NMTAllocTest 261 WB_ENTRY(void, WB_NMTFree(JNIEnv* env, jobject o, jlong mem)) 262 os::free((void*)(uintptr_t)mem, mtTest); 263 WB_END 264 265 WB_ENTRY(jlong, WB_NMTReserveMemory(JNIEnv* env, jobject o, jlong size)) 266 jlong addr = 0; 267 268 if (MemTracker::is_on() && !MemTracker::shutdown_in_progress()) { 269 addr = (jlong)(uintptr_t)os::reserve_memory(size); 270 MemTracker::record_virtual_memory_type((address)addr, mtTest); 271 } 272 273 return addr; 274 WB_END 275 276 277 WB_ENTRY(void, WB_NMTCommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size)) 278 os::commit_memory((char *)(uintptr_t)addr, size, !ExecMem); 279 MemTracker::record_virtual_memory_type((address)(uintptr_t)addr, mtTest); 280 WB_END 281 282 WB_ENTRY(void, WB_NMTUncommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size)) 283 os::uncommit_memory((char *)(uintptr_t)addr, size); 284 WB_END 285 286 WB_ENTRY(void, WB_NMTReleaseMemory(JNIEnv* env, jobject o, jlong addr, jlong size)) 287 os::release_memory((char *)(uintptr_t)addr, size); 288 WB_END 289 290 // Block until the current generation of NMT data to be merged, used to reliably test the NMT feature 291 WB_ENTRY(jboolean, WB_NMTWaitForDataMerge(JNIEnv* env)) 292 293 if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) { 294 return false; 295 } 296 297 return MemTracker::wbtest_wait_for_data_merge(); 298 WB_END 299 300 WB_ENTRY(jboolean, WB_NMTIsDetailSupported(JNIEnv* env)) 301 return MemTracker::tracking_level() == MemTracker::NMT_detail; 302 WB_END 303 304 #endif // INCLUDE_NMT 305 306 static jmethodID reflected_method_to_jmid(JavaThread* thread, JNIEnv* env, jobject method) { 307 assert(method != NULL, "method should not be null"); 308 ThreadToNativeFromVM ttn(thread); 309 return env->FromReflectedMethod(method); 310 } 311 312 WB_ENTRY(void, WB_DeoptimizeAll(JNIEnv* env, jobject o)) 313 MutexLockerEx mu(Compile_lock); 314 CodeCache::mark_all_nmethods_for_deoptimization(); 315 VM_Deoptimize op; 316 VMThread::execute(&op); 317 WB_END 318 319 WB_ENTRY(jint, WB_DeoptimizeMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr)) 320 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 321 int result = 0; 322 CHECK_JNI_EXCEPTION_(env, result); 323 MutexLockerEx mu(Compile_lock); 324 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 325 nmethod* code; 326 if (is_osr) { 327 int bci = InvocationEntryBci; 328 while ((code = mh->lookup_osr_nmethod_for(bci, CompLevel_none, false)) != NULL) { 329 code->mark_for_deoptimization(); 330 ++result; 331 bci = code->osr_entry_bci() + 1; 332 } 333 } else { 334 code = mh->code(); 335 } 336 if (code != NULL) { 337 code->mark_for_deoptimization(); 338 ++result; 339 } 340 result += CodeCache::mark_for_deoptimization(mh()); 341 if (result > 0) { 342 VM_Deoptimize op; 343 VMThread::execute(&op); 344 } 345 return result; 346 WB_END 347 348 WB_ENTRY(jboolean, WB_IsMethodCompiled(JNIEnv* env, jobject o, jobject method, jboolean is_osr)) 349 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 350 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 351 MutexLockerEx mu(Compile_lock); 352 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 353 nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code(); 354 if (code == NULL) { 355 return JNI_FALSE; 356 } 357 return (code->is_alive() && !code->is_marked_for_deoptimization()); 358 WB_END 359 360 WB_ENTRY(jboolean, WB_IsMethodCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr)) 361 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 362 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 363 MutexLockerEx mu(Compile_lock); 364 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 365 if (is_osr) { 366 return CompilationPolicy::can_be_osr_compiled(mh, comp_level); 367 } else { 368 return CompilationPolicy::can_be_compiled(mh, comp_level); 369 } 370 WB_END 371 372 WB_ENTRY(jboolean, WB_IsMethodQueuedForCompilation(JNIEnv* env, jobject o, jobject method)) 373 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 374 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 375 MutexLockerEx mu(Compile_lock); 376 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 377 return mh->queued_for_compilation(); 378 WB_END 379 380 WB_ENTRY(jint, WB_GetMethodCompilationLevel(JNIEnv* env, jobject o, jobject method, jboolean is_osr)) 381 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 382 CHECK_JNI_EXCEPTION_(env, CompLevel_none); 383 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 384 nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code(); 385 return (code != NULL ? code->comp_level() : CompLevel_none); 386 WB_END 387 388 WB_ENTRY(void, WB_MakeMethodNotCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr)) 389 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 390 CHECK_JNI_EXCEPTION(env); 391 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 392 if (is_osr) { 393 mh->set_not_osr_compilable(comp_level, true /* report */, "WhiteBox"); 394 } else { 395 mh->set_not_compilable(comp_level, true /* report */, "WhiteBox"); 396 } 397 WB_END 398 399 WB_ENTRY(jint, WB_GetMethodEntryBci(JNIEnv* env, jobject o, jobject method)) 400 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 401 CHECK_JNI_EXCEPTION_(env, InvocationEntryBci); 402 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 403 nmethod* code = mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false); 404 return (code != NULL && code->is_osr_method() ? code->osr_entry_bci() : InvocationEntryBci); 405 WB_END 406 407 WB_ENTRY(jboolean, WB_TestSetDontInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value)) 408 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 409 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 410 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 411 bool result = mh->dont_inline(); 412 mh->set_dont_inline(value == JNI_TRUE); 413 return result; 414 WB_END 415 416 WB_ENTRY(jint, WB_GetCompileQueueSize(JNIEnv* env, jobject o, jint comp_level)) 417 if (comp_level == CompLevel_any) { 418 return CompileBroker::queue_size(CompLevel_full_optimization) /* C2 */ + 419 CompileBroker::queue_size(CompLevel_full_profile) /* C1 */; 420 } else { 421 return CompileBroker::queue_size(comp_level); 422 } 423 WB_END 424 425 WB_ENTRY(jboolean, WB_TestSetForceInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value)) 426 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 427 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 428 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 429 bool result = mh->force_inline(); 430 mh->set_force_inline(value == JNI_TRUE); 431 return result; 432 WB_END 433 434 WB_ENTRY(jboolean, WB_EnqueueMethodForCompilation(JNIEnv* env, jobject o, jobject method, jint comp_level, jint bci)) 435 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 436 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 437 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 438 nmethod* nm = CompileBroker::compile_method(mh, bci, comp_level, mh, mh->invocation_count(), "WhiteBox", THREAD); 439 MutexLockerEx mu(Compile_lock); 440 return (mh->queued_for_compilation() || nm != NULL); 441 WB_END 442 443 class VM_WhiteBoxOperation : public VM_Operation { 444 public: 445 VM_WhiteBoxOperation() { } 446 VMOp_Type type() const { return VMOp_WhiteBoxOperation; } 447 bool allow_nested_vm_operations() const { return true; } 448 }; 449 450 class AlwaysFalseClosure : public BoolObjectClosure { 451 public: 452 bool do_object_b(oop p) { return false; } 453 }; 454 455 static AlwaysFalseClosure always_false; 456 457 class VM_WhiteBoxCleanMethodData : public VM_WhiteBoxOperation { 458 public: 459 VM_WhiteBoxCleanMethodData(MethodData* mdo) : _mdo(mdo) { } 460 void doit() { 461 _mdo->clean_method_data(&always_false); 462 } 463 private: 464 MethodData* _mdo; 465 }; 466 467 WB_ENTRY(void, WB_ClearMethodState(JNIEnv* env, jobject o, jobject method)) 468 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 469 CHECK_JNI_EXCEPTION(env); 470 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 471 MutexLockerEx mu(Compile_lock); 472 MethodData* mdo = mh->method_data(); 473 MethodCounters* mcs = mh->method_counters(); 474 475 if (mdo != NULL) { 476 mdo->init(); 477 ResourceMark rm; 478 int arg_count = mdo->method()->size_of_parameters(); 479 for (int i = 0; i < arg_count; i++) { 480 mdo->set_arg_modified(i, 0); 481 } 482 VM_WhiteBoxCleanMethodData op(mdo); 483 VMThread::execute(&op); 484 } 485 486 mh->clear_not_c1_compilable(); 487 mh->clear_not_c2_compilable(); 488 mh->clear_not_c2_osr_compilable(); 489 NOT_PRODUCT(mh->set_compiled_invocation_count(0)); 490 if (mcs != NULL) { 491 mcs->backedge_counter()->init(); 492 mcs->invocation_counter()->init(); 493 mcs->set_interpreter_invocation_count(0); 494 mcs->set_interpreter_throwout_count(0); 495 496 #ifdef TIERED 497 mcs->set_rate(0.0F); 498 mh->set_prev_event_count(0, THREAD); 499 mh->set_prev_time(0, THREAD); 500 #endif 501 } 502 WB_END 503 504 WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString)) 505 ResourceMark rm(THREAD); 506 int len; 507 jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len, CHECK_false); 508 return (StringTable::lookup(name, len) != NULL); 509 WB_END 510 511 WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o)) 512 Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(true); 513 Universe::heap()->collect(GCCause::_last_ditch_collection); 514 WB_END 515 516 517 WB_ENTRY(void, WB_ReadReservedMemory(JNIEnv* env, jobject o)) 518 // static+volatile in order to force the read to happen 519 // (not be eliminated by the compiler) 520 static char c; 521 static volatile char* p; 522 523 p = os::reserve_memory(os::vm_allocation_granularity(), NULL, 0); 524 if (p == NULL) { 525 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Failed to reserve memory"); 526 } 527 528 c = *p; 529 WB_END 530 531 WB_ENTRY(jstring, WB_GetCPUFeatures(JNIEnv* env, jobject o)) 532 const char* cpu_features = VM_Version::cpu_features(); 533 ThreadToNativeFromVM ttn(thread); 534 jstring features_string = env->NewStringUTF(cpu_features); 535 536 CHECK_JNI_EXCEPTION_(env, NULL); 537 538 return features_string; 539 WB_END 540 541 542 WB_ENTRY(jobjectArray, WB_GetNMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr)) 543 ResourceMark rm(THREAD); 544 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 545 CHECK_JNI_EXCEPTION_(env, NULL); 546 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 547 nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code(); 548 jobjectArray result = NULL; 549 if (code == NULL) { 550 return result; 551 } 552 int insts_size = code->insts_size(); 553 554 ThreadToNativeFromVM ttn(thread); 555 jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string()); 556 CHECK_JNI_EXCEPTION_(env, NULL); 557 result = env->NewObjectArray(2, clazz, NULL); 558 if (result == NULL) { 559 return result; 560 } 561 562 clazz = env->FindClass(vmSymbols::java_lang_Integer()->as_C_string()); 563 CHECK_JNI_EXCEPTION_(env, NULL); 564 jmethodID constructor = env->GetMethodID(clazz, vmSymbols::object_initializer_name()->as_C_string(), vmSymbols::int_void_signature()->as_C_string()); 565 CHECK_JNI_EXCEPTION_(env, NULL); 566 jobject obj = env->NewObject(clazz, constructor, code->comp_level()); 567 CHECK_JNI_EXCEPTION_(env, NULL); 568 env->SetObjectArrayElement(result, 0, obj); 569 570 jbyteArray insts = env->NewByteArray(insts_size); 571 CHECK_JNI_EXCEPTION_(env, NULL); 572 env->SetByteArrayRegion(insts, 0, insts_size, (jbyte*) code->insts_begin()); 573 env->SetObjectArrayElement(result, 1, insts); 574 575 return result; 576 WB_END 577 578 579 WB_ENTRY(jlong, WB_GetThreadFullStackSize(JNIEnv* env, jobject o)) 580 return (jlong) Thread::current()->stack_size(); 581 WB_END 582 583 WB_ENTRY(jlong, WB_GetThreadRemainingStackSize(JNIEnv* env, jobject o)) 584 JavaThread* t = JavaThread::current(); 585 return (jlong) ((ptrdiff_t) t->stack_available(os::current_stack_pointer()) - StackShadowPages * os::vm_page_size()); 586 WB_END 587 588 //Some convenience methods to deal with objects from java 589 int WhiteBox::offset_for_field(const char* field_name, oop object, 590 Symbol* signature_symbol) { 591 assert(field_name != NULL && strlen(field_name) > 0, "Field name not valid"); 592 Thread* THREAD = Thread::current(); 593 594 //Get the class of our object 595 Klass* arg_klass = object->klass(); 596 //Turn it into an instance-klass 597 InstanceKlass* ik = InstanceKlass::cast(arg_klass); 598 599 //Create symbols to look for in the class 600 TempNewSymbol name_symbol = SymbolTable::lookup(field_name, (int) strlen(field_name), 601 THREAD); 602 603 //To be filled in with an offset of the field we're looking for 604 fieldDescriptor fd; 605 606 Klass* res = ik->find_field(name_symbol, signature_symbol, &fd); 607 if (res == NULL) { 608 tty->print_cr("Invalid layout of %s at %s", ik->external_name(), 609 name_symbol->as_C_string()); 610 fatal("Invalid layout of preloaded class"); 611 } 612 613 //fetch the field at the offset we've found 614 int dest_offset = fd.offset(); 615 616 return dest_offset; 617 } 618 619 620 const char* WhiteBox::lookup_jstring(const char* field_name, oop object) { 621 int offset = offset_for_field(field_name, object, 622 vmSymbols::string_signature()); 623 oop string = object->obj_field(offset); 624 if (string == NULL) { 625 return NULL; 626 } 627 const char* ret = java_lang_String::as_utf8_string(string); 628 return ret; 629 } 630 631 bool WhiteBox::lookup_bool(const char* field_name, oop object) { 632 int offset = 633 offset_for_field(field_name, object, vmSymbols::bool_signature()); 634 bool ret = (object->bool_field(offset) == JNI_TRUE); 635 return ret; 636 } 637 638 639 #define CC (char*) 640 641 static JNINativeMethod methods[] = { 642 {CC"getObjectAddress", CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress }, 643 {CC"getHeapOopSize", CC"()I", (void*)&WB_GetHeapOopSize }, 644 {CC"isClassAlive0", CC"(Ljava/lang/String;)Z", (void*)&WB_IsClassAlive }, 645 {CC"parseCommandLine", 646 CC"(Ljava/lang/String;[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;", 647 (void*) &WB_ParseCommandLine 648 }, 649 {CC"getCompressedOopsMaxHeapSize", CC"()J", 650 (void*)&WB_GetCompressedOopsMaxHeapSize}, 651 {CC"printHeapSizes", CC"()V", (void*)&WB_PrintHeapSizes }, 652 {CC"runMemoryUnitTests", CC"()V", (void*)&WB_RunMemoryUnitTests}, 653 {CC"readFromNoaccessArea",CC"()V", (void*)&WB_ReadFromNoaccessArea}, 654 {CC"stressVirtualSpaceResize",CC"(JJJ)I", (void*)&WB_StressVirtualSpaceResize}, 655 #if INCLUDE_ALL_GCS 656 {CC"g1InConcurrentMark", CC"()Z", (void*)&WB_G1InConcurrentMark}, 657 {CC"g1IsHumongous", CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous }, 658 {CC"g1NumFreeRegions", CC"()J", (void*)&WB_G1NumFreeRegions }, 659 {CC"g1RegionSize", CC"()I", (void*)&WB_G1RegionSize }, 660 #endif // INCLUDE_ALL_GCS 661 #if INCLUDE_NMT 662 {CC"NMTMalloc", CC"(J)J", (void*)&WB_NMTMalloc }, 663 {CC"NMTFree", CC"(J)V", (void*)&WB_NMTFree }, 664 {CC"NMTReserveMemory", CC"(J)J", (void*)&WB_NMTReserveMemory }, 665 {CC"NMTCommitMemory", CC"(JJ)V", (void*)&WB_NMTCommitMemory }, 666 {CC"NMTUncommitMemory", CC"(JJ)V", (void*)&WB_NMTUncommitMemory }, 667 {CC"NMTReleaseMemory", CC"(JJ)V", (void*)&WB_NMTReleaseMemory }, 668 {CC"NMTWaitForDataMerge", CC"()Z", (void*)&WB_NMTWaitForDataMerge}, 669 {CC"NMTIsDetailSupported",CC"()Z", (void*)&WB_NMTIsDetailSupported}, 670 #endif // INCLUDE_NMT 671 {CC"deoptimizeAll", CC"()V", (void*)&WB_DeoptimizeAll }, 672 {CC"deoptimizeMethod", CC"(Ljava/lang/reflect/Executable;Z)I", 673 (void*)&WB_DeoptimizeMethod }, 674 {CC"isMethodCompiled", CC"(Ljava/lang/reflect/Executable;Z)Z", 675 (void*)&WB_IsMethodCompiled }, 676 {CC"isMethodCompilable", CC"(Ljava/lang/reflect/Executable;IZ)Z", 677 (void*)&WB_IsMethodCompilable}, 678 {CC"isMethodQueuedForCompilation", 679 CC"(Ljava/lang/reflect/Executable;)Z", (void*)&WB_IsMethodQueuedForCompilation}, 680 {CC"makeMethodNotCompilable", 681 CC"(Ljava/lang/reflect/Executable;IZ)V", (void*)&WB_MakeMethodNotCompilable}, 682 {CC"testSetDontInlineMethod", 683 CC"(Ljava/lang/reflect/Executable;Z)Z", (void*)&WB_TestSetDontInlineMethod}, 684 {CC"getMethodCompilationLevel", 685 CC"(Ljava/lang/reflect/Executable;Z)I", (void*)&WB_GetMethodCompilationLevel}, 686 {CC"getMethodEntryBci", 687 CC"(Ljava/lang/reflect/Executable;)I", (void*)&WB_GetMethodEntryBci}, 688 {CC"getCompileQueueSize", 689 CC"(I)I", (void*)&WB_GetCompileQueueSize}, 690 {CC"testSetForceInlineMethod", 691 CC"(Ljava/lang/reflect/Executable;Z)Z", (void*)&WB_TestSetForceInlineMethod}, 692 {CC"enqueueMethodForCompilation", 693 CC"(Ljava/lang/reflect/Executable;II)Z", (void*)&WB_EnqueueMethodForCompilation}, 694 {CC"clearMethodState", 695 CC"(Ljava/lang/reflect/Executable;)V", (void*)&WB_ClearMethodState}, 696 {CC"isInStringTable", CC"(Ljava/lang/String;)Z", (void*)&WB_IsInStringTable }, 697 {CC"fullGC", CC"()V", (void*)&WB_FullGC }, 698 {CC"readReservedMemory", CC"()V", (void*)&WB_ReadReservedMemory }, 699 {CC"getCPUFeatures", CC"()Ljava/lang/String;", (void*)&WB_GetCPUFeatures }, 700 {CC"getNMethod", CC"(Ljava/lang/reflect/Executable;Z)[Ljava/lang/Object;", 701 (void*)&WB_GetNMethod }, 702 {CC"getThreadFullStackSize", CC"()J", (void*)&WB_GetThreadFullStackSize }, 703 {CC"getThreadRemainingStackSize", CC"()J", (void*)&WB_GetThreadRemainingStackSize }, 704 }; 705 706 #undef CC 707 708 JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass)) 709 { 710 if (WhiteBoxAPI) { 711 // Make sure that wbclass is loaded by the null classloader 712 instanceKlassHandle ikh = instanceKlassHandle(JNIHandles::resolve(wbclass)->klass()); 713 Handle loader(ikh->class_loader()); 714 if (loader.is_null()) { 715 ResourceMark rm; 716 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 717 bool result = true; 718 // one by one registration natives for exception catching 719 jclass exceptionKlass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string()); 720 CHECK_JNI_EXCEPTION(env); 721 for (int i = 0, n = sizeof(methods) / sizeof(methods[0]); i < n; ++i) { 722 if (env->RegisterNatives(wbclass, methods + i, 1) != 0) { 723 result = false; 724 jthrowable throwable_obj = env->ExceptionOccurred(); 725 if (throwable_obj != NULL) { 726 env->ExceptionClear(); 727 if (env->IsInstanceOf(throwable_obj, exceptionKlass)) { 728 // j.l.NoSuchMethodError is thrown when a method can't be found or a method is not native 729 // ignoring the exception 730 tty->print_cr("Warning: 'NoSuchMethodError' on register of sun.hotspot.WhiteBox::%s%s", methods[i].name, methods[i].signature); 731 } 732 } else { 733 // register is failed w/o exception or w/ unexpected exception 734 tty->print_cr("Warning: unexpected error on register of sun.hotspot.WhiteBox::%s%s. All methods will be unregistered", methods[i].name, methods[i].signature); 735 env->UnregisterNatives(wbclass); 736 break; 737 } 738 } 739 } 740 741 if (result) { 742 WhiteBox::set_used(); 743 } 744 } 745 } 746 } 747 JVM_END