wasm_runtime.h 31 KB

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