wasm_runtime_common.h 36 KB

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