wasm_export.h 76 KB

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