wasm_export.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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 <stdint.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. #ifndef WASM_MODULE_T_DEFINED
  31. #define WASM_MODULE_T_DEFINED
  32. /* Uninstantiated WASM module loaded from WASM binary file
  33. or AoT binary file*/
  34. struct WASMModuleCommon;
  35. typedef struct WASMModuleCommon *wasm_module_t;
  36. #endif
  37. /* Instantiated WASM module */
  38. struct WASMModuleInstanceCommon;
  39. typedef struct WASMModuleInstanceCommon *wasm_module_inst_t;
  40. /* Function instance */
  41. typedef void WASMFunctionInstanceCommon;
  42. typedef WASMFunctionInstanceCommon *wasm_function_inst_t;
  43. /* WASM section */
  44. typedef struct wasm_section_t {
  45. struct wasm_section_t *next;
  46. /* section type */
  47. int section_type;
  48. /* section body, not include type and size */
  49. uint8_t *section_body;
  50. /* section body size */
  51. uint32_t section_body_size;
  52. } wasm_section_t, aot_section_t, *wasm_section_list_t, *aot_section_list_t;
  53. /* Execution environment, e.g. stack info */
  54. struct WASMExecEnv;
  55. typedef struct WASMExecEnv *wasm_exec_env_t;
  56. /* Package Type */
  57. typedef enum {
  58. Wasm_Module_Bytecode = 0,
  59. Wasm_Module_AoT,
  60. Package_Type_Unknown = 0xFFFF
  61. } package_type_t;
  62. /* Memory allocator type */
  63. typedef enum {
  64. /* pool mode, allocate memory from user defined heap buffer */
  65. Alloc_With_Pool = 0,
  66. /* user allocator mode, allocate memory from user defined
  67. malloc function */
  68. Alloc_With_Allocator,
  69. /* system allocator mode, allocate memory from system allocator,
  70. or, platform's os_malloc function */
  71. Alloc_With_System_Allocator,
  72. } mem_alloc_type_t;
  73. /* Memory allocator option */
  74. typedef union MemAllocOption {
  75. struct {
  76. void *heap_buf;
  77. uint32_t heap_size;
  78. } pool;
  79. struct {
  80. void *malloc_func;
  81. void *realloc_func;
  82. void *free_func;
  83. } allocator;
  84. } MemAllocOption;
  85. /* WASM runtime initialize arguments */
  86. typedef struct RuntimeInitArgs {
  87. mem_alloc_type_t mem_alloc_type;
  88. MemAllocOption mem_alloc_option;
  89. const char *native_module_name;
  90. NativeSymbol *native_symbols;
  91. uint32_t n_native_symbols;
  92. /* maximum thread number, only used when
  93. WASM_ENABLE_THREAD_MGR is defined */
  94. uint32_t max_thread_num;
  95. } RuntimeInitArgs;
  96. /**
  97. * Initialize the WASM runtime environment, and also initialize
  98. * the memory allocator with system allocator, which calls os_malloc
  99. * to allocate memory
  100. *
  101. * @return true if success, false otherwise
  102. */
  103. bool
  104. wasm_runtime_init(void);
  105. /**
  106. * Initialize the WASM runtime environment, and also initialize
  107. * the memory allocator and register native symbols, which are specified
  108. * with init arguments
  109. *
  110. * @param init_args specifies the init arguments
  111. *
  112. * @return return true if success, false otherwise
  113. */
  114. bool
  115. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  116. /**
  117. * Destroy the WASM runtime environment.
  118. */
  119. void
  120. wasm_runtime_destroy(void);
  121. /**
  122. * Allocate memory from runtime memory environment.
  123. *
  124. * @param size bytes need to allocate
  125. *
  126. * @return the pointer to memory allocated
  127. */
  128. void *
  129. wasm_runtime_malloc(unsigned int size);
  130. /**
  131. * Reallocate memory from runtime memory environment
  132. *
  133. * @param ptr the original memory
  134. * @param size bytes need to reallocate
  135. *
  136. * @return the pointer to memory reallocated
  137. */
  138. void *
  139. wasm_runtime_realloc(void *ptr, unsigned int size);
  140. /*
  141. * Free memory to runtime memory environment.
  142. */
  143. void wasm_runtime_free(void *ptr);
  144. /**
  145. * Get the package type of a buffer.
  146. *
  147. * @param buf the package buffer
  148. * @param size the package buffer size
  149. *
  150. * @return the package type, return Package_Type_Unknown if the type is unknown
  151. */
  152. package_type_t
  153. get_package_type(const uint8_t *buf, uint32_t size);
  154. #if WASM_ENABLE_MULTI_MODULE != 0
  155. /**
  156. * It is a callback for WAMR providing by embedding to load a module file
  157. * into a buffer
  158. */
  159. typedef bool (*module_reader)(const char *module_name,
  160. uint8_t **p_buffer, uint32_t *p_size);
  161. /**
  162. * It is a callback for WAMR providing by embedding to release the buffer which
  163. * is used by loading a module file
  164. */
  165. typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size);
  166. /**
  167. * To setup callbacks for reading and releasing a buffer about a module file
  168. *
  169. * @param reader a callback to read a module file into a buffer
  170. * @param destroyer a callback to release above buffer
  171. */
  172. void
  173. wasm_runtime_set_module_reader(const module_reader reader,
  174. const module_destroyer destroyer);
  175. /**
  176. * Give the "module" a name "module_name".
  177. * can not assign a new name to a module if it already has a name
  178. *
  179. * @param module_name indicate a name
  180. * @param module the target module
  181. * @param error_buf output of the exception info
  182. * @param error_buf_size the size of the exception string
  183. *
  184. * @return true means success, false means failed
  185. */
  186. bool
  187. wasm_runtime_register_module(const char *module_name, wasm_module_t module,
  188. char *error_buf, uint32_t error_buf_size);
  189. /**
  190. * To check if there is already a loaded module named module_name in the
  191. * runtime. you will not want to load repeately
  192. *
  193. * @param module_name indicate a name
  194. *
  195. * @return return WASM module loaded, NULL if failed
  196. */
  197. wasm_module_t
  198. wasm_runtime_find_module_registered(const char *module_name);
  199. #endif /* WASM_ENABLE_MULTI_MODULE */
  200. /**
  201. * Load a WASM module from a specified byte buffer. The byte buffer can be
  202. * WASM binary data when interpreter or JIT is enabled, or AOT binary data
  203. * when AOT is enabled. If it is AOT binary data, it must be 4-byte aligned.
  204. *
  205. * @param buf the byte buffer which contains the WASM binary data
  206. * @param size the size of the buffer
  207. * @param error_buf output of the exception info
  208. * @param error_buf_size the size of the exception string
  209. *
  210. * @return return WASM module loaded, NULL if failed
  211. */
  212. wasm_module_t
  213. wasm_runtime_load(const uint8_t *buf, uint32_t size,
  214. char *error_buf, uint32_t error_buf_size);
  215. /**
  216. * Load a WASM module from a specified WASM or AOT section list.
  217. *
  218. * @param section_list the section list which contains each section data
  219. * @param is_aot whether the section list is AOT section list
  220. * @param error_buf output of the exception info
  221. * @param error_buf_size the size of the exception string
  222. *
  223. * @return return WASM module loaded, NULL if failed
  224. */
  225. wasm_module_t
  226. wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
  227. char *error_buf, uint32_t error_buf_size);
  228. /**
  229. * Unload a WASM module.
  230. *
  231. * @param module the module to be unloaded
  232. */
  233. void
  234. wasm_runtime_unload(wasm_module_t module);
  235. void
  236. wasm_runtime_set_wasi_args(wasm_module_t module,
  237. const char *dir_list[], uint32_t dir_count,
  238. const char *map_dir_list[], uint32_t map_dir_count,
  239. const char *env[], uint32_t env_count,
  240. char *argv[], int argc);
  241. /**
  242. * Instantiate a WASM module.
  243. *
  244. * @param module the WASM module to instantiate
  245. * @param stack_size the default stack size of the module instance when the
  246. * exec env's operation stack isn't created by user, e.g. API
  247. * wasm_application_execute_main() and wasm_application_execute_func()
  248. * create the operation stack internally with the stack size specified
  249. * here. And API wasm_runtime_create_exec_env() creates the operation
  250. * stack with stack size specified by its parameter, the stack size
  251. * specified here is ignored.
  252. * @param heap_size the default heap size of the module instance, a heap will
  253. * be created besides the app memory space. Both wasm app and native
  254. * function can allocate memory from the heap. If heap_size is 0, the
  255. * default heap size will be used.
  256. * @param error_buf buffer to output the error info if failed
  257. * @param error_buf_size the size of the error buffer
  258. *
  259. * @return return the instantiated WASM module instance, NULL if failed
  260. */
  261. wasm_module_inst_t
  262. wasm_runtime_instantiate(const wasm_module_t module,
  263. uint32_t stack_size, uint32_t heap_size,
  264. char *error_buf, uint32_t error_buf_size);
  265. /**
  266. * Deinstantiate a WASM module instance, destroy the resources.
  267. *
  268. * @param module_inst the WASM module instance to destroy
  269. */
  270. void
  271. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  272. bool
  273. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  274. wasm_function_inst_t
  275. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  276. /**
  277. * Lookup an exported function in the WASM module instance.
  278. *
  279. * @param module_inst the module instance
  280. * @param name the name of the function
  281. * @param signature the signature of the function, ignored currently
  282. *
  283. * @return the function instance found, NULL if not found
  284. */
  285. wasm_function_inst_t
  286. wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
  287. const char *name, const char *signature);
  288. /**
  289. * Create execution environment for a WASM module instance.
  290. *
  291. * @param module_inst the module instance
  292. * @param stack_size the stack size to execute a WASM function
  293. *
  294. * @return the execution environment, NULL if failed, e.g. invalid
  295. * stack size is passed
  296. */
  297. wasm_exec_env_t
  298. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  299. uint32_t stack_size);
  300. /**
  301. * Destroy the execution environment.
  302. *
  303. * @param exec_env the execution environment to destroy
  304. */
  305. void
  306. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  307. /**
  308. * Get WASM module instance from execution environment
  309. *
  310. * @param exec_env the execution environment to retrieve
  311. *
  312. * @return the WASM module instance
  313. */
  314. wasm_module_inst_t
  315. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  316. /**
  317. * Call the given WASM function of a WASM module instance with
  318. * arguments (bytecode and AoT).
  319. *
  320. * @param exec_env the execution environment to call the function,
  321. * which must be created from wasm_create_exec_env()
  322. * @param function the function to call
  323. * @param argc the number of arguments
  324. * @param argv the arguments. If the function has return value,
  325. * the first (or first two in case 64-bit return value) element of
  326. * argv stores the return value of the called WASM function after this
  327. * function returns.
  328. *
  329. * @return true if success, false otherwise and exception will be thrown,
  330. * the caller can call wasm_runtime_get_exception to get the exception
  331. * info.
  332. */
  333. bool
  334. wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
  335. wasm_function_inst_t function,
  336. uint32_t argc, uint32_t argv[]);
  337. /**
  338. * Find the unique main function from a WASM module instance
  339. * and execute that function.
  340. *
  341. * @param module_inst the WASM module instance
  342. * @param argc the number of arguments
  343. * @param argv the arguments array
  344. *
  345. * @return true if the main function is called, false otherwise and exception
  346. * will be thrown, the caller can call wasm_runtime_get_exception to get
  347. * the exception info.
  348. */
  349. bool
  350. wasm_application_execute_main(wasm_module_inst_t module_inst,
  351. int32_t argc, char *argv[]);
  352. /**
  353. * Find the specified function in argv[0] from a WASM module instance
  354. * and execute that function.
  355. *
  356. * @param module_inst the WASM module instance
  357. * @param name the name of the function to execute.
  358. * to indicate the module name via: $module_name$function_name
  359. * or just a function name: function_name
  360. * @param argc the number of arguments
  361. * @param argv the arguments array
  362. *
  363. * @return true if the specified function is called, false otherwise and
  364. * exception will be thrown, the caller can call wasm_runtime_get_exception
  365. * to get the exception info.
  366. */
  367. bool
  368. wasm_application_execute_func(wasm_module_inst_t module_inst,
  369. const char *name, int32_t argc, char *argv[]);
  370. /**
  371. * Get exception info of the WASM module instance.
  372. *
  373. * @param module_inst the WASM module instance
  374. *
  375. * @return the exception string
  376. */
  377. const char *
  378. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  379. /**
  380. * Set exception info of the WASM module instance.
  381. *
  382. * @param module_inst the WASM module instance
  383. *
  384. * @param exception the exception string
  385. */
  386. void
  387. wasm_runtime_set_exception(wasm_module_inst_t module_inst,
  388. const char *exception);
  389. /**
  390. * Clear exception info of the WASM module instance.
  391. *
  392. * @param module_inst the WASM module instance
  393. */
  394. void
  395. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  396. /**
  397. * Set custom data to WASM module instance.
  398. *
  399. * @param module_inst the WASM module instance
  400. * @param custom_data the custom data to be set
  401. */
  402. void
  403. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  404. void *custom_data);
  405. /**
  406. * Get the custom data within a WASM module instance.
  407. *
  408. * @param module_inst the WASM module instance
  409. *
  410. * @return the custom data (NULL if not set yet)
  411. */
  412. void *
  413. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  414. /**
  415. * Allocate memory from the heap of WASM module instance
  416. *
  417. * @param module_inst the WASM module instance which contains heap
  418. * @param size the size bytes to allocate
  419. * @param p_native_addr return native address of the allocated memory
  420. * if it is not NULL, and return NULL if memory malloc failed
  421. *
  422. * @return the allocated memory address, which is a relative offset to the
  423. * base address of the module instance's memory space. Note that
  424. * it is not an absolute address.
  425. * Return non-zero if success, zero if failed.
  426. */
  427. uint32_t
  428. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size,
  429. void **p_native_addr);
  430. /**
  431. * Free memory to the heap of WASM module instance
  432. *
  433. * @param module_inst the WASM module instance which contains heap
  434. * @param ptr the pointer to free
  435. */
  436. void
  437. wasm_runtime_module_free(wasm_module_inst_t module_inst, uint32_t ptr);
  438. /**
  439. * Allocate memory from the heap of WASM module instance and initialize
  440. * the memory with src
  441. *
  442. * @param module_inst the WASM module instance which contains heap
  443. * @param src the source data to copy
  444. * @param size the size of the source data
  445. *
  446. * @return the allocated memory address, which is a relative offset to the
  447. * base address of the module instance's memory space. Note that
  448. * it is not an absolute address.
  449. * Return non-zero if success, zero if failed.
  450. */
  451. uint32_t
  452. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  453. const char *src, uint32_t size);
  454. /**
  455. * Validate the app address, check whether it belongs to WASM module
  456. * instance's address space, or in its heap space or memory space.
  457. *
  458. * @param module_inst the WASM module instance
  459. * @param app_offset the app address to validate, which is a relative address
  460. * @param size the size bytes of the app address
  461. *
  462. * @return true if success, false otherwise. If failed, an exception will
  463. * be thrown.
  464. */
  465. bool
  466. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  467. uint32_t app_offset, uint32_t size);
  468. /**
  469. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  470. * is not provided. This function validates the app string address, check whether it
  471. * belongs to WASM module instance's address space, or in its heap space or
  472. * memory space. Moreover, it checks whether it is the offset of a string that
  473. * is end with '\0'.
  474. * @param module_inst the WASM module instance
  475. * @param app_str_offset the app address of the string to validate, which is a
  476. * relative address
  477. *
  478. * @return true if success, false otherwise. If failed, an exception will
  479. * be thrown.
  480. */
  481. bool
  482. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  483. uint32_t app_str_offset);
  484. /**
  485. * Validate the native address, check whether it belongs to WASM module
  486. * instance's address space, or in its heap space or memory space.
  487. *
  488. * @param module_inst the WASM module instance
  489. * @param native_ptr the native address to validate, which is an absolute
  490. * address
  491. * @param size the size bytes of the app address
  492. *
  493. * @return true if success, false otherwise. If failed, an exception will
  494. * be thrown.
  495. */
  496. bool
  497. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  498. void *native_ptr, uint32_t size);
  499. /**
  500. * Convert app address(relative address) to native address(absolute address)
  501. *
  502. * @param module_inst the WASM module instance
  503. * @param app_offset the app adress
  504. *
  505. * @return the native address converted
  506. */
  507. void*
  508. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  509. uint32_t app_offset);
  510. /**
  511. * Convert native address(absolute address) to app address(relative address)
  512. *
  513. * @param module_inst the WASM module instance
  514. * @param native_ptr the native address
  515. *
  516. * @return the app address converted
  517. */
  518. uint32_t
  519. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  520. void *native_ptr);
  521. /**
  522. * Get the app address range (relative address) that a app address belongs to
  523. *
  524. * @param module_inst the WASM module instance
  525. * @param app_offset the app address to retrieve
  526. * @param p_app_start_offset buffer to output the app start offset if not NULL
  527. * @param p_app_end_offset buffer to output the app end offset if not NULL
  528. *
  529. * @return true if success, false otherwise.
  530. */
  531. bool
  532. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  533. uint32_t app_offset,
  534. uint32_t *p_app_start_offset,
  535. uint32_t *p_app_end_offset);
  536. /**
  537. * Get the native address range (absolute address) that a native address belongs to
  538. *
  539. * @param module_inst the WASM module instance
  540. * @param native_ptr the native address to retrieve
  541. * @param p_native_start_addr buffer to output the native start address if not NULL
  542. * @param p_native_end_addr buffer to output the native end address if not NULL
  543. *
  544. * @return true if success, false otherwise.
  545. */
  546. bool
  547. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  548. uint8_t *native_ptr,
  549. uint8_t **p_native_start_addr,
  550. uint8_t **p_native_end_addr);
  551. /**
  552. * Register native functions with same module name
  553. *
  554. * @param module_name the module name of the native functions
  555. * @param native_symbols specifies an array of NativeSymbol structures which
  556. * contain the names, function pointers and signatures
  557. * Note: WASM runtime will not allocate memory to clone the data, so
  558. * user must ensure the array can be used forever
  559. * Meanings of letters in function signature:
  560. * 'i': the parameter is i32 type
  561. * 'I': the parameter is i64 type
  562. * 'f': the parameter is f32 type
  563. * 'F': the parameter is f64 type
  564. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  565. * auto check its boundary before calling the native function.
  566. * If it is followed by '~', the checked length of the pointer
  567. * is gotten from the following parameter, if not, the checked
  568. * length of the pointer is 1.
  569. * '~': the parameter is the pointer's length with i32 type, and must
  570. * follow after '*'
  571. * '$': the parameter is a string (i32 in WASM), and runtime will
  572. * auto check its boundary before calling the native function
  573. * @param n_native_symbols specifies the number of native symbols in the array
  574. *
  575. * @return true if success, false otherwise
  576. */
  577. bool wasm_runtime_register_natives(const char *module_name,
  578. NativeSymbol *native_symbols,
  579. uint32_t n_native_symbols);
  580. /**
  581. * Register native functions with same module name, similar to
  582. * wasm_runtime_register_natives, the difference is that runtime passes raw
  583. * arguments to native API, which means that the native API should be defined as:
  584. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  585. * and native API should extract arguments one by one from args array with macro
  586. * native_raw_get_arg
  587. * and write the return value back to args[0] with macro
  588. * native_raw_return_type and native_raw_set_return
  589. */
  590. bool wasm_runtime_register_natives_raw(const char *module_name,
  591. NativeSymbol *native_symbols,
  592. uint32_t n_native_symbols);
  593. /**
  594. * Get attachment of native function from execution environment
  595. *
  596. * @param exec_env the execution environment to retrieve
  597. *
  598. * @return the attachment of native function
  599. */
  600. void *
  601. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  602. /**
  603. * Set user data to execution environment.
  604. *
  605. * @param exec_env the execution environment
  606. * @param user_data the user data to be set
  607. */
  608. void
  609. wasm_runtime_set_user_data(wasm_exec_env_t exec_env,
  610. void *user_data);
  611. /**
  612. * Get the user data within execution environment.
  613. *
  614. * @param exec_env the execution environment
  615. *
  616. * @return the user data (NULL if not set yet)
  617. */
  618. void *
  619. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  620. #if WASM_ENABLE_THREAD_MGR != 0
  621. /* wasm thread callback function type */
  622. typedef void* (*wasm_thread_callback_t)(wasm_exec_env_t, void *);
  623. /* wasm thread type */
  624. typedef uintptr_t wasm_thread_t;
  625. /**
  626. * Set the max thread num per cluster.
  627. *
  628. * @param num maximum thread num
  629. */
  630. void
  631. wasm_runtime_set_max_thread_num(uint32_t num);
  632. /**
  633. * spawn a new exec_env, the spawned exec_env
  634. * can be used in other threads
  635. *
  636. * @param num the original exec_env
  637. *
  638. * @return the spawned exec_env if success, NULL otherwise
  639. */
  640. wasm_exec_env_t
  641. wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
  642. /**
  643. * Destroy the spawned exec_env
  644. *
  645. * @param exec_env the spawned exec_env
  646. */
  647. void
  648. wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
  649. /**
  650. * spawn a thread from the given exec_env
  651. *
  652. * @param exec_env the original exec_env
  653. * @param tid thread id to be returned to the caller
  654. * @param callback the callback function provided by the user
  655. * @param arg the arguments passed to the callback
  656. *
  657. * @return 0 if success, -1 otherwise
  658. */
  659. int32_t
  660. wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
  661. wasm_thread_callback_t callback, void *arg);
  662. /**
  663. * waits a spawned thread to terminate
  664. *
  665. * @param tid thread id
  666. * @param retval if not NULL, output the return value of the thread
  667. *
  668. * @return 0 if success, error number otherwise
  669. */
  670. int32_t
  671. wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
  672. #endif
  673. #ifdef __cplusplus
  674. }
  675. #endif
  676. #endif /* end of _WASM_EXPORT_H */