Debian Codegraph @

js/src/jit/Snapshots.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 * 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_Snapshot_h
8 #define jit_Snapshot_h
9
10 #include "mozilla/Attributes.h"
11
12 #include "jit/CompactBuffer.h"
13 #include "jit/IonTypes.h"
14 #include "jit/Registers.h"
15 #include "js/AllocPolicy.h"
16 #include "js/HashTable.h"
17 #include "js/TypeDecls.h"
18
19 namespace js {
20 class JS_PUBLIC_API GenericPrinter;
21
22 namespace jit {
23
24 class RValueAllocation;
25
26 // A Recover Value Allocation mirror what is known at compiled time as being the
27 // MIRType and the LAllocation. This is read out of the snapshot to recover the
28 // value which would be there if this frame was an interpreter frame instead of
29 // an Ion frame.
30 //
31 // It is used with the SnapshotIterator to recover a Value from the stack,
32 // spilled registers or the list of constant of the compiled script.
33 //
34 // Unit tests are located in jsapi-tests/testJitRValueAlloc.cpp.
35 class RValueAllocation {
36 public:
37 // See RValueAllocation encoding in Snapshots.cpp
38 enum Mode {
39 CONSTANT = 0x00,
40 CST_UNDEFINED = 0x01,
41 CST_NULL = 0x02,
42 DOUBLE_REG = 0x03,
43 FLOAT32_REG = 0x04,
44 FLOAT32_STACK = 0x05,
45 #if defined(JS_NUNBOX32)
46 UNTYPED_REG_REG = 0x06,
47 UNTYPED_REG_STACK = 0x07,
48 UNTYPED_STACK_REG = 0x08,
49 UNTYPED_STACK_STACK = 0x09,
50 #elif defined(JS_PUNBOX64)
51 UNTYPED_REG = 0x06,
52 UNTYPED_STACK = 0x07,
53 #endif
54
55 // Recover instructions.
56 RECOVER_INSTRUCTION = 0x0a,
57 RI_WITH_DEFAULT_CST = 0x0b,
58
59 // IntPtr value.
60 INTPTR_CST = 0x0c,
61 INTPTR_REG = 0x0d,
62 INTPTR_STACK = 0x0e,
63 INTPTR_INT32_STACK = 0x0f,
64
65 // The JSValueType is packed in the Mode.
66 TYPED_REG_MIN = 0x10,
67 TYPED_REG_MAX = 0x1f,
68 TYPED_REG = TYPED_REG_MIN,
69
70 // The JSValueType is packed in the Mode.
71 TYPED_STACK_MIN = 0x20,
72 TYPED_STACK_MAX = 0x2f,
73 TYPED_STACK = TYPED_STACK_MIN,
74
75 // Int64 value.
76 INT64_CST = 0x30,
77 #if defined(JS_NUNBOX32)
78 INT64_REG_REG = 0x31,
79 INT64_REG_STACK = 0x32,
80 INT64_STACK_REG = 0x33,
81 INT64_STACK_STACK = 0x34,
82 #elif defined(JS_PUNBOX64)
83 INT64_REG = 0x31,
84 INT64_STACK = 0x32,
85 INT64_INT32_STACK = 0x33,
86 #endif
87
88 // This mask can be used with any other valid mode. When this flag is
89 // set on the mode, this inform the snapshot iterator that even if the
90 // allocation is readable, the content of if might be incomplete unless
91 // all side-effects are executed.
92 RECOVER_SIDE_EFFECT_MASK = 0x80,
93
94 // This mask represents the set of bits which can be used to encode a
95 // value in a snapshot. The mode is used to determine how to interpret
96 // the union of values and how to pack the value in memory.
97 MODE_BITS_MASK = 0x17f,
98
99 INVALID = 0x100,
100 };
101
102 enum { PACKED_TAG_MASK = 0x0f };
103
104 // See Payload encoding in Snapshots.cpp
105 enum PayloadType {
106 PAYLOAD_NONE,
107 PAYLOAD_INDEX,
108 PAYLOAD_STACK_OFFSET,
109 PAYLOAD_GPR,
110 PAYLOAD_FPU,
111 PAYLOAD_PACKED_TAG
112 };
113
114 struct Layout {
115 PayloadType type1;
116 PayloadType type2;
117 const char* name;
118 };
119
120 private:
121 Mode mode_;
122
123 // Additional information to recover the content of the allocation.
124 struct FloatRegisterBits {
125 uint32_t data;
126 bool operator==(const FloatRegisterBits& other) const {
127 return data == other.data;
128 }
129 uint32_t code() const { return data; }
130 const char* name() const {
131 FloatRegister tmp = FloatRegister::FromCode(data);
132 return tmp.name();
133 }
134 };
135
136 union Payload {
137 uint32_t index;
138 int32_t stackOffset;
139 Register gpr;
140 FloatRegisterBits fpu;
141 JSValueType type;
142
143 Payload() : index(0) {
144 static_assert(sizeof(index) == sizeof(Payload),
145 "All Payload bits are initialized.");
146 }
147 };
148
149 Payload arg1_;
150 Payload arg2_;
151
152 static Payload payloadOfIndex(uint32_t index) {
153 Payload p;
154 p.index = index;
155 return p;
156 }
157 static Payload payloadOfStackOffset(int32_t offset) {
158 Payload p;
159 p.stackOffset = offset;
160 return p;
161 }
162 static Payload payloadOfRegister(Register reg) {
163 Payload p;
164 p.gpr = reg;
165 return p;
166 }
167 static Payload payloadOfFloatRegister(FloatRegister reg) {
168 Payload p;
169 FloatRegisterBits b;
170 b.data = reg.code();
171 p.fpu = b;
172 return p;
173 }
174 static Payload payloadOfValueType(JSValueType type) {
175 Payload p;
176 p.type = type;
177 return p;
178 }
179
180 static const Layout& layoutFromMode(Mode mode);
181
182 static void readPayload(CompactBufferReader& reader, PayloadType t,
183 uint8_t* mode, Payload* p);
184 static void writePayload(CompactBufferWriter& writer, PayloadType t,
185 Payload p);
186 static void writePadding(CompactBufferWriter& writer);
187 #ifdef JS_JITSPEW
188 static void dumpPayload(GenericPrinter& out, PayloadType t, Payload p);
189 #endif
190 static bool equalPayloads(PayloadType t, Payload lhs, Payload rhs);
191
192 RValueAllocation(Mode mode, Payload a1, Payload a2)
193 : mode_(mode), arg1_(a1), arg2_(a2) {}
194
195 RValueAllocation(Mode mode, Payload a1) : mode_(mode), arg1_(a1) {
196 arg2_.index = 0;
197 }
198
199 explicit RValueAllocation(Mode mode) : mode_(mode) {
200 arg1_.index = 0;
201 arg2_.index = 0;
202 }
203
204 public:
205 RValueAllocation() : mode_(INVALID) {
206 arg1_.index = 0;
207 arg2_.index = 0;
208 }
209
210 // DOUBLE_REG
211 static RValueAllocation Double(FloatRegister reg) {
212 return RValueAllocation(DOUBLE_REG, payloadOfFloatRegister(reg));
213 }
214
215 // FLOAT32_REG or FLOAT32_STACK
216 static RValueAllocation Float32(FloatRegister reg) {
217 return RValueAllocation(FLOAT32_REG, payloadOfFloatRegister(reg));
218 }
219 static RValueAllocation Float32(int32_t offset) {
220 return RValueAllocation(FLOAT32_STACK, payloadOfStackOffset(offset));
221 }
222
223 // TYPED_REG or TYPED_STACK
224 static RValueAllocation Typed(JSValueType type, Register reg) {
225 MOZ_ASSERT(type != JSVAL_TYPE_DOUBLE && type != JSVAL_TYPE_MAGIC &&
226 type != JSVAL_TYPE_NULL && type != JSVAL_TYPE_UNDEFINED);
227 return RValueAllocation(TYPED_REG, payloadOfValueType(type),
228 payloadOfRegister(reg));
229 }
230 static RValueAllocation Typed(JSValueType type, int32_t offset) {
231 MOZ_ASSERT(type != JSVAL_TYPE_MAGIC && type != JSVAL_TYPE_NULL &&
232 type != JSVAL_TYPE_UNDEFINED);
233 return RValueAllocation(TYPED_STACK, payloadOfValueType(type),
234 payloadOfStackOffset(offset));
235 }
236
237 // UNTYPED
238 #if defined(JS_NUNBOX32)
239 static RValueAllocation Untyped(Register type, Register payload) {
240 return RValueAllocation(UNTYPED_REG_REG, payloadOfRegister(type),
241 payloadOfRegister(payload));
242 }
243
244 static RValueAllocation Untyped(Register type, int32_t payloadStackOffset) {
245 return RValueAllocation(UNTYPED_REG_STACK, payloadOfRegister(type),
246 payloadOfStackOffset(payloadStackOffset));
247 }
248
249 static RValueAllocation Untyped(int32_t typeStackOffset, Register payload) {
250 return RValueAllocation(UNTYPED_STACK_REG,
251 payloadOfStackOffset(typeStackOffset),
252 payloadOfRegister(payload));
253 }
254
255 static RValueAllocation Untyped(int32_t typeStackOffset,
256 int32_t payloadStackOffset) {
257 return RValueAllocation(UNTYPED_STACK_STACK,
258 payloadOfStackOffset(typeStackOffset),
259 payloadOfStackOffset(payloadStackOffset));
260 }
261
262 #elif defined(JS_PUNBOX64)
263 static RValueAllocation Untyped(Register reg) {
264 return RValueAllocation(UNTYPED_REG, payloadOfRegister(reg));
265 }
266
267 static RValueAllocation Untyped(int32_t stackOffset) {
268 return RValueAllocation(UNTYPED_STACK, payloadOfStackOffset(stackOffset));
269 }
270 #endif
271
272 // common constants.
273 static RValueAllocation Undefined() {
274 return RValueAllocation(CST_UNDEFINED);
275 }
276 static RValueAllocation Null() { return RValueAllocation(CST_NULL); }
277
278 // CONSTANT's index
279 static RValueAllocation ConstantPool(uint32_t index) {
280 return RValueAllocation(CONSTANT, payloadOfIndex(index));
281 }
282
283 // Recover instruction's index
284 static RValueAllocation RecoverInstruction(uint32_t index) {
285 return RValueAllocation(RECOVER_INSTRUCTION, payloadOfIndex(index));
286 }
287 static RValueAllocation RecoverInstruction(uint32_t riIndex,
288 uint32_t cstIndex) {
289 return RValueAllocation(RI_WITH_DEFAULT_CST, payloadOfIndex(riIndex),
290 payloadOfIndex(cstIndex));
291 }
292
293 // IntPtr
294 #if !defined(JS_64BIT)
295 static RValueAllocation IntPtrConstant(uint32_t index) {
296 return RValueAllocation(INTPTR_CST, payloadOfIndex(index));
297 }
298 #else
299 static RValueAllocation IntPtrConstant(uint32_t lowIndex,
300 uint32_t highIndex) {
301 return RValueAllocation(INTPTR_CST, payloadOfIndex(lowIndex),
302 payloadOfIndex(highIndex));
303 }
304 #endif
305 static RValueAllocation IntPtr(Register reg) {
306 return RValueAllocation(INTPTR_REG, payloadOfRegister(reg));
307 }
308 static RValueAllocation IntPtr(int32_t offset) {
309 return RValueAllocation(INTPTR_STACK, payloadOfStackOffset(offset));
310 }
311 static RValueAllocation IntPtrInt32(int32_t offset) {
312 return RValueAllocation(INTPTR_INT32_STACK, payloadOfStackOffset(offset));
313 }
314
315 // Int64
316 static RValueAllocation Int64Constant(uint32_t lowIndex, uint32_t highIndex) {
317 return RValueAllocation(INT64_CST, payloadOfIndex(lowIndex),
318 payloadOfIndex(highIndex));
319 }
320 #if defined(JS_NUNBOX32)
321 static RValueAllocation Int64(Register low, Register high) {
322 return RValueAllocation(INT64_REG_REG, payloadOfRegister(low),
323 payloadOfRegister(high));
324 }
325
326 static RValueAllocation Int64(Register low, int32_t highStackOffset) {
327 return RValueAllocation(INT64_REG_STACK, payloadOfRegister(low),
328 payloadOfStackOffset(highStackOffset));
329 }
330
331 static RValueAllocation Int64(int32_t lowStackOffset, Register high) {
332 return RValueAllocation(INT64_STACK_REG,
333 payloadOfStackOffset(lowStackOffset),
334 payloadOfRegister(high));
335 }
336
337 static RValueAllocation Int64(int32_t lowStackOffset,
338 int32_t highStackOffset) {
339 return RValueAllocation(INT64_STACK_STACK,
340 payloadOfStackOffset(lowStackOffset),
341 payloadOfStackOffset(highStackOffset));
342 }
343
344 #elif defined(JS_PUNBOX64)
345 static RValueAllocation Int64(Register reg) {
346 return RValueAllocation(INT64_REG, payloadOfRegister(reg));
347 }
348
349 static RValueAllocation Int64(int32_t stackOffset) {
350 return RValueAllocation(INT64_STACK, payloadOfStackOffset(stackOffset));
351 }
352
353 static RValueAllocation Int64Int32(int32_t stackOffset) {
354 return RValueAllocation(INT64_INT32_STACK,
355 payloadOfStackOffset(stackOffset));
356 }
357 #endif
358
359 void setNeedSideEffect() {
360 MOZ_ASSERT(!needSideEffect() && mode_ != INVALID);
361 mode_ = Mode(mode_ | RECOVER_SIDE_EFFECT_MASK);
362 }
363
364 void writeHeader(CompactBufferWriter& writer, JSValueType type,
365 uint32_t regCode) const;
366
367 public:
368 static RValueAllocation read(CompactBufferReader& reader);
369 void write(CompactBufferWriter& writer) const;
370
371 public:
372 bool valid() const { return mode_ != INVALID; }
373 Mode mode() const { return Mode(mode_ & MODE_BITS_MASK); }
374 bool needSideEffect() const { return mode_ & RECOVER_SIDE_EFFECT_MASK; }
375
376 uint32_t index() const {
377 MOZ_ASSERT(layoutFromMode(mode()).type1 == PAYLOAD_INDEX);
378 return arg1_.index;
379 }
380 int32_t stackOffset() const {
381 MOZ_ASSERT(layoutFromMode(mode()).type1 == PAYLOAD_STACK_OFFSET);
382 return arg1_.stackOffset;
383 }
384 Register reg() const {
385 MOZ_ASSERT(layoutFromMode(mode()).type1 == PAYLOAD_GPR);
386 return arg1_.gpr;
387 }
388 FloatRegister fpuReg() const {
389 MOZ_ASSERT(layoutFromMode(mode()).type1 == PAYLOAD_FPU);
390 FloatRegisterBits b = arg1_.fpu;
391 return FloatRegister::FromCode(b.data);
392 }
393 JSValueType knownType() const {
394 MOZ_ASSERT(layoutFromMode(mode()).type1 == PAYLOAD_PACKED_TAG);
395 return arg1_.type;
396 }
397
398 uint32_t index2() const {
399 MOZ_ASSERT(layoutFromMode(mode()).type2 == PAYLOAD_INDEX);
400 return arg2_.index;
401 }
402 int32_t stackOffset2() const {
403 MOZ_ASSERT(layoutFromMode(mode()).type2 == PAYLOAD_STACK_OFFSET);
404 return arg2_.stackOffset;
405 }
406 Register reg2() const {
407 MOZ_ASSERT(layoutFromMode(mode()).type2 == PAYLOAD_GPR);
408 return arg2_.gpr;
409 }
410
411 public:
412 #ifdef JS_JITSPEW
413 void dump(GenericPrinter& out) const;
414 #endif
415
416 bool operator==(const RValueAllocation& rhs) const {
417 // Note, this equality compares the verbatim content of the payload,
418 // which is made possible because we ensure that the payload content is
419 // fully initialized during the creation.
420 static_assert(sizeof(int32_t) == sizeof(Payload),
421 "All Payload bits are compared.");
422 return mode_ == rhs.mode_ && arg1_.index == rhs.arg1_.index &&
423 arg2_.index == rhs.arg2_.index;
424 }
425
426 HashNumber hash() const;
427
428 struct Hasher {
429 using Key = RValueAllocation;
430 using Lookup = Key;
431 static HashNumber hash(const Lookup& v) { return v.hash(); }
432 static bool match(const Key& k, const Lookup& l) { return k == l; }
433 };
434 };
435
436 class RecoverWriter;
437
438 // Collects snapshots in a contiguous buffer, which is copied into IonScript
439 // memory after code generation.
440 class SnapshotWriter {
441 CompactBufferWriter writer_;
442 CompactBufferWriter allocWriter_;
443
444 // Map RValueAllocations to an offset in the allocWriter_ buffer. This is
445 // useful as value allocations are repeated frequently.
446 using RVA = RValueAllocation;
447 using RValueAllocMap = HashMap<RVA, uint32_t, RVA::Hasher, SystemAllocPolicy>;
448 RValueAllocMap allocMap_;
449
450 // This is only used to assert sanity.
451 uint32_t allocWritten_;
452
453 // Used to report size of the snapshot in the spew messages.
454 SnapshotOffset lastStart_;
455
456 public:
457 SnapshotWriter();
458
459 SnapshotOffset startSnapshot(RecoverOffset recoverOffset, BailoutKind kind);
460 #ifdef TRACK_SNAPSHOTS
461 void trackSnapshot(uint32_t pcOpcode, uint32_t mirOpcode, uint32_t mirId,
462 uint32_t lirOpcode, uint32_t lirId);
463 #endif
464 [[nodiscard]] bool add(const RValueAllocation& slot);
465
466 uint32_t allocWritten() const { return allocWritten_; }
467 void endSnapshot();
468
469 bool oom() const {
470 return writer_.oom() || writer_.length() >= MAX_BUFFER_SIZE ||
471 allocWriter_.oom() || allocWriter_.length() >= MAX_BUFFER_SIZE;
472 }
473
474 size_t listSize() const { return writer_.length(); }
475 const uint8_t* listBuffer() const { return writer_.buffer(); }
476
477 size_t RVATableSize() const { return allocWriter_.length(); }
478 const uint8_t* RVATableBuffer() const { return allocWriter_.buffer(); }
479 };
480
481 class MNode;
482
483 class RecoverWriter {
484 CompactBufferWriter writer_;
485
486 uint32_t instructionCount_;
487 uint32_t instructionsWritten_;
488
489 public:
490 RecoverOffset startRecover(uint32_t instructionCount);
491
492 void writeInstruction(const MNode* rp);
493
494 void endRecover();
495
496 size_t size() const { return writer_.length(); }
497 const uint8_t* buffer() const { return writer_.buffer(); }
498
499 bool oom() const {
500 return writer_.oom() || writer_.length() >= MAX_BUFFER_SIZE;
501 }
502 };
503
504 class RecoverReader;
505
506 // A snapshot reader reads the entries out of the compressed snapshot buffer in
507 // a script. These entries describe the equivalent interpreter frames at a given
508 // position in JIT code. Each entry is an Ion's value allocations, used to
509 // recover the corresponding Value from an Ion frame.
510 class SnapshotReader {
511 CompactBufferReader reader_;
512 CompactBufferReader allocReader_;
513 const uint8_t* allocTable_;
514
515 BailoutKind bailoutKind_;
516 uint32_t allocRead_; // Number of slots that have been read.
517 RecoverOffset recoverOffset_; // Offset of the recover instructions.
518
519 #ifdef TRACK_SNAPSHOTS
520 private:
521 uint32_t pcOpcode_;
522 uint32_t mirOpcode_;
523 uint32_t mirId_;
524 uint32_t lirOpcode_;
525 uint32_t lirId_;
526
527 public:
528 void readTrackSnapshot();
529 void spewBailingFrom() const;
530 #endif
531
532 private:
533 void readSnapshotHeader();
534 uint32_t readAllocationIndex();
535
536 public:
537 SnapshotReader(const uint8_t* snapshots, uint32_t offset,
538 uint32_t RVATableSize, uint32_t listSize);
539
540 RValueAllocation readAllocation();
541 void skipAllocation() { readAllocationIndex(); }
542
543 BailoutKind bailoutKind() const { return bailoutKind_; }
544 RecoverOffset recoverOffset() const { return recoverOffset_; }
545
546 uint32_t numAllocationsRead() const { return allocRead_; }
547 void resetNumAllocationsRead() { allocRead_ = 0; }
548 };
549
550 class MOZ_NON_PARAM RInstructionStorage {
551 static constexpr size_t Size = 4 * sizeof(uint32_t);
552
553 // This presumes all RInstructionStorage are safely void*-alignable.
554 // RInstruction::readRecoverData asserts that no RInstruction subclass
555 // has stricter alignment requirements than RInstructionStorage.
556 static constexpr size_t Alignment = alignof(void*);
557
558 alignas(Alignment) unsigned char mem[Size];
559
560 public:
561 const void* addr() const { return mem; }
562 void* addr() { return mem; }
563
564 RInstructionStorage() = default;
565
566 // Making a copy of raw bytes holding a RInstruction instance would be a
567 // strict aliasing violation: see bug 1269319 for an instance of bytewise
568 // copying having caused crashes.
569 RInstructionStorage(const RInstructionStorage&) = delete;
570 RInstructionStorage& operator=(const RInstructionStorage& other) = delete;
571 };
572
573 class RInstruction;
574
575 class RecoverReader {
576 CompactBufferReader reader_;
577
578 // Number of encoded instructions.
579 uint32_t numInstructions_;
580
581 // Number of instruction read.
582 uint32_t numInstructionsRead_;
583
584 // Space is reserved as part of the RecoverReader to avoid allocations of
585 // data which is needed to decode the current instruction.
586 RInstructionStorage rawData_;
587
588 private:
589 void readRecoverHeader();
590 void readInstruction();
591
592 public:
593 RecoverReader(SnapshotReader& snapshot, const uint8_t* recovers,
594 uint32_t size);
595 explicit RecoverReader(const RecoverReader& rr);
596 RecoverReader& operator=(const RecoverReader& rr);
597
598 uint32_t numInstructions() const { return numInstructions_; }
599 uint32_t numInstructionsRead() const { return numInstructionsRead_; }
600
601 bool moreInstructions() const {
602 return numInstructionsRead_ < numInstructions_;
603 }
604 void nextInstruction() { readInstruction(); }
605
606 const RInstruction* instruction() const {
607 return reinterpret_cast<const RInstruction*>(rawData_.addr());
608 }
609 };
610
611 } // namespace jit
612 } // namespace js
613
614 #endif /* jit_Snapshot_h */