wasm_runtime.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. typedef 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 base offset of wasm app */
  31. int32 heap_base_offset;
  32. /* Heap data base address */
  33. uint8 *heap_data;
  34. /* The heap created */
  35. void *heap_handle;
  36. /* Memory data */
  37. uint8 *memory_data;
  38. /* End address of memory */
  39. uint8 *end_addr;
  40. #if WASM_ENABLE_MULTI_MODULE != 0
  41. /* to indicate which module instance create it */
  42. WASMModuleInstance *owner;
  43. #endif
  44. /* Base address, the layout is:
  45. heap_data + memory data
  46. memory data init size is: num_bytes_per_page * cur_page_count
  47. Note: when memory is re-allocated, the heap data and memory data
  48. must be copied to new memory also.
  49. */
  50. uint8 base_addr[1];
  51. } WASMMemoryInstance;
  52. typedef struct WASMTableInstance {
  53. /* The element type, TABLE_ELEM_TYPE_ANY_FUNC currently */
  54. uint8 elem_type;
  55. /* Current size */
  56. uint32 cur_size;
  57. /* Maximum size */
  58. uint32 max_size;
  59. #if WASM_ENABLE_MULTI_MODULE != 0
  60. /* just for import, keep the reference here */
  61. WASMTableInstance *table_inst_linked;
  62. #endif
  63. /* Base address */
  64. uint8 base_addr[1];
  65. } WASMTableInstance;
  66. typedef struct WASMGlobalInstance {
  67. /* value type, VALUE_TYPE_I32/I64/F32/F64 */
  68. uint8 type;
  69. /* mutable or constant */
  70. bool is_mutable;
  71. /* data offset to base_addr of WASMMemoryInstance */
  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. } WASMGlobalInstance;
  81. typedef struct WASMFunctionInstance {
  82. /* whether it is import function or WASM function */
  83. bool is_import_func;
  84. /* parameter count */
  85. uint16 param_count;
  86. /* local variable count, 0 for import function */
  87. uint16 local_count;
  88. /* cell num of parameters */
  89. uint16 param_cell_num;
  90. /* cell num of return type */
  91. uint16 ret_cell_num;
  92. /* cell num of local variables, 0 for import function */
  93. uint16 local_cell_num;
  94. #if WASM_ENABLE_FAST_INTERP != 0
  95. /* cell num of consts */
  96. uint16 const_cell_num;
  97. #endif
  98. uint16 *local_offsets;
  99. /* parameter types */
  100. uint8 *param_types;
  101. /* local types, NULL for import function */
  102. uint8 *local_types;
  103. union {
  104. WASMFunctionImport *func_import;
  105. WASMFunction *func;
  106. } u;
  107. #if WASM_ENABLE_MULTI_MODULE != 0
  108. WASMModuleInstance *import_module_inst;
  109. WASMFunctionInstance *import_func_inst;
  110. #endif
  111. } WASMFunctionInstance;
  112. typedef struct WASMExportFuncInstance {
  113. char *name;
  114. WASMFunctionInstance *function;
  115. } WASMExportFuncInstance;
  116. #if WASM_ENABLE_MULTI_MODULE != 0
  117. typedef struct WASMExportGlobInstance {
  118. char *name;
  119. WASMGlobalInstance *global;
  120. } WASMExportGlobInstance;
  121. typedef struct WASMExportTabInstance {
  122. char *name;
  123. WASMTableInstance *table;
  124. } WASMExportTabInstance;
  125. typedef struct WASMExportMemInstance {
  126. char *name;
  127. WASMMemoryInstance *memory;
  128. } WASMExportMemInstance;
  129. #endif
  130. typedef struct WASMModuleInstance {
  131. /* Module instance type, for module instance loaded from
  132. WASM bytecode binary, this field is Wasm_Module_Bytecode;
  133. for module instance loaded from AOT file, this field is
  134. Wasm_Module_AoT, and this structure should be treated as
  135. AOTModuleInstance structure. */
  136. uint32 module_type;
  137. uint32 memory_count;
  138. uint32 table_count;
  139. uint32 global_count;
  140. uint32 function_count;
  141. uint32 export_func_count;
  142. #if WASM_ENABLE_MULTI_MODULE != 0
  143. uint32 export_glob_count;
  144. uint32 export_mem_count;
  145. uint32 export_tab_count;
  146. #endif
  147. WASMMemoryInstance **memories;
  148. WASMTableInstance **tables;
  149. WASMGlobalInstance *globals;
  150. WASMFunctionInstance *functions;
  151. WASMExportFuncInstance *export_functions;
  152. #if WASM_ENABLE_MULTI_MODULE != 0
  153. WASMExportGlobInstance *export_globals;
  154. WASMExportMemInstance *export_memories;
  155. WASMExportTabInstance *export_tables;
  156. #endif
  157. WASMMemoryInstance *default_memory;
  158. WASMTableInstance *default_table;
  159. /* Global data of global instances */
  160. uint8 *global_data;
  161. WASMFunctionInstance *start_function;
  162. WASMModule *module;
  163. #if WASM_ENABLE_LIBC_WASI != 0
  164. WASIContext *wasi_ctx;
  165. #endif
  166. uint32 temp_ret;
  167. uint32 llvm_stack;
  168. /* Default WASM stack size of threads of this Module instance. */
  169. uint32 default_wasm_stack_size;
  170. /* The exception buffer of wasm interpreter for current thread. */
  171. char cur_exception[128];
  172. /* The custom data that can be set/get by
  173. * wasm_set_custom_data/wasm_get_custom_data */
  174. void *custom_data;
  175. /* Main exec env */
  176. WASMExecEnv *main_exec_env;
  177. #if WASM_ENABLE_MULTI_MODULE != 0
  178. // TODO: mutex ? mutli-threads ?
  179. bh_list sub_module_inst_list_head;
  180. bh_list *sub_module_inst_list;
  181. #endif
  182. } WASMModuleInstance;
  183. struct WASMInterpFrame;
  184. typedef struct WASMInterpFrame WASMRuntimeFrame;
  185. #if WASM_ENABLE_MULTI_MODULE != 0
  186. typedef struct WASMSubModInstNode {
  187. bh_list_link l;
  188. /* point to a string pool */
  189. const char *module_name;
  190. WASMModuleInstance *module_inst;
  191. } WASMSubModInstNode;
  192. #endif
  193. /**
  194. * Return the code block of a function.
  195. *
  196. * @param func the WASM function instance
  197. *
  198. * @return the code block of the function
  199. */
  200. static inline uint8*
  201. wasm_get_func_code(WASMFunctionInstance *func)
  202. {
  203. #if WASM_ENABLE_FAST_INTERP == 0
  204. return func->is_import_func ? NULL : func->u.func->code;
  205. #else
  206. return func->is_import_func ? NULL : func->u.func->code_compiled;
  207. #endif
  208. }
  209. /**
  210. * Return the code block end of a function.
  211. *
  212. * @param func the WASM function instance
  213. *
  214. * @return the code block end of the function
  215. */
  216. static inline uint8*
  217. wasm_get_func_code_end(WASMFunctionInstance *func)
  218. {
  219. #if WASM_ENABLE_FAST_INTERP == 0
  220. return func->is_import_func
  221. ? NULL : func->u.func->code + func->u.func->code_size;
  222. #else
  223. return func->is_import_func
  224. ? NULL
  225. : func->u.func->code_compiled + func->u.func->code_compiled_size;
  226. #endif
  227. }
  228. WASMModule *
  229. wasm_load(const uint8 *buf, uint32 size,
  230. char *error_buf, uint32 error_buf_size);
  231. WASMModule *
  232. wasm_load_from_sections(WASMSection *section_list,
  233. char *error_buf, uint32_t error_buf_size);
  234. void
  235. wasm_unload(WASMModule *module);
  236. WASMModuleInstance *
  237. wasm_instantiate(WASMModule *module, bool is_sub_inst,
  238. uint32 stack_size, uint32 heap_size,
  239. char *error_buf, uint32 error_buf_size);
  240. void
  241. wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst);
  242. WASMFunctionInstance *
  243. wasm_lookup_function(const WASMModuleInstance *module_inst,
  244. const char *name, const char *signature);
  245. #if WASM_ENABLE_MULTI_MODULE != 0
  246. WASMGlobalInstance *
  247. wasm_lookup_global(const WASMModuleInstance *module_inst, const char *name);
  248. WASMMemoryInstance *
  249. wasm_lookup_memory(const WASMModuleInstance *module_inst, const char *name);
  250. WASMTableInstance *
  251. wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name);
  252. #endif
  253. bool
  254. wasm_call_function(WASMExecEnv *exec_env,
  255. WASMFunctionInstance *function,
  256. unsigned argc, uint32 argv[]);
  257. bool
  258. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  259. WASMFunctionInstance *function,
  260. unsigned argc, uint32 argv[]);
  261. void
  262. wasm_set_exception(WASMModuleInstance *module, const char *exception);
  263. const char*
  264. wasm_get_exception(WASMModuleInstance *module);
  265. int32
  266. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  267. void **p_native_addr);
  268. void
  269. wasm_module_free(WASMModuleInstance *module_inst, int32 ptr);
  270. int32
  271. wasm_module_dup_data(WASMModuleInstance *module_inst,
  272. const char *src, uint32 size);
  273. bool
  274. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  275. int32 app_offset, uint32 size);
  276. bool
  277. wasm_validate_app_str_addr(WASMModuleInstance *module_inst,
  278. int32 app_offset);
  279. bool
  280. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  281. void *native_ptr, uint32 size);
  282. void *
  283. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  284. int32 app_offset);
  285. int32
  286. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  287. void *native_ptr);
  288. bool
  289. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  290. int32 app_offset,
  291. int32 *p_app_start_offset,
  292. int32 *p_app_end_offset);
  293. bool
  294. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  295. uint8_t *native_ptr,
  296. uint8_t **p_native_start_addr,
  297. uint8_t **p_native_end_addr);
  298. bool
  299. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count);
  300. bool
  301. wasm_call_indirect(WASMExecEnv *exec_env,
  302. uint32_t element_indices,
  303. uint32_t argc, uint32_t argv[]);
  304. #if WASM_ENABLE_THREAD_MGR != 0
  305. bool
  306. wasm_set_aux_stack(WASMExecEnv *exec_env,
  307. uint32 start_offset, uint32 size);
  308. bool
  309. wasm_get_aux_stack(WASMExecEnv *exec_env,
  310. uint32 *start_offset, uint32 *size);
  311. #endif
  312. #ifdef __cplusplus
  313. }
  314. #endif
  315. #endif /* end of _WASM_RUNTIME_H */