wasm_export.h 67 KB

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