wasm_runtime_common.h 9.2 KB

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