-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
function-body-decoder-impl.h
6251 lines (5871 loc) Β· 247 KB
/
function-body-decoder-impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#if !V8_ENABLE_WEBASSEMBLY
#error This header should only be included if WebAssembly is enabled.
#endif // !V8_ENABLE_WEBASSEMBLY
#ifndef V8_WASM_FUNCTION_BODY_DECODER_IMPL_H_
#define V8_WASM_FUNCTION_BODY_DECODER_IMPL_H_
// Do only include this header for implementing new Interface of the
// WasmFullDecoder.
#include <inttypes.h>
#include <optional>
#include "src/base/small-vector.h"
#include "src/base/strings.h"
#include "src/base/v8-fallthrough.h"
#include "src/strings/unicode.h"
#include "src/utils/bit-vector.h"
#include "src/wasm/decoder.h"
#include "src/wasm/function-body-decoder.h"
#include "src/wasm/value-type.h"
#include "src/wasm/wasm-features.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-opcodes.h"
#include "src/wasm/wasm-subtyping.h"
namespace v8 {
namespace internal {
namespace wasm {
struct WasmGlobal;
struct WasmTag;
#define TRACE(...) \
do { \
if (v8_flags.trace_wasm_decoder) PrintF(__VA_ARGS__); \
} while (false)
#define TRACE_INST_FORMAT " @%-8d #%-30s|"
// Return the evaluation of `condition` if validate==true, DCHECK that it's
// true and always return true otherwise.
#define VALIDATE(condition) \
(validate ? V8_LIKELY(condition) : [&] { \
DCHECK(condition); \
return true; \
}())
#define CHECK_PROTOTYPE_OPCODE(feat) \
DCHECK(this->module_->origin == kWasmOrigin); \
if (!VALIDATE(this->enabled_.has_##feat())) { \
this->DecodeError( \
"Invalid opcode 0x%02x (enable with --experimental-wasm-" #feat ")", \
opcode); \
return 0; \
} \
this->detected_->Add(kFeature_##feat);
static constexpr LoadType GetLoadType(WasmOpcode opcode) {
// Hard-code the list of load types. The opcodes are highly unlikely to
// ever change, and we have some checks here to guard against that.
static_assert(sizeof(LoadType) == sizeof(uint8_t), "LoadType is compact");
constexpr uint8_t kMinOpcode = kExprI32LoadMem;
constexpr uint8_t kMaxOpcode = kExprI64LoadMem32U;
constexpr LoadType kLoadTypes[] = {
LoadType::kI32Load, LoadType::kI64Load, LoadType::kF32Load,
LoadType::kF64Load, LoadType::kI32Load8S, LoadType::kI32Load8U,
LoadType::kI32Load16S, LoadType::kI32Load16U, LoadType::kI64Load8S,
LoadType::kI64Load8U, LoadType::kI64Load16S, LoadType::kI64Load16U,
LoadType::kI64Load32S, LoadType::kI64Load32U};
static_assert(arraysize(kLoadTypes) == kMaxOpcode - kMinOpcode + 1);
DCHECK_LE(kMinOpcode, opcode);
DCHECK_GE(kMaxOpcode, opcode);
return kLoadTypes[opcode - kMinOpcode];
}
static constexpr StoreType GetStoreType(WasmOpcode opcode) {
// Hard-code the list of store types. The opcodes are highly unlikely to
// ever change, and we have some checks here to guard against that.
static_assert(sizeof(StoreType) == sizeof(uint8_t), "StoreType is compact");
constexpr uint8_t kMinOpcode = kExprI32StoreMem;
constexpr uint8_t kMaxOpcode = kExprI64StoreMem32;
constexpr StoreType kStoreTypes[] = {
StoreType::kI32Store, StoreType::kI64Store, StoreType::kF32Store,
StoreType::kF64Store, StoreType::kI32Store8, StoreType::kI32Store16,
StoreType::kI64Store8, StoreType::kI64Store16, StoreType::kI64Store32};
static_assert(arraysize(kStoreTypes) == kMaxOpcode - kMinOpcode + 1);
DCHECK_LE(kMinOpcode, opcode);
DCHECK_GE(kMaxOpcode, opcode);
return kStoreTypes[opcode - kMinOpcode];
}
#define ATOMIC_OP_LIST(V) \
V(AtomicNotify, Uint32) \
V(I32AtomicWait, Uint32) \
V(I64AtomicWait, Uint64) \
V(I32AtomicLoad, Uint32) \
V(I64AtomicLoad, Uint64) \
V(I32AtomicLoad8U, Uint8) \
V(I32AtomicLoad16U, Uint16) \
V(I64AtomicLoad8U, Uint8) \
V(I64AtomicLoad16U, Uint16) \
V(I64AtomicLoad32U, Uint32) \
V(I32AtomicAdd, Uint32) \
V(I32AtomicAdd8U, Uint8) \
V(I32AtomicAdd16U, Uint16) \
V(I64AtomicAdd, Uint64) \
V(I64AtomicAdd8U, Uint8) \
V(I64AtomicAdd16U, Uint16) \
V(I64AtomicAdd32U, Uint32) \
V(I32AtomicSub, Uint32) \
V(I64AtomicSub, Uint64) \
V(I32AtomicSub8U, Uint8) \
V(I32AtomicSub16U, Uint16) \
V(I64AtomicSub8U, Uint8) \
V(I64AtomicSub16U, Uint16) \
V(I64AtomicSub32U, Uint32) \
V(I32AtomicAnd, Uint32) \
V(I64AtomicAnd, Uint64) \
V(I32AtomicAnd8U, Uint8) \
V(I32AtomicAnd16U, Uint16) \
V(I64AtomicAnd8U, Uint8) \
V(I64AtomicAnd16U, Uint16) \
V(I64AtomicAnd32U, Uint32) \
V(I32AtomicOr, Uint32) \
V(I64AtomicOr, Uint64) \
V(I32AtomicOr8U, Uint8) \
V(I32AtomicOr16U, Uint16) \
V(I64AtomicOr8U, Uint8) \
V(I64AtomicOr16U, Uint16) \
V(I64AtomicOr32U, Uint32) \
V(I32AtomicXor, Uint32) \
V(I64AtomicXor, Uint64) \
V(I32AtomicXor8U, Uint8) \
V(I32AtomicXor16U, Uint16) \
V(I64AtomicXor8U, Uint8) \
V(I64AtomicXor16U, Uint16) \
V(I64AtomicXor32U, Uint32) \
V(I32AtomicExchange, Uint32) \
V(I64AtomicExchange, Uint64) \
V(I32AtomicExchange8U, Uint8) \
V(I32AtomicExchange16U, Uint16) \
V(I64AtomicExchange8U, Uint8) \
V(I64AtomicExchange16U, Uint16) \
V(I64AtomicExchange32U, Uint32) \
V(I32AtomicCompareExchange, Uint32) \
V(I64AtomicCompareExchange, Uint64) \
V(I32AtomicCompareExchange8U, Uint8) \
V(I32AtomicCompareExchange16U, Uint16) \
V(I64AtomicCompareExchange8U, Uint8) \
V(I64AtomicCompareExchange16U, Uint16) \
V(I64AtomicCompareExchange32U, Uint32)
#define ATOMIC_STORE_OP_LIST(V) \
V(I32AtomicStore, Uint32) \
V(I64AtomicStore, Uint64) \
V(I32AtomicStore8U, Uint8) \
V(I32AtomicStore16U, Uint16) \
V(I64AtomicStore8U, Uint8) \
V(I64AtomicStore16U, Uint16) \
V(I64AtomicStore32U, Uint32)
// Decoder error with explicit PC and format arguments.
template <Decoder::ValidateFlag validate, typename... Args>
void DecodeError(Decoder* decoder, const byte* pc, const char* str,
Args&&... args) {
CHECK(validate == Decoder::kFullValidation ||
validate == Decoder::kBooleanValidation);
static_assert(sizeof...(Args) > 0);
if (validate == Decoder::kBooleanValidation) {
decoder->MarkError();
} else {
decoder->errorf(pc, str, std::forward<Args>(args)...);
}
}
// Decoder error with explicit PC and no format arguments.
template <Decoder::ValidateFlag validate>
void DecodeError(Decoder* decoder, const byte* pc, const char* str) {
CHECK(validate == Decoder::kFullValidation ||
validate == Decoder::kBooleanValidation);
if (validate == Decoder::kBooleanValidation) {
decoder->MarkError();
} else {
decoder->error(pc, str);
}
}
// Decoder error without explicit PC, but with format arguments.
template <Decoder::ValidateFlag validate, typename... Args>
void DecodeError(Decoder* decoder, const char* str, Args&&... args) {
CHECK(validate == Decoder::kFullValidation ||
validate == Decoder::kBooleanValidation);
static_assert(sizeof...(Args) > 0);
if (validate == Decoder::kBooleanValidation) {
decoder->MarkError();
} else {
decoder->errorf(str, std::forward<Args>(args)...);
}
}
// Decoder error without explicit PC and without format arguments.
template <Decoder::ValidateFlag validate>
void DecodeError(Decoder* decoder, const char* str) {
CHECK(validate == Decoder::kFullValidation ||
validate == Decoder::kBooleanValidation);
if (validate == Decoder::kBooleanValidation) {
decoder->MarkError();
} else {
decoder->error(str);
}
}
namespace value_type_reader {
// If {module} is not null, the read index will be checked against the module's
// type capacity.
template <Decoder::ValidateFlag validate>
HeapType read_heap_type(Decoder* decoder, const byte* pc,
uint32_t* const length, const WasmModule* module,
const WasmFeatures& enabled) {
int64_t heap_index = decoder->read_i33v<validate>(pc, length, "heap type");
if (heap_index < 0) {
int64_t min_1_byte_leb128 = -64;
if (!VALIDATE(heap_index >= min_1_byte_leb128)) {
DecodeError<validate>(decoder, pc, "Unknown heap type %" PRId64,
heap_index);
return HeapType(HeapType::kBottom);
}
uint8_t uint_7_mask = 0x7F;
uint8_t code = static_cast<ValueTypeCode>(heap_index) & uint_7_mask;
switch (code) {
case kEqRefCode:
case kI31RefCode:
case kDataRefCode:
case kArrayRefCode:
case kAnyRefCode:
case kNoneCode:
case kNoExternCode:
case kNoFuncCode:
if (!VALIDATE(enabled.has_gc())) {
DecodeError<validate>(
decoder, pc,
"invalid heap type '%s', enable with --experimental-wasm-gc",
HeapType::from_code(code).name().c_str());
}
V8_FALLTHROUGH;
case kExternRefCode:
case kFuncRefCode:
return HeapType::from_code(code);
case kStringRefCode:
case kStringViewWtf8Code:
case kStringViewWtf16Code:
case kStringViewIterCode:
if (!VALIDATE(enabled.has_stringref())) {
DecodeError<validate>(decoder, pc,
"invalid heap type '%s', enable with "
"--experimental-wasm-stringref",
HeapType::from_code(code).name().c_str());
}
return HeapType::from_code(code);
default:
DecodeError<validate>(decoder, pc, "Unknown heap type %" PRId64,
heap_index);
return HeapType(HeapType::kBottom);
}
} else {
if (!VALIDATE(enabled.has_typed_funcref())) {
DecodeError<validate>(decoder, pc,
"Invalid indexed heap type, enable with "
"--experimental-wasm-typed-funcref");
}
uint32_t type_index = static_cast<uint32_t>(heap_index);
if (!VALIDATE(type_index < kV8MaxWasmTypes)) {
DecodeError<validate>(
decoder, pc,
"Type index %u is greater than the maximum number %zu "
"of type definitions supported by V8",
type_index, kV8MaxWasmTypes);
return HeapType(HeapType::kBottom);
}
// We use capacity over size so this works mid-DecodeTypeSection.
if (!VALIDATE(module == nullptr || type_index < module->types.capacity())) {
DecodeError<validate>(decoder, pc, "Type index %u is out of bounds",
type_index);
}
return HeapType(type_index);
}
}
// Read a value type starting at address {pc} using {decoder}.
// No bytes are consumed.
// The length of the read value type is written in {length}.
// Registers an error for an invalid type only if {validate} is not
// kNoValidate.
template <Decoder::ValidateFlag validate>
ValueType read_value_type(Decoder* decoder, const byte* pc,
uint32_t* const length, const WasmModule* module,
const WasmFeatures& enabled) {
*length = 1;
byte val = decoder->read_u8<validate>(pc, "value type opcode");
if (decoder->failed()) {
*length = 0;
return kWasmBottom;
}
ValueTypeCode code = static_cast<ValueTypeCode>(val);
switch (code) {
case kEqRefCode:
case kI31RefCode:
case kDataRefCode:
case kArrayRefCode:
case kAnyRefCode:
case kNoneCode:
case kNoExternCode:
case kNoFuncCode:
if (!VALIDATE(enabled.has_gc())) {
DecodeError<validate>(
decoder, pc,
"invalid value type '%sref', enable with --experimental-wasm-gc",
HeapType::from_code(code).name().c_str());
return kWasmBottom;
}
V8_FALLTHROUGH;
case kExternRefCode:
case kFuncRefCode:
return ValueType::RefNull(HeapType::from_code(code));
case kStringRefCode:
case kStringViewWtf8Code:
case kStringViewWtf16Code:
case kStringViewIterCode: {
if (!VALIDATE(enabled.has_stringref())) {
DecodeError<validate>(decoder, pc,
"invalid value type '%sref', enable with "
"--experimental-wasm-stringref",
HeapType::from_code(code).name().c_str());
return kWasmBottom;
}
return ValueType::RefNull(HeapType::from_code(code));
}
case kI32Code:
return kWasmI32;
case kI64Code:
return kWasmI64;
case kF32Code:
return kWasmF32;
case kF64Code:
return kWasmF64;
case kRefCode:
case kRefNullCode: {
Nullability nullability = code == kRefNullCode ? kNullable : kNonNullable;
if (!VALIDATE(enabled.has_typed_funcref())) {
DecodeError<validate>(decoder, pc,
"Invalid type '(ref%s <heaptype>)', enable with "
"--experimental-wasm-typed-funcref",
nullability == kNullable ? " null" : "");
return kWasmBottom;
}
HeapType heap_type =
read_heap_type<validate>(decoder, pc + 1, length, module, enabled);
*length += 1;
return heap_type.is_bottom()
? kWasmBottom
: ValueType::RefMaybeNull(heap_type, nullability);
}
case kS128Code: {
if (!VALIDATE(enabled.has_simd())) {
DecodeError<validate>(
decoder, pc,
"invalid value type 's128', enable with --experimental-wasm-simd");
return kWasmBottom;
}
if (!VALIDATE(CheckHardwareSupportsSimd())) {
DecodeError<validate>(decoder, pc, "Wasm SIMD unsupported");
return kWasmBottom;
}
return kWasmS128;
}
// Although these codes are included in ValueTypeCode, they technically
// do not correspond to value types and are only used in specific
// contexts. The caller of this function is responsible for handling them.
case kVoidCode:
case kI8Code:
case kI16Code:
if (validate) {
DecodeError<validate>(decoder, pc, "invalid value type 0x%x", code);
}
return kWasmBottom;
}
// Anything that doesn't match an enumeration value is an invalid type code.
if (validate) {
DecodeError<validate>(decoder, pc, "invalid value type 0x%x", code);
}
return kWasmBottom;
}
} // namespace value_type_reader
enum DecodingMode { kFunctionBody, kConstantExpression };
// Helpers for decoding different kinds of immediates which follow bytecodes.
template <Decoder::ValidateFlag validate>
struct ImmI32Immediate {
int32_t value;
uint32_t length;
ImmI32Immediate(Decoder* decoder, const byte* pc) {
value = decoder->read_i32v<validate>(pc, &length, "immi32");
}
};
template <Decoder::ValidateFlag validate>
struct ImmI64Immediate {
int64_t value;
uint32_t length;
ImmI64Immediate(Decoder* decoder, const byte* pc) {
value = decoder->read_i64v<validate>(pc, &length, "immi64");
}
};
template <Decoder::ValidateFlag validate>
struct ImmF32Immediate {
float value;
uint32_t length = 4;
ImmF32Immediate(Decoder* decoder, const byte* pc) {
// We can't use base::bit_cast here because calling any helper function
// that returns a float would potentially flip NaN bits per C++ semantics,
// so we have to inline the memcpy call directly.
uint32_t tmp = decoder->read_u32<validate>(pc, "immf32");
memcpy(&value, &tmp, sizeof(value));
}
};
template <Decoder::ValidateFlag validate>
struct ImmF64Immediate {
double value;
uint32_t length = 8;
ImmF64Immediate(Decoder* decoder, const byte* pc) {
// Avoid base::bit_cast because it might not preserve the signalling bit
// of a NaN.
uint64_t tmp = decoder->read_u64<validate>(pc, "immf64");
memcpy(&value, &tmp, sizeof(value));
}
};
// This is different than IndexImmediate because {index} is a byte.
template <Decoder::ValidateFlag validate>
struct MemoryIndexImmediate {
uint8_t index = 0;
uint32_t length = 1;
MemoryIndexImmediate(Decoder* decoder, const byte* pc) {
index = decoder->read_u8<validate>(pc, "memory index");
}
};
// Parent class for all Immediates which read a u32v index value in their
// constructor.
template <Decoder::ValidateFlag validate>
struct IndexImmediate {
uint32_t index;
uint32_t length;
IndexImmediate(Decoder* decoder, const byte* pc, const char* name) {
index = decoder->read_u32v<validate>(pc, &length, name);
}
};
template <Decoder::ValidateFlag validate>
struct TagIndexImmediate : public IndexImmediate<validate> {
const WasmTag* tag = nullptr;
TagIndexImmediate(Decoder* decoder, const byte* pc)
: IndexImmediate<validate>(decoder, pc, "tag index") {}
};
template <Decoder::ValidateFlag validate>
struct GlobalIndexImmediate : public IndexImmediate<validate> {
const WasmGlobal* global = nullptr;
GlobalIndexImmediate(Decoder* decoder, const byte* pc)
: IndexImmediate<validate>(decoder, pc, "global index") {}
};
template <Decoder::ValidateFlag validate>
struct SigIndexImmediate : public IndexImmediate<validate> {
const FunctionSig* sig = nullptr;
SigIndexImmediate(Decoder* decoder, const byte* pc)
: IndexImmediate<validate>(decoder, pc, "signature index") {}
};
template <Decoder::ValidateFlag validate>
struct StructIndexImmediate : public IndexImmediate<validate> {
const StructType* struct_type = nullptr;
StructIndexImmediate(Decoder* decoder, const byte* pc)
: IndexImmediate<validate>(decoder, pc, "struct index") {}
};
template <Decoder::ValidateFlag validate>
struct ArrayIndexImmediate : public IndexImmediate<validate> {
const ArrayType* array_type = nullptr;
ArrayIndexImmediate(Decoder* decoder, const byte* pc)
: IndexImmediate<validate>(decoder, pc, "array index") {}
};
template <Decoder::ValidateFlag validate>
struct CallFunctionImmediate : public IndexImmediate<validate> {
const FunctionSig* sig = nullptr;
CallFunctionImmediate(Decoder* decoder, const byte* pc)
: IndexImmediate<validate>(decoder, pc, "function index") {}
};
template <Decoder::ValidateFlag validate>
struct SelectTypeImmediate {
uint32_t length;
ValueType type;
SelectTypeImmediate(const WasmFeatures& enabled, Decoder* decoder,
const byte* pc, const WasmModule* module) {
uint8_t num_types =
decoder->read_u32v<validate>(pc, &length, "number of select types");
if (!VALIDATE(num_types == 1)) {
DecodeError<validate>(
decoder, pc,
"Invalid number of types. Select accepts exactly one type");
return;
}
uint32_t type_length;
type = value_type_reader::read_value_type<validate>(
decoder, pc + length, &type_length, module, enabled);
length += type_length;
}
};
template <Decoder::ValidateFlag validate>
struct BlockTypeImmediate {
uint32_t length = 1;
ValueType type = kWasmVoid;
uint32_t sig_index = 0;
const FunctionSig* sig = nullptr;
BlockTypeImmediate(const WasmFeatures& enabled, Decoder* decoder,
const byte* pc, const WasmModule* module) {
int64_t block_type =
decoder->read_i33v<validate>(pc, &length, "block type");
if (block_type < 0) {
// All valid negative types are 1 byte in length, so we check against the
// minimum 1-byte LEB128 value.
constexpr int64_t min_1_byte_leb128 = -64;
if (!VALIDATE(block_type >= min_1_byte_leb128)) {
DecodeError<validate>(decoder, pc, "invalid block type %" PRId64,
block_type);
return;
}
if (static_cast<ValueTypeCode>(block_type & 0x7F) == kVoidCode) return;
type = value_type_reader::read_value_type<validate>(decoder, pc, &length,
module, enabled);
} else {
type = kWasmBottom;
sig_index = static_cast<uint32_t>(block_type);
}
}
uint32_t in_arity() const {
if (type != kWasmBottom) return 0;
return static_cast<uint32_t>(sig->parameter_count());
}
uint32_t out_arity() const {
if (type == kWasmVoid) return 0;
if (type != kWasmBottom) return 1;
return static_cast<uint32_t>(sig->return_count());
}
ValueType in_type(uint32_t index) {
DCHECK_EQ(kWasmBottom, type);
return sig->GetParam(index);
}
ValueType out_type(uint32_t index) {
if (type == kWasmBottom) return sig->GetReturn(index);
DCHECK_NE(kWasmVoid, type);
DCHECK_EQ(0, index);
return type;
}
};
template <Decoder::ValidateFlag validate>
struct BranchDepthImmediate {
uint32_t depth;
uint32_t length;
BranchDepthImmediate(Decoder* decoder, const byte* pc) {
depth = decoder->read_u32v<validate>(pc, &length, "branch depth");
}
};
template <Decoder::ValidateFlag validate>
struct FieldImmediate {
StructIndexImmediate<validate> struct_imm;
IndexImmediate<validate> field_imm;
uint32_t length;
FieldImmediate(Decoder* decoder, const byte* pc)
: struct_imm(decoder, pc),
field_imm(decoder, pc + struct_imm.length, "field index"),
length(struct_imm.length + field_imm.length) {}
};
template <Decoder::ValidateFlag validate>
struct CallIndirectImmediate {
IndexImmediate<validate> sig_imm;
IndexImmediate<validate> table_imm;
uint32_t length;
const FunctionSig* sig = nullptr;
CallIndirectImmediate(Decoder* decoder, const byte* pc)
: sig_imm(decoder, pc, "singature index"),
table_imm(decoder, pc + sig_imm.length, "table index"),
length(sig_imm.length + table_imm.length) {}
};
template <Decoder::ValidateFlag validate>
struct BranchTableImmediate {
uint32_t table_count;
const byte* start;
const byte* table;
BranchTableImmediate(Decoder* decoder, const byte* pc) {
start = pc;
uint32_t len = 0;
table_count = decoder->read_u32v<validate>(pc, &len, "table count");
table = pc + len;
}
};
// A helper to iterate over a branch table.
template <Decoder::ValidateFlag validate>
class BranchTableIterator {
public:
uint32_t cur_index() { return index_; }
bool has_next() { return VALIDATE(decoder_->ok()) && index_ <= table_count_; }
uint32_t next() {
DCHECK(has_next());
index_++;
uint32_t length;
uint32_t result =
decoder_->read_u32v<validate>(pc_, &length, "branch table entry");
pc_ += length;
return result;
}
// length, including the length of the {BranchTableImmediate}, but not the
// opcode.
uint32_t length() {
while (has_next()) next();
return static_cast<uint32_t>(pc_ - start_);
}
const byte* pc() { return pc_; }
BranchTableIterator(Decoder* decoder,
const BranchTableImmediate<validate>& imm)
: decoder_(decoder),
start_(imm.start),
pc_(imm.table),
table_count_(imm.table_count) {}
private:
Decoder* const decoder_;
const byte* start_;
const byte* pc_;
uint32_t index_ = 0; // the current index.
const uint32_t table_count_; // the count of entries, not including default.
};
template <Decoder::ValidateFlag validate,
DecodingMode decoding_mode = kFunctionBody>
class WasmDecoder;
template <Decoder::ValidateFlag validate>
struct MemoryAccessImmediate {
uint32_t alignment;
uint64_t offset;
uint32_t length = 0;
MemoryAccessImmediate(Decoder* decoder, const byte* pc,
uint32_t max_alignment, bool is_memory64) {
uint32_t alignment_length;
alignment =
decoder->read_u32v<validate>(pc, &alignment_length, "alignment");
if (!VALIDATE(alignment <= max_alignment)) {
DecodeError<validate>(
decoder, pc,
"invalid alignment; expected maximum alignment is %u, "
"actual alignment is %u",
max_alignment, alignment);
}
uint32_t offset_length;
offset = is_memory64 ? decoder->read_u64v<validate>(
pc + alignment_length, &offset_length, "offset")
: decoder->read_u32v<validate>(
pc + alignment_length, &offset_length, "offset");
length = alignment_length + offset_length;
}
};
// Immediate for SIMD lane operations.
template <Decoder::ValidateFlag validate>
struct SimdLaneImmediate {
uint8_t lane;
uint32_t length = 1;
SimdLaneImmediate(Decoder* decoder, const byte* pc) {
lane = decoder->read_u8<validate>(pc, "lane");
}
};
// Immediate for SIMD S8x16 shuffle operations.
template <Decoder::ValidateFlag validate>
struct Simd128Immediate {
uint8_t value[kSimd128Size] = {0};
Simd128Immediate(Decoder* decoder, const byte* pc) {
for (uint32_t i = 0; i < kSimd128Size; ++i) {
value[i] = decoder->read_u8<validate>(pc + i, "value");
}
}
};
template <Decoder::ValidateFlag validate>
struct MemoryInitImmediate {
IndexImmediate<validate> data_segment;
MemoryIndexImmediate<validate> memory;
uint32_t length;
MemoryInitImmediate(Decoder* decoder, const byte* pc)
: data_segment(decoder, pc, "data segment index"),
memory(decoder, pc + data_segment.length),
length(data_segment.length + memory.length) {}
};
template <Decoder::ValidateFlag validate>
struct MemoryCopyImmediate {
MemoryIndexImmediate<validate> memory_src;
MemoryIndexImmediate<validate> memory_dst;
uint32_t length;
MemoryCopyImmediate(Decoder* decoder, const byte* pc)
: memory_src(decoder, pc),
memory_dst(decoder, pc + memory_src.length),
length(memory_src.length + memory_dst.length) {}
};
template <Decoder::ValidateFlag validate>
struct TableInitImmediate {
IndexImmediate<validate> element_segment;
IndexImmediate<validate> table;
uint32_t length;
TableInitImmediate(Decoder* decoder, const byte* pc)
: element_segment(decoder, pc, "element segment index"),
table(decoder, pc + element_segment.length, "table index"),
length(element_segment.length + table.length) {}
};
template <Decoder::ValidateFlag validate>
struct TableCopyImmediate {
IndexImmediate<validate> table_dst;
IndexImmediate<validate> table_src;
uint32_t length;
TableCopyImmediate(Decoder* decoder, const byte* pc)
: table_dst(decoder, pc, "table index"),
table_src(decoder, pc + table_dst.length, "table index"),
length(table_src.length + table_dst.length) {}
};
template <Decoder::ValidateFlag validate>
struct HeapTypeImmediate {
uint32_t length = 1;
HeapType type;
HeapTypeImmediate(const WasmFeatures& enabled, Decoder* decoder,
const byte* pc, const WasmModule* module)
: type(value_type_reader::read_heap_type<validate>(decoder, pc, &length,
module, enabled)) {}
};
template <Decoder::ValidateFlag validate>
struct StringConstImmediate {
uint32_t index;
uint32_t length;
StringConstImmediate(Decoder* decoder, const byte* pc) {
index =
decoder->read_u32v<validate>(pc, &length, "stringref literal index");
}
};
template <Decoder::ValidateFlag validate>
struct PcForErrors {
explicit PcForErrors(const byte* /* pc */) {}
const byte* pc() const { return nullptr; }
};
template <>
struct PcForErrors<Decoder::kFullValidation> {
const byte* pc_for_errors = nullptr;
explicit PcForErrors(const byte* pc) : pc_for_errors(pc) {}
const byte* pc() const { return pc_for_errors; }
};
// An entry on the value stack.
template <Decoder::ValidateFlag validate>
struct ValueBase : public PcForErrors<validate> {
ValueType type = kWasmVoid;
ValueBase(const byte* pc, ValueType type)
: PcForErrors<validate>(pc), type(type) {}
};
template <typename Value>
struct Merge {
uint32_t arity = 0;
union { // Either multiple values or a single value.
Value* array;
Value first;
} vals = {nullptr}; // Initialize {array} with {nullptr}.
// Tracks whether this merge was ever reached. Uses precise reachability, like
// Reachability::kReachable.
bool reached;
explicit Merge(bool reached = false) : reached(reached) {}
Value& operator[](uint32_t i) {
DCHECK_GT(arity, i);
return arity == 1 ? vals.first : vals.array[i];
}
};
enum ControlKind : uint8_t {
kControlIf,
kControlIfElse,
kControlBlock,
kControlLoop,
kControlTry,
kControlTryCatch,
kControlTryCatchAll,
};
enum Reachability : uint8_t {
// reachable code.
kReachable,
// reachable code in unreachable block (implies normal validation).
kSpecOnlyReachable,
// code unreachable in its own block (implies polymorphic validation).
kUnreachable
};
// An entry on the control stack (i.e. if, block, loop, or try).
template <typename Value, Decoder::ValidateFlag validate>
struct ControlBase : public PcForErrors<validate> {
ControlKind kind = kControlBlock;
Reachability reachability = kReachable;
uint32_t stack_depth = 0; // Stack height at the beginning of the construct.
uint32_t init_stack_depth = 0; // Height of "locals initialization" stack
// at the beginning of the construct.
int32_t previous_catch = -1; // Depth of the innermost catch containing this
// 'try'.
// Values merged into the start or end of this control construct.
Merge<Value> start_merge;
Merge<Value> end_merge;
MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(ControlBase);
ControlBase(ControlKind kind, uint32_t stack_depth, uint32_t init_stack_depth,
const uint8_t* pc, Reachability reachability)
: PcForErrors<validate>(pc),
kind(kind),
reachability(reachability),
stack_depth(stack_depth),
init_stack_depth(init_stack_depth),
start_merge(reachability == kReachable) {}
// Check whether the current block is reachable.
bool reachable() const { return reachability == kReachable; }
// Check whether the rest of the block is unreachable.
// Note that this is different from {!reachable()}, as there is also the
// "indirect unreachable state", for which both {reachable()} and
// {unreachable()} return false.
bool unreachable() const { return reachability == kUnreachable; }
// Return the reachability of new control structs started in this block.
Reachability innerReachability() const {
return reachability == kReachable ? kReachable : kSpecOnlyReachable;
}
bool is_if() const { return is_onearmed_if() || is_if_else(); }
bool is_onearmed_if() const { return kind == kControlIf; }
bool is_if_else() const { return kind == kControlIfElse; }
bool is_block() const { return kind == kControlBlock; }
bool is_loop() const { return kind == kControlLoop; }
bool is_incomplete_try() const { return kind == kControlTry; }
bool is_try_catch() const { return kind == kControlTryCatch; }
bool is_try_catchall() const { return kind == kControlTryCatchAll; }
bool is_try() const {
return is_incomplete_try() || is_try_catch() || is_try_catchall();
}
Merge<Value>* br_merge() {
return is_loop() ? &this->start_merge : &this->end_merge;
}
};
// This is the list of callback functions that an interface for the
// WasmFullDecoder should implement.
// F(Name, args...)
#define INTERFACE_FUNCTIONS(F) \
INTERFACE_META_FUNCTIONS(F) \
INTERFACE_CONSTANT_FUNCTIONS(F) \
INTERFACE_NON_CONSTANT_FUNCTIONS(F)
#define INTERFACE_META_FUNCTIONS(F) \
F(TraceInstruction, uint32_t value) \
F(StartFunction) \
F(StartFunctionBody, Control* block) \
F(FinishFunction) \
F(OnFirstError) \
F(NextInstruction, WasmOpcode)
#define INTERFACE_CONSTANT_FUNCTIONS(F) /* force 80 columns */ \
F(I32Const, Value* result, int32_t value) \
F(I64Const, Value* result, int64_t value) \
F(F32Const, Value* result, float value) \
F(F64Const, Value* result, double value) \
F(S128Const, Simd128Immediate<validate>& imm, Value* result) \
F(GlobalGet, Value* result, const GlobalIndexImmediate<validate>& imm) \
F(DoReturn, uint32_t drop_values) \
F(BinOp, WasmOpcode opcode, const Value& lhs, const Value& rhs, \
Value* result) \
F(RefNull, ValueType type, Value* result) \
F(RefFunc, uint32_t function_index, Value* result) \
F(StructNew, const StructIndexImmediate<validate>& imm, const Value& rtt, \
const Value args[], Value* result) \
F(StructNewDefault, const StructIndexImmediate<validate>& imm, \
const Value& rtt, Value* result) \
F(ArrayNew, const ArrayIndexImmediate<validate>& imm, const Value& length, \
const Value& initial_value, const Value& rtt, Value* result) \
F(ArrayNewDefault, const ArrayIndexImmediate<validate>& imm, \
const Value& length, const Value& rtt, Value* result) \
F(ArrayNewFixed, const ArrayIndexImmediate<validate>& imm, \
const base::Vector<Value>& elements, const Value& rtt, Value* result) \
F(ArrayNewSegment, const ArrayIndexImmediate<validate>& array_imm, \
const IndexImmediate<validate>& data_segment, const Value& offset, \
const Value& length, const Value& rtt, Value* result) \
F(I31New, const Value& input, Value* result) \
F(RttCanon, uint32_t type_index, Value* result) \
F(StringConst, const StringConstImmediate<validate>& imm, Value* result)
#define INTERFACE_NON_CONSTANT_FUNCTIONS(F) /* force 80 columns */ \
/* Control: */ \
F(Block, Control* block) \
F(Loop, Control* block) \
F(Try, Control* block) \
F(If, const Value& cond, Control* if_block) \
F(FallThruTo, Control* c) \
F(PopControl, Control* block) \
/* Instructions: */ \
F(UnOp, WasmOpcode opcode, const Value& value, Value* result) \
F(RefAsNonNull, const Value& arg, Value* result) \
F(Drop) \
F(LocalGet, Value* result, const IndexImmediate<validate>& imm) \
F(LocalSet, const Value& value, const IndexImmediate<validate>& imm) \
F(LocalTee, const Value& value, Value* result, \
const IndexImmediate<validate>& imm) \
F(GlobalSet, const Value& value, const GlobalIndexImmediate<validate>& imm) \
F(TableGet, const Value& index, Value* result, \
const IndexImmediate<validate>& imm) \
F(TableSet, const Value& index, const Value& value, \
const IndexImmediate<validate>& imm) \
F(Trap, TrapReason reason) \
F(NopForTestingUnsupportedInLiftoff) \
F(Forward, const Value& from, Value* to) \
F(Select, const Value& cond, const Value& fval, const Value& tval, \
Value* result) \
F(BrOrRet, uint32_t depth, uint32_t drop_values) \
F(BrIf, const Value& cond, uint32_t depth) \
F(BrTable, const BranchTableImmediate<validate>& imm, const Value& key) \
F(Else, Control* if_block) \
F(LoadMem, LoadType type, const MemoryAccessImmediate<validate>& imm, \
const Value& index, Value* result) \
F(LoadTransform, LoadType type, LoadTransformationKind transform, \
const MemoryAccessImmediate<validate>& imm, const Value& index, \
Value* result) \
F(LoadLane, LoadType type, const Value& value, const Value& index, \
const MemoryAccessImmediate<validate>& imm, const uint8_t laneidx, \
Value* result) \
F(StoreMem, StoreType type, const MemoryAccessImmediate<validate>& imm, \
const Value& index, const Value& value) \
F(StoreLane, StoreType type, const MemoryAccessImmediate<validate>& imm, \