wasm_runtime.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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_hashmap.h"
  9. #include "../common/wasm_runtime_common.h"
  10. #include "../common/wasm_exec_env.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. typedef struct WASMModuleInstance WASMModuleInstance;
  15. typedef struct WASMFunctionInstance WASMFunctionInstance;
  16. typedef struct WASMMemoryInstance WASMMemoryInstance;
  17. typedef struct WASMTableInstance WASMTableInstance;
  18. typedef struct WASMGlobalInstance WASMGlobalInstance;
  19. struct WASMMemoryInstance {
  20. /* Module type */
  21. uint32 module_type;
  22. /* Shared memory flag */
  23. bool is_shared;
  24. /* Number bytes per page */
  25. uint32 num_bytes_per_page;
  26. /* Current page count */
  27. uint32 cur_page_count;
  28. /* Maximum page count */
  29. uint32 max_page_count;
  30. /* Heap data base address */
  31. uint8 *heap_data;
  32. /* Heap data end address */
  33. uint8 *heap_data_end;
  34. /* The heap created */
  35. void *heap_handle;
  36. #if WASM_ENABLE_SHARED_MEMORY != 0
  37. /* mutex lock for the memory, used in atomic operation */
  38. korp_mutex mem_lock;
  39. #endif
  40. /* Memory data end address */
  41. uint8 *memory_data_end;
  42. /* Memory data begin address, the layout is: memory data + heap data
  43. Note: when memory is re-allocated, the heap data and memory data
  44. must be copied to new memory also. */
  45. uint8 *memory_data;
  46. #if WASM_ENABLE_FAST_JIT != 0
  47. #if UINTPTR_MAX == UINT64_MAX
  48. uint64 mem_bound_check_1byte;
  49. uint64 mem_bound_check_2bytes;
  50. uint64 mem_bound_check_4bytes;
  51. uint64 mem_bound_check_8bytes;
  52. uint64 mem_bound_check_16bytes;
  53. #else
  54. uint32 mem_bound_check_1byte;
  55. uint32 mem_bound_check_2bytes;
  56. uint32 mem_bound_check_4bytes;
  57. uint32 mem_bound_check_8bytes;
  58. uint32 mem_bound_check_16bytes;
  59. #endif
  60. #endif
  61. };
  62. struct WASMTableInstance {
  63. /* The element type, VALUE_TYPE_FUNCREF/EXTERNREF currently */
  64. uint8 elem_type;
  65. /* Current size */
  66. uint32 cur_size;
  67. /* Maximum size */
  68. uint32 max_size;
  69. #if WASM_ENABLE_MULTI_MODULE != 0
  70. /* just for import, keep the reference here */
  71. WASMTableInstance *table_inst_linked;
  72. #endif
  73. /* Base address */
  74. uint8 base_addr[1];
  75. };
  76. struct WASMGlobalInstance {
  77. /* value type, VALUE_TYPE_I32/I64/F32/F64 */
  78. uint8 type;
  79. /* mutable or constant */
  80. bool is_mutable;
  81. /* data offset to base_addr of WASMMemoryInstance */
  82. uint32 data_offset;
  83. /* initial value */
  84. WASMValue initial_value;
  85. #if WASM_ENABLE_MULTI_MODULE != 0
  86. /* just for import, keep the reference here */
  87. WASMModuleInstance *import_module_inst;
  88. WASMGlobalInstance *import_global_inst;
  89. #endif
  90. };
  91. struct WASMFunctionInstance {
  92. /* whether it is import function or WASM function */
  93. bool is_import_func;
  94. /* parameter count */
  95. uint16 param_count;
  96. /* local variable count, 0 for import function */
  97. uint16 local_count;
  98. /* cell num of parameters */
  99. uint16 param_cell_num;
  100. /* cell num of return type */
  101. uint16 ret_cell_num;
  102. /* cell num of local variables, 0 for import function */
  103. uint16 local_cell_num;
  104. #if WASM_ENABLE_FAST_INTERP != 0
  105. /* cell num of consts */
  106. uint16 const_cell_num;
  107. #endif
  108. uint16 *local_offsets;
  109. /* parameter types */
  110. uint8 *param_types;
  111. /* local types, NULL for import function */
  112. uint8 *local_types;
  113. union {
  114. WASMFunctionImport *func_import;
  115. WASMFunction *func;
  116. } u;
  117. #if WASM_ENABLE_MULTI_MODULE != 0
  118. WASMModuleInstance *import_module_inst;
  119. WASMFunctionInstance *import_func_inst;
  120. #endif
  121. #if WASM_ENABLE_PERF_PROFILING != 0
  122. /* total execution time */
  123. uint64 total_exec_time;
  124. /* total execution count */
  125. uint32 total_exec_cnt;
  126. #endif
  127. };
  128. typedef struct WASMExportFuncInstance {
  129. char *name;
  130. WASMFunctionInstance *function;
  131. } WASMExportFuncInstance;
  132. #if WASM_ENABLE_MULTI_MODULE != 0
  133. typedef struct WASMExportGlobInstance {
  134. char *name;
  135. WASMGlobalInstance *global;
  136. } WASMExportGlobInstance;
  137. typedef struct WASMExportTabInstance {
  138. char *name;
  139. WASMTableInstance *table;
  140. } WASMExportTabInstance;
  141. typedef struct WASMExportMemInstance {
  142. char *name;
  143. WASMMemoryInstance *memory;
  144. } WASMExportMemInstance;
  145. #endif
  146. struct WASMModuleInstance {
  147. /* Module instance type, for module instance loaded from
  148. WASM bytecode binary, this field is Wasm_Module_Bytecode;
  149. for module instance loaded from AOT file, this field is
  150. Wasm_Module_AoT, and this structure should be treated as
  151. AOTModuleInstance structure. */
  152. uint32 module_type;
  153. uint32 memory_count;
  154. uint32 table_count;
  155. uint32 global_count;
  156. uint32 function_count;
  157. uint32 export_func_count;
  158. #if WASM_ENABLE_MULTI_MODULE != 0
  159. uint32 export_glob_count;
  160. uint32 export_mem_count;
  161. uint32 export_tab_count;
  162. #endif
  163. /* Array of function pointers to import functions */
  164. void **import_func_ptrs;
  165. #if WASM_ENABLE_FAST_JIT != 0
  166. /* point to JITed functions */
  167. void **fast_jit_func_ptrs;
  168. #endif
  169. WASMMemoryInstance **memories;
  170. WASMTableInstance **tables;
  171. WASMGlobalInstance *globals;
  172. WASMFunctionInstance *functions;
  173. WASMExportFuncInstance *export_functions;
  174. #if WASM_ENABLE_MULTI_MODULE != 0
  175. WASMExportGlobInstance *export_globals;
  176. WASMExportMemInstance *export_memories;
  177. WASMExportTabInstance *export_tables;
  178. #endif
  179. WASMMemoryInstance *default_memory;
  180. WASMTableInstance *default_table;
  181. /* Global data of global instances */
  182. uint8 *global_data;
  183. WASMFunctionInstance *start_function;
  184. WASMFunctionInstance *malloc_function;
  185. WASMFunctionInstance *free_function;
  186. WASMFunctionInstance *retain_function;
  187. WASMModule *module;
  188. #if WASM_ENABLE_LIBC_WASI != 0
  189. WASIContext *wasi_ctx;
  190. #endif
  191. WASMExecEnv *exec_env_singleton;
  192. /* Default WASM stack size of threads of this Module instance. */
  193. uint32 default_wasm_stack_size;
  194. /* The exception buffer of wasm interpreter for current thread. */
  195. char cur_exception[128];
  196. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  197. Vector *frames;
  198. #endif
  199. /* The custom data that can be set/get by
  200. * wasm_set_custom_data/wasm_get_custom_data */
  201. void *custom_data;
  202. #if WASM_ENABLE_MULTI_MODULE != 0
  203. /* TODO: add mutex for mutli-threads? */
  204. bh_list sub_module_inst_list_head;
  205. bh_list *sub_module_inst_list;
  206. #endif
  207. #if WASM_ENABLE_MEMORY_PROFILING != 0
  208. uint32 max_aux_stack_used;
  209. #endif
  210. };
  211. struct WASMInterpFrame;
  212. typedef struct WASMInterpFrame WASMRuntimeFrame;
  213. #if WASM_ENABLE_MULTI_MODULE != 0
  214. typedef struct WASMSubModInstNode {
  215. bh_list_link l;
  216. /* point to a string pool */
  217. const char *module_name;
  218. WASMModuleInstance *module_inst;
  219. } WASMSubModInstNode;
  220. #endif
  221. /**
  222. * Return the code block of a function.
  223. *
  224. * @param func the WASM function instance
  225. *
  226. * @return the code block of the function
  227. */
  228. static inline uint8 *
  229. wasm_get_func_code(WASMFunctionInstance *func)
  230. {
  231. #if WASM_ENABLE_FAST_INTERP == 0
  232. return func->is_import_func ? NULL : func->u.func->code;
  233. #else
  234. return func->is_import_func ? NULL : func->u.func->code_compiled;
  235. #endif
  236. }
  237. /**
  238. * Return the code block end of a function.
  239. *
  240. * @param func the WASM function instance
  241. *
  242. * @return the code block end of the function
  243. */
  244. static inline uint8 *
  245. wasm_get_func_code_end(WASMFunctionInstance *func)
  246. {
  247. #if WASM_ENABLE_FAST_INTERP == 0
  248. return func->is_import_func ? NULL
  249. : func->u.func->code + func->u.func->code_size;
  250. #else
  251. return func->is_import_func
  252. ? NULL
  253. : func->u.func->code_compiled + func->u.func->code_compiled_size;
  254. #endif
  255. }
  256. WASMModule *
  257. wasm_load(uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size);
  258. WASMModule *
  259. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  260. uint32 error_buf_size);
  261. void
  262. wasm_unload(WASMModule *module);
  263. WASMModuleInstance *
  264. wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
  265. uint32 heap_size, char *error_buf, uint32 error_buf_size);
  266. void
  267. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst);
  268. void
  269. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst);
  270. WASMFunctionInstance *
  271. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  272. const char *signature);
  273. #if WASM_ENABLE_MULTI_MODULE != 0
  274. WASMGlobalInstance *
  275. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name);
  276. WASMMemoryInstance *
  277. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name);
  278. WASMTableInstance *
  279. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name);
  280. #endif
  281. bool
  282. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  283. unsigned argc, uint32 argv[]);
  284. bool
  285. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  286. WASMFunctionInstance *function,
  287. unsigned argc, uint32 argv[]);
  288. bool
  289. wasm_create_exec_env_singleton(WASMModuleInstance *module_inst);
  290. void
  291. wasm_set_exception(WASMModuleInstance *module, const char *exception);
  292. const char *
  293. wasm_get_exception(WASMModuleInstance *module);
  294. uint32
  295. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  296. void **p_native_addr);
  297. uint32
  298. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  299. void **p_native_addr);
  300. void
  301. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr);
  302. uint32
  303. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  304. uint32 size);
  305. bool
  306. wasm_validate_app_addr(WASMModuleInstance *module_inst, uint32 app_offset,
  307. uint32 size);
  308. bool
  309. wasm_validate_app_str_addr(WASMModuleInstance *module_inst, uint32 app_offset);
  310. bool
  311. wasm_validate_native_addr(WASMModuleInstance *module_inst, void *native_ptr,
  312. uint32 size);
  313. void *
  314. wasm_addr_app_to_native(WASMModuleInstance *module_inst, uint32 app_offset);
  315. uint32
  316. wasm_addr_native_to_app(WASMModuleInstance *module_inst, void *native_ptr);
  317. bool
  318. wasm_get_app_addr_range(WASMModuleInstance *module_inst, uint32 app_offset,
  319. uint32 *p_app_start_offset, uint32 *p_app_end_offset);
  320. bool
  321. wasm_get_native_addr_range(WASMModuleInstance *module_inst, uint8 *native_ptr,
  322. uint8 **p_native_start_addr,
  323. uint8 **p_native_end_addr);
  324. bool
  325. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count);
  326. bool
  327. wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  328. uint32 argc, uint32 argv[]);
  329. #if WASM_ENABLE_FAST_JIT != 0
  330. bool
  331. jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  332. uint32 type_idx, uint32 argc, uint32 argv[]);
  333. #endif
  334. #if WASM_ENABLE_THREAD_MGR != 0
  335. bool
  336. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size);
  337. bool
  338. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size);
  339. #endif
  340. #ifdef OS_ENABLE_HW_BOUND_CHECK
  341. #ifndef BH_PLATFORM_WINDOWS
  342. void
  343. wasm_signal_handler(WASMSignalInfo *sig_info);
  344. #else
  345. LONG
  346. wasm_exception_handler(WASMSignalInfo *sig_info);
  347. #endif
  348. #endif
  349. void
  350. wasm_get_module_mem_consumption(const WASMModule *module,
  351. WASMModuleMemConsumption *mem_conspn);
  352. void
  353. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module,
  354. WASMModuleInstMemConsumption *mem_conspn);
  355. #if WASM_ENABLE_REF_TYPES != 0
  356. static inline bool
  357. wasm_elem_is_active(uint32 mode)
  358. {
  359. return (mode & 0x1) == 0x0;
  360. }
  361. static inline bool
  362. wasm_elem_is_passive(uint32 mode)
  363. {
  364. return (mode & 0x1) == 0x1;
  365. }
  366. static inline bool
  367. wasm_elem_is_declarative(uint32 mode)
  368. {
  369. return (mode & 0x3) == 0x3;
  370. }
  371. bool
  372. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  373. uint32 inc_entries, uint32 init_val);
  374. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  375. static inline WASMTableInstance *
  376. wasm_get_table_inst(const WASMModuleInstance *module_inst, const uint32 tbl_idx)
  377. {
  378. /* careful, it might be a table in another module */
  379. WASMTableInstance *tbl_inst = module_inst->tables[tbl_idx];
  380. #if WASM_ENABLE_MULTI_MODULE != 0
  381. if (tbl_inst->table_inst_linked) {
  382. tbl_inst = tbl_inst->table_inst_linked;
  383. }
  384. #endif
  385. bh_assert(tbl_inst);
  386. return tbl_inst;
  387. }
  388. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  389. bool
  390. wasm_interp_create_call_stack(struct WASMExecEnv *exec_env);
  391. /**
  392. * @brief Dump wasm call stack or get the size
  393. *
  394. * @param exec_env the execution environment
  395. * @param print whether to print to stdout or not
  396. * @param buf buffer to store the dumped content
  397. * @param len length of the buffer
  398. *
  399. * @return when print is true, return the bytes printed out to stdout; when
  400. * print is false and buf is NULL, return the size required to store the
  401. * callstack content; when print is false and buf is not NULL, return the size
  402. * dumped to the buffer, 0 means error and data in buf may be invalid
  403. */
  404. uint32
  405. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
  406. uint32 len);
  407. #endif
  408. const uint8 *
  409. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  410. uint32 *len);
  411. #ifdef __cplusplus
  412. }
  413. #endif
  414. #endif /* end of _WASM_RUNTIME_H */