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