wasm_export.h 35 KB

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