wasm_export.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. /**
  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. If heap_size is 0, the
  291. * default heap size will be used.
  292. * @param error_buf buffer to output the error info if failed
  293. * @param error_buf_size the size of the error buffer
  294. *
  295. * @return return the instantiated WASM module instance, NULL if failed
  296. */
  297. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  298. wasm_runtime_instantiate(const wasm_module_t module,
  299. uint32_t stack_size, uint32_t heap_size,
  300. char *error_buf, uint32_t error_buf_size);
  301. /**
  302. * Deinstantiate a WASM module instance, destroy the resources.
  303. *
  304. * @param module_inst the WASM module instance to destroy
  305. */
  306. WASM_RUNTIME_API_EXTERN void
  307. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  308. WASM_RUNTIME_API_EXTERN bool
  309. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  310. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  311. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  312. /**
  313. * Lookup an exported function in the WASM module instance.
  314. *
  315. * @param module_inst the module instance
  316. * @param name the name of the function
  317. * @param signature the signature of the function, ignored currently
  318. *
  319. * @return the function instance found, NULL if not found
  320. */
  321. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  322. wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
  323. const char *name, const char *signature);
  324. /**
  325. * Create execution environment for a WASM module instance.
  326. *
  327. * @param module_inst the module instance
  328. * @param stack_size the stack size to execute a WASM function
  329. *
  330. * @return the execution environment, NULL if failed, e.g. invalid
  331. * stack size is passed
  332. */
  333. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  334. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  335. uint32_t stack_size);
  336. /**
  337. * Destroy the execution environment.
  338. *
  339. * @param exec_env the execution environment to destroy
  340. */
  341. WASM_RUNTIME_API_EXTERN void
  342. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  343. /**
  344. * Get WASM module instance from execution environment
  345. *
  346. * @param exec_env the execution environment to retrieve
  347. *
  348. * @return the WASM module instance
  349. */
  350. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  351. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  352. /**
  353. * Call the given WASM function of a WASM module instance with
  354. * arguments (bytecode and AoT).
  355. *
  356. * @param exec_env the execution environment to call the function,
  357. * which must be created from wasm_create_exec_env()
  358. * @param function the function to call
  359. * @param argc the number of arguments
  360. * @param argv the arguments. If the function has return value,
  361. * the first (or first two in case 64-bit return value) element of
  362. * argv stores the return value of the called WASM function after this
  363. * function returns.
  364. *
  365. * @return true if success, false otherwise and exception will be thrown,
  366. * the caller can call wasm_runtime_get_exception to get the exception
  367. * info.
  368. */
  369. WASM_RUNTIME_API_EXTERN bool
  370. wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
  371. wasm_function_inst_t function,
  372. uint32_t argc, uint32_t argv[]);
  373. /**
  374. * Call the given WASM function of a WASM module instance with
  375. * provided results space and arguments (bytecode and AoT).
  376. *
  377. * @param exec_env the execution environment to call the function,
  378. * which must be created from wasm_create_exec_env()
  379. * @param function the function to call
  380. * @param num_results the number of results
  381. * @param results the pre-alloced pointer to get the results
  382. * @param num_args the number of arguments
  383. * @param args the arguments
  384. *
  385. * @return true if success, false otherwise and exception will be thrown,
  386. * the caller can call wasm_runtime_get_exception to get the exception
  387. * info.
  388. */
  389. WASM_RUNTIME_API_EXTERN bool
  390. wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
  391. wasm_function_inst_t function,
  392. uint32_t num_results, wasm_val_t results[],
  393. uint32_t num_args, wasm_val_t *args);
  394. /**
  395. * Call the given WASM function of a WASM module instance with
  396. * provided results space and variant arguments (bytecode and AoT).
  397. *
  398. * @param exec_env the execution environment to call the function,
  399. * which must be created from wasm_create_exec_env()
  400. * @param function the function to call
  401. * @param num_results the number of results
  402. * @param results the pre-alloced pointer to get the results
  403. * @param num_args the number of arguments
  404. * @param ... the variant arguments
  405. *
  406. * @return true if success, false otherwise and exception will be thrown,
  407. * the caller can call wasm_runtime_get_exception to get the exception
  408. * info.
  409. */
  410. WASM_RUNTIME_API_EXTERN bool
  411. wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
  412. wasm_function_inst_t function,
  413. uint32_t num_results, wasm_val_t results[],
  414. uint32_t num_args, ...);
  415. /**
  416. * Find the unique main function from a WASM module instance
  417. * and execute that function.
  418. *
  419. * @param module_inst the WASM module instance
  420. * @param argc the number of arguments
  421. * @param argv the arguments array
  422. *
  423. * @return true if the main function is called, false otherwise and exception
  424. * will be thrown, the caller can call wasm_runtime_get_exception to get
  425. * the exception info.
  426. */
  427. WASM_RUNTIME_API_EXTERN bool
  428. wasm_application_execute_main(wasm_module_inst_t module_inst,
  429. int32_t argc, char *argv[]);
  430. /**
  431. * Find the specified function in argv[0] from a WASM module instance
  432. * and execute that function.
  433. *
  434. * @param module_inst the WASM module instance
  435. * @param name the name of the function to execute.
  436. * to indicate the module name via: $module_name$function_name
  437. * or just a function name: function_name
  438. * @param argc the number of arguments
  439. * @param argv the arguments array
  440. *
  441. * @return true if the specified function is called, false otherwise and
  442. * exception will be thrown, the caller can call wasm_runtime_get_exception
  443. * to get the exception info.
  444. */
  445. WASM_RUNTIME_API_EXTERN bool
  446. wasm_application_execute_func(wasm_module_inst_t module_inst,
  447. const char *name, int32_t argc, char *argv[]);
  448. /**
  449. * Get exception info of the WASM module instance.
  450. *
  451. * @param module_inst the WASM module instance
  452. *
  453. * @return the exception string
  454. */
  455. WASM_RUNTIME_API_EXTERN const char *
  456. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  457. /**
  458. * Set exception info of the WASM module instance.
  459. *
  460. * @param module_inst the WASM module instance
  461. *
  462. * @param exception the exception string
  463. */
  464. WASM_RUNTIME_API_EXTERN void
  465. wasm_runtime_set_exception(wasm_module_inst_t module_inst,
  466. const char *exception);
  467. /**
  468. * Clear exception info of the WASM module instance.
  469. *
  470. * @param module_inst the WASM module instance
  471. */
  472. WASM_RUNTIME_API_EXTERN void
  473. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  474. /**
  475. * Set custom data to WASM module instance.
  476. *
  477. * @param module_inst the WASM module instance
  478. * @param custom_data the custom data to be set
  479. */
  480. WASM_RUNTIME_API_EXTERN void
  481. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  482. void *custom_data);
  483. /**
  484. * Get the custom data within a WASM module instance.
  485. *
  486. * @param module_inst the WASM module instance
  487. *
  488. * @return the custom data (NULL if not set yet)
  489. */
  490. WASM_RUNTIME_API_EXTERN void *
  491. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  492. /**
  493. * Allocate memory from the heap of WASM module instance
  494. *
  495. * @param module_inst the WASM module instance which contains heap
  496. * @param size the size bytes to allocate
  497. * @param p_native_addr return native address of the allocated memory
  498. * if it is not NULL, and return NULL if memory malloc failed
  499. *
  500. * @return the allocated memory address, which is a relative offset to the
  501. * base address of the module instance's memory space. Note that
  502. * it is not an absolute address.
  503. * Return non-zero if success, zero if failed.
  504. */
  505. WASM_RUNTIME_API_EXTERN uint32_t
  506. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size,
  507. void **p_native_addr);
  508. /**
  509. * Free memory to the heap of WASM module instance
  510. *
  511. * @param module_inst the WASM module instance which contains heap
  512. * @param ptr the pointer to free
  513. */
  514. WASM_RUNTIME_API_EXTERN void
  515. wasm_runtime_module_free(wasm_module_inst_t module_inst, uint32_t ptr);
  516. /**
  517. * Allocate memory from the heap of WASM module instance and initialize
  518. * the memory with src
  519. *
  520. * @param module_inst the WASM module instance which contains heap
  521. * @param src the source data to copy
  522. * @param size the size of the source data
  523. *
  524. * @return the allocated memory address, which is a relative offset to the
  525. * base address of the module instance's memory space. Note that
  526. * it is not an absolute address.
  527. * Return non-zero if success, zero if failed.
  528. */
  529. WASM_RUNTIME_API_EXTERN uint32_t
  530. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  531. const char *src, uint32_t size);
  532. /**
  533. * Validate the app address, check whether it belongs to WASM module
  534. * instance's address space, or in its heap space or memory space.
  535. *
  536. * @param module_inst the WASM module instance
  537. * @param app_offset the app address to validate, which is a relative address
  538. * @param size the size bytes of the app address
  539. *
  540. * @return true if success, false otherwise. If failed, an exception will
  541. * be thrown.
  542. */
  543. WASM_RUNTIME_API_EXTERN bool
  544. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  545. uint32_t app_offset, uint32_t size);
  546. /**
  547. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  548. * is not provided. This function validates the app string address, check whether it
  549. * belongs to WASM module instance's address space, or in its heap space or
  550. * memory space. Moreover, it checks whether it is the offset of a string that
  551. * is end with '\0'.
  552. * @param module_inst the WASM module instance
  553. * @param app_str_offset the app address of the string to validate, which is a
  554. * relative address
  555. *
  556. * @return true if success, false otherwise. If failed, an exception will
  557. * be thrown.
  558. */
  559. WASM_RUNTIME_API_EXTERN bool
  560. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  561. uint32_t app_str_offset);
  562. /**
  563. * Validate the native address, check whether it belongs to WASM module
  564. * instance's address space, or in its heap space or memory space.
  565. *
  566. * @param module_inst the WASM module instance
  567. * @param native_ptr the native address to validate, which is an absolute
  568. * address
  569. * @param size the size bytes of the app address
  570. *
  571. * @return true if success, false otherwise. If failed, an exception will
  572. * be thrown.
  573. */
  574. WASM_RUNTIME_API_EXTERN bool
  575. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  576. void *native_ptr, uint32_t size);
  577. /**
  578. * Convert app address(relative address) to native address(absolute address)
  579. *
  580. * @param module_inst the WASM module instance
  581. * @param app_offset the app adress
  582. *
  583. * @return the native address converted
  584. */
  585. WASM_RUNTIME_API_EXTERN void *
  586. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  587. uint32_t app_offset);
  588. /**
  589. * Convert native address(absolute address) to app address(relative address)
  590. *
  591. * @param module_inst the WASM module instance
  592. * @param native_ptr the native address
  593. *
  594. * @return the app address converted
  595. */
  596. WASM_RUNTIME_API_EXTERN uint32_t
  597. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  598. void *native_ptr);
  599. /**
  600. * Get the app address range (relative address) that a app address belongs to
  601. *
  602. * @param module_inst the WASM module instance
  603. * @param app_offset the app address to retrieve
  604. * @param p_app_start_offset buffer to output the app start offset if not NULL
  605. * @param p_app_end_offset buffer to output the app end offset if not NULL
  606. *
  607. * @return true if success, false otherwise.
  608. */
  609. WASM_RUNTIME_API_EXTERN bool
  610. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  611. uint32_t app_offset,
  612. uint32_t *p_app_start_offset,
  613. uint32_t *p_app_end_offset);
  614. /**
  615. * Get the native address range (absolute address) that a native address belongs to
  616. *
  617. * @param module_inst the WASM module instance
  618. * @param native_ptr the native address to retrieve
  619. * @param p_native_start_addr buffer to output the native start address if not NULL
  620. * @param p_native_end_addr buffer to output the native end address if not NULL
  621. *
  622. * @return true if success, false otherwise.
  623. */
  624. WASM_RUNTIME_API_EXTERN bool
  625. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  626. uint8_t *native_ptr,
  627. uint8_t **p_native_start_addr,
  628. uint8_t **p_native_end_addr);
  629. /**
  630. * Register native functions with same module name
  631. *
  632. * @param module_name the module name of the native functions
  633. * @param native_symbols specifies an array of NativeSymbol structures which
  634. * contain the names, function pointers and signatures
  635. * Note: WASM runtime will not allocate memory to clone the data, so
  636. * user must ensure the array can be used forever
  637. * Meanings of letters in function signature:
  638. * 'i': the parameter is i32 type
  639. * 'I': the parameter is i64 type
  640. * 'f': the parameter is f32 type
  641. * 'F': the parameter is f64 type
  642. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  643. * auto check its boundary before calling the native function.
  644. * If it is followed by '~', the checked length of the pointer
  645. * is gotten from the following parameter, if not, the checked
  646. * length of the pointer is 1.
  647. * '~': the parameter is the pointer's length with i32 type, and must
  648. * follow after '*'
  649. * '$': the parameter is a string (i32 in WASM), and runtime will
  650. * auto check its boundary before calling the native function
  651. * @param n_native_symbols specifies the number of native symbols in the array
  652. *
  653. * @return true if success, false otherwise
  654. */
  655. WASM_RUNTIME_API_EXTERN bool
  656. wasm_runtime_register_natives(const char *module_name,
  657. NativeSymbol *native_symbols,
  658. uint32_t n_native_symbols);
  659. /**
  660. * Register native functions with same module name, similar to
  661. * wasm_runtime_register_natives, the difference is that runtime passes raw
  662. * arguments to native API, which means that the native API should be defined as:
  663. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  664. * and native API should extract arguments one by one from args array with macro
  665. * native_raw_get_arg
  666. * and write the return value back to args[0] with macro
  667. * native_raw_return_type and native_raw_set_return
  668. */
  669. WASM_RUNTIME_API_EXTERN bool
  670. wasm_runtime_register_natives_raw(const char *module_name,
  671. NativeSymbol *native_symbols,
  672. uint32_t n_native_symbols);
  673. /**
  674. * Get attachment of native function from execution environment
  675. *
  676. * @param exec_env the execution environment to retrieve
  677. *
  678. * @return the attachment of native function
  679. */
  680. WASM_RUNTIME_API_EXTERN void *
  681. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  682. /**
  683. * Set user data to execution environment.
  684. *
  685. * @param exec_env the execution environment
  686. * @param user_data the user data to be set
  687. */
  688. WASM_RUNTIME_API_EXTERN void
  689. wasm_runtime_set_user_data(wasm_exec_env_t exec_env,
  690. void *user_data);
  691. /**
  692. * Get the user data within execution environment.
  693. *
  694. * @param exec_env the execution environment
  695. *
  696. * @return the user data (NULL if not set yet)
  697. */
  698. WASM_RUNTIME_API_EXTERN void *
  699. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  700. /**
  701. * Dump runtime memory consumption, including:
  702. * Exec env memory consumption
  703. * WASM module memory consumption
  704. * WASM module instance memory consumption
  705. * stack and app heap used info
  706. *
  707. * @param exec_env the execution environment
  708. */
  709. WASM_RUNTIME_API_EXTERN void
  710. wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env);
  711. /**
  712. * Dump runtime performance profiler data of each function
  713. *
  714. * @param module_inst the WASM module instance to profile
  715. */
  716. WASM_RUNTIME_API_EXTERN void
  717. wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
  718. /* wasm thread callback function type */
  719. typedef void* (*wasm_thread_callback_t)(wasm_exec_env_t, void *);
  720. /* wasm thread type */
  721. typedef uintptr_t wasm_thread_t;
  722. /**
  723. * Set the max thread num per cluster.
  724. *
  725. * @param num maximum thread num
  726. */
  727. WASM_RUNTIME_API_EXTERN void
  728. wasm_runtime_set_max_thread_num(uint32_t num);
  729. /**
  730. * spawn a new exec_env, the spawned exec_env
  731. * can be used in other threads
  732. *
  733. * @param num the original exec_env
  734. *
  735. * @return the spawned exec_env if success, NULL otherwise
  736. */
  737. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  738. wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
  739. /**
  740. * Destroy the spawned exec_env
  741. *
  742. * @param exec_env the spawned exec_env
  743. */
  744. WASM_RUNTIME_API_EXTERN void
  745. wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
  746. /**
  747. * spawn a thread from the given exec_env
  748. *
  749. * @param exec_env the original exec_env
  750. * @param tid thread id to be returned to the caller
  751. * @param callback the callback function provided by the user
  752. * @param arg the arguments passed to the callback
  753. *
  754. * @return 0 if success, -1 otherwise
  755. */
  756. WASM_RUNTIME_API_EXTERN int32_t
  757. wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
  758. wasm_thread_callback_t callback, void *arg);
  759. /**
  760. * waits a spawned thread to terminate
  761. *
  762. * @param tid thread id
  763. * @param retval if not NULL, output the return value of the thread
  764. *
  765. * @return 0 if success, error number otherwise
  766. */
  767. WASM_RUNTIME_API_EXTERN int32_t
  768. wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
  769. /**
  770. * dump the call stack
  771. *
  772. * @param exec_env the execution environment
  773. */
  774. WASM_RUNTIME_API_EXTERN void
  775. wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
  776. #ifdef __cplusplus
  777. }
  778. #endif
  779. #endif /* end of _WASM_EXPORT_H */