wasm_runtime_common.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "bh_thread.h"
  10. #include "wasm_exec_env.h"
  11. #include "wasm_native.h"
  12. #include "../include/wasm_export.h"
  13. #include "../interpreter/wasm.h"
  14. #if WASM_ENABLE_LIBC_WASI != 0
  15. #include "wasmtime_ssp.h"
  16. #include "posix.h"
  17. #endif
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. typedef struct WASMModuleCommon {
  22. /* Module type, for module loaded from WASM bytecode binary,
  23. this field is Wasm_Module_Bytecode, and this structure should
  24. be treated as WASMModule structure;
  25. for module loaded from AOT binary, this field is
  26. Wasm_Module_AoT, and this structure should be treated as
  27. AOTModule structure. */
  28. uint32 module_type;
  29. uint8 module_data[1];
  30. } WASMModuleCommon;
  31. typedef struct WASMModuleInstanceCommon {
  32. /* Module instance type, for module instance loaded from WASM
  33. bytecode binary, this field is Wasm_Module_Bytecode, and this
  34. structure should be treated as WASMModuleInstance structure;
  35. for module instance loaded from AOT binary, this field is
  36. Wasm_Module_AoT, and this structure should be treated as
  37. AOTModuleInstance structure. */
  38. uint32 module_type;
  39. uint8 module_inst_data[1];
  40. } WASMModuleInstanceCommon;
  41. #if WASM_ENABLE_LIBC_WASI != 0
  42. typedef struct WASIContext {
  43. struct fd_table *curfds;
  44. struct fd_prestats *prestats;
  45. struct argv_environ_values *argv_environ;
  46. } WASIContext;
  47. #endif
  48. typedef package_type_t PackageType;
  49. typedef wasm_section_t WASMSection, AOTSection;
  50. /* See wasm_export.h for description */
  51. bool
  52. wasm_runtime_init();
  53. /* See wasm_export.h for description */
  54. bool
  55. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  56. /* See wasm_export.h for description */
  57. void
  58. wasm_runtime_destroy();
  59. /* See wasm_export.h for description */
  60. PackageType
  61. get_package_type(const uint8 *buf, uint32 size);
  62. /* See wasm_export.h for description */
  63. WASMModuleCommon *
  64. wasm_runtime_load(const uint8 *buf, uint32 size,
  65. char *error_buf, uint32 error_buf_size);
  66. /* See wasm_export.h for description */
  67. WASMModuleCommon *
  68. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  69. char *error_buf, uint32_t error_buf_size);
  70. /* See wasm_export.h for description */
  71. void
  72. wasm_runtime_unload(WASMModuleCommon *module);
  73. /* See wasm_export.h for description */
  74. WASMModuleInstanceCommon *
  75. wasm_runtime_instantiate(WASMModuleCommon *module,
  76. uint32 stack_size, uint32 heap_size,
  77. char *error_buf, uint32 error_buf_size);
  78. /* See wasm_export.h for description */
  79. void
  80. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  81. /* See wasm_export.h for description */
  82. WASMFunctionInstanceCommon *
  83. wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
  84. const char *name, const char *signature);
  85. /* See wasm_export.h for description */
  86. WASMExecEnv *
  87. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  88. uint32 stack_size);
  89. /* See wasm_export.h for description */
  90. void
  91. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  92. /* See wasm_export.h for description */
  93. WASMModuleInstanceCommon *
  94. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  95. /* See wasm_export.h for description */
  96. bool
  97. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  98. WASMFunctionInstanceCommon *function,
  99. unsigned argc, uint32 argv[]);
  100. bool
  101. wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
  102. WASMFunctionInstanceCommon *function,
  103. unsigned argc, uint32 argv[]);
  104. /* See wasm_export.h for description */
  105. bool
  106. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
  107. int argc, char *argv[]);
  108. /* See wasm_export.h for description */
  109. bool
  110. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  111. const char *name, int argc, char *argv[]);
  112. /* See wasm_export.h for description */
  113. void
  114. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  115. const char *exception);
  116. /* See wasm_export.h for description */
  117. const char *
  118. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  119. /* See wasm_export.h for description */
  120. void
  121. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  122. /* See wasm_export.h for description */
  123. void
  124. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  125. void *custom_data);
  126. /* See wasm_export.h for description */
  127. void *
  128. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  129. /* See wasm_export.h for description */
  130. int32
  131. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  132. void **p_native_addr);
  133. /* See wasm_export.h for description */
  134. void
  135. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, int32 ptr);
  136. /* See wasm_export.h for description */
  137. int32
  138. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  139. const char *src, uint32 size);
  140. /* See wasm_export.h for description */
  141. bool
  142. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  143. int32 app_offset, uint32 size);
  144. /* See wasm_export.h for description */
  145. bool
  146. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  147. int32 app_str_offset);
  148. /* See wasm_export.h for description */
  149. bool
  150. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  151. void *native_ptr, uint32 size);
  152. /* See wasm_export.h for description */
  153. void *
  154. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  155. int32 app_offset);
  156. /* See wasm_export.h for description */
  157. int32
  158. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  159. void *native_ptr);
  160. /* See wasm_export.h for description */
  161. bool
  162. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  163. int32 app_offset,
  164. int32 *p_app_start_offset,
  165. int32 *p_app_end_offset);
  166. /* See wasm_export.h for description */
  167. bool
  168. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  169. uint8 *native_ptr,
  170. uint8 **p_native_start_addr,
  171. uint8 **p_native_end_addr);
  172. uint32
  173. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst);
  174. void
  175. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  176. uint32 temp_ret);
  177. uint32
  178. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst);
  179. void
  180. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  181. uint32 llvm_stack);
  182. #if WASM_ENABLE_LIBC_WASI != 0
  183. /* See wasm_export.h for description */
  184. void
  185. wasm_runtime_set_wasi_args(WASMModuleCommon *module,
  186. const char *dir_list[], uint32 dir_count,
  187. const char *map_dir_list[], uint32 map_dir_count,
  188. const char *env_list[], uint32 env_count,
  189. char *argv[], int argc);
  190. /* See wasm_export.h for description */
  191. bool
  192. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  193. /* See wasm_export.h for description */
  194. WASMFunctionInstanceCommon *
  195. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  196. bool
  197. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  198. const char *dir_list[], uint32 dir_count,
  199. const char *map_dir_list[], uint32 map_dir_count,
  200. const char *env[], uint32 env_count,
  201. char *argv[], uint32 argc,
  202. char *error_buf, uint32 error_buf_size);
  203. void
  204. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  205. void
  206. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  207. WASIContext *wasi_ctx);
  208. WASIContext *
  209. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  210. #endif /* end of WASM_ENABLE_LIBC_WASI */
  211. /**
  212. * Enlarge wasm memory data space.
  213. *
  214. * @param module the wasm module instance
  215. * @param inc_page_count denote the page number to increase
  216. * @return return true if enlarge successfully, false otherwise
  217. */
  218. bool
  219. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, uint32 inc_page_count);
  220. /* See wasm_export.h for description */
  221. bool
  222. wasm_runtime_register_natives(const char *module_name,
  223. NativeSymbol *native_symbols,
  224. uint32 n_native_symbols);
  225. bool
  226. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  227. const WASMType *func_type, const char *signature,
  228. uint32 *argv, uint32 argc, uint32 *ret);
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. #endif /* end of _WASM_COMMON_H */