wasm_runtime_common.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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. };
  565. /* See wasm_export.h for description */
  566. WASM_RUNTIME_API_EXTERN bool
  567. wasm_runtime_init(void);
  568. /* Internal API */
  569. RunningMode
  570. wasm_runtime_get_default_running_mode(void);
  571. #if WASM_ENABLE_JIT != 0
  572. /* Internal API */
  573. LLVMJITOptions *
  574. wasm_runtime_get_llvm_jit_options(void);
  575. #endif
  576. #if WASM_ENABLE_GC != 0
  577. /* Internal API */
  578. uint32
  579. wasm_runtime_get_gc_heap_size_default(void);
  580. #endif
  581. /* See wasm_export.h for description */
  582. WASM_RUNTIME_API_EXTERN bool
  583. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  584. /* See wasm_export.h for description */
  585. WASM_RUNTIME_API_EXTERN bool
  586. wasm_runtime_is_running_mode_supported(RunningMode running_mode);
  587. /* See wasm_export.h for description */
  588. WASM_RUNTIME_API_EXTERN bool
  589. wasm_runtime_set_default_running_mode(RunningMode running_mode);
  590. /* See wasm_export.h for description */
  591. WASM_RUNTIME_API_EXTERN void
  592. wasm_runtime_destroy(void);
  593. /* See wasm_export.h for description */
  594. WASM_RUNTIME_API_EXTERN PackageType
  595. get_package_type(const uint8 *buf, uint32 size);
  596. /* See wasm_export.h for description */
  597. WASM_RUNTIME_API_EXTERN bool
  598. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size);
  599. /* See wasm_export.h for description */
  600. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  601. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  602. uint32 error_buf_size);
  603. /* See wasm_export.h for description */
  604. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  605. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  606. char *error_buf, uint32 error_buf_size);
  607. /* See wasm_export.h for description */
  608. WASM_RUNTIME_API_EXTERN void
  609. wasm_runtime_unload(WASMModuleCommon *module);
  610. /* Internal API */
  611. uint32
  612. wasm_runtime_get_max_mem(uint32 max_memory_pages, uint32 module_init_page_count,
  613. uint32 module_max_page_count);
  614. /* Internal API */
  615. WASMModuleInstanceCommon *
  616. wasm_runtime_instantiate_internal(WASMModuleCommon *module,
  617. WASMModuleInstanceCommon *parent,
  618. WASMExecEnv *exec_env_main, uint32 stack_size,
  619. uint32 heap_size, uint32 max_memory_pages,
  620. char *error_buf, uint32 error_buf_size);
  621. /* Internal API */
  622. void
  623. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  624. bool is_sub_inst);
  625. /* See wasm_export.h for description */
  626. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  627. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 default_stack_size,
  628. uint32 host_managed_heap_size, char *error_buf,
  629. uint32 error_buf_size);
  630. /* See wasm_export.h for description */
  631. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  632. wasm_runtime_instantiate_ex(WASMModuleCommon *module,
  633. const InstantiationArgs *args, char *error_buf,
  634. uint32 error_buf_size);
  635. /* See wasm_export.h for description */
  636. WASM_RUNTIME_API_EXTERN
  637. bool
  638. wasm_runtime_instantiation_args_create(struct InstantiationArgs2 **p);
  639. /* See wasm_export.h for description */
  640. WASM_RUNTIME_API_EXTERN
  641. void
  642. wasm_runtime_instantiation_args_destroy(struct InstantiationArgs2 *p);
  643. /* See wasm_export.h for description */
  644. WASM_RUNTIME_API_EXTERN
  645. void
  646. wasm_runtime_instantiation_args_set_default_stack_size(
  647. struct InstantiationArgs2 *p, uint32 v);
  648. /* See wasm_export.h for description */
  649. WASM_RUNTIME_API_EXTERN
  650. void
  651. wasm_runtime_instantiation_args_set_host_managed_heap_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_max_memory_pages(
  657. struct InstantiationArgs2 *p, uint32 v);
  658. /* See wasm_export.h for description */
  659. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  660. wasm_runtime_instantiate_ex2(WASMModuleCommon *module,
  661. const struct InstantiationArgs2 *args,
  662. char *error_buf, uint32 error_buf_size);
  663. /* See wasm_export.h for description */
  664. WASM_RUNTIME_API_EXTERN bool
  665. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  666. RunningMode running_mode);
  667. /* See wasm_export.h for description */
  668. WASM_RUNTIME_API_EXTERN RunningMode
  669. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
  670. /* See wasm_export.h for description */
  671. WASM_RUNTIME_API_EXTERN void
  672. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  673. /* See wasm_export.h for description */
  674. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  675. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst);
  676. /* See wasm_export.h for description */
  677. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  678. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  679. const char *name);
  680. /* Internal API */
  681. WASMFuncType *
  682. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  683. uint32 module_type);
  684. /* See wasm_export.h for description */
  685. WASM_RUNTIME_API_EXTERN uint32
  686. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  687. WASMModuleInstanceCommon *const module_inst);
  688. /* See wasm_export.h for description */
  689. WASM_RUNTIME_API_EXTERN uint32
  690. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  691. WASMModuleInstanceCommon *const module_inst);
  692. /* See wasm_export.h for description */
  693. WASM_RUNTIME_API_EXTERN void
  694. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  695. WASMModuleInstanceCommon *const module_inst,
  696. wasm_valkind_t *param_types);
  697. /* See wasm_export.h for description */
  698. WASM_RUNTIME_API_EXTERN void
  699. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  700. WASMModuleInstanceCommon *const module_inst,
  701. wasm_valkind_t *result_types);
  702. /* See wasm_export.h for description */
  703. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  704. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  705. uint32 stack_size);
  706. /* See wasm_export.h for description */
  707. WASM_RUNTIME_API_EXTERN void
  708. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  709. #if WASM_ENABLE_COPY_CALL_STACK != 0
  710. WASM_RUNTIME_API_EXTERN uint32_t
  711. wasm_copy_callstack(const wasm_exec_env_t exec_env, WASMCApiFrame *buffer,
  712. const uint32 length, const uint32 skip_n, char *error_buf,
  713. uint32 error_buf_size);
  714. #endif // WASM_ENABLE_COPY_CALL_STACK
  715. /* See wasm_export.h for description */
  716. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  717. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  718. /* See wasm_export.h for description */
  719. WASM_RUNTIME_API_EXTERN void
  720. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  721. WASMModuleInstanceCommon *const module_inst);
  722. /* See wasm_export.h for description */
  723. WASM_RUNTIME_API_EXTERN void *
  724. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  725. /* See wasm_export.h for description */
  726. WASM_RUNTIME_API_EXTERN void
  727. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  728. /* See wasm_export.h for description */
  729. WASM_RUNTIME_API_EXTERN void *
  730. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  731. /* See wasm_export.h for description */
  732. WASM_RUNTIME_API_EXTERN void
  733. wasm_runtime_set_native_stack_boundary(WASMExecEnv *exec_env,
  734. uint8 *native_stack_boundary);
  735. #if WASM_ENABLE_INSTRUCTION_METERING != 0
  736. /* See wasm_export.h for description */
  737. WASM_RUNTIME_API_EXTERN void
  738. wasm_runtime_set_instruction_count_limit(WASMExecEnv *exec_env,
  739. int instructions_to_execute);
  740. #endif
  741. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  742. /* See wasm_export.h for description */
  743. WASM_RUNTIME_API_EXTERN
  744. void
  745. wasm_runtime_set_bounds_checks(WASMModuleInstanceCommon *module_inst,
  746. bool enable);
  747. /* See wasm_export.h for description */
  748. WASM_RUNTIME_API_EXTERN bool
  749. wasm_runtime_is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst);
  750. #endif
  751. #ifdef OS_ENABLE_HW_BOUND_CHECK
  752. /* Access exception check guard page to trigger the signal handler */
  753. void
  754. wasm_runtime_access_exce_check_guard_page();
  755. #endif
  756. /* See wasm_export.h for description */
  757. WASM_RUNTIME_API_EXTERN bool
  758. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  759. WASMFunctionInstanceCommon *function, uint32 argc,
  760. uint32 argv[]);
  761. WASM_RUNTIME_API_EXTERN bool
  762. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  763. WASMFunctionInstanceCommon *function,
  764. uint32 num_results, wasm_val_t *results,
  765. uint32 num_args, wasm_val_t *args);
  766. WASM_RUNTIME_API_EXTERN bool
  767. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  768. WASMFunctionInstanceCommon *function,
  769. uint32 num_results, wasm_val_t *results,
  770. uint32 num_args, ...);
  771. /* See wasm_export.h for description */
  772. WASM_RUNTIME_API_EXTERN bool
  773. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  774. uint32 argc, uint32 argv[]);
  775. #if WASM_ENABLE_DEBUG_INTERP != 0
  776. /* See wasm_export.h for description */
  777. WASM_RUNTIME_API_EXTERN uint32
  778. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
  779. int32_t port);
  780. /* See wasm_export.h for description */
  781. WASM_RUNTIME_API_EXTERN uint32
  782. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
  783. #endif
  784. bool
  785. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  786. /* See wasm_export.h for description */
  787. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  788. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  789. /* See wasm_export.h for description */
  790. WASM_RUNTIME_API_EXTERN bool
  791. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  792. char *argv[]);
  793. /* See wasm_export.h for description */
  794. WASM_RUNTIME_API_EXTERN bool
  795. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  796. const char *name, int32 argc, char *argv[]);
  797. /* See wasm_export.h for description */
  798. WASM_RUNTIME_API_EXTERN void
  799. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  800. const char *exception);
  801. /* See wasm_export.h for description */
  802. WASM_RUNTIME_API_EXTERN const char *
  803. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  804. /* See wasm_export.h for description */
  805. WASM_RUNTIME_API_EXTERN void
  806. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  807. /* See wasm_export.h for description */
  808. WASM_RUNTIME_API_EXTERN void
  809. wasm_runtime_terminate(WASMModuleInstanceCommon *module);
  810. /* Internal API */
  811. void
  812. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  813. void *custom_data);
  814. /* See wasm_export.h for description */
  815. WASM_RUNTIME_API_EXTERN void
  816. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  817. void *custom_data);
  818. /* See wasm_export.h for description */
  819. WASM_RUNTIME_API_EXTERN void *
  820. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  821. /* Internal API */
  822. uint64
  823. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  824. WASMExecEnv *exec_env, uint64 size,
  825. void **p_native_addr);
  826. /* Internal API */
  827. uint64
  828. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  829. WASMExecEnv *exec_env, uint64 ptr,
  830. uint64 size, void **p_native_addr);
  831. /* Internal API */
  832. void
  833. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  834. WASMExecEnv *exec_env, uint64 ptr);
  835. /* See wasm_export.h for description */
  836. WASM_RUNTIME_API_EXTERN uint64
  837. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint64 size,
  838. void **p_native_addr);
  839. /* See wasm_export.h for description */
  840. WASM_RUNTIME_API_EXTERN void
  841. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint64 ptr);
  842. /* See wasm_export.h for description */
  843. WASM_RUNTIME_API_EXTERN uint64
  844. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  845. const char *src, uint64 size);
  846. /* See wasm_export.h for description */
  847. WASM_RUNTIME_API_EXTERN bool
  848. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  849. uint64 app_offset, uint64 size);
  850. /* See wasm_export.h for description */
  851. WASM_RUNTIME_API_EXTERN bool
  852. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  853. uint64 app_str_offset);
  854. /* See wasm_export.h for description */
  855. WASM_RUNTIME_API_EXTERN bool
  856. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  857. void *native_ptr, uint64 size);
  858. /* See wasm_export.h for description */
  859. WASM_RUNTIME_API_EXTERN void *
  860. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  861. uint64 app_offset);
  862. /* See wasm_export.h for description */
  863. WASM_RUNTIME_API_EXTERN uint64
  864. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  865. void *native_ptr);
  866. /* See wasm_export.h for description */
  867. WASM_RUNTIME_API_EXTERN bool
  868. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  869. uint64 app_offset, uint64 *p_app_start_offset,
  870. uint64 *p_app_end_offset);
  871. /* See wasm_export.h for description */
  872. WASM_RUNTIME_API_EXTERN bool
  873. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  874. uint8 *native_ptr,
  875. uint8 **p_native_start_addr,
  876. uint8 **p_native_end_addr);
  877. /* See wasm_export.h for description */
  878. WASM_RUNTIME_API_EXTERN const uint8 *
  879. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  880. const char *name, uint32 *len);
  881. #if WASM_ENABLE_MULTI_MODULE != 0
  882. WASM_RUNTIME_API_EXTERN void
  883. wasm_runtime_set_module_reader(const module_reader reader,
  884. const module_destroyer destroyer);
  885. module_reader
  886. wasm_runtime_get_module_reader(void);
  887. module_destroyer
  888. wasm_runtime_get_module_destroyer(void);
  889. bool
  890. wasm_runtime_register_module_internal(const char *module_name,
  891. WASMModuleCommon *module,
  892. uint8 *orig_file_buf,
  893. uint32 orig_file_buf_size,
  894. char *error_buf, uint32 error_buf_size);
  895. void
  896. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  897. WASMModuleCommon *
  898. wasm_runtime_find_module_registered(const char *module_name);
  899. bool
  900. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  901. uint32 error_buf_size);
  902. void
  903. wasm_runtime_delete_loading_module(const char *module_name);
  904. bool
  905. wasm_runtime_is_loading_module(const char *module_name);
  906. void
  907. wasm_runtime_destroy_loading_module_list(void);
  908. WASMModuleCommon *
  909. wasm_runtime_search_sub_module(const WASMModuleCommon *parent_module,
  910. const char *sub_module_name);
  911. bool
  912. wasm_runtime_register_sub_module(const WASMModuleCommon *parent_module,
  913. const char *sub_module_name,
  914. WASMModuleCommon *sub_module);
  915. WASMModuleCommon *
  916. wasm_runtime_load_depended_module(const WASMModuleCommon *parent_module,
  917. const char *sub_module_name, char *error_buf,
  918. uint32 error_buf_size);
  919. bool
  920. wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
  921. WASMModuleInstanceCommon *module_inst,
  922. uint32 stack_size, uint32 heap_size,
  923. uint32 max_memory_pages, char *error_buf,
  924. uint32 error_buf_size);
  925. void
  926. wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst);
  927. #endif
  928. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  929. WASMExport *
  930. loader_find_export(const WASMModuleCommon *module, const char *module_name,
  931. const char *field_name, uint8 export_kind, char *error_buf,
  932. uint32 error_buf_size);
  933. #endif /* WASM_ENABLE_MULTI_MODULE */
  934. bool
  935. wasm_runtime_is_built_in_module(const char *module_name);
  936. #if WASM_ENABLE_THREAD_MGR != 0
  937. bool
  938. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint64 *start_offset,
  939. uint32 *size);
  940. bool
  941. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint64 start_offset,
  942. uint32 size);
  943. #endif
  944. #if WASM_ENABLE_LIBC_WASI != 0
  945. WASM_RUNTIME_API_EXTERN void
  946. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  947. uint32 dir_count, const char *map_dir_list[],
  948. uint32 map_dir_count, const char *env_list[],
  949. uint32 env_count, char *argv[], int argc,
  950. int64 stdinfd, int64 stdoutfd, int64 stderrfd);
  951. /* See wasm_export.h for description */
  952. WASM_RUNTIME_API_EXTERN void
  953. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  954. uint32 dir_count, const char *map_dir_list[],
  955. uint32 map_dir_count, const char *env_list[],
  956. uint32 env_count, char *argv[], int argc);
  957. /* See wasm_export.h for description */
  958. WASM_RUNTIME_API_EXTERN bool
  959. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  960. /* See wasm_export.h for description */
  961. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  962. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  963. /* See wasm_export.h for description */
  964. WASM_RUNTIME_API_EXTERN uint32_t
  965. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst);
  966. bool
  967. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  968. const char *dir_list[], uint32 dir_count,
  969. const char *map_dir_list[], uint32 map_dir_count,
  970. const char *env[], uint32 env_count,
  971. const char *addr_pool[], uint32 addr_pool_size,
  972. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  973. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  974. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  975. char *error_buf, uint32 error_buf_size);
  976. void
  977. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  978. void
  979. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  980. WASIContext *wasi_ctx);
  981. WASIContext *
  982. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  983. WASM_RUNTIME_API_EXTERN void
  984. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  985. uint32 addr_pool_size);
  986. WASM_RUNTIME_API_EXTERN void
  987. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  988. const char *ns_lookup_pool[],
  989. uint32 ns_lookup_pool_size);
  990. #endif /* end of WASM_ENABLE_LIBC_WASI */
  991. #if WASM_ENABLE_GC != 0
  992. void
  993. wasm_runtime_set_gc_heap_handle(WASMModuleInstanceCommon *module_inst,
  994. void *gc_heap_handle);
  995. void *
  996. wasm_runtime_get_gc_heap_handle(WASMModuleInstanceCommon *module_inst);
  997. #endif
  998. #if WASM_ENABLE_REF_TYPES != 0
  999. /* See wasm_export.h for description */
  1000. WASM_RUNTIME_API_EXTERN bool
  1001. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  1002. uint32 *p_externref_idx);
  1003. /* See wasm_export.h for description */
  1004. WASM_RUNTIME_API_EXTERN bool
  1005. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  1006. /* See wasm_export.h for description */
  1007. WASM_RUNTIME_API_EXTERN bool
  1008. wasm_externref_retain(uint32 externref_idx);
  1009. /**
  1010. * Reclaim the externref objects/indexes which are not used by
  1011. * module instance
  1012. */
  1013. void
  1014. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  1015. /**
  1016. * Cleanup the externref objects/indexes of the module instance
  1017. */
  1018. void
  1019. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  1020. #endif /* end of WASM_ENABLE_REF_TYPES */
  1021. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1022. /**
  1023. * @brief Internal implementation for dumping or printing callstack line
  1024. *
  1025. * @note if dump_or_print is true, then print to stdout directly;
  1026. * if dump_or_print is false, but *buf is NULL, then return the length of the
  1027. * line;
  1028. * if dump_or_print is false, and *buf is not NULL, then dump content to
  1029. * the memory pointed by *buf, and adjust *buf and *len according to actual
  1030. * bytes dumped, and return the actual dumped length
  1031. *
  1032. * @param line_buf current line to dump or print
  1033. * @param dump_or_print whether to print to stdout or dump to buf
  1034. * @param buf [INOUT] pointer to the buffer
  1035. * @param len [INOUT] pointer to remaining length
  1036. * @return bytes printed to stdout or dumped to buf
  1037. */
  1038. uint32
  1039. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  1040. char **buf, uint32 *len);
  1041. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 */
  1042. /* Get module of the current exec_env */
  1043. WASMModuleCommon *
  1044. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  1045. /* See wasm_export.h for description */
  1046. WASM_RUNTIME_API_EXTERN bool
  1047. wasm_runtime_register_natives(const char *module_name,
  1048. NativeSymbol *native_symbols,
  1049. uint32 n_native_symbols);
  1050. /* See wasm_export.h for description */
  1051. WASM_RUNTIME_API_EXTERN bool
  1052. wasm_runtime_register_natives_raw(const char *module_name,
  1053. NativeSymbol *native_symbols,
  1054. uint32 n_native_symbols);
  1055. /* See wasm_export.h for description */
  1056. WASM_RUNTIME_API_EXTERN bool
  1057. wasm_runtime_unregister_natives(const char *module_name,
  1058. NativeSymbol *native_symbols);
  1059. /* See wasm_export.h for description */
  1060. WASM_RUNTIME_API_EXTERN void *
  1061. wasm_runtime_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
  1062. void *ctx));
  1063. /* See wasm_export.h for description */
  1064. WASM_RUNTIME_API_EXTERN void
  1065. wasm_runtime_destroy_context_key(void *key);
  1066. /* See wasm_export.h for description */
  1067. WASM_RUNTIME_API_EXTERN void
  1068. wasm_runtime_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx);
  1069. /* See wasm_export.h for description */
  1070. WASM_RUNTIME_API_EXTERN void
  1071. wasm_runtime_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
  1072. void *ctx);
  1073. /* See wasm_export.h for description */
  1074. WASM_RUNTIME_API_EXTERN void *
  1075. wasm_runtime_get_context(WASMModuleInstanceCommon *inst, void *key);
  1076. bool
  1077. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  1078. const WASMFuncType *func_type, const char *signature,
  1079. void *attachment, uint32 *argv, uint32 argc,
  1080. uint32 *ret);
  1081. bool
  1082. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  1083. const WASMFuncType *func_type,
  1084. const char *signature, void *attachment,
  1085. uint32 *argv, uint32 argc, uint32 *ret);
  1086. void
  1087. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  1088. void
  1089. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  1090. void
  1091. wasm_runtime_dump_module_inst_mem_consumption(
  1092. const WASMModuleInstanceCommon *module_inst);
  1093. void
  1094. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  1095. bool
  1096. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  1097. uint32 table_idx, uint8 *out_elem_type,
  1098. #if WASM_ENABLE_GC != 0
  1099. WASMRefType **out_ref_type,
  1100. #endif
  1101. uint32 *out_min_size, uint32 *out_max_size);
  1102. bool
  1103. wasm_runtime_get_table_inst_elem_type(
  1104. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  1105. uint8 *out_elem_type,
  1106. #if WASM_ENABLE_GC != 0
  1107. WASMRefType **out_ref_type,
  1108. #endif
  1109. uint32 *out_min_size, uint32 *out_max_size);
  1110. bool
  1111. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  1112. const WASMExport *export_,
  1113. WASMFuncType **out);
  1114. bool
  1115. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  1116. const WASMExport *export_,
  1117. uint8 *out_val_type, bool *out_mutability);
  1118. bool
  1119. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  1120. const WASMExport *export_,
  1121. uint32 *out_min_page, uint32 *out_max_page);
  1122. bool
  1123. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  1124. const WASMExport *export_,
  1125. uint8 *out_elem_type,
  1126. #if WASM_ENABLE_GC != 0
  1127. WASMRefType **out_ref_type,
  1128. #endif
  1129. uint32 *out_min_size, uint32 *out_max_size);
  1130. bool
  1131. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  1132. void *func_ptr, WASMFuncType *func_type,
  1133. uint32 argc, uint32 *argv, bool with_env,
  1134. void *wasm_c_api_env);
  1135. struct CApiFuncImport;
  1136. /* A quick version of wasm_runtime_invoke_c_api_native to directly invoke
  1137. wasm-c-api import function from jitted code to improve performance */
  1138. bool
  1139. wasm_runtime_quick_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  1140. struct CApiFuncImport *c_api_import,
  1141. wasm_val_t *params, uint32 param_count,
  1142. wasm_val_t *results,
  1143. uint32 result_count);
  1144. void
  1145. wasm_runtime_show_app_heap_corrupted_prompt(void);
  1146. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  1147. void
  1148. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list);
  1149. #endif
  1150. WASM_RUNTIME_API_EXTERN bool
  1151. wasm_runtime_is_import_func_linked(const char *module_name,
  1152. const char *func_name);
  1153. WASM_RUNTIME_API_EXTERN bool
  1154. wasm_runtime_is_import_global_linked(const char *module_name,
  1155. const char *global_name);
  1156. WASM_RUNTIME_API_EXTERN bool
  1157. wasm_runtime_begin_blocking_op(WASMExecEnv *exec_env);
  1158. WASM_RUNTIME_API_EXTERN void
  1159. wasm_runtime_end_blocking_op(WASMExecEnv *exec_env);
  1160. void
  1161. wasm_runtime_interrupt_blocking_op(WASMExecEnv *exec_env);
  1162. WASM_RUNTIME_API_EXTERN bool
  1163. wasm_runtime_detect_native_stack_overflow(WASMExecEnv *exec_env);
  1164. WASM_RUNTIME_API_EXTERN bool
  1165. wasm_runtime_detect_native_stack_overflow_size(WASMExecEnv *exec_env,
  1166. uint32 requested_size);
  1167. WASM_RUNTIME_API_EXTERN bool
  1168. wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module);
  1169. #if WASM_ENABLE_LINUX_PERF != 0
  1170. bool
  1171. wasm_runtime_get_linux_perf(void);
  1172. void
  1173. wasm_runtime_set_linux_perf(bool flag);
  1174. #endif
  1175. #if WASM_ENABLE_SHARED_HEAP != 0
  1176. bool
  1177. wasm_runtime_check_and_update_last_used_shared_heap(
  1178. WASMModuleInstanceCommon *module_inst, uintptr_t app_offset, size_t bytes,
  1179. uintptr_t *shared_heap_start_off_p, uintptr_t *shared_heap_end_off_p,
  1180. uint8 **shared_heap_base_addr_adj_p, bool is_memory64);
  1181. #endif
  1182. #ifdef __cplusplus
  1183. }
  1184. #endif
  1185. #endif /* end of _WASM_COMMON_H */