wasm_export.h 81 KB

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