wasm_export.h 64 KB

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