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