wasm_export.h 47 KB

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