wasm_export.h 67 KB

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