-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathwasm_runtime.c
3675 lines (3215 loc) · 121 KB
/
wasm_runtime.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_runtime.h"
#include "wasm_loader.h"
#include "wasm_interp.h"
#include "bh_common.h"
#include "bh_log.h"
#include "mem_alloc.h"
#include "../common/wasm_runtime_common.h"
#include "../common/wasm_memory.h"
#if WASM_ENABLE_SHARED_MEMORY != 0
#include "../common/wasm_shared_memory.h"
#endif
#if WASM_ENABLE_THREAD_MGR != 0
#include "../libraries/thread-mgr/thread_manager.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"
#endif
#if WASM_ENABLE_JIT != 0
#include "../aot/aot_runtime.h"
#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 instantiate 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,
"WASM module instantiate failed: %s", buf);
}
}
WASMModule *
wasm_load(uint8 *buf, uint32 size,
#if WASM_ENABLE_MULTI_MODULE != 0
bool main_module,
#endif
char *error_buf, uint32 error_buf_size)
{
return wasm_loader_load(buf, size,
#if WASM_ENABLE_MULTI_MODULE != 0
main_module,
#endif
error_buf, error_buf_size);
}
WASMModule *
wasm_load_from_sections(WASMSection *section_list, char *error_buf,
uint32 error_buf_size)
{
return wasm_loader_load_from_sections(section_list, error_buf,
error_buf_size);
}
void
wasm_unload(WASMModule *module)
{
wasm_loader_unload(module);
}
static void *
runtime_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;
}
#if WASM_ENABLE_MULTI_MODULE != 0
static WASMModuleInstance *
get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
const WASMModule *sub_module)
{
bh_list *sub_module_inst_list = parent_module_inst->e->sub_module_inst_list;
WASMSubModInstNode *node = bh_list_first_elem(sub_module_inst_list);
while (node && sub_module != node->module_inst->module) {
node = bh_list_elem_next(node);
}
return node ? node->module_inst : NULL;
}
#endif
/**
* Destroy memory instances.
*/
static void
memories_deinstantiate(WASMModuleInstance *module_inst,
WASMMemoryInstance **memories, uint32 count)
{
#ifdef WASM_LINEAR_MEMORY_MMAP
uint64 map_size;
#endif
uint32 i;
if (memories) {
for (i = 0; i < count; i++) {
if (memories[i]) {
#if WASM_ENABLE_MULTI_MODULE != 0
WASMModule *module = module_inst->module;
if (i < module->import_memory_count
&& module->import_memories[i].u.memory.import_module) {
continue;
}
#endif
#if WASM_ENABLE_SHARED_MEMORY != 0
if (shared_memory_is_shared(memories[i])) {
uint32 ref_count = shared_memory_dec_reference(memories[i]);
/* if the reference count is not zero,
don't free the memory */
if (ref_count > 0)
continue;
}
#endif
if (memories[i]->heap_handle) {
mem_allocator_destroy(memories[i]->heap_handle);
wasm_runtime_free(memories[i]->heap_handle);
memories[i]->heap_handle = NULL;
}
if (memories[i]->memory_data) {
#ifndef OS_ENABLE_HW_BOUND_CHECK
#ifdef WASM_LINEAR_MEMORY_MMAP
if (shared_memory_is_shared(memories[i])) {
map_size = (uint64)memories[i]->num_bytes_per_page
* memories[i]->max_page_count;
wasm_munmap_linear_memory(memories[i]->memory_data,
map_size, map_size);
}
else
#endif
wasm_runtime_free(memories[i]->memory_data);
#else
map_size = (uint64)memories[i]->num_bytes_per_page
* memories[i]->cur_page_count;
wasm_munmap_linear_memory(memories[i]->memory_data,
map_size, 8 * (uint64)BH_GB);
#endif
}
}
}
wasm_runtime_free(memories);
}
(void)module_inst;
}
static WASMMemoryInstance *
memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent,
WASMMemoryInstance *memory, uint32 memory_idx,
uint32 num_bytes_per_page, uint32 init_page_count,
uint32 max_page_count, uint32 heap_size, uint32 flags,
char *error_buf, uint32 error_buf_size)
{
WASMModule *module = module_inst->module;
uint64 memory_data_size, max_memory_data_size;
uint32 heap_offset = num_bytes_per_page * init_page_count;
uint32 inc_page_count, aux_heap_base, global_idx;
uint32 bytes_of_last_page, bytes_to_page_end;
uint8 *global_addr;
#ifdef WASM_LINEAR_MEMORY_MMAP
uint8 *mapped_mem = NULL;
uint64 map_size;
#endif
#if WASM_ENABLE_SHARED_MEMORY != 0
bool is_shared_memory = flags & 0x02 ? true : false;
/* shared memory */
if (is_shared_memory && parent != NULL) {
bh_assert(parent->memory_count > memory_idx);
memory = parent->memories[memory_idx];
shared_memory_inc_reference(memory);
return memory;
}
#endif /* end of WASM_ENABLE_SHARED_MEMORY */
if (heap_size > 0 && module_inst->module->malloc_function != (uint32)-1
&& module_inst->module->free_function != (uint32)-1) {
/* Disable app heap, use malloc/free function exported
by wasm app to allocate/free memory instead */
heap_size = 0;
}
if (init_page_count == max_page_count && init_page_count == 1) {
/* If only one page and at most one page, we just append
the app heap to the end of linear memory, enlarge the
num_bytes_per_page, and don't change the page count */
heap_offset = num_bytes_per_page;
num_bytes_per_page += heap_size;
if (num_bytes_per_page < heap_size) {
set_error_buf(error_buf, error_buf_size,
"failed to insert app heap into linear memory, "
"try using `--heap-size=0` option");
return NULL;
}
}
else if (heap_size > 0) {
if (init_page_count == max_page_count && init_page_count == 0) {
/* If the memory data size is always 0, we resize it to
one page for app heap */
num_bytes_per_page = heap_size;
heap_offset = 0;
inc_page_count = 1;
}
else if (module->aux_heap_base_global_index != (uint32)-1
&& module->aux_heap_base
< num_bytes_per_page * init_page_count) {
/* Insert app heap before __heap_base */
aux_heap_base = module->aux_heap_base;
bytes_of_last_page = aux_heap_base % num_bytes_per_page;
if (bytes_of_last_page == 0)
bytes_of_last_page = num_bytes_per_page;
bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
inc_page_count =
(heap_size - bytes_to_page_end + num_bytes_per_page - 1)
/ num_bytes_per_page;
heap_offset = aux_heap_base;
aux_heap_base += heap_size;
bytes_of_last_page = aux_heap_base % num_bytes_per_page;
if (bytes_of_last_page == 0)
bytes_of_last_page = num_bytes_per_page;
bytes_to_page_end = num_bytes_per_page - bytes_of_last_page;
if (bytes_to_page_end < 1 * BH_KB) {
aux_heap_base += 1 * BH_KB;
inc_page_count++;
}
/* Adjust __heap_base global value */
global_idx = module->aux_heap_base_global_index;
bh_assert(module_inst->e->globals
&& global_idx < module_inst->e->global_count);
global_addr = module_inst->global_data
+ module_inst->e->globals[global_idx].data_offset;
*(uint32 *)global_addr = aux_heap_base;
LOG_VERBOSE("Reset __heap_base global to %u", aux_heap_base);
}
else {
/* Insert app heap before new page */
inc_page_count =
(heap_size + num_bytes_per_page - 1) / num_bytes_per_page;
heap_offset = num_bytes_per_page * init_page_count;
heap_size = num_bytes_per_page * inc_page_count;
if (heap_size > 0)
heap_size -= 1 * BH_KB;
}
init_page_count += inc_page_count;
max_page_count += inc_page_count;
if (init_page_count > DEFAULT_MAX_PAGES) {
set_error_buf(error_buf, error_buf_size,
"failed to insert app heap into linear memory, "
"try using `--heap-size=0` option");
return NULL;
}
else if (init_page_count == DEFAULT_MAX_PAGES) {
num_bytes_per_page = UINT32_MAX;
init_page_count = max_page_count = 1;
}
if (max_page_count > DEFAULT_MAX_PAGES)
max_page_count = DEFAULT_MAX_PAGES;
}
else { /* heap_size == 0 */
if (init_page_count == DEFAULT_MAX_PAGES) {
num_bytes_per_page = UINT32_MAX;
init_page_count = max_page_count = 1;
}
}
LOG_VERBOSE("Memory instantiate:");
LOG_VERBOSE(" page bytes: %u, init pages: %u, max pages: %u",
num_bytes_per_page, init_page_count, max_page_count);
LOG_VERBOSE(" heap offset: %u, heap size: %d\n", heap_offset, heap_size);
memory_data_size = (uint64)num_bytes_per_page * init_page_count;
max_memory_data_size = (uint64)num_bytes_per_page * max_page_count;
bh_assert(memory_data_size <= UINT32_MAX);
bh_assert(max_memory_data_size <= 4 * (uint64)BH_GB);
(void)max_memory_data_size;
bh_assert(memory != NULL);
#ifndef OS_ENABLE_HW_BOUND_CHECK
#if WASM_ENABLE_SHARED_MEMORY != 0
if (is_shared_memory) {
/* Allocate maximum memory size when memory is shared */
#if WASM_ENABLE_SHARED_MEMORY_MMAP != 0
map_size = max_memory_data_size;
if (max_memory_data_size > 0
&& !(memory->memory_data = mapped_mem =
wasm_mmap_linear_memory(map_size, &max_memory_data_size,
error_buf, error_buf_size))) {
goto fail1;
}
#else
if (max_memory_data_size > 0
&& !(memory->memory_data = runtime_malloc(
max_memory_data_size, error_buf, error_buf_size))) {
goto fail1;
}
#endif
}
else
#endif /* end of WASM_ENABLE_SHARED_MEMORY != 0 */
{
/* Allocate initial memory size when memory is not shared */
if (memory_data_size > 0
&& !(memory->memory_data = runtime_malloc(
memory_data_size, error_buf, error_buf_size))) {
goto fail1;
}
}
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
/* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
* ea = i + memarg.offset
* both i and memarg.offset are u32 in range 0 to 4G
* so the range of ea is 0 to 8G
*/
map_size = 8 * (uint64)BH_GB;
if (!(memory->memory_data = mapped_mem = wasm_mmap_linear_memory(
map_size, &memory_data_size, error_buf, error_buf_size))) {
set_error_buf(error_buf, error_buf_size, "mmap memory failed");
goto fail1;
}
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
memory->module_type = Wasm_Module_Bytecode;
memory->num_bytes_per_page = num_bytes_per_page;
memory->cur_page_count = init_page_count;
memory->max_page_count = max_page_count;
memory->memory_data_size = (uint32)memory_data_size;
memory->heap_data = memory->memory_data + heap_offset;
memory->heap_data_end = memory->heap_data + heap_size;
memory->memory_data_end = memory->memory_data + (uint32)memory_data_size;
/* Initialize heap */
if (heap_size > 0) {
uint32 heap_struct_size = mem_allocator_get_heap_struct_size();
if (!(memory->heap_handle = runtime_malloc(
(uint64)heap_struct_size, error_buf, error_buf_size))) {
goto fail2;
}
if (!mem_allocator_create_with_struct_and_pool(
memory->heap_handle, heap_struct_size, memory->heap_data,
heap_size)) {
set_error_buf(error_buf, error_buf_size, "init app heap failed");
goto fail3;
}
}
if (memory_data_size > 0) {
wasm_runtime_set_mem_bound_check_bytes(memory, memory_data_size);
}
#if WASM_ENABLE_SHARED_MEMORY != 0
if (is_shared_memory) {
memory->is_shared_memory = 1;
memory->ref_count = 1;
}
#endif
LOG_VERBOSE("Memory instantiate success.");
return memory;
fail3:
if (heap_size > 0)
wasm_runtime_free(memory->heap_handle);
fail2:
#ifdef WASM_LINEAR_MEMORY_MMAP
if (mapped_mem)
wasm_munmap_linear_memory(mapped_mem, memory_data_size, map_size);
else
#endif
{
if (memory->memory_data)
wasm_runtime_free(memory->memory_data);
}
fail1:
return NULL;
}
/**
* Instantiate memories in a module.
*/
static WASMMemoryInstance **
memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
WASMModuleInstance *parent, uint32 heap_size,
char *error_buf, uint32 error_buf_size)
{
WASMImport *import;
uint32 mem_index = 0, i,
memory_count = module->import_memory_count + module->memory_count;
uint64 total_size;
WASMMemoryInstance **memories, *memory;
total_size = sizeof(WASMMemoryInstance *) * (uint64)memory_count;
if (!(memories = runtime_malloc(total_size, error_buf, error_buf_size))) {
return NULL;
}
memory = module_inst->global_table_data.memory_instances;
/* instantiate memories from import section */
import = module->import_memories;
for (i = 0; i < module->import_memory_count; i++, import++, memory++) {
uint32 num_bytes_per_page = import->u.memory.num_bytes_per_page;
uint32 init_page_count = import->u.memory.init_page_count;
uint32 max_page_count = import->u.memory.max_page_count;
uint32 flags = import->u.memory.flags;
uint32 actual_heap_size = heap_size;
#if WASM_ENABLE_MULTI_MODULE != 0
if (import->u.memory.import_module != NULL) {
WASMModuleInstance *module_inst_linked;
if (!(module_inst_linked = get_sub_module_inst(
module_inst, import->u.memory.import_module))) {
set_error_buf(error_buf, error_buf_size, "unknown memory");
memories_deinstantiate(module_inst, memories, memory_count);
return NULL;
}
if (!(memories[mem_index++] = wasm_lookup_memory(
module_inst_linked, import->u.memory.field_name))) {
set_error_buf(error_buf, error_buf_size, "unknown memory");
memories_deinstantiate(module_inst, memories, memory_count);
return NULL;
}
}
else
#endif
{
if (!(memories[mem_index] = memory_instantiate(
module_inst, parent, memory, mem_index,
num_bytes_per_page, init_page_count, max_page_count,
actual_heap_size, flags, error_buf, error_buf_size))) {
memories_deinstantiate(module_inst, memories, memory_count);
return NULL;
}
mem_index++;
}
}
/* instantiate memories from memory section */
for (i = 0; i < module->memory_count; i++, memory++) {
if (!(memories[mem_index] = memory_instantiate(
module_inst, parent, memory, mem_index,
module->memories[i].num_bytes_per_page,
module->memories[i].init_page_count,
module->memories[i].max_page_count, heap_size,
module->memories[i].flags, error_buf, error_buf_size))) {
memories_deinstantiate(module_inst, memories, memory_count);
return NULL;
}
mem_index++;
}
bh_assert(mem_index == memory_count);
(void)module_inst;
return memories;
}
/**
* Destroy table instances.
*/
static void
tables_deinstantiate(WASMModuleInstance *module_inst)
{
if (module_inst->tables) {
wasm_runtime_free(module_inst->tables);
}
#if WASM_ENABLE_MULTI_MODULE != 0
if (module_inst->e->table_insts_linked) {
wasm_runtime_free(module_inst->e->table_insts_linked);
}
#endif
}
/**
* Instantiate tables in a module.
*/
static WASMTableInstance **
tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
WASMTableInstance *first_table, char *error_buf,
uint32 error_buf_size)
{
WASMImport *import;
uint32 table_index = 0, i;
uint32 table_count = module->import_table_count + module->table_count;
uint64 total_size = (uint64)sizeof(WASMTableInstance *) * table_count;
WASMTableInstance **tables, *table = first_table;
#if WASM_ENABLE_MULTI_MODULE != 0
uint64 total_size_of_tables_linked =
(uint64)sizeof(WASMTableInstance *) * module->import_table_count;
WASMTableInstance **table_linked = NULL;
#endif
if (!(tables = runtime_malloc(total_size, error_buf, error_buf_size))) {
return NULL;
}
#if WASM_ENABLE_MULTI_MODULE != 0
if (module->import_table_count > 0
&& !(module_inst->e->table_insts_linked = table_linked = runtime_malloc(
total_size_of_tables_linked, error_buf, error_buf_size))) {
goto fail;
}
#endif
/* instantiate tables from import section */
import = module->import_tables;
for (i = 0; i < module->import_table_count; i++, import++) {
uint32 max_size_fixed = 0;
#if WASM_ENABLE_MULTI_MODULE != 0
WASMTableInstance *table_inst_linked = NULL;
WASMModuleInstance *module_inst_linked = NULL;
if (import->u.table.import_module) {
if (!(module_inst_linked = get_sub_module_inst(
module_inst, import->u.table.import_module))) {
set_error_buf(error_buf, error_buf_size, "unknown table");
goto fail;
}
if (!(table_inst_linked = wasm_lookup_table(
module_inst_linked, import->u.table.field_name))) {
set_error_buf(error_buf, error_buf_size, "unknown table");
goto fail;
}
total_size = offsetof(WASMTableInstance, elems);
}
else
#endif
{
/* in order to save memory, alloc resource as few as possible */
max_size_fixed = import->u.table.possible_grow
? import->u.table.max_size
: import->u.table.init_size;
/* it is a built-in table, every module has its own */
total_size = offsetof(WASMTableInstance, elems);
total_size += (uint64)max_size_fixed * sizeof(uint32);
}
tables[table_index++] = table;
/* Set all elements to -1 to mark them as uninitialized elements */
memset(table, -1, (uint32)total_size);
#if WASM_ENABLE_MULTI_MODULE != 0
*table_linked = table_inst_linked;
if (table_inst_linked != NULL) {
table->cur_size = table_inst_linked->cur_size;
table->max_size = table_inst_linked->max_size;
}
else
#endif
{
table->cur_size = import->u.table.init_size;
table->max_size = max_size_fixed;
}
table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
#if WASM_ENABLE_MULTI_MODULE != 0
table_linked++;
#endif
}
/* instantiate tables from table section */
for (i = 0; i < module->table_count; i++) {
uint32 max_size_fixed = 0;
total_size = offsetof(WASMTableInstance, elems);
#if WASM_ENABLE_MULTI_MODULE != 0
/* in case, a module which imports this table will grow it */
max_size_fixed = module->tables[i].max_size;
#else
max_size_fixed = module->tables[i].possible_grow
? module->tables[i].max_size
: module->tables[i].init_size;
#endif
total_size += sizeof(uint32) * (uint64)max_size_fixed;
tables[table_index++] = table;
/* Set all elements to -1 to mark them as uninitialized elements */
memset(table, -1, (uint32)total_size);
table->cur_size = module->tables[i].init_size;
table->max_size = max_size_fixed;
table = (WASMTableInstance *)((uint8 *)table + (uint32)total_size);
}
bh_assert(table_index == table_count);
(void)module_inst;
return tables;
#if WASM_ENABLE_MULTI_MODULE != 0
fail:
wasm_runtime_free(tables);
return NULL;
#endif
}
/**
* Destroy function instances.
*/
static void
functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
{
if (functions) {
wasm_runtime_free(functions);
}
}
/**
* Instantiate functions in a module.
*/
static WASMFunctionInstance *
functions_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
char *error_buf, uint32 error_buf_size)
{
WASMImport *import;
uint32 i,
function_count = module->import_function_count + module->function_count;
uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
WASMFunctionInstance *functions, *function;
if (!(functions = runtime_malloc(total_size, error_buf, error_buf_size))) {
return NULL;
}
total_size = sizeof(void *) * (uint64)module->import_function_count;
if (total_size > 0
&& !(module_inst->import_func_ptrs =
runtime_malloc(total_size, error_buf, error_buf_size))) {
wasm_runtime_free(functions);
return NULL;
}
/* instantiate functions from import section */
function = functions;
import = module->import_functions;
for (i = 0; i < module->import_function_count; i++, import++) {
function->is_import_func = true;
#if WASM_ENABLE_MULTI_MODULE != 0
if (import->u.function.import_module) {
function->import_module_inst = get_sub_module_inst(
module_inst, import->u.function.import_module);
if (function->import_module_inst) {
function->import_func_inst =
wasm_lookup_function(function->import_module_inst,
import->u.function.field_name, NULL);
}
}
#endif /* WASM_ENABLE_MULTI_MODULE */
function->u.func_import = &import->u.function;
function->param_cell_num = import->u.function.func_type->param_cell_num;
function->ret_cell_num = import->u.function.func_type->ret_cell_num;
function->param_count =
(uint16)function->u.func_import->func_type->param_count;
function->param_types = function->u.func_import->func_type->types;
function->local_cell_num = 0;
function->local_count = 0;
function->local_types = NULL;
/* Copy the function pointer to current instance */
module_inst->import_func_ptrs[i] =
function->u.func_import->func_ptr_linked;
function++;
}
/* instantiate functions from function section */
for (i = 0; i < module->function_count; i++) {
function->is_import_func = false;
function->u.func = module->functions[i];
function->param_cell_num = function->u.func->param_cell_num;
function->ret_cell_num = function->u.func->ret_cell_num;
function->local_cell_num = function->u.func->local_cell_num;
function->param_count =
(uint16)function->u.func->func_type->param_count;
function->local_count = (uint16)function->u.func->local_count;
function->param_types = function->u.func->func_type->types;
function->local_types = function->u.func->local_types;
function->local_offsets = function->u.func->local_offsets;
#if WASM_ENABLE_FAST_INTERP != 0
function->const_cell_num = function->u.func->const_cell_num;
#endif
function++;
}
bh_assert((uint32)(function - functions) == function_count);
#if WASM_ENABLE_FAST_JIT != 0
module_inst->fast_jit_func_ptrs = module->fast_jit_func_ptrs;
#endif
return functions;
}
#if WASM_ENABLE_TAGS != 0
/**
* Destroy tags instances.
*/
static void
tags_deinstantiate(WASMTagInstance *tags, void **import_tag_ptrs)
{
if (tags) {
wasm_runtime_free(tags);
}
if (import_tag_ptrs) {
wasm_runtime_free(import_tag_ptrs);
}
}
/**
* Instantiate tags in a module.
*/
static WASMTagInstance *
tags_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
char *error_buf, uint32 error_buf_size)
{
WASMImport *import;
uint32 i, tag_count = module->import_tag_count + module->tag_count;
uint64 total_size = sizeof(WASMTagInstance) * (uint64)tag_count;
WASMTagInstance *tags, *tag;
if (!(tags = runtime_malloc(total_size, error_buf, error_buf_size))) {
return NULL;
}
total_size = sizeof(void *) * (uint64)module->import_tag_count;
if (total_size > 0
&& !(module_inst->e->import_tag_ptrs =
runtime_malloc(total_size, error_buf, error_buf_size))) {
wasm_runtime_free(tags);
return NULL;
}
/* instantiate tags from import section */
tag = tags;
import = module->import_tags;
for (i = 0; i < module->import_tag_count; i++, import++) {
tag->is_import_tag = true;
tag->u.tag_import = &import->u.tag;
tag->type = import->u.tag.type;
tag->attribute = import->u.tag.attribute;
#if WASM_ENABLE_MULTI_MODULE != 0
if (import->u.tag.import_module) {
if (!(tag->import_module_inst = get_sub_module_inst(
module_inst, import->u.tag.import_module))) {
set_error_buf(error_buf, error_buf_size, "unknown tag");
goto fail;
}
if (!(tag->import_tag_inst =
wasm_lookup_tag(tag->import_module_inst,
import->u.tag.field_name, NULL))) {
set_error_buf(error_buf, error_buf_size, "unknown tag");
goto fail;
}
/* Copy the imported tag to current instance */
module_inst->e->import_tag_ptrs[i] =
tag->u.tag_import->import_tag_linked;
}
#endif
tag++;
}
/* instantiate tags from tag section */
for (i = 0; i < module->tag_count; i++) {
tag->is_import_tag = false;
tag->type = module->tags[i]->type;
tag->u.tag = module->tags[i];
#if WASM_ENABLE_FAST_INTERP != 0
/* tag->const_cell_num = function->u.func->const_cell_num; */
#endif
tag++;
}
bh_assert((uint32)(tag - tags) == tag_count);
return tags;
#if WASM_ENABLE_MULTI_MODULE != 0
fail:
tags_deinstantiate(tags, module_inst->e->import_tag_ptrs);
/* clean up */
module_inst->e->import_tag_ptrs = NULL;
return NULL;
#endif
}
#endif
/**
* Destroy global instances.
*/
static void
globals_deinstantiate(WASMGlobalInstance *globals)
{
if (globals)
wasm_runtime_free(globals);
}
static bool
check_global_init_expr(const WASMModule *module, uint32 global_index,
char *error_buf, uint32 error_buf_size)
{
if (global_index >= module->import_global_count + module->global_count) {
set_error_buf_v(error_buf, error_buf_size, "unknown global %d",
global_index);
return false;
}
/**
* 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.
*
* And initializer expression cannot reference a mutable global.
*/
if (global_index >= module->import_global_count
|| (module->import_globals + global_index)->u.global.is_mutable) {
set_error_buf(error_buf, error_buf_size,
"constant expression required");
return false;
}
return true;
}
/**
* Instantiate globals in a module.
*/
static WASMGlobalInstance *
globals_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
char *error_buf, uint32 error_buf_size)
{
WASMImport *import;
uint32 global_data_offset = 0;
uint32 i, global_count = module->import_global_count + module->global_count;
uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
WASMGlobalInstance *globals, *global;
if (!(globals = runtime_malloc(total_size, error_buf, error_buf_size))) {
return NULL;
}
/* instantiate globals from import section */
global = globals;
import = module->import_globals;
for (i = 0; i < module->import_global_count; i++, import++) {
WASMGlobalImport *global_import = &import->u.global;
global->type = global_import->type;
global->is_mutable = global_import->is_mutable;
#if WASM_ENABLE_MULTI_MODULE != 0
if (global_import->import_module) {
if (!(global->import_module_inst = get_sub_module_inst(
module_inst, global_import->import_module))) {
set_error_buf(error_buf, error_buf_size, "unknown global");
goto fail;
}
if (!(global->import_global_inst = wasm_lookup_global(
global->import_module_inst, global_import->field_name))) {
set_error_buf(error_buf, error_buf_size, "unknown global");
goto fail;
}
/* The linked global instance has been initialized, we
just need to copy the value. */
bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
&(global_import->import_global_linked->init_expr),
sizeof(WASMValue));
}
else
#endif
{
/* native globals share their initial_values in one module */
bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
&(global_import->global_data_linked),
sizeof(WASMValue));
}
#if WASM_ENABLE_FAST_JIT != 0
bh_assert(global_data_offset == global_import->data_offset);
#endif
global->data_offset = global_data_offset;
global_data_offset += wasm_value_type_size(global->type);
global++;
}
/* instantiate globals from global section */
for (i = 0; i < module->global_count; i++) {
InitializerExpression *init_expr = &(module->globals[i].init_expr);
global->type = module->globals[i].type;
global->is_mutable = module->globals[i].is_mutable;
#if WASM_ENABLE_FAST_JIT != 0
bh_assert(global_data_offset == module->globals[i].data_offset);
#endif
global->data_offset = global_data_offset;
global_data_offset += wasm_value_type_size(global->type);
if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
if (!check_global_init_expr(module, init_expr->u.global_index,
error_buf, error_buf_size)) {
goto fail;
}
bh_memcpy_s(
&(global->initial_value), sizeof(WASMValue),
&(globals[init_expr->u.global_index].initial_value),
sizeof(globals[init_expr->u.global_index].initial_value));
}
#if WASM_ENABLE_REF_TYPES != 0
else if (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST) {
global->initial_value.u32 = (uint32)NULL_REF;
}
#endif
else {
bh_memcpy_s(&(global->initial_value), sizeof(WASMValue),
&(init_expr->u), sizeof(init_expr->u));
}
global++;
}
bh_assert((uint32)(global - globals) == global_count);
bh_assert(global_data_offset == module->global_data_size);
(void)module_inst;
return globals;
fail:
wasm_runtime_free(globals);
return NULL;
}
/**
* Return export function count in module export section.
*/
static uint32
get_export_count(const WASMModule *module, uint8 kind)
{
WASMExport *export = module->exports;
uint32 count = 0, i;
for (i = 0; i < module->export_count; i++, export ++)
if (export->kind == kind)
count++;
return count;
}
/**
* Destroy export function instances.
*/
static void
export_functions_deinstantiate(WASMExportFuncInstance *functions)
{
if (functions)
wasm_runtime_free(functions);
}
/**
* Instantiate export functions in a module.
*/