wasm_runtime.h 9.9 KB

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