wasm_runtime.h 25 KB

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