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