wasm_export.h 74 KB

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