| 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 vm_JitActivation_h |
| 8 |
#define vm_JitActivation_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 11 |
#include "mozilla/Atomics.h" // mozilla::Atomic, mozilla::Relaxed |
| 12 |
#include "mozilla/Maybe.h" // mozilla::Maybe |
| 13 |
#include "mozilla/RefPtr.h" // RefPtr |
| 14 |
|
| 15 |
#include <stddef.h> // size_t |
| 16 |
#include <stdint.h> // uint8_t, uint32_t, uintptr_t |
| 17 |
|
| 18 |
#include "jstypes.h" // JS_PUBLIC_API |
| 19 |
|
| 20 |
#include "jit/IonTypes.h" // CHECK_OSIPOINT_REGISTERS |
| 21 |
#include "jit/JSJitFrameIter.h" // js::jit::{JSJitFrameIter,RInstructionResults} |
| 22 |
#ifdef CHECK_OSIPOINT_REGISTERS |
| 23 |
# include "jit/Registers.h" // js::jit::RegisterDump |
| 24 |
#endif |
| 25 |
#include "jit/RematerializedFrame.h" // js::jit::RematerializedFrame |
| 26 |
#include "js/GCVector.h" // JS::GCVector |
| 27 |
#include "js/HashTable.h" // js::HashMap |
| 28 |
#include "js/UniquePtr.h" // js::UniquePtr |
| 29 |
#include "vm/Activation.h" // js::Activation |
| 30 |
#include "wasm/WasmCodegenTypes.h" // js::wasm::TrapData |
| 31 |
#include "wasm/WasmConstants.h" // js::wasm::Trap |
| 32 |
#include "wasm/WasmFrame.h" // js::wasm::Frame |
| 33 |
#include "wasm/WasmFrameIter.h" // js::wasm::{ExitReason,RegisterState,WasmFrameIter} |
| 34 |
|
| 35 |
struct JS_PUBLIC_API JSContext; |
| 36 |
class JS_PUBLIC_API JSTracer; |
| 37 |
|
| 38 |
namespace js { |
| 39 |
|
| 40 |
namespace jit { |
| 41 |
|
| 42 |
class BailoutFrameInfo; |
| 43 |
|
| 44 |
enum class IsLeavingFrame { No, Yes }; |
| 45 |
|
| 46 |
// A JitActivation is used for frames running in Baseline or Ion. |
| 47 |
class JitActivation : public Activation { |
| 48 |
// If Baseline, Ion or Wasm code is on the stack, and has called into C++, |
| 49 |
// this will be aligned to an ExitFrame. The last bit indicates if it's a |
| 50 |
// wasm frame (bit set to wasm::ExitOrJitEntryFPTag) or not |
| 51 |
// (bit set to ~wasm::ExitOrJitEntryFPTag). |
| 52 |
uint8_t* packedExitFP_; |
| 53 |
|
| 54 |
// When hasWasmExitFP(), encodedWasmExitReason_ holds ExitReason. |
| 55 |
uint32_t encodedWasmExitReason_; |
| 56 |
|
| 57 |
JitActivation* prevJitActivation_; |
| 58 |
|
| 59 |
// Rematerialized Ion frames which has info copied out of snapshots. Maps |
| 60 |
// frame pointers (i.e. packedExitFP_) to a vector of rematerializations of |
| 61 |
// all inline frames associated with that frame. |
| 62 |
// |
| 63 |
// This table is lazily initialized by calling getRematerializedFrame. |
| 64 |
using RematerializedFrameVector = |
| 65 |
JS::GCVector<js::UniquePtr<RematerializedFrame>>; |
| 66 |
using RematerializedFrameTable = |
| 67 |
js::HashMap<uint8_t*, RematerializedFrameVector>; |
| 68 |
js::UniquePtr<RematerializedFrameTable> rematerializedFrames_; |
| 69 |
|
| 70 |
// This vector is used to remember the outcome of the evaluation of recover |
| 71 |
// instructions. |
| 72 |
// |
| 73 |
// RInstructionResults are appended into this vector when Snapshot values |
| 74 |
// have to be read, or when the evaluation has to run before some mutating |
| 75 |
// code. Each RInstructionResults belongs to one frame which has to bailout |
| 76 |
// as soon as we get back to it. |
| 77 |
using IonRecoveryMap = Vector<RInstructionResults, 1>; |
| 78 |
IonRecoveryMap ionRecovery_; |
| 79 |
|
| 80 |
// If we are bailing out from Ion, then this field should be a non-null |
| 81 |
// pointer which references the BailoutFrameInfo used to walk the inner |
| 82 |
// frames. This field is used for all newly constructed JSJitFrameIters to |
| 83 |
// read the innermost frame information from this bailout data instead of |
| 84 |
// reading it from the stack. |
| 85 |
BailoutFrameInfo* bailoutData_; |
| 86 |
|
| 87 |
// When profiling is enabled, these fields will be updated to reflect the |
| 88 |
// last pushed frame for this activation, and if that frame has been |
| 89 |
// left for a call, the native code site of the call. |
| 90 |
mozilla::Atomic<JitFrameLayout*, mozilla::Relaxed> lastProfilingFrame_; |
| 91 |
mozilla::Atomic<void*, mozilla::Relaxed> lastProfilingCallSite_; |
| 92 |
static_assert(sizeof(mozilla::Atomic<void*, mozilla::Relaxed>) == |
| 93 |
sizeof(void*), |
| 94 |
"Atomic should have same memory format as underlying type."); |
| 95 |
|
| 96 |
// When wasm traps, the signal handler records some data for unwinding |
| 97 |
// purposes. Wasm code can't trap reentrantly. |
| 98 |
mozilla::Maybe<wasm::TrapData> wasmTrapData_; |
| 99 |
|
| 100 |
// Keeps the trapping code alive while the trap is handled. With tail calls |
| 101 |
// the trapping frame may already be unwound, so no wasm::Frame keeps it alive |
| 102 |
// and a GC (e.g. while building the RuntimeError) could otherwise free the |
| 103 |
// code segment we are still executing in. |
| 104 |
RefPtr<const wasm::Code> wasmTrapCode_; |
| 105 |
|
| 106 |
#ifdef CHECK_OSIPOINT_REGISTERS |
| 107 |
protected: |
| 108 |
// Used to verify that live registers don't change between a VM call and |
| 109 |
// the OsiPoint that follows it. Protected to silence Clang warning. |
| 110 |
uint32_t checkRegs_ = 0; |
| 111 |
RegisterDump regs_; |
| 112 |
#endif |
| 113 |
|
| 114 |
public: |
| 115 |
explicit JitActivation(JSContext* cx); |
| 116 |
~JitActivation(); |
| 117 |
|
| 118 |
bool isProfiling() const { |
| 119 |
// All JitActivations can be profiled. |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
JitActivation* prevJitActivation() const { return prevJitActivation_; } |
| 124 |
static size_t offsetOfPrevJitActivation() { |
| 125 |
return offsetof(JitActivation, prevJitActivation_); |
| 126 |
} |
| 127 |
|
| 128 |
bool hasExitFP() const { return !!packedExitFP_; } |
| 129 |
uint8_t* jsOrWasmExitFP() const { |
| 130 |
if (hasWasmExitFP()) { |
| 131 |
return wasm::Frame::untagExitFP(packedExitFP_); |
| 132 |
} |
| 133 |
return packedExitFP_; |
| 134 |
} |
| 135 |
static size_t offsetOfPackedExitFP() { |
| 136 |
return offsetof(JitActivation, packedExitFP_); |
| 137 |
} |
| 138 |
|
| 139 |
bool hasJSExitFP() const { return !hasWasmExitFP(); } |
| 140 |
|
| 141 |
uint8_t* jsExitFP() const { |
| 142 |
MOZ_ASSERT(hasJSExitFP()); |
| 143 |
return packedExitFP_; |
| 144 |
} |
| 145 |
void setJSExitFP(uint8_t* fp) { packedExitFP_ = fp; } |
| 146 |
|
| 147 |
uint8_t* packedExitFP() const { return packedExitFP_; } |
| 148 |
void setPackedExitFP(uint8_t* fp) { packedExitFP_ = fp; } |
| 149 |
|
| 150 |
#ifdef CHECK_OSIPOINT_REGISTERS |
| 151 |
void setCheckRegs(bool check) { checkRegs_ = check; } |
| 152 |
static size_t offsetOfCheckRegs() { |
| 153 |
return offsetof(JitActivation, checkRegs_); |
| 154 |
} |
| 155 |
static size_t offsetOfRegs() { return offsetof(JitActivation, regs_); } |
| 156 |
#endif |
| 157 |
|
| 158 |
// Look up a rematerialized frame keyed by the fp, rematerializing the |
| 159 |
// frame if one doesn't already exist. A frame can only be rematerialized |
| 160 |
// if an IonFrameIterator pointing to the nearest uninlined frame can be |
| 161 |
// provided, as values need to be read out of snapshots. |
| 162 |
// |
| 163 |
// The inlineDepth must be within bounds of the frame pointed to by iter. |
| 164 |
RematerializedFrame* getRematerializedFrame( |
| 165 |
JSContext* cx, const JSJitFrameIter& iter, size_t inlineDepth = 0, |
| 166 |
IsLeavingFrame leaving = IsLeavingFrame::No); |
| 167 |
|
| 168 |
// Look up a rematerialized frame by the fp. If inlineDepth is out of |
| 169 |
// bounds of what has been rematerialized, nullptr is returned. |
| 170 |
RematerializedFrame* lookupRematerializedFrame(uint8_t* top, |
| 171 |
size_t inlineDepth = 0); |
| 172 |
|
| 173 |
// Remove all rematerialized frames associated with the fp top from the |
| 174 |
// Debugger. |
| 175 |
void removeRematerializedFramesFromDebugger(JSContext* cx, uint8_t* top); |
| 176 |
|
| 177 |
bool hasRematerializedFrame(uint8_t* top, size_t inlineDepth = 0) { |
| 178 |
return !!lookupRematerializedFrame(top, inlineDepth); |
| 179 |
} |
| 180 |
|
| 181 |
// Remove a previous rematerialization by fp. |
| 182 |
void removeRematerializedFrame(uint8_t* top); |
| 183 |
|
| 184 |
void traceRematerializedFrames(JSTracer* trc); |
| 185 |
|
| 186 |
// Register the results of on Ion frame recovery. |
| 187 |
bool registerIonFrameRecovery(RInstructionResults&& results); |
| 188 |
|
| 189 |
// Return the pointer to the Ion frame recovery, if it is already registered. |
| 190 |
RInstructionResults* maybeIonFrameRecovery(JitFrameLayout* fp); |
| 191 |
|
| 192 |
// If an Ion frame recovery exists for the |fp| frame exists, then remove it |
| 193 |
// from the activation. |
| 194 |
void removeIonFrameRecovery(JitFrameLayout* fp); |
| 195 |
|
| 196 |
void traceIonRecovery(JSTracer* trc); |
| 197 |
|
| 198 |
// Return the bailout information if it is registered. |
| 199 |
const BailoutFrameInfo* bailoutData() const { return bailoutData_; } |
| 200 |
|
| 201 |
// Register the bailout data when it is constructed. |
| 202 |
void setBailoutData(BailoutFrameInfo* bailoutData); |
| 203 |
|
| 204 |
// Unregister the bailout data when the frame is reconstructed. |
| 205 |
void cleanBailoutData(); |
| 206 |
|
| 207 |
static size_t offsetOfLastProfilingFrame() { |
| 208 |
return offsetof(JitActivation, lastProfilingFrame_); |
| 209 |
} |
| 210 |
JitFrameLayout* lastProfilingFrame() { return lastProfilingFrame_; } |
| 211 |
void setLastProfilingFrame(JitFrameLayout* ptr) { lastProfilingFrame_ = ptr; } |
| 212 |
|
| 213 |
static size_t offsetOfLastProfilingCallSite() { |
| 214 |
return offsetof(JitActivation, lastProfilingCallSite_); |
| 215 |
} |
| 216 |
void* lastProfilingCallSite() { return lastProfilingCallSite_; } |
| 217 |
void setLastProfilingCallSite(void* ptr) { lastProfilingCallSite_ = ptr; } |
| 218 |
|
| 219 |
// WebAssembly specific attributes. |
| 220 |
bool hasWasmExitFP() const { return wasm::Frame::isExitFP(packedExitFP_); } |
| 221 |
wasm::Frame* wasmExitFP() const { |
| 222 |
MOZ_ASSERT(hasWasmExitFP()); |
| 223 |
return reinterpret_cast<wasm::Frame*>( |
| 224 |
wasm::Frame::untagExitFP(packedExitFP_)); |
| 225 |
} |
| 226 |
wasm::Instance* wasmExitInstance() const { |
| 227 |
return wasm::GetNearestEffectiveInstance(wasmExitFP()); |
| 228 |
} |
| 229 |
void setWasmExitFP(const wasm::Frame* fp) { |
| 230 |
if (fp) { |
| 231 |
MOZ_ASSERT(!wasm::Frame::isExitFP(fp)); |
| 232 |
packedExitFP_ = wasm::Frame::addExitFPTag(fp); |
| 233 |
MOZ_ASSERT(hasWasmExitFP()); |
| 234 |
} else { |
| 235 |
packedExitFP_ = nullptr; |
| 236 |
} |
| 237 |
} |
| 238 |
wasm::ExitReason wasmExitReason() const { |
| 239 |
MOZ_ASSERT(hasWasmExitFP()); |
| 240 |
return wasm::ExitReason::Decode(encodedWasmExitReason_); |
| 241 |
} |
| 242 |
static size_t offsetOfEncodedWasmExitReason() { |
| 243 |
return offsetof(JitActivation, encodedWasmExitReason_); |
| 244 |
} |
| 245 |
|
| 246 |
void startWasmTrap(wasm::Trap trap, const wasm::TrapSite& trapSite, |
| 247 |
const wasm::RegisterState& state); |
| 248 |
void finishWasmTrap(); |
| 249 |
bool isWasmTrapping() const { return !!wasmTrapData_; } |
| 250 |
const wasm::TrapData& wasmTrapData() { return *wasmTrapData_; } |
| 251 |
}; |
| 252 |
|
| 253 |
// A filtering of the ActivationIterator to only stop at JitActivations. |
| 254 |
class JitActivationIterator : public ActivationIterator { |
| 255 |
void settle() { |
| 256 |
while (!done() && !activation_->isJit()) { |
| 257 |
ActivationIterator::operator++(); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
public: |
| 262 |
explicit JitActivationIterator(JSContext* cx) : ActivationIterator(cx) { |
| 263 |
settle(); |
| 264 |
} |
| 265 |
|
| 266 |
JitActivationIterator& operator++() { |
| 267 |
ActivationIterator::operator++(); |
| 268 |
settle(); |
| 269 |
return *this; |
| 270 |
} |
| 271 |
}; |
| 272 |
|
| 273 |
} // namespace jit |
| 274 |
|
| 275 |
} // namespace js |
| 276 |
|
| 277 |
#endif // vm_JitActivation_h |