-
Notifications
You must be signed in to change notification settings - Fork 578
/
wasm_loader.c
15815 lines (14276 loc) · 581 KB
/
wasm_loader.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
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_loader.h"
#include "bh_common.h"
#include "bh_log.h"
#include "wasm.h"
#include "wasm_opcode.h"
#include "wasm_runtime.h"
#include "wasm_loader_common.h"
#include "../common/wasm_native.h"
#include "../common/wasm_memory.h"
#if WASM_ENABLE_GC != 0
#include "../common/gc/gc_type.h"
#include "../common/gc/gc_object.h"
#endif
#if WASM_ENABLE_DEBUG_INTERP != 0
#include "../libraries/debug-engine/debug_engine.h"
#endif
#if WASM_ENABLE_FAST_JIT != 0
#include "../fast-jit/jit_compiler.h"
#include "../fast-jit/jit_codecache.h"
#endif
#if WASM_ENABLE_JIT != 0
#include "../compilation/aot_llvm.h"
#endif
#ifndef TRACE_WASM_LOADER
#define TRACE_WASM_LOADER 0
#endif
/* Read a value of given type from the address pointed to by the given
pointer and increase the pointer to the position just after the
value being read. */
#define TEMPLATE_READ_VALUE(Type, p) \
(p += sizeof(Type), *(Type *)(p - sizeof(Type)))
#if WASM_ENABLE_MEMORY64 != 0
static bool
has_module_memory64(WASMModule *module)
{
/* TODO: multi-memories for now assuming the memory idx type is consistent
* across multi-memories */
if (module->import_memory_count > 0)
return !!(module->import_memories[0].u.memory.mem_type.flags
& MEMORY64_FLAG);
else if (module->memory_count > 0)
return !!(module->memories[0].flags & MEMORY64_FLAG);
return false;
}
#endif
static void
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
{
if (error_buf != NULL) {
snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
string);
}
}
#if WASM_ENABLE_MEMORY64 != 0
static void
set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
{
if (error_buf != NULL) {
snprintf(error_buf, error_buf_size, "offset out of range");
}
}
#endif
static void
set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
{
va_list args;
char buf[128];
if (error_buf != NULL) {
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
}
}
static bool
check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
char *error_buf, uint32 error_buf_size)
{
if ((uintptr_t)buf + length < (uintptr_t)buf
|| (uintptr_t)buf + length > (uintptr_t)buf_end) {
set_error_buf(error_buf, error_buf_size,
"unexpected end of section or function");
return false;
}
return true;
}
static bool
check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
char *error_buf, uint32 error_buf_size)
{
if ((uintptr_t)buf + length < (uintptr_t)buf
|| (uintptr_t)buf + length > (uintptr_t)buf_end) {
set_error_buf(error_buf, error_buf_size, "unexpected end");
return false;
}
return true;
}
#define CHECK_BUF(buf, buf_end, length) \
do { \
if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
goto fail; \
} \
} while (0)
#define CHECK_BUF1(buf, buf_end, length) \
do { \
if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
goto fail; \
} \
} while (0)
#define skip_leb(p) while (*p++ & 0x80)
#define skip_leb_int64(p, p_end) skip_leb(p)
#define skip_leb_uint32(p, p_end) skip_leb(p)
#define skip_leb_int32(p, p_end) skip_leb(p)
#define skip_leb_mem_offset(p, p_end) skip_leb(p)
static bool
read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
uint64 *p_result, char *error_buf, uint32 error_buf_size)
{
const uint8 *buf = *p_buf;
uint64 result = 0;
uint32 shift = 0;
uint32 offset = 0, bcnt = 0;
uint64 byte;
while (true) {
/* uN or SN must not exceed ceil(N/7) bytes */
if (bcnt + 1 > (maxbits + 6) / 7) {
set_error_buf(error_buf, error_buf_size,
"integer representation too long");
return false;
}
CHECK_BUF(buf, buf_end, offset + 1);
byte = buf[offset];
offset += 1;
result |= ((byte & 0x7f) << shift);
shift += 7;
bcnt += 1;
if ((byte & 0x80) == 0) {
break;
}
}
if (!sign && maxbits == 32 && shift >= maxbits) {
/* The top bits set represent values > 32 bits */
if (((uint8)byte) & 0xf0)
goto fail_integer_too_large;
}
else if (sign && maxbits == 32) {
if (shift < maxbits) {
/* Sign extend, second-highest bit is the sign bit */
if ((uint8)byte & 0x40)
result |= (~((uint64)0)) << shift;
}
else {
/* The top bits should be a sign-extension of the sign bit */
bool sign_bit_set = ((uint8)byte) & 0x8;
int top_bits = ((uint8)byte) & 0xf0;
if ((sign_bit_set && top_bits != 0x70)
|| (!sign_bit_set && top_bits != 0))
goto fail_integer_too_large;
}
}
else if (sign && maxbits == 64) {
if (shift < maxbits) {
/* Sign extend, second-highest bit is the sign bit */
if ((uint8)byte & 0x40)
result |= (~((uint64)0)) << shift;
}
else {
/* The top bits should be a sign-extension of the sign bit */
bool sign_bit_set = ((uint8)byte) & 0x1;
int top_bits = ((uint8)byte) & 0xfe;
if ((sign_bit_set && top_bits != 0x7e)
|| (!sign_bit_set && top_bits != 0))
goto fail_integer_too_large;
}
}
*p_buf += offset;
*p_result = result;
return true;
fail_integer_too_large:
set_error_buf(error_buf, error_buf_size, "integer too large");
fail:
return false;
}
#define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
#define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
#define read_leb_int64(p, p_end, res) \
do { \
uint64 res64; \
if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
error_buf_size)) \
goto fail; \
res = (int64)res64; \
} while (0)
#if WASM_ENABLE_MEMORY64 != 0
#define read_leb_mem_offset(p, p_end, res) \
do { \
uint64 res64; \
if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
&res64, error_buf, error_buf_size)) { \
set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
goto fail; \
} \
res = (mem_offset_t)res64; \
} while (0)
#else
#define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
#endif
#define read_leb_uint32(p, p_end, res) \
do { \
uint64 res64; \
if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
error_buf_size)) \
goto fail; \
res = (uint32)res64; \
} while (0)
#define read_leb_int32(p, p_end, res) \
do { \
uint64 res64; \
if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
error_buf_size)) \
goto fail; \
res = (int32)res64; \
} while (0)
static char *
type2str(uint8 type)
{
char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
#if WASM_ENABLE_GC != 0
char *type_str_ref[] = { "stringview_iter",
"stringview_wtf16",
"(ref null ht)",
"(ref ht)",
"", /* reserved */
"stringview_wtf8",
"stringref",
"", /* reserved */
"", /* reserved */
"arrayref",
"structref",
"i32ref",
"eqref",
"anyref",
"externref",
"funcref",
"nullref",
"nullexternref",
"nullfuncref" };
#endif
if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
return type_str[type - VALUE_TYPE_V128];
#if WASM_ENABLE_GC != 0
else if (wasm_is_type_reftype(type))
return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
#endif
else if (type == VALUE_TYPE_FUNCREF)
return "funcref";
else if (type == VALUE_TYPE_EXTERNREF)
return "externref";
else
return "unknown type";
}
static bool
is_32bit_type(uint8 type)
{
if (type == VALUE_TYPE_I32
|| type == VALUE_TYPE_F32
/* the operand stack is in polymorphic state */
|| type == VALUE_TYPE_ANY
#if WASM_ENABLE_GC != 0
|| (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
#elif WASM_ENABLE_REF_TYPES != 0
/* For reference types, we use uint32 index to represent
the funcref and externref */
|| type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
#endif
)
return true;
return false;
}
static bool
is_64bit_type(uint8 type)
{
if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
#if WASM_ENABLE_GC != 0
|| (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
#endif
)
return true;
return false;
}
static bool
is_value_type(uint8 type)
{
if (/* I32/I64/F32/F64, 0x7C to 0x7F */
(type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
#if WASM_ENABLE_GC != 0
/* reference types, 0x65 to 0x70 */
|| wasm_is_type_reftype(type)
#elif WASM_ENABLE_REF_TYPES != 0
|| (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
#endif
#if WASM_ENABLE_SIMD != 0
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
|| type == VALUE_TYPE_V128 /* 0x7B */
#endif
#endif
)
return true;
return false;
}
#if WASM_ENABLE_GC != 0
static bool
is_packed_type(uint8 type)
{
return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
}
#endif
static bool
is_byte_a_type(uint8 type)
{
return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
}
#if WASM_ENABLE_SIMD != 0
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
static V128
read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
{
V128 result;
uint8 i;
for (i = 0; i != 16; ++i) {
result.i8x16[i] = read_uint8(p_buf);
}
return result;
}
#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
#endif /* end of WASM_ENABLE_SIMD */
static void *
loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
{
void *mem;
if (size >= WASM_MEM_ALLOC_MAX_SIZE
|| !(mem = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size, "allocate memory failed");
return NULL;
}
memset(mem, 0, (uint32)size);
return mem;
}
static void *
memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
uint32 error_buf_size)
{
uint8 *mem_new;
bh_assert(size_new > size_old);
if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
bh_memcpy_s(mem_new, size_new, mem_old, size_old);
memset(mem_new + size_old, 0, size_new - size_old);
wasm_runtime_free(mem_old);
}
return mem_new;
}
#define MEM_REALLOC(mem, size_old, size_new) \
do { \
void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
error_buf_size); \
if (!mem_new) \
goto fail; \
mem = mem_new; \
} while (0)
#if WASM_ENABLE_GC != 0
static bool
check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
char *error_buf, uint32 error_buf_size)
{
if (type_index >= type_count) {
set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
type_index);
return false;
}
return true;
}
static bool
check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
uint32 error_buf_size)
{
if (!check_type_index(module, module->type_count, type_index, error_buf,
error_buf_size)) {
return false;
}
if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
set_error_buf(error_buf, error_buf_size, "unkown array type");
return false;
}
return true;
}
#endif
static bool
check_function_index(const WASMModule *module, uint32 function_index,
char *error_buf, uint32 error_buf_size)
{
if (function_index
>= module->import_function_count + module->function_count) {
set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
function_index);
return false;
}
return true;
}
typedef struct InitValue {
uint8 type;
uint8 flag;
#if WASM_ENABLE_GC != 0
uint8 gc_opcode;
WASMRefType ref_type;
#endif
WASMValue value;
} InitValue;
typedef struct ConstExprContext {
uint32 sp;
uint32 size;
WASMModule *module;
InitValue *stack;
InitValue data[WASM_CONST_EXPR_STACK_SIZE];
} ConstExprContext;
static void
init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
{
ctx->sp = 0;
ctx->module = module;
ctx->stack = ctx->data;
ctx->size = WASM_CONST_EXPR_STACK_SIZE;
}
static bool
push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
#if WASM_ENABLE_GC != 0
WASMRefType *ref_type, uint8 gc_opcode,
#endif
WASMValue *value, char *error_buf, uint32 error_buf_size)
{
InitValue *cur_value;
if (ctx->sp >= ctx->size) {
if (ctx->stack != ctx->data) {
MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
(ctx->size + 4) * sizeof(InitValue));
}
else {
if (!(ctx->stack =
loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
error_buf, error_buf_size))) {
goto fail;
}
bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
ctx->data, ctx->size * (uint32)sizeof(InitValue));
}
ctx->size += 4;
}
cur_value = &ctx->stack[ctx->sp++];
cur_value->type = type;
cur_value->flag = flag;
cur_value->value = *value;
#if WASM_ENABLE_GC != 0
cur_value->gc_opcode = gc_opcode;
if (wasm_is_type_multi_byte_type(type)) {
bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
ref_type, wasm_reftype_struct_size(ref_type));
}
#endif
return true;
fail:
return false;
}
#if WASM_ENABLE_GC != 0
static void
destroy_init_expr_data_recursive(WASMModule *module, void *data)
{
WASMStructNewInitValues *struct_init_values =
(WASMStructNewInitValues *)data;
WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
WASMType *wasm_type;
uint32 i;
if (!data)
return;
wasm_type = module->types[struct_init_values->type_idx];
/* The data can only be type of `WASMStructNewInitValues *`
or `WASMArrayNewInitValues *` */
bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
|| wasm_type->type_flag == WASM_TYPE_ARRAY);
if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
WASMStructType *struct_type = (WASMStructType *)wasm_type;
WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
WASMRefType *ref_type;
uint8 field_type;
for (i = 0; i < struct_init_values->count; i++) {
field_type = struct_type->fields[i].field_type;
if (wasm_is_type_multi_byte_type(field_type))
ref_type = ref_type_map->ref_type;
else
ref_type = NULL;
if (wasm_reftype_is_subtype_of(field_type, ref_type,
REF_TYPE_STRUCTREF, NULL,
module->types, module->type_count)
|| wasm_reftype_is_subtype_of(
field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
module->types, module->type_count)) {
destroy_init_expr_data_recursive(
module, struct_init_values->fields[i].data);
}
}
}
else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
WASMArrayType *array_type = (WASMArrayType *)wasm_type;
WASMRefType *elem_ref_type = array_type->elem_ref_type;
uint8 elem_type = array_type->elem_type;
for (i = 0; i < array_init_values->length; i++) {
if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
REF_TYPE_STRUCTREF, NULL,
module->types, module->type_count)
|| wasm_reftype_is_subtype_of(
elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
module->types, module->type_count)) {
destroy_init_expr_data_recursive(
module, array_init_values->elem_data[i].data);
}
}
}
wasm_runtime_free(data);
}
#endif
static bool
pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
#if WASM_ENABLE_GC != 0
WASMRefType *ref_type, uint8 *p_gc_opcode,
#endif
WASMValue *p_value, char *error_buf, uint32 error_buf_size)
{
InitValue *cur_value;
if (ctx->sp == 0) {
set_error_buf(error_buf, error_buf_size,
"type mismatch: const expr stack underflow");
return false;
}
cur_value = &ctx->stack[--ctx->sp];
#if WASM_ENABLE_GC == 0
if (cur_value->type != type) {
set_error_buf(error_buf, error_buf_size, "type mismatch");
return false;
}
#else
if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
ref_type, ctx->module->types,
ctx->module->type_count)) {
set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
"type mismatch: expect ", type2str(type),
" but got other");
goto fail;
}
#endif
if (p_flag)
*p_flag = cur_value->flag;
if (p_value)
*p_value = cur_value->value;
#if WASM_ENABLE_GC != 0
if (p_gc_opcode)
*p_gc_opcode = cur_value->gc_opcode;
#endif
return true;
#if WASM_ENABLE_GC != 0
fail:
if ((cur_value->flag == WASM_OP_GC_PREFIX)
&& (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
|| cur_value->gc_opcode == WASM_OP_ARRAY_NEW
|| cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
}
return false;
#endif
}
static void
destroy_const_expr_stack(ConstExprContext *ctx)
{
#if WASM_ENABLE_GC != 0
uint32 i;
for (i = 0; i < ctx->sp; i++) {
if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
&& (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
|| ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
|| ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
destroy_init_expr_data_recursive(ctx->module,
ctx->stack[i].value.data);
}
}
#endif
if (ctx->stack != ctx->data) {
wasm_runtime_free(ctx->stack);
}
}
#if WASM_ENABLE_GC != 0
static void
destroy_init_expr(WASMModule *module, InitializerExpression *expr)
{
if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
|| expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
|| expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
destroy_init_expr_data_recursive(module, expr->u.data);
}
}
#endif /* end of WASM_ENABLE_GC != 0 */
static bool
load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
InitializerExpression *init_expr, uint8 type, void *ref_type,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = *p_buf, *p_end = buf_end;
uint8 flag, *p_float;
uint32 i;
ConstExprContext const_expr_ctx = { 0 };
WASMValue cur_value;
#if WASM_ENABLE_GC != 0
uint32 opcode1, type_idx;
uint8 opcode;
WASMRefType cur_ref_type = { 0 };
#endif
init_const_expr_stack(&const_expr_ctx, module);
CHECK_BUF(p, p_end, 1);
flag = read_uint8(p);
while (flag != WASM_OP_END) {
switch (flag) {
/* i32.const */
case INIT_EXPR_TYPE_I32_CONST:
read_leb_int32(p, p_end, cur_value.i32);
if (!push_const_expr_stack(
&const_expr_ctx, flag, VALUE_TYPE_I32,
#if WASM_ENABLE_GC != 0
NULL, 0,
#endif
&cur_value, error_buf, error_buf_size))
goto fail;
break;
/* i64.const */
case INIT_EXPR_TYPE_I64_CONST:
read_leb_int64(p, p_end, cur_value.i64);
if (!push_const_expr_stack(
&const_expr_ctx, flag, VALUE_TYPE_I64,
#if WASM_ENABLE_GC != 0
NULL, 0,
#endif
&cur_value, error_buf, error_buf_size))
goto fail;
break;
/* f32.const */
case INIT_EXPR_TYPE_F32_CONST:
CHECK_BUF(p, p_end, 4);
p_float = (uint8 *)&cur_value.f32;
for (i = 0; i < sizeof(float32); i++)
*p_float++ = *p++;
if (!push_const_expr_stack(
&const_expr_ctx, flag, VALUE_TYPE_F32,
#if WASM_ENABLE_GC != 0
NULL, 0,
#endif
&cur_value, error_buf, error_buf_size))
goto fail;
break;
/* f64.const */
case INIT_EXPR_TYPE_F64_CONST:
CHECK_BUF(p, p_end, 8);
p_float = (uint8 *)&cur_value.f64;
for (i = 0; i < sizeof(float64); i++)
*p_float++ = *p++;
if (!push_const_expr_stack(
&const_expr_ctx, flag, VALUE_TYPE_F64,
#if WASM_ENABLE_GC != 0
NULL, 0,
#endif
&cur_value, error_buf, error_buf_size))
goto fail;
break;
#if WASM_ENABLE_SIMD != 0
#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
/* v128.const */
case INIT_EXPR_TYPE_V128_CONST:
{
uint64 high, low;
CHECK_BUF(p, p_end, 1);
(void)read_uint8(p);
CHECK_BUF(p, p_end, 16);
wasm_runtime_read_v128(p, &high, &low);
p += 16;
cur_value.v128.i64x2[0] = high;
cur_value.v128.i64x2[1] = low;
if (!push_const_expr_stack(
&const_expr_ctx, flag, VALUE_TYPE_V128,
#if WASM_ENABLE_GC != 0
NULL, 0,
#endif
&cur_value, error_buf, error_buf_size))
goto fail;
#if WASM_ENABLE_WAMR_COMPILER != 0
/* If any init_expr is v128.const, mark SIMD used */
module->is_simd_used = true;
#endif
break;
}
#endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
#endif /* end of WASM_ENABLE_SIMD */
#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
/* ref.func */
case INIT_EXPR_TYPE_FUNCREF_CONST:
{
uint32 func_idx;
read_leb_uint32(p, p_end, func_idx);
cur_value.ref_index = func_idx;
if (!check_function_index(module, func_idx, error_buf,
error_buf_size)) {
goto fail;
}
#if WASM_ENABLE_GC == 0
if (!push_const_expr_stack(&const_expr_ctx, flag,
VALUE_TYPE_FUNCREF, &cur_value,
error_buf, error_buf_size))
goto fail;
#else
if (func_idx < module->import_function_count) {
type_idx =
module->import_functions[func_idx].u.function.type_idx;
}
else {
type_idx = module
->functions[func_idx
- module->import_function_count]
->type_idx;
}
wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
false, type_idx);
if (!push_const_expr_stack(&const_expr_ctx, flag,
cur_ref_type.ref_type, &cur_ref_type,
0, &cur_value, error_buf,
error_buf_size))
goto fail;
#endif
#if WASM_ENABLE_WAMR_COMPILER != 0
module->is_ref_types_used = true;
#endif
break;
}
/* ref.null */
case INIT_EXPR_TYPE_REFNULL_CONST:
{
uint8 type1;
CHECK_BUF(p, p_end, 1);
type1 = read_uint8(p);
#if WASM_ENABLE_GC == 0
cur_value.ref_index = NULL_REF;
if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
&cur_value, error_buf,
error_buf_size))
goto fail;
#else
cur_value.gc_obj = NULL_REF;
if (!is_byte_a_type(type1)) {
p--;
read_leb_uint32(p, p_end, type_idx);
if (!check_type_index(module, module->type_count, type_idx,
error_buf, error_buf_size))
goto fail;
wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
true, type_idx);
if (!push_const_expr_stack(&const_expr_ctx, flag,
cur_ref_type.ref_type,
&cur_ref_type, 0, &cur_value,
error_buf, error_buf_size))
goto fail;
}
else {
if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
NULL, 0, &cur_value, error_buf,
error_buf_size))
goto fail;
}
#endif
#if WASM_ENABLE_WAMR_COMPILER != 0
module->is_ref_types_used = true;
#endif
break;
}
#endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
/* get_global */
case INIT_EXPR_TYPE_GET_GLOBAL:
{
uint32 global_idx;
uint8 global_type;
read_leb_uint32(p, p_end, cur_value.global_index);
global_idx = cur_value.global_index;
/*
* Currently, constant expressions occurring as initializers
* of globals are further constrained in that contained
* global.get instructions are
* only allowed to refer to imported globals.
*
* https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
*/
if (global_idx >= module->import_global_count
/* make spec test happy */
#if WASM_ENABLE_GC != 0
+ module->global_count
#endif
) {
set_error_buf_v(error_buf, error_buf_size,
"unknown global %u", global_idx);
goto fail;
}
if (
/* make spec test happy */
#if WASM_ENABLE_GC != 0
global_idx < module->import_global_count &&
#endif
module->import_globals[global_idx]
.u.global.type.is_mutable) {
set_error_buf_v(error_buf, error_buf_size,
"constant expression required");
goto fail;
}
if (global_idx < module->import_global_count) {
global_type = module->import_globals[global_idx]
.u.global.type.val_type;
#if WASM_ENABLE_GC != 0
if (wasm_is_type_multi_byte_type(global_type)) {
WASMRefType *global_ref_type =
module->import_globals[global_idx]
.u.global.ref_type;
bh_memcpy_s(&cur_ref_type,
wasm_reftype_struct_size(global_ref_type),
global_ref_type,
wasm_reftype_struct_size(global_ref_type));
}
#endif
}
else {
global_type =
module
->globals[global_idx - module->import_global_count]
.type.val_type;
#if WASM_ENABLE_GC != 0
if (wasm_is_type_multi_byte_type(global_type)) {
WASMRefType *global_ref_type =
module
->globals[global_idx
- module->import_global_count]
.ref_type;
bh_memcpy_s(&cur_ref_type,
wasm_reftype_struct_size(global_ref_type),
global_ref_type,
wasm_reftype_struct_size(global_ref_type));
}
#endif
}
if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
#if WASM_ENABLE_GC != 0
&cur_ref_type, 0,
#endif
&cur_value, error_buf,
error_buf_size))
goto fail;
break;
}
#if WASM_ENABLE_GC != 0
/* struct.new and array.new */
case WASM_OP_GC_PREFIX:
{
read_leb_uint32(p, p_end, opcode1);
switch (opcode1) {
case WASM_OP_STRUCT_NEW:
{
WASMStructType *struct_type;
WASMStructNewInitValues *struct_init_values = NULL;
uint32 field_count;
read_leb_uint32(p, p_end, type_idx);
if (!check_type_index(module, module->type_count,
type_idx, error_buf,
error_buf_size)) {
goto fail;
}
struct_type = (WASMStructType *)module->types[type_idx];
if (struct_type->base_type.type_flag
!= WASM_TYPE_STRUCT) {
set_error_buf(error_buf, error_buf_size,
"unkown struct type");
goto fail;
}
field_count = struct_type->field_count;
if (!(struct_init_values = loader_malloc(
offsetof(WASMStructNewInitValues, fields)
+ (uint64)field_count * sizeof(WASMValue),
error_buf, error_buf_size))) {