wasm_export.h 47 KB

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