| 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 |
#ifndef jit_JSJitFrameIter_h |
| 8 |
#define jit_JSJitFrameIter_h |
| 9 |
|
| 10 |
#include "mozilla/Maybe.h" |
| 11 |
|
| 12 |
#include "jstypes.h" |
| 13 |
|
| 14 |
#include "jit/JitCode.h" |
| 15 |
#include "jit/MachineState.h" |
| 16 |
#include "jit/Snapshots.h" |
| 17 |
#include "js/ProfilingFrameIterator.h" |
| 18 |
#include "vm/JSFunction.h" |
| 19 |
#include "vm/JSScript.h" |
| 20 |
|
| 21 |
namespace js { |
| 22 |
|
| 23 |
class ArgumentsObject; |
| 24 |
|
| 25 |
namespace jit { |
| 26 |
|
| 27 |
enum class FrameType { |
| 28 |
// A JS frame is analogous to a js::InterpreterFrame, representing one |
| 29 |
// scripted function activation. IonJS frames are used by the optimizing |
| 30 |
// compiler. |
| 31 |
IonJS, |
| 32 |
|
| 33 |
// JS frame used by the Baseline Interpreter and Baseline JIT. |
| 34 |
BaselineJS, |
| 35 |
|
| 36 |
// Frame pushed by Baseline stubs that make non-tail calls, so that the |
| 37 |
// return address -> ICEntry mapping works. |
| 38 |
BaselineStub, |
| 39 |
|
| 40 |
// The entry frame is the initial prologue block transitioning from the VM |
| 41 |
// into the Ion world. |
| 42 |
CppToJSJit, |
| 43 |
|
| 44 |
// This entry frame sits right before the baseline interpreter |
| 45 |
// so that external profilers can identify which function is being |
| 46 |
// interpreted. Only used under the --emit-interpreter-entry option. |
| 47 |
BaselineInterpreterEntry, |
| 48 |
|
| 49 |
// A rectifier frame sits in between two JS frames, adapting argc != nargs |
| 50 |
// mismatches in calls. |
| 51 |
Rectifier, |
| 52 |
|
| 53 |
// Ion IC calling a scripted getter/setter or a VMFunction. |
| 54 |
IonICCall, |
| 55 |
|
| 56 |
// An exit frame is necessary for transitioning from a JS frame into C++. |
| 57 |
// From within C++, an exit frame is always the last frame in any |
| 58 |
// JitActivation. |
| 59 |
Exit, |
| 60 |
|
| 61 |
// A bailout frame is a special IonJS jit frame after a bailout, and before |
| 62 |
// the reconstruction of the BaselineJS frame. From within C++, a bailout |
| 63 |
// frame is always the last frame in a JitActivation iff the bailout frame |
| 64 |
// information is recorded on the JitActivation. |
| 65 |
Bailout, |
| 66 |
|
| 67 |
// A wasm to JS frame is constructed during fast calls from wasm to the JS |
| 68 |
// jits, used as a marker to interleave JS jit and wasm frames. From the |
| 69 |
// point of view of JS JITs, this is just another kind of entry frame. |
| 70 |
WasmToJSJit, |
| 71 |
|
| 72 |
// Frame for a TrampolineNative, a JS builtin implemented with a JIT |
| 73 |
// trampoline. See jit/TrampolineNatives.h. |
| 74 |
TrampolineNative, |
| 75 |
}; |
| 76 |
|
| 77 |
enum class ReadFrameArgsBehavior { |
| 78 |
// Read all actual arguments. Will invoke the callback numActualArgs times. |
| 79 |
Actuals, |
| 80 |
|
| 81 |
// Read all argument values in the stack frame. Will invoke the callback |
| 82 |
// max(numFormalArgs, numActualArgs) times. |
| 83 |
ActualsAndFormals, |
| 84 |
}; |
| 85 |
|
| 86 |
class CommonFrameLayout; |
| 87 |
class JitFrameLayout; |
| 88 |
class ExitFrameLayout; |
| 89 |
|
| 90 |
class BaselineFrame; |
| 91 |
class JitActivation; |
| 92 |
class SafepointIndex; |
| 93 |
class OsiIndex; |
| 94 |
|
| 95 |
// Iterate over the JIT stack to assert that all invariants are respected. |
| 96 |
// - Check that all entry frames are aligned on JitStackAlignment. |
| 97 |
// - Check that all rectifier frames keep the JitStackAlignment. |
| 98 |
|
| 99 |
void AssertJitStackInvariants(JSContext* cx); |
| 100 |
|
| 101 |
// A JSJitFrameIter can iterate over a linear frame group of JS jit frames |
| 102 |
// only. It will stop at the first frame that is not of the same kind, or at |
| 103 |
// the end of an activation. |
| 104 |
// |
| 105 |
// If you want to handle every kind of frames (including wasm frames), use |
| 106 |
// JitFrameIter. If you want to skip interleaved frames of other kinds, use |
| 107 |
// OnlyJSJitFrameIter. |
| 108 |
|
| 109 |
class JSJitFrameIter { |
| 110 |
protected: |
| 111 |
uint8_t* current_; |
| 112 |
FrameType type_; |
| 113 |
uint8_t* resumePCinCurrentFrame_ = nullptr; |
| 114 |
|
| 115 |
// Size of the current Baseline frame. Equivalent to |
| 116 |
// BaselineFrame::debugFrameSize_ in debug builds. |
| 117 |
mozilla::Maybe<uint32_t> baselineFrameSize_; |
| 118 |
|
| 119 |
private: |
| 120 |
mutable const SafepointIndex* cachedSafepointIndex_ = nullptr; |
| 121 |
const JitActivation* activation_; |
| 122 |
|
| 123 |
void dumpBaseline() const; |
| 124 |
|
| 125 |
public: |
| 126 |
// See comment above the class. |
| 127 |
explicit JSJitFrameIter(const JitActivation* activation); |
| 128 |
|
| 129 |
// A constructor specialized for jit->wasm frames, which starts at a |
| 130 |
// specific FP. |
| 131 |
JSJitFrameIter(const JitActivation* activation, uint8_t* fp, bool unwinding); |
| 132 |
|
| 133 |
void setResumePCInCurrentFrame(uint8_t* newAddr) { |
| 134 |
resumePCinCurrentFrame_ = newAddr; |
| 135 |
} |
| 136 |
|
| 137 |
// Current frame information. |
| 138 |
FrameType type() const { return type_; } |
| 139 |
uint8_t* fp() const { return current_; } |
| 140 |
const JitActivation* activation() const { return activation_; } |
| 141 |
|
| 142 |
CommonFrameLayout* current() const { return (CommonFrameLayout*)current_; } |
| 143 |
|
| 144 |
inline uint8_t* returnAddress() const; |
| 145 |
|
| 146 |
// Return the pointer of the JitFrame, the iterator is assumed to be settled |
| 147 |
// on a scripted frame. |
| 148 |
JitFrameLayout* jsFrame() const; |
| 149 |
|
| 150 |
inline ExitFrameLayout* exitFrame() const; |
| 151 |
|
| 152 |
// Returns whether the JS frame has been invalidated and, if so, |
| 153 |
// places the invalidated Ion script in |ionScript|. |
| 154 |
bool checkInvalidation(IonScript** ionScript) const; |
| 155 |
bool checkInvalidation() const; |
| 156 |
|
| 157 |
bool isExitFrame() const { return type_ == FrameType::Exit; } |
| 158 |
bool isScripted() const { |
| 159 |
return type_ == FrameType::BaselineJS || type_ == FrameType::IonJS || |
| 160 |
type_ == FrameType::Bailout; |
| 161 |
} |
| 162 |
bool isBaselineJS() const { return type_ == FrameType::BaselineJS; } |
| 163 |
bool isIonScripted() const { |
| 164 |
return type_ == FrameType::IonJS || type_ == FrameType::Bailout; |
| 165 |
} |
| 166 |
bool isIonJS() const { return type_ == FrameType::IonJS; } |
| 167 |
bool isIonICCall() const { return type_ == FrameType::IonICCall; } |
| 168 |
bool isBailoutJS() const { return type_ == FrameType::Bailout; } |
| 169 |
bool isBaselineStub() const { return type_ == FrameType::BaselineStub; } |
| 170 |
bool isBaselineInterpreterEntry() const { |
| 171 |
return type_ == FrameType::BaselineInterpreterEntry; |
| 172 |
} |
| 173 |
bool isRectifier() const { return type_ == FrameType::Rectifier; } |
| 174 |
bool isTrampolineNative() const { |
| 175 |
return type_ == FrameType::TrampolineNative; |
| 176 |
} |
| 177 |
bool isBareExit() const; |
| 178 |
bool isUnwoundJitExit() const; |
| 179 |
template <typename T> |
| 180 |
bool isExitFrameLayout() const; |
| 181 |
|
| 182 |
static bool isEntry(FrameType type) { |
| 183 |
return type == FrameType::CppToJSJit || type == FrameType::WasmToJSJit; |
| 184 |
} |
| 185 |
bool isEntry() const { return isEntry(type_); } |
| 186 |
|
| 187 |
bool isFunctionFrame() const; |
| 188 |
|
| 189 |
bool isConstructing() const; |
| 190 |
|
| 191 |
void* calleeToken() const; |
| 192 |
JSFunction* callee() const; |
| 193 |
JSFunction* maybeCallee() const; |
| 194 |
unsigned numActualArgs() const; |
| 195 |
JSScript* script() const; |
| 196 |
JSScript* maybeForwardedScript() const; |
| 197 |
void baselineScriptAndPc(JSScript** scriptRes, jsbytecode** pcRes) const; |
| 198 |
Value* actualArgs() const; |
| 199 |
|
| 200 |
// Returns the address of the next instruction that will execute in this |
| 201 |
// frame, once control returns to this frame. |
| 202 |
uint8_t* resumePCinCurrentFrame() const { return resumePCinCurrentFrame_; } |
| 203 |
|
| 204 |
// Previous frame information extracted from the current frame. |
| 205 |
inline FrameType prevType() const; |
| 206 |
uint8_t* prevFp() const; |
| 207 |
|
| 208 |
// Functions used to iterate on frames. When prevType is an entry, |
| 209 |
// the current frame is the last JS Jit frame. |
| 210 |
bool done() const { return isEntry(); } |
| 211 |
void operator++(); |
| 212 |
|
| 213 |
// Returns the IonScript associated with this JS frame. |
| 214 |
IonScript* ionScript() const; |
| 215 |
|
| 216 |
// Returns the IonScript associated with this JS frame; the frame must |
| 217 |
// not be invalidated. |
| 218 |
IonScript* ionScriptFromCalleeToken() const; |
| 219 |
|
| 220 |
// Returns the Safepoint associated with this JS frame. Incurs a lookup |
| 221 |
// overhead. |
| 222 |
const SafepointIndex* safepoint() const; |
| 223 |
|
| 224 |
// Returns the OSI index associated with this JS frame. Incurs a lookup |
| 225 |
// overhead. |
| 226 |
const OsiIndex* osiIndex() const; |
| 227 |
|
| 228 |
// Returns the Snapshot offset associated with this JS frame. Incurs a |
| 229 |
// lookup overhead. |
| 230 |
SnapshotOffset snapshotOffset() const; |
| 231 |
|
| 232 |
uintptr_t* spillBase() const; |
| 233 |
MachineState machineState() const; |
| 234 |
|
| 235 |
template <class Op> |
| 236 |
void unaliasedForEachActual(Op op) const { |
| 237 |
MOZ_ASSERT(isBaselineJS()); |
| 238 |
|
| 239 |
unsigned nactual = numActualArgs(); |
| 240 |
Value* argv = actualArgs(); |
| 241 |
for (unsigned i = 0; i < nactual; i++) { |
| 242 |
op(argv[i]); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
void dump() const; |
| 247 |
|
| 248 |
inline BaselineFrame* baselineFrame() const; |
| 249 |
|
| 250 |
// Returns the number of local and expression stack Values for the current |
| 251 |
// Baseline frame. |
| 252 |
inline uint32_t baselineFrameNumValueSlots() const; |
| 253 |
|
| 254 |
// This function isn't used, but we keep it here (debug-only) because it is |
| 255 |
// helpful when chasing issues with the jitcode map. |
| 256 |
#ifdef DEBUG |
| 257 |
bool verifyReturnAddressUsingNativeToBytecodeMap(); |
| 258 |
#else |
| 259 |
bool verifyReturnAddressUsingNativeToBytecodeMap() { return true; } |
| 260 |
#endif |
| 261 |
}; |
| 262 |
|
| 263 |
class JitcodeGlobalTable; |
| 264 |
|
| 265 |
class JSJitProfilingFrameIterator { |
| 266 |
uint8_t* fp_; |
| 267 |
uint8_t* wasmCallerFP_ = nullptr; |
| 268 |
// See JS::ProfilingFrameIterator::endStackAddress_ comment. |
| 269 |
void* endStackAddress_ = nullptr; |
| 270 |
FrameType type_; |
| 271 |
void* resumePCinCurrentFrame_; |
| 272 |
|
| 273 |
inline JSScript* frameScript() const; |
| 274 |
[[nodiscard]] bool tryInitWithPC(void* pc); |
| 275 |
[[nodiscard]] bool tryInitWithTable(JitcodeGlobalTable* table, void* pc, |
| 276 |
bool forLastCallSite); |
| 277 |
|
| 278 |
void moveToNextFrame(CommonFrameLayout* frame); |
| 279 |
|
| 280 |
public: |
| 281 |
JSJitProfilingFrameIterator(JSContext* cx, void* pc, void* sp); |
| 282 |
explicit JSJitProfilingFrameIterator(CommonFrameLayout* exitFP); |
| 283 |
|
| 284 |
void operator++(); |
| 285 |
bool done() const { return fp_ == nullptr; } |
| 286 |
|
| 287 |
const char* baselineInterpreterLabel() const; |
| 288 |
void baselineInterpreterScriptPC(JSScript** script, jsbytecode** pc, |
| 289 |
uint64_t* realmID) const; |
| 290 |
|
| 291 |
void* fp() const { |
| 292 |
MOZ_ASSERT(!done()); |
| 293 |
return fp_; |
| 294 |
} |
| 295 |
void* wasmCallerFP() const { |
| 296 |
MOZ_ASSERT(done()); |
| 297 |
MOZ_ASSERT(bool(wasmCallerFP_) == (type_ == FrameType::WasmToJSJit)); |
| 298 |
return wasmCallerFP_; |
| 299 |
} |
| 300 |
inline JitFrameLayout* framePtr() const; |
| 301 |
void* stackAddress() const { return fp(); } |
| 302 |
FrameType frameType() const { |
| 303 |
MOZ_ASSERT(!done()); |
| 304 |
return type_; |
| 305 |
} |
| 306 |
void* resumePCinCurrentFrame() const { |
| 307 |
MOZ_ASSERT(!done()); |
| 308 |
return resumePCinCurrentFrame_; |
| 309 |
} |
| 310 |
|
| 311 |
void* endStackAddress() const { return endStackAddress_; } |
| 312 |
}; |
| 313 |
|
| 314 |
class RInstructionResults { |
| 315 |
// Vector of results of recover instructions. |
| 316 |
using Values = mozilla::Vector<HeapPtr<Value>, 1, SystemAllocPolicy>; |
| 317 |
UniquePtr<Values> results_; |
| 318 |
|
| 319 |
// The frame pointer is used as a key to check if the current frame already |
| 320 |
// bailed out. |
| 321 |
JitFrameLayout* fp_; |
| 322 |
|
| 323 |
// Record if we tried and succeed at allocating and filling the vector of |
| 324 |
// recover instruction results, if needed. This flag is needed in order to |
| 325 |
// avoid evaluating the recover instruction twice. |
| 326 |
bool initialized_; |
| 327 |
|
| 328 |
public: |
| 329 |
explicit RInstructionResults(JitFrameLayout* fp); |
| 330 |
RInstructionResults(RInstructionResults&& src); |
| 331 |
|
| 332 |
RInstructionResults& operator=(RInstructionResults&& rhs); |
| 333 |
|
| 334 |
~RInstructionResults(); |
| 335 |
|
| 336 |
[[nodiscard]] bool init(JSContext* cx, uint32_t numResults); |
| 337 |
bool isInitialized() const; |
| 338 |
size_t length() const; |
| 339 |
|
| 340 |
JitFrameLayout* frame() const; |
| 341 |
|
| 342 |
HeapPtr<Value>& operator[](size_t index); |
| 343 |
|
| 344 |
void trace(JSTracer* trc); |
| 345 |
}; |
| 346 |
|
| 347 |
struct MaybeReadFallback { |
| 348 |
enum FallbackConsequence { Fallback_Invalidate, Fallback_DoNothing }; |
| 349 |
|
| 350 |
JSContext* maybeCx = nullptr; |
| 351 |
JitActivation* activation = nullptr; |
| 352 |
const JSJitFrameIter* frame = nullptr; |
| 353 |
const FallbackConsequence consequence = Fallback_Invalidate; |
| 354 |
|
| 355 |
MaybeReadFallback() = default; |
| 356 |
|
| 357 |
MaybeReadFallback(JSContext* cx, JitActivation* activation, |
| 358 |
const JSJitFrameIter* frame, |
| 359 |
FallbackConsequence consequence = Fallback_Invalidate) |
| 360 |
: maybeCx(cx), |
| 361 |
activation(activation), |
| 362 |
frame(frame), |
| 363 |
consequence(consequence) {} |
| 364 |
|
| 365 |
bool canRecoverResults() { return maybeCx; } |
| 366 |
}; |
| 367 |
|
| 368 |
class RResumePoint; |
| 369 |
|
| 370 |
// Reads frame information in snapshot-encoding order (that is, outermost frame |
| 371 |
// to innermost frame). |
| 372 |
class SnapshotIterator { |
| 373 |
protected: |
| 374 |
SnapshotReader snapshot_; |
| 375 |
RecoverReader recover_; |
| 376 |
JitFrameLayout* fp_; |
| 377 |
const MachineState* machine_; |
| 378 |
IonScript* ionScript_; |
| 379 |
RInstructionResults* instructionResults_; |
| 380 |
|
| 381 |
enum class ReadMethod : bool { |
| 382 |
// Read the normal value. |
| 383 |
Normal, |
| 384 |
|
| 385 |
// Read the default value, or the normal value if there is no default. |
| 386 |
AlwaysDefault, |
| 387 |
}; |
| 388 |
|
| 389 |
private: |
| 390 |
// Read a spilled register from the machine state. |
| 391 |
bool hasRegister(Register reg) const { return machine_->has(reg); } |
| 392 |
uintptr_t fromRegister(Register reg) const { return machine_->read(reg); } |
| 393 |
|
| 394 |
bool hasRegister(FloatRegister reg) const { return machine_->has(reg); } |
| 395 |
template <typename T> |
| 396 |
T fromRegister(FloatRegister reg) const { |
| 397 |
return machine_->read<T>(reg); |
| 398 |
} |
| 399 |
|
| 400 |
// Read an uintptr_t from the stack. |
| 401 |
bool hasStack(int32_t offset) const { return true; } |
| 402 |
uintptr_t fromStack(int32_t offset) const; |
| 403 |
|
| 404 |
bool hasInstructionResult(uint32_t index) const { |
| 405 |
return instructionResults_; |
| 406 |
} |
| 407 |
bool hasInstructionResults() const { return instructionResults_; } |
| 408 |
Value fromInstructionResult(uint32_t index) const; |
| 409 |
|
| 410 |
Value allocationValue(const RValueAllocation& a, |
| 411 |
ReadMethod rm = ReadMethod::Normal); |
| 412 |
[[nodiscard]] bool allocationReadable(const RValueAllocation& a, |
| 413 |
ReadMethod rm = ReadMethod::Normal); |
| 414 |
void writeAllocationValuePayload(const RValueAllocation& a, const Value& v); |
| 415 |
void warnUnreadableAllocation(); |
| 416 |
|
| 417 |
public: |
| 418 |
// Handle iterating over RValueAllocations of the snapshots. |
| 419 |
inline RValueAllocation readAllocation() { |
| 420 |
MOZ_ASSERT(moreAllocations()); |
| 421 |
return snapshot_.readAllocation(); |
| 422 |
} |
| 423 |
void skip() { snapshot_.skipAllocation(); } |
| 424 |
|
| 425 |
const RResumePoint* resumePoint() const; |
| 426 |
const RInstruction* instruction() const { return recover_.instruction(); } |
| 427 |
|
| 428 |
uint32_t numAllocations() const; |
| 429 |
inline bool moreAllocations() const { |
| 430 |
return snapshot_.numAllocationsRead() < numAllocations(); |
| 431 |
} |
| 432 |
|
| 433 |
JitFrameLayout* frame() { return fp_; }; |
| 434 |
|
| 435 |
// Used by recover instruction to store the value back into the instruction |
| 436 |
// results array. |
| 437 |
void storeInstructionResult(const Value& v); |
| 438 |
|
| 439 |
public: |
| 440 |
// Exhibits frame properties contained in the snapshot. |
| 441 |
uint32_t pcOffset() const; |
| 442 |
ResumeMode resumeMode() const; |
| 443 |
|
| 444 |
bool resumeAfter() const { |
| 445 |
// Calls in outer frames are never considered resume-after. |
| 446 |
MOZ_ASSERT_IF(moreFrames(), !IsResumeAfter(resumeMode())); |
| 447 |
return IsResumeAfter(resumeMode()); |
| 448 |
} |
| 449 |
inline BailoutKind bailoutKind() const { return snapshot_.bailoutKind(); } |
| 450 |
|
| 451 |
IonScript* ionScript() const { return ionScript_; } |
| 452 |
|
| 453 |
public: |
| 454 |
// Read the next instruction available and get ready to either skip it or |
| 455 |
// evaluate it. |
| 456 |
inline void nextInstruction() { |
| 457 |
MOZ_ASSERT(snapshot_.numAllocationsRead() == numAllocations()); |
| 458 |
recover_.nextInstruction(); |
| 459 |
snapshot_.resetNumAllocationsRead(); |
| 460 |
} |
| 461 |
|
| 462 |
// Skip an Instruction by walking to the next instruction and by skipping |
| 463 |
// all the allocations corresponding to this instruction. |
| 464 |
void skipInstruction(); |
| 465 |
|
| 466 |
inline bool moreInstructions() const { return recover_.moreInstructions(); } |
| 467 |
|
| 468 |
// Register a vector used for storing the results of the evaluation of |
| 469 |
// recover instructions. This vector should be registered before the |
| 470 |
// beginning of the iteration. This function is in charge of allocating |
| 471 |
// enough space for all instructions results, and return false iff it fails. |
| 472 |
[[nodiscard]] bool initInstructionResults(MaybeReadFallback& fallback); |
| 473 |
|
| 474 |
protected: |
| 475 |
// This function is used internally for computing the result of the recover |
| 476 |
// instructions. |
| 477 |
[[nodiscard]] bool computeInstructionResults( |
| 478 |
JSContext* cx, RInstructionResults* results) const; |
| 479 |
|
| 480 |
public: |
| 481 |
// Handle iterating over frames of the snapshots. |
| 482 |
void nextFrame(); |
| 483 |
void settleOnFrame(); |
| 484 |
|
| 485 |
inline bool moreFrames() const { |
| 486 |
// The last instruction is recovering the innermost frame, so as long as |
| 487 |
// there is more instruction there is necesseray more frames. |
| 488 |
return moreInstructions(); |
| 489 |
} |
| 490 |
|
| 491 |
public: |
| 492 |
// Connect all informations about the current script in order to recover the |
| 493 |
// content of baseline frames. |
| 494 |
|
| 495 |
SnapshotIterator(const JSJitFrameIter& iter, |
| 496 |
const MachineState* machineState); |
| 497 |
SnapshotIterator(); |
| 498 |
|
| 499 |
Value read() { return allocationValue(readAllocation()); } |
| 500 |
|
| 501 |
// Like |read()| but also supports IntPtr and Int64 allocations, which are |
| 502 |
// returned as BigInt values. |
| 503 |
bool readMaybeUnpackedBigInt(JSContext* cx, MutableHandle<Value> result); |
| 504 |
|
| 505 |
int32_t readInt32() { |
| 506 |
Value val = read(); |
| 507 |
MOZ_RELEASE_ASSERT(val.isInt32()); |
| 508 |
return val.toInt32(); |
| 509 |
} |
| 510 |
|
| 511 |
double readNumber() { |
| 512 |
Value val = read(); |
| 513 |
MOZ_RELEASE_ASSERT(val.isNumber()); |
| 514 |
return val.toNumber(); |
| 515 |
} |
| 516 |
|
| 517 |
JSString* readString() { |
| 518 |
Value val = read(); |
| 519 |
MOZ_RELEASE_ASSERT(val.isString()); |
| 520 |
return val.toString(); |
| 521 |
} |
| 522 |
|
| 523 |
JS::BigInt* readBigInt() { |
| 524 |
Value val = read(); |
| 525 |
MOZ_RELEASE_ASSERT(val.isBigInt()); |
| 526 |
return val.toBigInt(); |
| 527 |
} |
| 528 |
|
| 529 |
JSObject* readObject() { |
| 530 |
Value val = read(); |
| 531 |
MOZ_RELEASE_ASSERT(val.isObject()); |
| 532 |
return &val.toObject(); |
| 533 |
} |
| 534 |
|
| 535 |
JS::GCCellPtr readGCCellPtr() { |
| 536 |
Value val = read(); |
| 537 |
MOZ_RELEASE_ASSERT(val.isGCThing()); |
| 538 |
return val.toGCCellPtr(); |
| 539 |
} |
| 540 |
|
| 541 |
// Read the |Normal| value unless it is not available and that the snapshot |
| 542 |
// provides a |Default| value. This is useful to avoid invalidations of the |
| 543 |
// frame while we are only interested in a few properties which are provided |
| 544 |
// by the |Default| value. |
| 545 |
Value readWithDefault(RValueAllocation* alloc) { |
| 546 |
*alloc = RValueAllocation(); |
| 547 |
RValueAllocation a = readAllocation(); |
| 548 |
if (allocationReadable(a)) { |
| 549 |
return allocationValue(a); |
| 550 |
} |
| 551 |
|
| 552 |
*alloc = a; |
| 553 |
return allocationValue(a, ReadMethod::AlwaysDefault); |
| 554 |
} |
| 555 |
|
| 556 |
Value maybeRead(const RValueAllocation& a, MaybeReadFallback& fallback); |
| 557 |
Value maybeRead(MaybeReadFallback& fallback) { |
| 558 |
RValueAllocation a = readAllocation(); |
| 559 |
return maybeRead(a, fallback); |
| 560 |
} |
| 561 |
|
| 562 |
bool tryRead(Value* result); |
| 563 |
|
| 564 |
private: |
| 565 |
int64_t allocationInt64(const RValueAllocation& alloc); |
| 566 |
intptr_t allocationIntPtr(const RValueAllocation& alloc); |
| 567 |
|
| 568 |
public: |
| 569 |
int64_t readInt64() { return allocationInt64(readAllocation()); } |
| 570 |
|
| 571 |
// Read either a BigInt or unpacked BigInt. |
| 572 |
JS::BigInt* readBigInt(JSContext* cx); |
| 573 |
|
| 574 |
void traceAllocation(JSTracer* trc); |
| 575 |
|
| 576 |
template <class Op> |
| 577 |
void readFunctionFrameArgs(Op& op, ArgumentsObject** argsObj, Value* thisv, |
| 578 |
unsigned start, unsigned end, JSScript* script, |
| 579 |
MaybeReadFallback& fallback) { |
| 580 |
// Assumes that the common frame arguments have already been read. |
| 581 |
if (script->needsArgsObj()) { |
| 582 |
if (argsObj) { |
| 583 |
Value v = maybeRead(fallback); |
| 584 |
if (v.isObject()) { |
| 585 |
*argsObj = &v.toObject().as<ArgumentsObject>(); |
| 586 |
} |
| 587 |
} else { |
| 588 |
skip(); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
if (thisv) { |
| 593 |
*thisv = maybeRead(fallback); |
| 594 |
} else { |
| 595 |
skip(); |
| 596 |
} |
| 597 |
|
| 598 |
unsigned i = 0; |
| 599 |
if (end < start) { |
| 600 |
i = start; |
| 601 |
} |
| 602 |
|
| 603 |
for (; i < start; i++) { |
| 604 |
skip(); |
| 605 |
} |
| 606 |
for (; i < end; i++) { |
| 607 |
// We are not always able to read values from the snapshots, some values |
| 608 |
// such as non-gc things may still be live in registers and cause an |
| 609 |
// error while reading the machine state. |
| 610 |
Value v = maybeRead(fallback); |
| 611 |
op(v); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
// Iterate over all the allocations and return only the value of the |
| 616 |
// allocation located at one index. |
| 617 |
Value maybeReadAllocByIndex(size_t index); |
| 618 |
|
| 619 |
#ifdef TRACK_SNAPSHOTS |
| 620 |
void spewBailingFrom() const { snapshot_.spewBailingFrom(); } |
| 621 |
#endif |
| 622 |
}; |
| 623 |
|
| 624 |
// Reads frame information in callstack order (that is, innermost frame to |
| 625 |
// outermost frame). |
| 626 |
class InlineFrameIterator { |
| 627 |
const JSJitFrameIter* frame_; |
| 628 |
SnapshotIterator start_; |
| 629 |
SnapshotIterator si_; |
| 630 |
uint32_t framesRead_; |
| 631 |
|
| 632 |
// When the inline-frame-iterator is created, this variable is defined to |
| 633 |
// UINT32_MAX. Then the first iteration of findNextFrame, which settle on |
| 634 |
// the innermost frame, is used to update this counter to the number of |
| 635 |
// frames contained in the recover buffer. |
| 636 |
uint32_t frameCount_; |
| 637 |
|
| 638 |
// The |calleeTemplate_| fields contains either the JSFunction or the |
| 639 |
// template from which it is supposed to be cloned. The |calleeRVA_| is an |
| 640 |
// Invalid value allocation, if the |calleeTemplate_| field is the effective |
| 641 |
// JSFunction, and not its template. On the other hand, any other value |
| 642 |
// allocation implies that the |calleeTemplate_| is the template JSFunction |
| 643 |
// from which the effective one would be derived and cached by the Recover |
| 644 |
// instruction result. |
| 645 |
RootedFunction calleeTemplate_; |
| 646 |
RValueAllocation calleeRVA_; |
| 647 |
|
| 648 |
RootedScript script_; |
| 649 |
jsbytecode* pc_; |
| 650 |
uint32_t numActualArgs_; |
| 651 |
|
| 652 |
// Register state, used by all snapshot iterators. |
| 653 |
MachineState machine_; |
| 654 |
|
| 655 |
struct Nop { |
| 656 |
void operator()(const Value& v) {} |
| 657 |
}; |
| 658 |
|
| 659 |
private: |
| 660 |
void findNextFrame(); |
| 661 |
JSObject* computeEnvironmentChain(const Value& envChainValue, |
| 662 |
MaybeReadFallback& fallback, |
| 663 |
bool* hasInitialEnv = nullptr) const; |
| 664 |
|
| 665 |
public: |
| 666 |
InlineFrameIterator(JSContext* cx, const JSJitFrameIter* iter); |
| 667 |
InlineFrameIterator(JSContext* cx, const InlineFrameIterator* iter); |
| 668 |
|
| 669 |
bool more() const { return frame_ && framesRead_ < frameCount_; } |
| 670 |
|
| 671 |
// Due to optimizations, we are not always capable of reading the callee of |
| 672 |
// inlined frames without invalidating the IonCode. This function might |
| 673 |
// return either the effective callee of the JSFunction which might be used |
| 674 |
// to create it. |
| 675 |
// |
| 676 |
// As such, the |calleeTemplate()| can be used to read most of the metadata |
| 677 |
// which are conserved across clones. |
| 678 |
JSFunction* calleeTemplate() const { |
| 679 |
MOZ_ASSERT(isFunctionFrame()); |
| 680 |
return calleeTemplate_; |
| 681 |
} |
| 682 |
JSFunction* maybeCalleeTemplate() const { return calleeTemplate_; } |
| 683 |
|
| 684 |
JSFunction* callee(MaybeReadFallback& fallback) const; |
| 685 |
|
| 686 |
unsigned numActualArgs() const { |
| 687 |
// The number of actual arguments for inline frames is determined by this |
| 688 |
// iterator based on the caller's bytecode instruction (Call, FunCall, |
| 689 |
// GetProp/SetProp, etc). For the outer function it's stored in the stack |
| 690 |
// frame. |
| 691 |
if (more()) { |
| 692 |
return numActualArgs_; |
| 693 |
} |
| 694 |
|
| 695 |
return frame_->numActualArgs(); |
| 696 |
} |
| 697 |
|
| 698 |
template <class ArgOp, class LocalOp> |
| 699 |
void readFrameArgsAndLocals(JSContext* cx, ArgOp& argOp, LocalOp& localOp, |
| 700 |
JSObject** envChain, bool* hasInitialEnv, |
| 701 |
Value* rval, ArgumentsObject** argsObj, |
| 702 |
Value* thisv, ReadFrameArgsBehavior behavior, |
| 703 |
MaybeReadFallback& fallback) const { |
| 704 |
SnapshotIterator s(si_); |
| 705 |
|
| 706 |
// Read the env chain. |
| 707 |
if (envChain) { |
| 708 |
Value envChainValue = s.maybeRead(fallback); |
| 709 |
*envChain = |
| 710 |
computeEnvironmentChain(envChainValue, fallback, hasInitialEnv); |
| 711 |
} else { |
| 712 |
s.skip(); |
| 713 |
} |
| 714 |
|
| 715 |
// Read return value. |
| 716 |
if (rval) { |
| 717 |
*rval = s.maybeRead(fallback); |
| 718 |
} else { |
| 719 |
s.skip(); |
| 720 |
} |
| 721 |
|
| 722 |
// Read arguments, which only function frames have. |
| 723 |
if (isFunctionFrame()) { |
| 724 |
unsigned nactual = numActualArgs(); |
| 725 |
unsigned nformal = calleeTemplate()->nargs(); |
| 726 |
|
| 727 |
// Read the formal arguments, which are taken from the inlined frame, |
| 728 |
// because it will have the updated value when JSOp::SetArg is used. |
| 729 |
unsigned numFormalsToRead; |
| 730 |
if (behavior == ReadFrameArgsBehavior::Actuals) { |
| 731 |
numFormalsToRead = std::min(nactual, nformal); |
| 732 |
} else { |
| 733 |
MOZ_ASSERT(behavior == ReadFrameArgsBehavior::ActualsAndFormals); |
| 734 |
numFormalsToRead = nformal; |
| 735 |
} |
| 736 |
s.readFunctionFrameArgs(argOp, argsObj, thisv, 0, numFormalsToRead, |
| 737 |
script(), fallback); |
| 738 |
|
| 739 |
// Skip formals we didn't read. |
| 740 |
for (unsigned i = numFormalsToRead; i < nformal; i++) { |
| 741 |
s.skip(); |
| 742 |
} |
| 743 |
|
| 744 |
if (nactual > nformal) { |
| 745 |
if (more()) { |
| 746 |
// There is still a parent frame of this inlined frame. All |
| 747 |
// arguments (also the overflown) are the last pushed values |
| 748 |
// in the parent frame. To get the overflown arguments, we |
| 749 |
// need to take them from there. |
| 750 |
|
| 751 |
// The overflown arguments are not available in current frame. |
| 752 |
// They are the last pushed arguments in the parent frame of |
| 753 |
// this inlined frame. |
| 754 |
InlineFrameIterator it(cx, this); |
| 755 |
++it; |
| 756 |
unsigned argsObjAdj = it.script()->needsArgsObj() ? 1 : 0; |
| 757 |
bool hasNewTarget = isConstructing(); |
| 758 |
SnapshotIterator parent_s(it.snapshotIterator()); |
| 759 |
|
| 760 |
// Skip over all slots until we get to the last slots |
| 761 |
// (= arguments slots of callee) the +3 is for [this], [returnvalue], |
| 762 |
// [envchain], and maybe +1 for [argsObj] |
| 763 |
MOZ_ASSERT(parent_s.numAllocations() >= |
| 764 |
nactual + 3 + argsObjAdj + hasNewTarget); |
| 765 |
unsigned skip = parent_s.numAllocations() - nactual - 3 - argsObjAdj - |
| 766 |
hasNewTarget; |
| 767 |
for (unsigned j = 0; j < skip; j++) { |
| 768 |
parent_s.skip(); |
| 769 |
} |
| 770 |
|
| 771 |
// Get the overflown arguments |
| 772 |
parent_s.skip(); // env chain |
| 773 |
parent_s.skip(); // return value |
| 774 |
parent_s.readFunctionFrameArgs(argOp, nullptr, nullptr, nformal, |
| 775 |
nactual, it.script(), fallback); |
| 776 |
} else { |
| 777 |
// There is no parent frame to this inlined frame, we can read |
| 778 |
// from the frame's Value vector directly. |
| 779 |
Value* argv = frame_->actualArgs(); |
| 780 |
for (unsigned i = nformal; i < nactual; i++) { |
| 781 |
argOp(argv[i]); |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
// At this point we've read all the formals in s, and can read the |
| 788 |
// locals. |
| 789 |
for (unsigned i = 0; i < script()->nfixed(); i++) { |
| 790 |
localOp(s.maybeRead(fallback)); |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
template <class Op> |
| 795 |
void unaliasedForEachActual(JSContext* cx, Op op, |
| 796 |
MaybeReadFallback& fallback) const { |
| 797 |
Nop nop; |
| 798 |
readFrameArgsAndLocals(cx, op, nop, nullptr, nullptr, nullptr, nullptr, |
| 799 |
nullptr, ReadFrameArgsBehavior::Actuals, fallback); |
| 800 |
} |
| 801 |
|
| 802 |
JSScript* script() const { return script_; } |
| 803 |
jsbytecode* pc() const { return pc_; } |
| 804 |
SnapshotIterator snapshotIterator() const { return si_; } |
| 805 |
bool isFunctionFrame() const; |
| 806 |
bool isModuleFrame() const; |
| 807 |
bool isConstructing() const; |
| 808 |
|
| 809 |
JSObject* environmentChain(MaybeReadFallback& fallback, |
| 810 |
bool* hasInitialEnvironment = nullptr) const { |
| 811 |
SnapshotIterator s(si_); |
| 812 |
|
| 813 |
// envChain |
| 814 |
Value v = s.maybeRead(fallback); |
| 815 |
return computeEnvironmentChain(v, fallback, hasInitialEnvironment); |
| 816 |
} |
| 817 |
|
| 818 |
Value thisArgument(MaybeReadFallback& fallback) const { |
| 819 |
SnapshotIterator s(si_); |
| 820 |
|
| 821 |
// envChain |
| 822 |
s.skip(); |
| 823 |
|
| 824 |
// return value |
| 825 |
s.skip(); |
| 826 |
|
| 827 |
// Arguments object. |
| 828 |
if (script()->needsArgsObj()) { |
| 829 |
s.skip(); |
| 830 |
} |
| 831 |
|
| 832 |
return s.maybeRead(fallback); |
| 833 |
} |
| 834 |
|
| 835 |
InlineFrameIterator& operator++() { |
| 836 |
findNextFrame(); |
| 837 |
return *this; |
| 838 |
} |
| 839 |
|
| 840 |
void dump() const; |
| 841 |
|
| 842 |
void resetOn(const JSJitFrameIter* iter); |
| 843 |
|
| 844 |
const JSJitFrameIter& frame() const { return *frame_; } |
| 845 |
|
| 846 |
// Inline frame number, 0 for the outermost (non-inlined) frame. |
| 847 |
size_t frameNo() const { return frameCount() - framesRead_; } |
| 848 |
size_t frameCount() const { |
| 849 |
MOZ_ASSERT(frameCount_ != UINT32_MAX); |
| 850 |
return frameCount_; |
| 851 |
} |
| 852 |
|
| 853 |
private: |
| 854 |
InlineFrameIterator() = delete; |
| 855 |
InlineFrameIterator(const InlineFrameIterator& iter) = delete; |
| 856 |
}; |
| 857 |
|
| 858 |
} // namespace jit |
| 859 |
} // namespace js |
| 860 |
|
| 861 |
#endif /* jit_JSJitFrameIter_h */ |