wasm_export.h 32 KB

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