wasm_runtime_common.h 28 KB

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