wasm_runtime_common.h 37 KB

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