wasm_runtime_common.h 37 KB

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