wasm_export.h 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. /**
  312. * Set WASI parameters.
  313. *
  314. * While this API operates on a module, these parameters will be used
  315. * only when the module is instantiated. That is, you can consider these
  316. * as extra parameters for wasm_runtime_instantiate().
  317. *
  318. * @param module The module to set WASI parameters.
  319. * @param dir_list The list of directories to preopen. (real path)
  320. * @param dir_count The number of elements in dir_list.
  321. * @param map_dir_list The list of directories to preopen. (mapped path)
  322. * @param map_dir_count The number of elements in map_dir_list.
  323. * If map_dir_count is smaller than dir_count,
  324. * mapped path is assumed to be same as the
  325. * corresponding real path for the rest of entries.
  326. * @param env The list of environment variables.
  327. * @param env_count The number of elements in env.
  328. * @param argv The list of command line arguments.
  329. * @param argc The number of elements in argv.
  330. * @param stdinfd The host file descriptor to back WASI STDIN_FILENO.
  331. * If -1 is specified, STDIN_FILENO is used.
  332. * @param stdoutfd The host file descriptor to back WASI STDOUT_FILENO.
  333. * If -1 is specified, STDOUT_FILENO is used.
  334. * @param stderrfd The host file descriptor to back WASI STDERR_FILENO.
  335. * If -1 is specified, STDERR_FILENO is used.
  336. */
  337. WASM_RUNTIME_API_EXTERN void
  338. wasm_runtime_set_wasi_args_ex(wasm_module_t module,
  339. const char *dir_list[], uint32_t dir_count,
  340. const char *map_dir_list[], uint32_t map_dir_count,
  341. const char *env[], uint32_t env_count,
  342. char *argv[], int argc,
  343. int stdinfd, int stdoutfd, int stderrfd);
  344. /**
  345. * Set WASI parameters.
  346. *
  347. * Same as wasm_runtime_set_wasi_args_ex with stdinfd = -1, stdoutfd = -1,
  348. * stderrfd = -1.
  349. */
  350. WASM_RUNTIME_API_EXTERN void
  351. wasm_runtime_set_wasi_args(wasm_module_t module,
  352. const char *dir_list[], uint32_t dir_count,
  353. const char *map_dir_list[], uint32_t map_dir_count,
  354. const char *env[], uint32_t env_count,
  355. char *argv[], int argc);
  356. WASM_RUNTIME_API_EXTERN void
  357. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  358. uint32_t addr_pool_size);
  359. WASM_RUNTIME_API_EXTERN void
  360. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module, const char *ns_lookup_pool[],
  361. uint32_t ns_lookup_pool_size);
  362. /**
  363. * Instantiate a WASM module.
  364. *
  365. * @param module the WASM module to instantiate
  366. * @param stack_size the default stack size of the module instance when the
  367. * exec env's operation stack isn't created by user, e.g. API
  368. * wasm_application_execute_main() and wasm_application_execute_func()
  369. * create the operation stack internally with the stack size specified
  370. * here. And API wasm_runtime_create_exec_env() creates the operation
  371. * stack with stack size specified by its parameter, the stack size
  372. * specified here is ignored.
  373. * @param heap_size the default heap size of the module instance, a heap will
  374. * be created besides the app memory space. Both wasm app and native
  375. * function can allocate memory from the heap.
  376. * @param error_buf buffer to output the error info if failed
  377. * @param error_buf_size the size of the error buffer
  378. *
  379. * @return return the instantiated WASM module instance, NULL if failed
  380. */
  381. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  382. wasm_runtime_instantiate(const wasm_module_t module,
  383. uint32_t stack_size, uint32_t heap_size,
  384. char *error_buf, uint32_t error_buf_size);
  385. /**
  386. * Deinstantiate a WASM module instance, destroy the resources.
  387. *
  388. * @param module_inst the WASM module instance to destroy
  389. */
  390. WASM_RUNTIME_API_EXTERN void
  391. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  392. WASM_RUNTIME_API_EXTERN bool
  393. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  394. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  395. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  396. /**
  397. * Lookup an exported function in the WASM module instance.
  398. *
  399. * @param module_inst the module instance
  400. * @param name the name of the function
  401. * @param signature the signature of the function, ignored currently
  402. *
  403. * @return the function instance found, NULL if not found
  404. */
  405. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  406. wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
  407. const char *name, const char *signature);
  408. /**
  409. * Get parameter count of the function instance
  410. *
  411. * @param func_inst the function instance
  412. * @param module_inst the module instance the function instance belongs to
  413. *
  414. * @return the parameter count of the function instance
  415. */
  416. WASM_RUNTIME_API_EXTERN uint32_t
  417. wasm_func_get_param_count(wasm_function_inst_t const func_inst,
  418. wasm_module_inst_t const module_inst);
  419. /**
  420. * Get result count of the function instance
  421. *
  422. * @param func_inst the function instance
  423. * @param module_inst the module instance the function instance belongs to
  424. *
  425. * @return the result count of the function instance
  426. */
  427. WASM_RUNTIME_API_EXTERN uint32_t
  428. wasm_func_get_result_count(wasm_function_inst_t const func_inst,
  429. wasm_module_inst_t const module_inst);
  430. /**
  431. * Get parameter types of the function instance
  432. *
  433. * @param func_inst the function instance
  434. * @param module_inst the module instance the function instance belongs to
  435. * @param param_types the parameter types returned
  436. */
  437. WASM_RUNTIME_API_EXTERN void
  438. wasm_func_get_param_types(wasm_function_inst_t const func_inst,
  439. wasm_module_inst_t const module_inst,
  440. wasm_valkind_t *param_types);
  441. /**
  442. * Get result types of the function instance
  443. *
  444. * @param func_inst the function instance
  445. * @param module_inst the module instance the function instance belongs to
  446. * @param result_types the result types returned
  447. */
  448. WASM_RUNTIME_API_EXTERN void
  449. wasm_func_get_result_types(wasm_function_inst_t const func_inst,
  450. wasm_module_inst_t const module_inst,
  451. wasm_valkind_t *result_types);
  452. /**
  453. * Create execution environment for a WASM module instance.
  454. *
  455. * @param module_inst the module instance
  456. * @param stack_size the stack size to execute a WASM function
  457. *
  458. * @return the execution environment, NULL if failed, e.g. invalid
  459. * stack size is passed
  460. */
  461. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  462. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  463. uint32_t stack_size);
  464. /**
  465. * Destroy the execution environment.
  466. *
  467. * @param exec_env the execution environment to destroy
  468. */
  469. WASM_RUNTIME_API_EXTERN void
  470. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  471. /**
  472. * Get the singleton execution environment for the instance.
  473. *
  474. * Note: The singleton execution environment is the execution
  475. * environment used internally by the runtime for the API functions
  476. * like wasm_application_execute_main, which don't take explicit
  477. * execution environment. It's associated to the corresponding
  478. * module instance and managed by the runtime. The API user should
  479. * not destroy it with wasm_runtime_destroy_exec_env.
  480. *
  481. * @param module_inst the module instance
  482. *
  483. * @return exec_env the execution environment to destroy
  484. */
  485. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  486. wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
  487. /**
  488. * Start debug instance based on given execution environment.
  489. * Note:
  490. * The debug instance will be destroyed during destroying the
  491. * execution environment, developers don't need to destroy it
  492. * manually.
  493. * If the cluster of this execution environment has already
  494. * been bound to a debug instance, this function will return true
  495. * directly.
  496. * If developer spawns some exec_env by wasm_runtime_spawn_exec_env,
  497. * don't need to call this function for every spawned exec_env as
  498. * they are sharing the same cluster with the main exec_env.
  499. *
  500. * @param exec_env the execution environment to start debug instance
  501. * @param port the port for the debug server to listen on.
  502. * 0 means automatic assignment.
  503. * -1 means to use the global setting in RuntimeInitArgs.
  504. *
  505. * @return debug port if success, 0 otherwise.
  506. */
  507. WASM_RUNTIME_API_EXTERN uint32_t
  508. wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env, int32_t port);
  509. /**
  510. * Same as wasm_runtime_start_debug_instance_with_port(env, -1).
  511. */
  512. WASM_RUNTIME_API_EXTERN uint32_t
  513. wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
  514. /**
  515. * Initialize the thread environment.
  516. * Note:
  517. * If developer creates a child thread by himself to call the
  518. * the wasm function in that thread, he should call this API
  519. * firstly before calling the wasm function and then call
  520. * wasm_runtime_destroy_thread_env() after calling the wasm
  521. * function. If the thread is created from the runtime API,
  522. * it is unnecessary to call these two APIs.
  523. *
  524. * @return true if success, false otherwise
  525. */
  526. WASM_RUNTIME_API_EXTERN bool
  527. wasm_runtime_init_thread_env(void);
  528. /**
  529. * Destroy the thread environment
  530. */
  531. WASM_RUNTIME_API_EXTERN void
  532. wasm_runtime_destroy_thread_env(void);
  533. /**
  534. * Whether the thread environment is initialized
  535. */
  536. WASM_RUNTIME_API_EXTERN bool
  537. wasm_runtime_thread_env_inited(void);
  538. /**
  539. * Get WASM module instance from execution environment
  540. *
  541. * @param exec_env the execution environment to retrieve
  542. *
  543. * @return the WASM module instance
  544. */
  545. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  546. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  547. /**
  548. * Set WASM module instance of execution environment
  549. * Caution:
  550. * normally the module instance is bound with the execution
  551. * environment one by one, if multiple module instances want
  552. * to share to the same execution environment, developer should
  553. * be responsible for the backup and restore of module instance
  554. *
  555. * @param exec_env the execution environment
  556. * @param module_inst the WASM module instance to set
  557. */
  558. WASM_RUNTIME_API_EXTERN void
  559. wasm_runtime_set_module_inst(wasm_exec_env_t exec_env,
  560. const wasm_module_inst_t module_inst);
  561. /**
  562. * Call the given WASM function of a WASM module instance with
  563. * arguments (bytecode and AoT).
  564. *
  565. * @param exec_env the execution environment to call the function,
  566. * which must be created from wasm_create_exec_env()
  567. * @param function the function to call
  568. * @param argc total cell number that the function parameters occupy,
  569. * a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument
  570. * occupies one cell, i64/f64 argument occupies two cells, note that
  571. * it might be different from the parameter number of the function
  572. * @param argv the arguments. If the function has return value,
  573. * the first (or first two in case 64-bit return value) element of
  574. * argv stores the return value of the called WASM function after this
  575. * function returns.
  576. *
  577. * @return true if success, false otherwise and exception will be thrown,
  578. * the caller can call wasm_runtime_get_exception to get the exception
  579. * info.
  580. */
  581. WASM_RUNTIME_API_EXTERN bool
  582. wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
  583. wasm_function_inst_t function,
  584. uint32_t argc, uint32_t argv[]);
  585. /**
  586. * Call the given WASM function of a WASM module instance with
  587. * provided results space and arguments (bytecode and AoT).
  588. *
  589. * @param exec_env the execution environment to call the function,
  590. * which must be created from wasm_create_exec_env()
  591. * @param function the function to call
  592. * @param num_results the number of results
  593. * @param results the pre-alloced pointer to get the results
  594. * @param num_args the number of arguments
  595. * @param args the arguments
  596. *
  597. * @return true if success, false otherwise and exception will be thrown,
  598. * the caller can call wasm_runtime_get_exception to get the exception
  599. * info.
  600. */
  601. WASM_RUNTIME_API_EXTERN bool
  602. wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
  603. wasm_function_inst_t function,
  604. uint32_t num_results, wasm_val_t results[],
  605. uint32_t num_args, wasm_val_t *args);
  606. /**
  607. * Call the given WASM function of a WASM module instance with
  608. * provided results space and variant arguments (bytecode and AoT).
  609. *
  610. * @param exec_env the execution environment to call the function,
  611. * which must be created from wasm_create_exec_env()
  612. * @param function the function to call
  613. * @param num_results the number of results
  614. * @param results the pre-alloced pointer to get the results
  615. * @param num_args the number of arguments
  616. * @param ... the variant arguments
  617. *
  618. * @return true if success, false otherwise and exception will be thrown,
  619. * the caller can call wasm_runtime_get_exception to get the exception
  620. * info.
  621. */
  622. WASM_RUNTIME_API_EXTERN bool
  623. wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
  624. wasm_function_inst_t function,
  625. uint32_t num_results, wasm_val_t results[],
  626. uint32_t num_args, ...);
  627. /**
  628. * Find the unique main function from a WASM module instance
  629. * and execute that function.
  630. *
  631. * @param module_inst the WASM module instance
  632. * @param argc the number of arguments
  633. * @param argv the arguments array, if the main function has return value,
  634. * *(int*)argv stores the return value of the called main function after
  635. * this function returns.
  636. *
  637. * @return true if the main function is called, false otherwise and exception
  638. * will be thrown, the caller can call wasm_runtime_get_exception to get
  639. * the exception info.
  640. */
  641. WASM_RUNTIME_API_EXTERN bool
  642. wasm_application_execute_main(wasm_module_inst_t module_inst,
  643. int32_t argc, char *argv[]);
  644. /**
  645. * Find the specified function in argv[0] from a WASM module instance
  646. * and execute that function.
  647. *
  648. * @param module_inst the WASM module instance
  649. * @param name the name of the function to execute.
  650. * to indicate the module name via: $module_name$function_name
  651. * or just a function name: function_name
  652. * @param argc the number of arguments
  653. * @param argv the arguments array
  654. *
  655. * @return true if the specified function is called, false otherwise and
  656. * exception will be thrown, the caller can call wasm_runtime_get_exception
  657. * to get the exception info.
  658. */
  659. WASM_RUNTIME_API_EXTERN bool
  660. wasm_application_execute_func(wasm_module_inst_t module_inst,
  661. const char *name, int32_t argc, char *argv[]);
  662. /**
  663. * Get exception info of the WASM module instance.
  664. *
  665. * @param module_inst the WASM module instance
  666. *
  667. * @return the exception string
  668. */
  669. WASM_RUNTIME_API_EXTERN const char *
  670. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  671. /**
  672. * Set exception info of the WASM module instance.
  673. *
  674. * @param module_inst the WASM module instance
  675. *
  676. * @param exception the exception string
  677. */
  678. WASM_RUNTIME_API_EXTERN void
  679. wasm_runtime_set_exception(wasm_module_inst_t module_inst,
  680. const char *exception);
  681. /**
  682. * Clear exception info of the WASM module instance.
  683. *
  684. * @param module_inst the WASM module instance
  685. */
  686. WASM_RUNTIME_API_EXTERN void
  687. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  688. /**
  689. * Set custom data to WASM module instance.
  690. * Note:
  691. * If WAMR_BUILD_LIB_PTHREAD is enabled, this API
  692. * will spread the custom data to all threads
  693. *
  694. * @param module_inst the WASM module instance
  695. * @param custom_data the custom data to be set
  696. */
  697. WASM_RUNTIME_API_EXTERN void
  698. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  699. void *custom_data);
  700. /**
  701. * Get the custom data within a WASM module instance.
  702. *
  703. * @param module_inst the WASM module instance
  704. *
  705. * @return the custom data (NULL if not set yet)
  706. */
  707. WASM_RUNTIME_API_EXTERN void *
  708. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  709. /**
  710. * Allocate memory from the heap of WASM module instance
  711. *
  712. * Note: wasm_runtime_module_malloc can call heap functions inside
  713. * the module instance and thus cause a memory growth.
  714. * This API needs to be used very carefully when you have a native
  715. * pointers to the module instance memory obtained with
  716. * wasm_runtime_addr_app_to_native or similar APIs.
  717. *
  718. * @param module_inst the WASM module instance which contains heap
  719. * @param size the size bytes to allocate
  720. * @param p_native_addr return native address of the allocated memory
  721. * if it is not NULL, and return NULL if memory malloc failed
  722. *
  723. * @return the allocated memory address, which is a relative offset to the
  724. * base address of the module instance's memory space. Note that
  725. * it is not an absolute address.
  726. * Return non-zero if success, zero if failed.
  727. */
  728. WASM_RUNTIME_API_EXTERN uint32_t
  729. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size,
  730. void **p_native_addr);
  731. /**
  732. * Free memory to the heap of WASM module instance
  733. *
  734. * @param module_inst the WASM module instance which contains heap
  735. * @param ptr the pointer to free
  736. */
  737. WASM_RUNTIME_API_EXTERN void
  738. wasm_runtime_module_free(wasm_module_inst_t module_inst, uint32_t ptr);
  739. /**
  740. * Allocate memory from the heap of WASM module instance and initialize
  741. * the memory with src
  742. *
  743. * @param module_inst the WASM module instance which contains heap
  744. * @param src the source data to copy
  745. * @param size the size of the source data
  746. *
  747. * @return the allocated memory address, which is a relative offset to the
  748. * base address of the module instance's memory space. Note that
  749. * it is not an absolute address.
  750. * Return non-zero if success, zero if failed.
  751. */
  752. WASM_RUNTIME_API_EXTERN uint32_t
  753. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  754. const char *src, uint32_t size);
  755. /**
  756. * Validate the app 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 app_offset the app address to validate, which is a relative address
  761. * @param size the size bytes of the app address
  762. *
  763. * @return true if success, false otherwise. If failed, an exception will
  764. * be thrown.
  765. */
  766. WASM_RUNTIME_API_EXTERN bool
  767. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  768. uint32_t app_offset, uint32_t size);
  769. /**
  770. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  771. * is not provided. This function validates the app string address, check
  772. * whether it belongs to WASM module instance's address space, or in its heap
  773. * space or memory space. Moreover, it checks whether it is the offset of a
  774. * string that is end with '\0'.
  775. *
  776. * Note: The validation result, especially the NUL termination check,
  777. * is not reliable for a module instance with multiple threads because
  778. * other threads can modify the heap behind us.
  779. *
  780. * @param module_inst the WASM module instance
  781. * @param app_str_offset the app address of the string to validate, which is a
  782. * relative address
  783. *
  784. * @return true if success, false otherwise. If failed, an exception will
  785. * be thrown.
  786. */
  787. WASM_RUNTIME_API_EXTERN bool
  788. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  789. uint32_t app_str_offset);
  790. /**
  791. * Validate the native address, check whether it belongs to WASM module
  792. * instance's address space, or in its heap space or memory space.
  793. *
  794. * @param module_inst the WASM module instance
  795. * @param native_ptr the native address to validate, which is an absolute
  796. * address
  797. * @param size the size bytes of the app address
  798. *
  799. * @return true if success, false otherwise. If failed, an exception will
  800. * be thrown.
  801. */
  802. WASM_RUNTIME_API_EXTERN bool
  803. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  804. void *native_ptr, uint32_t size);
  805. /**
  806. * Convert app address(relative address) to native address(absolute address)
  807. *
  808. * Note that native addresses to module instance memory can be invalidated
  809. * on a memory growth. (Except shared memory, whose native addresses are
  810. * stable.)
  811. *
  812. * @param module_inst the WASM module instance
  813. * @param app_offset the app adress
  814. *
  815. * @return the native address converted
  816. */
  817. WASM_RUNTIME_API_EXTERN void *
  818. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  819. uint32_t app_offset);
  820. /**
  821. * Convert native address(absolute address) to app address(relative address)
  822. *
  823. * @param module_inst the WASM module instance
  824. * @param native_ptr the native address
  825. *
  826. * @return the app address converted
  827. */
  828. WASM_RUNTIME_API_EXTERN uint32_t
  829. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  830. void *native_ptr);
  831. /**
  832. * Get the app address range (relative address) that a app address belongs to
  833. *
  834. * @param module_inst the WASM module instance
  835. * @param app_offset the app address to retrieve
  836. * @param p_app_start_offset buffer to output the app start offset if not NULL
  837. * @param p_app_end_offset buffer to output the app end offset if not NULL
  838. *
  839. * @return true if success, false otherwise.
  840. */
  841. WASM_RUNTIME_API_EXTERN bool
  842. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  843. uint32_t app_offset,
  844. uint32_t *p_app_start_offset,
  845. uint32_t *p_app_end_offset);
  846. /**
  847. * Get the native address range (absolute address) that a native address
  848. * belongs to
  849. *
  850. * @param module_inst the WASM module instance
  851. * @param native_ptr the native address to retrieve
  852. * @param p_native_start_addr buffer to output the native start address
  853. * if not NULL
  854. * @param p_native_end_addr buffer to output the native end address
  855. * if not NULL
  856. *
  857. * @return true if success, false otherwise.
  858. */
  859. WASM_RUNTIME_API_EXTERN bool
  860. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  861. uint8_t *native_ptr,
  862. uint8_t **p_native_start_addr,
  863. uint8_t **p_native_end_addr);
  864. /**
  865. * Register native functions with same module name
  866. *
  867. * Note: The array `native_symbols` should not be read-only because the
  868. * library can modify it in-place.
  869. *
  870. * Note: After successful call of this function, the array `native_symbols`
  871. * is owned by the library.
  872. *
  873. * @param module_name the module name of the native functions
  874. * @param native_symbols specifies an array of NativeSymbol structures which
  875. * contain the names, function pointers and signatures
  876. * Note: WASM runtime will not allocate memory to clone the data, so
  877. * user must ensure the array can be used forever
  878. * Meanings of letters in function signature:
  879. * 'i': the parameter is i32 type
  880. * 'I': the parameter is i64 type
  881. * 'f': the parameter is f32 type
  882. * 'F': the parameter is f64 type
  883. * 'r': the parameter is externref type, it should be a uintptr_t in host
  884. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  885. * auto check its boundary before calling the native function.
  886. * If it is followed by '~', the checked length of the pointer
  887. * is gotten from the following parameter, if not, the checked
  888. * length of the pointer is 1.
  889. * '~': the parameter is the pointer's length with i32 type, and must
  890. * follow after '*'
  891. * '$': the parameter is a string (i32 in WASM), and runtime will
  892. * auto check its boundary before calling the native function
  893. * @param n_native_symbols specifies the number of native symbols in the array
  894. *
  895. * @return true if success, false otherwise
  896. */
  897. WASM_RUNTIME_API_EXTERN bool
  898. wasm_runtime_register_natives(const char *module_name,
  899. NativeSymbol *native_symbols,
  900. uint32_t n_native_symbols);
  901. /**
  902. * Register native functions with same module name, similar to
  903. * wasm_runtime_register_natives, the difference is that runtime passes raw
  904. * arguments to native API, which means that the native API should be defined as
  905. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  906. * and native API should extract arguments one by one from args array with macro
  907. * native_raw_get_arg
  908. * and write the return value back to args[0] with macro
  909. * native_raw_return_type and native_raw_set_return
  910. */
  911. WASM_RUNTIME_API_EXTERN bool
  912. wasm_runtime_register_natives_raw(const char *module_name,
  913. NativeSymbol *native_symbols,
  914. uint32_t n_native_symbols);
  915. /**
  916. * Undo wasm_runtime_register_natives or wasm_runtime_register_natives_raw
  917. *
  918. * @param module_name Should be the same as the corresponding
  919. * wasm_runtime_register_natives.
  920. * (Same in term of strcmp.)
  921. *
  922. * @param native_symbols Should be the same as the corresponding
  923. * wasm_runtime_register_natives.
  924. * (Same in term of pointer comparison.)
  925. *
  926. * @return true if success, false otherwise
  927. */
  928. WASM_RUNTIME_API_EXTERN bool
  929. wasm_runtime_unregister_natives(const char *module_name,
  930. NativeSymbol *native_symbols);
  931. /**
  932. * Get attachment of native function from execution environment
  933. *
  934. * @param exec_env the execution environment to retrieve
  935. *
  936. * @return the attachment of native function
  937. */
  938. WASM_RUNTIME_API_EXTERN void *
  939. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  940. /**
  941. * Set user data to execution environment.
  942. *
  943. * @param exec_env the execution environment
  944. * @param user_data the user data to be set
  945. */
  946. WASM_RUNTIME_API_EXTERN void
  947. wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data);
  948. /**
  949. * Get the user data within execution environment.
  950. *
  951. * @param exec_env the execution environment
  952. *
  953. * @return the user data (NULL if not set yet)
  954. */
  955. WASM_RUNTIME_API_EXTERN void *
  956. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  957. /**
  958. * Dump runtime memory consumption, including:
  959. * Exec env memory consumption
  960. * WASM module memory consumption
  961. * WASM module instance memory consumption
  962. * stack and app heap used info
  963. *
  964. * @param exec_env the execution environment
  965. */
  966. WASM_RUNTIME_API_EXTERN void
  967. wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env);
  968. /**
  969. * Dump runtime performance profiler data of each function
  970. *
  971. * @param module_inst the WASM module instance to profile
  972. */
  973. WASM_RUNTIME_API_EXTERN void
  974. wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
  975. /* wasm thread callback function type */
  976. typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *);
  977. /* wasm thread type */
  978. typedef uintptr_t wasm_thread_t;
  979. /**
  980. * Set the max thread num per cluster.
  981. *
  982. * @param num maximum thread num
  983. */
  984. WASM_RUNTIME_API_EXTERN void
  985. wasm_runtime_set_max_thread_num(uint32_t num);
  986. /**
  987. * Spawn a new exec_env, the spawned exec_env
  988. * can be used in other threads
  989. *
  990. * @param num the original exec_env
  991. *
  992. * @return the spawned exec_env if success, NULL otherwise
  993. */
  994. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  995. wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
  996. /**
  997. * Destroy the spawned exec_env
  998. *
  999. * @param exec_env the spawned exec_env
  1000. */
  1001. WASM_RUNTIME_API_EXTERN void
  1002. wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
  1003. /**
  1004. * Spawn a thread from the given exec_env
  1005. *
  1006. * @param exec_env the original exec_env
  1007. * @param tid thread id to be returned to the caller
  1008. * @param callback the callback function provided by the user
  1009. * @param arg the arguments passed to the callback
  1010. *
  1011. * @return 0 if success, -1 otherwise
  1012. */
  1013. WASM_RUNTIME_API_EXTERN int32_t
  1014. wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
  1015. wasm_thread_callback_t callback, void *arg);
  1016. /**
  1017. * Wait a spawned thread to terminate
  1018. *
  1019. * @param tid thread id
  1020. * @param retval if not NULL, output the return value of the thread
  1021. *
  1022. * @return 0 if success, error number otherwise
  1023. */
  1024. WASM_RUNTIME_API_EXTERN int32_t
  1025. wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
  1026. /**
  1027. * Map external object to an internal externref index: if the index
  1028. * has been created, return it, otherwise create the index.
  1029. *
  1030. * @param module_inst the WASM module instance that the extern object
  1031. * belongs to
  1032. * @param extern_obj the external object to be mapped
  1033. * @param p_externref_idx return externref index of the external object
  1034. *
  1035. * @return true if success, false otherwise
  1036. */
  1037. WASM_RUNTIME_API_EXTERN bool
  1038. wasm_externref_obj2ref(wasm_module_inst_t module_inst,
  1039. void *extern_obj, uint32_t *p_externref_idx);
  1040. /**
  1041. * Retrieve the external object from an internal externref index
  1042. *
  1043. * @param externref_idx the externref index to retrieve
  1044. * @param p_extern_obj return the mapped external object of
  1045. * the externref index
  1046. *
  1047. * @return true if success, false otherwise
  1048. */
  1049. WASM_RUNTIME_API_EXTERN bool
  1050. wasm_externref_ref2obj(uint32_t externref_idx, void **p_extern_obj);
  1051. /**
  1052. * Retain an extern object which is mapped to the internal externref
  1053. * so that the object won't be cleaned during extern object reclaim
  1054. * if it isn't used.
  1055. *
  1056. * @param externref_idx the externref index of an external object
  1057. * to retain
  1058. * @return true if success, false otherwise
  1059. */
  1060. WASM_RUNTIME_API_EXTERN bool
  1061. wasm_externref_retain(uint32_t externref_idx);
  1062. /**
  1063. * Dump the call stack to stdout
  1064. *
  1065. * @param exec_env the execution environment
  1066. */
  1067. WASM_RUNTIME_API_EXTERN void
  1068. wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
  1069. /**
  1070. * Get the size required to store the call stack contents, including
  1071. * the space for terminating null byte ('\0')
  1072. *
  1073. * @param exec_env the execution environment
  1074. *
  1075. * @return size required to store the contents, 0 means error
  1076. */
  1077. WASM_RUNTIME_API_EXTERN uint32_t
  1078. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env);
  1079. /**
  1080. * Dump the call stack to buffer.
  1081. *
  1082. * @note this function is not thread-safe, please only use this API
  1083. * when the exec_env is not executing
  1084. *
  1085. * @param exec_env the execution environment
  1086. * @param buf buffer to store the dumped content
  1087. * @param len length of the buffer
  1088. *
  1089. * @return bytes dumped to the buffer, including the terminating null
  1090. * byte ('\0'), 0 means error and data in buf may be invalid
  1091. */
  1092. WASM_RUNTIME_API_EXTERN uint32_t
  1093. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  1094. uint32_t len);
  1095. /**
  1096. * Get a custom section by name
  1097. *
  1098. * @param module_comm the module to find
  1099. * @param name name of the custom section
  1100. * @param len return the length of the content if found
  1101. *
  1102. * @return Custom section content (not including the name length
  1103. * and name string) if found, NULL otherwise
  1104. */
  1105. WASM_RUNTIME_API_EXTERN const uint8_t *
  1106. wasm_runtime_get_custom_section(wasm_module_t const module_comm,
  1107. const char *name, uint32_t *len);
  1108. /**
  1109. * Get WAMR semantic version
  1110. */
  1111. WASM_RUNTIME_API_EXTERN void
  1112. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch);
  1113. /* clang-format on */
  1114. #ifdef __cplusplus
  1115. }
  1116. #endif
  1117. #endif /* end of _WASM_EXPORT_H */