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