wasm_export.h 68 KB

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