wasm_runtime.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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_MULTI_MODULE != 0
  37. /* to indicate which module instance create it */
  38. WASMModuleInstance *owner;
  39. #endif
  40. #if WASM_ENABLE_SHARED_MEMORY != 0
  41. /* mutex lock for the memory, used in atomic operation */
  42. korp_mutex mem_lock;
  43. #endif
  44. /* Memory data end address */
  45. uint8 *memory_data_end;
  46. /* Memory data begin address, the layout is: memory data + heap data
  47. Note: when memory is re-allocated, the heap data and memory data
  48. must be copied to new memory also. */
  49. uint8 *memory_data;
  50. };
  51. struct WASMTableInstance {
  52. /* The element type, VALUE_TYPE_FUNCREF/EXTERNREF currently */
  53. uint8 elem_type;
  54. /* Current size */
  55. uint32 cur_size;
  56. /* Maximum size */
  57. uint32 max_size;
  58. #if WASM_ENABLE_MULTI_MODULE != 0
  59. /* just for import, keep the reference here */
  60. WASMTableInstance *table_inst_linked;
  61. #endif
  62. /* Base address */
  63. uint8 base_addr[1];
  64. };
  65. struct WASMGlobalInstance {
  66. /* value type, VALUE_TYPE_I32/I64/F32/F64 */
  67. uint8 type;
  68. /* mutable or constant */
  69. bool is_mutable;
  70. /* data offset to base_addr of WASMMemoryInstance */
  71. uint8 * data;
  72. // uint32 data_offset;
  73. /* initial value */
  74. WASMValue initial_value;
  75. #if WASM_ENABLE_MULTI_MODULE != 0
  76. /* just for import, keep the reference here */
  77. WASMModuleInstance *import_module_inst;
  78. WASMGlobalInstance *import_global_inst;
  79. #endif
  80. };
  81. struct WASMFunctionInstance {
  82. /* the owner module instance */
  83. WASMModuleInstance * module_inst;
  84. /* whether it is import function or WASM function */
  85. bool is_import_func;
  86. /* function signature*/
  87. WASMType * func_type;
  88. /* local variable count, 0 for import function */
  89. uint16 local_count;
  90. /* cell num of local variables, 0 for import function */
  91. uint16 local_cell_num;
  92. #if WASM_ENABLE_FAST_INTERP != 0
  93. /* cell num of consts */
  94. uint16 const_cell_num;
  95. #endif
  96. uint16 *local_offsets;
  97. /* local types, NULL for import function */
  98. uint8 *local_types;
  99. union {
  100. WASMFunctionImport *func_import;
  101. WASMFunction *func;
  102. } u;
  103. #if WASM_ENABLE_MULTI_MODULE != 0 || WASM_ENABLE_DYNAMIC_LINKING != 0
  104. WASMModuleInstance *import_module_inst;
  105. WASMFunctionInstance *import_func_inst;
  106. #endif
  107. #if WASM_ENABLE_PERF_PROFILING != 0
  108. /* total execution time */
  109. uint64 total_exec_time;
  110. /* total execution count */
  111. uint32 total_exec_cnt;
  112. #endif
  113. };
  114. typedef struct WASMExportFuncInstance {
  115. const char * name;
  116. WASMFunctionInstance *function;
  117. } WASMExportFuncInstance;
  118. //#if WASM_ENABLE_MULTI_MODULE != 0
  119. typedef struct WASMExportGlobInstance {
  120. const char *name;
  121. WASMGlobalInstance *global;
  122. } WASMExportGlobInstance;
  123. typedef struct WASMExportTabInstance {
  124. char *name;
  125. WASMTableInstance *table;
  126. } WASMExportTabInstance;
  127. typedef struct WASMExportMemInstance {
  128. char *name;
  129. WASMMemoryInstance *memory;
  130. } WASMExportMemInstance;
  131. //#endif
  132. struct WASMModuleInstance {
  133. /* Module instance type, for module instance loaded from
  134. WASM bytecode binary, this field is Wasm_Module_Bytecode;
  135. for module instance loaded from AOT file, this field is
  136. Wasm_Module_AoT, and this structure should be treated as
  137. AOTModuleInstance structure. */
  138. uint32 module_type;
  139. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  140. //WASMModuleInstanceHead head;
  141. // unique instance id in program scope, used to alloc table space currently.
  142. uint32 inst_id;
  143. WASMRuntime * runtime;
  144. WASMProgramInstance * program;
  145. HashMap * local_implicit_dependency_modules_name_hmap;
  146. DependencyModuleInitGlobals init_globals;
  147. // explicit ref count, updated by dlopen/dlclose
  148. uint32 exp_ref_cnt;
  149. // implicit ref count, updated according to needed library entries
  150. uint32 imp_ref_cnt;
  151. #endif
  152. uint32 memory_count;
  153. uint32 table_count;
  154. uint32 global_count;
  155. uint32 function_count;
  156. uint32 export_func_count;
  157. #if WASM_ENABLE_MULTI_MODULE != 0 || WASM_ENABLE_DYNAMIC_LINKING != 0
  158. uint32 export_glob_count;
  159. uint32 export_mem_count;
  160. uint32 export_tab_count;
  161. #endif
  162. WASMMemoryInstance **memories;
  163. WASMTableInstance **tables;
  164. WASMGlobalInstance *globals;
  165. WASMFunctionInstance *functions;
  166. WASMExportFuncInstance *export_functions;
  167. #if WASM_ENABLE_MULTI_MODULE != 0 || WASM_ENABLE_DYNAMIC_LINKING != 0
  168. WASMExportGlobInstance *export_globals;
  169. WASMExportMemInstance *export_memories;
  170. WASMExportTabInstance *export_tables;
  171. #endif
  172. WASMMemoryInstance *default_memory;
  173. WASMTableInstance *default_table;
  174. /* Global data of global instances */
  175. uint8 *global_data;
  176. WASMFunctionInstance *start_function;
  177. WASMFunctionInstance *malloc_function;
  178. WASMFunctionInstance *free_function;
  179. WASMFunctionInstance *retain_function;
  180. WASMModule *module;
  181. #if WASM_ENABLE_LIBC_WASI != 0
  182. WASIContext *wasi_ctx;
  183. #endif
  184. WASMExecEnv *exec_env_singleton;
  185. uint32 temp_ret;
  186. uint32 llvm_stack;
  187. /* Default WASM stack size of threads of this Module instance. */
  188. uint32 default_wasm_stack_size;
  189. /* The exception buffer of wasm interpreter for current thread. */
  190. char cur_exception[128];
  191. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  192. Vector *frames;
  193. #endif
  194. /* The custom data that can be set/get by
  195. * wasm_set_custom_data/wasm_get_custom_data */
  196. void *custom_data;
  197. #if WASM_ENABLE_MULTI_MODULE != 0
  198. /* TODO: add mutex for mutli-threads? */
  199. bh_list sub_module_inst_list_head;
  200. bh_list *sub_module_inst_list;
  201. #endif
  202. #if WASM_ENABLE_MEMORY_PROFILING != 0
  203. uint32 max_aux_stack_used;
  204. #endif
  205. };
  206. struct WASMInterpFrame;
  207. typedef struct WASMInterpFrame WASMRuntimeFrame;
  208. #if WASM_ENABLE_MULTI_MODULE != 0
  209. typedef struct WASMSubModInstNode {
  210. bh_list_link l;
  211. /* point to a string pool */
  212. const char *module_name;
  213. WASMModuleInstance *module_inst;
  214. } WASMSubModInstNode;
  215. #endif
  216. /**
  217. * Return the code block of a function.
  218. *
  219. * @param func the WASM function instance
  220. *
  221. * @return the code block of the function
  222. */
  223. static inline uint8 *
  224. wasm_get_func_code(WASMFunctionInstance *func)
  225. {
  226. #if WASM_ENABLE_FAST_INTERP == 0
  227. return func->is_import_func ? NULL : func->u.func->code;
  228. #else
  229. return func->is_import_func ? NULL : func->u.func->code_compiled;
  230. #endif
  231. }
  232. /**
  233. * Return the code block end of a function.
  234. *
  235. * @param func the WASM function instance
  236. *
  237. * @return the code block end of the function
  238. */
  239. static inline uint8 *
  240. wasm_get_func_code_end(WASMFunctionInstance *func)
  241. {
  242. #if WASM_ENABLE_FAST_INTERP == 0
  243. return func->is_import_func ? NULL
  244. : func->u.func->code + func->u.func->code_size;
  245. #else
  246. return func->is_import_func
  247. ? NULL
  248. : func->u.func->code_compiled + func->u.func->code_compiled_size;
  249. #endif
  250. }
  251. WASMModule *
  252. wasm_load(const uint8 *buf, uint32 size, char *error_buf,
  253. uint32 error_buf_size);
  254. WASMModule *
  255. wasm_load_from_sections(WASMSection *section_list, char *error_buf,
  256. uint32_t error_buf_size);
  257. void
  258. wasm_unload(WASMModule *module);
  259. WASMModuleInstance *
  260. wasm_instantiate(WASMProgramInstance * program,
  261. WASMModule *module, bool is_sub_inst,
  262. uint32 stack_size, uint32 heap_size,
  263. char *error_buf, uint32 error_buf_size);
  264. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  265. WASMModuleInstance*
  266. wasm_instantiate_dependency(WASMModule *module, WASMProgramInstance * program,
  267. uint32 stack_size, DependencyModuleInitGlobals * init_globals);
  268. #endif
  269. void
  270. wasm_dump_perf_profiling(const WASMModuleInstance *module_inst);
  271. void
  272. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst);
  273. WASMFunctionInstance *
  274. wasm_lookup_function(const WASMModuleInstance *module_inst, const char *name,
  275. const char *signature);
  276. #if WASM_ENABLE_MULTI_MODULE != 0
  277. WASMGlobalInstance *
  278. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name);
  279. WASMMemoryInstance *
  280. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name);
  281. WASMTableInstance *
  282. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name);
  283. #endif
  284. bool
  285. wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
  286. unsigned argc, uint32 argv[]);
  287. bool
  288. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  289. WASMFunctionInstance *function,
  290. unsigned argc, uint32 argv[]);
  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_t *native_ptr,
  325. uint8_t **p_native_start_addr,
  326. uint8_t **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_t tbl_idx,
  331. uint32_t element_indices, uint32_t argc, uint32_t argv[]);
  332. #if WASM_ENABLE_THREAD_MGR != 0
  333. bool
  334. wasm_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset, uint32 size);
  335. bool
  336. wasm_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size);
  337. #endif
  338. void
  339. wasm_get_module_mem_consumption(const WASMModule *module,
  340. WASMModuleMemConsumption *mem_conspn);
  341. void
  342. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module,
  343. WASMModuleInstMemConsumption *mem_conspn);
  344. #if WASM_ENABLE_REF_TYPES != 0
  345. static inline bool
  346. wasm_elem_is_active(uint32 mode)
  347. {
  348. return (mode & 0x1) == 0x0;
  349. }
  350. static inline bool
  351. wasm_elem_is_passive(uint32 mode)
  352. {
  353. return (mode & 0x1) == 0x1;
  354. }
  355. static inline bool
  356. wasm_elem_is_declarative(uint32 mode)
  357. {
  358. return (mode & 0x3) == 0x3;
  359. }
  360. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  361. bool
  362. wasm_enlarge_table(WASMModuleInstance *module_inst, uint32 table_idx,
  363. uint32 inc_entries, uint32 init_val);
  364. static inline WASMTableInstance *
  365. wasm_get_table_inst(const WASMModuleInstance *module_inst, const uint32 tbl_idx)
  366. {
  367. /* careful, it might be a table in another module */
  368. WASMTableInstance *tbl_inst = module_inst->tables[tbl_idx];
  369. #if WASM_ENABLE_MULTI_MODULE != 0
  370. if (tbl_inst->table_inst_linked) {
  371. tbl_inst = tbl_inst->table_inst_linked;
  372. }
  373. #endif
  374. bh_assert(tbl_inst);
  375. return tbl_inst;
  376. }
  377. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  378. void
  379. wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env);
  380. #endif
  381. #ifdef __cplusplus
  382. }
  383. #endif
  384. #endif /* end of _WASM_RUNTIME_H */