forked from kanaka/wac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wa.c
1911 lines (1750 loc) · 74.4 KB
/
wa.c
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
#include <stdlib.h>
#include <stdint.h>
//#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include "util.h"
#include "platform.h"
#include "thunk.h"
#include "wa.h"
char OPERATOR_INFO[][20] = {
// Control flow operators
"unreachable", // 0x00
"nop", // 0x01
"block", // 0x02
"loop", // 0x03
"if", // 0x04
"else", // 0x05
"RESERVED", // 0x06
"RESERVED", // 0x07
"RESERVED", // 0x08
"RESERVED", // 0x09
"RESERVED", // 0x0a
"end", // 0x0b
"br", // 0x0c
"br_if", // 0x0d
"br_table", // 0x0e
"return", // 0x0f
// Call operators
"call", // 0x10
"call_indirect", // 0x11
"RESERVED", // 0x12
"RESERVED", // 0x13
"RESERVED", // 0x14
"RESERVED", // 0x15
"RESERVED", // 0x16
"RESERVED", // 0x17
"RESERVED", // 0x18
"RESERVED", // 0x19
// Parametric operators
"drop", // 0x1a
"select", // 0x1b
"RESERVED", // 0x1c
"RESERVED", // 0x1d
"RESERVED", // 0x1e
"RESERVED", // 0x1f
// Variable access
"get_local", // 0x20
"set_local", // 0x21
"tee_local", // 0x22
"get_global", // 0x23
"set_global", // 0x24
"RESERVED", // 0x25
"RESERVED", // 0x26
"RESERVED", // 0x27
// Memory-related operator
"i32.load", // 0x28
"i64.load", // 0x29
"f32.load", // 0x2a
"f64.load", // 0x2b
"i32.load8_s", // 0x2c
"i32.load8_u", // 0x2d
"i32.load16_s", // 0x2e
"i32.load16_u", // 0x2f
"i64.load8_s", // 0x30
"i64.load8_u", // 0x31
"i64.load16_s", // 0x32
"i64.load16_u", // 0x33
"i64.load32_s", // 0x34
"i64.load32_u", // 0x35
"i32.store", // 0x36
"i64.store", // 0x37
"f32.store", // 0x38
"f64.store", // 0x39
"i32.store8", // 0x3a
"i32.store16", // 0x3b
"i64.store8", // 0x3c
"i64.store16", // 0x3d
"i64.store32", // 0x3e
"current_memory", // 0x3f
"grow_memory", // 0x40
// Constants
"i32.const", // 0x41
"i64.const", // 0x42
"f32.const", // 0x43
"f64.const", // 0x44
// Comparison operators
"i32.eqz", // 0x45
"i32.eq", // 0x46
"i32.ne", // 0x47
"i32.lt_s", // 0x48
"i32.lt_u", // 0x49
"i32.gt_s", // 0x4a
"i32.gt_u", // 0x4b
"i32.le_s", // 0x4c
"i32.le_u", // 0x4d
"i32.ge_s", // 0x4e
"i32.ge_u", // 0x4f
"i64.eqz", // 0x50
"i64.eq", // 0x51
"i64.ne", // 0x52
"i64.lt_s", // 0x53
"i64.lt_u", // 0x54
"i64.gt_s", // 0x55
"i64.gt_u", // 0x56
"i64.le_s", // 0x57
"i64.le_u", // 0x58
"i64.ge_s", // 0x59
"i64.ge_u", // 0x5a
"f32.eq", // 0x5b
"f32.ne", // 0x5c
"f32.lt", // 0x5d
"f32.gt", // 0x5e
"f32.le", // 0x5f
"f32.ge", // 0x60
"f64.eq", // 0x61
"f64.ne", // 0x62
"f64.lt", // 0x63
"f64.gt", // 0x64
"f64.le", // 0x65
"f64.ge", // 0x66
// Numeric operators
"i32.clz", // 0x67
"i32.ctz", // 0x68
"i32.popcnt", // 0x69
"i32.add", // 0x6a
"i32.sub", // 0x6b
"i32.mul", // 0x6c
"i32.div_s", // 0x6d
"i32.div_u", // 0x6e
"i32.rem_s", // 0x6f
"i32.rem_u", // 0x70
"i32.and", // 0x71
"i32.or", // 0x72
"i32.xor", // 0x73
"i32.shl", // 0x74
"i32.shr_s", // 0x75
"i32.shr_u", // 0x76
"i32.rotl", // 0x77
"i32.rotr", // 0x78
"i64.clz", // 0x79
"i64.ctz", // 0x7a
"i64.popcnt", // 0x7b
"i64.add", // 0x7c
"i64.sub", // 0x7d
"i64.mul", // 0x7e
"i64.div_s", // 0x7f
"i64.div_u", // 0x80
"i64.rem_s", // 0x81
"i64.rem_u", // 0x82
"i64.and", // 0x83
"i64.or", // 0x84
"i64.xor", // 0x85
"i64.shl", // 0x86
"i64.shr_s", // 0x87
"i64.shr_u", // 0x88
"i64.rotl", // 0x89
"i64.rotr", // 0x8a
"f32.abs", // 0x8b
"f32.neg", // 0x8c
"f32.ceil", // 0x8d
"f32.floor", // 0x8e
"f32.trunc", // 0x8f
"f32.nearest", // 0x90
"f32.sqrt", // 0x91
"f32.add", // 0x92
"f32.sub", // 0x93
"f32.mul", // 0x94
"f32.div", // 0x95
"f32.min", // 0x96
"f32.max", // 0x97
"f32.copysign", // 0x98
"f64.abs", // 0x99
"f64.neg", // 0x9a
"f64.ceil", // 0x9b
"f64.floor", // 0x9c
"f64.trunc", // 0x9d
"f64.nearest", // 0x9e
"f64.sqrt", // 0x9f
"f64.add", // 0xa0
"f64.sub", // 0xa1
"f64.mul", // 0xa2
"f64.div", // 0xa3
"f64.min", // 0xa4
"f64.max", // 0xa5
"f64.copysign", // 0xa6
// Conversions
"i32.wrap/i64", // 0xa7
"i32.trunc_s/f32", // 0xa8
"i32.trunc_u/f32", // 0xa9
"i32.trunc_s/f64", // 0xaa
"i32.trunc_u/f64", // 0xab
"i64.extend_s/i32", // 0xac
"i64.extend_u/i32", // 0xad
"i64.trunc_s/f32", // 0xae
"i64.trunc_u/f32", // 0xaf
"i64.trunc_s/f64", // 0xb0
"i64.trunc_u/f64", // 0xb1
"f32.convert_s/i32", // 0xb2
"f32.convert_u/i32", // 0xb3
"f32.convert_s/i64", // 0xb4
"f32.convert_u/i64", // 0xb5
"f32.demote/f64", // 0xb6
"f64.convert_s/i32", // 0xb7
"f64.convert_u/i32", // 0xb8
"f64.convert_s/i64", // 0xb9
"f64.convert_u/i64", // 0xba
"f64.promote/f32", // 0xbb
// Reinterpretations
"i32.reinterpret/f32", // 0xbc
"i64.reinterpret/f64", // 0xbd
"f32.reinterpret/i32", // 0xbe
"f64.reinterpret/i64" // 0xbf
};
// Size of memory load.
// This starts with the first memory load operator at opcode 0x28
uint32_t LOAD_SIZE[] = {
4, 8, 4, 8, 1, 1, 2, 2, 1, 1, 2, 2, 4, 4, // loads
4, 8, 4, 8, 1, 2, 1, 2, 4}; // stores
// global exception message
char exception[4096];
// Static definition of block_types
uint32_t block_type_results[4][1] = {{I32}, {I64}, {F32}, {F64}};
Type block_types[5] = {
{ .form = BLOCK, .result_count = 0, },
{ .form = BLOCK, .result_count = 1, .results = block_type_results[0], },
{ .form = BLOCK, .result_count = 1, .results = block_type_results[1], },
{ .form = BLOCK, .result_count = 1, .results = block_type_results[2], },
{ .form = BLOCK, .result_count = 1, .results = block_type_results[3], }
};
Type *get_block_type(uint8_t value_type) {
switch (value_type) {
case 0x40: return &block_types[0];
case I32: return &block_types[1];
case I64: return &block_types[2];
case F32: return &block_types[3];
case F64: return &block_types[4];
default: FATAL("invalid block_type value_type: %d\n", value_type);
return NULL;
}
}
// TODO: calculate this while parsing types
uint64_t get_type_mask(Type *type) {
uint64_t mask = 0x80;
if (type->result_count == 1) {
mask |= 0x80 - type->results[0];
}
mask = mask << 4;
for(uint32_t p=0; p<type->param_count; p++) {
mask = ((uint64_t)mask) << 4;
mask |= 0x80 - type->params[p];
}
return mask;
}
char _value_str[256];
char *value_repr(StackValue *v) {
switch (v->value_type) {
case I32: snprintf(_value_str, 255, "0x%x:i32", v->value.uint32); break;
case I64: snprintf(_value_str, 255, "0x%I64u:i64", v->value.uint64); break;
case F32: snprintf(_value_str, 255, "%.7g:f32", v->value.f32); break;
case F64: snprintf(_value_str, 255, "%.7g:f64", v->value.f64); break;
}
return _value_str;
}
char _block_str[1024];
char *block_repr(Block *b) {
if (b->block_type == 0) {
snprintf(_block_str, 1023,
"fn0x%x<%d/%d->%d>", b->fidx, b->type->param_count,
b->local_count, b->type->result_count);
} else {
snprintf(_block_str, 1023, "%s<0/0->%d>",
b->block_type == 0x01 ? "init" :
b->block_type == 0x02 ? "block" :
b->block_type == 0x03 ? "loop" : "if",
b->type->result_count);
}
return _block_str;
}
void dump_stacks(Module *m) {
warn(" * stack: [");
for (int i=0; i<=m->sp; i++) {
if (i == m->fp) { warn("* "); }
warn("%s", value_repr(&m->stack[i]));
if (i != m->sp) { warn(" "); }
}
warn("]\n");
warn(" * callstack: [");
for (int i=0; i<=m->csp; i++) {
Frame *f = &m->callstack[i]; (void)f;
warn("%s(sp:%d/fp:%d/ra:0x%x)", block_repr(f->block), f->sp, f->fp,
f->ra);
if (i != m->csp) { warn(" "); }
}
warn("]\n");
}
void parse_table_type(Module *m, uint32_t *pos) {
m->table.elem_type = read_LEB(m->bytes, pos, 7);
ASSERT(m->table.elem_type == ANYFUNC,
"Table elem_type 0x%x unsupported",
m->table.elem_type);
uint32_t flags = read_LEB(m->bytes, pos, 32);
uint32_t tsize = read_LEB(m->bytes, pos, 32); // Initial size
m->table.initial = tsize;
m->table.size = tsize;
// Limit maximum to 64K
if (flags & 0x1) {
tsize = read_LEB(m->bytes, pos, 32); // Max size
m->table.maximum = 0x10000 < tsize ? 0x10000 : tsize;
} else {
m->table.maximum = 0x10000;
}
debug(" table size: %d\n", tsize);
}
void parse_memory_type(Module *m, uint32_t *pos) {
uint32_t flags = read_LEB(m->bytes, pos, 32);
uint32_t pages = read_LEB(m->bytes, pos, 32); // Initial size
m->memory.initial = pages;
m->memory.pages = pages;
// Limit the maximum to 2GB
if (flags & 0x1) {
pages = read_LEB(m->bytes, pos, 32); // Max size
m->memory.maximum = (uint32_t)fmin(0x8000, pages);
} else {
m->memory.maximum = 0x8000;
}
}
void skip_immediates(uint8_t *bytes, uint32_t *pos) {
uint32_t count, opcode = bytes[*pos];
*pos = *pos+1;
switch (opcode) {
// varuint1
case 0x3f ... 0x40: // current_memory, grow_memory
read_LEB(bytes, pos, 1); break;
// varuint32, varint32
case 0x0c ... 0x0d: // br, br_if
case 0x10: // call
case 0x20 ... 0x24: // get/set_local, tee_local, get/set_global
case 0x41: // i32.const
read_LEB(bytes, pos, 32); break;
// varuint32 + varuint1
case 0x11: // call_indirect
read_LEB(bytes, pos, 1); read_LEB(bytes, pos, 32); break;
// varint64
case 0x42: // i64.const
read_LEB(bytes, pos, 64); break;
// uint32
case 0x43: // f32.const
*pos += 4; break;
// uint64
case 0x44: // f64.const
*pos += 8; break;
// block_type
case 0x02 ... 0x04: // block, loop, if
read_LEB(bytes, pos, 7); break;
// memory_immediate
case 0x28 ... 0x3e: // *.load*, *.store*
read_LEB(bytes, pos, 32); read_LEB(bytes, pos, 32); break;
// br_table
case 0x0e: // br_table
count = read_LEB(bytes, pos, 32); // target count
for (uint32_t i=0; i<count; i++) {
read_LEB(bytes, pos, 32);
}
read_LEB(bytes, pos, 32); // default target
break;
default: // no immediates
break;
}
}
void find_blocks(Module *m) {
Block *function;
Block *block;
Block *blockstack[BLOCKSTACK_SIZE];
int top = -1;
uint8_t opcode = 0x00;
info(" find_blocks: function_count: %d\n", m->function_count);
for (uint32_t f=m->import_count; f<m->function_count; f++) {
function = &m->functions[f];
debug(" fidx: 0x%x, start: 0x%x, end: 0x%x\n",
f, function->start_addr, function->end_addr);
uint32_t pos = function->start_addr;
while (pos <= function->end_addr) {
opcode = m->bytes[pos];
switch (opcode) {
case 0x02: // block
case 0x03: // loop
case 0x04: // if
block = acalloc(1, sizeof(Block), "Block");
block->block_type = opcode;
block->type = get_block_type(m->bytes[pos+1]);
block->start_addr = pos;
blockstack[++top] = block;
m->block_lookup[pos] = block;
break;
case 0x05: // else
ASSERT(blockstack[top]->block_type == 0x04,
"else not matched with if")
blockstack[top]->else_addr = pos+1;
break;
case 0x0b: // end
if (pos == function->end_addr) { break; }
ASSERT(top >= 0, "blockstack underflow");
block = blockstack[top--];
block->end_addr = pos;
if (block->block_type == 0x03) {
// loop: label after start
block->br_addr = block->start_addr+2;
} else {
// block, if: label at end
block->br_addr = pos;
}
debug(" block start: 0x%x, end: 0x%x,"
" br_addr: 0x%x, else_addr: 0x%x\n",
block->start_addr, block->end_addr, block->br_addr,
block->else_addr);
break;
}
skip_immediates(m->bytes, &pos);
}
ASSERT(top == -1, "Function ended in middle of block\n")
ASSERT(opcode == 0x0b, "Function block did not end with 0xb\n")
}
}
//
// Stack machine (byte code related functions)
//
void push_block(Module *m, Block *block, int sp) {
m->csp += 1;
m->callstack[m->csp].block = block;
m->callstack[m->csp].sp = sp;
m->callstack[m->csp].fp = m->fp;
m->callstack[m->csp].ra = m->pc;
}
Block *pop_block(Module *m) {
Frame *frame = &m->callstack[m->csp--];
Type *t = frame->block->type;
// TODO: validate return value if there is one
m->fp = frame->fp; // Restore frame pointer
// Validate the return value
if (t->result_count == 1) {
if (m->stack[m->sp].value_type != t->results[0]) {
sprintf(exception, "call type mismatch");
return NULL;
}
}
// Restore stack pointer
if (t->result_count == 1) {
// Save top value as result
if (frame->sp < m->sp) {
m->stack[frame->sp+1] = m->stack[m->sp];
m->sp = frame->sp+1;
}
} else {
if (frame->sp < m->sp) {
m->sp = frame->sp;
}
}
if (frame->block->block_type == 0x00) {
// Function, set pc to return address
m->pc = frame->ra;
}
return frame->block;
}
// Setup a function
// Push params and locals on the stack and save a call frame on the call stack
// Sets new pc value for the start of the function
void setup_call(Module *m, uint32_t fidx) {
Block *func = &m->functions[fidx];
Type *type = func->type;
// Push current frame on the call stack
push_block(m, func, m->sp - type->param_count);
if (TRACE) {
warn(" >> fn0x%x(%d) %s(",
fidx, fidx, func->export_name ? func->export_name : "");
for (int p=type->param_count-1; p >= 0; p--) {
warn("%s%s", value_repr(&m->stack[m->sp-p]),
p ? " " : "");
}
warn("), %d locals, %d results\n",
func->local_count, type->result_count);
}
// Push locals (dropping extras)
m->fp = m->sp - type->param_count + 1;
// TODO: validate arguments vs formal params
// Push function locals
for (uint32_t lidx=0; lidx<func->local_count; lidx++) {
m->sp += 1;
m->stack[m->sp].value_type = func->locals[lidx];
m->stack[m->sp].value.uint64 = 0; // Initialize whole union to 0
}
// Set program counter to start of function
m->pc = func->start_addr;
return;
}
bool interpret(Module *m) {
uint8_t *bytes = m->bytes;
StackValue *stack = m->stack;
uint32_t cur_pc;
Block *block;
uint32_t arg, val, fidx, tidx, cond, depth, count;
uint32_t flags, offset, addr;
uint8_t *maddr, *mem_end;
//uint32_t *depths;
uint8_t opcode;
uint32_t a, b, c; // I32 math
uint64_t d, e, f; // I64 math
float g, h, i; // F32 math
double j, k, l; // F64 math
bool overflow = false;
while (m->pc < m->byte_count) {
opcode = bytes[m->pc];
cur_pc = m->pc;
m->pc += 1;
if (TRACE) {
if (DEBUG) { dump_stacks(m); }
info(" 0x%x <0x%x/%s>\n", cur_pc, opcode, OPERATOR_INFO[opcode]);
}
switch (opcode) {
//
// Control flow operators
//
case 0x00: // unreachable
sprintf(exception, "%s", "unreachable");
return false;
case 0x01: // nop
continue;
case 0x02: // block
read_LEB(bytes, &m->pc, 32); // ignore block type
if (m->csp >= CALLSTACK_SIZE) {
sprintf(exception, "call stack exhausted");
return false;
}
push_block(m, m->block_lookup[cur_pc], m->sp);
continue;
case 0x03: // loop
read_LEB(bytes, &m->pc, 32); // ignore block type
if (m->csp >= CALLSTACK_SIZE) {
sprintf(exception, "call stack exhausted");
return false;
}
push_block(m, m->block_lookup[cur_pc], m->sp);
continue;
case 0x04: // if
read_LEB(bytes, &m->pc, 32); // ignore block type
block = m->block_lookup[cur_pc];
if (m->csp >= CALLSTACK_SIZE) {
sprintf(exception, "call stack exhausted");
return false;
}
push_block(m, block, m->sp);
cond = stack[m->sp--].value.uint32;
if (cond == 0) { // if false (I32)
// branch to else block or after end of if
if (block->else_addr == 0) {
// no else block, pop if block and skip end
m->csp -= 1;
m->pc = block->br_addr+1;
} else {
m->pc = block->else_addr;
}
}
// if true, keep going
if (TRACE) {
debug(" - cond: 0x%x jump to 0x%x, block: %s\n",
cond, m->pc, block_repr(block));
}
continue;
case 0x05: // else
block = m->callstack[m->csp].block;
m->pc = block->br_addr;
if (TRACE) {
debug(" - of %s jump to 0x%x\n", block_repr(block), m->pc);
}
continue;
case 0x0b: // end
block = pop_block(m);
if (block == NULL) {
return false; // an exception (set by pop_block)
}
if (TRACE) { debug(" - of %s\n", block_repr(block)); }
if (block->block_type == 0x00) { // Function
if (TRACE) {
warn(" << fn0x%x(%d) %s = %s\n",
block->fidx, block->fidx,
block->export_name ? block->export_name : "",
block->type->result_count > 0 ?
value_repr(&m->stack[m->sp]) :
"_");
}
if (m->csp == -1) {
// Return to top-level
return true;
} else {
// Keep going at return address
}
} else if (block->block_type == 0x01) { // init_expr
return true;
} else { // Block
// End of block/loop/if, keep going
}
continue;
case 0x0c: // br
depth = read_LEB(bytes, &m->pc, 32);
m->csp -= depth;
// set to end for pop_block
m->pc = m->callstack[m->csp].block->br_addr;
if (TRACE) { debug(" - to: 0x%x\n", &m->pc); }
continue;
case 0x0d: // br_if
depth = read_LEB(bytes, &m->pc, 32);
cond = stack[m->sp--].value.uint32;
if (cond) { // if true
m->csp -= depth;
// set to end for pop_block
m->pc = m->callstack[m->csp].block->br_addr;
}
if (TRACE) { debug(" - depth: 0x%x, cond: 0x%x, to: 0x%x\n", depth, cond, m->pc); }
continue;
case 0x0e: // br_table
count = read_LEB(bytes, &m->pc, 32);
if (count > BR_TABLE_SIZE) {
// TODO: check this prior to runtime
sprintf(exception, "br_table size %d exceeds max %d\n",
count, BR_TABLE_SIZE);
return false;
}
for(uint32_t i=0; i<count; i++) {
m->br_table[i] = read_LEB(bytes, &m->pc, 32);
}
depth = read_LEB(bytes, &m->pc, 32);
int32_t didx = stack[m->sp--].value.int32;
if (didx >= 0 && didx < (int32_t)count) {
depth = m->br_table[didx];
}
m->csp -= depth;
// set to end for pop_block
m->pc = m->callstack[m->csp].block->br_addr;
if (TRACE) {
debug(" - count: %d, didx: %d, to: 0x%x\n", count, didx, m->pc);
}
continue;
case 0x0f: // return
while (m->csp >= 0 &&
m->callstack[m->csp].block->block_type != 0x00) {
m->csp--;
}
// Set the program count to the end of the function
// The actual pop_block and return is handled by the end opcode.
m->pc = m->callstack[0].block->end_addr;
if (TRACE) {
debug(" - to: 0x%x\n", m->pc);
}
continue;
//
// Call operators
//
case 0x10: // call
fidx = read_LEB(bytes, &m->pc, 32);
if (fidx < m->import_count) {
thunk_out(m, fidx); // import/thunk call
} else {
if (m->csp >= CALLSTACK_SIZE) {
sprintf(exception, "call stack exhausted");
return false;
}
setup_call(m, fidx); // regular function call
if (TRACE) {
debug(" - calling function fidx: %d at: 0x%x\n", fidx, m->pc);
}
}
continue;
case 0x11: // call_indirect
tidx = read_LEB(bytes, &m->pc, 32); // TODO: use tidx?
(void)tidx;
read_LEB(bytes, &m->pc, 1); // reserved immediate
val = stack[m->sp--].value.uint32;
if (m->options.mangle_table_index) {
// val is the table address + the index (not sized for the
// pointer size) so get the actual (sized) index
if (TRACE) {
debug(" - entries: %p, original val: 0x%x, new val: 0x%x\n",
m->table.entries, val, (uint32_t)m->table.entries - val);
}
//val = val - (uint32_t)((uint64_t)m->table.entries & 0xFFFFFFFF);
val = val - (uint32_t)m->table.entries;
}
if (val >= m->table.maximum) {
sprintf(exception, "undefined element 0x%x (max: 0x%x) in table",
val, m->table.maximum);
return false;
}
fidx = m->table.entries[val];
if (TRACE) {
debug(" - call_indirect tidx: %d, val: 0x%x, fidx: 0x%x\n",
tidx, val, fidx);
}
if (fidx < m->import_count) {
thunk_out(m, fidx); // import/thunk call
} else {
Block *func = &m->functions[fidx];
Type *ftype = func->type;
if (m->csp >= CALLSTACK_SIZE) {
sprintf(exception, "call stack exhausted");
return false;
}
if (ftype->mask != m->types[tidx].mask) {
sprintf(exception, "indirect call type mismatch (call type and function type differ)");
return false;
}
setup_call(m, fidx); // regular function call
// Validate signatures match
if (ftype->param_count + func->local_count != m->sp - m->fp + 1) {
sprintf(exception, "indirect call type mismatch (param counts differ)");
return false;
}
for (uint32_t f=0; f<ftype->param_count; f++) {
if (ftype->params[f] != m->stack[m->fp+f].value_type) {
sprintf(exception, "indirect call type mismatch (param types differ)");
return false;
}
}
if (TRACE) {
debug(" - tidx: %d, table idx: %d, "
"calling function fidx: %d at: 0x%x\n",
tidx, val, fidx, m->pc);
}
}
continue;
//
// Parametric operators
//
case 0x1a: // drop
m->sp--;
continue;
case 0x1b: // select
cond = stack[m->sp--].value.uint32;
m->sp--;
if (!cond) { // use a instead of b
stack[m->sp] = stack[m->sp+1];
}
continue;
//
// Variable access
//
case 0x20: // get_local
arg = read_LEB(bytes, &m->pc, 32);
if (TRACE) {
debug(" - arg: 0x%x, got %s\n",
arg, value_repr(&stack[m->fp+arg]));
}
stack[++m->sp] = stack[m->fp+arg];
continue;
case 0x21: // set_local
arg = read_LEB(bytes, &m->pc, 32);
stack[m->fp+arg] = stack[m->sp--];
if (TRACE) {
debug(" - arg: 0x%x, to %s\n",
arg, value_repr(&stack[m->sp]));
}
continue;
case 0x22: // tee_local
arg = read_LEB(bytes, &m->pc, 32);
stack[m->fp+arg] = stack[m->sp];
if (TRACE) {
debug(" - arg: 0x%x, to %s\n",
arg, value_repr(&stack[m->sp]));
}
continue;
case 0x23: // get_global
arg = read_LEB(bytes, &m->pc, 32);
if (TRACE) {
debug(" - arg: 0x%x, got %s\n",
arg, value_repr(&m->globals[arg]));
}
stack[++m->sp] = m->globals[arg];
continue;
case 0x24: // set_global
arg = read_LEB(bytes, &m->pc, 32);
m->globals[arg] = stack[m->sp--];
if (TRACE) {
debug(" - arg: 0x%x, to %s\n",
arg, value_repr(&m->globals[arg]));
}
continue;
//
// Memory-related operators
//
case 0x3f: // current_memory
read_LEB(bytes, &m->pc, 32); // ignore reserved
stack[++m->sp].value_type = I32;
stack[m->sp].value.uint32 = m->memory.pages;
continue;
case 0x40: // grow_memory
read_LEB(bytes, &m->pc, 32); // ignore reserved
uint32_t prev_pages = m->memory.pages;
uint32_t delta = stack[m->sp].value.uint32;
stack[m->sp].value.uint32 = prev_pages;
if (delta == 0) {
continue; // No change
} else if (delta+prev_pages > m->memory.maximum) {
stack[m->sp].value.uint32 = -1;
continue;
}
m->memory.pages += delta;
m->memory.bytes = arecalloc(m->memory.bytes,
prev_pages*PAGE_SIZE,
m->memory.pages*PAGE_SIZE,
sizeof(uint32_t),
"Module->memory.bytes");
continue;
// Memory load operators
case 0x28 ... 0x35:
flags = read_LEB(bytes, &m->pc, 32);
offset = read_LEB(bytes, &m->pc, 32);
addr = stack[m->sp--].value.uint32;
if (flags != 2 && TRACE) {
info(" - unaligned load - flags: 0x%x,"
" offset: 0x%x, addr: 0x%x\n",
flags, offset, addr);
}
if (offset+addr < addr) { overflow = true; }
maddr = m->memory.bytes+offset+addr;
if (maddr < m->memory.bytes) { overflow = true; }
mem_end = m->memory.bytes+m->memory.pages*(uint32_t)PAGE_SIZE;
if (maddr+LOAD_SIZE[opcode-0x28] > mem_end) {
overflow = true;
}
info(" - addr: 0x%x, offset: 0x%x, maddr: %p, mem_end: %p\n",
addr, offset, maddr, mem_end);
if (!m->options.disable_memory_bounds) {
if (overflow) {
warn("memory start: %p, memory end: %p, maddr: %p\n",
m->memory.bytes, mem_end, maddr);
sprintf(exception, "out of bounds memory access");
return false;
}
}
stack[++m->sp].value.uint64 = 0; // initialize to 0
switch (opcode) {
case 0x28: memcpy(&stack[m->sp].value, maddr, 4);
stack[m->sp].value_type = I32; break; // i32.load
case 0x29: memcpy(&stack[m->sp].value, maddr, 8);
stack[m->sp].value_type = I64; break; // i64.load
case 0x2a: memcpy(&stack[m->sp].value, maddr, 4);
stack[m->sp].value_type = F32; break; // f32.load
case 0x2b: memcpy(&stack[m->sp].value, maddr, 8);
stack[m->sp].value_type = F64; break; // f64.load
case 0x2c: memcpy(&stack[m->sp].value, maddr, 1);
sext_8_32(&stack[m->sp].value.uint32);
stack[m->sp].value_type = I32;
break; // i32.load8_s
case 0x2d: memcpy(&stack[m->sp].value, maddr, 1);
stack[m->sp].value_type = I32; break; // i32.load8_u
case 0x2e: memcpy(&stack[m->sp].value, maddr, 2);
sext_16_32(&stack[m->sp].value.uint32);
stack[m->sp].value_type = I32; break; // i32.load16_s
case 0x2f: memcpy(&stack[m->sp].value, maddr, 2);
stack[m->sp].value_type = I32; break; // i32.load16_u
case 0x30: memcpy(&stack[m->sp].value, maddr, 1);
sext_8_64(&stack[m->sp].value.uint64);
stack[m->sp].value_type = I64; break; // i64.load8_s
case 0x31: memcpy(&stack[m->sp].value, maddr, 1);
stack[m->sp].value_type = I64; break; // i64.load8_u
case 0x32: memcpy(&stack[m->sp].value, maddr, 2);
sext_16_64(&stack[m->sp].value.uint64);
stack[m->sp].value_type = I64; break; // i64.load16_s
case 0x33: memcpy(&stack[m->sp].value, maddr, 2);
stack[m->sp].value_type = I64; break; // i64.load16_u
case 0x34: memcpy(&stack[m->sp].value, maddr, 4);
sext_32_64(&stack[m->sp].value.uint64);
stack[m->sp].value_type = I64; break; // i64.load32_s
case 0x35: memcpy(&stack[m->sp].value, maddr, 4);
stack[m->sp].value_type = I64; break; // i64.load32_u
}
continue;
// Memory store operators
case 0x36 ... 0x3e:
flags = read_LEB(bytes, &m->pc, 32);
offset = read_LEB(bytes, &m->pc, 32);
StackValue *sval = &stack[m->sp--];
addr = stack[m->sp--].value.uint32;
if (flags != 2 && TRACE) {
info(" - unaligned store - flags: 0x%x,"
" offset: 0x%x, addr: 0x%x, val: %s\n",
flags, offset, addr, value_repr(sval));
}
if (offset+addr < addr) { overflow = true; }
maddr = m->memory.bytes+offset+addr;
if (maddr < m->memory.bytes) { overflow = true; }
mem_end = m->memory.bytes+m->memory.pages*(uint32_t)PAGE_SIZE;
if (maddr+LOAD_SIZE[opcode-0x28] > mem_end) {
overflow = true;
}
info(" - addr: 0x%x, offset: 0x%x, maddr: %p, mem_end: %p, value: %s\n",
addr, offset, maddr, mem_end, value_repr(sval));
if (!m->options.disable_memory_bounds) {
if (overflow) {
warn("memory start: %p, memory end: %p, maddr: %p\n",
m->memory.bytes, mem_end, maddr);
sprintf(exception, "out of bounds memory access");
return false;
}
}
switch (opcode) {
case 0x36: memcpy(maddr, &sval->value.uint32, 4); break; // i32.store
case 0x37: memcpy(maddr, &sval->value.uint64, 8); break; // i64.store
case 0x38: memcpy(maddr, &sval->value.f32, 4); break; // f32.store
case 0x39: memcpy(maddr, &sval->value.f64, 8); break; // f64.store
case 0x3a: memcpy(maddr, &sval->value.uint32, 1); break; // i32.store8
case 0x3b: memcpy(maddr, &sval->value.uint32, 2); break; // i32.store16
case 0x3c: memcpy(maddr, &sval->value.uint64, 1); break; // i64.store8
case 0x3d: memcpy(maddr, &sval->value.uint64, 2); break; // i64.store16
case 0x3e: memcpy(maddr, &sval->value.uint64, 4); break; // i64.store32
}
continue;
//