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