1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
/*
* Copyright (C) 2012-2018 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "CallLinkInfo.h"
#include "CCallHelpers.h"
#include "CallFrameShuffleData.h"
#include "DFGJITCode.h"
#include "DisallowMacroScratchRegisterUsage.h"
#include "FunctionCodeBlock.h"
#include "JITThunks.h"
#include "JSCellInlines.h"
#include "JSWebAssemblyModule.h"
#include "LLIntEntrypoint.h"
#include "LinkBuffer.h"
#include "Opcode.h"
#include "Repatch.h"
#include "ThunkGenerators.h"
#include <wtf/ListDump.h>
namespace JSC {
CallLinkInfo::CallType CallLinkInfo::callTypeFor(OpcodeID opcodeID)
{
switch (opcodeID) {
case op_tail_call_varargs:
case op_tail_call_forward_arguments:
return TailCallVarargs;
case op_call:
case op_call_ignore_result:
case op_call_direct_eval:
case op_iterator_open:
case op_iterator_next:
return Call;
case op_call_varargs:
return CallVarargs;
case op_construct:
case op_super_construct:
return Construct;
case op_construct_varargs:
case op_super_construct_varargs:
return ConstructVarargs;
case op_tail_call:
return TailCall;
default:
break;
}
RELEASE_ASSERT_NOT_REACHED();
return Call;
}
CallLinkInfo::~CallLinkInfo()
{
clearStub();
}
void CallLinkInfo::clearStub()
{
if (!stub())
return;
m_stub->clearCallNodesFor(this);
m_stub = nullptr;
}
void CallLinkInfo::unlinkOrUpgradeImpl(VM& vm, CodeBlock* oldCodeBlock, CodeBlock* newCodeBlock)
{
// We could be called even if we're not linked anymore because of how polymorphic calls
// work. Each callsite within the polymorphic call stub may separately ask us to unlink().
if (isOnList())
remove();
dataLogLnIf(Options::dumpDisassembly(), "Unlinking CallLinkInfo: ", RawPointer(this));
Mode mode = this->mode();
switch (mode) {
case Mode::Monomorphic: {
if (newCodeBlock && oldCodeBlock == m_codeBlock) {
// Upgrading Monomorphic DataIC with newCodeBlock.
ArityCheckMode arityCheck = oldCodeBlock->jitCode()->addressForCall(ArityCheckNotRequired) == m_monomorphicCallDestination ? ArityCheckNotRequired : MustCheckArity;
auto target = newCodeBlock->jitCode()->addressForCall(arityCheck);
m_codeBlock = newCodeBlock;
m_monomorphicCallDestination = target;
newCodeBlock->linkIncomingCall(nullptr, this); // This is just relinking. So owner and caller frame can be nullptr.
return;
}
revertCall(vm);
break;
}
case Mode::Polymorphic: {
revertCall(vm);
break;
}
case Mode::Init:
case Mode::Virtual: {
break;
}
}
// Either we were unlinked, in which case we should not have been on any list, or we unlinked
// ourselves so that we're not on any list anymore.
RELEASE_ASSERT(!isOnList(), static_cast<unsigned>(mode));
}
void CallLinkInfo::setMonomorphicCallee(VM& vm, JSCell* owner, JSObject* callee, CodeBlock* codeBlock, CodePtr<JSEntryPtrTag> codePtr)
{
RELEASE_ASSERT(!(std::bit_cast<uintptr_t>(callee) & polymorphicCalleeMask));
m_callee.set(vm, owner, callee);
m_codeBlock = codeBlock;
m_monomorphicCallDestination = codePtr;
m_mode = static_cast<unsigned>(Mode::Monomorphic);
}
void CallLinkInfo::clearCallee()
{
m_callee.clear();
m_codeBlock = nullptr;
m_monomorphicCallDestination = nullptr;
}
JSObject* CallLinkInfo::callee()
{
RELEASE_ASSERT(!(std::bit_cast<uintptr_t>(m_callee.get()) & polymorphicCalleeMask));
return m_callee.get();
}
void CallLinkInfo::setLastSeenCallee(VM& vm, const JSCell* owner, JSObject* callee)
{
m_lastSeenCallee.set(vm, owner, callee);
}
JSObject* CallLinkInfo::lastSeenCallee() const
{
return m_lastSeenCallee.get();
}
bool CallLinkInfo::haveLastSeenCallee() const
{
return !!m_lastSeenCallee;
}
void CallLinkInfo::visitWeak(VM& vm)
{
auto handleSpecificCallee = [&] (JSFunction* callee) {
if (vm.heap.isMarked(callee->executable()))
m_hasSeenClosure = true;
else
m_clearedByGC = true;
};
switch (mode()) {
case Mode::Init:
case Mode::Virtual:
break;
case Mode::Polymorphic: {
if (stub()) {
if (!stub()->visitWeak(vm)) {
dataLogLnIf(Options::verboseOSR(), "At ", codeOrigin(), ", ", RawPointer(this), ": clearing call stub to ", listDump(stub()->variants()), ", stub routine ", RawPointer(stub()), ".");
unlinkOrUpgrade(vm, nullptr, nullptr);
m_clearedByGC = true;
}
}
break;
}
case Mode::Monomorphic: {
auto* callee = m_callee.get();
if (callee && !vm.heap.isMarked(callee)) {
if (callee->type() == JSFunctionType) {
dataLogLnIf(Options::verboseOSR(), "Clearing call to ", RawPointer(callee), " (", static_cast<JSFunction*>(callee)->executable()->hashFor(specializationKind()), ").");
handleSpecificCallee(static_cast<JSFunction*>(callee));
} else {
dataLogLnIf(Options::verboseOSR(), "Clearing call to ", RawPointer(callee), ".");
m_clearedByGC = true;
}
unlinkOrUpgrade(vm, nullptr, nullptr);
}
break;
}
}
if (haveLastSeenCallee() && !vm.heap.isMarked(lastSeenCallee())) {
if (lastSeenCallee()->type() == JSFunctionType)
handleSpecificCallee(jsCast<JSFunction*>(lastSeenCallee()));
else
m_clearedByGC = true;
m_lastSeenCallee.clear();
}
}
void CallLinkInfo::revertCallToStub()
{
RELEASE_ASSERT(stub());
// The start of our JIT code is now a jump to the polymorphic stub. Rewrite the first instruction
// to be what we need for non stub ICs.
// this runs into some branch compaction crap I'd like to avoid for now. Essentially, the branch
// doesn't know if it can be compacted or not. So we end up with 28 bytes of machine code, for
// what in all likelihood fits in 24. So we just splat out the first instruction. Long term, we
// need something cleaner. But this works on arm64 for now.
m_callee.clear();
m_codeBlock = nullptr;
m_monomorphicCallDestination = nullptr;
}
void DataOnlyCallLinkInfo::initialize(VM& vm, CodeBlock* owner, CallType callType, CodeOrigin codeOrigin)
{
m_owner = owner;
m_type = static_cast<unsigned>(Type::DataOnly);
ASSERT(Type::DataOnly == type());
m_codeOrigin = codeOrigin;
m_callType = callType;
m_mode = static_cast<unsigned>(Mode::Init);
if (UNLIKELY(!Options::useLLIntICs()))
setVirtualCall(vm);
}
std::tuple<CodeBlock*, BytecodeIndex> CallLinkInfo::retrieveCaller(JSCell* owner)
{
auto* codeBlock = jsDynamicCast<CodeBlock*>(owner);
if (!codeBlock)
return { };
CodeOrigin codeOrigin = this->codeOrigin();
if (auto* baselineCodeBlock = codeOrigin.codeOriginOwner())
return std::tuple { baselineCodeBlock, codeOrigin.bytecodeIndex() };
return std::tuple { codeBlock, codeOrigin.bytecodeIndex() };
}
void CallLinkInfo::reset(VM&)
{
if (stub())
revertCallToStub();
clearCallee(); // This also clears the inline cache both for data and code-based caches.
clearSeen();
clearStub();
if (isOnList())
remove();
m_mode = static_cast<unsigned>(Mode::Init);
}
void CallLinkInfo::revertCall(VM& vm)
{
if (UNLIKELY(!Options::useLLIntICs() && type() == CallLinkInfo::Type::DataOnly))
setVirtualCall(vm);
else
reset(vm);
}
void CallLinkInfo::setVirtualCall(VM& vm)
{
reset(vm);
m_callee.clear();
*std::bit_cast<uintptr_t*>(m_callee.slot()) = polymorphicCalleeMask;
m_codeBlock = nullptr; // PolymorphicCallStubRoutine will set CodeBlock inside it.
m_monomorphicCallDestination = vm.getCTIVirtualCall(callMode()).code().template retagged<JSEntryPtrTag>();
setClearedByVirtual();
m_mode = static_cast<unsigned>(Mode::Virtual);
}
JSGlobalObject* CallLinkInfo::globalObjectForSlowPath(JSCell* owner)
{
auto [codeBlock, bytecodeIndex] = retrieveCaller(owner);
if (codeBlock)
return codeBlock->globalObject();
#if ENABLE(WEBASSEMBLY)
auto* module = jsDynamicCast<JSWebAssemblyModule*>(owner);
if (module)
return module->globalObject();
#endif
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
void CallLinkInfo::setStub(Ref<PolymorphicCallStubRoutine>&& newStub)
{
clearStub();
m_stub = WTFMove(newStub);
m_callee.clear();
*std::bit_cast<uintptr_t*>(m_callee.slot()) = polymorphicCalleeMask;
m_codeBlock = nullptr; // PolymorphicCallStubRoutine will set CodeBlock inside it.
m_monomorphicCallDestination = m_stub->code().code().retagged<JSEntryPtrTag>();
// The call link info no longer has a call cache apart from the jump to the polymorphic call stub.
if (isOnList())
remove();
m_mode = static_cast<unsigned>(Mode::Polymorphic);
}
#if ENABLE(JIT)
void CallLinkInfo::emitFastPathImpl(CallLinkInfo* callLinkInfo, CCallHelpers& jit, bool isTailCall, ScopedLambda<void()>&& prepareForTailCall)
{
if (callLinkInfo)
jit.move(CCallHelpers::TrustedImmPtr(callLinkInfo), BaselineJITRegisters::Call::callLinkInfoGPR);
#if USE(JSVALUE32_64)
// We need this on JSVALUE32_64 only as on JSVALUE64 a pointer comparison in the DataIC fast
// path catches this.
auto failed = jit.branchIfNotCell(BaselineJITRegisters::Call::calleeJSR);
#endif
// For RISCV64, scratch register usage here collides with MacroAssembler's internal usage
// that's necessary for the test-and-branch operation but is avoidable by loading from the callee
// address for each branch operation. Other MacroAssembler implementations handle this better by
// using a wider range of scratch registers or more potent branching instructions.
CCallHelpers::JumpList found;
jit.loadPtr(CCallHelpers::Address(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfMonomorphicCallDestination()), BaselineJITRegisters::Call::callTargetGPR);
if constexpr (isRISCV64()) {
CCallHelpers::Address calleeAddress(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfCallee());
found.append(jit.branchPtr(CCallHelpers::Equal, calleeAddress, BaselineJITRegisters::Call::calleeGPR));
found.append(jit.branchTestPtr(CCallHelpers::NonZero, calleeAddress, CCallHelpers::TrustedImm32(polymorphicCalleeMask)));
} else {
GPRReg scratchGPR = jit.scratchRegister();
DisallowMacroScratchRegisterUsage disallowScratch(jit);
jit.loadPtr(CCallHelpers::Address(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfCallee()), scratchGPR);
found.append(jit.branchPtr(CCallHelpers::Equal, scratchGPR, BaselineJITRegisters::Call::calleeGPR));
found.append(jit.branchTestPtr(CCallHelpers::NonZero, scratchGPR, CCallHelpers::TrustedImm32(polymorphicCalleeMask)));
}
#if USE(JSVALUE32_64)
failed.link(&jit);
#endif
jit.move(CCallHelpers::TrustedImmPtr(LLInt::defaultCall().code().taggedPtr()), BaselineJITRegisters::Call::callTargetGPR);
found.link(&jit);
if (isTailCall) {
prepareForTailCall();
jit.transferPtr(CCallHelpers::Address(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfCodeBlock()), CCallHelpers::calleeFrameCodeBlockBeforeTailCall());
jit.farJump(BaselineJITRegisters::Call::callTargetGPR, JSEntryPtrTag);
} else {
jit.transferPtr(CCallHelpers::Address(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfCodeBlock()), CCallHelpers::calleeFrameCodeBlockBeforeCall());
jit.call(BaselineJITRegisters::Call::callTargetGPR, JSEntryPtrTag);
}
return;
}
void CallLinkInfo::emitDataICFastPath(CCallHelpers& jit)
{
emitFastPathImpl(nullptr, jit, false, nullptr);
}
void CallLinkInfo::emitTailCallDataICFastPath(CCallHelpers& jit, ScopedLambda<void()>&& prepareForTailCall)
{
emitFastPathImpl(nullptr, jit, true, WTFMove(prepareForTailCall));
}
void CallLinkInfo::emitFastPath(CCallHelpers& jit, CompileTimeCallLinkInfo callLinkInfo)
{
if (std::holds_alternative<OptimizingCallLinkInfo*>(callLinkInfo))
return std::get<OptimizingCallLinkInfo*>(callLinkInfo)->emitFastPath(jit);
return CallLinkInfo::emitDataICFastPath(jit);
}
void CallLinkInfo::emitTailCallFastPath(CCallHelpers& jit, CompileTimeCallLinkInfo callLinkInfo, ScopedLambda<void()>&& prepareForTailCall)
{
if (std::holds_alternative<OptimizingCallLinkInfo*>(callLinkInfo))
return std::get<OptimizingCallLinkInfo*>(callLinkInfo)->emitTailCallFastPath(jit, WTFMove(prepareForTailCall));
return CallLinkInfo::emitTailCallDataICFastPath(jit, WTFMove(prepareForTailCall));
}
void OptimizingCallLinkInfo::emitFastPath(CCallHelpers& jit)
{
RELEASE_ASSERT(!isTailCall());
emitFastPathImpl(this, jit, isTailCall(), nullptr);
}
void OptimizingCallLinkInfo::emitTailCallFastPath(CCallHelpers& jit, ScopedLambda<void()>&& prepareForTailCall)
{
RELEASE_ASSERT(isTailCall());
emitFastPathImpl(this, jit, isTailCall(), WTFMove(prepareForTailCall));
}
#if ENABLE(DFG_JIT)
void OptimizingCallLinkInfo::initializeFromDFGUnlinkedCallLinkInfo(VM&, const DFG::UnlinkedCallLinkInfo& unlinkedCallLinkInfo, CodeBlock* owner)
{
m_owner = owner;
m_codeOrigin = unlinkedCallLinkInfo.codeOrigin;
m_callType = unlinkedCallLinkInfo.callType;
}
#endif
void DirectCallLinkInfo::reset()
{
if (isOnList())
remove();
#if ENABLE(JIT)
if (!isDataIC())
initialize();
#endif
m_target = { };
m_codeBlock = nullptr;
}
void DirectCallLinkInfo::unlinkOrUpgradeImpl(VM&, CodeBlock* oldCodeBlock, CodeBlock* newCodeBlock)
{
if (isOnList())
remove();
if (!!m_target) {
if (m_codeBlock && newCodeBlock && oldCodeBlock == m_codeBlock) {
ArityCheckMode arityCheck = oldCodeBlock->jitCode()->addressForCall(ArityCheckNotRequired) == m_target ? ArityCheckNotRequired : MustCheckArity;
auto target = newCodeBlock->jitCode()->addressForCall(arityCheck);
setCallTarget(newCodeBlock, CodeLocationLabel { target });
newCodeBlock->linkIncomingCall(nullptr, this); // This is just relinking. So owner and caller frame can be nullptr.
return;
}
dataLogLnIf(Options::dumpDisassembly(), "Unlinking CallLinkInfo: ", RawPointer(this));
reset();
}
// Either we were unlinked, in which case we should not have been on any list, or we unlinked
// ourselves so that we're not on any list anymore.
RELEASE_ASSERT(!isOnList());
}
void DirectCallLinkInfo::visitWeak(VM& vm)
{
if (m_codeBlock && !vm.heap.isMarked(m_codeBlock)) {
dataLogLnIf(Options::verboseOSR(), "Clearing call to ", RawPointer(m_codeBlock), " (", pointerDump(m_codeBlock), ").");
unlinkOrUpgrade(vm, nullptr, nullptr);
}
}
CCallHelpers::JumpList DirectCallLinkInfo::emitDirectFastPath(CCallHelpers& jit)
{
RELEASE_ASSERT(!isTailCall());
if (isDataIC()) {
CCallHelpers::JumpList slowPath;
jit.move(CCallHelpers::TrustedImmPtr(this), BaselineJITRegisters::Call::callLinkInfoGPR);
slowPath.append(jit.branchTestPtr(CCallHelpers::Zero, CCallHelpers::Address(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfTarget())));
jit.transferPtr(CCallHelpers::Address(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfCodeBlock()), CCallHelpers::calleeFrameCodeBlockBeforeCall());
jit.call(CCallHelpers::Address(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfTarget()), JSEntryPtrTag);
return slowPath;
}
auto codeBlockStore = jit.storePtrWithPatch(CCallHelpers::TrustedImmPtr(nullptr), CCallHelpers::calleeFrameCodeBlockBeforeCall());
auto call = jit.nearCall();
jit.addLinkTask([=, this] (LinkBuffer& linkBuffer) {
m_callLocation = linkBuffer.locationOfNearCall<JSInternalPtrTag>(call);
m_codeBlockLocation = linkBuffer.locationOf<JSInternalPtrTag>(codeBlockStore);
});
jit.addLateLinkTask([this](LinkBuffer&) {
repatchSpeculatively();
});
return { };
}
CCallHelpers::JumpList DirectCallLinkInfo::emitDirectTailCallFastPath(CCallHelpers& jit, ScopedLambda<void()>&& prepareForTailCall)
{
RELEASE_ASSERT(isTailCall());
if (isDataIC()) {
CCallHelpers::JumpList slowPath;
jit.move(CCallHelpers::TrustedImmPtr(this), BaselineJITRegisters::Call::callLinkInfoGPR);
slowPath.append(jit.branchTestPtr(CCallHelpers::Zero, CCallHelpers::Address(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfTarget())));
prepareForTailCall();
jit.transferPtr(CCallHelpers::Address(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfCodeBlock()), CCallHelpers::calleeFrameCodeBlockBeforeTailCall());
jit.farJump(CCallHelpers::Address(BaselineJITRegisters::Call::callLinkInfoGPR, offsetOfTarget()), JSEntryPtrTag);
return slowPath;
}
auto fastPathStart = jit.label();
// - If we're not yet linked, this is a jump to the slow path.
// - Once we're linked to a fast path, this goes back to being nops so we fall through to the linked jump.
jit.emitNops(CCallHelpers::patchableJumpSize());
prepareForTailCall();
auto codeBlockStore = jit.storePtrWithPatch(CCallHelpers::TrustedImmPtr(nullptr), CCallHelpers::calleeFrameCodeBlockBeforeTailCall());
auto call = jit.nearTailCall();
jit.addLinkTask([=, this] (LinkBuffer& linkBuffer) {
m_fastPathStart = linkBuffer.locationOf<JSInternalPtrTag>(fastPathStart);
m_callLocation = linkBuffer.locationOfNearCall<JSInternalPtrTag>(call);
m_codeBlockLocation = linkBuffer.locationOf<JSInternalPtrTag>(codeBlockStore);
});
jit.addLateLinkTask([this](LinkBuffer&) {
repatchSpeculatively();
});
return { };
}
void DirectCallLinkInfo::initialize()
{
ASSERT(m_callLocation);
ASSERT(m_codeBlockLocation);
if (isTailCall()) {
RELEASE_ASSERT(fastPathStart());
CCallHelpers::replaceWithJump(fastPathStart(), slowPathStart());
} else
MacroAssembler::repatchNearCall(m_callLocation, slowPathStart());
}
void DirectCallLinkInfo::setCallTarget(CodeBlock* codeBlock, CodeLocationLabel<JSEntryPtrTag> target)
{
m_codeBlock = codeBlock;
m_target = target;
if (!isDataIC()) {
if (isTailCall()) {
RELEASE_ASSERT(fastPathStart());
// We reserved this many bytes for the jump at fastPathStart(). Make that
// code nops now so we fall through to the jump to the fast path.
CCallHelpers::replaceWithNops(fastPathStart(), CCallHelpers::patchableJumpSize());
}
MacroAssembler::repatchNearCall(m_callLocation, target);
MacroAssembler::repatchPointer(m_codeBlockLocation, codeBlock);
}
}
void DirectCallLinkInfo::setMaxArgumentCountIncludingThis(unsigned value)
{
RELEASE_ASSERT(value);
m_maxArgumentCountIncludingThis = value;
}
CodeBlock* DirectCallLinkInfo::retrieveCodeBlock(FunctionExecutable* functionExecutable)
{
CodeSpecializationKind kind = specializationKind();
CodeBlock* codeBlock = functionExecutable->codeBlockFor(kind);
if (!codeBlock)
return nullptr;
CodeBlock* ownerCodeBlock = jsDynamicCast<CodeBlock*>(owner());
if (!ownerCodeBlock)
return nullptr;
if (ownerCodeBlock->alternative() == codeBlock)
return nullptr;
return codeBlock;
}
CodePtr<JSEntryPtrTag> DirectCallLinkInfo::retrieveCodePtr(const ConcurrentJSLocker& locker, CodeBlock* codeBlock)
{
unsigned argumentStackSlots = maxArgumentCountIncludingThis();
ArityCheckMode arityCheckMode = (argumentStackSlots < static_cast<size_t>(codeBlock->numParameters())) ? MustCheckArity : ArityCheckNotRequired;
return codeBlock->addressForCallConcurrently(locker, arityCheckMode);
}
void DirectCallLinkInfo::repatchSpeculatively()
{
if (m_executable->isHostFunction()) {
CodeSpecializationKind kind = specializationKind();
CodePtr<JSEntryPtrTag> codePtr;
if (kind == CodeForCall)
codePtr = m_executable->generatedJITCodeWithArityCheckForCall();
else
codePtr = m_executable->generatedJITCodeWithArityCheckForConstruct();
if (codePtr)
setCallTarget(nullptr, CodeLocationLabel { codePtr });
else
initialize();
return;
}
FunctionExecutable* functionExecutable = jsDynamicCast<FunctionExecutable*>(m_executable);
if (!functionExecutable) {
initialize();
return;
}
auto* codeBlock = retrieveCodeBlock(functionExecutable);
if (codeBlock) {
auto codePtr = retrieveCodePtr(ConcurrentJSLocker { codeBlock->m_lock }, codeBlock);
if (codePtr) {
m_codeBlock = codeBlock;
m_target = codePtr;
// Do not chain |this| to the calle codeBlock concurrently. It will be done in the main thread if the speculatively repatched one is still valid.
setCallTarget(codeBlock, CodeLocationLabel { codePtr });
return;
}
}
initialize();
}
void DirectCallLinkInfo::validateSpeculativeRepatchOnMainThread(VM&)
{
constexpr bool verbose = false;
FunctionExecutable* functionExecutable = jsDynamicCast<FunctionExecutable*>(m_executable);
if (!functionExecutable)
return;
auto* codeBlock = retrieveCodeBlock(functionExecutable);
CodePtr<JSEntryPtrTag> codePtr = nullptr;
if (codeBlock)
codePtr = retrieveCodePtr(ConcurrentJSLocker { NoLockingNecessary }, codeBlock);
if (m_codeBlock != codeBlock || m_target != codePtr) {
if (codeBlock && codePtr)
setCallTarget(codeBlock, CodeLocationLabel { codePtr });
else
reset();
} else
dataLogLnIf(verbose, "Speculative repatching succeeded ", RawPointer(m_codeBlock), " ", m_target);
if (m_codeBlock)
m_codeBlock->linkIncomingCall(owner(), this);
}
#endif
} // namespace JSC
|