wasm_export.h 55 KB

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