wasm_export.h 54 KB

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