wasm_export.h 27 KB

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