-
Notifications
You must be signed in to change notification settings - Fork 578
/
aot_loader.c
4397 lines (3855 loc) · 149 KB
/
aot_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 "aot_runtime.h"
#include "bh_common.h"
#include "bh_log.h"
#include "aot_reloc.h"
#include "../common/wasm_runtime_common.h"
#include "../common/wasm_native.h"
#include "../common/wasm_loader_common.h"
#include "../compilation/aot.h"
#if WASM_ENABLE_DEBUG_AOT != 0
#include "debug/elf_parser.h"
#include "debug/jit_debug.h"
#endif
#if WASM_ENABLE_LINUX_PERF != 0
#include "aot_perf_map.h"
#endif
#define YMM_PLT_PREFIX "__ymm@"
#define XMM_PLT_PREFIX "__xmm@"
#define REAL_PLT_PREFIX "__real@"
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, "AOT module load failed: %s",
string);
}
}
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, "AOT module load failed: %s", buf);
}
}
#define exchange_uint8(p_data) (void)0
static void
exchange_uint16(uint8 *p_data)
{
uint8 value = *p_data;
*p_data = *(p_data + 1);
*(p_data + 1) = value;
}
static void
exchange_uint32(uint8 *p_data)
{
uint8 value = *p_data;
*p_data = *(p_data + 3);
*(p_data + 3) = value;
value = *(p_data + 1);
*(p_data + 1) = *(p_data + 2);
*(p_data + 2) = value;
}
static void
exchange_uint64(uint8 *p_data)
{
uint32 value;
value = *(uint32 *)p_data;
*(uint32 *)p_data = *(uint32 *)(p_data + 4);
*(uint32 *)(p_data + 4) = value;
exchange_uint32(p_data);
exchange_uint32(p_data + 4);
}
static union {
int a;
char b;
} __ue = { .a = 1 };
#define is_little_endian() (__ue.b == 1)
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");
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)
static uint8 *
align_ptr(const uint8 *p, uint32 b)
{
uintptr_t v = (uintptr_t)p;
uintptr_t m = b - 1;
return (uint8 *)((v + m) & ~m);
}
static inline uint64
GET_U64_FROM_ADDR(uint32 *addr)
{
union {
uint64 val;
uint32 parts[2];
} u;
u.parts[0] = addr[0];
u.parts[1] = addr[1];
return u.val;
}
#if (WASM_ENABLE_WORD_ALIGN_READ != 0)
static inline uint8
GET_U8_FROM_ADDR(const uint8 *p)
{
uint8 res = 0;
bh_assert(p);
const uint8 *p_aligned = align_ptr(p, 4);
p_aligned = (p_aligned > p) ? p_aligned - 4 : p_aligned;
uint32 buf32 = *(const uint32 *)p_aligned;
const uint8 *pbuf = (const uint8 *)&buf32;
res = *(uint8 *)(pbuf + (p - p_aligned));
return res;
}
static inline uint16
GET_U16_FROM_ADDR(const uint8 *p)
{
uint16 res = 0;
bh_assert(p);
const uint8 *p_aligned = align_ptr(p, 4);
p_aligned = (p_aligned > p) ? p_aligned - 4 : p_aligned;
uint32 buf32 = *(const uint32 *)p_aligned;
const uint8 *pbuf = (const uint8 *)&buf32;
res = *(uint16 *)(pbuf + (p - p_aligned));
return res;
}
#define TEMPLATE_READ(p, p_end, res, type) \
do { \
if (sizeof(type) != sizeof(uint64)) \
p = (uint8 *)align_ptr(p, sizeof(type)); \
else \
/* align 4 bytes if type is uint64 */ \
p = (uint8 *)align_ptr(p, sizeof(uint32)); \
CHECK_BUF(p, p_end, sizeof(type)); \
if (sizeof(type) == sizeof(uint8)) \
res = GET_U8_FROM_ADDR(p); \
else if (sizeof(type) == sizeof(uint16)) \
res = GET_U16_FROM_ADDR(p); \
else if (sizeof(type) == sizeof(uint32)) \
res = *(type *)p; \
else \
res = (type)GET_U64_FROM_ADDR((uint32 *)p); \
if (!is_little_endian()) \
exchange_##type((uint8 *)&res); \
p += sizeof(type); \
} while (0)
#define read_byte_array(p, p_end, addr, len) \
do { \
CHECK_BUF(p, p_end, len); \
bh_memcpy_wa(addr, len, p, len); \
p += len; \
} while (0)
#define read_string(p, p_end, str) \
do { \
if (!(str = load_string((uint8 **)&p, p_end, module, \
is_load_from_file_buf, true, error_buf, \
error_buf_size))) \
goto fail; \
} while (0)
#else /* else of (WASM_ENABLE_WORD_ALIGN_READ != 0) */
#define TEMPLATE_READ(p, p_end, res, type) \
do { \
if (sizeof(type) != sizeof(uint64)) \
p = (uint8 *)align_ptr(p, sizeof(type)); \
else \
/* align 4 bytes if type is uint64 */ \
p = (uint8 *)align_ptr(p, sizeof(uint32)); \
CHECK_BUF(p, p_end, sizeof(type)); \
if (sizeof(type) != sizeof(uint64)) \
res = *(type *)p; \
else \
res = (type)GET_U64_FROM_ADDR((uint32 *)p); \
if (!is_little_endian()) \
exchange_##type((uint8 *)&res); \
p += sizeof(type); \
} while (0)
/* NOLINTBEGIN, disable lint for this region with clang-tidy */
#define read_byte_array(p, p_end, addr, len) \
do { \
CHECK_BUF(p, p_end, len); \
bh_memcpy_s(addr, len, p, len); \
p += len; \
} while (0)
#define read_string(p, p_end, str) \
do { \
if (!(str = load_string((uint8 **)&p, p_end, module, \
is_load_from_file_buf, error_buf, \
error_buf_size))) \
goto fail; \
} while (0)
#endif /* end of (WASM_ENABLE_WORD_ALIGN_READ != 0) */
#define read_uint8(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint8)
#define read_uint16(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint16)
#define read_uint32(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint32)
#define read_uint64(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint64)
/* NOLINTEND */
/* Legal values for bin_type */
#define BIN_TYPE_ELF32L 0 /* 32-bit little endian */
#define BIN_TYPE_ELF32B 1 /* 32-bit big endian */
#define BIN_TYPE_ELF64L 2 /* 64-bit little endian */
#define BIN_TYPE_ELF64B 3 /* 64-bit big endian */
#define BIN_TYPE_COFF32 4 /* 32-bit little endian */
#define BIN_TYPE_COFF64 6 /* 64-bit little endian */
/* Legal values for e_type (object file type). */
#define E_TYPE_NONE 0 /* No file type */
#define E_TYPE_REL 1 /* Relocatable file */
#define E_TYPE_EXEC 2 /* Executable file */
#define E_TYPE_DYN 3 /* Shared object file */
#define E_TYPE_XIP 4 /* eXecute In Place file */
/* Legal values for e_machine (architecture). */
#define E_MACHINE_386 3 /* Intel 80386 */
#define E_MACHINE_MIPS 8 /* MIPS R3000 big-endian */
#define E_MACHINE_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */
#define E_MACHINE_ARM 40 /* ARM/Thumb */
#define E_MACHINE_AARCH64 183 /* AArch64 */
#define E_MACHINE_ARC 45 /* Argonaut RISC Core */
#define E_MACHINE_IA_64 50 /* Intel Merced */
#define E_MACHINE_MIPS_X 51 /* Stanford MIPS-X */
#define E_MACHINE_X86_64 62 /* AMD x86-64 architecture */
#define E_MACHINE_ARC_COMPACT 93 /* ARC International ARCompact */
#define E_MACHINE_ARC_COMPACT2 195 /* Synopsys ARCompact V2 */
#define E_MACHINE_XTENSA 94 /* Tensilica Xtensa Architecture */
#define E_MACHINE_RISCV 243 /* RISC-V 32/64 */
#define E_MACHINE_WIN_I386 0x14c /* Windows i386 architecture */
#define E_MACHINE_WIN_X86_64 0x8664 /* Windows x86-64 architecture */
/* Legal values for e_version */
#define E_VERSION_CURRENT 1 /* Current version */
static void *
loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
{
void *mem;
if (size >= UINT32_MAX || !(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 char *
load_string(uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
bool is_load_from_file_buf,
#if (WASM_ENABLE_WORD_ALIGN_READ != 0)
bool is_vram_word_align,
#endif
char *error_buf, uint32 error_buf_size)
{
uint8 *p = *p_buf;
const uint8 *p_end = buf_end;
char *str;
uint16 str_len;
read_uint16(p, p_end, str_len);
CHECK_BUF(p, p_end, str_len);
if (str_len == 0) {
str = "";
}
#if (WASM_ENABLE_WORD_ALIGN_READ != 0)
else if (is_vram_word_align) {
if (!(str = aot_const_str_set_insert((uint8 *)p, str_len, module,
is_vram_word_align, error_buf,
error_buf_size))) {
goto fail;
}
}
#endif
else if (is_load_from_file_buf) {
/* The string is always terminated with '\0', use it directly.
* In this case, the file buffer can be referred to after loading.
*/
bh_assert(p[str_len - 1] == '\0');
str = (char *)p;
}
else {
/* Load from sections, the file buffer cannot be referred to
after loading, we must create another string and insert it
into const string set */
bh_assert(p[str_len - 1] == '\0');
if (!(str = aot_const_str_set_insert((uint8 *)p, str_len, module,
#if (WASM_ENABLE_WORD_ALIGN_READ != 0)
is_vram_word_align,
#endif
error_buf, error_buf_size))) {
goto fail;
}
}
p += str_len;
*p_buf = p;
return str;
fail:
return NULL;
}
static bool
get_aot_file_target(AOTTargetInfo *target_info, char *target_buf,
uint32 target_buf_size, char *error_buf,
uint32 error_buf_size)
{
char *machine_type = NULL;
switch (target_info->e_machine) {
case E_MACHINE_X86_64:
case E_MACHINE_WIN_X86_64:
machine_type = "x86_64";
break;
case E_MACHINE_386:
case E_MACHINE_WIN_I386:
machine_type = "i386";
break;
case E_MACHINE_ARM:
case E_MACHINE_AARCH64:
machine_type = target_info->arch;
break;
case E_MACHINE_MIPS:
machine_type = "mips";
break;
case E_MACHINE_XTENSA:
machine_type = "xtensa";
break;
case E_MACHINE_RISCV:
machine_type = "riscv";
break;
case E_MACHINE_ARC_COMPACT:
case E_MACHINE_ARC_COMPACT2:
machine_type = "arc";
break;
default:
set_error_buf_v(error_buf, error_buf_size,
"unknown machine type %d", target_info->e_machine);
return false;
}
if (strncmp(target_info->arch, machine_type, strlen(machine_type))) {
set_error_buf_v(
error_buf, error_buf_size,
"machine type (%s) isn't consistent with target type (%s)",
machine_type, target_info->arch);
return false;
}
snprintf(target_buf, target_buf_size, "%s", target_info->arch);
return true;
}
static bool
check_machine_info(AOTTargetInfo *target_info, char *error_buf,
uint32 error_buf_size)
{
char target_expected[32], target_got[32];
get_current_target(target_expected, sizeof(target_expected));
if (!get_aot_file_target(target_info, target_got, sizeof(target_got),
error_buf, error_buf_size))
return false;
if (strncmp(target_expected, target_got, strlen(target_expected))) {
set_error_buf_v(error_buf, error_buf_size,
"invalid target type, expected %s but got %s",
target_expected, target_got);
return false;
}
return true;
}
static bool
check_feature_flags(char *error_buf, uint32 error_buf_size,
uint64 feature_flags)
{
#if WASM_ENABLE_SIMD == 0
if (feature_flags & WASM_FEATURE_SIMD_128BIT) {
set_error_buf(error_buf, error_buf_size,
"SIMD is not enabled in this build");
return false;
}
#endif
#if WASM_ENABLE_BULK_MEMORY == 0
if (feature_flags & WASM_FEATURE_BULK_MEMORY) {
set_error_buf(error_buf, error_buf_size,
"bulk memory is not enabled in this build");
return false;
}
#endif
#if WASM_ENABLE_THREAD_MGR == 0
if (feature_flags & WASM_FEATURE_MULTI_THREAD) {
set_error_buf(error_buf, error_buf_size,
"thread is not enabled in this build");
return false;
}
#endif
#if WASM_ENABLE_REF_TYPES == 0
if (feature_flags & WASM_FEATURE_REF_TYPES) {
set_error_buf(error_buf, error_buf_size,
"reference types is not enabled in this build");
return false;
}
#endif
#if WASM_ENABLE_GC == 0
if (feature_flags & WASM_FEATURE_GARBAGE_COLLECTION) {
set_error_buf(error_buf, error_buf_size,
"garbage collection is not enabled in this build");
return false;
}
#endif
return true;
}
#if WASM_ENABLE_GC != 0
static WASMRefType *
reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
char *error_buf, uint32 error_buf_size)
{
WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
if (!ret) {
set_error_buf(error_buf, error_buf_size,
"insert ref type to hash set failed");
}
return ret;
}
#endif
static bool
load_target_info_section(const uint8 *buf, const uint8 *buf_end,
AOTModule *module, char *error_buf,
uint32 error_buf_size)
{
AOTTargetInfo target_info;
const uint8 *p = buf, *p_end = buf_end;
bool is_target_little_endian, is_target_64_bit;
read_uint16(p, p_end, target_info.bin_type);
read_uint16(p, p_end, target_info.abi_type);
read_uint16(p, p_end, target_info.e_type);
read_uint16(p, p_end, target_info.e_machine);
read_uint32(p, p_end, target_info.e_version);
read_uint32(p, p_end, target_info.e_flags);
read_uint64(p, p_end, target_info.feature_flags);
read_uint64(p, p_end, target_info.reserved);
read_byte_array(p, p_end, target_info.arch, sizeof(target_info.arch));
if (p != buf_end) {
set_error_buf(error_buf, error_buf_size, "invalid section size");
return false;
}
/* Check target endian type */
is_target_little_endian = target_info.bin_type & 1 ? false : true;
if (is_little_endian() != is_target_little_endian) {
set_error_buf_v(error_buf, error_buf_size,
"invalid target endian type, expected %s but got %s",
is_little_endian() ? "little endian" : "big endian",
is_target_little_endian ? "little endian"
: "big endian");
return false;
}
/* Check target bit width */
is_target_64_bit = target_info.bin_type & 2 ? true : false;
if ((sizeof(void *) == 8 ? true : false) != is_target_64_bit) {
set_error_buf_v(error_buf, error_buf_size,
"invalid target bit width, expected %s but got %s",
sizeof(void *) == 8 ? "64-bit" : "32-bit",
is_target_64_bit ? "64-bit" : "32-bit");
return false;
}
/* Check target elf file type */
if (target_info.e_type != E_TYPE_REL && target_info.e_type != E_TYPE_XIP) {
set_error_buf(error_buf, error_buf_size,
"invalid object file type, "
"expected relocatable or XIP file type but got others");
return false;
}
/* Check machine info */
if (!check_machine_info(&target_info, error_buf, error_buf_size)) {
return false;
}
if (target_info.e_version != E_VERSION_CURRENT) {
set_error_buf(error_buf, error_buf_size, "invalid elf file version");
return false;
}
/* Finally, check feature flags */
return check_feature_flags(error_buf, error_buf_size,
target_info.feature_flags);
fail:
return false;
}
static void *
get_native_symbol_by_name(const char *name)
{
void *func = NULL;
uint32 symnum = 0;
SymbolMap *sym = NULL;
sym = get_target_symbol_map(&symnum);
while (symnum--) {
if (strcmp(sym->symbol_name, name) == 0) {
func = sym->symbol_addr;
break;
}
sym++;
}
return func;
}
static bool
str2uint32(const char *buf, uint32 *p_res);
static bool
str2uint64(const char *buf, uint64 *p_res);
#if WASM_ENABLE_MULTI_MODULE != 0
static void *
aot_loader_resolve_function(const char *module_name, const char *function_name,
const AOTFuncType *expected_function_type,
char *error_buf, uint32 error_buf_size)
{
WASMModuleCommon *module_reg;
void *function = NULL;
AOTExport *export = NULL;
AOTModule *module = NULL;
AOTFuncType *target_function_type = NULL;
module_reg = wasm_runtime_find_module_registered(module_name);
if (!module_reg || module_reg->module_type != Wasm_Module_AoT) {
LOG_DEBUG("can not find a module named %s for function %s", module_name,
function_name);
set_error_buf(error_buf, error_buf_size, "unknown import");
return NULL;
}
module = (AOTModule *)module_reg;
export = loader_find_export(module_reg, module_name, function_name,
EXPORT_KIND_FUNC, error_buf, error_buf_size);
if (!export) {
return NULL;
}
/* resolve function type and function */
if (export->index < module->import_func_count) {
target_function_type = module->import_funcs[export->index].func_type;
function = module->import_funcs[export->index].func_ptr_linked;
}
else {
target_function_type =
(AOTFuncType *)module
->types[module->func_type_indexes[export->index
- module->import_func_count]];
function =
(module->func_ptrs[export->index - module->import_func_count]);
}
/* check function type */
if (!wasm_type_equal((WASMType *)expected_function_type,
(WASMType *)target_function_type, module->types,
module->type_count)) {
LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
set_error_buf(error_buf, error_buf_size, "incompatible import type");
return NULL;
}
return function;
}
#endif /* end of WASM_ENABLE_MULTI_MODULE */
static bool
load_native_symbol_section(const uint8 *buf, const uint8 *buf_end,
AOTModule *module, bool is_load_from_file_buf,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = buf, *p_end = buf_end;
uint32 cnt;
int32 i;
const char *symbol;
read_uint32(p, p_end, cnt);
if (cnt > 0) {
module->native_symbol_list = wasm_runtime_malloc(cnt * sizeof(void *));
if (module->native_symbol_list == NULL) {
set_error_buf(error_buf, error_buf_size,
"malloc native symbol list failed");
goto fail;
}
for (i = cnt - 1; i >= 0; i--) {
read_string(p, p_end, symbol);
if (!strncmp(symbol, "f32#", 4) || !strncmp(symbol, "i32#", 4)) {
uint32 u32;
/* Resolve the raw int bits of f32 const */
if (!str2uint32(symbol + 4, &u32)) {
set_error_buf_v(error_buf, error_buf_size,
"resolve symbol %s failed", symbol);
goto fail;
}
*(uint32 *)(&module->native_symbol_list[i]) = u32;
}
else if (!strncmp(symbol, "f64#", 4)
|| !strncmp(symbol, "i64#", 4)) {
uint64 u64;
/* Resolve the raw int bits of f64 const */
if (!str2uint64(symbol + 4, &u64)) {
set_error_buf_v(error_buf, error_buf_size,
"resolve symbol %s failed", symbol);
goto fail;
}
*(uint64 *)(&module->native_symbol_list[i]) = u64;
}
else if (!strncmp(symbol, "__ignore", 8)) {
/* Padding bytes to make f64 on 8-byte aligned address,
or it is the second 32-bit slot in 32-bit system */
continue;
}
else {
module->native_symbol_list[i] =
get_native_symbol_by_name(symbol);
if (module->native_symbol_list[i] == NULL) {
set_error_buf_v(error_buf, error_buf_size,
"missing native symbol: %s", symbol);
goto fail;
}
}
}
}
return true;
fail:
return false;
}
static bool
load_name_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
bool is_load_from_file_buf, char *error_buf,
uint32 error_buf_size)
{
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
const uint8 *p = buf, *p_end = buf_end;
uint32 *aux_func_indexes;
const char **aux_func_names;
uint32 name_type, subsection_size;
uint32 previous_name_type = 0;
uint32 num_func_name;
uint32 func_index;
uint32 previous_func_index = ~0U;
uint32 name_index;
int i = 0;
uint32 name_len;
uint64 size;
if (p >= p_end) {
set_error_buf(error_buf, error_buf_size, "unexpected end");
return false;
}
read_uint32(p, p_end, name_len);
if (name_len != 4 || p + name_len > p_end) {
set_error_buf(error_buf, error_buf_size, "unexpected end");
return false;
}
if (memcmp(p, "name", 4) != 0) {
set_error_buf(error_buf, error_buf_size, "invalid custom name section");
return false;
}
p += name_len;
while (p < p_end) {
read_uint32(p, p_end, name_type);
if (i != 0) {
if (name_type == previous_name_type) {
set_error_buf(error_buf, error_buf_size,
"duplicate sub-section");
return false;
}
if (name_type < previous_name_type) {
set_error_buf(error_buf, error_buf_size,
"out-of-order sub-section");
return false;
}
}
previous_name_type = name_type;
read_uint32(p, p_end, subsection_size);
CHECK_BUF(p, p_end, subsection_size);
switch (name_type) {
case SUB_SECTION_TYPE_FUNC:
if (subsection_size) {
read_uint32(p, p_end, num_func_name);
if (num_func_name
> module->import_func_count + module->func_count) {
set_error_buf(error_buf, error_buf_size,
"function name count out of bounds");
return false;
}
module->aux_func_name_count = num_func_name;
/* Allocate memory */
size = sizeof(uint32) * (uint64)module->aux_func_name_count;
if (!(aux_func_indexes = module->aux_func_indexes =
loader_malloc(size, error_buf, error_buf_size))) {
return false;
}
size =
sizeof(char **) * (uint64)module->aux_func_name_count;
if (!(aux_func_names = module->aux_func_names =
loader_malloc(size, error_buf, error_buf_size))) {
return false;
}
for (name_index = 0; name_index < num_func_name;
name_index++) {
read_uint32(p, p_end, func_index);
if (name_index != 0
&& func_index == previous_func_index) {
set_error_buf(error_buf, error_buf_size,
"duplicate function name");
return false;
}
if (name_index != 0
&& func_index < previous_func_index) {
set_error_buf(error_buf, error_buf_size,
"out-of-order function index ");
return false;
}
if (func_index
>= module->import_func_count + module->func_count) {
set_error_buf(error_buf, error_buf_size,
"function index out of bounds");
return false;
}
previous_func_index = func_index;
*(aux_func_indexes + name_index) = func_index;
read_string(p, p_end, *(aux_func_names + name_index));
#if 0
LOG_DEBUG("func_index %u -> aux_func_name = %s\n",
func_index, *(aux_func_names + name_index));
#endif
}
}
break;
case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
*/
case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
default:
p = p + subsection_size;
break;
}
i++;
}
return true;
fail:
return false;
#else
return true;
#endif /* WASM_ENABLE_CUSTOM_NAME_SECTION != 0 */
}
#if WASM_ENABLE_STRINGREF != 0
static bool
load_string_literal_section(const uint8 *buf, const uint8 *buf_end,
AOTModule *module, bool is_load_from_file_buf,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = buf, *p_end = buf_end;
uint32 reserved = 0, string_count = 0, i;
uint64 size;
read_uint32(p, p_end, reserved);
if (reserved != 0) {
set_error_buf(error_buf, error_buf_size,
"invalid reserved slot in string literal count");
goto fail;
}
read_uint32(p, p_end, string_count);
if (string_count == 0) {
set_error_buf(error_buf, error_buf_size,
"invalid string literal count");
goto fail;
}
module->string_literal_count = string_count;
size = (uint64)sizeof(char *) * string_count;
if (!(module->string_literal_ptrs =
loader_malloc(size, error_buf, error_buf_size))) {
goto fail;
}
size = (uint64)sizeof(uint32) * string_count;
if (!(module->string_literal_lengths =
loader_malloc(size, error_buf, error_buf_size))) {
goto fail;
}
for (i = 0; i < string_count; i++) {
read_uint32(p, p_end, module->string_literal_lengths[i]);
}
for (i = 0; i < string_count; i++) {
module->string_literal_ptrs[i] = p;
p += module->string_literal_lengths[i];
}
return true;
fail:
return false;
}
#endif /* end of WASM_ENABLE_STRINGREF != 0 */
static bool
load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
bool is_load_from_file_buf, char *error_buf,
uint32 error_buf_size)
{
const uint8 *p = buf, *p_end = buf_end;
uint32 sub_section_type;
read_uint32(p, p_end, sub_section_type);
buf = p;
switch (sub_section_type) {
case AOT_CUSTOM_SECTION_NATIVE_SYMBOL:
if (!load_native_symbol_section(buf, buf_end, module,
is_load_from_file_buf, error_buf,
error_buf_size))
goto fail;
break;
case AOT_CUSTOM_SECTION_NAME:
if (!load_name_section(buf, buf_end, module, is_load_from_file_buf,
error_buf, error_buf_size))
LOG_VERBOSE("Load name section failed.");
else
LOG_VERBOSE("Load name section success.");
break;
#if WASM_ENABLE_STRINGREF != 0
case AOT_CUSTOM_SECTION_STRING_LITERAL:
if (!load_string_literal_section(buf, buf_end, module,
is_load_from_file_buf, error_buf,
error_buf_size))
goto fail;
break;
#endif
#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
case AOT_CUSTOM_SECTION_RAW:
{
const char *section_name;
WASMCustomSection *section;
if (p >= p_end) {
set_error_buf(error_buf, error_buf_size, "unexpected end");
goto fail;
}
read_string(p, p_end, section_name);
section = loader_malloc(sizeof(WASMCustomSection), error_buf,
error_buf_size);
if (!section) {
goto fail;
}
section->name_addr = (char *)section_name;
section->name_len = (uint32)strlen(section_name);
section->content_addr = (uint8 *)p;
section->content_len = (uint32)(p_end - p);
section->next = module->custom_section_list;
module->custom_section_list = section;
LOG_VERBOSE("Load custom section [%s] success.", section_name);
break;
}
#endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */
default:
break;
}
return true;
fail:
return false;
}
static void
destroy_import_memories(AOTImportMemory *import_memories)
{
wasm_runtime_free(import_memories);
}
static void
destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count)
{
uint32 i;
for (i = 0; i < count; i++)
if (data_list[i])
wasm_runtime_free(data_list[i]);
wasm_runtime_free(data_list);
}
static bool
load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
InitializerExpression *expr, char *error_buf,
uint32 error_buf_size);
static bool
load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
AOTModule *module, char *error_buf,
uint32 error_buf_size)
{
const uint8 *buf = *p_buf;
AOTMemInitData **data_list;
uint64 size;
uint32 i;
/* Allocate memory */
size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count;
if (!(module->mem_init_data_list = data_list =
loader_malloc(size, error_buf, error_buf_size))) {
return false;
}
/* Create each memory data segment */
for (i = 0; i < module->mem_init_data_count; i++) {
uint32 byte_count;
uint32 is_passive;
uint32 memory_index;
InitializerExpression init_value;
read_uint32(buf, buf_end, is_passive);
read_uint32(buf, buf_end, memory_index);
if (!load_init_expr(&buf, buf_end, module, &init_value, error_buf,
error_buf_size)) {
return false;