wasm_runtime_common.h 38 KB

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