1 /* 2 * Copyright (c) 2012, 2017, 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 "asm/codeBuffer.hpp" 26 #include "classfile/javaClasses.inline.hpp" 27 #include "code/codeCache.hpp" 28 #include "compiler/compileBroker.hpp" 29 #include "compiler/disassembler.hpp" 30 #include "jvmci/jvmciRuntime.hpp" 31 #include "jvmci/jvmciCompilerToVM.hpp" 32 #include "jvmci/jvmciCompiler.hpp" 33 #include "jvmci/jvmciJavaClasses.hpp" 34 #include "jvmci/jvmciEnv.hpp" 35 #include "logging/log.hpp" 36 #include "memory/oopFactory.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "oops/objArrayOop.inline.hpp" 40 #include "prims/jvm.h" 41 #include "runtime/biasedLocking.hpp" 42 #include "runtime/interfaceSupport.hpp" 43 #include "runtime/reflection.hpp" 44 #include "runtime/sharedRuntime.hpp" 45 #include "utilities/debug.hpp" 46 #include "utilities/defaultStream.hpp" 47 #include "utilities/macros.hpp" 48 49 #if defined(_MSC_VER) 50 #define strtoll _strtoi64 51 #endif 52 53 jobject JVMCIRuntime::_HotSpotJVMCIRuntime_instance = NULL; 54 bool JVMCIRuntime::_HotSpotJVMCIRuntime_initialized = false; 55 bool JVMCIRuntime::_well_known_classes_initialized = false; 56 int JVMCIRuntime::_trivial_prefixes_count = 0; 57 char** JVMCIRuntime::_trivial_prefixes = NULL; 58 JVMCIRuntime::CompLevelAdjustment JVMCIRuntime::_comp_level_adjustment = JVMCIRuntime::none; 59 bool JVMCIRuntime::_shutdown_called = false; 60 61 BasicType JVMCIRuntime::kindToBasicType(Handle kind, TRAPS) { 62 if (kind.is_null()) { 63 THROW_(vmSymbols::java_lang_NullPointerException(), T_ILLEGAL); 64 } 65 jchar ch = JavaKind::typeChar(kind); 66 switch(ch) { 67 case 'Z': return T_BOOLEAN; 68 case 'B': return T_BYTE; 69 case 'S': return T_SHORT; 70 case 'C': return T_CHAR; 71 case 'I': return T_INT; 72 case 'F': return T_FLOAT; 73 case 'J': return T_LONG; 74 case 'D': return T_DOUBLE; 75 case 'A': return T_OBJECT; 76 case '-': return T_ILLEGAL; 77 default: 78 JVMCI_ERROR_(T_ILLEGAL, "unexpected Kind: %c", ch); 79 } 80 } 81 82 // Simple helper to see if the caller of a runtime stub which 83 // entered the VM has been deoptimized 84 85 static bool caller_is_deopted() { 86 JavaThread* thread = JavaThread::current(); 87 RegisterMap reg_map(thread, false); 88 frame runtime_frame = thread->last_frame(); 89 frame caller_frame = runtime_frame.sender(®_map); 90 assert(caller_frame.is_compiled_frame(), "must be compiled"); 91 return caller_frame.is_deoptimized_frame(); 92 } 93 94 // Stress deoptimization 95 static void deopt_caller() { 96 if ( !caller_is_deopted()) { 97 JavaThread* thread = JavaThread::current(); 98 RegisterMap reg_map(thread, false); 99 frame runtime_frame = thread->last_frame(); 100 frame caller_frame = runtime_frame.sender(®_map); 101 Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint); 102 assert(caller_is_deopted(), "Must be deoptimized"); 103 } 104 } 105 106 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_instance(JavaThread* thread, Klass* klass)) 107 JRT_BLOCK; 108 assert(klass->is_klass(), "not a class"); 109 Handle holder(THREAD, klass->klass_holder()); // keep the klass alive 110 InstanceKlass* ik = InstanceKlass::cast(klass); 111 ik->check_valid_for_instantiation(true, CHECK); 112 // make sure klass is initialized 113 ik->initialize(CHECK); 114 // allocate instance and return via TLS 115 oop obj = ik->allocate_instance(CHECK); 116 thread->set_vm_result(obj); 117 JRT_BLOCK_END; 118 119 SharedRuntime::on_slowpath_allocation(thread); 120 JRT_END 121 122 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_array(JavaThread* thread, Klass* array_klass, jint length)) 123 JRT_BLOCK; 124 // Note: no handle for klass needed since they are not used 125 // anymore after new_objArray() and no GC can happen before. 126 // (This may have to change if this code changes!) 127 assert(array_klass->is_klass(), "not a class"); 128 oop obj; 129 if (array_klass->is_typeArray_klass()) { 130 BasicType elt_type = TypeArrayKlass::cast(array_klass)->element_type(); 131 obj = oopFactory::new_typeArray(elt_type, length, CHECK); 132 } else { 133 Handle holder(THREAD, array_klass->klass_holder()); // keep the klass alive 134 Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass(); 135 obj = oopFactory::new_objArray(elem_klass, length, CHECK); 136 } 137 thread->set_vm_result(obj); 138 // This is pretty rare but this runtime patch is stressful to deoptimization 139 // if we deoptimize here so force a deopt to stress the path. 140 if (DeoptimizeALot) { 141 static int deopts = 0; 142 // Alternate between deoptimizing and raising an error (which will also cause a deopt) 143 if (deopts++ % 2 == 0) { 144 ResourceMark rm(THREAD); 145 THROW(vmSymbols::java_lang_OutOfMemoryError()); 146 } else { 147 deopt_caller(); 148 } 149 } 150 JRT_BLOCK_END; 151 152 SharedRuntime::on_slowpath_allocation(thread); 153 JRT_END 154 155 JRT_ENTRY(void, JVMCIRuntime::new_multi_array(JavaThread* thread, Klass* klass, int rank, jint* dims)) 156 assert(klass->is_klass(), "not a class"); 157 assert(rank >= 1, "rank must be nonzero"); 158 Handle holder(THREAD, klass->klass_holder()); // keep the klass alive 159 oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK); 160 thread->set_vm_result(obj); 161 JRT_END 162 163 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_array(JavaThread* thread, oopDesc* element_mirror, jint length)) 164 oop obj = Reflection::reflect_new_array(element_mirror, length, CHECK); 165 thread->set_vm_result(obj); 166 JRT_END 167 168 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_instance(JavaThread* thread, oopDesc* type_mirror)) 169 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(type_mirror)); 170 171 if (klass == NULL) { 172 ResourceMark rm(THREAD); 173 THROW(vmSymbols::java_lang_InstantiationException()); 174 } 175 176 // Create new instance (the receiver) 177 klass->check_valid_for_instantiation(false, CHECK); 178 179 // Make sure klass gets initialized 180 klass->initialize(CHECK); 181 182 oop obj = klass->allocate_instance(CHECK); 183 thread->set_vm_result(obj); 184 JRT_END 185 186 extern void vm_exit(int code); 187 188 // Enter this method from compiled code handler below. This is where we transition 189 // to VM mode. This is done as a helper routine so that the method called directly 190 // from compiled code does not have to transition to VM. This allows the entry 191 // method to see if the nmethod that we have just looked up a handler for has 192 // been deoptimized while we were in the vm. This simplifies the assembly code 193 // cpu directories. 194 // 195 // We are entering here from exception stub (via the entry method below) 196 // If there is a compiled exception handler in this method, we will continue there; 197 // otherwise we will unwind the stack and continue at the caller of top frame method 198 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to 199 // control the area where we can allow a safepoint. After we exit the safepoint area we can 200 // check to see if the handler we are going to return is now in a nmethod that has 201 // been deoptimized. If that is the case we return the deopt blob 202 // unpack_with_exception entry instead. This makes life for the exception blob easier 203 // because making that same check and diverting is painful from assembly language. 204 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, CompiledMethod*& cm)) 205 // Reset method handle flag. 206 thread->set_is_method_handle_return(false); 207 208 Handle exception(thread, ex); 209 cm = CodeCache::find_compiled(pc); 210 assert(cm != NULL, "this is not a compiled method"); 211 // Adjust the pc as needed/ 212 if (cm->is_deopt_pc(pc)) { 213 RegisterMap map(thread, false); 214 frame exception_frame = thread->last_frame().sender(&map); 215 // if the frame isn't deopted then pc must not correspond to the caller of last_frame 216 assert(exception_frame.is_deoptimized_frame(), "must be deopted"); 217 pc = exception_frame.pc(); 218 } 219 #ifdef ASSERT 220 assert(exception.not_null(), "NULL exceptions should be handled by throw_exception"); 221 assert(exception->is_oop(), "just checking"); 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 assert(h_obj()->is_oop(), "must be NULL or an object"); 368 if (UseBiasedLocking) { 369 // Retry fast entry if bias is revoked to avoid unnecessary inflation 370 ObjectSynchronizer::fast_enter(h_obj, lock, true, CHECK); 371 } else { 372 if (JVMCIUseFastLocking) { 373 // When using fast locking, the compiled code has already tried the fast case 374 ObjectSynchronizer::slow_enter(h_obj, lock, THREAD); 375 } else { 376 ObjectSynchronizer::fast_enter(h_obj, lock, false, THREAD); 377 } 378 } 379 TRACE_jvmci_3("%s: exiting locking slow with obj=" INTPTR_FORMAT, thread->name(), p2i(obj)); 380 JRT_END 381 382 JRT_LEAF(void, JVMCIRuntime::monitorexit(JavaThread* thread, oopDesc* obj, BasicLock* lock)) 383 assert(thread == JavaThread::current(), "threads must correspond"); 384 assert(thread->last_Java_sp(), "last_Java_sp must be set"); 385 // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown 386 EXCEPTION_MARK; 387 388 #ifdef DEBUG 389 if (!obj->is_oop()) { 390 ResetNoHandleMark rhm; 391 nmethod* method = thread->last_frame().cb()->as_nmethod_or_null(); 392 if (method != NULL) { 393 tty->print_cr("ERROR in monitorexit in method %s wrong obj " INTPTR_FORMAT, method->name(), p2i(obj)); 394 } 395 thread->print_stack_on(tty); 396 assert(false, "invalid lock object pointer dected"); 397 } 398 #endif 399 400 if (JVMCIUseFastLocking) { 401 // When using fast locking, the compiled code has already tried the fast case 402 ObjectSynchronizer::slow_exit(obj, lock, THREAD); 403 } else { 404 ObjectSynchronizer::fast_exit(obj, lock, THREAD); 405 } 406 IF_TRACE_jvmci_3 { 407 char type[O_BUFLEN]; 408 obj->klass()->name()->as_C_string(type, O_BUFLEN); 409 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)); 410 tty->flush(); 411 } 412 JRT_END 413 414 JRT_ENTRY(void, JVMCIRuntime::throw_and_post_jvmti_exception(JavaThread* thread, const char* exception, const char* message)) 415 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 416 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, message); 417 JRT_END 418 419 JRT_ENTRY(void, JVMCIRuntime::throw_klass_external_name_exception(JavaThread* thread, const char* exception, Klass* klass)) 420 ResourceMark rm(thread); 421 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 422 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, klass->external_name()); 423 JRT_END 424 425 JRT_ENTRY(void, JVMCIRuntime::throw_class_cast_exception(JavaThread* thread, const char* exception, Klass* caster_klass, Klass* target_klass)) 426 ResourceMark rm(thread); 427 const char* message = SharedRuntime::generate_class_cast_message(caster_klass, target_klass); 428 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 429 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, message); 430 JRT_END 431 432 JRT_LEAF(void, JVMCIRuntime::log_object(JavaThread* thread, oopDesc* obj, bool as_string, bool newline)) 433 ttyLocker ttyl; 434 435 if (obj == NULL) { 436 tty->print("NULL"); 437 } else if (obj->is_oop_or_null(true) && (!as_string || !java_lang_String::is_instance(obj))) { 438 if (obj->is_oop_or_null(true)) { 439 char buf[O_BUFLEN]; 440 tty->print("%s@" INTPTR_FORMAT, obj->klass()->name()->as_C_string(buf, O_BUFLEN), p2i(obj)); 441 } else { 442 tty->print(INTPTR_FORMAT, p2i(obj)); 443 } 444 } else { 445 ResourceMark rm; 446 assert(obj != NULL && java_lang_String::is_instance(obj), "must be"); 447 char *buf = java_lang_String::as_utf8_string(obj); 448 tty->print_raw(buf); 449 } 450 if (newline) { 451 tty->cr(); 452 } 453 JRT_END 454 455 JRT_LEAF(void, JVMCIRuntime::write_barrier_pre(JavaThread* thread, oopDesc* obj)) 456 thread->satb_mark_queue().enqueue(obj); 457 JRT_END 458 459 JRT_LEAF(void, JVMCIRuntime::write_barrier_post(JavaThread* thread, void* card_addr)) 460 thread->dirty_card_queue().enqueue(card_addr); 461 JRT_END 462 463 JRT_LEAF(jboolean, JVMCIRuntime::validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child)) 464 bool ret = true; 465 if(!Universe::heap()->is_in_closed_subset(parent)) { 466 tty->print_cr("Parent Object " INTPTR_FORMAT " not in heap", p2i(parent)); 467 parent->print(); 468 ret=false; 469 } 470 if(!Universe::heap()->is_in_closed_subset(child)) { 471 tty->print_cr("Child Object " INTPTR_FORMAT " not in heap", p2i(child)); 472 child->print(); 473 ret=false; 474 } 475 return (jint)ret; 476 JRT_END 477 478 JRT_ENTRY(void, JVMCIRuntime::vm_error(JavaThread* thread, jlong where, jlong format, jlong value)) 479 ResourceMark rm; 480 const char *error_msg = where == 0L ? "<internal JVMCI error>" : (char*) (address) where; 481 char *detail_msg = NULL; 482 if (format != 0L) { 483 const char* buf = (char*) (address) format; 484 size_t detail_msg_length = strlen(buf) * 2; 485 detail_msg = (char *) NEW_RESOURCE_ARRAY(u_char, detail_msg_length); 486 jio_snprintf(detail_msg, detail_msg_length, buf, value); 487 report_vm_error(__FILE__, __LINE__, error_msg, "%s", detail_msg); 488 } else { 489 report_vm_error(__FILE__, __LINE__, error_msg); 490 } 491 JRT_END 492 493 JRT_LEAF(oopDesc*, JVMCIRuntime::load_and_clear_exception(JavaThread* thread)) 494 oop exception = thread->exception_oop(); 495 assert(exception != NULL, "npe"); 496 thread->set_exception_oop(NULL); 497 thread->set_exception_pc(0); 498 return exception; 499 JRT_END 500 501 PRAGMA_DIAG_PUSH 502 PRAGMA_FORMAT_NONLITERAL_IGNORED 503 JRT_LEAF(void, JVMCIRuntime::log_printf(JavaThread* thread, oopDesc* format, jlong v1, jlong v2, jlong v3)) 504 ResourceMark rm; 505 assert(format != NULL && java_lang_String::is_instance(format), "must be"); 506 char *buf = java_lang_String::as_utf8_string(format); 507 tty->print((const char*)buf, v1, v2, v3); 508 JRT_END 509 PRAGMA_DIAG_POP 510 511 static void decipher(jlong v, bool ignoreZero) { 512 if (v != 0 || !ignoreZero) { 513 void* p = (void *)(address) v; 514 CodeBlob* cb = CodeCache::find_blob(p); 515 if (cb) { 516 if (cb->is_nmethod()) { 517 char buf[O_BUFLEN]; 518 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())); 519 return; 520 } 521 cb->print_value_on(tty); 522 return; 523 } 524 if (Universe::heap()->is_in(p)) { 525 oop obj = oop(p); 526 obj->print_value_on(tty); 527 return; 528 } 529 tty->print(INTPTR_FORMAT " [long: " JLONG_FORMAT ", double %lf, char %c]",p2i((void *)v), (jlong)v, (jdouble)v, (char)v); 530 } 531 } 532 533 PRAGMA_DIAG_PUSH 534 PRAGMA_FORMAT_NONLITERAL_IGNORED 535 JRT_LEAF(void, JVMCIRuntime::vm_message(jboolean vmError, jlong format, jlong v1, jlong v2, jlong v3)) 536 ResourceMark rm; 537 const char *buf = (const char*) (address) format; 538 if (vmError) { 539 if (buf != NULL) { 540 fatal(buf, v1, v2, v3); 541 } else { 542 fatal("<anonymous error>"); 543 } 544 } else if (buf != NULL) { 545 tty->print(buf, v1, v2, v3); 546 } else { 547 assert(v2 == 0, "v2 != 0"); 548 assert(v3 == 0, "v3 != 0"); 549 decipher(v1, false); 550 } 551 JRT_END 552 PRAGMA_DIAG_POP 553 554 JRT_LEAF(void, JVMCIRuntime::log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline)) 555 union { 556 jlong l; 557 jdouble d; 558 jfloat f; 559 } uu; 560 uu.l = value; 561 switch (typeChar) { 562 case 'Z': tty->print(value == 0 ? "false" : "true"); break; 563 case 'B': tty->print("%d", (jbyte) value); break; 564 case 'C': tty->print("%c", (jchar) value); break; 565 case 'S': tty->print("%d", (jshort) value); break; 566 case 'I': tty->print("%d", (jint) value); break; 567 case 'F': tty->print("%f", uu.f); break; 568 case 'J': tty->print(JLONG_FORMAT, value); break; 569 case 'D': tty->print("%lf", uu.d); break; 570 default: assert(false, "unknown typeChar"); break; 571 } 572 if (newline) { 573 tty->cr(); 574 } 575 JRT_END 576 577 JRT_ENTRY(jint, JVMCIRuntime::identity_hash_code(JavaThread* thread, oopDesc* obj)) 578 return (jint) obj->identity_hash(); 579 JRT_END 580 581 JRT_ENTRY(jboolean, JVMCIRuntime::thread_is_interrupted(JavaThread* thread, oopDesc* receiver, jboolean clear_interrupted)) 582 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate. 583 // This locking requires thread_in_vm which is why this method cannot be JRT_LEAF. 584 Handle receiverHandle(thread, receiver); 585 MutexLockerEx ml(thread->threadObj() == (void*)receiver ? NULL : Threads_lock); 586 JavaThread* receiverThread = java_lang_Thread::thread(receiverHandle()); 587 if (receiverThread == NULL) { 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(jint, 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 void JVMCIRuntime::initialize_HotSpotJVMCIRuntime(TRAPS) { 636 guarantee(!_HotSpotJVMCIRuntime_initialized, "cannot reinitialize HotSpotJVMCIRuntime"); 637 JVMCIRuntime::initialize_well_known_classes(CHECK); 638 // This should only be called in the context of the JVMCI class being initialized 639 InstanceKlass* klass = SystemDictionary::JVMCI_klass(); 640 guarantee(klass->is_being_initialized() && klass->is_reentrant_initialization(THREAD), 641 "HotSpotJVMCIRuntime initialization should only be triggered through JVMCI initialization"); 642 643 Handle result = callStatic("jdk/vm/ci/hotspot/HotSpotJVMCIRuntime", 644 "runtime", 645 "()Ljdk/vm/ci/hotspot/HotSpotJVMCIRuntime;", NULL, CHECK); 646 objArrayOop trivial_prefixes = HotSpotJVMCIRuntime::trivialPrefixes(result); 647 if (trivial_prefixes != NULL) { 648 char** prefixes = NEW_C_HEAP_ARRAY(char*, trivial_prefixes->length(), mtCompiler); 649 for (int i = 0; i < trivial_prefixes->length(); i++) { 650 oop str = trivial_prefixes->obj_at(i); 651 if (str == NULL) { 652 THROW(vmSymbols::java_lang_NullPointerException()); 653 } else { 654 prefixes[i] = strdup(java_lang_String::as_utf8_string(str)); 655 } 656 } 657 _trivial_prefixes = prefixes; 658 _trivial_prefixes_count = trivial_prefixes->length(); 659 } 660 int adjustment = HotSpotJVMCIRuntime::compilationLevelAdjustment(result); 661 assert(adjustment >= JVMCIRuntime::none && 662 adjustment <= JVMCIRuntime::by_full_signature, 663 "compilation level adjustment out of bounds"); 664 _comp_level_adjustment = (CompLevelAdjustment) adjustment; 665 _HotSpotJVMCIRuntime_initialized = true; 666 _HotSpotJVMCIRuntime_instance = JNIHandles::make_global(result); 667 } 668 669 void JVMCIRuntime::initialize_JVMCI(TRAPS) { 670 if (JNIHandles::resolve(_HotSpotJVMCIRuntime_instance) == NULL) { 671 callStatic("jdk/vm/ci/runtime/JVMCI", 672 "getRuntime", 673 "()Ljdk/vm/ci/runtime/JVMCIRuntime;", NULL, CHECK); 674 } 675 assert(_HotSpotJVMCIRuntime_initialized == true, "what?"); 676 } 677 678 bool JVMCIRuntime::can_initialize_JVMCI() { 679 // Initializing JVMCI requires the module system to be initialized past phase 3. 680 // The JVMCI API itself isn't available until phase 2 and ServiceLoader (which 681 // JVMCI initialization requires) isn't usable until after phase 3. Testing 682 // whether the system loader is initialized satisfies all these invariants. 683 if (SystemDictionary::java_system_loader() == NULL) { 684 return false; 685 } 686 assert(Universe::is_module_initialized(), "must be"); 687 return true; 688 } 689 690 void JVMCIRuntime::initialize_well_known_classes(TRAPS) { 691 if (JVMCIRuntime::_well_known_classes_initialized == false) { 692 guarantee(can_initialize_JVMCI(), "VM is not yet sufficiently booted to initialize JVMCI"); 693 SystemDictionary::WKID scan = SystemDictionary::FIRST_JVMCI_WKID; 694 SystemDictionary::initialize_wk_klasses_through(SystemDictionary::LAST_JVMCI_WKID, scan, CHECK); 695 JVMCIJavaClasses::compute_offsets(CHECK); 696 JVMCIRuntime::_well_known_classes_initialized = true; 697 } 698 } 699 700 void JVMCIRuntime::metadata_do(void f(Metadata*)) { 701 // For simplicity, the existence of HotSpotJVMCIMetaAccessContext in 702 // the SystemDictionary well known classes should ensure the other 703 // classes have already been loaded, so make sure their order in the 704 // table enforces that. 705 assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotResolvedJavaMethodImpl) < 706 SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier"); 707 assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotConstantPool) < 708 SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier"); 709 assert(SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotResolvedObjectTypeImpl) < 710 SystemDictionary::WK_KLASS_ENUM_NAME(jdk_vm_ci_hotspot_HotSpotJVMCIMetaAccessContext), "must be loaded earlier"); 711 712 if (HotSpotJVMCIMetaAccessContext::klass() == NULL || 713 !HotSpotJVMCIMetaAccessContext::klass()->is_linked()) { 714 // Nothing could be registered yet 715 return; 716 } 717 718 // WeakReference<HotSpotJVMCIMetaAccessContext>[] 719 objArrayOop allContexts = HotSpotJVMCIMetaAccessContext::allContexts(); 720 if (allContexts == NULL) { 721 return; 722 } 723 724 // These must be loaded at this point but the linking state doesn't matter. 725 assert(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass() != NULL, "must be loaded"); 726 assert(SystemDictionary::HotSpotConstantPool_klass() != NULL, "must be loaded"); 727 assert(SystemDictionary::HotSpotResolvedObjectTypeImpl_klass() != NULL, "must be loaded"); 728 729 for (int i = 0; i < allContexts->length(); i++) { 730 oop ref = allContexts->obj_at(i); 731 if (ref != NULL) { 732 oop referent = java_lang_ref_Reference::referent(ref); 733 if (referent != NULL) { 734 // Chunked Object[] with last element pointing to next chunk 735 objArrayOop metadataRoots = HotSpotJVMCIMetaAccessContext::metadataRoots(referent); 736 while (metadataRoots != NULL) { 737 for (int typeIndex = 0; typeIndex < metadataRoots->length() - 1; typeIndex++) { 738 oop reference = metadataRoots->obj_at(typeIndex); 739 if (reference == NULL) { 740 continue; 741 } 742 oop metadataRoot = java_lang_ref_Reference::referent(reference); 743 if (metadataRoot == NULL) { 744 continue; 745 } 746 if (metadataRoot->is_a(SystemDictionary::HotSpotResolvedJavaMethodImpl_klass())) { 747 Method* method = CompilerToVM::asMethod(metadataRoot); 748 f(method); 749 } else if (metadataRoot->is_a(SystemDictionary::HotSpotConstantPool_klass())) { 750 ConstantPool* constantPool = CompilerToVM::asConstantPool(metadataRoot); 751 f(constantPool); 752 } else if (metadataRoot->is_a(SystemDictionary::HotSpotResolvedObjectTypeImpl_klass())) { 753 Klass* klass = CompilerToVM::asKlass(metadataRoot); 754 f(klass); 755 } else { 756 metadataRoot->print(); 757 ShouldNotReachHere(); 758 } 759 } 760 metadataRoots = (objArrayOop)metadataRoots->obj_at(metadataRoots->length() - 1); 761 assert(metadataRoots == NULL || metadataRoots->is_objArray(), "wrong type"); 762 } 763 } 764 } 765 } 766 } 767 768 // private static void CompilerToVM.registerNatives() 769 JVM_ENTRY(void, JVM_RegisterJVMCINatives(JNIEnv *env, jclass c2vmClass)) 770 if (!EnableJVMCI) { 771 THROW_MSG(vmSymbols::java_lang_InternalError(), "JVMCI is not enabled"); 772 } 773 774 #ifdef _LP64 775 #ifndef SPARC 776 uintptr_t heap_end = (uintptr_t) Universe::heap()->reserved_region().end(); 777 uintptr_t allocation_end = heap_end + ((uintptr_t)16) * 1024 * 1024 * 1024; 778 guarantee(heap_end < allocation_end, "heap end too close to end of address space (might lead to erroneous TLAB allocations)"); 779 #endif // !SPARC 780 #else 781 fatal("check TLAB allocation code for address space conflicts"); 782 #endif // _LP64 783 784 JVMCIRuntime::initialize_well_known_classes(CHECK); 785 786 { 787 ThreadToNativeFromVM trans(thread); 788 env->RegisterNatives(c2vmClass, CompilerToVM::methods, CompilerToVM::methods_count()); 789 } 790 JVM_END 791 792 void JVMCIRuntime::shutdown(TRAPS) { 793 if (_HotSpotJVMCIRuntime_instance != NULL) { 794 _shutdown_called = true; 795 HandleMark hm(THREAD); 796 Handle receiver = get_HotSpotJVMCIRuntime(CHECK); 797 JavaValue result(T_VOID); 798 JavaCallArguments args; 799 args.push_oop(receiver); 800 JavaCalls::call_special(&result, receiver->klass(), vmSymbols::shutdown_method_name(), vmSymbols::void_method_signature(), &args, CHECK); 801 } 802 } 803 804 CompLevel JVMCIRuntime::adjust_comp_level_inner(methodHandle method, bool is_osr, CompLevel level, JavaThread* thread) { 805 JVMCICompiler* compiler = JVMCICompiler::instance(thread); 806 if (compiler != NULL && compiler->is_bootstrapping()) { 807 return level; 808 } 809 if (!is_HotSpotJVMCIRuntime_initialized() || !_comp_level_adjustment) { 810 // JVMCI cannot participate in compilation scheduling until 811 // JVMCI is initialized and indicates it wants to participate. 812 return level; 813 } 814 815 #define CHECK_RETURN THREAD); \ 816 if (HAS_PENDING_EXCEPTION) { \ 817 Handle exception(THREAD, PENDING_EXCEPTION); \ 818 CLEAR_PENDING_EXCEPTION; \ 819 \ 820 java_lang_Throwable::java_printStackTrace(exception, THREAD); \ 821 if (HAS_PENDING_EXCEPTION) { \ 822 CLEAR_PENDING_EXCEPTION; \ 823 } \ 824 return level; \ 825 } \ 826 (void)(0 827 828 829 Thread* THREAD = thread; 830 HandleMark hm; 831 Handle receiver = JVMCIRuntime::get_HotSpotJVMCIRuntime(CHECK_RETURN); 832 Handle name; 833 Handle sig; 834 if (_comp_level_adjustment == JVMCIRuntime::by_full_signature) { 835 name = java_lang_String::create_from_symbol(method->name(), CHECK_RETURN); 836 sig = java_lang_String::create_from_symbol(method->signature(), CHECK_RETURN); 837 } else { 838 name = Handle(); 839 sig = Handle(); 840 } 841 842 JavaValue result(T_INT); 843 JavaCallArguments args; 844 args.push_oop(receiver); 845 args.push_oop(Handle(THREAD, method->method_holder()->java_mirror())); 846 args.push_oop(name); 847 args.push_oop(sig); 848 args.push_int(is_osr); 849 args.push_int(level); 850 JavaCalls::call_special(&result, receiver->klass(), vmSymbols::adjustCompilationLevel_name(), 851 vmSymbols::adjustCompilationLevel_signature(), &args, CHECK_RETURN); 852 853 int comp_level = result.get_jint(); 854 if (comp_level < CompLevel_none || comp_level > CompLevel_full_optimization) { 855 assert(false, "compilation level out of bounds"); 856 return level; 857 } 858 return (CompLevel) comp_level; 859 #undef CHECK_RETURN 860 } 861 862 void JVMCIRuntime::bootstrap_finished(TRAPS) { 863 HandleMark hm(THREAD); 864 Handle receiver = get_HotSpotJVMCIRuntime(CHECK); 865 JavaValue result(T_VOID); 866 JavaCallArguments args; 867 args.push_oop(receiver); 868 JavaCalls::call_special(&result, receiver->klass(), vmSymbols::bootstrapFinished_method_name(), vmSymbols::void_method_signature(), &args, CHECK); 869 } 870 871 bool JVMCIRuntime::treat_as_trivial(Method* method) { 872 if (_HotSpotJVMCIRuntime_initialized) { 873 for (int i = 0; i < _trivial_prefixes_count; i++) { 874 if (method->method_holder()->name()->starts_with(_trivial_prefixes[i])) { 875 return true; 876 } 877 } 878 } 879 return false; 880 }