Debian Codegraph @

js/src/wasm/WasmFrameIter.h

raw
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 *
4 * Copyright 2014 Mozilla Foundation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #ifndef wasm_frame_iter_h
20 #define wasm_frame_iter_h
21
22 #include "js/ColumnNumber.h" // JS::TaggedColumnNumberOneOrigin
23 #include "js/ProfilingFrameIterator.h"
24 #include "js/TypeDecls.h"
25
26 #include "wasm/WasmCode.h" // For CodeBlockKind
27 #include "wasm/WasmCodegenTypes.h" // for BytecodeOffsetSpan
28
29 namespace js {
30
31 namespace jit {
32 class JitActivation;
33 class MacroAssembler;
34 struct Register;
35 enum class FrameType;
36 } // namespace jit
37
38 namespace wasm {
39
40 class CallIndirectId;
41 class Code;
42 class CodeRange;
43 class DebugFrame;
44 class Instance;
45 class Instance;
46
47 struct CallableOffsets;
48 struct ImportOffsets;
49 struct FuncOffsets;
50 struct Offsets;
51 class Frame;
52 class FrameWithInstances;
53
54 using RegisterState = JS::ProfilingFrameIterator::RegisterState;
55
56 // Iterates over a linear group of wasm frames of a single wasm JitActivation,
57 // called synchronously from C++ in the wasm thread. It will stop at the first
58 // frame that is not of the same kind, or at the end of an activation.
59 //
60 // If you want to handle every kind of frames (including JS jit frames), use
61 // JitFrameIter.
62
63 class WasmFrameIter {
64 //
65 // State that is constant for the entire wasm activation
66 //
67
68 jit::JitActivation* activation_ = nullptr;
69 bool isLeavingFrames_ = false;
70 bool enableInlinedFrames_ = false;
71
72 //
73 // State that is updated for every frame
74 //
75
76 const Code* code_ = nullptr;
77 uint32_t funcIndex_ = UINT32_MAX;
78 uint32_t lineOrBytecode_ = UINT32_MAX;
79 BytecodeOffsetSpan inlinedCallerOffsets_;
80 Frame* fp_ = nullptr;
81 Instance* instance_ = nullptr;
82 // The address of the next instruction that will execute in this frame, once
83 // control returns to this frame.
84 uint8_t* resumePCinCurrentFrame_ = nullptr;
85 // See wasm::TrapData for more information.
86 bool failedUnwindSignatureMismatch_ = false;
87 // Whether the current frame is on a different stack from the previous stack.
88 bool currentFrameStackSwitched_ = false;
89
90 //
91 // State that is found after we've unwound the entire wasm activation
92 //
93
94 // The address of the final wasm::Frame::returnAddress_. Only found once
95 // we've iterated over all wasm frames.
96 void** unwoundAddressOfReturnAddress_ = nullptr;
97 // The value of the final wasm::Frame::callerFP_. Only found once we've
98 // iterated over all wasm frames.
99 uint8_t* unwoundCallerFP_ = nullptr;
100 // Whether unwoundCallerFP_ is a JS JIT exit frame.
101 bool unwoundCallerFPIsJSJit_ = false;
102
103 void popFrame();
104
105 public:
106 // See comment above this class definition.
107 explicit WasmFrameIter(jit::JitActivation* activation, Frame* fp = nullptr);
108
109 // Iterate over frames from a known starting (fp, ra).
110 WasmFrameIter(FrameWithInstances* fp, void* returnAddress);
111
112 // Cause this WasmFrameIter to remove every popped from its JitActivation so
113 // that any other frame iteration will not see it.
114 //
115 // This is a method instead of a parameter to the constructor because it
116 // needs to be enabled after FrameIter has already created the WasmFrameIter.
117 void setIsLeavingFrames() {
118 MOZ_ASSERT(activation_);
119 MOZ_ASSERT(!isLeavingFrames_);
120 isLeavingFrames_ = true;
121 }
122
123 // Visit inlined frames instead of only 'physical' frames. This is required
124 // to access source information.
125 void enableInlinedFrames() { enableInlinedFrames_ = true; }
126
127 //
128 // Iteration methods
129 //
130
131 void operator++();
132 bool done() const;
133
134 //
135 // Source information about the current frame
136 //
137
138 bool hasSourceInfo() const;
139 const char* filename() const;
140 const char16_t* displayURL() const;
141 bool mutedErrors() const;
142 JSAtom* functionDisplayAtom() const;
143 unsigned lineOrBytecode() const;
144 uint32_t funcIndex() const;
145 unsigned computeLine(JS::TaggedColumnNumberOneOrigin* column) const;
146
147 //
148 // Physical information about the current (not inlined) frame
149 //
150
151 // The instance that the function for this wasm frame is from.
152 Instance* instance() const {
153 MOZ_ASSERT(!done());
154 // Getting the instance always works even with inlining because we never
155 // inline across instances.
156 return instance_;
157 }
158
159 // The wasm function frame pointer.
160 Frame* frame() const {
161 MOZ_ASSERT(!done());
162 MOZ_ASSERT(!enableInlinedFrames_);
163 return fp_;
164 }
165
166 // Returns the address of the next instruction that will execute in this
167 // frame, once control returns to this frame.
168 uint8_t* resumePCinCurrentFrame() const {
169 MOZ_ASSERT(!done());
170 MOZ_ASSERT(!enableInlinedFrames_);
171 return resumePCinCurrentFrame_;
172 }
173
174 // Whether the current frame is on a different stack from the previous frame.
175 bool currentFrameStackSwitched() const {
176 MOZ_ASSERT(!done());
177 return currentFrameStackSwitched_;
178 }
179
180 //
181 // Debug information about the current frame
182 //
183
184 // Whether this frame has a debuggable wasm frame.
185 bool debugEnabled() const;
186
187 // The debuggable wasm frame, if any.
188 DebugFrame* debugFrame() const;
189
190 //
191 // Information for after we've unwound the entire wasm activation
192 //
193
194 // The address of the final wasm::Frame::returnAddress_.
195 void** unwoundAddressOfReturnAddress() const {
196 MOZ_ASSERT(done());
197 MOZ_ASSERT(unwoundAddressOfReturnAddress_);
198 return unwoundAddressOfReturnAddress_;
199 }
200
201 // The value of the final wasm::Frame::callerFP_.
202 uint8_t* unwoundCallerFP() const {
203 MOZ_ASSERT(done());
204 MOZ_ASSERT(unwoundCallerFP_);
205 return unwoundCallerFP_;
206 }
207
208 // Whether 'unwoundCallerFP' is for a JS JIT frame or not.
209 bool unwoundCallerFPIsJSJit() const {
210 MOZ_ASSERT(done());
211 MOZ_ASSERT_IF(unwoundCallerFPIsJSJit_, unwoundCallerFP_);
212 return unwoundCallerFPIsJSJit_;
213 }
214 };
215
216 enum class SymbolicAddress;
217
218 // An ExitReason describes the possible reasons for leaving compiled wasm
219 // code or the state of not having left compiled wasm code
220 // (ExitReason::None). It is either a known reason, or a enumeration to a native
221 // function that is used for better display in the profiler.
222 class ExitReason {
223 public:
224 enum class Fixed : uint32_t {
225 None, // default state, the pc is in wasm code
226 ImportJit, // fast-path call directly into JIT code
227 ImportInterp, // slow-path call into C++ Invoke()
228 BuiltinNative, // fast-path call directly into native C++ code
229 Trap, // call to trap handler
230 DebugStub, // call to debug stub
231 RequestTierUp // call to request tier-2 compilation
232 };
233
234 private:
235 uint32_t payload_;
236
237 ExitReason() : ExitReason(Fixed::None) {}
238
239 public:
240 MOZ_IMPLICIT ExitReason(Fixed exitReason)
241 : payload_(0x0 | (uint32_t(exitReason) << 1)) {
242 MOZ_ASSERT(isFixed());
243 MOZ_ASSERT_IF(isNone(), payload_ == 0);
244 }
245
246 explicit ExitReason(SymbolicAddress sym)
247 : payload_(0x1 | (uint32_t(sym) << 1)) {
248 MOZ_ASSERT(uint32_t(sym) <= (UINT32_MAX << 1), "packing constraints");
249 MOZ_ASSERT(!isFixed());
250 }
251
252 static ExitReason Decode(uint32_t payload) {
253 ExitReason reason;
254 reason.payload_ = payload;
255 return reason;
256 }
257
258 static ExitReason None() { return ExitReason(ExitReason::Fixed::None); }
259
260 bool isFixed() const { return (payload_ & 0x1) == 0; }
261 bool isNone() const { return isFixed() && fixed() == Fixed::None; }
262 bool isNative() const {
263 return !isFixed() || fixed() == Fixed::BuiltinNative;
264 }
265
266 uint32_t encode() const { return payload_; }
267 Fixed fixed() const {
268 MOZ_ASSERT(isFixed());
269 return Fixed(payload_ >> 1);
270 }
271 SymbolicAddress symbolic() const {
272 MOZ_ASSERT(!isFixed());
273 return SymbolicAddress(payload_ >> 1);
274 }
275 };
276
277 // Iterates over the frames of a single wasm JitActivation, given an
278 // asynchronously-profiled thread's state.
279 class ProfilingFrameIterator {
280 public:
281 enum class Category {
282 Baseline,
283 Ion,
284 Other,
285 };
286
287 private:
288 const Code* code_;
289 const CodeRange* codeRange_;
290 Category category_;
291 uint8_t* callerFP_;
292 void* callerPC_;
293 void* stackAddress_;
294 // See JS::ProfilingFrameIterator::endStackAddress_ comment.
295 void* endStackAddress_ = nullptr;
296 uint8_t* unwoundJitCallerFP_;
297 ExitReason exitReason_;
298
299 void initFromExitFP(const Frame* fp);
300
301 public:
302 ProfilingFrameIterator();
303
304 // Start unwinding at a non-innermost activation that has necessarily been
305 // exited from wasm code (and thus activation.hasWasmExitFP).
306 explicit ProfilingFrameIterator(const jit::JitActivation& activation);
307
308 // Start unwinding at a group of wasm frames after unwinding an inner group
309 // of JSJit frames.
310 explicit ProfilingFrameIterator(const Frame* fp);
311
312 // Start unwinding at the innermost activation given the register state when
313 // the thread was suspended.
314 ProfilingFrameIterator(const jit::JitActivation& activation,
315 const RegisterState& state);
316
317 void operator++();
318
319 bool done() const {
320 MOZ_ASSERT_IF(!exitReason_.isNone(), codeRange_);
321 return !codeRange_;
322 }
323
324 void* stackAddress() const {
325 MOZ_ASSERT(!done());
326 return stackAddress_;
327 }
328 uint8_t* unwoundJitCallerFP() const {
329 MOZ_ASSERT(done());
330 return unwoundJitCallerFP_;
331 }
332 const char* label() const;
333
334 Category category() const;
335
336 void* endStackAddress() const { return endStackAddress_; }
337
338 // Convert a CodeBlockKind to a Category.
339 static ProfilingFrameIterator::Category categoryFromCodeBlock(
340 CodeBlockKind kind) {
341 if (kind == CodeBlockKind::BaselineTier) {
342 return ProfilingFrameIterator::Category::Baseline;
343 }
344 if (kind == CodeBlockKind::OptimizedTier) {
345 return ProfilingFrameIterator::Category::Ion;
346 }
347 return ProfilingFrameIterator::Category::Other;
348 }
349 };
350
351 // Prologue/epilogue code generation
352
353 void SetExitFP(jit::MacroAssembler& masm, ExitReason reason,
354 jit::Register scratch);
355 void ClearExitFP(jit::MacroAssembler& masm, jit::Register scratch);
356
357 void GenerateExitPrologue(jit::MacroAssembler& masm, unsigned framePushed,
358 ExitReason reason, CallableOffsets* offsets);
359 void GenerateExitEpilogue(jit::MacroAssembler& masm, unsigned framePushed,
360 ExitReason reason, CallableOffsets* offsets);
361
362 // Generate the most minimal possible prologue/epilogue: `push FP; FP := SP`
363 // and `pop FP; return` respectively.
364 void GenerateMinimalPrologue(jit::MacroAssembler& masm, uint32_t* entry);
365 void GenerateMinimalEpilogue(jit::MacroAssembler& masm, uint32_t* ret);
366
367 void GenerateJitExitPrologue(jit::MacroAssembler& masm, unsigned framePushed,
368 uint32_t fallbackOffset, ImportOffsets* offsets);
369 void GenerateJitExitEpilogue(jit::MacroAssembler& masm, unsigned framePushed,
370 CallableOffsets* offsets);
371
372 void GenerateJitEntryPrologue(jit::MacroAssembler& masm,
373 CallableOffsets* offsets);
374 void GenerateJitEntryEpilogue(jit::MacroAssembler& masm,
375 CallableOffsets* offsets);
376
377 void GenerateFunctionPrologue(jit::MacroAssembler& masm,
378 const CallIndirectId& callIndirectId,
379 const mozilla::Maybe<uint32_t>& tier1FuncIndex,
380 FuncOffsets* offsets);
381 void GenerateFunctionEpilogue(jit::MacroAssembler& masm, unsigned framePushed,
382 FuncOffsets* offsets);
383
384 // Iterates through frames for either possible cross-instance call or an entry
385 // stub to obtain instance that corresponds to the passed fp.
386 const Instance* GetNearestEffectiveInstance(const Frame* fp);
387 Instance* GetNearestEffectiveInstance(Frame* fp);
388
389 // Describes register state and associated code at a given call frame.
390
391 struct UnwindState {
392 uint8_t* fp;
393 void* pc;
394 const Code* code;
395 const CodeRange* codeRange;
396 UnwindState() : fp(nullptr), pc(nullptr), code(nullptr), codeRange(nullptr) {}
397 };
398
399 // Ensures the register state at a call site is consistent: pc must be in the
400 // code range of the code described by fp. This prevents issues when using
401 // the values of pc/fp, especially at call sites boundaries, where the state
402 // hasn't fully transitioned from the caller's to the callee's.
403 //
404 // unwoundCaller is set to true if we were in a transitional state and had to
405 // rewind to the caller's frame instead of the current frame.
406 //
407 // Returns true if it was possible to get to a clear state, or false if the
408 // frame should be ignored.
409
410 bool StartUnwinding(const RegisterState& registers, UnwindState* unwindState,
411 bool* unwoundCaller);
412
413 } // namespace wasm
414 } // namespace js
415
416 #endif // wasm_frame_iter_h