wasm_export.h 71 KB

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