wasm_runtime_common.h 44 KB

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