wasm_export.h 55 KB

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