wasm_runtime_common.h 36 KB

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