wasm_export.h 64 KB

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