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