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, bool caller_is_c1, 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, caller_is_c1); 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, caller_is_c1); 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 bool caller_is_c1, 513 CompiledICInfo& info, 514 TRAPS) { 515 CompiledMethod* method_code = method->code(); 516 517 address entry = nullptr; 518 if (method_code != nullptr && method_code->is_in_use() && !method_code->is_unloading()) { 519 assert(method_code->is_compiled(), "must be compiled"); 520 // Call to compiled code 521 // 522 // Note: the following problem exists with Compiler1: 523 // - at compile time we may or may not know if the destination is final 524 // - if we know that the destination is final (is_optimized), we will emit 525 // an optimized virtual call (no inline cache), and need a Method* to make 526 // a call to the interpreter 527 // - if we don't know if the destination is final, we emit a standard 528 // virtual call, and use CompiledICHolder to call interpreted code 529 // (no static call stub has been generated) 530 // - In the case that we here notice the call is static bound we 531 // convert the call into what looks to be an optimized virtual call, 532 // but we must use the unverified entry point (since there will be no 533 // null check on a call when the target isn't loaded). 534 // This causes problems when verifying the IC because 535 // it looks vanilla but is optimized. Code in is_call_to_interpreted 536 // is aware of this and weakens its asserts. 537 if (is_optimized) { 538 entry = caller_is_c1 ? method_code->verified_inline_entry_point() : method_code->verified_entry_point(); 539 } else { 540 entry = caller_is_c1 ? method_code->inline_entry_point() : method_code->entry_point(); 541 } 542 } 543 if (entry != nullptr) { 544 // Call to near compiled code. 545 info.set_compiled_entry(entry, is_optimized ? nullptr : receiver_klass, is_optimized); 546 } else { 547 if (is_optimized) { 548 // Use stub entry 549 address entry = caller_is_c1 ? method()->get_c2i_inline_entry() : method()->get_c2i_entry(); 550 info.set_interpreter_entry(entry, method()); 551 } else { 552 // Use icholder entry 553 assert(method_code == nullptr || method_code->is_compiled(), "must be compiled"); 554 CompiledICHolder* holder = new CompiledICHolder(method(), receiver_klass); 555 entry = (caller_is_c1)? method()->get_c2i_unverified_inline_entry() : method()->get_c2i_unverified_entry(); 556 info.set_icholder_entry(entry, holder); 557 } 558 } 559 assert(info.is_optimized() == is_optimized, "must agree"); 560 } 561 562 563 bool CompiledIC::is_icholder_entry(address entry) { 564 CodeBlob* cb = CodeCache::find_blob(entry); 565 if (cb == nullptr) { 566 return false; 567 } 568 if (cb->is_adapter_blob()) { 569 return true; 570 } else if (cb->is_vtable_blob()) { 571 return VtableStubs::is_icholder_entry(entry); 572 } 573 return false; 574 } 575 576 bool CompiledIC::is_icholder_call_site(virtual_call_Relocation* call_site, const CompiledMethod* cm) { 577 // This call site might have become stale so inspect it carefully. 578 address dest = cm->call_wrapper_at(call_site->addr())->destination(); 579 return is_icholder_entry(dest); 580 } 581 582 // ---------------------------------------------------------------------------- 583 584 bool CompiledStaticCall::set_to_clean(bool in_use) { 585 // in_use is unused but needed to match template function in CompiledMethod 586 assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call"); 587 // Reset call site 588 set_destination_mt_safe(resolve_call_stub()); 589 590 // Do not reset stub here: It is too expensive to call find_stub. 591 // Instead, rely on caller (nmethod::clear_inline_caches) to clear 592 // both the call and its stub. 593 return true; 594 } 595 596 bool CompiledStaticCall::is_clean() const { 597 return destination() == resolve_call_stub(); 598 } 599 600 bool CompiledStaticCall::is_call_to_compiled() const { 601 return CodeCache::contains(destination()); 602 } 603 604 bool CompiledDirectStaticCall::is_call_to_interpreted() const { 605 // It is a call to interpreted, if it calls to a stub. Hence, the destination 606 // must be in the stub part of the nmethod that contains the call 607 CompiledMethod* cm = CodeCache::find_compiled(instruction_address()); 608 return cm->stub_contains(destination()); 609 } 610 611 void CompiledStaticCall::set_to_compiled(address entry) { 612 if (TraceICs) { 613 ResourceMark rm; 614 tty->print_cr("%s@" INTPTR_FORMAT ": set_to_compiled " INTPTR_FORMAT, 615 name(), 616 p2i(instruction_address()), 617 p2i(entry)); 618 } 619 // Call to compiled code 620 assert(CodeCache::contains(entry), "wrong entry point"); 621 set_destination_mt_safe(entry); 622 } 623 624 void CompiledStaticCall::set(const StaticCallInfo& info) { 625 assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call"); 626 // Updating a cache to the wrong entry can cause bugs that are very hard 627 // to track down - if cache entry gets invalid - we just clean it. In 628 // this way it is always the same code path that is responsible for 629 // updating and resolving an inline cache 630 assert(is_clean(), "do not update a call entry - use clean"); 631 632 if (info._to_interpreter) { 633 // Call to interpreted code 634 set_to_interpreted(info.callee(), info.entry()); 635 } else { 636 set_to_compiled(info.entry()); 637 } 638 } 639 640 // Compute settings for a CompiledStaticCall. Since we might have to set 641 // the stub when calling to the interpreter, we need to return arguments. 642 void CompiledStaticCall::compute_entry(const methodHandle& m, CompiledMethod* caller_nm, StaticCallInfo& info) { 643 assert(!m->mismatch(), "Mismatch for static call"); 644 bool caller_is_nmethod = caller_nm->is_nmethod(); 645 CompiledMethod* m_code = m->code(); 646 info._callee = m; 647 if (m_code != nullptr && m_code->is_in_use() && !m_code->is_unloading()) { 648 info._to_interpreter = false; 649 if (caller_nm->is_compiled_by_c1()) { 650 info._entry = m_code->verified_inline_entry_point(); 651 } else { 652 info._entry = m_code->verified_entry_point(); 653 } 654 } else { 655 // Callee is interpreted code. In any case entering the interpreter 656 // puts a converter-frame on the stack to save arguments. 657 assert(!m->is_method_handle_intrinsic(), "Compiled code should never call interpreter MH intrinsics"); 658 info._to_interpreter = true; 659 if (caller_nm->is_compiled_by_c1()) { 660 // C1 -> interp: values passed as oops 661 info._entry = m()->get_c2i_inline_entry(); 662 } else { 663 // C2 -> interp: values passed as fields 664 info._entry = m()->get_c2i_entry(); 665 } 666 } 667 } 668 669 void CompiledStaticCall::compute_entry_for_continuation_entry(const methodHandle& m, StaticCallInfo& info) { 670 if (ContinuationEntry::is_interpreted_call(instruction_address())) { 671 info._to_interpreter = true; 672 info._entry = m()->get_c2i_entry(); 673 } 674 } 675 676 address CompiledDirectStaticCall::find_stub_for(address instruction) { 677 // Find reloc. information containing this call-site 678 RelocIterator iter((nmethod*)nullptr, instruction); 679 while (iter.next()) { 680 if (iter.addr() == instruction) { 681 switch(iter.type()) { 682 case relocInfo::static_call_type: 683 return iter.static_call_reloc()->static_stub(); 684 // We check here for opt_virtual_call_type, since we reuse the code 685 // from the CompiledIC implementation 686 case relocInfo::opt_virtual_call_type: 687 return iter.opt_virtual_call_reloc()->static_stub(); 688 case relocInfo::poll_type: 689 case relocInfo::poll_return_type: // A safepoint can't overlap a call. 690 default: 691 ShouldNotReachHere(); 692 } 693 } 694 } 695 return nullptr; 696 } 697 698 address CompiledDirectStaticCall::find_stub() { 699 return CompiledDirectStaticCall::find_stub_for(instruction_address()); 700 } 701 702 address CompiledDirectStaticCall::resolve_call_stub() const { 703 return SharedRuntime::get_resolve_static_call_stub(); 704 } 705 706 //----------------------------------------------------------------------------- 707 // Non-product mode code 708 #ifndef PRODUCT 709 710 void CompiledIC::verify() { 711 _call->verify(); 712 assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted() 713 || is_optimized() || is_megamorphic(), "sanity check"); 714 } 715 716 void CompiledIC::print() { 717 print_compiled_ic(); 718 tty->cr(); 719 } 720 721 void CompiledIC::print_compiled_ic() { 722 tty->print("Inline cache at " INTPTR_FORMAT ", calling %s " INTPTR_FORMAT " cached_value " INTPTR_FORMAT, 723 p2i(instruction_address()), is_call_to_interpreted() ? "interpreted " : "", p2i(ic_destination()), p2i(is_optimized() ? nullptr : cached_value())); 724 } 725 726 void CompiledDirectStaticCall::print() { 727 tty->print("static call at " INTPTR_FORMAT " -> ", p2i(instruction_address())); 728 if (is_clean()) { 729 tty->print("clean"); 730 } else if (is_call_to_compiled()) { 731 tty->print("compiled"); 732 } else if (is_call_to_interpreted()) { 733 tty->print("interpreted"); 734 } 735 tty->cr(); 736 } 737 738 void CompiledDirectStaticCall::verify_mt_safe(const methodHandle& callee, address entry, 739 NativeMovConstReg* method_holder, 740 NativeJump* jump) { 741 // A generated lambda form might be deleted from the Lambdaform 742 // cache in MethodTypeForm. If a jit compiled lambdaform method 743 // becomes not entrant and the cache access returns null, the new 744 // resolve will lead to a new generated LambdaForm. 745 Method* old_method = reinterpret_cast<Method*>(method_holder->data()); 746 assert(old_method == nullptr || old_method == callee() || 747 callee->is_compiled_lambda_form() || 748 !old_method->method_holder()->is_loader_alive() || 749 old_method->is_old(), // may be race patching deoptimized nmethod due to redefinition. 750 "a) MT-unsafe modification of inline cache"); 751 752 address destination = jump->jump_destination(); 753 assert(destination == (address)-1 || destination == entry 754 || old_method == nullptr || !old_method->method_holder()->is_loader_alive() // may have a race due to class unloading. 755 || old_method->is_old(), // may be race patching deoptimized nmethod due to redefinition. 756 "b) MT-unsafe modification of inline cache"); 757 } 758 #endif // !PRODUCT