wasm_runtime.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 "wasm_thread.h"
  9. #include "wasm_hashmap.h"
  10. #if WASM_ENABLE_WASI != 0
  11. #include "wasmtime_ssp.h"
  12. #include "posix.h"
  13. #endif
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef struct WASMMemoryInstance {
  18. /* Current page count */
  19. uint32 cur_page_count;
  20. /* Maximum page count */
  21. uint32 max_page_count;
  22. /* Data of import globals with address info, like _stdin/_stdout/_stderr,
  23. stdin/stdout/stderr is stored here, but the actual addr info, or offset
  24. to memory_data is stored in global_data section */
  25. uint8 *addr_data;
  26. /* Size of addr_data */
  27. uint32 addr_data_size;
  28. /* Heap data base address */
  29. uint8 *heap_data;
  30. /* Heap data end address */
  31. uint8 *heap_data_end;
  32. /* The heap created */
  33. void *heap_handle;
  34. /* Heap base offset of wasm app */
  35. int32 heap_base_offset;
  36. /* Memory data */
  37. uint8 *memory_data;
  38. /* Global data of global instances */
  39. uint8 *global_data;
  40. uint32 global_data_size;
  41. /* End address of memory */
  42. uint8 *end_addr;
  43. /* Base address, the layout is:
  44. addr_data + thunk_argv data + thunk arg offsets +
  45. memory data + global data
  46. memory data init size is: NumBytesPerPage * cur_page_count
  47. addr data size and global data size is calculated in module instantiating
  48. Note: when memory is re-allocated, the addr data, thunk argv data, thunk
  49. argv offsets and memory data must be copied to new memory also.
  50. */
  51. uint8 base_addr[1];
  52. } WASMMemoryInstance;
  53. typedef struct WASMTableInstance {
  54. /* The element type, TABLE_ELEM_TYPE_ANY_FUNC currently */
  55. uint8 elem_type;
  56. /* Current size */
  57. uint32 cur_size;
  58. /* Maximum size */
  59. uint32 max_size;
  60. /* Base address */
  61. uint8 base_addr[1];
  62. } WASMTableInstance;
  63. typedef struct WASMGlobalInstance {
  64. /* value type, VALUE_TYPE_I32/I64/F32/F64 */
  65. uint8 type;
  66. /* mutable or constant */
  67. bool is_mutable;
  68. bool is_addr;
  69. /* data offset to base_addr of WASMMemoryInstance */
  70. uint32 data_offset;
  71. /* initial value */
  72. WASMValue initial_value;
  73. } WASMGlobalInstance;
  74. typedef struct WASMFunctionInstance {
  75. /* whether it is import function or WASM function */
  76. bool is_import_func;
  77. /* parameter count */
  78. uint16 param_count;
  79. /* local variable count, 0 for import function */
  80. uint16 local_count;
  81. /* cell num of parameters */
  82. uint16 param_cell_num;
  83. /* cell num of return type */
  84. uint16 ret_cell_num;
  85. /* cell num of local variables, 0 for import function */
  86. uint16 local_cell_num;
  87. uint16 *local_offsets;
  88. /* parameter types */
  89. uint8 *param_types;
  90. /* local types, NULL for import function */
  91. uint8 *local_types;
  92. union {
  93. WASMFunctionImport *func_import;
  94. WASMFunction *func;
  95. } u;
  96. } WASMFunctionInstance;
  97. typedef struct WASMExportFuncInstance {
  98. char *name;
  99. WASMFunctionInstance *function;
  100. } WASMExportFuncInstance;
  101. /* Package Type */
  102. typedef enum {
  103. Wasm_Module_Bytecode = 0,
  104. Wasm_Module_AoT,
  105. Package_Type_Unknown = 0xFFFF
  106. } PackageType;
  107. #if WASM_ENABLE_WASI != 0
  108. typedef struct WASIContext {
  109. struct fd_table *curfds;
  110. struct fd_prestats *prestats;
  111. struct argv_environ_values *argv_environ;
  112. } WASIContext;
  113. #endif
  114. typedef struct WASMModuleInstance {
  115. /* Module instance type, for module instance loaded from
  116. WASM bytecode binary, this field is Wasm_Module_Bytecode;
  117. for module instance loaded from AOT package, this field is
  118. Wasm_Module_AoT, and this structure should be treated as
  119. WASMAOTContext structure. */
  120. uint32 module_type;
  121. uint32 memory_count;
  122. uint32 table_count;
  123. uint32 global_count;
  124. uint32 function_count;
  125. uint32 export_func_count;
  126. WASMMemoryInstance **memories;
  127. WASMTableInstance **tables;
  128. WASMGlobalInstance *globals;
  129. WASMFunctionInstance *functions;
  130. WASMExportFuncInstance *export_functions;
  131. WASMMemoryInstance *default_memory;
  132. WASMTableInstance *default_table;
  133. WASMFunctionInstance *start_function;
  134. WASMModule *module;
  135. #if WASM_ENABLE_WASI != 0
  136. WASIContext wasi_ctx;
  137. #endif
  138. uint32 DYNAMICTOP_PTR_offset;
  139. uint32 temp_ret;
  140. uint32 llvm_stack;
  141. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  142. int32 ext_mem_base_offset;
  143. uint8 *ext_mem_data;
  144. uint8 *ext_mem_data_end;
  145. uint32 ext_mem_size;
  146. #endif
  147. /* Default WASM stack size of threads of this Module instance. */
  148. uint32 wasm_stack_size;
  149. /* Default WASM stack */
  150. uint8 *wasm_stack;
  151. /* The exception buffer of wasm interpreter for current thread. */
  152. char cur_exception[128];
  153. /* The custom data that can be set/get by
  154. * wasm_runtime_set_custom_data/wasm_runtime_get_custom_data */
  155. void *custom_data;
  156. /* Main Thread */
  157. WASMThread main_tlr;
  158. } WASMModuleInstance;
  159. /* Execution environment, e.g. stack info */
  160. typedef struct WASMExecEnv {
  161. uint8_t *stack;
  162. uint32_t stack_size;
  163. } WASMExecEnv;
  164. struct WASMInterpFrame;
  165. typedef struct WASMInterpFrame WASMRuntimeFrame;
  166. /**
  167. * Return the current thread.
  168. *
  169. * @return the current thread
  170. */
  171. static inline WASMThread*
  172. wasm_runtime_get_self()
  173. {
  174. return (WASMThread*)ws_tls_get();
  175. }
  176. /**
  177. * Set self as the current thread.
  178. *
  179. * @param self the thread to be set as current thread
  180. */
  181. static inline void
  182. wasm_runtime_set_tlr(WASMThread *self)
  183. {
  184. ws_tls_put(self);
  185. }
  186. /**
  187. * Return the code block of a function.
  188. *
  189. * @param func the WASM function instance
  190. *
  191. * @return the code block of the function
  192. */
  193. static inline uint8*
  194. wasm_runtime_get_func_code(WASMFunctionInstance *func)
  195. {
  196. return func->is_import_func ? NULL : func->u.func->code;
  197. }
  198. /**
  199. * Return the code block end of a function.
  200. *
  201. * @param func the WASM function instance
  202. *
  203. * @return the code block end of the function
  204. */
  205. static inline uint8*
  206. wasm_runtime_get_func_code_end(WASMFunctionInstance *func)
  207. {
  208. return func->is_import_func
  209. ? NULL : func->u.func->code + func->u.func->code_size;
  210. }
  211. /**
  212. * Call the given WASM function of a WASM module instance with arguments (bytecode and AoT).
  213. *
  214. * @param module_inst the WASM module instance which the function belongs to
  215. * @param exec_env the execution environment to call the function. If the module instance
  216. * is created by AoT mode, it is ignored and just set it to NULL. If the module instance
  217. * is created by bytecode mode and it is NULL, a temporary env object will be created
  218. * @param function the function to be called
  219. * @param argc the number of arguments
  220. * @param argv the arguments. If the function method has return value,
  221. * the first (or first two in case 64-bit return value) element of
  222. * argv stores the return value of the called WASM function after this
  223. * function returns.
  224. *
  225. * @return true if success, false otherwise and exception will be thrown,
  226. * the caller can call wasm_runtime_get_exception to get exception info.
  227. */
  228. bool
  229. wasm_runtime_call_wasm(WASMModuleInstance *module,
  230. WASMExecEnv *exec_env,
  231. WASMFunctionInstance *function,
  232. unsigned argc, uint32 argv[]);
  233. /**
  234. * Set current exception string to global exception string.
  235. *
  236. * @param module the wasm module instance
  237. *
  238. * @param exception current exception string
  239. */
  240. void
  241. wasm_runtime_set_exception(WASMModuleInstance *module,
  242. const char *exception);
  243. /**
  244. * Get current exception string.
  245. *
  246. * @param module the wasm module instance
  247. *
  248. * @return return exception string if exception is thrown, NULL otherwise
  249. */
  250. const char*
  251. wasm_runtime_get_exception(WASMModuleInstance *module);
  252. /**
  253. * Enlarge wasm memory data space.
  254. *
  255. * @param module the wasm module instance
  256. * @param inc_page_count denote the page number to increase
  257. * @return return true if enlarge successfully, false otherwise
  258. */
  259. bool
  260. wasm_runtime_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count);
  261. /* See wasm_export.h for description */
  262. WASMModuleInstance *
  263. wasm_runtime_get_current_module_inst();
  264. /* See wasm_export.h for description */
  265. int32
  266. wasm_runtime_module_malloc(WASMModuleInstance *module_inst, uint32 size);
  267. /* See wasm_export.h for description */
  268. void
  269. wasm_runtime_module_free(WASMModuleInstance *module_inst, int32 ptr);
  270. /* See wasm_export.h for description */
  271. bool
  272. wasm_runtime_validate_app_addr(WASMModuleInstance *module_inst,
  273. int32 app_offset, uint32 size);
  274. /* See wasm_export.h for description */
  275. bool
  276. wasm_runtime_validate_app_str_addr(WASMModuleInstance *module_inst,
  277. int32 app_offset);
  278. /* See wasm_export.h for description */
  279. bool
  280. wasm_runtime_validate_native_addr(WASMModuleInstance *module_inst,
  281. void *native_ptr, uint32 size);
  282. /* See wasm_export.h for description */
  283. void *
  284. wasm_runtime_addr_app_to_native(WASMModuleInstance *module_inst,
  285. int32 app_offset);
  286. /* See wasm_export.h for description */
  287. int32
  288. wasm_runtime_addr_native_to_app(WASMModuleInstance *module_inst,
  289. void *native_ptr);
  290. /* See wasm_export.h for description */
  291. bool
  292. wasm_runtime_get_app_addr_range(WASMModuleInstance *module_inst,
  293. int32_t app_offset,
  294. int32_t *p_app_start_offset,
  295. int32_t *p_app_end_offset);
  296. /* See wasm_export.h for description */
  297. bool
  298. wasm_runtime_get_native_addr_range(WASMModuleInstance *module_inst,
  299. uint8_t *native_ptr,
  300. uint8_t **p_native_start_addr,
  301. uint8_t **p_native_end_addr);
  302. bool
  303. wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
  304. WASMModuleInstance *module_inst,
  305. uint32 *argv, uint32 argc, uint32 *ret);
  306. #ifdef __cplusplus
  307. }
  308. #endif
  309. #endif /* end of _WASM_RUNTIME_H */