wasm_runtime_common.h 46 KB

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