1 /* 2 * Copyright (c) 2012, 2019, 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 "compiler/compileBroker.hpp" 26 #include "jvmci/jniAccessMark.inline.hpp" 27 #include "jvmci/jvmciCompilerToVM.hpp" 28 #include "jvmci/jvmciRuntime.hpp" 29 #include "logging/log.hpp" 30 #include "memory/oopFactory.hpp" 31 #include "oops/constantPool.inline.hpp" 32 #include "oops/method.inline.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "runtime/biasedLocking.hpp" 35 #include "runtime/deoptimization.hpp" 36 #include "runtime/fieldDescriptor.inline.hpp" 37 #include "runtime/frame.inline.hpp" 38 #include "runtime/sharedRuntime.hpp" 39 #if INCLUDE_G1GC 40 #include "gc/g1/g1ThreadLocalData.hpp" 41 #endif // INCLUDE_G1GC 42 43 // Simple helper to see if the caller of a runtime stub which 44 // entered the VM has been deoptimized 45 46 static bool caller_is_deopted() { 47 JavaThread* thread = JavaThread::current(); 48 RegisterMap reg_map(thread, false); 49 frame runtime_frame = thread->last_frame(); 50 frame caller_frame = runtime_frame.sender(®_map); 51 assert(caller_frame.is_compiled_frame(), "must be compiled"); 52 return caller_frame.is_deoptimized_frame(); 53 } 54 55 // Stress deoptimization 56 static void deopt_caller() { 57 if ( !caller_is_deopted()) { 58 JavaThread* thread = JavaThread::current(); 59 RegisterMap reg_map(thread, false); 60 frame runtime_frame = thread->last_frame(); 61 frame caller_frame = runtime_frame.sender(®_map); 62 Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint); 63 assert(caller_is_deopted(), "Must be deoptimized"); 64 } 65 } 66 67 // Manages a scope for a JVMCI runtime call that attempts a heap allocation. 68 // If there is a pending exception upon closing the scope and the runtime 69 // call is of the variety where allocation failure returns NULL without an 70 // exception, the following action is taken: 71 // 1. The pending exception is cleared 72 // 2. NULL is written to JavaThread::_vm_result 73 // 3. Checks that an OutOfMemoryError is Universe::out_of_memory_error_retry(). 74 class RetryableAllocationMark: public StackObj { 75 private: 76 JavaThread* _thread; 77 public: 78 RetryableAllocationMark(JavaThread* thread, bool activate) { 79 if (activate) { 80 assert(!thread->in_retryable_allocation(), "retryable allocation scope is non-reentrant"); 81 _thread = thread; 82 _thread->set_in_retryable_allocation(true); 83 } else { 84 _thread = NULL; 85 } 86 } 87 ~RetryableAllocationMark() { 88 if (_thread != NULL) { 89 _thread->set_in_retryable_allocation(false); 90 JavaThread* THREAD = _thread; 91 if (HAS_PENDING_EXCEPTION) { 92 oop ex = PENDING_EXCEPTION; 93 CLEAR_PENDING_EXCEPTION; 94 oop retry_oome = Universe::out_of_memory_error_retry(); 95 if (ex->is_a(retry_oome->klass()) && retry_oome != ex) { 96 ResourceMark rm; 97 fatal("Unexpected exception in scope of retryable allocation: " INTPTR_FORMAT " of type %s", p2i(ex), ex->klass()->external_name()); 98 } 99 _thread->set_vm_result(NULL); 100 } 101 } 102 } 103 }; 104 105 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_instance_common(JavaThread* thread, Klass* klass, bool null_on_fail)) 106 JRT_BLOCK; 107 assert(klass->is_klass(), "not a class"); 108 Handle holder(THREAD, klass->klass_holder()); // keep the klass alive 109 InstanceKlass* h = InstanceKlass::cast(klass); 110 { 111 RetryableAllocationMark ram(thread, null_on_fail); 112 h->check_valid_for_instantiation(true, CHECK); 113 oop obj; 114 if (null_on_fail) { 115 if (!h->is_initialized()) { 116 // Cannot re-execute class initialization without side effects 117 // so return without attempting the initialization 118 return; 119 } 120 } else { 121 // make sure klass is initialized 122 h->initialize(CHECK); 123 } 124 // allocate instance and return via TLS 125 obj = h->allocate_instance(CHECK); 126 thread->set_vm_result(obj); 127 } 128 JRT_BLOCK_END; 129 SharedRuntime::on_slowpath_allocation_exit(thread); 130 JRT_END 131 132 JRT_BLOCK_ENTRY(void, JVMCIRuntime::new_array_common(JavaThread* thread, Klass* array_klass, jint length, bool null_on_fail)) 133 JRT_BLOCK; 134 // Note: no handle for klass needed since they are not used 135 // anymore after new_objArray() and no GC can happen before. 136 // (This may have to change if this code changes!) 137 assert(array_klass->is_klass(), "not a class"); 138 oop obj; 139 if (array_klass->is_typeArray_klass()) { 140 BasicType elt_type = TypeArrayKlass::cast(array_klass)->element_type(); 141 RetryableAllocationMark ram(thread, null_on_fail); 142 obj = oopFactory::new_typeArray(elt_type, length, CHECK); 143 } else { 144 Handle holder(THREAD, array_klass->klass_holder()); // keep the klass alive 145 Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass(); 146 RetryableAllocationMark ram(thread, null_on_fail); 147 obj = oopFactory::new_objArray(elem_klass, length, CHECK); 148 } 149 thread->set_vm_result(obj); 150 // This is pretty rare but this runtime patch is stressful to deoptimization 151 // if we deoptimize here so force a deopt to stress the path. 152 if (DeoptimizeALot) { 153 static int deopts = 0; 154 // Alternate between deoptimizing and raising an error (which will also cause a deopt) 155 if (deopts++ % 2 == 0) { 156 if (null_on_fail) { 157 return; 158 } else { 159 ResourceMark rm(THREAD); 160 THROW(vmSymbols::java_lang_OutOfMemoryError()); 161 } 162 } else { 163 deopt_caller(); 164 } 165 } 166 JRT_BLOCK_END; 167 SharedRuntime::on_slowpath_allocation_exit(thread); 168 JRT_END 169 170 JRT_ENTRY(void, JVMCIRuntime::new_multi_array_common(JavaThread* thread, Klass* klass, int rank, jint* dims, bool null_on_fail)) 171 assert(klass->is_klass(), "not a class"); 172 assert(rank >= 1, "rank must be nonzero"); 173 Handle holder(THREAD, klass->klass_holder()); // keep the klass alive 174 RetryableAllocationMark ram(thread, null_on_fail); 175 oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK); 176 thread->set_vm_result(obj); 177 JRT_END 178 179 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_array_common(JavaThread* thread, oopDesc* element_mirror, jint length, bool null_on_fail)) 180 RetryableAllocationMark ram(thread, null_on_fail); 181 oop obj = Reflection::reflect_new_array(element_mirror, length, CHECK); 182 thread->set_vm_result(obj); 183 JRT_END 184 185 JRT_ENTRY(void, JVMCIRuntime::dynamic_new_instance_common(JavaThread* thread, oopDesc* type_mirror, bool null_on_fail)) 186 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(type_mirror)); 187 188 if (klass == NULL) { 189 ResourceMark rm(THREAD); 190 THROW(vmSymbols::java_lang_InstantiationException()); 191 } 192 RetryableAllocationMark ram(thread, null_on_fail); 193 194 // Create new instance (the receiver) 195 klass->check_valid_for_instantiation(false, CHECK); 196 197 if (null_on_fail) { 198 if (!klass->is_initialized()) { 199 // Cannot re-execute class initialization without side effects 200 // so return without attempting the initialization 201 return; 202 } 203 } else { 204 // Make sure klass gets initialized 205 klass->initialize(CHECK); 206 } 207 208 oop obj = klass->allocate_instance(CHECK); 209 thread->set_vm_result(obj); 210 JRT_END 211 212 extern void vm_exit(int code); 213 214 // Enter this method from compiled code handler below. This is where we transition 215 // to VM mode. This is done as a helper routine so that the method called directly 216 // from compiled code does not have to transition to VM. This allows the entry 217 // method to see if the nmethod that we have just looked up a handler for has 218 // been deoptimized while we were in the vm. This simplifies the assembly code 219 // cpu directories. 220 // 221 // We are entering here from exception stub (via the entry method below) 222 // If there is a compiled exception handler in this method, we will continue there; 223 // otherwise we will unwind the stack and continue at the caller of top frame method 224 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to 225 // control the area where we can allow a safepoint. After we exit the safepoint area we can 226 // check to see if the handler we are going to return is now in a nmethod that has 227 // been deoptimized. If that is the case we return the deopt blob 228 // unpack_with_exception entry instead. This makes life for the exception blob easier 229 // because making that same check and diverting is painful from assembly language. 230 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, CompiledMethod*& cm)) 231 // Reset method handle flag. 232 thread->set_is_method_handle_return(false); 233 234 Handle exception(thread, ex); 235 cm = CodeCache::find_compiled(pc); 236 assert(cm != NULL, "this is not a compiled method"); 237 // Adjust the pc as needed/ 238 if (cm->is_deopt_pc(pc)) { 239 RegisterMap map(thread, false); 240 frame exception_frame = thread->last_frame().sender(&map); 241 // if the frame isn't deopted then pc must not correspond to the caller of last_frame 242 assert(exception_frame.is_deoptimized_frame(), "must be deopted"); 243 pc = exception_frame.pc(); 244 } 245 #ifdef ASSERT 246 assert(exception.not_null(), "NULL exceptions should be handled by throw_exception"); 247 assert(oopDesc::is_oop(exception()), "just checking"); 248 // Check that exception is a subclass of Throwable, otherwise we have a VerifyError 249 if (!(exception->is_a(SystemDictionary::Throwable_klass()))) { 250 if (ExitVMOnVerifyError) vm_exit(-1); 251 ShouldNotReachHere(); 252 } 253 #endif 254 255 // Check the stack guard pages and reenable them if necessary and there is 256 // enough space on the stack to do so. Use fast exceptions only if the guard 257 // pages are enabled. 258 bool guard_pages_enabled = thread->stack_guards_enabled(); 259 if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack(); 260 261 if (JvmtiExport::can_post_on_exceptions()) { 262 // To ensure correct notification of exception catches and throws 263 // we have to deoptimize here. If we attempted to notify the 264 // catches and throws during this exception lookup it's possible 265 // we could deoptimize on the way out of the VM and end back in 266 // the interpreter at the throw site. This would result in double 267 // notifications since the interpreter would also notify about 268 // these same catches and throws as it unwound the frame. 269 270 RegisterMap reg_map(thread); 271 frame stub_frame = thread->last_frame(); 272 frame caller_frame = stub_frame.sender(®_map); 273 274 // We don't really want to deoptimize the nmethod itself since we 275 // can actually continue in the exception handler ourselves but I 276 // don't see an easy way to have the desired effect. 277 Deoptimization::deoptimize_frame(thread, caller_frame.id(), Deoptimization::Reason_constraint); 278 assert(caller_is_deopted(), "Must be deoptimized"); 279 280 return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls(); 281 } 282 283 // ExceptionCache is used only for exceptions at call sites and not for implicit exceptions 284 if (guard_pages_enabled) { 285 address fast_continuation = cm->handler_for_exception_and_pc(exception, pc); 286 if (fast_continuation != NULL) { 287 // Set flag if return address is a method handle call site. 288 thread->set_is_method_handle_return(cm->is_method_handle_return(pc)); 289 return fast_continuation; 290 } 291 } 292 293 // If the stack guard pages are enabled, check whether there is a handler in 294 // the current method. Otherwise (guard pages disabled), force an unwind and 295 // skip the exception cache update (i.e., just leave continuation==NULL). 296 address continuation = NULL; 297 if (guard_pages_enabled) { 298 299 // New exception handling mechanism can support inlined methods 300 // with exception handlers since the mappings are from PC to PC 301 302 // debugging support 303 // tracing 304 if (log_is_enabled(Info, exceptions)) { 305 ResourceMark rm; 306 stringStream tempst; 307 assert(cm->method() != NULL, "Unexpected null method()"); 308 tempst.print("compiled method <%s>\n" 309 " at PC" INTPTR_FORMAT " for thread " INTPTR_FORMAT, 310 cm->method()->print_value_string(), p2i(pc), p2i(thread)); 311 Exceptions::log_exception(exception, tempst); 312 } 313 // for AbortVMOnException flag 314 NOT_PRODUCT(Exceptions::debug_check_abort(exception)); 315 316 // Clear out the exception oop and pc since looking up an 317 // exception handler can cause class loading, which might throw an 318 // exception and those fields are expected to be clear during 319 // normal bytecode execution. 320 thread->clear_exception_oop_and_pc(); 321 322 bool recursive_exception = false; 323 continuation = SharedRuntime::compute_compiled_exc_handler(cm, pc, exception, false, false, recursive_exception); 324 // If an exception was thrown during exception dispatch, the exception oop may have changed 325 thread->set_exception_oop(exception()); 326 thread->set_exception_pc(pc); 327 328 // The exception cache is used only for non-implicit exceptions 329 // Update the exception cache only when another exception did 330 // occur during the computation of the compiled exception handler 331 // (e.g., when loading the class of the catch type). 332 // Checking for exception oop equality is not 333 // sufficient because some exceptions are pre-allocated and reused. 334 if (continuation != NULL && !recursive_exception && !SharedRuntime::deopt_blob()->contains(continuation)) { 335 cm->add_handler_for_exception_and_pc(exception, pc, continuation); 336 } 337 } 338 339 // Set flag if return address is a method handle call site. 340 thread->set_is_method_handle_return(cm->is_method_handle_return(pc)); 341 342 if (log_is_enabled(Info, exceptions)) { 343 ResourceMark rm; 344 log_info(exceptions)("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT 345 " for exception thrown at PC " PTR_FORMAT, 346 p2i(thread), p2i(continuation), p2i(pc)); 347 } 348 349 return continuation; 350 JRT_END 351 352 // Enter this method from compiled code only if there is a Java exception handler 353 // in the method handling the exception. 354 // We are entering here from exception stub. We don't do a normal VM transition here. 355 // We do it in a helper. This is so we can check to see if the nmethod we have just 356 // searched for an exception handler has been deoptimized in the meantime. 357 address JVMCIRuntime::exception_handler_for_pc(JavaThread* thread) { 358 oop exception = thread->exception_oop(); 359 address pc = thread->exception_pc(); 360 // Still in Java mode 361 DEBUG_ONLY(ResetNoHandleMark rnhm); 362 CompiledMethod* cm = NULL; 363 address continuation = NULL; 364 { 365 // Enter VM mode by calling the helper 366 ResetNoHandleMark rnhm; 367 continuation = exception_handler_for_pc_helper(thread, exception, pc, cm); 368 } 369 // Back in JAVA, use no oops DON'T safepoint 370 371 // Now check to see if the compiled method we were called from is now deoptimized. 372 // If so we must return to the deopt blob and deoptimize the nmethod 373 if (cm != NULL && caller_is_deopted()) { 374 continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls(); 375 } 376 377 assert(continuation != NULL, "no handler found"); 378 return continuation; 379 } 380 381 JRT_ENTRY_NO_ASYNC(void, JVMCIRuntime::monitorenter(JavaThread* thread, oopDesc* obj, BasicLock* lock)) 382 IF_TRACE_jvmci_3 { 383 char type[O_BUFLEN]; 384 obj->klass()->name()->as_C_string(type, O_BUFLEN); 385 markOop mark = obj->mark(); 386 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)); 387 tty->flush(); 388 } 389 if (PrintBiasedLockingStatistics) { 390 Atomic::inc(BiasedLocking::slow_path_entry_count_addr()); 391 } 392 Handle h_obj(thread, obj); 393 assert(oopDesc::is_oop(h_obj()), "must be NULL or an object"); 394 if (UseBiasedLocking) { 395 // Retry fast entry if bias is revoked to avoid unnecessary inflation 396 ObjectSynchronizer::fast_enter(h_obj, lock, true, CHECK); 397 } else { 398 if (JVMCIUseFastLocking) { 399 // When using fast locking, the compiled code has already tried the fast case 400 ObjectSynchronizer::slow_enter(h_obj, lock, THREAD); 401 } else { 402 ObjectSynchronizer::fast_enter(h_obj, lock, false, THREAD); 403 } 404 } 405 TRACE_jvmci_3("%s: exiting locking slow with obj=" INTPTR_FORMAT, thread->name(), p2i(obj)); 406 JRT_END 407 408 JRT_LEAF(void, JVMCIRuntime::monitorexit(JavaThread* thread, oopDesc* obj, BasicLock* lock)) 409 assert(thread == JavaThread::current(), "threads must correspond"); 410 assert(thread->last_Java_sp(), "last_Java_sp must be set"); 411 // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown 412 EXCEPTION_MARK; 413 414 #ifdef ASSERT 415 if (!oopDesc::is_oop(obj)) { 416 ResetNoHandleMark rhm; 417 nmethod* method = thread->last_frame().cb()->as_nmethod_or_null(); 418 if (method != NULL) { 419 tty->print_cr("ERROR in monitorexit in method %s wrong obj " INTPTR_FORMAT, method->name(), p2i(obj)); 420 } 421 thread->print_stack_on(tty); 422 assert(false, "invalid lock object pointer dected"); 423 } 424 #endif 425 426 if (JVMCIUseFastLocking) { 427 // When using fast locking, the compiled code has already tried the fast case 428 ObjectSynchronizer::slow_exit(obj, lock, THREAD); 429 } else { 430 ObjectSynchronizer::fast_exit(obj, lock, THREAD); 431 } 432 IF_TRACE_jvmci_3 { 433 char type[O_BUFLEN]; 434 obj->klass()->name()->as_C_string(type, O_BUFLEN); 435 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)); 436 tty->flush(); 437 } 438 JRT_END 439 440 // Object.notify() fast path, caller does slow path 441 JRT_LEAF(jboolean, JVMCIRuntime::object_notify(JavaThread *thread, oopDesc* obj)) 442 443 // Very few notify/notifyAll operations find any threads on the waitset, so 444 // the dominant fast-path is to simply return. 445 // Relatedly, it's critical that notify/notifyAll be fast in order to 446 // reduce lock hold times. 447 if (!SafepointSynchronize::is_synchronizing()) { 448 if (ObjectSynchronizer::quick_notify(obj, thread, false)) { 449 return true; 450 } 451 } 452 return false; // caller must perform slow path 453 454 JRT_END 455 456 // Object.notifyAll() fast path, caller does slow path 457 JRT_LEAF(jboolean, JVMCIRuntime::object_notifyAll(JavaThread *thread, oopDesc* obj)) 458 459 if (!SafepointSynchronize::is_synchronizing() ) { 460 if (ObjectSynchronizer::quick_notify(obj, thread, true)) { 461 return true; 462 } 463 } 464 return false; // caller must perform slow path 465 466 JRT_END 467 468 JRT_ENTRY(void, JVMCIRuntime::throw_and_post_jvmti_exception(JavaThread* thread, const char* exception, const char* message)) 469 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 470 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, message); 471 JRT_END 472 473 JRT_ENTRY(void, JVMCIRuntime::throw_klass_external_name_exception(JavaThread* thread, const char* exception, Klass* klass)) 474 ResourceMark rm(thread); 475 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 476 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, klass->external_name()); 477 JRT_END 478 479 JRT_ENTRY(void, JVMCIRuntime::throw_class_cast_exception(JavaThread* thread, const char* exception, Klass* caster_klass, Klass* target_klass)) 480 ResourceMark rm(thread); 481 const char* message = SharedRuntime::generate_class_cast_message(caster_klass, target_klass); 482 TempNewSymbol symbol = SymbolTable::new_symbol(exception, CHECK); 483 SharedRuntime::throw_and_post_jvmti_exception(thread, symbol, message); 484 JRT_END 485 486 JRT_LEAF(void, JVMCIRuntime::log_object(JavaThread* thread, oopDesc* obj, bool as_string, bool newline)) 487 ttyLocker ttyl; 488 489 if (obj == NULL) { 490 tty->print("NULL"); 491 } else if (oopDesc::is_oop_or_null(obj, true) && (!as_string || !java_lang_String::is_instance(obj))) { 492 if (oopDesc::is_oop_or_null(obj, true)) { 493 char buf[O_BUFLEN]; 494 tty->print("%s@" INTPTR_FORMAT, obj->klass()->name()->as_C_string(buf, O_BUFLEN), p2i(obj)); 495 } else { 496 tty->print(INTPTR_FORMAT, p2i(obj)); 497 } 498 } else { 499 ResourceMark rm; 500 assert(obj != NULL && java_lang_String::is_instance(obj), "must be"); 501 char *buf = java_lang_String::as_utf8_string(obj); 502 tty->print_raw(buf); 503 } 504 if (newline) { 505 tty->cr(); 506 } 507 JRT_END 508 509 #if INCLUDE_G1GC 510 511 JRT_LEAF(void, JVMCIRuntime::write_barrier_pre(JavaThread* thread, oopDesc* obj)) 512 G1ThreadLocalData::satb_mark_queue(thread).enqueue(obj); 513 JRT_END 514 515 JRT_LEAF(void, JVMCIRuntime::write_barrier_post(JavaThread* thread, void* card_addr)) 516 G1ThreadLocalData::dirty_card_queue(thread).enqueue(card_addr); 517 JRT_END 518 519 #endif // INCLUDE_G1GC 520 521 JRT_LEAF(jboolean, JVMCIRuntime::validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child)) 522 bool ret = true; 523 if(!Universe::heap()->is_in(parent)) { 524 tty->print_cr("Parent Object " INTPTR_FORMAT " not in heap", p2i(parent)); 525 parent->print(); 526 ret=false; 527 } 528 if(!Universe::heap()->is_in(child)) { 529 tty->print_cr("Child Object " INTPTR_FORMAT " not in heap", p2i(child)); 530 child->print(); 531 ret=false; 532 } 533 return (jint)ret; 534 JRT_END 535 536 JRT_ENTRY(void, JVMCIRuntime::vm_error(JavaThread* thread, jlong where, jlong format, jlong value)) 537 ResourceMark rm; 538 const char *error_msg = where == 0L ? "<internal JVMCI error>" : (char*) (address) where; 539 char *detail_msg = NULL; 540 if (format != 0L) { 541 const char* buf = (char*) (address) format; 542 size_t detail_msg_length = strlen(buf) * 2; 543 detail_msg = (char *) NEW_RESOURCE_ARRAY(u_char, detail_msg_length); 544 jio_snprintf(detail_msg, detail_msg_length, buf, value); 545 } 546 report_vm_error(__FILE__, __LINE__, error_msg, "%s", detail_msg); 547 JRT_END 548 549 JRT_LEAF(oopDesc*, JVMCIRuntime::load_and_clear_exception(JavaThread* thread)) 550 oop exception = thread->exception_oop(); 551 assert(exception != NULL, "npe"); 552 thread->set_exception_oop(NULL); 553 thread->set_exception_pc(0); 554 return exception; 555 JRT_END 556 557 PRAGMA_DIAG_PUSH 558 PRAGMA_FORMAT_NONLITERAL_IGNORED 559 JRT_LEAF(void, JVMCIRuntime::log_printf(JavaThread* thread, const char* format, jlong v1, jlong v2, jlong v3)) 560 ResourceMark rm; 561 tty->print(format, v1, v2, v3); 562 JRT_END 563 PRAGMA_DIAG_POP 564 565 static void decipher(jlong v, bool ignoreZero) { 566 if (v != 0 || !ignoreZero) { 567 void* p = (void *)(address) v; 568 CodeBlob* cb = CodeCache::find_blob(p); 569 if (cb) { 570 if (cb->is_nmethod()) { 571 char buf[O_BUFLEN]; 572 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())); 573 return; 574 } 575 cb->print_value_on(tty); 576 return; 577 } 578 if (Universe::heap()->is_in(p)) { 579 oop obj = oop(p); 580 obj->print_value_on(tty); 581 return; 582 } 583 tty->print(INTPTR_FORMAT " [long: " JLONG_FORMAT ", double %lf, char %c]",p2i((void *)v), (jlong)v, (jdouble)v, (char)v); 584 } 585 } 586 587 PRAGMA_DIAG_PUSH 588 PRAGMA_FORMAT_NONLITERAL_IGNORED 589 JRT_LEAF(void, JVMCIRuntime::vm_message(jboolean vmError, jlong format, jlong v1, jlong v2, jlong v3)) 590 ResourceMark rm; 591 const char *buf = (const char*) (address) format; 592 if (vmError) { 593 if (buf != NULL) { 594 fatal(buf, v1, v2, v3); 595 } else { 596 fatal("<anonymous error>"); 597 } 598 } else if (buf != NULL) { 599 tty->print(buf, v1, v2, v3); 600 } else { 601 assert(v2 == 0, "v2 != 0"); 602 assert(v3 == 0, "v3 != 0"); 603 decipher(v1, false); 604 } 605 JRT_END 606 PRAGMA_DIAG_POP 607 608 JRT_LEAF(void, JVMCIRuntime::log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline)) 609 union { 610 jlong l; 611 jdouble d; 612 jfloat f; 613 } uu; 614 uu.l = value; 615 switch (typeChar) { 616 case 'Z': tty->print(value == 0 ? "false" : "true"); break; 617 case 'B': tty->print("%d", (jbyte) value); break; 618 case 'C': tty->print("%c", (jchar) value); break; 619 case 'S': tty->print("%d", (jshort) value); break; 620 case 'I': tty->print("%d", (jint) value); break; 621 case 'F': tty->print("%f", uu.f); break; 622 case 'J': tty->print(JLONG_FORMAT, value); break; 623 case 'D': tty->print("%lf", uu.d); break; 624 default: assert(false, "unknown typeChar"); break; 625 } 626 if (newline) { 627 tty->cr(); 628 } 629 JRT_END 630 631 JRT_ENTRY(jint, JVMCIRuntime::identity_hash_code(JavaThread* thread, oopDesc* obj)) 632 return (jint) obj->identity_hash(); 633 JRT_END 634 635 JRT_ENTRY(jboolean, JVMCIRuntime::thread_is_interrupted(JavaThread* thread, oopDesc* receiver, jboolean clear_interrupted)) 636 Handle receiverHandle(thread, receiver); 637 // A nested ThreadsListHandle may require the Threads_lock which 638 // requires thread_in_vm which is why this method cannot be JRT_LEAF. 639 ThreadsListHandle tlh; 640 641 JavaThread* receiverThread = java_lang_Thread::thread(receiverHandle()); 642 if (receiverThread == NULL || (EnableThreadSMRExtraValidityChecks && !tlh.includes(receiverThread))) { 643 // The other thread may exit during this process, which is ok so return false. 644 return JNI_FALSE; 645 } else { 646 return (jint) Thread::is_interrupted(receiverThread, clear_interrupted != 0); 647 } 648 JRT_END 649 650 JRT_ENTRY(jint, JVMCIRuntime::test_deoptimize_call_int(JavaThread* thread, int value)) 651 deopt_caller(); 652 return (jint) value; 653 JRT_END 654 655 656 // private static JVMCIRuntime JVMCI.initializeRuntime() 657 JVM_ENTRY_NO_ENV(jobject, JVM_GetJVMCIRuntime(JNIEnv *env, jclass c)) 658 JNI_JVMCIENV(env); 659 if (!EnableJVMCI) { 660 JVMCI_THROW_MSG_NULL(InternalError, "JVMCI is not enabled"); 661 } 662 JVMCIENV->runtime()->initialize_HotSpotJVMCIRuntime(JVMCI_CHECK_NULL); 663 JVMCIObject runtime = JVMCIENV->runtime()->get_HotSpotJVMCIRuntime(JVMCI_CHECK_NULL); 664 return JVMCIENV->get_jobject(runtime); 665 JVM_END 666 667 void JVMCIRuntime::call_getCompiler(TRAPS) { 668 THREAD_JVMCIENV(JavaThread::current()); 669 JVMCIObject jvmciRuntime = JVMCIRuntime::get_HotSpotJVMCIRuntime(JVMCI_CHECK); 670 initialize(JVMCIENV); 671 JVMCIENV->call_HotSpotJVMCIRuntime_getCompiler(jvmciRuntime, JVMCI_CHECK); 672 } 673 674 void JVMCINMethodData::initialize( 675 int nmethod_mirror_index, 676 const char* name, 677 FailedSpeculation** failed_speculations) 678 { 679 _failed_speculations = failed_speculations; 680 _nmethod_mirror_index = nmethod_mirror_index; 681 if (name != NULL) { 682 _has_name = true; 683 char* dest = (char*) this->name(); 684 strcpy(dest, name); 685 } else { 686 _has_name = false; 687 } 688 } 689 690 void JVMCINMethodData::add_failed_speculation(nmethod* nm, jlong speculation) { 691 uint index = (speculation >> 32) & 0xFFFFFFFF; 692 int length = (int) speculation; 693 if (index + length > (uint) nm->speculations_size()) { 694 fatal(INTPTR_FORMAT "[index: %d, length: %d] out of bounds wrt encoded speculations of length %u", speculation, index, length, nm->speculations_size()); 695 } 696 address data = nm->speculations_begin() + index; 697 FailedSpeculation::add_failed_speculation(nm, _failed_speculations, data, length); 698 } 699 700 oop JVMCINMethodData::get_nmethod_mirror(nmethod* nm) { 701 if (_nmethod_mirror_index == -1) { 702 return NULL; 703 } 704 return nm->oop_at(_nmethod_mirror_index); 705 } 706 707 void JVMCINMethodData::set_nmethod_mirror(nmethod* nm, oop new_mirror) { 708 assert(_nmethod_mirror_index != -1, "cannot set JVMCI mirror for nmethod"); 709 oop* addr = nm->oop_addr_at(_nmethod_mirror_index); 710 assert(new_mirror != NULL, "use clear_nmethod_mirror to clear the mirror"); 711 assert(*addr == NULL, "cannot overwrite non-null mirror"); 712 713 *addr = new_mirror; 714 715 // Since we've patched some oops in the nmethod, 716 // (re)register it with the heap. 717 Universe::heap()->register_nmethod(nm); 718 } 719 720 void JVMCINMethodData::clear_nmethod_mirror(nmethod* nm) { 721 if (_nmethod_mirror_index != -1) { 722 oop* addr = nm->oop_addr_at(_nmethod_mirror_index); 723 *addr = NULL; 724 } 725 } 726 727 void JVMCINMethodData::invalidate_nmethod_mirror(nmethod* nm) { 728 oop nmethod_mirror = get_nmethod_mirror(nm); 729 if (nmethod_mirror == NULL) { 730 return; 731 } 732 733 // Update the values in the mirror if it still refers to nm. 734 // We cannot use JVMCIObject to wrap the mirror as this is called 735 // during GC, forbidding the creation of JNIHandles. 736 JVMCIEnv* jvmciEnv = NULL; 737 nmethod* current = (nmethod*) HotSpotJVMCI::InstalledCode::address(jvmciEnv, nmethod_mirror); 738 if (nm == current) { 739 if (!nm->is_alive()) { 740 // Break the link from the mirror to nm such that 741 // future invocations via the mirror will result in 742 // an InvalidInstalledCodeException. 743 HotSpotJVMCI::InstalledCode::set_address(jvmciEnv, nmethod_mirror, 0); 744 HotSpotJVMCI::InstalledCode::set_entryPoint(jvmciEnv, nmethod_mirror, 0); 745 } else if (nm->is_not_entrant()) { 746 // Zero the entry point so any new invocation will fail but keep 747 // the address link around that so that existing activations can 748 // be deoptimized via the mirror (i.e. JVMCIEnv::invalidate_installed_code). 749 HotSpotJVMCI::InstalledCode::set_entryPoint(jvmciEnv, nmethod_mirror, 0); 750 } 751 } 752 } 753 754 void JVMCIRuntime::initialize_HotSpotJVMCIRuntime(JVMCI_TRAPS) { 755 if (is_HotSpotJVMCIRuntime_initialized()) { 756 if (JVMCIENV->is_hotspot() && UseJVMCINativeLibrary) { 757 JVMCI_THROW_MSG(InternalError, "JVMCI has already been enabled in the JVMCI shared library"); 758 } 759 } 760 761 initialize(JVMCIENV); 762 763 // This should only be called in the context of the JVMCI class being initialized 764 JVMCIObject result = JVMCIENV->call_HotSpotJVMCIRuntime_runtime(JVMCI_CHECK); 765 766 _HotSpotJVMCIRuntime_instance = JVMCIENV->make_global(result); 767 } 768 769 void JVMCIRuntime::initialize(JVMCIEnv* JVMCIENV) { 770 assert(this != NULL, "sanity"); 771 // Check first without JVMCI_lock 772 if (_initialized) { 773 return; 774 } 775 776 MutexLocker locker(JVMCI_lock); 777 // Check again under JVMCI_lock 778 if (_initialized) { 779 return; 780 } 781 782 while (_being_initialized) { 783 JVMCI_lock->wait(); 784 if (_initialized) { 785 return; 786 } 787 } 788 789 _being_initialized = true; 790 791 { 792 MutexUnlocker unlock(JVMCI_lock); 793 794 HandleMark hm; 795 ResourceMark rm; 796 JavaThread* THREAD = JavaThread::current(); 797 if (JVMCIENV->is_hotspot()) { 798 HotSpotJVMCI::compute_offsets(CHECK_EXIT); 799 } else { 800 JNIAccessMark jni(JVMCIENV); 801 802 JNIJVMCI::initialize_ids(jni.env()); 803 if (jni()->ExceptionCheck()) { 804 jni()->ExceptionDescribe(); 805 fatal("JNI exception during init"); 806 } 807 } 808 create_jvmci_primitive_type(T_BOOLEAN, JVMCI_CHECK_EXIT_((void)0)); 809 create_jvmci_primitive_type(T_BYTE, JVMCI_CHECK_EXIT_((void)0)); 810 create_jvmci_primitive_type(T_CHAR, JVMCI_CHECK_EXIT_((void)0)); 811 create_jvmci_primitive_type(T_SHORT, JVMCI_CHECK_EXIT_((void)0)); 812 create_jvmci_primitive_type(T_INT, JVMCI_CHECK_EXIT_((void)0)); 813 create_jvmci_primitive_type(T_LONG, JVMCI_CHECK_EXIT_((void)0)); 814 create_jvmci_primitive_type(T_FLOAT, JVMCI_CHECK_EXIT_((void)0)); 815 create_jvmci_primitive_type(T_DOUBLE, JVMCI_CHECK_EXIT_((void)0)); 816 create_jvmci_primitive_type(T_VOID, JVMCI_CHECK_EXIT_((void)0)); 817 818 if (!JVMCIENV->is_hotspot()) { 819 JVMCIENV->copy_saved_properties(); 820 } 821 } 822 823 _initialized = true; 824 _being_initialized = false; 825 JVMCI_lock->notify_all(); 826 } 827 828 JVMCIObject JVMCIRuntime::create_jvmci_primitive_type(BasicType type, JVMCI_TRAPS) { 829 Thread* THREAD = Thread::current(); 830 // These primitive types are long lived and are created before the runtime is fully set up 831 // so skip registering them for scanning. 832 JVMCIObject mirror = JVMCIENV->get_object_constant(java_lang_Class::primitive_mirror(type), false, true); 833 if (JVMCIENV->is_hotspot()) { 834 JavaValue result(T_OBJECT); 835 JavaCallArguments args; 836 args.push_oop(Handle(THREAD, HotSpotJVMCI::resolve(mirror))); 837 args.push_int(type2char(type)); 838 JavaCalls::call_static(&result, HotSpotJVMCI::HotSpotResolvedPrimitiveType::klass(), vmSymbols::fromMetaspace_name(), vmSymbols::primitive_fromMetaspace_signature(), &args, CHECK_(JVMCIObject())); 839 840 return JVMCIENV->wrap(JNIHandles::make_local((oop)result.get_jobject())); 841 } else { 842 JNIAccessMark jni(JVMCIENV); 843 jobject result = jni()->CallStaticObjectMethod(JNIJVMCI::HotSpotResolvedPrimitiveType::clazz(), 844 JNIJVMCI::HotSpotResolvedPrimitiveType_fromMetaspace_method(), 845 mirror.as_jobject(), type2char(type)); 846 if (jni()->ExceptionCheck()) { 847 return JVMCIObject(); 848 } 849 return JVMCIENV->wrap(result); 850 } 851 } 852 853 void JVMCIRuntime::initialize_JVMCI(JVMCI_TRAPS) { 854 if (!is_HotSpotJVMCIRuntime_initialized()) { 855 initialize(JVMCI_CHECK); 856 JVMCIENV->call_JVMCI_getRuntime(JVMCI_CHECK); 857 } 858 } 859 860 JVMCIObject JVMCIRuntime::get_HotSpotJVMCIRuntime(JVMCI_TRAPS) { 861 initialize(JVMCIENV); 862 initialize_JVMCI(JVMCI_CHECK_(JVMCIObject())); 863 return _HotSpotJVMCIRuntime_instance; 864 } 865 866 867 // private void CompilerToVM.registerNatives() 868 JVM_ENTRY_NO_ENV(void, JVM_RegisterJVMCINatives(JNIEnv *env, jclass c2vmClass)) 869 870 #ifdef _LP64 871 #ifndef TARGET_ARCH_sparc 872 uintptr_t heap_end = (uintptr_t) Universe::heap()->reserved_region().end(); 873 uintptr_t allocation_end = heap_end + ((uintptr_t)16) * 1024 * 1024 * 1024; 874 guarantee(heap_end < allocation_end, "heap end too close to end of address space (might lead to erroneous TLAB allocations)"); 875 #endif // TARGET_ARCH_sparc 876 #else 877 fatal("check TLAB allocation code for address space conflicts"); 878 #endif 879 880 JNI_JVMCIENV(env); 881 882 if (!EnableJVMCI) { 883 JVMCI_THROW_MSG(InternalError, "JVMCI is not enabled"); 884 } 885 886 JVMCIENV->runtime()->initialize(JVMCIENV); 887 888 { 889 ResourceMark rm; 890 HandleMark hm(thread); 891 ThreadToNativeFromVM trans(thread); 892 893 // Ensure _non_oop_bits is initialized 894 Universe::non_oop_word(); 895 896 if (JNI_OK != env->RegisterNatives(c2vmClass, CompilerToVM::methods, CompilerToVM::methods_count())) { 897 if (!env->ExceptionCheck()) { 898 for (int i = 0; i < CompilerToVM::methods_count(); i++) { 899 if (JNI_OK != env->RegisterNatives(c2vmClass, CompilerToVM::methods + i, 1)) { 900 guarantee(false, "Error registering JNI method %s%s", CompilerToVM::methods[i].name, CompilerToVM::methods[i].signature); 901 break; 902 } 903 } 904 } else { 905 env->ExceptionDescribe(); 906 } 907 guarantee(false, "Failed registering CompilerToVM native methods"); 908 } 909 } 910 JVM_END 911 912 913 void JVMCIRuntime::shutdown() { 914 if (is_HotSpotJVMCIRuntime_initialized()) { 915 _shutdown_called = true; 916 917 THREAD_JVMCIENV(JavaThread::current()); 918 JVMCIENV->call_HotSpotJVMCIRuntime_shutdown(_HotSpotJVMCIRuntime_instance); 919 } 920 } 921 922 void JVMCIRuntime::bootstrap_finished(TRAPS) { 923 if (is_HotSpotJVMCIRuntime_initialized()) { 924 THREAD_JVMCIENV(JavaThread::current()); 925 JVMCIENV->call_HotSpotJVMCIRuntime_bootstrapFinished(_HotSpotJVMCIRuntime_instance, JVMCIENV); 926 } 927 } 928 929 void JVMCIRuntime::describe_pending_hotspot_exception(JavaThread* THREAD, bool clear) { 930 if (HAS_PENDING_EXCEPTION) { 931 Handle exception(THREAD, PENDING_EXCEPTION); 932 const char* exception_file = THREAD->exception_file(); 933 int exception_line = THREAD->exception_line(); 934 CLEAR_PENDING_EXCEPTION; 935 if (exception->is_a(SystemDictionary::ThreadDeath_klass())) { 936 // Don't print anything if we are being killed. 937 } else { 938 java_lang_Throwable::print(exception(), tty); 939 tty->cr(); 940 java_lang_Throwable::print_stack_trace(exception, tty); 941 942 // Clear and ignore any exceptions raised during printing 943 CLEAR_PENDING_EXCEPTION; 944 } 945 if (!clear) { 946 THREAD->set_pending_exception(exception(), exception_file, exception_line); 947 } 948 } 949 } 950 951 952 void JVMCIRuntime::exit_on_pending_exception(JVMCIEnv* JVMCIENV, const char* message) { 953 JavaThread* THREAD = JavaThread::current(); 954 955 static volatile int report_error = 0; 956 if (!report_error && Atomic::cmpxchg(1, &report_error, 0) == 0) { 957 // Only report an error once 958 tty->print_raw_cr(message); 959 if (JVMCIENV != NULL) { 960 JVMCIENV->describe_pending_exception(true); 961 } else { 962 describe_pending_hotspot_exception(THREAD, true); 963 } 964 } else { 965 // Allow error reporting thread to print the stack trace. Windows 966 // doesn't allow uninterruptible wait for JavaThreads 967 const bool interruptible = true; 968 os::sleep(THREAD, 200, interruptible); 969 } 970 971 before_exit(THREAD); 972 vm_exit(-1); 973 } 974 975 // ------------------------------------------------------------------ 976 // Note: the logic of this method should mirror the logic of 977 // constantPoolOopDesc::verify_constant_pool_resolve. 978 bool JVMCIRuntime::check_klass_accessibility(Klass* accessing_klass, Klass* resolved_klass) { 979 if (accessing_klass->is_objArray_klass()) { 980 accessing_klass = ObjArrayKlass::cast(accessing_klass)->bottom_klass(); 981 } 982 if (!accessing_klass->is_instance_klass()) { 983 return true; 984 } 985 986 if (resolved_klass->is_objArray_klass()) { 987 // Find the element klass, if this is an array. 988 resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass(); 989 } 990 if (resolved_klass->is_instance_klass()) { 991 Reflection::VerifyClassAccessResults result = 992 Reflection::verify_class_access(accessing_klass, InstanceKlass::cast(resolved_klass), true); 993 return result == Reflection::ACCESS_OK; 994 } 995 return true; 996 } 997 998 // ------------------------------------------------------------------ 999 Klass* JVMCIRuntime::get_klass_by_name_impl(Klass*& accessing_klass, 1000 const constantPoolHandle& cpool, 1001 Symbol* sym, 1002 bool require_local) { 1003 JVMCI_EXCEPTION_CONTEXT; 1004 1005 // Now we need to check the SystemDictionary 1006 if (sym->char_at(0) == 'L' && 1007 sym->char_at(sym->utf8_length()-1) == ';') { 1008 // This is a name from a signature. Strip off the trimmings. 1009 // Call recursive to keep scope of strippedsym. 1010 TempNewSymbol strippedsym = SymbolTable::new_symbol(sym->as_utf8()+1, 1011 sym->utf8_length()-2, 1012 CHECK_NULL); 1013 return get_klass_by_name_impl(accessing_klass, cpool, strippedsym, require_local); 1014 } 1015 1016 Handle loader(THREAD, (oop)NULL); 1017 Handle domain(THREAD, (oop)NULL); 1018 if (accessing_klass != NULL) { 1019 loader = Handle(THREAD, accessing_klass->class_loader()); 1020 domain = Handle(THREAD, accessing_klass->protection_domain()); 1021 } 1022 1023 Klass* found_klass; 1024 { 1025 ttyUnlocker ttyul; // release tty lock to avoid ordering problems 1026 MutexLocker ml(Compile_lock); 1027 if (!require_local) { 1028 found_klass = SystemDictionary::find_constrained_instance_or_array_klass(sym, loader, CHECK_NULL); 1029 } else { 1030 found_klass = SystemDictionary::find_instance_or_array_klass(sym, loader, domain, CHECK_NULL); 1031 } 1032 } 1033 1034 // If we fail to find an array klass, look again for its element type. 1035 // The element type may be available either locally or via constraints. 1036 // In either case, if we can find the element type in the system dictionary, 1037 // we must build an array type around it. The CI requires array klasses 1038 // to be loaded if their element klasses are loaded, except when memory 1039 // is exhausted. 1040 if (sym->char_at(0) == '[' && 1041 (sym->char_at(1) == '[' || sym->char_at(1) == 'L')) { 1042 // We have an unloaded array. 1043 // Build it on the fly if the element class exists. 1044 TempNewSymbol elem_sym = SymbolTable::new_symbol(sym->as_utf8()+1, 1045 sym->utf8_length()-1, 1046 CHECK_NULL); 1047 1048 // Get element Klass recursively. 1049 Klass* elem_klass = 1050 get_klass_by_name_impl(accessing_klass, 1051 cpool, 1052 elem_sym, 1053 require_local); 1054 if (elem_klass != NULL) { 1055 // Now make an array for it 1056 return elem_klass->array_klass(THREAD); 1057 } 1058 } 1059 1060 if (found_klass == NULL && !cpool.is_null() && cpool->has_preresolution()) { 1061 // Look inside the constant pool for pre-resolved class entries. 1062 for (int i = cpool->length() - 1; i >= 1; i--) { 1063 if (cpool->tag_at(i).is_klass()) { 1064 Klass* kls = cpool->resolved_klass_at(i); 1065 if (kls->name() == sym) { 1066 return kls; 1067 } 1068 } 1069 } 1070 } 1071 1072 return found_klass; 1073 } 1074 1075 // ------------------------------------------------------------------ 1076 Klass* JVMCIRuntime::get_klass_by_name(Klass* accessing_klass, 1077 Symbol* klass_name, 1078 bool require_local) { 1079 ResourceMark rm; 1080 constantPoolHandle cpool; 1081 return get_klass_by_name_impl(accessing_klass, 1082 cpool, 1083 klass_name, 1084 require_local); 1085 } 1086 1087 // ------------------------------------------------------------------ 1088 // Implementation of get_klass_by_index. 1089 Klass* JVMCIRuntime::get_klass_by_index_impl(const constantPoolHandle& cpool, 1090 int index, 1091 bool& is_accessible, 1092 Klass* accessor) { 1093 JVMCI_EXCEPTION_CONTEXT; 1094 Klass* klass = ConstantPool::klass_at_if_loaded(cpool, index); 1095 Symbol* klass_name = NULL; 1096 if (klass == NULL) { 1097 klass_name = cpool->klass_name_at(index); 1098 } 1099 1100 if (klass == NULL) { 1101 // Not found in constant pool. Use the name to do the lookup. 1102 Klass* k = get_klass_by_name_impl(accessor, 1103 cpool, 1104 klass_name, 1105 false); 1106 // Calculate accessibility the hard way. 1107 if (k == NULL) { 1108 is_accessible = false; 1109 } else if (k->class_loader() != accessor->class_loader() && 1110 get_klass_by_name_impl(accessor, cpool, k->name(), true) == NULL) { 1111 // Loaded only remotely. Not linked yet. 1112 is_accessible = false; 1113 } else { 1114 // Linked locally, and we must also check public/private, etc. 1115 is_accessible = check_klass_accessibility(accessor, k); 1116 } 1117 if (!is_accessible) { 1118 return NULL; 1119 } 1120 return k; 1121 } 1122 1123 // It is known to be accessible, since it was found in the constant pool. 1124 is_accessible = true; 1125 return klass; 1126 } 1127 1128 // ------------------------------------------------------------------ 1129 // Get a klass from the constant pool. 1130 Klass* JVMCIRuntime::get_klass_by_index(const constantPoolHandle& cpool, 1131 int index, 1132 bool& is_accessible, 1133 Klass* accessor) { 1134 ResourceMark rm; 1135 Klass* result = get_klass_by_index_impl(cpool, index, is_accessible, accessor); 1136 return result; 1137 } 1138 1139 // ------------------------------------------------------------------ 1140 // Implementation of get_field_by_index. 1141 // 1142 // Implementation note: the results of field lookups are cached 1143 // in the accessor klass. 1144 void JVMCIRuntime::get_field_by_index_impl(InstanceKlass* klass, fieldDescriptor& field_desc, 1145 int index) { 1146 JVMCI_EXCEPTION_CONTEXT; 1147 1148 assert(klass->is_linked(), "must be linked before using its constant-pool"); 1149 1150 constantPoolHandle cpool(thread, klass->constants()); 1151 1152 // Get the field's name, signature, and type. 1153 Symbol* name = cpool->name_ref_at(index); 1154 1155 int nt_index = cpool->name_and_type_ref_index_at(index); 1156 int sig_index = cpool->signature_ref_index_at(nt_index); 1157 Symbol* signature = cpool->symbol_at(sig_index); 1158 1159 // Get the field's declared holder. 1160 int holder_index = cpool->klass_ref_index_at(index); 1161 bool holder_is_accessible; 1162 Klass* declared_holder = get_klass_by_index(cpool, holder_index, 1163 holder_is_accessible, 1164 klass); 1165 1166 // The declared holder of this field may not have been loaded. 1167 // Bail out with partial field information. 1168 if (!holder_is_accessible) { 1169 return; 1170 } 1171 1172 1173 // Perform the field lookup. 1174 Klass* canonical_holder = 1175 InstanceKlass::cast(declared_holder)->find_field(name, signature, &field_desc); 1176 if (canonical_holder == NULL) { 1177 return; 1178 } 1179 1180 assert(canonical_holder == field_desc.field_holder(), "just checking"); 1181 } 1182 1183 // ------------------------------------------------------------------ 1184 // Get a field by index from a klass's constant pool. 1185 void JVMCIRuntime::get_field_by_index(InstanceKlass* accessor, fieldDescriptor& fd, int index) { 1186 ResourceMark rm; 1187 return get_field_by_index_impl(accessor, fd, index); 1188 } 1189 1190 // ------------------------------------------------------------------ 1191 // Perform an appropriate method lookup based on accessor, holder, 1192 // name, signature, and bytecode. 1193 methodHandle JVMCIRuntime::lookup_method(InstanceKlass* accessor, 1194 Klass* holder, 1195 Symbol* name, 1196 Symbol* sig, 1197 Bytecodes::Code bc, 1198 constantTag tag) { 1199 // Accessibility checks are performed in JVMCIEnv::get_method_by_index_impl(). 1200 assert(check_klass_accessibility(accessor, holder), "holder not accessible"); 1201 1202 methodHandle dest_method; 1203 LinkInfo link_info(holder, name, sig, accessor, LinkInfo::needs_access_check, tag); 1204 switch (bc) { 1205 case Bytecodes::_invokestatic: 1206 dest_method = 1207 LinkResolver::resolve_static_call_or_null(link_info); 1208 break; 1209 case Bytecodes::_invokespecial: 1210 dest_method = 1211 LinkResolver::resolve_special_call_or_null(link_info); 1212 break; 1213 case Bytecodes::_invokeinterface: 1214 dest_method = 1215 LinkResolver::linktime_resolve_interface_method_or_null(link_info); 1216 break; 1217 case Bytecodes::_invokevirtual: 1218 dest_method = 1219 LinkResolver::linktime_resolve_virtual_method_or_null(link_info); 1220 break; 1221 default: ShouldNotReachHere(); 1222 } 1223 1224 return dest_method; 1225 } 1226 1227 1228 // ------------------------------------------------------------------ 1229 methodHandle JVMCIRuntime::get_method_by_index_impl(const constantPoolHandle& cpool, 1230 int index, Bytecodes::Code bc, 1231 InstanceKlass* accessor) { 1232 if (bc == Bytecodes::_invokedynamic) { 1233 ConstantPoolCacheEntry* cpce = cpool->invokedynamic_cp_cache_entry_at(index); 1234 bool is_resolved = !cpce->is_f1_null(); 1235 if (is_resolved) { 1236 // Get the invoker Method* from the constant pool. 1237 // (The appendix argument, if any, will be noted in the method's signature.) 1238 Method* adapter = cpce->f1_as_method(); 1239 return methodHandle(adapter); 1240 } 1241 1242 return NULL; 1243 } 1244 1245 int holder_index = cpool->klass_ref_index_at(index); 1246 bool holder_is_accessible; 1247 Klass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor); 1248 1249 // Get the method's name and signature. 1250 Symbol* name_sym = cpool->name_ref_at(index); 1251 Symbol* sig_sym = cpool->signature_ref_at(index); 1252 1253 if (cpool->has_preresolution() 1254 || ((holder == SystemDictionary::MethodHandle_klass() || holder == SystemDictionary::VarHandle_klass()) && 1255 MethodHandles::is_signature_polymorphic_name(holder, name_sym))) { 1256 // Short-circuit lookups for JSR 292-related call sites. 1257 // That is, do not rely only on name-based lookups, because they may fail 1258 // if the names are not resolvable in the boot class loader (7056328). 1259 switch (bc) { 1260 case Bytecodes::_invokevirtual: 1261 case Bytecodes::_invokeinterface: 1262 case Bytecodes::_invokespecial: 1263 case Bytecodes::_invokestatic: 1264 { 1265 Method* m = ConstantPool::method_at_if_loaded(cpool, index); 1266 if (m != NULL) { 1267 return m; 1268 } 1269 } 1270 break; 1271 default: 1272 break; 1273 } 1274 } 1275 1276 if (holder_is_accessible) { // Our declared holder is loaded. 1277 constantTag tag = cpool->tag_ref_at(index); 1278 methodHandle m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag); 1279 if (!m.is_null()) { 1280 // We found the method. 1281 return m; 1282 } 1283 } 1284 1285 // Either the declared holder was not loaded, or the method could 1286 // not be found. 1287 1288 return NULL; 1289 } 1290 1291 // ------------------------------------------------------------------ 1292 InstanceKlass* JVMCIRuntime::get_instance_klass_for_declared_method_holder(Klass* method_holder) { 1293 // For the case of <array>.clone(), the method holder can be an ArrayKlass* 1294 // instead of an InstanceKlass*. For that case simply pretend that the 1295 // declared holder is Object.clone since that's where the call will bottom out. 1296 if (method_holder->is_instance_klass()) { 1297 return InstanceKlass::cast(method_holder); 1298 } else if (method_holder->is_array_klass()) { 1299 return InstanceKlass::cast(SystemDictionary::Object_klass()); 1300 } else { 1301 ShouldNotReachHere(); 1302 } 1303 return NULL; 1304 } 1305 1306 1307 // ------------------------------------------------------------------ 1308 methodHandle JVMCIRuntime::get_method_by_index(const constantPoolHandle& cpool, 1309 int index, Bytecodes::Code bc, 1310 InstanceKlass* accessor) { 1311 ResourceMark rm; 1312 return get_method_by_index_impl(cpool, index, bc, accessor); 1313 } 1314 1315 // ------------------------------------------------------------------ 1316 // Check for changes to the system dictionary during compilation 1317 // class loads, evolution, breakpoints 1318 JVMCI::CodeInstallResult JVMCIRuntime::validate_compile_task_dependencies(Dependencies* dependencies, JVMCICompileState* compile_state, char** failure_detail) { 1319 // If JVMTI capabilities were enabled during compile, the compilation is invalidated. 1320 if (compile_state != NULL && compile_state->jvmti_state_changed()) { 1321 *failure_detail = (char*) "Jvmti state change during compilation invalidated dependencies"; 1322 return JVMCI::dependencies_failed; 1323 } 1324 1325 // Dependencies must be checked when the system dictionary changes 1326 // or if we don't know whether it has changed (i.e., compile_state == NULL). 1327 bool counter_changed = compile_state == NULL || compile_state->system_dictionary_modification_counter() != SystemDictionary::number_of_modifications(); 1328 CompileTask* task = compile_state == NULL ? NULL : compile_state->task(); 1329 Dependencies::DepType result = dependencies->validate_dependencies(task, counter_changed, failure_detail); 1330 if (result == Dependencies::end_marker) { 1331 return JVMCI::ok; 1332 } 1333 1334 if (!Dependencies::is_klass_type(result) || counter_changed) { 1335 return JVMCI::dependencies_failed; 1336 } 1337 // The dependencies were invalid at the time of installation 1338 // without any intervening modification of the system 1339 // dictionary. That means they were invalidly constructed. 1340 return JVMCI::dependencies_invalid; 1341 } 1342 1343 1344 void JVMCIRuntime::compile_method(JVMCIEnv* JVMCIENV, JVMCICompiler* compiler, const methodHandle& method, int entry_bci) { 1345 JVMCI_EXCEPTION_CONTEXT 1346 1347 JVMCICompileState* compile_state = JVMCIENV->compile_state(); 1348 1349 bool is_osr = entry_bci != InvocationEntryBci; 1350 if (compiler->is_bootstrapping() && is_osr) { 1351 // no OSR compilations during bootstrap - the compiler is just too slow at this point, 1352 // and we know that there are no endless loops 1353 compile_state->set_failure(true, "No OSR during boostrap"); 1354 return; 1355 } 1356 1357 HandleMark hm; 1358 JVMCIObject receiver = get_HotSpotJVMCIRuntime(JVMCIENV); 1359 if (JVMCIENV->has_pending_exception()) { 1360 JVMCIENV->describe_pending_exception(true); 1361 compile_state->set_failure(false, "exception getting HotSpotJVMCIRuntime object"); 1362 return; 1363 } 1364 JVMCIObject jvmci_method = JVMCIENV->get_jvmci_method(method, JVMCIENV); 1365 if (JVMCIENV->has_pending_exception()) { 1366 JVMCIENV->describe_pending_exception(true); 1367 compile_state->set_failure(false, "exception getting JVMCI wrapper method"); 1368 return; 1369 } 1370 1371 JVMCIObject result_object = JVMCIENV->call_HotSpotJVMCIRuntime_compileMethod(receiver, jvmci_method, entry_bci, 1372 (jlong) compile_state, compile_state->task()->compile_id()); 1373 if (!JVMCIENV->has_pending_exception()) { 1374 if (result_object.is_non_null()) { 1375 JVMCIObject failure_message = JVMCIENV->get_HotSpotCompilationRequestResult_failureMessage(result_object); 1376 if (failure_message.is_non_null()) { 1377 // Copy failure reason into resource memory first ... 1378 const char* failure_reason = JVMCIENV->as_utf8_string(failure_message); 1379 // ... and then into the C heap. 1380 failure_reason = os::strdup(failure_reason, mtJVMCI); 1381 bool retryable = JVMCIENV->get_HotSpotCompilationRequestResult_retry(result_object) != 0; 1382 compile_state->set_failure(retryable, failure_reason, true); 1383 } else { 1384 if (compile_state->task()->code() == NULL) { 1385 compile_state->set_failure(true, "no nmethod produced"); 1386 } else { 1387 compile_state->task()->set_num_inlined_bytecodes(JVMCIENV->get_HotSpotCompilationRequestResult_inlinedBytecodes(result_object)); 1388 compiler->inc_methods_compiled(); 1389 } 1390 } 1391 } else { 1392 assert(false, "JVMCICompiler.compileMethod should always return non-null"); 1393 } 1394 } else { 1395 // An uncaught exception was thrown during compilation. Generally these 1396 // should be handled by the Java code in some useful way but if they leak 1397 // through to here report them instead of dying or silently ignoring them. 1398 JVMCIENV->describe_pending_exception(true); 1399 compile_state->set_failure(false, "unexpected exception thrown"); 1400 } 1401 if (compiler->is_bootstrapping()) { 1402 compiler->set_bootstrap_compilation_request_handled(); 1403 } 1404 } 1405 1406 1407 // ------------------------------------------------------------------ 1408 JVMCI::CodeInstallResult JVMCIRuntime::register_method(JVMCIEnv* JVMCIENV, 1409 const methodHandle& method, 1410 nmethod*& nm, 1411 int entry_bci, 1412 CodeOffsets* offsets, 1413 int orig_pc_offset, 1414 CodeBuffer* code_buffer, 1415 int frame_words, 1416 OopMapSet* oop_map_set, 1417 ExceptionHandlerTable* handler_table, 1418 AbstractCompiler* compiler, 1419 DebugInformationRecorder* debug_info, 1420 Dependencies* dependencies, 1421 int compile_id, 1422 bool has_unsafe_access, 1423 bool has_wide_vector, 1424 JVMCIObject compiled_code, 1425 JVMCIObject nmethod_mirror, 1426 FailedSpeculation** failed_speculations, 1427 char* speculations, 1428 int speculations_len) { 1429 JVMCI_EXCEPTION_CONTEXT; 1430 nm = NULL; 1431 int comp_level = CompLevel_full_optimization; 1432 char* failure_detail = NULL; 1433 1434 bool install_default = JVMCIENV->get_HotSpotNmethod_isDefault(nmethod_mirror) != 0; 1435 assert(JVMCIENV->isa_HotSpotNmethod(nmethod_mirror), "must be"); 1436 JVMCIObject name = JVMCIENV->get_InstalledCode_name(nmethod_mirror); 1437 const char* nmethod_mirror_name = name.is_null() ? NULL : JVMCIENV->as_utf8_string(name); 1438 int nmethod_mirror_index; 1439 if (!install_default) { 1440 // Reserve or initialize mirror slot in the oops table. 1441 OopRecorder* oop_recorder = debug_info->oop_recorder(); 1442 nmethod_mirror_index = oop_recorder->allocate_oop_index(nmethod_mirror.is_hotspot() ? nmethod_mirror.as_jobject() : NULL); 1443 } else { 1444 // A default HotSpotNmethod mirror is never tracked by the nmethod 1445 nmethod_mirror_index = -1; 1446 } 1447 1448 JVMCI::CodeInstallResult result; 1449 { 1450 // To prevent compile queue updates. 1451 MutexLocker locker(MethodCompileQueue_lock, THREAD); 1452 1453 // Prevent SystemDictionary::add_to_hierarchy from running 1454 // and invalidating our dependencies until we install this method. 1455 MutexLocker ml(Compile_lock); 1456 1457 // Encode the dependencies now, so we can check them right away. 1458 dependencies->encode_content_bytes(); 1459 1460 // Record the dependencies for the current compile in the log 1461 if (LogCompilation) { 1462 for (Dependencies::DepStream deps(dependencies); deps.next(); ) { 1463 deps.log_dependency(); 1464 } 1465 } 1466 1467 // Check for {class loads, evolution, breakpoints} during compilation 1468 result = validate_compile_task_dependencies(dependencies, JVMCIENV->compile_state(), &failure_detail); 1469 if (result != JVMCI::ok) { 1470 // While not a true deoptimization, it is a preemptive decompile. 1471 MethodData* mdp = method()->method_data(); 1472 if (mdp != NULL) { 1473 mdp->inc_decompile_count(); 1474 #ifdef ASSERT 1475 if (mdp->decompile_count() > (uint)PerMethodRecompilationCutoff) { 1476 ResourceMark m; 1477 tty->print_cr("WARN: endless recompilation of %s. Method was set to not compilable.", method()->name_and_sig_as_C_string()); 1478 } 1479 #endif 1480 } 1481 1482 // All buffers in the CodeBuffer are allocated in the CodeCache. 1483 // If the code buffer is created on each compile attempt 1484 // as in C2, then it must be freed. 1485 //code_buffer->free_blob(); 1486 } else { 1487 ImplicitExceptionTable implicit_tbl; 1488 nm = nmethod::new_nmethod(method, 1489 compile_id, 1490 entry_bci, 1491 offsets, 1492 orig_pc_offset, 1493 debug_info, dependencies, code_buffer, 1494 frame_words, oop_map_set, 1495 handler_table, &implicit_tbl, 1496 compiler, comp_level, 1497 speculations, speculations_len, 1498 nmethod_mirror_index, nmethod_mirror_name, failed_speculations); 1499 1500 1501 // Free codeBlobs 1502 if (nm == NULL) { 1503 // The CodeCache is full. Print out warning and disable compilation. 1504 { 1505 MutexUnlocker ml(Compile_lock); 1506 MutexUnlocker locker(MethodCompileQueue_lock); 1507 CompileBroker::handle_full_code_cache(CodeCache::get_code_blob_type(comp_level)); 1508 } 1509 } else { 1510 nm->set_has_unsafe_access(has_unsafe_access); 1511 nm->set_has_wide_vectors(has_wide_vector); 1512 1513 // Record successful registration. 1514 // (Put nm into the task handle *before* publishing to the Java heap.) 1515 if (JVMCIENV->compile_state() != NULL) { 1516 JVMCIENV->compile_state()->task()->set_code(nm); 1517 } 1518 1519 JVMCINMethodData* data = nm->jvmci_nmethod_data(); 1520 assert(data != NULL, "must be"); 1521 if (install_default) { 1522 assert(!nmethod_mirror.is_hotspot() || data->get_nmethod_mirror(nm) == NULL, "must be"); 1523 if (entry_bci == InvocationEntryBci) { 1524 if (TieredCompilation) { 1525 // If there is an old version we're done with it 1526 CompiledMethod* old = method->code(); 1527 if (TraceMethodReplacement && old != NULL) { 1528 ResourceMark rm; 1529 char *method_name = method->name_and_sig_as_C_string(); 1530 tty->print_cr("Replacing method %s", method_name); 1531 } 1532 if (old != NULL ) { 1533 old->make_not_entrant(); 1534 } 1535 } 1536 if (TraceNMethodInstalls) { 1537 ResourceMark rm; 1538 char *method_name = method->name_and_sig_as_C_string(); 1539 ttyLocker ttyl; 1540 tty->print_cr("Installing method (%d) %s [entry point: %p]", 1541 comp_level, 1542 method_name, nm->entry_point()); 1543 } 1544 // Allow the code to be executed 1545 method->set_code(method, nm); 1546 } else { 1547 if (TraceNMethodInstalls ) { 1548 ResourceMark rm; 1549 char *method_name = method->name_and_sig_as_C_string(); 1550 ttyLocker ttyl; 1551 tty->print_cr("Installing osr method (%d) %s @ %d", 1552 comp_level, 1553 method_name, 1554 entry_bci); 1555 } 1556 InstanceKlass::cast(method->method_holder())->add_osr_nmethod(nm); 1557 } 1558 } else { 1559 assert(!nmethod_mirror.is_hotspot() || data->get_nmethod_mirror(nm) == HotSpotJVMCI::resolve(nmethod_mirror), "must be"); 1560 } 1561 nm->make_in_use(); 1562 } 1563 result = nm != NULL ? JVMCI::ok :JVMCI::cache_full; 1564 } 1565 } 1566 1567 // String creation must be done outside lock 1568 if (failure_detail != NULL) { 1569 // A failure to allocate the string is silently ignored. 1570 JVMCIObject message = JVMCIENV->create_string(failure_detail, JVMCIENV); 1571 JVMCIENV->set_HotSpotCompiledNmethod_installationFailureMessage(compiled_code, message); 1572 } 1573 1574 // JVMTI -- compiled method notification (must be done outside lock) 1575 if (nm != NULL) { 1576 nm->post_compiled_method_load_event(); 1577 } 1578 1579 return result; 1580 }