wasm_export.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. typedef void WASMFunctionInstanceCommon;
  22. typedef WASMFunctionInstanceCommon *wasm_function_inst_t;
  23. /* WASM section */
  24. typedef struct wasm_section_t {
  25. struct wasm_section_t *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, aot_section_t, *wasm_section_list_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. /* Memory allocator type */
  43. typedef enum {
  44. /* pool mode, allocate memory from user defined heap buffer */
  45. Alloc_With_Pool = 0,
  46. /* user allocator mode, allocate memory from user defined
  47. malloc function */
  48. Alloc_With_Allocator,
  49. /* system allocator mode, allocate memory from system allocator,
  50. or, platform's os_malloc function */
  51. Alloc_With_System_Allocator,
  52. } mem_alloc_type_t;
  53. /* Memory allocator option */
  54. typedef union MemAllocOption {
  55. struct {
  56. void *heap_buf;
  57. uint32_t heap_size;
  58. } pool;
  59. struct {
  60. void *malloc_func;
  61. void *realloc_func;
  62. void *free_func;
  63. } allocator;
  64. } MemAllocOption;
  65. /* WASM runtime initialize arguments */
  66. typedef struct RuntimeInitArgs {
  67. mem_alloc_type_t mem_alloc_type;
  68. MemAllocOption mem_alloc_option;
  69. const char *native_module_name;
  70. NativeSymbol *native_symbols;
  71. uint32_t n_native_symbols;
  72. } RuntimeInitArgs;
  73. /**
  74. * Initialize the WASM runtime environment, and also initialize
  75. * the memory allocator with system allocator, which calls os_malloc
  76. * to allocate memory
  77. *
  78. * @return true if success, false otherwise
  79. */
  80. bool
  81. wasm_runtime_init();
  82. /**
  83. * Initialize the WASM runtime environment, and also initialize
  84. * the memory allocator and register native symbols, which are specified
  85. * with init arguments
  86. *
  87. * @param init_args specifies the init arguments
  88. *
  89. * @return return true if success, false otherwise
  90. */
  91. bool
  92. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  93. /**
  94. * Destroy the WASM runtime environment.
  95. */
  96. void
  97. wasm_runtime_destroy();
  98. /**
  99. * Allocate memory from runtime memory environment.
  100. *
  101. * @param size bytes need to allocate
  102. *
  103. * @return the pointer to memory allocated
  104. */
  105. void *
  106. wasm_runtime_malloc(unsigned int size);
  107. /**
  108. * Reallocate memory from runtime memory environment
  109. *
  110. * @param ptr the original memory
  111. * @param size bytes need to reallocate
  112. *
  113. * @return the pointer to memory reallocated
  114. */
  115. void *
  116. wasm_runtime_realloc(void *ptr, unsigned int size);
  117. /*
  118. * Free memory to runtime memory environment.
  119. */
  120. void wasm_runtime_free(void *ptr);
  121. /**
  122. * Get the package type of a buffer.
  123. *
  124. * @param buf the package buffer
  125. * @param size the package buffer size
  126. *
  127. * @return the package type, return Package_Type_Unknown if the type is unknown
  128. */
  129. package_type_t
  130. get_package_type(const uint8_t *buf, uint32_t size);
  131. /**
  132. * Load a WASM module from a specified byte buffer.
  133. *
  134. * @param buf the byte buffer which contains the WASM binary data
  135. * @param size the size of the buffer
  136. * @param error_buf output of the exception info
  137. * @param error_buf_size the size of the exception string
  138. *
  139. * @return return WASM module loaded, NULL if failed
  140. */
  141. wasm_module_t
  142. wasm_runtime_load(const uint8_t *buf, uint32_t size,
  143. char *error_buf, uint32_t error_buf_size);
  144. /**
  145. * Load a WASM module from a specified WASM or AOT section list.
  146. *
  147. * @param section_list the section list which contains each section data
  148. * @param is_aot whether the section list is AOT section list
  149. * @param error_buf output of the exception info
  150. * @param error_buf_size the size of the exception string
  151. *
  152. * @return return WASM module loaded, NULL if failed
  153. */
  154. wasm_module_t
  155. wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
  156. char *error_buf, uint32_t error_buf_size);
  157. /**
  158. * Unload a WASM module.
  159. *
  160. * @param module the module to be unloaded
  161. */
  162. void
  163. wasm_runtime_unload(wasm_module_t module);
  164. void
  165. wasm_runtime_set_wasi_args(wasm_module_t module,
  166. const char *dir_list[], uint32_t dir_count,
  167. const char *map_dir_list[], uint32_t map_dir_count,
  168. const char *env[], uint32_t env_count,
  169. char *argv[], int argc);
  170. /**
  171. * Instantiate a WASM module.
  172. *
  173. * @param module the WASM module to instantiate
  174. * @param stack_size the default stack size of the module instance, a stack
  175. * will be created when function wasm_runtime_call_wasm() is called
  176. * to run WASM function and the exec_env argument passed to
  177. * wasm_runtime_call_wasm() is NULL. That means this parameter is
  178. * ignored if exec_env is not NULL.
  179. * @param heap_size the default heap size of the module instance, a heap will
  180. * be created besides the app memory space. Both wasm app and native
  181. * function can allocate memory from the heap. If heap_size is 0, the
  182. * default heap size will be used.
  183. * @param error_buf buffer to output the error info if failed
  184. * @param error_buf_size the size of the error buffer
  185. *
  186. * @return return the instantiated WASM module instance, NULL if failed
  187. */
  188. wasm_module_inst_t
  189. wasm_runtime_instantiate(const wasm_module_t module,
  190. uint32_t stack_size, uint32_t heap_size,
  191. char *error_buf, uint32_t error_buf_size);
  192. /**
  193. * Deinstantiate a WASM module instance, destroy the resources.
  194. *
  195. * @param module_inst the WASM module instance to destroy
  196. */
  197. void
  198. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  199. bool
  200. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  201. wasm_function_inst_t
  202. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  203. /**
  204. * Lookup an exported function in the WASM module instance.
  205. *
  206. * @param module_inst the module instance
  207. * @param name the name of the function
  208. * @param signature the signature of the function, ignored currently
  209. *
  210. * @return the function instance found
  211. */
  212. wasm_function_inst_t
  213. wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
  214. const char *name, const char *signature);
  215. /**
  216. * Create execution environment for a WASM module instance.
  217. *
  218. * @param module_inst the module instance
  219. * @param stack_size the stack size to execute a WASM function
  220. *
  221. * @return the execution environment
  222. */
  223. wasm_exec_env_t
  224. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  225. uint32_t stack_size);
  226. /**
  227. * Destroy the execution environment.
  228. *
  229. * @param env the execution environment to destroy
  230. */
  231. void
  232. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  233. /**
  234. * Get WASM module instance from execution environment
  235. *
  236. * @param exec_env the execution environment to retrieve
  237. *
  238. * @return the WASM module instance
  239. */
  240. wasm_module_inst_t
  241. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  242. /**
  243. * Call the given WASM function of a WASM module instance with
  244. * arguments (bytecode and AoT).
  245. *
  246. * @param exec_env the execution environment to call the function
  247. * which must be created from wasm_create_exec_env()
  248. * @param function the function to be called
  249. * @param argc the number of arguments
  250. * @param argv the arguments. If the function method has return value,
  251. * the first (or first two in case 64-bit return value) element of
  252. * argv stores the return value of the called WASM function after this
  253. * function returns.
  254. *
  255. * @return true if success, false otherwise and exception will be thrown,
  256. * the caller can call wasm_runtime_get_exception to get exception info.
  257. */
  258. bool
  259. wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
  260. wasm_function_inst_t function,
  261. uint32_t argc, uint32_t argv[]);
  262. /**
  263. * Find the unique main function from a WASM module instance
  264. * and execute that function.
  265. *
  266. * @param module_inst the WASM module instance
  267. * @param argc the number of arguments
  268. * @param argv the arguments array
  269. *
  270. * @return true if the main function is called, false otherwise and exception will be thrown,
  271. * the caller can call wasm_runtime_get_exception to get exception info.
  272. */
  273. bool
  274. wasm_application_execute_main(wasm_module_inst_t module_inst,
  275. int32_t argc, char *argv[]);
  276. /**
  277. * Find the specified function in argv[0] from a WASM module instance
  278. * and execute that function.
  279. *
  280. * @param module_inst the WASM module instance
  281. * @param name the name of the function to execute
  282. * @param argc the number of arguments
  283. * @param argv the arguments array
  284. *
  285. * @return true if the specified function is called, false otherwise and exception will be thrown,
  286. * the caller can call wasm_runtime_get_exception to get exception info.
  287. */
  288. bool
  289. wasm_application_execute_func(wasm_module_inst_t module_inst,
  290. const char *name, int32_t argc, char *argv[]);
  291. /**
  292. * Get exception info of the WASM module instance.
  293. *
  294. * @param module_inst the WASM module instance
  295. *
  296. * @return the exception string
  297. */
  298. const char *
  299. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  300. /**
  301. * Clear exception info of the WASM module instance.
  302. *
  303. * @param module_inst the WASM module instance
  304. */
  305. void
  306. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  307. /**
  308. * Set custom data to WASM module instance.
  309. *
  310. * @param module_inst the WASM module instance
  311. * @param custom_data the custom data to be set
  312. */
  313. void
  314. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  315. void *custom_data);
  316. /**
  317. * Get the custom data within a WASM module instance.
  318. *
  319. * @param module_inst the WASM module instance
  320. *
  321. * @return the custom data (NULL if not set yet)
  322. */
  323. void *
  324. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  325. /**
  326. * Allocate memory from the heap of WASM module instance
  327. *
  328. * @param module_inst the WASM module instance which contains heap
  329. * @param size the size bytes to allocate
  330. * @param p_native_addr return native address of the allocated memory
  331. * if it is not NULL, and return NULL if memory malloc failed
  332. *
  333. * @return the allocated memory address, which is a relative offset to the
  334. * base address of the module instance's memory space, the value range
  335. * is (-heap_size, 0). Note that it is not an absolute address.
  336. * Return non-zero if success, zero if failed.
  337. */
  338. int32_t
  339. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size,
  340. void **p_native_addr);
  341. /**
  342. * Free memory to the heap of WASM module instance
  343. *
  344. * @param module_inst the WASM module instance which contains heap
  345. * @param ptr the pointer to free
  346. */
  347. void
  348. wasm_runtime_module_free(wasm_module_inst_t module_inst, int32_t ptr);
  349. /**
  350. * Allocate memory from the heap of WASM module instance and initialize
  351. * the memory with src
  352. *
  353. * @param module_inst the WASM module instance which contains heap
  354. * @param src the source data to copy
  355. * @param size the size of the source data
  356. *
  357. * @return the allocated memory address, which is a relative offset to the
  358. * base address of the module instance's memory space, the value range
  359. * is (-heap_size, 0). Note that it is not an absolute address.
  360. * Return non-zero if success, zero if failed.
  361. */
  362. int32_t
  363. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  364. const char *src, uint32_t size);
  365. /**
  366. * Validate the app address, check whether it belongs to WASM module
  367. * instance's address space, or in its heap space or memory space.
  368. *
  369. * @param module_inst the WASM module instance
  370. * @param app_offset the app address to validate, which is a relative address
  371. * @param size the size bytes of the app address
  372. *
  373. * @return true if success, false otherwise. If failed, an exception will
  374. * be thrown.
  375. */
  376. bool
  377. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  378. int32_t app_offset, uint32_t size);
  379. /**
  380. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  381. * is not provided. This function validates the app string address, check whether it
  382. * belongs to WASM module instance's address space, or in its heap space or
  383. * memory space. Moreover, it checks whether it is the offset of a string that
  384. * is end with '\0'.
  385. * @param module_inst the WASM module instance
  386. * @param app_str_offset the app address of the string to validate, which is a
  387. * relative address
  388. *
  389. * @return true if success, false otherwise. If failed, an exception will
  390. * be thrown.
  391. */
  392. bool
  393. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  394. int32_t app_str_offset);
  395. /**
  396. * Validate the native address, check whether it belongs to WASM module
  397. * instance's address space, or in its heap space or memory space.
  398. *
  399. * @param module_inst the WASM module instance
  400. * @param native_ptr the native address to validate, which is an absolute
  401. * address
  402. * @param size the size bytes of the app address
  403. *
  404. * @return true if success, false otherwise. If failed, an exception will
  405. * be thrown.
  406. */
  407. bool
  408. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  409. void *native_ptr, uint32_t size);
  410. /**
  411. * Convert app address(relative address) to native address(absolute address)
  412. *
  413. * @param module_inst the WASM module instance
  414. * @param app_offset the app adress
  415. *
  416. * @return the native address converted
  417. */
  418. void*
  419. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  420. int32_t app_offset);
  421. /**
  422. * Convert native address(absolute address) to app address(relative address)
  423. *
  424. * @param module_inst the WASM module instance
  425. * @param native_ptr the native address
  426. *
  427. * @return the app address converted
  428. */
  429. int32_t
  430. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  431. void *native_ptr);
  432. /**
  433. * Get the app address range (relative address) that a app address belongs to
  434. *
  435. * @param module_inst the WASM module instance
  436. * @param app_offset the app address to retrieve
  437. * @param p_app_start_offset buffer to output the app start offset if not NULL
  438. * @param p_app_end_offset buffer to output the app end offset if not NULL
  439. *
  440. * @return true if success, false otherwise.
  441. */
  442. bool
  443. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  444. int32_t app_offset,
  445. int32_t *p_app_start_offset,
  446. int32_t *p_app_end_offset);
  447. /**
  448. * Get the native address range (absolute address) that a native address belongs to
  449. *
  450. * @param module_inst the WASM module instance
  451. * @param native_ptr the native address to retrieve
  452. * @param p_native_start_addr buffer to output the native start address if not NULL
  453. * @param p_native_end_addr buffer to output the native end address if not NULL
  454. *
  455. * @return true if success, false otherwise.
  456. */
  457. bool
  458. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  459. uint8_t *native_ptr,
  460. uint8_t **p_native_start_addr,
  461. uint8_t **p_native_end_addr);
  462. /**
  463. * Register native functions with same module name
  464. *
  465. * @param module_name the module name of the native functions
  466. * @param native_symbols specifies an array of NativeSymbol structures which
  467. * contain the names, function pointers and signatures
  468. * Note: WASM runtime will not allocate memory to clone the data, so
  469. * user must ensure the array can be used forever
  470. * Meanings of letters in function signature:
  471. * 'i': the parameter is i32 type
  472. * 'I': the parameter is i64 type
  473. * 'f': the parameter is f32 type
  474. * 'F': the parameter is f64 type
  475. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  476. * auto check its boundary before calling the native function.
  477. * If it is followed by '~', the checked length of the pointer
  478. * is gotten from the following parameter, if not, the checked
  479. * length of the pointer is 1.
  480. * '~': the parameter is the pointer's length with i32 type, and must
  481. * follow after '*'
  482. * '$': the parameter is a string (i32 in WASM), and runtime will
  483. * auto check its boundary before calling the native function
  484. * @param n_native_symbols specifies the number of native symbols in the array
  485. *
  486. * @return true if success, false otherwise
  487. */
  488. bool wasm_runtime_register_natives(const char *module_name,
  489. NativeSymbol *native_symbols,
  490. uint32_t n_native_symbols);
  491. #ifdef __cplusplus
  492. }
  493. #endif
  494. #endif /* end of _WASM_EXPORT_H */