wasm_export.h 79 KB

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