| 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/BaselineJIT.h" |
| 8 |
|
| 9 |
#include "mozilla/BinarySearch.h" |
| 10 |
#include "mozilla/CheckedInt.h" |
| 11 |
#include "mozilla/MemoryReporting.h" |
| 12 |
#include "mozilla/ScopeExit.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
|
| 16 |
#include "debugger/DebugAPI.h" |
| 17 |
#include "gc/GCContext.h" |
| 18 |
#include "gc/PublicIterators.h" |
| 19 |
#include "jit/AutoWritableJitCode.h" |
| 20 |
#include "jit/BaselineCodeGen.h" |
| 21 |
#include "jit/BaselineCompileTask.h" |
| 22 |
#include "jit/BaselineDebugModeOSR.h" |
| 23 |
#include "jit/BaselineIC.h" |
| 24 |
#include "jit/CalleeToken.h" |
| 25 |
#include "jit/Ion.h" |
| 26 |
#include "jit/IonOptimizationLevels.h" |
| 27 |
#include "jit/JitCommon.h" |
| 28 |
#include "jit/JitRuntime.h" |
| 29 |
#include "jit/JitSpewer.h" |
| 30 |
#include "jit/MacroAssembler.h" |
| 31 |
#include "js/friend/StackLimits.h" // js::AutoCheckRecursionLimit |
| 32 |
#include "vm/Interpreter.h" |
| 33 |
|
| 34 |
#include "debugger/DebugAPI-inl.h" |
| 35 |
#include "gc/GC-inl.h" |
| 36 |
#include "jit/JitHints-inl.h" |
| 37 |
#include "jit/JitScript-inl.h" |
| 38 |
#include "vm/GeckoProfiler-inl.h" |
| 39 |
#include "vm/JSScript-inl.h" |
| 40 |
#include "vm/Stack-inl.h" |
| 41 |
|
| 42 |
using mozilla::BinarySearchIf; |
| 43 |
using mozilla::CheckedInt; |
| 44 |
|
| 45 |
using namespace js; |
| 46 |
using namespace js::jit; |
| 47 |
|
| 48 |
void ICStubSpace::freeAllAfterMinorGC(Zone* zone) { |
| 49 |
if (zone->isAtomsZone()) { |
| 50 |
MOZ_ASSERT(allocator_.isEmpty()); |
| 51 |
} else { |
| 52 |
JSRuntime* rt = zone->runtimeFromMainThread(); |
| 53 |
rt->gc.queueAllLifoBlocksForFreeAfterMinorGC(&allocator_); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
static bool CheckFrame(InterpreterFrame* fp) { |
| 58 |
if (fp->isDebuggerEvalFrame()) { |
| 59 |
// Debugger eval-in-frame. These are likely short-running scripts so |
| 60 |
// don't bother compiling them for now. |
| 61 |
JitSpew(JitSpew_BaselineAbort, "debugger frame"); |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
if (fp->isFunctionFrame() && TooManyActualArguments(fp->numActualArgs())) { |
| 66 |
// Fall back to the interpreter to avoid running out of stack space. |
| 67 |
JitSpew(JitSpew_BaselineAbort, "Too many arguments (%u)", |
| 68 |
fp->numActualArgs()); |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
struct EnterJitData { |
| 76 |
explicit EnterJitData(JSContext* cx) |
| 77 |
: jitcode(nullptr), |
| 78 |
osrFrame(nullptr), |
| 79 |
calleeToken(nullptr), |
| 80 |
maxArgv(nullptr), |
| 81 |
maxArgc(0), |
| 82 |
numActualArgs(0), |
| 83 |
osrNumStackValues(0), |
| 84 |
envChain(cx), |
| 85 |
result(cx), |
| 86 |
constructing(false) {} |
| 87 |
|
| 88 |
uint8_t* jitcode; |
| 89 |
InterpreterFrame* osrFrame; |
| 90 |
|
| 91 |
void* calleeToken; |
| 92 |
|
| 93 |
Value* maxArgv; |
| 94 |
unsigned maxArgc; |
| 95 |
unsigned numActualArgs; |
| 96 |
unsigned osrNumStackValues; |
| 97 |
|
| 98 |
RootedObject envChain; |
| 99 |
RootedValue result; |
| 100 |
|
| 101 |
bool constructing; |
| 102 |
}; |
| 103 |
|
| 104 |
static JitExecStatus EnterBaseline(JSContext* cx, EnterJitData& data) { |
| 105 |
MOZ_ASSERT(data.osrFrame); |
| 106 |
|
| 107 |
// Check for potential stack overflow before OSR-ing. |
| 108 |
uint32_t extra = |
| 109 |
BaselineFrame::Size() + (data.osrNumStackValues * sizeof(Value)); |
| 110 |
AutoCheckRecursionLimit recursion(cx); |
| 111 |
if (!recursion.checkWithExtra(cx, extra)) { |
| 112 |
return JitExec_Aborted; |
| 113 |
} |
| 114 |
|
| 115 |
#ifdef DEBUG |
| 116 |
// Assert we don't GC before entering JIT code. A GC could discard JIT code |
| 117 |
// or move the function stored in the CalleeToken (it won't be traced at |
| 118 |
// this point). We use Maybe<> here so we can call reset() to call the |
| 119 |
// AutoAssertNoGC destructor before we enter JIT code. |
| 120 |
mozilla::Maybe<JS::AutoAssertNoGC> nogc; |
| 121 |
nogc.emplace(cx); |
| 122 |
#endif |
| 123 |
|
| 124 |
MOZ_ASSERT(IsBaselineInterpreterEnabled()); |
| 125 |
MOZ_ASSERT(CheckFrame(data.osrFrame)); |
| 126 |
|
| 127 |
EnterJitCode enter = cx->runtime()->jitRuntime()->enterJit(); |
| 128 |
|
| 129 |
// Caller must construct |this| before invoking the function. |
| 130 |
MOZ_ASSERT_IF(data.constructing, |
| 131 |
data.maxArgv[0].isObject() || |
| 132 |
data.maxArgv[0].isMagic(JS_UNINITIALIZED_LEXICAL)); |
| 133 |
|
| 134 |
data.result.setInt32(data.numActualArgs); |
| 135 |
{ |
| 136 |
AssertRealmUnchanged aru(cx); |
| 137 |
JitActivation activation(cx); |
| 138 |
|
| 139 |
data.osrFrame->setRunningInJit(); |
| 140 |
|
| 141 |
#ifdef DEBUG |
| 142 |
nogc.reset(); |
| 143 |
#endif |
| 144 |
// Single transition point from Interpreter to Baseline. |
| 145 |
CALL_GENERATED_CODE(enter, data.jitcode, data.maxArgc, data.maxArgv, |
| 146 |
data.osrFrame, data.calleeToken, data.envChain.get(), |
| 147 |
data.osrNumStackValues, data.result.address()); |
| 148 |
|
| 149 |
data.osrFrame->clearRunningInJit(); |
| 150 |
} |
| 151 |
|
| 152 |
// Jit callers wrap primitive constructor return, except for derived |
| 153 |
// class constructors, which are forced to do it themselves. |
| 154 |
if (!data.result.isMagic() && data.constructing && |
| 155 |
data.result.isPrimitive()) { |
| 156 |
MOZ_ASSERT(data.maxArgv[0].isObject()); |
| 157 |
data.result = data.maxArgv[0]; |
| 158 |
} |
| 159 |
|
| 160 |
// Release temporary buffer used for OSR into Ion. |
| 161 |
cx->runtime()->jitRuntime()->freeIonOsrTempData(); |
| 162 |
|
| 163 |
MOZ_ASSERT_IF(data.result.isMagic(), data.result.isMagic(JS_ION_ERROR)); |
| 164 |
return data.result.isMagic() ? JitExec_Error : JitExec_Ok; |
| 165 |
} |
| 166 |
|
| 167 |
JitExecStatus jit::EnterBaselineInterpreterAtBranch(JSContext* cx, |
| 168 |
InterpreterFrame* fp, |
| 169 |
jsbytecode* pc) { |
| 170 |
MOZ_ASSERT(JSOp(*pc) == JSOp::LoopHead); |
| 171 |
|
| 172 |
EnterJitData data(cx); |
| 173 |
|
| 174 |
// Use the entry point that skips the debug trap because the C++ interpreter |
| 175 |
// already handled this for the current op. |
| 176 |
const BaselineInterpreter& interp = |
| 177 |
cx->runtime()->jitRuntime()->baselineInterpreter(); |
| 178 |
data.jitcode = interp.interpretOpNoDebugTrapAddr().value; |
| 179 |
|
| 180 |
data.osrFrame = fp; |
| 181 |
data.osrNumStackValues = |
| 182 |
fp->script()->nfixed() + cx->interpreterRegs().stackDepth(); |
| 183 |
|
| 184 |
if (fp->isFunctionFrame()) { |
| 185 |
data.constructing = fp->isConstructing(); |
| 186 |
data.numActualArgs = fp->numActualArgs(); |
| 187 |
data.maxArgc = std::max(fp->numActualArgs(), fp->numFormalArgs()) + |
| 188 |
1; // +1 = include |this| |
| 189 |
data.maxArgv = fp->argv() - 1; // -1 = include |this| |
| 190 |
data.envChain = nullptr; |
| 191 |
data.calleeToken = CalleeToToken(&fp->callee(), data.constructing); |
| 192 |
} else { |
| 193 |
data.constructing = false; |
| 194 |
data.numActualArgs = 0; |
| 195 |
data.maxArgc = 0; |
| 196 |
data.maxArgv = nullptr; |
| 197 |
data.envChain = fp->environmentChain(); |
| 198 |
data.calleeToken = CalleeToToken(fp->script()); |
| 199 |
} |
| 200 |
|
| 201 |
JitExecStatus status = EnterBaseline(cx, data); |
| 202 |
if (status != JitExec_Ok) { |
| 203 |
return status; |
| 204 |
} |
| 205 |
|
| 206 |
fp->setReturnValue(data.result); |
| 207 |
return JitExec_Ok; |
| 208 |
} |
| 209 |
|
| 210 |
static bool OffThreadBaselineCompilationAvailable(JSContext* cx, |
| 211 |
JSScript* script) { |
| 212 |
if (!cx->runtime()->canUseOffthreadBaselineCompilation()) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
// TODO: Support off-thread scriptcounts? |
| 216 |
if (cx->runtime()->profilingScripts) { |
| 217 |
return false; |
| 218 |
} |
| 219 |
if (script->hasScriptCounts() || cx->realm()->collectCoverageForDebug()) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
if (script->isDebuggee()) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
return CanUseExtraThreads(); |
| 226 |
} |
| 227 |
|
| 228 |
static bool DispatchOffThreadBaselineCompile(JSContext* cx, |
| 229 |
BaselineSnapshot& snapshot) { |
| 230 |
JSScript* script = snapshot.script(); |
| 231 |
MOZ_ASSERT(OffThreadBaselineCompilationAvailable(cx, script)); |
| 232 |
|
| 233 |
auto alloc = cx->make_unique<LifoAlloc>(TempAllocator::PreferredLifoChunkSize, |
| 234 |
js::BackgroundMallocArena); |
| 235 |
if (!alloc) { |
| 236 |
ReportOutOfMemory(cx); |
| 237 |
return false; |
| 238 |
} |
| 239 |
auto* snapshotCopy = alloc->new_<OffThreadBaselineSnapshot>(snapshot); |
| 240 |
if (!snapshotCopy) { |
| 241 |
ReportOutOfMemory(cx); |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
BaselineSnapshotList snapshots; |
| 246 |
snapshots.insertFront(snapshotCopy); |
| 247 |
CompileRealm* realm = CompileRealm::get(cx->realm()); |
| 248 |
BaselineCompileTask* task = alloc->new_<BaselineCompileTask>( |
| 249 |
realm, alloc.get(), std::move(snapshots)); |
| 250 |
if (!task) { |
| 251 |
snapshots.clear(); |
| 252 |
ReportOutOfMemory(cx); |
| 253 |
return false; |
| 254 |
} |
| 255 |
|
| 256 |
AutoLockHelperThreadState lock; |
| 257 |
if (!StartOffThreadBaselineCompile(task, lock)) { |
| 258 |
ReportOutOfMemory(cx); |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
script->jitScript()->setIsBaselineCompiling(script); |
| 263 |
|
| 264 |
// The allocator and associated data will be destroyed after being |
| 265 |
// processed in the finishedOffThreadCompilations list. |
| 266 |
(void)alloc.release(); |
| 267 |
|
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
bool jit::DispatchOffThreadBaselineBatch(JSContext* cx) { |
| 272 |
BaselineCompileQueue& queue = cx->realm()->baselineCompileQueue(); |
| 273 |
MOZ_ASSERT(queue.numQueued() > 0); |
| 274 |
|
| 275 |
auto alloc = cx->make_unique<LifoAlloc>(TempAllocator::PreferredLifoChunkSize, |
| 276 |
js::BackgroundMallocArena); |
| 277 |
if (!alloc) { |
| 278 |
ReportOutOfMemory(cx); |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
gc::AutoSuppressGC suppressGC(cx); |
| 283 |
|
| 284 |
BaselineSnapshotList snapshots; |
| 285 |
auto clearSnapshotList = mozilla::MakeScopeExit([&]() { snapshots.clear(); }); |
| 286 |
|
| 287 |
GlobalLexicalEnvironmentObject* globalLexical = |
| 288 |
&cx->global()->lexicalEnvironment(); |
| 289 |
JSObject* globalThis = globalLexical->thisObject(); |
| 290 |
|
| 291 |
Rooted<JSScript*> script(cx); |
| 292 |
while (!queue.isEmpty()) { |
| 293 |
script = queue.pop(); |
| 294 |
script->jitScript()->clearIsBaselineQueued(script); |
| 295 |
|
| 296 |
MOZ_ASSERT(cx->realm() == script->realm()); |
| 297 |
|
| 298 |
if (!OffThreadBaselineCompilationAvailable(cx, script)) { |
| 299 |
BaselineOptions options({BaselineOption::ForceMainThreadCompilation}); |
| 300 |
MethodStatus status = BaselineCompile(cx, script, options); |
| 301 |
if (status != Method_Compiled) { |
| 302 |
return false; |
| 303 |
} |
| 304 |
continue; |
| 305 |
} |
| 306 |
|
| 307 |
bool compileDebugInstrumentation = false; |
| 308 |
if (!BaselineCompiler::PrepareToCompile(cx, script, |
| 309 |
compileDebugInstrumentation)) { |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
uint32_t baseWarmUpThreshold = |
| 314 |
OptimizationInfo::baseWarmUpThresholdForScript(cx, script); |
| 315 |
bool isIonCompileable = IsIonEnabled(cx) && CanIonCompileScript(cx, script); |
| 316 |
|
| 317 |
MOZ_ASSERT(!script->isDebuggee()); |
| 318 |
|
| 319 |
auto* offThreadSnapshot = alloc->new_<OffThreadBaselineSnapshot>( |
| 320 |
script, globalLexical, globalThis, baseWarmUpThreshold, |
| 321 |
isIonCompileable, compileDebugInstrumentation); |
| 322 |
if (!offThreadSnapshot) { |
| 323 |
ReportOutOfMemory(cx); |
| 324 |
return false; |
| 325 |
} |
| 326 |
snapshots.insertFront(offThreadSnapshot); |
| 327 |
} |
| 328 |
|
| 329 |
if (snapshots.isEmpty()) { |
| 330 |
return true; |
| 331 |
} |
| 332 |
|
| 333 |
CompileRealm* realm = CompileRealm::get(cx->realm()); |
| 334 |
BaselineCompileTask* task = alloc->new_<BaselineCompileTask>( |
| 335 |
realm, alloc.get(), std::move(snapshots)); |
| 336 |
if (!task) { |
| 337 |
ReportOutOfMemory(cx); |
| 338 |
return false; |
| 339 |
} |
| 340 |
task->markScriptsAsCompiling(); |
| 341 |
clearSnapshotList.release(); |
| 342 |
|
| 343 |
AutoLockHelperThreadState lock; |
| 344 |
if (!StartOffThreadBaselineCompile(task, lock)) { |
| 345 |
ReportOutOfMemory(cx); |
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
(void)alloc.release(); |
| 350 |
|
| 351 |
return true; |
| 352 |
} |
| 353 |
|
| 354 |
MethodStatus jit::BaselineCompile(JSContext* cx, JSScript* script, |
| 355 |
BaselineOptions options) { |
| 356 |
cx->check(script); |
| 357 |
MOZ_ASSERT(!script->hasBaselineScript()); |
| 358 |
MOZ_ASSERT(script->canBaselineCompile()); |
| 359 |
MOZ_ASSERT(IsBaselineJitEnabled(cx)); |
| 360 |
AutoGeckoProfilerEntry pseudoFrame( |
| 361 |
cx, "Baseline script compilation", |
| 362 |
JS::ProfilingCategoryPair::JS_BaselineCompilation); |
| 363 |
|
| 364 |
bool compileDebugInstrumentation = |
| 365 |
script->isDebuggee() || |
| 366 |
options.hasFlag(BaselineOption::ForceDebugInstrumentation); |
| 367 |
bool forceMainThread = |
| 368 |
compileDebugInstrumentation || |
| 369 |
options.hasFlag(BaselineOption::ForceMainThreadCompilation); |
| 370 |
|
| 371 |
JitContext jctx(cx); |
| 372 |
|
| 373 |
// Suppress GC during compilation. |
| 374 |
gc::AutoSuppressGC suppressGC(cx); |
| 375 |
|
| 376 |
Rooted<JSScript*> rooted(cx, script); |
| 377 |
if (!BaselineCompiler::PrepareToCompile(cx, rooted, |
| 378 |
compileDebugInstrumentation)) { |
| 379 |
return Method_Error; |
| 380 |
} |
| 381 |
|
| 382 |
GlobalLexicalEnvironmentObject* globalLexical = |
| 383 |
&cx->global()->lexicalEnvironment(); |
| 384 |
JSObject* globalThis = globalLexical->thisObject(); |
| 385 |
uint32_t baseWarmUpThreshold = |
| 386 |
OptimizationInfo::baseWarmUpThresholdForScript(cx, script); |
| 387 |
bool isIonCompileable = IsIonEnabled(cx) && CanIonCompileScript(cx, script); |
| 388 |
|
| 389 |
BaselineSnapshot snapshot(script, globalLexical, globalThis, |
| 390 |
baseWarmUpThreshold, isIonCompileable, |
| 391 |
compileDebugInstrumentation); |
| 392 |
|
| 393 |
if (OffThreadBaselineCompilationAvailable(cx, script) && !forceMainThread) { |
| 394 |
if (!DispatchOffThreadBaselineCompile(cx, snapshot)) { |
| 395 |
ReportOutOfMemory(cx); |
| 396 |
return Method_Error; |
| 397 |
} |
| 398 |
return Method_Skipped; |
| 399 |
} |
| 400 |
|
| 401 |
TempAllocator temp(&cx->tempLifoAlloc()); |
| 402 |
|
| 403 |
StackMacroAssembler masm(cx, temp); |
| 404 |
|
| 405 |
BaselineCompiler compiler(temp, CompileRuntime::get(cx->runtime()), masm, |
| 406 |
&snapshot); |
| 407 |
if (!compiler.init()) { |
| 408 |
ReportOutOfMemory(cx); |
| 409 |
return Method_Error; |
| 410 |
} |
| 411 |
|
| 412 |
MethodStatus status = compiler.compile(cx); |
| 413 |
|
| 414 |
MOZ_ASSERT_IF(status == Method_Compiled, script->hasBaselineScript()); |
| 415 |
MOZ_ASSERT_IF(status != Method_Compiled, !script->hasBaselineScript()); |
| 416 |
|
| 417 |
if (status == Method_CantCompile) { |
| 418 |
script->disableBaselineCompile(); |
| 419 |
} |
| 420 |
|
| 421 |
return status; |
| 422 |
} |
| 423 |
|
| 424 |
static MethodStatus CanEnterBaselineJIT(JSContext* cx, HandleScript script, |
| 425 |
AbstractFramePtr osrSourceFrame) { |
| 426 |
// Skip if the script has been disabled. |
| 427 |
if (!script->canBaselineCompile()) { |
| 428 |
return Method_Skipped; |
| 429 |
} |
| 430 |
|
| 431 |
if (!IsBaselineJitEnabled(cx)) { |
| 432 |
script->disableBaselineCompile(); |
| 433 |
return Method_CantCompile; |
| 434 |
} |
| 435 |
|
| 436 |
// This check is needed in the following corner case. Consider a function h, |
| 437 |
// |
| 438 |
// function h(x) { |
| 439 |
// if (!x) |
| 440 |
// return; |
| 441 |
// h(false); |
| 442 |
// for (var i = 0; i < N; i++) |
| 443 |
// /* do stuff */ |
| 444 |
// } |
| 445 |
// |
| 446 |
// Suppose h is not yet compiled in baseline and is executing in the |
| 447 |
// interpreter. Let this interpreter frame be f_older. The debugger marks |
| 448 |
// f_older as isDebuggee. At the point of the recursive call h(false), h is |
| 449 |
// compiled in baseline without debug instrumentation, pushing a baseline |
| 450 |
// frame f_newer. The debugger never flags f_newer as isDebuggee, and never |
| 451 |
// recompiles h. When the recursive call returns and execution proceeds to |
| 452 |
// the loop, the interpreter attempts to OSR into baseline. Since h is |
| 453 |
// already compiled in baseline, execution jumps directly into baseline |
| 454 |
// code. This is incorrect as h's baseline script does not have debug |
| 455 |
// instrumentation. |
| 456 |
if (osrSourceFrame && osrSourceFrame.isDebuggee() && |
| 457 |
!DebugAPI::ensureExecutionObservabilityOfOsrFrame(cx, osrSourceFrame)) { |
| 458 |
return Method_Error; |
| 459 |
} |
| 460 |
|
| 461 |
if (script->length() > BaselineMaxScriptLength) { |
| 462 |
script->disableBaselineCompile(); |
| 463 |
return Method_CantCompile; |
| 464 |
} |
| 465 |
|
| 466 |
if (script->nslots() > BaselineMaxScriptSlots) { |
| 467 |
script->disableBaselineCompile(); |
| 468 |
return Method_CantCompile; |
| 469 |
} |
| 470 |
|
| 471 |
if (script->hasBaselineScript()) { |
| 472 |
return Method_Compiled; |
| 473 |
} |
| 474 |
|
| 475 |
if (script->isBaselineCompilingOffThread()) { |
| 476 |
return Method_Skipped; |
| 477 |
} |
| 478 |
|
| 479 |
// If a hint is available, skip the warmup count threshold. |
| 480 |
bool mightHaveEagerBaselineHint = false; |
| 481 |
if (!JitOptions.disableJitHints && !script->noEagerBaselineHint() && |
| 482 |
cx->runtime()->jitRuntime()->hasJitHintsMap()) { |
| 483 |
JitHintsMap* jitHints = cx->runtime()->jitRuntime()->getJitHintsMap(); |
| 484 |
// If this lookup fails, the NoEagerBaselineHint script flag is set |
| 485 |
// to true to prevent any further lookups for this script. |
| 486 |
if (jitHints->mightHaveEagerBaselineHint(script)) { |
| 487 |
mightHaveEagerBaselineHint = true; |
| 488 |
} |
| 489 |
} |
| 490 |
// Check script warm-up counter if no hint. |
| 491 |
if (!mightHaveEagerBaselineHint) { |
| 492 |
if (script->getWarmUpCount() <= JitOptions.baselineJitWarmUpThreshold) { |
| 493 |
return Method_Skipped; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
// Check this before calling ensureJitZoneExists, so we're less |
| 498 |
// likely to report OOM in JSRuntime::createJitRuntime. |
| 499 |
if (!CanLikelyAllocateMoreExecutableMemory()) { |
| 500 |
return Method_Skipped; |
| 501 |
} |
| 502 |
|
| 503 |
if (!cx->zone()->ensureJitZoneExists(cx)) { |
| 504 |
return Method_Error; |
| 505 |
} |
| 506 |
|
| 507 |
if (script->hasForceInterpreterOp()) { |
| 508 |
script->disableBaselineCompile(); |
| 509 |
return Method_CantCompile; |
| 510 |
} |
| 511 |
|
| 512 |
// Frames can be marked as debuggee frames independently of its underlying |
| 513 |
// script being a debuggee script, e.g., when performing |
| 514 |
// Debugger.Frame.prototype.eval. |
| 515 |
BaselineOptions options; |
| 516 |
if (osrSourceFrame && osrSourceFrame.isDebuggee()) { |
| 517 |
options.setFlag(BaselineOption::ForceDebugInstrumentation); |
| 518 |
} |
| 519 |
return BaselineCompile(cx, script, options); |
| 520 |
} |
| 521 |
|
| 522 |
bool jit::CanBaselineInterpretScript(JSScript* script) { |
| 523 |
MOZ_ASSERT(IsBaselineInterpreterEnabled()); |
| 524 |
|
| 525 |
if (script->hasForceInterpreterOp()) { |
| 526 |
return false; |
| 527 |
} |
| 528 |
|
| 529 |
if (script->nslots() > BaselineMaxScriptSlots) { |
| 530 |
// Avoid overrecursion exceptions when the script has a ton of stack slots |
| 531 |
// by forcing such scripts to run in the C++ interpreter with heap-allocated |
| 532 |
// stack frames. |
| 533 |
return false; |
| 534 |
} |
| 535 |
|
| 536 |
return true; |
| 537 |
} |
| 538 |
|
| 539 |
static bool MaybeCreateBaselineInterpreterEntryScript(JSContext* cx, |
| 540 |
JSScript* script) { |
| 541 |
MOZ_ASSERT(script->hasJitScript()); |
| 542 |
|
| 543 |
JitRuntime* jitRuntime = cx->runtime()->jitRuntime(); |
| 544 |
if (script->jitCodeRaw() != jitRuntime->baselineInterpreter().codeRaw()) { |
| 545 |
// script already has an updated interpreter trampoline. |
| 546 |
#ifdef DEBUG |
| 547 |
auto p = jitRuntime->getInterpreterEntryMap()->lookup(script); |
| 548 |
MOZ_ASSERT(p); |
| 549 |
MOZ_ASSERT(p->value().raw() == script->jitCodeRaw()); |
| 550 |
#endif |
| 551 |
return true; |
| 552 |
} |
| 553 |
|
| 554 |
auto p = jitRuntime->getInterpreterEntryMap()->lookupForAdd(script); |
| 555 |
if (!p) { |
| 556 |
Rooted<JitCode*> code( |
| 557 |
cx, jitRuntime->generateEntryTrampolineForScript(cx, script)); |
| 558 |
if (!code) { |
| 559 |
return false; |
| 560 |
} |
| 561 |
|
| 562 |
EntryTrampoline entry(cx, code); |
| 563 |
if (!jitRuntime->getInterpreterEntryMap()->add(p, script, entry)) { |
| 564 |
return false; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
script->updateJitCodeRaw(cx->runtime()); |
| 569 |
return true; |
| 570 |
} |
| 571 |
|
| 572 |
static MethodStatus CanEnterBaselineInterpreter(JSContext* cx, |
| 573 |
JSScript* script) { |
| 574 |
MOZ_ASSERT(IsBaselineInterpreterEnabled()); |
| 575 |
|
| 576 |
if (script->hasJitScript()) { |
| 577 |
return Method_Compiled; |
| 578 |
} |
| 579 |
|
| 580 |
if (!CanBaselineInterpretScript(script)) { |
| 581 |
return Method_CantCompile; |
| 582 |
} |
| 583 |
|
| 584 |
// Check script warm-up counter. |
| 585 |
if (script->getWarmUpCount() <= |
| 586 |
JitOptions.baselineInterpreterWarmUpThreshold) { |
| 587 |
return Method_Skipped; |
| 588 |
} |
| 589 |
|
| 590 |
if (!cx->zone()->ensureJitZoneExists(cx)) { |
| 591 |
return Method_Error; |
| 592 |
} |
| 593 |
|
| 594 |
AutoKeepJitScripts keepJitScript(cx); |
| 595 |
if (!script->ensureHasJitScript(cx, keepJitScript)) { |
| 596 |
return Method_Error; |
| 597 |
} |
| 598 |
|
| 599 |
if (JitOptions.emitInterpreterEntryTrampoline) { |
| 600 |
if (!MaybeCreateBaselineInterpreterEntryScript(cx, script)) { |
| 601 |
return Method_Error; |
| 602 |
} |
| 603 |
} |
| 604 |
return Method_Compiled; |
| 605 |
} |
| 606 |
|
| 607 |
MethodStatus jit::CanEnterBaselineInterpreterAtBranch(JSContext* cx, |
| 608 |
InterpreterFrame* fp) { |
| 609 |
if (!CheckFrame(fp)) { |
| 610 |
return Method_CantCompile; |
| 611 |
} |
| 612 |
|
| 613 |
// JITs do not respect the debugger's OnNativeCall hook, so JIT execution is |
| 614 |
// disabled if this hook might need to be called. |
| 615 |
if (cx->realm()->debuggerObservesNativeCall()) { |
| 616 |
return Method_CantCompile; |
| 617 |
} |
| 618 |
|
| 619 |
return CanEnterBaselineInterpreter(cx, fp->script()); |
| 620 |
} |
| 621 |
|
| 622 |
template <BaselineTier Tier> |
| 623 |
MethodStatus jit::CanEnterBaselineMethod(JSContext* cx, RunState& state) { |
| 624 |
if (state.isInvoke()) { |
| 625 |
InvokeState& invoke = *state.asInvoke(); |
| 626 |
if (TooManyActualArguments(invoke.args().length())) { |
| 627 |
JitSpew(JitSpew_BaselineAbort, "Too many arguments (%u)", |
| 628 |
invoke.args().length()); |
| 629 |
return Method_CantCompile; |
| 630 |
} |
| 631 |
} else { |
| 632 |
if (state.asExecute()->isDebuggerEval()) { |
| 633 |
JitSpew(JitSpew_BaselineAbort, "debugger frame"); |
| 634 |
return Method_CantCompile; |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
RootedScript script(cx, state.script()); |
| 639 |
switch (Tier) { |
| 640 |
case BaselineTier::Interpreter: |
| 641 |
return CanEnterBaselineInterpreter(cx, script); |
| 642 |
|
| 643 |
case BaselineTier::Compiler: |
| 644 |
return CanEnterBaselineJIT(cx, script, |
| 645 |
/* osrSourceFrame = */ NullFramePtr()); |
| 646 |
} |
| 647 |
|
| 648 |
MOZ_CRASH("Unexpected tier"); |
| 649 |
} |
| 650 |
|
| 651 |
template MethodStatus jit::CanEnterBaselineMethod<BaselineTier::Interpreter>( |
| 652 |
JSContext* cx, RunState& state); |
| 653 |
template MethodStatus jit::CanEnterBaselineMethod<BaselineTier::Compiler>( |
| 654 |
JSContext* cx, RunState& state); |
| 655 |
|
| 656 |
bool jit::BaselineCompileFromBaselineInterpreter(JSContext* cx, |
| 657 |
BaselineFrame* frame, |
| 658 |
uint8_t** res) { |
| 659 |
MOZ_ASSERT(frame->runningInInterpreter()); |
| 660 |
|
| 661 |
RootedScript script(cx, frame->script()); |
| 662 |
jsbytecode* pc = frame->interpreterPC(); |
| 663 |
MOZ_ASSERT(pc == script->code() || JSOp(*pc) == JSOp::LoopHead); |
| 664 |
|
| 665 |
MethodStatus status = CanEnterBaselineJIT(cx, script, |
| 666 |
/* osrSourceFrame = */ frame); |
| 667 |
switch (status) { |
| 668 |
case Method_Error: |
| 669 |
return false; |
| 670 |
|
| 671 |
case Method_CantCompile: |
| 672 |
case Method_Skipped: |
| 673 |
*res = nullptr; |
| 674 |
return true; |
| 675 |
|
| 676 |
case Method_Compiled: { |
| 677 |
if (JSOp(*pc) == JSOp::LoopHead) { |
| 678 |
MOZ_ASSERT(pc > script->code(), |
| 679 |
"Prologue vs OSR cases must not be ambiguous"); |
| 680 |
BaselineScript* baselineScript = script->baselineScript(); |
| 681 |
uint32_t pcOffset = script->pcToOffset(pc); |
| 682 |
*res = baselineScript->nativeCodeForOSREntry(pcOffset); |
| 683 |
} else { |
| 684 |
*res = script->baselineScript()->warmUpCheckPrologueAddr(); |
| 685 |
} |
| 686 |
frame->prepareForBaselineInterpreterToJitOSR(); |
| 687 |
return true; |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
MOZ_CRASH("Unexpected status"); |
| 692 |
} |
| 693 |
|
| 694 |
void BaselineCompileQueue::trace(JSTracer* trc) { |
| 695 |
assertInvariants(); |
| 696 |
for (uint32_t i = 0; i < numQueued_; i++) { |
| 697 |
TraceEdge(trc, &queue_[i], "baseline_compile_queue"); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
void BaselineCompileQueue::remove(JSScript* script) { |
| 702 |
assertInvariants(); |
| 703 |
for (uint32_t i = 0; i < numQueued_; i++) { |
| 704 |
if (queue_[i] == script) { |
| 705 |
std::swap(queue_[i], queue_[numQueued_ - 1]); |
| 706 |
pop(); |
| 707 |
break; |
| 708 |
} |
| 709 |
} |
| 710 |
assertInvariants(); |
| 711 |
} |
| 712 |
|
| 713 |
BaselineScript* BaselineScript::New(JSContext* cx, |
| 714 |
uint32_t warmUpCheckPrologueOffset, |
| 715 |
uint32_t profilerEnterToggleOffset, |
| 716 |
uint32_t profilerExitToggleOffset, |
| 717 |
size_t retAddrEntries, size_t osrEntries, |
| 718 |
size_t debugTrapEntries, |
| 719 |
size_t resumeEntries) { |
| 720 |
// Compute size including trailing arrays. |
| 721 |
CheckedInt<Offset> size = sizeof(BaselineScript); |
| 722 |
size += CheckedInt<Offset>(resumeEntries) * sizeof(uintptr_t); |
| 723 |
size += CheckedInt<Offset>(retAddrEntries) * sizeof(RetAddrEntry); |
| 724 |
size += CheckedInt<Offset>(osrEntries) * sizeof(OSREntry); |
| 725 |
size += CheckedInt<Offset>(debugTrapEntries) * sizeof(DebugTrapEntry); |
| 726 |
|
| 727 |
if (!size.isValid()) { |
| 728 |
ReportAllocationOverflow(cx); |
| 729 |
return nullptr; |
| 730 |
} |
| 731 |
|
| 732 |
// Allocate contiguous raw buffer. |
| 733 |
void* raw = cx->pod_malloc<uint8_t>(size.value()); |
| 734 |
MOZ_ASSERT(uintptr_t(raw) % alignof(BaselineScript) == 0); |
| 735 |
if (!raw) { |
| 736 |
return nullptr; |
| 737 |
} |
| 738 |
BaselineScript* script = new (raw) |
| 739 |
BaselineScript(warmUpCheckPrologueOffset, profilerEnterToggleOffset, |
| 740 |
profilerExitToggleOffset); |
| 741 |
|
| 742 |
Offset cursor = sizeof(BaselineScript); |
| 743 |
|
| 744 |
MOZ_ASSERT(isAlignedOffset<uintptr_t>(cursor)); |
| 745 |
script->resumeEntriesOffset_ = cursor; |
| 746 |
cursor += resumeEntries * sizeof(uintptr_t); |
| 747 |
|
| 748 |
MOZ_ASSERT(isAlignedOffset<RetAddrEntry>(cursor)); |
| 749 |
script->retAddrEntriesOffset_ = cursor; |
| 750 |
cursor += retAddrEntries * sizeof(RetAddrEntry); |
| 751 |
|
| 752 |
MOZ_ASSERT(isAlignedOffset<OSREntry>(cursor)); |
| 753 |
script->osrEntriesOffset_ = cursor; |
| 754 |
cursor += osrEntries * sizeof(OSREntry); |
| 755 |
|
| 756 |
MOZ_ASSERT(isAlignedOffset<DebugTrapEntry>(cursor)); |
| 757 |
script->debugTrapEntriesOffset_ = cursor; |
| 758 |
cursor += debugTrapEntries * sizeof(DebugTrapEntry); |
| 759 |
|
| 760 |
MOZ_ASSERT(isAlignedOffset<uint32_t>(cursor)); |
| 761 |
|
| 762 |
script->allocBytes_ = cursor; |
| 763 |
|
| 764 |
MOZ_ASSERT(script->endOffset() == size.value()); |
| 765 |
|
| 766 |
return script; |
| 767 |
} |
| 768 |
|
| 769 |
BaselineScript* BaselineScript::Copy(JSContext* cx, BaselineScript* bs) { |
| 770 |
BaselineScript* script = jit::BaselineScript::New( |
| 771 |
cx, bs->warmUpCheckPrologueOffset_, bs->profilerEnterToggleOffset_, |
| 772 |
bs->profilerExitToggleOffset_, bs->retAddrEntries().size(), |
| 773 |
bs->osrEntries().size(), bs->debugTrapEntries().size(), |
| 774 |
bs->resumeEntryList().size()); |
| 775 |
if (!script) { |
| 776 |
return nullptr; |
| 777 |
} |
| 778 |
|
| 779 |
script->setMethod(bs->method()); |
| 780 |
script->copyRetAddrEntries(bs->retAddrEntries().data()); |
| 781 |
script->copyOSREntries(bs->osrEntries().data()); |
| 782 |
script->copyDebugTrapEntries(bs->debugTrapEntries().data()); |
| 783 |
|
| 784 |
script->flags_ = bs->flags_; |
| 785 |
|
| 786 |
// copyResumeNativeOffsets() |
| 787 |
std::copy_n(bs->resumeEntryList().begin(), script->resumeEntryList().size(), |
| 788 |
script->resumeEntryList().data()); |
| 789 |
|
| 790 |
if (bs->hasDebugInstrumentation()) { |
| 791 |
script->setHasDebugInstrumentation(); |
| 792 |
} |
| 793 |
MOZ_ASSERT(script->method_ == bs->method_); |
| 794 |
MOZ_ASSERT(script->pendingIonCompileTask_ == bs->pendingIonCompileTask_); |
| 795 |
MOZ_ASSERT(script->warmUpCheckPrologueOffset_ == |
| 796 |
bs->warmUpCheckPrologueOffset_); |
| 797 |
MOZ_ASSERT(script->profilerEnterToggleOffset_ == |
| 798 |
bs->profilerEnterToggleOffset_); |
| 799 |
MOZ_ASSERT(script->profilerExitToggleOffset_ == |
| 800 |
bs->profilerExitToggleOffset_); |
| 801 |
MOZ_ASSERT(script->resumeEntriesOffset_ == bs->resumeEntriesOffset_); |
| 802 |
MOZ_ASSERT(script->retAddrEntriesOffset_ == bs->retAddrEntriesOffset_); |
| 803 |
MOZ_ASSERT(script->osrEntriesOffset_ == bs->osrEntriesOffset_); |
| 804 |
MOZ_ASSERT(script->debugTrapEntriesOffset_ == bs->debugTrapEntriesOffset_); |
| 805 |
MOZ_ASSERT(script->allocBytes_ == bs->allocBytes_); |
| 806 |
MOZ_ASSERT(script->flags_ == bs->flags_); |
| 807 |
return script; |
| 808 |
} |
| 809 |
|
| 810 |
void BaselineScript::trace(JSTracer* trc) { |
| 811 |
TraceEdge(trc, &method_, "baseline-method"); |
| 812 |
} |
| 813 |
|
| 814 |
void BaselineScript::Destroy(JS::GCContext* gcx, BaselineScript* script) { |
| 815 |
MOZ_ASSERT(!script->hasPendingIonCompileTask()); |
| 816 |
|
| 817 |
// This allocation is tracked by JSScript::setBaselineScriptImpl. |
| 818 |
gcx->deleteUntracked(script); |
| 819 |
} |
| 820 |
|
| 821 |
void JS::DeletePolicy<js::jit::BaselineScript>::operator()( |
| 822 |
const js::jit::BaselineScript* script) { |
| 823 |
BaselineScript::Destroy(rt_->gcContext(), |
| 824 |
const_cast<BaselineScript*>(script)); |
| 825 |
} |
| 826 |
|
| 827 |
const RetAddrEntry& BaselineScript::retAddrEntryFromReturnOffset( |
| 828 |
CodeOffset returnOffset) { |
| 829 |
mozilla::Span<RetAddrEntry> entries = retAddrEntries(); |
| 830 |
size_t loc; |
| 831 |
#ifdef DEBUG |
| 832 |
bool found = |
| 833 |
#endif |
| 834 |
BinarySearchIf( |
| 835 |
entries.data(), 0, entries.size(), |
| 836 |
[&returnOffset](const RetAddrEntry& entry) { |
| 837 |
size_t roffset = returnOffset.offset(); |
| 838 |
size_t entryRoffset = entry.returnOffset().offset(); |
| 839 |
if (roffset < entryRoffset) { |
| 840 |
return -1; |
| 841 |
} |
| 842 |
if (entryRoffset < roffset) { |
| 843 |
return 1; |
| 844 |
} |
| 845 |
return 0; |
| 846 |
}, |
| 847 |
&loc); |
| 848 |
|
| 849 |
MOZ_ASSERT(found); |
| 850 |
MOZ_ASSERT(entries[loc].returnOffset().offset() == returnOffset.offset()); |
| 851 |
return entries[loc]; |
| 852 |
} |
| 853 |
|
| 854 |
template <typename Entry> |
| 855 |
static bool ComputeBinarySearchMid(mozilla::Span<Entry> entries, |
| 856 |
uint32_t pcOffset, size_t* loc) { |
| 857 |
return BinarySearchIf( |
| 858 |
entries.data(), 0, entries.size(), |
| 859 |
[pcOffset](const Entry& entry) { |
| 860 |
uint32_t entryOffset = entry.pcOffset(); |
| 861 |
if (pcOffset < entryOffset) { |
| 862 |
return -1; |
| 863 |
} |
| 864 |
if (entryOffset < pcOffset) { |
| 865 |
return 1; |
| 866 |
} |
| 867 |
return 0; |
| 868 |
}, |
| 869 |
loc); |
| 870 |
} |
| 871 |
|
| 872 |
uint8_t* BaselineScript::returnAddressForEntry(const RetAddrEntry& ent) { |
| 873 |
return method()->raw() + ent.returnOffset().offset(); |
| 874 |
} |
| 875 |
|
| 876 |
const RetAddrEntry& BaselineScript::retAddrEntryFromPCOffset( |
| 877 |
uint32_t pcOffset, RetAddrEntry::Kind kind) { |
| 878 |
mozilla::Span<RetAddrEntry> entries = retAddrEntries(); |
| 879 |
size_t mid; |
| 880 |
MOZ_ALWAYS_TRUE(ComputeBinarySearchMid(entries, pcOffset, &mid)); |
| 881 |
MOZ_ASSERT(mid < entries.size()); |
| 882 |
|
| 883 |
// Search for the first entry for this pc. |
| 884 |
size_t first = mid; |
| 885 |
while (first > 0 && entries[first - 1].pcOffset() == pcOffset) { |
| 886 |
first--; |
| 887 |
} |
| 888 |
|
| 889 |
// Search for the last entry for this pc. |
| 890 |
size_t last = mid; |
| 891 |
while (last + 1 < entries.size() && |
| 892 |
entries[last + 1].pcOffset() == pcOffset) { |
| 893 |
last++; |
| 894 |
} |
| 895 |
|
| 896 |
MOZ_ASSERT(first <= last); |
| 897 |
MOZ_ASSERT(entries[first].pcOffset() == pcOffset); |
| 898 |
MOZ_ASSERT(entries[last].pcOffset() == pcOffset); |
| 899 |
|
| 900 |
for (size_t i = first; i <= last; i++) { |
| 901 |
const RetAddrEntry& entry = entries[i]; |
| 902 |
if (entry.kind() != kind) { |
| 903 |
continue; |
| 904 |
} |
| 905 |
|
| 906 |
#ifdef DEBUG |
| 907 |
// There must be a unique entry for this pcOffset and Kind to ensure our |
| 908 |
// return value is well-defined. |
| 909 |
for (size_t j = i + 1; j <= last; j++) { |
| 910 |
MOZ_ASSERT(entries[j].kind() != kind); |
| 911 |
} |
| 912 |
#endif |
| 913 |
|
| 914 |
return entry; |
| 915 |
} |
| 916 |
|
| 917 |
MOZ_CRASH("Didn't find RetAddrEntry."); |
| 918 |
} |
| 919 |
|
| 920 |
const RetAddrEntry& BaselineScript::prologueRetAddrEntry( |
| 921 |
RetAddrEntry::Kind kind) { |
| 922 |
MOZ_ASSERT(kind == RetAddrEntry::Kind::StackCheck); |
| 923 |
|
| 924 |
// The prologue entries will always be at a very low offset, so just do a |
| 925 |
// linear search from the beginning. |
| 926 |
for (const RetAddrEntry& entry : retAddrEntries()) { |
| 927 |
if (entry.pcOffset() != 0) { |
| 928 |
break; |
| 929 |
} |
| 930 |
if (entry.kind() == kind) { |
| 931 |
return entry; |
| 932 |
} |
| 933 |
} |
| 934 |
MOZ_CRASH("Didn't find prologue RetAddrEntry."); |
| 935 |
} |
| 936 |
|
| 937 |
const RetAddrEntry& BaselineScript::retAddrEntryFromReturnAddress( |
| 938 |
const uint8_t* returnAddr) { |
| 939 |
MOZ_ASSERT(returnAddr > method_->raw()); |
| 940 |
MOZ_ASSERT(returnAddr < method_->raw() + method_->instructionsSize()); |
| 941 |
CodeOffset offset(returnAddr - method_->raw()); |
| 942 |
return retAddrEntryFromReturnOffset(offset); |
| 943 |
} |
| 944 |
|
| 945 |
uint8_t* BaselineScript::nativeCodeForOSREntry(uint32_t pcOffset) { |
| 946 |
mozilla::Span<OSREntry> entries = osrEntries(); |
| 947 |
size_t mid; |
| 948 |
if (!ComputeBinarySearchMid(entries, pcOffset, &mid)) { |
| 949 |
return nullptr; |
| 950 |
} |
| 951 |
|
| 952 |
uint32_t nativeOffset = entries[mid].nativeOffset(); |
| 953 |
return method_->raw() + nativeOffset; |
| 954 |
} |
| 955 |
|
| 956 |
void BaselineScript::computeResumeNativeOffsets( |
| 957 |
JSScript* script, const ResumeOffsetEntryVector& entries) { |
| 958 |
// Translate pcOffset to BaselineScript native address. This may return |
| 959 |
// nullptr if compiler decided code was unreachable. |
| 960 |
auto computeNative = [this, &entries](uint32_t pcOffset) -> uint8_t* { |
| 961 |
mozilla::Span<const ResumeOffsetEntry> entriesSpan = |
| 962 |
mozilla::Span(entries.begin(), entries.length()); |
| 963 |
size_t mid; |
| 964 |
if (!ComputeBinarySearchMid(entriesSpan, pcOffset, &mid)) { |
| 965 |
return nullptr; |
| 966 |
} |
| 967 |
|
| 968 |
uint32_t nativeOffset = entries[mid].nativeOffset(); |
| 969 |
return method_->raw() + nativeOffset; |
| 970 |
}; |
| 971 |
|
| 972 |
mozilla::Span<const uint32_t> pcOffsets = script->resumeOffsets(); |
| 973 |
mozilla::Span<uint8_t*> nativeOffsets = resumeEntryList(); |
| 974 |
std::transform(pcOffsets.begin(), pcOffsets.end(), nativeOffsets.begin(), |
| 975 |
computeNative); |
| 976 |
} |
| 977 |
|
| 978 |
uint8_t* BaselineScript::OSREntryForFrame(BaselineFrame* frame) { |
| 979 |
AutoUnsafeCallWithABI unsafe; |
| 980 |
MOZ_ASSERT(frame->runningInInterpreter()); |
| 981 |
|
| 982 |
uint8_t* entry = nullptr; |
| 983 |
JSScript* script = frame->script(); |
| 984 |
BaselineScript* baselineScript = script->baselineScript(); |
| 985 |
jsbytecode* pc = frame->interpreterPC(); |
| 986 |
size_t pcOffset = script->pcToOffset(pc); |
| 987 |
|
| 988 |
if (MOZ_UNLIKELY(frame->isDebuggee() && |
| 989 |
!baselineScript->hasDebugInstrumentation())) { |
| 990 |
// This check is needed in the following corner case. Consider a function h, |
| 991 |
// |
| 992 |
// function h(x) { |
| 993 |
// if (!x) |
| 994 |
// return; |
| 995 |
// h(false); |
| 996 |
// for (var i = 0; i < N; i++) |
| 997 |
// /* do stuff */ |
| 998 |
// } |
| 999 |
// |
| 1000 |
// Suppose h is not yet compiled in baseline and is executing in the |
| 1001 |
// baseline interpreter. Let this interpreter frame be f_older. The debugger |
| 1002 |
// marks f_older as isDebuggee. At the point of the recursive call h(false), |
| 1003 |
// h is compiled in baseline without debug instrumentation, pushing a |
| 1004 |
// baseline frame f_newer. The debugger never flags f_newer as isDebuggee, |
| 1005 |
// and never recompiles h. When the recursive call returns and execution |
| 1006 |
// proceeds to the loop, the baseline interpreter attempts to OSR into |
| 1007 |
// baseline. Since h is already compiled in baseline, execution jumps |
| 1008 |
// directly into baseline code. This is incorrect as h's baseline script |
| 1009 |
// does not have debug instrumentation. |
| 1010 |
JSContext* cx = TlsContext.get(); |
| 1011 |
if (!RecompileBaselineScriptForDebugMode(cx, script, DebugAPI::Observing)) { |
| 1012 |
cx->recoverFromOutOfMemory(); |
| 1013 |
return nullptr; |
| 1014 |
} |
| 1015 |
baselineScript = script->baselineScript(); |
| 1016 |
} |
| 1017 |
|
| 1018 |
if (JSOp(*pc) == JSOp::LoopHead) { |
| 1019 |
MOZ_ASSERT(pc > script->code(), |
| 1020 |
"Prologue vs OSR cases must not be ambiguous"); |
| 1021 |
entry = baselineScript->nativeCodeForOSREntry(pcOffset); |
| 1022 |
} else { |
| 1023 |
entry = baselineScript->warmUpCheckPrologueAddr(); |
| 1024 |
} |
| 1025 |
|
| 1026 |
frame->prepareForBaselineInterpreterToJitOSR(); |
| 1027 |
return entry; |
| 1028 |
} |
| 1029 |
|
| 1030 |
void BaselineScript::copyRetAddrEntries(const RetAddrEntry* entries) { |
| 1031 |
std::copy_n(entries, retAddrEntries().size(), retAddrEntries().data()); |
| 1032 |
} |
| 1033 |
|
| 1034 |
void BaselineScript::copyOSREntries(const OSREntry* entries) { |
| 1035 |
std::copy_n(entries, osrEntries().size(), osrEntries().data()); |
| 1036 |
} |
| 1037 |
|
| 1038 |
void BaselineScript::copyDebugTrapEntries(const DebugTrapEntry* entries) { |
| 1039 |
std::copy_n(entries, debugTrapEntries().size(), debugTrapEntries().data()); |
| 1040 |
} |
| 1041 |
|
| 1042 |
jsbytecode* BaselineScript::approximatePcForNativeAddress( |
| 1043 |
JSScript* script, uint8_t* nativeAddress) { |
| 1044 |
MOZ_ASSERT(script->baselineScript() == this); |
| 1045 |
MOZ_ASSERT(containsCodeAddress(nativeAddress)); |
| 1046 |
|
| 1047 |
uint32_t nativeOffset = nativeAddress - method_->raw(); |
| 1048 |
|
| 1049 |
// Use the RetAddrEntry list (sorted on pc and return address) to look for the |
| 1050 |
// first pc that has a return address >= nativeOffset. This isn't perfect but |
| 1051 |
// it's a reasonable approximation for the profiler because most non-trivial |
| 1052 |
// bytecode ops have a RetAddrEntry. |
| 1053 |
|
| 1054 |
for (const RetAddrEntry& entry : retAddrEntries()) { |
| 1055 |
uint32_t retOffset = entry.returnOffset().offset(); |
| 1056 |
if (retOffset >= nativeOffset) { |
| 1057 |
return script->offsetToPC(entry.pcOffset()); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
// Return the last entry's pc. Every BaselineScript has at least one |
| 1062 |
// RetAddrEntry for the prologue stack overflow check. |
| 1063 |
MOZ_ASSERT(!retAddrEntries().empty()); |
| 1064 |
return script->offsetToPC(retAddrEntries().crbegin()->pcOffset()); |
| 1065 |
} |
| 1066 |
|
| 1067 |
void BaselineScript::toggleDebugTraps(JSScript* script, jsbytecode* pc) { |
| 1068 |
MOZ_ASSERT(script->baselineScript() == this); |
| 1069 |
|
| 1070 |
// Only scripts compiled for debug mode have toggled calls. |
| 1071 |
if (!hasDebugInstrumentation()) { |
| 1072 |
return; |
| 1073 |
} |
| 1074 |
|
| 1075 |
AutoWritableJitCode awjc(method()); |
| 1076 |
|
| 1077 |
for (const DebugTrapEntry& entry : debugTrapEntries()) { |
| 1078 |
jsbytecode* entryPC = script->offsetToPC(entry.pcOffset()); |
| 1079 |
|
| 1080 |
// If the |pc| argument is non-null we can skip all other bytecode ops. |
| 1081 |
if (pc && pc != entryPC) { |
| 1082 |
continue; |
| 1083 |
} |
| 1084 |
|
| 1085 |
bool enabled = DebugAPI::stepModeEnabled(script) || |
| 1086 |
DebugAPI::hasBreakpointsAt(script, entryPC); |
| 1087 |
|
| 1088 |
// Patch the trap. |
| 1089 |
CodeLocationLabel label(method(), CodeOffset(entry.nativeOffset())); |
| 1090 |
Assembler::ToggleCall(label, enabled); |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
void BaselineScript::setPendingIonCompileTask(JSRuntime* rt, JSScript* script, |
| 1095 |
IonCompileTask* task) { |
| 1096 |
MOZ_ASSERT(script->baselineScript() == this); |
| 1097 |
MOZ_ASSERT(task); |
| 1098 |
MOZ_ASSERT(!hasPendingIonCompileTask()); |
| 1099 |
|
| 1100 |
if (script->isIonCompilingOffThread()) { |
| 1101 |
script->jitScript()->clearIsIonCompilingOffThread(script); |
| 1102 |
} |
| 1103 |
|
| 1104 |
pendingIonCompileTask_ = task; |
| 1105 |
script->updateJitCodeRaw(rt); |
| 1106 |
} |
| 1107 |
|
| 1108 |
void BaselineScript::removePendingIonCompileTask(JSRuntime* rt, |
| 1109 |
JSScript* script) { |
| 1110 |
MOZ_ASSERT(script->baselineScript() == this); |
| 1111 |
MOZ_ASSERT(hasPendingIonCompileTask()); |
| 1112 |
|
| 1113 |
pendingIonCompileTask_ = nullptr; |
| 1114 |
script->updateJitCodeRaw(rt); |
| 1115 |
} |
| 1116 |
|
| 1117 |
static void ToggleProfilerInstrumentation(JitCode* code, |
| 1118 |
uint32_t profilerEnterToggleOffset, |
| 1119 |
uint32_t profilerExitToggleOffset, |
| 1120 |
bool enable) { |
| 1121 |
CodeLocationLabel enterToggleLocation(code, |
| 1122 |
CodeOffset(profilerEnterToggleOffset)); |
| 1123 |
CodeLocationLabel exitToggleLocation(code, |
| 1124 |
CodeOffset(profilerExitToggleOffset)); |
| 1125 |
if (enable) { |
| 1126 |
Assembler::ToggleToCmp(enterToggleLocation); |
| 1127 |
Assembler::ToggleToCmp(exitToggleLocation); |
| 1128 |
} else { |
| 1129 |
Assembler::ToggleToJmp(enterToggleLocation); |
| 1130 |
Assembler::ToggleToJmp(exitToggleLocation); |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
void BaselineScript::toggleProfilerInstrumentation(bool enable) { |
| 1135 |
if (enable == isProfilerInstrumentationOn()) { |
| 1136 |
return; |
| 1137 |
} |
| 1138 |
|
| 1139 |
JitSpew(JitSpew_BaselineIC, " toggling profiling %s for BaselineScript %p", |
| 1140 |
enable ? "on" : "off", this); |
| 1141 |
|
| 1142 |
ToggleProfilerInstrumentation(method_, profilerEnterToggleOffset_, |
| 1143 |
profilerExitToggleOffset_, enable); |
| 1144 |
|
| 1145 |
if (enable) { |
| 1146 |
flags_ |= uint32_t(PROFILER_INSTRUMENTATION_ON); |
| 1147 |
} else { |
| 1148 |
flags_ &= ~uint32_t(PROFILER_INSTRUMENTATION_ON); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
void BaselineInterpreter::toggleProfilerInstrumentation(bool enable) { |
| 1153 |
if (!IsBaselineInterpreterEnabled()) { |
| 1154 |
return; |
| 1155 |
} |
| 1156 |
|
| 1157 |
AutoWritableJitCode awjc(code_); |
| 1158 |
ToggleProfilerInstrumentation(code_, profilerEnterToggleOffset_, |
| 1159 |
profilerExitToggleOffset_, enable); |
| 1160 |
} |
| 1161 |
|
| 1162 |
void BaselineInterpreter::toggleDebuggerInstrumentation(bool enable) { |
| 1163 |
if (!IsBaselineInterpreterEnabled()) { |
| 1164 |
return; |
| 1165 |
} |
| 1166 |
|
| 1167 |
AutoWritableJitCode awjc(code_); |
| 1168 |
|
| 1169 |
// Toggle jumps for debugger instrumentation. |
| 1170 |
for (uint32_t offset : debugInstrumentationOffsets_) { |
| 1171 |
CodeLocationLabel label(code_, CodeOffset(offset)); |
| 1172 |
if (enable) { |
| 1173 |
Assembler::ToggleToCmp(label); |
| 1174 |
} else { |
| 1175 |
Assembler::ToggleToJmp(label); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
// Toggle DebugTrapHandler calls. |
| 1180 |
|
| 1181 |
uint8_t* debugTrapHandler = codeAtOffset(debugTrapHandlerOffset_); |
| 1182 |
|
| 1183 |
for (uint32_t offset : debugTrapOffsets_) { |
| 1184 |
uint8_t* trap = codeAtOffset(offset); |
| 1185 |
if (enable) { |
| 1186 |
MacroAssembler::patchNopToCall(trap, debugTrapHandler); |
| 1187 |
} else { |
| 1188 |
MacroAssembler::patchCallToNop(trap); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
void BaselineInterpreter::toggleCodeCoverageInstrumentationUnchecked( |
| 1194 |
bool enable) { |
| 1195 |
if (!IsBaselineInterpreterEnabled()) { |
| 1196 |
return; |
| 1197 |
} |
| 1198 |
|
| 1199 |
AutoWritableJitCode awjc(code_); |
| 1200 |
|
| 1201 |
for (uint32_t offset : codeCoverageOffsets_) { |
| 1202 |
CodeLocationLabel label(code_, CodeOffset(offset)); |
| 1203 |
if (enable) { |
| 1204 |
Assembler::ToggleToCmp(label); |
| 1205 |
} else { |
| 1206 |
Assembler::ToggleToJmp(label); |
| 1207 |
} |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
void BaselineInterpreter::toggleCodeCoverageInstrumentation(bool enable) { |
| 1212 |
if (coverage::IsLCovEnabled()) { |
| 1213 |
// Instrumentation is enabled no matter what. |
| 1214 |
return; |
| 1215 |
} |
| 1216 |
|
| 1217 |
toggleCodeCoverageInstrumentationUnchecked(enable); |
| 1218 |
} |
| 1219 |
|
| 1220 |
void jit::FinishDiscardBaselineScript(JS::GCContext* gcx, JSScript* script) { |
| 1221 |
MOZ_ASSERT(script->hasBaselineScript()); |
| 1222 |
MOZ_ASSERT(!script->jitScript()->icScript()->active()); |
| 1223 |
|
| 1224 |
BaselineScript* baseline = |
| 1225 |
script->jitScript()->clearBaselineScript(gcx, script); |
| 1226 |
BaselineScript::Destroy(gcx, baseline); |
| 1227 |
} |
| 1228 |
|
| 1229 |
void jit::AddSizeOfBaselineData(JSScript* script, |
| 1230 |
mozilla::MallocSizeOf mallocSizeOf, |
| 1231 |
size_t* data) { |
| 1232 |
if (script->hasBaselineScript()) { |
| 1233 |
script->baselineScript()->addSizeOfIncludingThis(mallocSizeOf, data); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
void jit::ToggleBaselineProfiling(JSContext* cx, bool enable) { |
| 1238 |
JitRuntime* jrt = cx->runtime()->jitRuntime(); |
| 1239 |
if (!jrt) { |
| 1240 |
return; |
| 1241 |
} |
| 1242 |
|
| 1243 |
jrt->baselineInterpreter().toggleProfilerInstrumentation(enable); |
| 1244 |
|
| 1245 |
for (ZonesIter zone(cx->runtime(), SkipAtoms); !zone.done(); zone.next()) { |
| 1246 |
if (!zone->jitZone()) { |
| 1247 |
continue; |
| 1248 |
} |
| 1249 |
zone->jitZone()->forEachJitScript([&](jit::JitScript* jitScript) { |
| 1250 |
JSScript* script = jitScript->owningScript(); |
| 1251 |
if (enable) { |
| 1252 |
jitScript->ensureProfileString(cx, script); |
| 1253 |
} |
| 1254 |
if (script->hasBaselineScript()) { |
| 1255 |
AutoWritableJitCode awjc(script->baselineScript()->method()); |
| 1256 |
script->baselineScript()->toggleProfilerInstrumentation(enable); |
| 1257 |
} |
| 1258 |
}); |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
void BaselineInterpreter::init(JitCode* code, uint32_t interpretOpOffset, |
| 1263 |
uint32_t interpretOpNoDebugTrapOffset, |
| 1264 |
uint32_t bailoutPrologueOffset, |
| 1265 |
uint32_t profilerEnterToggleOffset, |
| 1266 |
uint32_t profilerExitToggleOffset, |
| 1267 |
uint32_t debugTrapHandlerOffset, |
| 1268 |
CodeOffsetVector&& debugInstrumentationOffsets, |
| 1269 |
CodeOffsetVector&& debugTrapOffsets, |
| 1270 |
CodeOffsetVector&& codeCoverageOffsets, |
| 1271 |
ICReturnOffsetVector&& icReturnOffsets, |
| 1272 |
const CallVMOffsets& callVMOffsets) { |
| 1273 |
code_ = code; |
| 1274 |
interpretOpOffset_ = interpretOpOffset; |
| 1275 |
interpretOpNoDebugTrapOffset_ = interpretOpNoDebugTrapOffset; |
| 1276 |
bailoutPrologueOffset_ = bailoutPrologueOffset; |
| 1277 |
profilerEnterToggleOffset_ = profilerEnterToggleOffset; |
| 1278 |
profilerExitToggleOffset_ = profilerExitToggleOffset; |
| 1279 |
debugTrapHandlerOffset_ = debugTrapHandlerOffset; |
| 1280 |
debugInstrumentationOffsets_ = std::move(debugInstrumentationOffsets); |
| 1281 |
debugTrapOffsets_ = std::move(debugTrapOffsets); |
| 1282 |
codeCoverageOffsets_ = std::move(codeCoverageOffsets); |
| 1283 |
icReturnOffsets_ = std::move(icReturnOffsets); |
| 1284 |
callVMOffsets_ = callVMOffsets; |
| 1285 |
} |
| 1286 |
|
| 1287 |
uint8_t* BaselineInterpreter::retAddrForIC(JSOp op) const { |
| 1288 |
for (const ICReturnOffset& entry : icReturnOffsets_) { |
| 1289 |
if (entry.op == op) { |
| 1290 |
return codeAtOffset(entry.offset); |
| 1291 |
} |
| 1292 |
} |
| 1293 |
MOZ_CRASH("Unexpected op"); |
| 1294 |
} |
| 1295 |
|
| 1296 |
bool jit::GenerateBaselineInterpreter(JSContext* cx, |
| 1297 |
BaselineInterpreter& interpreter) { |
| 1298 |
if (IsBaselineInterpreterEnabled()) { |
| 1299 |
TempAllocator temp(&cx->tempLifoAlloc()); |
| 1300 |
StackMacroAssembler masm(cx, temp); |
| 1301 |
BaselineInterpreterGenerator generator(cx, temp, masm); |
| 1302 |
return generator.generate(cx, interpreter); |
| 1303 |
} |
| 1304 |
|
| 1305 |
return true; |
| 1306 |
} |