| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "jit/JitScript-inl.h" |
| 8 |
|
| 9 |
#include "mozilla/BinarySearch.h" |
| 10 |
#include "mozilla/CheckedInt.h" |
| 11 |
|
| 12 |
#include <utility> |
| 13 |
|
| 14 |
#include "jit/BaselineIC.h" |
| 15 |
#include "jit/BaselineJIT.h" |
| 16 |
#include "jit/BytecodeAnalysis.h" |
| 17 |
#include "jit/IonScript.h" |
| 18 |
#include "jit/JitFrames.h" |
| 19 |
#include "jit/JitSpewer.h" |
| 20 |
#include "jit/ScriptFromCalleeToken.h" |
| 21 |
#include "jit/TrialInlining.h" |
| 22 |
#include "js/ColumnNumber.h" // JS::LimitedColumnNumberOneOrigin |
| 23 |
#include "vm/BytecodeUtil.h" |
| 24 |
#include "vm/Compartment.h" |
| 25 |
#include "vm/FrameIter.h" // js::OnlyJSJitFrameIter |
| 26 |
#include "vm/JitActivation.h" |
| 27 |
#include "vm/JSScript.h" |
| 28 |
|
| 29 |
#include "gc/GCContext-inl.h" |
| 30 |
#include "jit/JSJitFrameIter-inl.h" |
| 31 |
#include "vm/JSContext-inl.h" |
| 32 |
#include "vm/JSScript-inl.h" |
| 33 |
|
| 34 |
using namespace js; |
| 35 |
using namespace js::jit; |
| 36 |
|
| 37 |
using mozilla::CheckedInt; |
| 38 |
|
| 39 |
JitScript::JitScript(JSScript* script, Offset fallbackStubsOffset, |
| 40 |
Offset endOffset, const char* profileString) |
| 41 |
: profileString_(profileString), |
| 42 |
owningScript_(script), |
| 43 |
endOffset_(endOffset), |
| 44 |
icScript_(script->getWarmUpCount(), |
| 45 |
fallbackStubsOffset - offsetOfICScript(), |
| 46 |
endOffset - offsetOfICScript(), |
| 47 |
/*depth=*/0, script->length()) { |
| 48 |
// Ensure the baselineScript_ and ionScript_ fields match the BaselineDisabled |
| 49 |
// and IonDisabled script flags. |
| 50 |
if (!script->canBaselineCompile()) { |
| 51 |
setBaselineScriptImpl(script, BaselineDisabledScriptPtr); |
| 52 |
} |
| 53 |
if (!script->canIonCompile()) { |
| 54 |
setIonScriptImpl(script, IonDisabledScriptPtr); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
ICScript::~ICScript() { |
| 59 |
// The contents of the AllocSite LifoAlloc are removed and freed separately |
| 60 |
// after the next minor GC. See prepareForDestruction. |
| 61 |
MOZ_ASSERT(allocSitesSpace_.isEmpty()); |
| 62 |
MOZ_ASSERT(!envAllocSite_); |
| 63 |
} |
| 64 |
|
| 65 |
#ifdef DEBUG |
| 66 |
JitScript::~JitScript() { |
| 67 |
// BaselineScript and IonScript must have been destroyed at this point. |
| 68 |
MOZ_ASSERT(!hasBaselineScript()); |
| 69 |
MOZ_ASSERT(!hasIonScript()); |
| 70 |
|
| 71 |
MOZ_ASSERT(!isInList()); |
| 72 |
} |
| 73 |
#else |
| 74 |
JitScript::~JitScript() = default; |
| 75 |
#endif |
| 76 |
|
| 77 |
bool JSScript::createJitScript(JSContext* cx) { |
| 78 |
MOZ_ASSERT(!hasJitScript()); |
| 79 |
cx->check(this); |
| 80 |
|
| 81 |
// Scripts with a JitScript can run in the Baseline Interpreter. Make sure |
| 82 |
// we don't create a JitScript for scripts we shouldn't Baseline interpret. |
| 83 |
MOZ_ASSERT_IF(IsBaselineInterpreterEnabled(), |
| 84 |
CanBaselineInterpretScript(this)); |
| 85 |
|
| 86 |
// Store the profile string in the JitScript if the profiler is enabled. |
| 87 |
const char* profileString = nullptr; |
| 88 |
if (cx->runtime()->geckoProfiler().enabled()) { |
| 89 |
profileString = cx->runtime()->geckoProfiler().profileString(cx, this); |
| 90 |
if (!profileString) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
static_assert(sizeof(JitScript) % sizeof(uintptr_t) == 0, |
| 96 |
"Trailing arrays must be aligned properly"); |
| 97 |
static_assert(sizeof(ICEntry) % sizeof(uintptr_t) == 0, |
| 98 |
"Trailing arrays must be aligned properly"); |
| 99 |
|
| 100 |
static_assert( |
| 101 |
sizeof(JitScript) == offsetof(JitScript, icScript_) + sizeof(ICScript), |
| 102 |
"icScript_ must be the last field"); |
| 103 |
|
| 104 |
// Calculate allocation size. |
| 105 |
CheckedInt<uint32_t> allocSize = sizeof(JitScript); |
| 106 |
allocSize += CheckedInt<uint32_t>(numICEntries()) * sizeof(ICEntry); |
| 107 |
allocSize += CheckedInt<uint32_t>(numICEntries()) * sizeof(ICFallbackStub); |
| 108 |
if (!allocSize.isValid()) { |
| 109 |
ReportAllocationOverflow(cx); |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
void* raw = cx->pod_malloc<uint8_t>(allocSize.value()); |
| 114 |
MOZ_ASSERT(uintptr_t(raw) % alignof(JitScript) == 0); |
| 115 |
if (!raw) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
size_t fallbackStubsOffset = |
| 120 |
sizeof(JitScript) + numICEntries() * sizeof(ICEntry); |
| 121 |
|
| 122 |
UniquePtr<JitScript> jitScript(new (raw) JitScript( |
| 123 |
this, fallbackStubsOffset, allocSize.value(), profileString)); |
| 124 |
|
| 125 |
// Sanity check the length computation. |
| 126 |
MOZ_ASSERT(jitScript->numICEntries() == numICEntries()); |
| 127 |
|
| 128 |
jitScript->icScript()->initICEntries(cx, this); |
| 129 |
|
| 130 |
cx->zone()->jitZone()->registerJitScript(jitScript.get()); |
| 131 |
|
| 132 |
warmUpData_.initJitScript(jitScript.release()); |
| 133 |
AddCellMemory(this, allocSize.value(), MemoryUse::JitScript); |
| 134 |
|
| 135 |
// We have a JitScript so we can set the script's jitCodeRaw pointer to the |
| 136 |
// Baseline Interpreter code. |
| 137 |
updateJitCodeRaw(cx->runtime()); |
| 138 |
|
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
void JSScript::maybeReleaseJitScript(JS::GCContext* gcx) { |
| 143 |
MOZ_ASSERT(hasJitScript()); |
| 144 |
|
| 145 |
if (zone()->jitZone()->keepJitScripts() || jitScript()->hasBaselineScript() || |
| 146 |
jitScript()->icScript()->active()) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
releaseJitScript(gcx); |
| 151 |
} |
| 152 |
|
| 153 |
void JSScript::releaseJitScript(JS::GCContext* gcx) { |
| 154 |
MOZ_ASSERT(hasJitScript()); |
| 155 |
MOZ_ASSERT(!hasBaselineScript()); |
| 156 |
MOZ_ASSERT(!hasIonScript()); |
| 157 |
|
| 158 |
gcx->removeCellMemory(this, jitScript()->allocBytes(), MemoryUse::JitScript); |
| 159 |
|
| 160 |
JitScript::Destroy(zone(), jitScript()); |
| 161 |
warmUpData_.clearJitScript(); |
| 162 |
updateJitCodeRaw(gcx->runtime()); |
| 163 |
} |
| 164 |
|
| 165 |
void JSScript::releaseJitScriptOnFinalize(JS::GCContext* gcx) { |
| 166 |
MOZ_ASSERT(hasJitScript()); |
| 167 |
|
| 168 |
if (hasIonScript()) { |
| 169 |
IonScript* ion = jitScript()->clearIonScript(gcx, this); |
| 170 |
jit::IonScript::Destroy(gcx, ion); |
| 171 |
} |
| 172 |
|
| 173 |
if (hasBaselineScript()) { |
| 174 |
BaselineScript* baseline = jitScript()->clearBaselineScript(gcx, this); |
| 175 |
jit::BaselineScript::Destroy(gcx, baseline); |
| 176 |
} |
| 177 |
|
| 178 |
releaseJitScript(gcx); |
| 179 |
} |
| 180 |
|
| 181 |
void JitScript::trace(JSTracer* trc) { |
| 182 |
TraceEdge(trc, &owningScript_, "JitScript::owningScript_"); |
| 183 |
|
| 184 |
icScript_.trace(trc); |
| 185 |
|
| 186 |
if (hasBaselineScript()) { |
| 187 |
baselineScript()->trace(trc); |
| 188 |
} |
| 189 |
|
| 190 |
if (hasIonScript()) { |
| 191 |
ionScript()->trace(trc); |
| 192 |
} |
| 193 |
|
| 194 |
if (templateEnv_.isSome()) { |
| 195 |
TraceNullableEdge(trc, templateEnv_.ptr(), "jitscript-template-env"); |
| 196 |
} |
| 197 |
|
| 198 |
if (hasInliningRoot()) { |
| 199 |
inliningRoot()->trace(trc); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
void JitScript::traceWeak(JSTracer* trc) { |
| 204 |
if (!icScript_.traceWeak(trc)) { |
| 205 |
notePurgedStubs(); |
| 206 |
} |
| 207 |
|
| 208 |
if (hasInliningRoot()) { |
| 209 |
if (!inliningRoot()->traceWeak(trc)) { |
| 210 |
notePurgedStubs(); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if (hasIonScript()) { |
| 215 |
ionScript()->traceWeak(trc); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
void ICScript::trace(JSTracer* trc) { |
| 220 |
// Mark all IC stub codes hanging off the IC stub entries. |
| 221 |
for (size_t i = 0; i < numICEntries(); i++) { |
| 222 |
ICEntry& ent = icEntry(i); |
| 223 |
ent.trace(trc); |
| 224 |
} |
| 225 |
|
| 226 |
for (gc::AllocSite* site : allocSites_) { |
| 227 |
site->trace(trc); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
bool ICScript::traceWeak(JSTracer* trc) { |
| 232 |
// Mark all IC stub codes hanging off the IC stub entries. |
| 233 |
bool allSurvived = true; |
| 234 |
for (size_t i = 0; i < numICEntries(); i++) { |
| 235 |
ICEntry& ent = icEntry(i); |
| 236 |
if (!ent.traceWeak(trc)) { |
| 237 |
allSurvived = false; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
return allSurvived; |
| 242 |
} |
| 243 |
|
| 244 |
bool ICScript::addInlinedChild(JSContext* cx, UniquePtr<ICScript> child, |
| 245 |
uint32_t pcOffset) { |
| 246 |
MOZ_ASSERT(!hasInlinedChild(pcOffset)); |
| 247 |
|
| 248 |
if (!inlinedChildren_) { |
| 249 |
inlinedChildren_ = cx->make_unique<Vector<CallSite>>(cx); |
| 250 |
if (!inlinedChildren_) { |
| 251 |
return false; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
// First reserve space in inlinedChildren_ to ensure that if the ICScript is |
| 256 |
// added to the inlining root, it can also be added to inlinedChildren_. |
| 257 |
CallSite callsite(child.get(), pcOffset); |
| 258 |
if (!inlinedChildren_->reserve(inlinedChildren_->length() + 1)) { |
| 259 |
return false; |
| 260 |
} |
| 261 |
if (!inliningRoot()->addInlinedScript(std::move(child))) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
inlinedChildren_->infallibleAppend(callsite); |
| 265 |
return true; |
| 266 |
} |
| 267 |
|
| 268 |
ICScript* ICScript::findInlinedChild(uint32_t pcOffset) { |
| 269 |
for (auto& callsite : *inlinedChildren_) { |
| 270 |
if (callsite.pcOffset_ == pcOffset) { |
| 271 |
return callsite.callee_; |
| 272 |
} |
| 273 |
} |
| 274 |
MOZ_CRASH("Inlined child expected at pcOffset"); |
| 275 |
} |
| 276 |
|
| 277 |
void ICScript::removeInlinedChild(uint32_t pcOffset) { |
| 278 |
MOZ_ASSERT(inliningRoot()); |
| 279 |
inlinedChildren_->eraseIf([pcOffset](const CallSite& callsite) -> bool { |
| 280 |
return callsite.pcOffset_ == pcOffset; |
| 281 |
}); |
| 282 |
} |
| 283 |
|
| 284 |
bool ICScript::hasInlinedChild(uint32_t pcOffset) { |
| 285 |
if (!inlinedChildren_) { |
| 286 |
return false; |
| 287 |
} |
| 288 |
for (auto& callsite : *inlinedChildren_) { |
| 289 |
if (callsite.pcOffset_ == pcOffset) { |
| 290 |
return true; |
| 291 |
} |
| 292 |
} |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
void ICScript::purgeInactiveICScripts() { |
| 297 |
MOZ_ASSERT(inliningRoot()); |
| 298 |
|
| 299 |
if (!inlinedChildren_) { |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
inlinedChildren_->eraseIf( |
| 304 |
[](const CallSite& callsite) { return !callsite.callee_->active(); }); |
| 305 |
|
| 306 |
if (inlinedChildren_->empty()) { |
| 307 |
inlinedChildren_.reset(); |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
// We have an active callee ICScript. This means the current ICScript must be |
| 312 |
// active too. |
| 313 |
MOZ_ASSERT(active()); |
| 314 |
} |
| 315 |
|
| 316 |
void JitScript::resetWarmUpCount(uint32_t count) { |
| 317 |
forEachICScript([&](ICScript* script) { script->resetWarmUpCount(count); }); |
| 318 |
} |
| 319 |
|
| 320 |
#ifdef DEBUG |
| 321 |
bool JitScript::hasActiveICScript() const { |
| 322 |
bool hasActive = false; |
| 323 |
forEachICScript([&](const ICScript* script) { |
| 324 |
if (script->active()) { |
| 325 |
hasActive = true; |
| 326 |
} |
| 327 |
}); |
| 328 |
return hasActive; |
| 329 |
} |
| 330 |
#endif |
| 331 |
|
| 332 |
void JitScript::resetAllActiveFlags() { |
| 333 |
forEachICScript([](ICScript* script) { script->resetActive(); }); |
| 334 |
} |
| 335 |
|
| 336 |
void JitScript::ensureProfileString(JSContext* cx, JSScript* script) { |
| 337 |
MOZ_ASSERT(cx->runtime()->geckoProfiler().enabled()); |
| 338 |
|
| 339 |
if (profileString_) { |
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 344 |
profileString_ = cx->runtime()->geckoProfiler().profileString(cx, script); |
| 345 |
if (!profileString_) { |
| 346 |
oomUnsafe.crash("Failed to allocate profile string"); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
/* static */ |
| 351 |
void JitScript::Destroy(Zone* zone, JitScript* script) { |
| 352 |
script->prepareForDestruction(zone); |
| 353 |
|
| 354 |
// Remove from JitZone's linked list of JitScripts. |
| 355 |
script->remove(); |
| 356 |
|
| 357 |
js_delete(script); |
| 358 |
} |
| 359 |
|
| 360 |
template <typename F> |
| 361 |
void JitScript::forEachICScript(const F& f) { |
| 362 |
f(&icScript_); |
| 363 |
if (hasInliningRoot()) { |
| 364 |
inliningRoot()->forEachInlinedScript(f); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
template <typename F> |
| 369 |
void JitScript::forEachICScript(const F& f) const { |
| 370 |
f(&icScript_); |
| 371 |
if (hasInliningRoot()) { |
| 372 |
inliningRoot()->forEachInlinedScript(f); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
void ICScript::prepareForDestruction(Zone* zone) { |
| 377 |
envAllocSite_ = nullptr; // Points into allocSitesSpace_. |
| 378 |
|
| 379 |
// Defer freeing AllocSite memory until after the next minor GC, because the |
| 380 |
// nursery can point to these alloc sites. |
| 381 |
JSRuntime* rt = zone->runtimeFromMainThread(); |
| 382 |
rt->gc.queueAllLifoBlocksForFreeAfterMinorGC(&allocSitesSpace_); |
| 383 |
|
| 384 |
// Trigger write barriers. |
| 385 |
PreWriteBarrier(zone, this); |
| 386 |
} |
| 387 |
|
| 388 |
void JitScript::prepareForDestruction(Zone* zone) { |
| 389 |
forEachICScript( |
| 390 |
[&](ICScript* script) { script->prepareForDestruction(zone); }); |
| 391 |
|
| 392 |
// Trigger write barriers. |
| 393 |
owningScript_ = nullptr; |
| 394 |
baselineScript_.set(zone, nullptr); |
| 395 |
ionScript_.set(zone, nullptr); |
| 396 |
} |
| 397 |
|
| 398 |
struct FallbackStubs { |
| 399 |
ICScript* const icScript_; |
| 400 |
|
| 401 |
explicit FallbackStubs(ICScript* icScript) : icScript_(icScript) {} |
| 402 |
|
| 403 |
size_t numEntries() const { return icScript_->numICEntries(); } |
| 404 |
ICFallbackStub* operator[](size_t index) const { |
| 405 |
return icScript_->fallbackStub(index); |
| 406 |
} |
| 407 |
}; |
| 408 |
|
| 409 |
static bool ComputeBinarySearchMid(FallbackStubs stubs, uint32_t pcOffset, |
| 410 |
size_t* loc) { |
| 411 |
return mozilla::BinarySearchIf( |
| 412 |
stubs, 0, stubs.numEntries(), |
| 413 |
[pcOffset](const ICFallbackStub* stub) { |
| 414 |
if (pcOffset < stub->pcOffset()) { |
| 415 |
return -1; |
| 416 |
} |
| 417 |
if (stub->pcOffset() < pcOffset) { |
| 418 |
return 1; |
| 419 |
} |
| 420 |
return 0; |
| 421 |
}, |
| 422 |
loc); |
| 423 |
} |
| 424 |
|
| 425 |
ICEntry& ICScript::icEntryFromPCOffset(uint32_t pcOffset) { |
| 426 |
size_t mid; |
| 427 |
bool success = ComputeBinarySearchMid(FallbackStubs(this), pcOffset, &mid); |
| 428 |
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED |
| 429 |
if (!success) { |
| 430 |
MOZ_CRASH_UNSAFE_PRINTF("Missing icEntry for offset %d (max offset: %d)", |
| 431 |
int(pcOffset), |
| 432 |
int(fallbackStub(numICEntries() - 1)->pcOffset())); |
| 433 |
} |
| 434 |
#endif |
| 435 |
MOZ_ALWAYS_TRUE(success); |
| 436 |
|
| 437 |
MOZ_ASSERT(mid < numICEntries()); |
| 438 |
|
| 439 |
ICEntry& entry = icEntry(mid); |
| 440 |
MOZ_ASSERT(fallbackStubForICEntry(&entry)->pcOffset() == pcOffset); |
| 441 |
return entry; |
| 442 |
} |
| 443 |
|
| 444 |
ICEntry* ICScript::interpreterICEntryFromPCOffset(uint32_t pcOffset) { |
| 445 |
// We have to return the entry to store in BaselineFrame::interpreterICEntry |
| 446 |
// when resuming in the Baseline Interpreter at pcOffset. The bytecode op at |
| 447 |
// pcOffset does not necessarily have an ICEntry, so we want to return the |
| 448 |
// first ICEntry for which the following is true: |
| 449 |
// |
| 450 |
// entry.pcOffset() >= pcOffset |
| 451 |
// |
| 452 |
// Fortunately, ComputeBinarySearchMid returns exactly this entry. |
| 453 |
|
| 454 |
size_t mid; |
| 455 |
ComputeBinarySearchMid(FallbackStubs(this), pcOffset, &mid); |
| 456 |
|
| 457 |
if (mid < numICEntries()) { |
| 458 |
ICEntry& entry = icEntry(mid); |
| 459 |
MOZ_ASSERT(fallbackStubForICEntry(&entry)->pcOffset() >= pcOffset); |
| 460 |
return &entry; |
| 461 |
} |
| 462 |
|
| 463 |
// Resuming at a pc after the last ICEntry. Just return nullptr: |
| 464 |
// BaselineFrame::interpreterICEntry will never be used in this case. |
| 465 |
return nullptr; |
| 466 |
} |
| 467 |
|
| 468 |
void JitScript::purgeInactiveICScripts() { |
| 469 |
if (!hasInliningRoot()) { |
| 470 |
return; |
| 471 |
} |
| 472 |
|
| 473 |
forEachICScript([](ICScript* script) { script->purgeInactiveICScripts(); }); |
| 474 |
|
| 475 |
inliningRoot()->purgeInactiveICScripts(); |
| 476 |
if (inliningRoot()->numInlinedScripts() == 0) { |
| 477 |
inliningRoot_.reset(); |
| 478 |
icScript()->inliningRoot_ = nullptr; |
| 479 |
} else { |
| 480 |
// If a callee script is active on the stack, the root script must be active |
| 481 |
// too. |
| 482 |
MOZ_ASSERT(icScript()->active()); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
void JitScript::purgeStubs(JSScript* script, ICStubSpace& newStubSpace) { |
| 487 |
MOZ_ASSERT(script->jitScript() == this); |
| 488 |
|
| 489 |
Zone* zone = script->zone(); |
| 490 |
if (IsAboutToBeFinalizedUnbarriered(script)) { |
| 491 |
// We're sweeping and the script is dead. Don't purge optimized stubs |
| 492 |
// because (1) accessing CacheIRStubInfo pointers in ICStubs is invalid |
| 493 |
// because we may have swept them already when we started (incremental) |
| 494 |
// sweeping and (2) it's unnecessary because this script will be finalized |
| 495 |
// soon anyway. |
| 496 |
return; |
| 497 |
} |
| 498 |
|
| 499 |
JitSpew(JitSpew_BaselineIC, "Purging optimized stubs"); |
| 500 |
|
| 501 |
forEachICScript( |
| 502 |
[&](ICScript* script) { script->purgeStubs(zone, newStubSpace); }); |
| 503 |
|
| 504 |
notePurgedStubs(); |
| 505 |
} |
| 506 |
|
| 507 |
void ICScript::purgeStubs(Zone* zone, ICStubSpace& newStubSpace) { |
| 508 |
for (size_t i = 0; i < numICEntries(); i++) { |
| 509 |
ICEntry& entry = icEntry(i); |
| 510 |
ICFallbackStub* fallback = fallbackStub(i); |
| 511 |
|
| 512 |
// If this is a trial inlining call site and the callee's ICScript hasn't |
| 513 |
// been discarded, clone the IC chain instead of purging stubs. In this case |
| 514 |
// both the current ICScript and the callee's inlined ICScript must be |
| 515 |
// active on the stack. |
| 516 |
// |
| 517 |
// We can't purge the IC stubs in this case because it'd confuse trial |
| 518 |
// inlining if we try to inline again later and we already have an ICScript |
| 519 |
// for this call site. |
| 520 |
if (fallback->trialInliningState() == TrialInliningState::Inlined && |
| 521 |
hasInlinedChild(fallback->pcOffset())) { |
| 522 |
MOZ_ASSERT(active()); |
| 523 |
#ifdef DEBUG |
| 524 |
// The callee script must be active. Also assert its bytecode size field |
| 525 |
// is valid, because this helps catch memory safety issues (bug 1871947). |
| 526 |
ICScript* callee = findInlinedChild(fallback->pcOffset()); |
| 527 |
MOZ_ASSERT(callee->active()); |
| 528 |
MOZ_ASSERT(callee->bytecodeSize() < inliningRoot()->totalBytecodeSize()); |
| 529 |
#endif |
| 530 |
|
| 531 |
JSRuntime* rt = zone->runtimeFromMainThread(); |
| 532 |
ICCacheIRStub* prev = nullptr; |
| 533 |
ICStub* stub = entry.firstStub(); |
| 534 |
while (stub != fallback) { |
| 535 |
ICCacheIRStub* clone = stub->toCacheIRStub()->clone( |
| 536 |
rt, newStubSpace, ICCacheIRStub::ICScriptHandling::AssertActive); |
| 537 |
if (prev) { |
| 538 |
prev->setNext(clone); |
| 539 |
} else { |
| 540 |
entry.setFirstStub(clone); |
| 541 |
} |
| 542 |
MOZ_ASSERT(stub->toCacheIRStub()->next() == clone->next()); |
| 543 |
prev = clone; |
| 544 |
stub = clone->next(); |
| 545 |
} |
| 546 |
continue; |
| 547 |
} |
| 548 |
|
| 549 |
MOZ_ASSERT(!hasInlinedChild(fallback->pcOffset())); |
| 550 |
|
| 551 |
fallback->discardStubs(zone, &entry); |
| 552 |
fallback->state().reset(); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
bool JitScript::ensureHasCachedBaselineJitData(JSContext* cx, |
| 557 |
HandleScript script) { |
| 558 |
if (templateEnv_.isSome()) { |
| 559 |
return true; |
| 560 |
} |
| 561 |
|
| 562 |
if (!script->function() || |
| 563 |
!script->function()->needsFunctionEnvironmentObjects()) { |
| 564 |
templateEnv_.emplace(); |
| 565 |
return true; |
| 566 |
} |
| 567 |
|
| 568 |
Rooted<EnvironmentObject*> templateEnv(cx); |
| 569 |
Rooted<JSFunction*> fun(cx, script->function()); |
| 570 |
|
| 571 |
if (fun->needsNamedLambdaEnvironment()) { |
| 572 |
templateEnv = NamedLambdaObject::createTemplateObject(cx, fun); |
| 573 |
if (!templateEnv) { |
| 574 |
return false; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
if (fun->needsCallObject()) { |
| 579 |
templateEnv = CallObject::createTemplateObject(cx, script, templateEnv); |
| 580 |
if (!templateEnv) { |
| 581 |
return false; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
templateEnv_.emplace(templateEnv); |
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
bool JitScript::ensureHasCachedIonData(JSContext* cx, HandleScript script) { |
| 590 |
MOZ_ASSERT(script->jitScript() == this); |
| 591 |
|
| 592 |
if (usesEnvironmentChain_.isSome()) { |
| 593 |
return true; |
| 594 |
} |
| 595 |
|
| 596 |
if (!ensureHasCachedBaselineJitData(cx, script)) { |
| 597 |
return false; |
| 598 |
} |
| 599 |
|
| 600 |
usesEnvironmentChain_.emplace(ScriptUsesEnvironmentChain(script)); |
| 601 |
return true; |
| 602 |
} |
| 603 |
|
| 604 |
std::pair<CallObject*, NamedLambdaObject*> |
| 605 |
JitScript::functionEnvironmentTemplates(JSFunction* fun) const { |
| 606 |
EnvironmentObject* templateEnv = templateEnvironment(); |
| 607 |
|
| 608 |
CallObject* callObjectTemplate = nullptr; |
| 609 |
if (fun->needsCallObject()) { |
| 610 |
callObjectTemplate = &templateEnv->as<CallObject>(); |
| 611 |
} |
| 612 |
|
| 613 |
NamedLambdaObject* namedLambdaTemplate = nullptr; |
| 614 |
if (fun->needsNamedLambdaEnvironment()) { |
| 615 |
if (callObjectTemplate) { |
| 616 |
namedLambdaTemplate = |
| 617 |
&callObjectTemplate->enclosingEnvironment().as<NamedLambdaObject>(); |
| 618 |
} else { |
| 619 |
namedLambdaTemplate = &templateEnv->as<NamedLambdaObject>(); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
return {callObjectTemplate, namedLambdaTemplate}; |
| 624 |
} |
| 625 |
|
| 626 |
void JitScript::setBaselineScriptImpl(JSScript* script, |
| 627 |
BaselineScript* baselineScript) { |
| 628 |
JSRuntime* rt = script->runtimeFromMainThread(); |
| 629 |
setBaselineScriptImpl(rt->gcContext(), script, baselineScript); |
| 630 |
} |
| 631 |
|
| 632 |
void JitScript::setBaselineScriptImpl(JS::GCContext* gcx, JSScript* script, |
| 633 |
BaselineScript* baselineScript) { |
| 634 |
if (hasBaselineScript()) { |
| 635 |
gcx->removeCellMemory(script, baselineScript_->allocBytes(), |
| 636 |
MemoryUse::BaselineScript); |
| 637 |
baselineScript_.set(script->zone(), nullptr); |
| 638 |
} |
| 639 |
|
| 640 |
MOZ_ASSERT(ionScript_ == nullptr || ionScript_ == IonDisabledScriptPtr); |
| 641 |
|
| 642 |
baselineScript_.set(script->zone(), baselineScript); |
| 643 |
if (hasBaselineScript()) { |
| 644 |
AddCellMemory(script, baselineScript_->allocBytes(), |
| 645 |
MemoryUse::BaselineScript); |
| 646 |
} |
| 647 |
|
| 648 |
script->resetWarmUpResetCounter(); |
| 649 |
script->updateJitCodeRaw(gcx->runtime()); |
| 650 |
} |
| 651 |
|
| 652 |
void JitScript::setIonScriptImpl(JSScript* script, IonScript* ionScript) { |
| 653 |
JSRuntime* rt = script->runtimeFromMainThread(); |
| 654 |
setIonScriptImpl(rt->gcContext(), script, ionScript); |
| 655 |
} |
| 656 |
|
| 657 |
void JitScript::setIonScriptImpl(JS::GCContext* gcx, JSScript* script, |
| 658 |
IonScript* ionScript) { |
| 659 |
MOZ_ASSERT_IF(ionScript != IonDisabledScriptPtr, |
| 660 |
!baselineScript()->hasPendingIonCompileTask()); |
| 661 |
|
| 662 |
JS::Zone* zone = script->zone(); |
| 663 |
if (hasIonScript()) { |
| 664 |
gcx->removeCellMemory(script, ionScript_->allocBytes(), |
| 665 |
MemoryUse::IonScript); |
| 666 |
ionScript_.set(zone, nullptr); |
| 667 |
} |
| 668 |
|
| 669 |
ionScript_.set(zone, ionScript); |
| 670 |
MOZ_ASSERT_IF(hasIonScript(), hasBaselineScript()); |
| 671 |
if (hasIonScript()) { |
| 672 |
AddCellMemory(script, ionScript_->allocBytes(), MemoryUse::IonScript); |
| 673 |
} |
| 674 |
|
| 675 |
script->updateJitCodeRaw(gcx->runtime()); |
| 676 |
} |
| 677 |
|
| 678 |
#ifdef JS_STRUCTURED_SPEW |
| 679 |
static bool HasEnteredCounters(ICEntry& entry) { |
| 680 |
ICStub* stub = entry.firstStub(); |
| 681 |
if (stub && !stub->isFallback()) { |
| 682 |
return true; |
| 683 |
} |
| 684 |
return false; |
| 685 |
} |
| 686 |
|
| 687 |
void jit::JitSpewBaselineICStats(JSScript* script, const char* dumpReason) { |
| 688 |
MOZ_ASSERT(script->hasJitScript()); |
| 689 |
JSContext* cx = TlsContext.get(); |
| 690 |
AutoStructuredSpewer spew(cx, SpewChannel::BaselineICStats, script); |
| 691 |
if (!spew) { |
| 692 |
return; |
| 693 |
} |
| 694 |
|
| 695 |
JitScript* jitScript = script->jitScript(); |
| 696 |
spew->property("reason", dumpReason); |
| 697 |
spew->beginListProperty("entries"); |
| 698 |
for (size_t i = 0; i < jitScript->numICEntries(); i++) { |
| 699 |
ICEntry& entry = jitScript->icEntry(i); |
| 700 |
ICFallbackStub* fallback = jitScript->fallbackStub(i); |
| 701 |
if (!HasEnteredCounters(entry)) { |
| 702 |
continue; |
| 703 |
} |
| 704 |
|
| 705 |
uint32_t pcOffset = fallback->pcOffset(); |
| 706 |
jsbytecode* pc = script->offsetToPC(pcOffset); |
| 707 |
|
| 708 |
JS::LimitedColumnNumberOneOrigin column; |
| 709 |
unsigned int line = PCToLineNumber(script, pc, &column); |
| 710 |
|
| 711 |
spew->beginObject(); |
| 712 |
spew->property("op", CodeName(JSOp(*pc))); |
| 713 |
spew->property("pc", pcOffset); |
| 714 |
spew->property("line", line); |
| 715 |
spew->property("column", column.oneOriginValue()); |
| 716 |
|
| 717 |
spew->beginListProperty("counts"); |
| 718 |
ICStub* stub = entry.firstStub(); |
| 719 |
while (stub && !stub->isFallback()) { |
| 720 |
uint32_t count = stub->enteredCount(); |
| 721 |
spew->value(count); |
| 722 |
stub = stub->toCacheIRStub()->next(); |
| 723 |
} |
| 724 |
spew->endList(); |
| 725 |
spew->property("fallback_count", fallback->enteredCount()); |
| 726 |
spew->endObject(); |
| 727 |
} |
| 728 |
spew->endList(); |
| 729 |
} |
| 730 |
#endif |
| 731 |
|
| 732 |
using StubHashMap = HashMap<ICCacheIRStub*, ICCacheIRStub*, |
| 733 |
DefaultHasher<ICCacheIRStub*>, SystemAllocPolicy>; |
| 734 |
|
| 735 |
static void MarkActiveICScriptsAndCopyStubs( |
| 736 |
JSContext* cx, const JitActivationIterator& activation, |
| 737 |
ICStubSpace& newStubSpace, StubHashMap& alreadyClonedStubs) { |
| 738 |
for (OnlyJSJitFrameIter iter(activation); !iter.done(); ++iter) { |
| 739 |
const JSJitFrameIter& frame = iter.frame(); |
| 740 |
switch (frame.type()) { |
| 741 |
case FrameType::BaselineJS: |
| 742 |
frame.script()->jitScript()->icScript()->setActive(); |
| 743 |
// If the frame is using a trial-inlining ICScript, we have to preserve |
| 744 |
// it too. |
| 745 |
if (frame.baselineFrame()->icScript()->isInlined()) { |
| 746 |
frame.baselineFrame()->icScript()->setActive(); |
| 747 |
} |
| 748 |
break; |
| 749 |
case FrameType::BaselineStub: { |
| 750 |
auto* layout = reinterpret_cast<BaselineStubFrameLayout*>(frame.fp()); |
| 751 |
if (layout->maybeStubPtr() && !layout->maybeStubPtr()->isFallback()) { |
| 752 |
ICCacheIRStub* stub = layout->maybeStubPtr()->toCacheIRStub(); |
| 753 |
auto lookup = alreadyClonedStubs.lookupForAdd(stub); |
| 754 |
if (!lookup) { |
| 755 |
ICCacheIRStub* newStub = |
| 756 |
stub->clone(cx->runtime(), newStubSpace, |
| 757 |
ICCacheIRStub::ICScriptHandling::MarkActive); |
| 758 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 759 |
if (!alreadyClonedStubs.add(lookup, stub, newStub)) { |
| 760 |
oomUnsafe.crash("MarkActiveICScriptsAndCopyStubs"); |
| 761 |
} |
| 762 |
} |
| 763 |
layout->setStubPtr(lookup->value()); |
| 764 |
} |
| 765 |
break; |
| 766 |
} |
| 767 |
case FrameType::Exit: |
| 768 |
if (frame.exitFrame()->is<LazyLinkExitFrameLayout>()) { |
| 769 |
LazyLinkExitFrameLayout* ll = |
| 770 |
frame.exitFrame()->as<LazyLinkExitFrameLayout>(); |
| 771 |
JSScript* script = |
| 772 |
ScriptFromCalleeToken(ll->jsFrame()->calleeToken()); |
| 773 |
script->jitScript()->icScript()->setActive(); |
| 774 |
} |
| 775 |
break; |
| 776 |
case FrameType::Bailout: |
| 777 |
case FrameType::IonJS: { |
| 778 |
// Keep the JitScript and BaselineScript around, since bailouts from |
| 779 |
// the ion jitcode need to re-enter into the Baseline code. |
| 780 |
frame.script()->jitScript()->icScript()->setActive(); |
| 781 |
for (InlineFrameIterator inlineIter(cx, &frame); inlineIter.more(); |
| 782 |
++inlineIter) { |
| 783 |
inlineIter.script()->jitScript()->icScript()->setActive(); |
| 784 |
} |
| 785 |
// Because we're purging ICScripts, the bailout machinery should use |
| 786 |
// the generic ICScript for inlined callees. |
| 787 |
frame.ionScript()->notePurgedICScripts(); |
| 788 |
break; |
| 789 |
} |
| 790 |
default:; |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
void jit::MarkActiveICScriptsAndCopyStubs(Zone* zone, |
| 796 |
ICStubSpace& newStubSpace) { |
| 797 |
if (zone->isAtomsZone()) { |
| 798 |
return; |
| 799 |
} |
| 800 |
StubHashMap alreadyClonedStubs; |
| 801 |
JSContext* cx = TlsContext.get(); |
| 802 |
for (JitActivationIterator iter(cx); !iter.done(); ++iter) { |
| 803 |
if (iter->compartment()->zone() == zone) { |
| 804 |
MarkActiveICScriptsAndCopyStubs(cx, iter, newStubSpace, |
| 805 |
alreadyClonedStubs); |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
InliningRoot* JitScript::getOrCreateInliningRoot(JSContext* cx, |
| 811 |
JSScript* script) { |
| 812 |
MOZ_ASSERT(script->jitScript() == this); |
| 813 |
|
| 814 |
if (!inliningRoot_) { |
| 815 |
inliningRoot_ = js::MakeUnique<InliningRoot>(cx, script); |
| 816 |
if (!inliningRoot_) { |
| 817 |
ReportOutOfMemory(cx); |
| 818 |
return nullptr; |
| 819 |
} |
| 820 |
icScript_.inliningRoot_ = inliningRoot_.get(); |
| 821 |
} |
| 822 |
return inliningRoot_.get(); |
| 823 |
} |
| 824 |
|
| 825 |
gc::AllocSite* ICScript::getOrCreateAllocSite(JSScript* outerScript, |
| 826 |
uint32_t pcOffset) { |
| 827 |
// The script must be the outer script. |
| 828 |
MOZ_ASSERT(outerScript->jitScript()->icScript() == this || |
| 829 |
(inliningRoot() && inliningRoot()->owningScript() == outerScript)); |
| 830 |
|
| 831 |
// The pcOffset must be valid for this (maybe inlined) script. |
| 832 |
MOZ_ASSERT_IF(pcOffset != gc::AllocSite::EnvSitePCOffset, |
| 833 |
pcOffset < bytecodeSize()); |
| 834 |
|
| 835 |
for (gc::AllocSite* site : allocSites_) { |
| 836 |
if (site->pcOffset() == pcOffset) { |
| 837 |
MOZ_ASSERT(site->isNormal()); |
| 838 |
MOZ_ASSERT(site->script() == outerScript); |
| 839 |
MOZ_ASSERT(site->traceKind() == JS::TraceKind::Object); |
| 840 |
return site; |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
Nursery& nursery = outerScript->runtimeFromMainThread()->gc.nursery(); |
| 845 |
if (!nursery.canCreateAllocSite()) { |
| 846 |
// Don't block attaching an optimized stub, but don't process allocations |
| 847 |
// for this site. |
| 848 |
return outerScript->zone()->unknownAllocSite(JS::TraceKind::Object); |
| 849 |
} |
| 850 |
|
| 851 |
if (!allocSites_.reserve(allocSites_.length() + 1)) { |
| 852 |
return nullptr; |
| 853 |
} |
| 854 |
|
| 855 |
auto* site = allocSitesSpace_.new_<gc::AllocSite>( |
| 856 |
outerScript->zone(), outerScript, pcOffset, JS::TraceKind::Object); |
| 857 |
if (!site) { |
| 858 |
return nullptr; |
| 859 |
} |
| 860 |
|
| 861 |
allocSites_.infallibleAppend(site); |
| 862 |
|
| 863 |
nursery.noteAllocSiteCreated(); |
| 864 |
|
| 865 |
return site; |
| 866 |
} |
| 867 |
|
| 868 |
void ICScript::ensureEnvAllocSite(JSScript* outerScript) { |
| 869 |
if (envAllocSite_) { |
| 870 |
return; |
| 871 |
} |
| 872 |
|
| 873 |
// Use a dummy offset for this site. |
| 874 |
uint32_t pcoffset = gc::AllocSite::EnvSitePCOffset; |
| 875 |
gc::AllocSite* site = getOrCreateAllocSite(outerScript, pcoffset); |
| 876 |
if (!site) { |
| 877 |
// Use the unknown site on failure. |
| 878 |
site = outerScript->zone()->unknownAllocSite(JS::TraceKind::Object); |
| 879 |
} |
| 880 |
|
| 881 |
envAllocSite_ = site; |
| 882 |
} |
| 883 |
|
| 884 |
bool JitScript::resetAllocSites(bool resetNurserySites, |
| 885 |
bool resetPretenuredSites) { |
| 886 |
MOZ_ASSERT(resetNurserySites || resetPretenuredSites); |
| 887 |
|
| 888 |
bool anyReset = false; |
| 889 |
|
| 890 |
forEachICScript([&](ICScript* script) { |
| 891 |
for (gc::AllocSite* site : script->allocSites_) { |
| 892 |
if ((resetNurserySites && site->initialHeap() == gc::Heap::Default) || |
| 893 |
(resetPretenuredSites && site->initialHeap() == gc::Heap::Tenured)) { |
| 894 |
if (site->maybeResetState()) { |
| 895 |
anyReset = true; |
| 896 |
} |
| 897 |
} |
| 898 |
} |
| 899 |
}); |
| 900 |
|
| 901 |
return anyReset; |
| 902 |
} |
| 903 |
|
| 904 |
bool JitScript::hasPretenuredAllocSites() { |
| 905 |
bool found = false; |
| 906 |
forEachICScript([&](ICScript* script) { |
| 907 |
if (!found) { |
| 908 |
for (gc::AllocSite* site : script->allocSites_) { |
| 909 |
if (site->initialHeap() == gc::Heap::Tenured) { |
| 910 |
found = true; |
| 911 |
} |
| 912 |
} |
| 913 |
} |
| 914 |
}); |
| 915 |
|
| 916 |
return found; |
| 917 |
} |
| 918 |
|
| 919 |
void JitScript::addSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf, |
| 920 |
size_t* data, size_t* allocSites) const { |
| 921 |
*data += mallocSizeOf(this); |
| 922 |
|
| 923 |
forEachICScript([=](const ICScript* script) { |
| 924 |
// |data| already includes the outer ICScript because it's part of the |
| 925 |
// JitScript. |
| 926 |
if (script != &icScript_) { |
| 927 |
*data += mallocSizeOf(script); |
| 928 |
} |
| 929 |
|
| 930 |
// |data| already includes the LifoAlloc and Vector, so use |
| 931 |
// sizeOfExcludingThis. |
| 932 |
*allocSites += script->allocSitesSpace_.sizeOfExcludingThis(mallocSizeOf); |
| 933 |
*allocSites += script->allocSites_.sizeOfExcludingThis(mallocSizeOf); |
| 934 |
}); |
| 935 |
} |
| 936 |
|
| 937 |
JitScript* ICScript::outerJitScript() { |
| 938 |
MOZ_ASSERT(!isInlined()); |
| 939 |
uint8_t* ptr = reinterpret_cast<uint8_t*>(this); |
| 940 |
return reinterpret_cast<JitScript*>(ptr - JitScript::offsetOfICScript()); |
| 941 |
} |
| 942 |
|
| 943 |
#ifdef DEBUG |
| 944 |
// This hash is used to verify that we do not recompile after a |
| 945 |
// TranspiledCacheIR invalidation with the exact same ICs. |
| 946 |
// |
| 947 |
// It should change iff an ICEntry in this ICScript (or an ICScript |
| 948 |
// inlined into this ICScript) is modified such that we will make a |
| 949 |
// different decision in WarpScriptOracle::maybeInlineIC. This means: |
| 950 |
// |
| 951 |
// 1. The hash will change if we attach a new stub. |
| 952 |
// 2. The hash will change if the entered count of any CacheIR stub |
| 953 |
// other than the first changes from 0. |
| 954 |
// 3. The hash will change if the entered count of the fallback stub |
| 955 |
// changes from 0. |
| 956 |
// 4. The hash will change if the failure count of the fallback stub |
| 957 |
// changes from 0. |
| 958 |
HashNumber ICScript::hash() { |
| 959 |
HashNumber h = 0; |
| 960 |
for (size_t i = 0; i < numICEntries(); i++) { |
| 961 |
ICStub* stub = icEntry(i).firstStub(); |
| 962 |
|
| 963 |
// Hash the address of the first stub. |
| 964 |
h = mozilla::AddToHash(h, stub); |
| 965 |
|
| 966 |
// Hash whether subsequent stubs have entry count 0. |
| 967 |
if (!stub->isFallback()) { |
| 968 |
stub = stub->toCacheIRStub()->next(); |
| 969 |
while (!stub->isFallback()) { |
| 970 |
h = mozilla::AddToHash(h, stub->enteredCount() == 0); |
| 971 |
stub = stub->toCacheIRStub()->next(); |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
// Hash whether the fallback has entry count 0 and failure count 0. |
| 976 |
MOZ_ASSERT(stub->isFallback()); |
| 977 |
h = mozilla::AddToHash(h, stub->enteredCount() == 0); |
| 978 |
h = mozilla::AddToHash(h, stub->toFallbackStub()->state().hasFailures()); |
| 979 |
} |
| 980 |
|
| 981 |
return h; |
| 982 |
} |
| 983 |
#endif |