krillplayer-plugin 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/LICENSE +2 -1
  2. package/README.md +2 -0
  3. package/dist/cli.js +397 -80
  4. package/dist/crypto.js +38 -11
  5. package/dist/engine/engine.c +6 -3
  6. package/kernel/Sources/CQuickJS/include/kernel_qjs.h +8 -1
  7. package/kernel/Sources/CQuickJS/kernel_internal.h +54 -0
  8. package/kernel/Sources/CQuickJS/kernel_qjs.c +17 -29
  9. package/kernel/Sources/CQuickJS/kernel_wasm.c +1155 -0
  10. package/kernel/Sources/CWAMR/include/kernel_wamr.h +151 -0
  11. package/kernel/Sources/CWAMR/include/kernel_wamr_config.h +66 -0
  12. package/kernel/Sources/CWAMR/vendor/LICENSE +219 -0
  13. package/kernel/Sources/CWAMR/vendor/core/config.h +727 -0
  14. package/kernel/Sources/CWAMR/vendor/core/iwasm/aot/aot_runtime.h +899 -0
  15. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/arch/invokeNative_general.c +123 -0
  16. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_application.c +933 -0
  17. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_blocking_op.c +92 -0
  18. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api.c +5391 -0
  19. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api_internal.h +246 -0
  20. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.c +336 -0
  21. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.h +328 -0
  22. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.c +242 -0
  23. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.h +62 -0
  24. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.c +1999 -0
  25. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.h +154 -0
  26. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.c +1532 -0
  27. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.h +117 -0
  28. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.c +8070 -0
  29. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.h +1396 -0
  30. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.c +477 -0
  31. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.h +63 -0
  32. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_suspend_flags.h +52 -0
  33. package/kernel/Sources/CWAMR/vendor/core/iwasm/compilation/aot.h +366 -0
  34. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/lib_export.h +60 -0
  35. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_c_api.h +910 -0
  36. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_export.h +2440 -0
  37. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm.h +1492 -0
  38. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp.h +122 -0
  39. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp_fast.c +7998 -0
  40. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.c +16909 -0
  41. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.h +80 -0
  42. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_opcode.h +1043 -0
  43. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.c +5174 -0
  44. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.h +912 -0
  45. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.c +1569 -0
  46. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.h +256 -0
  47. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_alloc.c +1169 -0
  48. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.c +497 -0
  49. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.h +350 -0
  50. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc_internal.h +384 -0
  51. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_hmu.c +95 -0
  52. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_kfc.c +547 -0
  53. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.c +252 -0
  54. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.h +96 -0
  55. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/memory/mremap.c +12 -0
  56. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_blocking_op.c +70 -0
  57. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_malloc.c +72 -0
  58. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_memmap.c +302 -0
  59. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_sleep.c +20 -0
  60. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_thread.c +784 -0
  61. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_time.c +28 -0
  62. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_init.c +43 -0
  63. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_internal.h +126 -0
  64. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_extension.h +1676 -0
  65. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_vmcore.h +192 -0
  66. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_common.h +204 -0
  67. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_wasi_types.h +610 -0
  68. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_init.c +43 -0
  69. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_internal.h +139 -0
  70. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.c +25 -0
  71. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.h +42 -0
  72. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_atomic.h +300 -0
  73. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.c +27 -0
  74. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.h +114 -0
  75. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.c +217 -0
  76. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.h +83 -0
  77. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.c +339 -0
  78. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.h +168 -0
  79. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.c +77 -0
  80. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.h +30 -0
  81. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.c +111 -0
  82. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.h +109 -0
  83. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.c +111 -0
  84. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.h +94 -0
  85. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_platform.h +38 -0
  86. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.c +256 -0
  87. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.h +80 -0
  88. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.c +279 -0
  89. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.h +126 -0
  90. package/kernel/Sources/CWAMR/vendor/core/shared/utils/gnuc.h +14 -0
  91. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.c +469 -0
  92. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.h +51 -0
  93. package/kernel/Sources/CWAMR/vendor/core/version.h +24 -0
  94. package/kernel/Sources/CWAMR/wamr_bh_assert.c +98 -0
  95. package/kernel/Sources/CWAMR/wamr_bh_bitmap.c +98 -0
  96. package/kernel/Sources/CWAMR/wamr_bh_common.c +98 -0
  97. package/kernel/Sources/CWAMR/wamr_bh_hashmap.c +98 -0
  98. package/kernel/Sources/CWAMR/wamr_bh_leb128.c +98 -0
  99. package/kernel/Sources/CWAMR/wamr_bh_list.c +98 -0
  100. package/kernel/Sources/CWAMR/wamr_bh_log.c +98 -0
  101. package/kernel/Sources/CWAMR/wamr_bh_queue.c +98 -0
  102. package/kernel/Sources/CWAMR/wamr_bh_vector.c +98 -0
  103. package/kernel/Sources/CWAMR/wamr_ems_alloc.c +98 -0
  104. package/kernel/Sources/CWAMR/wamr_ems_gc.c +98 -0
  105. package/kernel/Sources/CWAMR/wamr_ems_hmu.c +98 -0
  106. package/kernel/Sources/CWAMR/wamr_ems_kfc.c +98 -0
  107. package/kernel/Sources/CWAMR/wamr_invokeNative_general.c +98 -0
  108. package/kernel/Sources/CWAMR/wamr_mem_alloc.c +98 -0
  109. package/kernel/Sources/CWAMR/wamr_mremap.c +98 -0
  110. package/kernel/Sources/CWAMR/wamr_platform_init_darwin.c +100 -0
  111. package/kernel/Sources/CWAMR/wamr_platform_init_linux.c +100 -0
  112. package/kernel/Sources/CWAMR/wamr_posix_blocking_op.c +98 -0
  113. package/kernel/Sources/CWAMR/wamr_posix_malloc.c +98 -0
  114. package/kernel/Sources/CWAMR/wamr_posix_memmap.c +98 -0
  115. package/kernel/Sources/CWAMR/wamr_posix_sleep.c +98 -0
  116. package/kernel/Sources/CWAMR/wamr_posix_thread.c +98 -0
  117. package/kernel/Sources/CWAMR/wamr_posix_time.c +98 -0
  118. package/kernel/Sources/CWAMR/wamr_runtime_timer.c +98 -0
  119. package/kernel/Sources/CWAMR/wamr_thread_manager.c +98 -0
  120. package/kernel/Sources/CWAMR/wamr_wasm_application.c +98 -0
  121. package/kernel/Sources/CWAMR/wamr_wasm_blocking_op.c +98 -0
  122. package/kernel/Sources/CWAMR/wamr_wasm_c_api.c +98 -0
  123. package/kernel/Sources/CWAMR/wamr_wasm_exec_env.c +98 -0
  124. package/kernel/Sources/CWAMR/wamr_wasm_interp_fast.c +98 -0
  125. package/kernel/Sources/CWAMR/wamr_wasm_loader.c +98 -0
  126. package/kernel/Sources/CWAMR/wamr_wasm_loader_common.c +98 -0
  127. package/kernel/Sources/CWAMR/wamr_wasm_memory.c +98 -0
  128. package/kernel/Sources/CWAMR/wamr_wasm_native.c +98 -0
  129. package/kernel/Sources/CWAMR/wamr_wasm_runtime.c +98 -0
  130. package/kernel/Sources/CWAMR/wamr_wasm_runtime_common.c +98 -0
  131. package/kernel/Sources/CWAMR/wamr_wasm_shared_memory.c +98 -0
  132. package/kernel/Sources/Kernel/PluginPrelude.js +5 -2
  133. package/kernel/Tests/KernelTests/CasePins.js +50 -0
  134. package/package.json +2 -2
  135. package/skill/krillplayer-plugin/SKILL.md +2 -0
  136. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-content-rejected.json +8 -4
  137. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-login-expired.json +8 -4
  138. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-muted.json +8 -4
  139. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-other.json +8 -4
  140. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-rate-limited.json +8 -4
  141. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-sent.json +8 -4
  142. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-logged-in.json +39 -0
  143. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-backup-line.json +46 -0
  144. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-line-gone.json +46 -0
  145. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live.json +21 -1
  146. package/skill/krillplayer-plugin/assets/samples/live-video-platform/plugin.ts +39 -4
  147. package/skill/krillplayer-plugin/references/cases.md +20 -2
  148. package/skill/krillplayer-plugin/references/contract.d.ts +114 -12
  149. package/skill/krillplayer-plugin/references/crypto-tools.md +104 -0
  150. package/skill/krillplayer-plugin/references/errors-and-fixes.md +5 -1
@@ -0,0 +1,2440 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ /**
7
+ * @file wasm_export.h
8
+ *
9
+ * @brief This file defines the exported common runtime APIs
10
+ */
11
+
12
+ #ifndef _WASM_EXPORT_H
13
+ #define _WASM_EXPORT_H
14
+
15
+ #include <stdint.h>
16
+ #include <stdbool.h>
17
+ #include "lib_export.h"
18
+
19
+ #ifndef WASM_RUNTIME_API_EXTERN
20
+ #if defined(_MSC_BUILD)
21
+ #if defined(COMPILING_WASM_RUNTIME_API)
22
+ #define WASM_RUNTIME_API_EXTERN __declspec(dllexport)
23
+ #else
24
+ #define WASM_RUNTIME_API_EXTERN __declspec(dllimport)
25
+ #endif
26
+ #elif defined(__GNUC__) || defined(__clang__)
27
+ #define WASM_RUNTIME_API_EXTERN __attribute__((visibility("default")))
28
+ #else
29
+ #define WASM_RUNTIME_API_EXTERN
30
+ #endif
31
+ #endif
32
+
33
+ #ifdef __cplusplus
34
+ extern "C" {
35
+ #endif
36
+
37
+ #define get_module_inst(exec_env) wasm_runtime_get_module_inst(exec_env)
38
+
39
+ #define validate_app_addr(offset, size) \
40
+ wasm_runtime_validate_app_addr(module_inst, offset, size)
41
+
42
+ #define validate_app_str_addr(offset) \
43
+ wasm_runtime_validate_app_str_addr(module_inst, offset)
44
+
45
+ #define addr_app_to_native(offset) \
46
+ wasm_runtime_addr_app_to_native(module_inst, offset)
47
+
48
+ #define addr_native_to_app(ptr) \
49
+ wasm_runtime_addr_native_to_app(module_inst, ptr)
50
+
51
+ #define module_malloc(size, p_native_addr) \
52
+ wasm_runtime_module_malloc(module_inst, size, p_native_addr)
53
+
54
+ #define module_free(offset) wasm_runtime_module_free(module_inst, offset)
55
+
56
+ #define native_raw_return_type(type, args) type *raw_ret = (type *)(args)
57
+
58
+ #define native_raw_get_arg(type, name, args) type name = *((type *)(args++))
59
+
60
+ #define native_raw_set_return(val) *raw_ret = (val)
61
+
62
+ #ifndef WASM_MODULE_T_DEFINED
63
+ #define WASM_MODULE_T_DEFINED
64
+ /* Uninstantiated WASM module loaded from WASM binary file
65
+ or AoT binary file*/
66
+ struct WASMModuleCommon;
67
+ typedef struct WASMModuleCommon *wasm_module_t;
68
+ #endif
69
+
70
+ typedef enum {
71
+ WASM_IMPORT_EXPORT_KIND_FUNC,
72
+ WASM_IMPORT_EXPORT_KIND_TABLE,
73
+ WASM_IMPORT_EXPORT_KIND_MEMORY,
74
+ WASM_IMPORT_EXPORT_KIND_GLOBAL
75
+ } wasm_import_export_kind_t;
76
+
77
+ struct WASMFuncType;
78
+ typedef struct WASMFuncType *wasm_func_type_t;
79
+
80
+ struct WASMTableType;
81
+ typedef struct WASMTableType *wasm_table_type_t;
82
+
83
+ struct WASMGlobalType;
84
+ typedef struct WASMGlobalType *wasm_global_type_t;
85
+
86
+ #ifndef WASM_MEMORY_T_DEFINED
87
+ #define WASM_MEMORY_T_DEFINED
88
+ struct WASMMemory;
89
+ typedef struct WASMMemory WASMMemoryType;
90
+ #endif
91
+ typedef WASMMemoryType *wasm_memory_type_t;
92
+
93
+ typedef struct wasm_import_t {
94
+ const char *module_name;
95
+ const char *name;
96
+ wasm_import_export_kind_t kind;
97
+ bool linked;
98
+ union {
99
+ wasm_func_type_t func_type;
100
+ wasm_table_type_t table_type;
101
+ wasm_global_type_t global_type;
102
+ wasm_memory_type_t memory_type;
103
+ } u;
104
+ } wasm_import_t;
105
+
106
+ typedef struct wasm_export_t {
107
+ const char *name;
108
+ wasm_import_export_kind_t kind;
109
+ union {
110
+ wasm_func_type_t func_type;
111
+ wasm_table_type_t table_type;
112
+ wasm_global_type_t global_type;
113
+ wasm_memory_type_t memory_type;
114
+ } u;
115
+ } wasm_export_t;
116
+
117
+ /* Instantiated WASM module */
118
+ struct WASMModuleInstanceCommon;
119
+ typedef struct WASMModuleInstanceCommon *wasm_module_inst_t;
120
+
121
+ /* Function instance */
122
+ typedef void WASMFunctionInstanceCommon;
123
+ typedef WASMFunctionInstanceCommon *wasm_function_inst_t;
124
+
125
+ /* Memory instance */
126
+ struct WASMMemoryInstance;
127
+ typedef struct WASMMemoryInstance *wasm_memory_inst_t;
128
+
129
+ typedef struct wasm_frame_t {
130
+ /* wasm_instance_t */
131
+ void *instance;
132
+ uint32_t module_offset;
133
+ uint32_t func_index;
134
+ uint32_t func_offset;
135
+ const char *func_name_wp;
136
+
137
+ uint32_t *sp;
138
+ uint8_t *frame_ref;
139
+ uint32_t *lp;
140
+ } WASMCApiFrame;
141
+
142
+ /* WASM section */
143
+ typedef struct wasm_section_t {
144
+ struct wasm_section_t *next;
145
+ /* section type */
146
+ int section_type;
147
+ /* section body, not include type and size */
148
+ uint8_t *section_body;
149
+ /* section body size */
150
+ uint32_t section_body_size;
151
+ } wasm_section_t, aot_section_t, *wasm_section_list_t, *aot_section_list_t;
152
+
153
+ /* Execution environment, e.g. stack info */
154
+ struct WASMExecEnv;
155
+ typedef struct WASMExecEnv *wasm_exec_env_t;
156
+
157
+ struct WASMSharedHeap;
158
+ typedef struct WASMSharedHeap *wasm_shared_heap_t;
159
+
160
+ /* Package Type */
161
+ typedef enum {
162
+ Wasm_Module_Bytecode = 0,
163
+ Wasm_Module_AoT,
164
+ Package_Type_Unknown = 0xFFFF
165
+ } package_type_t;
166
+
167
+ #ifndef MEM_ALLOC_OPTION_DEFINED
168
+ #define MEM_ALLOC_OPTION_DEFINED
169
+ /* Memory allocator type */
170
+ typedef enum {
171
+ /* pool mode, allocate memory from user defined heap buffer */
172
+ Alloc_With_Pool = 0,
173
+ /* user allocator mode, allocate memory from user defined
174
+ malloc function */
175
+ Alloc_With_Allocator,
176
+ /* system allocator mode, allocate memory from system allocator,
177
+ or, platform's os_malloc function */
178
+ Alloc_With_System_Allocator,
179
+ } mem_alloc_type_t;
180
+
181
+ typedef enum { Alloc_For_Runtime, Alloc_For_LinearMemory } mem_alloc_usage_t;
182
+
183
+ /* Memory allocator option */
184
+ typedef union MemAllocOption {
185
+ struct {
186
+ void *heap_buf;
187
+ uint32_t heap_size;
188
+ } pool;
189
+ struct {
190
+ /* the function signature is varied when
191
+ WASM_MEM_ALLOC_WITH_USER_DATA and
192
+ WASM_MEM_ALLOC_WITH_USAGE are defined */
193
+ void *malloc_func;
194
+ void *realloc_func;
195
+ void *free_func;
196
+ /* allocator user data, only used when
197
+ WASM_MEM_ALLOC_WITH_USER_DATA is defined */
198
+ void *user_data;
199
+ } allocator;
200
+ } MemAllocOption;
201
+ #endif
202
+
203
+ /* Memory pool info */
204
+ typedef struct mem_alloc_info_t {
205
+ uint32_t total_size;
206
+ uint32_t total_free_size;
207
+ uint32_t highmark_size;
208
+ } mem_alloc_info_t;
209
+
210
+ /* Running mode of runtime and module instance*/
211
+ typedef enum RunningMode {
212
+ Mode_Interp = 1,
213
+ Mode_Fast_JIT,
214
+ Mode_LLVM_JIT,
215
+ Mode_Multi_Tier_JIT,
216
+ } RunningMode;
217
+
218
+ /* WASM runtime initialize arguments */
219
+ typedef struct RuntimeInitArgs {
220
+ mem_alloc_type_t mem_alloc_type;
221
+ MemAllocOption mem_alloc_option;
222
+
223
+ const char *native_module_name;
224
+ NativeSymbol *native_symbols;
225
+ uint32_t n_native_symbols;
226
+
227
+ /* maximum thread number, only used when
228
+ WASM_ENABLE_THREAD_MGR is defined */
229
+ uint32_t max_thread_num;
230
+
231
+ /* Debug settings, only used when
232
+ WASM_ENABLE_DEBUG_INTERP != 0 */
233
+ char ip_addr[128];
234
+ int unused; /* was platform_port */
235
+ int instance_port;
236
+
237
+ /* Fast JIT code cache size */
238
+ uint32_t fast_jit_code_cache_size;
239
+
240
+ /* Default GC heap size */
241
+ uint32_t gc_heap_size;
242
+
243
+ /* Default running mode of the runtime */
244
+ RunningMode running_mode;
245
+
246
+ /* LLVM JIT opt and size level */
247
+ uint32_t llvm_jit_opt_level;
248
+ uint32_t llvm_jit_size_level;
249
+ /* Segue optimization flags for LLVM JIT */
250
+ uint32_t segue_flags;
251
+ /**
252
+ * If enabled
253
+ * - llvm-jit will output a jitdump file for `perf inject`
254
+ * - aot will output a perf-${pid}.map for `perf record`
255
+ * - fast-jit. TBD
256
+ * - multi-tier-jit. TBD
257
+ * - interpreter. TBD
258
+ */
259
+ bool enable_linux_perf;
260
+ } RuntimeInitArgs;
261
+
262
+ #ifndef LOAD_ARGS_OPTION_DEFINED
263
+ #define LOAD_ARGS_OPTION_DEFINED
264
+ typedef struct LoadArgs {
265
+ char *name;
266
+ /* This option is only used by the Wasm C API (see wasm_c_api.h) */
267
+ bool clone_wasm_binary;
268
+ /* False by default, used by AOT/wasm loader only.
269
+ If true, the AOT/wasm loader creates a copy of some module fields (e.g.
270
+ const strings), making it possible to free the wasm binary buffer after
271
+ loading. */
272
+ bool wasm_binary_freeable;
273
+
274
+ /* false by default, if true, don't resolve the symbols yet. The
275
+ wasm_runtime_load_ex has to be followed by a wasm_runtime_resolve_symbols
276
+ call */
277
+ bool no_resolve;
278
+ /* TODO: more fields? */
279
+ } LoadArgs;
280
+ #endif /* LOAD_ARGS_OPTION_DEFINED */
281
+
282
+ #ifndef INSTANTIATION_ARGS_OPTION_DEFINED
283
+ #define INSTANTIATION_ARGS_OPTION_DEFINED
284
+ /* WASM module instantiation arguments */
285
+ typedef struct InstantiationArgs {
286
+ uint32_t default_stack_size;
287
+ uint32_t host_managed_heap_size;
288
+ uint32_t max_memory_pages;
289
+ } InstantiationArgs;
290
+ #endif /* INSTANTIATION_ARGS_OPTION_DEFINED */
291
+
292
+ struct InstantiationArgs2;
293
+
294
+ #ifndef WASM_VALKIND_T_DEFINED
295
+ #define WASM_VALKIND_T_DEFINED
296
+ typedef uint8_t wasm_valkind_t;
297
+ enum wasm_valkind_enum {
298
+ WASM_I32,
299
+ WASM_I64,
300
+ WASM_F32,
301
+ WASM_F64,
302
+ WASM_V128,
303
+ WASM_EXTERNREF = 128,
304
+ WASM_FUNCREF,
305
+ };
306
+ #endif
307
+
308
+ #ifndef WASM_VAL_T_DEFINED
309
+ #define WASM_VAL_T_DEFINED
310
+ struct wasm_ref_t;
311
+
312
+ typedef struct wasm_val_t {
313
+ wasm_valkind_t kind;
314
+ uint8_t _paddings[7];
315
+ union {
316
+ /* also represent a function index */
317
+ int32_t i32;
318
+ int64_t i64;
319
+ float f32;
320
+ double f64;
321
+ /* represent a foreign object, aka externref in .wat */
322
+ uintptr_t foreign;
323
+ struct wasm_ref_t *ref;
324
+ } of;
325
+ } wasm_val_t;
326
+ #endif
327
+
328
+ /* Global instance*/
329
+ typedef struct wasm_global_inst_t {
330
+ wasm_valkind_t kind;
331
+ bool is_mutable;
332
+ void *global_data;
333
+ } wasm_global_inst_t;
334
+
335
+ /* Table instance*/
336
+ typedef struct wasm_table_inst_t {
337
+ wasm_valkind_t elem_kind;
338
+ uint32_t cur_size;
339
+ uint32_t max_size;
340
+ /* represents the elements of the table, for internal use only */
341
+ void *elems;
342
+ } wasm_table_inst_t;
343
+
344
+ typedef enum {
345
+ WASM_LOG_LEVEL_FATAL = 0,
346
+ WASM_LOG_LEVEL_ERROR = 1,
347
+ WASM_LOG_LEVEL_WARNING = 2,
348
+ WASM_LOG_LEVEL_DEBUG = 3,
349
+ WASM_LOG_LEVEL_VERBOSE = 4
350
+ } log_level_t;
351
+
352
+ typedef struct SharedHeapInitArgs {
353
+ uint32_t size;
354
+ void *pre_allocated_addr;
355
+ } SharedHeapInitArgs;
356
+
357
+ /**
358
+ * Initialize the WASM runtime environment, and also initialize
359
+ * the memory allocator with system allocator, which calls os_malloc
360
+ * to allocate memory
361
+ *
362
+ * @return true if success, false otherwise
363
+ */
364
+ WASM_RUNTIME_API_EXTERN bool
365
+ wasm_runtime_init(void);
366
+
367
+ /**
368
+ * Initialize the WASM runtime environment, WASM running mode,
369
+ * and also initialize the memory allocator and register native symbols,
370
+ * which are specified with init arguments
371
+ *
372
+ * @param init_args specifies the init arguments
373
+ *
374
+ * @return return true if success, false otherwise
375
+ */
376
+ WASM_RUNTIME_API_EXTERN bool
377
+ wasm_runtime_full_init(RuntimeInitArgs *init_args);
378
+
379
+ /**
380
+ * Set the log level. To be called after the runtime is initialized.
381
+ *
382
+ * @param level the log level to set
383
+ */
384
+ WASM_RUNTIME_API_EXTERN void
385
+ wasm_runtime_set_log_level(log_level_t level);
386
+
387
+ /**
388
+ * Query whether a certain running mode is supported for the runtime
389
+ *
390
+ * @param running_mode the running mode to query
391
+ *
392
+ * @return true if this running mode is supported, false otherwise
393
+ */
394
+ WASM_RUNTIME_API_EXTERN bool
395
+ wasm_runtime_is_running_mode_supported(RunningMode running_mode);
396
+
397
+ /**
398
+ * Set the default running mode for the runtime. It is inherited
399
+ * to set the running mode of a module instance when it is instantiated,
400
+ * and can be changed by calling wasm_runtime_set_running_mode
401
+ *
402
+ * @param running_mode the running mode to set
403
+ *
404
+ * @return true if success, false otherwise
405
+ */
406
+ WASM_RUNTIME_API_EXTERN bool
407
+ wasm_runtime_set_default_running_mode(RunningMode running_mode);
408
+
409
+ /**
410
+ * Destroy the WASM runtime environment.
411
+ */
412
+ WASM_RUNTIME_API_EXTERN void
413
+ wasm_runtime_destroy(void);
414
+
415
+ /**
416
+ * Allocate memory from runtime memory environment.
417
+ *
418
+ * @param size bytes need to allocate
419
+ *
420
+ * @return the pointer to memory allocated
421
+ */
422
+ WASM_RUNTIME_API_EXTERN void *
423
+ wasm_runtime_malloc(unsigned int size);
424
+
425
+ /**
426
+ * Reallocate memory from runtime memory environment
427
+ *
428
+ * @param ptr the original memory
429
+ * @param size bytes need to reallocate
430
+ *
431
+ * @return the pointer to memory reallocated
432
+ */
433
+ WASM_RUNTIME_API_EXTERN void *
434
+ wasm_runtime_realloc(void *ptr, unsigned int size);
435
+
436
+ /*
437
+ * Free memory to runtime memory environment.
438
+ */
439
+ WASM_RUNTIME_API_EXTERN void
440
+ wasm_runtime_free(void *ptr);
441
+
442
+ /*
443
+ * Get memory info, only pool mode is supported now.
444
+ */
445
+ WASM_RUNTIME_API_EXTERN bool
446
+ wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info);
447
+
448
+ /**
449
+ * Get the package type of a buffer.
450
+ *
451
+ * @param buf the package buffer
452
+ * @param size the package buffer size
453
+ *
454
+ * @return the package type, return Package_Type_Unknown if the type is unknown
455
+ */
456
+ WASM_RUNTIME_API_EXTERN package_type_t
457
+ get_package_type(const uint8_t *buf, uint32_t size);
458
+
459
+ /**
460
+ * Get the package type of a buffer (same as get_package_type).
461
+ *
462
+ * @param buf the package buffer
463
+ * @param size the package buffer size
464
+ *
465
+ * @return the package type, return Package_Type_Unknown if the type is unknown
466
+ */
467
+ WASM_RUNTIME_API_EXTERN package_type_t
468
+ wasm_runtime_get_file_package_type(const uint8_t *buf, uint32_t size);
469
+
470
+ /**
471
+ * Get the package type of a module.
472
+ *
473
+ * @param module the module
474
+ *
475
+ * @return the package type, return Package_Type_Unknown if the type is
476
+ * unknown
477
+ */
478
+ WASM_RUNTIME_API_EXTERN package_type_t
479
+ wasm_runtime_get_module_package_type(const wasm_module_t module);
480
+
481
+ /**
482
+ * Get the package version of a buffer.
483
+ *
484
+ * @param buf the package buffer
485
+ * @param size the package buffer size
486
+ *
487
+ * @return the package version, return zero if the version is unknown
488
+ */
489
+ WASM_RUNTIME_API_EXTERN uint32_t
490
+ wasm_runtime_get_file_package_version(const uint8_t *buf, uint32_t size);
491
+
492
+ /**
493
+ * Get the package version of a module
494
+ *
495
+ * @param module the module
496
+ *
497
+ * @return the package version, or zero if version is unknown
498
+ */
499
+ WASM_RUNTIME_API_EXTERN uint32_t
500
+ wasm_runtime_get_module_package_version(const wasm_module_t module);
501
+
502
+ /**
503
+ * Get the currently supported version of the package type
504
+ *
505
+ * @param package_type the package type
506
+ *
507
+ * @return the currently supported version, or zero if package type is unknown
508
+ */
509
+ WASM_RUNTIME_API_EXTERN uint32_t
510
+ wasm_runtime_get_current_package_version(package_type_t package_type);
511
+
512
+ /**
513
+ * Check whether a file is an AOT XIP (Execution In Place) file
514
+ *
515
+ * @param buf the package buffer
516
+ * @param size the package buffer size
517
+ *
518
+ * @return true if success, false otherwise
519
+ */
520
+ WASM_RUNTIME_API_EXTERN bool
521
+ wasm_runtime_is_xip_file(const uint8_t *buf, uint32_t size);
522
+
523
+ /**
524
+ * Callback to load a module file into a buffer in multi-module feature
525
+ */
526
+ typedef bool (*module_reader)(package_type_t module_type,
527
+ const char *module_name, uint8_t **p_buffer,
528
+ uint32_t *p_size);
529
+
530
+ /**
531
+ * Callback to release the buffer loaded by module_reader callback
532
+ */
533
+ typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size);
534
+
535
+ /**
536
+ * Setup callbacks for reading and releasing a buffer about a module file
537
+ *
538
+ * @param reader a callback to read a module file into a buffer
539
+ * @param destroyer a callback to release above buffer
540
+ */
541
+ WASM_RUNTIME_API_EXTERN void
542
+ wasm_runtime_set_module_reader(const module_reader reader,
543
+ const module_destroyer destroyer);
544
+ /**
545
+ * Give the "module" a name "module_name".
546
+ * Can not assign a new name to a module if it already has a name
547
+ *
548
+ * @param module_name indicate a name
549
+ * @param module the target module
550
+ * @param error_buf output of the exception info
551
+ * @param error_buf_size the size of the exception string
552
+ *
553
+ * @return true means success, false means failed
554
+ */
555
+ WASM_RUNTIME_API_EXTERN bool
556
+ wasm_runtime_register_module(const char *module_name, wasm_module_t module,
557
+ char *error_buf, uint32_t error_buf_size);
558
+
559
+ /**
560
+ * Check if there is already a loaded module named module_name in the
561
+ * runtime. Repeatedly loading a module with the same name is not allowed.
562
+ *
563
+ * @param module_name indicate a name
564
+ *
565
+ * @return return WASM module loaded, NULL if failed
566
+ */
567
+ WASM_RUNTIME_API_EXTERN wasm_module_t
568
+ wasm_runtime_find_module_registered(const char *module_name);
569
+
570
+ /**
571
+ * Load a WASM module from a specified byte buffer. The byte buffer can be
572
+ * WASM binary data when interpreter or JIT is enabled, or AOT binary data
573
+ * when AOT is enabled. If it is AOT binary data, it must be 4-byte aligned.
574
+ *
575
+ * Note: In case of AOT XIP modules, the runtime doesn't make modifications
576
+ * to the buffer. (Except the "Known issues" mentioned in doc/xip.md.)
577
+ * Otherwise, the runtime can make modifications to the buffer for its
578
+ * internal purposes. Thus, in general, it isn't safe to create multiple
579
+ * modules from a single buffer.
580
+ *
581
+ * @param buf the byte buffer which contains the WASM/AOT binary data,
582
+ * note that the byte buffer must be writable since runtime may
583
+ * change its content for footprint and performance purpose, and
584
+ * it must be referenceable until wasm_runtime_unload is called
585
+ * @param size the size of the buffer
586
+ * @param error_buf output of the exception info
587
+ * @param error_buf_size the size of the exception string
588
+ *
589
+ * @return return WASM module loaded, NULL if failed
590
+ */
591
+ WASM_RUNTIME_API_EXTERN wasm_module_t
592
+ wasm_runtime_load(uint8_t *buf, uint32_t size, char *error_buf,
593
+ uint32_t error_buf_size);
594
+
595
+ /**
596
+ * Load a WASM module with specified load argument.
597
+ */
598
+ WASM_RUNTIME_API_EXTERN wasm_module_t
599
+ wasm_runtime_load_ex(uint8_t *buf, uint32_t size, const LoadArgs *args,
600
+ char *error_buf, uint32_t error_buf_size);
601
+
602
+ /**
603
+ * Resolve symbols for a previously loaded WASM module. Only useful when the
604
+ * module was loaded with LoadArgs::no_resolve set to true
605
+ */
606
+ WASM_RUNTIME_API_EXTERN bool
607
+ wasm_runtime_resolve_symbols(wasm_module_t module);
608
+ /**
609
+ * Load a WASM module from a specified WASM or AOT section list.
610
+ *
611
+ * @param section_list the section list which contains each section data
612
+ * @param is_aot whether the section list is AOT section list
613
+ * @param error_buf output of the exception info
614
+ * @param error_buf_size the size of the exception string
615
+ *
616
+ * @return return WASM module loaded, NULL if failed
617
+ */
618
+ WASM_RUNTIME_API_EXTERN wasm_module_t
619
+ wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
620
+ char *error_buf, uint32_t error_buf_size);
621
+
622
+ /**
623
+ * Unload a WASM module.
624
+ *
625
+ * @param module the module to be unloaded
626
+ */
627
+ WASM_RUNTIME_API_EXTERN void
628
+ wasm_runtime_unload(wasm_module_t module);
629
+
630
+ /**
631
+ * Get the module hash of a WASM module, currently only available on
632
+ * linux-sgx platform when the remote attestation feature is enabled
633
+ *
634
+ * @param module the WASM module to retrieve
635
+ *
636
+ * @return the module hash of the WASM module
637
+ */
638
+ char *
639
+ wasm_runtime_get_module_hash(wasm_module_t module);
640
+
641
+ /**
642
+ * Set WASI parameters.
643
+ *
644
+ * While this API operates on a module, these parameters will be used
645
+ * only when the module is instantiated. That is, you can consider these
646
+ * as extra parameters for wasm_runtime_instantiate().
647
+ *
648
+ * @param module The module to set WASI parameters.
649
+ * @param dir_list The list of directories to preopen. (real path)
650
+ * @param dir_count The number of elements in dir_list.
651
+ * @param map_dir_list The list of directories to preopen. (mapped path)
652
+ * Format for each map entry: <guest-path>::<host-path>
653
+ * @param map_dir_count The number of elements in map_dir_list.
654
+ * If map_dir_count is smaller than dir_count,
655
+ * mapped path is assumed to be same as the
656
+ * corresponding real path for the rest of entries.
657
+ * @param env The list of environment variables.
658
+ * @param env_count The number of elements in env.
659
+ * @param argv The list of command line arguments.
660
+ * @param argc The number of elements in argv.
661
+ * @param stdin_handle The raw host handle to back WASI STDIN_FILENO.
662
+ * If an invalid handle is specified (e.g. -1 on POSIX,
663
+ * INVALID_HANDLE_VALUE on Windows), the platform default
664
+ * for STDIN is used.
665
+ * @param stdoutfd The raw host handle to back WASI STDOUT_FILENO.
666
+ * If an invalid handle is specified (e.g. -1 on POSIX,
667
+ * INVALID_HANDLE_VALUE on Windows), the platform default
668
+ * for STDOUT is used.
669
+ * @param stderrfd The raw host handle to back WASI STDERR_FILENO.
670
+ * If an invalid handle is specified (e.g. -1 on POSIX,
671
+ * INVALID_HANDLE_VALUE on Windows), the platform default
672
+ * for STDERR is used.
673
+ */
674
+ WASM_RUNTIME_API_EXTERN void
675
+ wasm_runtime_set_wasi_args_ex(wasm_module_t module, const char *dir_list[],
676
+ uint32_t dir_count, const char *map_dir_list[],
677
+ uint32_t map_dir_count, const char *env[],
678
+ uint32_t env_count, char *argv[], int argc,
679
+ int64_t stdinfd, int64_t stdoutfd,
680
+ int64_t stderrfd);
681
+
682
+ /**
683
+ * Set WASI parameters.
684
+ *
685
+ * Same as wasm_runtime_set_wasi_args_ex but with default stdio handles
686
+ */
687
+ WASM_RUNTIME_API_EXTERN void
688
+ wasm_runtime_set_wasi_args(wasm_module_t module, const char *dir_list[],
689
+ uint32_t dir_count, const char *map_dir_list[],
690
+ uint32_t map_dir_count, const char *env[],
691
+ uint32_t env_count, char *argv[], int argc);
692
+
693
+ WASM_RUNTIME_API_EXTERN void
694
+ wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
695
+ uint32_t addr_pool_size);
696
+
697
+ WASM_RUNTIME_API_EXTERN void
698
+ wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
699
+ const char *ns_lookup_pool[],
700
+ uint32_t ns_lookup_pool_size);
701
+
702
+ /**
703
+ * Instantiate a WASM module.
704
+ *
705
+ * @param module the WASM module to instantiate
706
+ * @param default_stack_size the default stack size of the module instance when
707
+ * the exec env's operation stack isn't created by user, e.g. API
708
+ * wasm_application_execute_main() and wasm_application_execute_func()
709
+ * create the operation stack internally with the stack size specified
710
+ * here. And API wasm_runtime_create_exec_env() creates the operation
711
+ * stack with stack size specified by its parameter, the stack size
712
+ * specified here is ignored.
713
+ * @param host_managed_heap_size the default heap size of the module instance,
714
+ * a heap will be created besides the app memory space. Both wasm app
715
+ * and native function can allocate memory from the heap.
716
+ * @param error_buf buffer to output the error info if failed
717
+ * @param error_buf_size the size of the error buffer
718
+ *
719
+ * @return return the instantiated WASM module instance, NULL if failed
720
+ */
721
+ WASM_RUNTIME_API_EXTERN wasm_module_inst_t
722
+ wasm_runtime_instantiate(const wasm_module_t module,
723
+ uint32_t default_stack_size,
724
+ uint32_t host_managed_heap_size, char *error_buf,
725
+ uint32_t error_buf_size);
726
+
727
+ /**
728
+ * Instantiate a WASM module, with specified instantiation arguments
729
+ *
730
+ * Same as wasm_runtime_instantiate, but it also allows overwriting maximum
731
+ * memory
732
+ */
733
+ WASM_RUNTIME_API_EXTERN wasm_module_inst_t
734
+ wasm_runtime_instantiate_ex(const wasm_module_t module,
735
+ const InstantiationArgs *args, char *error_buf,
736
+ uint32_t error_buf_size);
737
+
738
+ /**
739
+ * Create an InstantiationArgs2 object with default parameters.
740
+ *
741
+ * @return true if success, false otherwise
742
+ */
743
+ WASM_RUNTIME_API_EXTERN bool
744
+ wasm_runtime_instantiation_args_create(struct InstantiationArgs2 **p);
745
+
746
+ /**
747
+ * Dispose an InstantiationArgs2 object.
748
+ */
749
+ WASM_RUNTIME_API_EXTERN void
750
+ wasm_runtime_instantiation_args_destroy(struct InstantiationArgs2 *p);
751
+
752
+ /**
753
+ * Setter functions for the InstantiationArgs2 object.
754
+ */
755
+ WASM_RUNTIME_API_EXTERN void
756
+ wasm_runtime_instantiation_args_set_default_stack_size(
757
+ struct InstantiationArgs2 *p, uint32_t v);
758
+
759
+ WASM_RUNTIME_API_EXTERN void
760
+ wasm_runtime_instantiation_args_set_host_managed_heap_size(
761
+ struct InstantiationArgs2 *p, uint32_t v);
762
+
763
+ WASM_RUNTIME_API_EXTERN void
764
+ wasm_runtime_instantiation_args_set_max_memory_pages(
765
+ struct InstantiationArgs2 *p, uint32_t v);
766
+
767
+ /**
768
+ * Instantiate a WASM module, with specified instantiation arguments
769
+ *
770
+ * Same as wasm_runtime_instantiate_ex, but this version takes
771
+ * InstantiationArgs2, which can be extended without breaking the ABI.
772
+ */
773
+ WASM_RUNTIME_API_EXTERN wasm_module_inst_t
774
+ wasm_runtime_instantiate_ex2(const wasm_module_t module,
775
+ const struct InstantiationArgs2 *args,
776
+ char *error_buf, uint32_t error_buf_size);
777
+
778
+ /**
779
+ * Set the running mode of a WASM module instance, override the
780
+ * default running mode of the runtime. Note that it only makes sense when
781
+ * the input is a wasm bytecode file: for the AOT file, runtime always runs
782
+ * it with AOT engine, and this function always returns true.
783
+ *
784
+ * @param module_inst the WASM module instance to set running mode
785
+ * @param running_mode the running mode to set
786
+ *
787
+ * @return true if success, false otherwise
788
+ */
789
+ WASM_RUNTIME_API_EXTERN bool
790
+ wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
791
+ RunningMode running_mode);
792
+
793
+ /**
794
+ * Get the running mode of a WASM module instance, if no running mode
795
+ * is explicitly set the default running mode of runtime will
796
+ * be used and returned. Note that it only makes sense when the input is a
797
+ * wasm bytecode file: for the AOT file, this function always returns 0.
798
+ *
799
+ * @param module_inst the WASM module instance to query for running mode
800
+ *
801
+ * @return the running mode this module instance currently use
802
+ */
803
+ WASM_RUNTIME_API_EXTERN RunningMode
804
+ wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
805
+
806
+ /**
807
+ * Deinstantiate a WASM module instance, destroy the resources.
808
+ *
809
+ * @param module_inst the WASM module instance to destroy
810
+ */
811
+ WASM_RUNTIME_API_EXTERN void
812
+ wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
813
+
814
+ /**
815
+ * Get WASM module from WASM module instance
816
+ *
817
+ * @param module_inst the WASM module instance to retrieve
818
+ *
819
+ * @return the WASM module
820
+ */
821
+ WASM_RUNTIME_API_EXTERN wasm_module_t
822
+ wasm_runtime_get_module(wasm_module_inst_t module_inst);
823
+
824
+ WASM_RUNTIME_API_EXTERN bool
825
+ wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
826
+
827
+ WASM_RUNTIME_API_EXTERN wasm_function_inst_t
828
+ wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
829
+
830
+ /**
831
+ * Get WASI exit code.
832
+ *
833
+ * After a WASI command completed its execution, an embedder can
834
+ * call this function to get its exit code. (that is, the value given
835
+ * to proc_exit.)
836
+ *
837
+ * @param module_inst the module instance
838
+ */
839
+ WASM_RUNTIME_API_EXTERN uint32_t
840
+ wasm_runtime_get_wasi_exit_code(wasm_module_inst_t module_inst);
841
+
842
+ /**
843
+ * Lookup an exported function in the WASM module instance.
844
+ *
845
+ * @param module_inst the module instance
846
+ * @param name the name of the function
847
+ *
848
+ * @return the function instance found, NULL if not found
849
+ */
850
+ WASM_RUNTIME_API_EXTERN wasm_function_inst_t
851
+ wasm_runtime_lookup_function(const wasm_module_inst_t module_inst,
852
+ const char *name);
853
+
854
+ /**
855
+ * Get parameter count of the function instance
856
+ *
857
+ * @param func_inst the function instance
858
+ * @param module_inst the module instance the function instance belongs to
859
+ *
860
+ * @return the parameter count of the function instance
861
+ */
862
+ WASM_RUNTIME_API_EXTERN uint32_t
863
+ wasm_func_get_param_count(const wasm_function_inst_t func_inst,
864
+ const wasm_module_inst_t module_inst);
865
+
866
+ /**
867
+ * Get result count of the function instance
868
+ *
869
+ * @param func_inst the function instance
870
+ * @param module_inst the module instance the function instance belongs to
871
+ *
872
+ * @return the result count of the function instance
873
+ */
874
+ WASM_RUNTIME_API_EXTERN uint32_t
875
+ wasm_func_get_result_count(const wasm_function_inst_t func_inst,
876
+ const wasm_module_inst_t module_inst);
877
+
878
+ /**
879
+ * Get parameter types of the function instance
880
+ *
881
+ * @param func_inst the function instance
882
+ * @param module_inst the module instance the function instance belongs to
883
+ * @param param_types the parameter types returned
884
+ */
885
+ WASM_RUNTIME_API_EXTERN void
886
+ wasm_func_get_param_types(const wasm_function_inst_t func_inst,
887
+ const wasm_module_inst_t module_inst,
888
+ wasm_valkind_t *param_types);
889
+
890
+ /**
891
+ * Get result types of the function instance
892
+ *
893
+ * @param func_inst the function instance
894
+ * @param module_inst the module instance the function instance belongs to
895
+ * @param result_types the result types returned
896
+ */
897
+ WASM_RUNTIME_API_EXTERN void
898
+ wasm_func_get_result_types(const wasm_function_inst_t func_inst,
899
+ const wasm_module_inst_t module_inst,
900
+ wasm_valkind_t *result_types);
901
+
902
+ /**
903
+ * Create execution environment for a WASM module instance.
904
+ *
905
+ * @param module_inst the module instance
906
+ * @param stack_size the stack size to execute a WASM function
907
+ *
908
+ * @return the execution environment, NULL if failed, e.g. invalid
909
+ * stack size is passed
910
+ */
911
+ WASM_RUNTIME_API_EXTERN wasm_exec_env_t
912
+ wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
913
+ uint32_t stack_size);
914
+
915
+ /**
916
+ * Destroy the execution environment.
917
+ *
918
+ * @param exec_env the execution environment to destroy
919
+ */
920
+ WASM_RUNTIME_API_EXTERN void
921
+ wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
922
+
923
+ /**
924
+ * @brief Copy callstack frames.
925
+ *
926
+ * Caution: This is not a thread-safe function. Ensure the exec_env
927
+ * is suspended before calling it from another thread.
928
+ *
929
+ * Usage: In the callback to read frames fields use APIs
930
+ * for wasm_frame_t from wasm_c_api.h
931
+ *
932
+ * Note: The function is async-signal-safe if called with verified arguments.
933
+ * Meaning it's safe to call it from a signal handler even on a signal
934
+ * interruption from another thread if next variables hold valid pointers
935
+ * - exec_env
936
+ * - exec_env->module_inst
937
+ * - exec_env->module_inst->module
938
+ *
939
+ * @param exec_env the execution environment that containes frames
940
+ * @param buffer the buffer of size equal length * sizeof(wasm_frame_t) to copy
941
+ * frames to
942
+ * @param length the number of frames to copy
943
+ * @param skip_n the number of frames to skip from the top of the stack
944
+ *
945
+ * @return number of copied frames
946
+ */
947
+ WASM_RUNTIME_API_EXTERN uint32_t
948
+ wasm_copy_callstack(const wasm_exec_env_t exec_env, WASMCApiFrame *buffer,
949
+ const uint32_t length, const uint32_t skip_n,
950
+ char *error_buf, uint32_t error_buf_size);
951
+
952
+ /**
953
+ * Get the singleton execution environment for the instance.
954
+ *
955
+ * Note: The singleton execution environment is the execution
956
+ * environment used internally by the runtime for the API functions
957
+ * like wasm_application_execute_main, which don't take explicit
958
+ * execution environment. It's associated to the corresponding
959
+ * module instance and managed by the runtime. The API user should
960
+ * not destroy it with wasm_runtime_destroy_exec_env.
961
+ *
962
+ * @param module_inst the module instance
963
+ *
964
+ * @return exec_env the execution environment to destroy
965
+ */
966
+ WASM_RUNTIME_API_EXTERN wasm_exec_env_t
967
+ wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
968
+
969
+ /**
970
+ * Start debug instance based on given execution environment.
971
+ * Note:
972
+ * The debug instance will be destroyed during destroying the
973
+ * execution environment, developers don't need to destroy it
974
+ * manually.
975
+ * If the cluster of this execution environment has already
976
+ * been bound to a debug instance, this function will return true
977
+ * directly.
978
+ * If developer spawns some exec_env by wasm_runtime_spawn_exec_env,
979
+ * don't need to call this function for every spawned exec_env as
980
+ * they are sharing the same cluster with the main exec_env.
981
+ *
982
+ * @param exec_env the execution environment to start debug instance
983
+ * @param port the port for the debug server to listen on.
984
+ * 0 means automatic assignment.
985
+ * -1 means to use the global setting in RuntimeInitArgs.
986
+ *
987
+ * @return debug port if success, 0 otherwise.
988
+ */
989
+ WASM_RUNTIME_API_EXTERN uint32_t
990
+ wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env,
991
+ int32_t port);
992
+
993
+ /**
994
+ * Same as wasm_runtime_start_debug_instance_with_port(env, -1).
995
+ */
996
+ WASM_RUNTIME_API_EXTERN uint32_t
997
+ wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
998
+
999
+ /**
1000
+ * Initialize the thread environment.
1001
+ * Note:
1002
+ * If developer creates a child thread by himself to call the
1003
+ * the wasm function in that thread, he should call this API
1004
+ * firstly before calling the wasm function and then call
1005
+ * wasm_runtime_destroy_thread_env() after calling the wasm
1006
+ * function. If the thread is created from the runtime API,
1007
+ * it is unnecessary to call these two APIs.
1008
+ *
1009
+ * @return true if success, false otherwise
1010
+ */
1011
+ WASM_RUNTIME_API_EXTERN bool
1012
+ wasm_runtime_init_thread_env(void);
1013
+
1014
+ /**
1015
+ * Destroy the thread environment
1016
+ */
1017
+ WASM_RUNTIME_API_EXTERN void
1018
+ wasm_runtime_destroy_thread_env(void);
1019
+
1020
+ /**
1021
+ * Whether the thread environment is initialized
1022
+ */
1023
+ WASM_RUNTIME_API_EXTERN bool
1024
+ wasm_runtime_thread_env_inited(void);
1025
+
1026
+ /**
1027
+ * Get WASM module instance from execution environment
1028
+ *
1029
+ * @param exec_env the execution environment to retrieve
1030
+ *
1031
+ * @return the WASM module instance
1032
+ */
1033
+ WASM_RUNTIME_API_EXTERN wasm_module_inst_t
1034
+ wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
1035
+
1036
+ /**
1037
+ * Set WASM module instance of execution environment
1038
+ * Caution:
1039
+ * normally the module instance is bound with the execution
1040
+ * environment one by one, if multiple module instances want
1041
+ * to share to the same execution environment, developer should
1042
+ * be responsible for the backup and restore of module instance
1043
+ *
1044
+ * @param exec_env the execution environment
1045
+ * @param module_inst the WASM module instance to set
1046
+ */
1047
+ WASM_RUNTIME_API_EXTERN void
1048
+ wasm_runtime_set_module_inst(wasm_exec_env_t exec_env,
1049
+ const wasm_module_inst_t module_inst);
1050
+
1051
+ /**
1052
+ * @brief Lookup a memory instance by name
1053
+ *
1054
+ * @param module_inst The module instance
1055
+ * @param name The name of the memory instance
1056
+ *
1057
+ * @return The memory instance if found, NULL otherwise
1058
+ */
1059
+ WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
1060
+ wasm_runtime_lookup_memory(const wasm_module_inst_t module_inst,
1061
+ const char *name);
1062
+
1063
+ /**
1064
+ * @brief Get the default memory instance
1065
+ *
1066
+ * @param module_inst The module instance
1067
+ *
1068
+ * @return The memory instance if found, NULL otherwise
1069
+ */
1070
+ WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
1071
+ wasm_runtime_get_default_memory(const wasm_module_inst_t module_inst);
1072
+
1073
+ /**
1074
+ * @brief Get a memory instance by index
1075
+ *
1076
+ * @param module_inst The module instance
1077
+ * @param index The index of the memory instance
1078
+ *
1079
+ * @return The memory instance if found, NULL otherwise
1080
+ */
1081
+ WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
1082
+ wasm_runtime_get_memory(const wasm_module_inst_t module_inst, uint32_t index);
1083
+
1084
+ /**
1085
+ * @brief Get the current number of pages for a memory instance
1086
+ *
1087
+ * @param memory_inst The memory instance
1088
+ *
1089
+ * @return The current number of pages
1090
+ */
1091
+ WASM_RUNTIME_API_EXTERN uint64_t
1092
+ wasm_memory_get_cur_page_count(const wasm_memory_inst_t memory_inst);
1093
+
1094
+ /**
1095
+ * @brief Get the maximum number of pages for a memory instance
1096
+ *
1097
+ * @param memory_inst The memory instance
1098
+ *
1099
+ * @return The maximum number of pages
1100
+ */
1101
+ WASM_RUNTIME_API_EXTERN uint64_t
1102
+ wasm_memory_get_max_page_count(const wasm_memory_inst_t memory_inst);
1103
+
1104
+ /**
1105
+ * @brief Get the number of bytes per page for a memory instance
1106
+ *
1107
+ * @param memory_inst The memory instance
1108
+ *
1109
+ * @return The number of bytes per page
1110
+ */
1111
+ WASM_RUNTIME_API_EXTERN uint64_t
1112
+ wasm_memory_get_bytes_per_page(const wasm_memory_inst_t memory_inst);
1113
+
1114
+ /**
1115
+ * @brief Get the shared status for a memory instance
1116
+ *
1117
+ * @param memory_inst The memory instance
1118
+ *
1119
+ * @return True if shared, false otherwise
1120
+ */
1121
+ WASM_RUNTIME_API_EXTERN bool
1122
+ wasm_memory_get_shared(const wasm_memory_inst_t memory_inst);
1123
+
1124
+ /**
1125
+ * @brief Get the base address for a memory instance
1126
+ *
1127
+ * @param memory_inst The memory instance
1128
+ *
1129
+ * @return The base address on success, false otherwise
1130
+ */
1131
+ WASM_RUNTIME_API_EXTERN void *
1132
+ wasm_memory_get_base_address(const wasm_memory_inst_t memory_inst);
1133
+
1134
+ /**
1135
+ * @brief Enlarge a memory instance by a number of pages
1136
+ *
1137
+ * @param memory_inst The memory instance
1138
+ * @param inc_page_count The number of pages to add
1139
+ *
1140
+ * @return True if successful, false otherwise
1141
+ */
1142
+ WASM_RUNTIME_API_EXTERN bool
1143
+ wasm_memory_enlarge(wasm_memory_inst_t memory_inst, uint64_t inc_page_count);
1144
+
1145
+ /**
1146
+ * Call the given WASM function of a WASM module instance with
1147
+ * arguments (bytecode and AoT).
1148
+ *
1149
+ * @param exec_env the execution environment to call the function,
1150
+ * which must be created from wasm_create_exec_env()
1151
+ * @param function the function to call
1152
+ * @param argc total cell number that the function parameters occupy,
1153
+ * a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument
1154
+ * occupies one cell, i64/f64 argument occupies two cells, note that
1155
+ * it might be different from the parameter number of the function
1156
+ * @param argv the arguments. If the function has return value,
1157
+ * the first (or first two in case 64-bit return value) element of
1158
+ * argv stores the return value of the called WASM function after this
1159
+ * function returns.
1160
+ *
1161
+ * @return true if success, false otherwise and exception will be thrown,
1162
+ * the caller can call wasm_runtime_get_exception to get the exception
1163
+ * info.
1164
+ */
1165
+ WASM_RUNTIME_API_EXTERN bool
1166
+ wasm_runtime_call_wasm(wasm_exec_env_t exec_env, wasm_function_inst_t function,
1167
+ uint32_t argc, uint32_t argv[]);
1168
+
1169
+ /**
1170
+ * Call the given WASM function of a WASM module instance with
1171
+ * provided results space and arguments (bytecode and AoT).
1172
+ *
1173
+ * @param exec_env the execution environment to call the function,
1174
+ * which must be created from wasm_create_exec_env()
1175
+ * @param function the function to call
1176
+ * @param num_results the number of results
1177
+ * @param results the pre-alloced pointer to get the results
1178
+ * @param num_args the number of arguments
1179
+ * @param args the arguments
1180
+ *
1181
+ * @return true if success, false otherwise and exception will be thrown,
1182
+ * the caller can call wasm_runtime_get_exception to get the exception
1183
+ * info.
1184
+ */
1185
+ WASM_RUNTIME_API_EXTERN bool
1186
+ wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
1187
+ wasm_function_inst_t function, uint32_t num_results,
1188
+ wasm_val_t results[], uint32_t num_args,
1189
+ wasm_val_t *args);
1190
+
1191
+ /**
1192
+ * Call the given WASM function of a WASM module instance with
1193
+ * provided results space and variant arguments (bytecode and AoT).
1194
+ *
1195
+ * @param exec_env the execution environment to call the function,
1196
+ * which must be created from wasm_create_exec_env()
1197
+ * @param function the function to call
1198
+ * @param num_results the number of results
1199
+ * @param results the pre-alloced pointer to get the results
1200
+ * @param num_args the number of arguments
1201
+ * @param ... the variant arguments
1202
+ *
1203
+ * @return true if success, false otherwise and exception will be thrown,
1204
+ * the caller can call wasm_runtime_get_exception to get the exception
1205
+ * info.
1206
+ */
1207
+ WASM_RUNTIME_API_EXTERN bool
1208
+ wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
1209
+ wasm_function_inst_t function, uint32_t num_results,
1210
+ wasm_val_t results[], uint32_t num_args, ...);
1211
+
1212
+ /**
1213
+ * Call a function reference of a given WASM runtime instance with
1214
+ * arguments.
1215
+ *
1216
+ * Note: this can be used to call a function which is not exported
1217
+ * by the module explicitly. You might consider it as an abstraction
1218
+ * violation.
1219
+ *
1220
+ * @param exec_env the execution environment to call the function
1221
+ * which must be created from wasm_create_exec_env()
1222
+ * @param element_index the function reference index, usually
1223
+ * provided by the caller of a registered native function
1224
+ * @param argc the number of arguments
1225
+ * @param argv the arguments. If the function method has return value,
1226
+ * the first (or first two in case 64-bit return value) element of
1227
+ * argv stores the return value of the called WASM function after this
1228
+ * function returns.
1229
+ *
1230
+ * @return true if success, false otherwise and exception will be thrown,
1231
+ * the caller can call wasm_runtime_get_exception to get exception info.
1232
+ */
1233
+ WASM_RUNTIME_API_EXTERN bool
1234
+ wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32_t element_index,
1235
+ uint32_t argc, uint32_t argv[]);
1236
+
1237
+ /**
1238
+ * Find the unique main function from a WASM module instance
1239
+ * and execute that function.
1240
+ *
1241
+ * @param module_inst the WASM module instance
1242
+ * @param argc the number of arguments
1243
+ * @param argv the arguments array, if the main function has return value,
1244
+ * *(int*)argv stores the return value of the called main function after
1245
+ * this function returns.
1246
+ *
1247
+ * @return true if the main function is called, false otherwise and exception
1248
+ * will be thrown, the caller can call wasm_runtime_get_exception to get
1249
+ * the exception info.
1250
+ */
1251
+ WASM_RUNTIME_API_EXTERN bool
1252
+ wasm_application_execute_main(wasm_module_inst_t module_inst, int32_t argc,
1253
+ char *argv[]);
1254
+
1255
+ /**
1256
+ * Find the specified function from a WASM module instance and execute
1257
+ * that function.
1258
+ *
1259
+ * @param module_inst the WASM module instance
1260
+ * @param name the name of the function to execute.
1261
+ * to indicate the module name via: $module_name$function_name
1262
+ * or just a function name: function_name
1263
+ * @param argc the number of arguments
1264
+ * @param argv the arguments array
1265
+ *
1266
+ * @return true if the specified function is called, false otherwise and
1267
+ * exception will be thrown, the caller can call wasm_runtime_get_exception
1268
+ * to get the exception info.
1269
+ */
1270
+ WASM_RUNTIME_API_EXTERN bool
1271
+ wasm_application_execute_func(wasm_module_inst_t module_inst, const char *name,
1272
+ int32_t argc, char *argv[]);
1273
+
1274
+ /**
1275
+ * Get exception info of the WASM module instance.
1276
+ *
1277
+ * @param module_inst the WASM module instance
1278
+ *
1279
+ * @return the exception string
1280
+ */
1281
+ WASM_RUNTIME_API_EXTERN const char *
1282
+ wasm_runtime_get_exception(wasm_module_inst_t module_inst);
1283
+
1284
+ /**
1285
+ * Set exception info of the WASM module instance.
1286
+ *
1287
+ * @param module_inst the WASM module instance
1288
+ *
1289
+ * @param exception the exception string
1290
+ */
1291
+ WASM_RUNTIME_API_EXTERN void
1292
+ wasm_runtime_set_exception(wasm_module_inst_t module_inst,
1293
+ const char *exception);
1294
+
1295
+ /**
1296
+ * Clear exception info of the WASM module instance.
1297
+ *
1298
+ * @param module_inst the WASM module instance
1299
+ */
1300
+ WASM_RUNTIME_API_EXTERN void
1301
+ wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
1302
+
1303
+ /**
1304
+ * Terminate the WASM module instance.
1305
+ *
1306
+ * This function causes the module instance fail as if it raised a trap.
1307
+ *
1308
+ * This is intended to be used in situations like:
1309
+ *
1310
+ * - A thread is executing the WASM module instance
1311
+ * (eg. it's in the middle of `wasm_application_execute_main`)
1312
+ *
1313
+ * - Another thread has a copy of `wasm_module_inst_t` of
1314
+ * the module instance and wants to terminate it asynchronously.
1315
+ *
1316
+ * @param module_inst the WASM module instance
1317
+ */
1318
+ WASM_RUNTIME_API_EXTERN void
1319
+ wasm_runtime_terminate(wasm_module_inst_t module_inst);
1320
+
1321
+ /**
1322
+ * Set custom data to WASM module instance.
1323
+ * Note:
1324
+ * If WAMR_BUILD_LIB_PTHREAD is enabled, this API
1325
+ * will spread the custom data to all threads
1326
+ *
1327
+ * @param module_inst the WASM module instance
1328
+ * @param custom_data the custom data to be set
1329
+ */
1330
+ WASM_RUNTIME_API_EXTERN void
1331
+ wasm_runtime_set_custom_data(wasm_module_inst_t module_inst, void *custom_data);
1332
+
1333
+ /**
1334
+ * Get the custom data within a WASM module instance.
1335
+ *
1336
+ * @param module_inst the WASM module instance
1337
+ *
1338
+ * @return the custom data (NULL if not set yet)
1339
+ */
1340
+ WASM_RUNTIME_API_EXTERN void *
1341
+ wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
1342
+
1343
+ /**
1344
+ * Set the memory bounds checks flag of a WASM module instance.
1345
+ *
1346
+ * @param module_inst the WASM module instance
1347
+ * @param enable the flag to enable/disable the memory bounds checks
1348
+ */
1349
+ WASM_RUNTIME_API_EXTERN void
1350
+ wasm_runtime_set_bounds_checks(wasm_module_inst_t module_inst, bool enable);
1351
+
1352
+ /**
1353
+ * Check if the memory bounds checks flag is enabled for a WASM module instance.
1354
+ *
1355
+ * @param module_inst the WASM module instance
1356
+ * @return true if the memory bounds checks flag is enabled, false otherwise
1357
+ */
1358
+ WASM_RUNTIME_API_EXTERN bool
1359
+ wasm_runtime_is_bounds_checks_enabled(wasm_module_inst_t module_inst);
1360
+
1361
+ /**
1362
+ * Allocate memory from the heap of WASM module instance
1363
+ *
1364
+ * Note: wasm_runtime_module_malloc can call heap functions inside
1365
+ * the module instance and thus cause a memory growth.
1366
+ * This API needs to be used very carefully when you have a native
1367
+ * pointers to the module instance memory obtained with
1368
+ * wasm_runtime_addr_app_to_native or similar APIs.
1369
+ *
1370
+ * @param module_inst the WASM module instance which contains heap
1371
+ * @param size the size bytes to allocate
1372
+ * @param p_native_addr return native address of the allocated memory
1373
+ * if it is not NULL, and return NULL if memory malloc failed
1374
+ *
1375
+ * @return the allocated memory address, which is a relative offset to the
1376
+ * base address of the module instance's memory space. Note that
1377
+ * it is not an absolute address.
1378
+ * Return non-zero if success, zero if failed.
1379
+ */
1380
+ WASM_RUNTIME_API_EXTERN uint64_t
1381
+ wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint64_t size,
1382
+ void **p_native_addr);
1383
+
1384
+ /**
1385
+ * Free memory to the heap of WASM module instance
1386
+ *
1387
+ * @param module_inst the WASM module instance which contains heap
1388
+ * @param ptr the pointer to free
1389
+ */
1390
+ WASM_RUNTIME_API_EXTERN void
1391
+ wasm_runtime_module_free(wasm_module_inst_t module_inst, uint64_t ptr);
1392
+
1393
+ /**
1394
+ * Allocate memory from the heap of WASM module instance and initialize
1395
+ * the memory with src
1396
+ *
1397
+ * @param module_inst the WASM module instance which contains heap
1398
+ * @param src the source data to copy
1399
+ * @param size the size of the source data
1400
+ *
1401
+ * @return the allocated memory address, which is a relative offset to the
1402
+ * base address of the module instance's memory space. Note that
1403
+ * it is not an absolute address.
1404
+ * Return non-zero if success, zero if failed.
1405
+ */
1406
+ WASM_RUNTIME_API_EXTERN uint64_t
1407
+ wasm_runtime_module_dup_data(wasm_module_inst_t module_inst, const char *src,
1408
+ uint64_t size);
1409
+
1410
+ /**
1411
+ * Validate the app address, check whether it belongs to WASM module
1412
+ * instance's address space, or in its heap space or memory space.
1413
+ *
1414
+ * @param module_inst the WASM module instance
1415
+ * @param app_offset the app address to validate, which is a relative address
1416
+ * @param size the size bytes of the app address
1417
+ *
1418
+ * @return true if success, false otherwise. If failed, an exception will
1419
+ * be thrown.
1420
+ */
1421
+ WASM_RUNTIME_API_EXTERN bool
1422
+ wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
1423
+ uint64_t app_offset, uint64_t size);
1424
+
1425
+ /**
1426
+ * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
1427
+ * is not provided. This function validates the app string address, check
1428
+ * whether it belongs to WASM module instance's address space, or in its heap
1429
+ * space or memory space. Moreover, it checks whether it is the offset of a
1430
+ * string that is end with '\0'.
1431
+ *
1432
+ * Note: The validation result, especially the NUL termination check,
1433
+ * is not reliable for a module instance with multiple threads because
1434
+ * other threads can modify the heap behind us.
1435
+ *
1436
+ * @param module_inst the WASM module instance
1437
+ * @param app_str_offset the app address of the string to validate, which is a
1438
+ * relative address
1439
+ *
1440
+ * @return true if success, false otherwise. If failed, an exception will
1441
+ * be thrown.
1442
+ */
1443
+ WASM_RUNTIME_API_EXTERN bool
1444
+ wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
1445
+ uint64_t app_str_offset);
1446
+
1447
+ /**
1448
+ * Validate the native address, check whether it belongs to WASM module
1449
+ * instance's address space, or in its heap space or memory space.
1450
+ *
1451
+ * @param module_inst the WASM module instance
1452
+ * @param native_ptr the native address to validate, which is an absolute
1453
+ * address
1454
+ * @param size the size bytes of the app address
1455
+ *
1456
+ * @return true if success, false otherwise. If failed, an exception will
1457
+ * be thrown.
1458
+ */
1459
+ WASM_RUNTIME_API_EXTERN bool
1460
+ wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
1461
+ void *native_ptr, uint64_t size);
1462
+
1463
+ /**
1464
+ * Convert app address (relative address) to native address (absolute address)
1465
+ *
1466
+ * Note that native addresses to module instance memory can be invalidated
1467
+ * on a memory growth. (Except shared memory, whose native addresses are
1468
+ * stable.)
1469
+ *
1470
+ * @param module_inst the WASM module instance
1471
+ * @param app_offset the app address
1472
+ *
1473
+ * @return the native address converted
1474
+ */
1475
+ WASM_RUNTIME_API_EXTERN void *
1476
+ wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
1477
+ uint64_t app_offset);
1478
+
1479
+ /**
1480
+ * Convert native address (absolute address) to app address (relative address)
1481
+ *
1482
+ * @param module_inst the WASM module instance
1483
+ * @param native_ptr the native address
1484
+ *
1485
+ * @return the app address converted
1486
+ */
1487
+ WASM_RUNTIME_API_EXTERN uint64_t
1488
+ wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
1489
+ void *native_ptr);
1490
+
1491
+ /**
1492
+ * Get the app address range (relative address) that a app address belongs to
1493
+ *
1494
+ * @param module_inst the WASM module instance
1495
+ * @param app_offset the app address to retrieve
1496
+ * @param p_app_start_offset buffer to output the app start offset if not NULL
1497
+ * @param p_app_end_offset buffer to output the app end offset if not NULL
1498
+ *
1499
+ * @return true if success, false otherwise.
1500
+ */
1501
+ WASM_RUNTIME_API_EXTERN bool
1502
+ wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
1503
+ uint64_t app_offset,
1504
+ uint64_t *p_app_start_offset,
1505
+ uint64_t *p_app_end_offset);
1506
+
1507
+ /**
1508
+ * Get the native address range (absolute address) that a native address
1509
+ * belongs to
1510
+ *
1511
+ * @param module_inst the WASM module instance
1512
+ * @param native_ptr the native address to retrieve
1513
+ * @param p_native_start_addr buffer to output the native start address
1514
+ * if not NULL
1515
+ * @param p_native_end_addr buffer to output the native end address
1516
+ * if not NULL
1517
+ *
1518
+ * @return true if success, false otherwise.
1519
+ */
1520
+ WASM_RUNTIME_API_EXTERN bool
1521
+ wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
1522
+ uint8_t *native_ptr,
1523
+ uint8_t **p_native_start_addr,
1524
+ uint8_t **p_native_end_addr);
1525
+
1526
+ /**
1527
+ * Get the number of import items for a WASM module
1528
+ *
1529
+ * @param module the WASM module
1530
+ *
1531
+ * @return the number of imports (zero for none), or -1 for failure
1532
+ */
1533
+ WASM_RUNTIME_API_EXTERN int32_t
1534
+ wasm_runtime_get_import_count(const wasm_module_t module);
1535
+
1536
+ /**
1537
+ * Get information about a specific WASM module import
1538
+ *
1539
+ * @param module the WASM module
1540
+ * @param import_index the desired import index
1541
+ * @param import_type the location to store information about the import
1542
+ */
1543
+ WASM_RUNTIME_API_EXTERN void
1544
+ wasm_runtime_get_import_type(const wasm_module_t module, int32_t import_index,
1545
+ wasm_import_t *import_type);
1546
+
1547
+ /**
1548
+ * Get the number of export items for a WASM module
1549
+ *
1550
+ * @param module the WASM module
1551
+ *
1552
+ * @return the number of exports (zero for none), or -1 for failure
1553
+ */
1554
+ WASM_RUNTIME_API_EXTERN int32_t
1555
+ wasm_runtime_get_export_count(const wasm_module_t module);
1556
+
1557
+ /**
1558
+ * Get information about a specific WASM module export
1559
+ *
1560
+ * @param module the WASM module
1561
+ * @param export_index the desired export index
1562
+ * @param export_type the location to store information about the export
1563
+ */
1564
+ WASM_RUNTIME_API_EXTERN void
1565
+ wasm_runtime_get_export_type(const wasm_module_t module, int32_t export_index,
1566
+ wasm_export_t *export_type);
1567
+
1568
+ /**
1569
+ * Get the number of parameters for a function type
1570
+ *
1571
+ * @param func_type the function type
1572
+ *
1573
+ * @return the number of parameters for the function type
1574
+ */
1575
+ WASM_RUNTIME_API_EXTERN uint32_t
1576
+ wasm_func_type_get_param_count(const wasm_func_type_t func_type);
1577
+
1578
+ /**
1579
+ * Get the kind of a parameter for a function type
1580
+ *
1581
+ * @param func_type the function type
1582
+ * @param param_index the index of the parameter to get
1583
+ *
1584
+ * @return the kind of the parameter if successful, -1 otherwise
1585
+ */
1586
+ WASM_RUNTIME_API_EXTERN wasm_valkind_t
1587
+ wasm_func_type_get_param_valkind(const wasm_func_type_t func_type,
1588
+ uint32_t param_index);
1589
+
1590
+ /**
1591
+ * Get the number of results for a function type
1592
+ *
1593
+ * @param func_type the function type
1594
+ *
1595
+ * @return the number of results for the function type
1596
+ */
1597
+ WASM_RUNTIME_API_EXTERN uint32_t
1598
+ wasm_func_type_get_result_count(const wasm_func_type_t func_type);
1599
+
1600
+ /**
1601
+ * Get the kind of a result for a function type
1602
+ *
1603
+ * @param func_type the function type
1604
+ * @param result_index the index of the result to get
1605
+ *
1606
+ * @return the kind of the result if successful, -1 otherwise
1607
+ */
1608
+ WASM_RUNTIME_API_EXTERN wasm_valkind_t
1609
+ wasm_func_type_get_result_valkind(const wasm_func_type_t func_type,
1610
+ uint32_t result_index);
1611
+
1612
+ /**
1613
+ * Get the kind for a global type
1614
+ *
1615
+ * @param global_type the global type
1616
+ *
1617
+ * @return the kind of the global
1618
+ */
1619
+ WASM_RUNTIME_API_EXTERN wasm_valkind_t
1620
+ wasm_global_type_get_valkind(const wasm_global_type_t global_type);
1621
+
1622
+ /**
1623
+ * Get the mutability for a global type
1624
+ *
1625
+ * @param global_type the global type
1626
+ *
1627
+ * @return true if mutable, false otherwise
1628
+ */
1629
+ WASM_RUNTIME_API_EXTERN bool
1630
+ wasm_global_type_get_mutable(const wasm_global_type_t global_type);
1631
+
1632
+ /**
1633
+ * Get the shared setting for a memory type
1634
+ *
1635
+ * @param memory_type the memory type
1636
+ *
1637
+ * @return true if shared, false otherwise
1638
+ */
1639
+ WASM_RUNTIME_API_EXTERN bool
1640
+ wasm_memory_type_get_shared(const wasm_memory_type_t memory_type);
1641
+
1642
+ /**
1643
+ * Get the initial page count for a memory type
1644
+ *
1645
+ * @param memory_type the memory type
1646
+ *
1647
+ * @return the initial memory page count
1648
+ */
1649
+ WASM_RUNTIME_API_EXTERN uint32_t
1650
+ wasm_memory_type_get_init_page_count(const wasm_memory_type_t memory_type);
1651
+
1652
+ /**
1653
+ * Get the maximum page count for a memory type
1654
+ *
1655
+ * @param memory_type the memory type
1656
+ *
1657
+ * @return the maximum memory page count
1658
+ */
1659
+ WASM_RUNTIME_API_EXTERN uint32_t
1660
+ wasm_memory_type_get_max_page_count(const wasm_memory_type_t memory_type);
1661
+
1662
+ /**
1663
+ * Get the element kind for a table type
1664
+ *
1665
+ * @param table_type the table type
1666
+ *
1667
+ * @return the element kind
1668
+ */
1669
+ WASM_RUNTIME_API_EXTERN wasm_valkind_t
1670
+ wasm_table_type_get_elem_kind(const wasm_table_type_t table_type);
1671
+
1672
+ /**
1673
+ * Get the sharing setting for a table type
1674
+ *
1675
+ * @param table_type the table type
1676
+ *
1677
+ * @return true if shared, false otherwise
1678
+ */
1679
+ WASM_RUNTIME_API_EXTERN bool
1680
+ wasm_table_type_get_shared(const wasm_table_type_t table_type);
1681
+
1682
+ /**
1683
+ * Get the initial size for a table type
1684
+ *
1685
+ * @param table_type the table type
1686
+ *
1687
+ * @return the initial table size
1688
+ */
1689
+ WASM_RUNTIME_API_EXTERN uint32_t
1690
+ wasm_table_type_get_init_size(const wasm_table_type_t table_type);
1691
+
1692
+ /**
1693
+ * Get the maximum size for a table type
1694
+ *
1695
+ * @param table_type the table type
1696
+ *
1697
+ * @return the maximum table size
1698
+ */
1699
+ WASM_RUNTIME_API_EXTERN uint32_t
1700
+ wasm_table_type_get_max_size(const wasm_table_type_t table_type);
1701
+
1702
+ /**
1703
+ * Register native functions with same module name
1704
+ *
1705
+ * Note: The array `native_symbols` should not be read-only because the
1706
+ * library can modify it in-place.
1707
+ *
1708
+ * Note: After successful call of this function, the array `native_symbols`
1709
+ * is owned by the library.
1710
+ *
1711
+ * @param module_name the module name of the native functions
1712
+ * @param native_symbols specifies an array of NativeSymbol structures which
1713
+ * contain the names, function pointers and signatures
1714
+ * Note: WASM runtime will not allocate memory to clone the data, so
1715
+ * user must ensure the array can be used forever
1716
+ * Meanings of letters in function signature:
1717
+ * 'i': the parameter is i32 type
1718
+ * 'I': the parameter is i64 type
1719
+ * 'f': the parameter is f32 type
1720
+ * 'F': the parameter is f64 type
1721
+ * 'r': the parameter is externref type, it should be a uintptr_t
1722
+ * in host
1723
+ * '*': the parameter is a pointer (i32 in WASM), and runtime will
1724
+ * auto check its boundary before calling the native function.
1725
+ * If it is followed by '~', the checked length of the pointer
1726
+ * is gotten from the following parameter, if not, the checked
1727
+ * length of the pointer is 1.
1728
+ * '~': the parameter is the pointer's length with i32 type, and must
1729
+ * follow after '*'
1730
+ * '$': the parameter is a string (i32 in WASM), and runtime will
1731
+ * auto check its boundary before calling the native function
1732
+ * @param n_native_symbols specifies the number of native symbols in the array
1733
+ *
1734
+ * @return true if success, false otherwise
1735
+ */
1736
+ WASM_RUNTIME_API_EXTERN bool
1737
+ wasm_runtime_register_natives(const char *module_name,
1738
+ NativeSymbol *native_symbols,
1739
+ uint32_t n_native_symbols);
1740
+
1741
+ /**
1742
+ * Register native functions with same module name, similar to
1743
+ * wasm_runtime_register_natives, the difference is that runtime passes raw
1744
+ * arguments to native API, which means that the native API should be defined as
1745
+ * void foo(wasm_exec_env_t exec_env, uint64 *args);
1746
+ * and native API should extract arguments one by one from args array with macro
1747
+ * native_raw_get_arg
1748
+ * and write the return value back to args[0] with macro
1749
+ * native_raw_return_type and native_raw_set_return
1750
+ */
1751
+ WASM_RUNTIME_API_EXTERN bool
1752
+ wasm_runtime_register_natives_raw(const char *module_name,
1753
+ NativeSymbol *native_symbols,
1754
+ uint32_t n_native_symbols);
1755
+
1756
+ /**
1757
+ * Undo wasm_runtime_register_natives or wasm_runtime_register_natives_raw
1758
+ *
1759
+ * @param module_name Should be the same as the corresponding
1760
+ * wasm_runtime_register_natives.
1761
+ * (Same in term of strcmp.)
1762
+ *
1763
+ * @param native_symbols Should be the same as the corresponding
1764
+ * wasm_runtime_register_natives.
1765
+ * (Same in term of pointer comparison.)
1766
+ *
1767
+ * @return true if success, false otherwise
1768
+ */
1769
+ WASM_RUNTIME_API_EXTERN bool
1770
+ wasm_runtime_unregister_natives(const char *module_name,
1771
+ NativeSymbol *native_symbols);
1772
+
1773
+ /**
1774
+ * Get an export global instance
1775
+ *
1776
+ * @param module_inst the module instance
1777
+ * @param name the export global name
1778
+ * @param global_inst location to store the global instance
1779
+ *
1780
+ * @return true if success, false otherwise
1781
+ *
1782
+ */
1783
+ WASM_RUNTIME_API_EXTERN bool
1784
+ wasm_runtime_get_export_global_inst(const wasm_module_inst_t module_inst,
1785
+ const char *name,
1786
+ wasm_global_inst_t *global_inst);
1787
+
1788
+ /**
1789
+ * Get an export table instance
1790
+ *
1791
+ * @param module_inst the module instance
1792
+ * @param name the export table name
1793
+ * @param table_inst location to store the table instance
1794
+ *
1795
+ * @return true if success, false otherwise
1796
+ *
1797
+ */
1798
+ WASM_RUNTIME_API_EXTERN bool
1799
+ wasm_runtime_get_export_table_inst(const wasm_module_inst_t module_inst,
1800
+ const char *name,
1801
+ wasm_table_inst_t *table_inst);
1802
+
1803
+ /**
1804
+ * Get a function instance from a table.
1805
+ *
1806
+ * @param module_inst the module instance
1807
+ * @param table_inst the table instance
1808
+ * @param idx the index in the table
1809
+ *
1810
+ * @return the function instance if successful, NULL otherwise
1811
+ */
1812
+ WASM_RUNTIME_API_EXTERN wasm_function_inst_t
1813
+ wasm_table_get_func_inst(const wasm_module_inst_t module_inst,
1814
+ const wasm_table_inst_t *table_inst, uint32_t idx);
1815
+
1816
+ /**
1817
+ * Get attachment of native function from execution environment
1818
+ *
1819
+ * @param exec_env the execution environment to retrieve
1820
+ *
1821
+ * @return the attachment of native function
1822
+ */
1823
+ WASM_RUNTIME_API_EXTERN void *
1824
+ wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
1825
+
1826
+ /**
1827
+ * Set user data to execution environment.
1828
+ *
1829
+ * @param exec_env the execution environment
1830
+ * @param user_data the user data to be set
1831
+ */
1832
+ WASM_RUNTIME_API_EXTERN void
1833
+ wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data);
1834
+
1835
+ /**
1836
+ * Get the user data within execution environment.
1837
+ *
1838
+ * @param exec_env the execution environment
1839
+ *
1840
+ * @return the user data (NULL if not set yet)
1841
+ */
1842
+ WASM_RUNTIME_API_EXTERN void *
1843
+ wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
1844
+
1845
+ /**
1846
+ * Set native stack boundary to execution environment, if it is set,
1847
+ * it will be used instead of getting the boundary with the platform
1848
+ * layer API when calling wasm functions. This is useful for some
1849
+ * fiber cases.
1850
+ *
1851
+ * Note: unlike setting the boundary by runtime, this API doesn't add
1852
+ * the WASM_STACK_GUARD_SIZE(see comments in core/config.h) to the
1853
+ * exec_env's native_stack_boundary to reserve bytes to the native
1854
+ * thread stack boundary, which is used to throw native stack overflow
1855
+ * exception if the guard boundary is reached. Developer should ensure
1856
+ * that enough guard bytes are kept.
1857
+ *
1858
+ * @param exec_env the execution environment
1859
+ * @param native_stack_boundary the user data to be set
1860
+ */
1861
+ WASM_RUNTIME_API_EXTERN void
1862
+ wasm_runtime_set_native_stack_boundary(wasm_exec_env_t exec_env,
1863
+ uint8_t *native_stack_boundary);
1864
+
1865
+ /**
1866
+ * Set the instruction count limit to the execution environment.
1867
+ * By default the instruction count limit is -1, which means no limit.
1868
+ * However, if the instruction count limit is set to a positive value,
1869
+ * the execution will be terminated when the instruction count reaches
1870
+ * the limit.
1871
+ *
1872
+ * @param exec_env the execution environment
1873
+ * @param instruction_count the instruction count limit
1874
+ */
1875
+ WASM_RUNTIME_API_EXTERN void
1876
+ wasm_runtime_set_instruction_count_limit(wasm_exec_env_t exec_env,
1877
+ int instruction_count);
1878
+
1879
+ /**
1880
+ * Dump runtime memory consumption, including:
1881
+ * Exec env memory consumption
1882
+ * WASM module memory consumption
1883
+ * WASM module instance memory consumption
1884
+ * stack and app heap used info
1885
+ *
1886
+ * @param exec_env the execution environment
1887
+ */
1888
+ WASM_RUNTIME_API_EXTERN void
1889
+ wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env);
1890
+
1891
+ /**
1892
+ * Dump runtime performance profiler data of each function
1893
+ *
1894
+ * @param module_inst the WASM module instance to profile
1895
+ */
1896
+ WASM_RUNTIME_API_EXTERN void
1897
+ wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
1898
+
1899
+ /**
1900
+ * Return total wasm functions' execution time in ms
1901
+ *
1902
+ * @param module_inst the WASM module instance to profile
1903
+ */
1904
+ WASM_RUNTIME_API_EXTERN double
1905
+ wasm_runtime_sum_wasm_exec_time(wasm_module_inst_t module_inst);
1906
+
1907
+ /**
1908
+ * Return execution time in ms of a given wasm function with
1909
+ * func_name. If the function is not found, return 0.
1910
+ *
1911
+ * @param module_inst the WASM module instance to profile
1912
+ * @param func_name could be an export name or a name in the
1913
+ * name section
1914
+ */
1915
+ WASM_RUNTIME_API_EXTERN double
1916
+ wasm_runtime_get_wasm_func_exec_time(wasm_module_inst_t inst,
1917
+ const char *func_name);
1918
+
1919
+ /* wasm thread callback function type */
1920
+ typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *);
1921
+ /* wasm thread type */
1922
+ typedef uintptr_t wasm_thread_t;
1923
+
1924
+ /**
1925
+ * Set the max thread num per cluster.
1926
+ *
1927
+ * @param num maximum thread num
1928
+ */
1929
+ WASM_RUNTIME_API_EXTERN void
1930
+ wasm_runtime_set_max_thread_num(uint32_t num);
1931
+
1932
+ /**
1933
+ * Spawn a new exec_env, the spawned exec_env
1934
+ * can be used in other threads
1935
+ *
1936
+ * @param num the original exec_env
1937
+ *
1938
+ * @return the spawned exec_env if success, NULL otherwise
1939
+ */
1940
+ WASM_RUNTIME_API_EXTERN wasm_exec_env_t
1941
+ wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
1942
+
1943
+ /**
1944
+ * Destroy the spawned exec_env
1945
+ *
1946
+ * @param exec_env the spawned exec_env
1947
+ */
1948
+ WASM_RUNTIME_API_EXTERN void
1949
+ wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
1950
+
1951
+ /**
1952
+ * Spawn a thread from the given exec_env
1953
+ *
1954
+ * @param exec_env the original exec_env
1955
+ * @param tid thread id to be returned to the caller
1956
+ * @param callback the callback function provided by the user
1957
+ * @param arg the arguments passed to the callback
1958
+ *
1959
+ * @return 0 if success, -1 otherwise
1960
+ */
1961
+ WASM_RUNTIME_API_EXTERN int32_t
1962
+ wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
1963
+ wasm_thread_callback_t callback, void *arg);
1964
+
1965
+ /**
1966
+ * Wait a spawned thread to terminate
1967
+ *
1968
+ * @param tid thread id
1969
+ * @param retval if not NULL, output the return value of the thread
1970
+ *
1971
+ * @return 0 if success, error number otherwise
1972
+ */
1973
+ WASM_RUNTIME_API_EXTERN int32_t
1974
+ wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
1975
+
1976
+ /**
1977
+ * Map external object to an internal externref index: if the index
1978
+ * has been created, return it, otherwise create the index.
1979
+ *
1980
+ * @param module_inst the WASM module instance that the extern object
1981
+ * belongs to
1982
+ * @param extern_obj the external object to be mapped
1983
+ * @param p_externref_idx return externref index of the external object
1984
+ *
1985
+ * @return true if success, false otherwise
1986
+ */
1987
+ WASM_RUNTIME_API_EXTERN bool
1988
+ wasm_externref_obj2ref(wasm_module_inst_t module_inst, void *extern_obj,
1989
+ uint32_t *p_externref_idx);
1990
+
1991
+ /**
1992
+ * Delete external object registered by `wasm_externref_obj2ref`.
1993
+ *
1994
+ * @param module_inst the WASM module instance that the extern object
1995
+ * belongs to
1996
+ * @param extern_obj the external object to be deleted
1997
+ *
1998
+ * @return true if success, false otherwise
1999
+ */
2000
+ WASM_RUNTIME_API_EXTERN bool
2001
+ wasm_externref_objdel(wasm_module_inst_t module_inst, void *extern_obj);
2002
+
2003
+ /**
2004
+ * Set cleanup callback to release external object.
2005
+ *
2006
+ * @param module_inst the WASM module instance that the extern object
2007
+ * belongs to
2008
+ * @param extern_obj the external object to which to set the
2009
+ * `extern_obj_cleanup` cleanup callback.
2010
+ * @param extern_obj_cleanup a callback to release `extern_obj`
2011
+ *
2012
+ * @return true if success, false otherwise
2013
+ */
2014
+ WASM_RUNTIME_API_EXTERN bool
2015
+ wasm_externref_set_cleanup(wasm_module_inst_t module_inst, void *extern_obj,
2016
+ void (*extern_obj_cleanup)(void *));
2017
+
2018
+ /**
2019
+ * Retrieve the external object from an internal externref index
2020
+ *
2021
+ * @param externref_idx the externref index to retrieve
2022
+ * @param p_extern_obj return the mapped external object of
2023
+ * the externref index
2024
+ *
2025
+ * @return true if success, false otherwise
2026
+ */
2027
+ WASM_RUNTIME_API_EXTERN bool
2028
+ wasm_externref_ref2obj(uint32_t externref_idx, void **p_extern_obj);
2029
+
2030
+ /**
2031
+ * Retain an extern object which is mapped to the internal externref
2032
+ * so that the object won't be cleaned during extern object reclaim
2033
+ * if it isn't used.
2034
+ *
2035
+ * @param externref_idx the externref index of an external object
2036
+ * to retain
2037
+ * @return true if success, false otherwise
2038
+ */
2039
+ WASM_RUNTIME_API_EXTERN bool
2040
+ wasm_externref_retain(uint32_t externref_idx);
2041
+
2042
+ /**
2043
+ * Dump the call stack to stdout
2044
+ *
2045
+ * @param exec_env the execution environment
2046
+ */
2047
+ WASM_RUNTIME_API_EXTERN void
2048
+ wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
2049
+
2050
+ /**
2051
+ * Get the size required to store the call stack contents, including
2052
+ * the space for terminating null byte ('\0')
2053
+ *
2054
+ * @param exec_env the execution environment
2055
+ *
2056
+ * @return size required to store the contents, 0 means error
2057
+ */
2058
+ WASM_RUNTIME_API_EXTERN uint32_t
2059
+ wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env);
2060
+
2061
+ /**
2062
+ * Dump the call stack to buffer.
2063
+ *
2064
+ * @note this function is not thread-safe, please only use this API
2065
+ * when the exec_env is not executing
2066
+ *
2067
+ * @param exec_env the execution environment
2068
+ * @param buf buffer to store the dumped content
2069
+ * @param len length of the buffer
2070
+ *
2071
+ * @return bytes dumped to the buffer, including the terminating null
2072
+ * byte ('\0'), 0 means error and data in buf may be invalid
2073
+ */
2074
+ WASM_RUNTIME_API_EXTERN uint32_t
2075
+ wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
2076
+ uint32_t len);
2077
+
2078
+ /**
2079
+ * Get the size required to store the LLVM PGO profile data
2080
+ *
2081
+ * @param module_inst the WASM module instance
2082
+ *
2083
+ * @return size required to store the contents, 0 means error
2084
+ */
2085
+ WASM_RUNTIME_API_EXTERN uint32_t
2086
+ wasm_runtime_get_pgo_prof_data_size(wasm_module_inst_t module_inst);
2087
+
2088
+ /**
2089
+ * Dump the LLVM PGO profile data to buffer
2090
+ *
2091
+ * @param module_inst the WASM module instance
2092
+ * @param buf buffer to store the dumped content
2093
+ * @param len length of the buffer
2094
+ *
2095
+ * @return bytes dumped to the buffer, 0 means error and data in buf
2096
+ * may be invalid
2097
+ */
2098
+ WASM_RUNTIME_API_EXTERN uint32_t
2099
+ wasm_runtime_dump_pgo_prof_data_to_buf(wasm_module_inst_t module_inst,
2100
+ char *buf, uint32_t len);
2101
+
2102
+ /**
2103
+ * Get a custom section by name
2104
+ *
2105
+ * @param module_comm the module to find
2106
+ * @param name name of the custom section
2107
+ * @param len return the length of the content if found
2108
+ *
2109
+ * @return Custom section content (not including the name length
2110
+ * and name string) if found, NULL otherwise
2111
+ */
2112
+ WASM_RUNTIME_API_EXTERN const uint8_t *
2113
+ wasm_runtime_get_custom_section(const wasm_module_t module_comm,
2114
+ const char *name, uint32_t *len);
2115
+
2116
+ /**
2117
+ * Get WAMR semantic version
2118
+ */
2119
+ WASM_RUNTIME_API_EXTERN void
2120
+ wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch);
2121
+
2122
+ /**
2123
+ * Check whether an import func `(import <module_name> <func_name> (func ...))`
2124
+ * is linked or not with runtime registered native functions
2125
+ */
2126
+ WASM_RUNTIME_API_EXTERN bool
2127
+ wasm_runtime_is_import_func_linked(const char *module_name,
2128
+ const char *func_name);
2129
+
2130
+ /**
2131
+ * Check whether an import global `(import <module_name> <global_name>
2132
+ * (global ...))` is linked or not with runtime registered native globals
2133
+ */
2134
+ WASM_RUNTIME_API_EXTERN bool
2135
+ wasm_runtime_is_import_global_linked(const char *module_name,
2136
+ const char *global_name);
2137
+
2138
+ /**
2139
+ * Enlarge the memory region for a module instance
2140
+ *
2141
+ * @param module_inst the module instance
2142
+ * @param inc_page_count the number of pages to add
2143
+ *
2144
+ * @return true if success, false otherwise
2145
+ */
2146
+ WASM_RUNTIME_API_EXTERN bool
2147
+ wasm_runtime_enlarge_memory(wasm_module_inst_t module_inst,
2148
+ uint64_t inc_page_count);
2149
+
2150
+ typedef enum {
2151
+ INTERNAL_ERROR,
2152
+ MAX_SIZE_REACHED,
2153
+ } enlarge_memory_error_reason_t;
2154
+
2155
+ typedef void (*enlarge_memory_error_callback_t)(
2156
+ uint32_t inc_page_count, uint64_t current_memory_size,
2157
+ uint32_t memory_index, enlarge_memory_error_reason_t failure_reason,
2158
+ wasm_module_inst_t instance, wasm_exec_env_t exec_env, void *user_data);
2159
+
2160
+ /**
2161
+ * Setup callback invoked when memory.grow fails
2162
+ */
2163
+ WASM_RUNTIME_API_EXTERN void
2164
+ wasm_runtime_set_enlarge_mem_error_callback(
2165
+ const enlarge_memory_error_callback_t callback, void *user_data);
2166
+
2167
+ /*
2168
+ * module instance context APIs
2169
+ * wasm_runtime_create_context_key
2170
+ * wasm_runtime_destroy_context_key
2171
+ * wasm_runtime_set_context
2172
+ * wasm_runtime_set_context_spread
2173
+ * wasm_runtime_get_context
2174
+ *
2175
+ * This set of APIs is intended to be used by an embedder which provides
2176
+ * extra sets of native functions, which need per module instance state
2177
+ * and are maintained outside of the WAMR tree.
2178
+ *
2179
+ * It's modelled after the pthread specific API.
2180
+ *
2181
+ * wasm_runtime_set_context_spread is similar to
2182
+ * wasm_runtime_set_context, except that
2183
+ * wasm_runtime_set_context_spread applies the change
2184
+ * to all threads in the cluster.
2185
+ * It's an undefined behavior if multiple threads in a cluster call
2186
+ * wasm_runtime_set_context_spread on the same key
2187
+ * simultaneously. It's a caller's responsibility to perform necessary
2188
+ * serialization if necessary. For example:
2189
+ *
2190
+ * if (wasm_runtime_get_context(inst, key) == NULL) {
2191
+ * newctx = alloc_and_init(...);
2192
+ * lock(some_lock);
2193
+ * if (wasm_runtime_get_context(inst, key) == NULL) {
2194
+ * // this thread won the race
2195
+ * wasm_runtime_set_context_spread(inst, key, newctx);
2196
+ * newctx = NULL;
2197
+ * }
2198
+ * unlock(some_lock);
2199
+ * if (newctx != NULL) {
2200
+ * // this thread lost the race, free it
2201
+ * cleanup_and_free(newctx);
2202
+ * }
2203
+ * }
2204
+ *
2205
+ * Note: dynamic key create/destroy while instances are live is not
2206
+ * implemented as of writing this.
2207
+ * it's caller's responsibility to ensure destroying all module instances
2208
+ * before calling wasm_runtime_create_context_key or
2209
+ * wasm_runtime_destroy_context_key.
2210
+ * otherwise, it's an undefined behavior.
2211
+ *
2212
+ * Note about threads:
2213
+ * - When spawning a thread, the contexts (the pointers given to
2214
+ * wasm_runtime_set_context) are copied from the parent
2215
+ * instance.
2216
+ * - The destructor is called only on the main instance.
2217
+ */
2218
+
2219
+ WASM_RUNTIME_API_EXTERN void *
2220
+ wasm_runtime_create_context_key(void (*dtor)(wasm_module_inst_t inst,
2221
+ void *ctx));
2222
+
2223
+ WASM_RUNTIME_API_EXTERN void
2224
+ wasm_runtime_destroy_context_key(void *key);
2225
+
2226
+ WASM_RUNTIME_API_EXTERN void
2227
+ wasm_runtime_set_context(wasm_module_inst_t inst, void *key, void *ctx);
2228
+
2229
+ WASM_RUNTIME_API_EXTERN void
2230
+ wasm_runtime_set_context_spread(wasm_module_inst_t inst, void *key, void *ctx);
2231
+
2232
+ WASM_RUNTIME_API_EXTERN void *
2233
+ wasm_runtime_get_context(wasm_module_inst_t inst, void *key);
2234
+
2235
+ /*
2236
+ * wasm_runtime_begin_blocking_op/wasm_runtime_end_blocking_op
2237
+ *
2238
+ * These APIs are intended to be used by the implementations of
2239
+ * host functions. It wraps an operation which possibly blocks for long
2240
+ * to prepare for async termination.
2241
+ *
2242
+ * For simplicity, we recommend to wrap only the very minimum piece of
2243
+ * the code with this. Ideally, just a single system call.
2244
+ *
2245
+ * eg.
2246
+ *
2247
+ * if (!wasm_runtime_begin_blocking_op(exec_env)) {
2248
+ * return EINTR;
2249
+ * }
2250
+ * ret = possibly_blocking_op();
2251
+ * wasm_runtime_end_blocking_op(exec_env);
2252
+ * return ret;
2253
+ *
2254
+ * If threading support (WASM_ENABLE_THREAD_MGR) is not enabled,
2255
+ * these functions are no-op.
2256
+ *
2257
+ * If the underlying platform support (OS_ENABLE_WAKEUP_BLOCKING_OP) is
2258
+ * not available, these functions are no-op. In that case, the runtime
2259
+ * might not terminate a blocking thread in a timely manner.
2260
+ *
2261
+ * If the underlying platform support is available, it's used to wake up
2262
+ * the thread for async termination. The expectation here is that a
2263
+ * `os_wakeup_blocking_op` call makes the blocking operation
2264
+ * (`possibly_blocking_op` in the above example) return in a timely manner.
2265
+ *
2266
+ * The actual wake up mechanism used by `os_wakeup_blocking_op` is
2267
+ * platform-dependent. It might impose some platform-dependent restrictions
2268
+ * on the implementation of the blocking operation.
2269
+ *
2270
+ * For example, on POSIX-like platforms, a signal (by default SIGUSR1) is
2271
+ * used. The signal delivery configurations (eg. signal handler, signal mask,
2272
+ * etc) for the signal are set up by the runtime. You can change the signal
2273
+ * to use for this purpose by calling os_set_signal_number_for_blocking_op
2274
+ * before the runtime initialization.
2275
+ */
2276
+ WASM_RUNTIME_API_EXTERN bool
2277
+ wasm_runtime_begin_blocking_op(wasm_exec_env_t exec_env);
2278
+
2279
+ WASM_RUNTIME_API_EXTERN void
2280
+ wasm_runtime_end_blocking_op(wasm_exec_env_t exec_env);
2281
+
2282
+ WASM_RUNTIME_API_EXTERN bool
2283
+ wasm_runtime_set_module_name(wasm_module_t module, const char *name,
2284
+ char *error_buf, uint32_t error_buf_size);
2285
+
2286
+ /* return the most recently set module name or "" if never set before */
2287
+ WASM_RUNTIME_API_EXTERN const char *
2288
+ wasm_runtime_get_module_name(wasm_module_t module);
2289
+
2290
+ /*
2291
+ * wasm_runtime_detect_native_stack_overflow
2292
+ *
2293
+ * Detect native stack shortage.
2294
+ * Ensure that the calling thread still has a reasonable amount of
2295
+ * native stack (WASM_STACK_GUARD_SIZE bytes) available.
2296
+ *
2297
+ * If enough stack is left, this function returns true.
2298
+ * Otherwise, this function raises a "native stack overflow" trap and
2299
+ * returns false.
2300
+ *
2301
+ * Note: please do not expect a very strict detection. it's a good idea
2302
+ * to give some margins. wasm_runtime_detect_native_stack_overflow itself
2303
+ * requires a small amount of stack to run.
2304
+ */
2305
+ WASM_RUNTIME_API_EXTERN bool
2306
+ wasm_runtime_detect_native_stack_overflow(wasm_exec_env_t exec_env);
2307
+
2308
+ /*
2309
+ * wasm_runtime_detect_native_stack_overflow_size
2310
+ *
2311
+ * Similar to wasm_runtime_detect_native_stack_overflow,
2312
+ * but use the caller-specified size instead of WASM_STACK_GUARD_SIZE.
2313
+ *
2314
+ * An expected usage:
2315
+ * ```c
2316
+ * __attribute__((noinline)) // inlining can break the stack check
2317
+ * void stack_hog(void)
2318
+ * {
2319
+ * // consume a lot of stack here
2320
+ * }
2321
+ *
2322
+ * void
2323
+ * stack_hog_wrapper(exec_env) {
2324
+ * // the amount of stack stack_hog would consume,
2325
+ * // plus a small margin
2326
+ * uint32_t size = 10000000;
2327
+ *
2328
+ * if (!wasm_runtime_detect_native_stack_overflow_size(exec_env, size)) {
2329
+ * // wasm_runtime_detect_native_stack_overflow_size has raised
2330
+ * // a trap.
2331
+ * return;
2332
+ * }
2333
+ * stack_hog();
2334
+ * }
2335
+ * ```
2336
+ */
2337
+ WASM_RUNTIME_API_EXTERN bool
2338
+ wasm_runtime_detect_native_stack_overflow_size(wasm_exec_env_t exec_env,
2339
+ uint32_t required_size);
2340
+
2341
+ /**
2342
+ * Query whether the wasm binary buffer used to create the module can be freed
2343
+ *
2344
+ * @param module the target module
2345
+ * @return true if the wasm binary buffer can be freed
2346
+ */
2347
+ WASM_RUNTIME_API_EXTERN bool
2348
+ wasm_runtime_is_underlying_binary_freeable(const wasm_module_t module);
2349
+
2350
+ /**
2351
+ * Create a shared heap
2352
+ *
2353
+ * @param init_args the initialization arguments
2354
+ * @return the shared heap created
2355
+ */
2356
+ WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
2357
+ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
2358
+
2359
+ /**
2360
+ * This function links two shared heap(lists), `head` and `body` in to a single
2361
+ * shared heap list, where `head` becomes the new shared heap list head. The
2362
+ * shared heap list remains one continuous shared heap in wasm app's point of
2363
+ * view. At most one shared heap in shared heap list can be dynamically
2364
+ * allocated, the rest have to be the pre-allocated shared heap. *
2365
+ *
2366
+ * @param head The head of the shared heap chain.
2367
+ * @param body The body of the shared heap chain to be appended.
2368
+ * @return The new head of the shared heap chain. NULL if failed.
2369
+ */
2370
+ WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
2371
+ wasm_runtime_chain_shared_heaps(wasm_shared_heap_t head,
2372
+ wasm_shared_heap_t body);
2373
+
2374
+ /**
2375
+ * This function unchains the shared heaps from the given head. If
2376
+ * `entire_chain` is true, it will unchain the entire chain of shared heaps.
2377
+ * Otherwise, it will unchain only the first shared heap in the chain.
2378
+ *
2379
+ * @param head The head of the shared heap chain.
2380
+ * @param entire_chain A boolean flag indicating whether to unchain the entire
2381
+ * chain.
2382
+ * @return The new head of the shared heap chain. Or the last shared heap in the
2383
+ * chain if `entire_chain` is true.
2384
+ */
2385
+ wasm_shared_heap_t
2386
+ wasm_runtime_unchain_shared_heaps(wasm_shared_heap_t head, bool entire_chain);
2387
+
2388
+ /**
2389
+ * Attach a shared heap, it can be the head of shared heap chain, in that case,
2390
+ * attach the shared heap chain, to a module instance
2391
+ *
2392
+ * @param module_inst the module instance
2393
+ * @param shared_heap the shared heap
2394
+ * @return true if success, false if failed
2395
+ */
2396
+ WASM_RUNTIME_API_EXTERN bool
2397
+ wasm_runtime_attach_shared_heap(wasm_module_inst_t module_inst,
2398
+ wasm_shared_heap_t shared_heap);
2399
+
2400
+ /**
2401
+ * Detach a shared heap from a module instance
2402
+ *
2403
+ * @param module_inst the module instance
2404
+ */
2405
+ WASM_RUNTIME_API_EXTERN void
2406
+ wasm_runtime_detach_shared_heap(wasm_module_inst_t module_inst);
2407
+
2408
+ /**
2409
+ * Allocate memory from a shared heap, or the non-preallocated shared heap from
2410
+ * the shared heap chain
2411
+ *
2412
+ * @param module_inst the module instance
2413
+ * @param size required memory size
2414
+ * @param p_native_addr native address of allocated memory
2415
+ *
2416
+ * @return return the allocated memory address, which reuses part of the wasm
2417
+ * address space and is in the range of [UINT32 - shared_heap_size + 1, UINT32]
2418
+ * (when the wasm memory is 32-bit) or [UINT64 - shared_heap_size + 1, UINT64]
2419
+ * (when the wasm memory is 64-bit). Note that it is not an absolute address.
2420
+ * Return non-zero if success, zero if failed.
2421
+ */
2422
+ WASM_RUNTIME_API_EXTERN uint64_t
2423
+ wasm_runtime_shared_heap_malloc(wasm_module_inst_t module_inst, uint64_t size,
2424
+ void **p_native_addr);
2425
+
2426
+ /**
2427
+ * Free the memory allocated from shared heap, or the non-preallocated shared
2428
+ * heap from the shared heap chain
2429
+ *
2430
+ * @param module_inst the module instance
2431
+ * @param ptr the offset in wasm app
2432
+ */
2433
+ WASM_RUNTIME_API_EXTERN void
2434
+ wasm_runtime_shared_heap_free(wasm_module_inst_t module_inst, uint64_t ptr);
2435
+
2436
+ #ifdef __cplusplus
2437
+ }
2438
+ #endif
2439
+
2440
+ #endif /* end of _WASM_EXPORT_H */