wasm_export.h 39 KB

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