wasm_runtime.h 27 KB

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