wasm_runtime_common.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. /* See wasm_export.h for description */
  305. WASM_RUNTIME_API_EXTERN bool
  306. wasm_runtime_init(void);
  307. /* See wasm_export.h for description */
  308. WASM_RUNTIME_API_EXTERN bool
  309. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  310. /* See wasm_export.h for description */
  311. WASM_RUNTIME_API_EXTERN void
  312. wasm_runtime_destroy(void);
  313. /* See wasm_export.h for description */
  314. WASM_RUNTIME_API_EXTERN PackageType
  315. get_package_type(const uint8 *buf, uint32 size);
  316. /* See wasm_export.h for description */
  317. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  318. wasm_runtime_load(const uint8 *buf, uint32 size,
  319. char *error_buf, uint32 error_buf_size);
  320. /* See wasm_export.h for description */
  321. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  322. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  323. char *error_buf, uint32 error_buf_size);
  324. /* See wasm_export.h for description */
  325. WASM_RUNTIME_API_EXTERN void
  326. wasm_runtime_unload(WASMModuleCommon *module);
  327. /* Internal API */
  328. WASMModuleInstanceCommon *
  329. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  330. uint32 stack_size, uint32 heap_size,
  331. char *error_buf, uint32 error_buf_size);
  332. /* Internal API */
  333. void
  334. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  335. bool is_sub_inst);
  336. /* See wasm_export.h for description */
  337. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  338. wasm_runtime_instantiate(WASMModuleCommon *module,
  339. uint32 stack_size, uint32 heap_size,
  340. char *error_buf, uint32 error_buf_size);
  341. /* See wasm_export.h for description */
  342. WASM_RUNTIME_API_EXTERN void
  343. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  344. /* See wasm_export.h for description */
  345. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  346. wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
  347. const char *name, const char *signature);
  348. /* Internal API */
  349. WASMType *
  350. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  351. uint32 module_type);
  352. /* See wasm_export.h for description */
  353. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  354. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  355. uint32 stack_size);
  356. /* See wasm_export.h for description */
  357. WASM_RUNTIME_API_EXTERN void
  358. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  359. /* See wasm_export.h for description */
  360. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  361. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  362. /* See wasm_export.h for description */
  363. WASM_RUNTIME_API_EXTERN void *
  364. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  365. /* See wasm_export.h for description */
  366. WASM_RUNTIME_API_EXTERN void
  367. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  368. /* See wasm_export.h for description */
  369. WASM_RUNTIME_API_EXTERN void *
  370. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  371. /* See wasm_export.h for description */
  372. WASM_RUNTIME_API_EXTERN bool
  373. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  374. WASMFunctionInstanceCommon *function,
  375. uint32 argc, uint32 argv[]);
  376. WASM_RUNTIME_API_EXTERN bool
  377. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  378. WASMFunctionInstanceCommon *function,
  379. uint32 num_results, wasm_val_t *results,
  380. uint32 num_args, wasm_val_t *args);
  381. WASM_RUNTIME_API_EXTERN bool
  382. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  383. WASMFunctionInstanceCommon *function,
  384. uint32 num_results, wasm_val_t *results,
  385. uint32 num_args, ...);
  386. /**
  387. * Call a function reference of a given WASM runtime instance with
  388. * arguments.
  389. *
  390. * @param exec_env the execution environment to call the function
  391. * which must be created from wasm_create_exec_env()
  392. * @param element_indices the function ference indicies, usually
  393. * prvovided by the caller of a registed native function
  394. * @param argc the number of arguments
  395. * @param argv the arguments. If the function method has return value,
  396. * the first (or first two in case 64-bit return value) element of
  397. * argv stores the return value of the called WASM function after this
  398. * function returns.
  399. *
  400. * @return true if success, false otherwise and exception will be thrown,
  401. * the caller can call wasm_runtime_get_exception to get exception info.
  402. */
  403. bool
  404. wasm_runtime_call_indirect(WASMExecEnv *exec_env,
  405. uint32 element_indices,
  406. uint32 argc, uint32 argv[]);
  407. bool
  408. wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
  409. WASMFunctionInstanceCommon *function,
  410. uint32 argc, uint32 argv[]);
  411. /* See wasm_export.h for description */
  412. WASM_RUNTIME_API_EXTERN bool
  413. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
  414. int32 argc, char *argv[]);
  415. /* See wasm_export.h for description */
  416. WASM_RUNTIME_API_EXTERN bool
  417. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  418. const char *name, int32 argc, char *argv[]);
  419. /* See wasm_export.h for description */
  420. WASM_RUNTIME_API_EXTERN void
  421. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  422. const char *exception);
  423. /* See wasm_export.h for description */
  424. WASM_RUNTIME_API_EXTERN const char *
  425. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  426. /* See wasm_export.h for description */
  427. WASM_RUNTIME_API_EXTERN void
  428. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  429. /* See wasm_export.h for description */
  430. WASM_RUNTIME_API_EXTERN void
  431. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  432. void *custom_data);
  433. /* See wasm_export.h for description */
  434. WASM_RUNTIME_API_EXTERN void *
  435. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  436. /* See wasm_export.h for description */
  437. WASM_RUNTIME_API_EXTERN uint32
  438. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  439. void **p_native_addr);
  440. /* See wasm_export.h for description */
  441. WASM_RUNTIME_API_EXTERN void
  442. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  443. /* See wasm_export.h for description */
  444. WASM_RUNTIME_API_EXTERN uint32
  445. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  446. const char *src, uint32 size);
  447. /* See wasm_export.h for description */
  448. WASM_RUNTIME_API_EXTERN bool
  449. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  450. uint32 app_offset, uint32 size);
  451. /* See wasm_export.h for description */
  452. WASM_RUNTIME_API_EXTERN bool
  453. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  454. uint32 app_str_offset);
  455. /* See wasm_export.h for description */
  456. WASM_RUNTIME_API_EXTERN bool
  457. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  458. void *native_ptr, uint32 size);
  459. /* See wasm_export.h for description */
  460. WASM_RUNTIME_API_EXTERN void *
  461. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  462. uint32 app_offset);
  463. /* See wasm_export.h for description */
  464. WASM_RUNTIME_API_EXTERN uint32
  465. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  466. void *native_ptr);
  467. /* See wasm_export.h for description */
  468. WASM_RUNTIME_API_EXTERN bool
  469. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  470. uint32 app_offset,
  471. uint32 *p_app_start_offset,
  472. uint32 *p_app_end_offset);
  473. /* See wasm_export.h for description */
  474. WASM_RUNTIME_API_EXTERN bool
  475. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  476. uint8 *native_ptr,
  477. uint8 **p_native_start_addr,
  478. uint8 **p_native_end_addr);
  479. uint32
  480. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst);
  481. void
  482. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  483. uint32 temp_ret);
  484. uint32
  485. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst);
  486. void
  487. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  488. uint32 llvm_stack);
  489. #if WASM_ENABLE_MULTI_MODULE != 0
  490. WASM_RUNTIME_API_EXTERN void
  491. wasm_runtime_set_module_reader(const module_reader reader,
  492. const module_destroyer destroyer);
  493. module_reader
  494. wasm_runtime_get_module_reader();
  495. module_destroyer
  496. wasm_runtime_get_module_destroyer();
  497. bool
  498. wasm_runtime_register_module_internal(const char *module_name,
  499. WASMModuleCommon *module,
  500. uint8 *orig_file_buf,
  501. uint32 orig_file_buf_size,
  502. char *error_buf,
  503. uint32 error_buf_size);
  504. void
  505. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  506. bool
  507. wasm_runtime_is_module_registered(const char *module_name);
  508. bool
  509. wasm_runtime_add_loading_module(const char *module_name,
  510. char *error_buf, uint32 error_buf_size);
  511. void
  512. wasm_runtime_delete_loading_module(const char *module_name);
  513. bool
  514. wasm_runtime_is_loading_module(const char *module_name);
  515. void
  516. wasm_runtime_destroy_loading_module_list();
  517. #endif /* WASM_ENALBE_MULTI_MODULE */
  518. bool
  519. wasm_runtime_is_built_in_module(const char *module_name);
  520. #if WASM_ENABLE_THREAD_MGR != 0
  521. bool
  522. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env,
  523. uint32 *start_offset, uint32 *size);
  524. bool
  525. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env,
  526. uint32 start_offset, uint32 size);
  527. #endif
  528. #if WASM_ENABLE_LIBC_WASI != 0
  529. /* See wasm_export.h for description */
  530. WASM_RUNTIME_API_EXTERN void
  531. wasm_runtime_set_wasi_args(WASMModuleCommon *module,
  532. const char *dir_list[], uint32 dir_count,
  533. const char *map_dir_list[], uint32 map_dir_count,
  534. const char *env_list[], uint32 env_count,
  535. char *argv[], int argc);
  536. /* See wasm_export.h for description */
  537. WASM_RUNTIME_API_EXTERN bool
  538. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  539. /* See wasm_export.h for description */
  540. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  541. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  542. bool
  543. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  544. const char *dir_list[], uint32 dir_count,
  545. const char *map_dir_list[], uint32 map_dir_count,
  546. const char *env[], uint32 env_count,
  547. char *argv[], uint32 argc,
  548. char *error_buf, uint32 error_buf_size);
  549. void
  550. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  551. void
  552. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  553. WASIContext *wasi_ctx);
  554. WASIContext *
  555. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  556. #endif /* end of WASM_ENABLE_LIBC_WASI */
  557. #if WASM_ENABLE_REF_TYPES != 0
  558. /* See wasm_export.h for description */
  559. WASM_RUNTIME_API_EXTERN bool
  560. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst,
  561. void *extern_obj, uint32 *p_externref_idx);
  562. /* See wasm_export.h for description */
  563. WASM_RUNTIME_API_EXTERN bool
  564. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  565. /* See wasm_export.h for description */
  566. WASM_RUNTIME_API_EXTERN bool
  567. wasm_externref_retain(uint32 externref_idx);
  568. /**
  569. * Reclaim the externref objects/indexes which are not used by
  570. * module instance
  571. */
  572. void
  573. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  574. /**
  575. * Cleanup the externref objects/indexes of the module instance
  576. */
  577. void
  578. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  579. #endif /* end of WASM_ENABLE_REF_TYPES */
  580. /* Get module of the current exec_env */
  581. WASMModuleCommon*
  582. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  583. /**
  584. * Enlarge wasm memory data space.
  585. *
  586. * @param module the wasm module instance
  587. * @param inc_page_count denote the page number to increase
  588. * @return return true if enlarge successfully, false otherwise
  589. */
  590. bool
  591. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module, uint32 inc_page_count);
  592. /* See wasm_export.h for description */
  593. WASM_RUNTIME_API_EXTERN bool
  594. wasm_runtime_register_natives(const char *module_name,
  595. NativeSymbol *native_symbols,
  596. uint32 n_native_symbols);
  597. /* See wasm_export.h for description */
  598. WASM_RUNTIME_API_EXTERN bool
  599. wasm_runtime_register_natives_raw(const char *module_name,
  600. NativeSymbol *native_symbols,
  601. uint32 n_native_symbols);
  602. bool
  603. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  604. const WASMType *func_type, const char *signature,
  605. void *attachment,
  606. uint32 *argv, uint32 argc, uint32 *ret);
  607. bool
  608. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  609. const WASMType *func_type, const char *signature,
  610. void *attachment,
  611. uint32 *argv, uint32 argc, uint32 *ret);
  612. void
  613. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  614. void
  615. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  616. void
  617. wasm_runtime_dump_module_inst_mem_consumption(const WASMModuleInstanceCommon
  618. *module_inst);
  619. void
  620. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  621. #if WASM_ENABLE_REF_TYPES != 0
  622. void
  623. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  624. WASMFunctionInstanceCommon *function);
  625. void
  626. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  627. WASMFunctionInstanceCommon *function,
  628. bool ret, uint32 *argv);
  629. #endif
  630. bool
  631. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  632. const WASMExport *export,
  633. WASMType **out);
  634. bool
  635. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  636. const WASMExport *export,
  637. uint8 *out_val_type,
  638. bool *out_mutability);
  639. bool
  640. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  641. const WASMExport *export,
  642. uint32 *out_min_page,
  643. uint32 *out_max_page);
  644. bool
  645. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  646. const WASMExport *export,
  647. uint8 *out_elem_type,
  648. uint32 *out_min_size,
  649. uint32 *out_max_size);
  650. uint8 *
  651. wasm_runtime_get_memory_data(const WASMModuleInstanceCommon *module_inst_comm,
  652. uint32 memory_inst_idx);
  653. uint32
  654. wasm_runtime_get_memory_data_size(const WASMModuleInstanceCommon *module_inst_comm,
  655. uint32 memory_inst_idx);
  656. #ifdef __cplusplus
  657. }
  658. #endif
  659. #endif /* end of _WASM_COMMON_H */