wasm_runtime_common.h 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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 WASM_ENABLE_COPY_CALL_STACK != 0
  679. WASM_RUNTIME_API_EXTERN uint32_t
  680. wasm_copy_callstack(const wasm_exec_env_t exec_env, WASMCApiFrame *buffer,
  681. const uint32 length, const uint32 skip_n, char *error_buf,
  682. uint32 error_buf_size);
  683. #endif // WASM_ENABLE_COPY_CALL_STACK
  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_ENABLE_INSTRUCTION_METERING != 0
  705. /* See wasm_export.h for description */
  706. WASM_RUNTIME_API_EXTERN void
  707. wasm_runtime_set_instruction_count_limit(WASMExecEnv *exec_env,
  708. int instructions_to_execute);
  709. #endif
  710. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  711. /* See wasm_export.h for description */
  712. WASM_RUNTIME_API_EXTERN
  713. void
  714. wasm_runtime_set_bounds_checks(WASMModuleInstanceCommon *module_inst,
  715. bool enable);
  716. /* See wasm_export.h for description */
  717. WASM_RUNTIME_API_EXTERN bool
  718. wasm_runtime_is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst);
  719. #endif
  720. #ifdef OS_ENABLE_HW_BOUND_CHECK
  721. /* Access exception check guard page to trigger the signal handler */
  722. void
  723. wasm_runtime_access_exce_check_guard_page();
  724. #endif
  725. /* See wasm_export.h for description */
  726. WASM_RUNTIME_API_EXTERN bool
  727. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  728. WASMFunctionInstanceCommon *function, uint32 argc,
  729. uint32 argv[]);
  730. WASM_RUNTIME_API_EXTERN bool
  731. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  732. WASMFunctionInstanceCommon *function,
  733. uint32 num_results, wasm_val_t *results,
  734. uint32 num_args, wasm_val_t *args);
  735. WASM_RUNTIME_API_EXTERN bool
  736. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  737. WASMFunctionInstanceCommon *function,
  738. uint32 num_results, wasm_val_t *results,
  739. uint32 num_args, ...);
  740. /* See wasm_export.h for description */
  741. WASM_RUNTIME_API_EXTERN bool
  742. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  743. uint32 argc, uint32 argv[]);
  744. #if WASM_ENABLE_DEBUG_INTERP != 0
  745. /* See wasm_export.h for description */
  746. WASM_RUNTIME_API_EXTERN uint32
  747. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
  748. int32_t port);
  749. /* See wasm_export.h for description */
  750. WASM_RUNTIME_API_EXTERN uint32
  751. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
  752. #endif
  753. bool
  754. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  755. /* See wasm_export.h for description */
  756. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  757. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  758. /* See wasm_export.h for description */
  759. WASM_RUNTIME_API_EXTERN bool
  760. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  761. char *argv[]);
  762. /* See wasm_export.h for description */
  763. WASM_RUNTIME_API_EXTERN bool
  764. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  765. const char *name, int32 argc, char *argv[]);
  766. /* See wasm_export.h for description */
  767. WASM_RUNTIME_API_EXTERN void
  768. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  769. const char *exception);
  770. /* See wasm_export.h for description */
  771. WASM_RUNTIME_API_EXTERN const char *
  772. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  773. /* See wasm_export.h for description */
  774. WASM_RUNTIME_API_EXTERN void
  775. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  776. /* See wasm_export.h for description */
  777. WASM_RUNTIME_API_EXTERN void
  778. wasm_runtime_terminate(WASMModuleInstanceCommon *module);
  779. /* Internal API */
  780. void
  781. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  782. void *custom_data);
  783. /* See wasm_export.h for description */
  784. WASM_RUNTIME_API_EXTERN void
  785. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  786. void *custom_data);
  787. /* See wasm_export.h for description */
  788. WASM_RUNTIME_API_EXTERN void *
  789. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  790. /* Internal API */
  791. uint64
  792. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  793. WASMExecEnv *exec_env, uint64 size,
  794. void **p_native_addr);
  795. /* Internal API */
  796. uint64
  797. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  798. WASMExecEnv *exec_env, uint64 ptr,
  799. uint64 size, void **p_native_addr);
  800. /* Internal API */
  801. void
  802. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  803. WASMExecEnv *exec_env, uint64 ptr);
  804. /* See wasm_export.h for description */
  805. WASM_RUNTIME_API_EXTERN uint64
  806. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint64 size,
  807. void **p_native_addr);
  808. /* See wasm_export.h for description */
  809. WASM_RUNTIME_API_EXTERN void
  810. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint64 ptr);
  811. /* See wasm_export.h for description */
  812. WASM_RUNTIME_API_EXTERN uint64
  813. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  814. const char *src, uint64 size);
  815. /* See wasm_export.h for description */
  816. WASM_RUNTIME_API_EXTERN bool
  817. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  818. uint64 app_offset, uint64 size);
  819. /* See wasm_export.h for description */
  820. WASM_RUNTIME_API_EXTERN bool
  821. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  822. uint64 app_str_offset);
  823. /* See wasm_export.h for description */
  824. WASM_RUNTIME_API_EXTERN bool
  825. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  826. void *native_ptr, uint64 size);
  827. /* See wasm_export.h for description */
  828. WASM_RUNTIME_API_EXTERN void *
  829. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  830. uint64 app_offset);
  831. /* See wasm_export.h for description */
  832. WASM_RUNTIME_API_EXTERN uint64
  833. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  834. void *native_ptr);
  835. /* See wasm_export.h for description */
  836. WASM_RUNTIME_API_EXTERN bool
  837. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  838. uint64 app_offset, uint64 *p_app_start_offset,
  839. uint64 *p_app_end_offset);
  840. /* See wasm_export.h for description */
  841. WASM_RUNTIME_API_EXTERN bool
  842. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  843. uint8 *native_ptr,
  844. uint8 **p_native_start_addr,
  845. uint8 **p_native_end_addr);
  846. /* See wasm_export.h for description */
  847. WASM_RUNTIME_API_EXTERN const uint8 *
  848. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  849. const char *name, uint32 *len);
  850. #if WASM_ENABLE_MULTI_MODULE != 0
  851. WASM_RUNTIME_API_EXTERN void
  852. wasm_runtime_set_module_reader(const module_reader reader,
  853. const module_destroyer destroyer);
  854. module_reader
  855. wasm_runtime_get_module_reader(void);
  856. module_destroyer
  857. wasm_runtime_get_module_destroyer(void);
  858. bool
  859. wasm_runtime_register_module_internal(const char *module_name,
  860. WASMModuleCommon *module,
  861. uint8 *orig_file_buf,
  862. uint32 orig_file_buf_size,
  863. char *error_buf, uint32 error_buf_size);
  864. void
  865. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  866. WASMModuleCommon *
  867. wasm_runtime_find_module_registered(const char *module_name);
  868. bool
  869. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  870. uint32 error_buf_size);
  871. void
  872. wasm_runtime_delete_loading_module(const char *module_name);
  873. bool
  874. wasm_runtime_is_loading_module(const char *module_name);
  875. void
  876. wasm_runtime_destroy_loading_module_list(void);
  877. WASMModuleCommon *
  878. wasm_runtime_search_sub_module(const WASMModuleCommon *parent_module,
  879. const char *sub_module_name);
  880. bool
  881. wasm_runtime_register_sub_module(const WASMModuleCommon *parent_module,
  882. const char *sub_module_name,
  883. WASMModuleCommon *sub_module);
  884. WASMModuleCommon *
  885. wasm_runtime_load_depended_module(const WASMModuleCommon *parent_module,
  886. const char *sub_module_name, char *error_buf,
  887. uint32 error_buf_size);
  888. bool
  889. wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
  890. WASMModuleInstanceCommon *module_inst,
  891. uint32 stack_size, uint32 heap_size,
  892. uint32 max_memory_pages, char *error_buf,
  893. uint32 error_buf_size);
  894. void
  895. wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst);
  896. #endif
  897. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  898. WASMExport *
  899. loader_find_export(const WASMModuleCommon *module, const char *module_name,
  900. const char *field_name, uint8 export_kind, char *error_buf,
  901. uint32 error_buf_size);
  902. #endif /* WASM_ENABLE_MULTI_MODULE */
  903. bool
  904. wasm_runtime_is_built_in_module(const char *module_name);
  905. #if WASM_ENABLE_THREAD_MGR != 0
  906. bool
  907. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint64 *start_offset,
  908. uint32 *size);
  909. bool
  910. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint64 start_offset,
  911. uint32 size);
  912. #endif
  913. #if WASM_ENABLE_LIBC_WASI != 0
  914. WASM_RUNTIME_API_EXTERN void
  915. wasm_runtime_set_wasi_args_ex(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. int64 stdinfd, int64 stdoutfd, int64 stderrfd);
  920. /* See wasm_export.h for description */
  921. WASM_RUNTIME_API_EXTERN void
  922. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  923. uint32 dir_count, const char *map_dir_list[],
  924. uint32 map_dir_count, const char *env_list[],
  925. uint32 env_count, char *argv[], int argc);
  926. /* See wasm_export.h for description */
  927. WASM_RUNTIME_API_EXTERN bool
  928. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  929. /* See wasm_export.h for description */
  930. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  931. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  932. /* See wasm_export.h for description */
  933. WASM_RUNTIME_API_EXTERN uint32_t
  934. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst);
  935. bool
  936. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  937. const char *dir_list[], uint32 dir_count,
  938. const char *map_dir_list[], uint32 map_dir_count,
  939. const char *env[], uint32 env_count,
  940. const char *addr_pool[], uint32 addr_pool_size,
  941. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  942. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  943. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  944. char *error_buf, uint32 error_buf_size);
  945. void
  946. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  947. void
  948. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  949. WASIContext *wasi_ctx);
  950. WASIContext *
  951. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  952. WASM_RUNTIME_API_EXTERN void
  953. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  954. uint32 addr_pool_size);
  955. WASM_RUNTIME_API_EXTERN void
  956. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  957. const char *ns_lookup_pool[],
  958. uint32 ns_lookup_pool_size);
  959. #endif /* end of WASM_ENABLE_LIBC_WASI */
  960. #if WASM_ENABLE_GC != 0
  961. void
  962. wasm_runtime_set_gc_heap_handle(WASMModuleInstanceCommon *module_inst,
  963. void *gc_heap_handle);
  964. void *
  965. wasm_runtime_get_gc_heap_handle(WASMModuleInstanceCommon *module_inst);
  966. #endif
  967. #if WASM_ENABLE_REF_TYPES != 0
  968. /* See wasm_export.h for description */
  969. WASM_RUNTIME_API_EXTERN bool
  970. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  971. uint32 *p_externref_idx);
  972. /* See wasm_export.h for description */
  973. WASM_RUNTIME_API_EXTERN bool
  974. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  975. /* See wasm_export.h for description */
  976. WASM_RUNTIME_API_EXTERN bool
  977. wasm_externref_retain(uint32 externref_idx);
  978. /**
  979. * Reclaim the externref objects/indexes which are not used by
  980. * module instance
  981. */
  982. void
  983. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  984. /**
  985. * Cleanup the externref objects/indexes of the module instance
  986. */
  987. void
  988. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  989. #endif /* end of WASM_ENABLE_REF_TYPES */
  990. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  991. /**
  992. * @brief Internal implementation for dumping or printing callstack line
  993. *
  994. * @note if dump_or_print is true, then print to stdout directly;
  995. * if dump_or_print is false, but *buf is NULL, then return the length of the
  996. * line;
  997. * if dump_or_print is false, and *buf is not NULL, then dump content to
  998. * the memory pointed by *buf, and adjust *buf and *len according to actual
  999. * bytes dumped, and return the actual dumped length
  1000. *
  1001. * @param line_buf current line to dump or print
  1002. * @param dump_or_print whether to print to stdout or dump to buf
  1003. * @param buf [INOUT] pointer to the buffer
  1004. * @param len [INOUT] pointer to remaining length
  1005. * @return bytes printed to stdout or dumped to buf
  1006. */
  1007. uint32
  1008. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  1009. char **buf, uint32 *len);
  1010. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 */
  1011. /* Get module of the current exec_env */
  1012. WASMModuleCommon *
  1013. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  1014. /* See wasm_export.h for description */
  1015. WASM_RUNTIME_API_EXTERN bool
  1016. wasm_runtime_register_natives(const char *module_name,
  1017. NativeSymbol *native_symbols,
  1018. uint32 n_native_symbols);
  1019. /* See wasm_export.h for description */
  1020. WASM_RUNTIME_API_EXTERN bool
  1021. wasm_runtime_register_natives_raw(const char *module_name,
  1022. NativeSymbol *native_symbols,
  1023. uint32 n_native_symbols);
  1024. /* See wasm_export.h for description */
  1025. WASM_RUNTIME_API_EXTERN bool
  1026. wasm_runtime_unregister_natives(const char *module_name,
  1027. NativeSymbol *native_symbols);
  1028. /* See wasm_export.h for description */
  1029. WASM_RUNTIME_API_EXTERN void *
  1030. wasm_runtime_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
  1031. void *ctx));
  1032. /* See wasm_export.h for description */
  1033. WASM_RUNTIME_API_EXTERN void
  1034. wasm_runtime_destroy_context_key(void *key);
  1035. /* See wasm_export.h for description */
  1036. WASM_RUNTIME_API_EXTERN void
  1037. wasm_runtime_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx);
  1038. /* See wasm_export.h for description */
  1039. WASM_RUNTIME_API_EXTERN void
  1040. wasm_runtime_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
  1041. void *ctx);
  1042. /* See wasm_export.h for description */
  1043. WASM_RUNTIME_API_EXTERN void *
  1044. wasm_runtime_get_context(WASMModuleInstanceCommon *inst, void *key);
  1045. bool
  1046. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  1047. const WASMFuncType *func_type, const char *signature,
  1048. void *attachment, uint32 *argv, uint32 argc,
  1049. uint32 *ret);
  1050. bool
  1051. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  1052. const WASMFuncType *func_type,
  1053. const char *signature, void *attachment,
  1054. uint32 *argv, uint32 argc, uint32 *ret);
  1055. void
  1056. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  1057. void
  1058. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  1059. void
  1060. wasm_runtime_dump_module_inst_mem_consumption(
  1061. const WASMModuleInstanceCommon *module_inst);
  1062. void
  1063. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  1064. bool
  1065. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  1066. uint32 table_idx, uint8 *out_elem_type,
  1067. #if WASM_ENABLE_GC != 0
  1068. WASMRefType **out_ref_type,
  1069. #endif
  1070. uint32 *out_min_size, uint32 *out_max_size);
  1071. bool
  1072. wasm_runtime_get_table_inst_elem_type(
  1073. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  1074. uint8 *out_elem_type,
  1075. #if WASM_ENABLE_GC != 0
  1076. WASMRefType **out_ref_type,
  1077. #endif
  1078. uint32 *out_min_size, uint32 *out_max_size);
  1079. bool
  1080. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  1081. const WASMExport *export_,
  1082. WASMFuncType **out);
  1083. bool
  1084. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  1085. const WASMExport *export_,
  1086. uint8 *out_val_type, bool *out_mutability);
  1087. bool
  1088. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  1089. const WASMExport *export_,
  1090. uint32 *out_min_page, uint32 *out_max_page);
  1091. bool
  1092. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  1093. const WASMExport *export_,
  1094. uint8 *out_elem_type,
  1095. #if WASM_ENABLE_GC != 0
  1096. WASMRefType **out_ref_type,
  1097. #endif
  1098. uint32 *out_min_size, uint32 *out_max_size);
  1099. bool
  1100. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  1101. void *func_ptr, WASMFuncType *func_type,
  1102. uint32 argc, uint32 *argv, bool with_env,
  1103. void *wasm_c_api_env);
  1104. struct CApiFuncImport;
  1105. /* A quick version of wasm_runtime_invoke_c_api_native to directly invoke
  1106. wasm-c-api import function from jitted code to improve performance */
  1107. bool
  1108. wasm_runtime_quick_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  1109. struct CApiFuncImport *c_api_import,
  1110. wasm_val_t *params, uint32 param_count,
  1111. wasm_val_t *results,
  1112. uint32 result_count);
  1113. void
  1114. wasm_runtime_show_app_heap_corrupted_prompt(void);
  1115. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  1116. void
  1117. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list);
  1118. #endif
  1119. WASM_RUNTIME_API_EXTERN bool
  1120. wasm_runtime_is_import_func_linked(const char *module_name,
  1121. const char *func_name);
  1122. WASM_RUNTIME_API_EXTERN bool
  1123. wasm_runtime_is_import_global_linked(const char *module_name,
  1124. const char *global_name);
  1125. WASM_RUNTIME_API_EXTERN bool
  1126. wasm_runtime_begin_blocking_op(WASMExecEnv *exec_env);
  1127. WASM_RUNTIME_API_EXTERN void
  1128. wasm_runtime_end_blocking_op(WASMExecEnv *exec_env);
  1129. void
  1130. wasm_runtime_interrupt_blocking_op(WASMExecEnv *exec_env);
  1131. WASM_RUNTIME_API_EXTERN bool
  1132. wasm_runtime_detect_native_stack_overflow(WASMExecEnv *exec_env);
  1133. WASM_RUNTIME_API_EXTERN bool
  1134. wasm_runtime_detect_native_stack_overflow_size(WASMExecEnv *exec_env,
  1135. uint32 requested_size);
  1136. WASM_RUNTIME_API_EXTERN bool
  1137. wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module);
  1138. #if WASM_ENABLE_LINUX_PERF != 0
  1139. bool
  1140. wasm_runtime_get_linux_perf(void);
  1141. void
  1142. wasm_runtime_set_linux_perf(bool flag);
  1143. #endif
  1144. #ifdef __cplusplus
  1145. }
  1146. #endif
  1147. #endif /* end of _WASM_COMMON_H */