wasm_export.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. struct WASMModule;
  14. typedef struct WASMModule *wasm_module_t;
  15. /* Instantiated WASM module */
  16. struct WASMModuleInstance;
  17. typedef struct WASMModuleInstance *wasm_module_inst_t;
  18. /* Function instance */
  19. struct WASMFunctionInstance;
  20. typedef struct WASMFunctionInstance *wasm_function_inst_t;
  21. /* WASM section */
  22. typedef struct wasm_section {
  23. struct wasm_section *next;
  24. /* section type */
  25. int section_type;
  26. /* section body, not include type and size */
  27. uint8_t *section_body;
  28. /* section body size */
  29. uint32_t section_body_size;
  30. } wasm_section_t, *wasm_section_list_t;
  31. /* Execution environment, e.g. stack info */
  32. typedef struct WASMExecEnv {
  33. uint8_t *stack;
  34. uint32_t stack_size;
  35. } *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 section list.
  79. *
  80. * @param section_list the section list which contains each section data
  81. * @param error_buf output of the exception info
  82. * @param error_buf_size the size of the exception string
  83. *
  84. * @return return WASM module loaded, NULL if failed
  85. */
  86. wasm_module_t
  87. wasm_runtime_load_from_sections(wasm_section_list_t section_list,
  88. char *error_buf, uint32_t error_buf_size);
  89. /**
  90. * Unload a WASM module.
  91. *
  92. * @param module the module to be unloaded
  93. */
  94. void
  95. wasm_runtime_unload(wasm_module_t module);
  96. #if WASM_ENABLE_WASI != 0
  97. void
  98. wasm_runtime_set_wasi_args(wasm_module_t module,
  99. const char *dir_list[], uint32 dir_count,
  100. const char *map_dir_list[], uint32 map_dir_count,
  101. const char *env[], uint32 env_count,
  102. char *argv[], int argc);
  103. #endif
  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. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  134. bool
  135. wasm_runtime_set_ext_memory(wasm_module_inst_t module_inst,
  136. uint8_t *ext_mem_data, uint32_t ext_mem_size,
  137. char *error_buf, uint32_t error_buf_size);
  138. #endif
  139. /**
  140. * Load WASM module instance from AOT file.
  141. *
  142. * @param aot_file the AOT file of a WASM module
  143. * @param aot_file_size the AOT file size
  144. * @param heap_size the default heap size of the module instance, a heap will
  145. * be created besides the app memory space. Both wasm app and native
  146. * function can allocate memory from the heap. If heap_size is 0, the
  147. * default heap size will be used.
  148. * @param error_buf buffer to output the error info if failed
  149. * @param error_buf_size the size of the error buffer
  150. *
  151. * @return the instantiated WASM module instance, NULL if failed
  152. */
  153. wasm_module_inst_t
  154. wasm_runtime_load_aot(uint8_t *aot_file, uint32_t aot_file_size,
  155. uint32_t heap_size,
  156. char *error_buf, uint32_t error_buf_size);
  157. /**
  158. * Lookup an exported function in the WASM module instance.
  159. *
  160. * @param module_inst the module instance
  161. * @param name the name of the function
  162. * @param signature the signature of the function, use "i32"/"i64"/"f32"/"f64"
  163. * to represent the type of i32/i64/f32/f64, e.g. "(i32i64)" "(i32)f32"
  164. *
  165. * @return the function instance found, if the module instance is loaded from
  166. * the AOT file, the return value is the function pointer
  167. */
  168. wasm_function_inst_t
  169. wasm_runtime_lookup_function(const wasm_module_inst_t module_inst,
  170. const char *name, const char *signature);
  171. /**
  172. * Create execution environment.
  173. *
  174. * @param stack_size the stack size to execute a WASM function
  175. *
  176. * @return the execution environment
  177. */
  178. wasm_exec_env_t
  179. wasm_runtime_create_exec_env(uint32_t stack_size);
  180. /**
  181. * Destroy the execution environment.
  182. *
  183. * @param env the execution environment to destroy
  184. */
  185. void
  186. wasm_runtime_destroy_exec_env(wasm_exec_env_t env);
  187. /**
  188. * Call the given WASM function of a WASM module instance with
  189. * arguments (bytecode and AoT).
  190. *
  191. * @param module_inst the WASM module instance which the function belongs to
  192. * @param exec_env the execution environment to call the function. If the module
  193. * instance is created by AoT mode, it is ignored and just set it to NULL.
  194. * If the module instance is created by bytecode mode and it is NULL,
  195. * a temporary env object will be created
  196. * @param function the function to be called
  197. * @param argc the number of arguments
  198. * @param argv the arguments. If the function method has return value,
  199. * the first (or first two in case 64-bit return value) element of
  200. * argv stores the return value of the called WASM function after this
  201. * function returns.
  202. *
  203. * @return true if success, false otherwise and exception will be thrown,
  204. * the caller can call wasm_runtime_get_exception to get exception info.
  205. */
  206. bool
  207. wasm_runtime_call_wasm(wasm_module_inst_t module_inst,
  208. wasm_exec_env_t exec_env,
  209. wasm_function_inst_t function,
  210. uint32_t argc, uint32_t argv[]);
  211. /**
  212. * Get exception info of the WASM module instance.
  213. *
  214. * @param module_inst the WASM module instance
  215. *
  216. * @return the exception string
  217. */
  218. const char*
  219. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  220. /**
  221. * Clear exception info of the WASM module instance.
  222. *
  223. * @param module_inst the WASM module instance
  224. */
  225. void
  226. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  227. /**
  228. * Set custom data to WASM module instance.
  229. *
  230. * @param module_inst the WASM module instance
  231. * @param custom_data the custom data to be set
  232. */
  233. void
  234. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  235. void *custom_data);
  236. /**
  237. * Get the custom data within a WASM module instance.
  238. *
  239. * @param module_inst the WASM module instance
  240. *
  241. * @return the custom data (NULL if not set yet)
  242. */
  243. void*
  244. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  245. /**
  246. * Allocate memory from the heap of WASM module instance
  247. *
  248. * @param module_inst the WASM module instance which contains heap
  249. * @param size the size bytes to allocate
  250. *
  251. * @return the allocated memory address, which is a relative offset to the
  252. * base address of the module instance's memory space, the value range
  253. * is (-heap_size, 0). Note that it is not an absolute address.
  254. * Return non-zero if success, zero if failed.
  255. */
  256. int32_t
  257. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size);
  258. /**
  259. * Free memory to the heap of WASM module instance
  260. *
  261. * @param module_inst the WASM module instance which contains heap
  262. * @param ptr the pointer to free
  263. */
  264. void
  265. wasm_runtime_module_free(wasm_module_inst_t module_inst, int32_t ptr);
  266. /**
  267. * Allocate memory from the heap of WASM module instance and initialize
  268. * the memory with src
  269. *
  270. * @param module_inst the WASM module instance which contains heap
  271. * @param src the source data to copy
  272. * @param size the size of the source data
  273. *
  274. * @return the allocated memory address, which is a relative offset to the
  275. * base address of the module instance's memory space, the value range
  276. * is (-heap_size, 0). Note that it is not an absolute address.
  277. * Return non-zero if success, zero if failed.
  278. */
  279. int32_t
  280. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  281. const char *src, uint32_t size);
  282. /**
  283. * Validate the app address, check whether it belongs to WASM module
  284. * instance's address space, or in its heap space or memory space.
  285. *
  286. * @param module_inst the WASM module instance
  287. * @param app_offset the app address to validate, which is a relative address
  288. * @param size the size bytes of the app address
  289. *
  290. * @return true if success, false otherwise. If failed, an exception will
  291. * be thrown.
  292. */
  293. bool
  294. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  295. int32_t app_offset, uint32_t size);
  296. /**
  297. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  298. * is not provided. This function validates the app string address, check whether it
  299. * belongs to WASM module instance's address space, or in its heap space or
  300. * memory space. Moreover, it checks whether it is the offset of a string that
  301. * is end with '\0'.
  302. * @param module_inst the WASM module instance
  303. * @param app_str_offset the app address of the string to validate, which is a
  304. * relative address
  305. *
  306. * @return true if success, false otherwise. If failed, an exception will
  307. * be thrown.
  308. */
  309. bool
  310. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  311. int32_t app_str_offset);
  312. /**
  313. * Validate the native address, check whether it belongs to WASM module
  314. * instance's address space, or in its heap space or memory space.
  315. *
  316. * @param module_inst the WASM module instance
  317. * @param native_ptr the native address to validate, which is an absolute
  318. * address
  319. * @param size the size bytes of the app address
  320. *
  321. * @return true if success, false otherwise. If failed, an exception will
  322. * be thrown.
  323. */
  324. bool
  325. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  326. void *native_ptr, uint32_t size);
  327. /**
  328. * Convert app address(relative address) to native address(absolute address)
  329. *
  330. * @param module_inst the WASM module instance
  331. * @param app_offset the app adress
  332. *
  333. * @return the native address converted
  334. */
  335. void *
  336. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  337. int32_t app_offset);
  338. /**
  339. * Convert native address(absolute address) to app address(relative address)
  340. *
  341. * @param module_inst the WASM module instance
  342. * @param native_ptr the native address
  343. *
  344. * @return the app address converted
  345. */
  346. int32_t
  347. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  348. void *native_ptr);
  349. /**
  350. * Get the app address range (relative address) that a app address belongs to
  351. *
  352. * @param module_inst the WASM module instance
  353. * @param app_offset the app address to retrieve
  354. * @param p_app_start_offset buffer to output the app start offset if not NULL
  355. * @param p_app_end_offset buffer to output the app end offset if not NULL
  356. *
  357. * @return true if success, false otherwise.
  358. */
  359. bool
  360. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  361. int32_t app_offset,
  362. int32_t *p_app_start_offset,
  363. int32_t *p_app_end_offset);
  364. /**
  365. * Get the native address range (absolute address) that a native address belongs to
  366. *
  367. * @param module_inst the WASM module instance
  368. * @param native_ptr the native address to retrieve
  369. * @param p_native_start_addr buffer to output the native start address if not NULL
  370. * @param p_native_end_addr buffer to output the native end address if not NULL
  371. *
  372. * @return true if success, false otherwise.
  373. */
  374. bool
  375. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  376. uint8_t *native_ptr,
  377. uint8_t **p_native_start_addr,
  378. uint8_t **p_native_end_addr);
  379. #ifdef __cplusplus
  380. }
  381. #endif
  382. #endif /* end of _WASM_EXPORT_H */