wasm_export.h 48 KB

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