wasm_runtime.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 WASMMemoryInstance {
  15. /* Number bytes per page */
  16. uint32 num_bytes_per_page;
  17. /* Current page count */
  18. uint32 cur_page_count;
  19. /* Maximum page count */
  20. uint32 max_page_count;
  21. /* Heap base offset of wasm app */
  22. int32 heap_base_offset;
  23. /* Heap data base address */
  24. uint8 *heap_data;
  25. /* The heap created */
  26. void *heap_handle;
  27. /* Memory data */
  28. uint8 *memory_data;
  29. /* Global data of global instances */
  30. uint8 *global_data;
  31. uint32 global_data_size;
  32. /* End address of memory */
  33. uint8 *end_addr;
  34. /* Base address, the layout is:
  35. heap_data + memory data + global data
  36. memory data init size is: num_bytes_per_page * cur_page_count
  37. global data size is calculated in module instantiating
  38. Note: when memory is re-allocated, the heap data and memory data
  39. must be copied to new memory also.
  40. */
  41. uint8 base_addr[1];
  42. } WASMMemoryInstance;
  43. typedef struct WASMTableInstance {
  44. /* The element type, TABLE_ELEM_TYPE_ANY_FUNC currently */
  45. uint8 elem_type;
  46. /* Current size */
  47. uint32 cur_size;
  48. /* Maximum size */
  49. uint32 max_size;
  50. /* Base address */
  51. uint8 base_addr[1];
  52. } WASMTableInstance;
  53. typedef struct WASMGlobalInstance {
  54. /* value type, VALUE_TYPE_I32/I64/F32/F64 */
  55. uint8 type;
  56. /* mutable or constant */
  57. bool is_mutable;
  58. /* data offset to base_addr of WASMMemoryInstance */
  59. uint32 data_offset;
  60. /* initial value */
  61. WASMValue initial_value;
  62. } WASMGlobalInstance;
  63. typedef struct WASMFunctionInstance {
  64. /* whether it is import function or WASM function */
  65. bool is_import_func;
  66. /* parameter count */
  67. uint16 param_count;
  68. /* local variable count, 0 for import function */
  69. uint16 local_count;
  70. /* cell num of parameters */
  71. uint16 param_cell_num;
  72. /* cell num of return type */
  73. uint16 ret_cell_num;
  74. /* cell num of local variables, 0 for import function */
  75. uint16 local_cell_num;
  76. #if WASM_ENABLE_FAST_INTERP != 0
  77. /* cell num of consts */
  78. uint16 const_cell_num;
  79. #endif
  80. uint16 *local_offsets;
  81. /* parameter types */
  82. uint8 *param_types;
  83. /* local types, NULL for import function */
  84. uint8 *local_types;
  85. union {
  86. WASMFunctionImport *func_import;
  87. WASMFunction *func;
  88. } u;
  89. } WASMFunctionInstance;
  90. typedef struct WASMExportFuncInstance {
  91. char *name;
  92. WASMFunctionInstance *function;
  93. } WASMExportFuncInstance;
  94. typedef struct WASMModuleInstance {
  95. /* Module instance type, for module instance loaded from
  96. WASM bytecode binary, this field is Wasm_Module_Bytecode;
  97. for module instance loaded from AOT file, this field is
  98. Wasm_Module_AoT, and this structure should be treated as
  99. AOTModuleInstance structure. */
  100. uint32 module_type;
  101. uint32 memory_count;
  102. uint32 table_count;
  103. uint32 global_count;
  104. uint32 function_count;
  105. uint32 export_func_count;
  106. WASMMemoryInstance **memories;
  107. WASMTableInstance **tables;
  108. WASMGlobalInstance *globals;
  109. WASMFunctionInstance *functions;
  110. WASMExportFuncInstance *export_functions;
  111. WASMMemoryInstance *default_memory;
  112. WASMTableInstance *default_table;
  113. WASMFunctionInstance *start_function;
  114. WASMModule *module;
  115. #if WASM_ENABLE_LIBC_WASI != 0
  116. WASIContext *wasi_ctx;
  117. #endif
  118. uint32 DYNAMICTOP_PTR_offset;
  119. uint32 temp_ret;
  120. uint32 llvm_stack;
  121. /* Default WASM stack size of threads of this Module instance. */
  122. uint32 default_wasm_stack_size;
  123. /* The exception buffer of wasm interpreter for current thread. */
  124. char cur_exception[128];
  125. /* The custom data that can be set/get by
  126. * wasm_set_custom_data/wasm_get_custom_data */
  127. void *custom_data;
  128. /* Main exec env */
  129. WASMExecEnv *main_exec_env;
  130. } WASMModuleInstance;
  131. struct WASMInterpFrame;
  132. typedef struct WASMInterpFrame WASMRuntimeFrame;
  133. /**
  134. * Return the code block of a function.
  135. *
  136. * @param func the WASM function instance
  137. *
  138. * @return the code block of the function
  139. */
  140. static inline uint8*
  141. wasm_get_func_code(WASMFunctionInstance *func)
  142. {
  143. #if WASM_ENABLE_FAST_INTERP == 0
  144. return func->is_import_func ? NULL : func->u.func->code;
  145. #else
  146. return func->is_import_func ? NULL : func->u.func->code_compiled;
  147. #endif
  148. }
  149. /**
  150. * Return the code block end of a function.
  151. *
  152. * @param func the WASM function instance
  153. *
  154. * @return the code block end of the function
  155. */
  156. static inline uint8*
  157. wasm_get_func_code_end(WASMFunctionInstance *func)
  158. {
  159. #if WASM_ENABLE_FAST_INTERP == 0
  160. return func->is_import_func
  161. ? NULL : func->u.func->code + func->u.func->code_size;
  162. #else
  163. return func->is_import_func
  164. ? NULL : func->u.func->code_compiled + func->u.func->code_compiled_size;
  165. #endif
  166. }
  167. WASMModule *
  168. wasm_load(const uint8 *buf, uint32 size,
  169. char *error_buf, uint32 error_buf_size);
  170. WASMModule *
  171. wasm_load_from_sections(WASMSection *section_list,
  172. char *error_buf, uint32_t error_buf_size);
  173. void
  174. wasm_unload(WASMModule *module);
  175. WASMModuleInstance *
  176. wasm_instantiate(WASMModule *module,
  177. uint32 stack_size, uint32 heap_size,
  178. char *error_buf, uint32 error_buf_size);
  179. void
  180. wasm_deinstantiate(WASMModuleInstance *module_inst);
  181. WASMFunctionInstance *
  182. wasm_lookup_function(const WASMModuleInstance *module_inst,
  183. const char *name, const char *signature);
  184. bool
  185. wasm_call_function(WASMExecEnv *exec_env,
  186. WASMFunctionInstance *function,
  187. unsigned argc, uint32 argv[]);
  188. bool
  189. wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
  190. WASMFunctionInstance *function,
  191. unsigned argc, uint32 argv[]);
  192. void
  193. wasm_set_exception(WASMModuleInstance *module, const char *exception);
  194. const char*
  195. wasm_get_exception(WASMModuleInstance *module);
  196. int32
  197. wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
  198. void **p_native_addr);
  199. void
  200. wasm_module_free(WASMModuleInstance *module_inst, int32 ptr);
  201. int32
  202. wasm_module_dup_data(WASMModuleInstance *module_inst,
  203. const char *src, uint32 size);
  204. bool
  205. wasm_validate_app_addr(WASMModuleInstance *module_inst,
  206. int32 app_offset, uint32 size);
  207. bool
  208. wasm_validate_app_str_addr(WASMModuleInstance *module_inst,
  209. int32 app_offset);
  210. bool
  211. wasm_validate_native_addr(WASMModuleInstance *module_inst,
  212. void *native_ptr, uint32 size);
  213. void *
  214. wasm_addr_app_to_native(WASMModuleInstance *module_inst,
  215. int32 app_offset);
  216. int32
  217. wasm_addr_native_to_app(WASMModuleInstance *module_inst,
  218. void *native_ptr);
  219. bool
  220. wasm_get_app_addr_range(WASMModuleInstance *module_inst,
  221. int32 app_offset,
  222. int32 *p_app_start_offset,
  223. int32 *p_app_end_offset);
  224. bool
  225. wasm_get_native_addr_range(WASMModuleInstance *module_inst,
  226. uint8_t *native_ptr,
  227. uint8_t **p_native_start_addr,
  228. uint8_t **p_native_end_addr);
  229. bool
  230. wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count);
  231. bool
  232. wasm_call_indirect(WASMExecEnv *exec_env,
  233. uint32_t element_indices,
  234. uint32_t argc, uint32_t argv[]);
  235. #ifdef __cplusplus
  236. }
  237. #endif
  238. #endif /* end of _WASM_RUNTIME_H */