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