wasm_export.h 76 KB

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