wasm_export.h 57 KB

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