wasm-runtime.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #define DEFAULT_WASM_STACK_SIZE (8 * 1024)
  25. #define DEFAULT_WASM_HEAP_SIZE (8 * 1024)
  26. #define MIN_WASM_HEAP_SIZE (1 * 1024)
  27. typedef struct WASMMemoryInstance {
  28. /* Current page count */
  29. uint32 cur_page_count;
  30. /* Maximum page count */
  31. uint32 max_page_count;
  32. /* Data of import globals with address info, like _stdin/_stdout/_stderr,
  33. stdin/stdout/stderr is stored here, but the actual addr info, or offset
  34. to memory_data is stored in global_data section */
  35. uint8 *addr_data;
  36. /* Size of addr_data */
  37. uint32 addr_data_size;
  38. /* Thunk data of argument strings */
  39. uint8 *thunk_argv_data;
  40. uint32 thunk_argv_data_size;
  41. /* Thunk argument count */
  42. uint32 thunk_argc;
  43. /* Thunk argument offsets */
  44. uint8 *thunk_argv_offsets;
  45. /* Heap data */
  46. uint8 *heap_data;
  47. /* Heap size */
  48. uint32 heap_data_size;
  49. /* The heap created */
  50. void *heap_handle;
  51. /* Memory data */
  52. uint8 *memory_data;
  53. /* Global data of global instances */
  54. uint8 *global_data;
  55. uint32 global_data_size;
  56. /* End address of memory */
  57. uint8 *end_addr;
  58. /* Base address, the layout is:
  59. addr_data + thunk_argv data + thunk arg offsets +
  60. heap data + memory data + global data
  61. memory data init size is: NumBytesPerPage * cur_page_count
  62. addr data size and global data size is calculated in module instantiating
  63. Note: when memory is re-allocated, the addr data, thunk argv data, thunk
  64. argv offsets and memory data must be copied to new memory also.
  65. */
  66. uint8 base_addr[1];
  67. } WASMMemoryInstance;
  68. typedef struct WASMTableInstance {
  69. /* The element type, TABLE_ELEM_TYPE_ANY_FUNC currently */
  70. uint8 elem_type;
  71. /* Current size */
  72. uint32 cur_size;
  73. /* Maximum size */
  74. uint32 max_size;
  75. /* Base address */
  76. uint8 base_addr[1];
  77. } WASMTableInstance;
  78. typedef struct WASMGlobalInstance {
  79. /* value type, VALUE_TYPE_I32/I64/F32/F64 */
  80. uint8 type;
  81. /* mutable or constant */
  82. bool is_mutable;
  83. bool is_addr;
  84. /* data offset to base_addr of WASMMemoryInstance */
  85. uint32 data_offset;
  86. /* initial value */
  87. WASMValue initial_value;
  88. } WASMGlobalInstance;
  89. typedef struct WASMFunctionInstance {
  90. /* whether it is import function or WASM function */
  91. bool is_import_func;
  92. /* cell num of parameters */
  93. uint16 param_cell_num;
  94. /* cell num of return type */
  95. uint16 ret_cell_num;
  96. /* cell num of local variables, 0 for import function */
  97. uint16 local_cell_num;
  98. uint16 *local_offsets;
  99. union {
  100. WASMFunctionImport *func_import;
  101. WASMFunction *func;
  102. } u;
  103. } WASMFunctionInstance;
  104. typedef struct WASMExportFuncInstance {
  105. char *name;
  106. WASMFunctionInstance *function;
  107. } WASMExportFuncInstance;
  108. /* Package Type */
  109. typedef enum {
  110. Wasm_Module_Bytecode = 0,
  111. Wasm_Module_AoT,
  112. Package_Type_Unknown = 0xFFFF
  113. } PackageType;
  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. HashMap *branch_set;
  135. const WASMModule *module;
  136. uint32 DYNAMICTOP_PTR_offset;
  137. uint32 temp_ret;
  138. uint32 llvm_stack;
  139. /* Default WASM stack size of threads of this Module instance. */
  140. uint32 wasm_stack_size;
  141. /* Default WASM stack */
  142. uint8 *wasm_stack;
  143. /* The exception buffer of wasm interpreter for current thread. */
  144. char cur_exception[128];
  145. /* The thread data of the attaching thread */
  146. void *thread_data;
  147. /* Main Thread */
  148. WASMThread main_tlr;
  149. } WASMModuleInstance;
  150. /* Execution environment, e.g. stack info */
  151. typedef struct WASMExecEnv {
  152. uint8_t *stack;
  153. uint32_t stack_size;
  154. } WASMExecEnv;
  155. struct WASMInterpFrame;
  156. typedef struct WASMInterpFrame WASMRuntimeFrame;
  157. /**
  158. * Return the current thread.
  159. *
  160. * @return the current thread
  161. */
  162. static inline WASMThread*
  163. wasm_runtime_get_self()
  164. {
  165. return (WASMThread*)ws_tls_get();
  166. }
  167. /**
  168. * Set self as the current thread.
  169. *
  170. * @param self the thread to be set as current thread
  171. */
  172. static inline void
  173. wasm_runtime_set_tlr(WASMThread *self)
  174. {
  175. ws_tls_put(self);
  176. }
  177. /**
  178. * Return the code block of a function.
  179. *
  180. * @param func the WASM function instance
  181. *
  182. * @return the code block of the function
  183. */
  184. static inline uint8*
  185. wasm_runtime_get_func_code(WASMFunctionInstance *func)
  186. {
  187. return func->is_import_func ? NULL : func->u.func->code;
  188. }
  189. /**
  190. * Return the code block end of a function.
  191. *
  192. * @param func the WASM function instance
  193. *
  194. * @return the code block end of the function
  195. */
  196. static inline uint8*
  197. wasm_runtime_get_func_code_end(WASMFunctionInstance *func)
  198. {
  199. return func->is_import_func
  200. ? NULL : func->u.func->code + func->u.func->code_size;
  201. }
  202. /**
  203. * Call the given WASM function of a WASM module instance with arguments (bytecode and AoT).
  204. *
  205. * @param module_inst the WASM module instance which the function belongs to
  206. * @param exec_env the execution environment to call the function. If the module instance
  207. * is created by AoT mode, it is ignored and just set it to NULL. If the module instance
  208. * is created by bytecode mode and it is NULL, a temporary env object will be created
  209. * @param function the function to be called
  210. * @param argc the number of arguments
  211. * @param argv the arguments. If the function method has return value,
  212. * the first (or first two in case 64-bit return value) element of
  213. * argv stores the return value of the called WASM function after this
  214. * function returns.
  215. *
  216. * @return true if success, false otherwise and exception will be thrown,
  217. * the caller can call wasm_runtime_get_exception to get exception info.
  218. */
  219. bool
  220. wasm_runtime_call_wasm(WASMModuleInstance *module,
  221. WASMExecEnv *exec_env,
  222. WASMFunctionInstance *function,
  223. unsigned argc, uint32 argv[]);
  224. /**
  225. * Set current exception string to global exception string.
  226. *
  227. * @param module the wasm module instance
  228. *
  229. * @param exception current exception string
  230. */
  231. void
  232. wasm_runtime_set_exception(WASMModuleInstance *module,
  233. const char *exception);
  234. /**
  235. * Get current exception string.
  236. *
  237. * @param module the wasm module instance
  238. *
  239. * @return return exception string if exception is thrown, NULL otherwise
  240. */
  241. const char*
  242. wasm_runtime_get_exception(WASMModuleInstance *module);
  243. /**
  244. * Enlarge wasm memory data space.
  245. *
  246. * @param module the wasm module instance
  247. * @param inc_page_count denote the page number to increase
  248. * @return return true if enlarge successfully, false otherwise
  249. */
  250. bool
  251. wasm_runtime_enlarge_memory(WASMModuleInstance *module, int inc_page_count);
  252. /* See wasm-export.h for description */
  253. WASMModuleInstance *
  254. wasm_runtime_get_current_module_inst();
  255. /* See wasm-export.h for description */
  256. int32_t
  257. wasm_runtime_module_malloc(WASMModuleInstance *module_inst, uint32_t size);
  258. /* See wasm-export.h for description */
  259. void
  260. wasm_runtime_module_free(WASMModuleInstance *module_inst, int32_t ptr);
  261. /* See wasm-export.h for description */
  262. bool
  263. wasm_runtime_validate_app_addr(WASMModuleInstance *module_inst,
  264. int32_t app_offset, uint32_t size);
  265. /* See wasm-export.h for description */
  266. bool
  267. wasm_runtime_validate_native_addr(WASMModuleInstance *module_inst,
  268. void *native_ptr, uint32_t size);
  269. /* See wasm-export.h for description */
  270. void *
  271. wasm_runtime_addr_app_to_native(WASMModuleInstance *module_inst,
  272. int32_t app_offset);
  273. /* See wasm-export.h for description */
  274. int32_t
  275. wasm_runtime_addr_native_to_app(WASMModuleInstance *module_inst,
  276. void *native_ptr);
  277. #ifdef __cplusplus
  278. }
  279. #endif
  280. #endif /* end of _WASM_RUNTIME_H */