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