wasm_runtime.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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, TABLE_ELEM_TYPE_ANY_FUNC 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. uint32 data_offset;
  72. /* initial value */
  73. WASMValue initial_value;
  74. #if WASM_ENABLE_MULTI_MODULE != 0
  75. /* just for import, keep the reference here */
  76. WASMModuleInstance *import_module_inst;
  77. WASMGlobalInstance *import_global_inst;
  78. #endif
  79. };
  80. struct WASMFunctionInstance {
  81. /* whether it is import function or WASM function */
  82. bool is_import_func;
  83. /* parameter count */
  84. uint16 param_count;
  85. /* local variable count, 0 for import function */
  86. uint16 local_count;
  87. /* cell num of parameters */
  88. uint16 param_cell_num;
  89. /* cell num of return type */
  90. uint16 ret_cell_num;
  91. /* cell num of local variables, 0 for import function */
  92. uint16 local_cell_num;
  93. #if WASM_ENABLE_FAST_INTERP != 0
  94. /* cell num of consts */
  95. uint16 const_cell_num;
  96. #endif
  97. uint16 *local_offsets;
  98. /* parameter types */
  99. uint8 *param_types;
  100. /* local types, NULL for import function */
  101. uint8 *local_types;
  102. union {
  103. WASMFunctionImport *func_import;
  104. WASMFunction *func;
  105. } u;
  106. #if WASM_ENABLE_MULTI_MODULE != 0
  107. WASMModuleInstance *import_module_inst;
  108. WASMFunctionInstance *import_func_inst;
  109. #endif
  110. };
  111. typedef struct WASMExportFuncInstance {
  112. char *name;
  113. WASMFunctionInstance *function;
  114. } WASMExportFuncInstance;
  115. #if WASM_ENABLE_MULTI_MODULE != 0
  116. typedef struct WASMExportGlobInstance {
  117. char *name;
  118. WASMGlobalInstance *global;
  119. } WASMExportGlobInstance;
  120. typedef struct WASMExportTabInstance {
  121. char *name;
  122. WASMTableInstance *table;
  123. } WASMExportTabInstance;
  124. typedef struct WASMExportMemInstance {
  125. char *name;
  126. WASMMemoryInstance *memory;
  127. } WASMExportMemInstance;
  128. #endif
  129. struct WASMModuleInstance {
  130. /* Module instance type, for module instance loaded from
  131. WASM bytecode binary, this field is Wasm_Module_Bytecode;
  132. for module instance loaded from AOT file, this field is
  133. Wasm_Module_AoT, and this structure should be treated as
  134. AOTModuleInstance structure. */
  135. uint32 module_type;
  136. uint32 memory_count;
  137. uint32 table_count;
  138. uint32 global_count;
  139. uint32 function_count;
  140. uint32 export_func_count;
  141. #if WASM_ENABLE_MULTI_MODULE != 0
  142. uint32 export_glob_count;
  143. uint32 export_mem_count;
  144. uint32 export_tab_count;
  145. #endif
  146. WASMMemoryInstance **memories;
  147. WASMTableInstance **tables;
  148. WASMGlobalInstance *globals;
  149. WASMFunctionInstance *functions;
  150. WASMExportFuncInstance *export_functions;
  151. #if WASM_ENABLE_MULTI_MODULE != 0
  152. WASMExportGlobInstance *export_globals;
  153. WASMExportMemInstance *export_memories;
  154. WASMExportTabInstance *export_tables;
  155. #endif
  156. WASMMemoryInstance *default_memory;
  157. WASMTableInstance *default_table;
  158. /* Global data of global instances */
  159. uint8 *global_data;
  160. WASMFunctionInstance *start_function;
  161. WASMFunctionInstance *malloc_function;
  162. WASMFunctionInstance *free_function;
  163. WASMFunctionInstance *retain_function;
  164. WASMModule *module;
  165. #if WASM_ENABLE_LIBC_WASI != 0
  166. WASIContext *wasi_ctx;
  167. #endif
  168. uint32 temp_ret;
  169. uint32 llvm_stack;
  170. /* Default WASM stack size of threads of this Module instance. */
  171. uint32 default_wasm_stack_size;
  172. /* The exception buffer of wasm interpreter for current thread. */
  173. char cur_exception[128];
  174. /* The custom data that can be set/get by
  175. * wasm_set_custom_data/wasm_get_custom_data */
  176. void *custom_data;
  177. #if WASM_ENABLE_MULTI_MODULE != 0
  178. /* TODO: add mutex for mutli-threads? */
  179. bh_list sub_module_inst_list_head;
  180. bh_list *sub_module_inst_list;
  181. #endif
  182. #if WASM_ENABLE_MEMORY_PROFILING != 0
  183. uint32 max_aux_stack_used;
  184. #endif
  185. };
  186. struct WASMInterpFrame;
  187. typedef struct WASMInterpFrame WASMRuntimeFrame;
  188. #if WASM_ENABLE_MULTI_MODULE != 0
  189. typedef struct WASMSubModInstNode {
  190. bh_list_link l;
  191. /* point to a string pool */
  192. const char *module_name;
  193. WASMModuleInstance *module_inst;
  194. } WASMSubModInstNode;
  195. #endif
  196. /**
  197. * Return the code block of a function.
  198. *
  199. * @param func the WASM function instance
  200. *
  201. * @return the code block of the function
  202. */
  203. static inline uint8*
  204. wasm_get_func_code(WASMFunctionInstance *func)
  205. {
  206. #if WASM_ENABLE_FAST_INTERP == 0
  207. return func->is_import_func ? NULL : func->u.func->code;
  208. #else
  209. return func->is_import_func ? NULL : func->u.func->code_compiled;
  210. #endif
  211. }
  212. /**
  213. * Return the code block end of a function.
  214. *
  215. * @param func the WASM function instance
  216. *
  217. * @return the code block end of the function
  218. */
  219. static inline uint8*
  220. wasm_get_func_code_end(WASMFunctionInstance *func)
  221. {
  222. #if WASM_ENABLE_FAST_INTERP == 0
  223. return func->is_import_func
  224. ? NULL : func->u.func->code + func->u.func->code_size;
  225. #else
  226. return func->is_import_func
  227. ? NULL
  228. : func->u.func->code_compiled + func->u.func->code_compiled_size;
  229. #endif
  230. }
  231. WASMModule *
  232. wasm_load(const uint8 *buf, uint32 size,
  233. char *error_buf, uint32 error_buf_size);
  234. WASMModule *
  235. wasm_load_from_sections(WASMSection *section_list,
  236. char *error_buf, uint32_t error_buf_size);
  237. void
  238. wasm_unload(WASMModule *module);
  239. WASMModuleInstance *
  240. wasm_instantiate(WASMModule *module, bool is_sub_inst,
  241. uint32 stack_size, uint32 heap_size,
  242. char *error_buf, uint32 error_buf_size);
  243. void
  244. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst);
  245. WASMFunctionInstance *
  246. wasm_lookup_function(const WASMModuleInstance *module_inst,
  247. const char *name, const char *signature);
  248. #if WASM_ENABLE_MULTI_MODULE != 0
  249. WASMGlobalInstance *
  250. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name);
  251. WASMMemoryInstance *
  252. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name);
  253. WASMTableInstance *
  254. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name);
  255. #endif
  256. bool
  257. wasm_call_function(WASMExecEnv *exec_env,
  258. WASMFunctionInstance *function,
  259. unsigned argc, uint32 argv[]);
  260. bool
  261. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  262. WASMFunctionInstance *function,
  263. unsigned argc, uint32 argv[]);
  264. void
  265. wasm_set_exception(WASMModuleInstance *module, const char *exception);
  266. const char*
  267. wasm_get_exception(WASMModuleInstance *module);
  268. uint32
  269. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  270. void **p_native_addr);
  271. void
  272. wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr);
  273. uint32
  274. wasm_module_dup_data(WASMModuleInstance *module_inst,
  275. const char *src, uint32 size);
  276. bool
  277. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  278. uint32 app_offset, uint32 size);
  279. bool
  280. wasm_validate_app_str_addr(WASMModuleInstance *module_inst,
  281. uint32 app_offset);
  282. bool
  283. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  284. void *native_ptr, uint32 size);
  285. void *
  286. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  287. uint32 app_offset);
  288. uint32
  289. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  290. void *native_ptr);
  291. bool
  292. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  293. uint32 app_offset,
  294. uint32 *p_app_start_offset,
  295. uint32 *p_app_end_offset);
  296. bool
  297. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  298. uint8_t *native_ptr,
  299. uint8_t **p_native_start_addr,
  300. uint8_t **p_native_end_addr);
  301. bool
  302. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count);
  303. bool
  304. wasm_call_indirect(WASMExecEnv *exec_env,
  305. uint32_t element_indices,
  306. uint32_t argc, uint32_t argv[]);
  307. #if WASM_ENABLE_THREAD_MGR != 0
  308. bool
  309. wasm_set_aux_stack(WASMExecEnv *exec_env,
  310. uint32 start_offset, uint32 size);
  311. bool
  312. wasm_get_aux_stack(WASMExecEnv *exec_env,
  313. uint32 *start_offset, uint32 *size);
  314. #endif
  315. void
  316. wasm_get_module_mem_consumption(const WASMModule *module,
  317. WASMModuleMemConsumption *mem_conspn);
  318. void
  319. wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module,
  320. WASMModuleInstMemConsumption *mem_conspn);
  321. #ifdef __cplusplus
  322. }
  323. #endif
  324. #endif /* end of _WASM_RUNTIME_H */