wasm_export.h 20 KB

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