-
Notifications
You must be signed in to change notification settings - Fork 786
Expand file tree
/
Copy pathwasm_native.c
More file actions
1576 lines (1421 loc) · 53.7 KB
/
wasm_native.c
File metadata and controls
1576 lines (1421 loc) · 53.7 KB
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_native.h"
#include "wasm_runtime_common.h"
#include "bh_log.h"
#if WASM_ENABLE_INTERP != 0
#include "../interpreter/wasm_runtime.h"
#endif
#if WASM_ENABLE_AOT != 0
#include "../aot/aot_runtime.h"
#endif
#if WASM_ENABLE_THREAD_MGR != 0
#include "../libraries/thread-mgr/thread_manager.h"
#endif
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
#include "wasi_nn_host.h"
#endif
static NativeSymbolsList g_native_symbols_list = NULL;
#if WASM_ENABLE_LIBC_WASI != 0
static void *g_wasi_context_key;
#endif /* WASM_ENABLE_LIBC_WASI */
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
static void *g_wasi_nn_registry_key;
#endif
uint32
get_libc_builtin_export_apis(NativeSymbol **p_libc_builtin_apis);
#if WASM_ENABLE_SPEC_TEST != 0
uint32
get_spectest_export_apis(NativeSymbol **p_libc_builtin_apis);
#endif
#if WASM_ENABLE_SHARED_HEAP != 0
uint32
get_lib_shared_heap_export_apis(NativeSymbol **p_shared_heap_apis);
#endif
uint32
get_libc_wasi_export_apis(NativeSymbol **p_libc_wasi_apis);
uint32
get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
uint32
get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
#if WASM_ENABLE_LIB_PTHREAD != 0
bool
lib_pthread_init();
void
lib_pthread_destroy();
uint32
get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis);
#endif
#if WASM_ENABLE_LIB_WASI_THREADS != 0
bool
lib_wasi_threads_init(void);
void
lib_wasi_threads_destroy(void);
uint32
get_lib_wasi_threads_export_apis(NativeSymbol **p_lib_wasi_threads_apis);
#endif
uint32
get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis);
uint32
get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis);
static bool
compare_type_with_signature(uint8 type, const char signature)
{
const char num_sig_map[] = { 'F', 'f', 'I', 'i' };
if (VALUE_TYPE_F64 <= type && type <= VALUE_TYPE_I32
&& signature == num_sig_map[type - VALUE_TYPE_F64]) {
return true;
}
#if WASM_ENABLE_REF_TYPES != 0
if ('r' == signature
#if WASM_ENABLE_GC != 0
#if WASM_ENABLE_STRINGREF != 0
&& (type >= REF_TYPE_STRINGVIEWITER && type <= REF_TYPE_NULLFUNCREF)
#else
&& (type >= REF_TYPE_HT_NULLABLE && type <= REF_TYPE_NULLFUNCREF)
#endif
#else
&& type == VALUE_TYPE_EXTERNREF
#endif
)
return true;
#endif
/* TODO: a v128 parameter */
return false;
}
static bool
check_symbol_signature(const WASMFuncType *type, const char *signature)
{
const char *p = signature, *p_end;
char sig;
uint32 i = 0;
if (!p || strlen(p) < 2)
return false;
p_end = p + strlen(signature);
if (*p++ != '(')
return false;
if ((uint32)(p_end - p) < (uint32)(type->param_count + 1))
/* signatures of parameters, and ')' */
return false;
for (i = 0; i < type->param_count; i++) {
sig = *p++;
/* a f64/f32/i64/i32/externref parameter */
if (compare_type_with_signature(type->types[i], sig))
continue;
/* a pointer/string parameter */
if (type->types[i] != VALUE_TYPE_I32)
/* pointer and string must be i32 type */
return false;
if (sig == '*') {
/* it is a pointer */
if (i + 1 < type->param_count
&& type->types[i + 1] == VALUE_TYPE_I32 && *p == '~') {
/* pointer length followed */
i++;
p++;
}
}
else if (sig == '$') {
/* it is a string */
}
else {
/* invalid signature */
return false;
}
}
if (*p++ != ')')
return false;
if (type->result_count) {
if (p >= p_end)
return false;
/* result types includes: f64,f32,i64,i32,externref */
if (!compare_type_with_signature(type->types[i], *p))
return false;
p++;
}
if (*p != '\0')
return false;
return true;
}
static int
native_symbol_cmp(const void *native_symbol1, const void *native_symbol2)
{
return strcmp(((const NativeSymbol *)native_symbol1)->symbol,
((const NativeSymbol *)native_symbol2)->symbol);
}
static void *
lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
const char *symbol, const char **p_signature, void **p_attachment)
{
NativeSymbol *native_symbol, key = { 0 };
key.symbol = symbol;
if ((native_symbol = bsearch(&key, native_symbols, n_native_symbols,
sizeof(NativeSymbol), native_symbol_cmp))) {
*p_signature = native_symbol->signature;
*p_attachment = native_symbol->attachment;
return native_symbol->func_ptr;
}
return NULL;
}
/**
* allow func_type and all outputs, like p_signature, p_attachment and
* p_call_conv_raw to be NULL
*/
void *
wasm_native_resolve_symbol(const char *module_name, const char *field_name,
const WASMFuncType *func_type,
const char **p_signature, void **p_attachment,
bool *p_call_conv_raw)
{
NativeSymbolsNode *node, *node_next;
const char *signature = NULL;
void *func_ptr = NULL, *attachment = NULL;
node = g_native_symbols_list;
while (node) {
node_next = node->next;
if (!strcmp(node->module_name, module_name)) {
if ((func_ptr =
lookup_symbol(node->native_symbols, node->n_native_symbols,
field_name, &signature, &attachment))
|| (field_name[0] == '_'
&& (func_ptr = lookup_symbol(
node->native_symbols, node->n_native_symbols,
field_name + 1, &signature, &attachment))))
break;
}
node = node_next;
}
if (!p_signature || !p_attachment || !p_call_conv_raw)
return func_ptr;
if (func_ptr) {
if (signature && signature[0] != '\0') {
/* signature is not empty, check its format */
if (!func_type || !check_symbol_signature(func_type, signature)) {
#if WASM_ENABLE_WAMR_COMPILER == 0
/* Output warning except running aot compiler */
LOG_WARNING("failed to check signature '%s' and resolve "
"pointer params for import function (%s, %s)\n",
signature, module_name, field_name);
#endif
return NULL;
}
else
/* Save signature for runtime to do pointer check and
address conversion */
*p_signature = signature;
}
else
/* signature is empty */
*p_signature = NULL;
*p_attachment = attachment;
*p_call_conv_raw = node->call_conv_raw;
}
return func_ptr;
}
static bool
register_natives(const char *module_name, NativeSymbol *native_symbols,
uint32 n_native_symbols, bool call_conv_raw)
{
NativeSymbolsNode *node;
if (!(node = wasm_runtime_malloc(sizeof(NativeSymbolsNode))))
return false;
#if WASM_ENABLE_MEMORY_TRACING != 0
os_printf("Register native, size: %u\n", sizeof(NativeSymbolsNode));
#endif
node->module_name = module_name;
node->native_symbols = native_symbols;
node->n_native_symbols = n_native_symbols;
node->call_conv_raw = call_conv_raw;
/* Add to list head */
node->next = g_native_symbols_list;
g_native_symbols_list = node;
qsort(native_symbols, n_native_symbols, sizeof(NativeSymbol),
native_symbol_cmp);
return true;
}
bool
wasm_native_register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols)
{
return register_natives(module_name, native_symbols, n_native_symbols,
false);
}
bool
wasm_native_register_natives_raw(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols)
{
return register_natives(module_name, native_symbols, n_native_symbols,
true);
}
bool
wasm_native_unregister_natives(const char *module_name,
NativeSymbol *native_symbols)
{
NativeSymbolsNode **prevp;
NativeSymbolsNode *node;
prevp = &g_native_symbols_list;
while ((node = *prevp) != NULL) {
if (node->native_symbols == native_symbols
&& !strcmp(node->module_name, module_name)) {
*prevp = node->next;
wasm_runtime_free(node);
return true;
}
prevp = &node->next;
}
return false;
}
#if WASM_ENABLE_MODULE_INST_CONTEXT != 0
static uint32
context_key_to_idx(void *key)
{
bh_assert(key != NULL);
uint32 idx = (uint32)(uintptr_t)key;
bh_assert(idx > 0);
bh_assert(idx <= WASM_MAX_INSTANCE_CONTEXTS);
return idx - 1;
}
static void *
context_idx_to_key(uint32 idx)
{
bh_assert(idx < WASM_MAX_INSTANCE_CONTEXTS);
return (void *)(uintptr_t)(idx + 1);
}
typedef void (*dtor_t)(WASMModuleInstanceCommon *, void *);
static dtor_t g_context_dtors[WASM_MAX_INSTANCE_CONTEXTS];
static void
dtor_noop(WASMModuleInstanceCommon *inst, void *ctx)
{
}
void *
wasm_native_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
void *ctx))
{
uint32 i;
for (i = 0; i < WASM_MAX_INSTANCE_CONTEXTS; i++) {
if (g_context_dtors[i] == NULL) {
if (dtor == NULL) {
dtor = dtor_noop;
}
g_context_dtors[i] = dtor;
return context_idx_to_key(i);
}
}
LOG_ERROR("failed to allocate instance context key");
return NULL;
}
void
wasm_native_destroy_context_key(void *key)
{
uint32 idx = context_key_to_idx(key);
bh_assert(g_context_dtors[idx] != NULL);
g_context_dtors[idx] = NULL;
}
static WASMModuleInstanceExtraCommon *
wasm_module_inst_extra_common(WASMModuleInstanceCommon *inst)
{
#if WASM_ENABLE_INTERP != 0
if (inst->module_type == Wasm_Module_Bytecode) {
return &((WASMModuleInstance *)inst)->e->common;
}
#endif
#if WASM_ENABLE_AOT != 0
if (inst->module_type == Wasm_Module_AoT) {
return &((AOTModuleInstanceExtra *)((AOTModuleInstance *)inst)->e)
->common;
}
#endif
bh_assert(false);
return NULL;
}
void
wasm_native_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx)
{
uint32 idx = context_key_to_idx(key);
WASMModuleInstanceExtraCommon *common = wasm_module_inst_extra_common(inst);
common->contexts[idx] = ctx;
}
void
wasm_native_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
void *ctx)
{
#if WASM_ENABLE_THREAD_MGR != 0
wasm_cluster_set_context(inst, key, ctx);
#else
wasm_native_set_context(inst, key, ctx);
#endif
}
void *
wasm_native_get_context(WASMModuleInstanceCommon *inst, void *key)
{
uint32 idx = context_key_to_idx(key);
WASMModuleInstanceExtraCommon *common = wasm_module_inst_extra_common(inst);
return common->contexts[idx];
}
void
wasm_native_call_context_dtors(WASMModuleInstanceCommon *inst)
{
WASMModuleInstanceExtraCommon *common = wasm_module_inst_extra_common(inst);
uint32 i;
for (i = 0; i < WASM_MAX_INSTANCE_CONTEXTS; i++) {
dtor_t dtor = g_context_dtors[i];
if (dtor != NULL) {
dtor(inst, common->contexts[i]);
}
}
}
void
wasm_native_inherit_contexts(WASMModuleInstanceCommon *child,
WASMModuleInstanceCommon *parent)
{
WASMModuleInstanceExtraCommon *parent_common =
wasm_module_inst_extra_common(parent);
WASMModuleInstanceExtraCommon *child_common =
wasm_module_inst_extra_common(child);
bh_memcpy_s(child_common->contexts,
sizeof(*child_common->contexts) * WASM_MAX_INSTANCE_CONTEXTS,
parent_common->contexts,
sizeof(*parent_common->contexts) * WASM_MAX_INSTANCE_CONTEXTS);
}
#endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
#if WASM_ENABLE_LIBC_WASI != 0
WASIContext *
wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst_comm)
{
return wasm_native_get_context(module_inst_comm, g_wasi_context_key);
}
void
wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst_comm,
WASIContext *wasi_ctx)
{
wasm_native_set_context(module_inst_comm, g_wasi_context_key, wasi_ctx);
}
static void
wasi_context_dtor(WASMModuleInstanceCommon *inst, void *ctx)
{
if (ctx == NULL) {
return;
}
wasm_runtime_destroy_wasi(inst);
}
#endif /* end of WASM_ENABLE_LIBC_WASI */
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
WASINNRegistry *
wasm_runtime_get_wasi_nn_registry(WASMModuleInstanceCommon *module_inst_comm)
{
return wasm_native_get_context(module_inst_comm, g_wasi_nn_registry_key);
}
void
wasm_runtime_set_wasi_nn_registry(WASMModuleInstanceCommon *module_inst_comm,
WASINNRegistry *wasi_nn_ctx)
{
wasm_native_set_context(module_inst_comm, g_wasi_nn_registry_key,
wasi_nn_ctx);
}
static void
wasi_nn_context_dtor(WASMModuleInstanceCommon *inst, void *ctx)
{
if (ctx == NULL) {
return;
}
wasm_runtime_wasi_nn_registry_destroy(ctx);
}
#endif
#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
static bool
quick_aot_entry_init(void);
#endif
bool
wasm_native_init()
{
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0 \
|| WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0 \
|| WASM_ENABLE_SHARED_HEAP != 0
NativeSymbol *native_symbols;
uint32 n_native_symbols;
#endif
#if WASM_ENABLE_LIBC_BUILTIN != 0
n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
if (!wasm_native_register_natives("env", native_symbols, n_native_symbols))
goto fail;
#endif /* WASM_ENABLE_LIBC_BUILTIN */
#if WASM_ENABLE_SPEC_TEST
n_native_symbols = get_spectest_export_apis(&native_symbols);
if (!wasm_native_register_natives("spectest", native_symbols,
n_native_symbols))
goto fail;
#endif /* WASM_ENABLE_SPEC_TEST */
#if WASM_ENABLE_LIBC_WASI != 0
g_wasi_context_key = wasm_native_create_context_key(wasi_context_dtor);
if (g_wasi_context_key == NULL) {
goto fail;
}
n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
if (!wasm_native_register_natives("wasi_unstable", native_symbols,
n_native_symbols))
goto fail;
if (!wasm_native_register_natives("wasi_snapshot_preview1", native_symbols,
n_native_symbols))
goto fail;
#endif
#if WASM_ENABLE_SHARED_HEAP != 0
n_native_symbols = get_lib_shared_heap_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
goto fail;
#endif
#if WASM_ENABLE_BASE_LIB != 0
n_native_symbols = get_base_lib_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
goto fail;
#endif
#if WASM_ENABLE_APP_FRAMEWORK != 0
n_native_symbols = get_ext_lib_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
goto fail;
#endif
#if WASM_ENABLE_LIB_PTHREAD != 0
if (!lib_pthread_init())
goto fail;
n_native_symbols = get_lib_pthread_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
goto fail;
#endif
#if WASM_ENABLE_LIB_WASI_THREADS != 0
if (!lib_wasi_threads_init())
goto fail;
n_native_symbols = get_lib_wasi_threads_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("wasi", native_symbols,
n_native_symbols))
goto fail;
#endif
#if WASM_ENABLE_LIBC_EMCC != 0
n_native_symbols = get_libc_emcc_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
goto fail;
#endif /* WASM_ENABLE_LIBC_EMCC */
#if WASM_ENABLE_LIB_RATS != 0
n_native_symbols = get_lib_rats_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives("env", native_symbols,
n_native_symbols))
goto fail;
#endif /* WASM_ENABLE_LIB_RATS */
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
g_wasi_nn_registry_key =
wasm_native_create_context_key(wasi_nn_context_dtor);
if (g_wasi_nn_registry_key == NULL) {
goto fail;
}
if (!wasi_nn_initialize())
goto fail;
n_native_symbols = get_wasi_nn_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives(
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
"wasi_ephemeral_nn",
#else
"wasi_nn",
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
native_symbols, n_native_symbols))
goto fail;
#endif /* WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
if (!quick_aot_entry_init()) {
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0 \
|| WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0 \
|| WASM_ENABLE_SHARED_HEAP != 0
goto fail;
#else
return false;
#endif
}
#endif
return true;
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0 \
|| WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0 \
|| WASM_ENABLE_SHARED_HEAP != 0
fail:
wasm_native_destroy();
return false;
#endif
}
void
wasm_native_destroy()
{
NativeSymbolsNode *node, *node_next;
#if WASM_ENABLE_LIBC_WASI != 0
if (g_wasi_context_key != NULL) {
wasm_native_destroy_context_key(g_wasi_context_key);
g_wasi_context_key = NULL;
}
#endif
#if WASM_ENABLE_LIB_PTHREAD != 0
lib_pthread_destroy();
#endif
#if WASM_ENABLE_LIB_WASI_THREADS != 0
lib_wasi_threads_destroy();
#endif
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
if (g_wasi_nn_registry_key != NULL) {
wasm_native_destroy_context_key(g_wasi_nn_registry_key);
g_wasi_nn_registry_key = NULL;
}
wasi_nn_destroy();
#endif
node = g_native_symbols_list;
while (node) {
node_next = node->next;
wasm_runtime_free(node);
node = node_next;
}
g_native_symbols_list = NULL;
}
#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
static void
invoke_no_args_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *) = func_ptr;
native_code(exec_env);
}
static void
invoke_no_args_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *) = func_ptr;
argv_ret[0] = native_code(exec_env);
}
static void
invoke_no_args_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *) = func_ptr;
int64 ret = native_code(exec_env);
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_i_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int32) = func_ptr;
native_code(exec_env, argv[0]);
}
static void
invoke_i_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int32) = func_ptr;
argv_ret[0] = native_code(exec_env, argv[0]);
}
static void
invoke_i_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int32) = func_ptr;
int64 ret = native_code(exec_env, argv[0]);
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_I_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int64) = func_ptr;
native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv));
}
static void
invoke_I_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int64) = func_ptr;
argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv));
}
static void
invoke_I_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int64) = func_ptr;
int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv));
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_ii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int32, int32) = func_ptr;
native_code(exec_env, argv[0], argv[1]);
}
static void
invoke_ii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int32, int32) = func_ptr;
argv_ret[0] = native_code(exec_env, argv[0], argv[1]);
}
static void
invoke_ii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int32, int32) = func_ptr;
int64 ret = native_code(exec_env, argv[0], argv[1]);
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_iI_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int32, int64) = func_ptr;
native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1));
}
static void
invoke_iI_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int32, int64) = func_ptr;
argv_ret[0] =
native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1));
}
static void
invoke_iI_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int32, int64) = func_ptr;
int64 ret =
native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1));
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_Ii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int64, int32) = func_ptr;
native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2]);
}
static void
invoke_Ii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int64, int32) = func_ptr;
argv_ret[0] =
native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2]);
}
static void
invoke_Ii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int64, int32) = func_ptr;
int64 ret =
native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2]);
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_II_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int64, int64) = func_ptr;
native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv),
GET_I64_FROM_ADDR((uint32 *)argv + 2));
}
static void
invoke_II_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int64, int64) = func_ptr;
argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv),
GET_I64_FROM_ADDR((uint32 *)argv + 2));
}
static void
invoke_II_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int64, int64) = func_ptr;
int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv),
GET_I64_FROM_ADDR((uint32 *)argv + 2));
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_iii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int32, int32, int32) = func_ptr;
native_code(exec_env, argv[0], argv[1], argv[2]);
}
static void
invoke_iii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int32, int32, int32) = func_ptr;
argv_ret[0] = native_code(exec_env, argv[0], argv[1], argv[2]);
}
static void
invoke_iii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int32, int32, int32) = func_ptr;
int64 ret = native_code(exec_env, argv[0], argv[1], argv[2]);
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_iiI_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int32, int32, int64) = func_ptr;
native_code(exec_env, argv[0], argv[1],
GET_I64_FROM_ADDR((uint32 *)argv + 2));
}
static void
invoke_iiI_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int32, int32, int64) = func_ptr;
argv_ret[0] = native_code(exec_env, argv[0], argv[1],
GET_I64_FROM_ADDR((uint32 *)argv + 2));
}
static void
invoke_iiI_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int32, int32, int64) = func_ptr;
int64 ret = native_code(exec_env, argv[0], argv[1],
GET_I64_FROM_ADDR((uint32 *)argv + 2));
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_iIi_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int32, int64, int32) = func_ptr;
native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1),
argv[3]);
}
static void
invoke_iIi_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int32, int64, int32) = func_ptr;
argv_ret[0] = native_code(exec_env, argv[0],
GET_I64_FROM_ADDR((uint32 *)argv + 1), argv[3]);
}
static void
invoke_iIi_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int32, int64, int32) = func_ptr;
int64 ret = native_code(exec_env, argv[0],
GET_I64_FROM_ADDR((uint32 *)argv + 1), argv[3]);
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_iII_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int32, int64, int64) = func_ptr;
native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1),
GET_I64_FROM_ADDR((uint32 *)argv + 3));
}
static void
invoke_iII_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int32, int64, int64) = func_ptr;
argv_ret[0] =
native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1),
GET_I64_FROM_ADDR((uint32 *)argv + 3));
}
static void
invoke_iII_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int32, int64, int64) = func_ptr;
int64 ret =
native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1),
GET_I64_FROM_ADDR((uint32 *)argv + 3));
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_Iii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int64, int32, int32) = func_ptr;
native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], argv[3]);
}
static void
invoke_Iii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int64, int32, int32) = func_ptr;
argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv),
argv[2], argv[3]);
}
static void
invoke_Iii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int64, int32, int32) = func_ptr;
int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv),
argv[2], argv[3]);
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_IiI_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int64, int32, int64) = func_ptr;
native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2],
GET_I64_FROM_ADDR((uint32 *)argv + 3));
}
static void
invoke_IiI_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int64, int32, int64) = func_ptr;
argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv),
argv[2], GET_I64_FROM_ADDR((uint32 *)argv + 3));
}
static void
invoke_IiI_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int64 (*native_code)(WASMExecEnv *, int64, int32, int64) = func_ptr;
int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv),
argv[2], GET_I64_FROM_ADDR((uint32 *)argv + 3));
PUT_I64_TO_ADDR(argv_ret, ret);
}
static void
invoke_IIi_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
void (*native_code)(WASMExecEnv *, int64, int64, int32) = func_ptr;
native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv),
GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4]);
}
static void
invoke_IIi_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret)
{
int32 (*native_code)(WASMExecEnv *, int64, int64, int32) = func_ptr;
argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv),
GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4]);