wasm_runtime_common.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  25. #define PUT_I64_TO_ADDR(addr, value) do { \
  26. *(int64*)(addr) = (int64)(value); \
  27. } while (0)
  28. #define PUT_F64_TO_ADDR(addr, value) do { \
  29. *(float64*)(addr) = (float64)(value); \
  30. } while (0)
  31. #define GET_I64_FROM_ADDR(addr) (*(int64*)(addr))
  32. #define GET_F64_FROM_ADDR(addr) (*(float64*)(addr))
  33. /* For STORE opcodes */
  34. #define STORE_I64 PUT_I64_TO_ADDR
  35. #define STORE_U32(addr, value) do { \
  36. *(uint32*)(addr) = (uint32)(value); \
  37. } while (0)
  38. #define STORE_U16(addr, value) do { \
  39. *(uint16*)(addr) = (uint16)(value); \
  40. } while (0)
  41. /* For LOAD opcodes */
  42. #define LOAD_I64(addr) (*(int64*)(addr))
  43. #define LOAD_F64(addr) (*(float64*)(addr))
  44. #define LOAD_I32(addr) (*(int32*)(addr))
  45. #define LOAD_U32(addr) (*(uint32*)(addr))
  46. #define LOAD_I16(addr) (*(int16*)(addr))
  47. #define LOAD_U16(addr) (*(uint16*)(addr))
  48. #define STORE_PTR(addr, ptr) do { \
  49. *(void**)addr = (void*)ptr; \
  50. } while (0)
  51. #define LOAD_PTR(addr) (*(void**)(addr))
  52. #else /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
  53. #define PUT_I64_TO_ADDR(addr, value) do { \
  54. uint32 *addr_u32 = (uint32*)(addr); \
  55. union { int64 val; uint32 parts[2]; } u; \
  56. u.val = (int64)(value); \
  57. addr_u32[0] = u.parts[0]; \
  58. addr_u32[1] = u.parts[1]; \
  59. } while (0)
  60. #define PUT_F64_TO_ADDR(addr, value) do { \
  61. uint32 *addr_u32 = (uint32*)(addr); \
  62. union { float64 val; uint32 parts[2]; } u; \
  63. u.val = (value); \
  64. addr_u32[0] = u.parts[0]; \
  65. addr_u32[1] = u.parts[1]; \
  66. } while (0)
  67. static inline int64
  68. GET_I64_FROM_ADDR(uint32 *addr)
  69. {
  70. union { int64 val; uint32 parts[2]; } u;
  71. u.parts[0] = addr[0];
  72. u.parts[1] = addr[1];
  73. return u.val;
  74. }
  75. static inline float64
  76. GET_F64_FROM_ADDR (uint32 *addr)
  77. {
  78. union { float64 val; uint32 parts[2]; } u;
  79. u.parts[0] = addr[0];
  80. u.parts[1] = addr[1];
  81. return u.val;
  82. }
  83. /* For STORE opcodes */
  84. #define STORE_I64(addr, value) do { \
  85. uintptr_t addr1 = (uintptr_t)(addr); \
  86. union { int64 val; uint32 u32[2]; \
  87. uint16 u16[4]; uint8 u8[8]; } u; \
  88. if ((addr1 & (uintptr_t)7) == 0) \
  89. *(int64*)(addr) = (int64)(value); \
  90. else { \
  91. u.val = (int64)(value); \
  92. if ((addr1 & (uintptr_t)3) == 0) { \
  93. ((uint32*)(addr))[0] = u.u32[0]; \
  94. ((uint32*)(addr))[1] = u.u32[1]; \
  95. } \
  96. else if ((addr1 & (uintptr_t)1) == 0) { \
  97. ((uint16*)(addr))[0] = u.u16[0]; \
  98. ((uint16*)(addr))[1] = u.u16[1]; \
  99. ((uint16*)(addr))[2] = u.u16[2]; \
  100. ((uint16*)(addr))[3] = u.u16[3]; \
  101. } \
  102. else { \
  103. int32 t; \
  104. for (t = 0; t < 8; t++) \
  105. ((uint8*)(addr))[t] = u.u8[t]; \
  106. } \
  107. } \
  108. } while (0)
  109. #define STORE_U32(addr, value) do { \
  110. uintptr_t addr1 = (uintptr_t)(addr); \
  111. union { uint32 val; \
  112. uint16 u16[2]; uint8 u8[4]; } u; \
  113. if ((addr1 & (uintptr_t)3) == 0) \
  114. *(uint32*)(addr) = (uint32)(value); \
  115. else { \
  116. u.val = (uint32)(value); \
  117. if ((addr1 & (uintptr_t)1) == 0) { \
  118. ((uint16*)(addr))[0] = u.u16[0]; \
  119. ((uint16*)(addr))[1] = u.u16[1]; \
  120. } \
  121. else { \
  122. ((uint8*)(addr))[0] = u.u8[0]; \
  123. ((uint8*)(addr))[1] = u.u8[1]; \
  124. ((uint8*)(addr))[2] = u.u8[2]; \
  125. ((uint8*)(addr))[3] = u.u8[3]; \
  126. } \
  127. } \
  128. } while (0)
  129. #define STORE_U16(addr, value) do { \
  130. union { uint16 val; uint8 u8[2]; } u; \
  131. u.val = (uint16)(value); \
  132. ((uint8*)(addr))[0] = u.u8[0]; \
  133. ((uint8*)(addr))[1] = u.u8[1]; \
  134. } while (0)
  135. /* For LOAD opcodes */
  136. static inline int64
  137. LOAD_I64(void *addr)
  138. {
  139. uintptr_t addr1 = (uintptr_t)addr;
  140. union { int64 val; uint32 u32[2];
  141. uint16 u16[4]; uint8 u8[8]; } u;
  142. if ((addr1 & (uintptr_t)7) == 0)
  143. return *(int64*)addr;
  144. if ((addr1 & (uintptr_t)3) == 0) {
  145. u.u32[0] = ((uint32*)addr)[0];
  146. u.u32[1] = ((uint32*)addr)[1];
  147. }
  148. else if ((addr1 & (uintptr_t)1) == 0) {
  149. u.u16[0] = ((uint16*)addr)[0];
  150. u.u16[1] = ((uint16*)addr)[1];
  151. u.u16[2] = ((uint16*)addr)[2];
  152. u.u16[3] = ((uint16*)addr)[3];
  153. }
  154. else {
  155. int32 t;
  156. for (t = 0; t < 8; t++)
  157. u.u8[t] = ((uint8*)addr)[t];
  158. }
  159. return u.val;
  160. }
  161. static inline float64
  162. LOAD_F64(void *addr)
  163. {
  164. uintptr_t addr1 = (uintptr_t)addr;
  165. union { float64 val; uint32 u32[2];
  166. uint16 u16[4]; uint8 u8[8]; } u;
  167. if ((addr1 & (uintptr_t)7) == 0)
  168. return *(float64*)addr;
  169. if ((addr1 & (uintptr_t)3) == 0) {
  170. u.u32[0] = ((uint32*)addr)[0];
  171. u.u32[1] = ((uint32*)addr)[1];
  172. }
  173. else if ((addr1 & (uintptr_t)1) == 0) {
  174. u.u16[0] = ((uint16*)addr)[0];
  175. u.u16[1] = ((uint16*)addr)[1];
  176. u.u16[2] = ((uint16*)addr)[2];
  177. u.u16[3] = ((uint16*)addr)[3];
  178. }
  179. else {
  180. int32 t;
  181. for (t = 0; t < 8; t++)
  182. u.u8[t] = ((uint8*)addr)[t];
  183. }
  184. return u.val;
  185. }
  186. static inline int32
  187. LOAD_I32(void *addr)
  188. {
  189. uintptr_t addr1 = (uintptr_t)addr;
  190. union { int32 val; uint16 u16[2]; uint8 u8[4]; } u;
  191. if ((addr1 & (uintptr_t)3) == 0)
  192. return *(int32*)addr;
  193. if ((addr1 & (uintptr_t)1) == 0) {
  194. u.u16[0] = ((uint16*)addr)[0];
  195. u.u16[1] = ((uint16*)addr)[1];
  196. }
  197. else {
  198. u.u8[0] = ((uint8*)addr)[0];
  199. u.u8[1] = ((uint8*)addr)[1];
  200. u.u8[2] = ((uint8*)addr)[2];
  201. u.u8[3] = ((uint8*)addr)[3];
  202. }
  203. return u.val;
  204. }
  205. static inline int16
  206. LOAD_I16(void *addr)
  207. {
  208. uintptr_t addr1 = (uintptr_t)addr;
  209. union { int16 val; uint8 u8[2]; } u;
  210. if ((addr1 & (uintptr_t)1)) {
  211. u.u8[0] = ((uint8*)addr)[0];
  212. u.u8[1] = ((uint8*)addr)[1];
  213. return u.val;
  214. }
  215. return *(int16*)addr;
  216. }
  217. #define LOAD_U32(addr) ((uint32)LOAD_I32(addr))
  218. #define LOAD_U16(addr) ((uint16)LOAD_I16(addr))
  219. #if UINTPTR_MAX == UINT32_MAX
  220. #define STORE_PTR(addr, ptr) STORE_U32(addr, (uintptr_t)ptr)
  221. #elif UINTPTR_MAX == UINT64_MAX
  222. #define STORE_PTR(addr, ptr) STORE_I64(addr, (uintptr_t)ptr)
  223. #endif
  224. #endif /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
  225. typedef struct WASMModuleCommon {
  226. /* Module type, for module loaded from WASM bytecode binary,
  227. this field is Wasm_Module_Bytecode, and this structure should
  228. be treated as WASMModule structure;
  229. for module loaded from AOT binary, this field is
  230. Wasm_Module_AoT, and this structure should be treated as
  231. AOTModule structure. */
  232. uint32 module_type;
  233. uint8 module_data[1];
  234. } WASMModuleCommon;
  235. typedef struct WASMModuleInstanceCommon {
  236. /* Module instance type, for module instance loaded from WASM
  237. bytecode binary, this field is Wasm_Module_Bytecode, and this
  238. structure should be treated as WASMModuleInstance structure;
  239. for module instance loaded from AOT binary, this field is
  240. Wasm_Module_AoT, and this structure should be treated as
  241. AOTModuleInstance structure. */
  242. uint32 module_type;
  243. uint8 module_inst_data[1];
  244. } WASMModuleInstanceCommon;
  245. typedef struct WASMModuleMemConsumption {
  246. uint32 total_size;
  247. uint32 module_struct_size;
  248. uint32 types_size;
  249. uint32 imports_size;
  250. uint32 functions_size;
  251. uint32 tables_size;
  252. uint32 memories_size;
  253. uint32 globals_size;
  254. uint32 exports_size;
  255. uint32 table_segs_size;
  256. uint32 data_segs_size;
  257. uint32 const_strs_size;
  258. #if WASM_ENABLE_AOT != 0
  259. uint32 aot_code_size;
  260. #endif
  261. } WASMModuleMemConsumption;
  262. typedef struct WASMModuleInstMemConsumption {
  263. uint32 total_size;
  264. uint32 module_inst_struct_size;
  265. uint32 memories_size;
  266. uint32 app_heap_size;
  267. uint32 tables_size;
  268. uint32 globals_size;
  269. uint32 functions_size;
  270. uint32 exports_size;
  271. } WASMModuleInstMemConsumption;
  272. #if WASM_ENABLE_LIBC_WASI != 0
  273. #if WASM_ENABLE_UVWASI == 0
  274. typedef struct WASIContext {
  275. struct fd_table *curfds;
  276. struct fd_prestats *prestats;
  277. struct argv_environ_values *argv_environ;
  278. char *argv_buf;
  279. char **argv_list;
  280. char *env_buf;
  281. char **env_list;
  282. } WASIContext;
  283. #else
  284. typedef uvwasi_t WASIContext;
  285. #endif
  286. #endif
  287. #if WASM_ENABLE_MULTI_MODULE != 0
  288. typedef struct WASMRegisteredModule {
  289. bh_list_link l;
  290. /* point to a string pool */
  291. const char *module_name;
  292. WASMModuleCommon *module;
  293. /* to store the original module file buffer address */
  294. uint8 *orig_file_buf;
  295. uint32 orig_file_buf_size;
  296. } WASMRegisteredModule;
  297. #endif
  298. typedef struct WASMMemoryInstanceCommon {
  299. uint32 module_type;
  300. uint8 memory_inst_data[1];
  301. } WASMMemoryInstanceCommon;
  302. typedef package_type_t PackageType;
  303. typedef wasm_section_t WASMSection, AOTSection;
  304. typedef struct wasm_frame_t {
  305. /* wasm_instance_t */
  306. void *instance;
  307. uint32 module_offset;
  308. uint32 func_index;
  309. uint32 func_offset;
  310. } WASMCApiFrame;
  311. /* See wasm_export.h for description */
  312. WASM_RUNTIME_API_EXTERN bool
  313. wasm_runtime_init(void);
  314. /* See wasm_export.h for description */
  315. WASM_RUNTIME_API_EXTERN bool
  316. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  317. /* See wasm_export.h for description */
  318. WASM_RUNTIME_API_EXTERN void
  319. wasm_runtime_destroy(void);
  320. /* See wasm_export.h for description */
  321. WASM_RUNTIME_API_EXTERN PackageType
  322. get_package_type(const uint8 *buf, uint32 size);
  323. /* See wasm_export.h for description */
  324. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  325. wasm_runtime_load(const uint8 *buf, uint32 size,
  326. char *error_buf, uint32 error_buf_size);
  327. /* See wasm_export.h for description */
  328. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  329. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  330. char *error_buf, uint32 error_buf_size);
  331. /* See wasm_export.h for description */
  332. WASM_RUNTIME_API_EXTERN void
  333. wasm_runtime_unload(WASMModuleCommon *module);
  334. /* Internal API */
  335. WASMModuleInstanceCommon *
  336. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  337. uint32 stack_size, uint32 heap_size,
  338. char *error_buf, uint32 error_buf_size);
  339. /* Internal API */
  340. void
  341. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  342. bool is_sub_inst);
  343. /* See wasm_export.h for description */
  344. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  345. wasm_runtime_instantiate(WASMModuleCommon *module,
  346. uint32 stack_size, uint32 heap_size,
  347. char *error_buf, uint32 error_buf_size);
  348. /* See wasm_export.h for description */
  349. WASM_RUNTIME_API_EXTERN void
  350. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  351. /* See wasm_export.h for description */
  352. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  353. wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
  354. const char *name, const char *signature);
  355. /* Internal API */
  356. WASMType *
  357. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  358. uint32 module_type);
  359. /* See wasm_export.h for description */
  360. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  361. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  362. uint32 stack_size);
  363. /* See wasm_export.h for description */
  364. WASM_RUNTIME_API_EXTERN void
  365. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  366. /* See wasm_export.h for description */
  367. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  368. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  369. /* See wasm_export.h for description */
  370. WASM_RUNTIME_API_EXTERN void *
  371. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  372. /* See wasm_export.h for description */
  373. WASM_RUNTIME_API_EXTERN void
  374. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  375. /* See wasm_export.h for description */
  376. WASM_RUNTIME_API_EXTERN void *
  377. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  378. /* See wasm_export.h for description */
  379. WASM_RUNTIME_API_EXTERN bool
  380. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  381. WASMFunctionInstanceCommon *function,
  382. uint32 argc, uint32 argv[]);
  383. WASM_RUNTIME_API_EXTERN bool
  384. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  385. WASMFunctionInstanceCommon *function,
  386. uint32 num_results, wasm_val_t *results,
  387. uint32 num_args, wasm_val_t *args);
  388. WASM_RUNTIME_API_EXTERN bool
  389. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  390. WASMFunctionInstanceCommon *function,
  391. uint32 num_results, wasm_val_t *results,
  392. uint32 num_args, ...);
  393. /**
  394. * Call a function reference of a given WASM runtime instance with
  395. * arguments.
  396. *
  397. * @param exec_env the execution environment to call the function
  398. * which must be created from wasm_create_exec_env()
  399. * @param element_indices the function ference indicies, usually
  400. * prvovided by the caller of a registed native function
  401. * @param argc the number of arguments
  402. * @param argv the arguments. If the function method has return value,
  403. * the first (or first two in case 64-bit return value) element of
  404. * argv stores the return value of the called WASM function after this
  405. * function returns.
  406. *
  407. * @return true if success, false otherwise and exception will be thrown,
  408. * the caller can call wasm_runtime_get_exception to get exception info.
  409. */
  410. bool
  411. wasm_runtime_call_indirect(WASMExecEnv *exec_env,
  412. uint32 element_indices,
  413. uint32 argc, uint32 argv[]);
  414. bool
  415. wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
  416. WASMFunctionInstanceCommon *function,
  417. uint32 argc, uint32 argv[]);
  418. bool
  419. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  420. WASMExecEnv *
  421. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  422. /* See wasm_export.h for description */
  423. WASM_RUNTIME_API_EXTERN bool
  424. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
  425. int32 argc, char *argv[]);
  426. /* See wasm_export.h for description */
  427. WASM_RUNTIME_API_EXTERN bool
  428. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  429. const char *name, int32 argc, char *argv[]);
  430. /* See wasm_export.h for description */
  431. WASM_RUNTIME_API_EXTERN void
  432. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  433. const char *exception);
  434. /* See wasm_export.h for description */
  435. WASM_RUNTIME_API_EXTERN const char *
  436. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  437. /* See wasm_export.h for description */
  438. WASM_RUNTIME_API_EXTERN void
  439. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  440. /* Internal API */
  441. void
  442. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  443. void *custom_data);
  444. /* See wasm_export.h for description */
  445. WASM_RUNTIME_API_EXTERN void
  446. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  447. void *custom_data);
  448. /* See wasm_export.h for description */
  449. WASM_RUNTIME_API_EXTERN void *
  450. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  451. /* See wasm_export.h for description */
  452. WASM_RUNTIME_API_EXTERN uint32
  453. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  454. void **p_native_addr);
  455. /* See wasm_export.h for description */
  456. WASM_RUNTIME_API_EXTERN void
  457. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  458. /* See wasm_export.h for description */
  459. WASM_RUNTIME_API_EXTERN uint32
  460. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  461. const char *src, uint32 size);
  462. /* See wasm_export.h for description */
  463. WASM_RUNTIME_API_EXTERN bool
  464. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  465. uint32 app_offset, uint32 size);
  466. /* See wasm_export.h for description */
  467. WASM_RUNTIME_API_EXTERN bool
  468. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  469. uint32 app_str_offset);
  470. /* See wasm_export.h for description */
  471. WASM_RUNTIME_API_EXTERN bool
  472. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  473. void *native_ptr, uint32 size);
  474. /* See wasm_export.h for description */
  475. WASM_RUNTIME_API_EXTERN void *
  476. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  477. uint32 app_offset);
  478. /* See wasm_export.h for description */
  479. WASM_RUNTIME_API_EXTERN uint32
  480. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  481. void *native_ptr);
  482. /* See wasm_export.h for description */
  483. WASM_RUNTIME_API_EXTERN bool
  484. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  485. uint32 app_offset,
  486. uint32 *p_app_start_offset,
  487. uint32 *p_app_end_offset);
  488. /* See wasm_export.h for description */
  489. WASM_RUNTIME_API_EXTERN bool
  490. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  491. uint8 *native_ptr,
  492. uint8 **p_native_start_addr,
  493. uint8 **p_native_end_addr);
  494. uint32
  495. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst);
  496. void
  497. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  498. uint32 temp_ret);
  499. uint32
  500. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst);
  501. void
  502. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  503. uint32 llvm_stack);
  504. #if WASM_ENABLE_MULTI_MODULE != 0
  505. WASM_RUNTIME_API_EXTERN void
  506. wasm_runtime_set_module_reader(const module_reader reader,
  507. const module_destroyer destroyer);
  508. module_reader
  509. wasm_runtime_get_module_reader();
  510. module_destroyer
  511. wasm_runtime_get_module_destroyer();
  512. bool
  513. wasm_runtime_register_module_internal(const char *module_name,
  514. WASMModuleCommon *module,
  515. uint8 *orig_file_buf,
  516. uint32 orig_file_buf_size,
  517. char *error_buf,
  518. uint32 error_buf_size);
  519. void
  520. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  521. bool
  522. wasm_runtime_is_module_registered(const char *module_name);
  523. bool
  524. wasm_runtime_add_loading_module(const char *module_name,
  525. char *error_buf, uint32 error_buf_size);
  526. void
  527. wasm_runtime_delete_loading_module(const char *module_name);
  528. bool
  529. wasm_runtime_is_loading_module(const char *module_name);
  530. void
  531. wasm_runtime_destroy_loading_module_list();
  532. #endif /* WASM_ENALBE_MULTI_MODULE */
  533. bool
  534. wasm_runtime_is_built_in_module(const char *module_name);
  535. #if WASM_ENABLE_THREAD_MGR != 0
  536. bool
  537. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env,
  538. uint32 *start_offset, uint32 *size);
  539. bool
  540. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env,
  541. uint32 start_offset, uint32 size);
  542. #endif
  543. #if WASM_ENABLE_LIBC_WASI != 0
  544. WASM_RUNTIME_API_EXTERN void
  545. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module,
  546. const char *dir_list[], uint32 dir_count,
  547. const char *map_dir_list[], uint32 map_dir_count,
  548. const char *env_list[], uint32 env_count,
  549. char *argv[], int argc,
  550. int stdinfd, int stdoutfd, int stderrfd);
  551. /* See wasm_export.h for description */
  552. WASM_RUNTIME_API_EXTERN void
  553. wasm_runtime_set_wasi_args(WASMModuleCommon *module,
  554. const char *dir_list[], uint32 dir_count,
  555. const char *map_dir_list[], uint32 map_dir_count,
  556. const char *env_list[], uint32 env_count,
  557. char *argv[], int argc);
  558. /* See wasm_export.h for description */
  559. WASM_RUNTIME_API_EXTERN bool
  560. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  561. /* See wasm_export.h for description */
  562. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  563. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  564. bool
  565. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  566. const char *dir_list[], uint32 dir_count,
  567. const char *map_dir_list[], uint32 map_dir_count,
  568. const char *env[], uint32 env_count,
  569. char *argv[], uint32 argc,
  570. int stdinfd, int stdoutfd, int stderrfd,
  571. char *error_buf, uint32 error_buf_size);
  572. void
  573. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  574. void
  575. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  576. WASIContext *wasi_ctx);
  577. WASIContext *
  578. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  579. #endif /* end of WASM_ENABLE_LIBC_WASI */
  580. #if WASM_ENABLE_REF_TYPES != 0
  581. /* See wasm_export.h for description */
  582. WASM_RUNTIME_API_EXTERN bool
  583. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst,
  584. void *extern_obj, uint32 *p_externref_idx);
  585. /* See wasm_export.h for description */
  586. WASM_RUNTIME_API_EXTERN bool
  587. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  588. /* See wasm_export.h for description */
  589. WASM_RUNTIME_API_EXTERN bool
  590. wasm_externref_retain(uint32 externref_idx);
  591. /**
  592. * Reclaim the externref objects/indexes which are not used by
  593. * module instance
  594. */
  595. void
  596. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  597. /**
  598. * Cleanup the externref objects/indexes of the module instance
  599. */
  600. void
  601. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  602. #endif /* end of WASM_ENABLE_REF_TYPES */
  603. /* Get module of the current exec_env */
  604. WASMModuleCommon*
  605. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  606. /**
  607. * Enlarge wasm memory data space.
  608. *
  609. * @param module the wasm module instance
  610. * @param inc_page_count denote the page number to increase
  611. * @return return true if enlarge successfully, false otherwise
  612. */
  613. bool
  614. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, uint32 inc_page_count);
  615. /* See wasm_export.h for description */
  616. WASM_RUNTIME_API_EXTERN bool
  617. wasm_runtime_register_natives(const char *module_name,
  618. NativeSymbol *native_symbols,
  619. uint32 n_native_symbols);
  620. /* See wasm_export.h for description */
  621. WASM_RUNTIME_API_EXTERN bool
  622. wasm_runtime_register_natives_raw(const char *module_name,
  623. NativeSymbol *native_symbols,
  624. uint32 n_native_symbols);
  625. bool
  626. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  627. const WASMType *func_type, const char *signature,
  628. void *attachment,
  629. uint32 *argv, uint32 argc, uint32 *ret);
  630. bool
  631. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  632. const WASMType *func_type, const char *signature,
  633. void *attachment,
  634. uint32 *argv, uint32 argc, uint32 *ret);
  635. void
  636. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  637. void
  638. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  639. void
  640. wasm_runtime_dump_module_inst_mem_consumption(const WASMModuleInstanceCommon
  641. *module_inst);
  642. void
  643. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  644. #if WASM_ENABLE_REF_TYPES != 0
  645. void
  646. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  647. WASMFunctionInstanceCommon *function);
  648. void
  649. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  650. WASMFunctionInstanceCommon *function,
  651. bool ret, uint32 *argv);
  652. #endif
  653. bool
  654. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  655. const WASMExport *export,
  656. WASMType **out);
  657. bool
  658. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  659. const WASMExport *export,
  660. uint8 *out_val_type,
  661. bool *out_mutability);
  662. bool
  663. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  664. const WASMExport *export,
  665. uint32 *out_min_page,
  666. uint32 *out_max_page);
  667. bool
  668. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  669. const WASMExport *export,
  670. uint8 *out_elem_type,
  671. uint32 *out_min_size,
  672. uint32 *out_max_size);
  673. uint8 *
  674. wasm_runtime_get_memory_data(const WASMModuleInstanceCommon *module_inst_comm,
  675. uint32 memory_inst_idx);
  676. uint32
  677. wasm_runtime_get_memory_data_size(const WASMModuleInstanceCommon *module_inst_comm,
  678. uint32 memory_inst_idx);
  679. bool
  680. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  681. void *func_ptr, WASMType *func_type,
  682. uint32 argc, uint32 *argv,
  683. bool with_env, void *wasm_c_api_env);
  684. #ifdef __cplusplus
  685. }
  686. #endif
  687. #endif /* end of _WASM_COMMON_H */