wasm_export.h 57 KB

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