wasm_export.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. The byte buffer can be
  150. * WASM binary data when interpreter or JIT is enabled, or AOT binary data
  151. * when AOT is enabled. If it is AOT binary data, it must be 4-byte aligned.
  152. *
  153. * @param buf the byte buffer which contains the WASM binary data
  154. * @param size the size of the buffer
  155. * @param error_buf output of the exception info
  156. * @param error_buf_size the size of the exception string
  157. *
  158. * @return return WASM module loaded, NULL if failed
  159. */
  160. wasm_module_t
  161. wasm_runtime_load(const uint8_t *buf, uint32_t size,
  162. char *error_buf, uint32_t error_buf_size);
  163. /**
  164. * Load a WASM module from a specified WASM or AOT section list.
  165. *
  166. * @param section_list the section list which contains each section data
  167. * @param is_aot whether the section list is AOT section list
  168. * @param error_buf output of the exception info
  169. * @param error_buf_size the size of the exception string
  170. *
  171. * @return return WASM module loaded, NULL if failed
  172. */
  173. wasm_module_t
  174. wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
  175. char *error_buf, uint32_t error_buf_size);
  176. /**
  177. * Unload a WASM module.
  178. *
  179. * @param module the module to be unloaded
  180. */
  181. void
  182. wasm_runtime_unload(wasm_module_t module);
  183. void
  184. wasm_runtime_set_wasi_args(wasm_module_t module,
  185. const char *dir_list[], uint32_t dir_count,
  186. const char *map_dir_list[], uint32_t map_dir_count,
  187. const char *env[], uint32_t env_count,
  188. char *argv[], int argc);
  189. /**
  190. * Instantiate a WASM module.
  191. *
  192. * @param module the WASM module to instantiate
  193. * @param stack_size the default stack size of the module instance when the
  194. * exec env's operation stack isn't created by user, e.g. API
  195. * wasm_application_execute_main() and wasm_application_execute_func()
  196. * create the operation stack internally with the stack size specified
  197. * here. And API wasm_runtime_create_exec_env() creates the operation
  198. * stack with stack size specified by its parameter, the stack size
  199. * specified here is ignored.
  200. * @param heap_size the default heap size of the module instance, a heap will
  201. * be created besides the app memory space. Both wasm app and native
  202. * function can allocate memory from the heap. If heap_size is 0, the
  203. * default heap size will be used.
  204. * @param error_buf buffer to output the error info if failed
  205. * @param error_buf_size the size of the error buffer
  206. *
  207. * @return return the instantiated WASM module instance, NULL if failed
  208. */
  209. wasm_module_inst_t
  210. wasm_runtime_instantiate(const wasm_module_t module,
  211. uint32_t stack_size, uint32_t heap_size,
  212. char *error_buf, uint32_t error_buf_size);
  213. /**
  214. * Deinstantiate a WASM module instance, destroy the resources.
  215. *
  216. * @param module_inst the WASM module instance to destroy
  217. */
  218. void
  219. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  220. bool
  221. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  222. wasm_function_inst_t
  223. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  224. /**
  225. * Lookup an exported function in the WASM module instance.
  226. *
  227. * @param module_inst the module instance
  228. * @param name the name of the function
  229. * @param signature the signature of the function, ignored currently
  230. *
  231. * @return the function instance found
  232. */
  233. wasm_function_inst_t
  234. wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
  235. const char *name, const char *signature);
  236. /**
  237. * Create execution environment for a WASM module instance.
  238. *
  239. * @param module_inst the module instance
  240. * @param stack_size the stack size to execute a WASM function
  241. *
  242. * @return the execution environment
  243. */
  244. wasm_exec_env_t
  245. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  246. uint32_t stack_size);
  247. /**
  248. * Destroy the execution environment.
  249. *
  250. * @param exec_env the execution environment to destroy
  251. */
  252. void
  253. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  254. /**
  255. * Get WASM module instance from execution environment
  256. *
  257. * @param exec_env the execution environment to retrieve
  258. *
  259. * @return the WASM module instance
  260. */
  261. wasm_module_inst_t
  262. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  263. /**
  264. * Call the given WASM function of a WASM module instance with
  265. * arguments (bytecode and AoT).
  266. *
  267. * @param exec_env the execution environment to call the function,
  268. * which must be created from wasm_create_exec_env()
  269. * @param function the function to call
  270. * @param argc the number of arguments
  271. * @param argv the arguments. If the function has return value,
  272. * the first (or first two in case 64-bit return value) element of
  273. * argv stores the return value of the called WASM function after this
  274. * function returns.
  275. *
  276. * @return true if success, false otherwise and exception will be thrown,
  277. * the caller can call wasm_runtime_get_exception to get the exception
  278. * info.
  279. */
  280. bool
  281. wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
  282. wasm_function_inst_t function,
  283. uint32_t argc, uint32_t argv[]);
  284. /**
  285. * Find the unique main function from a WASM module instance
  286. * and execute that function.
  287. *
  288. * @param module_inst the WASM module instance
  289. * @param argc the number of arguments
  290. * @param argv the arguments array
  291. *
  292. * @return true if the main function is called, false otherwise and exception
  293. * will be thrown, the caller can call wasm_runtime_get_exception to get
  294. * the exception info.
  295. */
  296. bool
  297. wasm_application_execute_main(wasm_module_inst_t module_inst,
  298. int32_t argc, char *argv[]);
  299. /**
  300. * Find the specified function in argv[0] from a WASM module instance
  301. * and execute that function.
  302. *
  303. * @param module_inst the WASM module instance
  304. * @param name the name of the function to execute
  305. * @param argc the number of arguments
  306. * @param argv the arguments array
  307. *
  308. * @return true if the specified function is called, false otherwise and
  309. * exception will be thrown, the caller can call wasm_runtime_get_exception
  310. * to get the exception info.
  311. */
  312. bool
  313. wasm_application_execute_func(wasm_module_inst_t module_inst,
  314. const char *name, int32_t argc, char *argv[]);
  315. /**
  316. * Get exception info of the WASM module instance.
  317. *
  318. * @param module_inst the WASM module instance
  319. *
  320. * @return the exception string
  321. */
  322. const char *
  323. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  324. /**
  325. * Set exception info of the WASM module instance.
  326. *
  327. * @param module_inst the WASM module instance
  328. *
  329. * @param exception the exception string
  330. */
  331. void
  332. wasm_runtime_set_exception(wasm_module_inst_t module_inst,
  333. const char *exception);
  334. /**
  335. * Clear exception info of the WASM module instance.
  336. *
  337. * @param module_inst the WASM module instance
  338. */
  339. void
  340. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  341. /**
  342. * Set custom data to WASM module instance.
  343. *
  344. * @param module_inst the WASM module instance
  345. * @param custom_data the custom data to be set
  346. */
  347. void
  348. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  349. void *custom_data);
  350. /**
  351. * Get the custom data within a WASM module instance.
  352. *
  353. * @param module_inst the WASM module instance
  354. *
  355. * @return the custom data (NULL if not set yet)
  356. */
  357. void *
  358. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  359. /**
  360. * Allocate memory from the heap of WASM module instance
  361. *
  362. * @param module_inst the WASM module instance which contains heap
  363. * @param size the size bytes to allocate
  364. * @param p_native_addr return native address of the allocated memory
  365. * if it is not NULL, and return NULL if memory malloc failed
  366. *
  367. * @return the allocated memory address, which is a relative offset to the
  368. * base address of the module instance's memory space, the value range
  369. * is (-heap_size, 0). Note that it is not an absolute address.
  370. * Return non-zero if success, zero if failed.
  371. */
  372. int32_t
  373. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size,
  374. void **p_native_addr);
  375. /**
  376. * Free memory to the heap of WASM module instance
  377. *
  378. * @param module_inst the WASM module instance which contains heap
  379. * @param ptr the pointer to free
  380. */
  381. void
  382. wasm_runtime_module_free(wasm_module_inst_t module_inst, int32_t ptr);
  383. /**
  384. * Allocate memory from the heap of WASM module instance and initialize
  385. * the memory with src
  386. *
  387. * @param module_inst the WASM module instance which contains heap
  388. * @param src the source data to copy
  389. * @param size the size of the source data
  390. *
  391. * @return the allocated memory address, which is a relative offset to the
  392. * base address of the module instance's memory space, the value range
  393. * is (-heap_size, 0). Note that it is not an absolute address.
  394. * Return non-zero if success, zero if failed.
  395. */
  396. int32_t
  397. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  398. const char *src, uint32_t size);
  399. /**
  400. * Validate the app address, check whether it belongs to WASM module
  401. * instance's address space, or in its heap space or memory space.
  402. *
  403. * @param module_inst the WASM module instance
  404. * @param app_offset the app address to validate, which is a relative address
  405. * @param size the size bytes of the app address
  406. *
  407. * @return true if success, false otherwise. If failed, an exception will
  408. * be thrown.
  409. */
  410. bool
  411. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  412. int32_t app_offset, uint32_t size);
  413. /**
  414. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  415. * is not provided. This function validates the app string address, check whether it
  416. * belongs to WASM module instance's address space, or in its heap space or
  417. * memory space. Moreover, it checks whether it is the offset of a string that
  418. * is end with '\0'.
  419. * @param module_inst the WASM module instance
  420. * @param app_str_offset the app address of the string to validate, which is a
  421. * relative address
  422. *
  423. * @return true if success, false otherwise. If failed, an exception will
  424. * be thrown.
  425. */
  426. bool
  427. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  428. int32_t app_str_offset);
  429. /**
  430. * Validate the native address, check whether it belongs to WASM module
  431. * instance's address space, or in its heap space or memory space.
  432. *
  433. * @param module_inst the WASM module instance
  434. * @param native_ptr the native address to validate, which is an absolute
  435. * address
  436. * @param size the size bytes of the app address
  437. *
  438. * @return true if success, false otherwise. If failed, an exception will
  439. * be thrown.
  440. */
  441. bool
  442. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  443. void *native_ptr, uint32_t size);
  444. /**
  445. * Convert app address(relative address) to native address(absolute address)
  446. *
  447. * @param module_inst the WASM module instance
  448. * @param app_offset the app adress
  449. *
  450. * @return the native address converted
  451. */
  452. void*
  453. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  454. int32_t app_offset);
  455. /**
  456. * Convert native address(absolute address) to app address(relative address)
  457. *
  458. * @param module_inst the WASM module instance
  459. * @param native_ptr the native address
  460. *
  461. * @return the app address converted
  462. */
  463. int32_t
  464. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  465. void *native_ptr);
  466. /**
  467. * Get the app address range (relative address) that a app address belongs to
  468. *
  469. * @param module_inst the WASM module instance
  470. * @param app_offset the app address to retrieve
  471. * @param p_app_start_offset buffer to output the app start offset if not NULL
  472. * @param p_app_end_offset buffer to output the app end offset if not NULL
  473. *
  474. * @return true if success, false otherwise.
  475. */
  476. bool
  477. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  478. int32_t app_offset,
  479. int32_t *p_app_start_offset,
  480. int32_t *p_app_end_offset);
  481. /**
  482. * Get the native address range (absolute address) that a native address belongs to
  483. *
  484. * @param module_inst the WASM module instance
  485. * @param native_ptr the native address to retrieve
  486. * @param p_native_start_addr buffer to output the native start address if not NULL
  487. * @param p_native_end_addr buffer to output the native end address if not NULL
  488. *
  489. * @return true if success, false otherwise.
  490. */
  491. bool
  492. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  493. uint8_t *native_ptr,
  494. uint8_t **p_native_start_addr,
  495. uint8_t **p_native_end_addr);
  496. /**
  497. * Register native functions with same module name
  498. *
  499. * @param module_name the module name of the native functions
  500. * @param native_symbols specifies an array of NativeSymbol structures which
  501. * contain the names, function pointers and signatures
  502. * Note: WASM runtime will not allocate memory to clone the data, so
  503. * user must ensure the array can be used forever
  504. * Meanings of letters in function signature:
  505. * 'i': the parameter is i32 type
  506. * 'I': the parameter is i64 type
  507. * 'f': the parameter is f32 type
  508. * 'F': the parameter is f64 type
  509. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  510. * auto check its boundary before calling the native function.
  511. * If it is followed by '~', the checked length of the pointer
  512. * is gotten from the following parameter, if not, the checked
  513. * length of the pointer is 1.
  514. * '~': the parameter is the pointer's length with i32 type, and must
  515. * follow after '*'
  516. * '$': the parameter is a string (i32 in WASM), and runtime will
  517. * auto check its boundary before calling the native function
  518. * @param n_native_symbols specifies the number of native symbols in the array
  519. *
  520. * @return true if success, false otherwise
  521. */
  522. bool wasm_runtime_register_natives(const char *module_name,
  523. NativeSymbol *native_symbols,
  524. uint32_t n_native_symbols);
  525. /**
  526. * Register native functions with same module name, similar to
  527. * wasm_runtime_register_natives, the difference is that runtime passes raw
  528. * arguments to native API, which means that the native API should be defined as:
  529. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  530. * and native API should extract arguments one by one from args array with macro
  531. * native_raw_get_arg
  532. * and write the return value back to args[0] with macro
  533. * native_raw_return_type and native_raw_set_return
  534. */
  535. bool wasm_runtime_register_natives_raw(const char *module_name,
  536. NativeSymbol *native_symbols,
  537. uint32_t n_native_symbols);
  538. /**
  539. * Get attachment of native function from execution environment
  540. *
  541. * @param exec_env the execution environment to retrieve
  542. *
  543. * @return the attachment of native function
  544. */
  545. void *
  546. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  547. /**
  548. * Set user data to execution environment.
  549. *
  550. * @param exec_env the execution environment
  551. * @param user_data the user data to be set
  552. */
  553. void
  554. wasm_runtime_set_user_data(wasm_exec_env_t exec_env,
  555. void *user_data);
  556. /**
  557. * Get the user data within execution environment.
  558. *
  559. * @param exec_env the execution environment
  560. *
  561. * @return the user data (NULL if not set yet)
  562. */
  563. void *
  564. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  565. #ifdef __cplusplus
  566. }
  567. #endif
  568. #endif /* end of _WASM_EXPORT_H */