wasm_runtime_common.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. } WASMCApiFrame;
  420. #ifdef WASM_ENABLE_JIT
  421. typedef struct LLVMJITOptions {
  422. uint32 opt_level;
  423. uint32 size_level;
  424. uint32 segue_flags;
  425. } LLVMJITOptions;
  426. #endif
  427. #ifdef OS_ENABLE_HW_BOUND_CHECK
  428. /* Signal info passing to interp/aot signal handler */
  429. typedef struct WASMSignalInfo {
  430. WASMExecEnv *exec_env_tls;
  431. #ifndef BH_PLATFORM_WINDOWS
  432. void *sig_addr;
  433. #else
  434. EXCEPTION_POINTERS *exce_info;
  435. #endif
  436. } WASMSignalInfo;
  437. /* Set exec_env of thread local storage */
  438. void
  439. wasm_runtime_set_exec_env_tls(WASMExecEnv *exec_env);
  440. /* Get exec_env of thread local storage */
  441. WASMExecEnv *
  442. wasm_runtime_get_exec_env_tls(void);
  443. #endif
  444. /* See wasm_export.h for description */
  445. WASM_RUNTIME_API_EXTERN bool
  446. wasm_runtime_init(void);
  447. /* Internal API */
  448. RunningMode
  449. wasm_runtime_get_default_running_mode(void);
  450. #if WASM_ENABLE_JIT != 0
  451. /* Internal API */
  452. LLVMJITOptions
  453. wasm_runtime_get_llvm_jit_options(void);
  454. #endif
  455. #if WASM_ENABLE_GC != 0
  456. /* Internal API */
  457. uint32
  458. wasm_runtime_get_gc_heap_size_default(void);
  459. #endif
  460. /* See wasm_export.h for description */
  461. WASM_RUNTIME_API_EXTERN bool
  462. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  463. /* See wasm_export.h for description */
  464. WASM_RUNTIME_API_EXTERN bool
  465. wasm_runtime_is_running_mode_supported(RunningMode running_mode);
  466. /* See wasm_export.h for description */
  467. WASM_RUNTIME_API_EXTERN bool
  468. wasm_runtime_set_default_running_mode(RunningMode running_mode);
  469. /* See wasm_export.h for description */
  470. WASM_RUNTIME_API_EXTERN void
  471. wasm_runtime_destroy(void);
  472. /* See wasm_export.h for description */
  473. WASM_RUNTIME_API_EXTERN PackageType
  474. get_package_type(const uint8 *buf, uint32 size);
  475. /* See wasm_export.h for description */
  476. WASM_RUNTIME_API_EXTERN bool
  477. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size);
  478. /* See wasm_export.h for description */
  479. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  480. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  481. uint32 error_buf_size);
  482. /* See wasm_export.h for description */
  483. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  484. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  485. char *error_buf, uint32 error_buf_size);
  486. /* See wasm_export.h for description */
  487. WASM_RUNTIME_API_EXTERN void
  488. wasm_runtime_unload(WASMModuleCommon *module);
  489. /* Internal API */
  490. WASMModuleInstanceCommon *
  491. wasm_runtime_instantiate_internal(WASMModuleCommon *module,
  492. WASMModuleInstanceCommon *parent,
  493. WASMExecEnv *exec_env_main, uint32 stack_size,
  494. uint32 heap_size, char *error_buf,
  495. uint32 error_buf_size);
  496. /* Internal API */
  497. void
  498. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  499. bool is_sub_inst);
  500. /* See wasm_export.h for description */
  501. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  502. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 default_stack_size,
  503. uint32 host_managed_heap_size, char *error_buf,
  504. uint32 error_buf_size);
  505. /* See wasm_export.h for description */
  506. WASM_RUNTIME_API_EXTERN bool
  507. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  508. RunningMode running_mode);
  509. /* See wasm_export.h for description */
  510. WASM_RUNTIME_API_EXTERN RunningMode
  511. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
  512. /* See wasm_export.h for description */
  513. WASM_RUNTIME_API_EXTERN void
  514. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  515. /* See wasm_export.h for description */
  516. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  517. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst);
  518. /* See wasm_export.h for description */
  519. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  520. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  521. const char *name, const char *signature);
  522. /* Internal API */
  523. WASMFuncType *
  524. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  525. uint32 module_type);
  526. /* See wasm_export.h for description */
  527. WASM_RUNTIME_API_EXTERN uint32
  528. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  529. WASMModuleInstanceCommon *const module_inst);
  530. /* See wasm_export.h for description */
  531. WASM_RUNTIME_API_EXTERN uint32
  532. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  533. WASMModuleInstanceCommon *const module_inst);
  534. /* See wasm_export.h for description */
  535. WASM_RUNTIME_API_EXTERN void
  536. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  537. WASMModuleInstanceCommon *const module_inst,
  538. wasm_valkind_t *param_types);
  539. /* See wasm_export.h for description */
  540. WASM_RUNTIME_API_EXTERN void
  541. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  542. WASMModuleInstanceCommon *const module_inst,
  543. wasm_valkind_t *result_types);
  544. /* See wasm_export.h for description */
  545. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  546. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  547. uint32 stack_size);
  548. /* See wasm_export.h for description */
  549. WASM_RUNTIME_API_EXTERN void
  550. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  551. /* See wasm_export.h for description */
  552. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  553. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  554. /* See wasm_export.h for description */
  555. WASM_RUNTIME_API_EXTERN void
  556. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  557. WASMModuleInstanceCommon *const module_inst);
  558. /* See wasm_export.h for description */
  559. WASM_RUNTIME_API_EXTERN void *
  560. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  561. /* See wasm_export.h for description */
  562. WASM_RUNTIME_API_EXTERN void
  563. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  564. /* See wasm_export.h for description */
  565. WASM_RUNTIME_API_EXTERN void *
  566. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  567. #if WASM_CONFIGUABLE_BOUNDS_CHECKS != 0
  568. /* See wasm_export.h for description */
  569. WASM_RUNTIME_API_EXTERN void
  570. wasm_runtime_set_bounds_checks(WASMModuleInstanceCommon *module_inst,
  571. bool enable);
  572. /* See wasm_export.h for description */
  573. WASM_RUNTIME_API_EXTERN bool
  574. wasm_runtime_is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst);
  575. #endif
  576. #ifdef OS_ENABLE_HW_BOUND_CHECK
  577. /* Access exception check guard page to trigger the signal handler */
  578. void
  579. wasm_runtime_access_exce_check_guard_page();
  580. #endif
  581. /* See wasm_export.h for description */
  582. WASM_RUNTIME_API_EXTERN bool
  583. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  584. WASMFunctionInstanceCommon *function, uint32 argc,
  585. uint32 argv[]);
  586. WASM_RUNTIME_API_EXTERN bool
  587. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  588. WASMFunctionInstanceCommon *function,
  589. uint32 num_results, wasm_val_t *results,
  590. uint32 num_args, wasm_val_t *args);
  591. WASM_RUNTIME_API_EXTERN bool
  592. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  593. WASMFunctionInstanceCommon *function,
  594. uint32 num_results, wasm_val_t *results,
  595. uint32 num_args, ...);
  596. /* See wasm_export.h for description */
  597. WASM_RUNTIME_API_EXTERN bool
  598. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  599. uint32 argc, uint32 argv[]);
  600. #if WASM_ENABLE_DEBUG_INTERP != 0
  601. /* See wasm_export.h for description */
  602. WASM_RUNTIME_API_EXTERN uint32
  603. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
  604. int32_t port);
  605. /* See wasm_export.h for description */
  606. WASM_RUNTIME_API_EXTERN uint32
  607. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
  608. #endif
  609. bool
  610. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  611. /* See wasm_export.h for description */
  612. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  613. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  614. /* See wasm_export.h for description */
  615. WASM_RUNTIME_API_EXTERN bool
  616. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  617. char *argv[]);
  618. /* See wasm_export.h for description */
  619. WASM_RUNTIME_API_EXTERN bool
  620. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  621. const char *name, int32 argc, char *argv[]);
  622. /* See wasm_export.h for description */
  623. WASM_RUNTIME_API_EXTERN void
  624. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  625. const char *exception);
  626. /* See wasm_export.h for description */
  627. WASM_RUNTIME_API_EXTERN const char *
  628. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  629. /* See wasm_export.h for description */
  630. WASM_RUNTIME_API_EXTERN void
  631. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  632. /* Internal API */
  633. void
  634. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  635. void *custom_data);
  636. /* See wasm_export.h for description */
  637. WASM_RUNTIME_API_EXTERN void
  638. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  639. void *custom_data);
  640. /* See wasm_export.h for description */
  641. WASM_RUNTIME_API_EXTERN void *
  642. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  643. /* Internal API */
  644. uint32
  645. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  646. WASMExecEnv *exec_env, uint32 size,
  647. void **p_native_addr);
  648. /* Internal API */
  649. uint32
  650. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  651. WASMExecEnv *exec_env, uint32 ptr,
  652. uint32 size, void **p_native_addr);
  653. /* Internal API */
  654. void
  655. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  656. WASMExecEnv *exec_env, uint32 ptr);
  657. /* See wasm_export.h for description */
  658. WASM_RUNTIME_API_EXTERN uint32
  659. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  660. void **p_native_addr);
  661. /* See wasm_export.h for description */
  662. WASM_RUNTIME_API_EXTERN void
  663. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  664. /* See wasm_export.h for description */
  665. WASM_RUNTIME_API_EXTERN uint32
  666. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  667. const char *src, uint32 size);
  668. /* See wasm_export.h for description */
  669. WASM_RUNTIME_API_EXTERN bool
  670. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  671. uint32 app_offset, uint32 size);
  672. /* See wasm_export.h for description */
  673. WASM_RUNTIME_API_EXTERN bool
  674. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  675. uint32 app_str_offset);
  676. /* See wasm_export.h for description */
  677. WASM_RUNTIME_API_EXTERN bool
  678. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  679. void *native_ptr, uint32 size);
  680. /* See wasm_export.h for description */
  681. WASM_RUNTIME_API_EXTERN void *
  682. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  683. uint32 app_offset);
  684. /* See wasm_export.h for description */
  685. WASM_RUNTIME_API_EXTERN uint32
  686. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  687. void *native_ptr);
  688. /* See wasm_export.h for description */
  689. WASM_RUNTIME_API_EXTERN bool
  690. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  691. uint32 app_offset, uint32 *p_app_start_offset,
  692. uint32 *p_app_end_offset);
  693. /* See wasm_export.h for description */
  694. WASM_RUNTIME_API_EXTERN bool
  695. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  696. uint8 *native_ptr,
  697. uint8 **p_native_start_addr,
  698. uint8 **p_native_end_addr);
  699. /* See wasm_export.h for description */
  700. WASM_RUNTIME_API_EXTERN const uint8 *
  701. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  702. const char *name, uint32 *len);
  703. #if WASM_ENABLE_MULTI_MODULE != 0
  704. WASM_RUNTIME_API_EXTERN void
  705. wasm_runtime_set_module_reader(const module_reader reader,
  706. const module_destroyer destroyer);
  707. module_reader
  708. wasm_runtime_get_module_reader();
  709. module_destroyer
  710. wasm_runtime_get_module_destroyer();
  711. bool
  712. wasm_runtime_register_module_internal(const char *module_name,
  713. WASMModuleCommon *module,
  714. uint8 *orig_file_buf,
  715. uint32 orig_file_buf_size,
  716. char *error_buf, uint32 error_buf_size);
  717. void
  718. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  719. bool
  720. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  721. uint32 error_buf_size);
  722. void
  723. wasm_runtime_delete_loading_module(const char *module_name);
  724. bool
  725. wasm_runtime_is_loading_module(const char *module_name);
  726. void
  727. wasm_runtime_destroy_loading_module_list();
  728. #endif /* WASM_ENALBE_MULTI_MODULE */
  729. bool
  730. wasm_runtime_is_built_in_module(const char *module_name);
  731. #if WASM_ENABLE_THREAD_MGR != 0
  732. bool
  733. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  734. uint32 *size);
  735. bool
  736. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  737. uint32 size);
  738. #endif
  739. #if WASM_ENABLE_LIBC_WASI != 0
  740. WASM_RUNTIME_API_EXTERN void
  741. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  742. uint32 dir_count, const char *map_dir_list[],
  743. uint32 map_dir_count, const char *env_list[],
  744. uint32 env_count, char *argv[], int argc,
  745. int stdinfd, int stdoutfd, int stderrfd);
  746. /* See wasm_export.h for description */
  747. WASM_RUNTIME_API_EXTERN void
  748. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  749. uint32 dir_count, const char *map_dir_list[],
  750. uint32 map_dir_count, const char *env_list[],
  751. uint32 env_count, char *argv[], int argc);
  752. /* See wasm_export.h for description */
  753. WASM_RUNTIME_API_EXTERN bool
  754. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  755. /* See wasm_export.h for description */
  756. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  757. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  758. /* See wasm_export.h for description */
  759. WASM_RUNTIME_API_EXTERN uint32_t
  760. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst);
  761. bool
  762. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  763. const char *dir_list[], uint32 dir_count,
  764. const char *map_dir_list[], uint32 map_dir_count,
  765. const char *env[], uint32 env_count,
  766. const char *addr_pool[], uint32 addr_pool_size,
  767. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  768. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  769. int stderrfd, char *error_buf, uint32 error_buf_size);
  770. void
  771. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  772. void
  773. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  774. WASIContext *wasi_ctx);
  775. WASIContext *
  776. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  777. WASM_RUNTIME_API_EXTERN void
  778. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  779. uint32 addr_pool_size);
  780. WASM_RUNTIME_API_EXTERN void
  781. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  782. const char *ns_lookup_pool[],
  783. uint32 ns_lookup_pool_size);
  784. #endif /* end of WASM_ENABLE_LIBC_WASI */
  785. #if WASM_ENABLE_GC != 0
  786. void
  787. wasm_runtime_set_gc_heap_handle(WASMModuleInstanceCommon *module_inst,
  788. void *gc_heap_handle);
  789. void *
  790. wasm_runtime_get_gc_heap_handle(WASMModuleInstanceCommon *module_inst);
  791. #endif
  792. #if WASM_ENABLE_REF_TYPES != 0
  793. /* See wasm_export.h for description */
  794. WASM_RUNTIME_API_EXTERN bool
  795. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  796. uint32 *p_externref_idx);
  797. /* See wasm_export.h for description */
  798. WASM_RUNTIME_API_EXTERN bool
  799. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  800. /* See wasm_export.h for description */
  801. WASM_RUNTIME_API_EXTERN bool
  802. wasm_externref_retain(uint32 externref_idx);
  803. /**
  804. * Reclaim the externref objects/indexes which are not used by
  805. * module instance
  806. */
  807. void
  808. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  809. /**
  810. * Cleanup the externref objects/indexes of the module instance
  811. */
  812. void
  813. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  814. #endif /* end of WASM_ENABLE_REF_TYPES */
  815. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  816. /**
  817. * @brief Internal implementation for dumping or printing callstack line
  818. *
  819. * @note if dump_or_print is true, then print to stdout directly;
  820. * if dump_or_print is false, but *buf is NULL, then return the length of the
  821. * line;
  822. * if dump_or_print is false, and *buf is not NULL, then dump content to
  823. * the memory pointed by *buf, and adjust *buf and *len according to actual
  824. * bytes dumped, and return the actual dumped length
  825. *
  826. * @param line_buf current line to dump or print
  827. * @param dump_or_print whether to print to stdout or dump to buf
  828. * @param buf [INOUT] pointer to the buffer
  829. * @param len [INOUT] pointer to remaining length
  830. * @return bytes printed to stdout or dumped to buf
  831. */
  832. uint32
  833. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  834. char **buf, uint32 *len);
  835. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 */
  836. /* Get module of the current exec_env */
  837. WASMModuleCommon *
  838. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  839. /* See wasm_export.h for description */
  840. WASM_RUNTIME_API_EXTERN bool
  841. wasm_runtime_register_natives(const char *module_name,
  842. NativeSymbol *native_symbols,
  843. uint32 n_native_symbols);
  844. /* See wasm_export.h for description */
  845. WASM_RUNTIME_API_EXTERN bool
  846. wasm_runtime_register_natives_raw(const char *module_name,
  847. NativeSymbol *native_symbols,
  848. uint32 n_native_symbols);
  849. /* See wasm_export.h for description */
  850. WASM_RUNTIME_API_EXTERN bool
  851. wasm_runtime_unregister_natives(const char *module_name,
  852. NativeSymbol *native_symbols);
  853. bool
  854. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  855. const WASMFuncType *func_type, const char *signature,
  856. void *attachment, uint32 *argv, uint32 argc,
  857. uint32 *ret);
  858. bool
  859. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  860. const WASMFuncType *func_type,
  861. const char *signature, void *attachment,
  862. uint32 *argv, uint32 argc, uint32 *ret);
  863. void
  864. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  865. void
  866. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  867. void
  868. wasm_runtime_dump_module_inst_mem_consumption(
  869. const WASMModuleInstanceCommon *module_inst);
  870. void
  871. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  872. bool
  873. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  874. uint32 table_idx, uint8 *out_elem_type,
  875. #if WASM_ENABLE_GC != 0
  876. WASMRefType **out_ref_type,
  877. #endif
  878. uint32 *out_min_size, uint32 *out_max_size);
  879. bool
  880. wasm_runtime_get_table_inst_elem_type(
  881. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  882. uint8 *out_elem_type,
  883. #if WASM_ENABLE_GC != 0
  884. WASMRefType **out_ref_type,
  885. #endif
  886. uint32 *out_min_size, uint32 *out_max_size);
  887. bool
  888. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  889. const WASMExport *export_,
  890. WASMFuncType **out);
  891. bool
  892. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  893. const WASMExport *export_,
  894. uint8 *out_val_type, bool *out_mutability);
  895. bool
  896. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  897. const WASMExport *export_,
  898. uint32 *out_min_page, uint32 *out_max_page);
  899. bool
  900. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  901. const WASMExport *export_,
  902. uint8 *out_elem_type,
  903. #if WASM_ENABLE_GC != 0
  904. WASMRefType **out_ref_type,
  905. #endif
  906. uint32 *out_min_size, uint32 *out_max_size);
  907. bool
  908. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  909. void *func_ptr, WASMFuncType *func_type,
  910. uint32 argc, uint32 *argv, bool with_env,
  911. void *wasm_c_api_env);
  912. void
  913. wasm_runtime_show_app_heap_corrupted_prompt();
  914. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  915. void
  916. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list);
  917. #endif
  918. WASM_RUNTIME_API_EXTERN bool
  919. wasm_runtime_is_import_func_linked(const char *module_name,
  920. const char *func_name);
  921. WASM_RUNTIME_API_EXTERN bool
  922. wasm_runtime_is_import_global_linked(const char *module_name,
  923. const char *global_name);
  924. #ifdef __cplusplus
  925. }
  926. #endif
  927. #endif /* end of _WASM_COMMON_H */