1 /* 2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "classfile/javaClasses.inline.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "classfile/vmClasses.hpp" 28 #include "classfile/vmSymbols.hpp" 29 #include "code/codeCache.hpp" 30 #include "compiler/compilationPolicy.hpp" 31 #include "compiler/compileBroker.hpp" 32 #include "compiler/disassembler.hpp" 33 #include "gc/shared/barrierSetNMethod.hpp" 34 #include "gc/shared/collectedHeap.hpp" 35 #include "interpreter/bytecodeTracer.hpp" 36 #include "interpreter/interpreter.hpp" 37 #include "interpreter/interpreterRuntime.hpp" 38 #include "interpreter/linkResolver.hpp" 39 #include "interpreter/templateTable.hpp" 40 #include "jvm_io.h" 41 #include "logging/log.hpp" 42 #include "memory/oopFactory.hpp" 43 #include "memory/resourceArea.hpp" 44 #include "memory/universe.hpp" 45 #include "oops/constantPool.inline.hpp" 46 #include "oops/cpCache.inline.hpp" 47 #include "oops/instanceKlass.inline.hpp" 48 #include "oops/klass.inline.hpp" 49 #include "oops/methodData.hpp" 50 #include "oops/method.inline.hpp" 51 #include "oops/objArrayKlass.hpp" 52 #include "oops/objArrayOop.inline.hpp" 53 #include "oops/oop.inline.hpp" 54 #include "oops/symbol.hpp" 55 #include "prims/jvmtiExport.hpp" 56 #include "prims/methodHandles.hpp" 57 #include "prims/nativeLookup.hpp" 58 #include "runtime/atomic.hpp" 59 #include "runtime/continuation.hpp" 60 #include "runtime/deoptimization.hpp" 61 #include "runtime/fieldDescriptor.inline.hpp" 62 #include "runtime/frame.inline.hpp" 63 #include "runtime/handles.inline.hpp" 64 #include "runtime/icache.hpp" 65 #include "runtime/interfaceSupport.inline.hpp" 66 #include "runtime/java.hpp" 67 #include "runtime/javaCalls.hpp" 68 #include "runtime/jfieldIDWorkaround.hpp" 69 #include "runtime/osThread.hpp" 70 #include "runtime/sharedRuntime.hpp" 71 #include "runtime/stackWatermarkSet.hpp" 72 #include "runtime/stubRoutines.hpp" 73 #include "runtime/synchronizer.inline.hpp" 74 #include "runtime/threadCritical.hpp" 75 #include "utilities/align.hpp" 76 #include "utilities/checkedCast.hpp" 77 #include "utilities/copy.hpp" 78 #include "utilities/events.hpp" 79 80 // Helper class to access current interpreter state 81 class LastFrameAccessor : public StackObj { 82 frame _last_frame; 83 public: 84 LastFrameAccessor(JavaThread* current) { 85 assert(current == Thread::current(), "sanity"); 86 _last_frame = current->last_frame(); 87 } 88 bool is_interpreted_frame() const { return _last_frame.is_interpreted_frame(); } 89 Method* method() const { return _last_frame.interpreter_frame_method(); } 90 address bcp() const { return _last_frame.interpreter_frame_bcp(); } 91 int bci() const { return _last_frame.interpreter_frame_bci(); } 92 address mdp() const { return _last_frame.interpreter_frame_mdp(); } 93 94 void set_bcp(address bcp) { _last_frame.interpreter_frame_set_bcp(bcp); } 95 void set_mdp(address dp) { _last_frame.interpreter_frame_set_mdp(dp); } 96 97 // pass method to avoid calling unsafe bcp_to_method (partial fix 4926272) 98 Bytecodes::Code code() const { return Bytecodes::code_at(method(), bcp()); } 99 100 Bytecode bytecode() const { return Bytecode(method(), bcp()); } 101 int get_index_u1(Bytecodes::Code bc) const { return bytecode().get_index_u1(bc); } 102 int get_index_u2(Bytecodes::Code bc) const { return bytecode().get_index_u2(bc); } 103 int get_index_u4(Bytecodes::Code bc) const { return bytecode().get_index_u4(bc); } 104 int number_of_dimensions() const { return bcp()[3]; } 105 106 oop callee_receiver(Symbol* signature) { 107 return _last_frame.interpreter_callee_receiver(signature); 108 } 109 BasicObjectLock* monitor_begin() const { 110 return _last_frame.interpreter_frame_monitor_begin(); 111 } 112 BasicObjectLock* monitor_end() const { 113 return _last_frame.interpreter_frame_monitor_end(); 114 } 115 BasicObjectLock* next_monitor(BasicObjectLock* current) const { 116 return _last_frame.next_monitor_in_interpreter_frame(current); 117 } 118 119 frame& get_frame() { return _last_frame; } 120 }; 121 122 class ProfileTrapsMark : public StackObj { 123 JavaThread* _thread; 124 public: 125 ProfileTrapsMark(JavaThread* current) : _thread(current) {} 126 ~ProfileTrapsMark() { 127 JavaThread* THREAD = _thread; 128 if (HAS_PENDING_EXCEPTION) { 129 if (ProfileTraps && PENDING_EXCEPTION->klass()->name() == vmSymbols::java_lang_NullPointerException()) { 130 // Preserve the original exception across the call to note_trap() 131 PreserveExceptionMark pm(_thread); 132 // Recording the trap will help the compiler to potentially recognize this exception as "hot" 133 InterpreterRuntime::note_trap(_thread, Deoptimization::Reason_null_check); 134 } 135 } 136 } 137 }; 138 139 //------------------------------------------------------------------------------------------------------------------------ 140 // State accessors 141 142 void InterpreterRuntime::set_bcp_and_mdp(address bcp, JavaThread* current) { 143 LastFrameAccessor last_frame(current); 144 last_frame.set_bcp(bcp); 145 if (ProfileInterpreter) { 146 // ProfileTraps uses MDOs independently of ProfileInterpreter. 147 // That is why we must check both ProfileInterpreter and mdo != nullptr. 148 MethodData* mdo = last_frame.method()->method_data(); 149 if (mdo != nullptr) { 150 NEEDS_CLEANUP; 151 last_frame.set_mdp(mdo->bci_to_dp(last_frame.bci())); 152 } 153 } 154 } 155 156 //------------------------------------------------------------------------------------------------------------------------ 157 // Constants 158 159 160 JRT_ENTRY(void, InterpreterRuntime::ldc(JavaThread* current, bool wide)) 161 // access constant pool 162 LastFrameAccessor last_frame(current); 163 ConstantPool* pool = last_frame.method()->constants(); 164 int cp_index = wide ? last_frame.get_index_u2(Bytecodes::_ldc_w) : last_frame.get_index_u1(Bytecodes::_ldc); 165 constantTag tag = pool->tag_at(cp_index); 166 167 assert (tag.is_unresolved_klass() || tag.is_klass(), "wrong ldc call"); 168 Klass* klass = pool->klass_at(cp_index, CHECK); 169 oop java_class = klass->java_mirror(); 170 current->set_vm_result_oop(java_class); 171 JRT_END 172 173 JRT_ENTRY(void, InterpreterRuntime::resolve_ldc(JavaThread* current, Bytecodes::Code bytecode)) { 174 assert(bytecode == Bytecodes::_ldc || 175 bytecode == Bytecodes::_ldc_w || 176 bytecode == Bytecodes::_ldc2_w || 177 bytecode == Bytecodes::_fast_aldc || 178 bytecode == Bytecodes::_fast_aldc_w, "wrong bc"); 179 ResourceMark rm(current); 180 const bool is_fast_aldc = (bytecode == Bytecodes::_fast_aldc || 181 bytecode == Bytecodes::_fast_aldc_w); 182 LastFrameAccessor last_frame(current); 183 methodHandle m (current, last_frame.method()); 184 Bytecode_loadconstant ldc(m, last_frame.bci()); 185 186 // Double-check the size. (Condy can have any type.) 187 BasicType type = ldc.result_type(); 188 switch (type2size[type]) { 189 case 2: guarantee(bytecode == Bytecodes::_ldc2_w, ""); break; 190 case 1: guarantee(bytecode != Bytecodes::_ldc2_w, ""); break; 191 default: ShouldNotReachHere(); 192 } 193 194 // Resolve the constant. This does not do unboxing. 195 // But it does replace Universe::the_null_sentinel by null. 196 oop result = ldc.resolve_constant(CHECK); 197 assert(result != nullptr || is_fast_aldc, "null result only valid for fast_aldc"); 198 199 #ifdef ASSERT 200 { 201 // The bytecode wrappers aren't GC-safe so construct a new one 202 Bytecode_loadconstant ldc2(m, last_frame.bci()); 203 int rindex = ldc2.cache_index(); 204 if (rindex < 0) 205 rindex = m->constants()->cp_to_object_index(ldc2.pool_index()); 206 if (rindex >= 0) { 207 oop coop = m->constants()->resolved_reference_at(rindex); 208 oop roop = (result == nullptr ? Universe::the_null_sentinel() : result); 209 assert(roop == coop, "expected result for assembly code"); 210 } 211 } 212 #endif 213 current->set_vm_result_oop(result); 214 if (!is_fast_aldc) { 215 // Tell the interpreter how to unbox the primitive. 216 guarantee(java_lang_boxing_object::is_instance(result, type), ""); 217 int offset = java_lang_boxing_object::value_offset(type); 218 intptr_t flags = ((as_TosState(type) << ConstantPoolCache::tos_state_shift) 219 | (offset & ConstantPoolCache::field_index_mask)); 220 current->set_vm_result_metadata((Metadata*)flags); 221 } 222 } 223 JRT_END 224 225 226 //------------------------------------------------------------------------------------------------------------------------ 227 // Allocation 228 229 JRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* current, ConstantPool* pool, int index)) 230 Klass* k = pool->klass_at(index, CHECK); 231 InstanceKlass* klass = InstanceKlass::cast(k); 232 233 // Make sure we are not instantiating an abstract klass 234 klass->check_valid_for_instantiation(true, CHECK); 235 236 // Make sure klass is initialized 237 klass->initialize_preemptable(CHECK_AND_CLEAR_PREEMPTED); 238 239 oop obj = klass->allocate_instance(CHECK); 240 current->set_vm_result_oop(obj); 241 JRT_END 242 243 244 JRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* current, BasicType type, jint size)) 245 oop obj = oopFactory::new_typeArray(type, size, CHECK); 246 current->set_vm_result_oop(obj); 247 JRT_END 248 249 250 JRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* current, ConstantPool* pool, int index, jint size)) 251 Klass* klass = pool->klass_at(index, CHECK); 252 objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK); 253 current->set_vm_result_oop(obj); 254 JRT_END 255 256 257 JRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* current, jint* first_size_address)) 258 // We may want to pass in more arguments - could make this slightly faster 259 LastFrameAccessor last_frame(current); 260 ConstantPool* constants = last_frame.method()->constants(); 261 int i = last_frame.get_index_u2(Bytecodes::_multianewarray); 262 Klass* klass = constants->klass_at(i, CHECK); 263 int nof_dims = last_frame.number_of_dimensions(); 264 assert(klass->is_klass(), "not a class"); 265 assert(nof_dims >= 1, "multianewarray rank must be nonzero"); 266 267 // We must create an array of jints to pass to multi_allocate. 268 ResourceMark rm(current); 269 const int small_dims = 10; 270 jint dim_array[small_dims]; 271 jint *dims = &dim_array[0]; 272 if (nof_dims > small_dims) { 273 dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims); 274 } 275 for (int index = 0; index < nof_dims; index++) { 276 // offset from first_size_address is addressed as local[index] 277 int n = Interpreter::local_offset_in_bytes(index)/jintSize; 278 dims[index] = first_size_address[n]; 279 } 280 oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK); 281 current->set_vm_result_oop(obj); 282 JRT_END 283 284 285 JRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* current, oopDesc* obj)) 286 assert(oopDesc::is_oop(obj), "must be a valid oop"); 287 assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise"); 288 InstanceKlass::register_finalizer(instanceOop(obj), CHECK); 289 JRT_END 290 291 292 // Quicken instance-of and check-cast bytecodes 293 JRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* current)) 294 // Force resolving; quicken the bytecode 295 LastFrameAccessor last_frame(current); 296 int which = last_frame.get_index_u2(Bytecodes::_checkcast); 297 ConstantPool* cpool = last_frame.method()->constants(); 298 // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded 299 // program we might have seen an unquick'd bytecode in the interpreter but have another 300 // thread quicken the bytecode before we get here. 301 // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" ); 302 Klass* klass = cpool->klass_at(which, CHECK); 303 current->set_vm_result_metadata(klass); 304 JRT_END 305 306 307 //------------------------------------------------------------------------------------------------------------------------ 308 // Exceptions 309 310 void InterpreterRuntime::note_trap_inner(JavaThread* current, int reason, 311 const methodHandle& trap_method, int trap_bci) { 312 if (trap_method.not_null()) { 313 MethodData* trap_mdo = trap_method->method_data(); 314 if (trap_mdo == nullptr) { 315 ExceptionMark em(current); 316 JavaThread* THREAD = current; // For exception macros. 317 Method::build_profiling_method_data(trap_method, THREAD); 318 if (HAS_PENDING_EXCEPTION) { 319 // Only metaspace OOM is expected. No Java code executed. 320 assert((PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())), 321 "we expect only an OOM error here"); 322 CLEAR_PENDING_EXCEPTION; 323 } 324 trap_mdo = trap_method->method_data(); 325 // and fall through... 326 } 327 if (trap_mdo != nullptr) { 328 // Update per-method count of trap events. The interpreter 329 // is updating the MDO to simulate the effect of compiler traps. 330 Deoptimization::update_method_data_from_interpreter(trap_mdo, trap_bci, reason); 331 } 332 } 333 } 334 335 // Assume the compiler is (or will be) interested in this event. 336 // If necessary, create an MDO to hold the information, and record it. 337 void InterpreterRuntime::note_trap(JavaThread* current, int reason) { 338 assert(ProfileTraps, "call me only if profiling"); 339 LastFrameAccessor last_frame(current); 340 methodHandle trap_method(current, last_frame.method()); 341 int trap_bci = trap_method->bci_from(last_frame.bcp()); 342 note_trap_inner(current, reason, trap_method, trap_bci); 343 } 344 345 static Handle get_preinitialized_exception(Klass* k, TRAPS) { 346 // get klass 347 InstanceKlass* klass = InstanceKlass::cast(k); 348 assert(klass->is_initialized(), 349 "this klass should have been initialized during VM initialization"); 350 // create instance - do not call constructor since we may have no 351 // (java) stack space left (should assert constructor is empty) 352 Handle exception; 353 oop exception_oop = klass->allocate_instance(CHECK_(exception)); 354 exception = Handle(THREAD, exception_oop); 355 if (StackTraceInThrowable) { 356 java_lang_Throwable::fill_in_stack_trace(exception); 357 } 358 return exception; 359 } 360 361 // Special handling for stack overflow: since we don't have any (java) stack 362 // space left we use the pre-allocated & pre-initialized StackOverflowError 363 // klass to create an stack overflow error instance. We do not call its 364 // constructor for the same reason (it is empty, anyway). 365 JRT_ENTRY(void, InterpreterRuntime::throw_StackOverflowError(JavaThread* current)) 366 Handle exception = get_preinitialized_exception( 367 vmClasses::StackOverflowError_klass(), 368 CHECK); 369 // Increment counter for hs_err file reporting 370 Atomic::inc(&Exceptions::_stack_overflow_errors); 371 // Remove the ScopedValue bindings in case we got a StackOverflowError 372 // while we were trying to manipulate ScopedValue bindings. 373 current->clear_scopedValueBindings(); 374 THROW_HANDLE(exception); 375 JRT_END 376 377 JRT_ENTRY(void, InterpreterRuntime::throw_delayed_StackOverflowError(JavaThread* current)) 378 Handle exception = get_preinitialized_exception( 379 vmClasses::StackOverflowError_klass(), 380 CHECK); 381 java_lang_Throwable::set_message(exception(), 382 Universe::delayed_stack_overflow_error_message()); 383 // Increment counter for hs_err file reporting 384 Atomic::inc(&Exceptions::_stack_overflow_errors); 385 // Remove the ScopedValue bindings in case we got a StackOverflowError 386 // while we were trying to manipulate ScopedValue bindings. 387 current->clear_scopedValueBindings(); 388 THROW_HANDLE(exception); 389 JRT_END 390 391 JRT_ENTRY(void, InterpreterRuntime::create_exception(JavaThread* current, char* name, char* message)) 392 // lookup exception klass 393 TempNewSymbol s = SymbolTable::new_symbol(name); 394 if (ProfileTraps) { 395 if (s == vmSymbols::java_lang_ArithmeticException()) { 396 note_trap(current, Deoptimization::Reason_div0_check); 397 } else if (s == vmSymbols::java_lang_NullPointerException()) { 398 note_trap(current, Deoptimization::Reason_null_check); 399 } 400 } 401 // create exception 402 Handle exception = Exceptions::new_exception(current, s, message); 403 current->set_vm_result_oop(exception()); 404 JRT_END 405 406 407 JRT_ENTRY(void, InterpreterRuntime::create_klass_exception(JavaThread* current, char* name, oopDesc* obj)) 408 // Produce the error message first because note_trap can safepoint 409 ResourceMark rm(current); 410 const char* klass_name = obj->klass()->external_name(); 411 // lookup exception klass 412 TempNewSymbol s = SymbolTable::new_symbol(name); 413 if (ProfileTraps) { 414 if (s == vmSymbols::java_lang_ArrayStoreException()) { 415 note_trap(current, Deoptimization::Reason_array_check); 416 } else { 417 note_trap(current, Deoptimization::Reason_class_check); 418 } 419 } 420 // create exception, with klass name as detail message 421 Handle exception = Exceptions::new_exception(current, s, klass_name); 422 current->set_vm_result_oop(exception()); 423 JRT_END 424 425 JRT_ENTRY(void, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException(JavaThread* current, arrayOopDesc* a, jint index)) 426 // Produce the error message first because note_trap can safepoint 427 ResourceMark rm(current); 428 stringStream ss; 429 ss.print("Index %d out of bounds for length %d", index, a->length()); 430 431 if (ProfileTraps) { 432 note_trap(current, Deoptimization::Reason_range_check); 433 } 434 435 THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string()); 436 JRT_END 437 438 JRT_ENTRY(void, InterpreterRuntime::throw_ClassCastException( 439 JavaThread* current, oopDesc* obj)) 440 441 // Produce the error message first because note_trap can safepoint 442 ResourceMark rm(current); 443 char* message = SharedRuntime::generate_class_cast_message( 444 current, obj->klass()); 445 446 if (ProfileTraps) { 447 note_trap(current, Deoptimization::Reason_class_check); 448 } 449 450 // create exception 451 THROW_MSG(vmSymbols::java_lang_ClassCastException(), message); 452 JRT_END 453 454 // exception_handler_for_exception(...) returns the continuation address, 455 // the exception oop (via TLS) and sets the bci/bcp for the continuation. 456 // The exception oop is returned to make sure it is preserved over GC (it 457 // is only on the stack if the exception was thrown explicitly via athrow). 458 // During this operation, the expression stack contains the values for the 459 // bci where the exception happened. If the exception was propagated back 460 // from a call, the expression stack contains the values for the bci at the 461 // invoke w/o arguments (i.e., as if one were inside the call). 462 // Note that the implementation of this method assumes it's only called when an exception has actually occured 463 JRT_ENTRY(address, InterpreterRuntime::exception_handler_for_exception(JavaThread* current, oopDesc* exception)) 464 // We get here after we have unwound from a callee throwing an exception 465 // into the interpreter. Any deferred stack processing is notified of 466 // the event via the StackWatermarkSet. 467 StackWatermarkSet::after_unwind(current); 468 469 LastFrameAccessor last_frame(current); 470 Handle h_exception(current, exception); 471 methodHandle h_method (current, last_frame.method()); 472 constantPoolHandle h_constants(current, h_method->constants()); 473 bool should_repeat; 474 int handler_bci; 475 int current_bci = last_frame.bci(); 476 477 if (current->frames_to_pop_failed_realloc() > 0) { 478 // Allocation of scalar replaced object used in this frame 479 // failed. Unconditionally pop the frame. 480 current->dec_frames_to_pop_failed_realloc(); 481 current->set_vm_result_oop(h_exception()); 482 // If the method is synchronized we already unlocked the monitor 483 // during deoptimization so the interpreter needs to skip it when 484 // the frame is popped. 485 current->set_do_not_unlock_if_synchronized(true); 486 return Interpreter::remove_activation_entry(); 487 } 488 489 // Need to do this check first since when _do_not_unlock_if_synchronized 490 // is set, we don't want to trigger any classloading which may make calls 491 // into java, or surprisingly find a matching exception handler for bci 0 492 // since at this moment the method hasn't been "officially" entered yet. 493 if (current->do_not_unlock_if_synchronized()) { 494 ResourceMark rm; 495 assert(current_bci == 0, "bci isn't zero for do_not_unlock_if_synchronized"); 496 current->set_vm_result_oop(exception); 497 return Interpreter::remove_activation_entry(); 498 } 499 500 do { 501 should_repeat = false; 502 503 // assertions 504 assert(h_exception.not_null(), "null exceptions should be handled by athrow"); 505 // Check that exception is a subclass of Throwable. 506 assert(h_exception->is_a(vmClasses::Throwable_klass()), 507 "Exception not subclass of Throwable"); 508 509 // tracing 510 if (log_is_enabled(Info, exceptions)) { 511 ResourceMark rm(current); 512 stringStream tempst; 513 tempst.print("interpreter method <%s>\n" 514 " at bci %d for thread " INTPTR_FORMAT " (%s)", 515 h_method->print_value_string(), current_bci, p2i(current), current->name()); 516 Exceptions::log_exception(h_exception, tempst.as_string()); 517 } 518 // Don't go paging in something which won't be used. 519 // else if (extable->length() == 0) { 520 // // disabled for now - interpreter is not using shortcut yet 521 // // (shortcut is not to call runtime if we have no exception handlers) 522 // // warning("performance bug: should not call runtime if method has no exception handlers"); 523 // } 524 // for AbortVMOnException flag 525 Exceptions::debug_check_abort(h_exception); 526 527 // exception handler lookup 528 Klass* klass = h_exception->klass(); 529 handler_bci = Method::fast_exception_handler_bci_for(h_method, klass, current_bci, THREAD); 530 if (HAS_PENDING_EXCEPTION) { 531 // We threw an exception while trying to find the exception handler. 532 // Transfer the new exception to the exception handle which will 533 // be set into thread local storage, and do another lookup for an 534 // exception handler for this exception, this time starting at the 535 // BCI of the exception handler which caused the exception to be 536 // thrown (bug 4307310). 537 h_exception = Handle(THREAD, PENDING_EXCEPTION); 538 CLEAR_PENDING_EXCEPTION; 539 if (handler_bci >= 0) { 540 current_bci = handler_bci; 541 should_repeat = true; 542 } 543 } 544 } while (should_repeat == true); 545 546 #if INCLUDE_JVMCI 547 if (EnableJVMCI && h_method->method_data() != nullptr) { 548 ResourceMark rm(current); 549 MethodData* mdo = h_method->method_data(); 550 551 // Lock to read ProfileData, and ensure lock is not broken by a safepoint 552 MutexLocker ml(mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag); 553 554 ProfileData* pdata = mdo->allocate_bci_to_data(current_bci, nullptr); 555 if (pdata != nullptr && pdata->is_BitData()) { 556 BitData* bit_data = (BitData*) pdata; 557 bit_data->set_exception_seen(); 558 } 559 } 560 #endif 561 562 // notify JVMTI of an exception throw; JVMTI will detect if this is a first 563 // time throw or a stack unwinding throw and accordingly notify the debugger 564 if (JvmtiExport::can_post_on_exceptions()) { 565 JvmtiExport::post_exception_throw(current, h_method(), last_frame.bcp(), h_exception()); 566 } 567 568 address continuation = nullptr; 569 address handler_pc = nullptr; 570 if (handler_bci < 0 || !current->stack_overflow_state()->reguard_stack((address) &continuation)) { 571 // Forward exception to callee (leaving bci/bcp untouched) because (a) no 572 // handler in this method, or (b) after a stack overflow there is not yet 573 // enough stack space available to reprotect the stack. 574 continuation = Interpreter::remove_activation_entry(); 575 #if COMPILER2_OR_JVMCI 576 // Count this for compilation purposes 577 h_method->interpreter_throwout_increment(THREAD); 578 #endif 579 } else { 580 // handler in this method => change bci/bcp to handler bci/bcp and continue there 581 handler_pc = h_method->code_base() + handler_bci; 582 h_method->set_exception_handler_entered(handler_bci); // profiling 583 #ifndef ZERO 584 set_bcp_and_mdp(handler_pc, current); 585 continuation = Interpreter::dispatch_table(vtos)[*handler_pc]; 586 #else 587 continuation = (address)(intptr_t) handler_bci; 588 #endif 589 } 590 591 // notify debugger of an exception catch 592 // (this is good for exceptions caught in native methods as well) 593 if (JvmtiExport::can_post_on_exceptions()) { 594 JvmtiExport::notice_unwind_due_to_exception(current, h_method(), handler_pc, h_exception(), (handler_pc != nullptr)); 595 } 596 597 current->set_vm_result_oop(h_exception()); 598 return continuation; 599 JRT_END 600 601 602 JRT_ENTRY(void, InterpreterRuntime::throw_pending_exception(JavaThread* current)) 603 assert(current->has_pending_exception(), "must only be called if there's an exception pending"); 604 // nothing to do - eventually we should remove this code entirely (see comments @ call sites) 605 JRT_END 606 607 608 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodError(JavaThread* current)) 609 THROW(vmSymbols::java_lang_AbstractMethodError()); 610 JRT_END 611 612 // This method is called from the "abstract_entry" of the interpreter. 613 // At that point, the arguments have already been removed from the stack 614 // and therefore we don't have the receiver object at our fingertips. (Though, 615 // on some platforms the receiver still resides in a register...). Thus, 616 // we have no choice but print an error message not containing the receiver 617 // type. 618 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current, 619 Method* missingMethod)) 620 ResourceMark rm(current); 621 assert(missingMethod != nullptr, "sanity"); 622 methodHandle m(current, missingMethod); 623 LinkResolver::throw_abstract_method_error(m, THREAD); 624 JRT_END 625 626 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current, 627 Klass* recvKlass, 628 Method* missingMethod)) 629 ResourceMark rm(current); 630 methodHandle mh = methodHandle(current, missingMethod); 631 LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD); 632 JRT_END 633 634 635 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current)) 636 THROW(vmSymbols::java_lang_IncompatibleClassChangeError()); 637 JRT_END 638 639 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current, 640 Klass* recvKlass, 641 Klass* interfaceKlass)) 642 ResourceMark rm(current); 643 char buf[1000]; 644 buf[0] = '\0'; 645 jio_snprintf(buf, sizeof(buf), 646 "Class %s does not implement the requested interface %s", 647 recvKlass ? recvKlass->external_name() : "nullptr", 648 interfaceKlass ? interfaceKlass->external_name() : "nullptr"); 649 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 650 JRT_END 651 652 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current)) 653 THROW(vmSymbols::java_lang_NullPointerException()); 654 JRT_END 655 656 //------------------------------------------------------------------------------------------------------------------------ 657 // Fields 658 // 659 660 void InterpreterRuntime::resolve_get_put(JavaThread* current, Bytecodes::Code bytecode) { 661 JavaThread* THREAD = current; 662 LastFrameAccessor last_frame(current); 663 constantPoolHandle pool(current, last_frame.method()->constants()); 664 methodHandle m(current, last_frame.method()); 665 666 resolve_get_put(bytecode, last_frame.get_index_u2(bytecode), m, pool, StaticMode::initialize_klass_preemptable, CHECK_AND_CLEAR_PREEMPTED); 667 } 668 669 void InterpreterRuntime::resolve_get_put(Bytecodes::Code bytecode, int field_index, 670 methodHandle& m, 671 constantPoolHandle& pool, 672 StaticMode static_mode, TRAPS) { 673 fieldDescriptor info; 674 bool is_put = (bytecode == Bytecodes::_putfield || bytecode == Bytecodes::_nofast_putfield || 675 bytecode == Bytecodes::_putstatic); 676 bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic); 677 678 { 679 JvmtiHideSingleStepping jhss(THREAD); 680 LinkResolver::resolve_field_access(info, pool, field_index, 681 m, bytecode, static_mode, CHECK); 682 } // end JvmtiHideSingleStepping 683 684 // check if link resolution caused cpCache to be updated 685 if (pool->resolved_field_entry_at(field_index)->is_resolved(bytecode)) return; 686 687 // compute auxiliary field attributes 688 TosState state = as_TosState(info.field_type()); 689 690 // Resolution of put instructions on final fields is delayed. That is required so that 691 // exceptions are thrown at the correct place (when the instruction is actually invoked). 692 // If we do not resolve an instruction in the current pass, leaving the put_code 693 // set to zero will cause the next put instruction to the same field to reresolve. 694 695 // Resolution of put instructions to final instance fields with invalid updates (i.e., 696 // to final instance fields with updates originating from a method different than <init>) 697 // is inhibited. A putfield instruction targeting an instance final field must throw 698 // an IllegalAccessError if the instruction is not in an instance 699 // initializer method <init>. If resolution were not inhibited, a putfield 700 // in an initializer method could be resolved in the initializer. Subsequent 701 // putfield instructions to the same field would then use cached information. 702 // As a result, those instructions would not pass through the VM. That is, 703 // checks in resolve_field_access() would not be executed for those instructions 704 // and the required IllegalAccessError would not be thrown. 705 // 706 // Also, we need to delay resolving getstatic and putstatic instructions until the 707 // class is initialized. This is required so that access to the static 708 // field will call the initialization function every time until the class 709 // is completely initialized ala. in 2.17.5 in JVM Specification. 710 InstanceKlass* klass = info.field_holder(); 711 bool uninitialized_static = is_static && !klass->is_initialized(); 712 bool has_initialized_final_update = info.field_holder()->major_version() >= 53 && 713 info.has_initialized_final_update(); 714 assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final"); 715 716 Bytecodes::Code get_code = (Bytecodes::Code)0; 717 Bytecodes::Code put_code = (Bytecodes::Code)0; 718 if (!uninitialized_static) { 719 get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield); 720 if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) { 721 put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield); 722 } 723 } 724 725 ResolvedFieldEntry* entry = pool->resolved_field_entry_at(field_index); 726 entry->set_flags(info.access_flags().is_final(), info.access_flags().is_volatile()); 727 entry->fill_in(info.field_holder(), info.offset(), 728 checked_cast<u2>(info.index()), checked_cast<u1>(state), 729 static_cast<u1>(get_code), static_cast<u1>(put_code)); 730 } 731 732 733 //------------------------------------------------------------------------------------------------------------------------ 734 // Synchronization 735 // 736 // The interpreter's synchronization code is factored out so that it can 737 // be shared by method invocation and synchronized blocks. 738 //%note synchronization_3 739 740 //%note monitor_1 741 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem)) 742 #ifdef ASSERT 743 current->last_frame().interpreter_frame_verify_monitor(elem); 744 #endif 745 Handle h_obj(current, elem->obj()); 746 assert(Universe::heap()->is_in_or_null(h_obj()), 747 "must be null or an object"); 748 ObjectSynchronizer::enter(h_obj, elem->lock(), current); 749 assert(Universe::heap()->is_in_or_null(elem->obj()), 750 "must be null or an object"); 751 #ifdef ASSERT 752 if (!current->preempting()) current->last_frame().interpreter_frame_verify_monitor(elem); 753 #endif 754 JRT_END 755 756 JRT_LEAF(void, InterpreterRuntime::monitorexit(BasicObjectLock* elem)) 757 oop obj = elem->obj(); 758 assert(Universe::heap()->is_in(obj), "must be an object"); 759 // The object could become unlocked through a JNI call, which we have no other checks for. 760 // Give a fatal message if CheckJNICalls. Otherwise we ignore it. 761 if (obj->is_unlocked()) { 762 if (CheckJNICalls) { 763 fatal("Object has been unlocked by JNI"); 764 } 765 return; 766 } 767 ObjectSynchronizer::exit(obj, elem->lock(), JavaThread::current()); 768 // Free entry. If it is not cleared, the exception handling code will try to unlock the monitor 769 // again at method exit or in the case of an exception. 770 elem->set_obj(nullptr); 771 JRT_END 772 773 774 JRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* current)) 775 THROW(vmSymbols::java_lang_IllegalMonitorStateException()); 776 JRT_END 777 778 779 JRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* current)) 780 // Returns an illegal exception to install into the current thread. The 781 // pending_exception flag is cleared so normal exception handling does not 782 // trigger. Any current installed exception will be overwritten. This 783 // method will be called during an exception unwind. 784 785 assert(!HAS_PENDING_EXCEPTION, "no pending exception"); 786 Handle exception(current, current->vm_result_oop()); 787 assert(exception() != nullptr, "vm result should be set"); 788 current->set_vm_result_oop(nullptr); // clear vm result before continuing (may cause memory leaks and assert failures) 789 exception = get_preinitialized_exception(vmClasses::IllegalMonitorStateException_klass(), CATCH); 790 current->set_vm_result_oop(exception()); 791 JRT_END 792 793 794 //------------------------------------------------------------------------------------------------------------------------ 795 // Invokes 796 797 JRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* current, Method* method, address bcp)) 798 return method->orig_bytecode_at(method->bci_from(bcp)); 799 JRT_END 800 801 JRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* current, Method* method, address bcp, Bytecodes::Code new_code)) 802 method->set_orig_bytecode_at(method->bci_from(bcp), new_code); 803 JRT_END 804 805 JRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* current, Method* method, address bcp)) 806 JvmtiExport::post_raw_breakpoint(current, method, bcp); 807 JRT_END 808 809 void InterpreterRuntime::resolve_invoke(JavaThread* current, Bytecodes::Code bytecode) { 810 LastFrameAccessor last_frame(current); 811 // extract receiver from the outgoing argument list if necessary 812 Handle receiver(current, nullptr); 813 if (bytecode == Bytecodes::_invokevirtual || bytecode == Bytecodes::_invokeinterface || 814 bytecode == Bytecodes::_invokespecial) { 815 ResourceMark rm(current); 816 methodHandle m (current, last_frame.method()); 817 Bytecode_invoke call(m, last_frame.bci()); 818 Symbol* signature = call.signature(); 819 receiver = Handle(current, last_frame.callee_receiver(signature)); 820 821 assert(Universe::heap()->is_in_or_null(receiver()), 822 "sanity check"); 823 assert(receiver.is_null() || 824 !Universe::heap()->is_in(receiver->klass()), 825 "sanity check"); 826 } 827 828 // resolve method 829 CallInfo info; 830 constantPoolHandle pool(current, last_frame.method()->constants()); 831 832 methodHandle resolved_method; 833 834 int method_index = last_frame.get_index_u2(bytecode); 835 { ProfileTrapsMark pt(current); 836 JvmtiHideSingleStepping jhss(current); 837 JavaThread* THREAD = current; // For exception macros. 838 LinkResolver::resolve_invoke(info, receiver, pool, 839 method_index, bytecode, 840 StaticMode::initialize_klass_preemptable, CHECK_AND_CLEAR_PREEMPTED); 841 resolved_method = methodHandle(current, info.resolved_method()); 842 ResourceMark rm(THREAD); 843 } // end JvmtiHideSingleStepping 844 845 update_invoke_cp_cache_entry(info, bytecode, resolved_method, pool, method_index); 846 } 847 848 void InterpreterRuntime::update_invoke_cp_cache_entry(CallInfo& info, Bytecodes::Code bytecode, 849 methodHandle& resolved_method, 850 constantPoolHandle& pool, 851 int method_index) { 852 // Don't allow safepoints until the method is cached. 853 NoSafepointVerifier nsv; 854 855 // check if link resolution caused cpCache to be updated 856 ConstantPoolCache* cache = pool->cache(); 857 if (cache->resolved_method_entry_at(method_index)->is_resolved(bytecode)) return; 858 859 #ifdef ASSERT 860 if (bytecode == Bytecodes::_invokeinterface) { 861 if (resolved_method->method_holder() == vmClasses::Object_klass()) { 862 // NOTE: THIS IS A FIX FOR A CORNER CASE in the JVM spec 863 // (see also CallInfo::set_interface for details) 864 assert(info.call_kind() == CallInfo::vtable_call || 865 info.call_kind() == CallInfo::direct_call, ""); 866 assert(resolved_method->is_final() || info.has_vtable_index(), 867 "should have been set already"); 868 } else if (!resolved_method->has_itable_index()) { 869 // Resolved something like CharSequence.toString. Use vtable not itable. 870 assert(info.call_kind() != CallInfo::itable_call, ""); 871 } else { 872 // Setup itable entry 873 assert(info.call_kind() == CallInfo::itable_call, ""); 874 int index = resolved_method->itable_index(); 875 assert(info.itable_index() == index, ""); 876 } 877 } else if (bytecode == Bytecodes::_invokespecial) { 878 assert(info.call_kind() == CallInfo::direct_call, "must be direct call"); 879 } else { 880 assert(info.call_kind() == CallInfo::direct_call || 881 info.call_kind() == CallInfo::vtable_call, ""); 882 } 883 #endif 884 // Get sender and only set cpCache entry to resolved if it is not an 885 // interface. The receiver for invokespecial calls within interface 886 // methods must be checked for every call. 887 InstanceKlass* sender = pool->pool_holder(); 888 889 switch (info.call_kind()) { 890 case CallInfo::direct_call: 891 cache->set_direct_call(bytecode, method_index, resolved_method, sender->is_interface()); 892 break; 893 case CallInfo::vtable_call: 894 cache->set_vtable_call(bytecode, method_index, resolved_method, info.vtable_index()); 895 break; 896 case CallInfo::itable_call: 897 cache->set_itable_call( 898 bytecode, 899 method_index, 900 info.resolved_klass(), 901 resolved_method, 902 info.itable_index()); 903 break; 904 default: ShouldNotReachHere(); 905 } 906 } 907 908 void InterpreterRuntime::cds_resolve_invoke(Bytecodes::Code bytecode, int method_index, 909 constantPoolHandle& pool, TRAPS) { 910 LinkInfo link_info(pool, method_index, bytecode, CHECK); 911 912 if (!link_info.resolved_klass()->is_instance_klass() || InstanceKlass::cast(link_info.resolved_klass())->is_linked()) { 913 CallInfo call_info; 914 switch (bytecode) { 915 case Bytecodes::_invokevirtual: LinkResolver::cds_resolve_virtual_call (call_info, link_info, CHECK); break; 916 case Bytecodes::_invokeinterface: LinkResolver::cds_resolve_interface_call(call_info, link_info, CHECK); break; 917 case Bytecodes::_invokespecial: LinkResolver::cds_resolve_special_call (call_info, link_info, CHECK); break; 918 919 default: fatal("Unimplemented: %s", Bytecodes::name(bytecode)); 920 } 921 methodHandle resolved_method(THREAD, call_info.resolved_method()); 922 guarantee(resolved_method->method_holder()->is_linked(), ""); 923 update_invoke_cp_cache_entry(call_info, bytecode, resolved_method, pool, method_index); 924 } else { 925 // FIXME: why a shared class is not linked yet? 926 // Can't link it here since there are no guarantees it'll be prelinked on the next run. 927 ResourceMark rm; 928 InstanceKlass* resolved_iklass = InstanceKlass::cast(link_info.resolved_klass()); 929 log_info(cds, resolve)("Not resolved: class not linked: %s %s %s", 930 resolved_iklass->is_shared() ? "is_shared" : "", 931 resolved_iklass->init_state_name(), 932 resolved_iklass->external_name()); 933 } 934 } 935 936 // First time execution: Resolve symbols, create a permanent MethodType object. 937 void InterpreterRuntime::resolve_invokehandle(JavaThread* current) { 938 const Bytecodes::Code bytecode = Bytecodes::_invokehandle; 939 LastFrameAccessor last_frame(current); 940 941 // resolve method 942 CallInfo info; 943 constantPoolHandle pool(current, last_frame.method()->constants()); 944 int method_index = last_frame.get_index_u2(bytecode); 945 { 946 JvmtiHideSingleStepping jhss(current); 947 JavaThread* THREAD = current; // For exception macros. 948 LinkResolver::resolve_invoke(info, Handle(), pool, 949 method_index, bytecode, 950 CHECK); 951 } // end JvmtiHideSingleStepping 952 953 pool->cache()->set_method_handle(method_index, info); 954 } 955 956 void InterpreterRuntime::cds_resolve_invokehandle(int raw_index, 957 constantPoolHandle& pool, TRAPS) { 958 const Bytecodes::Code bytecode = Bytecodes::_invokehandle; 959 CallInfo info; 960 LinkResolver::resolve_invoke(info, Handle(), pool, raw_index, bytecode, CHECK); 961 962 pool->cache()->set_method_handle(raw_index, info); 963 } 964 965 // First time execution: Resolve symbols, create a permanent CallSite object. 966 void InterpreterRuntime::resolve_invokedynamic(JavaThread* current) { 967 LastFrameAccessor last_frame(current); 968 const Bytecodes::Code bytecode = Bytecodes::_invokedynamic; 969 970 // resolve method 971 CallInfo info; 972 constantPoolHandle pool(current, last_frame.method()->constants()); 973 int index = last_frame.get_index_u4(bytecode); 974 { 975 JvmtiHideSingleStepping jhss(current); 976 JavaThread* THREAD = current; // For exception macros. 977 LinkResolver::resolve_invoke(info, Handle(), pool, 978 index, bytecode, CHECK); 979 } // end JvmtiHideSingleStepping 980 981 pool->cache()->set_dynamic_call(info, index); 982 } 983 984 void InterpreterRuntime::cds_resolve_invokedynamic(int raw_index, 985 constantPoolHandle& pool, TRAPS) { 986 const Bytecodes::Code bytecode = Bytecodes::_invokedynamic; 987 CallInfo info; 988 LinkResolver::resolve_invoke(info, Handle(), pool, raw_index, bytecode, CHECK); 989 pool->cache()->set_dynamic_call(info, raw_index); 990 } 991 992 // This function is the interface to the assembly code. It returns the resolved 993 // cpCache entry. This doesn't safepoint, but the helper routines safepoint. 994 // This function will check for redefinition! 995 JRT_ENTRY(void, InterpreterRuntime::resolve_from_cache(JavaThread* current, Bytecodes::Code bytecode)) { 996 switch (bytecode) { 997 case Bytecodes::_getstatic: 998 case Bytecodes::_putstatic: 999 case Bytecodes::_getfield: 1000 case Bytecodes::_putfield: 1001 resolve_get_put(current, bytecode); 1002 break; 1003 case Bytecodes::_invokevirtual: 1004 case Bytecodes::_invokespecial: 1005 case Bytecodes::_invokestatic: 1006 case Bytecodes::_invokeinterface: 1007 resolve_invoke(current, bytecode); 1008 break; 1009 case Bytecodes::_invokehandle: 1010 resolve_invokehandle(current); 1011 break; 1012 case Bytecodes::_invokedynamic: 1013 resolve_invokedynamic(current); 1014 break; 1015 default: 1016 fatal("unexpected bytecode: %s", Bytecodes::name(bytecode)); 1017 break; 1018 } 1019 } 1020 JRT_END 1021 1022 //------------------------------------------------------------------------------------------------------------------------ 1023 // Miscellaneous 1024 1025 1026 nmethod* InterpreterRuntime::frequency_counter_overflow(JavaThread* current, address branch_bcp) { 1027 // Enable WXWrite: the function is called directly by interpreter. 1028 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current)); 1029 1030 // frequency_counter_overflow_inner can throw async exception. 1031 nmethod* nm = frequency_counter_overflow_inner(current, branch_bcp); 1032 assert(branch_bcp != nullptr || nm == nullptr, "always returns null for non OSR requests"); 1033 if (branch_bcp != nullptr && nm != nullptr) { 1034 // This was a successful request for an OSR nmethod. Because 1035 // frequency_counter_overflow_inner ends with a safepoint check, 1036 // nm could have been unloaded so look it up again. It's unsafe 1037 // to examine nm directly since it might have been freed and used 1038 // for something else. 1039 LastFrameAccessor last_frame(current); 1040 Method* method = last_frame.method(); 1041 int bci = method->bci_from(last_frame.bcp()); 1042 nm = method->lookup_osr_nmethod_for(bci, CompLevel_none, false); 1043 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod(); 1044 if (nm != nullptr) { 1045 // in case the transition passed a safepoint we need to barrier this again 1046 if (!bs_nm->nmethod_osr_entry_barrier(nm)) { 1047 nm = nullptr; 1048 } 1049 } 1050 } 1051 if (nm != nullptr && current->is_interp_only_mode()) { 1052 // Normally we never get an nm if is_interp_only_mode() is true, because 1053 // policy()->event has a check for this and won't compile the method when 1054 // true. However, it's possible for is_interp_only_mode() to become true 1055 // during the compilation. We don't want to return the nm in that case 1056 // because we want to continue to execute interpreted. 1057 nm = nullptr; 1058 } 1059 #ifndef PRODUCT 1060 if (TraceOnStackReplacement) { 1061 if (nm != nullptr) { 1062 tty->print("OSR entry @ pc: " INTPTR_FORMAT ": ", p2i(nm->osr_entry())); 1063 nm->print(); 1064 } 1065 } 1066 #endif 1067 return nm; 1068 } 1069 1070 JRT_ENTRY(nmethod*, 1071 InterpreterRuntime::frequency_counter_overflow_inner(JavaThread* current, address branch_bcp)) 1072 // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized 1073 // flag, in case this method triggers classloading which will call into Java. 1074 UnlockFlagSaver fs(current); 1075 1076 LastFrameAccessor last_frame(current); 1077 assert(last_frame.is_interpreted_frame(), "must come from interpreter"); 1078 methodHandle method(current, last_frame.method()); 1079 const int branch_bci = branch_bcp != nullptr ? method->bci_from(branch_bcp) : InvocationEntryBci; 1080 const int bci = branch_bcp != nullptr ? method->bci_from(last_frame.bcp()) : InvocationEntryBci; 1081 1082 nmethod* osr_nm = CompilationPolicy::event(method, method, branch_bci, bci, CompLevel_none, nullptr, CHECK_NULL); 1083 1084 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod(); 1085 if (osr_nm != nullptr) { 1086 if (!bs_nm->nmethod_osr_entry_barrier(osr_nm)) { 1087 osr_nm = nullptr; 1088 } 1089 } 1090 return osr_nm; 1091 JRT_END 1092 1093 JRT_LEAF(jint, InterpreterRuntime::bcp_to_di(Method* method, address cur_bcp)) 1094 assert(ProfileInterpreter, "must be profiling interpreter"); 1095 int bci = method->bci_from(cur_bcp); 1096 MethodData* mdo = method->method_data(); 1097 if (mdo == nullptr) return 0; 1098 return mdo->bci_to_di(bci); 1099 JRT_END 1100 1101 #ifdef ASSERT 1102 JRT_LEAF(void, InterpreterRuntime::verify_mdp(Method* method, address bcp, address mdp)) 1103 assert(ProfileInterpreter, "must be profiling interpreter"); 1104 1105 MethodData* mdo = method->method_data(); 1106 assert(mdo != nullptr, "must not be null"); 1107 1108 int bci = method->bci_from(bcp); 1109 1110 address mdp2 = mdo->bci_to_dp(bci); 1111 if (mdp != mdp2) { 1112 ResourceMark rm; 1113 tty->print_cr("FAILED verify : actual mdp %p expected mdp %p @ bci %d", mdp, mdp2, bci); 1114 int current_di = mdo->dp_to_di(mdp); 1115 int expected_di = mdo->dp_to_di(mdp2); 1116 tty->print_cr(" actual di %d expected di %d", current_di, expected_di); 1117 int expected_approx_bci = mdo->data_at(expected_di)->bci(); 1118 int approx_bci = -1; 1119 if (current_di >= 0) { 1120 approx_bci = mdo->data_at(current_di)->bci(); 1121 } 1122 tty->print_cr(" actual bci is %d expected bci %d", approx_bci, expected_approx_bci); 1123 mdo->print_on(tty); 1124 method->print_codes(); 1125 } 1126 assert(mdp == mdp2, "wrong mdp"); 1127 JRT_END 1128 #endif // ASSERT 1129 1130 JRT_ENTRY(void, InterpreterRuntime::update_mdp_for_ret(JavaThread* current, int return_bci)) 1131 assert(ProfileInterpreter, "must be profiling interpreter"); 1132 ResourceMark rm(current); 1133 LastFrameAccessor last_frame(current); 1134 assert(last_frame.is_interpreted_frame(), "must come from interpreter"); 1135 MethodData* h_mdo = last_frame.method()->method_data(); 1136 1137 // Grab a lock to ensure atomic access to setting the return bci and 1138 // the displacement. This can block and GC, invalidating all naked oops. 1139 MutexLocker ml(RetData_lock); 1140 1141 // ProfileData is essentially a wrapper around a derived oop, so we 1142 // need to take the lock before making any ProfileData structures. 1143 ProfileData* data = h_mdo->data_at(h_mdo->dp_to_di(last_frame.mdp())); 1144 guarantee(data != nullptr, "profile data must be valid"); 1145 RetData* rdata = data->as_RetData(); 1146 address new_mdp = rdata->fixup_ret(return_bci, h_mdo); 1147 last_frame.set_mdp(new_mdp); 1148 JRT_END 1149 1150 JRT_ENTRY(MethodCounters*, InterpreterRuntime::build_method_counters(JavaThread* current, Method* m)) 1151 return Method::build_method_counters(current, m); 1152 JRT_END 1153 1154 1155 JRT_ENTRY(void, InterpreterRuntime::at_safepoint(JavaThread* current)) 1156 // We used to need an explicit preserve_arguments here for invoke bytecodes. However, 1157 // stack traversal automatically takes care of preserving arguments for invoke, so 1158 // this is no longer needed. 1159 1160 // JRT_END does an implicit safepoint check, hence we are guaranteed to block 1161 // if this is called during a safepoint 1162 1163 if (JvmtiExport::should_post_single_step()) { 1164 // This function is called by the interpreter when single stepping. Such single 1165 // stepping could unwind a frame. Then, it is important that we process any frames 1166 // that we might return into. 1167 StackWatermarkSet::before_unwind(current); 1168 1169 // We are called during regular safepoints and when the VM is 1170 // single stepping. If any thread is marked for single stepping, 1171 // then we may have JVMTI work to do. 1172 LastFrameAccessor last_frame(current); 1173 JvmtiExport::at_single_stepping_point(current, last_frame.method(), last_frame.bcp()); 1174 } 1175 JRT_END 1176 1177 JRT_LEAF(void, InterpreterRuntime::at_unwind(JavaThread* current)) 1178 assert(current == JavaThread::current(), "pre-condition"); 1179 // This function is called by the interpreter when the return poll found a reason 1180 // to call the VM. The reason could be that we are returning into a not yet safe 1181 // to access frame. We handle that below. 1182 // Note that this path does not check for single stepping, because we do not want 1183 // to single step when unwinding frames for an exception being thrown. Instead, 1184 // such single stepping code will use the safepoint table, which will use the 1185 // InterpreterRuntime::at_safepoint callback. 1186 StackWatermarkSet::before_unwind(current); 1187 JRT_END 1188 1189 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj, 1190 ResolvedFieldEntry *entry)) 1191 1192 // check the access_flags for the field in the klass 1193 1194 InstanceKlass* ik = entry->field_holder(); 1195 int index = entry->field_index(); 1196 if (!ik->field_status(index).is_access_watched()) return; 1197 1198 bool is_static = (obj == nullptr); 1199 HandleMark hm(current); 1200 1201 Handle h_obj; 1202 if (!is_static) { 1203 // non-static field accessors have an object, but we need a handle 1204 h_obj = Handle(current, obj); 1205 } 1206 InstanceKlass* field_holder = entry->field_holder(); // HERE 1207 jfieldID fid = jfieldIDWorkaround::to_jfieldID(field_holder, entry->field_offset(), is_static); 1208 LastFrameAccessor last_frame(current); 1209 JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), field_holder, h_obj, fid); 1210 JRT_END 1211 1212 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj, 1213 ResolvedFieldEntry *entry, jvalue *value)) 1214 1215 InstanceKlass* ik = entry->field_holder(); 1216 1217 // check the access_flags for the field in the klass 1218 int index = entry->field_index(); 1219 // bail out if field modifications are not watched 1220 if (!ik->field_status(index).is_modification_watched()) return; 1221 1222 char sig_type = '\0'; 1223 1224 switch((TosState)entry->tos_state()) { 1225 case btos: sig_type = JVM_SIGNATURE_BYTE; break; 1226 case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break; 1227 case ctos: sig_type = JVM_SIGNATURE_CHAR; break; 1228 case stos: sig_type = JVM_SIGNATURE_SHORT; break; 1229 case itos: sig_type = JVM_SIGNATURE_INT; break; 1230 case ftos: sig_type = JVM_SIGNATURE_FLOAT; break; 1231 case atos: sig_type = JVM_SIGNATURE_CLASS; break; 1232 case ltos: sig_type = JVM_SIGNATURE_LONG; break; 1233 case dtos: sig_type = JVM_SIGNATURE_DOUBLE; break; 1234 default: ShouldNotReachHere(); return; 1235 } 1236 bool is_static = (obj == nullptr); 1237 1238 HandleMark hm(current); 1239 jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, entry->field_offset(), is_static); 1240 jvalue fvalue; 1241 #ifdef _LP64 1242 fvalue = *value; 1243 #else 1244 // Long/double values are stored unaligned and also noncontiguously with 1245 // tagged stacks. We can't just do a simple assignment even in the non- 1246 // J/D cases because a C++ compiler is allowed to assume that a jvalue is 1247 // 8-byte aligned, and interpreter stack slots are only 4-byte aligned. 1248 // We assume that the two halves of longs/doubles are stored in interpreter 1249 // stack slots in platform-endian order. 1250 jlong_accessor u; 1251 jint* newval = (jint*)value; 1252 u.words[0] = newval[0]; 1253 u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag 1254 fvalue.j = u.long_value; 1255 #endif // _LP64 1256 1257 Handle h_obj; 1258 if (!is_static) { 1259 // non-static field accessors have an object, but we need a handle 1260 h_obj = Handle(current, obj); 1261 } 1262 1263 LastFrameAccessor last_frame(current); 1264 JvmtiExport::post_raw_field_modification(current, last_frame.method(), last_frame.bcp(), ik, h_obj, 1265 fid, sig_type, &fvalue); 1266 JRT_END 1267 1268 JRT_ENTRY(void, InterpreterRuntime::post_method_entry(JavaThread* current)) 1269 LastFrameAccessor last_frame(current); 1270 JvmtiExport::post_method_entry(current, last_frame.method(), last_frame.get_frame()); 1271 JRT_END 1272 1273 1274 // This is a JRT_BLOCK_ENTRY because we have to stash away the return oop 1275 // before transitioning to VM, and restore it after transitioning back 1276 // to Java. The return oop at the top-of-stack, is not walked by the GC. 1277 JRT_BLOCK_ENTRY(void, InterpreterRuntime::post_method_exit(JavaThread* current)) 1278 LastFrameAccessor last_frame(current); 1279 JvmtiExport::post_method_exit(current, last_frame.method(), last_frame.get_frame()); 1280 JRT_END 1281 1282 JRT_LEAF(int, InterpreterRuntime::interpreter_contains(address pc)) 1283 { 1284 return (Interpreter::contains(Continuation::get_top_return_pc_post_barrier(JavaThread::current(), pc)) ? 1 : 0); 1285 } 1286 JRT_END 1287 1288 1289 // Implementation of SignatureHandlerLibrary 1290 1291 #ifndef SHARING_FAST_NATIVE_FINGERPRINTS 1292 // Dummy definition (else normalization method is defined in CPU 1293 // dependent code) 1294 uint64_t InterpreterRuntime::normalize_fast_native_fingerprint(uint64_t fingerprint) { 1295 return fingerprint; 1296 } 1297 #endif 1298 1299 address SignatureHandlerLibrary::set_handler_blob() { 1300 BufferBlob* handler_blob = BufferBlob::create("native signature handlers", blob_size); 1301 if (handler_blob == nullptr) { 1302 return nullptr; 1303 } 1304 address handler = handler_blob->code_begin(); 1305 _handler_blob = handler_blob; 1306 _handler = handler; 1307 return handler; 1308 } 1309 1310 void SignatureHandlerLibrary::initialize() { 1311 if (_fingerprints != nullptr) { 1312 return; 1313 } 1314 if (set_handler_blob() == nullptr) { 1315 vm_exit_out_of_memory(blob_size, OOM_MALLOC_ERROR, "native signature handlers"); 1316 } 1317 1318 BufferBlob* bb = BufferBlob::create("Signature Handler Temp Buffer", 1319 SignatureHandlerLibrary::buffer_size); 1320 _buffer = bb->code_begin(); 1321 1322 _fingerprints = new (mtCode) GrowableArray<uint64_t>(32, mtCode); 1323 _handlers = new (mtCode) GrowableArray<address>(32, mtCode); 1324 } 1325 1326 address SignatureHandlerLibrary::set_handler(CodeBuffer* buffer) { 1327 address handler = _handler; 1328 int insts_size = buffer->pure_insts_size(); 1329 if (handler + insts_size > _handler_blob->code_end()) { 1330 // get a new handler blob 1331 handler = set_handler_blob(); 1332 } 1333 if (handler != nullptr) { 1334 memcpy(handler, buffer->insts_begin(), insts_size); 1335 pd_set_handler(handler); 1336 ICache::invalidate_range(handler, insts_size); 1337 _handler = handler + insts_size; 1338 } 1339 return handler; 1340 } 1341 1342 void SignatureHandlerLibrary::add(const methodHandle& method) { 1343 if (method->signature_handler() == nullptr) { 1344 // use slow signature handler if we can't do better 1345 int handler_index = -1; 1346 // check if we can use customized (fast) signature handler 1347 if (UseFastSignatureHandlers && method->size_of_parameters() <= Fingerprinter::fp_max_size_of_parameters) { 1348 // use customized signature handler 1349 MutexLocker mu(SignatureHandlerLibrary_lock); 1350 // make sure data structure is initialized 1351 initialize(); 1352 // lookup method signature's fingerprint 1353 uint64_t fingerprint = Fingerprinter(method).fingerprint(); 1354 // allow CPU dependent code to optimize the fingerprints for the fast handler 1355 fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint); 1356 handler_index = _fingerprints->find(fingerprint); 1357 // create handler if necessary 1358 if (handler_index < 0) { 1359 ResourceMark rm; 1360 ptrdiff_t align_offset = align_up(_buffer, CodeEntryAlignment) - (address)_buffer; 1361 CodeBuffer buffer((address)(_buffer + align_offset), 1362 checked_cast<int>(SignatureHandlerLibrary::buffer_size - align_offset)); 1363 InterpreterRuntime::SignatureHandlerGenerator(method, &buffer).generate(fingerprint); 1364 // copy into code heap 1365 address handler = set_handler(&buffer); 1366 if (handler == nullptr) { 1367 // use slow signature handler (without memorizing it in the fingerprints) 1368 } else { 1369 // debugging support 1370 if (PrintSignatureHandlers && (handler != Interpreter::slow_signature_handler())) { 1371 ttyLocker ttyl; 1372 tty->cr(); 1373 tty->print_cr("argument handler #%d for: %s %s (fingerprint = " UINT64_FORMAT ", %d bytes generated)", 1374 _handlers->length(), 1375 (method->is_static() ? "static" : "receiver"), 1376 method->name_and_sig_as_C_string(), 1377 fingerprint, 1378 buffer.insts_size()); 1379 if (buffer.insts_size() > 0) { 1380 Disassembler::decode(handler, handler + buffer.insts_size(), tty 1381 NOT_PRODUCT(COMMA &buffer.asm_remarks())); 1382 } 1383 #ifndef PRODUCT 1384 address rh_begin = Interpreter::result_handler(method()->result_type()); 1385 if (CodeCache::contains(rh_begin)) { 1386 // else it might be special platform dependent values 1387 tty->print_cr(" --- associated result handler ---"); 1388 address rh_end = rh_begin; 1389 while (*(int*)rh_end != 0) { 1390 rh_end += sizeof(int); 1391 } 1392 Disassembler::decode(rh_begin, rh_end); 1393 } else { 1394 tty->print_cr(" associated result handler: " PTR_FORMAT, p2i(rh_begin)); 1395 } 1396 #endif 1397 } 1398 // add handler to library 1399 _fingerprints->append(fingerprint); 1400 _handlers->append(handler); 1401 // set handler index 1402 assert(_fingerprints->length() == _handlers->length(), "sanity check"); 1403 handler_index = _fingerprints->length() - 1; 1404 } 1405 } 1406 // Set handler under SignatureHandlerLibrary_lock 1407 if (handler_index < 0) { 1408 // use generic signature handler 1409 method->set_signature_handler(Interpreter::slow_signature_handler()); 1410 } else { 1411 // set handler 1412 method->set_signature_handler(_handlers->at(handler_index)); 1413 } 1414 } else { 1415 DEBUG_ONLY(JavaThread::current()->check_possible_safepoint()); 1416 // use generic signature handler 1417 method->set_signature_handler(Interpreter::slow_signature_handler()); 1418 } 1419 } 1420 #ifdef ASSERT 1421 int handler_index = -1; 1422 int fingerprint_index = -2; 1423 { 1424 // '_handlers' and '_fingerprints' are 'GrowableArray's and are NOT synchronized 1425 // in any way if accessed from multiple threads. To avoid races with another 1426 // thread which may change the arrays in the above, mutex protected block, we 1427 // have to protect this read access here with the same mutex as well! 1428 MutexLocker mu(SignatureHandlerLibrary_lock); 1429 if (_handlers != nullptr) { 1430 handler_index = _handlers->find(method->signature_handler()); 1431 uint64_t fingerprint = Fingerprinter(method).fingerprint(); 1432 fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint); 1433 fingerprint_index = _fingerprints->find(fingerprint); 1434 } 1435 } 1436 assert(method->signature_handler() == Interpreter::slow_signature_handler() || 1437 handler_index == fingerprint_index, "sanity check"); 1438 #endif // ASSERT 1439 } 1440 1441 BufferBlob* SignatureHandlerLibrary::_handler_blob = nullptr; 1442 address SignatureHandlerLibrary::_handler = nullptr; 1443 GrowableArray<uint64_t>* SignatureHandlerLibrary::_fingerprints = nullptr; 1444 GrowableArray<address>* SignatureHandlerLibrary::_handlers = nullptr; 1445 address SignatureHandlerLibrary::_buffer = nullptr; 1446 1447 1448 JRT_ENTRY(void, InterpreterRuntime::prepare_native_call(JavaThread* current, Method* method)) 1449 methodHandle m(current, method); 1450 assert(m->is_native(), "sanity check"); 1451 // lookup native function entry point if it doesn't exist 1452 if (!m->has_native_function()) { 1453 NativeLookup::lookup(m, CHECK); 1454 } 1455 // make sure signature handler is installed 1456 SignatureHandlerLibrary::add(m); 1457 // The interpreter entry point checks the signature handler first, 1458 // before trying to fetch the native entry point and klass mirror. 1459 // We must set the signature handler last, so that multiple processors 1460 // preparing the same method will be sure to see non-null entry & mirror. 1461 JRT_END 1462 1463 #if defined(IA32) || defined(AMD64) || defined(ARM) 1464 JRT_LEAF(void, InterpreterRuntime::popframe_move_outgoing_args(JavaThread* current, void* src_address, void* dest_address)) 1465 assert(current == JavaThread::current(), "pre-condition"); 1466 if (src_address == dest_address) { 1467 return; 1468 } 1469 ResourceMark rm; 1470 LastFrameAccessor last_frame(current); 1471 assert(last_frame.is_interpreted_frame(), ""); 1472 jint bci = last_frame.bci(); 1473 methodHandle mh(current, last_frame.method()); 1474 Bytecode_invoke invoke(mh, bci); 1475 ArgumentSizeComputer asc(invoke.signature()); 1476 int size_of_arguments = (asc.size() + (invoke.has_receiver() ? 1 : 0)); // receiver 1477 Copy::conjoint_jbytes(src_address, dest_address, 1478 size_of_arguments * Interpreter::stackElementSize); 1479 JRT_END 1480 #endif 1481 1482 #if INCLUDE_JVMTI 1483 // This is a support of the JVMTI PopFrame interface. 1484 // Make sure it is an invokestatic of a polymorphic intrinsic that has a member_name argument 1485 // and return it as a vm_result_oop so that it can be reloaded in the list of invokestatic parameters. 1486 // The member_name argument is a saved reference (in local#0) to the member_name. 1487 // For backward compatibility with some JDK versions (7, 8) it can also be a direct method handle. 1488 // FIXME: remove DMH case after j.l.i.InvokerBytecodeGenerator code shape is updated. 1489 JRT_ENTRY(void, InterpreterRuntime::member_name_arg_or_null(JavaThread* current, address member_name, 1490 Method* method, address bcp)) 1491 Bytecodes::Code code = Bytecodes::code_at(method, bcp); 1492 if (code != Bytecodes::_invokestatic) { 1493 return; 1494 } 1495 ConstantPool* cpool = method->constants(); 1496 int cp_index = Bytes::get_native_u2(bcp + 1); 1497 Symbol* cname = cpool->klass_name_at(cpool->klass_ref_index_at(cp_index, code)); 1498 Symbol* mname = cpool->name_ref_at(cp_index, code); 1499 1500 if (MethodHandles::has_member_arg(cname, mname)) { 1501 oop member_name_oop = cast_to_oop(member_name); 1502 if (java_lang_invoke_DirectMethodHandle::is_instance(member_name_oop)) { 1503 // FIXME: remove after j.l.i.InvokerBytecodeGenerator code shape is updated. 1504 member_name_oop = java_lang_invoke_DirectMethodHandle::member(member_name_oop); 1505 } 1506 current->set_vm_result_oop(member_name_oop); 1507 } else { 1508 current->set_vm_result_oop(nullptr); 1509 } 1510 JRT_END 1511 #endif // INCLUDE_JVMTI 1512 1513 #ifndef PRODUCT 1514 // This must be a JRT_LEAF function because the interpreter must save registers on x86 to 1515 // call this, which changes rsp and makes the interpreter's expression stack not walkable. 1516 // The generated code still uses call_VM because that will set up the frame pointer for 1517 // bcp and method. 1518 JRT_LEAF(intptr_t, InterpreterRuntime::trace_bytecode(JavaThread* current, intptr_t preserve_this_value, intptr_t tos, intptr_t tos2)) 1519 assert(current == JavaThread::current(), "pre-condition"); 1520 LastFrameAccessor last_frame(current); 1521 assert(last_frame.is_interpreted_frame(), "must be an interpreted frame"); 1522 methodHandle mh(current, last_frame.method()); 1523 BytecodeTracer::trace_interpreter(mh, last_frame.bcp(), tos, tos2); 1524 return preserve_this_value; 1525 JRT_END 1526 #endif // !PRODUCT