wasm_export.h 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. /**
  6. * @file wasm_export.h
  7. *
  8. * @brief This file defines the exported common runtime APIs
  9. */
  10. #ifndef _WASM_EXPORT_H
  11. #define _WASM_EXPORT_H
  12. #include <stdint.h>
  13. #include <stdbool.h>
  14. #include "lib_export.h"
  15. #ifndef WASM_RUNTIME_API_EXTERN
  16. #if defined(_MSC_BUILD)
  17. #if defined(COMPILING_WASM_RUNTIME_API)
  18. #define WASM_RUNTIME_API_EXTERN __declspec(dllexport)
  19. #else
  20. #define WASM_RUNTIME_API_EXTERN __declspec(dllimport)
  21. #endif
  22. #else
  23. #define WASM_RUNTIME_API_EXTERN
  24. #endif
  25. #endif
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #define get_module_inst(exec_env) wasm_runtime_get_module_inst(exec_env)
  30. #define validate_app_addr(offset, size) \
  31. wasm_runtime_validate_app_addr(module_inst, offset, size)
  32. #define validate_app_str_addr(offset) \
  33. wasm_runtime_validate_app_str_addr(module_inst, offset)
  34. #define addr_app_to_native(offset) \
  35. wasm_runtime_addr_app_to_native(module_inst, offset)
  36. #define addr_native_to_app(ptr) \
  37. wasm_runtime_addr_native_to_app(module_inst, ptr)
  38. #define module_malloc(size, p_native_addr) \
  39. wasm_runtime_module_malloc(module_inst, size, p_native_addr)
  40. #define module_free(offset) wasm_runtime_module_free(module_inst, offset)
  41. #define native_raw_return_type(type, args) type *raw_ret = (type *)(args)
  42. #define native_raw_get_arg(type, name, args) type name = *((type *)(args++))
  43. #define native_raw_set_return(val) *raw_ret = (val)
  44. #ifndef WASM_MODULE_T_DEFINED
  45. #define WASM_MODULE_T_DEFINED
  46. /* Uninstantiated WASM module loaded from WASM binary file
  47. or AoT binary file*/
  48. struct WASMModuleCommon;
  49. typedef struct WASMModuleCommon *wasm_module_t;
  50. #endif
  51. typedef enum {
  52. WASM_IMPORT_EXPORT_KIND_FUNC,
  53. WASM_IMPORT_EXPORT_KIND_TABLE,
  54. WASM_IMPORT_EXPORT_KIND_MEMORY,
  55. WASM_IMPORT_EXPORT_KIND_GLOBAL
  56. } wasm_import_export_kind_t;
  57. typedef struct wasm_import_type {
  58. const char *module_name;
  59. const char *name;
  60. wasm_import_export_kind_t kind;
  61. bool linked;
  62. } wasm_import_type;
  63. typedef struct wasm_export_type {
  64. const char *name;
  65. wasm_import_export_kind_t kind;
  66. } wasm_export_type;
  67. /* Instantiated WASM module */
  68. struct WASMModuleInstanceCommon;
  69. typedef struct WASMModuleInstanceCommon *wasm_module_inst_t;
  70. /* Function instance */
  71. typedef void WASMFunctionInstanceCommon;
  72. typedef WASMFunctionInstanceCommon *wasm_function_inst_t;
  73. /* WASM section */
  74. typedef struct wasm_section_t {
  75. struct wasm_section_t *next;
  76. /* section type */
  77. int section_type;
  78. /* section body, not include type and size */
  79. uint8_t *section_body;
  80. /* section body size */
  81. uint32_t section_body_size;
  82. } wasm_section_t, aot_section_t, *wasm_section_list_t, *aot_section_list_t;
  83. /* Execution environment, e.g. stack info */
  84. struct WASMExecEnv;
  85. typedef struct WASMExecEnv *wasm_exec_env_t;
  86. /* Package Type */
  87. typedef enum {
  88. Wasm_Module_Bytecode = 0,
  89. Wasm_Module_AoT,
  90. Package_Type_Unknown = 0xFFFF
  91. } package_type_t;
  92. #ifndef MEM_ALLOC_OPTION_DEFINED
  93. #define MEM_ALLOC_OPTION_DEFINED
  94. /* Memory allocator type */
  95. typedef enum {
  96. /* pool mode, allocate memory from user defined heap buffer */
  97. Alloc_With_Pool = 0,
  98. /* user allocator mode, allocate memory from user defined
  99. malloc function */
  100. Alloc_With_Allocator,
  101. /* system allocator mode, allocate memory from system allocator,
  102. or, platform's os_malloc function */
  103. Alloc_With_System_Allocator,
  104. } mem_alloc_type_t;
  105. typedef enum { Alloc_For_Runtime, Alloc_For_LinearMemory } mem_alloc_usage_t;
  106. /* Memory allocator option */
  107. typedef union MemAllocOption {
  108. struct {
  109. void *heap_buf;
  110. uint32_t heap_size;
  111. } pool;
  112. struct {
  113. /* the function signature is varied when
  114. WASM_MEM_ALLOC_WITH_USER_DATA and
  115. WASM_MEM_ALLOC_WITH_USAGE are defined */
  116. void *malloc_func;
  117. void *realloc_func;
  118. void *free_func;
  119. /* allocator user data, only used when
  120. WASM_MEM_ALLOC_WITH_USER_DATA is defined */
  121. void *user_data;
  122. } allocator;
  123. } MemAllocOption;
  124. #endif
  125. /* Memory pool info */
  126. typedef struct mem_alloc_info_t {
  127. uint32_t total_size;
  128. uint32_t total_free_size;
  129. uint32_t highmark_size;
  130. } mem_alloc_info_t;
  131. /* Running mode of runtime and module instance*/
  132. typedef enum RunningMode {
  133. Mode_Interp = 1,
  134. Mode_Fast_JIT,
  135. Mode_LLVM_JIT,
  136. Mode_Multi_Tier_JIT,
  137. } RunningMode;
  138. /* WASM runtime initialize arguments */
  139. typedef struct RuntimeInitArgs {
  140. mem_alloc_type_t mem_alloc_type;
  141. MemAllocOption mem_alloc_option;
  142. const char *native_module_name;
  143. NativeSymbol *native_symbols;
  144. uint32_t n_native_symbols;
  145. /* maximum thread number, only used when
  146. WASM_ENABLE_THREAD_MGR is defined */
  147. uint32_t max_thread_num;
  148. /* Debug settings, only used when
  149. WASM_ENABLE_DEBUG_INTERP != 0 */
  150. char ip_addr[128];
  151. int unused; /* was platform_port */
  152. int instance_port;
  153. /* Fast JIT code cache size */
  154. uint32_t fast_jit_code_cache_size;
  155. /* Default GC heap size */
  156. uint32_t gc_heap_size;
  157. /* Default running mode of the runtime */
  158. RunningMode running_mode;
  159. /* LLVM JIT opt and size level */
  160. uint32_t llvm_jit_opt_level;
  161. uint32_t llvm_jit_size_level;
  162. /* Segue optimization flags for LLVM JIT */
  163. uint32_t segue_flags;
  164. /**
  165. * If enabled
  166. * - llvm-jit will output a jitdump file for `perf inject`
  167. * - aot will output a perf-${pid}.map for `perf record`
  168. * - fast-jit. TBD
  169. * - multi-tier-jit. TBD
  170. * - interpreter. TBD
  171. */
  172. bool enable_linux_perf;
  173. } RuntimeInitArgs;
  174. #ifndef LOAD_ARGS_OPTION_DEFINED
  175. #define LOAD_ARGS_OPTION_DEFINED
  176. typedef struct LoadArgs {
  177. char *name;
  178. /* TODO: more fields? */
  179. } LoadArgs;
  180. #endif /* LOAD_ARGS_OPTION_DEFINED */
  181. #ifndef INSTANTIATION_ARGS_OPTION_DEFINED
  182. #define INSTANTIATION_ARGS_OPTION_DEFINED
  183. /* WASM module instantiation arguments */
  184. typedef struct InstantiationArgs {
  185. uint32_t default_stack_size;
  186. uint32_t host_managed_heap_size;
  187. uint32_t max_memory_pages;
  188. } InstantiationArgs;
  189. #endif /* INSTANTIATION_ARGS_OPTION_DEFINED */
  190. #ifndef WASM_VALKIND_T_DEFINED
  191. #define WASM_VALKIND_T_DEFINED
  192. typedef uint8_t wasm_valkind_t;
  193. enum wasm_valkind_enum {
  194. WASM_I32,
  195. WASM_I64,
  196. WASM_F32,
  197. WASM_F64,
  198. WASM_ANYREF = 128,
  199. WASM_FUNCREF,
  200. };
  201. #endif
  202. #ifndef WASM_VAL_T_DEFINED
  203. #define WASM_VAL_T_DEFINED
  204. struct wasm_ref_t;
  205. typedef struct wasm_val_t {
  206. wasm_valkind_t kind;
  207. uint8_t __paddings[7];
  208. union {
  209. /* also represent a function index */
  210. int32_t i32;
  211. int64_t i64;
  212. float f32;
  213. double f64;
  214. /* represent a foreign object, aka externref in .wat */
  215. uintptr_t foreign;
  216. struct wasm_ref_t *ref;
  217. } of;
  218. } wasm_val_t;
  219. #endif
  220. typedef enum {
  221. WASM_LOG_LEVEL_FATAL = 0,
  222. WASM_LOG_LEVEL_ERROR = 1,
  223. WASM_LOG_LEVEL_WARNING = 2,
  224. WASM_LOG_LEVEL_DEBUG = 3,
  225. WASM_LOG_LEVEL_VERBOSE = 4
  226. } log_level_t;
  227. /**
  228. * Initialize the WASM runtime environment, and also initialize
  229. * the memory allocator with system allocator, which calls os_malloc
  230. * to allocate memory
  231. *
  232. * @return true if success, false otherwise
  233. */
  234. WASM_RUNTIME_API_EXTERN bool
  235. wasm_runtime_init(void);
  236. /**
  237. * Initialize the WASM runtime environment, WASM running mode,
  238. * and also initialize the memory allocator and register native symbols,
  239. * which are specified with init arguments
  240. *
  241. * @param init_args specifies the init arguments
  242. *
  243. * @return return true if success, false otherwise
  244. */
  245. WASM_RUNTIME_API_EXTERN bool
  246. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  247. /**
  248. * Set the log level. To be called after the runtime is initialized.
  249. *
  250. * @param level the log level to set
  251. */
  252. WASM_RUNTIME_API_EXTERN void
  253. wasm_runtime_set_log_level(log_level_t level);
  254. /**
  255. * Query whether a certain running mode is supported for the runtime
  256. *
  257. * @param running_mode the running mode to query
  258. *
  259. * @return true if this running mode is supported, false otherwise
  260. */
  261. WASM_RUNTIME_API_EXTERN bool
  262. wasm_runtime_is_running_mode_supported(RunningMode running_mode);
  263. /**
  264. * Set the default running mode for the runtime. It is inherited
  265. * to set the running mode of a module instance when it is instantiated,
  266. * and can be changed by calling wasm_runtime_set_running_mode
  267. *
  268. * @param running_mode the running mode to set
  269. *
  270. * @return true if success, false otherwise
  271. */
  272. WASM_RUNTIME_API_EXTERN bool
  273. wasm_runtime_set_default_running_mode(RunningMode running_mode);
  274. /**
  275. * Destroy the WASM runtime environment.
  276. */
  277. WASM_RUNTIME_API_EXTERN void
  278. wasm_runtime_destroy(void);
  279. /**
  280. * Allocate memory from runtime memory environment.
  281. *
  282. * @param size bytes need to allocate
  283. *
  284. * @return the pointer to memory allocated
  285. */
  286. WASM_RUNTIME_API_EXTERN void *
  287. wasm_runtime_malloc(unsigned int size);
  288. /**
  289. * Reallocate memory from runtime memory environment
  290. *
  291. * @param ptr the original memory
  292. * @param size bytes need to reallocate
  293. *
  294. * @return the pointer to memory reallocated
  295. */
  296. WASM_RUNTIME_API_EXTERN void *
  297. wasm_runtime_realloc(void *ptr, unsigned int size);
  298. /*
  299. * Free memory to runtime memory environment.
  300. */
  301. WASM_RUNTIME_API_EXTERN void
  302. wasm_runtime_free(void *ptr);
  303. /*
  304. * Get memory info, only pool mode is supported now.
  305. */
  306. WASM_RUNTIME_API_EXTERN bool
  307. wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info);
  308. /**
  309. * Get the package type of a buffer.
  310. *
  311. * @param buf the package buffer
  312. * @param size the package buffer size
  313. *
  314. * @return the package type, return Package_Type_Unknown if the type is unknown
  315. */
  316. WASM_RUNTIME_API_EXTERN package_type_t
  317. get_package_type(const uint8_t *buf, uint32_t size);
  318. /**
  319. * Check whether a file is an AOT XIP (Execution In Place) file
  320. *
  321. * @param buf the package buffer
  322. * @param size the package buffer size
  323. *
  324. * @return true if success, false otherwise
  325. */
  326. WASM_RUNTIME_API_EXTERN bool
  327. wasm_runtime_is_xip_file(const uint8_t *buf, uint32_t size);
  328. /**
  329. * Callback to load a module file into a buffer in multi-module feature
  330. */
  331. typedef bool (*module_reader)(package_type_t module_type,
  332. const char *module_name, uint8_t **p_buffer,
  333. uint32_t *p_size);
  334. /**
  335. * Callback to release the buffer loaded by module_reader callback
  336. */
  337. typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size);
  338. /**
  339. * Setup callbacks for reading and releasing a buffer about a module file
  340. *
  341. * @param reader a callback to read a module file into a buffer
  342. * @param destroyer a callback to release above buffer
  343. */
  344. WASM_RUNTIME_API_EXTERN void
  345. wasm_runtime_set_module_reader(const module_reader reader,
  346. const module_destroyer destroyer);
  347. /**
  348. * Give the "module" a name "module_name".
  349. * Can not assign a new name to a module if it already has a name
  350. *
  351. * @param module_name indicate a name
  352. * @param module the target module
  353. * @param error_buf output of the exception info
  354. * @param error_buf_size the size of the exception string
  355. *
  356. * @return true means success, false means failed
  357. */
  358. WASM_RUNTIME_API_EXTERN bool
  359. wasm_runtime_register_module(const char *module_name, wasm_module_t module,
  360. char *error_buf, uint32_t error_buf_size);
  361. /**
  362. * Check if there is already a loaded module named module_name in the
  363. * runtime. Repeately loading a module with the same name is not allowed.
  364. *
  365. * @param module_name indicate a name
  366. *
  367. * @return return WASM module loaded, NULL if failed
  368. */
  369. WASM_RUNTIME_API_EXTERN wasm_module_t
  370. wasm_runtime_find_module_registered(const char *module_name);
  371. /**
  372. * Load a WASM module from a specified byte buffer. The byte buffer can be
  373. * WASM binary data when interpreter or JIT is enabled, or AOT binary data
  374. * when AOT is enabled. If it is AOT binary data, it must be 4-byte aligned.
  375. *
  376. * Note: In case of AOT XIP modules, the runtime doesn't make modifications
  377. * to the buffer. (Except the "Known issues" mentioned in doc/xip.md.)
  378. * Otherwise, the runtime can make modifications to the buffer for its
  379. * internal purposes. Thus, in general, it isn't safe to create multiple
  380. * modules from a single buffer.
  381. *
  382. * @param buf the byte buffer which contains the WASM/AOT binary data,
  383. * note that the byte buffer must be writable since runtime may
  384. * change its content for footprint and performance purpose, and
  385. * it must be referencable until wasm_runtime_unload is called
  386. * @param size the size of the buffer
  387. * @param error_buf output of the exception info
  388. * @param error_buf_size the size of the exception string
  389. *
  390. * @return return WASM module loaded, NULL if failed
  391. */
  392. WASM_RUNTIME_API_EXTERN wasm_module_t
  393. wasm_runtime_load(uint8_t *buf, uint32_t size, char *error_buf,
  394. uint32_t error_buf_size);
  395. /**
  396. * Load a WASM module with specified load argument.
  397. */
  398. WASM_RUNTIME_API_EXTERN wasm_module_t
  399. wasm_runtime_load_ex(uint8_t *buf, uint32_t size, const LoadArgs *args,
  400. char *error_buf, uint32_t error_buf_size);
  401. /**
  402. * Load a WASM module from a specified WASM or AOT section list.
  403. *
  404. * @param section_list the section list which contains each section data
  405. * @param is_aot whether the section list is AOT section list
  406. * @param error_buf output of the exception info
  407. * @param error_buf_size the size of the exception string
  408. *
  409. * @return return WASM module loaded, NULL if failed
  410. */
  411. WASM_RUNTIME_API_EXTERN wasm_module_t
  412. wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
  413. char *error_buf, uint32_t error_buf_size);
  414. /**
  415. * Unload a WASM module.
  416. *
  417. * @param module the module to be unloaded
  418. */
  419. WASM_RUNTIME_API_EXTERN void
  420. wasm_runtime_unload(wasm_module_t module);
  421. /**
  422. * Get the module hash of a WASM module, currently only available on
  423. * linux-sgx platform when the remote attestation feature is enabled
  424. *
  425. * @param module the WASM module to retrieve
  426. *
  427. * @return the module hash of the WASM module
  428. */
  429. char *
  430. wasm_runtime_get_module_hash(wasm_module_t module);
  431. /**
  432. * Set WASI parameters.
  433. *
  434. * While this API operates on a module, these parameters will be used
  435. * only when the module is instantiated. That is, you can consider these
  436. * as extra parameters for wasm_runtime_instantiate().
  437. *
  438. * @param module The module to set WASI parameters.
  439. * @param dir_list The list of directories to preopen. (real path)
  440. * @param dir_count The number of elements in dir_list.
  441. * @param map_dir_list The list of directories to preopen. (mapped path)
  442. * Format for each map entry: <guest-path>::<host-path>
  443. * @param map_dir_count The number of elements in map_dir_list.
  444. * If map_dir_count is smaller than dir_count,
  445. * mapped path is assumed to be same as the
  446. * corresponding real path for the rest of entries.
  447. * @param env The list of environment variables.
  448. * @param env_count The number of elements in env.
  449. * @param argv The list of command line arguments.
  450. * @param argc The number of elements in argv.
  451. * @param stdin_handle The raw host handle to back WASI STDIN_FILENO.
  452. * If an invalid handle is specified (e.g. -1 on POSIX,
  453. * INVALID_HANDLE_VALUE on Windows), the platform default
  454. * for STDIN is used.
  455. * @param stdoutfd The raw host handle to back WASI STDOUT_FILENO.
  456. * If an invalid handle is specified (e.g. -1 on POSIX,
  457. * INVALID_HANDLE_VALUE on Windows), the platform default
  458. * for STDOUT is used.
  459. * @param stderrfd The raw host handle to back WASI STDERR_FILENO.
  460. * If an invalid handle is specified (e.g. -1 on POSIX,
  461. * INVALID_HANDLE_VALUE on Windows), the platform default
  462. * for STDERR is used.
  463. */
  464. WASM_RUNTIME_API_EXTERN void
  465. wasm_runtime_set_wasi_args_ex(wasm_module_t module, const char *dir_list[],
  466. uint32_t dir_count, const char *map_dir_list[],
  467. uint32_t map_dir_count, const char *env[],
  468. uint32_t env_count, char *argv[], int argc,
  469. int64_t stdinfd, int64_t stdoutfd,
  470. int64_t stderrfd);
  471. /**
  472. * Set WASI parameters.
  473. *
  474. * Same as wasm_runtime_set_wasi_args_ex but with default stdio handles
  475. */
  476. WASM_RUNTIME_API_EXTERN void
  477. wasm_runtime_set_wasi_args(wasm_module_t module, const char *dir_list[],
  478. uint32_t dir_count, const char *map_dir_list[],
  479. uint32_t map_dir_count, const char *env[],
  480. uint32_t env_count, char *argv[], int argc);
  481. WASM_RUNTIME_API_EXTERN void
  482. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  483. uint32_t addr_pool_size);
  484. WASM_RUNTIME_API_EXTERN void
  485. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  486. const char *ns_lookup_pool[],
  487. uint32_t ns_lookup_pool_size);
  488. /**
  489. * Instantiate a WASM module.
  490. *
  491. * @param module the WASM module to instantiate
  492. * @param default_stack_size the default stack size of the module instance when
  493. * the exec env's operation stack isn't created by user, e.g. API
  494. * wasm_application_execute_main() and wasm_application_execute_func()
  495. * create the operation stack internally with the stack size specified
  496. * here. And API wasm_runtime_create_exec_env() creates the operation
  497. * stack with stack size specified by its parameter, the stack size
  498. * specified here is ignored.
  499. * @param host_managed_heap_size the default heap size of the module instance,
  500. * a heap will be created besides the app memory space. Both wasm app
  501. * and native function can allocate memory from the heap.
  502. * @param error_buf buffer to output the error info if failed
  503. * @param error_buf_size the size of the error buffer
  504. *
  505. * @return return the instantiated WASM module instance, NULL if failed
  506. */
  507. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  508. wasm_runtime_instantiate(const wasm_module_t module,
  509. uint32_t default_stack_size,
  510. uint32_t host_managed_heap_size, char *error_buf,
  511. uint32_t error_buf_size);
  512. /**
  513. * Instantiate a WASM module, with specified instantiation arguments
  514. *
  515. * Same as wasm_runtime_instantiate, but it also allows overwriting maximum
  516. * memory
  517. */
  518. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  519. wasm_runtime_instantiate_ex(const wasm_module_t module,
  520. const InstantiationArgs *args, char *error_buf,
  521. uint32_t error_buf_size);
  522. /**
  523. * Set the running mode of a WASM module instance, override the
  524. * default running mode of the runtime. Note that it only makes sense when
  525. * the input is a wasm bytecode file: for the AOT file, runtime always runs
  526. * it with AOT engine, and this function always returns true.
  527. *
  528. * @param module_inst the WASM module instance to set running mode
  529. * @param running_mode the running mode to set
  530. *
  531. * @return true if success, false otherwise
  532. */
  533. WASM_RUNTIME_API_EXTERN bool
  534. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  535. RunningMode running_mode);
  536. /**
  537. * Get the running mode of a WASM module instance, if no running mode
  538. * is explicitly set the default running mode of runtime will
  539. * be used and returned. Note that it only makes sense when the input is a
  540. * wasm bytecode file: for the AOT file, this function always returns 0.
  541. *
  542. * @param module_inst the WASM module instance to query for running mode
  543. *
  544. * @return the running mode this module instance currently use
  545. */
  546. WASM_RUNTIME_API_EXTERN RunningMode
  547. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
  548. /**
  549. * Deinstantiate a WASM module instance, destroy the resources.
  550. *
  551. * @param module_inst the WASM module instance to destroy
  552. */
  553. WASM_RUNTIME_API_EXTERN void
  554. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  555. /**
  556. * Get WASM module from WASM module instance
  557. *
  558. * @param module_inst the WASM module instance to retrieve
  559. *
  560. * @return the WASM module
  561. */
  562. WASM_RUNTIME_API_EXTERN wasm_module_t
  563. wasm_runtime_get_module(wasm_module_inst_t module_inst);
  564. WASM_RUNTIME_API_EXTERN bool
  565. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  566. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  567. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  568. /**
  569. * Get WASI exit code.
  570. *
  571. * After a WASI command completed its execution, an embedder can
  572. * call this function to get its exit code. (that is, the value given
  573. * to proc_exit.)
  574. *
  575. * @param module_inst the module instance
  576. */
  577. WASM_RUNTIME_API_EXTERN uint32_t
  578. wasm_runtime_get_wasi_exit_code(wasm_module_inst_t module_inst);
  579. /**
  580. * Lookup an exported function in the WASM module instance.
  581. *
  582. * @param module_inst the module instance
  583. * @param name the name of the function
  584. *
  585. * @return the function instance found, NULL if not found
  586. */
  587. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  588. wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
  589. const char *name);
  590. /**
  591. * Get parameter count of the function instance
  592. *
  593. * @param func_inst the function instance
  594. * @param module_inst the module instance the function instance belongs to
  595. *
  596. * @return the parameter count of the function instance
  597. */
  598. WASM_RUNTIME_API_EXTERN uint32_t
  599. wasm_func_get_param_count(wasm_function_inst_t const func_inst,
  600. wasm_module_inst_t const module_inst);
  601. /**
  602. * Get result count of the function instance
  603. *
  604. * @param func_inst the function instance
  605. * @param module_inst the module instance the function instance belongs to
  606. *
  607. * @return the result count of the function instance
  608. */
  609. WASM_RUNTIME_API_EXTERN uint32_t
  610. wasm_func_get_result_count(wasm_function_inst_t const func_inst,
  611. wasm_module_inst_t const module_inst);
  612. /**
  613. * Get parameter types of the function instance
  614. *
  615. * @param func_inst the function instance
  616. * @param module_inst the module instance the function instance belongs to
  617. * @param param_types the parameter types returned
  618. */
  619. WASM_RUNTIME_API_EXTERN void
  620. wasm_func_get_param_types(wasm_function_inst_t const func_inst,
  621. wasm_module_inst_t const module_inst,
  622. wasm_valkind_t *param_types);
  623. /**
  624. * Get result types of the function instance
  625. *
  626. * @param func_inst the function instance
  627. * @param module_inst the module instance the function instance belongs to
  628. * @param result_types the result types returned
  629. */
  630. WASM_RUNTIME_API_EXTERN void
  631. wasm_func_get_result_types(wasm_function_inst_t const func_inst,
  632. wasm_module_inst_t const module_inst,
  633. wasm_valkind_t *result_types);
  634. /**
  635. * Create execution environment for a WASM module instance.
  636. *
  637. * @param module_inst the module instance
  638. * @param stack_size the stack size to execute a WASM function
  639. *
  640. * @return the execution environment, NULL if failed, e.g. invalid
  641. * stack size is passed
  642. */
  643. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  644. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  645. uint32_t stack_size);
  646. /**
  647. * Destroy the execution environment.
  648. *
  649. * @param exec_env the execution environment to destroy
  650. */
  651. WASM_RUNTIME_API_EXTERN void
  652. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  653. /**
  654. * Get the singleton execution environment for the instance.
  655. *
  656. * Note: The singleton execution environment is the execution
  657. * environment used internally by the runtime for the API functions
  658. * like wasm_application_execute_main, which don't take explicit
  659. * execution environment. It's associated to the corresponding
  660. * module instance and managed by the runtime. The API user should
  661. * not destroy it with wasm_runtime_destroy_exec_env.
  662. *
  663. * @param module_inst the module instance
  664. *
  665. * @return exec_env the execution environment to destroy
  666. */
  667. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  668. wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
  669. /**
  670. * Start debug instance based on given execution environment.
  671. * Note:
  672. * The debug instance will be destroyed during destroying the
  673. * execution environment, developers don't need to destroy it
  674. * manually.
  675. * If the cluster of this execution environment has already
  676. * been bound to a debug instance, this function will return true
  677. * directly.
  678. * If developer spawns some exec_env by wasm_runtime_spawn_exec_env,
  679. * don't need to call this function for every spawned exec_env as
  680. * they are sharing the same cluster with the main exec_env.
  681. *
  682. * @param exec_env the execution environment to start debug instance
  683. * @param port the port for the debug server to listen on.
  684. * 0 means automatic assignment.
  685. * -1 means to use the global setting in RuntimeInitArgs.
  686. *
  687. * @return debug port if success, 0 otherwise.
  688. */
  689. WASM_RUNTIME_API_EXTERN uint32_t
  690. wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env,
  691. int32_t port);
  692. /**
  693. * Same as wasm_runtime_start_debug_instance_with_port(env, -1).
  694. */
  695. WASM_RUNTIME_API_EXTERN uint32_t
  696. wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
  697. /**
  698. * Initialize the thread environment.
  699. * Note:
  700. * If developer creates a child thread by himself to call the
  701. * the wasm function in that thread, he should call this API
  702. * firstly before calling the wasm function and then call
  703. * wasm_runtime_destroy_thread_env() after calling the wasm
  704. * function. If the thread is created from the runtime API,
  705. * it is unnecessary to call these two APIs.
  706. *
  707. * @return true if success, false otherwise
  708. */
  709. WASM_RUNTIME_API_EXTERN bool
  710. wasm_runtime_init_thread_env(void);
  711. /**
  712. * Destroy the thread environment
  713. */
  714. WASM_RUNTIME_API_EXTERN void
  715. wasm_runtime_destroy_thread_env(void);
  716. /**
  717. * Whether the thread environment is initialized
  718. */
  719. WASM_RUNTIME_API_EXTERN bool
  720. wasm_runtime_thread_env_inited(void);
  721. /**
  722. * Get WASM module instance from execution environment
  723. *
  724. * @param exec_env the execution environment to retrieve
  725. *
  726. * @return the WASM module instance
  727. */
  728. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  729. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  730. /**
  731. * Set WASM module instance of execution environment
  732. * Caution:
  733. * normally the module instance is bound with the execution
  734. * environment one by one, if multiple module instances want
  735. * to share to the same execution environment, developer should
  736. * be responsible for the backup and restore of module instance
  737. *
  738. * @param exec_env the execution environment
  739. * @param module_inst the WASM module instance to set
  740. */
  741. WASM_RUNTIME_API_EXTERN void
  742. wasm_runtime_set_module_inst(wasm_exec_env_t exec_env,
  743. const wasm_module_inst_t module_inst);
  744. /**
  745. * Call the given WASM function of a WASM module instance with
  746. * arguments (bytecode and AoT).
  747. *
  748. * @param exec_env the execution environment to call the function,
  749. * which must be created from wasm_create_exec_env()
  750. * @param function the function to call
  751. * @param argc total cell number that the function parameters occupy,
  752. * a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument
  753. * occupies one cell, i64/f64 argument occupies two cells, note that
  754. * it might be different from the parameter number of the function
  755. * @param argv the arguments. If the function has return value,
  756. * the first (or first two in case 64-bit return value) element of
  757. * argv stores the return value of the called WASM function after this
  758. * function returns.
  759. *
  760. * @return true if success, false otherwise and exception will be thrown,
  761. * the caller can call wasm_runtime_get_exception to get the exception
  762. * info.
  763. */
  764. WASM_RUNTIME_API_EXTERN bool
  765. wasm_runtime_call_wasm(wasm_exec_env_t exec_env, wasm_function_inst_t function,
  766. uint32_t argc, uint32_t argv[]);
  767. /**
  768. * Call the given WASM function of a WASM module instance with
  769. * provided results space and arguments (bytecode and AoT).
  770. *
  771. * @param exec_env the execution environment to call the function,
  772. * which must be created from wasm_create_exec_env()
  773. * @param function the function to call
  774. * @param num_results the number of results
  775. * @param results the pre-alloced pointer to get the results
  776. * @param num_args the number of arguments
  777. * @param args the arguments
  778. *
  779. * @return true if success, false otherwise and exception will be thrown,
  780. * the caller can call wasm_runtime_get_exception to get the exception
  781. * info.
  782. */
  783. WASM_RUNTIME_API_EXTERN bool
  784. wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
  785. wasm_function_inst_t function, uint32_t num_results,
  786. wasm_val_t results[], uint32_t num_args,
  787. wasm_val_t *args);
  788. /**
  789. * Call the given WASM function of a WASM module instance with
  790. * provided results space and variant arguments (bytecode and AoT).
  791. *
  792. * @param exec_env the execution environment to call the function,
  793. * which must be created from wasm_create_exec_env()
  794. * @param function the function to call
  795. * @param num_results the number of results
  796. * @param results the pre-alloced pointer to get the results
  797. * @param num_args the number of arguments
  798. * @param ... the variant arguments
  799. *
  800. * @return true if success, false otherwise and exception will be thrown,
  801. * the caller can call wasm_runtime_get_exception to get the exception
  802. * info.
  803. */
  804. WASM_RUNTIME_API_EXTERN bool
  805. wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
  806. wasm_function_inst_t function, uint32_t num_results,
  807. wasm_val_t results[], uint32_t num_args, ...);
  808. /**
  809. * Call a function reference of a given WASM runtime instance with
  810. * arguments.
  811. *
  812. * Note: this can be used to call a function which is not exported
  813. * by the module explicitly. You might consider it as an abstraction
  814. * violation.
  815. *
  816. * @param exec_env the execution environment to call the function
  817. * which must be created from wasm_create_exec_env()
  818. * @param element_index the function reference index, usually
  819. * prvovided by the caller of a registed native function
  820. * @param argc the number of arguments
  821. * @param argv the arguments. If the function method has return value,
  822. * the first (or first two in case 64-bit return value) element of
  823. * argv stores the return value of the called WASM function after this
  824. * function returns.
  825. *
  826. * @return true if success, false otherwise and exception will be thrown,
  827. * the caller can call wasm_runtime_get_exception to get exception info.
  828. */
  829. WASM_RUNTIME_API_EXTERN bool
  830. wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32_t element_index,
  831. uint32_t argc, uint32_t argv[]);
  832. /**
  833. * Find the unique main function from a WASM module instance
  834. * and execute that function.
  835. *
  836. * @param module_inst the WASM module instance
  837. * @param argc the number of arguments
  838. * @param argv the arguments array, if the main function has return value,
  839. * *(int*)argv stores the return value of the called main function after
  840. * this function returns.
  841. *
  842. * @return true if the main function is called, false otherwise and exception
  843. * will be thrown, the caller can call wasm_runtime_get_exception to get
  844. * the exception info.
  845. */
  846. WASM_RUNTIME_API_EXTERN bool
  847. wasm_application_execute_main(wasm_module_inst_t module_inst, int32_t argc,
  848. char *argv[]);
  849. /**
  850. * Find the specified function in argv[0] from a WASM module instance
  851. * and execute that function.
  852. *
  853. * @param module_inst the WASM module instance
  854. * @param name the name of the function to execute.
  855. * to indicate the module name via: $module_name$function_name
  856. * or just a function name: function_name
  857. * @param argc the number of arguments
  858. * @param argv the arguments array
  859. *
  860. * @return true if the specified function is called, false otherwise and
  861. * exception will be thrown, the caller can call wasm_runtime_get_exception
  862. * to get the exception info.
  863. */
  864. WASM_RUNTIME_API_EXTERN bool
  865. wasm_application_execute_func(wasm_module_inst_t module_inst, const char *name,
  866. int32_t argc, char *argv[]);
  867. /**
  868. * Get exception info of the WASM module instance.
  869. *
  870. * @param module_inst the WASM module instance
  871. *
  872. * @return the exception string
  873. */
  874. WASM_RUNTIME_API_EXTERN const char *
  875. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  876. /**
  877. * Set exception info of the WASM module instance.
  878. *
  879. * @param module_inst the WASM module instance
  880. *
  881. * @param exception the exception string
  882. */
  883. WASM_RUNTIME_API_EXTERN void
  884. wasm_runtime_set_exception(wasm_module_inst_t module_inst,
  885. const char *exception);
  886. /**
  887. * Clear exception info of the WASM module instance.
  888. *
  889. * @param module_inst the WASM module instance
  890. */
  891. WASM_RUNTIME_API_EXTERN void
  892. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  893. /**
  894. * Terminate the WASM module instance.
  895. *
  896. * This function causes the module instance fail as if it raised a trap.
  897. *
  898. * This is intended to be used in situations like:
  899. *
  900. * - A thread is executing the WASM module instance
  901. * (eg. it's in the middle of `wasm_application_execute_main`)
  902. *
  903. * - Another thread has a copy of `wasm_module_inst_t` of
  904. * the module instance and wants to terminate it asynchronously.
  905. *
  906. * @param module_inst the WASM module instance
  907. */
  908. WASM_RUNTIME_API_EXTERN void
  909. wasm_runtime_terminate(wasm_module_inst_t module_inst);
  910. /**
  911. * Set custom data to WASM module instance.
  912. * Note:
  913. * If WAMR_BUILD_LIB_PTHREAD is enabled, this API
  914. * will spread the custom data to all threads
  915. *
  916. * @param module_inst the WASM module instance
  917. * @param custom_data the custom data to be set
  918. */
  919. WASM_RUNTIME_API_EXTERN void
  920. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst, void *custom_data);
  921. /**
  922. * Get the custom data within a WASM module instance.
  923. *
  924. * @param module_inst the WASM module instance
  925. *
  926. * @return the custom data (NULL if not set yet)
  927. */
  928. WASM_RUNTIME_API_EXTERN void *
  929. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  930. /**
  931. * Set the memory bounds checks flag of a WASM module instance.
  932. *
  933. * @param module_inst the WASM module instance
  934. * @param enable the flag to enable/disable the memory bounds checks
  935. */
  936. WASM_RUNTIME_API_EXTERN void
  937. wasm_runtime_set_bounds_checks(wasm_module_inst_t module_inst, bool enable);
  938. /**
  939. * Check if the memory bounds checks flag is enabled for a WASM module instance.
  940. *
  941. * @param module_inst the WASM module instance
  942. * @return true if the memory bounds checks flag is enabled, false otherwise
  943. */
  944. WASM_RUNTIME_API_EXTERN bool
  945. wasm_runtime_is_bounds_checks_enabled(wasm_module_inst_t module_inst);
  946. /**
  947. * Allocate memory from the heap of WASM module instance
  948. *
  949. * Note: wasm_runtime_module_malloc can call heap functions inside
  950. * the module instance and thus cause a memory growth.
  951. * This API needs to be used very carefully when you have a native
  952. * pointers to the module instance memory obtained with
  953. * wasm_runtime_addr_app_to_native or similar APIs.
  954. *
  955. * @param module_inst the WASM module instance which contains heap
  956. * @param size the size bytes to allocate
  957. * @param p_native_addr return native address of the allocated memory
  958. * if it is not NULL, and return NULL if memory malloc failed
  959. *
  960. * @return the allocated memory address, which is a relative offset to the
  961. * base address of the module instance's memory space. Note that
  962. * it is not an absolute address.
  963. * Return non-zero if success, zero if failed.
  964. */
  965. WASM_RUNTIME_API_EXTERN uint64_t
  966. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint64_t size,
  967. void **p_native_addr);
  968. /**
  969. * Free memory to the heap of WASM module instance
  970. *
  971. * @param module_inst the WASM module instance which contains heap
  972. * @param ptr the pointer to free
  973. */
  974. WASM_RUNTIME_API_EXTERN void
  975. wasm_runtime_module_free(wasm_module_inst_t module_inst, uint64_t ptr);
  976. /**
  977. * Allocate memory from the heap of WASM module instance and initialize
  978. * the memory with src
  979. *
  980. * @param module_inst the WASM module instance which contains heap
  981. * @param src the source data to copy
  982. * @param size the size of the source data
  983. *
  984. * @return the allocated memory address, which is a relative offset to the
  985. * base address of the module instance's memory space. Note that
  986. * it is not an absolute address.
  987. * Return non-zero if success, zero if failed.
  988. */
  989. WASM_RUNTIME_API_EXTERN uint64_t
  990. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst, const char *src,
  991. uint64_t size);
  992. /**
  993. * Validate the app address, check whether it belongs to WASM module
  994. * instance's address space, or in its heap space or memory space.
  995. *
  996. * @param module_inst the WASM module instance
  997. * @param app_offset the app address to validate, which is a relative address
  998. * @param size the size bytes of the app address
  999. *
  1000. * @return true if success, false otherwise. If failed, an exception will
  1001. * be thrown.
  1002. */
  1003. WASM_RUNTIME_API_EXTERN bool
  1004. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  1005. uint64_t app_offset, uint64_t size);
  1006. /**
  1007. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  1008. * is not provided. This function validates the app string address, check
  1009. * whether it belongs to WASM module instance's address space, or in its heap
  1010. * space or memory space. Moreover, it checks whether it is the offset of a
  1011. * string that is end with '\0'.
  1012. *
  1013. * Note: The validation result, especially the NUL termination check,
  1014. * is not reliable for a module instance with multiple threads because
  1015. * other threads can modify the heap behind us.
  1016. *
  1017. * @param module_inst the WASM module instance
  1018. * @param app_str_offset the app address of the string to validate, which is a
  1019. * relative address
  1020. *
  1021. * @return true if success, false otherwise. If failed, an exception will
  1022. * be thrown.
  1023. */
  1024. WASM_RUNTIME_API_EXTERN bool
  1025. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  1026. uint64_t app_str_offset);
  1027. /**
  1028. * Validate the native address, check whether it belongs to WASM module
  1029. * instance's address space, or in its heap space or memory space.
  1030. *
  1031. * @param module_inst the WASM module instance
  1032. * @param native_ptr the native address to validate, which is an absolute
  1033. * address
  1034. * @param size the size bytes of the app address
  1035. *
  1036. * @return true if success, false otherwise. If failed, an exception will
  1037. * be thrown.
  1038. */
  1039. WASM_RUNTIME_API_EXTERN bool
  1040. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  1041. void *native_ptr, uint64_t size);
  1042. /**
  1043. * Convert app address(relative address) to native address(absolute address)
  1044. *
  1045. * Note that native addresses to module instance memory can be invalidated
  1046. * on a memory growth. (Except shared memory, whose native addresses are
  1047. * stable.)
  1048. *
  1049. * @param module_inst the WASM module instance
  1050. * @param app_offset the app adress
  1051. *
  1052. * @return the native address converted
  1053. */
  1054. WASM_RUNTIME_API_EXTERN void *
  1055. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  1056. uint64_t app_offset);
  1057. /**
  1058. * Convert native address(absolute address) to app address(relative address)
  1059. *
  1060. * @param module_inst the WASM module instance
  1061. * @param native_ptr the native address
  1062. *
  1063. * @return the app address converted
  1064. */
  1065. WASM_RUNTIME_API_EXTERN uint64_t
  1066. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  1067. void *native_ptr);
  1068. /**
  1069. * Get the app address range (relative address) that a app address belongs to
  1070. *
  1071. * @param module_inst the WASM module instance
  1072. * @param app_offset the app address to retrieve
  1073. * @param p_app_start_offset buffer to output the app start offset if not NULL
  1074. * @param p_app_end_offset buffer to output the app end offset if not NULL
  1075. *
  1076. * @return true if success, false otherwise.
  1077. */
  1078. WASM_RUNTIME_API_EXTERN bool
  1079. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  1080. uint64_t app_offset,
  1081. uint64_t *p_app_start_offset,
  1082. uint64_t *p_app_end_offset);
  1083. /**
  1084. * Get the native address range (absolute address) that a native address
  1085. * belongs to
  1086. *
  1087. * @param module_inst the WASM module instance
  1088. * @param native_ptr the native address to retrieve
  1089. * @param p_native_start_addr buffer to output the native start address
  1090. * if not NULL
  1091. * @param p_native_end_addr buffer to output the native end address
  1092. * if not NULL
  1093. *
  1094. * @return true if success, false otherwise.
  1095. */
  1096. WASM_RUNTIME_API_EXTERN bool
  1097. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  1098. uint8_t *native_ptr,
  1099. uint8_t **p_native_start_addr,
  1100. uint8_t **p_native_end_addr);
  1101. /**
  1102. * Get the number of import items for a WASM module
  1103. *
  1104. * @param module the WASM module
  1105. *
  1106. * @return the number of imports (zero for none), or -1 for failure
  1107. */
  1108. WASM_RUNTIME_API_EXTERN int32_t
  1109. wasm_runtime_get_import_count(const wasm_module_t module);
  1110. /**
  1111. * Get information about a specific WASM module import
  1112. *
  1113. * @param module the WASM module
  1114. * @param import_index the desired import index
  1115. * @param import_type the location to store information about the import
  1116. */
  1117. WASM_RUNTIME_API_EXTERN void
  1118. wasm_runtime_get_import_type(const wasm_module_t module, int32_t import_index,
  1119. wasm_import_type *import_type);
  1120. /**
  1121. * Get the number of export items for a WASM module
  1122. *
  1123. * @param module the WASM module
  1124. *
  1125. * @return the number of exports (zero for none), or -1 for failure
  1126. */
  1127. WASM_RUNTIME_API_EXTERN int32_t
  1128. wasm_runtime_get_export_count(const wasm_module_t module);
  1129. /**
  1130. * Get information about a specific WASM module export
  1131. *
  1132. * @param module the WASM module
  1133. * @param export_index the desired export index
  1134. * @param export_type the location to store information about the export
  1135. */
  1136. WASM_RUNTIME_API_EXTERN void
  1137. wasm_runtime_get_export_type(const wasm_module_t module, int32_t export_index,
  1138. wasm_export_type *export_type);
  1139. /**
  1140. * Register native functions with same module name
  1141. *
  1142. * Note: The array `native_symbols` should not be read-only because the
  1143. * library can modify it in-place.
  1144. *
  1145. * Note: After successful call of this function, the array `native_symbols`
  1146. * is owned by the library.
  1147. *
  1148. * @param module_name the module name of the native functions
  1149. * @param native_symbols specifies an array of NativeSymbol structures which
  1150. * contain the names, function pointers and signatures
  1151. * Note: WASM runtime will not allocate memory to clone the data, so
  1152. * user must ensure the array can be used forever
  1153. * Meanings of letters in function signature:
  1154. * 'i': the parameter is i32 type
  1155. * 'I': the parameter is i64 type
  1156. * 'f': the parameter is f32 type
  1157. * 'F': the parameter is f64 type
  1158. * 'r': the parameter is externref type, it should be a uintptr_t
  1159. * in host
  1160. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  1161. * auto check its boundary before calling the native function.
  1162. * If it is followed by '~', the checked length of the pointer
  1163. * is gotten from the following parameter, if not, the checked
  1164. * length of the pointer is 1.
  1165. * '~': the parameter is the pointer's length with i32 type, and must
  1166. * follow after '*'
  1167. * '$': the parameter is a string (i32 in WASM), and runtime will
  1168. * auto check its boundary before calling the native function
  1169. * @param n_native_symbols specifies the number of native symbols in the array
  1170. *
  1171. * @return true if success, false otherwise
  1172. */
  1173. WASM_RUNTIME_API_EXTERN bool
  1174. wasm_runtime_register_natives(const char *module_name,
  1175. NativeSymbol *native_symbols,
  1176. uint32_t n_native_symbols);
  1177. /**
  1178. * Register native functions with same module name, similar to
  1179. * wasm_runtime_register_natives, the difference is that runtime passes raw
  1180. * arguments to native API, which means that the native API should be defined as
  1181. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  1182. * and native API should extract arguments one by one from args array with macro
  1183. * native_raw_get_arg
  1184. * and write the return value back to args[0] with macro
  1185. * native_raw_return_type and native_raw_set_return
  1186. */
  1187. WASM_RUNTIME_API_EXTERN bool
  1188. wasm_runtime_register_natives_raw(const char *module_name,
  1189. NativeSymbol *native_symbols,
  1190. uint32_t n_native_symbols);
  1191. /**
  1192. * Undo wasm_runtime_register_natives or wasm_runtime_register_natives_raw
  1193. *
  1194. * @param module_name Should be the same as the corresponding
  1195. * wasm_runtime_register_natives.
  1196. * (Same in term of strcmp.)
  1197. *
  1198. * @param native_symbols Should be the same as the corresponding
  1199. * wasm_runtime_register_natives.
  1200. * (Same in term of pointer comparison.)
  1201. *
  1202. * @return true if success, false otherwise
  1203. */
  1204. WASM_RUNTIME_API_EXTERN bool
  1205. wasm_runtime_unregister_natives(const char *module_name,
  1206. NativeSymbol *native_symbols);
  1207. /**
  1208. * Get attachment of native function from execution environment
  1209. *
  1210. * @param exec_env the execution environment to retrieve
  1211. *
  1212. * @return the attachment of native function
  1213. */
  1214. WASM_RUNTIME_API_EXTERN void *
  1215. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  1216. /**
  1217. * Set user data to execution environment.
  1218. *
  1219. * @param exec_env the execution environment
  1220. * @param user_data the user data to be set
  1221. */
  1222. WASM_RUNTIME_API_EXTERN void
  1223. wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data);
  1224. /**
  1225. * Get the user data within execution environment.
  1226. *
  1227. * @param exec_env the execution environment
  1228. *
  1229. * @return the user data (NULL if not set yet)
  1230. */
  1231. WASM_RUNTIME_API_EXTERN void *
  1232. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  1233. /**
  1234. * Dump runtime memory consumption, including:
  1235. * Exec env memory consumption
  1236. * WASM module memory consumption
  1237. * WASM module instance memory consumption
  1238. * stack and app heap used info
  1239. *
  1240. * @param exec_env the execution environment
  1241. */
  1242. WASM_RUNTIME_API_EXTERN void
  1243. wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env);
  1244. /**
  1245. * Dump runtime performance profiler data of each function
  1246. *
  1247. * @param module_inst the WASM module instance to profile
  1248. */
  1249. WASM_RUNTIME_API_EXTERN void
  1250. wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
  1251. /**
  1252. * Return total wasm functions' execution time in ms
  1253. *
  1254. * @param module_inst the WASM module instance to profile
  1255. */
  1256. WASM_RUNTIME_API_EXTERN double
  1257. wasm_runtime_sum_wasm_exec_time(wasm_module_inst_t module_inst);
  1258. /**
  1259. * Return execution time in ms of a given wasm funciton with
  1260. * func_name. If the function is not found, return 0.
  1261. *
  1262. * @param module_inst the WASM module instance to profile
  1263. * @param func_name could be an export name or a name in the
  1264. * name section
  1265. */
  1266. WASM_RUNTIME_API_EXTERN double
  1267. wasm_runtime_get_wasm_func_exec_time(wasm_module_inst_t inst,
  1268. const char *func_name);
  1269. /* wasm thread callback function type */
  1270. typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *);
  1271. /* wasm thread type */
  1272. typedef uintptr_t wasm_thread_t;
  1273. /**
  1274. * Set the max thread num per cluster.
  1275. *
  1276. * @param num maximum thread num
  1277. */
  1278. WASM_RUNTIME_API_EXTERN void
  1279. wasm_runtime_set_max_thread_num(uint32_t num);
  1280. /**
  1281. * Spawn a new exec_env, the spawned exec_env
  1282. * can be used in other threads
  1283. *
  1284. * @param num the original exec_env
  1285. *
  1286. * @return the spawned exec_env if success, NULL otherwise
  1287. */
  1288. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  1289. wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
  1290. /**
  1291. * Destroy the spawned exec_env
  1292. *
  1293. * @param exec_env the spawned exec_env
  1294. */
  1295. WASM_RUNTIME_API_EXTERN void
  1296. wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
  1297. /**
  1298. * Spawn a thread from the given exec_env
  1299. *
  1300. * @param exec_env the original exec_env
  1301. * @param tid thread id to be returned to the caller
  1302. * @param callback the callback function provided by the user
  1303. * @param arg the arguments passed to the callback
  1304. *
  1305. * @return 0 if success, -1 otherwise
  1306. */
  1307. WASM_RUNTIME_API_EXTERN int32_t
  1308. wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
  1309. wasm_thread_callback_t callback, void *arg);
  1310. /**
  1311. * Wait a spawned thread to terminate
  1312. *
  1313. * @param tid thread id
  1314. * @param retval if not NULL, output the return value of the thread
  1315. *
  1316. * @return 0 if success, error number otherwise
  1317. */
  1318. WASM_RUNTIME_API_EXTERN int32_t
  1319. wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
  1320. /**
  1321. * Map external object to an internal externref index: if the index
  1322. * has been created, return it, otherwise create the index.
  1323. *
  1324. * @param module_inst the WASM module instance that the extern object
  1325. * belongs to
  1326. * @param extern_obj the external object to be mapped
  1327. * @param p_externref_idx return externref index of the external object
  1328. *
  1329. * @return true if success, false otherwise
  1330. */
  1331. WASM_RUNTIME_API_EXTERN bool
  1332. wasm_externref_obj2ref(wasm_module_inst_t module_inst, void *extern_obj,
  1333. uint32_t *p_externref_idx);
  1334. /**
  1335. * Delete external object registered by `wasm_externref_obj2ref`.
  1336. *
  1337. * @param module_inst the WASM module instance that the extern object
  1338. * belongs to
  1339. * @param extern_obj the external object to be deleted
  1340. *
  1341. * @return true if success, false otherwise
  1342. */
  1343. WASM_RUNTIME_API_EXTERN bool
  1344. wasm_externref_objdel(wasm_module_inst_t module_inst, void *extern_obj);
  1345. /**
  1346. * Set cleanup callback to release external object.
  1347. *
  1348. * @param module_inst the WASM module instance that the extern object
  1349. * belongs to
  1350. * @param extern_obj the external object to which to set the
  1351. * `extern_obj_cleanup` cleanup callback.
  1352. * @param extern_obj_cleanup a callback to release `extern_obj`
  1353. *
  1354. * @return true if success, false otherwise
  1355. */
  1356. WASM_RUNTIME_API_EXTERN bool
  1357. wasm_externref_set_cleanup(wasm_module_inst_t module_inst, void *extern_obj,
  1358. void (*extern_obj_cleanup)(void *));
  1359. /**
  1360. * Retrieve the external object from an internal externref index
  1361. *
  1362. * @param externref_idx the externref index to retrieve
  1363. * @param p_extern_obj return the mapped external object of
  1364. * the externref index
  1365. *
  1366. * @return true if success, false otherwise
  1367. */
  1368. WASM_RUNTIME_API_EXTERN bool
  1369. wasm_externref_ref2obj(uint32_t externref_idx, void **p_extern_obj);
  1370. /**
  1371. * Retain an extern object which is mapped to the internal externref
  1372. * so that the object won't be cleaned during extern object reclaim
  1373. * if it isn't used.
  1374. *
  1375. * @param externref_idx the externref index of an external object
  1376. * to retain
  1377. * @return true if success, false otherwise
  1378. */
  1379. WASM_RUNTIME_API_EXTERN bool
  1380. wasm_externref_retain(uint32_t externref_idx);
  1381. /**
  1382. * Dump the call stack to stdout
  1383. *
  1384. * @param exec_env the execution environment
  1385. */
  1386. WASM_RUNTIME_API_EXTERN void
  1387. wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
  1388. /**
  1389. * Get the size required to store the call stack contents, including
  1390. * the space for terminating null byte ('\0')
  1391. *
  1392. * @param exec_env the execution environment
  1393. *
  1394. * @return size required to store the contents, 0 means error
  1395. */
  1396. WASM_RUNTIME_API_EXTERN uint32_t
  1397. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env);
  1398. /**
  1399. * Dump the call stack to buffer.
  1400. *
  1401. * @note this function is not thread-safe, please only use this API
  1402. * when the exec_env is not executing
  1403. *
  1404. * @param exec_env the execution environment
  1405. * @param buf buffer to store the dumped content
  1406. * @param len length of the buffer
  1407. *
  1408. * @return bytes dumped to the buffer, including the terminating null
  1409. * byte ('\0'), 0 means error and data in buf may be invalid
  1410. */
  1411. WASM_RUNTIME_API_EXTERN uint32_t
  1412. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  1413. uint32_t len);
  1414. /**
  1415. * Get the size required to store the LLVM PGO profile data
  1416. *
  1417. * @param module_inst the WASM module instance
  1418. *
  1419. * @return size required to store the contents, 0 means error
  1420. */
  1421. WASM_RUNTIME_API_EXTERN uint32_t
  1422. wasm_runtime_get_pgo_prof_data_size(wasm_module_inst_t module_inst);
  1423. /**
  1424. * Dump the LLVM PGO profile data to buffer
  1425. *
  1426. * @param module_inst the WASM module instance
  1427. * @param buf buffer to store the dumped content
  1428. * @param len length of the buffer
  1429. *
  1430. * @return bytes dumped to the buffer, 0 means error and data in buf
  1431. * may be invalid
  1432. */
  1433. WASM_RUNTIME_API_EXTERN uint32_t
  1434. wasm_runtime_dump_pgo_prof_data_to_buf(wasm_module_inst_t module_inst,
  1435. char *buf, uint32_t len);
  1436. /**
  1437. * Get a custom section by name
  1438. *
  1439. * @param module_comm the module to find
  1440. * @param name name of the custom section
  1441. * @param len return the length of the content if found
  1442. *
  1443. * @return Custom section content (not including the name length
  1444. * and name string) if found, NULL otherwise
  1445. */
  1446. WASM_RUNTIME_API_EXTERN const uint8_t *
  1447. wasm_runtime_get_custom_section(wasm_module_t const module_comm,
  1448. const char *name, uint32_t *len);
  1449. /**
  1450. * Get WAMR semantic version
  1451. */
  1452. WASM_RUNTIME_API_EXTERN void
  1453. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch);
  1454. /**
  1455. * Check whether an import func `(import <module_name> <func_name> (func ...))`
  1456. * is linked or not with runtime registered natvie functions
  1457. */
  1458. WASM_RUNTIME_API_EXTERN bool
  1459. wasm_runtime_is_import_func_linked(const char *module_name,
  1460. const char *func_name);
  1461. /**
  1462. * Check whether an import global `(import <module_name> <global_name>
  1463. * (global ...))` is linked or not with runtime registered natvie globals
  1464. */
  1465. WASM_RUNTIME_API_EXTERN bool
  1466. wasm_runtime_is_import_global_linked(const char *module_name,
  1467. const char *global_name);
  1468. typedef enum {
  1469. INTERNAL_ERROR,
  1470. MAX_SIZE_REACHED,
  1471. } enlarge_memory_error_reason_t;
  1472. typedef void (*enlarge_memory_error_callback_t)(
  1473. uint32_t inc_page_count, uint64_t current_memory_size,
  1474. uint32_t memory_index, enlarge_memory_error_reason_t failure_reason,
  1475. wasm_module_inst_t instance, wasm_exec_env_t exec_env, void *user_data);
  1476. /**
  1477. * Setup callback invoked when memory.grow fails
  1478. */
  1479. WASM_RUNTIME_API_EXTERN void
  1480. wasm_runtime_set_enlarge_mem_error_callback(
  1481. const enlarge_memory_error_callback_t callback, void *user_data);
  1482. /*
  1483. * module instance context APIs
  1484. * wasm_runtime_create_context_key
  1485. * wasm_runtime_destroy_context_key
  1486. * wasm_runtime_set_context
  1487. * wasm_runtime_set_context_spread
  1488. * wasm_runtime_get_context
  1489. *
  1490. * This set of APIs is intended to be used by an embedder which provides
  1491. * extra sets of native functions, which need per module instance state
  1492. * and are maintained outside of the WAMR tree.
  1493. *
  1494. * It's modelled after the pthread specific API.
  1495. *
  1496. * wasm_runtime_set_context_spread is similar to
  1497. * wasm_runtime_set_context, except that
  1498. * wasm_runtime_set_context_spread applies the change
  1499. * to all threads in the cluster.
  1500. * It's an undefined behavior if multiple threads in a cluster call
  1501. * wasm_runtime_set_context_spread on the same key
  1502. * simultaneously. It's a caller's resposibility to perform necessary
  1503. * serialization if necessary. For example:
  1504. *
  1505. * if (wasm_runtime_get_context(inst, key) == NULL) {
  1506. * newctx = alloc_and_init(...);
  1507. * lock(some_lock);
  1508. * if (wasm_runtime_get_context(inst, key) == NULL) {
  1509. * // this thread won the race
  1510. * wasm_runtime_set_context_spread(inst, key, newctx);
  1511. * newctx = NULL;
  1512. * }
  1513. * unlock(some_lock);
  1514. * if (newctx != NULL) {
  1515. * // this thread lost the race, free it
  1516. * cleanup_and_free(newctx);
  1517. * }
  1518. * }
  1519. *
  1520. * Note: dynamic key create/destroy while instances are live is not
  1521. * implemented as of writing this.
  1522. * it's caller's resposibility to ensure destorying all module instances
  1523. * before calling wasm_runtime_create_context_key or
  1524. * wasm_runtime_destroy_context_key.
  1525. * otherwise, it's an undefined behavior.
  1526. *
  1527. * Note about threads:
  1528. * - When spawning a thread, the contexts (the pointers given to
  1529. * wasm_runtime_set_context) are copied from the parent
  1530. * instance.
  1531. * - The destructor is called only on the main instance.
  1532. */
  1533. WASM_RUNTIME_API_EXTERN void *
  1534. wasm_runtime_create_context_key(void (*dtor)(wasm_module_inst_t inst,
  1535. void *ctx));
  1536. WASM_RUNTIME_API_EXTERN void
  1537. wasm_runtime_destroy_context_key(void *key);
  1538. WASM_RUNTIME_API_EXTERN void
  1539. wasm_runtime_set_context(wasm_module_inst_t inst, void *key, void *ctx);
  1540. WASM_RUNTIME_API_EXTERN void
  1541. wasm_runtime_set_context_spread(wasm_module_inst_t inst, void *key, void *ctx);
  1542. WASM_RUNTIME_API_EXTERN void *
  1543. wasm_runtime_get_context(wasm_module_inst_t inst, void *key);
  1544. /*
  1545. * wasm_runtime_begin_blocking_op/wasm_runtime_end_blocking_op
  1546. *
  1547. * These APIs are intended to be used by the implementations of
  1548. * host functions. It wraps an operation which possibly blocks for long
  1549. * to prepare for async termination.
  1550. *
  1551. * For simplicity, we recommend to wrap only the very minimum piece of
  1552. * the code with this. Ideally, just a single system call.
  1553. *
  1554. * eg.
  1555. *
  1556. * if (!wasm_runtime_begin_blocking_op(exec_env)) {
  1557. * return EINTR;
  1558. * }
  1559. * ret = possibly_blocking_op();
  1560. * wasm_runtime_end_blocking_op(exec_env);
  1561. * return ret;
  1562. *
  1563. * If threading support (WASM_ENABLE_THREAD_MGR) is not enabled,
  1564. * these functions are no-op.
  1565. *
  1566. * If the underlying platform support (OS_ENABLE_WAKEUP_BLOCKING_OP) is
  1567. * not available, these functions are no-op. In that case, the runtime
  1568. * might not terminate a blocking thread in a timely manner.
  1569. *
  1570. * If the underlying platform support is available, it's used to wake up
  1571. * the thread for async termination. The expectation here is that a
  1572. * `os_wakeup_blocking_op` call makes the blocking operation
  1573. * (`possibly_blocking_op` in the above example) return in a timely manner.
  1574. *
  1575. * The actual wake up mechanism used by `os_wakeup_blocking_op` is
  1576. * platform-dependent. It might impose some platform-dependent restrictions
  1577. * on the implementation of the blocking opearation.
  1578. *
  1579. * For example, on POSIX-like platforms, a signal (by default SIGUSR1) is
  1580. * used. The signal delivery configurations (eg. signal handler, signal mask,
  1581. * etc) for the signal are set up by the runtime. You can change the signal
  1582. * to use for this purpose by calling os_set_signal_number_for_blocking_op
  1583. * before the runtime initialization.
  1584. */
  1585. WASM_RUNTIME_API_EXTERN bool
  1586. wasm_runtime_begin_blocking_op(wasm_exec_env_t exec_env);
  1587. WASM_RUNTIME_API_EXTERN void
  1588. wasm_runtime_end_blocking_op(wasm_exec_env_t exec_env);
  1589. WASM_RUNTIME_API_EXTERN bool
  1590. wasm_runtime_set_module_name(wasm_module_t module, const char *name,
  1591. char *error_buf, uint32_t error_buf_size);
  1592. /* return the most recently set module name or "" if never set before */
  1593. WASM_RUNTIME_API_EXTERN const char *
  1594. wasm_runtime_get_module_name(wasm_module_t module);
  1595. #ifdef __cplusplus
  1596. }
  1597. #endif
  1598. #endif /* end of _WASM_EXPORT_H */