wasm_export.h 62 KB

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