1 /* 2 * Copyright (c) 1997, 2023, 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 "code/codeBehaviours.hpp" 27 #include "code/codeCache.hpp" 28 #include "code/compiledIC.hpp" 29 #include "code/icBuffer.hpp" 30 #include "code/nmethod.hpp" 31 #include "code/vtableStubs.hpp" 32 #include "interpreter/interpreter.hpp" 33 #include "interpreter/linkResolver.hpp" 34 #include "memory/metadataFactory.hpp" 35 #include "memory/oopFactory.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "memory/universe.hpp" 38 #include "oops/klass.inline.hpp" 39 #include "oops/method.inline.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "oops/symbol.hpp" 42 #include "runtime/continuationEntry.hpp" 43 #include "runtime/handles.inline.hpp" 44 #include "runtime/icache.hpp" 45 #include "runtime/safepoint.hpp" 46 #include "runtime/sharedRuntime.hpp" 47 #include "runtime/stubRoutines.hpp" 48 #include "sanitizers/leak.hpp" 49 #include "utilities/events.hpp" 50 51 52 // Every time a compiled IC is changed or its type is being accessed, 53 // either the CompiledIC_lock must be set or we must be at a safe point. 54 55 CompiledICLocker::CompiledICLocker(CompiledMethod* method) 56 : _method(method), 57 _behaviour(CompiledICProtectionBehaviour::current()), 58 _locked(_behaviour->lock(_method)) { 59 } 60 61 CompiledICLocker::~CompiledICLocker() { 62 if (_locked) { 63 _behaviour->unlock(_method); 64 } 65 } 66 67 bool CompiledICLocker::is_safe(CompiledMethod* method) { 68 return CompiledICProtectionBehaviour::current()->is_safe(method); 69 } 70 71 bool CompiledICLocker::is_safe(address code) { 72 CodeBlob* cb = CodeCache::find_blob(code); 73 assert(cb != nullptr && cb->is_compiled(), "must be compiled"); 74 CompiledMethod* cm = cb->as_compiled_method(); 75 return CompiledICProtectionBehaviour::current()->is_safe(cm); 76 } 77 78 //----------------------------------------------------------------------------- 79 // Low-level access to an inline cache. Private, since they might not be 80 // MT-safe to use. 81 82 void* CompiledIC::cached_value() const { 83 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 84 assert (!is_optimized(), "an optimized virtual call does not have a cached metadata"); 85 86 if (!is_in_transition_state()) { 87 void* data = get_data(); 88 // If we let the metadata value here be initialized to zero... 89 assert(data != nullptr || Universe::non_oop_word() == nullptr, 90 "no raw nulls in CompiledIC metadatas, because of patching races"); 91 return (data == (void*)Universe::non_oop_word()) ? nullptr : data; 92 } else { 93 return InlineCacheBuffer::cached_value_for((CompiledIC *)this); 94 } 95 } 96 97 98 void CompiledIC::internal_set_ic_destination(address entry_point, bool is_icstub, void* cache, bool is_icholder) { 99 assert(entry_point != nullptr, "must set legal entry point"); 100 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 101 assert (!is_optimized() || cache == nullptr, "an optimized virtual call does not have a cached metadata"); 102 assert (cache == nullptr || cache != (Metadata*)badOopVal, "invalid metadata"); 103 104 assert(!is_icholder || is_icholder_entry(entry_point), "must be"); 105 106 // Don't use ic_destination for this test since that forwards 107 // through ICBuffer instead of returning the actual current state of 108 // the CompiledIC. 109 if (is_icholder_entry(_call->destination())) { 110 // When patching for the ICStub case the cached value isn't 111 // overwritten until the ICStub copied into the CompiledIC during 112 // the next safepoint. Make sure that the CompiledICHolder* is 113 // marked for release at this point since it won't be identifiable 114 // once the entry point is overwritten. 115 InlineCacheBuffer::queue_for_release((CompiledICHolder*)get_data()); 116 } 117 118 if (TraceCompiledIC) { 119 tty->print(" "); 120 print_compiled_ic(); 121 tty->print(" changing destination to " INTPTR_FORMAT, p2i(entry_point)); 122 if (!is_optimized()) { 123 tty->print(" changing cached %s to " INTPTR_FORMAT, is_icholder ? "icholder" : "metadata", p2i((address)cache)); 124 } 125 if (is_icstub) { 126 tty->print(" (icstub)"); 127 } 128 tty->cr(); 129 } 130 131 { 132 CodeBlob* cb = CodeCache::find_blob(_call->instruction_address()); 133 assert(cb != nullptr && cb->is_compiled(), "must be compiled"); 134 _call->set_destination_mt_safe(entry_point); 135 } 136 137 if (is_optimized() || is_icstub) { 138 // Optimized call sites don't have a cache value and ICStub call 139 // sites only change the entry point. Changing the value in that 140 // case could lead to MT safety issues. 141 assert(cache == nullptr, "must be null"); 142 return; 143 } 144 145 if (cache == nullptr) cache = Universe::non_oop_word(); 146 147 set_data((intptr_t)cache); 148 } 149 150 151 void CompiledIC::set_ic_destination(ICStub* stub) { 152 internal_set_ic_destination(stub->code_begin(), true, nullptr, false); 153 } 154 155 156 157 address CompiledIC::ic_destination() const { 158 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 159 if (!is_in_transition_state()) { 160 return _call->destination(); 161 } else { 162 return InlineCacheBuffer::ic_destination_for((CompiledIC *)this); 163 } 164 } 165 166 167 bool CompiledIC::is_in_transition_state() const { 168 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 169 return InlineCacheBuffer::contains(_call->destination());; 170 } 171 172 173 bool CompiledIC::is_icholder_call() const { 174 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 175 return !_is_optimized && is_icholder_entry(ic_destination()); 176 } 177 178 // Returns native address of 'call' instruction in inline-cache. Used by 179 // the InlineCacheBuffer when it needs to find the stub. 180 address CompiledIC::stub_address() const { 181 assert(is_in_transition_state(), "should only be called when we are in a transition state"); 182 return _call->destination(); 183 } 184 185 // Clears the IC stub if the compiled IC is in transition state 186 void CompiledIC::clear_ic_stub() { 187 if (is_in_transition_state()) { 188 ICStub* stub = ICStub_from_destination_address(stub_address()); 189 stub->clear(); 190 } 191 } 192 193 //----------------------------------------------------------------------------- 194 // High-level access to an inline cache. Guaranteed to be MT-safe. 195 196 void CompiledIC::initialize_from_iter(RelocIterator* iter) { 197 assert(iter->addr() == _call->instruction_address(), "must find ic_call"); 198 199 if (iter->type() == relocInfo::virtual_call_type) { 200 virtual_call_Relocation* r = iter->virtual_call_reloc(); 201 _is_optimized = false; 202 _value = _call->get_load_instruction(r); 203 } else { 204 assert(iter->type() == relocInfo::opt_virtual_call_type, "must be a virtual call"); 205 _is_optimized = true; 206 _value = nullptr; 207 } 208 } 209 210 CompiledIC::CompiledIC(CompiledMethod* cm, NativeCall* call) 211 : _method(cm) 212 { 213 _call = _method->call_wrapper_at((address) call); 214 address ic_call = _call->instruction_address(); 215 216 assert(ic_call != nullptr, "ic_call address must be set"); 217 assert(cm != nullptr, "must pass compiled method"); 218 assert(cm->contains(ic_call), "must be in compiled method"); 219 220 // Search for the ic_call at the given address. 221 RelocIterator iter(cm, ic_call, ic_call+1); 222 bool ret = iter.next(); 223 assert(ret == true, "relocInfo must exist at this address"); 224 assert(iter.addr() == ic_call, "must find ic_call"); 225 226 initialize_from_iter(&iter); 227 } 228 229 CompiledIC::CompiledIC(RelocIterator* iter) 230 : _method(iter->code()) 231 { 232 _call = _method->call_wrapper_at(iter->addr()); 233 address ic_call = _call->instruction_address(); 234 235 CompiledMethod* nm = iter->code(); 236 assert(ic_call != nullptr, "ic_call address must be set"); 237 assert(nm != nullptr, "must pass compiled method"); 238 assert(nm->contains(ic_call), "must be in compiled method"); 239 240 initialize_from_iter(iter); 241 } 242 243 // This function may fail for two reasons: either due to running out of vtable 244 // stubs, or due to running out of IC stubs in an attempted transition to a 245 // transitional state. The needs_ic_stub_refill value will be set if the failure 246 // was due to running out of IC stubs, in which case the caller will refill IC 247 // stubs and retry. 248 bool CompiledIC::set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, 249 bool& needs_ic_stub_refill, TRAPS) { 250 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 251 assert(!is_optimized(), "cannot set an optimized virtual call to megamorphic"); 252 assert(is_call_to_compiled() || is_call_to_interpreted(), "going directly to megamorphic?"); 253 254 address entry; 255 if (call_info->call_kind() == CallInfo::itable_call) { 256 assert(bytecode == Bytecodes::_invokeinterface, ""); 257 int itable_index = call_info->itable_index(); 258 entry = VtableStubs::find_itable_stub(itable_index); 259 if (entry == nullptr) { 260 return false; 261 } 262 #ifdef ASSERT 263 int index = call_info->resolved_method()->itable_index(); 264 assert(index == itable_index, "CallInfo pre-computes this"); 265 InstanceKlass* k = call_info->resolved_method()->method_holder(); 266 assert(k->verify_itable_index(itable_index), "sanity check"); 267 #endif //ASSERT 268 CompiledICHolder* holder = new CompiledICHolder(call_info->resolved_method()->method_holder(), 269 call_info->resolved_klass(), false); 270 holder->claim(); 271 if (!InlineCacheBuffer::create_transition_stub(this, holder, entry)) { 272 delete holder; 273 needs_ic_stub_refill = true; 274 return false; 275 } 276 // LSan appears unable to follow malloc-based memory consistently when embedded as an immediate 277 // in generated machine code. So we have to ignore it. 278 LSAN_IGNORE_OBJECT(holder); 279 } else { 280 assert(call_info->call_kind() == CallInfo::vtable_call, "either itable or vtable"); 281 // Can be different than selected_method->vtable_index(), due to package-private etc. 282 int vtable_index = call_info->vtable_index(); 283 assert(call_info->resolved_klass()->verify_vtable_index(vtable_index), "sanity check"); 284 entry = VtableStubs::find_vtable_stub(vtable_index); 285 if (entry == nullptr) { 286 return false; 287 } 288 if (!InlineCacheBuffer::create_transition_stub(this, nullptr, entry)) { 289 needs_ic_stub_refill = true; 290 return false; 291 } 292 } 293 294 if (TraceICs) { 295 ResourceMark rm; 296 assert(call_info->selected_method() != nullptr, "Unexpected null selected method"); 297 tty->print_cr ("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT, 298 p2i(instruction_address()), call_info->selected_method()->print_value_string(), p2i(entry)); 299 } 300 301 // We can't check this anymore. With lazy deopt we could have already 302 // cleaned this IC entry before we even return. This is possible if 303 // we ran out of space in the inline cache buffer trying to do the 304 // set_next and we safepointed to free up space. This is a benign 305 // race because the IC entry was complete when we safepointed so 306 // cleaning it immediately is harmless. 307 // assert(is_megamorphic(), "sanity check"); 308 return true; 309 } 310 311 312 // true if destination is megamorphic stub 313 bool CompiledIC::is_megamorphic() const { 314 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 315 assert(!is_optimized(), "an optimized call cannot be megamorphic"); 316 317 // Cannot rely on cached_value. It is either an interface or a method. 318 return VtableStubs::entry_point(ic_destination()) != nullptr; 319 } 320 321 bool CompiledIC::is_call_to_compiled() const { 322 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 323 324 CodeBlob* cb = CodeCache::find_blob(ic_destination()); 325 bool is_monomorphic = (cb != nullptr && cb->is_compiled()); 326 // Check that the cached_value is a klass for non-optimized monomorphic calls 327 // This assertion is invalid for compiler1: a call that does not look optimized (no static stub) can be used 328 // for calling directly to vep without using the inline cache (i.e., cached_value == nullptr). 329 // For JVMCI this occurs because CHA is only used to improve inlining so call sites which could be optimized 330 // virtuals because there are no currently loaded subclasses of a type are left as virtual call sites. 331 #ifdef ASSERT 332 CodeBlob* caller = CodeCache::find_blob(instruction_address()); 333 bool is_c1_or_jvmci_method = caller->is_compiled_by_c1() || caller->is_compiled_by_jvmci(); 334 assert( is_c1_or_jvmci_method || 335 !is_monomorphic || 336 is_optimized() || 337 (cached_metadata() != nullptr && cached_metadata()->is_klass()), "sanity check"); 338 #endif // ASSERT 339 return is_monomorphic; 340 } 341 342 343 bool CompiledIC::is_call_to_interpreted() const { 344 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 345 // Call to interpreter if destination is either calling to a stub (if it 346 // is optimized), or calling to an I2C blob 347 bool is_call_to_interpreted = false; 348 if (!is_optimized()) { 349 CodeBlob* cb = CodeCache::find_blob(ic_destination()); 350 is_call_to_interpreted = (cb != nullptr && cb->is_adapter_blob()); 351 assert(!is_call_to_interpreted || (is_icholder_call() && cached_icholder() != nullptr), "sanity check"); 352 } else { 353 // Check if we are calling into our own codeblob (i.e., to a stub) 354 address dest = ic_destination(); 355 #ifdef ASSERT 356 { 357 _call->verify_resolve_call(dest); 358 } 359 #endif /* ASSERT */ 360 is_call_to_interpreted = _call->is_call_to_interpreted(dest); 361 } 362 return is_call_to_interpreted; 363 } 364 365 bool CompiledIC::set_to_clean(bool in_use) { 366 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 367 if (TraceInlineCacheClearing || TraceICs) { 368 tty->print_cr("IC@" INTPTR_FORMAT ": set to clean", p2i(instruction_address())); 369 print(); 370 } 371 372 address entry = _call->get_resolve_call_stub(is_optimized()); 373 374 bool safe_transition = _call->is_safe_for_patching() || !in_use || is_optimized() || SafepointSynchronize::is_at_safepoint(); 375 376 if (safe_transition) { 377 // Kill any leftover stub we might have too 378 clear_ic_stub(); 379 if (is_optimized()) { 380 set_ic_destination(entry); 381 } else { 382 set_ic_destination_and_value(entry, (void*)nullptr); 383 } 384 } else { 385 // Unsafe transition - create stub. 386 if (!InlineCacheBuffer::create_transition_stub(this, nullptr, entry)) { 387 return false; 388 } 389 } 390 // We can't check this anymore. With lazy deopt we could have already 391 // cleaned this IC entry before we even return. This is possible if 392 // we ran out of space in the inline cache buffer trying to do the 393 // set_next and we safepointed to free up space. This is a benign 394 // race because the IC entry was complete when we safepointed so 395 // cleaning it immediately is harmless. 396 // assert(is_clean(), "sanity check"); 397 return true; 398 } 399 400 bool CompiledIC::is_clean() const { 401 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 402 bool is_clean = false; 403 address dest = ic_destination(); 404 is_clean = dest == _call->get_resolve_call_stub(is_optimized()); 405 assert(!is_clean || is_optimized() || cached_value() == nullptr, "sanity check"); 406 return is_clean; 407 } 408 409 bool CompiledIC::set_to_monomorphic(CompiledICInfo& info) { 410 assert(CompiledICLocker::is_safe(_method), "mt unsafe call"); 411 // Updating a cache to the wrong entry can cause bugs that are very hard 412 // to track down - if cache entry gets invalid - we just clean it. In 413 // this way it is always the same code path that is responsible for 414 // updating and resolving an inline cache 415 // 416 // The above is no longer true. SharedRuntime::fixup_callers_callsite will change optimized 417 // callsites. In addition ic_miss code will update a site to monomorphic if it determines 418 // that an monomorphic call to the interpreter can now be monomorphic to compiled code. 419 // 420 // In both of these cases the only thing being modified is the jump/call target and these 421 // transitions are mt_safe 422 423 Thread *thread = Thread::current(); 424 if (info.to_interpreter()) { 425 // Call to interpreter 426 if (info.is_optimized() && is_optimized()) { 427 assert(is_clean(), "unsafe IC path"); 428 // the call analysis (callee structure) specifies that the call is optimized 429 // (either because of CHA or the static target is final) 430 // At code generation time, this call has been emitted as static call 431 // Call via stub 432 assert(info.cached_metadata() != nullptr && info.cached_metadata()->is_method(), "sanity check"); 433 methodHandle method (thread, (Method*)info.cached_metadata()); 434 _call->set_to_interpreted(method, info); 435 436 if (TraceICs) { 437 ResourceMark rm(thread); 438 tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to interpreter: %s", 439 p2i(instruction_address()), 440 method->print_value_string()); 441 } 442 } else { 443 // Call via method-klass-holder 444 CompiledICHolder* holder = info.claim_cached_icholder(); 445 if (!InlineCacheBuffer::create_transition_stub(this, holder, info.entry())) { 446 delete holder; 447 return false; 448 } 449 // LSan appears unable to follow malloc-based memory consistently when embedded as an 450 // immediate in generated machine code. So we have to ignore it. 451 LSAN_IGNORE_OBJECT(holder); 452 if (TraceICs) { 453 ResourceMark rm(thread); 454 tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to interpreter via icholder ", p2i(instruction_address())); 455 } 456 } 457 } else { 458 // Call to compiled code 459 bool static_bound = info.is_optimized() || (info.cached_metadata() == nullptr); 460 #ifdef ASSERT 461 CodeBlob* cb = CodeCache::find_blob(info.entry()); 462 assert (cb != nullptr && cb->is_compiled(), "must be compiled!"); 463 #endif /* ASSERT */ 464 465 // This is MT safe if we come from a clean-cache and go through a 466 // non-verified entry point 467 bool safe = SafepointSynchronize::is_at_safepoint() || 468 (!is_in_transition_state() && (info.is_optimized() || static_bound || is_clean())); 469 470 if (!safe) { 471 if (!InlineCacheBuffer::create_transition_stub(this, info.cached_metadata(), info.entry())) { 472 return false; 473 } 474 } else { 475 if (is_optimized()) { 476 set_ic_destination(info.entry()); 477 } else { 478 set_ic_destination_and_value(info.entry(), info.cached_metadata()); 479 } 480 } 481 482 if (TraceICs) { 483 ResourceMark rm(thread); 484 assert(info.cached_metadata() == nullptr || info.cached_metadata()->is_klass(), "must be"); 485 tty->print_cr ("IC@" INTPTR_FORMAT ": monomorphic to compiled (rcvr klass = %s) %s", 486 p2i(instruction_address()), 487 (info.cached_metadata() != nullptr) ? ((Klass*)info.cached_metadata())->print_value_string() : "nullptr", 488 (safe) ? "" : " via stub"); 489 } 490 } 491 // We can't check this anymore. With lazy deopt we could have already 492 // cleaned this IC entry before we even return. This is possible if 493 // we ran out of space in the inline cache buffer trying to do the 494 // set_next and we safepointed to free up space. This is a benign 495 // race because the IC entry was complete when we safepointed so 496 // cleaning it immediately is harmless. 497 // assert(is_call_to_compiled() || is_call_to_interpreted(), "sanity check"); 498 return true; 499 } 500 501 502 // is_optimized: Compiler has generated an optimized call (i.e. fixed, no inline cache) 503 // static_bound: The call can be static bound. If it isn't also optimized, the property 504 // wasn't provable at time of compilation. An optimized call will have any necessary 505 // null check, while a static_bound won't. A static_bound (but not optimized) must 506 // therefore use the unverified entry point. 507 void CompiledIC::compute_monomorphic_entry(const methodHandle& method, 508 Klass* receiver_klass, 509 bool is_optimized, 510 bool static_bound, 511 bool caller_is_nmethod, 512 CompiledICInfo& info, 513 TRAPS) { 514 CompiledMethod* method_code = method->code(); 515 516 address entry = nullptr; 517 if (method_code != nullptr && method_code->is_in_use() && !method_code->is_unloading()) { 518 assert(method_code->is_compiled(), "must be compiled"); 519 // Call to compiled code 520 // 521 // Note: the following problem exists with Compiler1: 522 // - at compile time we may or may not know if the destination is final 523 // - if we know that the destination is final (is_optimized), we will emit 524 // an optimized virtual call (no inline cache), and need a Method* to make 525 // a call to the interpreter 526 // - if we don't know if the destination is final, we emit a standard 527 // virtual call, and use CompiledICHolder to call interpreted code 528 // (no static call stub has been generated) 529 // - In the case that we here notice the call is static bound we 530 // convert the call into what looks to be an optimized virtual call, 531 // but we must use the unverified entry point (since there will be no 532 // null check on a call when the target isn't loaded). 533 // This causes problems when verifying the IC because 534 // it looks vanilla but is optimized. Code in is_call_to_interpreted 535 // is aware of this and weakens its asserts. 536 if (is_optimized) { 537 entry = method_code->verified_entry_point(); 538 } else { 539 entry = method_code->entry_point(); 540 } 541 } 542 if (entry != nullptr) { 543 // Call to near compiled code. 544 info.set_compiled_entry(entry, is_optimized ? nullptr : receiver_klass, is_optimized); 545 } else { 546 if (is_optimized) { 547 // Use stub entry 548 info.set_interpreter_entry(method()->get_c2i_entry(), method()); 549 } else { 550 // Use icholder entry 551 assert(method_code == nullptr || method_code->is_compiled(), "must be compiled"); 552 CompiledICHolder* holder = new CompiledICHolder(method(), receiver_klass); 553 info.set_icholder_entry(method()->get_c2i_unverified_entry(), holder); 554 } 555 } 556 assert(info.is_optimized() == is_optimized, "must agree"); 557 } 558 559 560 bool CompiledIC::is_icholder_entry(address entry) { 561 CodeBlob* cb = CodeCache::find_blob(entry); 562 if (cb == nullptr) { 563 return false; 564 } 565 if (cb->is_adapter_blob()) { 566 return true; 567 } else if (cb->is_vtable_blob()) { 568 return VtableStubs::is_icholder_entry(entry); 569 } 570 return false; 571 } 572 573 bool CompiledIC::is_icholder_call_site(virtual_call_Relocation* call_site, const CompiledMethod* cm) { 574 // This call site might have become stale so inspect it carefully. 575 address dest = cm->call_wrapper_at(call_site->addr())->destination(); 576 return is_icholder_entry(dest); 577 } 578 579 // ---------------------------------------------------------------------------- 580 581 bool CompiledStaticCall::set_to_clean(bool in_use) { 582 // in_use is unused but needed to match template function in CompiledMethod 583 assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call"); 584 // Reset call site 585 set_destination_mt_safe(resolve_call_stub()); 586 587 // Do not reset stub here: It is too expensive to call find_stub. 588 // Instead, rely on caller (nmethod::clear_inline_caches) to clear 589 // both the call and its stub. 590 return true; 591 } 592 593 bool CompiledStaticCall::is_clean() const { 594 return destination() == resolve_call_stub(); 595 } 596 597 bool CompiledStaticCall::is_call_to_compiled() const { 598 return CodeCache::contains(destination()); 599 } 600 601 bool CompiledDirectStaticCall::is_call_to_interpreted() const { 602 // It is a call to interpreted, if it calls to a stub. Hence, the destination 603 // must be in the stub part of the nmethod that contains the call 604 CompiledMethod* cm = CodeCache::find_compiled(instruction_address()); 605 return cm->stub_contains(destination()); 606 } 607 608 void CompiledStaticCall::set_to_compiled(address entry) { 609 if (TraceICs) { 610 ResourceMark rm; 611 tty->print_cr("%s@" INTPTR_FORMAT ": set_to_compiled " INTPTR_FORMAT, 612 name(), 613 p2i(instruction_address()), 614 p2i(entry)); 615 } 616 // Call to compiled code 617 assert(CodeCache::contains(entry), "wrong entry point"); 618 set_destination_mt_safe(entry); 619 } 620 621 void CompiledStaticCall::set(const StaticCallInfo& info) { 622 assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call"); 623 // Updating a cache to the wrong entry can cause bugs that are very hard 624 // to track down - if cache entry gets invalid - we just clean it. In 625 // this way it is always the same code path that is responsible for 626 // updating and resolving an inline cache 627 assert(is_clean(), "do not update a call entry - use clean"); 628 629 if (info._to_interpreter) { 630 // Call to interpreted code 631 set_to_interpreted(info.callee(), info.entry()); 632 } else { 633 set_to_compiled(info.entry()); 634 } 635 } 636 637 // Compute settings for a CompiledStaticCall. Since we might have to set 638 // the stub when calling to the interpreter, we need to return arguments. 639 void CompiledStaticCall::compute_entry(const methodHandle& m, bool caller_is_nmethod, StaticCallInfo& info) { 640 CompiledMethod* m_code = m->code(); 641 info._callee = m; 642 if (m_code != nullptr && m_code->is_in_use() && !m_code->is_unloading()) { 643 info._to_interpreter = false; 644 info._entry = m_code->verified_entry_point(); 645 } else { 646 // Callee is interpreted code. In any case entering the interpreter 647 // puts a converter-frame on the stack to save arguments. 648 assert(!m->is_method_handle_intrinsic(), "Compiled code should never call interpreter MH intrinsics"); 649 info._to_interpreter = true; 650 info._entry = m()->get_c2i_entry(); 651 } 652 } 653 654 void CompiledStaticCall::compute_entry_for_continuation_entry(const methodHandle& m, StaticCallInfo& info) { 655 if (ContinuationEntry::is_interpreted_call(instruction_address())) { 656 info._to_interpreter = true; 657 info._entry = m()->get_c2i_entry(); 658 } 659 } 660 661 address CompiledDirectStaticCall::find_stub_for(address instruction) { 662 // Find reloc. information containing this call-site 663 RelocIterator iter((nmethod*)nullptr, instruction); 664 while (iter.next()) { 665 if (iter.addr() == instruction) { 666 switch(iter.type()) { 667 case relocInfo::static_call_type: 668 return iter.static_call_reloc()->static_stub(); 669 // We check here for opt_virtual_call_type, since we reuse the code 670 // from the CompiledIC implementation 671 case relocInfo::opt_virtual_call_type: 672 return iter.opt_virtual_call_reloc()->static_stub(); 673 case relocInfo::poll_type: 674 case relocInfo::poll_return_type: // A safepoint can't overlap a call. 675 default: 676 ShouldNotReachHere(); 677 } 678 } 679 } 680 return nullptr; 681 } 682 683 address CompiledDirectStaticCall::find_stub() { 684 return CompiledDirectStaticCall::find_stub_for(instruction_address()); 685 } 686 687 address CompiledDirectStaticCall::resolve_call_stub() const { 688 return SharedRuntime::get_resolve_static_call_stub(); 689 } 690 691 //----------------------------------------------------------------------------- 692 // Non-product mode code 693 #ifndef PRODUCT 694 695 void CompiledIC::verify() { 696 _call->verify(); 697 assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted() 698 || is_optimized() || is_megamorphic(), "sanity check"); 699 } 700 701 void CompiledIC::print() { 702 print_compiled_ic(); 703 tty->cr(); 704 } 705 706 void CompiledIC::print_compiled_ic() { 707 tty->print("Inline cache at " INTPTR_FORMAT ", calling %s " INTPTR_FORMAT " cached_value " INTPTR_FORMAT, 708 p2i(instruction_address()), is_call_to_interpreted() ? "interpreted " : "", p2i(ic_destination()), p2i(is_optimized() ? nullptr : cached_value())); 709 } 710 711 void CompiledDirectStaticCall::print() { 712 tty->print("static call at " INTPTR_FORMAT " -> ", p2i(instruction_address())); 713 if (is_clean()) { 714 tty->print("clean"); 715 } else if (is_call_to_compiled()) { 716 tty->print("compiled"); 717 } else if (is_call_to_interpreted()) { 718 tty->print("interpreted"); 719 } 720 tty->cr(); 721 } 722 723 void CompiledDirectStaticCall::verify_mt_safe(const methodHandle& callee, address entry, 724 NativeMovConstReg* method_holder, 725 NativeJump* jump) { 726 // A generated lambda form might be deleted from the Lambdaform 727 // cache in MethodTypeForm. If a jit compiled lambdaform method 728 // becomes not entrant and the cache access returns null, the new 729 // resolve will lead to a new generated LambdaForm. 730 Method* old_method = reinterpret_cast<Method*>(method_holder->data()); 731 assert(old_method == nullptr || old_method == callee() || 732 callee->is_compiled_lambda_form() || 733 !old_method->method_holder()->is_loader_alive() || 734 old_method->is_old(), // may be race patching deoptimized nmethod due to redefinition. 735 "a) MT-unsafe modification of inline cache"); 736 737 address destination = jump->jump_destination(); 738 assert(destination == (address)-1 || destination == entry 739 || old_method == nullptr || !old_method->method_holder()->is_loader_alive() // may have a race due to class unloading. 740 || old_method->is_old(), // may be race patching deoptimized nmethod due to redefinition. 741 "b) MT-unsafe modification of inline cache"); 742 } 743 #endif // !PRODUCT