wasm_export.h 76 KB

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