wasm_runtime.h 7.3 KB

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