wasm_export.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_EXPORT_H
  6. #define _WASM_EXPORT_H
  7. #include <inttypes.h>
  8. #include <stdbool.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /* Uninstantiated WASM module loaded from WASM binary file
  13. or AoT binary file*/
  14. struct WASMModuleCommon;
  15. typedef struct WASMModuleCommon *wasm_module_t;
  16. /* Instantiated WASM module */
  17. struct WASMModuleInstanceCommon;
  18. typedef struct WASMModuleInstanceCommon *wasm_module_inst_t;
  19. /* Function instance */
  20. struct WASMFunctionInstanceCommon;
  21. typedef struct WASMFunctionInstanceCommon *wasm_function_inst_t;
  22. /* WASM section */
  23. typedef struct wasm_section {
  24. struct wasm_section *next;
  25. /* section type */
  26. int section_type;
  27. /* section body, not include type and size */
  28. uint8_t *section_body;
  29. /* section body size */
  30. uint32_t section_body_size;
  31. } wasm_section_t, *wasm_section_list_t;
  32. typedef wasm_section_t aot_section_t, *aot_section_list_t;
  33. /* Execution environment, e.g. stack info */
  34. struct WASMExecEnv;
  35. typedef struct WASMExecEnv *wasm_exec_env_t;
  36. /* Package Type */
  37. typedef enum {
  38. Wasm_Module_Bytecode = 0,
  39. Wasm_Module_AoT,
  40. Package_Type_Unknown = 0xFFFF
  41. } package_type_t;
  42. /**
  43. * Initialize the WASM runtime environment.
  44. *
  45. * @return true if success, false otherwise
  46. */
  47. bool
  48. wasm_runtime_init();
  49. /**
  50. * Destroy the WASM runtime environment.
  51. */
  52. void
  53. wasm_runtime_destroy();
  54. /**
  55. * Get the package type of a buffer.
  56. *
  57. * @param buf the package buffer
  58. * @param size the package buffer size
  59. *
  60. * @return the package type, return Package_Type_Unknown if the type is unknown
  61. */
  62. package_type_t
  63. get_package_type(const uint8_t *buf, uint32_t size);
  64. /**
  65. * Load a WASM module from a specified byte buffer.
  66. *
  67. * @param buf the byte buffer which contains the WASM binary data
  68. * @param size the size of the buffer
  69. * @param error_buf output of the exception info
  70. * @param error_buf_size the size of the exception string
  71. *
  72. * @return return WASM module loaded, NULL if failed
  73. */
  74. wasm_module_t
  75. wasm_runtime_load(const uint8_t *buf, uint32_t size,
  76. char *error_buf, uint32_t error_buf_size);
  77. /**
  78. * Load a WASM module from a specified WASM or AOT section list.
  79. *
  80. * @param section_list the section list which contains each section data
  81. * @param is_aot whether the section list is AOT section list
  82. * @param error_buf output of the exception info
  83. * @param error_buf_size the size of the exception string
  84. *
  85. * @return return WASM module loaded, NULL if failed
  86. */
  87. wasm_module_t
  88. wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
  89. char *error_buf, uint32_t error_buf_size);
  90. /**
  91. * Unload a WASM module.
  92. *
  93. * @param module the module to be unloaded
  94. */
  95. void
  96. wasm_runtime_unload(wasm_module_t module);
  97. void
  98. wasm_runtime_set_wasi_args(wasm_module_t module,
  99. const char *dir_list[], uint32_t dir_count,
  100. const char *map_dir_list[], uint32_t map_dir_count,
  101. const char *env[], uint32_t env_count,
  102. char *argv[], int argc);
  103. /**
  104. * Instantiate a WASM module.
  105. *
  106. * @param module the WASM module to instantiate
  107. * @param stack_size the default stack size of the module instance, a stack
  108. * will be created when function wasm_runtime_call_wasm() is called
  109. * to run WASM function and the exec_env argument passed to
  110. * wasm_runtime_call_wasm() is NULL. That means this parameter is
  111. * ignored if exec_env is not NULL.
  112. * @param heap_size the default heap size of the module instance, a heap will
  113. * be created besides the app memory space. Both wasm app and native
  114. * function can allocate memory from the heap. If heap_size is 0, the
  115. * default heap size will be used.
  116. * @param error_buf buffer to output the error info if failed
  117. * @param error_buf_size the size of the error buffer
  118. *
  119. * @return return the instantiated WASM module instance, NULL if failed
  120. */
  121. wasm_module_inst_t
  122. wasm_runtime_instantiate(const wasm_module_t module,
  123. uint32_t stack_size, uint32_t heap_size,
  124. char *error_buf, uint32_t error_buf_size);
  125. /**
  126. * Deinstantiate a WASM module instance, destroy the resources.
  127. *
  128. * @param module_inst the WASM module instance to destroy
  129. */
  130. void
  131. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  132. bool
  133. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  134. wasm_function_inst_t
  135. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  136. /**
  137. * Lookup an exported function in the WASM module instance.
  138. *
  139. * @param module_inst the module instance
  140. * @param name the name of the function
  141. * @param signature the signature of the function, use "i32"/"i64"/"f32"/"f64"
  142. * to represent the type of i32/i64/f32/f64, e.g. "(i32i64)" "(i32)f32"
  143. *
  144. * @return the function instance found
  145. */
  146. wasm_function_inst_t
  147. wasm_runtime_lookup_function(const wasm_module_inst_t module_inst,
  148. const char *name, const char *signature);
  149. /**
  150. * Create execution environment for a WASM module instance.
  151. *
  152. * @param module_inst the module instance
  153. * @param stack_size the stack size to execute a WASM function
  154. *
  155. * @return the execution environment
  156. */
  157. wasm_exec_env_t
  158. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  159. uint32_t stack_size);
  160. /**
  161. * Destroy the execution environment.
  162. *
  163. * @param env the execution environment to destroy
  164. */
  165. void
  166. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  167. /**
  168. * Get WASM module instance from execution environment
  169. *
  170. * @param exec_env the execution environment to retrieve
  171. *
  172. * @return the WASM module instance
  173. */
  174. wasm_module_inst_t
  175. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  176. /**
  177. * Call the given WASM function of a WASM module instance with
  178. * arguments (bytecode and AoT).
  179. *
  180. * @param exec_env the execution environment to call the function
  181. * which must be created from wasm_create_exec_env()
  182. * @param function the function to be called
  183. * @param argc the number of arguments
  184. * @param argv the arguments. If the function method has return value,
  185. * the first (or first two in case 64-bit return value) element of
  186. * argv stores the return value of the called WASM function after this
  187. * function returns.
  188. *
  189. * @return true if success, false otherwise and exception will be thrown,
  190. * the caller can call wasm_runtime_get_exception to get exception info.
  191. */
  192. bool
  193. wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
  194. wasm_function_inst_t function,
  195. uint32_t argc, uint32_t argv[]);
  196. /**
  197. * Find the unique main function from a WASM module instance
  198. * and execute that function.
  199. *
  200. * @param module_inst the WASM module instance
  201. * @param argc the number of arguments
  202. * @param argv the arguments array
  203. *
  204. * @return true if the main function is called, false otherwise and exception will be thrown,
  205. * the caller can call wasm_runtime_get_exception to get exception info.
  206. */
  207. bool
  208. wasm_application_execute_main(wasm_module_inst_t module_inst,
  209. int32_t argc, char *argv[]);
  210. /**
  211. * Find the specified function in argv[0] from a WASM module instance
  212. * and execute that function.
  213. *
  214. * @param module_inst the WASM module instance
  215. * @param name the name of the function to execute
  216. * @param argc the number of arguments
  217. * @param argv the arguments array
  218. *
  219. * @return true if the specified function is called, false otherwise and exception will be thrown,
  220. * the caller can call wasm_runtime_get_exception to get exception info.
  221. */
  222. bool
  223. wasm_application_execute_func(wasm_module_inst_t module_inst,
  224. const char *name, int32_t argc, char *argv[]);
  225. /**
  226. * Get exception info of the WASM module instance.
  227. *
  228. * @param module_inst the WASM module instance
  229. *
  230. * @return the exception string
  231. */
  232. const char *
  233. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  234. /**
  235. * Clear exception info of the WASM module instance.
  236. *
  237. * @param module_inst the WASM module instance
  238. */
  239. void
  240. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  241. /**
  242. * Set custom data to WASM module instance.
  243. *
  244. * @param module_inst the WASM module instance
  245. * @param custom_data the custom data to be set
  246. */
  247. void
  248. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  249. void *custom_data);
  250. /**
  251. * Get the custom data within a WASM module instance.
  252. *
  253. * @param module_inst the WASM module instance
  254. *
  255. * @return the custom data (NULL if not set yet)
  256. */
  257. void *
  258. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  259. /**
  260. * Allocate memory from the heap of WASM module instance
  261. *
  262. * @param module_inst the WASM module instance which contains heap
  263. * @param size the size bytes to allocate
  264. *
  265. * @return the allocated memory address, which is a relative offset to the
  266. * base address of the module instance's memory space, the value range
  267. * is (-heap_size, 0). Note that it is not an absolute address.
  268. * Return non-zero if success, zero if failed.
  269. */
  270. int32_t
  271. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size);
  272. /**
  273. * Free memory to the heap of WASM module instance
  274. *
  275. * @param module_inst the WASM module instance which contains heap
  276. * @param ptr the pointer to free
  277. */
  278. void
  279. wasm_runtime_module_free(wasm_module_inst_t module_inst, int32_t ptr);
  280. /**
  281. * Allocate memory from the heap of WASM module instance and initialize
  282. * the memory with src
  283. *
  284. * @param module_inst the WASM module instance which contains heap
  285. * @param src the source data to copy
  286. * @param size the size of the source data
  287. *
  288. * @return the allocated memory address, which is a relative offset to the
  289. * base address of the module instance's memory space, the value range
  290. * is (-heap_size, 0). Note that it is not an absolute address.
  291. * Return non-zero if success, zero if failed.
  292. */
  293. int32_t
  294. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  295. const char *src, uint32_t size);
  296. /**
  297. * Validate the app address, check whether it belongs to WASM module
  298. * instance's address space, or in its heap space or memory space.
  299. *
  300. * @param module_inst the WASM module instance
  301. * @param app_offset the app address to validate, which is a relative address
  302. * @param size the size bytes of the app address
  303. *
  304. * @return true if success, false otherwise. If failed, an exception will
  305. * be thrown.
  306. */
  307. bool
  308. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  309. int32_t app_offset, uint32_t size);
  310. /**
  311. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  312. * is not provided. This function validates the app string address, check whether it
  313. * belongs to WASM module instance's address space, or in its heap space or
  314. * memory space. Moreover, it checks whether it is the offset of a string that
  315. * is end with '\0'.
  316. * @param module_inst the WASM module instance
  317. * @param app_str_offset the app address of the string to validate, which is a
  318. * relative address
  319. *
  320. * @return true if success, false otherwise. If failed, an exception will
  321. * be thrown.
  322. */
  323. bool
  324. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  325. int32_t app_str_offset);
  326. /**
  327. * Validate the native address, check whether it belongs to WASM module
  328. * instance's address space, or in its heap space or memory space.
  329. *
  330. * @param module_inst the WASM module instance
  331. * @param native_ptr the native address to validate, which is an absolute
  332. * address
  333. * @param size the size bytes of the app address
  334. *
  335. * @return true if success, false otherwise. If failed, an exception will
  336. * be thrown.
  337. */
  338. bool
  339. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  340. void *native_ptr, uint32_t size);
  341. /**
  342. * Convert app address(relative address) to native address(absolute address)
  343. *
  344. * @param module_inst the WASM module instance
  345. * @param app_offset the app adress
  346. *
  347. * @return the native address converted
  348. */
  349. void*
  350. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  351. int32_t app_offset);
  352. /**
  353. * Convert native address(absolute address) to app address(relative address)
  354. *
  355. * @param module_inst the WASM module instance
  356. * @param native_ptr the native address
  357. *
  358. * @return the app address converted
  359. */
  360. int32_t
  361. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  362. void *native_ptr);
  363. /**
  364. * Get the app address range (relative address) that a app address belongs to
  365. *
  366. * @param module_inst the WASM module instance
  367. * @param app_offset the app address to retrieve
  368. * @param p_app_start_offset buffer to output the app start offset if not NULL
  369. * @param p_app_end_offset buffer to output the app end offset if not NULL
  370. *
  371. * @return true if success, false otherwise.
  372. */
  373. bool
  374. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  375. int32_t app_offset,
  376. int32_t *p_app_start_offset,
  377. int32_t *p_app_end_offset);
  378. /**
  379. * Get the native address range (absolute address) that a native address belongs to
  380. *
  381. * @param module_inst the WASM module instance
  382. * @param native_ptr the native address to retrieve
  383. * @param p_native_start_addr buffer to output the native start address if not NULL
  384. * @param p_native_end_addr buffer to output the native end address if not NULL
  385. *
  386. * @return true if success, false otherwise.
  387. */
  388. bool
  389. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  390. uint8_t *native_ptr,
  391. uint8_t **p_native_start_addr,
  392. uint8_t **p_native_end_addr);
  393. #ifdef __cplusplus
  394. }
  395. #endif
  396. #endif /* end of _WASM_EXPORT_H */