wasm_runtime_common.h 36 KB

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