wasm_export.h 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456
  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. /**
  674. * Instantiate a WASM module, with specified instantiation arguments
  675. *
  676. * Same as wasm_runtime_instantiate_ex, but this version takes
  677. * InstantiationArgs2, which can be extended without breaking the ABI.
  678. */
  679. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  680. wasm_runtime_instantiate_ex2(const wasm_module_t module,
  681. const struct InstantiationArgs2 *args,
  682. char *error_buf, uint32_t error_buf_size);
  683. /**
  684. * Set the running mode of a WASM module instance, override the
  685. * default running mode of the runtime. Note that it only makes sense when
  686. * the input is a wasm bytecode file: for the AOT file, runtime always runs
  687. * it with AOT engine, and this function always returns true.
  688. *
  689. * @param module_inst the WASM module instance to set running mode
  690. * @param running_mode the running mode to set
  691. *
  692. * @return true if success, false otherwise
  693. */
  694. WASM_RUNTIME_API_EXTERN bool
  695. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  696. RunningMode running_mode);
  697. /**
  698. * Get the running mode of a WASM module instance, if no running mode
  699. * is explicitly set the default running mode of runtime will
  700. * be used and returned. Note that it only makes sense when the input is a
  701. * wasm bytecode file: for the AOT file, this function always returns 0.
  702. *
  703. * @param module_inst the WASM module instance to query for running mode
  704. *
  705. * @return the running mode this module instance currently use
  706. */
  707. WASM_RUNTIME_API_EXTERN RunningMode
  708. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
  709. /**
  710. * Deinstantiate a WASM module instance, destroy the resources.
  711. *
  712. * @param module_inst the WASM module instance to destroy
  713. */
  714. WASM_RUNTIME_API_EXTERN void
  715. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  716. /**
  717. * Get WASM module from WASM module instance
  718. *
  719. * @param module_inst the WASM module instance to retrieve
  720. *
  721. * @return the WASM module
  722. */
  723. WASM_RUNTIME_API_EXTERN wasm_module_t
  724. wasm_runtime_get_module(wasm_module_inst_t module_inst);
  725. WASM_RUNTIME_API_EXTERN bool
  726. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  727. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  728. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  729. /**
  730. * Get WASI exit code.
  731. *
  732. * After a WASI command completed its execution, an embedder can
  733. * call this function to get its exit code. (that is, the value given
  734. * to proc_exit.)
  735. *
  736. * @param module_inst the module instance
  737. */
  738. WASM_RUNTIME_API_EXTERN uint32_t
  739. wasm_runtime_get_wasi_exit_code(wasm_module_inst_t module_inst);
  740. /**
  741. * Lookup an exported function in the WASM module instance.
  742. *
  743. * @param module_inst the module instance
  744. * @param name the name of the function
  745. *
  746. * @return the function instance found, NULL if not found
  747. */
  748. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  749. wasm_runtime_lookup_function(const wasm_module_inst_t module_inst,
  750. const char *name);
  751. /**
  752. * Get parameter count of the function instance
  753. *
  754. * @param func_inst the function instance
  755. * @param module_inst the module instance the function instance belongs to
  756. *
  757. * @return the parameter count of the function instance
  758. */
  759. WASM_RUNTIME_API_EXTERN uint32_t
  760. wasm_func_get_param_count(const wasm_function_inst_t func_inst,
  761. const wasm_module_inst_t module_inst);
  762. /**
  763. * Get result count of the function instance
  764. *
  765. * @param func_inst the function instance
  766. * @param module_inst the module instance the function instance belongs to
  767. *
  768. * @return the result count of the function instance
  769. */
  770. WASM_RUNTIME_API_EXTERN uint32_t
  771. wasm_func_get_result_count(const wasm_function_inst_t func_inst,
  772. const wasm_module_inst_t module_inst);
  773. /**
  774. * Get parameter types of the function instance
  775. *
  776. * @param func_inst the function instance
  777. * @param module_inst the module instance the function instance belongs to
  778. * @param param_types the parameter types returned
  779. */
  780. WASM_RUNTIME_API_EXTERN void
  781. wasm_func_get_param_types(const wasm_function_inst_t func_inst,
  782. const wasm_module_inst_t module_inst,
  783. wasm_valkind_t *param_types);
  784. /**
  785. * Get result types of the function instance
  786. *
  787. * @param func_inst the function instance
  788. * @param module_inst the module instance the function instance belongs to
  789. * @param result_types the result types returned
  790. */
  791. WASM_RUNTIME_API_EXTERN void
  792. wasm_func_get_result_types(const wasm_function_inst_t func_inst,
  793. const wasm_module_inst_t module_inst,
  794. wasm_valkind_t *result_types);
  795. /**
  796. * Create execution environment for a WASM module instance.
  797. *
  798. * @param module_inst the module instance
  799. * @param stack_size the stack size to execute a WASM function
  800. *
  801. * @return the execution environment, NULL if failed, e.g. invalid
  802. * stack size is passed
  803. */
  804. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  805. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  806. uint32_t stack_size);
  807. /**
  808. * Destroy the execution environment.
  809. *
  810. * @param exec_env the execution environment to destroy
  811. */
  812. WASM_RUNTIME_API_EXTERN void
  813. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  814. /**
  815. * @brief Copy callstack frames.
  816. *
  817. * Caution: This is not a thread-safe function. Ensure the exec_env
  818. * is suspended before calling it from another thread.
  819. *
  820. * Usage: In the callback to read frames fields use APIs
  821. * for wasm_frame_t from wasm_c_api.h
  822. *
  823. * Note: The function is async-signal-safe if called with verified arguments.
  824. * Meaning it's safe to call it from a signal handler even on a signal
  825. * interruption from another thread if next variables hold valid pointers
  826. * - exec_env
  827. * - exec_env->module_inst
  828. * - exec_env->module_inst->module
  829. *
  830. * @param exec_env the execution environment that containes frames
  831. * @param buffer the buffer of size equal length * sizeof(wasm_frame_t) to copy
  832. * frames to
  833. * @param length the number of frames to copy
  834. * @param skip_n the number of frames to skip from the top of the stack
  835. *
  836. * @return number of copied frames
  837. */
  838. WASM_RUNTIME_API_EXTERN uint32_t
  839. wasm_copy_callstack(const wasm_exec_env_t exec_env, WASMCApiFrame *buffer,
  840. const uint32_t length, const uint32_t skip_n,
  841. char *error_buf, uint32_t error_buf_size);
  842. /**
  843. * Get the singleton execution environment for the instance.
  844. *
  845. * Note: The singleton execution environment is the execution
  846. * environment used internally by the runtime for the API functions
  847. * like wasm_application_execute_main, which don't take explicit
  848. * execution environment. It's associated to the corresponding
  849. * module instance and managed by the runtime. The API user should
  850. * not destroy it with wasm_runtime_destroy_exec_env.
  851. *
  852. * @param module_inst the module instance
  853. *
  854. * @return exec_env the execution environment to destroy
  855. */
  856. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  857. wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
  858. /**
  859. * Start debug instance based on given execution environment.
  860. * Note:
  861. * The debug instance will be destroyed during destroying the
  862. * execution environment, developers don't need to destroy it
  863. * manually.
  864. * If the cluster of this execution environment has already
  865. * been bound to a debug instance, this function will return true
  866. * directly.
  867. * If developer spawns some exec_env by wasm_runtime_spawn_exec_env,
  868. * don't need to call this function for every spawned exec_env as
  869. * they are sharing the same cluster with the main exec_env.
  870. *
  871. * @param exec_env the execution environment to start debug instance
  872. * @param port the port for the debug server to listen on.
  873. * 0 means automatic assignment.
  874. * -1 means to use the global setting in RuntimeInitArgs.
  875. *
  876. * @return debug port if success, 0 otherwise.
  877. */
  878. WASM_RUNTIME_API_EXTERN uint32_t
  879. wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env,
  880. int32_t port);
  881. /**
  882. * Same as wasm_runtime_start_debug_instance_with_port(env, -1).
  883. */
  884. WASM_RUNTIME_API_EXTERN uint32_t
  885. wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
  886. /**
  887. * Initialize the thread environment.
  888. * Note:
  889. * If developer creates a child thread by himself to call the
  890. * the wasm function in that thread, he should call this API
  891. * firstly before calling the wasm function and then call
  892. * wasm_runtime_destroy_thread_env() after calling the wasm
  893. * function. If the thread is created from the runtime API,
  894. * it is unnecessary to call these two APIs.
  895. *
  896. * @return true if success, false otherwise
  897. */
  898. WASM_RUNTIME_API_EXTERN bool
  899. wasm_runtime_init_thread_env(void);
  900. /**
  901. * Destroy the thread environment
  902. */
  903. WASM_RUNTIME_API_EXTERN void
  904. wasm_runtime_destroy_thread_env(void);
  905. /**
  906. * Whether the thread environment is initialized
  907. */
  908. WASM_RUNTIME_API_EXTERN bool
  909. wasm_runtime_thread_env_inited(void);
  910. /**
  911. * Get WASM module instance from execution environment
  912. *
  913. * @param exec_env the execution environment to retrieve
  914. *
  915. * @return the WASM module instance
  916. */
  917. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  918. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  919. /**
  920. * Set WASM module instance of execution environment
  921. * Caution:
  922. * normally the module instance is bound with the execution
  923. * environment one by one, if multiple module instances want
  924. * to share to the same execution environment, developer should
  925. * be responsible for the backup and restore of module instance
  926. *
  927. * @param exec_env the execution environment
  928. * @param module_inst the WASM module instance to set
  929. */
  930. WASM_RUNTIME_API_EXTERN void
  931. wasm_runtime_set_module_inst(wasm_exec_env_t exec_env,
  932. const wasm_module_inst_t module_inst);
  933. /**
  934. * @brief Lookup a memory instance by name
  935. *
  936. * @param module_inst The module instance
  937. * @param name The name of the memory instance
  938. *
  939. * @return The memory instance if found, NULL otherwise
  940. */
  941. WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
  942. wasm_runtime_lookup_memory(const wasm_module_inst_t module_inst,
  943. const char *name);
  944. /**
  945. * @brief Get the default memory instance
  946. *
  947. * @param module_inst The module instance
  948. *
  949. * @return The memory instance if found, NULL otherwise
  950. */
  951. WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
  952. wasm_runtime_get_default_memory(const wasm_module_inst_t module_inst);
  953. /**
  954. * @brief Get a memory instance by index
  955. *
  956. * @param module_inst The module instance
  957. * @param index The index of the memory instance
  958. *
  959. * @return The memory instance if found, NULL otherwise
  960. */
  961. WASM_RUNTIME_API_EXTERN wasm_memory_inst_t
  962. wasm_runtime_get_memory(const wasm_module_inst_t module_inst, uint32_t index);
  963. /**
  964. * @brief Get the current number of pages for a memory instance
  965. *
  966. * @param memory_inst The memory instance
  967. *
  968. * @return The current number of pages
  969. */
  970. WASM_RUNTIME_API_EXTERN uint64_t
  971. wasm_memory_get_cur_page_count(const wasm_memory_inst_t memory_inst);
  972. /**
  973. * @brief Get the maximum number of pages for a memory instance
  974. *
  975. * @param memory_inst The memory instance
  976. *
  977. * @return The maximum number of pages
  978. */
  979. WASM_RUNTIME_API_EXTERN uint64_t
  980. wasm_memory_get_max_page_count(const wasm_memory_inst_t memory_inst);
  981. /**
  982. * @brief Get the number of bytes per page for a memory instance
  983. *
  984. * @param memory_inst The memory instance
  985. *
  986. * @return The number of bytes per page
  987. */
  988. WASM_RUNTIME_API_EXTERN uint64_t
  989. wasm_memory_get_bytes_per_page(const wasm_memory_inst_t memory_inst);
  990. /**
  991. * @brief Get the shared status for a memory instance
  992. *
  993. * @param memory_inst The memory instance
  994. *
  995. * @return True if shared, false otherwise
  996. */
  997. WASM_RUNTIME_API_EXTERN bool
  998. wasm_memory_get_shared(const wasm_memory_inst_t memory_inst);
  999. /**
  1000. * @brief Get the base address for a memory instance
  1001. *
  1002. * @param memory_inst The memory instance
  1003. *
  1004. * @return The base address on success, false otherwise
  1005. */
  1006. WASM_RUNTIME_API_EXTERN void *
  1007. wasm_memory_get_base_address(const wasm_memory_inst_t memory_inst);
  1008. /**
  1009. * @brief Enlarge a memory instance by a number of pages
  1010. *
  1011. * @param memory_inst The memory instance
  1012. * @param inc_page_count The number of pages to add
  1013. *
  1014. * @return True if successful, false otherwise
  1015. */
  1016. WASM_RUNTIME_API_EXTERN bool
  1017. wasm_memory_enlarge(wasm_memory_inst_t memory_inst, uint64_t inc_page_count);
  1018. /**
  1019. * Call the given WASM function of a WASM module instance with
  1020. * arguments (bytecode and AoT).
  1021. *
  1022. * @param exec_env the execution environment to call the function,
  1023. * which must be created from wasm_create_exec_env()
  1024. * @param function the function to call
  1025. * @param argc total cell number that the function parameters occupy,
  1026. * a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument
  1027. * occupies one cell, i64/f64 argument occupies two cells, note that
  1028. * it might be different from the parameter number of the function
  1029. * @param argv the arguments. If the function has return value,
  1030. * the first (or first two in case 64-bit return value) element of
  1031. * argv stores the return value of the called WASM function after this
  1032. * function returns.
  1033. *
  1034. * @return true if success, false otherwise and exception will be thrown,
  1035. * the caller can call wasm_runtime_get_exception to get the exception
  1036. * info.
  1037. */
  1038. WASM_RUNTIME_API_EXTERN bool
  1039. wasm_runtime_call_wasm(wasm_exec_env_t exec_env, wasm_function_inst_t function,
  1040. uint32_t argc, uint32_t argv[]);
  1041. /**
  1042. * Call the given WASM function of a WASM module instance with
  1043. * provided results space and arguments (bytecode and AoT).
  1044. *
  1045. * @param exec_env the execution environment to call the function,
  1046. * which must be created from wasm_create_exec_env()
  1047. * @param function the function to call
  1048. * @param num_results the number of results
  1049. * @param results the pre-alloced pointer to get the results
  1050. * @param num_args the number of arguments
  1051. * @param args the arguments
  1052. *
  1053. * @return true if success, false otherwise and exception will be thrown,
  1054. * the caller can call wasm_runtime_get_exception to get the exception
  1055. * info.
  1056. */
  1057. WASM_RUNTIME_API_EXTERN bool
  1058. wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
  1059. wasm_function_inst_t function, uint32_t num_results,
  1060. wasm_val_t results[], uint32_t num_args,
  1061. wasm_val_t *args);
  1062. /**
  1063. * Call the given WASM function of a WASM module instance with
  1064. * provided results space and variant arguments (bytecode and AoT).
  1065. *
  1066. * @param exec_env the execution environment to call the function,
  1067. * which must be created from wasm_create_exec_env()
  1068. * @param function the function to call
  1069. * @param num_results the number of results
  1070. * @param results the pre-alloced pointer to get the results
  1071. * @param num_args the number of arguments
  1072. * @param ... the variant arguments
  1073. *
  1074. * @return true if success, false otherwise and exception will be thrown,
  1075. * the caller can call wasm_runtime_get_exception to get the exception
  1076. * info.
  1077. */
  1078. WASM_RUNTIME_API_EXTERN bool
  1079. wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
  1080. wasm_function_inst_t function, uint32_t num_results,
  1081. wasm_val_t results[], uint32_t num_args, ...);
  1082. /**
  1083. * Call a function reference of a given WASM runtime instance with
  1084. * arguments.
  1085. *
  1086. * Note: this can be used to call a function which is not exported
  1087. * by the module explicitly. You might consider it as an abstraction
  1088. * violation.
  1089. *
  1090. * @param exec_env the execution environment to call the function
  1091. * which must be created from wasm_create_exec_env()
  1092. * @param element_index the function reference index, usually
  1093. * provided by the caller of a registered native function
  1094. * @param argc the number of arguments
  1095. * @param argv the arguments. If the function method has return value,
  1096. * the first (or first two in case 64-bit return value) element of
  1097. * argv stores the return value of the called WASM function after this
  1098. * function returns.
  1099. *
  1100. * @return true if success, false otherwise and exception will be thrown,
  1101. * the caller can call wasm_runtime_get_exception to get exception info.
  1102. */
  1103. WASM_RUNTIME_API_EXTERN bool
  1104. wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32_t element_index,
  1105. uint32_t argc, uint32_t argv[]);
  1106. /**
  1107. * Find the unique main function from a WASM module instance
  1108. * and execute that function.
  1109. *
  1110. * @param module_inst the WASM module instance
  1111. * @param argc the number of arguments
  1112. * @param argv the arguments array, if the main function has return value,
  1113. * *(int*)argv stores the return value of the called main function after
  1114. * this function returns.
  1115. *
  1116. * @return true if the main function is called, false otherwise and exception
  1117. * will be thrown, the caller can call wasm_runtime_get_exception to get
  1118. * the exception info.
  1119. */
  1120. WASM_RUNTIME_API_EXTERN bool
  1121. wasm_application_execute_main(wasm_module_inst_t module_inst, int32_t argc,
  1122. char *argv[]);
  1123. /**
  1124. * Find the specified function from a WASM module instance and execute
  1125. * that function.
  1126. *
  1127. * @param module_inst the WASM module instance
  1128. * @param name the name of the function to execute.
  1129. * to indicate the module name via: $module_name$function_name
  1130. * or just a function name: function_name
  1131. * @param argc the number of arguments
  1132. * @param argv the arguments array
  1133. *
  1134. * @return true if the specified function is called, false otherwise and
  1135. * exception will be thrown, the caller can call wasm_runtime_get_exception
  1136. * to get the exception info.
  1137. */
  1138. WASM_RUNTIME_API_EXTERN bool
  1139. wasm_application_execute_func(wasm_module_inst_t module_inst, const char *name,
  1140. int32_t argc, char *argv[]);
  1141. /**
  1142. * Get exception info of the WASM module instance.
  1143. *
  1144. * @param module_inst the WASM module instance
  1145. *
  1146. * @return the exception string
  1147. */
  1148. WASM_RUNTIME_API_EXTERN const char *
  1149. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  1150. /**
  1151. * Set exception info of the WASM module instance.
  1152. *
  1153. * @param module_inst the WASM module instance
  1154. *
  1155. * @param exception the exception string
  1156. */
  1157. WASM_RUNTIME_API_EXTERN void
  1158. wasm_runtime_set_exception(wasm_module_inst_t module_inst,
  1159. const char *exception);
  1160. /**
  1161. * Clear exception info of the WASM module instance.
  1162. *
  1163. * @param module_inst the WASM module instance
  1164. */
  1165. WASM_RUNTIME_API_EXTERN void
  1166. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  1167. /**
  1168. * Terminate the WASM module instance.
  1169. *
  1170. * This function causes the module instance fail as if it raised a trap.
  1171. *
  1172. * This is intended to be used in situations like:
  1173. *
  1174. * - A thread is executing the WASM module instance
  1175. * (eg. it's in the middle of `wasm_application_execute_main`)
  1176. *
  1177. * - Another thread has a copy of `wasm_module_inst_t` of
  1178. * the module instance and wants to terminate it asynchronously.
  1179. *
  1180. * @param module_inst the WASM module instance
  1181. */
  1182. WASM_RUNTIME_API_EXTERN void
  1183. wasm_runtime_terminate(wasm_module_inst_t module_inst);
  1184. /**
  1185. * Set custom data to WASM module instance.
  1186. * Note:
  1187. * If WAMR_BUILD_LIB_PTHREAD is enabled, this API
  1188. * will spread the custom data to all threads
  1189. *
  1190. * @param module_inst the WASM module instance
  1191. * @param custom_data the custom data to be set
  1192. */
  1193. WASM_RUNTIME_API_EXTERN void
  1194. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst, void *custom_data);
  1195. /**
  1196. * Get the custom data within a WASM module instance.
  1197. *
  1198. * @param module_inst the WASM module instance
  1199. *
  1200. * @return the custom data (NULL if not set yet)
  1201. */
  1202. WASM_RUNTIME_API_EXTERN void *
  1203. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  1204. /**
  1205. * Set the memory bounds checks flag of a WASM module instance.
  1206. *
  1207. * @param module_inst the WASM module instance
  1208. * @param enable the flag to enable/disable the memory bounds checks
  1209. */
  1210. WASM_RUNTIME_API_EXTERN void
  1211. wasm_runtime_set_bounds_checks(wasm_module_inst_t module_inst, bool enable);
  1212. /**
  1213. * Check if the memory bounds checks flag is enabled for a WASM module instance.
  1214. *
  1215. * @param module_inst the WASM module instance
  1216. * @return true if the memory bounds checks flag is enabled, false otherwise
  1217. */
  1218. WASM_RUNTIME_API_EXTERN bool
  1219. wasm_runtime_is_bounds_checks_enabled(wasm_module_inst_t module_inst);
  1220. /**
  1221. * Allocate memory from the heap of WASM module instance
  1222. *
  1223. * Note: wasm_runtime_module_malloc can call heap functions inside
  1224. * the module instance and thus cause a memory growth.
  1225. * This API needs to be used very carefully when you have a native
  1226. * pointers to the module instance memory obtained with
  1227. * wasm_runtime_addr_app_to_native or similar APIs.
  1228. *
  1229. * @param module_inst the WASM module instance which contains heap
  1230. * @param size the size bytes to allocate
  1231. * @param p_native_addr return native address of the allocated memory
  1232. * if it is not NULL, and return NULL if memory malloc failed
  1233. *
  1234. * @return the allocated memory address, which is a relative offset to the
  1235. * base address of the module instance's memory space. Note that
  1236. * it is not an absolute address.
  1237. * Return non-zero if success, zero if failed.
  1238. */
  1239. WASM_RUNTIME_API_EXTERN uint64_t
  1240. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint64_t size,
  1241. void **p_native_addr);
  1242. /**
  1243. * Free memory to the heap of WASM module instance
  1244. *
  1245. * @param module_inst the WASM module instance which contains heap
  1246. * @param ptr the pointer to free
  1247. */
  1248. WASM_RUNTIME_API_EXTERN void
  1249. wasm_runtime_module_free(wasm_module_inst_t module_inst, uint64_t ptr);
  1250. /**
  1251. * Allocate memory from the heap of WASM module instance and initialize
  1252. * the memory with src
  1253. *
  1254. * @param module_inst the WASM module instance which contains heap
  1255. * @param src the source data to copy
  1256. * @param size the size of the source data
  1257. *
  1258. * @return the allocated memory address, which is a relative offset to the
  1259. * base address of the module instance's memory space. Note that
  1260. * it is not an absolute address.
  1261. * Return non-zero if success, zero if failed.
  1262. */
  1263. WASM_RUNTIME_API_EXTERN uint64_t
  1264. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst, const char *src,
  1265. uint64_t size);
  1266. /**
  1267. * Validate the app address, check whether it belongs to WASM module
  1268. * instance's address space, or in its heap space or memory space.
  1269. *
  1270. * @param module_inst the WASM module instance
  1271. * @param app_offset the app address to validate, which is a relative address
  1272. * @param size the size bytes of the app address
  1273. *
  1274. * @return true if success, false otherwise. If failed, an exception will
  1275. * be thrown.
  1276. */
  1277. WASM_RUNTIME_API_EXTERN bool
  1278. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  1279. uint64_t app_offset, uint64_t size);
  1280. /**
  1281. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  1282. * is not provided. This function validates the app string address, check
  1283. * whether it belongs to WASM module instance's address space, or in its heap
  1284. * space or memory space. Moreover, it checks whether it is the offset of a
  1285. * string that is end with '\0'.
  1286. *
  1287. * Note: The validation result, especially the NUL termination check,
  1288. * is not reliable for a module instance with multiple threads because
  1289. * other threads can modify the heap behind us.
  1290. *
  1291. * @param module_inst the WASM module instance
  1292. * @param app_str_offset the app address of the string to validate, which is a
  1293. * relative address
  1294. *
  1295. * @return true if success, false otherwise. If failed, an exception will
  1296. * be thrown.
  1297. */
  1298. WASM_RUNTIME_API_EXTERN bool
  1299. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  1300. uint64_t app_str_offset);
  1301. /**
  1302. * Validate the native address, check whether it belongs to WASM module
  1303. * instance's address space, or in its heap space or memory space.
  1304. *
  1305. * @param module_inst the WASM module instance
  1306. * @param native_ptr the native address to validate, which is an absolute
  1307. * address
  1308. * @param size the size bytes of the app address
  1309. *
  1310. * @return true if success, false otherwise. If failed, an exception will
  1311. * be thrown.
  1312. */
  1313. WASM_RUNTIME_API_EXTERN bool
  1314. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  1315. void *native_ptr, uint64_t size);
  1316. /**
  1317. * Convert app address (relative address) to native address (absolute address)
  1318. *
  1319. * Note that native addresses to module instance memory can be invalidated
  1320. * on a memory growth. (Except shared memory, whose native addresses are
  1321. * stable.)
  1322. *
  1323. * @param module_inst the WASM module instance
  1324. * @param app_offset the app address
  1325. *
  1326. * @return the native address converted
  1327. */
  1328. WASM_RUNTIME_API_EXTERN void *
  1329. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  1330. uint64_t app_offset);
  1331. /**
  1332. * Convert native address (absolute address) to app address (relative address)
  1333. *
  1334. * @param module_inst the WASM module instance
  1335. * @param native_ptr the native address
  1336. *
  1337. * @return the app address converted
  1338. */
  1339. WASM_RUNTIME_API_EXTERN uint64_t
  1340. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  1341. void *native_ptr);
  1342. /**
  1343. * Get the app address range (relative address) that a app address belongs to
  1344. *
  1345. * @param module_inst the WASM module instance
  1346. * @param app_offset the app address to retrieve
  1347. * @param p_app_start_offset buffer to output the app start offset if not NULL
  1348. * @param p_app_end_offset buffer to output the app end offset if not NULL
  1349. *
  1350. * @return true if success, false otherwise.
  1351. */
  1352. WASM_RUNTIME_API_EXTERN bool
  1353. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  1354. uint64_t app_offset,
  1355. uint64_t *p_app_start_offset,
  1356. uint64_t *p_app_end_offset);
  1357. /**
  1358. * Get the native address range (absolute address) that a native address
  1359. * belongs to
  1360. *
  1361. * @param module_inst the WASM module instance
  1362. * @param native_ptr the native address to retrieve
  1363. * @param p_native_start_addr buffer to output the native start address
  1364. * if not NULL
  1365. * @param p_native_end_addr buffer to output the native end address
  1366. * if not NULL
  1367. *
  1368. * @return true if success, false otherwise.
  1369. */
  1370. WASM_RUNTIME_API_EXTERN bool
  1371. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  1372. uint8_t *native_ptr,
  1373. uint8_t **p_native_start_addr,
  1374. uint8_t **p_native_end_addr);
  1375. /**
  1376. * Get the number of import items for a WASM module
  1377. *
  1378. * Typical usage scenario:
  1379. * Combine this function with wasm_runtime_get_import_count() to traverse
  1380. * all import items in a module. Use import_type.kind to filter and identify
  1381. * different types of import items.
  1382. *
  1383. * Example usage (as wasm_runtime_for_each_import_func() in
  1384. * samples/import-func-callback)
  1385. *
  1386. * @param module the WASM module
  1387. *
  1388. * @return the number of imports (zero for none), or -1 for failure
  1389. */
  1390. WASM_RUNTIME_API_EXTERN int32_t
  1391. wasm_runtime_get_import_count(const wasm_module_t module);
  1392. /**
  1393. * Get information about a specific WASM module import
  1394. *
  1395. * Typical usage scenario:
  1396. * Combine this function with wasm_runtime_get_import_count() to traverse
  1397. * all import items in a module. Use import_type.kind to filter and identify
  1398. * different types of import items.
  1399. *
  1400. * Example usage (as wasm_runtime_for_each_import_func() in
  1401. * samples/import-func-callback)
  1402. *
  1403. * @param module the WASM module
  1404. * @param import_index the desired import index
  1405. * @param import_type the location to store information about the import
  1406. */
  1407. WASM_RUNTIME_API_EXTERN void
  1408. wasm_runtime_get_import_type(const wasm_module_t module, int32_t import_index,
  1409. wasm_import_t *import_type);
  1410. /**
  1411. * Get the number of export items for a WASM module
  1412. *
  1413. * @param module the WASM module
  1414. *
  1415. * @return the number of exports (zero for none), or -1 for failure
  1416. */
  1417. WASM_RUNTIME_API_EXTERN int32_t
  1418. wasm_runtime_get_export_count(const wasm_module_t module);
  1419. /**
  1420. * Get information about a specific WASM module export
  1421. *
  1422. * @param module the WASM module
  1423. * @param export_index the desired export index
  1424. * @param export_type the location to store information about the export
  1425. */
  1426. WASM_RUNTIME_API_EXTERN void
  1427. wasm_runtime_get_export_type(const wasm_module_t module, int32_t export_index,
  1428. wasm_export_t *export_type);
  1429. /**
  1430. * Get the number of parameters for a function type
  1431. *
  1432. * @param func_type the function type
  1433. *
  1434. * @return the number of parameters for the function type
  1435. */
  1436. WASM_RUNTIME_API_EXTERN uint32_t
  1437. wasm_func_type_get_param_count(const wasm_func_type_t func_type);
  1438. /**
  1439. * Get the kind of a parameter for a function type
  1440. *
  1441. * @param func_type the function type
  1442. * @param param_index the index of the parameter to get
  1443. *
  1444. * @return the kind of the parameter if successful, -1 otherwise
  1445. */
  1446. WASM_RUNTIME_API_EXTERN wasm_valkind_t
  1447. wasm_func_type_get_param_valkind(const wasm_func_type_t func_type,
  1448. uint32_t param_index);
  1449. /**
  1450. * Get the number of results for a function type
  1451. *
  1452. * @param func_type the function type
  1453. *
  1454. * @return the number of results for the function type
  1455. */
  1456. WASM_RUNTIME_API_EXTERN uint32_t
  1457. wasm_func_type_get_result_count(const wasm_func_type_t func_type);
  1458. /**
  1459. * Get the kind of a result for a function type
  1460. *
  1461. * @param func_type the function type
  1462. * @param result_index the index of the result to get
  1463. *
  1464. * @return the kind of the result if successful, -1 otherwise
  1465. */
  1466. WASM_RUNTIME_API_EXTERN wasm_valkind_t
  1467. wasm_func_type_get_result_valkind(const wasm_func_type_t func_type,
  1468. uint32_t result_index);
  1469. /**
  1470. * Get the kind for a global type
  1471. *
  1472. * @param global_type the global type
  1473. *
  1474. * @return the kind of the global
  1475. */
  1476. WASM_RUNTIME_API_EXTERN wasm_valkind_t
  1477. wasm_global_type_get_valkind(const wasm_global_type_t global_type);
  1478. /**
  1479. * Get the mutability for a global type
  1480. *
  1481. * @param global_type the global type
  1482. *
  1483. * @return true if mutable, false otherwise
  1484. */
  1485. WASM_RUNTIME_API_EXTERN bool
  1486. wasm_global_type_get_mutable(const wasm_global_type_t global_type);
  1487. /**
  1488. * Get the shared setting for a memory type
  1489. *
  1490. * @param memory_type the memory type
  1491. *
  1492. * @return true if shared, false otherwise
  1493. */
  1494. WASM_RUNTIME_API_EXTERN bool
  1495. wasm_memory_type_get_shared(const wasm_memory_type_t memory_type);
  1496. /**
  1497. * Get the initial page count for a memory type
  1498. *
  1499. * @param memory_type the memory type
  1500. *
  1501. * @return the initial memory page count
  1502. */
  1503. WASM_RUNTIME_API_EXTERN uint32_t
  1504. wasm_memory_type_get_init_page_count(const wasm_memory_type_t memory_type);
  1505. /**
  1506. * Get the maximum page count for a memory type
  1507. *
  1508. * @param memory_type the memory type
  1509. *
  1510. * @return the maximum memory page count
  1511. */
  1512. WASM_RUNTIME_API_EXTERN uint32_t
  1513. wasm_memory_type_get_max_page_count(const wasm_memory_type_t memory_type);
  1514. /**
  1515. * Get the element kind for a table type
  1516. *
  1517. * @param table_type the table type
  1518. *
  1519. * @return the element kind
  1520. */
  1521. WASM_RUNTIME_API_EXTERN wasm_valkind_t
  1522. wasm_table_type_get_elem_kind(const wasm_table_type_t table_type);
  1523. /**
  1524. * Get the sharing setting for a table type
  1525. *
  1526. * @param table_type the table type
  1527. *
  1528. * @return true if shared, false otherwise
  1529. */
  1530. WASM_RUNTIME_API_EXTERN bool
  1531. wasm_table_type_get_shared(const wasm_table_type_t table_type);
  1532. /**
  1533. * Get the initial size for a table type
  1534. *
  1535. * @param table_type the table type
  1536. *
  1537. * @return the initial table size
  1538. */
  1539. WASM_RUNTIME_API_EXTERN uint32_t
  1540. wasm_table_type_get_init_size(const wasm_table_type_t table_type);
  1541. /**
  1542. * Get the maximum size for a table type
  1543. *
  1544. * @param table_type the table type
  1545. *
  1546. * @return the maximum table size
  1547. */
  1548. WASM_RUNTIME_API_EXTERN uint32_t
  1549. wasm_table_type_get_max_size(const wasm_table_type_t table_type);
  1550. /**
  1551. * Register native functions with same module name
  1552. *
  1553. * Note: The array `native_symbols` should not be read-only because the
  1554. * library can modify it in-place.
  1555. *
  1556. * Note: After successful call of this function, the array `native_symbols`
  1557. * is owned by the library.
  1558. *
  1559. * @param module_name the module name of the native functions
  1560. * @param native_symbols specifies an array of NativeSymbol structures which
  1561. * contain the names, function pointers and signatures
  1562. * Note: WASM runtime will not allocate memory to clone the data, so
  1563. * user must ensure the array can be used forever
  1564. * Meanings of letters in function signature:
  1565. * 'i': the parameter is i32 type
  1566. * 'I': the parameter is i64 type
  1567. * 'f': the parameter is f32 type
  1568. * 'F': the parameter is f64 type
  1569. * 'r': the parameter is externref type, it should be a uintptr_t
  1570. * in host
  1571. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  1572. * auto check its boundary before calling the native function.
  1573. * If it is followed by '~', the checked length of the pointer
  1574. * is gotten from the following parameter, if not, the checked
  1575. * length of the pointer is 1.
  1576. * '~': the parameter is the pointer's length with i32 type, and must
  1577. * follow after '*'
  1578. * '$': the parameter is a string (i32 in WASM), and runtime will
  1579. * auto check its boundary before calling the native function
  1580. * @param n_native_symbols specifies the number of native symbols in the array
  1581. *
  1582. * @return true if success, false otherwise
  1583. */
  1584. WASM_RUNTIME_API_EXTERN bool
  1585. wasm_runtime_register_natives(const char *module_name,
  1586. NativeSymbol *native_symbols,
  1587. uint32_t n_native_symbols);
  1588. /**
  1589. * Register native functions with same module name, similar to
  1590. * wasm_runtime_register_natives, the difference is that runtime passes raw
  1591. * arguments to native API, which means that the native API should be defined as
  1592. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  1593. * and native API should extract arguments one by one from args array with macro
  1594. * native_raw_get_arg
  1595. * and write the return value back to args[0] with macro
  1596. * native_raw_return_type and native_raw_set_return
  1597. */
  1598. WASM_RUNTIME_API_EXTERN bool
  1599. wasm_runtime_register_natives_raw(const char *module_name,
  1600. NativeSymbol *native_symbols,
  1601. uint32_t n_native_symbols);
  1602. /**
  1603. * Undo wasm_runtime_register_natives or wasm_runtime_register_natives_raw
  1604. *
  1605. * @param module_name Should be the same as the corresponding
  1606. * wasm_runtime_register_natives.
  1607. * (Same in term of strcmp.)
  1608. *
  1609. * @param native_symbols Should be the same as the corresponding
  1610. * wasm_runtime_register_natives.
  1611. * (Same in term of pointer comparison.)
  1612. *
  1613. * @return true if success, false otherwise
  1614. */
  1615. WASM_RUNTIME_API_EXTERN bool
  1616. wasm_runtime_unregister_natives(const char *module_name,
  1617. NativeSymbol *native_symbols);
  1618. /**
  1619. * Get an export global instance
  1620. *
  1621. * @param module_inst the module instance
  1622. * @param name the export global name
  1623. * @param global_inst location to store the global instance
  1624. *
  1625. * @return true if success, false otherwise
  1626. *
  1627. */
  1628. WASM_RUNTIME_API_EXTERN bool
  1629. wasm_runtime_get_export_global_inst(const wasm_module_inst_t module_inst,
  1630. const char *name,
  1631. wasm_global_inst_t *global_inst);
  1632. /**
  1633. * Get an export table instance
  1634. *
  1635. * @param module_inst the module instance
  1636. * @param name the export table name
  1637. * @param table_inst location to store the table instance
  1638. *
  1639. * @return true if success, false otherwise
  1640. *
  1641. */
  1642. WASM_RUNTIME_API_EXTERN bool
  1643. wasm_runtime_get_export_table_inst(const wasm_module_inst_t module_inst,
  1644. const char *name,
  1645. wasm_table_inst_t *table_inst);
  1646. /**
  1647. * Get a function instance from a table.
  1648. *
  1649. * @param module_inst the module instance
  1650. * @param table_inst the table instance
  1651. * @param idx the index in the table
  1652. *
  1653. * @return the function instance if successful, NULL otherwise
  1654. */
  1655. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  1656. wasm_table_get_func_inst(const wasm_module_inst_t module_inst,
  1657. const wasm_table_inst_t *table_inst, uint32_t idx);
  1658. /**
  1659. * Get attachment of native function from execution environment
  1660. *
  1661. * @param exec_env the execution environment to retrieve
  1662. *
  1663. * @return the attachment of native function
  1664. */
  1665. WASM_RUNTIME_API_EXTERN void *
  1666. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  1667. /**
  1668. * Set user data to execution environment.
  1669. *
  1670. * @param exec_env the execution environment
  1671. * @param user_data the user data to be set
  1672. */
  1673. WASM_RUNTIME_API_EXTERN void
  1674. wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data);
  1675. /**
  1676. * Get the user data within execution environment.
  1677. *
  1678. * @param exec_env the execution environment
  1679. *
  1680. * @return the user data (NULL if not set yet)
  1681. */
  1682. WASM_RUNTIME_API_EXTERN void *
  1683. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  1684. /**
  1685. * Set native stack boundary to execution environment, if it is set,
  1686. * it will be used instead of getting the boundary with the platform
  1687. * layer API when calling wasm functions. This is useful for some
  1688. * fiber cases.
  1689. *
  1690. * Note: unlike setting the boundary by runtime, this API doesn't add
  1691. * the WASM_STACK_GUARD_SIZE(see comments in core/config.h) to the
  1692. * exec_env's native_stack_boundary to reserve bytes to the native
  1693. * thread stack boundary, which is used to throw native stack overflow
  1694. * exception if the guard boundary is reached. Developer should ensure
  1695. * that enough guard bytes are kept.
  1696. *
  1697. * @param exec_env the execution environment
  1698. * @param native_stack_boundary the user data to be set
  1699. */
  1700. WASM_RUNTIME_API_EXTERN void
  1701. wasm_runtime_set_native_stack_boundary(wasm_exec_env_t exec_env,
  1702. uint8_t *native_stack_boundary);
  1703. /**
  1704. * Set the instruction count limit to the execution environment.
  1705. * By default the instruction count limit is -1, which means no limit.
  1706. * However, if the instruction count limit is set to a positive value,
  1707. * the execution will be terminated when the instruction count reaches
  1708. * the limit.
  1709. *
  1710. * @param exec_env the execution environment
  1711. * @param instruction_count the instruction count limit
  1712. */
  1713. WASM_RUNTIME_API_EXTERN void
  1714. wasm_runtime_set_instruction_count_limit(wasm_exec_env_t exec_env,
  1715. int instruction_count);
  1716. /**
  1717. * Dump runtime memory consumption, including:
  1718. * Exec env memory consumption
  1719. * WASM module memory consumption
  1720. * WASM module instance memory consumption
  1721. * stack and app heap used info
  1722. *
  1723. * @param exec_env the execution environment
  1724. */
  1725. WASM_RUNTIME_API_EXTERN void
  1726. wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env);
  1727. /**
  1728. * Dump runtime performance profiler data of each function
  1729. *
  1730. * @param module_inst the WASM module instance to profile
  1731. */
  1732. WASM_RUNTIME_API_EXTERN void
  1733. wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
  1734. /**
  1735. * Return total wasm functions' execution time in ms
  1736. *
  1737. * @param module_inst the WASM module instance to profile
  1738. */
  1739. WASM_RUNTIME_API_EXTERN double
  1740. wasm_runtime_sum_wasm_exec_time(wasm_module_inst_t module_inst);
  1741. /**
  1742. * Return execution time in ms of a given wasm function with
  1743. * func_name. If the function is not found, return 0.
  1744. *
  1745. * @param module_inst the WASM module instance to profile
  1746. * @param func_name could be an export name or a name in the
  1747. * name section
  1748. */
  1749. WASM_RUNTIME_API_EXTERN double
  1750. wasm_runtime_get_wasm_func_exec_time(wasm_module_inst_t inst,
  1751. const char *func_name);
  1752. /* wasm thread callback function type */
  1753. typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *);
  1754. /* wasm thread type */
  1755. typedef uintptr_t wasm_thread_t;
  1756. /**
  1757. * Set the max thread num per cluster.
  1758. *
  1759. * @param num maximum thread num
  1760. */
  1761. WASM_RUNTIME_API_EXTERN void
  1762. wasm_runtime_set_max_thread_num(uint32_t num);
  1763. /**
  1764. * Spawn a new exec_env, the spawned exec_env
  1765. * can be used in other threads
  1766. *
  1767. * @param num the original exec_env
  1768. *
  1769. * @return the spawned exec_env if success, NULL otherwise
  1770. */
  1771. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  1772. wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
  1773. /**
  1774. * Destroy the spawned exec_env
  1775. *
  1776. * @param exec_env the spawned exec_env
  1777. */
  1778. WASM_RUNTIME_API_EXTERN void
  1779. wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
  1780. /**
  1781. * Spawn a thread from the given exec_env
  1782. *
  1783. * @param exec_env the original exec_env
  1784. * @param tid thread id to be returned to the caller
  1785. * @param callback the callback function provided by the user
  1786. * @param arg the arguments passed to the callback
  1787. *
  1788. * @return 0 if success, -1 otherwise
  1789. */
  1790. WASM_RUNTIME_API_EXTERN int32_t
  1791. wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
  1792. wasm_thread_callback_t callback, void *arg);
  1793. /**
  1794. * Wait a spawned thread to terminate
  1795. *
  1796. * @param tid thread id
  1797. * @param retval if not NULL, output the return value of the thread
  1798. *
  1799. * @return 0 if success, error number otherwise
  1800. */
  1801. WASM_RUNTIME_API_EXTERN int32_t
  1802. wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
  1803. /**
  1804. * Map external object to an internal externref index: if the index
  1805. * has been created, return it, otherwise create the index.
  1806. *
  1807. * @param module_inst the WASM module instance that the extern object
  1808. * belongs to
  1809. * @param extern_obj the external object to be mapped
  1810. * @param p_externref_idx return externref index of the external object
  1811. *
  1812. * @return true if success, false otherwise
  1813. */
  1814. WASM_RUNTIME_API_EXTERN bool
  1815. wasm_externref_obj2ref(wasm_module_inst_t module_inst, void *extern_obj,
  1816. uint32_t *p_externref_idx);
  1817. /**
  1818. * Delete external object registered by `wasm_externref_obj2ref`.
  1819. *
  1820. * @param module_inst the WASM module instance that the extern object
  1821. * belongs to
  1822. * @param extern_obj the external object to be deleted
  1823. *
  1824. * @return true if success, false otherwise
  1825. */
  1826. WASM_RUNTIME_API_EXTERN bool
  1827. wasm_externref_objdel(wasm_module_inst_t module_inst, void *extern_obj);
  1828. /**
  1829. * Set cleanup callback to release external object.
  1830. *
  1831. * @param module_inst the WASM module instance that the extern object
  1832. * belongs to
  1833. * @param extern_obj the external object to which to set the
  1834. * `extern_obj_cleanup` cleanup callback.
  1835. * @param extern_obj_cleanup a callback to release `extern_obj`
  1836. *
  1837. * @return true if success, false otherwise
  1838. */
  1839. WASM_RUNTIME_API_EXTERN bool
  1840. wasm_externref_set_cleanup(wasm_module_inst_t module_inst, void *extern_obj,
  1841. void (*extern_obj_cleanup)(void *));
  1842. /**
  1843. * Retrieve the external object from an internal externref index
  1844. *
  1845. * @param externref_idx the externref index to retrieve
  1846. * @param p_extern_obj return the mapped external object of
  1847. * the externref index
  1848. *
  1849. * @return true if success, false otherwise
  1850. */
  1851. WASM_RUNTIME_API_EXTERN bool
  1852. wasm_externref_ref2obj(uint32_t externref_idx, void **p_extern_obj);
  1853. /**
  1854. * Retain an extern object which is mapped to the internal externref
  1855. * so that the object won't be cleaned during extern object reclaim
  1856. * if it isn't used.
  1857. *
  1858. * @param externref_idx the externref index of an external object
  1859. * to retain
  1860. * @return true if success, false otherwise
  1861. */
  1862. WASM_RUNTIME_API_EXTERN bool
  1863. wasm_externref_retain(uint32_t externref_idx);
  1864. /**
  1865. * Dump the call stack to stdout
  1866. *
  1867. * @param exec_env the execution environment
  1868. */
  1869. WASM_RUNTIME_API_EXTERN void
  1870. wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
  1871. /**
  1872. * Get the size required to store the call stack contents, including
  1873. * the space for terminating null byte ('\0')
  1874. *
  1875. * @param exec_env the execution environment
  1876. *
  1877. * @return size required to store the contents, 0 means error
  1878. */
  1879. WASM_RUNTIME_API_EXTERN uint32_t
  1880. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env);
  1881. /**
  1882. * Dump the call stack to buffer.
  1883. *
  1884. * @note this function is not thread-safe, please only use this API
  1885. * when the exec_env is not executing
  1886. *
  1887. * @param exec_env the execution environment
  1888. * @param buf buffer to store the dumped content
  1889. * @param len length of the buffer
  1890. *
  1891. * @return bytes dumped to the buffer, including the terminating null
  1892. * byte ('\0'), 0 means error and data in buf may be invalid
  1893. */
  1894. WASM_RUNTIME_API_EXTERN uint32_t
  1895. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  1896. uint32_t len);
  1897. /**
  1898. * Get the size required to store the LLVM PGO profile data
  1899. *
  1900. * @param module_inst the WASM module instance
  1901. *
  1902. * @return size required to store the contents, 0 means error
  1903. */
  1904. WASM_RUNTIME_API_EXTERN uint32_t
  1905. wasm_runtime_get_pgo_prof_data_size(wasm_module_inst_t module_inst);
  1906. /**
  1907. * Dump the LLVM PGO profile data to buffer
  1908. *
  1909. * @param module_inst the WASM module instance
  1910. * @param buf buffer to store the dumped content
  1911. * @param len length of the buffer
  1912. *
  1913. * @return bytes dumped to the buffer, 0 means error and data in buf
  1914. * may be invalid
  1915. */
  1916. WASM_RUNTIME_API_EXTERN uint32_t
  1917. wasm_runtime_dump_pgo_prof_data_to_buf(wasm_module_inst_t module_inst,
  1918. char *buf, uint32_t len);
  1919. /**
  1920. * Get a custom section by name
  1921. *
  1922. * @param module_comm the module to find
  1923. * @param name name of the custom section
  1924. * @param len return the length of the content if found
  1925. *
  1926. * @return Custom section content (not including the name length
  1927. * and name string) if found, NULL otherwise
  1928. */
  1929. WASM_RUNTIME_API_EXTERN const uint8_t *
  1930. wasm_runtime_get_custom_section(const wasm_module_t module_comm,
  1931. const char *name, uint32_t *len);
  1932. /**
  1933. * Get WAMR semantic version
  1934. */
  1935. WASM_RUNTIME_API_EXTERN void
  1936. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch);
  1937. /**
  1938. * Check whether an import func `(import <module_name> <func_name> (func ...))`
  1939. * is linked or not with runtime registered native functions
  1940. */
  1941. WASM_RUNTIME_API_EXTERN bool
  1942. wasm_runtime_is_import_func_linked(const char *module_name,
  1943. const char *func_name);
  1944. /**
  1945. * Check whether an import global `(import <module_name> <global_name>
  1946. * (global ...))` is linked or not with runtime registered native globals
  1947. */
  1948. WASM_RUNTIME_API_EXTERN bool
  1949. wasm_runtime_is_import_global_linked(const char *module_name,
  1950. const char *global_name);
  1951. /**
  1952. * Enlarge the memory region for a module instance
  1953. *
  1954. * @param module_inst the module instance
  1955. * @param inc_page_count the number of pages to add
  1956. *
  1957. * @return true if success, false otherwise
  1958. */
  1959. WASM_RUNTIME_API_EXTERN bool
  1960. wasm_runtime_enlarge_memory(wasm_module_inst_t module_inst,
  1961. uint64_t inc_page_count);
  1962. typedef enum {
  1963. INTERNAL_ERROR,
  1964. MAX_SIZE_REACHED,
  1965. } enlarge_memory_error_reason_t;
  1966. typedef void (*enlarge_memory_error_callback_t)(
  1967. uint32_t inc_page_count, uint64_t current_memory_size,
  1968. uint32_t memory_index, enlarge_memory_error_reason_t failure_reason,
  1969. wasm_module_inst_t instance, wasm_exec_env_t exec_env, void *user_data);
  1970. /**
  1971. * Setup callback invoked when memory.grow fails
  1972. */
  1973. WASM_RUNTIME_API_EXTERN void
  1974. wasm_runtime_set_enlarge_mem_error_callback(
  1975. const enlarge_memory_error_callback_t callback, void *user_data);
  1976. /*
  1977. * module instance context APIs
  1978. * wasm_runtime_create_context_key
  1979. * wasm_runtime_destroy_context_key
  1980. * wasm_runtime_set_context
  1981. * wasm_runtime_set_context_spread
  1982. * wasm_runtime_get_context
  1983. *
  1984. * This set of APIs is intended to be used by an embedder which provides
  1985. * extra sets of native functions, which need per module instance state
  1986. * and are maintained outside of the WAMR tree.
  1987. *
  1988. * It's modelled after the pthread specific API.
  1989. *
  1990. * wasm_runtime_set_context_spread is similar to
  1991. * wasm_runtime_set_context, except that
  1992. * wasm_runtime_set_context_spread applies the change
  1993. * to all threads in the cluster.
  1994. * It's an undefined behavior if multiple threads in a cluster call
  1995. * wasm_runtime_set_context_spread on the same key
  1996. * simultaneously. It's a caller's responsibility to perform necessary
  1997. * serialization if necessary. For example:
  1998. *
  1999. * if (wasm_runtime_get_context(inst, key) == NULL) {
  2000. * newctx = alloc_and_init(...);
  2001. * lock(some_lock);
  2002. * if (wasm_runtime_get_context(inst, key) == NULL) {
  2003. * // this thread won the race
  2004. * wasm_runtime_set_context_spread(inst, key, newctx);
  2005. * newctx = NULL;
  2006. * }
  2007. * unlock(some_lock);
  2008. * if (newctx != NULL) {
  2009. * // this thread lost the race, free it
  2010. * cleanup_and_free(newctx);
  2011. * }
  2012. * }
  2013. *
  2014. * Note: dynamic key create/destroy while instances are live is not
  2015. * implemented as of writing this.
  2016. * it's caller's responsibility to ensure destroying all module instances
  2017. * before calling wasm_runtime_create_context_key or
  2018. * wasm_runtime_destroy_context_key.
  2019. * otherwise, it's an undefined behavior.
  2020. *
  2021. * Note about threads:
  2022. * - When spawning a thread, the contexts (the pointers given to
  2023. * wasm_runtime_set_context) are copied from the parent
  2024. * instance.
  2025. * - The destructor is called only on the main instance.
  2026. */
  2027. WASM_RUNTIME_API_EXTERN void *
  2028. wasm_runtime_create_context_key(void (*dtor)(wasm_module_inst_t inst,
  2029. void *ctx));
  2030. WASM_RUNTIME_API_EXTERN void
  2031. wasm_runtime_destroy_context_key(void *key);
  2032. WASM_RUNTIME_API_EXTERN void
  2033. wasm_runtime_set_context(wasm_module_inst_t inst, void *key, void *ctx);
  2034. WASM_RUNTIME_API_EXTERN void
  2035. wasm_runtime_set_context_spread(wasm_module_inst_t inst, void *key, void *ctx);
  2036. WASM_RUNTIME_API_EXTERN void *
  2037. wasm_runtime_get_context(wasm_module_inst_t inst, void *key);
  2038. /*
  2039. * wasm_runtime_begin_blocking_op/wasm_runtime_end_blocking_op
  2040. *
  2041. * These APIs are intended to be used by the implementations of
  2042. * host functions. It wraps an operation which possibly blocks for long
  2043. * to prepare for async termination.
  2044. *
  2045. * For simplicity, we recommend to wrap only the very minimum piece of
  2046. * the code with this. Ideally, just a single system call.
  2047. *
  2048. * eg.
  2049. *
  2050. * if (!wasm_runtime_begin_blocking_op(exec_env)) {
  2051. * return EINTR;
  2052. * }
  2053. * ret = possibly_blocking_op();
  2054. * wasm_runtime_end_blocking_op(exec_env);
  2055. * return ret;
  2056. *
  2057. * If threading support (WASM_ENABLE_THREAD_MGR) is not enabled,
  2058. * these functions are no-op.
  2059. *
  2060. * If the underlying platform support (OS_ENABLE_WAKEUP_BLOCKING_OP) is
  2061. * not available, these functions are no-op. In that case, the runtime
  2062. * might not terminate a blocking thread in a timely manner.
  2063. *
  2064. * If the underlying platform support is available, it's used to wake up
  2065. * the thread for async termination. The expectation here is that a
  2066. * `os_wakeup_blocking_op` call makes the blocking operation
  2067. * (`possibly_blocking_op` in the above example) return in a timely manner.
  2068. *
  2069. * The actual wake up mechanism used by `os_wakeup_blocking_op` is
  2070. * platform-dependent. It might impose some platform-dependent restrictions
  2071. * on the implementation of the blocking operation.
  2072. *
  2073. * For example, on POSIX-like platforms, a signal (by default SIGUSR1) is
  2074. * used. The signal delivery configurations (eg. signal handler, signal mask,
  2075. * etc) for the signal are set up by the runtime. You can change the signal
  2076. * to use for this purpose by calling os_set_signal_number_for_blocking_op
  2077. * before the runtime initialization.
  2078. */
  2079. WASM_RUNTIME_API_EXTERN bool
  2080. wasm_runtime_begin_blocking_op(wasm_exec_env_t exec_env);
  2081. WASM_RUNTIME_API_EXTERN void
  2082. wasm_runtime_end_blocking_op(wasm_exec_env_t exec_env);
  2083. WASM_RUNTIME_API_EXTERN bool
  2084. wasm_runtime_set_module_name(wasm_module_t module, const char *name,
  2085. char *error_buf, uint32_t error_buf_size);
  2086. /* return the most recently set module name or "" if never set before */
  2087. WASM_RUNTIME_API_EXTERN const char *
  2088. wasm_runtime_get_module_name(wasm_module_t module);
  2089. /*
  2090. * wasm_runtime_detect_native_stack_overflow
  2091. *
  2092. * Detect native stack shortage.
  2093. * Ensure that the calling thread still has a reasonable amount of
  2094. * native stack (WASM_STACK_GUARD_SIZE bytes) available.
  2095. *
  2096. * If enough stack is left, this function returns true.
  2097. * Otherwise, this function raises a "native stack overflow" trap and
  2098. * returns false.
  2099. *
  2100. * Note: please do not expect a very strict detection. it's a good idea
  2101. * to give some margins. wasm_runtime_detect_native_stack_overflow itself
  2102. * requires a small amount of stack to run.
  2103. */
  2104. WASM_RUNTIME_API_EXTERN bool
  2105. wasm_runtime_detect_native_stack_overflow(wasm_exec_env_t exec_env);
  2106. /*
  2107. * wasm_runtime_detect_native_stack_overflow_size
  2108. *
  2109. * Similar to wasm_runtime_detect_native_stack_overflow,
  2110. * but use the caller-specified size instead of WASM_STACK_GUARD_SIZE.
  2111. *
  2112. * An expected usage:
  2113. * ```c
  2114. * __attribute__((noinline)) // inlining can break the stack check
  2115. * void stack_hog(void)
  2116. * {
  2117. * // consume a lot of stack here
  2118. * }
  2119. *
  2120. * void
  2121. * stack_hog_wrapper(exec_env) {
  2122. * // the amount of stack stack_hog would consume,
  2123. * // plus a small margin
  2124. * uint32_t size = 10000000;
  2125. *
  2126. * if (!wasm_runtime_detect_native_stack_overflow_size(exec_env, size)) {
  2127. * // wasm_runtime_detect_native_stack_overflow_size has raised
  2128. * // a trap.
  2129. * return;
  2130. * }
  2131. * stack_hog();
  2132. * }
  2133. * ```
  2134. */
  2135. WASM_RUNTIME_API_EXTERN bool
  2136. wasm_runtime_detect_native_stack_overflow_size(wasm_exec_env_t exec_env,
  2137. uint32_t required_size);
  2138. /**
  2139. * Query whether the wasm binary buffer used to create the module can be freed
  2140. *
  2141. * @param module the target module
  2142. * @return true if the wasm binary buffer can be freed
  2143. */
  2144. WASM_RUNTIME_API_EXTERN bool
  2145. wasm_runtime_is_underlying_binary_freeable(const wasm_module_t module);
  2146. /**
  2147. * Create a shared heap
  2148. *
  2149. * @param init_args the initialization arguments
  2150. * @return the shared heap created
  2151. */
  2152. WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
  2153. wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
  2154. /**
  2155. * This function links two shared heap(lists), `head` and `body` in to a single
  2156. * shared heap list, where `head` becomes the new shared heap list head. The
  2157. * shared heap list remains one continuous shared heap in wasm app's point of
  2158. * view. At most one shared heap in shared heap list can be dynamically
  2159. * allocated, the rest have to be the pre-allocated shared heap. *
  2160. *
  2161. * @param head The head of the shared heap chain.
  2162. * @param body The body of the shared heap chain to be appended.
  2163. * @return The new head of the shared heap chain. NULL if failed.
  2164. */
  2165. WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
  2166. wasm_runtime_chain_shared_heaps(wasm_shared_heap_t head,
  2167. wasm_shared_heap_t body);
  2168. /**
  2169. * This function unchains the shared heaps from the given head. If
  2170. * `entire_chain` is true, it will unchain the entire chain of shared heaps.
  2171. * Otherwise, it will unchain only the first shared heap in the chain.
  2172. *
  2173. * @param head The head of the shared heap chain.
  2174. * @param entire_chain A boolean flag indicating whether to unchain the entire
  2175. * chain.
  2176. * @return The new head of the shared heap chain. Or the last shared heap in the
  2177. * chain if `entire_chain` is true.
  2178. */
  2179. wasm_shared_heap_t
  2180. wasm_runtime_unchain_shared_heaps(wasm_shared_heap_t head, bool entire_chain);
  2181. /**
  2182. * Attach a shared heap, it can be the head of shared heap chain, in that case,
  2183. * attach the shared heap chain, to a module instance
  2184. *
  2185. * @param module_inst the module instance
  2186. * @param shared_heap the shared heap
  2187. * @return true if success, false if failed
  2188. */
  2189. WASM_RUNTIME_API_EXTERN bool
  2190. wasm_runtime_attach_shared_heap(wasm_module_inst_t module_inst,
  2191. wasm_shared_heap_t shared_heap);
  2192. /**
  2193. * Detach a shared heap from a module instance
  2194. *
  2195. * @param module_inst the module instance
  2196. */
  2197. WASM_RUNTIME_API_EXTERN void
  2198. wasm_runtime_detach_shared_heap(wasm_module_inst_t module_inst);
  2199. /**
  2200. * Allocate memory from a shared heap, or the non-preallocated shared heap from
  2201. * the shared heap chain
  2202. *
  2203. * @param module_inst the module instance
  2204. * @param size required memory size
  2205. * @param p_native_addr native address of allocated memory
  2206. *
  2207. * @return return the allocated memory address, which reuses part of the wasm
  2208. * address space and is in the range of [UINT32 - shared_heap_size + 1, UINT32]
  2209. * (when the wasm memory is 32-bit) or [UINT64 - shared_heap_size + 1, UINT64]
  2210. * (when the wasm memory is 64-bit). Note that it is not an absolute address.
  2211. * Return non-zero if success, zero if failed.
  2212. */
  2213. WASM_RUNTIME_API_EXTERN uint64_t
  2214. wasm_runtime_shared_heap_malloc(wasm_module_inst_t module_inst, uint64_t size,
  2215. void **p_native_addr);
  2216. /**
  2217. * Free the memory allocated from shared heap, or the non-preallocated shared
  2218. * heap from the shared heap chain
  2219. *
  2220. * @param module_inst the module instance
  2221. * @param ptr the offset in wasm app
  2222. */
  2223. WASM_RUNTIME_API_EXTERN void
  2224. wasm_runtime_shared_heap_free(wasm_module_inst_t module_inst, uint64_t ptr);
  2225. #ifdef __cplusplus
  2226. }
  2227. #endif
  2228. #endif /* end of _WASM_EXPORT_H */