wasm_runtime.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. uint32 temp_ret;
  193. uint32 llvm_stack;
  194. /* Default WASM stack size of threads of this Module instance. */
  195. uint32 default_wasm_stack_size;
  196. /* The exception buffer of wasm interpreter for current thread. */
  197. char cur_exception[128];
  198. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  199. Vector *frames;
  200. #endif
  201. /* The custom data that can be set/get by
  202. * wasm_set_custom_data/wasm_get_custom_data */
  203. void *custom_data;
  204. #if WASM_ENABLE_MULTI_MODULE != 0
  205. /* TODO: add mutex for mutli-threads? */
  206. bh_list sub_module_inst_list_head;
  207. bh_list *sub_module_inst_list;
  208. #endif
  209. #if WASM_ENABLE_MEMORY_PROFILING != 0
  210. uint32 max_aux_stack_used;
  211. #endif
  212. };
  213. struct WASMInterpFrame;
  214. typedef struct WASMInterpFrame WASMRuntimeFrame;
  215. #if WASM_ENABLE_MULTI_MODULE != 0
  216. typedef struct WASMSubModInstNode {
  217. bh_list_link l;
  218. /* point to a string pool */
  219. const char *module_name;
  220. WASMModuleInstance *module_inst;
  221. } WASMSubModInstNode;
  222. #endif
  223. /**
  224. * Return the code block of a function.
  225. *
  226. * @param func the WASM function instance
  227. *
  228. * @return the code block of the function
  229. */
  230. static inline uint8 *
  231. wasm_get_func_code(WASMFunctionInstance *func)
  232. {
  233. #if WASM_ENABLE_FAST_INTERP == 0
  234. return func->is_import_func ? NULL : func->u.func->code;
  235. #else
  236. return func->is_import_func ? NULL : func->u.func->code_compiled;
  237. #endif
  238. }
  239. /**
  240. * Return the code block end of a function.
  241. *
  242. * @param func the WASM function instance
  243. *
  244. * @return the code block end of the function
  245. */
  246. static inline uint8 *
  247. wasm_get_func_code_end(WASMFunctionInstance *func)
  248. {
  249. #if WASM_ENABLE_FAST_INTERP == 0
  250. return func->is_import_func ? NULL
  251. : func->u.func->code + func->u.func->code_size;
  252. #else
  253. return func->is_import_func
  254. ? NULL
  255. : func->u.func->code_compiled + func->u.func->code_compiled_size;
  256. #endif
  257. }
  258. WASMModule *
  259. wasm_load(uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size);
  260. WASMModule *
  261. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  262. uint32 error_buf_size);
  263. void
  264. wasm_unload(WASMModule *module);
  265. WASMModuleInstance *
  266. wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
  267. uint32 heap_size, char *error_buf, uint32 error_buf_size);
  268. void
  269. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst);
  270. void
  271. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst);
  272. WASMFunctionInstance *
  273. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  274. const char *signature);
  275. #if WASM_ENABLE_MULTI_MODULE != 0
  276. WASMGlobalInstance *
  277. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name);
  278. WASMMemoryInstance *
  279. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name);
  280. WASMTableInstance *
  281. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name);
  282. #endif
  283. bool
  284. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  285. unsigned argc, uint32 argv[]);
  286. bool
  287. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  288. WASMFunctionInstance *function,
  289. unsigned argc, uint32 argv[],
  290. bool enable_debug);
  291. bool
  292. wasm_create_exec_env_singleton(WASMModuleInstance *module_inst);
  293. void
  294. wasm_set_exception(WASMModuleInstance *module, const char *exception);
  295. const char *
  296. wasm_get_exception(WASMModuleInstance *module);
  297. uint32
  298. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  299. void **p_native_addr);
  300. uint32
  301. wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
  302. void **p_native_addr);
  303. void
  304. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr);
  305. uint32
  306. wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
  307. uint32 size);
  308. bool
  309. wasm_validate_app_addr(WASMModuleInstance *module_inst, uint32 app_offset,
  310. uint32 size);
  311. bool
  312. wasm_validate_app_str_addr(WASMModuleInstance *module_inst, uint32 app_offset);
  313. bool
  314. wasm_validate_native_addr(WASMModuleInstance *module_inst, void *native_ptr,
  315. uint32 size);
  316. void *
  317. wasm_addr_app_to_native(WASMModuleInstance *module_inst, uint32 app_offset);
  318. uint32
  319. wasm_addr_native_to_app(WASMModuleInstance *module_inst, void *native_ptr);
  320. bool
  321. wasm_get_app_addr_range(WASMModuleInstance *module_inst, uint32 app_offset,
  322. uint32 *p_app_start_offset, uint32 *p_app_end_offset);
  323. bool
  324. wasm_get_native_addr_range(WASMModuleInstance *module_inst, uint8 *native_ptr,
  325. uint8 **p_native_start_addr,
  326. uint8 **p_native_end_addr);
  327. bool
  328. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count);
  329. bool
  330. wasm_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  331. uint32 argc, uint32 argv[]);
  332. #if WASM_ENABLE_FAST_JIT != 0
  333. bool
  334. jit_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
  335. uint32 type_idx, uint32 argc, uint32 argv[]);
  336. #endif
  337. #if WASM_ENABLE_THREAD_MGR != 0
  338. bool
  339. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size);
  340. bool
  341. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size);
  342. #endif
  343. #ifdef OS_ENABLE_HW_BOUND_CHECK
  344. #ifndef BH_PLATFORM_WINDOWS
  345. void
  346. wasm_signal_handler(WASMSignalInfo *sig_info);
  347. #else
  348. LONG
  349. wasm_exception_handler(WASMSignalInfo *sig_info);
  350. #endif
  351. #endif
  352. void
  353. wasm_get_module_mem_consumption(const WASMModule *module,
  354. WASMModuleMemConsumption *mem_conspn);
  355. void
  356. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module,
  357. WASMModuleInstMemConsumption *mem_conspn);
  358. #if WASM_ENABLE_REF_TYPES != 0
  359. static inline bool
  360. wasm_elem_is_active(uint32 mode)
  361. {
  362. return (mode & 0x1) == 0x0;
  363. }
  364. static inline bool
  365. wasm_elem_is_passive(uint32 mode)
  366. {
  367. return (mode & 0x1) == 0x1;
  368. }
  369. static inline bool
  370. wasm_elem_is_declarative(uint32 mode)
  371. {
  372. return (mode & 0x3) == 0x3;
  373. }
  374. bool
  375. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  376. uint32 inc_entries, uint32 init_val);
  377. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  378. static inline WASMTableInstance *
  379. wasm_get_table_inst(const WASMModuleInstance *module_inst, const uint32 tbl_idx)
  380. {
  381. /* careful, it might be a table in another module */
  382. WASMTableInstance *tbl_inst = module_inst->tables[tbl_idx];
  383. #if WASM_ENABLE_MULTI_MODULE != 0
  384. if (tbl_inst->table_inst_linked) {
  385. tbl_inst = tbl_inst->table_inst_linked;
  386. }
  387. #endif
  388. bh_assert(tbl_inst);
  389. return tbl_inst;
  390. }
  391. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  392. bool
  393. wasm_interp_create_call_stack(struct WASMExecEnv *exec_env);
  394. /**
  395. * @brief Dump wasm call stack or get the size
  396. *
  397. * @param exec_env the execution environment
  398. * @param print whether to print to stdout or not
  399. * @param buf buffer to store the dumped content
  400. * @param len length of the buffer
  401. *
  402. * @return when print is true, return the bytes printed out to stdout; when
  403. * print is false and buf is NULL, return the size required to store the
  404. * callstack content; when print is false and buf is not NULL, return the size
  405. * dumped to the buffer, 0 means error and data in buf may be invalid
  406. */
  407. uint32
  408. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
  409. uint32 len);
  410. #endif
  411. const uint8 *
  412. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  413. uint32 *len);
  414. #ifdef __cplusplus
  415. }
  416. #endif
  417. #endif /* end of _WASM_RUNTIME_H */