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
|
/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Debug.h>
#include <LibWasm/AbstractMachine/AbstractMachine.h>
#include <LibWasm/AbstractMachine/BytecodeInterpreter.h>
#include <LibWasm/AbstractMachine/Configuration.h>
#include <LibWasm/AbstractMachine/Operators.h>
#include <LibWasm/Opcode.h>
#include <LibWasm/Printer/Printer.h>
namespace Wasm {
#define TRAP_IF_NOT(x) \
do { \
if (trap_if_not(x, #x##sv)) { \
dbgln_if(WASM_TRACE_DEBUG, "Trapped because {} failed, at line {}", #x, __LINE__); \
return; \
} \
} while (false)
#define TRAP_IF_NOT_NORETURN(x) \
do { \
if (trap_if_not(x, #x##sv)) { \
dbgln_if(WASM_TRACE_DEBUG, "Trapped because {} failed, at line {}", #x, __LINE__); \
} \
} while (false)
void BytecodeInterpreter::interpret(Configuration& configuration)
{
m_stack_info = {};
m_trap.clear();
auto& instructions = configuration.frame().expression().instructions();
auto max_ip_value = InstructionPointer { instructions.size() };
auto& current_ip_value = configuration.ip();
auto const should_limit_instruction_count = configuration.should_limit_instruction_count();
u64 executed_instructions = 0;
while (current_ip_value < max_ip_value) {
if (should_limit_instruction_count) {
if (executed_instructions++ >= Constants::max_allowed_executed_instructions_per_call) [[unlikely]] {
m_trap = Trap { "Exceeded maximum allowed number of instructions" };
return;
}
}
auto& instruction = instructions[current_ip_value.value()];
auto old_ip = current_ip_value;
interpret(configuration, current_ip_value, instruction);
if (m_trap.has_value())
return;
if (current_ip_value == old_ip) // If no jump occurred
++current_ip_value;
}
}
void BytecodeInterpreter::branch_to_label(Configuration& configuration, LabelIndex index)
{
dbgln_if(WASM_TRACE_DEBUG, "Branch to label with index {}...", index.value());
auto label = configuration.nth_label(index.value());
TRAP_IF_NOT(label.has_value());
dbgln_if(WASM_TRACE_DEBUG, "...which is actually IP {}, and has {} result(s)", label->continuation().value(), label->arity());
auto results = pop_values(configuration, label->arity());
size_t drop_count = index.value() + 1;
for (; !configuration.stack().is_empty();) {
auto& entry = configuration.stack().peek();
if (entry.has<Label>()) {
if (--drop_count == 0)
break;
}
configuration.stack().pop();
}
for (auto& result : results)
configuration.stack().push(move(result));
configuration.ip() = label->continuation();
}
template<typename ReadType, typename PushType>
void BytecodeInterpreter::load_and_push(Configuration& configuration, Instruction const& instruction)
{
auto& address = configuration.frame().module().memories().first();
auto memory = configuration.store().get(address);
if (!memory) {
m_trap = Trap { "Nonexistent memory" };
return;
}
auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
TRAP_IF_NOT(!configuration.stack().is_empty());
auto& entry = configuration.stack().peek();
TRAP_IF_NOT(entry.has<Value>());
auto base = entry.get<Value>().to<i32>();
if (!base.has_value()) {
m_trap = Trap { "Memory access out of bounds" };
return;
}
auto instance_address = base.value() + static_cast<i64>(arg.offset);
if (instance_address < 0 || static_cast<u64>(instance_address + sizeof(ReadType)) > memory->size()) {
m_trap = Trap { "Memory access out of bounds" };
dbgln("LibWasm: Memory access out of bounds (expected 0 <= {} and {} <= {})", instance_address, instance_address + sizeof(ReadType), memory->size());
return;
}
dbgln_if(WASM_TRACE_DEBUG, "load({} : {}) -> stack", instance_address, sizeof(ReadType));
auto slice = memory->data().bytes().slice(instance_address, sizeof(ReadType));
configuration.stack().peek() = Value(static_cast<PushType>(read_value<ReadType>(slice)));
}
void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruction const& instruction, ReadonlyBytes data)
{
auto& address = configuration.frame().module().memories().first();
auto memory = configuration.store().get(address);
TRAP_IF_NOT(memory);
auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
TRAP_IF_NOT(!configuration.stack().is_empty());
auto entry = configuration.stack().pop();
TRAP_IF_NOT(entry.has<Value>());
auto base = entry.get<Value>().to<i32>();
TRAP_IF_NOT(base.has_value());
auto instance_address = base.value() + static_cast<i64>(arg.offset);
if (instance_address < 0 || static_cast<u64>(instance_address + data.size()) > memory->size()) {
m_trap = Trap { "Memory access out of bounds" };
dbgln("LibWasm: Memory access out of bounds (expected 0 <= {} and {} <= {})", instance_address, instance_address + data.size(), memory->size());
return;
}
dbgln_if(WASM_TRACE_DEBUG, "tempoaray({}b) -> store({})", data.size(), instance_address);
data.copy_to(memory->data().bytes().slice(instance_address, data.size()));
}
void BytecodeInterpreter::call_address(Configuration& configuration, FunctionAddress address)
{
TRAP_IF_NOT(m_stack_info.size_free() >= Constants::minimum_stack_space_to_keep_free);
auto instance = configuration.store().get(address);
TRAP_IF_NOT(instance);
FunctionType const* type { nullptr };
instance->visit([&](auto const& function) { type = &function.type(); });
TRAP_IF_NOT(type);
TRAP_IF_NOT(configuration.stack().entries().size() > type->parameters().size());
Vector<Value> args;
args.ensure_capacity(type->parameters().size());
auto span = configuration.stack().entries().span().slice_from_end(type->parameters().size());
for (auto& entry : span) {
auto* ptr = entry.get_pointer<Value>();
TRAP_IF_NOT(ptr != nullptr);
args.unchecked_append(*ptr);
}
configuration.stack().entries().remove(configuration.stack().size() - span.size(), span.size());
Result result { Trap { ""sv } };
{
CallFrameHandle handle { *this, configuration };
result = configuration.call(*this, address, move(args));
}
if (result.is_trap()) {
m_trap = move(result.trap());
return;
}
configuration.stack().entries().ensure_capacity(configuration.stack().size() + result.values().size());
for (auto& entry : result.values())
configuration.stack().entries().unchecked_append(move(entry));
}
template<typename PopType, typename PushType, typename Operator>
void BytecodeInterpreter::binary_numeric_operation(Configuration& configuration)
{
TRAP_IF_NOT(!configuration.stack().is_empty());
auto rhs_entry = configuration.stack().pop();
auto& lhs_entry = configuration.stack().peek();
auto rhs_ptr = rhs_entry.get_pointer<Value>();
auto lhs_ptr = lhs_entry.get_pointer<Value>();
TRAP_IF_NOT(rhs_ptr);
TRAP_IF_NOT(lhs_ptr);
auto rhs = rhs_ptr->to<PopType>();
auto lhs = lhs_ptr->to<PopType>();
TRAP_IF_NOT(lhs.has_value());
TRAP_IF_NOT(rhs.has_value());
PushType result;
auto call_result = Operator {}(lhs.value(), rhs.value());
if constexpr (IsSpecializationOf<decltype(call_result), AK::Result>) {
if (call_result.is_error()) {
trap_if_not(false, call_result.error());
return;
}
result = call_result.release_value();
} else {
result = call_result;
}
dbgln_if(WASM_TRACE_DEBUG, "{} {} {} = {}", lhs.value(), Operator::name(), rhs.value(), result);
configuration.stack().peek() = Value(result);
}
template<typename PopType, typename PushType, typename Operator>
void BytecodeInterpreter::unary_operation(Configuration& configuration)
{
TRAP_IF_NOT(!configuration.stack().is_empty());
auto& entry = configuration.stack().peek();
auto entry_ptr = entry.get_pointer<Value>();
TRAP_IF_NOT(entry_ptr);
auto value = entry_ptr->to<PopType>();
TRAP_IF_NOT(value.has_value());
auto call_result = Operator {}(*value);
PushType result;
if constexpr (IsSpecializationOf<decltype(call_result), AK::Result>) {
if (call_result.is_error()) {
trap_if_not(false, call_result.error());
return;
}
result = call_result.release_value();
} else {
result = call_result;
}
dbgln_if(WASM_TRACE_DEBUG, "map({}) {} = {}", Operator::name(), *value, result);
configuration.stack().peek() = Value(result);
}
#define LOAD_AND_PUSH(read_type, push_type) \
do { \
return load_and_push<read_type, push_type>(configuration, instruction); \
} while (false)
#define POP_AND_STORE(pop_type, store_type) \
do { \
TRAP_IF_NOT(!configuration.stack().is_empty()); \
auto entry = configuration.stack().pop(); \
TRAP_IF_NOT(entry.has<Value>()); \
auto value = ConvertToRaw<store_type> {}(*entry.get<Value>().to<pop_type>()); \
dbgln_if(WASM_TRACE_DEBUG, "stack({}) -> temporary({}b)", value, sizeof(store_type)); \
store_to_memory(configuration, instruction, { &value, sizeof(store_type) }); \
return; \
} while (false)
template<typename T>
T BytecodeInterpreter::read_value(ReadonlyBytes data)
{
LittleEndian<T> value;
InputMemoryStream stream { data };
stream >> value;
if (stream.handle_any_error()) {
dbgln("Read from {} failed", data.data());
m_trap = Trap { "Read from memory failed" };
}
return value;
}
template<>
float BytecodeInterpreter::read_value<float>(ReadonlyBytes data)
{
InputMemoryStream stream { data };
LittleEndian<u32> raw_value;
stream >> raw_value;
if (stream.handle_any_error())
m_trap = Trap { "Read from memory failed" };
return bit_cast<float>(static_cast<u32>(raw_value));
}
template<>
double BytecodeInterpreter::read_value<double>(ReadonlyBytes data)
{
InputMemoryStream stream { data };
LittleEndian<u64> raw_value;
stream >> raw_value;
if (stream.handle_any_error())
m_trap = Trap { "Read from memory failed" };
return bit_cast<double>(static_cast<u64>(raw_value));
}
template<typename T>
struct ConvertToRaw {
T operator()(T value)
{
return LittleEndian<T>(value);
}
};
template<>
struct ConvertToRaw<float> {
u32 operator()(float value)
{
LittleEndian<u32> res;
ReadonlyBytes bytes { &value, sizeof(float) };
InputMemoryStream stream { bytes };
stream >> res;
VERIFY(!stream.has_any_error());
return static_cast<u32>(res);
}
};
template<>
struct ConvertToRaw<double> {
u64 operator()(double value)
{
LittleEndian<u64> res;
ReadonlyBytes bytes { &value, sizeof(double) };
InputMemoryStream stream { bytes };
stream >> res;
VERIFY(!stream.has_any_error());
return static_cast<u64>(res);
}
};
template<typename V, typename T>
MakeSigned<T> BytecodeInterpreter::checked_signed_truncate(V value)
{
if (isnan(value) || isinf(value)) { // "undefined", let's just trap.
m_trap = Trap { "Signed truncation undefined behaviour" };
return 0;
}
double truncated;
if constexpr (IsSame<float, V>)
truncated = truncf(value);
else
truncated = trunc(value);
using SignedT = MakeSigned<T>;
if (NumericLimits<SignedT>::min() <= truncated && static_cast<double>(NumericLimits<SignedT>::max()) >= truncated)
return static_cast<SignedT>(truncated);
dbgln_if(WASM_TRACE_DEBUG, "Truncate out of range error");
m_trap = Trap { "Signed truncation out of range" };
return true;
}
template<typename V, typename T>
MakeUnsigned<T> BytecodeInterpreter::checked_unsigned_truncate(V value)
{
if (isnan(value) || isinf(value)) { // "undefined", let's just trap.
m_trap = Trap { "Unsigned truncation undefined behaviour" };
return 0;
}
double truncated;
if constexpr (IsSame<float, V>)
truncated = truncf(value);
else
truncated = trunc(value);
using UnsignedT = MakeUnsigned<T>;
if (NumericLimits<UnsignedT>::min() <= truncated && static_cast<double>(NumericLimits<UnsignedT>::max()) >= truncated)
return static_cast<UnsignedT>(truncated);
dbgln_if(WASM_TRACE_DEBUG, "Truncate out of range error");
m_trap = Trap { "Unsigned truncation out of range" };
return true;
}
Vector<Value> BytecodeInterpreter::pop_values(Configuration& configuration, size_t count)
{
Vector<Value> results;
results.resize(count);
for (size_t i = 0; i < count; ++i) {
auto top_of_stack = configuration.stack().pop();
if (auto value = top_of_stack.get_pointer<Value>())
results[i] = move(*value);
else
TRAP_IF_NOT_NORETURN(value);
}
return results;
}
void BytecodeInterpreter::interpret(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction)
{
dbgln_if(WASM_TRACE_DEBUG, "Executing instruction {} at ip {}", instruction_name(instruction.opcode()), ip.value());
switch (instruction.opcode().value()) {
case Instructions::unreachable.value():
m_trap = Trap { "Unreachable" };
return;
case Instructions::nop.value():
return;
case Instructions::local_get.value():
configuration.stack().push(Value(configuration.frame().locals()[instruction.arguments().get<LocalIndex>().value()]));
return;
case Instructions::local_set.value(): {
TRAP_IF_NOT(!configuration.stack().is_empty());
auto entry = configuration.stack().pop();
TRAP_IF_NOT(entry.has<Value>());
configuration.frame().locals()[instruction.arguments().get<LocalIndex>().value()] = move(entry.get<Value>());
return;
}
case Instructions::i32_const.value():
configuration.stack().push(Value(ValueType { ValueType::I32 }, static_cast<i64>(instruction.arguments().get<i32>())));
return;
case Instructions::i64_const.value():
configuration.stack().push(Value(ValueType { ValueType::I64 }, instruction.arguments().get<i64>()));
return;
case Instructions::f32_const.value():
configuration.stack().push(Value(ValueType { ValueType::F32 }, static_cast<double>(instruction.arguments().get<float>())));
return;
case Instructions::f64_const.value():
configuration.stack().push(Value(ValueType { ValueType::F64 }, instruction.arguments().get<double>()));
return;
case Instructions::block.value(): {
size_t arity = 0;
auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
if (args.block_type.kind() != BlockType::Empty)
arity = 1;
configuration.stack().push(Label(arity, args.end_ip));
return;
}
case Instructions::loop.value(): {
size_t arity = 0;
auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
if (args.block_type.kind() != BlockType::Empty)
arity = 1;
configuration.stack().push(Label(arity, ip.value() + 1));
return;
}
case Instructions::if_.value(): {
size_t arity = 0;
auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
if (args.block_type.kind() != BlockType::Empty)
arity = 1;
TRAP_IF_NOT(!configuration.stack().is_empty());
auto entry = configuration.stack().pop();
TRAP_IF_NOT(entry.has<Value>());
auto value = entry.get<Value>().to<i32>();
TRAP_IF_NOT(value.has_value());
auto end_label = Label(arity, args.end_ip.value());
if (value.value() == 0) {
if (args.else_ip.has_value()) {
configuration.ip() = args.else_ip.value();
configuration.stack().push(move(end_label));
} else {
configuration.ip() = args.end_ip.value() + 1;
}
} else {
configuration.stack().push(move(end_label));
}
return;
}
case Instructions::structured_end.value():
case Instructions::structured_else.value(): {
auto index = configuration.nth_label_index(0);
TRAP_IF_NOT(index.has_value());
auto label = configuration.stack().entries()[*index].get<Label>();
configuration.stack().entries().remove(*index, 1);
if (instruction.opcode() == Instructions::structured_end)
return;
// Jump to the end label
configuration.ip() = label.continuation();
return;
}
case Instructions::return_.value(): {
auto& frame = configuration.frame();
size_t end = configuration.stack().size() - frame.arity();
size_t start = end;
for (; start + 1 > 0 && start < configuration.stack().size(); --start) {
auto& entry = configuration.stack().entries()[start];
if (entry.has<Frame>()) {
// Leave the frame, _and_ its label.
start += 2;
break;
}
}
configuration.stack().entries().remove(start, end - start);
// Jump past the call/indirect instruction
configuration.ip() = configuration.frame().expression().instructions().size();
return;
}
case Instructions::br.value():
return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
case Instructions::br_if.value(): {
TRAP_IF_NOT(!configuration.stack().is_empty());
auto entry = configuration.stack().pop();
TRAP_IF_NOT(entry.has<Value>());
if (entry.get<Value>().to<i32>().value_or(0) == 0)
return;
return branch_to_label(configuration, instruction.arguments().get<LabelIndex>());
}
case Instructions::br_table.value(): {
auto& arguments = instruction.arguments().get<Instruction::TableBranchArgs>();
TRAP_IF_NOT(!configuration.stack().is_empty());
auto entry = configuration.stack().pop();
TRAP_IF_NOT(entry.has<Value>());
auto maybe_i = entry.get<Value>().to<i32>();
TRAP_IF_NOT(maybe_i.has_value());
if (0 <= *maybe_i) {
size_t i = *maybe_i;
if (i < arguments.labels.size())
return branch_to_label(configuration, arguments.labels[i]);
}
return branch_to_label(configuration, arguments.default_);
}
case Instructions::call.value(): {
auto index = instruction.arguments().get<FunctionIndex>();
TRAP_IF_NOT(index.value() < configuration.frame().module().functions().size());
auto address = configuration.frame().module().functions()[index.value()];
dbgln_if(WASM_TRACE_DEBUG, "call({})", address.value());
call_address(configuration, address);
return;
}
case Instructions::call_indirect.value(): {
auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
TRAP_IF_NOT(args.table.value() < configuration.frame().module().tables().size());
auto table_address = configuration.frame().module().tables()[args.table.value()];
auto table_instance = configuration.store().get(table_address);
TRAP_IF_NOT(!configuration.stack().is_empty());
auto entry = configuration.stack().pop();
TRAP_IF_NOT(entry.has<Value>());
auto index = entry.get<Value>().to<i32>();
TRAP_IF_NOT(index.has_value());
TRAP_IF_NOT(index.value() >= 0);
TRAP_IF_NOT(static_cast<size_t>(index.value()) < table_instance->elements().size());
auto element = table_instance->elements()[index.value()];
TRAP_IF_NOT(element.has_value());
TRAP_IF_NOT(element->ref().has<Reference::Func>());
auto address = element->ref().get<Reference::Func>().address;
dbgln_if(WASM_TRACE_DEBUG, "call_indirect({} -> {})", index.value(), address.value());
call_address(configuration, address);
return;
}
case Instructions::i32_load.value():
LOAD_AND_PUSH(i32, i32);
case Instructions::i64_load.value():
LOAD_AND_PUSH(i64, i64);
case Instructions::f32_load.value():
LOAD_AND_PUSH(float, float);
case Instructions::f64_load.value():
LOAD_AND_PUSH(double, double);
case Instructions::i32_load8_s.value():
LOAD_AND_PUSH(i8, i32);
case Instructions::i32_load8_u.value():
LOAD_AND_PUSH(u8, i32);
case Instructions::i32_load16_s.value():
LOAD_AND_PUSH(i16, i32);
case Instructions::i32_load16_u.value():
LOAD_AND_PUSH(u16, i32);
case Instructions::i64_load8_s.value():
LOAD_AND_PUSH(i8, i64);
case Instructions::i64_load8_u.value():
LOAD_AND_PUSH(u8, i64);
case Instructions::i64_load16_s.value():
LOAD_AND_PUSH(i16, i64);
case Instructions::i64_load16_u.value():
LOAD_AND_PUSH(u16, i64);
case Instructions::i64_load32_s.value():
LOAD_AND_PUSH(i32, i64);
case Instructions::i64_load32_u.value():
LOAD_AND_PUSH(u32, i64);
case Instructions::i32_store.value():
POP_AND_STORE(i32, i32);
case Instructions::i64_store.value():
POP_AND_STORE(i64, i64);
case Instructions::f32_store.value():
POP_AND_STORE(float, float);
case Instructions::f64_store.value():
POP_AND_STORE(double, double);
case Instructions::i32_store8.value():
POP_AND_STORE(i32, i8);
case Instructions::i32_store16.value():
POP_AND_STORE(i32, i16);
case Instructions::i64_store8.value():
POP_AND_STORE(i64, i8);
case Instructions::i64_store16.value():
POP_AND_STORE(i64, i16);
case Instructions::i64_store32.value():
POP_AND_STORE(i64, i32);
case Instructions::local_tee.value(): {
TRAP_IF_NOT(!configuration.stack().is_empty());
auto& entry = configuration.stack().peek();
TRAP_IF_NOT(entry.has<Value>());
auto value = entry.get<Value>();
auto local_index = instruction.arguments().get<LocalIndex>();
TRAP_IF_NOT(configuration.frame().locals().size() > local_index.value());
dbgln_if(WASM_TRACE_DEBUG, "stack:peek -> locals({})", local_index.value());
configuration.frame().locals()[local_index.value()] = move(value);
return;
}
case Instructions::global_get.value(): {
auto global_index = instruction.arguments().get<GlobalIndex>();
TRAP_IF_NOT(configuration.frame().module().globals().size() > global_index.value());
auto address = configuration.frame().module().globals()[global_index.value()];
dbgln_if(WASM_TRACE_DEBUG, "global({}) -> stack", address.value());
auto global = configuration.store().get(address);
configuration.stack().push(Value(global->value()));
return;
}
case Instructions::global_set.value(): {
auto global_index = instruction.arguments().get<GlobalIndex>();
TRAP_IF_NOT(configuration.frame().module().globals().size() > global_index.value());
auto address = configuration.frame().module().globals()[global_index.value()];
TRAP_IF_NOT(!configuration.stack().is_empty());
auto entry = configuration.stack().pop();
TRAP_IF_NOT(entry.has<Value>());
auto value = entry.get<Value>();
dbgln_if(WASM_TRACE_DEBUG, "stack -> global({})", address.value());
auto global = configuration.store().get(address);
global->set_value(move(value));
return;
}
case Instructions::memory_size.value(): {
TRAP_IF_NOT(configuration.frame().module().memories().size() > 0);
auto address = configuration.frame().module().memories()[0];
auto instance = configuration.store().get(address);
auto pages = instance->size() / Constants::page_size;
dbgln_if(WASM_TRACE_DEBUG, "memory.size -> stack({})", pages);
configuration.stack().push(Value((i32)pages));
return;
}
case Instructions::memory_grow.value(): {
TRAP_IF_NOT(configuration.frame().module().memories().size() > 0);
auto address = configuration.frame().module().memories()[0];
auto instance = configuration.store().get(address);
i32 old_pages = instance->size() / Constants::page_size;
TRAP_IF_NOT(!configuration.stack().is_empty());
auto& entry = configuration.stack().peek();
TRAP_IF_NOT(entry.has<Value>());
auto new_pages = entry.get<Value>().to<i32>();
TRAP_IF_NOT(new_pages.has_value());
dbgln_if(WASM_TRACE_DEBUG, "memory.grow({}), previously {} pages...", *new_pages, old_pages);
if (instance->grow(new_pages.value() * Constants::page_size))
configuration.stack().peek() = Value((i32)old_pages);
else
configuration.stack().peek() = Value((i32)-1);
return;
}
case Instructions::table_get.value():
case Instructions::table_set.value():
goto unimplemented;
case Instructions::ref_null.value(): {
auto type = instruction.arguments().get<ValueType>();
TRAP_IF_NOT(type.is_reference());
configuration.stack().push(Value(Reference(Reference::Null { type })));
return;
};
case Instructions::ref_func.value(): {
auto index = instruction.arguments().get<FunctionIndex>().value();
auto& functions = configuration.frame().module().functions();
TRAP_IF_NOT(functions.size() > index);
auto address = functions[index];
configuration.stack().push(Value(ValueType(ValueType::FunctionReference), address.value()));
return;
}
case Instructions::ref_is_null.value(): {
TRAP_IF_NOT(!configuration.stack().is_empty());
auto top = configuration.stack().peek().get_pointer<Value>();
TRAP_IF_NOT(top);
TRAP_IF_NOT(top->type().is_reference());
auto is_null = top->to<Reference::Null>().has_value();
configuration.stack().peek() = Value(ValueType(ValueType::I32), static_cast<u64>(is_null ? 1 : 0));
return;
}
case Instructions::drop.value():
TRAP_IF_NOT(!configuration.stack().is_empty());
configuration.stack().pop();
return;
case Instructions::select.value():
case Instructions::select_typed.value(): {
// Note: The type seems to only be used for validation.
TRAP_IF_NOT(!configuration.stack().is_empty());
auto entry = configuration.stack().pop();
TRAP_IF_NOT(entry.has<Value>());
auto value = entry.get<Value>().to<i32>();
TRAP_IF_NOT(value.has_value());
dbgln_if(WASM_TRACE_DEBUG, "select({})", value.value());
auto rhs_entry = configuration.stack().pop();
TRAP_IF_NOT(rhs_entry.has<Value>());
auto& lhs_entry = configuration.stack().peek();
TRAP_IF_NOT(lhs_entry.has<Value>());
auto rhs = move(rhs_entry.get<Value>());
auto lhs = move(lhs_entry.get<Value>());
configuration.stack().peek() = value.value() != 0 ? move(lhs) : move(rhs);
return;
}
case Instructions::i32_eqz.value():
return unary_operation<i32, i32, Operators::EqualsZero>(configuration);
case Instructions::i32_eq.value():
return binary_numeric_operation<i32, i32, Operators::Equals>(configuration);
case Instructions::i32_ne.value():
return binary_numeric_operation<i32, i32, Operators::NotEquals>(configuration);
case Instructions::i32_lts.value():
return binary_numeric_operation<i32, i32, Operators::LessThan>(configuration);
case Instructions::i32_ltu.value():
return binary_numeric_operation<u32, i32, Operators::LessThan>(configuration);
case Instructions::i32_gts.value():
return binary_numeric_operation<i32, i32, Operators::GreaterThan>(configuration);
case Instructions::i32_gtu.value():
return binary_numeric_operation<u32, i32, Operators::GreaterThan>(configuration);
case Instructions::i32_les.value():
return binary_numeric_operation<i32, i32, Operators::LessThanOrEquals>(configuration);
case Instructions::i32_leu.value():
return binary_numeric_operation<u32, i32, Operators::LessThanOrEquals>(configuration);
case Instructions::i32_ges.value():
return binary_numeric_operation<i32, i32, Operators::GreaterThanOrEquals>(configuration);
case Instructions::i32_geu.value():
return binary_numeric_operation<u32, i32, Operators::GreaterThanOrEquals>(configuration);
case Instructions::i64_eqz.value():
return unary_operation<i64, i32, Operators::EqualsZero>(configuration);
case Instructions::i64_eq.value():
return binary_numeric_operation<i64, i32, Operators::Equals>(configuration);
case Instructions::i64_ne.value():
return binary_numeric_operation<i64, i32, Operators::NotEquals>(configuration);
case Instructions::i64_lts.value():
return binary_numeric_operation<i64, i32, Operators::LessThan>(configuration);
case Instructions::i64_ltu.value():
return binary_numeric_operation<u64, i32, Operators::LessThan>(configuration);
case Instructions::i64_gts.value():
return binary_numeric_operation<i64, i32, Operators::GreaterThan>(configuration);
case Instructions::i64_gtu.value():
return binary_numeric_operation<u64, i32, Operators::GreaterThan>(configuration);
case Instructions::i64_les.value():
return binary_numeric_operation<i64, i32, Operators::LessThanOrEquals>(configuration);
case Instructions::i64_leu.value():
return binary_numeric_operation<u64, i32, Operators::LessThanOrEquals>(configuration);
case Instructions::i64_ges.value():
return binary_numeric_operation<i64, i32, Operators::GreaterThanOrEquals>(configuration);
case Instructions::i64_geu.value():
return binary_numeric_operation<u64, i32, Operators::GreaterThanOrEquals>(configuration);
case Instructions::f32_eq.value():
return binary_numeric_operation<float, i32, Operators::Equals>(configuration);
case Instructions::f32_ne.value():
return binary_numeric_operation<float, i32, Operators::NotEquals>(configuration);
case Instructions::f32_lt.value():
return binary_numeric_operation<float, i32, Operators::LessThan>(configuration);
case Instructions::f32_gt.value():
return binary_numeric_operation<float, i32, Operators::GreaterThan>(configuration);
case Instructions::f32_le.value():
return binary_numeric_operation<float, i32, Operators::LessThanOrEquals>(configuration);
case Instructions::f32_ge.value():
return binary_numeric_operation<float, i32, Operators::GreaterThanOrEquals>(configuration);
case Instructions::f64_eq.value():
return binary_numeric_operation<double, i32, Operators::Equals>(configuration);
case Instructions::f64_ne.value():
return binary_numeric_operation<double, i32, Operators::NotEquals>(configuration);
case Instructions::f64_lt.value():
return binary_numeric_operation<double, i32, Operators::LessThan>(configuration);
case Instructions::f64_gt.value():
return binary_numeric_operation<double, i32, Operators::GreaterThan>(configuration);
case Instructions::f64_le.value():
return binary_numeric_operation<double, i32, Operators::LessThanOrEquals>(configuration);
case Instructions::f64_ge.value():
return binary_numeric_operation<double, i32, Operators::GreaterThanOrEquals>(configuration);
case Instructions::i32_clz.value():
return unary_operation<i32, i32, Operators::CountLeadingZeros>(configuration);
case Instructions::i32_ctz.value():
return unary_operation<i32, i32, Operators::CountTrailingZeros>(configuration);
case Instructions::i32_popcnt.value():
return unary_operation<i32, i32, Operators::PopCount>(configuration);
case Instructions::i32_add.value():
return binary_numeric_operation<u32, i32, Operators::Add>(configuration);
case Instructions::i32_sub.value():
return binary_numeric_operation<u32, i32, Operators::Subtract>(configuration);
case Instructions::i32_mul.value():
return binary_numeric_operation<u32, i32, Operators::Multiply>(configuration);
case Instructions::i32_divs.value():
return binary_numeric_operation<i32, i32, Operators::Divide>(configuration);
case Instructions::i32_divu.value():
return binary_numeric_operation<u32, i32, Operators::Divide>(configuration);
case Instructions::i32_rems.value():
return binary_numeric_operation<i32, i32, Operators::Modulo>(configuration);
case Instructions::i32_remu.value():
return binary_numeric_operation<u32, i32, Operators::Modulo>(configuration);
case Instructions::i32_and.value():
return binary_numeric_operation<i32, i32, Operators::BitAnd>(configuration);
case Instructions::i32_or.value():
return binary_numeric_operation<i32, i32, Operators::BitOr>(configuration);
case Instructions::i32_xor.value():
return binary_numeric_operation<i32, i32, Operators::BitXor>(configuration);
case Instructions::i32_shl.value():
return binary_numeric_operation<u32, i32, Operators::BitShiftLeft>(configuration);
case Instructions::i32_shrs.value():
return binary_numeric_operation<i32, i32, Operators::BitShiftRight>(configuration);
case Instructions::i32_shru.value():
return binary_numeric_operation<u32, i32, Operators::BitShiftRight>(configuration);
case Instructions::i32_rotl.value():
return binary_numeric_operation<u32, i32, Operators::BitRotateLeft>(configuration);
case Instructions::i32_rotr.value():
return binary_numeric_operation<u32, i32, Operators::BitRotateRight>(configuration);
case Instructions::i64_clz.value():
return unary_operation<i64, i64, Operators::CountLeadingZeros>(configuration);
case Instructions::i64_ctz.value():
return unary_operation<i64, i64, Operators::CountTrailingZeros>(configuration);
case Instructions::i64_popcnt.value():
return unary_operation<i64, i64, Operators::PopCount>(configuration);
case Instructions::i64_add.value():
return binary_numeric_operation<u64, i64, Operators::Add>(configuration);
case Instructions::i64_sub.value():
return binary_numeric_operation<u64, i64, Operators::Subtract>(configuration);
case Instructions::i64_mul.value():
return binary_numeric_operation<u64, i64, Operators::Multiply>(configuration);
case Instructions::i64_divs.value():
return binary_numeric_operation<i64, i64, Operators::Divide>(configuration);
case Instructions::i64_divu.value():
return binary_numeric_operation<u64, i64, Operators::Divide>(configuration);
case Instructions::i64_rems.value():
return binary_numeric_operation<i64, i64, Operators::Modulo>(configuration);
case Instructions::i64_remu.value():
return binary_numeric_operation<u64, i64, Operators::Modulo>(configuration);
case Instructions::i64_and.value():
return binary_numeric_operation<i64, i64, Operators::BitAnd>(configuration);
case Instructions::i64_or.value():
return binary_numeric_operation<i64, i64, Operators::BitOr>(configuration);
case Instructions::i64_xor.value():
return binary_numeric_operation<i64, i64, Operators::BitXor>(configuration);
case Instructions::i64_shl.value():
return binary_numeric_operation<u64, i64, Operators::BitShiftLeft>(configuration);
case Instructions::i64_shrs.value():
return binary_numeric_operation<i64, i64, Operators::BitShiftRight>(configuration);
case Instructions::i64_shru.value():
return binary_numeric_operation<u64, i64, Operators::BitShiftLeft>(configuration);
case Instructions::i64_rotl.value():
return binary_numeric_operation<u64, i64, Operators::BitRotateLeft>(configuration);
case Instructions::i64_rotr.value():
return binary_numeric_operation<u64, i64, Operators::BitRotateRight>(configuration);
case Instructions::f32_abs.value():
return unary_operation<float, float, Operators::Absolute>(configuration);
case Instructions::f32_neg.value():
return unary_operation<float, float, Operators::Negate>(configuration);
case Instructions::f32_ceil.value():
return unary_operation<float, float, Operators::Ceil>(configuration);
case Instructions::f32_floor.value():
return unary_operation<float, float, Operators::Floor>(configuration);
case Instructions::f32_trunc.value():
return unary_operation<float, float, Operators::Truncate>(configuration);
case Instructions::f32_nearest.value():
return unary_operation<float, float, Operators::Round>(configuration);
case Instructions::f32_sqrt.value():
return unary_operation<float, float, Operators::SquareRoot>(configuration);
case Instructions::f32_add.value():
return binary_numeric_operation<float, float, Operators::Add>(configuration);
case Instructions::f32_sub.value():
return binary_numeric_operation<float, float, Operators::Subtract>(configuration);
case Instructions::f32_mul.value():
return binary_numeric_operation<float, float, Operators::Multiply>(configuration);
case Instructions::f32_div.value():
return binary_numeric_operation<float, float, Operators::Divide>(configuration);
case Instructions::f32_min.value():
return binary_numeric_operation<float, float, Operators::Minimum>(configuration);
case Instructions::f32_max.value():
return binary_numeric_operation<float, float, Operators::Maximum>(configuration);
case Instructions::f32_copysign.value():
return binary_numeric_operation<float, float, Operators::CopySign>(configuration);
case Instructions::f64_abs.value():
return unary_operation<double, double, Operators::Absolute>(configuration);
case Instructions::f64_neg.value():
return unary_operation<double, double, Operators::Negate>(configuration);
case Instructions::f64_ceil.value():
return unary_operation<double, double, Operators::Ceil>(configuration);
case Instructions::f64_floor.value():
return unary_operation<double, double, Operators::Floor>(configuration);
case Instructions::f64_trunc.value():
return unary_operation<double, double, Operators::Truncate>(configuration);
case Instructions::f64_nearest.value():
return unary_operation<double, double, Operators::Round>(configuration);
case Instructions::f64_sqrt.value():
return unary_operation<double, double, Operators::SquareRoot>(configuration);
case Instructions::f64_add.value():
return binary_numeric_operation<double, double, Operators::Add>(configuration);
case Instructions::f64_sub.value():
return binary_numeric_operation<double, double, Operators::Subtract>(configuration);
case Instructions::f64_mul.value():
return binary_numeric_operation<double, double, Operators::Multiply>(configuration);
case Instructions::f64_div.value():
return binary_numeric_operation<double, double, Operators::Divide>(configuration);
case Instructions::f64_min.value():
return binary_numeric_operation<double, double, Operators::Minimum>(configuration);
case Instructions::f64_max.value():
return binary_numeric_operation<double, double, Operators::Maximum>(configuration);
case Instructions::f64_copysign.value():
return binary_numeric_operation<double, double, Operators::CopySign>(configuration);
case Instructions::i32_wrap_i64.value():
return unary_operation<i64, i32, Operators::Wrap<i32>>(configuration);
case Instructions::i32_trunc_sf32.value():
return unary_operation<float, i32, Operators::CheckedTruncate<i32>>(configuration);
case Instructions::i32_trunc_uf32.value():
return unary_operation<float, i32, Operators::CheckedTruncate<u32>>(configuration);
case Instructions::i32_trunc_sf64.value():
return unary_operation<double, i32, Operators::CheckedTruncate<i32>>(configuration);
case Instructions::i32_trunc_uf64.value():
return unary_operation<double, i32, Operators::CheckedTruncate<u32>>(configuration);
case Instructions::i64_trunc_sf32.value():
return unary_operation<float, i64, Operators::CheckedTruncate<i64>>(configuration);
case Instructions::i64_trunc_uf32.value():
return unary_operation<float, i64, Operators::CheckedTruncate<u64>>(configuration);
case Instructions::i64_trunc_sf64.value():
return unary_operation<double, i64, Operators::CheckedTruncate<i64>>(configuration);
case Instructions::i64_trunc_uf64.value():
return unary_operation<double, i64, Operators::CheckedTruncate<u64>>(configuration);
case Instructions::i64_extend_si32.value():
return unary_operation<i32, i64, Operators::Extend<i64>>(configuration);
case Instructions::i64_extend_ui32.value():
return unary_operation<u32, i64, Operators::Extend<i64>>(configuration);
case Instructions::f32_convert_si32.value():
return unary_operation<i32, float, Operators::Convert<float>>(configuration);
case Instructions::f32_convert_ui32.value():
return unary_operation<u32, float, Operators::Convert<float>>(configuration);
case Instructions::f32_convert_si64.value():
return unary_operation<i64, float, Operators::Convert<float>>(configuration);
case Instructions::f32_convert_ui64.value():
return unary_operation<u64, float, Operators::Convert<float>>(configuration);
case Instructions::f32_demote_f64.value():
return unary_operation<double, float, Operators::Demote>(configuration);
case Instructions::f64_convert_si32.value():
return unary_operation<i32, double, Operators::Convert<double>>(configuration);
case Instructions::f64_convert_ui32.value():
return unary_operation<u32, double, Operators::Convert<double>>(configuration);
case Instructions::f64_convert_si64.value():
return unary_operation<i64, double, Operators::Convert<double>>(configuration);
case Instructions::f64_convert_ui64.value():
return unary_operation<u64, double, Operators::Convert<double>>(configuration);
case Instructions::f64_promote_f32.value():
return unary_operation<float, double, Operators::Promote>(configuration);
case Instructions::i32_reinterpret_f32.value():
return unary_operation<float, i32, Operators::Reinterpret<i32>>(configuration);
case Instructions::i64_reinterpret_f64.value():
return unary_operation<double, i64, Operators::Reinterpret<i64>>(configuration);
case Instructions::f32_reinterpret_i32.value():
return unary_operation<i32, float, Operators::Reinterpret<float>>(configuration);
case Instructions::f64_reinterpret_i64.value():
return unary_operation<i64, double, Operators::Reinterpret<double>>(configuration);
case Instructions::i32_extend8_s.value():
return unary_operation<i32, i32, Operators::SignExtend<i8>>(configuration);
case Instructions::i32_extend16_s.value():
return unary_operation<i32, i32, Operators::SignExtend<i16>>(configuration);
case Instructions::i64_extend8_s.value():
return unary_operation<i64, i64, Operators::SignExtend<i8>>(configuration);
case Instructions::i64_extend16_s.value():
return unary_operation<i64, i64, Operators::SignExtend<i16>>(configuration);
case Instructions::i64_extend32_s.value():
return unary_operation<i64, i64, Operators::SignExtend<i32>>(configuration);
case Instructions::i32_trunc_sat_f32_s.value():
return unary_operation<float, i32, Operators::SaturatingTruncate<i32>>(configuration);
case Instructions::i32_trunc_sat_f32_u.value():
return unary_operation<float, i32, Operators::SaturatingTruncate<u32>>(configuration);
case Instructions::i32_trunc_sat_f64_s.value():
return unary_operation<double, i32, Operators::SaturatingTruncate<i32>>(configuration);
case Instructions::i32_trunc_sat_f64_u.value():
return unary_operation<double, i32, Operators::SaturatingTruncate<u32>>(configuration);
case Instructions::i64_trunc_sat_f32_s.value():
return unary_operation<float, i64, Operators::SaturatingTruncate<i64>>(configuration);
case Instructions::i64_trunc_sat_f32_u.value():
return unary_operation<float, i64, Operators::SaturatingTruncate<u64>>(configuration);
case Instructions::i64_trunc_sat_f64_s.value():
return unary_operation<double, i64, Operators::SaturatingTruncate<i64>>(configuration);
case Instructions::i64_trunc_sat_f64_u.value():
return unary_operation<double, i64, Operators::SaturatingTruncate<u64>>(configuration);
case Instructions::memory_init.value():
case Instructions::data_drop.value():
case Instructions::memory_copy.value():
case Instructions::memory_fill.value():
case Instructions::table_init.value():
case Instructions::elem_drop.value():
case Instructions::table_copy.value():
case Instructions::table_grow.value():
case Instructions::table_size.value():
case Instructions::table_fill.value():
default:
unimplemented:;
dbgln("Instruction '{}' not implemented", instruction_name(instruction.opcode()));
m_trap = Trap { String::formatted("Unimplemented instruction {}", instruction_name(instruction.opcode())) };
return;
}
}
void DebuggerBytecodeInterpreter::interpret(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction)
{
if (pre_interpret_hook) {
auto result = pre_interpret_hook(configuration, ip, instruction);
if (!result) {
m_trap = Trap { "Trapped by user request" };
return;
}
}
BytecodeInterpreter::interpret(configuration, ip, instruction);
if (post_interpret_hook) {
auto result = post_interpret_hook(configuration, ip, instruction, *this);
if (!result) {
m_trap = Trap { "Trapped by user request" };
return;
}
}
}
}
|