wasm_export.h 46 KB

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