wasm_export.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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(_MSC_BUILD )
  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. /**
  193. * It is a callback for WAMR providing by embedding to load a module file
  194. * into a buffer
  195. */
  196. typedef bool (*module_reader)(const char *module_name,
  197. uint8_t **p_buffer, uint32_t *p_size);
  198. /**
  199. * It is a callback for WAMR providing by embedding to release the buffer which
  200. * is used by loading a module file
  201. */
  202. typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size);
  203. /**
  204. * To setup callbacks for reading and releasing a buffer about a module file
  205. *
  206. * @param reader a callback to read a module file into a buffer
  207. * @param destroyer a callback to release above buffer
  208. */
  209. WASM_RUNTIME_API_EXTERN void
  210. wasm_runtime_set_module_reader(const module_reader reader,
  211. const module_destroyer destroyer);
  212. /**
  213. * Give the "module" a name "module_name".
  214. * can not assign a new name to a module if it already has a name
  215. *
  216. * @param module_name indicate a name
  217. * @param module the target module
  218. * @param error_buf output of the exception info
  219. * @param error_buf_size the size of the exception string
  220. *
  221. * @return true means success, false means failed
  222. */
  223. WASM_RUNTIME_API_EXTERN bool
  224. wasm_runtime_register_module(const char *module_name, wasm_module_t module,
  225. char *error_buf, uint32_t error_buf_size);
  226. /**
  227. * To check if there is already a loaded module named module_name in the
  228. * runtime. you will not want to load repeately
  229. *
  230. * @param module_name indicate a name
  231. *
  232. * @return return WASM module loaded, NULL if failed
  233. */
  234. WASM_RUNTIME_API_EXTERN wasm_module_t
  235. wasm_runtime_find_module_registered(const char *module_name);
  236. /**
  237. * Load a WASM module from a specified byte buffer. The byte buffer can be
  238. * WASM binary data when interpreter or JIT is enabled, or AOT binary data
  239. * when AOT is enabled. If it is AOT binary data, it must be 4-byte aligned.
  240. *
  241. * @param buf the byte buffer which contains the WASM binary data
  242. * @param size the size of the buffer
  243. * @param error_buf output of the exception info
  244. * @param error_buf_size the size of the exception string
  245. *
  246. * @return return WASM module loaded, NULL if failed
  247. */
  248. WASM_RUNTIME_API_EXTERN wasm_module_t
  249. wasm_runtime_load(const uint8_t *buf, uint32_t size,
  250. char *error_buf, uint32_t error_buf_size);
  251. /**
  252. * Load a WASM module from a specified WASM or AOT section list.
  253. *
  254. * @param section_list the section list which contains each section data
  255. * @param is_aot whether the section list is AOT section list
  256. * @param error_buf output of the exception info
  257. * @param error_buf_size the size of the exception string
  258. *
  259. * @return return WASM module loaded, NULL if failed
  260. */
  261. WASM_RUNTIME_API_EXTERN wasm_module_t
  262. wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
  263. char *error_buf, uint32_t error_buf_size);
  264. /**
  265. * Unload a WASM module.
  266. *
  267. * @param module the module to be unloaded
  268. */
  269. WASM_RUNTIME_API_EXTERN void
  270. wasm_runtime_unload(wasm_module_t module);
  271. WASM_RUNTIME_API_EXTERN void
  272. wasm_runtime_set_wasi_args(wasm_module_t module,
  273. const char *dir_list[], uint32_t dir_count,
  274. const char *map_dir_list[], uint32_t map_dir_count,
  275. const char *env[], uint32_t env_count,
  276. char *argv[], int argc);
  277. /**
  278. * Instantiate a WASM module.
  279. *
  280. * @param module the WASM module to instantiate
  281. * @param stack_size the default stack size of the module instance when the
  282. * exec env's operation stack isn't created by user, e.g. API
  283. * wasm_application_execute_main() and wasm_application_execute_func()
  284. * create the operation stack internally with the stack size specified
  285. * here. And API wasm_runtime_create_exec_env() creates the operation
  286. * stack with stack size specified by its parameter, the stack size
  287. * specified here is ignored.
  288. * @param heap_size the default heap size of the module instance, a heap will
  289. * be created besides the app memory space. Both wasm app and native
  290. * function can allocate memory from the heap.
  291. * @param error_buf buffer to output the error info if failed
  292. * @param error_buf_size the size of the error buffer
  293. *
  294. * @return return the instantiated WASM module instance, NULL if failed
  295. */
  296. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  297. wasm_runtime_instantiate(const wasm_module_t module,
  298. uint32_t stack_size, uint32_t heap_size,
  299. char *error_buf, uint32_t error_buf_size);
  300. /**
  301. * Deinstantiate a WASM module instance, destroy the resources.
  302. *
  303. * @param module_inst the WASM module instance to destroy
  304. */
  305. WASM_RUNTIME_API_EXTERN void
  306. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  307. WASM_RUNTIME_API_EXTERN bool
  308. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  309. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  310. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  311. /**
  312. * Lookup an exported function in the WASM module instance.
  313. *
  314. * @param module_inst the module instance
  315. * @param name the name of the function
  316. * @param signature the signature of the function, ignored currently
  317. *
  318. * @return the function instance found, NULL if not found
  319. */
  320. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  321. wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
  322. const char *name, const char *signature);
  323. /**
  324. * Create execution environment for a WASM module instance.
  325. *
  326. * @param module_inst the module instance
  327. * @param stack_size the stack size to execute a WASM function
  328. *
  329. * @return the execution environment, NULL if failed, e.g. invalid
  330. * stack size is passed
  331. */
  332. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  333. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  334. uint32_t stack_size);
  335. /**
  336. * Destroy the execution environment.
  337. *
  338. * @param exec_env the execution environment to destroy
  339. */
  340. WASM_RUNTIME_API_EXTERN void
  341. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  342. /**
  343. * Initialize thread environment.
  344. * Note:
  345. * If developer creates a child thread by himself to call the
  346. * the wasm function in that thread, he should call this API
  347. * firstly before calling the wasm function and then call
  348. * wasm_runtime_destroy_thread_env() after calling the wasm
  349. * function. If the thread is created from the runtime API,
  350. * it is unnecessary to call these two APIs.
  351. *
  352. * @return true if success, false otherwise
  353. */
  354. WASM_RUNTIME_API_EXTERN bool
  355. wasm_runtime_init_thread_env();
  356. /**
  357. * Destroy thread environment
  358. */
  359. WASM_RUNTIME_API_EXTERN void
  360. wasm_runtime_destroy_thread_env();
  361. /**
  362. * Get WASM module instance from execution environment
  363. *
  364. * @param exec_env the execution environment to retrieve
  365. *
  366. * @return the WASM module instance
  367. */
  368. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  369. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  370. /**
  371. * Call the given WASM function of a WASM module instance with
  372. * arguments (bytecode and AoT).
  373. *
  374. * @param exec_env the execution environment to call the function,
  375. * which must be created from wasm_create_exec_env()
  376. * @param function the function to call
  377. * @param argc the number of arguments
  378. * @param argv the arguments. If the function has return value,
  379. * the first (or first two in case 64-bit return value) element of
  380. * argv stores the return value of the called WASM function after this
  381. * function returns.
  382. *
  383. * @return true if success, false otherwise and exception will be thrown,
  384. * the caller can call wasm_runtime_get_exception to get the exception
  385. * info.
  386. */
  387. WASM_RUNTIME_API_EXTERN bool
  388. wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
  389. wasm_function_inst_t function,
  390. uint32_t argc, uint32_t argv[]);
  391. /**
  392. * Call the given WASM function of a WASM module instance with
  393. * provided results space and arguments (bytecode and AoT).
  394. *
  395. * @param exec_env the execution environment to call the function,
  396. * which must be created from wasm_create_exec_env()
  397. * @param function the function to call
  398. * @param num_results the number of results
  399. * @param results the pre-alloced pointer to get the results
  400. * @param num_args the number of arguments
  401. * @param args the arguments
  402. *
  403. * @return true if success, false otherwise and exception will be thrown,
  404. * the caller can call wasm_runtime_get_exception to get the exception
  405. * info.
  406. */
  407. WASM_RUNTIME_API_EXTERN bool
  408. wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
  409. wasm_function_inst_t function,
  410. uint32_t num_results, wasm_val_t results[],
  411. uint32_t num_args, wasm_val_t *args);
  412. /**
  413. * Call the given WASM function of a WASM module instance with
  414. * provided results space and variant arguments (bytecode and AoT).
  415. *
  416. * @param exec_env the execution environment to call the function,
  417. * which must be created from wasm_create_exec_env()
  418. * @param function the function to call
  419. * @param num_results the number of results
  420. * @param results the pre-alloced pointer to get the results
  421. * @param num_args the number of arguments
  422. * @param ... the variant arguments
  423. *
  424. * @return true if success, false otherwise and exception will be thrown,
  425. * the caller can call wasm_runtime_get_exception to get the exception
  426. * info.
  427. */
  428. WASM_RUNTIME_API_EXTERN bool
  429. wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
  430. wasm_function_inst_t function,
  431. uint32_t num_results, wasm_val_t results[],
  432. uint32_t num_args, ...);
  433. /**
  434. * Find the unique main function from a WASM module instance
  435. * and execute that function.
  436. *
  437. * @param module_inst the WASM module instance
  438. * @param argc the number of arguments
  439. * @param argv the arguments array
  440. *
  441. * @return true if the main function is called, false otherwise and exception
  442. * will be thrown, the caller can call wasm_runtime_get_exception to get
  443. * the exception info.
  444. */
  445. WASM_RUNTIME_API_EXTERN bool
  446. wasm_application_execute_main(wasm_module_inst_t module_inst,
  447. int32_t argc, char *argv[]);
  448. /**
  449. * Find the specified function in argv[0] from a WASM module instance
  450. * and execute that function.
  451. *
  452. * @param module_inst the WASM module instance
  453. * @param name the name of the function to execute.
  454. * to indicate the module name via: $module_name$function_name
  455. * or just a function name: function_name
  456. * @param argc the number of arguments
  457. * @param argv the arguments array
  458. *
  459. * @return true if the specified function is called, false otherwise and
  460. * exception will be thrown, the caller can call wasm_runtime_get_exception
  461. * to get the exception info.
  462. */
  463. WASM_RUNTIME_API_EXTERN bool
  464. wasm_application_execute_func(wasm_module_inst_t module_inst,
  465. const char *name, int32_t argc, char *argv[]);
  466. /**
  467. * Get exception info of the WASM module instance.
  468. *
  469. * @param module_inst the WASM module instance
  470. *
  471. * @return the exception string
  472. */
  473. WASM_RUNTIME_API_EXTERN const char *
  474. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  475. /**
  476. * Set exception info of the WASM module instance.
  477. *
  478. * @param module_inst the WASM module instance
  479. *
  480. * @param exception the exception string
  481. */
  482. WASM_RUNTIME_API_EXTERN void
  483. wasm_runtime_set_exception(wasm_module_inst_t module_inst,
  484. const char *exception);
  485. /**
  486. * Clear exception info of the WASM module instance.
  487. *
  488. * @param module_inst the WASM module instance
  489. */
  490. WASM_RUNTIME_API_EXTERN void
  491. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  492. /**
  493. * Set custom data to WASM module instance.
  494. *
  495. * @param module_inst the WASM module instance
  496. * @param custom_data the custom data to be set
  497. */
  498. WASM_RUNTIME_API_EXTERN void
  499. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  500. void *custom_data);
  501. /**
  502. * Get the custom data within a WASM module instance.
  503. *
  504. * @param module_inst the WASM module instance
  505. *
  506. * @return the custom data (NULL if not set yet)
  507. */
  508. WASM_RUNTIME_API_EXTERN void *
  509. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  510. /**
  511. * Allocate memory from the heap of WASM module instance
  512. *
  513. * @param module_inst the WASM module instance which contains heap
  514. * @param size the size bytes to allocate
  515. * @param p_native_addr return native address of the allocated memory
  516. * if it is not NULL, and return NULL if memory malloc failed
  517. *
  518. * @return the allocated memory address, which is a relative offset to the
  519. * base address of the module instance's memory space. Note that
  520. * it is not an absolute address.
  521. * Return non-zero if success, zero if failed.
  522. */
  523. WASM_RUNTIME_API_EXTERN uint32_t
  524. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size,
  525. void **p_native_addr);
  526. /**
  527. * Free memory to the heap of WASM module instance
  528. *
  529. * @param module_inst the WASM module instance which contains heap
  530. * @param ptr the pointer to free
  531. */
  532. WASM_RUNTIME_API_EXTERN void
  533. wasm_runtime_module_free(wasm_module_inst_t module_inst, uint32_t ptr);
  534. /**
  535. * Allocate memory from the heap of WASM module instance and initialize
  536. * the memory with src
  537. *
  538. * @param module_inst the WASM module instance which contains heap
  539. * @param src the source data to copy
  540. * @param size the size of the source data
  541. *
  542. * @return the allocated memory address, which is a relative offset to the
  543. * base address of the module instance's memory space. Note that
  544. * it is not an absolute address.
  545. * Return non-zero if success, zero if failed.
  546. */
  547. WASM_RUNTIME_API_EXTERN uint32_t
  548. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  549. const char *src, uint32_t size);
  550. /**
  551. * Validate the app address, check whether it belongs to WASM module
  552. * instance's address space, or in its heap space or memory space.
  553. *
  554. * @param module_inst the WASM module instance
  555. * @param app_offset the app address to validate, which is a relative address
  556. * @param size the size bytes of the app 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_addr(wasm_module_inst_t module_inst,
  563. uint32_t app_offset, uint32_t size);
  564. /**
  565. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  566. * is not provided. This function validates the app string address, check whether it
  567. * belongs to WASM module instance's address space, or in its heap space or
  568. * memory space. Moreover, it checks whether it is the offset of a string that
  569. * is end with '\0'.
  570. * @param module_inst the WASM module instance
  571. * @param app_str_offset the app address of the string to validate, which is a
  572. * relative address
  573. *
  574. * @return true if success, false otherwise. If failed, an exception will
  575. * be thrown.
  576. */
  577. WASM_RUNTIME_API_EXTERN bool
  578. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  579. uint32_t app_str_offset);
  580. /**
  581. * Validate the native address, check whether it belongs to WASM module
  582. * instance's address space, or in its heap space or memory space.
  583. *
  584. * @param module_inst the WASM module instance
  585. * @param native_ptr the native address to validate, which is an absolute
  586. * address
  587. * @param size the size bytes of the app address
  588. *
  589. * @return true if success, false otherwise. If failed, an exception will
  590. * be thrown.
  591. */
  592. WASM_RUNTIME_API_EXTERN bool
  593. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  594. void *native_ptr, uint32_t size);
  595. /**
  596. * Convert app address(relative address) to native address(absolute address)
  597. *
  598. * @param module_inst the WASM module instance
  599. * @param app_offset the app adress
  600. *
  601. * @return the native address converted
  602. */
  603. WASM_RUNTIME_API_EXTERN void *
  604. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  605. uint32_t app_offset);
  606. /**
  607. * Convert native address(absolute address) to app address(relative address)
  608. *
  609. * @param module_inst the WASM module instance
  610. * @param native_ptr the native address
  611. *
  612. * @return the app address converted
  613. */
  614. WASM_RUNTIME_API_EXTERN uint32_t
  615. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  616. void *native_ptr);
  617. /**
  618. * Get the app address range (relative address) that a app address belongs to
  619. *
  620. * @param module_inst the WASM module instance
  621. * @param app_offset the app address to retrieve
  622. * @param p_app_start_offset buffer to output the app start offset if not NULL
  623. * @param p_app_end_offset buffer to output the app end offset if not NULL
  624. *
  625. * @return true if success, false otherwise.
  626. */
  627. WASM_RUNTIME_API_EXTERN bool
  628. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  629. uint32_t app_offset,
  630. uint32_t *p_app_start_offset,
  631. uint32_t *p_app_end_offset);
  632. /**
  633. * Get the native address range (absolute address) that a native address belongs to
  634. *
  635. * @param module_inst the WASM module instance
  636. * @param native_ptr the native address to retrieve
  637. * @param p_native_start_addr buffer to output the native start address if not NULL
  638. * @param p_native_end_addr buffer to output the native end address if not NULL
  639. *
  640. * @return true if success, false otherwise.
  641. */
  642. WASM_RUNTIME_API_EXTERN bool
  643. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  644. uint8_t *native_ptr,
  645. uint8_t **p_native_start_addr,
  646. uint8_t **p_native_end_addr);
  647. /**
  648. * Register native functions with same module name
  649. *
  650. * @param module_name the module name of the native functions
  651. * @param native_symbols specifies an array of NativeSymbol structures which
  652. * contain the names, function pointers and signatures
  653. * Note: WASM runtime will not allocate memory to clone the data, so
  654. * user must ensure the array can be used forever
  655. * Meanings of letters in function signature:
  656. * 'i': the parameter is i32 type
  657. * 'I': the parameter is i64 type
  658. * 'f': the parameter is f32 type
  659. * 'F': the parameter is f64 type
  660. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  661. * auto check its boundary before calling the native function.
  662. * If it is followed by '~', the checked length of the pointer
  663. * is gotten from the following parameter, if not, the checked
  664. * length of the pointer is 1.
  665. * '~': the parameter is the pointer's length with i32 type, and must
  666. * follow after '*'
  667. * '$': the parameter is a string (i32 in WASM), and runtime will
  668. * auto check its boundary before calling the native function
  669. * @param n_native_symbols specifies the number of native symbols in the array
  670. *
  671. * @return true if success, false otherwise
  672. */
  673. WASM_RUNTIME_API_EXTERN bool
  674. wasm_runtime_register_natives(const char *module_name,
  675. NativeSymbol *native_symbols,
  676. uint32_t n_native_symbols);
  677. /**
  678. * Register native functions with same module name, similar to
  679. * wasm_runtime_register_natives, the difference is that runtime passes raw
  680. * arguments to native API, which means that the native API should be defined as:
  681. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  682. * and native API should extract arguments one by one from args array with macro
  683. * native_raw_get_arg
  684. * and write the return value back to args[0] with macro
  685. * native_raw_return_type and native_raw_set_return
  686. */
  687. WASM_RUNTIME_API_EXTERN bool
  688. wasm_runtime_register_natives_raw(const char *module_name,
  689. NativeSymbol *native_symbols,
  690. uint32_t n_native_symbols);
  691. /**
  692. * Get attachment of native function from execution environment
  693. *
  694. * @param exec_env the execution environment to retrieve
  695. *
  696. * @return the attachment of native function
  697. */
  698. WASM_RUNTIME_API_EXTERN void *
  699. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  700. /**
  701. * Set user data to execution environment.
  702. *
  703. * @param exec_env the execution environment
  704. * @param user_data the user data to be set
  705. */
  706. WASM_RUNTIME_API_EXTERN void
  707. wasm_runtime_set_user_data(wasm_exec_env_t exec_env,
  708. void *user_data);
  709. /**
  710. * Get the user data within execution environment.
  711. *
  712. * @param exec_env the execution environment
  713. *
  714. * @return the user data (NULL if not set yet)
  715. */
  716. WASM_RUNTIME_API_EXTERN void *
  717. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  718. /**
  719. * Dump runtime memory consumption, including:
  720. * Exec env memory consumption
  721. * WASM module memory consumption
  722. * WASM module instance memory consumption
  723. * stack and app heap used info
  724. *
  725. * @param exec_env the execution environment
  726. */
  727. WASM_RUNTIME_API_EXTERN void
  728. wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env);
  729. /**
  730. * Dump runtime performance profiler data of each function
  731. *
  732. * @param module_inst the WASM module instance to profile
  733. */
  734. WASM_RUNTIME_API_EXTERN void
  735. wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
  736. /* wasm thread callback function type */
  737. typedef void* (*wasm_thread_callback_t)(wasm_exec_env_t, void *);
  738. /* wasm thread type */
  739. typedef uintptr_t wasm_thread_t;
  740. /**
  741. * Set the max thread num per cluster.
  742. *
  743. * @param num maximum thread num
  744. */
  745. WASM_RUNTIME_API_EXTERN void
  746. wasm_runtime_set_max_thread_num(uint32_t num);
  747. /**
  748. * spawn a new exec_env, the spawned exec_env
  749. * can be used in other threads
  750. *
  751. * @param num the original exec_env
  752. *
  753. * @return the spawned exec_env if success, NULL otherwise
  754. */
  755. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  756. wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
  757. /**
  758. * Destroy the spawned exec_env
  759. *
  760. * @param exec_env the spawned exec_env
  761. */
  762. WASM_RUNTIME_API_EXTERN void
  763. wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
  764. /**
  765. * spawn a thread from the given exec_env
  766. *
  767. * @param exec_env the original exec_env
  768. * @param tid thread id to be returned to the caller
  769. * @param callback the callback function provided by the user
  770. * @param arg the arguments passed to the callback
  771. *
  772. * @return 0 if success, -1 otherwise
  773. */
  774. WASM_RUNTIME_API_EXTERN int32_t
  775. wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
  776. wasm_thread_callback_t callback, void *arg);
  777. /**
  778. * waits a spawned thread to terminate
  779. *
  780. * @param tid thread id
  781. * @param retval if not NULL, output the return value of the thread
  782. *
  783. * @return 0 if success, error number otherwise
  784. */
  785. WASM_RUNTIME_API_EXTERN int32_t
  786. wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
  787. /**
  788. * Map external object to an internal externref index: if the index
  789. * has been created, return it, otherwise create the index.
  790. *
  791. * @param module_inst the WASM module instance that the extern object
  792. * belongs to
  793. * @param extern_obj the external object to be mapped
  794. * @param p_externref_idx return externref index of the external object
  795. *
  796. * @return true if success, false otherwise
  797. */
  798. WASM_RUNTIME_API_EXTERN bool
  799. wasm_externref_obj2ref(wasm_module_inst_t module_inst,
  800. void *extern_obj, uint32_t *p_externref_idx);
  801. /**
  802. * Retrieve the external object from an internal externref index
  803. *
  804. * @param externref_idx the externref index to retrieve
  805. * @param p_extern_obj return the mapped external object of
  806. * the externref index
  807. *
  808. * @return true if success, false otherwise
  809. */
  810. WASM_RUNTIME_API_EXTERN bool
  811. wasm_externref_ref2obj(uint32_t externref_idx, void **p_extern_obj);
  812. /**
  813. * Retain an extern object which is mapped to the internal externref
  814. * so that the object won't be cleaned during extern object reclaim
  815. * if it isn't used.
  816. *
  817. * @param externref_idx the externref index of an external object
  818. * to retain
  819. * @return true if success, false otherwise
  820. */
  821. WASM_RUNTIME_API_EXTERN bool
  822. wasm_externref_retain(uint32_t externref_idx);
  823. /**
  824. * dump the call stack
  825. *
  826. * @param exec_env the execution environment
  827. */
  828. WASM_RUNTIME_API_EXTERN void
  829. wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
  830. #ifdef __cplusplus
  831. }
  832. #endif
  833. #endif /* end of _WASM_EXPORT_H */