wasm_runtime_common.h 37 KB

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