wasm_export.h 30 KB

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