wasm_runtime.h 22 KB

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