wasm_export.h 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  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. The runtime will also convert
  1576. * the app pointer to a native pointer, thus there is no need
  1577. * to manually call `wasm_runtime_addr_app_to_native`.
  1578. * '~': the parameter is the pointer's length with i32 type, and must
  1579. * follow after '*'
  1580. * '$': the parameter is a string (i32 in WASM), and runtime will
  1581. * auto check its boundary before calling the native function.
  1582. * Like '*', the runtime will also convert the app pointer to a
  1583. * native pointer.
  1584. * @param n_native_symbols specifies the number of native symbols in the array
  1585. *
  1586. * @return true if success, false otherwise
  1587. */
  1588. WASM_RUNTIME_API_EXTERN bool
  1589. wasm_runtime_register_natives(const char *module_name,
  1590. NativeSymbol *native_symbols,
  1591. uint32_t n_native_symbols);
  1592. /**
  1593. * Register native functions with same module name, similar to
  1594. * wasm_runtime_register_natives, the difference is that runtime passes raw
  1595. * arguments to native API, which means that the native API should be defined as
  1596. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  1597. * and native API should extract arguments one by one from args array with macro
  1598. * native_raw_get_arg
  1599. * and write the return value back to args[0] with macro
  1600. * native_raw_return_type and native_raw_set_return
  1601. */
  1602. WASM_RUNTIME_API_EXTERN bool
  1603. wasm_runtime_register_natives_raw(const char *module_name,
  1604. NativeSymbol *native_symbols,
  1605. uint32_t n_native_symbols);
  1606. /**
  1607. * Undo wasm_runtime_register_natives or wasm_runtime_register_natives_raw
  1608. *
  1609. * @param module_name Should be the same as the corresponding
  1610. * wasm_runtime_register_natives.
  1611. * (Same in term of strcmp.)
  1612. *
  1613. * @param native_symbols Should be the same as the corresponding
  1614. * wasm_runtime_register_natives.
  1615. * (Same in term of pointer comparison.)
  1616. *
  1617. * @return true if success, false otherwise
  1618. */
  1619. WASM_RUNTIME_API_EXTERN bool
  1620. wasm_runtime_unregister_natives(const char *module_name,
  1621. NativeSymbol *native_symbols);
  1622. /**
  1623. * Get an export global instance
  1624. *
  1625. * @param module_inst the module instance
  1626. * @param name the export global name
  1627. * @param global_inst location to store the global instance
  1628. *
  1629. * @return true if success, false otherwise
  1630. *
  1631. */
  1632. WASM_RUNTIME_API_EXTERN bool
  1633. wasm_runtime_get_export_global_inst(const wasm_module_inst_t module_inst,
  1634. const char *name,
  1635. wasm_global_inst_t *global_inst);
  1636. /**
  1637. * Get an export table instance
  1638. *
  1639. * @param module_inst the module instance
  1640. * @param name the export table name
  1641. * @param table_inst location to store the table instance
  1642. *
  1643. * @return true if success, false otherwise
  1644. *
  1645. */
  1646. WASM_RUNTIME_API_EXTERN bool
  1647. wasm_runtime_get_export_table_inst(const wasm_module_inst_t module_inst,
  1648. const char *name,
  1649. wasm_table_inst_t *table_inst);
  1650. /**
  1651. * Get a function instance from a table.
  1652. *
  1653. * @param module_inst the module instance
  1654. * @param table_inst the table instance
  1655. * @param idx the index in the table
  1656. *
  1657. * @return the function instance if successful, NULL otherwise
  1658. */
  1659. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  1660. wasm_table_get_func_inst(const wasm_module_inst_t module_inst,
  1661. const wasm_table_inst_t *table_inst, uint32_t idx);
  1662. /**
  1663. * Get attachment of native function from execution environment
  1664. *
  1665. * @param exec_env the execution environment to retrieve
  1666. *
  1667. * @return the attachment of native function
  1668. */
  1669. WASM_RUNTIME_API_EXTERN void *
  1670. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  1671. /**
  1672. * Set user data to execution environment.
  1673. *
  1674. * @param exec_env the execution environment
  1675. * @param user_data the user data to be set
  1676. */
  1677. WASM_RUNTIME_API_EXTERN void
  1678. wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data);
  1679. /**
  1680. * Get the user data within execution environment.
  1681. *
  1682. * @param exec_env the execution environment
  1683. *
  1684. * @return the user data (NULL if not set yet)
  1685. */
  1686. WASM_RUNTIME_API_EXTERN void *
  1687. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  1688. /**
  1689. * Set native stack boundary to execution environment, if it is set,
  1690. * it will be used instead of getting the boundary with the platform
  1691. * layer API when calling wasm functions. This is useful for some
  1692. * fiber cases.
  1693. *
  1694. * Note: unlike setting the boundary by runtime, this API doesn't add
  1695. * the WASM_STACK_GUARD_SIZE(see comments in core/config.h) to the
  1696. * exec_env's native_stack_boundary to reserve bytes to the native
  1697. * thread stack boundary, which is used to throw native stack overflow
  1698. * exception if the guard boundary is reached. Developer should ensure
  1699. * that enough guard bytes are kept.
  1700. *
  1701. * @param exec_env the execution environment
  1702. * @param native_stack_boundary the user data to be set
  1703. */
  1704. WASM_RUNTIME_API_EXTERN void
  1705. wasm_runtime_set_native_stack_boundary(wasm_exec_env_t exec_env,
  1706. uint8_t *native_stack_boundary);
  1707. /**
  1708. * Set the instruction count limit to the execution environment.
  1709. * By default the instruction count limit is -1, which means no limit.
  1710. * However, if the instruction count limit is set to a positive value,
  1711. * the execution will be terminated when the instruction count reaches
  1712. * the limit.
  1713. *
  1714. * @param exec_env the execution environment
  1715. * @param instruction_count the instruction count limit
  1716. */
  1717. WASM_RUNTIME_API_EXTERN void
  1718. wasm_runtime_set_instruction_count_limit(wasm_exec_env_t exec_env,
  1719. int instruction_count);
  1720. /**
  1721. * Dump runtime memory consumption, including:
  1722. * Exec env memory consumption
  1723. * WASM module memory consumption
  1724. * WASM module instance memory consumption
  1725. * stack and app heap used info
  1726. *
  1727. * @param exec_env the execution environment
  1728. */
  1729. WASM_RUNTIME_API_EXTERN void
  1730. wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env);
  1731. /**
  1732. * Dump runtime performance profiler data of each function
  1733. *
  1734. * @param module_inst the WASM module instance to profile
  1735. */
  1736. WASM_RUNTIME_API_EXTERN void
  1737. wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
  1738. /**
  1739. * Return total wasm functions' execution time in ms
  1740. *
  1741. * @param module_inst the WASM module instance to profile
  1742. */
  1743. WASM_RUNTIME_API_EXTERN double
  1744. wasm_runtime_sum_wasm_exec_time(wasm_module_inst_t module_inst);
  1745. /**
  1746. * Return execution time in ms of a given wasm function with
  1747. * func_name. If the function is not found, return 0.
  1748. *
  1749. * @param module_inst the WASM module instance to profile
  1750. * @param func_name could be an export name or a name in the
  1751. * name section
  1752. */
  1753. WASM_RUNTIME_API_EXTERN double
  1754. wasm_runtime_get_wasm_func_exec_time(wasm_module_inst_t inst,
  1755. const char *func_name);
  1756. /* wasm thread callback function type */
  1757. typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *);
  1758. /* wasm thread type */
  1759. typedef uintptr_t wasm_thread_t;
  1760. /**
  1761. * Set the max thread num per cluster.
  1762. *
  1763. * @param num maximum thread num
  1764. */
  1765. WASM_RUNTIME_API_EXTERN void
  1766. wasm_runtime_set_max_thread_num(uint32_t num);
  1767. /**
  1768. * Spawn a new exec_env, the spawned exec_env
  1769. * can be used in other threads
  1770. *
  1771. * @param num the original exec_env
  1772. *
  1773. * @return the spawned exec_env if success, NULL otherwise
  1774. */
  1775. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  1776. wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
  1777. /**
  1778. * Destroy the spawned exec_env
  1779. *
  1780. * @param exec_env the spawned exec_env
  1781. */
  1782. WASM_RUNTIME_API_EXTERN void
  1783. wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
  1784. /**
  1785. * Spawn a thread from the given exec_env
  1786. *
  1787. * @param exec_env the original exec_env
  1788. * @param tid thread id to be returned to the caller
  1789. * @param callback the callback function provided by the user
  1790. * @param arg the arguments passed to the callback
  1791. *
  1792. * @return 0 if success, -1 otherwise
  1793. */
  1794. WASM_RUNTIME_API_EXTERN int32_t
  1795. wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
  1796. wasm_thread_callback_t callback, void *arg);
  1797. /**
  1798. * Wait a spawned thread to terminate
  1799. *
  1800. * @param tid thread id
  1801. * @param retval if not NULL, output the return value of the thread
  1802. *
  1803. * @return 0 if success, error number otherwise
  1804. */
  1805. WASM_RUNTIME_API_EXTERN int32_t
  1806. wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
  1807. /**
  1808. * Map external object to an internal externref index: if the index
  1809. * has been created, return it, otherwise create the index.
  1810. *
  1811. * @param module_inst the WASM module instance that the extern object
  1812. * belongs to
  1813. * @param extern_obj the external object to be mapped
  1814. * @param p_externref_idx return externref index of the external object
  1815. *
  1816. * @return true if success, false otherwise
  1817. */
  1818. WASM_RUNTIME_API_EXTERN bool
  1819. wasm_externref_obj2ref(wasm_module_inst_t module_inst, void *extern_obj,
  1820. uint32_t *p_externref_idx);
  1821. /**
  1822. * Delete external object registered by `wasm_externref_obj2ref`.
  1823. *
  1824. * @param module_inst the WASM module instance that the extern object
  1825. * belongs to
  1826. * @param extern_obj the external object to be deleted
  1827. *
  1828. * @return true if success, false otherwise
  1829. */
  1830. WASM_RUNTIME_API_EXTERN bool
  1831. wasm_externref_objdel(wasm_module_inst_t module_inst, void *extern_obj);
  1832. /**
  1833. * Set cleanup callback to release external object.
  1834. *
  1835. * @param module_inst the WASM module instance that the extern object
  1836. * belongs to
  1837. * @param extern_obj the external object to which to set the
  1838. * `extern_obj_cleanup` cleanup callback.
  1839. * @param extern_obj_cleanup a callback to release `extern_obj`
  1840. *
  1841. * @return true if success, false otherwise
  1842. */
  1843. WASM_RUNTIME_API_EXTERN bool
  1844. wasm_externref_set_cleanup(wasm_module_inst_t module_inst, void *extern_obj,
  1845. void (*extern_obj_cleanup)(void *));
  1846. /**
  1847. * Retrieve the external object from an internal externref index
  1848. *
  1849. * @param externref_idx the externref index to retrieve
  1850. * @param p_extern_obj return the mapped external object of
  1851. * the externref index
  1852. *
  1853. * @return true if success, false otherwise
  1854. */
  1855. WASM_RUNTIME_API_EXTERN bool
  1856. wasm_externref_ref2obj(uint32_t externref_idx, void **p_extern_obj);
  1857. /**
  1858. * Retain an extern object which is mapped to the internal externref
  1859. * so that the object won't be cleaned during extern object reclaim
  1860. * if it isn't used.
  1861. *
  1862. * @param externref_idx the externref index of an external object
  1863. * to retain
  1864. * @return true if success, false otherwise
  1865. */
  1866. WASM_RUNTIME_API_EXTERN bool
  1867. wasm_externref_retain(uint32_t externref_idx);
  1868. /**
  1869. * Dump the call stack to stdout
  1870. *
  1871. * @param exec_env the execution environment
  1872. */
  1873. WASM_RUNTIME_API_EXTERN void
  1874. wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
  1875. /**
  1876. * Get the size required to store the call stack contents, including
  1877. * the space for terminating null byte ('\0')
  1878. *
  1879. * @param exec_env the execution environment
  1880. *
  1881. * @return size required to store the contents, 0 means error
  1882. */
  1883. WASM_RUNTIME_API_EXTERN uint32_t
  1884. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env);
  1885. /**
  1886. * Dump the call stack to buffer.
  1887. *
  1888. * @note this function is not thread-safe, please only use this API
  1889. * when the exec_env is not executing
  1890. *
  1891. * @param exec_env the execution environment
  1892. * @param buf buffer to store the dumped content
  1893. * @param len length of the buffer
  1894. *
  1895. * @return bytes dumped to the buffer, including the terminating null
  1896. * byte ('\0'), 0 means error and data in buf may be invalid
  1897. */
  1898. WASM_RUNTIME_API_EXTERN uint32_t
  1899. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  1900. uint32_t len);
  1901. /**
  1902. * Get the size required to store the LLVM PGO profile data
  1903. *
  1904. * @param module_inst the WASM module instance
  1905. *
  1906. * @return size required to store the contents, 0 means error
  1907. */
  1908. WASM_RUNTIME_API_EXTERN uint32_t
  1909. wasm_runtime_get_pgo_prof_data_size(wasm_module_inst_t module_inst);
  1910. /**
  1911. * Dump the LLVM PGO profile data to buffer
  1912. *
  1913. * @param module_inst the WASM module instance
  1914. * @param buf buffer to store the dumped content
  1915. * @param len length of the buffer
  1916. *
  1917. * @return bytes dumped to the buffer, 0 means error and data in buf
  1918. * may be invalid
  1919. */
  1920. WASM_RUNTIME_API_EXTERN uint32_t
  1921. wasm_runtime_dump_pgo_prof_data_to_buf(wasm_module_inst_t module_inst,
  1922. char *buf, uint32_t len);
  1923. /**
  1924. * Get a custom section by name
  1925. *
  1926. * @param module_comm the module to find
  1927. * @param name name of the custom section
  1928. * @param len return the length of the content if found
  1929. *
  1930. * @return Custom section content (not including the name length
  1931. * and name string) if found, NULL otherwise
  1932. */
  1933. WASM_RUNTIME_API_EXTERN const uint8_t *
  1934. wasm_runtime_get_custom_section(const wasm_module_t module_comm,
  1935. const char *name, uint32_t *len);
  1936. /**
  1937. * Get WAMR semantic version
  1938. */
  1939. WASM_RUNTIME_API_EXTERN void
  1940. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch);
  1941. /**
  1942. * Check whether an import func `(import <module_name> <func_name> (func ...))`
  1943. * is linked or not with runtime registered native functions
  1944. */
  1945. WASM_RUNTIME_API_EXTERN bool
  1946. wasm_runtime_is_import_func_linked(const char *module_name,
  1947. const char *func_name);
  1948. /**
  1949. * Check whether an import global `(import <module_name> <global_name>
  1950. * (global ...))` is linked or not with runtime registered native globals
  1951. */
  1952. WASM_RUNTIME_API_EXTERN bool
  1953. wasm_runtime_is_import_global_linked(const char *module_name,
  1954. const char *global_name);
  1955. /**
  1956. * Enlarge the memory region for a module instance
  1957. *
  1958. * @param module_inst the module instance
  1959. * @param inc_page_count the number of pages to add
  1960. *
  1961. * @return true if success, false otherwise
  1962. */
  1963. WASM_RUNTIME_API_EXTERN bool
  1964. wasm_runtime_enlarge_memory(wasm_module_inst_t module_inst,
  1965. uint64_t inc_page_count);
  1966. typedef enum {
  1967. INTERNAL_ERROR,
  1968. MAX_SIZE_REACHED,
  1969. } enlarge_memory_error_reason_t;
  1970. typedef void (*enlarge_memory_error_callback_t)(
  1971. uint32_t inc_page_count, uint64_t current_memory_size,
  1972. uint32_t memory_index, enlarge_memory_error_reason_t failure_reason,
  1973. wasm_module_inst_t instance, wasm_exec_env_t exec_env, void *user_data);
  1974. /**
  1975. * Setup callback invoked when memory.grow fails
  1976. */
  1977. WASM_RUNTIME_API_EXTERN void
  1978. wasm_runtime_set_enlarge_mem_error_callback(
  1979. const enlarge_memory_error_callback_t callback, void *user_data);
  1980. /*
  1981. * module instance context APIs
  1982. * wasm_runtime_create_context_key
  1983. * wasm_runtime_destroy_context_key
  1984. * wasm_runtime_set_context
  1985. * wasm_runtime_set_context_spread
  1986. * wasm_runtime_get_context
  1987. *
  1988. * This set of APIs is intended to be used by an embedder which provides
  1989. * extra sets of native functions, which need per module instance state
  1990. * and are maintained outside of the WAMR tree.
  1991. *
  1992. * It's modelled after the pthread specific API.
  1993. *
  1994. * wasm_runtime_set_context_spread is similar to
  1995. * wasm_runtime_set_context, except that
  1996. * wasm_runtime_set_context_spread applies the change
  1997. * to all threads in the cluster.
  1998. * It's an undefined behavior if multiple threads in a cluster call
  1999. * wasm_runtime_set_context_spread on the same key
  2000. * simultaneously. It's a caller's responsibility to perform necessary
  2001. * serialization if necessary. For example:
  2002. *
  2003. * if (wasm_runtime_get_context(inst, key) == NULL) {
  2004. * newctx = alloc_and_init(...);
  2005. * lock(some_lock);
  2006. * if (wasm_runtime_get_context(inst, key) == NULL) {
  2007. * // this thread won the race
  2008. * wasm_runtime_set_context_spread(inst, key, newctx);
  2009. * newctx = NULL;
  2010. * }
  2011. * unlock(some_lock);
  2012. * if (newctx != NULL) {
  2013. * // this thread lost the race, free it
  2014. * cleanup_and_free(newctx);
  2015. * }
  2016. * }
  2017. *
  2018. * Note: dynamic key create/destroy while instances are live is not
  2019. * implemented as of writing this.
  2020. * it's caller's responsibility to ensure destroying all module instances
  2021. * before calling wasm_runtime_create_context_key or
  2022. * wasm_runtime_destroy_context_key.
  2023. * otherwise, it's an undefined behavior.
  2024. *
  2025. * Note about threads:
  2026. * - When spawning a thread, the contexts (the pointers given to
  2027. * wasm_runtime_set_context) are copied from the parent
  2028. * instance.
  2029. * - The destructor is called only on the main instance.
  2030. */
  2031. WASM_RUNTIME_API_EXTERN void *
  2032. wasm_runtime_create_context_key(void (*dtor)(wasm_module_inst_t inst,
  2033. void *ctx));
  2034. WASM_RUNTIME_API_EXTERN void
  2035. wasm_runtime_destroy_context_key(void *key);
  2036. WASM_RUNTIME_API_EXTERN void
  2037. wasm_runtime_set_context(wasm_module_inst_t inst, void *key, void *ctx);
  2038. WASM_RUNTIME_API_EXTERN void
  2039. wasm_runtime_set_context_spread(wasm_module_inst_t inst, void *key, void *ctx);
  2040. WASM_RUNTIME_API_EXTERN void *
  2041. wasm_runtime_get_context(wasm_module_inst_t inst, void *key);
  2042. /*
  2043. * wasm_runtime_begin_blocking_op/wasm_runtime_end_blocking_op
  2044. *
  2045. * These APIs are intended to be used by the implementations of
  2046. * host functions. It wraps an operation which possibly blocks for long
  2047. * to prepare for async termination.
  2048. *
  2049. * For simplicity, we recommend to wrap only the very minimum piece of
  2050. * the code with this. Ideally, just a single system call.
  2051. *
  2052. * eg.
  2053. *
  2054. * if (!wasm_runtime_begin_blocking_op(exec_env)) {
  2055. * return EINTR;
  2056. * }
  2057. * ret = possibly_blocking_op();
  2058. * wasm_runtime_end_blocking_op(exec_env);
  2059. * return ret;
  2060. *
  2061. * If threading support (WASM_ENABLE_THREAD_MGR) is not enabled,
  2062. * these functions are no-op.
  2063. *
  2064. * If the underlying platform support (OS_ENABLE_WAKEUP_BLOCKING_OP) is
  2065. * not available, these functions are no-op. In that case, the runtime
  2066. * might not terminate a blocking thread in a timely manner.
  2067. *
  2068. * If the underlying platform support is available, it's used to wake up
  2069. * the thread for async termination. The expectation here is that a
  2070. * `os_wakeup_blocking_op` call makes the blocking operation
  2071. * (`possibly_blocking_op` in the above example) return in a timely manner.
  2072. *
  2073. * The actual wake up mechanism used by `os_wakeup_blocking_op` is
  2074. * platform-dependent. It might impose some platform-dependent restrictions
  2075. * on the implementation of the blocking operation.
  2076. *
  2077. * For example, on POSIX-like platforms, a signal (by default SIGUSR1) is
  2078. * used. The signal delivery configurations (eg. signal handler, signal mask,
  2079. * etc) for the signal are set up by the runtime. You can change the signal
  2080. * to use for this purpose by calling os_set_signal_number_for_blocking_op
  2081. * before the runtime initialization.
  2082. */
  2083. WASM_RUNTIME_API_EXTERN bool
  2084. wasm_runtime_begin_blocking_op(wasm_exec_env_t exec_env);
  2085. WASM_RUNTIME_API_EXTERN void
  2086. wasm_runtime_end_blocking_op(wasm_exec_env_t exec_env);
  2087. WASM_RUNTIME_API_EXTERN bool
  2088. wasm_runtime_set_module_name(wasm_module_t module, const char *name,
  2089. char *error_buf, uint32_t error_buf_size);
  2090. /* return the most recently set module name or "" if never set before */
  2091. WASM_RUNTIME_API_EXTERN const char *
  2092. wasm_runtime_get_module_name(wasm_module_t module);
  2093. /*
  2094. * wasm_runtime_detect_native_stack_overflow
  2095. *
  2096. * Detect native stack shortage.
  2097. * Ensure that the calling thread still has a reasonable amount of
  2098. * native stack (WASM_STACK_GUARD_SIZE bytes) available.
  2099. *
  2100. * If enough stack is left, this function returns true.
  2101. * Otherwise, this function raises a "native stack overflow" trap and
  2102. * returns false.
  2103. *
  2104. * Note: please do not expect a very strict detection. it's a good idea
  2105. * to give some margins. wasm_runtime_detect_native_stack_overflow itself
  2106. * requires a small amount of stack to run.
  2107. */
  2108. WASM_RUNTIME_API_EXTERN bool
  2109. wasm_runtime_detect_native_stack_overflow(wasm_exec_env_t exec_env);
  2110. /*
  2111. * wasm_runtime_detect_native_stack_overflow_size
  2112. *
  2113. * Similar to wasm_runtime_detect_native_stack_overflow,
  2114. * but use the caller-specified size instead of WASM_STACK_GUARD_SIZE.
  2115. *
  2116. * An expected usage:
  2117. * ```c
  2118. * __attribute__((noinline)) // inlining can break the stack check
  2119. * void stack_hog(void)
  2120. * {
  2121. * // consume a lot of stack here
  2122. * }
  2123. *
  2124. * void
  2125. * stack_hog_wrapper(exec_env) {
  2126. * // the amount of stack stack_hog would consume,
  2127. * // plus a small margin
  2128. * uint32_t size = 10000000;
  2129. *
  2130. * if (!wasm_runtime_detect_native_stack_overflow_size(exec_env, size)) {
  2131. * // wasm_runtime_detect_native_stack_overflow_size has raised
  2132. * // a trap.
  2133. * return;
  2134. * }
  2135. * stack_hog();
  2136. * }
  2137. * ```
  2138. */
  2139. WASM_RUNTIME_API_EXTERN bool
  2140. wasm_runtime_detect_native_stack_overflow_size(wasm_exec_env_t exec_env,
  2141. uint32_t required_size);
  2142. /**
  2143. * Query whether the wasm binary buffer used to create the module can be freed
  2144. *
  2145. * @param module the target module
  2146. * @return true if the wasm binary buffer can be freed
  2147. */
  2148. WASM_RUNTIME_API_EXTERN bool
  2149. wasm_runtime_is_underlying_binary_freeable(const wasm_module_t module);
  2150. /**
  2151. * Create a shared heap
  2152. *
  2153. * @param init_args the initialization arguments
  2154. * @return the shared heap created
  2155. */
  2156. WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
  2157. wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
  2158. /**
  2159. * This function links two shared heap(lists), `head` and `body` in to a single
  2160. * shared heap list, where `head` becomes the new shared heap list head. The
  2161. * shared heap list remains one continuous shared heap in wasm app's point of
  2162. * view. At most one shared heap in shared heap list can be dynamically
  2163. * allocated, the rest have to be the pre-allocated shared heap. *
  2164. *
  2165. * @param head The head of the shared heap chain.
  2166. * @param body The body of the shared heap chain to be appended.
  2167. * @return The new head of the shared heap chain. NULL if failed.
  2168. */
  2169. WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
  2170. wasm_runtime_chain_shared_heaps(wasm_shared_heap_t head,
  2171. wasm_shared_heap_t body);
  2172. /**
  2173. * This function unchains the shared heaps from the given head. If
  2174. * `entire_chain` is true, it will unchain the entire chain of shared heaps.
  2175. * Otherwise, it will unchain only the first shared heap in the chain.
  2176. *
  2177. * @param head The head of the shared heap chain.
  2178. * @param entire_chain A boolean flag indicating whether to unchain the entire
  2179. * chain.
  2180. * @return The new head of the shared heap chain. Or the last shared heap in the
  2181. * chain if `entire_chain` is true.
  2182. */
  2183. wasm_shared_heap_t
  2184. wasm_runtime_unchain_shared_heaps(wasm_shared_heap_t head, bool entire_chain);
  2185. /**
  2186. * Attach a shared heap, it can be the head of shared heap chain, in that case,
  2187. * attach the shared heap chain, to a module instance
  2188. *
  2189. * @param module_inst the module instance
  2190. * @param shared_heap the shared heap
  2191. * @return true if success, false if failed
  2192. */
  2193. WASM_RUNTIME_API_EXTERN bool
  2194. wasm_runtime_attach_shared_heap(wasm_module_inst_t module_inst,
  2195. wasm_shared_heap_t shared_heap);
  2196. /**
  2197. * Detach a shared heap from a module instance
  2198. *
  2199. * @param module_inst the module instance
  2200. */
  2201. WASM_RUNTIME_API_EXTERN void
  2202. wasm_runtime_detach_shared_heap(wasm_module_inst_t module_inst);
  2203. /**
  2204. * Allocate memory from a shared heap, or the non-preallocated shared heap from
  2205. * the shared heap chain
  2206. *
  2207. * @param module_inst the module instance
  2208. * @param size required memory size
  2209. * @param p_native_addr native address of allocated memory
  2210. *
  2211. * @return return the allocated memory address, which reuses part of the wasm
  2212. * address space and is in the range of [UINT32 - shared_heap_size + 1, UINT32]
  2213. * (when the wasm memory is 32-bit) or [UINT64 - shared_heap_size + 1, UINT64]
  2214. * (when the wasm memory is 64-bit). Note that it is not an absolute address.
  2215. * Return non-zero if success, zero if failed.
  2216. */
  2217. WASM_RUNTIME_API_EXTERN uint64_t
  2218. wasm_runtime_shared_heap_malloc(wasm_module_inst_t module_inst, uint64_t size,
  2219. void **p_native_addr);
  2220. /**
  2221. * Free the memory allocated from shared heap, or the non-preallocated shared
  2222. * heap from the shared heap chain
  2223. *
  2224. * @param module_inst the module instance
  2225. * @param ptr the offset in wasm app
  2226. */
  2227. WASM_RUNTIME_API_EXTERN void
  2228. wasm_runtime_shared_heap_free(wasm_module_inst_t module_inst, uint64_t ptr);
  2229. #ifdef __cplusplus
  2230. }
  2231. #endif
  2232. #endif /* end of _WASM_EXPORT_H */