wasm_runtime_common.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_COMMON_H
  6. #define _WASM_COMMON_H
  7. #include "bh_platform.h"
  8. #include "bh_common.h"
  9. #include "wasm_exec_env.h"
  10. #include "wasm_native.h"
  11. #include "../include/wasm_export.h"
  12. #include "../interpreter/wasm.h"
  13. #if WASM_ENABLE_LIBC_WASI != 0
  14. #include "wasmtime_ssp.h"
  15. #include "posix.h"
  16. #endif
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. typedef struct WASMModuleCommon {
  21. /* Module type, for module loaded from WASM bytecode binary,
  22. this field is Wasm_Module_Bytecode, and this structure should
  23. be treated as WASMModule structure;
  24. for module loaded from AOT binary, this field is
  25. Wasm_Module_AoT, and this structure should be treated as
  26. AOTModule structure. */
  27. uint32 module_type;
  28. uint8 module_data[1];
  29. } WASMModuleCommon;
  30. typedef struct WASMModuleInstanceCommon {
  31. /* Module instance type, for module instance loaded from WASM
  32. bytecode binary, this field is Wasm_Module_Bytecode, and this
  33. structure should be treated as WASMModuleInstance structure;
  34. for module instance loaded from AOT binary, this field is
  35. Wasm_Module_AoT, and this structure should be treated as
  36. AOTModuleInstance structure. */
  37. uint32 module_type;
  38. uint8 module_inst_data[1];
  39. } WASMModuleInstanceCommon;
  40. #if WASM_ENABLE_LIBC_WASI != 0
  41. typedef struct WASIContext {
  42. /* Use offset but not native address, since these fields are
  43. allocated from app's heap, and the heap space may be re-allocated
  44. after memory.grow opcode is executed, the original native address
  45. cannot be accessed again. */
  46. uint32 curfds_offset;
  47. uint32 prestats_offset;
  48. uint32 argv_environ_offset;
  49. uint32 argv_buf_offset;
  50. uint32 argv_offsets_offset;
  51. uint32 env_buf_offset;
  52. uint32 env_offsets_offset;
  53. } WASIContext;
  54. #endif
  55. #if WASM_ENABLE_MULTI_MODULE != 0
  56. typedef struct WASMRegisteredModule {
  57. bh_list_link l;
  58. /* point to a string pool */
  59. const char *module_name;
  60. WASMModuleCommon *module;
  61. /* to store the original module file buffer address */
  62. uint8 *orig_file_buf;
  63. uint32 orig_file_buf_size;
  64. } WASMRegisteredModule;
  65. #endif
  66. typedef struct WASMMemoryInstanceCommon {
  67. uint32 module_type;
  68. uint8 memory_inst_data[1];
  69. } WASMMemoryInstanceCommon;
  70. typedef package_type_t PackageType;
  71. typedef wasm_section_t WASMSection, AOTSection;
  72. /* See wasm_export.h for description */
  73. bool
  74. wasm_runtime_init(void);
  75. /* See wasm_export.h for description */
  76. bool
  77. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  78. /* See wasm_export.h for description */
  79. void
  80. wasm_runtime_destroy(void);
  81. /* See wasm_export.h for description */
  82. PackageType
  83. get_package_type(const uint8 *buf, uint32 size);
  84. /* See wasm_export.h for description */
  85. WASMModuleCommon *
  86. wasm_runtime_load(const uint8 *buf, uint32 size,
  87. char *error_buf, uint32 error_buf_size);
  88. /* See wasm_export.h for description */
  89. WASMModuleCommon *
  90. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  91. char *error_buf, uint32 error_buf_size);
  92. /* See wasm_export.h for description */
  93. void
  94. wasm_runtime_unload(WASMModuleCommon *module);
  95. /* Internal API */
  96. WASMModuleInstanceCommon *
  97. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  98. uint32 stack_size, uint32 heap_size,
  99. char *error_buf, uint32 error_buf_size);
  100. /* Internal API */
  101. void
  102. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  103. bool is_sub_inst);
  104. /* See wasm_export.h for description */
  105. WASMModuleInstanceCommon *
  106. wasm_runtime_instantiate(WASMModuleCommon *module,
  107. uint32 stack_size, uint32 heap_size,
  108. char *error_buf, uint32 error_buf_size);
  109. /* See wasm_export.h for description */
  110. void
  111. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  112. /* See wasm_export.h for description */
  113. WASMFunctionInstanceCommon *
  114. wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
  115. const char *name, const char *signature);
  116. /* See wasm_export.h for description */
  117. WASMExecEnv *
  118. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  119. uint32 stack_size);
  120. /* See wasm_export.h for description */
  121. void
  122. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  123. /* See wasm_export.h for description */
  124. WASMModuleInstanceCommon *
  125. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  126. /* See wasm_export.h for description */
  127. void *
  128. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  129. /* See wasm_export.h for description */
  130. void
  131. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  132. /* See wasm_export.h for description */
  133. void *
  134. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  135. /* See wasm_export.h for description */
  136. bool
  137. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  138. WASMFunctionInstanceCommon *function,
  139. uint32 argc, uint32 argv[]);
  140. /**
  141. * Call a function reference of a given WASM runtime instance with
  142. * arguments.
  143. *
  144. * @param exec_env the execution environment to call the function
  145. * which must be created from wasm_create_exec_env()
  146. * @param element_indices the function ference indicies, usually
  147. * prvovided by the caller of a registed native function
  148. * @param argc the number of arguments
  149. * @param argv the arguments. If the function method has return value,
  150. * the first (or first two in case 64-bit return value) element of
  151. * argv stores the return value of the called WASM function after this
  152. * function returns.
  153. *
  154. * @return true if success, false otherwise and exception will be thrown,
  155. * the caller can call wasm_runtime_get_exception to get exception info.
  156. */
  157. bool
  158. wasm_runtime_call_indirect(WASMExecEnv *exec_env,
  159. uint32 element_indices,
  160. uint32 argc, uint32 argv[]);
  161. bool
  162. wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
  163. WASMFunctionInstanceCommon *function,
  164. uint32 argc, uint32 argv[]);
  165. /* See wasm_export.h for description */
  166. bool
  167. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
  168. int32 argc, char *argv[]);
  169. /* See wasm_export.h for description */
  170. bool
  171. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  172. const char *name, int32 argc, char *argv[]);
  173. /* See wasm_export.h for description */
  174. void
  175. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  176. const char *exception);
  177. /* See wasm_export.h for description */
  178. const char *
  179. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  180. /* See wasm_export.h for description */
  181. void
  182. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  183. /* See wasm_export.h for description */
  184. void
  185. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  186. void *custom_data);
  187. /* See wasm_export.h for description */
  188. void *
  189. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  190. /* See wasm_export.h for description */
  191. uint32
  192. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  193. void **p_native_addr);
  194. /* See wasm_export.h for description */
  195. void
  196. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  197. /* See wasm_export.h for description */
  198. uint32
  199. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  200. const char *src, uint32 size);
  201. /* See wasm_export.h for description */
  202. bool
  203. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  204. uint32 app_offset, uint32 size);
  205. /* See wasm_export.h for description */
  206. bool
  207. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  208. uint32 app_str_offset);
  209. /* See wasm_export.h for description */
  210. bool
  211. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  212. void *native_ptr, uint32 size);
  213. /* See wasm_export.h for description */
  214. void *
  215. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  216. uint32 app_offset);
  217. /* See wasm_export.h for description */
  218. uint32
  219. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  220. void *native_ptr);
  221. /* See wasm_export.h for description */
  222. bool
  223. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  224. uint32 app_offset,
  225. uint32 *p_app_start_offset,
  226. uint32 *p_app_end_offset);
  227. /* See wasm_export.h for description */
  228. bool
  229. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  230. uint8 *native_ptr,
  231. uint8 **p_native_start_addr,
  232. uint8 **p_native_end_addr);
  233. uint32
  234. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst);
  235. void
  236. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  237. uint32 temp_ret);
  238. uint32
  239. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst);
  240. void
  241. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  242. uint32 llvm_stack);
  243. #if WASM_ENABLE_MULTI_MODULE != 0
  244. void
  245. wasm_runtime_set_module_reader(const module_reader reader,
  246. const module_destroyer destroyer);
  247. module_reader
  248. wasm_runtime_get_module_reader();
  249. module_destroyer
  250. wasm_runtime_get_module_destroyer();
  251. bool
  252. wasm_runtime_register_module_internal(const char *module_name,
  253. WASMModuleCommon *module,
  254. uint8 *orig_file_buf,
  255. uint32 orig_file_buf_size,
  256. char *error_buf,
  257. uint32 error_buf_size);
  258. void
  259. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  260. bool
  261. wasm_runtime_is_module_registered(const char *module_name);
  262. bool
  263. wasm_runtime_add_loading_module(const char *module_name,
  264. char *error_buf, uint32 error_buf_size);
  265. void
  266. wasm_runtime_delete_loading_module(const char *module_name);
  267. bool
  268. wasm_runtime_is_loading_module(const char *module_name);
  269. void
  270. wasm_runtime_destroy_loading_module_list();
  271. #endif /* WASM_ENALBE_MULTI_MODULE */
  272. bool
  273. wasm_runtime_is_built_in_module(const char *module_name);
  274. #if WASM_ENABLE_THREAD_MGR != 0
  275. bool
  276. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env,
  277. uint32 *start_offset, uint32 *size);
  278. bool
  279. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env,
  280. uint32 start_offset, uint32 size);
  281. #endif
  282. #if WASM_ENABLE_LIBC_WASI != 0
  283. /* See wasm_export.h for description */
  284. void
  285. wasm_runtime_set_wasi_args(WASMModuleCommon *module,
  286. const char *dir_list[], uint32 dir_count,
  287. const char *map_dir_list[], uint32 map_dir_count,
  288. const char *env_list[], uint32 env_count,
  289. char *argv[], int argc);
  290. /* See wasm_export.h for description */
  291. bool
  292. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  293. /* See wasm_export.h for description */
  294. WASMFunctionInstanceCommon *
  295. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  296. bool
  297. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  298. const char *dir_list[], uint32 dir_count,
  299. const char *map_dir_list[], uint32 map_dir_count,
  300. const char *env[], uint32 env_count,
  301. char *argv[], uint32 argc,
  302. char *error_buf, uint32 error_buf_size);
  303. void
  304. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  305. void
  306. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  307. WASIContext *wasi_ctx);
  308. WASIContext *
  309. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  310. #endif /* end of WASM_ENABLE_LIBC_WASI */
  311. /* Get module of the current exec_env */
  312. WASMModuleCommon*
  313. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  314. /**
  315. * Enlarge wasm memory data space.
  316. *
  317. * @param module the wasm module instance
  318. * @param inc_page_count denote the page number to increase
  319. * @return return true if enlarge successfully, false otherwise
  320. */
  321. bool
  322. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, uint32 inc_page_count);
  323. /* See wasm_export.h for description */
  324. bool
  325. wasm_runtime_register_natives(const char *module_name,
  326. NativeSymbol *native_symbols,
  327. uint32 n_native_symbols);
  328. /* See wasm_export.h for description */
  329. bool
  330. wasm_runtime_register_natives_raw(const char *module_name,
  331. NativeSymbol *native_symbols,
  332. uint32 n_native_symbols);
  333. bool
  334. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  335. const WASMType *func_type, const char *signature,
  336. void *attachment,
  337. uint32 *argv, uint32 argc, uint32 *ret);
  338. bool
  339. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  340. const WASMType *func_type, const char *signature,
  341. void *attachment,
  342. uint32 *argv, uint32 argc, uint32 *ret);
  343. #ifdef __cplusplus
  344. }
  345. #endif
  346. #endif /* end of _WASM_COMMON_H */