wasm_export.h 56 KB

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