wasm_export.h 71 KB

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