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