wasm_runtime_common.h 14 KB

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