wasm_export.h 76 KB

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