wasm_runtime_common.h 43 KB

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