wasm_export.h 74 KB

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