wasm_runtime_common.h 35 KB

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