wasm_export.h 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_EXPORT_H
  6. #define _WASM_EXPORT_H
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "lib_export.h"
  10. #ifndef WASM_RUNTIME_API_EXTERN
  11. #if defined(_MSC_BUILD)
  12. #if defined(COMPILING_WASM_RUNTIME_API)
  13. #define WASM_RUNTIME_API_EXTERN __declspec(dllexport)
  14. #else
  15. #define WASM_RUNTIME_API_EXTERN __declspec(dllimport)
  16. #endif
  17. #else
  18. #define WASM_RUNTIME_API_EXTERN
  19. #endif
  20. #endif
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* clang-format off */
  25. #define get_module_inst(exec_env) \
  26. wasm_runtime_get_module_inst(exec_env)
  27. #define validate_app_addr(offset, size) \
  28. wasm_runtime_validate_app_addr(module_inst, offset, size)
  29. #define validate_app_str_addr(offset) \
  30. wasm_runtime_validate_app_str_addr(module_inst, offset)
  31. #define addr_app_to_native(offset) \
  32. wasm_runtime_addr_app_to_native(module_inst, offset)
  33. #define addr_native_to_app(ptr) \
  34. wasm_runtime_addr_native_to_app(module_inst, ptr)
  35. #define module_malloc(size, p_native_addr) \
  36. wasm_runtime_module_malloc(module_inst, size, p_native_addr)
  37. #define module_free(offset) \
  38. wasm_runtime_module_free(module_inst, offset)
  39. #define native_raw_return_type(type, args) type *raw_ret = (type *)(args)
  40. #define native_raw_get_arg(type, name, args) type name = *((type *)(args++))
  41. #define native_raw_set_return(val) *raw_ret = (val)
  42. #ifndef WASM_MODULE_T_DEFINED
  43. #define WASM_MODULE_T_DEFINED
  44. /* Uninstantiated WASM module loaded from WASM binary file
  45. or AoT binary file*/
  46. struct WASMModuleCommon;
  47. typedef struct WASMModuleCommon *wasm_module_t;
  48. #endif
  49. /* Instantiated WASM module */
  50. struct WASMModuleInstanceCommon;
  51. typedef struct WASMModuleInstanceCommon *wasm_module_inst_t;
  52. /* Function instance */
  53. typedef void WASMFunctionInstanceCommon;
  54. typedef WASMFunctionInstanceCommon *wasm_function_inst_t;
  55. /* WASM section */
  56. typedef struct wasm_section_t {
  57. struct wasm_section_t *next;
  58. /* section type */
  59. int section_type;
  60. /* section body, not include type and size */
  61. uint8_t *section_body;
  62. /* section body size */
  63. uint32_t section_body_size;
  64. } wasm_section_t, aot_section_t, *wasm_section_list_t, *aot_section_list_t;
  65. /* Execution environment, e.g. stack info */
  66. struct WASMExecEnv;
  67. typedef struct WASMExecEnv *wasm_exec_env_t;
  68. /* Package Type */
  69. typedef enum {
  70. Wasm_Module_Bytecode = 0,
  71. Wasm_Module_AoT,
  72. Package_Type_Unknown = 0xFFFF
  73. } package_type_t;
  74. #ifndef MEM_ALLOC_OPTION_DEFINED
  75. #define MEM_ALLOC_OPTION_DEFINED
  76. /* Memory allocator type */
  77. typedef enum {
  78. /* pool mode, allocate memory from user defined heap buffer */
  79. Alloc_With_Pool = 0,
  80. /* user allocator mode, allocate memory from user defined
  81. malloc function */
  82. Alloc_With_Allocator,
  83. /* system allocator mode, allocate memory from system allocator,
  84. or, platform's os_malloc function */
  85. Alloc_With_System_Allocator,
  86. } mem_alloc_type_t;
  87. /* Memory allocator option */
  88. typedef union MemAllocOption {
  89. struct {
  90. void *heap_buf;
  91. uint32_t heap_size;
  92. } pool;
  93. struct {
  94. void *malloc_func;
  95. void *realloc_func;
  96. void *free_func;
  97. /* allocator user data, only used when
  98. WASM_MEM_ALLOC_WITH_USER_DATA is defined */
  99. void *user_data;
  100. } allocator;
  101. } MemAllocOption;
  102. #endif
  103. /* Memory pool info */
  104. typedef struct mem_alloc_info_t {
  105. uint32_t total_size;
  106. uint32_t total_free_size;
  107. uint32_t highmark_size;
  108. } mem_alloc_info_t;
  109. /* Running mode of runtime and module instance*/
  110. typedef enum RunningMode {
  111. Mode_Interp = 1,
  112. Mode_Fast_JIT,
  113. Mode_LLVM_JIT,
  114. Mode_Multi_Tier_JIT,
  115. } RunningMode;
  116. /* WASM runtime initialize arguments */
  117. typedef struct RuntimeInitArgs {
  118. mem_alloc_type_t mem_alloc_type;
  119. MemAllocOption mem_alloc_option;
  120. const char *native_module_name;
  121. NativeSymbol *native_symbols;
  122. uint32_t n_native_symbols;
  123. /* maximum thread number, only used when
  124. WASM_ENABLE_THREAD_MGR is defined */
  125. uint32_t max_thread_num;
  126. /* Debug settings, only used when
  127. WASM_ENABLE_DEBUG_INTERP != 0 */
  128. char ip_addr[128];
  129. int unused; /* was platform_port */
  130. int instance_port;
  131. /* Fast JIT code cache size */
  132. uint32_t fast_jit_code_cache_size;
  133. /* Default GC heap size */
  134. uint32_t gc_heap_size;
  135. /* Default running mode of the runtime */
  136. RunningMode running_mode;
  137. /* LLVM JIT opt and size level */
  138. uint32_t llvm_jit_opt_level;
  139. uint32_t llvm_jit_size_level;
  140. /* Segue optimization flags for LLVM JIT */
  141. uint32_t segue_flags;
  142. /**
  143. * If enabled
  144. * - llvm-jit will output a jitdump file for `perf inject`
  145. * - aot. TBD
  146. * - fast-jit. TBD
  147. * - multi-tier-jit. TBD
  148. * - interpreter. TBD
  149. */
  150. bool linux_perf_support;
  151. } RuntimeInitArgs;
  152. #ifndef WASM_VALKIND_T_DEFINED
  153. #define WASM_VALKIND_T_DEFINED
  154. typedef uint8_t wasm_valkind_t;
  155. enum wasm_valkind_enum {
  156. WASM_I32,
  157. WASM_I64,
  158. WASM_F32,
  159. WASM_F64,
  160. WASM_ANYREF = 128,
  161. WASM_FUNCREF,
  162. };
  163. #endif
  164. #ifndef WASM_VAL_T_DEFINED
  165. #define WASM_VAL_T_DEFINED
  166. struct wasm_ref_t;
  167. typedef struct wasm_val_t {
  168. wasm_valkind_t kind;
  169. union {
  170. /* also represent a function index */
  171. int32_t i32;
  172. int64_t i64;
  173. float f32;
  174. double f64;
  175. /* represent a foreign object, aka externref in .wat */
  176. uintptr_t foreign;
  177. struct wasm_ref_t *ref;
  178. } of;
  179. } wasm_val_t;
  180. #endif
  181. /**
  182. * Initialize the WASM runtime environment, and also initialize
  183. * the memory allocator with system allocator, which calls os_malloc
  184. * to allocate memory
  185. *
  186. * @return true if success, false otherwise
  187. */
  188. WASM_RUNTIME_API_EXTERN bool
  189. wasm_runtime_init(void);
  190. /**
  191. * Initialize the WASM runtime environment, WASM running mode,
  192. * and also initialize the memory allocator and register native symbols,
  193. * which are specified with init arguments
  194. *
  195. * @param init_args specifies the init arguments
  196. *
  197. * @return return true if success, false otherwise
  198. */
  199. WASM_RUNTIME_API_EXTERN bool
  200. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  201. /**
  202. * Query whether a certain running mode is supported for the runtime
  203. *
  204. * @param running_mode the running mode to query
  205. *
  206. * @return true if this running mode is supported, false otherwise
  207. */
  208. WASM_RUNTIME_API_EXTERN bool
  209. wasm_runtime_is_running_mode_supported(RunningMode running_mode);
  210. /**
  211. * Set the default running mode for the runtime. It is inherited
  212. * to set the running mode of a module instance when it is instantiated,
  213. * and can be changed by calling wasm_runtime_set_running_mode
  214. *
  215. * @param running_mode the running mode to set
  216. *
  217. * @return true if success, false otherwise
  218. */
  219. WASM_RUNTIME_API_EXTERN bool
  220. wasm_runtime_set_default_running_mode(RunningMode running_mode);
  221. /**
  222. * Destroy the WASM runtime environment.
  223. */
  224. WASM_RUNTIME_API_EXTERN void
  225. wasm_runtime_destroy(void);
  226. /**
  227. * Allocate memory from runtime memory environment.
  228. *
  229. * @param size bytes need to allocate
  230. *
  231. * @return the pointer to memory allocated
  232. */
  233. WASM_RUNTIME_API_EXTERN void *
  234. wasm_runtime_malloc(unsigned int size);
  235. /**
  236. * Reallocate memory from runtime memory environment
  237. *
  238. * @param ptr the original memory
  239. * @param size bytes need to reallocate
  240. *
  241. * @return the pointer to memory reallocated
  242. */
  243. WASM_RUNTIME_API_EXTERN void *
  244. wasm_runtime_realloc(void *ptr, unsigned int size);
  245. /*
  246. * Free memory to runtime memory environment.
  247. */
  248. WASM_RUNTIME_API_EXTERN void
  249. wasm_runtime_free(void *ptr);
  250. /*
  251. * Get memory info, only pool mode is supported now.
  252. */
  253. WASM_RUNTIME_API_EXTERN bool
  254. wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info);
  255. /**
  256. * Get the package type of a buffer.
  257. *
  258. * @param buf the package buffer
  259. * @param size the package buffer size
  260. *
  261. * @return the package type, return Package_Type_Unknown if the type is unknown
  262. */
  263. WASM_RUNTIME_API_EXTERN package_type_t
  264. get_package_type(const uint8_t *buf, uint32_t size);
  265. /**
  266. * Check whether a file is an AOT XIP (Execution In Place) file
  267. *
  268. * @param buf the package buffer
  269. * @param size the package buffer size
  270. *
  271. * @return true if success, false otherwise
  272. */
  273. WASM_RUNTIME_API_EXTERN bool
  274. wasm_runtime_is_xip_file(const uint8_t *buf, uint32_t size);
  275. /**
  276. * Callback to load a module file into a buffer in multi-module feature
  277. */
  278. typedef bool (*module_reader)(package_type_t module_type,
  279. const char *module_name,
  280. uint8_t **p_buffer, uint32_t *p_size);
  281. /**
  282. * Callback to release the buffer loaded by module_reader callback
  283. */
  284. typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size);
  285. /**
  286. * Setup callbacks for reading and releasing a buffer about a module file
  287. *
  288. * @param reader a callback to read a module file into a buffer
  289. * @param destroyer a callback to release above buffer
  290. */
  291. WASM_RUNTIME_API_EXTERN void
  292. wasm_runtime_set_module_reader(const module_reader reader,
  293. const module_destroyer destroyer);
  294. /**
  295. * Give the "module" a name "module_name".
  296. * Can not assign a new name to a module if it already has a name
  297. *
  298. * @param module_name indicate a name
  299. * @param module the target module
  300. * @param error_buf output of the exception info
  301. * @param error_buf_size the size of the exception string
  302. *
  303. * @return true means success, false means failed
  304. */
  305. WASM_RUNTIME_API_EXTERN bool
  306. wasm_runtime_register_module(const char *module_name, wasm_module_t module,
  307. char *error_buf, uint32_t error_buf_size);
  308. /**
  309. * Check if there is already a loaded module named module_name in the
  310. * runtime. Repeately loading a module with the same name is not allowed.
  311. *
  312. * @param module_name indicate a name
  313. *
  314. * @return return WASM module loaded, NULL if failed
  315. */
  316. WASM_RUNTIME_API_EXTERN wasm_module_t
  317. wasm_runtime_find_module_registered(const char *module_name);
  318. /**
  319. * Load a WASM module from a specified byte buffer. The byte buffer can be
  320. * WASM binary data when interpreter or JIT is enabled, or AOT binary data
  321. * when AOT is enabled. If it is AOT binary data, it must be 4-byte aligned.
  322. *
  323. * Note: In case of AOT XIP modules, the runtime doesn't make modifications
  324. * to the buffer. (Except the "Known issues" mentioned in doc/xip.md.)
  325. * Otherwise, the runtime can make modifications to the buffer for its
  326. * internal purposes. Thus, in general, it isn't safe to create multiple
  327. * modules from a single buffer.
  328. *
  329. * @param buf the byte buffer which contains the WASM/AOT binary data,
  330. * note that the byte buffer must be writable since runtime may
  331. * change its content for footprint and performance purpose, and
  332. * it must be referencable until wasm_runtime_unload is called
  333. * @param size the size of the buffer
  334. * @param error_buf output of the exception info
  335. * @param error_buf_size the size of the exception string
  336. *
  337. * @return return WASM module loaded, NULL if failed
  338. */
  339. WASM_RUNTIME_API_EXTERN wasm_module_t
  340. wasm_runtime_load(uint8_t *buf, uint32_t size,
  341. char *error_buf, uint32_t error_buf_size);
  342. /**
  343. * Load a WASM module from a specified WASM or AOT section list.
  344. *
  345. * @param section_list the section list which contains each section data
  346. * @param is_aot whether the section list is AOT section list
  347. * @param error_buf output of the exception info
  348. * @param error_buf_size the size of the exception string
  349. *
  350. * @return return WASM module loaded, NULL if failed
  351. */
  352. WASM_RUNTIME_API_EXTERN wasm_module_t
  353. wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
  354. char *error_buf, uint32_t error_buf_size);
  355. /**
  356. * Unload a WASM module.
  357. *
  358. * @param module the module to be unloaded
  359. */
  360. WASM_RUNTIME_API_EXTERN void
  361. wasm_runtime_unload(wasm_module_t module);
  362. /**
  363. * Get the module hash of a WASM module, currently only available on
  364. * linux-sgx platform when the remote attestation feature is enabled
  365. *
  366. * @param module the WASM module to retrieve
  367. *
  368. * @return the module hash of the WASM module
  369. */
  370. char *
  371. wasm_runtime_get_module_hash(wasm_module_t module);
  372. /**
  373. * Set WASI parameters.
  374. *
  375. * While this API operates on a module, these parameters will be used
  376. * only when the module is instantiated. That is, you can consider these
  377. * as extra parameters for wasm_runtime_instantiate().
  378. *
  379. * @param module The module to set WASI parameters.
  380. * @param dir_list The list of directories to preopen. (real path)
  381. * @param dir_count The number of elements in dir_list.
  382. * @param map_dir_list The list of directories to preopen. (mapped path)
  383. * Format for each map entry: <guest-path>::<host-path>
  384. * @param map_dir_count The number of elements in map_dir_list.
  385. * If map_dir_count is smaller than dir_count,
  386. * mapped path is assumed to be same as the
  387. * corresponding real path for the rest of entries.
  388. * @param env The list of environment variables.
  389. * @param env_count The number of elements in env.
  390. * @param argv The list of command line arguments.
  391. * @param argc The number of elements in argv.
  392. * @param stdin_handle The raw host handle to back WASI STDIN_FILENO.
  393. * If an invalid handle is specified (e.g. -1 on POSIX,
  394. * INVALID_HANDLE_VALUE on Windows), the platform default
  395. * for STDIN is used.
  396. * @param stdoutfd The raw host handle to back WASI STDOUT_FILENO.
  397. * If an invalid handle is specified (e.g. -1 on POSIX,
  398. * INVALID_HANDLE_VALUE on Windows), the platform default
  399. * for STDOUT is used.
  400. * @param stderrfd The raw host handle to back WASI STDERR_FILENO.
  401. * If an invalid handle is specified (e.g. -1 on POSIX,
  402. * INVALID_HANDLE_VALUE on Windows), the platform default
  403. * for STDERR is used.
  404. */
  405. WASM_RUNTIME_API_EXTERN void
  406. wasm_runtime_set_wasi_args_ex(wasm_module_t module,
  407. const char *dir_list[], uint32_t dir_count,
  408. const char *map_dir_list[], uint32_t map_dir_count,
  409. const char *env[], uint32_t env_count,
  410. char *argv[], int argc, int64_t stdinfd,
  411. int64_t stdoutfd, int64_t stderrfd);
  412. /**
  413. * Set WASI parameters.
  414. *
  415. * Same as wasm_runtime_set_wasi_args_ex but with default stdio handles
  416. */
  417. WASM_RUNTIME_API_EXTERN void
  418. wasm_runtime_set_wasi_args(wasm_module_t module,
  419. const char *dir_list[], uint32_t dir_count,
  420. const char *map_dir_list[], uint32_t map_dir_count,
  421. const char *env[], uint32_t env_count,
  422. char *argv[], int argc);
  423. WASM_RUNTIME_API_EXTERN void
  424. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  425. uint32_t addr_pool_size);
  426. WASM_RUNTIME_API_EXTERN void
  427. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module, const char *ns_lookup_pool[],
  428. uint32_t ns_lookup_pool_size);
  429. /**
  430. * Instantiate a WASM module.
  431. *
  432. * @param module the WASM module to instantiate
  433. * @param default_stack_size the default stack size of the module instance when the
  434. * exec env's operation stack isn't created by user, e.g. API
  435. * wasm_application_execute_main() and wasm_application_execute_func()
  436. * create the operation stack internally with the stack size specified
  437. * here. And API wasm_runtime_create_exec_env() creates the operation
  438. * stack with stack size specified by its parameter, the stack size
  439. * specified here is ignored.
  440. * @param host_managed_heap_size the default heap size of the module instance, a heap will
  441. * be created besides the app memory space. Both wasm app and native
  442. * function can allocate memory from the heap.
  443. * @param error_buf buffer to output the error info if failed
  444. * @param error_buf_size the size of the error buffer
  445. *
  446. * @return return the instantiated WASM module instance, NULL if failed
  447. */
  448. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  449. wasm_runtime_instantiate(const wasm_module_t module,
  450. uint32_t default_stack_size, uint32_t host_managed_heap_size,
  451. char *error_buf, uint32_t error_buf_size);
  452. /**
  453. * Set the running mode of a WASM module instance, override the
  454. * default running mode of the runtime. Note that it only makes sense when
  455. * the input is a wasm bytecode file: for the AOT file, runtime always runs
  456. * it with AOT engine, and this function always returns true.
  457. *
  458. * @param module_inst the WASM module instance to set running mode
  459. * @param running_mode the running mode to set
  460. *
  461. * @return true if success, false otherwise
  462. */
  463. WASM_RUNTIME_API_EXTERN bool
  464. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  465. RunningMode running_mode);
  466. /**
  467. * Get the running mode of a WASM module instance, if no running mode
  468. * is explicitly set the default running mode of runtime will
  469. * be used and returned. Note that it only makes sense when the input is a
  470. * wasm bytecode file: for the AOT file, this function always returns 0.
  471. *
  472. * @param module_inst the WASM module instance to query for running mode
  473. *
  474. * @return the running mode this module instance currently use
  475. */
  476. WASM_RUNTIME_API_EXTERN RunningMode
  477. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
  478. /**
  479. * Deinstantiate a WASM module instance, destroy the resources.
  480. *
  481. * @param module_inst the WASM module instance to destroy
  482. */
  483. WASM_RUNTIME_API_EXTERN void
  484. wasm_runtime_deinstantiate(wasm_module_inst_t module_inst);
  485. /**
  486. * Get WASM module from WASM module instance
  487. *
  488. * @param module_inst the WASM module instance to retrieve
  489. *
  490. * @return the WASM module
  491. */
  492. WASM_RUNTIME_API_EXTERN wasm_module_t
  493. wasm_runtime_get_module(wasm_module_inst_t module_inst);
  494. WASM_RUNTIME_API_EXTERN bool
  495. wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
  496. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  497. wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
  498. /**
  499. * Get WASI exit code.
  500. *
  501. * After a WASI command completed its execution, an embedder can
  502. * call this function to get its exit code. (that is, the value given
  503. * to proc_exit.)
  504. *
  505. * @param module_inst the module instance
  506. */
  507. WASM_RUNTIME_API_EXTERN uint32_t
  508. wasm_runtime_get_wasi_exit_code(wasm_module_inst_t module_inst);
  509. /**
  510. * Lookup an exported function in the WASM module instance.
  511. *
  512. * @param module_inst the module instance
  513. * @param name the name of the function
  514. * @param signature the signature of the function, ignored currently
  515. *
  516. * @return the function instance found, NULL if not found
  517. */
  518. WASM_RUNTIME_API_EXTERN wasm_function_inst_t
  519. wasm_runtime_lookup_function(wasm_module_inst_t const module_inst,
  520. const char *name, const char *signature);
  521. /**
  522. * Get parameter count of the function instance
  523. *
  524. * @param func_inst the function instance
  525. * @param module_inst the module instance the function instance belongs to
  526. *
  527. * @return the parameter count of the function instance
  528. */
  529. WASM_RUNTIME_API_EXTERN uint32_t
  530. wasm_func_get_param_count(wasm_function_inst_t const func_inst,
  531. wasm_module_inst_t const module_inst);
  532. /**
  533. * Get result count of the function instance
  534. *
  535. * @param func_inst the function instance
  536. * @param module_inst the module instance the function instance belongs to
  537. *
  538. * @return the result count of the function instance
  539. */
  540. WASM_RUNTIME_API_EXTERN uint32_t
  541. wasm_func_get_result_count(wasm_function_inst_t const func_inst,
  542. wasm_module_inst_t const module_inst);
  543. /**
  544. * Get parameter types of the function instance
  545. *
  546. * @param func_inst the function instance
  547. * @param module_inst the module instance the function instance belongs to
  548. * @param param_types the parameter types returned
  549. */
  550. WASM_RUNTIME_API_EXTERN void
  551. wasm_func_get_param_types(wasm_function_inst_t const func_inst,
  552. wasm_module_inst_t const module_inst,
  553. wasm_valkind_t *param_types);
  554. /**
  555. * Get result types of the function instance
  556. *
  557. * @param func_inst the function instance
  558. * @param module_inst the module instance the function instance belongs to
  559. * @param result_types the result types returned
  560. */
  561. WASM_RUNTIME_API_EXTERN void
  562. wasm_func_get_result_types(wasm_function_inst_t const func_inst,
  563. wasm_module_inst_t const module_inst,
  564. wasm_valkind_t *result_types);
  565. /**
  566. * Create execution environment for a WASM module instance.
  567. *
  568. * @param module_inst the module instance
  569. * @param stack_size the stack size to execute a WASM function
  570. *
  571. * @return the execution environment, NULL if failed, e.g. invalid
  572. * stack size is passed
  573. */
  574. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  575. wasm_runtime_create_exec_env(wasm_module_inst_t module_inst,
  576. uint32_t stack_size);
  577. /**
  578. * Destroy the execution environment.
  579. *
  580. * @param exec_env the execution environment to destroy
  581. */
  582. WASM_RUNTIME_API_EXTERN void
  583. wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env);
  584. /**
  585. * Get the singleton execution environment for the instance.
  586. *
  587. * Note: The singleton execution environment is the execution
  588. * environment used internally by the runtime for the API functions
  589. * like wasm_application_execute_main, which don't take explicit
  590. * execution environment. It's associated to the corresponding
  591. * module instance and managed by the runtime. The API user should
  592. * not destroy it with wasm_runtime_destroy_exec_env.
  593. *
  594. * @param module_inst the module instance
  595. *
  596. * @return exec_env the execution environment to destroy
  597. */
  598. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  599. wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
  600. /**
  601. * Start debug instance based on given execution environment.
  602. * Note:
  603. * The debug instance will be destroyed during destroying the
  604. * execution environment, developers don't need to destroy it
  605. * manually.
  606. * If the cluster of this execution environment has already
  607. * been bound to a debug instance, this function will return true
  608. * directly.
  609. * If developer spawns some exec_env by wasm_runtime_spawn_exec_env,
  610. * don't need to call this function for every spawned exec_env as
  611. * they are sharing the same cluster with the main exec_env.
  612. *
  613. * @param exec_env the execution environment to start debug instance
  614. * @param port the port for the debug server to listen on.
  615. * 0 means automatic assignment.
  616. * -1 means to use the global setting in RuntimeInitArgs.
  617. *
  618. * @return debug port if success, 0 otherwise.
  619. */
  620. WASM_RUNTIME_API_EXTERN uint32_t
  621. wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env, int32_t port);
  622. /**
  623. * Same as wasm_runtime_start_debug_instance_with_port(env, -1).
  624. */
  625. WASM_RUNTIME_API_EXTERN uint32_t
  626. wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
  627. /**
  628. * Initialize the thread environment.
  629. * Note:
  630. * If developer creates a child thread by himself to call the
  631. * the wasm function in that thread, he should call this API
  632. * firstly before calling the wasm function and then call
  633. * wasm_runtime_destroy_thread_env() after calling the wasm
  634. * function. If the thread is created from the runtime API,
  635. * it is unnecessary to call these two APIs.
  636. *
  637. * @return true if success, false otherwise
  638. */
  639. WASM_RUNTIME_API_EXTERN bool
  640. wasm_runtime_init_thread_env(void);
  641. /**
  642. * Destroy the thread environment
  643. */
  644. WASM_RUNTIME_API_EXTERN void
  645. wasm_runtime_destroy_thread_env(void);
  646. /**
  647. * Whether the thread environment is initialized
  648. */
  649. WASM_RUNTIME_API_EXTERN bool
  650. wasm_runtime_thread_env_inited(void);
  651. /**
  652. * Get WASM module instance from execution environment
  653. *
  654. * @param exec_env the execution environment to retrieve
  655. *
  656. * @return the WASM module instance
  657. */
  658. WASM_RUNTIME_API_EXTERN wasm_module_inst_t
  659. wasm_runtime_get_module_inst(wasm_exec_env_t exec_env);
  660. /**
  661. * Set WASM module instance of execution environment
  662. * Caution:
  663. * normally the module instance is bound with the execution
  664. * environment one by one, if multiple module instances want
  665. * to share to the same execution environment, developer should
  666. * be responsible for the backup and restore of module instance
  667. *
  668. * @param exec_env the execution environment
  669. * @param module_inst the WASM module instance to set
  670. */
  671. WASM_RUNTIME_API_EXTERN void
  672. wasm_runtime_set_module_inst(wasm_exec_env_t exec_env,
  673. const wasm_module_inst_t module_inst);
  674. /**
  675. * Call the given WASM function of a WASM module instance with
  676. * arguments (bytecode and AoT).
  677. *
  678. * @param exec_env the execution environment to call the function,
  679. * which must be created from wasm_create_exec_env()
  680. * @param function the function to call
  681. * @param argc total cell number that the function parameters occupy,
  682. * a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument
  683. * occupies one cell, i64/f64 argument occupies two cells, note that
  684. * it might be different from the parameter number of the function
  685. * @param argv the arguments. If the function has return value,
  686. * the first (or first two in case 64-bit return value) element of
  687. * argv stores the return value of the called WASM function after this
  688. * function returns.
  689. *
  690. * @return true if success, false otherwise and exception will be thrown,
  691. * the caller can call wasm_runtime_get_exception to get the exception
  692. * info.
  693. */
  694. WASM_RUNTIME_API_EXTERN bool
  695. wasm_runtime_call_wasm(wasm_exec_env_t exec_env,
  696. wasm_function_inst_t function,
  697. uint32_t argc, uint32_t argv[]);
  698. /**
  699. * Call the given WASM function of a WASM module instance with
  700. * provided results space and arguments (bytecode and AoT).
  701. *
  702. * @param exec_env the execution environment to call the function,
  703. * which must be created from wasm_create_exec_env()
  704. * @param function the function to call
  705. * @param num_results the number of results
  706. * @param results the pre-alloced pointer to get the results
  707. * @param num_args the number of arguments
  708. * @param args the arguments
  709. *
  710. * @return true if success, false otherwise and exception will be thrown,
  711. * the caller can call wasm_runtime_get_exception to get the exception
  712. * info.
  713. */
  714. WASM_RUNTIME_API_EXTERN bool
  715. wasm_runtime_call_wasm_a(wasm_exec_env_t exec_env,
  716. wasm_function_inst_t function,
  717. uint32_t num_results, wasm_val_t results[],
  718. uint32_t num_args, wasm_val_t *args);
  719. /**
  720. * Call the given WASM function of a WASM module instance with
  721. * provided results space and variant arguments (bytecode and AoT).
  722. *
  723. * @param exec_env the execution environment to call the function,
  724. * which must be created from wasm_create_exec_env()
  725. * @param function the function to call
  726. * @param num_results the number of results
  727. * @param results the pre-alloced pointer to get the results
  728. * @param num_args the number of arguments
  729. * @param ... the variant arguments
  730. *
  731. * @return true if success, false otherwise and exception will be thrown,
  732. * the caller can call wasm_runtime_get_exception to get the exception
  733. * info.
  734. */
  735. WASM_RUNTIME_API_EXTERN bool
  736. wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
  737. wasm_function_inst_t function,
  738. uint32_t num_results, wasm_val_t results[],
  739. uint32_t num_args, ...);
  740. /**
  741. * Call a function reference of a given WASM runtime instance with
  742. * arguments.
  743. *
  744. * Note: this can be used to call a function which is not exported
  745. * by the module explicitly. You might consider it as an abstraction
  746. * violation.
  747. *
  748. * @param exec_env the execution environment to call the function
  749. * which must be created from wasm_create_exec_env()
  750. * @param element_index the function reference index, usually
  751. * prvovided by the caller of a registed native function
  752. * @param argc the number of arguments
  753. * @param argv the arguments. If the function method has return value,
  754. * the first (or first two in case 64-bit return value) element of
  755. * argv stores the return value of the called WASM function after this
  756. * function returns.
  757. *
  758. * @return true if success, false otherwise and exception will be thrown,
  759. * the caller can call wasm_runtime_get_exception to get exception info.
  760. */
  761. WASM_RUNTIME_API_EXTERN bool
  762. wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32_t element_index,
  763. uint32_t argc, uint32_t argv[]);
  764. /**
  765. * Find the unique main function from a WASM module instance
  766. * and execute that function.
  767. *
  768. * @param module_inst the WASM module instance
  769. * @param argc the number of arguments
  770. * @param argv the arguments array, if the main function has return value,
  771. * *(int*)argv stores the return value of the called main function after
  772. * this function returns.
  773. *
  774. * @return true if the main function is called, false otherwise and exception
  775. * will be thrown, the caller can call wasm_runtime_get_exception to get
  776. * the exception info.
  777. */
  778. WASM_RUNTIME_API_EXTERN bool
  779. wasm_application_execute_main(wasm_module_inst_t module_inst,
  780. int32_t argc, char *argv[]);
  781. /**
  782. * Find the specified function in argv[0] from a WASM module instance
  783. * and execute that function.
  784. *
  785. * @param module_inst the WASM module instance
  786. * @param name the name of the function to execute.
  787. * to indicate the module name via: $module_name$function_name
  788. * or just a function name: function_name
  789. * @param argc the number of arguments
  790. * @param argv the arguments array
  791. *
  792. * @return true if the specified function is called, false otherwise and
  793. * exception will be thrown, the caller can call wasm_runtime_get_exception
  794. * to get the exception info.
  795. */
  796. WASM_RUNTIME_API_EXTERN bool
  797. wasm_application_execute_func(wasm_module_inst_t module_inst,
  798. const char *name, int32_t argc, char *argv[]);
  799. /**
  800. * Get exception info of the WASM module instance.
  801. *
  802. * @param module_inst the WASM module instance
  803. *
  804. * @return the exception string
  805. */
  806. WASM_RUNTIME_API_EXTERN const char *
  807. wasm_runtime_get_exception(wasm_module_inst_t module_inst);
  808. /**
  809. * Set exception info of the WASM module instance.
  810. *
  811. * @param module_inst the WASM module instance
  812. *
  813. * @param exception the exception string
  814. */
  815. WASM_RUNTIME_API_EXTERN void
  816. wasm_runtime_set_exception(wasm_module_inst_t module_inst,
  817. const char *exception);
  818. /**
  819. * Clear exception info of the WASM module instance.
  820. *
  821. * @param module_inst the WASM module instance
  822. */
  823. WASM_RUNTIME_API_EXTERN void
  824. wasm_runtime_clear_exception(wasm_module_inst_t module_inst);
  825. /**
  826. * Terminate the WASM module instance.
  827. *
  828. * This function causes the module instance fail as if it raised a trap.
  829. *
  830. * This is intended to be used in situations like:
  831. *
  832. * - A thread is executing the WASM module instance
  833. * (eg. it's in the middle of `wasm_application_execute_main`)
  834. *
  835. * - Another thread has a copy of `wasm_module_inst_t` of
  836. * the module instance and wants to terminate it asynchronously.
  837. *
  838. * This function is provided only when WAMR is built with threading enabled.
  839. * (`WASM_ENABLE_THREAD_MGR=1`)
  840. *
  841. * @param module_inst the WASM module instance
  842. */
  843. WASM_RUNTIME_API_EXTERN void
  844. wasm_runtime_terminate(wasm_module_inst_t module_inst);
  845. /**
  846. * Set custom data to WASM module instance.
  847. * Note:
  848. * If WAMR_BUILD_LIB_PTHREAD is enabled, this API
  849. * will spread the custom data to all threads
  850. *
  851. * @param module_inst the WASM module instance
  852. * @param custom_data the custom data to be set
  853. */
  854. WASM_RUNTIME_API_EXTERN void
  855. wasm_runtime_set_custom_data(wasm_module_inst_t module_inst,
  856. void *custom_data);
  857. /**
  858. * Get the custom data within a WASM module instance.
  859. *
  860. * @param module_inst the WASM module instance
  861. *
  862. * @return the custom data (NULL if not set yet)
  863. */
  864. WASM_RUNTIME_API_EXTERN void *
  865. wasm_runtime_get_custom_data(wasm_module_inst_t module_inst);
  866. /**
  867. * Set the memory bounds checks flag of a WASM module instance.
  868. *
  869. * @param module_inst the WASM module instance
  870. * @param enable the flag to enable/disable the memory bounds checks
  871. */
  872. WASM_RUNTIME_API_EXTERN void
  873. wasm_runtime_set_bounds_checks(wasm_module_inst_t module_inst,
  874. bool enable);
  875. /**
  876. * Check if the memory bounds checks flag is enabled for a WASM module instance.
  877. *
  878. * @param module_inst the WASM module instance
  879. * @return true if the memory bounds checks flag is enabled, false otherwise
  880. */
  881. WASM_RUNTIME_API_EXTERN bool
  882. wasm_runtime_is_bounds_checks_enabled(
  883. wasm_module_inst_t module_inst);
  884. /**
  885. * Allocate memory from the heap of WASM module instance
  886. *
  887. * Note: wasm_runtime_module_malloc can call heap functions inside
  888. * the module instance and thus cause a memory growth.
  889. * This API needs to be used very carefully when you have a native
  890. * pointers to the module instance memory obtained with
  891. * wasm_runtime_addr_app_to_native or similar APIs.
  892. *
  893. * @param module_inst the WASM module instance which contains heap
  894. * @param size the size bytes to allocate
  895. * @param p_native_addr return native address of the allocated memory
  896. * if it is not NULL, and return NULL if memory malloc failed
  897. *
  898. * @return the allocated memory address, which is a relative offset to the
  899. * base address of the module instance's memory space. Note that
  900. * it is not an absolute address.
  901. * Return non-zero if success, zero if failed.
  902. */
  903. WASM_RUNTIME_API_EXTERN uint32_t
  904. wasm_runtime_module_malloc(wasm_module_inst_t module_inst, uint32_t size,
  905. void **p_native_addr);
  906. /**
  907. * Free memory to the heap of WASM module instance
  908. *
  909. * @param module_inst the WASM module instance which contains heap
  910. * @param ptr the pointer to free
  911. */
  912. WASM_RUNTIME_API_EXTERN void
  913. wasm_runtime_module_free(wasm_module_inst_t module_inst, uint32_t ptr);
  914. /**
  915. * Allocate memory from the heap of WASM module instance and initialize
  916. * the memory with src
  917. *
  918. * @param module_inst the WASM module instance which contains heap
  919. * @param src the source data to copy
  920. * @param size the size of the source data
  921. *
  922. * @return the allocated memory address, which is a relative offset to the
  923. * base address of the module instance's memory space. Note that
  924. * it is not an absolute address.
  925. * Return non-zero if success, zero if failed.
  926. */
  927. WASM_RUNTIME_API_EXTERN uint32_t
  928. wasm_runtime_module_dup_data(wasm_module_inst_t module_inst,
  929. const char *src, uint32_t size);
  930. /**
  931. * Validate the app address, check whether it belongs to WASM module
  932. * instance's address space, or in its heap space or memory space.
  933. *
  934. * @param module_inst the WASM module instance
  935. * @param app_offset the app address to validate, which is a relative address
  936. * @param size the size bytes of the app address
  937. *
  938. * @return true if success, false otherwise. If failed, an exception will
  939. * be thrown.
  940. */
  941. WASM_RUNTIME_API_EXTERN bool
  942. wasm_runtime_validate_app_addr(wasm_module_inst_t module_inst,
  943. uint32_t app_offset, uint32_t size);
  944. /**
  945. * Similar to wasm_runtime_validate_app_addr(), except that the size parameter
  946. * is not provided. This function validates the app string address, check
  947. * whether it belongs to WASM module instance's address space, or in its heap
  948. * space or memory space. Moreover, it checks whether it is the offset of a
  949. * string that is end with '\0'.
  950. *
  951. * Note: The validation result, especially the NUL termination check,
  952. * is not reliable for a module instance with multiple threads because
  953. * other threads can modify the heap behind us.
  954. *
  955. * @param module_inst the WASM module instance
  956. * @param app_str_offset the app address of the string to validate, which is a
  957. * relative address
  958. *
  959. * @return true if success, false otherwise. If failed, an exception will
  960. * be thrown.
  961. */
  962. WASM_RUNTIME_API_EXTERN bool
  963. wasm_runtime_validate_app_str_addr(wasm_module_inst_t module_inst,
  964. uint32_t app_str_offset);
  965. /**
  966. * Validate the native address, check whether it belongs to WASM module
  967. * instance's address space, or in its heap space or memory space.
  968. *
  969. * @param module_inst the WASM module instance
  970. * @param native_ptr the native address to validate, which is an absolute
  971. * address
  972. * @param size the size bytes of the app address
  973. *
  974. * @return true if success, false otherwise. If failed, an exception will
  975. * be thrown.
  976. */
  977. WASM_RUNTIME_API_EXTERN bool
  978. wasm_runtime_validate_native_addr(wasm_module_inst_t module_inst,
  979. void *native_ptr, uint32_t size);
  980. /**
  981. * Convert app address(relative address) to native address(absolute address)
  982. *
  983. * Note that native addresses to module instance memory can be invalidated
  984. * on a memory growth. (Except shared memory, whose native addresses are
  985. * stable.)
  986. *
  987. * @param module_inst the WASM module instance
  988. * @param app_offset the app adress
  989. *
  990. * @return the native address converted
  991. */
  992. WASM_RUNTIME_API_EXTERN void *
  993. wasm_runtime_addr_app_to_native(wasm_module_inst_t module_inst,
  994. uint32_t app_offset);
  995. /**
  996. * Convert native address(absolute address) to app address(relative address)
  997. *
  998. * @param module_inst the WASM module instance
  999. * @param native_ptr the native address
  1000. *
  1001. * @return the app address converted
  1002. */
  1003. WASM_RUNTIME_API_EXTERN uint32_t
  1004. wasm_runtime_addr_native_to_app(wasm_module_inst_t module_inst,
  1005. void *native_ptr);
  1006. /**
  1007. * Get the app address range (relative address) that a app address belongs to
  1008. *
  1009. * @param module_inst the WASM module instance
  1010. * @param app_offset the app address to retrieve
  1011. * @param p_app_start_offset buffer to output the app start offset if not NULL
  1012. * @param p_app_end_offset buffer to output the app end offset if not NULL
  1013. *
  1014. * @return true if success, false otherwise.
  1015. */
  1016. WASM_RUNTIME_API_EXTERN bool
  1017. wasm_runtime_get_app_addr_range(wasm_module_inst_t module_inst,
  1018. uint32_t app_offset,
  1019. uint32_t *p_app_start_offset,
  1020. uint32_t *p_app_end_offset);
  1021. /**
  1022. * Get the native address range (absolute address) that a native address
  1023. * belongs to
  1024. *
  1025. * @param module_inst the WASM module instance
  1026. * @param native_ptr the native address to retrieve
  1027. * @param p_native_start_addr buffer to output the native start address
  1028. * if not NULL
  1029. * @param p_native_end_addr buffer to output the native end address
  1030. * if not NULL
  1031. *
  1032. * @return true if success, false otherwise.
  1033. */
  1034. WASM_RUNTIME_API_EXTERN bool
  1035. wasm_runtime_get_native_addr_range(wasm_module_inst_t module_inst,
  1036. uint8_t *native_ptr,
  1037. uint8_t **p_native_start_addr,
  1038. uint8_t **p_native_end_addr);
  1039. /**
  1040. * Register native functions with same module name
  1041. *
  1042. * Note: The array `native_symbols` should not be read-only because the
  1043. * library can modify it in-place.
  1044. *
  1045. * Note: After successful call of this function, the array `native_symbols`
  1046. * is owned by the library.
  1047. *
  1048. * @param module_name the module name of the native functions
  1049. * @param native_symbols specifies an array of NativeSymbol structures which
  1050. * contain the names, function pointers and signatures
  1051. * Note: WASM runtime will not allocate memory to clone the data, so
  1052. * user must ensure the array can be used forever
  1053. * Meanings of letters in function signature:
  1054. * 'i': the parameter is i32 type
  1055. * 'I': the parameter is i64 type
  1056. * 'f': the parameter is f32 type
  1057. * 'F': the parameter is f64 type
  1058. * 'r': the parameter is externref type, it should be a uintptr_t in host
  1059. * '*': the parameter is a pointer (i32 in WASM), and runtime will
  1060. * auto check its boundary before calling the native function.
  1061. * If it is followed by '~', the checked length of the pointer
  1062. * is gotten from the following parameter, if not, the checked
  1063. * length of the pointer is 1.
  1064. * '~': the parameter is the pointer's length with i32 type, and must
  1065. * follow after '*'
  1066. * '$': the parameter is a string (i32 in WASM), and runtime will
  1067. * auto check its boundary before calling the native function
  1068. * @param n_native_symbols specifies the number of native symbols in the array
  1069. *
  1070. * @return true if success, false otherwise
  1071. */
  1072. WASM_RUNTIME_API_EXTERN bool
  1073. wasm_runtime_register_natives(const char *module_name,
  1074. NativeSymbol *native_symbols,
  1075. uint32_t n_native_symbols);
  1076. /**
  1077. * Register native functions with same module name, similar to
  1078. * wasm_runtime_register_natives, the difference is that runtime passes raw
  1079. * arguments to native API, which means that the native API should be defined as
  1080. * void foo(wasm_exec_env_t exec_env, uint64 *args);
  1081. * and native API should extract arguments one by one from args array with macro
  1082. * native_raw_get_arg
  1083. * and write the return value back to args[0] with macro
  1084. * native_raw_return_type and native_raw_set_return
  1085. */
  1086. WASM_RUNTIME_API_EXTERN bool
  1087. wasm_runtime_register_natives_raw(const char *module_name,
  1088. NativeSymbol *native_symbols,
  1089. uint32_t n_native_symbols);
  1090. /**
  1091. * Undo wasm_runtime_register_natives or wasm_runtime_register_natives_raw
  1092. *
  1093. * @param module_name Should be the same as the corresponding
  1094. * wasm_runtime_register_natives.
  1095. * (Same in term of strcmp.)
  1096. *
  1097. * @param native_symbols Should be the same as the corresponding
  1098. * wasm_runtime_register_natives.
  1099. * (Same in term of pointer comparison.)
  1100. *
  1101. * @return true if success, false otherwise
  1102. */
  1103. WASM_RUNTIME_API_EXTERN bool
  1104. wasm_runtime_unregister_natives(const char *module_name,
  1105. NativeSymbol *native_symbols);
  1106. /**
  1107. * Get attachment of native function from execution environment
  1108. *
  1109. * @param exec_env the execution environment to retrieve
  1110. *
  1111. * @return the attachment of native function
  1112. */
  1113. WASM_RUNTIME_API_EXTERN void *
  1114. wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
  1115. /**
  1116. * Set user data to execution environment.
  1117. *
  1118. * @param exec_env the execution environment
  1119. * @param user_data the user data to be set
  1120. */
  1121. WASM_RUNTIME_API_EXTERN void
  1122. wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data);
  1123. /**
  1124. * Get the user data within execution environment.
  1125. *
  1126. * @param exec_env the execution environment
  1127. *
  1128. * @return the user data (NULL if not set yet)
  1129. */
  1130. WASM_RUNTIME_API_EXTERN void *
  1131. wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
  1132. /**
  1133. * Dump runtime memory consumption, including:
  1134. * Exec env memory consumption
  1135. * WASM module memory consumption
  1136. * WASM module instance memory consumption
  1137. * stack and app heap used info
  1138. *
  1139. * @param exec_env the execution environment
  1140. */
  1141. WASM_RUNTIME_API_EXTERN void
  1142. wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env);
  1143. /**
  1144. * Dump runtime performance profiler data of each function
  1145. *
  1146. * @param module_inst the WASM module instance to profile
  1147. */
  1148. WASM_RUNTIME_API_EXTERN void
  1149. wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
  1150. /* wasm thread callback function type */
  1151. typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *);
  1152. /* wasm thread type */
  1153. typedef uintptr_t wasm_thread_t;
  1154. /**
  1155. * Set the max thread num per cluster.
  1156. *
  1157. * @param num maximum thread num
  1158. */
  1159. WASM_RUNTIME_API_EXTERN void
  1160. wasm_runtime_set_max_thread_num(uint32_t num);
  1161. /**
  1162. * Spawn a new exec_env, the spawned exec_env
  1163. * can be used in other threads
  1164. *
  1165. * @param num the original exec_env
  1166. *
  1167. * @return the spawned exec_env if success, NULL otherwise
  1168. */
  1169. WASM_RUNTIME_API_EXTERN wasm_exec_env_t
  1170. wasm_runtime_spawn_exec_env(wasm_exec_env_t exec_env);
  1171. /**
  1172. * Destroy the spawned exec_env
  1173. *
  1174. * @param exec_env the spawned exec_env
  1175. */
  1176. WASM_RUNTIME_API_EXTERN void
  1177. wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
  1178. /**
  1179. * Spawn a thread from the given exec_env
  1180. *
  1181. * @param exec_env the original exec_env
  1182. * @param tid thread id to be returned to the caller
  1183. * @param callback the callback function provided by the user
  1184. * @param arg the arguments passed to the callback
  1185. *
  1186. * @return 0 if success, -1 otherwise
  1187. */
  1188. WASM_RUNTIME_API_EXTERN int32_t
  1189. wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
  1190. wasm_thread_callback_t callback, void *arg);
  1191. /**
  1192. * Wait a spawned thread to terminate
  1193. *
  1194. * @param tid thread id
  1195. * @param retval if not NULL, output the return value of the thread
  1196. *
  1197. * @return 0 if success, error number otherwise
  1198. */
  1199. WASM_RUNTIME_API_EXTERN int32_t
  1200. wasm_runtime_join_thread(wasm_thread_t tid, void **retval);
  1201. /**
  1202. * Map external object to an internal externref index: if the index
  1203. * has been created, return it, otherwise create the index.
  1204. *
  1205. * @param module_inst the WASM module instance that the extern object
  1206. * belongs to
  1207. * @param extern_obj the external object to be mapped
  1208. * @param p_externref_idx return externref index of the external object
  1209. *
  1210. * @return true if success, false otherwise
  1211. */
  1212. WASM_RUNTIME_API_EXTERN bool
  1213. wasm_externref_obj2ref(wasm_module_inst_t module_inst,
  1214. void *extern_obj, uint32_t *p_externref_idx);
  1215. /**
  1216. * Delete external object registered by `wasm_externref_obj2ref`.
  1217. *
  1218. * @param module_inst the WASM module instance that the extern object
  1219. * belongs to
  1220. * @param extern_obj the external object to be deleted
  1221. *
  1222. * @return true if success, false otherwise
  1223. */
  1224. WASM_RUNTIME_API_EXTERN bool
  1225. wasm_externref_objdel(wasm_module_inst_t module_inst, void *extern_obj);
  1226. /**
  1227. * Set cleanup callback to release external object.
  1228. *
  1229. * @param module_inst the WASM module instance that the extern object
  1230. * belongs to
  1231. * @param extern_obj the external object to which to set the
  1232. * `extern_obj_cleanup` cleanup callback.
  1233. * @param extern_obj_cleanup a callback to release `extern_obj`
  1234. *
  1235. * @return true if success, false otherwise
  1236. */
  1237. WASM_RUNTIME_API_EXTERN bool
  1238. wasm_externref_set_cleanup(wasm_module_inst_t module_inst, void *extern_obj,
  1239. void (*extern_obj_cleanup)(void *));
  1240. /**
  1241. * Retrieve the external object from an internal externref index
  1242. *
  1243. * @param externref_idx the externref index to retrieve
  1244. * @param p_extern_obj return the mapped external object of
  1245. * the externref index
  1246. *
  1247. * @return true if success, false otherwise
  1248. */
  1249. WASM_RUNTIME_API_EXTERN bool
  1250. wasm_externref_ref2obj(uint32_t externref_idx, void **p_extern_obj);
  1251. /**
  1252. * Retain an extern object which is mapped to the internal externref
  1253. * so that the object won't be cleaned during extern object reclaim
  1254. * if it isn't used.
  1255. *
  1256. * @param externref_idx the externref index of an external object
  1257. * to retain
  1258. * @return true if success, false otherwise
  1259. */
  1260. WASM_RUNTIME_API_EXTERN bool
  1261. wasm_externref_retain(uint32_t externref_idx);
  1262. /**
  1263. * Dump the call stack to stdout
  1264. *
  1265. * @param exec_env the execution environment
  1266. */
  1267. WASM_RUNTIME_API_EXTERN void
  1268. wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
  1269. /**
  1270. * Get the size required to store the call stack contents, including
  1271. * the space for terminating null byte ('\0')
  1272. *
  1273. * @param exec_env the execution environment
  1274. *
  1275. * @return size required to store the contents, 0 means error
  1276. */
  1277. WASM_RUNTIME_API_EXTERN uint32_t
  1278. wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env);
  1279. /**
  1280. * Dump the call stack to buffer.
  1281. *
  1282. * @note this function is not thread-safe, please only use this API
  1283. * when the exec_env is not executing
  1284. *
  1285. * @param exec_env the execution environment
  1286. * @param buf buffer to store the dumped content
  1287. * @param len length of the buffer
  1288. *
  1289. * @return bytes dumped to the buffer, including the terminating null
  1290. * byte ('\0'), 0 means error and data in buf may be invalid
  1291. */
  1292. WASM_RUNTIME_API_EXTERN uint32_t
  1293. wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
  1294. uint32_t len);
  1295. /**
  1296. * Get the size required to store the LLVM PGO profile data
  1297. *
  1298. * @param module_inst the WASM module instance
  1299. *
  1300. * @return size required to store the contents, 0 means error
  1301. */
  1302. WASM_RUNTIME_API_EXTERN uint32_t
  1303. wasm_runtime_get_pgo_prof_data_size(wasm_module_inst_t module_inst);
  1304. /**
  1305. * Dump the LLVM PGO profile data to buffer
  1306. *
  1307. * @param module_inst the WASM module instance
  1308. * @param buf buffer to store the dumped content
  1309. * @param len length of the buffer
  1310. *
  1311. * @return bytes dumped to the buffer, 0 means error and data in buf
  1312. * may be invalid
  1313. */
  1314. WASM_RUNTIME_API_EXTERN uint32_t
  1315. wasm_runtime_dump_pgo_prof_data_to_buf(wasm_module_inst_t module_inst,
  1316. char *buf, uint32_t len);
  1317. /**
  1318. * Get a custom section by name
  1319. *
  1320. * @param module_comm the module to find
  1321. * @param name name of the custom section
  1322. * @param len return the length of the content if found
  1323. *
  1324. * @return Custom section content (not including the name length
  1325. * and name string) if found, NULL otherwise
  1326. */
  1327. WASM_RUNTIME_API_EXTERN const uint8_t *
  1328. wasm_runtime_get_custom_section(wasm_module_t const module_comm,
  1329. const char *name, uint32_t *len);
  1330. /**
  1331. * Get WAMR semantic version
  1332. */
  1333. WASM_RUNTIME_API_EXTERN void
  1334. wasm_runtime_get_version(uint32_t *major, uint32_t *minor, uint32_t *patch);
  1335. /**
  1336. * Check whether an import func `(import <module_name> <func_name> (func ...))`
  1337. * is linked or not with runtime registered natvie functions
  1338. */
  1339. WASM_RUNTIME_API_EXTERN bool
  1340. wasm_runtime_is_import_func_linked(const char *module_name,
  1341. const char *func_name);
  1342. /**
  1343. * Check whether an import global `(import <module_name> <global_name> (global ...))`
  1344. * is linked or not with runtime registered natvie globals
  1345. */
  1346. WASM_RUNTIME_API_EXTERN bool
  1347. wasm_runtime_is_import_global_linked(const char *module_name,
  1348. const char *global_name);
  1349. typedef enum {
  1350. INTERNAL_ERROR,
  1351. MAX_SIZE_REACHED,
  1352. } enlarge_memory_error_reason_t;
  1353. typedef void (*enlarge_memory_error_callback_t)(
  1354. uint32_t inc_page_count, uint64_t current_memory_size,
  1355. uint32_t memory_index, enlarge_memory_error_reason_t failure_reason,
  1356. wasm_module_inst_t instance, wasm_exec_env_t exec_env,
  1357. void* user_data);
  1358. /**
  1359. * Setup callback invoked when memory.grow fails
  1360. */
  1361. WASM_RUNTIME_API_EXTERN void
  1362. wasm_runtime_set_enlarge_mem_error_callback(
  1363. const enlarge_memory_error_callback_t callback, void *user_data);
  1364. /*
  1365. * module instance context APIs
  1366. * wasm_runtime_create_context_key
  1367. * wasm_runtime_destroy_context_key
  1368. * wasm_runtime_set_context
  1369. * wasm_runtime_set_context_spread
  1370. * wasm_runtime_get_context
  1371. *
  1372. * This set of APIs is intended to be used by an embedder which provides
  1373. * extra sets of native functions, which need per module instance state
  1374. * and are maintained outside of the WAMR tree.
  1375. *
  1376. * It's modelled after the pthread specific API.
  1377. *
  1378. * wasm_runtime_set_context_spread is similar to
  1379. * wasm_runtime_set_context, except that
  1380. * wasm_runtime_set_context_spread applies the change
  1381. * to all threads in the cluster.
  1382. * It's an undefined behavior if multiple threads in a cluster call
  1383. * wasm_runtime_set_context_spread on the same key
  1384. * simultaneously. It's a caller's resposibility to perform necessary
  1385. * serialization if necessary. For example:
  1386. *
  1387. * if (wasm_runtime_get_context(inst, key) == NULL) {
  1388. * newctx = alloc_and_init(...);
  1389. * lock(some_lock);
  1390. * if (wasm_runtime_get_context(inst, key) == NULL) {
  1391. * // this thread won the race
  1392. * wasm_runtime_set_context_spread(inst, key, newctx);
  1393. * newctx = NULL;
  1394. * }
  1395. * unlock(some_lock);
  1396. * if (newctx != NULL) {
  1397. * // this thread lost the race, free it
  1398. * cleanup_and_free(newctx);
  1399. * }
  1400. * }
  1401. *
  1402. * Note: dynamic key create/destroy while instances are live is not
  1403. * implemented as of writing this.
  1404. * it's caller's resposibility to ensure destorying all module instances
  1405. * before calling wasm_runtime_create_context_key or
  1406. * wasm_runtime_destroy_context_key.
  1407. * otherwise, it's an undefined behavior.
  1408. *
  1409. * Note about threads:
  1410. * - When spawning a thread, the contexts (the pointers given to
  1411. * wasm_runtime_set_context) are copied from the parent
  1412. * instance.
  1413. * - The destructor is called only on the main instance.
  1414. */
  1415. WASM_RUNTIME_API_EXTERN void *
  1416. wasm_runtime_create_context_key(
  1417. void (*dtor)(wasm_module_inst_t inst, void *ctx));
  1418. WASM_RUNTIME_API_EXTERN void
  1419. wasm_runtime_destroy_context_key(void *key);
  1420. WASM_RUNTIME_API_EXTERN void
  1421. wasm_runtime_set_context(wasm_module_inst_t inst, void *key, void *ctx);
  1422. WASM_RUNTIME_API_EXTERN void
  1423. wasm_runtime_set_context_spread(wasm_module_inst_t inst, void *key, void *ctx);
  1424. WASM_RUNTIME_API_EXTERN void *
  1425. wasm_runtime_get_context(wasm_module_inst_t inst, void *key);
  1426. /*
  1427. * wasm_runtime_begin_blocking_op/wasm_runtime_end_blocking_op
  1428. *
  1429. * These APIs are intended to be used by the implementations of
  1430. * host functions. It wraps an operation which possibly blocks for long
  1431. * to prepare for async termination.
  1432. *
  1433. * eg.
  1434. *
  1435. * if (!wasm_runtime_begin_blocking_op(exec_env)) {
  1436. * return EINTR;
  1437. * }
  1438. * ret = possibly_blocking_op();
  1439. * wasm_runtime_end_blocking_op(exec_env);
  1440. * return ret;
  1441. *
  1442. * If threading support (WASM_ENABLE_THREAD_MGR) is not enabled,
  1443. * these functions are no-op.
  1444. *
  1445. * If the underlying platform support (OS_ENABLE_WAKEUP_BLOCKING_OP) is
  1446. * not available, these functions are no-op. In that case, the runtime
  1447. * might not terminate a blocking thread in a timely manner.
  1448. *
  1449. * If the underlying platform support is available, it's used to wake up
  1450. * the thread for async termination. The expectation here is that a
  1451. * `os_wakeup_blocking_op` call makes the blocking operation
  1452. * (`possibly_blocking_op` in the above example) return in a timely manner.
  1453. *
  1454. * The actual wake up mechanism used by `os_wakeup_blocking_op` is
  1455. * platform-dependent. It might impose some platform-dependent restrictions
  1456. * on the implementation of the blocking opearation.
  1457. *
  1458. * For example, on POSIX-like platforms, a signal (by default SIGUSR1) is
  1459. * used. The signal delivery configurations (eg. signal handler, signal mask,
  1460. * etc) for the signal are set up by the runtime. You can change the signal
  1461. * to use for this purpose by calling os_set_signal_number_for_blocking_op
  1462. * before the runtime initialization.
  1463. */
  1464. WASM_RUNTIME_API_EXTERN bool
  1465. wasm_runtime_begin_blocking_op(wasm_exec_env_t exec_env);
  1466. WASM_RUNTIME_API_EXTERN void
  1467. wasm_runtime_end_blocking_op(wasm_exec_env_t exec_env);
  1468. /* clang-format on */
  1469. #ifdef __cplusplus
  1470. }
  1471. #endif
  1472. #endif /* end of _WASM_EXPORT_H */