wasm_runtime.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_RUNTIME_H
  6. #define _WASM_RUNTIME_H
  7. #include "wasm.h"
  8. #include "bh_atomic.h"
  9. #include "bh_bitmap.h"
  10. #include "bh_hashmap.h"
  11. #include "../common/wasm_runtime_common.h"
  12. #include "../common/wasm_exec_env.h"
  13. #if WASM_ENABLE_WASI_NN != 0
  14. #include "../libraries/wasi-nn/src/wasi_nn_private.h"
  15. #endif
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define EXCEPTION_BUF_LEN 128
  20. typedef struct WASMModuleInstance WASMModuleInstance;
  21. typedef struct WASMFunctionInstance WASMFunctionInstance;
  22. typedef struct WASMMemoryInstance WASMMemoryInstance;
  23. typedef struct WASMTableInstance WASMTableInstance;
  24. typedef struct WASMGlobalInstance WASMGlobalInstance;
  25. #if WASM_ENABLE_TAGS != 0
  26. typedef struct WASMTagInstance WASMTagInstance;
  27. #endif
  28. /**
  29. * When LLVM JIT, WAMR compiler or AOT is enabled, we should ensure that
  30. * some offsets of the same field in the interpreter module instance and
  31. * aot module instance are the same, so that the LLVM JITed/AOTed code
  32. * can smoothly access the interpreter module instance.
  33. * Same for the memory instance and table instance.
  34. * We use the macro DefPointer to define some related pointer fields.
  35. */
  36. #if (WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 \
  37. || WASM_ENABLE_AOT != 0) \
  38. && UINTPTR_MAX == UINT32_MAX
  39. /* Add u32 padding if LLVM JIT, WAMR compiler or AOT is enabled on
  40. 32-bit platform */
  41. #define DefPointer(type, field) \
  42. type field; \
  43. uint32 field##_padding
  44. #else
  45. #define DefPointer(type, field) type field
  46. #endif
  47. typedef enum WASMExceptionID {
  48. EXCE_UNREACHABLE = 0,
  49. EXCE_OUT_OF_MEMORY,
  50. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS,
  51. EXCE_INTEGER_OVERFLOW,
  52. EXCE_INTEGER_DIVIDE_BY_ZERO,
  53. EXCE_INVALID_CONVERSION_TO_INTEGER,
  54. EXCE_INVALID_FUNCTION_TYPE_INDEX,
  55. EXCE_INVALID_FUNCTION_INDEX,
  56. EXCE_UNDEFINED_ELEMENT,
  57. EXCE_UNINITIALIZED_ELEMENT,
  58. EXCE_CALL_UNLINKED_IMPORT_FUNC,
  59. EXCE_NATIVE_STACK_OVERFLOW,
  60. EXCE_UNALIGNED_ATOMIC,
  61. EXCE_AUX_STACK_OVERFLOW,
  62. EXCE_AUX_STACK_UNDERFLOW,
  63. EXCE_OUT_OF_BOUNDS_TABLE_ACCESS,
  64. EXCE_OPERAND_STACK_OVERFLOW,
  65. EXCE_FAILED_TO_COMPILE_FAST_JIT_FUNC,
  66. /* GC related exceptions */
  67. EXCE_NULL_FUNC_OBJ,
  68. EXCE_NULL_STRUCT_OBJ,
  69. EXCE_NULL_ARRAY_OBJ,
  70. EXCE_NULL_I31_OBJ,
  71. EXCE_NULL_REFERENCE,
  72. EXCE_FAILED_TO_CREATE_RTT_TYPE,
  73. EXCE_FAILED_TO_CREATE_STRUCT_OBJ,
  74. EXCE_FAILED_TO_CREATE_ARRAY_OBJ,
  75. EXCE_FAILED_TO_CREATE_EXTERNREF_OBJ,
  76. EXCE_CAST_FAILURE,
  77. EXCE_ARRAY_IDX_OOB,
  78. EXCE_FAILED_TO_CREATE_STRING,
  79. EXCE_FAILED_TO_CREATE_STRINGREF,
  80. EXCE_FAILED_TO_CREATE_STRINGVIEW,
  81. EXCE_FAILED_TO_ENCODE_STRING,
  82. EXCE_ALREADY_THROWN,
  83. EXCE_NUM,
  84. } WASMExceptionID;
  85. typedef union {
  86. uint64 u64;
  87. uint32 u32[2];
  88. } MemBound;
  89. struct WASMMemoryInstance {
  90. /* Module type */
  91. uint32 module_type;
  92. /* Whether the memory is shared */
  93. uint8 is_shared_memory;
  94. /* One byte padding */
  95. uint8 __padding__;
  96. /* Reference count of the memory instance:
  97. 0: non-shared memory, > 0: shared memory */
  98. bh_atomic_16_t ref_count;
  99. /* Number bytes per page */
  100. uint32 num_bytes_per_page;
  101. /* Current page count */
  102. uint32 cur_page_count;
  103. /* Maximum page count */
  104. uint32 max_page_count;
  105. /* Memory data size */
  106. uint32 memory_data_size;
  107. /**
  108. * Memory data begin address, Note:
  109. * the app-heap might be inserted in to the linear memory,
  110. * when memory is re-allocated, the heap data and memory data
  111. * must be copied to new memory also
  112. */
  113. DefPointer(uint8 *, memory_data);
  114. /* Memory data end address */
  115. DefPointer(uint8 *, memory_data_end);
  116. /* Heap data base address */
  117. DefPointer(uint8 *, heap_data);
  118. /* Heap data end address */
  119. DefPointer(uint8 *, heap_data_end);
  120. /* The heap created */
  121. DefPointer(void *, heap_handle);
  122. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  123. || WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_AOT != 0
  124. MemBound mem_bound_check_1byte;
  125. MemBound mem_bound_check_2bytes;
  126. MemBound mem_bound_check_4bytes;
  127. MemBound mem_bound_check_8bytes;
  128. MemBound mem_bound_check_16bytes;
  129. #endif
  130. };
  131. /* WASMTableInstance is used to represent table instance in
  132. * runtime, to compute the table element address with index
  133. * we need to know the element type and the element ref type.
  134. * For pointer type, it's 32-bit or 64-bit, align up to 8 bytes
  135. * to simplify the computation.
  136. * And each struct member should be 4-byte or 8-byte aligned.
  137. */
  138. struct WASMTableInstance {
  139. /* The element type */
  140. uint8 elem_type;
  141. uint8 __padding__[7];
  142. union {
  143. #if WASM_ENABLE_GC != 0
  144. WASMRefType *elem_ref_type;
  145. #endif
  146. uint64 __padding__;
  147. } elem_ref_type;
  148. /* Current size */
  149. uint32 cur_size;
  150. /* Maximum size */
  151. uint32 max_size;
  152. /* Table elements */
  153. table_elem_type_t elems[1];
  154. };
  155. struct WASMGlobalInstance {
  156. /* value type, VALUE_TYPE_I32/I64/F32/F64 */
  157. uint8 type;
  158. /* mutable or constant */
  159. bool is_mutable;
  160. /* data offset to base_addr of WASMMemoryInstance */
  161. uint32 data_offset;
  162. /* initial value */
  163. WASMValue initial_value;
  164. #if WASM_ENABLE_GC != 0
  165. WASMRefType *ref_type;
  166. #endif
  167. #if WASM_ENABLE_MULTI_MODULE != 0
  168. /* just for import, keep the reference here */
  169. WASMModuleInstance *import_module_inst;
  170. WASMGlobalInstance *import_global_inst;
  171. #endif
  172. };
  173. struct WASMFunctionInstance {
  174. /* whether it is import function or WASM function */
  175. bool is_import_func;
  176. /* parameter count */
  177. uint16 param_count;
  178. /* local variable count, 0 for import function */
  179. uint16 local_count;
  180. /* cell num of parameters */
  181. uint16 param_cell_num;
  182. /* cell num of return type */
  183. uint16 ret_cell_num;
  184. /* cell num of local variables, 0 for import function */
  185. uint16 local_cell_num;
  186. #if WASM_ENABLE_FAST_INTERP != 0
  187. /* cell num of consts */
  188. uint16 const_cell_num;
  189. #endif
  190. uint16 *local_offsets;
  191. /* parameter types */
  192. uint8 *param_types;
  193. /* local types, NULL for import function */
  194. uint8 *local_types;
  195. union {
  196. WASMFunctionImport *func_import;
  197. WASMFunction *func;
  198. } u;
  199. #if WASM_ENABLE_MULTI_MODULE != 0
  200. WASMModuleInstance *import_module_inst;
  201. WASMFunctionInstance *import_func_inst;
  202. #endif
  203. #if WASM_ENABLE_PERF_PROFILING != 0
  204. /* total execution time */
  205. uint64 total_exec_time;
  206. /* total execution count */
  207. uint32 total_exec_cnt;
  208. /* children execution time */
  209. uint64 children_exec_time;
  210. #endif
  211. };
  212. #if WASM_ENABLE_TAGS != 0
  213. struct WASMTagInstance {
  214. bool is_import_tag;
  215. /* tag attribute */
  216. uint8 attribute;
  217. /* tag type index */
  218. uint32 type;
  219. union {
  220. WASMTagImport *tag_import;
  221. WASMTag *tag;
  222. } u;
  223. #if WASM_ENABLE_MULTI_MODULE != 0
  224. WASMModuleInstance *import_module_inst;
  225. WASMTagInstance *import_tag_inst;
  226. #endif
  227. };
  228. #endif
  229. #if WASM_ENABLE_EXCE_HANDLING != 0
  230. #define INVALID_TAGINDEX ((uint32)0xFFFFFFFF)
  231. #define SET_INVALID_TAGINDEX(tag) (tag = INVALID_TAGINDEX)
  232. #define IS_INVALID_TAGINDEX(tag) ((tag & INVALID_TAGINDEX) == INVALID_TAGINDEX)
  233. #endif
  234. typedef struct WASMExportFuncInstance {
  235. char *name;
  236. WASMFunctionInstance *function;
  237. } WASMExportFuncInstance;
  238. typedef struct WASMExportGlobInstance {
  239. char *name;
  240. WASMGlobalInstance *global;
  241. } WASMExportGlobInstance;
  242. typedef struct WASMExportTabInstance {
  243. char *name;
  244. WASMTableInstance *table;
  245. } WASMExportTabInstance;
  246. typedef struct WASMExportMemInstance {
  247. char *name;
  248. WASMMemoryInstance *memory;
  249. } WASMExportMemInstance;
  250. #if WASM_ENABLE_TAGS != 0
  251. typedef struct WASMExportTagInstance {
  252. char *name;
  253. WASMTagInstance *tag;
  254. } WASMExportTagInstance;
  255. #endif
  256. /* wasm-c-api import function info */
  257. typedef struct CApiFuncImport {
  258. /* host func pointer after linked */
  259. void *func_ptr_linked;
  260. /* whether the host func has env argument */
  261. bool with_env_arg;
  262. /* the env argument of the host func */
  263. void *env_arg;
  264. } CApiFuncImport;
  265. /* The common part of WASMModuleInstanceExtra and AOTModuleInstanceExtra */
  266. typedef struct WASMModuleInstanceExtraCommon {
  267. CApiFuncImport *c_api_func_imports;
  268. void *contexts[WASM_MAX_INSTANCE_CONTEXTS];
  269. /* pointer to the exec env currently used */
  270. WASMExecEnv *cur_exec_env;
  271. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  272. /* Disable bounds checks or not */
  273. bool disable_bounds_checks;
  274. #endif
  275. #if WASM_ENABLE_BULK_MEMORY != 0
  276. bh_bitmap *data_dropped;
  277. #endif
  278. #if WASM_ENABLE_REF_TYPES != 0
  279. bh_bitmap *elem_dropped;
  280. #endif
  281. #if WASM_ENABLE_GC != 0
  282. /* The gc heap memory pool */
  283. uint8 *gc_heap_pool;
  284. /* The gc heap created */
  285. void *gc_heap_handle;
  286. #endif
  287. } WASMModuleInstanceExtraCommon;
  288. /* Extra info of WASM module instance for interpreter/jit mode */
  289. typedef struct WASMModuleInstanceExtra {
  290. WASMModuleInstanceExtraCommon common;
  291. WASMGlobalInstance *globals;
  292. WASMFunctionInstance *functions;
  293. uint32 global_count;
  294. uint32 function_count;
  295. WASMFunctionInstance *start_function;
  296. WASMFunctionInstance *malloc_function;
  297. WASMFunctionInstance *free_function;
  298. WASMFunctionInstance *retain_function;
  299. RunningMode running_mode;
  300. #if WASM_ENABLE_MULTI_MODULE != 0
  301. bh_list sub_module_inst_list_head;
  302. bh_list *sub_module_inst_list;
  303. /* linked table instances of import table instances */
  304. WASMTableInstance **table_insts_linked;
  305. #endif
  306. #if WASM_ENABLE_TAGS != 0
  307. uint32 tag_count;
  308. uint32 export_tag_count;
  309. WASMTagInstance *tags;
  310. WASMExportTagInstance *export_tags;
  311. void **import_tag_ptrs;
  312. #endif
  313. #if WASM_ENABLE_MEMORY_PROFILING != 0
  314. uint32 max_aux_stack_used;
  315. #endif
  316. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  317. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  318. && WASM_ENABLE_LAZY_JIT != 0)
  319. WASMModuleInstance *next;
  320. #endif
  321. } WASMModuleInstanceExtra;
  322. struct AOTFuncPerfProfInfo;
  323. struct WASMModuleInstance {
  324. /* Module instance type, for module instance loaded from
  325. WASM bytecode binary, this field is Wasm_Module_Bytecode;
  326. for module instance loaded from AOT file, this field is
  327. Wasm_Module_AoT, and this structure should be treated as
  328. AOTModuleInstance structure. */
  329. uint32 module_type;
  330. uint32 memory_count;
  331. DefPointer(WASMMemoryInstance **, memories);
  332. /* global and table info */
  333. uint32 global_data_size;
  334. uint32 table_count;
  335. DefPointer(uint8 *, global_data);
  336. /* For AOTModuleInstance, it denotes `AOTTableInstance *` */
  337. DefPointer(WASMTableInstance **, tables);
  338. /* import func ptrs + llvm jit func ptrs */
  339. DefPointer(void **, func_ptrs);
  340. /* function type indexes */
  341. DefPointer(uint32 *, func_type_indexes);
  342. uint32 export_func_count;
  343. uint32 export_global_count;
  344. uint32 export_memory_count;
  345. uint32 export_table_count;
  346. /* For AOTModuleInstance, it denotes `AOTFunctionInstance *` */
  347. DefPointer(WASMExportFuncInstance *, export_functions);
  348. DefPointer(WASMExportGlobInstance *, export_globals);
  349. DefPointer(WASMExportMemInstance *, export_memories);
  350. DefPointer(WASMExportTabInstance *, export_tables);
  351. /* The exception buffer of wasm interpreter for current thread. */
  352. char cur_exception[EXCEPTION_BUF_LEN];
  353. /* The WASM module or AOT module, for AOTModuleInstance,
  354. it denotes `AOTModule *` */
  355. DefPointer(WASMModule *, module);
  356. DefPointer(void *, used_to_be_wasi_ctx); /* unused */
  357. DefPointer(WASMExecEnv *, exec_env_singleton);
  358. /* Array of function pointers to import functions,
  359. not available in AOTModuleInstance */
  360. DefPointer(void **, import_func_ptrs);
  361. /* Array of function pointers to fast jit functions,
  362. not available in AOTModuleInstance:
  363. Only when the multi-tier JIT macros are all enabled and the running
  364. mode of current module instance is set to Mode_Fast_JIT, runtime
  365. will allocate new memory for it, otherwise it always points to the
  366. module->fast_jit_func_ptrs */
  367. DefPointer(void **, fast_jit_func_ptrs);
  368. /* The custom data that can be set/get by wasm_{get|set}_custom_data */
  369. DefPointer(void *, custom_data);
  370. /* Stack frames, used in call stack dump and perf profiling */
  371. DefPointer(Vector *, frames);
  372. /* Function performance profiling info list, only available
  373. in AOTModuleInstance */
  374. DefPointer(struct AOTFuncPerfProfInfo *, func_perf_profilings);
  375. /* WASM/AOT module extra info, for AOTModuleInstance,
  376. it denotes `AOTModuleInstanceExtra *` */
  377. DefPointer(WASMModuleInstanceExtra *, e);
  378. /* Default WASM operand stack size */
  379. uint32 default_wasm_stack_size;
  380. uint32 reserved[3];
  381. /*
  382. * +------------------------------+ <-- memories
  383. * | WASMMemoryInstance[mem_count], mem_count is always 1 for LLVM JIT/AOT
  384. * +------------------------------+ <-- global_data
  385. * | global data
  386. * +------------------------------+ <-- tables
  387. * | WASMTableInstance[table_count]
  388. * +------------------------------+ <-- e
  389. * | WASMModuleInstanceExtra
  390. * +------------------------------+
  391. */
  392. union {
  393. uint64 _make_it_8_byte_aligned_;
  394. WASMMemoryInstance memory_instances[1];
  395. uint8 bytes[1];
  396. } global_table_data;
  397. };
  398. struct WASMInterpFrame;
  399. typedef struct WASMInterpFrame WASMRuntimeFrame;
  400. #if WASM_ENABLE_MULTI_MODULE != 0
  401. typedef struct WASMSubModInstNode {
  402. bh_list_link l;
  403. /* point to a string pool */
  404. const char *module_name;
  405. WASMModuleInstance *module_inst;
  406. } WASMSubModInstNode;
  407. #endif
  408. /**
  409. * Return the code block of a function.
  410. *
  411. * @param func the WASM function instance
  412. *
  413. * @return the code block of the function
  414. */
  415. static inline uint8 *
  416. wasm_get_func_code(WASMFunctionInstance *func)
  417. {
  418. #if WASM_ENABLE_FAST_INTERP == 0
  419. return func->is_import_func ? NULL : func->u.func->code;
  420. #else
  421. return func->is_import_func ? NULL : func->u.func->code_compiled;
  422. #endif
  423. }
  424. /**
  425. * Return the code block end of a function.
  426. *
  427. * @param func the WASM function instance
  428. *
  429. * @return the code block end of the function
  430. */
  431. static inline uint8 *
  432. wasm_get_func_code_end(WASMFunctionInstance *func)
  433. {
  434. #if WASM_ENABLE_FAST_INTERP == 0
  435. return func->is_import_func ? NULL
  436. : func->u.func->code + func->u.func->code_size;
  437. #else
  438. return func->is_import_func
  439. ? NULL
  440. : func->u.func->code_compiled + func->u.func->code_compiled_size;
  441. #endif
  442. }
  443. WASMModule *
  444. wasm_load(uint8 *buf, uint32 size,
  445. #if WASM_ENABLE_MULTI_MODULE != 0
  446. bool main_module,
  447. #endif
  448. char *error_buf, uint32 error_buf_size);
  449. WASMModule *
  450. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  451. uint32 error_buf_size);
  452. void
  453. wasm_unload(WASMModule *module);
  454. WASMModuleInstance *
  455. wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
  456. WASMExecEnv *exec_env_main, uint32 stack_size,
  457. uint32 heap_size, char *error_buf, uint32 error_buf_size);
  458. void
  459. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst);
  460. double
  461. wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst);
  462. double
  463. wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst,
  464. const char *func_name);
  465. void
  466. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst);
  467. bool
  468. wasm_set_running_mode(WASMModuleInstance *module_inst,
  469. RunningMode running_mode);
  470. WASMFunctionInstance *
  471. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  472. const char *signature);
  473. #if WASM_ENABLE_MULTI_MODULE != 0
  474. WASMGlobalInstance *
  475. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name);
  476. WASMMemoryInstance *
  477. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name);
  478. WASMTableInstance *
  479. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name);
  480. #if WASM_ENABLE_TAGS != 0
  481. WASMTagInstance *
  482. wasm_lookup_tag(const WASMModuleInstance *module_inst, const char *name,
  483. const char *signature);
  484. #endif
  485. #endif
  486. bool
  487. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  488. unsigned argc, uint32 argv[]);
  489. void
  490. wasm_set_exception(WASMModuleInstance *module, const char *exception);
  491. void
  492. wasm_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id);
  493. const char *
  494. wasm_get_exception(WASMModuleInstance *module);
  495. /**
  496. * @brief Copy exception in buffer passed as parameter. Thread-safe version of
  497. * `wasm_get_exception()`
  498. * @note Buffer size must be no smaller than EXCEPTION_BUF_LEN
  499. * @return true if exception found
  500. */
  501. bool
  502. wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf);
  503. uint32
  504. wasm_module_malloc_internal(WASMModuleInstance *module_inst,
  505. WASMExecEnv *exec_env, uint32 size,
  506. void **p_native_addr);
  507. uint32
  508. wasm_module_realloc_internal(WASMModuleInstance *module_inst,
  509. WASMExecEnv *exec_env, uint32 ptr, uint32 size,
  510. void **p_native_addr);
  511. void
  512. wasm_module_free_internal(WASMModuleInstance *module_inst,
  513. WASMExecEnv *exec_env, uint32 ptr);
  514. uint32
  515. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  516. void **p_native_addr);
  517. uint32
  518. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  519. void **p_native_addr);
  520. void
  521. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr);
  522. uint32
  523. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  524. uint32 size);
  525. /**
  526. * Check whether the app address and the buf is inside the linear memory,
  527. * and convert the app address into native address
  528. */
  529. bool
  530. wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  531. uint32 app_buf_addr, uint32 app_buf_size,
  532. void **p_native_addr);
  533. WASMMemoryInstance *
  534. wasm_get_default_memory(WASMModuleInstance *module_inst);
  535. bool
  536. wasm_enlarge_memory(WASMModuleInstance *module_inst, uint32 inc_page_count);
  537. bool
  538. wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  539. uint32 argc, uint32 argv[]);
  540. #if WASM_ENABLE_THREAD_MGR != 0
  541. bool
  542. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size);
  543. bool
  544. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size);
  545. #endif
  546. void
  547. wasm_get_module_mem_consumption(const WASMModule *module,
  548. WASMModuleMemConsumption *mem_conspn);
  549. void
  550. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module,
  551. WASMModuleInstMemConsumption *mem_conspn);
  552. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  553. static inline bool
  554. wasm_elem_is_active(uint32 mode)
  555. {
  556. return (mode & 0x1) == 0x0;
  557. }
  558. static inline bool
  559. wasm_elem_is_passive(uint32 mode)
  560. {
  561. return (mode & 0x1) == 0x1;
  562. }
  563. static inline bool
  564. wasm_elem_is_declarative(uint32 mode)
  565. {
  566. return (mode & 0x3) == 0x3;
  567. }
  568. bool
  569. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  570. uint32 inc_entries, table_elem_type_t init_val);
  571. #endif /* WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  572. #if WASM_ENABLE_GC != 0
  573. void *
  574. wasm_create_func_obj(WASMModuleInstance *module_inst, uint32 func_idx,
  575. bool throw_exce, char *error_buf, uint32 error_buf_size);
  576. bool
  577. wasm_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap);
  578. #endif
  579. static inline WASMTableInstance *
  580. wasm_get_table_inst(const WASMModuleInstance *module_inst, uint32 tbl_idx)
  581. {
  582. /* careful, it might be a table in another module */
  583. WASMTableInstance *tbl_inst = module_inst->tables[tbl_idx];
  584. #if WASM_ENABLE_MULTI_MODULE != 0
  585. if (tbl_idx < module_inst->module->import_table_count
  586. && module_inst->e->table_insts_linked[tbl_idx]) {
  587. tbl_inst = module_inst->e->table_insts_linked[tbl_idx];
  588. }
  589. #endif
  590. bh_assert(tbl_inst);
  591. return tbl_inst;
  592. }
  593. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  594. bool
  595. wasm_interp_create_call_stack(struct WASMExecEnv *exec_env);
  596. /**
  597. * @brief Dump wasm call stack or get the size
  598. *
  599. * @param exec_env the execution environment
  600. * @param print whether to print to stdout or not
  601. * @param buf buffer to store the dumped content
  602. * @param len length of the buffer
  603. *
  604. * @return when print is true, return the bytes printed out to stdout; when
  605. * print is false and buf is NULL, return the size required to store the
  606. * callstack content; when print is false and buf is not NULL, return the size
  607. * dumped to the buffer, 0 means error and data in buf may be invalid
  608. */
  609. uint32
  610. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
  611. uint32 len);
  612. #endif
  613. const uint8 *
  614. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  615. uint32 *len);
  616. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  617. || WASM_ENABLE_WAMR_COMPILER != 0
  618. void
  619. jit_set_exception_with_id(WASMModuleInstance *module_inst, uint32 id);
  620. /**
  621. * Check whether the app address and the buf is inside the linear memory,
  622. * and convert the app address into native address
  623. */
  624. bool
  625. jit_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
  626. uint32 app_buf_addr, uint32 app_buf_size,
  627. void **p_native_addr);
  628. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  629. || WASM_ENABLE_WAMR_COMPILER != 0 */
  630. #if WASM_ENABLE_FAST_JIT != 0
  631. bool
  632. fast_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  633. uint32 type_idx, uint32 argc, uint32 *argv);
  634. bool
  635. fast_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
  636. struct WASMInterpFrame *prev_frame);
  637. #endif
  638. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  639. bool
  640. llvm_jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  641. uint32 argc, uint32 *argv);
  642. bool
  643. llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
  644. uint32 *argv);
  645. #if WASM_ENABLE_BULK_MEMORY != 0
  646. bool
  647. llvm_jit_memory_init(WASMModuleInstance *module_inst, uint32 seg_index,
  648. uint32 offset, uint32 len, uint32 dst);
  649. bool
  650. llvm_jit_data_drop(WASMModuleInstance *module_inst, uint32 seg_index);
  651. #endif
  652. #if WASM_ENABLE_REF_TYPES != 0
  653. void
  654. llvm_jit_drop_table_seg(WASMModuleInstance *module_inst, uint32 tbl_seg_idx);
  655. void
  656. llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
  657. uint32 tbl_seg_idx, uint32 length, uint32 src_offset,
  658. uint32 dst_offset);
  659. void
  660. llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
  661. uint32 dst_tbl_idx, uint32 length, uint32 src_offset,
  662. uint32 dst_offset);
  663. void
  664. llvm_jit_table_fill(WASMModuleInstance *module_inst, uint32 tbl_idx,
  665. uint32 length, uintptr_t val, uint32 data_offset);
  666. uint32
  667. llvm_jit_table_grow(WASMModuleInstance *module_inst, uint32 tbl_idx,
  668. uint32 inc_entries, uintptr_t init_val);
  669. #endif
  670. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0 \
  671. || WASM_ENABLE_AOT_STACK_FRAME != 0
  672. bool
  673. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index);
  674. void
  675. llvm_jit_free_frame(WASMExecEnv *exec_env);
  676. void
  677. llvm_jit_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame);
  678. #endif
  679. #if WASM_ENABLE_GC != 0
  680. void *
  681. llvm_jit_create_func_obj(WASMModuleInstance *module_inst, uint32 func_idx,
  682. bool throw_exce, char *error_buf,
  683. uint32 error_buf_size);
  684. bool
  685. llvm_jit_obj_is_instance_of(WASMModuleInstance *module_inst,
  686. WASMObjectRef gc_obj, uint32 type_index);
  687. WASMRttTypeRef
  688. llvm_jit_rtt_type_new(WASMModuleInstance *module_inst, uint32 type_index);
  689. bool
  690. llvm_array_init_with_data(WASMModuleInstance *module_inst, uint32 seg_index,
  691. uint32 data_seg_offset, WASMArrayObjectRef array_obj,
  692. uint32 elem_size, uint32 array_len);
  693. #endif
  694. #endif /* end of WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0 */
  695. #if WASM_ENABLE_LIBC_WASI != 0 && WASM_ENABLE_MULTI_MODULE != 0
  696. void
  697. wasm_propagate_wasi_args(WASMModule *module);
  698. #endif
  699. #if WASM_ENABLE_THREAD_MGR != 0
  700. void
  701. exception_lock(WASMModuleInstance *module_inst);
  702. void
  703. exception_unlock(WASMModuleInstance *module_inst);
  704. #else
  705. #define exception_lock(module_inst) (void)(module_inst)
  706. #define exception_unlock(module_inst) (void)(module_inst)
  707. #endif
  708. #ifdef __cplusplus
  709. }
  710. #endif
  711. #endif /* end of _WASM_RUNTIME_H */