1 /* 2 * Copyright (c) 2012, 2018, 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 #include "precompiled.hpp" 25 #include "jvm.h" 26 #include "asm/codeBuffer.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "code/codeCache.hpp" 29 #include "compiler/compileBroker.hpp" 30 #include "compiler/disassembler.hpp" 31 #include "jvmci/jvmciRuntime.hpp" 32 #include "jvmci/jvmciCompilerToVM.hpp" 33 #include "jvmci/jvmciCompiler.hpp" 34 #include "jvmci/jvmciJavaClasses.hpp" 35 #include "jvmci/jvmciEnv.hpp" 36 #include "logging/log.hpp" 37 #include "memory/allocation.inline.hpp" 38 #include "memory/oopFactory.hpp" 39 #include "memory/resourceArea.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "oops/objArrayOop.inline.hpp" 42 #include "runtime/biasedLocking.hpp" 43 #include "runtime/interfaceSupport.hpp" 44 #include "runtime/jniHandles.inline.hpp" 45 #include "runtime/reflection.hpp" 46 #include "runtime/sharedRuntime.hpp" 47 #include "runtime/threadSMR.hpp" 48 #include "utilities/debug.hpp" 49 #include "utilities/defaultStream.hpp" 50 #include "utilities/macros.hpp" 51 52 #if defined(_MSC_VER) 53 #define strtoll _strtoi64 54 #endif 55 56 jobject JVMCIRuntime::_HotSpotJVMCIRuntime_instance = NULL; 57 bool JVMCIRuntime::_HotSpotJVMCIRuntime_initialized = false; 58 bool JVMCIRuntime::_well_known_classes_initialized = false; 59 int JVMCIRuntime::_trivial_prefixes_count = 0; 60 char** JVMCIRuntime::_trivial_prefixes = NULL; 61 JVMCIRuntime::CompLevelAdjustment JVMCIRuntime::_comp_level_adjustment = JVMCIRuntime::none; 62 bool JVMCIRuntime::_shutdown_called = false; 63 64 BasicType JVMCIRuntime::kindToBasicType(Handle kind, TRAPS) { 65 if (kind.is_null()) { 66 THROW_(vmSymbols::java_lang_NullPointerException(), T_ILLEGAL); 67 } 68 jchar ch = JavaKind::typeChar(kind); 69 switch(ch) { 70 case 'Z': return T_BOOLEAN; 71 case 'B': return T_BYTE; 72 case 'S': return T_SHORT; 73 case 'C': return T_CHAR; 74 case 'I': return T_INT; 75 case 'F': return T_FLOAT; 76 case 'J': return T_LONG; 77 case 'D': return T_DOUBLE; 78 case 'A': return T_OBJECT; 79 case '-': return T_ILLEGAL; 80 default: 81 JVMCI_ERROR_(T_ILLEGAL, "unexpected Kind: %c", ch); 82 } 83 } 84 85 // Simple helper to see if the caller of a runtime stub which 86 // entered the VM has been deoptimized 87 88 static bool caller_is_deopted() { 89 JavaThread* thread = JavaThread::current(); 90 RegisterMap reg_map(thread, false); 91 frame runtime_frame = thread->last_frame(); 92 frame caller_frame = runtime_frame.sender(®_map); 93 assert(caller_frame.is_compiled_frame(), "must be compiled"); 94 return caller_frame.is_deoptimized_frame(); 95 } 96 97 // Stress deoptimization 98 static void deopt_caller() { 99 if ( !caller_is_deopted()) { 100 JavaThread* thread = JavaThread::current(); 101 RegisterMap reg_map(thread, false); 102 frame runtime_frame = thread->last_frame(); 103 frame caller_frame = runtime_frame.sender(®_map); 104 Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint); 105 assert(caller_is_deopted(), "Must be deoptimized"); 106 } 107 } 108 109 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_instance(JavaThread* thread, Klass* klass)) 110 JRT_BLOCK; 111 assert(klass->is_klass(), "not a class"); 112 Handle holder(THREAD, klass->klass_holder()); // keep the klass alive 113 InstanceKlass* ik = InstanceKlass::cast(klass); 114 ik->check_valid_for_instantiation(true, CHECK); 115 // make sure klass is initialized 116 ik->initialize(CHECK); 117 // allocate instance and return via TLS 118 oop obj = ik->allocate_instance(CHECK); 119 thread->set_vm_result(obj); 120 JRT_BLOCK_END; 121 SharedRuntime::on_slowpath_allocation_exit(thread); 122 JRT_END 123 124 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_array(JavaThread* thread, Klass* array_klass, jint length)) 125 JRT_BLOCK; 126 // Note: no handle for klass needed since they are not used 127 // anymore after new_objArray() and no GC can happen before. 128 // (This may have to change if this code changes!) 129 assert(array_klass->is_klass(), "not a class"); 130 oop obj; 131 if (array_klass->is_typeArray_klass()) { 132 BasicType elt_type = TypeArrayKlass::cast(array_klass)->element_type(); 133 obj = oopFactory::new_typeArray(elt_type, length, CHECK); 134 } else { 135 Handle holder(THREAD, array_klass->klass_holder()); // keep the klass alive 136 Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass(); 137 obj = oopFactory::new_objArray(elem_klass, length, CHECK); 138 } 139 thread->set_vm_result(obj); 140 // This is pretty rare but this runtime patch is stressful to deoptimization 141 // if we deoptimize here so force a deopt to stress the path. 142 if (DeoptimizeALot) { 143 static int deopts = 0; 144 // Alternate between deoptimizing and raising an error (which will also cause a deopt) 145 if (deopts++ % 2 == 0) { 146 ResourceMark rm(THREAD); 147 THROW(vmSymbols::java_lang_OutOfMemoryError()); 148 } else { 149 deopt_caller(); 150 } 151 } 152 JRT_BLOCK_END; 153 SharedRuntime::on_slowpath_allocation_exit(thread); 154 JRT_END 155 156 JRT_ENTRY(void, JVMCIRuntime::new_multi_array(JavaThread* thread, Klass* klass, int rank, jint* dims)) 157 assert(klass->is_klass(), "not a class"); 158 assert(rank >= 1, "rank must be nonzero"); 159 Handle holder(THREAD, klass->klass_holder()); // keep the klass alive 160 oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK); 161 thread->set_vm_result(obj); 162 JRT_END 163 164 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_array(JavaThread* thread, oopDesc* element_mirror, jint length)) 165 oop obj = Reflection::reflect_new_array(element_mirror, length, CHECK); 166 thread->set_vm_result(obj); 167 JRT_END 168 169 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_instance(JavaThread* thread, oopDesc* type_mirror)) 170 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(type_mirror)); 171 172 if (klass == NULL) { 173 ResourceMark rm(THREAD); 174 THROW(vmSymbols::java_lang_InstantiationException()); 175 } 176 177 // Create new instance (the receiver) 178 klass->check_valid_for_instantiation(false, CHECK); 179 180 // Make sure klass gets initialized 181 klass->initialize(CHECK); 182 183 oop obj = klass->allocate_instance(CHECK); 184 thread->set_vm_result(obj); 185 JRT_END 186 187 extern void vm_exit(int code); 188 189 // Enter this method from compiled code handler below. This is where we transition 190 // to VM mode. This is done as a helper routine so that the method called directly 191 // from compiled code does not have to transition to VM. This allows the entry 192 // method to see if the nmethod that we have just looked up a handler for has 193 // been deoptimized while we were in the vm. This simplifies the assembly code 194 // cpu directories. 195 // 196 // We are entering here from exception stub (via the entry method below) 197 // If there is a compiled exception handler in this method, we will continue there; 198 // otherwise we will unwind the stack and continue at the caller of top frame method 199 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to 200 // control the area where we can allow a safepoint. After we exit the safepoint area we can 201 // check to see if the handler we are going to return is now in a nmethod that has 202 // been deoptimized. If that is the case we return the deopt blob 203 // unpack_with_exception entry instead. This makes life for the exception blob easier 204 // because making that same check and diverting is painful from assembly language. 205 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, CompiledMethod*& cm)) 206 // Reset method handle flag. 207 thread->set_is_method_handle_return(false); 208 209 Handle exception(thread, ex); 210 cm = CodeCache::find_compiled(pc); 211 assert(cm != NULL, "this is not a compiled method"); 212 // Adjust the pc as needed/ 213 if (cm->is_deopt_pc(pc)) { 214 RegisterMap map(thread, false); 215 frame exception_frame = thread->last_frame().sender(&map); 216 // if the frame isn't deopted then pc must not correspond to the caller of last_frame 217 assert(exception_frame.is_deoptimized_frame(), "must be deopted"); 218 pc = exception_frame.pc(); 219 } 220 #ifdef ASSERT 221 assert(exception.not_null(), "NULL exceptions should be handled by throw_exception"); 222 // Check that exception is a subclass of Throwable, otherwise we have a VerifyError 223 if (!(exception->is_a(SystemDictionary::Throwable_klass()))) { 224 if (ExitVMOnVerifyError) vm_exit(-1); 225 ShouldNotReachHere(); 226 } 227 #endif 228 229 // Check the stack guard pages and reenable them if necessary and there is 230 // enough space on the stack to do so. Use fast exceptions only if the guard 231 // pages are enabled. 232 bool guard_pages_enabled = thread->stack_guards_enabled(); 233 if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack(); 234 235 if (JvmtiExport::can_post_on_exceptions()) { 236 // To ensure correct notification of exception catches and throws 237 // we have to deoptimize here. If we attempted to notify the 238 // catches and throws during this exception lookup it's possible 239 // we could deoptimize on the way out of the VM and end back in 240 // the interpreter at the throw site. This would result in double 241 // notifications since the interpreter would also notify about 242 // these same catches and throws as it unwound the frame. 243 244 RegisterMap reg_map(thread); 245 frame stub_frame = thread->last_frame(); 246 frame caller_frame = stub_frame.sender(®_map); 247 248 // We don't really want to deoptimize the nmethod itself since we 249 // can actually continue in the exception handler ourselves but I 250 // don't see an easy way to have the desired effect. 251 Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint); 252 assert(caller_is_deopted(), "Must be deoptimized"); 253 254 return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls(); 255 } 256 257 // ExceptionCache is used only for exceptions at call sites and not for implicit exceptions 258 if (guard_pages_enabled) { 259 address fast_continuation = cm->handler_for_exception_and_pc(exception, pc); 260 if (fast_continuation != NULL) { 261 // Set flag if return address is a method handle call site. 262 thread->set_is_method_handle_return(cm->is_method_handle_return(pc)); 263 return fast_continuation; 264 } 265 } 266 267 // If the stack guard pages are enabled, check whether there is a handler in 268 // the current method. Otherwise (guard pages disabled), force an unwind and 269 // skip the exception cache update (i.e., just leave continuation==NULL). 270 address continuation = NULL; 271 if (guard_pages_enabled) { 272 273 // New exception handling mechanism can support inlined methods 274 // with exception handlers since the mappings are from PC to PC 275 276 // debugging support 277 // tracing 278 if (log_is_enabled(Info, exceptions)) { 279 ResourceMark rm; 280 stringStream tempst; 281 tempst.print("compiled method <%s>\n" 282 " at PC" INTPTR_FORMAT " for thread " INTPTR_FORMAT, 283 cm->method()->print_value_string(), p2i(pc), p2i(thread)); 284 Exceptions::log_exception(exception, tempst); 285 } 286 // for AbortVMOnException flag 287 NOT_PRODUCT(Exceptions::debug_check_abort(exception)); 288 289 // Clear out the exception oop and pc since looking up an 290 // exception handler can cause class loading, which might throw an 291 // exception and those fields are expected to be clear during 292 // normal bytecode execution. 293 thread->clear_exception_oop_and_pc(); 294 295 bool recursive_exception = false; 296 continuation = SharedRuntime::compute_compiled_exc_handler(cm, pc, exception, false, false, recursive_exception); 297 // If an exception was thrown during exception dispatch, the exception oop may have changed 298 thread->set_exception_oop(exception()); 299 thread->set_exception_pc(pc); 300 301 // the exception cache is used only by non-implicit exceptions 302 // Update the exception cache only when there didn't happen 303 // another exception during the computation of the compiled 304 // exception handler. Checking for exception oop equality is not 305 // sufficient because some exceptions are pre-allocated and reused. 306 if (continuation != NULL && !recursive_exception && !SharedRuntime::deopt_blob()->contains(continuation)) { 307 cm->add_handler_for_exception_and_pc(exception, pc, continuation); 308 } 309 } 310 311 // Set flag if return address is a method handle call site. 312 thread->set_is_method_handle_return(cm->is_method_handle_return(pc)); 313 314 if (log_is_enabled(Info, exceptions)) { 315 ResourceMark rm; 316 log_info(exceptions)("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT 317 " for exception thrown at PC " PTR_FORMAT, 318 p2i(thread), p2i(continuation), p2i(pc)); 319 } 320 321 return continuation; 322 JRT_END 323 324 // Enter this method from compiled code only if there is a Java exception handler 325 // in the method handling the exception. 326 // We are entering here from exception stub. We don't do a normal VM transition here. 327 // We do it in a helper. This is so we can check to see if the nmethod we have just 328 // searched for an exception handler has been deoptimized in the meantime. 329 address JVMCIRuntime::exception_handler_for_pc(JavaThread* thread) { 330 oop exception = thread->exception_oop(); 331 address pc = thread->exception_pc(); 332 // Still in Java mode 333 DEBUG_ONLY(ResetNoHandleMark rnhm); 334 CompiledMethod* cm = NULL; 335 address continuation = NULL; 336 { 337 // Enter VM mode by calling the helper 338 ResetNoHandleMark rnhm; 339 continuation = exception_handler_for_pc_helper(thread, exception, pc, cm); 340 } 341 // Back in JAVA, use no oops DON'T safepoint 342 343 // Now check to see if the compiled method we were called from is now deoptimized. 344 // If so we must return to the deopt blob and deoptimize the nmethod 345 if (cm != NULL && caller_is_deopted()) { 346 continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls(); 347 } 348 349 assert(continuation != NULL, "no handler found"); 350 return continuation; 351 } 352 353 JRT_ENTRY_NO_ASYNC(void, JVMCIRuntime::monitorenter(JavaThread* thread, oopDesc* obj, BasicLock* lock)) 354 IF_TRACE_jvmci_3 { 355 char type[O_BUFLEN]; 356 obj->klass()->name()->as_C_string(type, O_BUFLEN); 357 markOop mark = obj->mark(); 358 TRACE_jvmci_3("%s: entered locking slow case with obj=" INTPTR_FORMAT ", type=%s, mark=" INTPTR_FORMAT ", lock=" INTPTR_FORMAT, thread->name(), p2i(obj), type, p2i(mark), p2i(lock)); 359 tty->flush(); 360 } 361 #ifdef ASSERT 362 if (PrintBiasedLockingStatistics) { 363 Atomic::inc(BiasedLocking::slow_path_entry_count_addr()); 364 } 365 #endif 366 Handle h_obj(thread, obj); 367 if (UseBiasedLocking) { 368 // Retry fast entry if bias is revoked to avoid unnecessary inflation 369 ObjectSynchronizer::fast_enter(h_obj, lock, true, CHECK); 370 } else { 371 if (JVMCIUseFastLocking) { 372 // When using fast locking, the compiled code has already tried the fast case 373 ObjectSynchronizer::slow_enter(h_obj, lock, THREAD); 374 } else { 375 ObjectSynchronizer::fast_enter(h_obj, lock, false, THREAD); 376 } 377 } 378 TRACE_jvmci_3("%s: exiting locking slow with obj=" INTPTR_FORMAT, thread->name(), p2i(obj)); 379 JRT_END 380 381 JRT_LEAF(void, JVMCIRuntime::monitorexit(JavaThread* thread, oopDesc* obj, BasicLock* lock)) 382 assert(thread == JavaThread::current(), "threads must correspond"); 383 assert(thread->last_Java_sp(), "last_Java_sp must be set"); 384 // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown 385 EXCEPTION_MARK; 386 387 #ifdef DEBUG 388 if (!oopDesc::is_oop(obj)) { 389 ResetNoHandleMark rhm; 390 nmethod* method = thread->last_frame().cb()->as_nmethod_or_null(); 391 if (method != NULL) { 392 tty->print_cr("ERROR in monitorexit in method %s wrong obj " INTPTR_FORMAT, method->name(), p2i(obj)); 393 } 394 thread->print_stack_on(tty); 395 assert(false, "invalid lock object pointer dected"); 396 } 397 #endif 398 399 if (JVMCIUseFastLocking) { 400 // When using fast locking, the compiled code has already tried the fast case 401 ObjectSynchronizer::slow_exit(obj, lock, THREAD); 402 } else { 403 ObjectSynchronizer::fast_exit(obj, lock, THREAD); 404 } 405 IF_TRACE_jvmci_3 { 406 char type[O_BUFLEN]; 407 obj->klass()->name()->as_C_string(type, O_BUFLEN); 408 TRACE_jvmci_3("%s: exited locking slow case with obj=" INTPTR_FORMAT ", type=%s, mark=" INTPTR_FORMAT ", lock=" INTPTR_FORMAT, thread->name(), p2i(obj), type, p2i(obj->mark()), p2i(lock)); 409 tty->flush(); 410 } 411 JRT_END 412 413 JRT_ENTRY(void, JVMCIRuntime::throw_and_post_jvmti_exception(JavaThread* thread, const char* exception, const char* message)) 414 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 415 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, message); 416 JRT_END 417 418 JRT_ENTRY(void, JVMCIRuntime::throw_klass_external_name_exception(JavaThread* thread, const char* exception, Klass* klass)) 419 ResourceMark rm(thread); 420 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 421 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, klass->external_name()); 422 JRT_END 423 424 JRT_ENTRY(void, JVMCIRuntime::throw_class_cast_exception(JavaThread* thread, const char* exception, Klass* caster_klass, Klass* target_klass)) 425 ResourceMark rm(thread); 426 const char* message = SharedRuntime::generate_class_cast_message(caster_klass, target_klass); 427 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 428 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, message); 429 JRT_END 430 431 JRT_LEAF(void, JVMCIRuntime::log_object(JavaThread* thread, oopDesc* obj, bool as_string, bool newline)) 432 ttyLocker ttyl; 433 434 if (obj == NULL) { 435 tty->print("NULL"); 436 } else if (oopDesc::is_oop_or_null(obj, true) && (!as_string || !java_lang_String::is_instance(obj))) { 437 if (oopDesc::is_oop_or_null(obj, true)) { 438 char buf[O_BUFLEN]; 439 tty->print("%s@" INTPTR_FORMAT, obj->klass()->name()->as_C_string(buf, O_BUFLEN), p2i(obj)); 440 } else { 441 tty->print(INTPTR_FORMAT, p2i(obj)); 442 } 443 } else { 444 ResourceMark rm; 445 assert(obj != NULL && java_lang_String::is_instance(obj), "must be"); 446 char *buf = java_lang_String::as_utf8_string(obj); 447 tty->print_raw(buf); 448 } 449 if (newline) { 450 tty->cr(); 451 } 452 JRT_END 453 454 JRT_LEAF(void, JVMCIRuntime::write_barrier_pre(JavaThread* thread, oopDesc* obj)) 455 thread->satb_mark_queue().enqueue(obj); 456 JRT_END 457 458 JRT_LEAF(void, JVMCIRuntime::write_barrier_post(JavaThread* thread, void* card_addr)) 459 thread->dirty_card_queue().enqueue(card_addr); 460 JRT_END 461 462 JRT_LEAF(jboolean, JVMCIRuntime::validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child)) 463 bool ret = true; 464 if(!Universe::heap()->is_in_closed_subset(parent)) { 465 tty->print_cr("Parent Object " INTPTR_FORMAT " not in heap", p2i(parent)); 466 parent->print(); 467 ret=false; 468 } 469 if(!Universe::heap()->is_in_closed_subset(child)) { 470 tty->print_cr("Child Object " INTPTR_FORMAT " not in heap", p2i(child)); 471 child->print(); 472 ret=false; 473 } 474 return (jint)ret; 475 JRT_END 476 477 JRT_ENTRY(void, JVMCIRuntime::vm_error(JavaThread* thread, jlong where, jlong format, jlong value)) 478 ResourceMark rm; 479 const char *error_msg = where == 0L ? "<internal JVMCI error>" : (char*) (address) where; 480 char *detail_msg = NULL; 481 if (format != 0L) { 482 const char* buf = (char*) (address) format; 483 size_t detail_msg_length = strlen(buf) * 2; 484 detail_msg = (char *) NEW_RESOURCE_ARRAY(u_char, detail_msg_length); 485 jio_snprintf(detail_msg, detail_msg_length, buf, value); 486 report_vm_error(__FILE__, __LINE__, error_msg, "%s", detail_msg); 487 } else { 488 report_vm_error(__FILE__, __LINE__, error_msg); 489 } 490 JRT_END 491 492 JRT_LEAF(oopDesc*, JVMCIRuntime::load_and_clear_exception(JavaThread* thread)) 493 oop exception = thread->exception_oop(); 494 assert(exception != NULL, "npe"); 495 thread->set_exception_oop(NULL); 496 thread->set_exception_pc(0); 497 return exception; 498 JRT_END 499 500 PRAGMA_DIAG_PUSH 501 PRAGMA_FORMAT_NONLITERAL_IGNORED 502 JRT_LEAF(void, JVMCIRuntime::log_printf(JavaThread* thread, oopDesc* format, jlong v1, jlong v2, jlong v3)) 503 ResourceMark rm; 504 assert(format != NULL && java_lang_String::is_instance(format), "must be"); 505 char *buf = java_lang_String::as_utf8_string(format); 506 tty->print((const char*)buf, v1, v2, v3); 507 JRT_END 508 PRAGMA_DIAG_POP 509 510 static void decipher(jlong v, bool ignoreZero) { 511 if (v != 0 || !ignoreZero) { 512 void* p = (void *)(address) v; 513 CodeBlob* cb = CodeCache::find_blob(p); 514 if (cb) { 515 if (cb->is_nmethod()) { 516 char buf[O_BUFLEN]; 517 tty->print("%s [" INTPTR_FORMAT "+" JLONG_FORMAT "]", cb->as_nmethod_or_null()->method()->name_and_sig_as_C_string(buf, O_BUFLEN), p2i(cb->code_begin()), (jlong)((address)v - cb->code_begin())); 518 return; 519 } 520 cb->print_value_on(tty); 521 return; 522 } 523 if (Universe::heap()->is_in(p)) { 524 oop obj = oop(p); 525 obj->print_value_on(tty); 526 return; 527 } 528 tty->print(INTPTR_FORMAT " [long: " JLONG_FORMAT ", double %lf, char %c]",p2i((void *)v), (jlong)v, (jdouble)v, (char)v); 529 } 530 } 531 532 PRAGMA_DIAG_PUSH 533 PRAGMA_FORMAT_NONLITERAL_IGNORED 534 JRT_LEAF(void, JVMCIRuntime::vm_message(jboolean vmError, jlong format, jlong v1, jlong v2, jlong v3)) 535 ResourceMark rm; 536 const char *buf = (const char*) (address) format; 537 if (vmError) { 538 if (buf != NULL) { 539 fatal(buf, v1, v2, v3); 540 } else { 541 fatal("<anonymous error>"); 542 } 543 } else if (buf != NULL) { 544 tty->print(buf, v1, v2, v3); 545 } else { 546 assert(v2 == 0, "v2 != 0"); 547 assert(v3 == 0, "v3 != 0"); 548 decipher(v1, false); 549 } 550 JRT_END 551 PRAGMA_DIAG_POP 552 553 JRT_LEAF(void, JVMCIRuntime::log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline)) 554 union { 555 jlong l; 556 jdouble d; 557 jfloat f; 558 } uu; 559 uu.l = value; 560 switch (typeChar) { 561 case 'Z': tty->print(value == 0 ? "false" : "true"); break; 562 case 'B': tty->print("%d", (jbyte) value); break; 563 case 'C': tty->print("%c", (jchar) value); break; 564 case 'S': tty->print("%d", (jshort) value); break; 565 case 'I': tty->print("%d", (jint) value); break; 566 case 'F': tty->print("%f", uu.f); break; 567 case 'J': tty->print(JLONG_FORMAT, value); break; 568 case 'D': tty->print("%lf", uu.d); break; 569 default: assert(false, "unknown typeChar"); break; 570 } 571 if (newline) { 572 tty->cr(); 573 } 574 JRT_END 575 576 JRT_ENTRY(jint, JVMCIRuntime::identity_hash_code(JavaThread* thread, oopDesc* obj)) 577 return (jint) obj->identity_hash(); 578 JRT_END 579 580 JRT_ENTRY(jboolean, JVMCIRuntime::thread_is_interrupted(JavaThread* thread, oopDesc* receiver, jboolean clear_interrupted)) 581 Handle receiverHandle(thread, receiver); 582 // A nested ThreadsListHandle may require the Threads_lock which 583 // requires thread_in_vm which is why this method cannot be JRT_LEAF. 584 ThreadsListHandle tlh; 585 586 JavaThread* receiverThread = java_lang_Thread::thread(receiverHandle()); 587 if (receiverThread == NULL || (EnableThreadSMRExtraValidityChecks && !tlh.includes(receiverThread))) { 588 // The other thread may exit during this process, which is ok so return false. 589 return JNI_FALSE; 590 } else { 591 return (jint) Thread::is_interrupted(receiverThread, clear_interrupted != 0); 592 } 593 JRT_END 594 595 JRT_ENTRY(int, JVMCIRuntime::test_deoptimize_call_int(JavaThread* thread, int value)) 596 deopt_caller(); 597 return value; 598 JRT_END 599 600 void JVMCIRuntime::force_initialization(TRAPS) { 601 JVMCIRuntime::initialize_well_known_classes(CHECK); 602 603 ResourceMark rm; 604 TempNewSymbol getCompiler = SymbolTable::new_symbol("getCompiler", CHECK); 605 TempNewSymbol sig = SymbolTable::new_symbol("()Ljdk/vm/ci/runtime/JVMCICompiler;", CHECK); 606 Handle jvmciRuntime = JVMCIRuntime::get_HotSpotJVMCIRuntime(CHECK); 607 JavaValue result(T_OBJECT); 608 JavaCalls::call_virtual(&result, jvmciRuntime, HotSpotJVMCIRuntime::klass(), getCompiler, sig, CHECK); 609 } 610 611 // private static JVMCIRuntime JVMCI.initializeRuntime() 612 JVM_ENTRY(jobject, JVM_GetJVMCIRuntime(JNIEnv *env, jclass c)) 613 if (!EnableJVMCI) { 614 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVMCI is not enabled") 615 } 616 JVMCIRuntime::initialize_HotSpotJVMCIRuntime(CHECK_NULL); 617 jobject ret = JVMCIRuntime::get_HotSpotJVMCIRuntime_jobject(CHECK_NULL); 618 return ret; 619 JVM_END 620 621 Handle JVMCIRuntime::callStatic(const char* className, const char* methodName, const char* signature, JavaCallArguments* args, TRAPS) { 622 TempNewSymbol name = SymbolTable::new_symbol(className, CHECK_(Handle())); 623 Klass* klass = SystemDictionary::resolve_or_fail(name, true, CHECK_(Handle())); 624 TempNewSymbol runtime = SymbolTable::new_symbol(methodName, CHECK_(Handle())); 625 TempNewSymbol sig = SymbolTable::new_symbol(signature, CHECK_(Handle())); 626 JavaValue result(T_OBJECT); 627 if (args == NULL) { 628 JavaCalls::call_static(&result, klass, runtime, sig, CHECK_(Handle())); 629 } else { 630 JavaCalls::call_static(&result, klass, runtime, sig, args, CHECK_(Handle())); 631 } 632 return Handle(THREAD, (oop)result.get_jobject()); 633 } 634 635 Handle JVMCIRuntime::get_HotSpotJVMCIRuntime(TRAPS) { 636 initialize_JVMCI(CHECK_(Handle())); 637 return Handle(THREAD, JNIHandles::resolve_non_null(_HotSpotJVMCIRuntime_instance)); 638 } 639 640 void JVMCIRuntime::initialize_HotSpotJVMCIRuntime(TRAPS) { 641 guarantee(!_HotSpotJVMCIRuntime_initialized, "cannot reinitialize HotSpotJVMCIRuntime"); 642 JVMCIRuntime::initialize_well_known_classes(CHECK); 643 // This should only be called in the context of the JVMCI class being initialized 644 InstanceKlass* klass = SystemDictionary::JVMCI_klass(); 645 guarantee(klass->is_being_initialized() && klass->is_reentrant_initialization(THREAD), 646 "HotSpotJVMCIRuntime initialization should only be triggered through JVMCI initialization"); 647 648 Handle result = callStatic("jdk/vm/ci/hotspot/HotSpotJVMCIRuntime", 649 "runtime", 650 "()Ljdk/vm/ci/hotspot/HotSpotJVMCIRuntime;", NULL, CHECK); 651 objArrayOop trivial_prefixes = HotSpotJVMCIRuntime::trivialPrefixes(result); 652 if (trivial_prefixes != NULL) { 653 char** prefixes = NEW_C_HEAP_ARRAY(char*, trivial_prefixes->length(), mtCompiler); 654 for (int i = 0; i < trivial_prefixes->length(); i++) { 655 oop str = trivial_prefixes->obj_at(i); 656 if (str == NULL) { 657 THROW(vmSymbols::java_lang_NullPointerException()); 658 } else { 659 prefixes[i] = strdup(java_lang_String::as_utf8_string(str)); 660 } 661 } 662 _trivial_prefixes = prefixes; 663 _trivial_prefixes_count = trivial_prefixes->length(); 664 } 665 int adjustment = HotSpotJVMCIRuntime::compilationLevelAdjustment(result); 666 assert(adjustment >= JVMCIRuntime::none && 667 adjustment <= JVMCIRuntime::by_full_signature, 668 "compilation level adjustment out of bounds"); 669 _comp_level_adjustment = (CompLevelAdjustment) adjustment; 670 _HotSpotJVMCIRuntime_initialized = true; 671 _HotSpotJVMCIRuntime_instance = JNIHandles::make_global(result); 672 } 673 674 void JVMCIRuntime::initialize_JVMCI(TRAPS) { 675 if (JNIHandles::resolve(_HotSpotJVMCIRuntime_instance) == NULL) { 676 callStatic("jdk/vm/ci/runtime/JVMCI", 677 "getRuntime", 678 "()Ljdk/vm/ci/runtime/JVMCIRuntime;", NULL, CHECK); 679 } 680 assert(_HotSpotJVMCIRuntime_initialized == true, "what?"); 681 } 682 683 bool JVMCIRuntime::can_initialize_JVMCI() { 684 // Initializing JVMCI requires the module system to be initialized past phase 3. 685 // The JVMCI API itself isn't available until phase 2 and ServiceLoader (which 686 // JVMCI initialization requires) isn't usable until after phase 3. Testing 687 // whether the system loader is initialized satisfies all these invariants. 688 if (SystemDictionary::java_system_loader() == NULL) { 689 return false; 690 } 691 assert(Universe::is_module_initialized(), "must be"); 692 return true; 693 } 694 695 void JVMCIRuntime::initialize_well_known_classes(TRAPS) { 696 if (JVMCIRuntime::_well_known_classes_initialized == false) { 697 guarantee(can_initialize_JVMCI(), "VM is not yet sufficiently booted to initialize JVMCI"); 698 SystemDictionary::WKID scan = SystemDictionary::FIRST_JVMCI_WKID; 699 SystemDictionary::initialize_wk_klasses_through(SystemDictionary::LAST_JVMCI_WKID, scan, CHECK); 700 JVMCIJavaClasses::compute_offsets(CHECK); 701 JVMCIRuntime::_well_known_classes_initialized = true; 702 } 703 } 704 705 void JVMCIRuntime::metadata_do(void f(Metadata*)) { 706 // For simplicity, the existence of HotSpotJVMCIMetaAccessContext in 707 // the SystemDictionary well known classes should ensure the other 708 // classes have already been loaded, so make sure their order in the 709 // table enforces that. 710 assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotResolvedJavaMethodImpl) < 711 SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier"); 712 assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotConstantPool) < 713 SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier"); 714 assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotResolvedObjectTypeImpl) < 715 SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier"); 716 717 if (HotSpotJVMCIMetaAccessContext::klass() == NULL || 718 !HotSpotJVMCIMetaAccessContext::klass()->is_linked()) { 719 // Nothing could be registered yet 720 return; 721 } 722 723 // WeakReference<HotSpotJVMCIMetaAccessContext>[] 724 objArrayOop allContexts = HotSpotJVMCIMetaAccessContext::allContexts(); 725 if (allContexts == NULL) { 726 return; 727 } 728 729 // These must be loaded at this point but the linking state doesn't matter. 730 assert(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass() != NULL, "must be loaded"); 731 assert(SystemDictionary::HotSpotConstantPool_klass() != NULL, "must be loaded"); 732 assert(SystemDictionary::HotSpotResolvedObjectTypeImpl_klass() != NULL, "must be loaded"); 733 734 for (int i = 0; i < allContexts->length(); i++) { 735 oop ref = allContexts->obj_at(i); 736 if (ref != NULL) { 737 oop referent = java_lang_ref_Reference::referent(ref); 738 if (referent != NULL) { 739 // Chunked Object[] with last element pointing to next chunk 740 objArrayOop metadataRoots = HotSpotJVMCIMetaAccessContext::metadataRoots(referent); 741 while (metadataRoots != NULL) { 742 for (int typeIndex = 0; typeIndex < metadataRoots->length() - 1; typeIndex++) { 743 oop reference = metadataRoots->obj_at(typeIndex); 744 if (reference == NULL) { 745 continue; 746 } 747 oop metadataRoot = java_lang_ref_Reference::referent(reference); 748 if (metadataRoot == NULL) { 749 continue; 750 } 751 if (metadataRoot->is_a(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass())) { 752 Method* method = CompilerToVM::asMethod(metadataRoot); 753 f(method); 754 } else if (metadataRoot->is_a(SystemDictionary::HotSpotConstantPool_klass())) { 755 ConstantPool* constantPool = CompilerToVM::asConstantPool(metadataRoot); 756 f(constantPool); 757 } else if (metadataRoot->is_a(SystemDictionary::HotSpotResolvedObjectTypeImpl_klass())) { 758 Klass* klass = CompilerToVM::asKlass(metadataRoot); 759 f(klass); 760 } else { 761 metadataRoot->print(); 762 ShouldNotReachHere(); 763 } 764 } 765 metadataRoots = (objArrayOop)metadataRoots->obj_at(metadataRoots->length() - 1); 766 assert(metadataRoots == NULL || metadataRoots->is_objArray(), "wrong type"); 767 } 768 } 769 } 770 } 771 } 772 773 // private static void CompilerToVM.registerNatives() 774 JVM_ENTRY(void, JVM_RegisterJVMCINatives(JNIEnv *env, jclass c2vmClass)) 775 if (!EnableJVMCI) { 776 THROW_MSG(vmSymbols::java_lang_InternalError(), "JVMCI is not enabled"); 777 } 778 779 #ifdef _LP64 780 #ifndef SPARC 781 uintptr_t heap_end = (uintptr_t) Universe::heap()->reserved_region().end(); 782 uintptr_t allocation_end = heap_end + ((uintptr_t)16) * 1024 * 1024 * 1024; 783 guarantee(heap_end < allocation_end, "heap end too close to end of address space (might lead to erroneous TLAB allocations)"); 784 #endif // !SPARC 785 #else 786 fatal("check TLAB allocation code for address space conflicts"); 787 #endif // _LP64 788 789 JVMCIRuntime::initialize_well_known_classes(CHECK); 790 791 { 792 ThreadToNativeFromVM trans(thread); 793 env->RegisterNatives(c2vmClass, CompilerToVM::methods, CompilerToVM::methods_count()); 794 } 795 JVM_END 796 797 void JVMCIRuntime::shutdown(TRAPS) { 798 if (_HotSpotJVMCIRuntime_instance != NULL) { 799 _shutdown_called = true; 800 HandleMark hm(THREAD); 801 Handle receiver = get_HotSpotJVMCIRuntime(CHECK); 802 JavaValue result(T_VOID); 803 JavaCallArguments args; 804 args.push_oop(receiver); 805 JavaCalls::call_special(&result, receiver->klass(), vmSymbols::shutdown_method_name(), vmSymbols::void_method_signature(), &args, CHECK); 806 } 807 } 808 809 CompLevel JVMCIRuntime::adjust_comp_level_inner(const methodHandle& method, bool is_osr, CompLevel level, JavaThread* thread) { 810 JVMCICompiler* compiler = JVMCICompiler::instance(false, thread); 811 if (compiler != NULL && compiler->is_bootstrapping()) { 812 return level; 813 } 814 if (!is_HotSpotJVMCIRuntime_initialized() || _comp_level_adjustment == JVMCIRuntime::none) { 815 // JVMCI cannot participate in compilation scheduling until 816 // JVMCI is initialized and indicates it wants to participate. 817 return level; 818 } 819 820 #define CHECK_RETURN THREAD); \ 821 if (HAS_PENDING_EXCEPTION) { \ 822 Handle exception(THREAD, PENDING_EXCEPTION); \ 823 CLEAR_PENDING_EXCEPTION; \ 824 \ 825 if (exception->is_a(SystemDictionary::ThreadDeath_klass())) { \ 826 /* In the special case of ThreadDeath, we need to reset the */ \ 827 /* pending async exception so that it is propagated. */ \ 828 thread->set_pending_async_exception(exception()); \ 829 return level; \ 830 } \ 831 tty->print("Uncaught exception while adjusting compilation level: "); \ 832 java_lang_Throwable::print(exception(), tty); \ 833 tty->cr(); \ 834 java_lang_Throwable::print_stack_trace(exception, tty); \ 835 if (HAS_PENDING_EXCEPTION) { \ 836 CLEAR_PENDING_EXCEPTION; \ 837 } \ 838 return level; \ 839 } \ 840 (void)(0 841 842 843 Thread* THREAD = thread; 844 HandleMark hm; 845 Handle receiver = JVMCIRuntime::get_HotSpotJVMCIRuntime(CHECK_RETURN); 846 Handle name; 847 Handle sig; 848 if (_comp_level_adjustment == JVMCIRuntime::by_full_signature) { 849 name = java_lang_String::create_from_symbol(method->name(), CHECK_RETURN); 850 sig = java_lang_String::create_from_symbol(method->signature(), CHECK_RETURN); 851 } else { 852 name = Handle(); 853 sig = Handle(); 854 } 855 856 JavaValue result(T_INT); 857 JavaCallArguments args; 858 args.push_oop(receiver); 859 args.push_oop(Handle(THREAD, method->method_holder()->java_mirror())); 860 args.push_oop(name); 861 args.push_oop(sig); 862 args.push_int(is_osr); 863 args.push_int(level); 864 JavaCalls::call_special(&result, receiver->klass(), vmSymbols::adjustCompilationLevel_name(), 865 vmSymbols::adjustCompilationLevel_signature(), &args, CHECK_RETURN); 866 867 int comp_level = result.get_jint(); 868 if (comp_level < CompLevel_none || comp_level > CompLevel_full_optimization) { 869 assert(false, "compilation level out of bounds"); 870 return level; 871 } 872 return (CompLevel) comp_level; 873 #undef CHECK_RETURN 874 } 875 876 void JVMCIRuntime::bootstrap_finished(TRAPS) { 877 HandleMark hm(THREAD); 878 Handle receiver = get_HotSpotJVMCIRuntime(CHECK); 879 JavaValue result(T_VOID); 880 JavaCallArguments args; 881 args.push_oop(receiver); 882 JavaCalls::call_special(&result, receiver->klass(), vmSymbols::bootstrapFinished_method_name(), vmSymbols::void_method_signature(), &args, CHECK); 883 } 884 885 bool JVMCIRuntime::treat_as_trivial(Method* method) { 886 if (_HotSpotJVMCIRuntime_initialized) { 887 for (int i = 0; i < _trivial_prefixes_count; i++) { 888 if (method->method_holder()->name()->starts_with(_trivial_prefixes[i])) { 889 return true; 890 } 891 } 892 } 893 return false; 894 }