wasm_export.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. #include "lib_export.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /* Uninstantiated WASM module loaded from WASM binary file
  14. or AoT binary file*/
  15. struct WASMModuleCommon;
  16. typedef struct WASMModuleCommon *wasm_module_t;
  17. /* Instantiated WASM module */
  18. struct WASMModuleInstanceCommon;
  19. typedef struct WASMModuleInstanceCommon *wasm_module_inst_t;
  20. /* Function instance */
  21. struct WASMFunctionInstanceCommon;
  22. typedef struct WASMFunctionInstanceCommon *wasm_function_inst_t;
  23. /* WASM section */
  24. typedef struct wasm_section {
  25. struct wasm_section *next;
  26. /* section type */
  27. int section_type;
  28. /* section body, not include type and size */
  29. uint8_t *section_body;
  30. /* section body size */
  31. uint32_t section_body_size;
  32. } wasm_section_t, *wasm_section_list_t;
  33. typedef wasm_section_t aot_section_t, *aot_section_list_t;
  34. /* Execution environment, e.g. stack info */
  35. struct WASMExecEnv;
  36. typedef struct WASMExecEnv *wasm_exec_env_t;
  37. /* Package Type */
  38. typedef enum {
  39. Wasm_Module_Bytecode = 0,
  40. Wasm_Module_AoT,
  41. Package_Type_Unknown = 0xFFFF
  42. } package_type_t;
  43. /**
  44. * Initialize the WASM runtime environment.
  45. *
  46. * @return true if success, false otherwise
  47. */
  48. bool
  49. wasm_runtime_init();
  50. /**
  51. * Destroy the WASM runtime environment.
  52. */
  53. void
  54. wasm_runtime_destroy();
  55. /**
  56. * Get the package type of a buffer.
  57. *
  58. * @param buf the package buffer
  59. * @param size the package buffer size
  60. *
  61. * @return the package type, return Package_Type_Unknown if the type is unknown
  62. */
  63. package_type_t
  64. get_package_type(const uint8_t *buf, uint32_t size);
  65. /**
  66. * Load a WASM module from a specified byte buffer.
  67. *
  68. * @param buf the byte buffer which contains the WASM binary data
  69. * @param size the size of the buffer
  70. * @param error_buf output of the exception info
  71. * @param error_buf_size the size of the exception string
  72. *
  73. * @return return WASM module loaded, NULL if failed
  74. */
  75. wasm_module_t
  76. wasm_runtime_load(const uint8_t *buf, uint32_t size,
  77. char *error_buf, uint32_t error_buf_size);
  78. /**
  79. * Load a WASM module from a specified WASM or AOT section list.
  80. *
  81. * @param section_list the section list which contains each section data
  82. * @param is_aot whether the section list is AOT section list
  83. * @param error_buf output of the exception info
  84. * @param error_buf_size the size of the exception string
  85. *
  86. * @return return WASM module loaded, NULL if failed
  87. */
  88. wasm_module_t
  89. wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
  90. char *error_buf, uint32_t error_buf_size);
  91. /**
  92. * Unload a WASM module.
  93. *
  94. * @param module the module to be unloaded
  95. */
  96. void
  97. wasm_runtime_unload(wasm_module_t module);
  98. void
  99. wasm_runtime_set_wasi_args(wasm_module_t module,
  100. const char *dir_list[], uint32_t dir_count,
  101. const char *map_dir_list[], uint32_t map_dir_count,
  102. const char *env[], uint32_t env_count,
  103. char *argv[], int argc);
  104. /**
  105. * Instantiate a WASM module.
  106. *
  107. * @param module the WASM module to instantiate
  108. * @param stack_size the default stack size of the module instance, a stack
  109. * will be created when function wasm_runtime_call_wasm() is called
  110. * to run WASM function and the exec_env argument passed to
  111. * wasm_runtime_call_wasm() is NULL. That means this parameter is
  112. * ignored if exec_env is not NULL.
  113. * @param heap_size the default heap size of the module instance, a heap will
  114. * be created besides the app memory space. Both wasm app and native
  115. * function can allocate memory from the heap. If heap_size is 0, the
  116. * default heap size will be used.
  117. * @param error_buf buffer to output the error info if failed
  118. * @param error_buf_size the size of the error buffer
  119. *
  120. * @return return the instantiated WASM module instance, NULL if failed
  121. */
  122. wasm_module_inst_t
  123. wasm_runtime_instantiate(const wasm_module_t module,
  124. uint32_t stack_size, uint32_t heap_size,
  125. char *error_buf, uint32_t error_buf_size);
  126. /**
  127. * Deinstantiate a WASM module instance, destroy the resources.
  128. *
  129. * @param module_inst the WASM module instance to destroy
  130. */
  131. void
  132. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  133. bool
  134. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  135. wasm_function_inst_t
  136. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  137. /**
  138. * Lookup an exported function in the WASM module instance.
  139. *
  140. * @param module_inst the module instance
  141. * @param name the name of the function
  142. * @param signature the signature of the function, ignored currently
  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. * @param p_native_addr return native address of the allocated memory
  265. * if it is not NULL, and return NULL if memory malloc failed
  266. *
  267. * @return the allocated memory address, which is a relative offset to the
  268. * base address of the module instance's memory space, the value range
  269. * is (-heap_size, 0). Note that it is not an absolute address.
  270. * Return non-zero if success, zero if failed.
  271. */
  272. int32_t
  273. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size,
  274. void **p_native_addr);
  275. /**
  276. * Free memory to the heap of WASM module instance
  277. *
  278. * @param module_inst the WASM module instance which contains heap
  279. * @param ptr the pointer to free
  280. */
  281. void
  282. wasm_runtime_module_free(wasm_module_inst_t module_inst, int32_t ptr);
  283. /**
  284. * Allocate memory from the heap of WASM module instance and initialize
  285. * the memory with src
  286. *
  287. * @param module_inst the WASM module instance which contains heap
  288. * @param src the source data to copy
  289. * @param size the size of the source data
  290. *
  291. * @return the allocated memory address, which is a relative offset to the
  292. * base address of the module instance's memory space, the value range
  293. * is (-heap_size, 0). Note that it is not an absolute address.
  294. * Return non-zero if success, zero if failed.
  295. */
  296. int32_t
  297. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  298. const char *src, uint32_t size);
  299. /**
  300. * Validate the app address, check whether it belongs to WASM module
  301. * instance's address space, or in its heap space or memory space.
  302. *
  303. * @param module_inst the WASM module instance
  304. * @param app_offset the app address to validate, which is a relative address
  305. * @param size the size bytes of the app address
  306. *
  307. * @return true if success, false otherwise. If failed, an exception will
  308. * be thrown.
  309. */
  310. bool
  311. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  312. int32_t app_offset, uint32_t size);
  313. /**
  314. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  315. * is not provided. This function validates the app string address, check whether it
  316. * belongs to WASM module instance's address space, or in its heap space or
  317. * memory space. Moreover, it checks whether it is the offset of a string that
  318. * is end with '\0'.
  319. * @param module_inst the WASM module instance
  320. * @param app_str_offset the app address of the string to validate, which is a
  321. * relative address
  322. *
  323. * @return true if success, false otherwise. If failed, an exception will
  324. * be thrown.
  325. */
  326. bool
  327. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  328. int32_t app_str_offset);
  329. /**
  330. * Validate the native address, check whether it belongs to WASM module
  331. * instance's address space, or in its heap space or memory space.
  332. *
  333. * @param module_inst the WASM module instance
  334. * @param native_ptr the native address to validate, which is an absolute
  335. * address
  336. * @param size the size bytes of the app address
  337. *
  338. * @return true if success, false otherwise. If failed, an exception will
  339. * be thrown.
  340. */
  341. bool
  342. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  343. void *native_ptr, uint32_t size);
  344. /**
  345. * Convert app address(relative address) to native address(absolute address)
  346. *
  347. * @param module_inst the WASM module instance
  348. * @param app_offset the app adress
  349. *
  350. * @return the native address converted
  351. */
  352. void*
  353. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  354. int32_t app_offset);
  355. /**
  356. * Convert native address(absolute address) to app address(relative address)
  357. *
  358. * @param module_inst the WASM module instance
  359. * @param native_ptr the native address
  360. *
  361. * @return the app address converted
  362. */
  363. int32_t
  364. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  365. void *native_ptr);
  366. /**
  367. * Get the app address range (relative address) that a app address belongs to
  368. *
  369. * @param module_inst the WASM module instance
  370. * @param app_offset the app address to retrieve
  371. * @param p_app_start_offset buffer to output the app start offset if not NULL
  372. * @param p_app_end_offset buffer to output the app end offset if not NULL
  373. *
  374. * @return true if success, false otherwise.
  375. */
  376. bool
  377. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  378. int32_t app_offset,
  379. int32_t *p_app_start_offset,
  380. int32_t *p_app_end_offset);
  381. /**
  382. * Get the native address range (absolute address) that a native address belongs to
  383. *
  384. * @param module_inst the WASM module instance
  385. * @param native_ptr the native address to retrieve
  386. * @param p_native_start_addr buffer to output the native start address if not NULL
  387. * @param p_native_end_addr buffer to output the native end address if not NULL
  388. *
  389. * @return true if success, false otherwise.
  390. */
  391. bool
  392. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  393. uint8_t *native_ptr,
  394. uint8_t **p_native_start_addr,
  395. uint8_t **p_native_end_addr);
  396. /**
  397. * Register native functions with same module name
  398. *
  399. * @param module_name the module name of the native functions
  400. * @param native_symbols specifies an array of NativeSymbol structures which
  401. * contain the names, function pointers and signatures
  402. * Note: WASM runtime will not allocate memory to clone the data, so
  403. * user must ensure the array can be used forever
  404. * Meanings of letters in function signature:
  405. * 'i': the parameter is i32 type
  406. * 'I': the parameter is i64 type
  407. * 'f': the parameter is f32 type
  408. * 'F': the parameter is f64 type
  409. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  410. * auto check its boundary before calling the native function.
  411. * If it is followed by '~', the checked length of the pointer
  412. * is gotten from the following parameter, if not, the checked
  413. * length of the pointer is 1.
  414. * '~': the parameter is the pointer's length with i32 type, and must
  415. * follow after '*'
  416. * '$': the parameter is a string (i32 in WASM), and runtime will
  417. * auto check its boundary before calling the native function
  418. * @param n_native_symbols specifies the number of native symbols in the array
  419. *
  420. * @return true if success, false otherwise
  421. */
  422. bool wasm_runtime_register_natives(const char *module_name,
  423. NativeSymbol *native_symbols,
  424. uint32_t n_native_symbols);
  425. #ifdef __cplusplus
  426. }
  427. #endif
  428. #endif /* end of _WASM_EXPORT_H */