wasm_export.h 55 KB

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