wasm_export.h 75 KB

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