wasm_export.h 33 KB

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