wasm_export.h 18 KB

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