wasm_export.h 68 KB

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