wasm_export.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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. /* Debug settings, only used when
  111. WASM_ENABLE_DEBUG_INTERP != 0 */
  112. char ip_addr[128];
  113. int unused; /* was platform_port */
  114. int instance_port;
  115. /* Fast JIT code cache size */
  116. uint32_t fast_jit_code_cache_size;
  117. } RuntimeInitArgs;
  118. #ifndef WASM_VALKIND_T_DEFINED
  119. #define WASM_VALKIND_T_DEFINED
  120. typedef uint8_t wasm_valkind_t;
  121. enum wasm_valkind_enum {
  122. WASM_I32,
  123. WASM_I64,
  124. WASM_F32,
  125. WASM_F64,
  126. WASM_ANYREF = 128,
  127. WASM_FUNCREF,
  128. };
  129. #endif
  130. #ifndef WASM_VAL_T_DEFINED
  131. #define WASM_VAL_T_DEFINED
  132. typedef struct wasm_val_t {
  133. wasm_valkind_t kind;
  134. union {
  135. /* also represent a function index */
  136. int32_t i32;
  137. int64_t i64;
  138. float f32;
  139. double f64;
  140. /* represent a foreign object, aka externref in .wat */
  141. uintptr_t foreign;
  142. } of;
  143. } wasm_val_t;
  144. #endif
  145. /**
  146. * Initialize the WASM runtime environment, and also initialize
  147. * the memory allocator with system allocator, which calls os_malloc
  148. * to allocate memory
  149. *
  150. * @return true if success, false otherwise
  151. */
  152. WASM_RUNTIME_API_EXTERN bool
  153. wasm_runtime_init(void);
  154. /**
  155. * Initialize the WASM runtime environment, and also initialize
  156. * the memory allocator and register native symbols, which are specified
  157. * with init arguments
  158. *
  159. * @param init_args specifies the init arguments
  160. *
  161. * @return return true if success, false otherwise
  162. */
  163. WASM_RUNTIME_API_EXTERN bool
  164. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  165. /**
  166. * Destroy the WASM runtime environment.
  167. */
  168. WASM_RUNTIME_API_EXTERN void
  169. wasm_runtime_destroy(void);
  170. /**
  171. * Allocate memory from runtime memory environment.
  172. *
  173. * @param size bytes need to allocate
  174. *
  175. * @return the pointer to memory allocated
  176. */
  177. WASM_RUNTIME_API_EXTERN void *
  178. wasm_runtime_malloc(unsigned int size);
  179. /**
  180. * Reallocate memory from runtime memory environment
  181. *
  182. * @param ptr the original memory
  183. * @param size bytes need to reallocate
  184. *
  185. * @return the pointer to memory reallocated
  186. */
  187. WASM_RUNTIME_API_EXTERN void *
  188. wasm_runtime_realloc(void *ptr, unsigned int size);
  189. /*
  190. * Free memory to runtime memory environment.
  191. */
  192. WASM_RUNTIME_API_EXTERN void
  193. wasm_runtime_free(void *ptr);
  194. /**
  195. * Get the package type of a buffer.
  196. *
  197. * @param buf the package buffer
  198. * @param size the package buffer size
  199. *
  200. * @return the package type, return Package_Type_Unknown if the type is unknown
  201. */
  202. WASM_RUNTIME_API_EXTERN package_type_t
  203. get_package_type(const uint8_t *buf, uint32_t size);
  204. /**
  205. * Check whether a file is an AOT XIP (Execution In Place) file
  206. *
  207. * @param buf the package buffer
  208. * @param size the package buffer size
  209. *
  210. * @return true if success, false otherwise
  211. */
  212. WASM_RUNTIME_API_EXTERN bool
  213. wasm_runtime_is_xip_file(const uint8_t *buf, uint32_t size);
  214. /**
  215. * It is a callback for WAMR providing by embedding to load a module file
  216. * into a buffer
  217. */
  218. typedef bool (*module_reader)(const char *module_name,
  219. uint8_t **p_buffer, uint32_t *p_size);
  220. /**
  221. * It is a callback for WAMR providing by embedding to release the buffer which
  222. * is used by loading a module file
  223. */
  224. typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size);
  225. /**
  226. * To setup callbacks for reading and releasing a buffer about a module file
  227. *
  228. * @param reader a callback to read a module file into a buffer
  229. * @param destroyer a callback to release above buffer
  230. */
  231. WASM_RUNTIME_API_EXTERN void
  232. wasm_runtime_set_module_reader(const module_reader reader,
  233. const module_destroyer destroyer);
  234. /**
  235. * Give the "module" a name "module_name".
  236. * can not assign a new name to a module if it already has a name
  237. *
  238. * @param module_name indicate a name
  239. * @param module the target module
  240. * @param error_buf output of the exception info
  241. * @param error_buf_size the size of the exception string
  242. *
  243. * @return true means success, false means failed
  244. */
  245. WASM_RUNTIME_API_EXTERN bool
  246. wasm_runtime_register_module(const char *module_name, wasm_module_t module,
  247. char *error_buf, uint32_t error_buf_size);
  248. /**
  249. * To check if there is already a loaded module named module_name in the
  250. * runtime. you will not want to load repeately
  251. *
  252. * @param module_name indicate a name
  253. *
  254. * @return return WASM module loaded, NULL if failed
  255. */
  256. WASM_RUNTIME_API_EXTERN wasm_module_t
  257. wasm_runtime_find_module_registered(const char *module_name);
  258. /**
  259. * Load a WASM module from a specified byte buffer. The byte buffer can be
  260. * WASM binary data when interpreter or JIT is enabled, or AOT binary data
  261. * when AOT is enabled. If it is AOT binary data, it must be 4-byte aligned.
  262. *
  263. * Note: In case of AOT XIP modules, the runtime doesn't make modifications
  264. * to the buffer. (Except the "Known issues" mentioned in doc/xip.md.)
  265. * Otherwise, the runtime can make modifications to the buffer for its
  266. * internal purposes. Thus, in general, it isn't safe to create multiple
  267. * modules from a single buffer.
  268. *
  269. * @param buf the byte buffer which contains the WASM/AOT binary data,
  270. * note that the byte buffer must be writable since runtime may
  271. * change its content for footprint and performance purpose, and
  272. * it must be referencable until wasm_runtime_unload is called
  273. * @param size the size of the buffer
  274. * @param error_buf output of the exception info
  275. * @param error_buf_size the size of the exception string
  276. *
  277. * @return return WASM module loaded, NULL if failed
  278. */
  279. WASM_RUNTIME_API_EXTERN wasm_module_t
  280. wasm_runtime_load(uint8_t *buf, uint32_t size,
  281. char *error_buf, uint32_t error_buf_size);
  282. /**
  283. * Load a WASM module from a specified WASM or AOT section list.
  284. *
  285. * @param section_list the section list which contains each section data
  286. * @param is_aot whether the section list is AOT section list
  287. * @param error_buf output of the exception info
  288. * @param error_buf_size the size of the exception string
  289. *
  290. * @return return WASM module loaded, NULL if failed
  291. */
  292. WASM_RUNTIME_API_EXTERN wasm_module_t
  293. wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
  294. char *error_buf, uint32_t error_buf_size);
  295. /**
  296. * Unload a WASM module.
  297. *
  298. * @param module the module to be unloaded
  299. */
  300. WASM_RUNTIME_API_EXTERN void
  301. wasm_runtime_unload(wasm_module_t module);
  302. WASM_RUNTIME_API_EXTERN void
  303. wasm_runtime_set_wasi_args_ex(wasm_module_t module,
  304. const char *dir_list[], uint32_t dir_count,
  305. const char *map_dir_list[], uint32_t map_dir_count,
  306. const char *env[], uint32_t env_count,
  307. char *argv[], int argc,
  308. int stdinfd, int stdoutfd, int stderrfd);
  309. WASM_RUNTIME_API_EXTERN void
  310. wasm_runtime_set_wasi_args(wasm_module_t module,
  311. const char *dir_list[], uint32_t dir_count,
  312. const char *map_dir_list[], uint32_t map_dir_count,
  313. const char *env[], uint32_t env_count,
  314. char *argv[], int argc);
  315. WASM_RUNTIME_API_EXTERN void
  316. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  317. uint32_t addr_pool_size);
  318. /**
  319. * Instantiate a WASM module.
  320. *
  321. * @param module the WASM module to instantiate
  322. * @param stack_size the default stack size of the module instance when the
  323. * exec env's operation stack isn't created by user, e.g. API
  324. * wasm_application_execute_main() and wasm_application_execute_func()
  325. * create the operation stack internally with the stack size specified
  326. * here. And API wasm_runtime_create_exec_env() creates the operation
  327. * stack with stack size specified by its parameter, the stack size
  328. * specified here is ignored.
  329. * @param heap_size the default heap size of the module instance, a heap will
  330. * be created besides the app memory space. Both wasm app and native
  331. * function can allocate memory from the heap.
  332. * @param error_buf buffer to output the error info if failed
  333. * @param error_buf_size the size of the error buffer
  334. *
  335. * @return return the instantiated WASM module instance, NULL if failed
  336. */
  337. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  338. wasm_runtime_instantiate(const wasm_module_t module,
  339. uint32_t stack_size, uint32_t heap_size,
  340. char *error_buf, uint32_t error_buf_size);
  341. /**
  342. * Deinstantiate a WASM module instance, destroy the resources.
  343. *
  344. * @param module_inst the WASM module instance to destroy
  345. */
  346. WASM_RUNTIME_API_EXTERN void
  347. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  348. WASM_RUNTIME_API_EXTERN bool
  349. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  350. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  351. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  352. /**
  353. * Lookup an exported function in the WASM module instance.
  354. *
  355. * @param module_inst the module instance
  356. * @param name the name of the function
  357. * @param signature the signature of the function, ignored currently
  358. *
  359. * @return the function instance found, NULL if not found
  360. */
  361. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  362. wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
  363. const char *name, const char *signature);
  364. /**
  365. * Get parameter count of the function instance
  366. *
  367. * @param func_inst the function instance
  368. * @param module_inst the module instance the function instance belongs to
  369. *
  370. * @return the parameter count of the function instance
  371. */
  372. WASM_RUNTIME_API_EXTERN uint32_t
  373. wasm_func_get_param_count(wasm_function_inst_t const func_inst,
  374. wasm_module_inst_t const module_inst);
  375. /**
  376. * Get result count of the function instance
  377. *
  378. * @param func_inst the function instance
  379. * @param module_inst the module instance the function instance belongs to
  380. *
  381. * @return the result count of the function instance
  382. */
  383. WASM_RUNTIME_API_EXTERN uint32_t
  384. wasm_func_get_result_count(wasm_function_inst_t const func_inst,
  385. wasm_module_inst_t const module_inst);
  386. /**
  387. * Get parameter types of the function instance
  388. *
  389. * @param func_inst the function instance
  390. * @param module_inst the module instance the function instance belongs to
  391. * @param param_types the parameter types returned
  392. */
  393. WASM_RUNTIME_API_EXTERN void
  394. wasm_func_get_param_types(wasm_function_inst_t const func_inst,
  395. wasm_module_inst_t const module_inst,
  396. wasm_valkind_t *param_types);
  397. /**
  398. * Get result types of the function instance
  399. *
  400. * @param func_inst the function instance
  401. * @param module_inst the module instance the function instance belongs to
  402. * @param result_types the result types returned
  403. */
  404. WASM_RUNTIME_API_EXTERN void
  405. wasm_func_get_result_types(wasm_function_inst_t const func_inst,
  406. wasm_module_inst_t const module_inst,
  407. wasm_valkind_t *result_types);
  408. /**
  409. * Create execution environment for a WASM module instance.
  410. *
  411. * @param module_inst the module instance
  412. * @param stack_size the stack size to execute a WASM function
  413. *
  414. * @return the execution environment, NULL if failed, e.g. invalid
  415. * stack size is passed
  416. */
  417. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  418. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  419. uint32_t stack_size);
  420. /**
  421. * Destroy the execution environment.
  422. *
  423. * @param exec_env the execution environment to destroy
  424. */
  425. WASM_RUNTIME_API_EXTERN void
  426. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  427. /**
  428. * Get the singleton execution environment for the instance.
  429. *
  430. * Note: The singleton execution environment is the execution
  431. * environment used internally by the runtime for the API functions
  432. * like wasm_application_execute_main, which don't take explicit
  433. * execution environment. It's associated to the corresponding
  434. * module instance and managed by the runtime. The API user should
  435. * not destroy it with wasm_runtime_destroy_exec_env.
  436. *
  437. * @param module_inst the module instance
  438. *
  439. * @return exec_env the execution environment to destroy
  440. */
  441. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  442. wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
  443. /**
  444. * Start debug instance based on given execution environment.
  445. * Note:
  446. * The debug instance will be destroyed during destroying the
  447. * execution environment, developers don't need to destroy it
  448. * manually.
  449. * If the cluster of this execution environment has already
  450. * been bound to a debug instance, this function will return true
  451. * directly.
  452. * If developer spawns some exec_env by wasm_runtime_spawn_exec_env,
  453. * don't need to call this function for every spawned exec_env as
  454. * they are sharing the same cluster with the main exec_env.
  455. *
  456. * @param exec_env the execution environment to start debug instance
  457. *
  458. * @return debug port if success, 0 otherwise.
  459. */
  460. WASM_RUNTIME_API_EXTERN uint32_t
  461. wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
  462. /**
  463. * Initialize the thread environment.
  464. * Note:
  465. * If developer creates a child thread by himself to call the
  466. * the wasm function in that thread, he should call this API
  467. * firstly before calling the wasm function and then call
  468. * wasm_runtime_destroy_thread_env() after calling the wasm
  469. * function. If the thread is created from the runtime API,
  470. * it is unnecessary to call these two APIs.
  471. *
  472. * @return true if success, false otherwise
  473. */
  474. WASM_RUNTIME_API_EXTERN bool
  475. wasm_runtime_init_thread_env(void);
  476. /**
  477. * Destroy the thread environment
  478. */
  479. WASM_RUNTIME_API_EXTERN void
  480. wasm_runtime_destroy_thread_env(void);
  481. /**
  482. * Whether the thread environment is initialized
  483. */
  484. WASM_RUNTIME_API_EXTERN bool
  485. wasm_runtime_thread_env_inited(void);
  486. /**
  487. * Get WASM module instance from execution environment
  488. *
  489. * @param exec_env the execution environment to retrieve
  490. *
  491. * @return the WASM module instance
  492. */
  493. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  494. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  495. /**
  496. * Set WASM module instance of execution environment
  497. * Caution:
  498. * normally the module instance is bound with the execution
  499. * environment one by one, if multiple module instances want
  500. * to share to the same execution environment, developer should
  501. * be responsible for the backup and restore of module instance
  502. *
  503. * @param exec_env the execution environment
  504. * @param module_inst the WASM module instance to set
  505. */
  506. WASM_RUNTIME_API_EXTERN void
  507. wasm_runtime_set_module_inst(wasm_exec_env_t exec_env,
  508. const wasm_module_inst_t module_inst);
  509. /**
  510. * Call the given WASM function of a WASM module instance with
  511. * arguments (bytecode and AoT).
  512. *
  513. * @param exec_env the execution environment to call the function,
  514. * which must be created from wasm_create_exec_env()
  515. * @param function the function to call
  516. * @param argc total cell number that the function parameters occupy,
  517. * a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument
  518. * occupies one cell, i64/f64 argument occupies two cells, note that
  519. * it might be different from the parameter number of the function
  520. * @param argv the arguments. If the function has return value,
  521. * the first (or first two in case 64-bit return value) element of
  522. * argv stores the return value of the called WASM function after this
  523. * function returns.
  524. *
  525. * @return true if success, false otherwise and exception will be thrown,
  526. * the caller can call wasm_runtime_get_exception to get the exception
  527. * info.
  528. */
  529. WASM_RUNTIME_API_EXTERN bool
  530. wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
  531. wasm_function_inst_t function,
  532. uint32_t argc, uint32_t argv[]);
  533. /**
  534. * Call the given WASM function of a WASM module instance with
  535. * provided results space and arguments (bytecode and AoT).
  536. *
  537. * @param exec_env the execution environment to call the function,
  538. * which must be created from wasm_create_exec_env()
  539. * @param function the function to call
  540. * @param num_results the number of results
  541. * @param results the pre-alloced pointer to get the results
  542. * @param num_args the number of arguments
  543. * @param args the arguments
  544. *
  545. * @return true if success, false otherwise and exception will be thrown,
  546. * the caller can call wasm_runtime_get_exception to get the exception
  547. * info.
  548. */
  549. WASM_RUNTIME_API_EXTERN bool
  550. wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
  551. wasm_function_inst_t function,
  552. uint32_t num_results, wasm_val_t results[],
  553. uint32_t num_args, wasm_val_t *args);
  554. /**
  555. * Call the given WASM function of a WASM module instance with
  556. * provided results space and variant arguments (bytecode and AoT).
  557. *
  558. * @param exec_env the execution environment to call the function,
  559. * which must be created from wasm_create_exec_env()
  560. * @param function the function to call
  561. * @param num_results the number of results
  562. * @param results the pre-alloced pointer to get the results
  563. * @param num_args the number of arguments
  564. * @param ... the variant arguments
  565. *
  566. * @return true if success, false otherwise and exception will be thrown,
  567. * the caller can call wasm_runtime_get_exception to get the exception
  568. * info.
  569. */
  570. WASM_RUNTIME_API_EXTERN bool
  571. wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
  572. wasm_function_inst_t function,
  573. uint32_t num_results, wasm_val_t results[],
  574. uint32_t num_args, ...);
  575. /**
  576. * Find the unique main function from a WASM module instance
  577. * and execute that function.
  578. *
  579. * @param module_inst the WASM module instance
  580. * @param argc the number of arguments
  581. * @param argv the arguments array, if the main function has return value,
  582. * *(int*)argv stores the return value of the called main function after
  583. * this function returns.
  584. *
  585. * @return true if the main function is called, false otherwise and exception
  586. * will be thrown, the caller can call wasm_runtime_get_exception to get
  587. * the exception info.
  588. */
  589. WASM_RUNTIME_API_EXTERN bool
  590. wasm_application_execute_main(wasm_module_inst_t module_inst,
  591. int32_t argc, char *argv[]);
  592. /**
  593. * Find the specified function in argv[0] from a WASM module instance
  594. * and execute that function.
  595. *
  596. * @param module_inst the WASM module instance
  597. * @param name the name of the function to execute.
  598. * to indicate the module name via: $module_name$function_name
  599. * or just a function name: function_name
  600. * @param argc the number of arguments
  601. * @param argv the arguments array
  602. *
  603. * @return true if the specified function is called, false otherwise and
  604. * exception will be thrown, the caller can call wasm_runtime_get_exception
  605. * to get the exception info.
  606. */
  607. WASM_RUNTIME_API_EXTERN bool
  608. wasm_application_execute_func(wasm_module_inst_t module_inst,
  609. const char *name, int32_t argc, char *argv[]);
  610. /**
  611. * Get exception info of the WASM module instance.
  612. *
  613. * @param module_inst the WASM module instance
  614. *
  615. * @return the exception string
  616. */
  617. WASM_RUNTIME_API_EXTERN const char *
  618. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  619. /**
  620. * Set exception info of the WASM module instance.
  621. *
  622. * @param module_inst the WASM module instance
  623. *
  624. * @param exception the exception string
  625. */
  626. WASM_RUNTIME_API_EXTERN void
  627. wasm_runtime_set_exception(wasm_module_inst_t module_inst,
  628. const char *exception);
  629. /**
  630. * Clear exception info of the WASM module instance.
  631. *
  632. * @param module_inst the WASM module instance
  633. */
  634. WASM_RUNTIME_API_EXTERN void
  635. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  636. /**
  637. * Set custom data to WASM module instance.
  638. * Note:
  639. * If WAMR_BUILD_LIB_PTHREAD is enabled, this API
  640. * will spread the custom data to all threads
  641. *
  642. * @param module_inst the WASM module instance
  643. * @param custom_data the custom data to be set
  644. */
  645. WASM_RUNTIME_API_EXTERN void
  646. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  647. void *custom_data);
  648. /**
  649. * Get the custom data within a WASM module instance.
  650. *
  651. * @param module_inst the WASM module instance
  652. *
  653. * @return the custom data (NULL if not set yet)
  654. */
  655. WASM_RUNTIME_API_EXTERN void *
  656. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  657. /**
  658. * Allocate memory from the heap of WASM module instance
  659. *
  660. * Note: wasm_runtime_module_malloc can call heap functions inside
  661. * the module instance and thus cause a memory growth.
  662. * This API needs to be used very carefully when you have a native
  663. * pointers to the module instance memory obtained with
  664. * wasm_runtime_addr_app_to_native or similar APIs.
  665. *
  666. * @param module_inst the WASM module instance which contains heap
  667. * @param size the size bytes to allocate
  668. * @param p_native_addr return native address of the allocated memory
  669. * if it is not NULL, and return NULL if memory malloc failed
  670. *
  671. * @return the allocated memory address, which is a relative offset to the
  672. * base address of the module instance's memory space. Note that
  673. * it is not an absolute address.
  674. * Return non-zero if success, zero if failed.
  675. */
  676. WASM_RUNTIME_API_EXTERN uint32_t
  677. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size,
  678. void **p_native_addr);
  679. /**
  680. * Free memory to the heap of WASM module instance
  681. *
  682. * @param module_inst the WASM module instance which contains heap
  683. * @param ptr the pointer to free
  684. */
  685. WASM_RUNTIME_API_EXTERN void
  686. wasm_runtime_module_free(wasm_module_inst_t module_inst, uint32_t ptr);
  687. /**
  688. * Allocate memory from the heap of WASM module instance and initialize
  689. * the memory with src
  690. *
  691. * @param module_inst the WASM module instance which contains heap
  692. * @param src the source data to copy
  693. * @param size the size of the source data
  694. *
  695. * @return the allocated memory address, which is a relative offset to the
  696. * base address of the module instance's memory space. Note that
  697. * it is not an absolute address.
  698. * Return non-zero if success, zero if failed.
  699. */
  700. WASM_RUNTIME_API_EXTERN uint32_t
  701. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  702. const char *src, uint32_t size);
  703. /**
  704. * Validate the app address, check whether it belongs to WASM module
  705. * instance's address space, or in its heap space or memory space.
  706. *
  707. * @param module_inst the WASM module instance
  708. * @param app_offset the app address to validate, which is a relative address
  709. * @param size the size bytes of the app address
  710. *
  711. * @return true if success, false otherwise. If failed, an exception will
  712. * be thrown.
  713. */
  714. WASM_RUNTIME_API_EXTERN bool
  715. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  716. uint32_t app_offset, uint32_t size);
  717. /**
  718. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  719. * is not provided. This function validates the app string address, check
  720. * whether it belongs to WASM module instance's address space, or in its heap
  721. * space or memory space. Moreover, it checks whether it is the offset of a
  722. * string that is end with '\0'.
  723. *
  724. * Note: The validation result, especially the NUL termination check,
  725. * is not reliable for a module instance with multiple threads because
  726. * other threads can modify the heap behind us.
  727. *
  728. * @param module_inst the WASM module instance
  729. * @param app_str_offset the app address of the string to validate, which is a
  730. * relative address
  731. *
  732. * @return true if success, false otherwise. If failed, an exception will
  733. * be thrown.
  734. */
  735. WASM_RUNTIME_API_EXTERN bool
  736. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  737. uint32_t app_str_offset);
  738. /**
  739. * Validate the native address, check whether it belongs to WASM module
  740. * instance's address space, or in its heap space or memory space.
  741. *
  742. * @param module_inst the WASM module instance
  743. * @param native_ptr the native address to validate, which is an absolute
  744. * address
  745. * @param size the size bytes of the app address
  746. *
  747. * @return true if success, false otherwise. If failed, an exception will
  748. * be thrown.
  749. */
  750. WASM_RUNTIME_API_EXTERN bool
  751. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  752. void *native_ptr, uint32_t size);
  753. /**
  754. * Convert app address(relative address) to native address(absolute address)
  755. *
  756. * Note that native addresses to module instance memory can be invalidated
  757. * on a memory growth. (Except shared memory, whose native addresses are
  758. * stable.)
  759. *
  760. * @param module_inst the WASM module instance
  761. * @param app_offset the app adress
  762. *
  763. * @return the native address converted
  764. */
  765. WASM_RUNTIME_API_EXTERN void *
  766. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  767. uint32_t app_offset);
  768. /**
  769. * Convert native address(absolute address) to app address(relative address)
  770. *
  771. * @param module_inst the WASM module instance
  772. * @param native_ptr the native address
  773. *
  774. * @return the app address converted
  775. */
  776. WASM_RUNTIME_API_EXTERN uint32_t
  777. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  778. void *native_ptr);
  779. /**
  780. * Get the app address range (relative address) that a app address belongs to
  781. *
  782. * @param module_inst the WASM module instance
  783. * @param app_offset the app address to retrieve
  784. * @param p_app_start_offset buffer to output the app start offset if not NULL
  785. * @param p_app_end_offset buffer to output the app end offset if not NULL
  786. *
  787. * @return true if success, false otherwise.
  788. */
  789. WASM_RUNTIME_API_EXTERN bool
  790. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  791. uint32_t app_offset,
  792. uint32_t *p_app_start_offset,
  793. uint32_t *p_app_end_offset);
  794. /**
  795. * Get the native address range (absolute address) that a native address
  796. * belongs to
  797. *
  798. * @param module_inst the WASM module instance
  799. * @param native_ptr the native address to retrieve
  800. * @param p_native_start_addr buffer to output the native start address
  801. * if not NULL
  802. * @param p_native_end_addr buffer to output the native end address
  803. * if not NULL
  804. *
  805. * @return true if success, false otherwise.
  806. */
  807. WASM_RUNTIME_API_EXTERN bool
  808. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  809. uint8_t *native_ptr,
  810. uint8_t **p_native_start_addr,
  811. uint8_t **p_native_end_addr);
  812. /**
  813. * Register native functions with same module name
  814. *
  815. * @param module_name the module name of the native functions
  816. * @param native_symbols specifies an array of NativeSymbol structures which
  817. * contain the names, function pointers and signatures
  818. * Note: WASM runtime will not allocate memory to clone the data, so
  819. * user must ensure the array can be used forever
  820. * Meanings of letters in function signature:
  821. * 'i': the parameter is i32 type
  822. * 'I': the parameter is i64 type
  823. * 'f': the parameter is f32 type
  824. * 'F': the parameter is f64 type
  825. * 'r': the parameter is externref type, it should be a uintptr_t in host
  826. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  827. * auto check its boundary before calling the native function.
  828. * If it is followed by '~', the checked length of the pointer
  829. * is gotten from the following parameter, if not, the checked
  830. * length of the pointer is 1.
  831. * '~': the parameter is the pointer's length with i32 type, and must
  832. * follow after '*'
  833. * '$': the parameter is a string (i32 in WASM), and runtime will
  834. * auto check its boundary before calling the native function
  835. * @param n_native_symbols specifies the number of native symbols in the array
  836. *
  837. * @return true if success, false otherwise
  838. */
  839. WASM_RUNTIME_API_EXTERN bool
  840. wasm_runtime_register_natives(const char *module_name,
  841. NativeSymbol *native_symbols,
  842. uint32_t n_native_symbols);
  843. /**
  844. * Register native functions with same module name, similar to
  845. * wasm_runtime_register_natives, the difference is that runtime passes raw
  846. * arguments to native API, which means that the native API should be defined as
  847. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  848. * and native API should extract arguments one by one from args array with macro
  849. * native_raw_get_arg
  850. * and write the return value back to args[0] with macro
  851. * native_raw_return_type and native_raw_set_return
  852. */
  853. WASM_RUNTIME_API_EXTERN bool
  854. wasm_runtime_register_natives_raw(const char *module_name,
  855. NativeSymbol *native_symbols,
  856. uint32_t n_native_symbols);
  857. /**
  858. * Get attachment of native function from execution environment
  859. *
  860. * @param exec_env the execution environment to retrieve
  861. *
  862. * @return the attachment of native function
  863. */
  864. WASM_RUNTIME_API_EXTERN void *
  865. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  866. /**
  867. * Set user data to execution environment.
  868. *
  869. * @param exec_env the execution environment
  870. * @param user_data the user data to be set
  871. */
  872. WASM_RUNTIME_API_EXTERN void
  873. wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data);
  874. /**
  875. * Get the user data within execution environment.
  876. *
  877. * @param exec_env the execution environment
  878. *
  879. * @return the user data (NULL if not set yet)
  880. */
  881. WASM_RUNTIME_API_EXTERN void *
  882. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  883. /**
  884. * Dump runtime memory consumption, including:
  885. * Exec env memory consumption
  886. * WASM module memory consumption
  887. * WASM module instance memory consumption
  888. * stack and app heap used info
  889. *
  890. * @param exec_env the execution environment
  891. */
  892. WASM_RUNTIME_API_EXTERN void
  893. wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env);
  894. /**
  895. * Dump runtime performance profiler data of each function
  896. *
  897. * @param module_inst the WASM module instance to profile
  898. */
  899. WASM_RUNTIME_API_EXTERN void
  900. wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
  901. /* wasm thread callback function type */
  902. typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *);
  903. /* wasm thread type */
  904. typedef uintptr_t wasm_thread_t;
  905. /**
  906. * Set the max thread num per cluster.
  907. *
  908. * @param num maximum thread num
  909. */
  910. WASM_RUNTIME_API_EXTERN void
  911. wasm_runtime_set_max_thread_num(uint32_t num);
  912. /**
  913. * Spawn a new exec_env, the spawned exec_env
  914. * can be used in other threads
  915. *
  916. * @param num the original exec_env
  917. *
  918. * @return the spawned exec_env if success, NULL otherwise
  919. */
  920. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  921. wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
  922. /**
  923. * Destroy the spawned exec_env
  924. *
  925. * @param exec_env the spawned exec_env
  926. */
  927. WASM_RUNTIME_API_EXTERN void
  928. wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
  929. /**
  930. * Spawn a thread from the given exec_env
  931. *
  932. * @param exec_env the original exec_env
  933. * @param tid thread id to be returned to the caller
  934. * @param callback the callback function provided by the user
  935. * @param arg the arguments passed to the callback
  936. *
  937. * @return 0 if success, -1 otherwise
  938. */
  939. WASM_RUNTIME_API_EXTERN int32_t
  940. wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
  941. wasm_thread_callback_t callback, void *arg);
  942. /**
  943. * Waits a spawned thread to terminate
  944. *
  945. * @param tid thread id
  946. * @param retval if not NULL, output the return value of the thread
  947. *
  948. * @return 0 if success, error number otherwise
  949. */
  950. WASM_RUNTIME_API_EXTERN int32_t
  951. wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
  952. /**
  953. * Map external object to an internal externref index: if the index
  954. * has been created, return it, otherwise create the index.
  955. *
  956. * @param module_inst the WASM module instance that the extern object
  957. * belongs to
  958. * @param extern_obj the external object to be mapped
  959. * @param p_externref_idx return externref index of the external object
  960. *
  961. * @return true if success, false otherwise
  962. */
  963. WASM_RUNTIME_API_EXTERN bool
  964. wasm_externref_obj2ref(wasm_module_inst_t module_inst,
  965. void *extern_obj, uint32_t *p_externref_idx);
  966. /**
  967. * Retrieve the external object from an internal externref index
  968. *
  969. * @param externref_idx the externref index to retrieve
  970. * @param p_extern_obj return the mapped external object of
  971. * the externref index
  972. *
  973. * @return true if success, false otherwise
  974. */
  975. WASM_RUNTIME_API_EXTERN bool
  976. wasm_externref_ref2obj(uint32_t externref_idx, void **p_extern_obj);
  977. /**
  978. * Retain an extern object which is mapped to the internal externref
  979. * so that the object won't be cleaned during extern object reclaim
  980. * if it isn't used.
  981. *
  982. * @param externref_idx the externref index of an external object
  983. * to retain
  984. * @return true if success, false otherwise
  985. */
  986. WASM_RUNTIME_API_EXTERN bool
  987. wasm_externref_retain(uint32_t externref_idx);
  988. /**
  989. * Dump the call stack to stdout
  990. *
  991. * @param exec_env the execution environment
  992. */
  993. WASM_RUNTIME_API_EXTERN void
  994. wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
  995. /**
  996. * Get the size required to store the call stack contents, including
  997. * the space for terminating null byte ('\0')
  998. *
  999. * @param exec_env the execution environment
  1000. *
  1001. * @return size required to store the contents, 0 means error
  1002. */
  1003. WASM_RUNTIME_API_EXTERN uint32_t
  1004. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env);
  1005. /**
  1006. * Dump the call stack to buffer.
  1007. *
  1008. * @note this function is not thread-safe, please only use this API
  1009. * when the exec_env is not executing
  1010. *
  1011. * @param exec_env the execution environment
  1012. * @param buf buffer to store the dumped content
  1013. * @param len length of the buffer
  1014. *
  1015. * @return bytes dumped to the buffer, including the terminating null
  1016. * byte ('\0'), 0 means error and data in buf may be invalid
  1017. */
  1018. WASM_RUNTIME_API_EXTERN uint32_t
  1019. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  1020. uint32_t len);
  1021. /**
  1022. * Get a custom section by name
  1023. *
  1024. * @param module_comm the module to find
  1025. * @param name name of the custom section
  1026. * @param len return the length of the content if found
  1027. *
  1028. * @return Custom section content (not including the name length
  1029. * and name string) if found, NULL otherwise
  1030. */
  1031. WASM_RUNTIME_API_EXTERN const uint8_t *
  1032. wasm_runtime_get_custom_section(wasm_module_t const module_comm,
  1033. const char *name, uint32_t *len);
  1034. /**
  1035. * Get WAMR semantic version
  1036. */
  1037. WASM_RUNTIME_API_EXTERN void
  1038. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch);
  1039. /* clang-format on */
  1040. #ifdef __cplusplus
  1041. }
  1042. #endif
  1043. #endif /* end of _WASM_EXPORT_H */