wasm_runtime_common.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_COMMON_H
  6. #define _WASM_COMMON_H
  7. #include "bh_platform.h"
  8. #include "bh_common.h"
  9. #include "wasm_exec_env.h"
  10. #include "wasm_native.h"
  11. #include "../include/wasm_export.h"
  12. #include "../interpreter/wasm.h"
  13. #if WASM_ENABLE_LIBC_WASI != 0
  14. #if WASM_ENABLE_UVWASI == 0
  15. #include "wasmtime_ssp.h"
  16. #include "posix.h"
  17. #else
  18. #include "uvwasi.h"
  19. #endif
  20. #endif
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* Internal use for setting default running mode */
  25. #define Mode_Default 0
  26. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  27. #define PUT_I64_TO_ADDR(addr, value) \
  28. do { \
  29. *(int64 *)(addr) = (int64)(value); \
  30. } while (0)
  31. #define PUT_F64_TO_ADDR(addr, value) \
  32. do { \
  33. *(float64 *)(addr) = (float64)(value); \
  34. } while (0)
  35. #define GET_I64_FROM_ADDR(addr) (*(int64 *)(addr))
  36. #define GET_F64_FROM_ADDR(addr) (*(float64 *)(addr))
  37. /* For STORE opcodes */
  38. #define STORE_I64 PUT_I64_TO_ADDR
  39. #define STORE_U32(addr, value) \
  40. do { \
  41. *(uint32 *)(addr) = (uint32)(value); \
  42. } while (0)
  43. #define STORE_U16(addr, value) \
  44. do { \
  45. *(uint16 *)(addr) = (uint16)(value); \
  46. } while (0)
  47. /* For LOAD opcodes */
  48. #define LOAD_I64(addr) (*(int64 *)(addr))
  49. #define LOAD_F64(addr) (*(float64 *)(addr))
  50. #define LOAD_I32(addr) (*(int32 *)(addr))
  51. #define LOAD_U32(addr) (*(uint32 *)(addr))
  52. #define LOAD_I16(addr) (*(int16 *)(addr))
  53. #define LOAD_U16(addr) (*(uint16 *)(addr))
  54. #define STORE_PTR(addr, ptr) \
  55. do { \
  56. *(void **)addr = (void *)ptr; \
  57. } while (0)
  58. #else /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
  59. #define PUT_I64_TO_ADDR(addr, value) \
  60. do { \
  61. uint32 *addr_u32 = (uint32 *)(addr); \
  62. union { \
  63. int64 val; \
  64. uint32 parts[2]; \
  65. } u; \
  66. u.val = (int64)(value); \
  67. addr_u32[0] = u.parts[0]; \
  68. addr_u32[1] = u.parts[1]; \
  69. } while (0)
  70. #define PUT_F64_TO_ADDR(addr, value) \
  71. do { \
  72. uint32 *addr_u32 = (uint32 *)(addr); \
  73. union { \
  74. float64 val; \
  75. uint32 parts[2]; \
  76. } u; \
  77. u.val = (value); \
  78. addr_u32[0] = u.parts[0]; \
  79. addr_u32[1] = u.parts[1]; \
  80. } while (0)
  81. static inline int64
  82. GET_I64_FROM_ADDR(uint32 *addr)
  83. {
  84. union {
  85. int64 val;
  86. uint32 parts[2];
  87. } u;
  88. u.parts[0] = addr[0];
  89. u.parts[1] = addr[1];
  90. return u.val;
  91. }
  92. static inline float64
  93. GET_F64_FROM_ADDR(uint32 *addr)
  94. {
  95. union {
  96. float64 val;
  97. uint32 parts[2];
  98. } u;
  99. u.parts[0] = addr[0];
  100. u.parts[1] = addr[1];
  101. return u.val;
  102. }
  103. /* For STORE opcodes */
  104. #define STORE_I64(addr, value) \
  105. do { \
  106. uintptr_t addr_ = (uintptr_t)(addr); \
  107. union { \
  108. int64 val; \
  109. uint32 u32[2]; \
  110. uint16 u16[4]; \
  111. uint8 u8[8]; \
  112. } u; \
  113. if ((addr_ & (uintptr_t)7) == 0) \
  114. *(int64 *)(addr) = (int64)(value); \
  115. else { \
  116. u.val = (int64)(value); \
  117. if ((addr_ & (uintptr_t)3) == 0) { \
  118. ((uint32 *)(addr))[0] = u.u32[0]; \
  119. ((uint32 *)(addr))[1] = u.u32[1]; \
  120. } \
  121. else if ((addr_ & (uintptr_t)1) == 0) { \
  122. ((uint16 *)(addr))[0] = u.u16[0]; \
  123. ((uint16 *)(addr))[1] = u.u16[1]; \
  124. ((uint16 *)(addr))[2] = u.u16[2]; \
  125. ((uint16 *)(addr))[3] = u.u16[3]; \
  126. } \
  127. else { \
  128. int32 t; \
  129. for (t = 0; t < 8; t++) \
  130. ((uint8 *)(addr))[t] = u.u8[t]; \
  131. } \
  132. } \
  133. } while (0)
  134. #define STORE_U32(addr, value) \
  135. do { \
  136. uintptr_t addr_ = (uintptr_t)(addr); \
  137. union { \
  138. uint32 val; \
  139. uint16 u16[2]; \
  140. uint8 u8[4]; \
  141. } u; \
  142. if ((addr_ & (uintptr_t)3) == 0) \
  143. *(uint32 *)(addr) = (uint32)(value); \
  144. else { \
  145. u.val = (uint32)(value); \
  146. if ((addr_ & (uintptr_t)1) == 0) { \
  147. ((uint16 *)(addr))[0] = u.u16[0]; \
  148. ((uint16 *)(addr))[1] = u.u16[1]; \
  149. } \
  150. else { \
  151. ((uint8 *)(addr))[0] = u.u8[0]; \
  152. ((uint8 *)(addr))[1] = u.u8[1]; \
  153. ((uint8 *)(addr))[2] = u.u8[2]; \
  154. ((uint8 *)(addr))[3] = u.u8[3]; \
  155. } \
  156. } \
  157. } while (0)
  158. #define STORE_U16(addr, value) \
  159. do { \
  160. union { \
  161. uint16 val; \
  162. uint8 u8[2]; \
  163. } u; \
  164. u.val = (uint16)(value); \
  165. ((uint8 *)(addr))[0] = u.u8[0]; \
  166. ((uint8 *)(addr))[1] = u.u8[1]; \
  167. } while (0)
  168. /* For LOAD opcodes */
  169. static inline int64
  170. LOAD_I64(void *addr)
  171. {
  172. uintptr_t addr1 = (uintptr_t)addr;
  173. union {
  174. int64 val;
  175. uint32 u32[2];
  176. uint16 u16[4];
  177. uint8 u8[8];
  178. } u;
  179. if ((addr1 & (uintptr_t)7) == 0)
  180. return *(int64 *)addr;
  181. if ((addr1 & (uintptr_t)3) == 0) {
  182. u.u32[0] = ((uint32 *)addr)[0];
  183. u.u32[1] = ((uint32 *)addr)[1];
  184. }
  185. else if ((addr1 & (uintptr_t)1) == 0) {
  186. u.u16[0] = ((uint16 *)addr)[0];
  187. u.u16[1] = ((uint16 *)addr)[1];
  188. u.u16[2] = ((uint16 *)addr)[2];
  189. u.u16[3] = ((uint16 *)addr)[3];
  190. }
  191. else {
  192. int32 t;
  193. for (t = 0; t < 8; t++)
  194. u.u8[t] = ((uint8 *)addr)[t];
  195. }
  196. return u.val;
  197. }
  198. static inline float64
  199. LOAD_F64(void *addr)
  200. {
  201. uintptr_t addr1 = (uintptr_t)addr;
  202. union {
  203. float64 val;
  204. uint32 u32[2];
  205. uint16 u16[4];
  206. uint8 u8[8];
  207. } u;
  208. if ((addr1 & (uintptr_t)7) == 0)
  209. return *(float64 *)addr;
  210. if ((addr1 & (uintptr_t)3) == 0) {
  211. u.u32[0] = ((uint32 *)addr)[0];
  212. u.u32[1] = ((uint32 *)addr)[1];
  213. }
  214. else if ((addr1 & (uintptr_t)1) == 0) {
  215. u.u16[0] = ((uint16 *)addr)[0];
  216. u.u16[1] = ((uint16 *)addr)[1];
  217. u.u16[2] = ((uint16 *)addr)[2];
  218. u.u16[3] = ((uint16 *)addr)[3];
  219. }
  220. else {
  221. int32 t;
  222. for (t = 0; t < 8; t++)
  223. u.u8[t] = ((uint8 *)addr)[t];
  224. }
  225. return u.val;
  226. }
  227. static inline int32
  228. LOAD_I32(void *addr)
  229. {
  230. uintptr_t addr1 = (uintptr_t)addr;
  231. union {
  232. int32 val;
  233. uint16 u16[2];
  234. uint8 u8[4];
  235. } u;
  236. if ((addr1 & (uintptr_t)3) == 0)
  237. return *(int32 *)addr;
  238. if ((addr1 & (uintptr_t)1) == 0) {
  239. u.u16[0] = ((uint16 *)addr)[0];
  240. u.u16[1] = ((uint16 *)addr)[1];
  241. }
  242. else {
  243. u.u8[0] = ((uint8 *)addr)[0];
  244. u.u8[1] = ((uint8 *)addr)[1];
  245. u.u8[2] = ((uint8 *)addr)[2];
  246. u.u8[3] = ((uint8 *)addr)[3];
  247. }
  248. return u.val;
  249. }
  250. static inline int16
  251. LOAD_I16(void *addr)
  252. {
  253. uintptr_t addr1 = (uintptr_t)addr;
  254. union {
  255. int16 val;
  256. uint8 u8[2];
  257. } u;
  258. if ((addr1 & (uintptr_t)1)) {
  259. u.u8[0] = ((uint8 *)addr)[0];
  260. u.u8[1] = ((uint8 *)addr)[1];
  261. return u.val;
  262. }
  263. return *(int16 *)addr;
  264. }
  265. #define LOAD_U32(addr) ((uint32)LOAD_I32(addr))
  266. #define LOAD_U16(addr) ((uint16)LOAD_I16(addr))
  267. #if UINTPTR_MAX == UINT32_MAX
  268. #define STORE_PTR(addr, ptr) STORE_U32(addr, (uintptr_t)ptr)
  269. #elif UINTPTR_MAX == UINT64_MAX
  270. #define STORE_PTR(addr, ptr) STORE_I64(addr, (uintptr_t)ptr)
  271. #endif
  272. #endif /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
  273. typedef struct WASMModuleCommon {
  274. /* Module type, for module loaded from WASM bytecode binary,
  275. this field is Wasm_Module_Bytecode, and this structure should
  276. be treated as WASMModule structure;
  277. for module loaded from AOT binary, this field is
  278. Wasm_Module_AoT, and this structure should be treated as
  279. AOTModule structure. */
  280. uint32 module_type;
  281. /* The following uint8[1] member is a dummy just to indicate
  282. some module_type dependent members follow.
  283. Typically it should be accessed by casting to the corresponding
  284. actual module_type dependent structure, not via this member. */
  285. uint8 module_data[1];
  286. } WASMModuleCommon;
  287. typedef struct WASMModuleInstanceCommon {
  288. /* Module instance type, for module instance loaded from WASM
  289. bytecode binary, this field is Wasm_Module_Bytecode, and this
  290. structure should be treated as WASMModuleInstance structure;
  291. for module instance loaded from AOT binary, this field is
  292. Wasm_Module_AoT, and this structure should be treated as
  293. AOTModuleInstance structure. */
  294. uint32 module_type;
  295. /* The following uint8[1] member is a dummy just to indicate
  296. some module_type dependent members follow.
  297. Typically it should be accessed by casting to the corresponding
  298. actual module_type dependent structure, not via this member. */
  299. uint8 module_inst_data[1];
  300. } WASMModuleInstanceCommon;
  301. typedef struct WASMModuleMemConsumption {
  302. uint32 total_size;
  303. uint32 module_struct_size;
  304. uint32 types_size;
  305. uint32 imports_size;
  306. uint32 functions_size;
  307. uint32 tables_size;
  308. uint32 memories_size;
  309. uint32 globals_size;
  310. uint32 exports_size;
  311. uint32 table_segs_size;
  312. uint32 data_segs_size;
  313. uint32 const_strs_size;
  314. #if WASM_ENABLE_AOT != 0
  315. uint32 aot_code_size;
  316. #endif
  317. } WASMModuleMemConsumption;
  318. typedef struct WASMModuleInstMemConsumption {
  319. uint32 total_size;
  320. uint32 module_inst_struct_size;
  321. uint32 memories_size;
  322. uint32 app_heap_size;
  323. uint32 tables_size;
  324. uint32 globals_size;
  325. uint32 functions_size;
  326. uint32 exports_size;
  327. } WASMModuleInstMemConsumption;
  328. #if WASM_ENABLE_LIBC_WASI != 0
  329. #if WASM_ENABLE_UVWASI == 0
  330. typedef struct WASIContext {
  331. struct fd_table *curfds;
  332. struct fd_prestats *prestats;
  333. struct argv_environ_values *argv_environ;
  334. struct addr_pool *addr_pool;
  335. char *ns_lookup_buf;
  336. char **ns_lookup_list;
  337. char *argv_buf;
  338. char **argv_list;
  339. char *env_buf;
  340. char **env_list;
  341. uint32_t exit_code;
  342. } WASIContext;
  343. #else
  344. typedef struct WASIContext {
  345. uvwasi_t uvwasi;
  346. uint32_t exit_code;
  347. } WASIContext;
  348. #endif
  349. #endif
  350. #if WASM_ENABLE_MULTI_MODULE != 0
  351. typedef struct WASMRegisteredModule {
  352. bh_list_link l;
  353. /* point to a string pool */
  354. const char *module_name;
  355. WASMModuleCommon *module;
  356. /* to store the original module file buffer address */
  357. uint8 *orig_file_buf;
  358. uint32 orig_file_buf_size;
  359. } WASMRegisteredModule;
  360. #endif
  361. typedef struct WASMMemoryInstanceCommon {
  362. uint32 module_type;
  363. /* The following uint8[1] member is a dummy just to indicate
  364. some module_type dependent members follow.
  365. Typically it should be accessed by casting to the corresponding
  366. actual module_type dependent structure, not via this member. */
  367. uint8 memory_inst_data[1];
  368. } WASMMemoryInstanceCommon;
  369. typedef package_type_t PackageType;
  370. typedef wasm_section_t WASMSection, AOTSection;
  371. typedef struct wasm_frame_t {
  372. /* wasm_instance_t */
  373. void *instance;
  374. uint32 module_offset;
  375. uint32 func_index;
  376. uint32 func_offset;
  377. const char *func_name_wp;
  378. } WASMCApiFrame;
  379. #ifdef WASM_ENABLE_JIT
  380. typedef struct LLVMJITOptions {
  381. uint32 opt_level;
  382. uint32 size_level;
  383. uint32 segue_flags;
  384. } LLVMJITOptions;
  385. #endif
  386. #ifdef OS_ENABLE_HW_BOUND_CHECK
  387. /* Signal info passing to interp/aot signal handler */
  388. typedef struct WASMSignalInfo {
  389. WASMExecEnv *exec_env_tls;
  390. #ifndef BH_PLATFORM_WINDOWS
  391. void *sig_addr;
  392. #else
  393. EXCEPTION_POINTERS *exce_info;
  394. #endif
  395. } WASMSignalInfo;
  396. /* Set exec_env of thread local storage */
  397. void
  398. wasm_runtime_set_exec_env_tls(WASMExecEnv *exec_env);
  399. /* Get exec_env of thread local storage */
  400. WASMExecEnv *
  401. wasm_runtime_get_exec_env_tls(void);
  402. #endif
  403. /* See wasm_export.h for description */
  404. WASM_RUNTIME_API_EXTERN bool
  405. wasm_runtime_init(void);
  406. /* Internal API */
  407. RunningMode
  408. wasm_runtime_get_default_running_mode(void);
  409. #if WASM_ENABLE_JIT != 0
  410. /* Internal API */
  411. LLVMJITOptions
  412. wasm_runtime_get_llvm_jit_options(void);
  413. #endif
  414. /* See wasm_export.h for description */
  415. WASM_RUNTIME_API_EXTERN bool
  416. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  417. /* See wasm_export.h for description */
  418. WASM_RUNTIME_API_EXTERN bool
  419. wasm_runtime_is_running_mode_supported(RunningMode running_mode);
  420. /* See wasm_export.h for description */
  421. WASM_RUNTIME_API_EXTERN bool
  422. wasm_runtime_set_default_running_mode(RunningMode running_mode);
  423. /* See wasm_export.h for description */
  424. WASM_RUNTIME_API_EXTERN void
  425. wasm_runtime_destroy(void);
  426. /* See wasm_export.h for description */
  427. WASM_RUNTIME_API_EXTERN PackageType
  428. get_package_type(const uint8 *buf, uint32 size);
  429. /* See wasm_export.h for description */
  430. WASM_RUNTIME_API_EXTERN bool
  431. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size);
  432. /* See wasm_export.h for description */
  433. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  434. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  435. uint32 error_buf_size);
  436. /* See wasm_export.h for description */
  437. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  438. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  439. char *error_buf, uint32 error_buf_size);
  440. /* See wasm_export.h for description */
  441. WASM_RUNTIME_API_EXTERN void
  442. wasm_runtime_unload(WASMModuleCommon *module);
  443. /* Internal API */
  444. WASMModuleInstanceCommon *
  445. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  446. WASMExecEnv *exec_env_main, uint32 stack_size,
  447. uint32 heap_size, char *error_buf,
  448. uint32 error_buf_size);
  449. /* Internal API */
  450. void
  451. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  452. bool is_sub_inst);
  453. /* See wasm_export.h for description */
  454. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  455. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 default_stack_size,
  456. uint32 host_managed_heap_size, char *error_buf,
  457. uint32 error_buf_size);
  458. /* See wasm_export.h for description */
  459. WASM_RUNTIME_API_EXTERN bool
  460. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  461. RunningMode running_mode);
  462. /* See wasm_export.h for description */
  463. WASM_RUNTIME_API_EXTERN RunningMode
  464. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
  465. /* See wasm_export.h for description */
  466. WASM_RUNTIME_API_EXTERN void
  467. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  468. /* See wasm_export.h for description */
  469. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  470. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst);
  471. /* See wasm_export.h for description */
  472. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  473. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  474. const char *name, const char *signature);
  475. /* Internal API */
  476. WASMType *
  477. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  478. uint32 module_type);
  479. /* See wasm_export.h for description */
  480. WASM_RUNTIME_API_EXTERN uint32
  481. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  482. WASMModuleInstanceCommon *const module_inst);
  483. /* See wasm_export.h for description */
  484. WASM_RUNTIME_API_EXTERN uint32
  485. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  486. WASMModuleInstanceCommon *const module_inst);
  487. /* See wasm_export.h for description */
  488. WASM_RUNTIME_API_EXTERN void
  489. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  490. WASMModuleInstanceCommon *const module_inst,
  491. wasm_valkind_t *param_types);
  492. /* See wasm_export.h for description */
  493. WASM_RUNTIME_API_EXTERN void
  494. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  495. WASMModuleInstanceCommon *const module_inst,
  496. wasm_valkind_t *result_types);
  497. /* See wasm_export.h for description */
  498. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  499. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  500. uint32 stack_size);
  501. /* See wasm_export.h for description */
  502. WASM_RUNTIME_API_EXTERN void
  503. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  504. /* See wasm_export.h for description */
  505. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  506. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  507. /* See wasm_export.h for description */
  508. WASM_RUNTIME_API_EXTERN void
  509. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  510. WASMModuleInstanceCommon *const module_inst);
  511. /* See wasm_export.h for description */
  512. WASM_RUNTIME_API_EXTERN void *
  513. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  514. /* See wasm_export.h for description */
  515. WASM_RUNTIME_API_EXTERN void
  516. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  517. /* See wasm_export.h for description */
  518. WASM_RUNTIME_API_EXTERN void *
  519. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  520. #ifdef OS_ENABLE_HW_BOUND_CHECK
  521. /* Access exception check guard page to trigger the signal handler */
  522. void
  523. wasm_runtime_access_exce_check_guard_page();
  524. #endif
  525. /* See wasm_export.h for description */
  526. WASM_RUNTIME_API_EXTERN bool
  527. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  528. WASMFunctionInstanceCommon *function, uint32 argc,
  529. uint32 argv[]);
  530. WASM_RUNTIME_API_EXTERN bool
  531. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  532. WASMFunctionInstanceCommon *function,
  533. uint32 num_results, wasm_val_t *results,
  534. uint32 num_args, wasm_val_t *args);
  535. WASM_RUNTIME_API_EXTERN bool
  536. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  537. WASMFunctionInstanceCommon *function,
  538. uint32 num_results, wasm_val_t *results,
  539. uint32 num_args, ...);
  540. /* See wasm_export.h for description */
  541. WASM_RUNTIME_API_EXTERN bool
  542. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  543. uint32 argc, uint32 argv[]);
  544. #if WASM_ENABLE_DEBUG_INTERP != 0
  545. /* See wasm_export.h for description */
  546. WASM_RUNTIME_API_EXTERN uint32
  547. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
  548. int32_t port);
  549. /* See wasm_export.h for description */
  550. WASM_RUNTIME_API_EXTERN uint32
  551. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
  552. #endif
  553. bool
  554. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  555. /* See wasm_export.h for description */
  556. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  557. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  558. /* See wasm_export.h for description */
  559. WASM_RUNTIME_API_EXTERN bool
  560. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  561. char *argv[]);
  562. /* See wasm_export.h for description */
  563. WASM_RUNTIME_API_EXTERN bool
  564. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  565. const char *name, int32 argc, char *argv[]);
  566. /* See wasm_export.h for description */
  567. WASM_RUNTIME_API_EXTERN void
  568. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  569. const char *exception);
  570. /* See wasm_export.h for description */
  571. WASM_RUNTIME_API_EXTERN const char *
  572. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  573. /* See wasm_export.h for description */
  574. WASM_RUNTIME_API_EXTERN void
  575. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  576. /* Internal API */
  577. void
  578. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  579. void *custom_data);
  580. /* See wasm_export.h for description */
  581. WASM_RUNTIME_API_EXTERN void
  582. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  583. void *custom_data);
  584. /* See wasm_export.h for description */
  585. WASM_RUNTIME_API_EXTERN void *
  586. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  587. /* Internal API */
  588. uint32
  589. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  590. WASMExecEnv *exec_env, uint32 size,
  591. void **p_native_addr);
  592. /* Internal API */
  593. uint32
  594. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  595. WASMExecEnv *exec_env, uint32 ptr,
  596. uint32 size, void **p_native_addr);
  597. /* Internal API */
  598. void
  599. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  600. WASMExecEnv *exec_env, uint32 ptr);
  601. /* See wasm_export.h for description */
  602. WASM_RUNTIME_API_EXTERN uint32
  603. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  604. void **p_native_addr);
  605. /* See wasm_export.h for description */
  606. WASM_RUNTIME_API_EXTERN void
  607. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  608. /* See wasm_export.h for description */
  609. WASM_RUNTIME_API_EXTERN uint32
  610. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  611. const char *src, uint32 size);
  612. /* See wasm_export.h for description */
  613. WASM_RUNTIME_API_EXTERN bool
  614. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  615. uint32 app_offset, uint32 size);
  616. /* See wasm_export.h for description */
  617. WASM_RUNTIME_API_EXTERN bool
  618. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  619. uint32 app_str_offset);
  620. /* See wasm_export.h for description */
  621. WASM_RUNTIME_API_EXTERN bool
  622. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  623. void *native_ptr, uint32 size);
  624. /* See wasm_export.h for description */
  625. WASM_RUNTIME_API_EXTERN void *
  626. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  627. uint32 app_offset);
  628. /* See wasm_export.h for description */
  629. WASM_RUNTIME_API_EXTERN uint32
  630. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  631. void *native_ptr);
  632. /* See wasm_export.h for description */
  633. WASM_RUNTIME_API_EXTERN bool
  634. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  635. uint32 app_offset, uint32 *p_app_start_offset,
  636. uint32 *p_app_end_offset);
  637. /* See wasm_export.h for description */
  638. WASM_RUNTIME_API_EXTERN bool
  639. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  640. uint8 *native_ptr,
  641. uint8 **p_native_start_addr,
  642. uint8 **p_native_end_addr);
  643. /* See wasm_export.h for description */
  644. WASM_RUNTIME_API_EXTERN const uint8 *
  645. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  646. const char *name, uint32 *len);
  647. #if WASM_ENABLE_MULTI_MODULE != 0
  648. WASM_RUNTIME_API_EXTERN void
  649. wasm_runtime_set_module_reader(const module_reader reader,
  650. const module_destroyer destroyer);
  651. module_reader
  652. wasm_runtime_get_module_reader();
  653. module_destroyer
  654. wasm_runtime_get_module_destroyer();
  655. bool
  656. wasm_runtime_register_module_internal(const char *module_name,
  657. WASMModuleCommon *module,
  658. uint8 *orig_file_buf,
  659. uint32 orig_file_buf_size,
  660. char *error_buf, uint32 error_buf_size);
  661. void
  662. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  663. bool
  664. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  665. uint32 error_buf_size);
  666. void
  667. wasm_runtime_delete_loading_module(const char *module_name);
  668. bool
  669. wasm_runtime_is_loading_module(const char *module_name);
  670. void
  671. wasm_runtime_destroy_loading_module_list();
  672. #endif /* WASM_ENALBE_MULTI_MODULE */
  673. bool
  674. wasm_runtime_is_built_in_module(const char *module_name);
  675. #if WASM_ENABLE_THREAD_MGR != 0
  676. bool
  677. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  678. uint32 *size);
  679. bool
  680. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  681. uint32 size);
  682. #endif
  683. #if WASM_ENABLE_LIBC_WASI != 0
  684. WASM_RUNTIME_API_EXTERN void
  685. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  686. uint32 dir_count, const char *map_dir_list[],
  687. uint32 map_dir_count, const char *env_list[],
  688. uint32 env_count, char *argv[], int argc,
  689. int stdinfd, int stdoutfd, int stderrfd);
  690. /* See wasm_export.h for description */
  691. WASM_RUNTIME_API_EXTERN void
  692. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  693. uint32 dir_count, const char *map_dir_list[],
  694. uint32 map_dir_count, const char *env_list[],
  695. uint32 env_count, char *argv[], int argc);
  696. /* See wasm_export.h for description */
  697. WASM_RUNTIME_API_EXTERN bool
  698. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  699. /* See wasm_export.h for description */
  700. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  701. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  702. /* See wasm_export.h for description */
  703. WASM_RUNTIME_API_EXTERN uint32_t
  704. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst);
  705. bool
  706. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  707. const char *dir_list[], uint32 dir_count,
  708. const char *map_dir_list[], uint32 map_dir_count,
  709. const char *env[], uint32 env_count,
  710. const char *addr_pool[], uint32 addr_pool_size,
  711. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  712. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  713. int stderrfd, char *error_buf, uint32 error_buf_size);
  714. void
  715. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  716. void
  717. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  718. WASIContext *wasi_ctx);
  719. WASIContext *
  720. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  721. WASM_RUNTIME_API_EXTERN void
  722. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  723. uint32 addr_pool_size);
  724. WASM_RUNTIME_API_EXTERN void
  725. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  726. const char *ns_lookup_pool[],
  727. uint32 ns_lookup_pool_size);
  728. #endif /* end of WASM_ENABLE_LIBC_WASI */
  729. #if WASM_ENABLE_REF_TYPES != 0
  730. /* See wasm_export.h for description */
  731. WASM_RUNTIME_API_EXTERN bool
  732. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  733. uint32 *p_externref_idx);
  734. /* See wasm_export.h for description */
  735. WASM_RUNTIME_API_EXTERN bool
  736. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  737. /* See wasm_export.h for description */
  738. WASM_RUNTIME_API_EXTERN bool
  739. wasm_externref_retain(uint32 externref_idx);
  740. /**
  741. * Reclaim the externref objects/indexes which are not used by
  742. * module instance
  743. */
  744. void
  745. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  746. /**
  747. * Cleanup the externref objects/indexes of the module instance
  748. */
  749. void
  750. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  751. #endif /* end of WASM_ENABLE_REF_TYPES */
  752. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  753. /**
  754. * @brief Internal implementation for dumping or printing callstack line
  755. *
  756. * @note if dump_or_print is true, then print to stdout directly;
  757. * if dump_or_print is false, but *buf is NULL, then return the length of the
  758. * line;
  759. * if dump_or_print is false, and *buf is not NULL, then dump content to
  760. * the memory pointed by *buf, and adjust *buf and *len according to actual
  761. * bytes dumped, and return the actual dumped length
  762. *
  763. * @param line_buf current line to dump or print
  764. * @param dump_or_print whether to print to stdout or dump to buf
  765. * @param buf [INOUT] pointer to the buffer
  766. * @param len [INOUT] pointer to remaining length
  767. * @return bytes printed to stdout or dumped to buf
  768. */
  769. uint32
  770. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  771. char **buf, uint32 *len);
  772. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 */
  773. /* Get module of the current exec_env */
  774. WASMModuleCommon *
  775. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  776. /* See wasm_export.h for description */
  777. WASM_RUNTIME_API_EXTERN bool
  778. wasm_runtime_register_natives(const char *module_name,
  779. NativeSymbol *native_symbols,
  780. uint32 n_native_symbols);
  781. /* See wasm_export.h for description */
  782. WASM_RUNTIME_API_EXTERN bool
  783. wasm_runtime_register_natives_raw(const char *module_name,
  784. NativeSymbol *native_symbols,
  785. uint32 n_native_symbols);
  786. /* See wasm_export.h for description */
  787. WASM_RUNTIME_API_EXTERN bool
  788. wasm_runtime_unregister_natives(const char *module_name,
  789. NativeSymbol *native_symbols);
  790. bool
  791. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  792. const WASMType *func_type, const char *signature,
  793. void *attachment, uint32 *argv, uint32 argc,
  794. uint32 *ret);
  795. bool
  796. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  797. const WASMType *func_type, const char *signature,
  798. void *attachment, uint32 *argv, uint32 argc,
  799. uint32 *ret);
  800. void
  801. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  802. void
  803. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  804. void
  805. wasm_runtime_dump_module_inst_mem_consumption(
  806. const WASMModuleInstanceCommon *module_inst);
  807. void
  808. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  809. bool
  810. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  811. uint32 table_idx, uint8 *out_elem_type,
  812. uint32 *out_min_size, uint32 *out_max_size);
  813. bool
  814. wasm_runtime_get_table_inst_elem_type(
  815. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  816. uint8 *out_elem_type, uint32 *out_min_size, uint32 *out_max_size);
  817. bool
  818. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  819. const WASMExport *export_, WASMType **out);
  820. bool
  821. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  822. const WASMExport *export_,
  823. uint8 *out_val_type, bool *out_mutability);
  824. bool
  825. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  826. const WASMExport *export_,
  827. uint32 *out_min_page, uint32 *out_max_page);
  828. bool
  829. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  830. const WASMExport *export_,
  831. uint8 *out_elem_type, uint32 *out_min_size,
  832. uint32 *out_max_size);
  833. bool
  834. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  835. void *func_ptr, WASMType *func_type,
  836. uint32 argc, uint32 *argv, bool with_env,
  837. void *wasm_c_api_env);
  838. void
  839. wasm_runtime_show_app_heap_corrupted_prompt();
  840. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  841. void
  842. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list);
  843. #endif
  844. WASM_RUNTIME_API_EXTERN bool
  845. wasm_runtime_is_import_func_linked(const char *module_name,
  846. const char *func_name);
  847. WASM_RUNTIME_API_EXTERN bool
  848. wasm_runtime_is_import_global_linked(const char *module_name,
  849. const char *global_name);
  850. #ifdef __cplusplus
  851. }
  852. #endif
  853. #endif /* end of _WASM_COMMON_H */