wasm_export.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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;
  98. } MemAllocOption;
  99. #endif
  100. /* WASM runtime initialize arguments */
  101. typedef struct RuntimeInitArgs {
  102. mem_alloc_type_t mem_alloc_type;
  103. MemAllocOption mem_alloc_option;
  104. const char *native_module_name;
  105. NativeSymbol *native_symbols;
  106. uint32_t n_native_symbols;
  107. /* maximum thread number, only used when
  108. WASM_ENABLE_THREAD_MGR is defined */
  109. uint32_t max_thread_num;
  110. #if WASM_ENABLE_DEBUG_INTERP != 0
  111. char ip_addr[128];
  112. int platform_port;
  113. int instance_port;
  114. #endif
  115. } RuntimeInitArgs;
  116. #ifndef WASM_VALKIND_T_DEFINED
  117. #define WASM_VALKIND_T_DEFINED
  118. typedef uint8_t wasm_valkind_t;
  119. enum wasm_valkind_enum {
  120. WASM_I32,
  121. WASM_I64,
  122. WASM_F32,
  123. WASM_F64,
  124. WASM_ANYREF = 128,
  125. WASM_FUNCREF,
  126. };
  127. #endif
  128. #ifndef WASM_VAL_T_DEFINED
  129. #define WASM_VAL_T_DEFINED
  130. struct wasm_ref_t;
  131. typedef struct wasm_val_t {
  132. wasm_valkind_t kind;
  133. union {
  134. int32_t i32;
  135. int64_t i64;
  136. float f32;
  137. double f64;
  138. struct wasm_ref_t *ref;
  139. } of;
  140. } wasm_val_t;
  141. #endif
  142. /**
  143. * Initialize the WASM runtime environment, and also initialize
  144. * the memory allocator with system allocator, which calls os_malloc
  145. * to allocate memory
  146. *
  147. * @return true if success, false otherwise
  148. */
  149. WASM_RUNTIME_API_EXTERN bool
  150. wasm_runtime_init(void);
  151. /**
  152. * Initialize the WASM runtime environment, and also initialize
  153. * the memory allocator and register native symbols, which are specified
  154. * with init arguments
  155. *
  156. * @param init_args specifies the init arguments
  157. *
  158. * @return return true if success, false otherwise
  159. */
  160. WASM_RUNTIME_API_EXTERN bool
  161. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  162. /**
  163. * Destroy the WASM runtime environment.
  164. */
  165. WASM_RUNTIME_API_EXTERN void
  166. wasm_runtime_destroy(void);
  167. /**
  168. * Allocate memory from runtime memory environment.
  169. *
  170. * @param size bytes need to allocate
  171. *
  172. * @return the pointer to memory allocated
  173. */
  174. WASM_RUNTIME_API_EXTERN void *
  175. wasm_runtime_malloc(unsigned int size);
  176. /**
  177. * Reallocate memory from runtime memory environment
  178. *
  179. * @param ptr the original memory
  180. * @param size bytes need to reallocate
  181. *
  182. * @return the pointer to memory reallocated
  183. */
  184. WASM_RUNTIME_API_EXTERN void *
  185. wasm_runtime_realloc(void *ptr, unsigned int size);
  186. /*
  187. * Free memory to runtime memory environment.
  188. */
  189. WASM_RUNTIME_API_EXTERN void
  190. wasm_runtime_free(void *ptr);
  191. /**
  192. * Get the package type of a buffer.
  193. *
  194. * @param buf the package buffer
  195. * @param size the package buffer size
  196. *
  197. * @return the package type, return Package_Type_Unknown if the type is unknown
  198. */
  199. WASM_RUNTIME_API_EXTERN package_type_t
  200. get_package_type(const uint8_t *buf, uint32_t size);
  201. /**
  202. * It is a callback for WAMR providing by embedding to load a module file
  203. * into a buffer
  204. */
  205. typedef bool (*module_reader)(const char *module_name,
  206. uint8_t **p_buffer, uint32_t *p_size);
  207. /**
  208. * It is a callback for WAMR providing by embedding to release the buffer which
  209. * is used by loading a module file
  210. */
  211. typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size);
  212. /**
  213. * To setup callbacks for reading and releasing a buffer about a module file
  214. *
  215. * @param reader a callback to read a module file into a buffer
  216. * @param destroyer a callback to release above buffer
  217. */
  218. WASM_RUNTIME_API_EXTERN void
  219. wasm_runtime_set_module_reader(const module_reader reader,
  220. const module_destroyer destroyer);
  221. /**
  222. * Give the "module" a name "module_name".
  223. * can not assign a new name to a module if it already has a name
  224. *
  225. * @param module_name indicate a name
  226. * @param module the target module
  227. * @param error_buf output of the exception info
  228. * @param error_buf_size the size of the exception string
  229. *
  230. * @return true means success, false means failed
  231. */
  232. WASM_RUNTIME_API_EXTERN bool
  233. wasm_runtime_register_module(const char *module_name, wasm_module_t module,
  234. char *error_buf, uint32_t error_buf_size);
  235. /**
  236. * To check if there is already a loaded module named module_name in the
  237. * runtime. you will not want to load repeately
  238. *
  239. * @param module_name indicate a name
  240. *
  241. * @return return WASM module loaded, NULL if failed
  242. */
  243. WASM_RUNTIME_API_EXTERN wasm_module_t
  244. wasm_runtime_find_module_registered(const char *module_name);
  245. /**
  246. * Load a WASM module from a specified byte buffer. The byte buffer can be
  247. * WASM binary data when interpreter or JIT is enabled, or AOT binary data
  248. * when AOT is enabled. If it is AOT binary data, it must be 4-byte aligned.
  249. *
  250. * @param buf the byte buffer which contains the WASM binary data
  251. * @param size the size of the buffer
  252. * @param error_buf output of the exception info
  253. * @param error_buf_size the size of the exception string
  254. *
  255. * @return return WASM module loaded, NULL if failed
  256. */
  257. WASM_RUNTIME_API_EXTERN wasm_module_t
  258. wasm_runtime_load(const uint8_t *buf, uint32_t size,
  259. char *error_buf, uint32_t error_buf_size);
  260. /**
  261. * Load a WASM module from a specified WASM or AOT section list.
  262. *
  263. * @param section_list the section list which contains each section data
  264. * @param is_aot whether the section list is AOT section list
  265. * @param error_buf output of the exception info
  266. * @param error_buf_size the size of the exception string
  267. *
  268. * @return return WASM module loaded, NULL if failed
  269. */
  270. WASM_RUNTIME_API_EXTERN wasm_module_t
  271. wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
  272. char *error_buf, uint32_t error_buf_size);
  273. /**
  274. * Unload a WASM module.
  275. *
  276. * @param module the module to be unloaded
  277. */
  278. WASM_RUNTIME_API_EXTERN void
  279. wasm_runtime_unload(wasm_module_t module);
  280. WASM_RUNTIME_API_EXTERN void
  281. wasm_runtime_set_wasi_args_ex(wasm_module_t module,
  282. const char *dir_list[], uint32_t dir_count,
  283. const char *map_dir_list[], uint32_t map_dir_count,
  284. const char *env[], uint32_t env_count,
  285. char *argv[], int argc,
  286. int stdinfd, int stdoutfd, int stderrfd);
  287. WASM_RUNTIME_API_EXTERN void
  288. wasm_runtime_set_wasi_args(wasm_module_t module,
  289. const char *dir_list[], uint32_t dir_count,
  290. const char *map_dir_list[], uint32_t map_dir_count,
  291. const char *env[], uint32_t env_count,
  292. char *argv[], int argc);
  293. /**
  294. * Instantiate a WASM module.
  295. *
  296. * @param module the WASM module to instantiate
  297. * @param stack_size the default stack size of the module instance when the
  298. * exec env's operation stack isn't created by user, e.g. API
  299. * wasm_application_execute_main() and wasm_application_execute_func()
  300. * create the operation stack internally with the stack size specified
  301. * here. And API wasm_runtime_create_exec_env() creates the operation
  302. * stack with stack size specified by its parameter, the stack size
  303. * specified here is ignored.
  304. * @param heap_size the default heap size of the module instance, a heap will
  305. * be created besides the app memory space. Both wasm app and native
  306. * function can allocate memory from the heap.
  307. * @param error_buf buffer to output the error info if failed
  308. * @param error_buf_size the size of the error buffer
  309. *
  310. * @return return the instantiated WASM module instance, NULL if failed
  311. */
  312. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  313. wasm_runtime_instantiate(const wasm_module_t module,
  314. uint32_t stack_size, uint32_t heap_size,
  315. char *error_buf, uint32_t error_buf_size);
  316. /**
  317. * Deinstantiate a WASM module instance, destroy the resources.
  318. *
  319. * @param module_inst the WASM module instance to destroy
  320. */
  321. WASM_RUNTIME_API_EXTERN void
  322. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  323. WASM_RUNTIME_API_EXTERN bool
  324. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  325. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  326. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  327. /**
  328. * Lookup an exported function in the WASM module instance.
  329. *
  330. * @param module_inst the module instance
  331. * @param name the name of the function
  332. * @param signature the signature of the function, ignored currently
  333. *
  334. * @return the function instance found, NULL if not found
  335. */
  336. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  337. wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
  338. const char *name, const char *signature);
  339. /**
  340. * Create execution environment for a WASM module instance.
  341. *
  342. * @param module_inst the module instance
  343. * @param stack_size the stack size to execute a WASM function
  344. *
  345. * @return the execution environment, NULL if failed, e.g. invalid
  346. * stack size is passed
  347. */
  348. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  349. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  350. uint32_t stack_size);
  351. /**
  352. * Destroy the execution environment.
  353. *
  354. * @param exec_env the execution environment to destroy
  355. */
  356. WASM_RUNTIME_API_EXTERN void
  357. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  358. /**
  359. * Initialize thread environment.
  360. * Note:
  361. * If developer creates a child thread by himself to call the
  362. * the wasm function in that thread, he should call this API
  363. * firstly before calling the wasm function and then call
  364. * wasm_runtime_destroy_thread_env() after calling the wasm
  365. * function. If the thread is created from the runtime API,
  366. * it is unnecessary to call these two APIs.
  367. *
  368. * @return true if success, false otherwise
  369. */
  370. WASM_RUNTIME_API_EXTERN bool
  371. wasm_runtime_init_thread_env(void);
  372. /**
  373. * Destroy thread environment
  374. */
  375. WASM_RUNTIME_API_EXTERN void
  376. wasm_runtime_destroy_thread_env(void);
  377. /**
  378. * Get WASM module instance from execution environment
  379. *
  380. * @param exec_env the execution environment to retrieve
  381. *
  382. * @return the WASM module instance
  383. */
  384. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  385. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  386. /**
  387. * Call the given WASM function of a WASM module instance with
  388. * arguments (bytecode and AoT).
  389. *
  390. * @param exec_env the execution environment to call the function,
  391. * which must be created from wasm_create_exec_env()
  392. * @param function the function to call
  393. * @param argc the number of arguments
  394. * @param argv the arguments. If the function has return value,
  395. * the first (or first two in case 64-bit return value) element of
  396. * argv stores the return value of the called WASM function after this
  397. * function returns.
  398. *
  399. * @return true if success, false otherwise and exception will be thrown,
  400. * the caller can call wasm_runtime_get_exception to get the exception
  401. * info.
  402. */
  403. WASM_RUNTIME_API_EXTERN bool
  404. wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
  405. wasm_function_inst_t function,
  406. uint32_t argc, uint32_t argv[]);
  407. /**
  408. * Call the given WASM function of a WASM module instance with
  409. * provided results space and arguments (bytecode and AoT).
  410. *
  411. * @param exec_env the execution environment to call the function,
  412. * which must be created from wasm_create_exec_env()
  413. * @param function the function to call
  414. * @param num_results the number of results
  415. * @param results the pre-alloced pointer to get the results
  416. * @param num_args the number of arguments
  417. * @param args the arguments
  418. *
  419. * @return true if success, false otherwise and exception will be thrown,
  420. * the caller can call wasm_runtime_get_exception to get the exception
  421. * info.
  422. */
  423. WASM_RUNTIME_API_EXTERN bool
  424. wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
  425. wasm_function_inst_t function,
  426. uint32_t num_results, wasm_val_t results[],
  427. uint32_t num_args, wasm_val_t *args);
  428. /**
  429. * Call the given WASM function of a WASM module instance with
  430. * provided results space and variant arguments (bytecode and AoT).
  431. *
  432. * @param exec_env the execution environment to call the function,
  433. * which must be created from wasm_create_exec_env()
  434. * @param function the function to call
  435. * @param num_results the number of results
  436. * @param results the pre-alloced pointer to get the results
  437. * @param num_args the number of arguments
  438. * @param ... the variant arguments
  439. *
  440. * @return true if success, false otherwise and exception will be thrown,
  441. * the caller can call wasm_runtime_get_exception to get the exception
  442. * info.
  443. */
  444. WASM_RUNTIME_API_EXTERN bool
  445. wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
  446. wasm_function_inst_t function,
  447. uint32_t num_results, wasm_val_t results[],
  448. uint32_t num_args, ...);
  449. /**
  450. * Find the unique main function from a WASM module instance
  451. * and execute that function.
  452. *
  453. * @param module_inst the WASM module instance
  454. * @param argc the number of arguments
  455. * @param argv the arguments array, if the main function has return value,
  456. * *(int*)argv stores the return value of the called main function after
  457. * this function returns.
  458. *
  459. * @return true if the main function is called, false otherwise and exception
  460. * will be thrown, the caller can call wasm_runtime_get_exception to get
  461. * the exception info.
  462. */
  463. WASM_RUNTIME_API_EXTERN bool
  464. wasm_application_execute_main(wasm_module_inst_t module_inst,
  465. int32_t argc, char *argv[]);
  466. /**
  467. * Find the specified function in argv[0] from a WASM module instance
  468. * and execute that function.
  469. *
  470. * @param module_inst the WASM module instance
  471. * @param name the name of the function to execute.
  472. * to indicate the module name via: $module_name$function_name
  473. * or just a function name: function_name
  474. * @param argc the number of arguments
  475. * @param argv the arguments array
  476. *
  477. * @return true if the specified function is called, false otherwise and
  478. * exception will be thrown, the caller can call wasm_runtime_get_exception
  479. * to get the exception info.
  480. */
  481. WASM_RUNTIME_API_EXTERN bool
  482. wasm_application_execute_func(wasm_module_inst_t module_inst,
  483. const char *name, int32_t argc, char *argv[]);
  484. /**
  485. * Get exception info of the WASM module instance.
  486. *
  487. * @param module_inst the WASM module instance
  488. *
  489. * @return the exception string
  490. */
  491. WASM_RUNTIME_API_EXTERN const char *
  492. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  493. /**
  494. * Set exception info of the WASM module instance.
  495. *
  496. * @param module_inst the WASM module instance
  497. *
  498. * @param exception the exception string
  499. */
  500. WASM_RUNTIME_API_EXTERN void
  501. wasm_runtime_set_exception(wasm_module_inst_t module_inst,
  502. const char *exception);
  503. /**
  504. * Clear exception info of the WASM module instance.
  505. *
  506. * @param module_inst the WASM module instance
  507. */
  508. WASM_RUNTIME_API_EXTERN void
  509. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  510. /**
  511. * Set custom data to WASM module instance.
  512. * Note:
  513. * If WAMR_BUILD_LIB_PTHREAD is enabled, this API
  514. * will spread the custom data to all threads
  515. *
  516. * @param module_inst the WASM module instance
  517. * @param custom_data the custom data to be set
  518. */
  519. WASM_RUNTIME_API_EXTERN void
  520. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  521. void *custom_data);
  522. /**
  523. * Get the custom data within a WASM module instance.
  524. *
  525. * @param module_inst the WASM module instance
  526. *
  527. * @return the custom data (NULL if not set yet)
  528. */
  529. WASM_RUNTIME_API_EXTERN void *
  530. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  531. /**
  532. * Allocate memory from the heap of WASM module instance
  533. *
  534. * @param module_inst the WASM module instance which contains heap
  535. * @param size the size bytes to allocate
  536. * @param p_native_addr return native address of the allocated memory
  537. * if it is not NULL, and return NULL if memory malloc failed
  538. *
  539. * @return the allocated memory address, which is a relative offset to the
  540. * base address of the module instance's memory space. Note that
  541. * it is not an absolute address.
  542. * Return non-zero if success, zero if failed.
  543. */
  544. WASM_RUNTIME_API_EXTERN uint32_t
  545. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size,
  546. void **p_native_addr);
  547. /**
  548. * Free memory to the heap of WASM module instance
  549. *
  550. * @param module_inst the WASM module instance which contains heap
  551. * @param ptr the pointer to free
  552. */
  553. WASM_RUNTIME_API_EXTERN void
  554. wasm_runtime_module_free(wasm_module_inst_t module_inst, uint32_t ptr);
  555. /**
  556. * Allocate memory from the heap of WASM module instance and initialize
  557. * the memory with src
  558. *
  559. * @param module_inst the WASM module instance which contains heap
  560. * @param src the source data to copy
  561. * @param size the size of the source data
  562. *
  563. * @return the allocated memory address, which is a relative offset to the
  564. * base address of the module instance's memory space. Note that
  565. * it is not an absolute address.
  566. * Return non-zero if success, zero if failed.
  567. */
  568. WASM_RUNTIME_API_EXTERN uint32_t
  569. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  570. const char *src, uint32_t size);
  571. /**
  572. * Validate the app address, check whether it belongs to WASM module
  573. * instance's address space, or in its heap space or memory space.
  574. *
  575. * @param module_inst the WASM module instance
  576. * @param app_offset the app address to validate, which is a relative address
  577. * @param size the size bytes of the app address
  578. *
  579. * @return true if success, false otherwise. If failed, an exception will
  580. * be thrown.
  581. */
  582. WASM_RUNTIME_API_EXTERN bool
  583. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  584. uint32_t app_offset, uint32_t size);
  585. /**
  586. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  587. * is not provided. This function validates the app string address, check
  588. * whether it belongs to WASM module instance's address space, or in its heap
  589. * space or memory space. Moreover, it checks whether it is the offset of a
  590. * string that is end with '\0'.
  591. *
  592. * @param module_inst the WASM module instance
  593. * @param app_str_offset the app address of the string to validate, which is a
  594. * relative address
  595. *
  596. * @return true if success, false otherwise. If failed, an exception will
  597. * be thrown.
  598. */
  599. WASM_RUNTIME_API_EXTERN bool
  600. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  601. uint32_t app_str_offset);
  602. /**
  603. * Validate the native address, check whether it belongs to WASM module
  604. * instance's address space, or in its heap space or memory space.
  605. *
  606. * @param module_inst the WASM module instance
  607. * @param native_ptr the native address to validate, which is an absolute
  608. * address
  609. * @param size the size bytes of the app address
  610. *
  611. * @return true if success, false otherwise. If failed, an exception will
  612. * be thrown.
  613. */
  614. WASM_RUNTIME_API_EXTERN bool
  615. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  616. void *native_ptr, uint32_t size);
  617. /**
  618. * Convert app address(relative address) to native address(absolute address)
  619. *
  620. * @param module_inst the WASM module instance
  621. * @param app_offset the app adress
  622. *
  623. * @return the native address converted
  624. */
  625. WASM_RUNTIME_API_EXTERN void *
  626. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  627. uint32_t app_offset);
  628. /**
  629. * Convert native address(absolute address) to app address(relative address)
  630. *
  631. * @param module_inst the WASM module instance
  632. * @param native_ptr the native address
  633. *
  634. * @return the app address converted
  635. */
  636. WASM_RUNTIME_API_EXTERN uint32_t
  637. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  638. void *native_ptr);
  639. /**
  640. * Get the app address range (relative address) that a app address belongs to
  641. *
  642. * @param module_inst the WASM module instance
  643. * @param app_offset the app address to retrieve
  644. * @param p_app_start_offset buffer to output the app start offset if not NULL
  645. * @param p_app_end_offset buffer to output the app end offset if not NULL
  646. *
  647. * @return true if success, false otherwise.
  648. */
  649. WASM_RUNTIME_API_EXTERN bool
  650. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  651. uint32_t app_offset,
  652. uint32_t *p_app_start_offset,
  653. uint32_t *p_app_end_offset);
  654. /**
  655. * Get the native address range (absolute address) that a native address
  656. * belongs to
  657. *
  658. * @param module_inst the WASM module instance
  659. * @param native_ptr the native address to retrieve
  660. * @param p_native_start_addr buffer to output the native start address
  661. * if not NULL
  662. * @param p_native_end_addr buffer to output the native end address
  663. * if not NULL
  664. *
  665. * @return true if success, false otherwise.
  666. */
  667. WASM_RUNTIME_API_EXTERN bool
  668. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  669. uint8_t *native_ptr,
  670. uint8_t **p_native_start_addr,
  671. uint8_t **p_native_end_addr);
  672. /**
  673. * Register native functions with same module name
  674. *
  675. * @param module_name the module name of the native functions
  676. * @param native_symbols specifies an array of NativeSymbol structures which
  677. * contain the names, function pointers and signatures
  678. * Note: WASM runtime will not allocate memory to clone the data, so
  679. * user must ensure the array can be used forever
  680. * Meanings of letters in function signature:
  681. * 'i': the parameter is i32 type
  682. * 'I': the parameter is i64 type
  683. * 'f': the parameter is f32 type
  684. * 'F': the parameter is f64 type
  685. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  686. * auto check its boundary before calling the native function.
  687. * If it is followed by '~', the checked length of the pointer
  688. * is gotten from the following parameter, if not, the checked
  689. * length of the pointer is 1.
  690. * '~': the parameter is the pointer's length with i32 type, and must
  691. * follow after '*'
  692. * '$': the parameter is a string (i32 in WASM), and runtime will
  693. * auto check its boundary before calling the native function
  694. * @param n_native_symbols specifies the number of native symbols in the array
  695. *
  696. * @return true if success, false otherwise
  697. */
  698. WASM_RUNTIME_API_EXTERN bool
  699. wasm_runtime_register_natives(const char *module_name,
  700. NativeSymbol *native_symbols,
  701. uint32_t n_native_symbols);
  702. /**
  703. * Register native functions with same module name, similar to
  704. * wasm_runtime_register_natives, the difference is that runtime passes raw
  705. * arguments to native API, which means that the native API should be defined as
  706. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  707. * and native API should extract arguments one by one from args array with macro
  708. * native_raw_get_arg
  709. * and write the return value back to args[0] with macro
  710. * native_raw_return_type and native_raw_set_return
  711. */
  712. WASM_RUNTIME_API_EXTERN bool
  713. wasm_runtime_register_natives_raw(const char *module_name,
  714. NativeSymbol *native_symbols,
  715. uint32_t n_native_symbols);
  716. /**
  717. * Get attachment of native function from execution environment
  718. *
  719. * @param exec_env the execution environment to retrieve
  720. *
  721. * @return the attachment of native function
  722. */
  723. WASM_RUNTIME_API_EXTERN void *
  724. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  725. /**
  726. * Set user data to execution environment.
  727. *
  728. * @param exec_env the execution environment
  729. * @param user_data the user data to be set
  730. */
  731. WASM_RUNTIME_API_EXTERN void
  732. wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data);
  733. /**
  734. * Get the user data within execution environment.
  735. *
  736. * @param exec_env the execution environment
  737. *
  738. * @return the user data (NULL if not set yet)
  739. */
  740. WASM_RUNTIME_API_EXTERN void *
  741. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  742. /**
  743. * Dump runtime memory consumption, including:
  744. * Exec env memory consumption
  745. * WASM module memory consumption
  746. * WASM module instance memory consumption
  747. * stack and app heap used info
  748. *
  749. * @param exec_env the execution environment
  750. */
  751. WASM_RUNTIME_API_EXTERN void
  752. wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env);
  753. /**
  754. * Dump runtime performance profiler data of each function
  755. *
  756. * @param module_inst the WASM module instance to profile
  757. */
  758. WASM_RUNTIME_API_EXTERN void
  759. wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
  760. /* wasm thread callback function type */
  761. typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *);
  762. /* wasm thread type */
  763. typedef uintptr_t wasm_thread_t;
  764. /**
  765. * Set the max thread num per cluster.
  766. *
  767. * @param num maximum thread num
  768. */
  769. WASM_RUNTIME_API_EXTERN void
  770. wasm_runtime_set_max_thread_num(uint32_t num);
  771. /**
  772. * spawn a new exec_env, the spawned exec_env
  773. * can be used in other threads
  774. *
  775. * @param num the original exec_env
  776. *
  777. * @return the spawned exec_env if success, NULL otherwise
  778. */
  779. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  780. wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
  781. /**
  782. * Destroy the spawned exec_env
  783. *
  784. * @param exec_env the spawned exec_env
  785. */
  786. WASM_RUNTIME_API_EXTERN void
  787. wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
  788. /**
  789. * spawn a thread from the given exec_env
  790. *
  791. * @param exec_env the original exec_env
  792. * @param tid thread id to be returned to the caller
  793. * @param callback the callback function provided by the user
  794. * @param arg the arguments passed to the callback
  795. *
  796. * @return 0 if success, -1 otherwise
  797. */
  798. WASM_RUNTIME_API_EXTERN int32_t
  799. wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
  800. wasm_thread_callback_t callback, void *arg);
  801. /**
  802. * waits a spawned thread to terminate
  803. *
  804. * @param tid thread id
  805. * @param retval if not NULL, output the return value of the thread
  806. *
  807. * @return 0 if success, error number otherwise
  808. */
  809. WASM_RUNTIME_API_EXTERN int32_t
  810. wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
  811. /**
  812. * Map external object to an internal externref index: if the index
  813. * has been created, return it, otherwise create the index.
  814. *
  815. * @param module_inst the WASM module instance that the extern object
  816. * belongs to
  817. * @param extern_obj the external object to be mapped
  818. * @param p_externref_idx return externref index of the external object
  819. *
  820. * @return true if success, false otherwise
  821. */
  822. WASM_RUNTIME_API_EXTERN bool
  823. wasm_externref_obj2ref(wasm_module_inst_t module_inst,
  824. void *extern_obj, uint32_t *p_externref_idx);
  825. /**
  826. * Retrieve the external object from an internal externref index
  827. *
  828. * @param externref_idx the externref index to retrieve
  829. * @param p_extern_obj return the mapped external object of
  830. * the externref index
  831. *
  832. * @return true if success, false otherwise
  833. */
  834. WASM_RUNTIME_API_EXTERN bool
  835. wasm_externref_ref2obj(uint32_t externref_idx, void **p_extern_obj);
  836. /**
  837. * Retain an extern object which is mapped to the internal externref
  838. * so that the object won't be cleaned during extern object reclaim
  839. * if it isn't used.
  840. *
  841. * @param externref_idx the externref index of an external object
  842. * to retain
  843. * @return true if success, false otherwise
  844. */
  845. WASM_RUNTIME_API_EXTERN bool
  846. wasm_externref_retain(uint32_t externref_idx);
  847. /**
  848. * dump the call stack
  849. *
  850. * @param exec_env the execution environment
  851. */
  852. WASM_RUNTIME_API_EXTERN void
  853. wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
  854. /* clang-format on */
  855. #ifdef __cplusplus
  856. }
  857. #endif
  858. #endif /* end of _WASM_EXPORT_H */