| 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 "vm/JitActivation.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" // MOZ_ASSERT, MOZ_RELEASE_ASSERT |
| 10 |
|
| 11 |
#include <stddef.h> // size_t |
| 12 |
#include <stdint.h> // uint8_t, uint32_t |
| 13 |
#include <utility> // std::move |
| 14 |
|
| 15 |
#include "debugger/DebugAPI.h" // js::DebugAPI |
| 16 |
#include "jit/Invalidation.h" // js::jit::Invalidate |
| 17 |
#include "jit/JSJitFrameIter.h" // js::jit::InlineFrameIterator |
| 18 |
#include "jit/RematerializedFrame.h" // js::jit::RematerializedFrame |
| 19 |
#include "js/AllocPolicy.h" // js::ReportOutOfMemory |
| 20 |
#include "vm/EnvironmentObject.h" // js::DebugEnvironments |
| 21 |
#include "vm/JSContext.h" // JSContext |
| 22 |
#include "vm/Realm.h" // js::AutoRealmUnchecked |
| 23 |
#include "wasm/WasmCode.h" // js::wasm::Code |
| 24 |
#include "wasm/WasmConstants.h" // js::wasm::Trap |
| 25 |
#include "wasm/WasmFrameIter.h" // js::wasm::{RegisterState,StartUnwinding,UnwindState} |
| 26 |
#include "wasm/WasmInstance.h" // js::wasm::Instance |
| 27 |
#include "wasm/WasmProcess.h" // js::wasm::LookupCode |
| 28 |
|
| 29 |
#include "vm/Realm-inl.h" // js::~AutoRealm |
| 30 |
|
| 31 |
class JS_PUBLIC_API JSTracer; |
| 32 |
|
| 33 |
js::jit::JitActivation::JitActivation(JSContext* cx) |
| 34 |
: Activation(cx, Jit), |
| 35 |
packedExitFP_(nullptr), |
| 36 |
encodedWasmExitReason_(0), |
| 37 |
prevJitActivation_(cx->jitActivation), |
| 38 |
ionRecovery_(cx), |
| 39 |
bailoutData_(nullptr), |
| 40 |
lastProfilingFrame_(nullptr), |
| 41 |
lastProfilingCallSite_(nullptr) { |
| 42 |
cx->jitActivation = this; |
| 43 |
registerProfiling(); |
| 44 |
} |
| 45 |
|
| 46 |
js::jit::JitActivation::~JitActivation() { |
| 47 |
if (isProfiling()) { |
| 48 |
unregisterProfiling(); |
| 49 |
} |
| 50 |
cx_->jitActivation = prevJitActivation_; |
| 51 |
|
| 52 |
// All reocvered value are taken from activation during the bailout. |
| 53 |
MOZ_ASSERT(ionRecovery_.empty()); |
| 54 |
|
| 55 |
// The BailoutFrameInfo should have unregistered itself from the |
| 56 |
// JitActivations. |
| 57 |
MOZ_ASSERT(!bailoutData_); |
| 58 |
|
| 59 |
// Traps get handled immediately. |
| 60 |
MOZ_ASSERT(!isWasmTrapping()); |
| 61 |
|
| 62 |
// Rematerialized frames must have been removed by either the bailout code or |
| 63 |
// the exception handler. |
| 64 |
MOZ_ASSERT_IF(rematerializedFrames_, rematerializedFrames_->empty()); |
| 65 |
} |
| 66 |
|
| 67 |
void js::jit::JitActivation::setBailoutData( |
| 68 |
jit::BailoutFrameInfo* bailoutData) { |
| 69 |
MOZ_ASSERT(!bailoutData_); |
| 70 |
bailoutData_ = bailoutData; |
| 71 |
} |
| 72 |
|
| 73 |
void js::jit::JitActivation::cleanBailoutData() { |
| 74 |
MOZ_ASSERT(bailoutData_); |
| 75 |
bailoutData_ = nullptr; |
| 76 |
} |
| 77 |
|
| 78 |
void js::jit::JitActivation::removeRematerializedFrame(uint8_t* top) { |
| 79 |
if (!rematerializedFrames_) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
if (RematerializedFrameTable::Ptr p = rematerializedFrames_->lookup(top)) { |
| 84 |
rematerializedFrames_->remove(p); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
js::jit::RematerializedFrame* js::jit::JitActivation::getRematerializedFrame( |
| 89 |
JSContext* cx, const JSJitFrameIter& iter, size_t inlineDepth, |
| 90 |
IsLeavingFrame leaving) { |
| 91 |
MOZ_ASSERT(iter.activation() == this); |
| 92 |
MOZ_ASSERT(iter.isIonScripted()); |
| 93 |
|
| 94 |
if (!rematerializedFrames_) { |
| 95 |
rematerializedFrames_ = cx->make_unique<RematerializedFrameTable>(cx); |
| 96 |
if (!rematerializedFrames_) { |
| 97 |
return nullptr; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
uint8_t* top = iter.fp(); |
| 102 |
RematerializedFrameTable::AddPtr p = rematerializedFrames_->lookupForAdd(top); |
| 103 |
if (!p) { |
| 104 |
RematerializedFrameVector frames(cx); |
| 105 |
|
| 106 |
// The unit of rematerialization is an uninlined frame and its inlined |
| 107 |
// frames. Since inlined frames do not exist outside of snapshots, it |
| 108 |
// is impossible to synchronize their rematerialized copies to |
| 109 |
// preserve identity. Therefore, we always rematerialize an uninlined |
| 110 |
// frame and all its inlined frames at once. |
| 111 |
InlineFrameIterator inlineIter(cx, &iter); |
| 112 |
|
| 113 |
// We can run recover instructions without invalidating if we're always |
| 114 |
// leaving the frame. |
| 115 |
MaybeReadFallback::FallbackConsequence consequence = |
| 116 |
MaybeReadFallback::Fallback_Invalidate; |
| 117 |
if (leaving == IsLeavingFrame::Yes) { |
| 118 |
consequence = MaybeReadFallback::Fallback_DoNothing; |
| 119 |
} |
| 120 |
MaybeReadFallback recover(cx, this, &iter, consequence); |
| 121 |
|
| 122 |
// Frames are often rematerialized with the cx inside a Debugger's |
| 123 |
// realm. To recover slots and to create CallObjects, we need to |
| 124 |
// be in the script's realm. |
| 125 |
AutoRealmUnchecked ar(cx, iter.script()->realm()); |
| 126 |
|
| 127 |
// The Ion frame must be invalidated to ensure the rematerialized frame will |
| 128 |
// be removed by the bailout code or the exception handler. If we're always |
| 129 |
// leaving the frame, the caller is responsible for cleaning up the |
| 130 |
// rematerialized frame. |
| 131 |
if (leaving == IsLeavingFrame::No && !iter.checkInvalidation()) { |
| 132 |
jit::Invalidate(cx, iter.script()); |
| 133 |
} |
| 134 |
|
| 135 |
if (!RematerializedFrame::RematerializeInlineFrames(cx, top, inlineIter, |
| 136 |
recover, frames)) { |
| 137 |
return nullptr; |
| 138 |
} |
| 139 |
|
| 140 |
if (!rematerializedFrames_->add(p, top, std::move(frames))) { |
| 141 |
ReportOutOfMemory(cx); |
| 142 |
return nullptr; |
| 143 |
} |
| 144 |
|
| 145 |
// See comment in unsetPrevUpToDateUntil. |
| 146 |
DebugEnvironments::unsetPrevUpToDateUntil(cx, |
| 147 |
p->value()[inlineDepth].get()); |
| 148 |
} |
| 149 |
|
| 150 |
return p->value()[inlineDepth].get(); |
| 151 |
} |
| 152 |
|
| 153 |
js::jit::RematerializedFrame* js::jit::JitActivation::lookupRematerializedFrame( |
| 154 |
uint8_t* top, size_t inlineDepth) { |
| 155 |
if (!rematerializedFrames_) { |
| 156 |
return nullptr; |
| 157 |
} |
| 158 |
if (RematerializedFrameTable::Ptr p = rematerializedFrames_->lookup(top)) { |
| 159 |
return inlineDepth < p->value().length() ? p->value()[inlineDepth].get() |
| 160 |
: nullptr; |
| 161 |
} |
| 162 |
return nullptr; |
| 163 |
} |
| 164 |
|
| 165 |
void js::jit::JitActivation::removeRematerializedFramesFromDebugger( |
| 166 |
JSContext* cx, uint8_t* top) { |
| 167 |
// Ion bailout can fail due to overrecursion and OOM. In such cases we |
| 168 |
// cannot honor any further Debugger hooks on the frame, and need to |
| 169 |
// ensure that its Debugger.Frame entry is cleaned up. |
| 170 |
if (!cx->realm()->isDebuggee() || !rematerializedFrames_) { |
| 171 |
return; |
| 172 |
} |
| 173 |
if (RematerializedFrameTable::Ptr p = rematerializedFrames_->lookup(top)) { |
| 174 |
for (uint32_t i = 0; i < p->value().length(); i++) { |
| 175 |
DebugAPI::handleUnrecoverableIonBailoutError(cx, p->value()[i].get()); |
| 176 |
} |
| 177 |
rematerializedFrames_->remove(p); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
void js::jit::JitActivation::traceRematerializedFrames(JSTracer* trc) { |
| 182 |
if (!rematerializedFrames_) { |
| 183 |
return; |
| 184 |
} |
| 185 |
for (RematerializedFrameTable::Enum e(*rematerializedFrames_); !e.empty(); |
| 186 |
e.popFront()) { |
| 187 |
e.front().value().trace(trc); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
bool js::jit::JitActivation::registerIonFrameRecovery( |
| 192 |
RInstructionResults&& results) { |
| 193 |
// Check that there is no entry in the vector yet. |
| 194 |
MOZ_ASSERT(!maybeIonFrameRecovery(results.frame())); |
| 195 |
if (!ionRecovery_.append(std::move(results))) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
js::jit::RInstructionResults* js::jit::JitActivation::maybeIonFrameRecovery( |
| 203 |
JitFrameLayout* fp) { |
| 204 |
for (RInstructionResults* it = ionRecovery_.begin(); it != ionRecovery_.end(); |
| 205 |
it++) { |
| 206 |
if (it->frame() == fp) { |
| 207 |
return it; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
return nullptr; |
| 212 |
} |
| 213 |
|
| 214 |
void js::jit::JitActivation::removeIonFrameRecovery(JitFrameLayout* fp) { |
| 215 |
RInstructionResults* elem = maybeIonFrameRecovery(fp); |
| 216 |
if (!elem) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
ionRecovery_.erase(elem); |
| 221 |
} |
| 222 |
|
| 223 |
void js::jit::JitActivation::traceIonRecovery(JSTracer* trc) { |
| 224 |
for (RInstructionResults* it = ionRecovery_.begin(); it != ionRecovery_.end(); |
| 225 |
it++) { |
| 226 |
it->trace(trc); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
void js::jit::JitActivation::startWasmTrap(wasm::Trap trap, |
| 231 |
const wasm::TrapSite& trapSite, |
| 232 |
const wasm::RegisterState& state) { |
| 233 |
MOZ_ASSERT(!isWasmTrapping()); |
| 234 |
|
| 235 |
bool unwound; |
| 236 |
wasm::UnwindState unwindState; |
| 237 |
MOZ_RELEASE_ASSERT(wasm::StartUnwinding(state, &unwindState, &unwound)); |
| 238 |
// With return calls, it is possible to not unwind when there is only an |
| 239 |
// entry left on the stack, e.g. the return call trampoline that is created |
| 240 |
// to restore realm before returning to the interpreter entry stub. |
| 241 |
MOZ_ASSERT_IF(unwound, trap == wasm::Trap::IndirectCallBadSig); |
| 242 |
|
| 243 |
void* pc = unwindState.pc; |
| 244 |
const wasm::Frame* fp = wasm::Frame::fromUntaggedWasmExitFP(unwindState.fp); |
| 245 |
|
| 246 |
const wasm::Code& code = wasm::GetNearestEffectiveInstance(fp)->code(); |
| 247 |
MOZ_RELEASE_ASSERT(&code == wasm::LookupCode(pc)); |
| 248 |
|
| 249 |
// Keep the trapping code alive across a GC. For an IndirectCallBadSig trap |
| 250 |
// reached through a tail call, this code can belong to a different instance |
| 251 |
// than the effective one above, whose frame has already been unwound. Nothing |
| 252 |
// else then keeps alive the code segment we are about to run the trap stub |
| 253 |
// in. |
| 254 |
wasmTrapCode_ = wasm::LookupCode(state.pc); |
| 255 |
MOZ_RELEASE_ASSERT(wasmTrapCode_); |
| 256 |
|
| 257 |
setWasmExitFP(fp); |
| 258 |
wasmTrapData_.emplace(); |
| 259 |
wasmTrapData_->resumePC = |
| 260 |
((uint8_t*)state.pc) + jit::WasmTrapInstructionLength; |
| 261 |
wasmTrapData_->unwoundPC = pc; |
| 262 |
wasmTrapData_->trap = trap; |
| 263 |
// If the frame was unwound, the source location must be recovered from the |
| 264 |
// callsite so that it is accurate. |
| 265 |
if (unwound) { |
| 266 |
wasm::CallSite site; |
| 267 |
MOZ_ALWAYS_TRUE(code.lookupCallSite(pc, &site)); |
| 268 |
wasmTrapData_->trapSite.bytecodeOffset = |
| 269 |
wasm::BytecodeOffset(site.lineOrBytecode()); |
| 270 |
wasmTrapData_->trapSite.inlinedCallerOffsets = site.inlinedCallerOffsets(); |
| 271 |
} else { |
| 272 |
wasmTrapData_->trapSite = trapSite; |
| 273 |
} |
| 274 |
wasmTrapData_->failedUnwindSignatureMismatch = |
| 275 |
!unwound && trap == wasm::Trap::IndirectCallBadSig; |
| 276 |
|
| 277 |
MOZ_ASSERT(isWasmTrapping()); |
| 278 |
} |
| 279 |
|
| 280 |
void js::jit::JitActivation::finishWasmTrap() { |
| 281 |
MOZ_ASSERT(isWasmTrapping()); |
| 282 |
// Keep wasmTrapCode_ alive: we are still executing inside this code and are |
| 283 |
// about to return into it, so releasing it now could free it. It is released |
| 284 |
// on the next trap or when the activation is destroyed. |
| 285 |
packedExitFP_ = nullptr; |
| 286 |
wasmTrapData_.reset(); |
| 287 |
MOZ_ASSERT(!isWasmTrapping()); |
| 288 |
} |