wasm_export.h 39 KB

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