wasm_runtime_common.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 addr_ = (uintptr_t)(addr); \
  105. union { \
  106. int64 val; \
  107. uint32 u32[2]; \
  108. uint16 u16[4]; \
  109. uint8 u8[8]; \
  110. } u; \
  111. if ((addr_ & (uintptr_t)7) == 0) \
  112. *(int64 *)(addr) = (int64)(value); \
  113. else { \
  114. u.val = (int64)(value); \
  115. if ((addr_ & (uintptr_t)3) == 0) { \
  116. ((uint32 *)(addr))[0] = u.u32[0]; \
  117. ((uint32 *)(addr))[1] = u.u32[1]; \
  118. } \
  119. else if ((addr_ & (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 addr_ = (uintptr_t)(addr); \
  135. union { \
  136. uint32 val; \
  137. uint16 u16[2]; \
  138. uint8 u8[4]; \
  139. } u; \
  140. if ((addr_ & (uintptr_t)3) == 0) \
  141. *(uint32 *)(addr) = (uint32)(value); \
  142. else { \
  143. u.val = (uint32)(value); \
  144. if ((addr_ & (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. /* The following uint8[1] member is a dummy just to indicate
  280. some module_type dependent members follow.
  281. Typically it should be accessed by casting to the corresponding
  282. actual module_type dependent structure, not via this member. */
  283. uint8 module_data[1];
  284. } WASMModuleCommon;
  285. typedef struct WASMModuleInstanceCommon {
  286. /* Module instance type, for module instance loaded from WASM
  287. bytecode binary, this field is Wasm_Module_Bytecode, and this
  288. structure should be treated as WASMModuleInstance structure;
  289. for module instance loaded from AOT binary, this field is
  290. Wasm_Module_AoT, and this structure should be treated as
  291. AOTModuleInstance structure. */
  292. uint32 module_type;
  293. /* The following uint8[1] member is a dummy just to indicate
  294. some module_type dependent members follow.
  295. Typically it should be accessed by casting to the corresponding
  296. actual module_type dependent structure, not via this member. */
  297. uint8 module_inst_data[1];
  298. } WASMModuleInstanceCommon;
  299. typedef struct WASMModuleMemConsumption {
  300. uint32 total_size;
  301. uint32 module_struct_size;
  302. uint32 types_size;
  303. uint32 imports_size;
  304. uint32 functions_size;
  305. uint32 tables_size;
  306. uint32 memories_size;
  307. uint32 globals_size;
  308. uint32 exports_size;
  309. uint32 table_segs_size;
  310. uint32 data_segs_size;
  311. uint32 const_strs_size;
  312. #if WASM_ENABLE_AOT != 0
  313. uint32 aot_code_size;
  314. #endif
  315. } WASMModuleMemConsumption;
  316. typedef struct WASMModuleInstMemConsumption {
  317. uint32 total_size;
  318. uint32 module_inst_struct_size;
  319. uint32 memories_size;
  320. uint32 app_heap_size;
  321. uint32 tables_size;
  322. uint32 globals_size;
  323. uint32 functions_size;
  324. uint32 exports_size;
  325. } WASMModuleInstMemConsumption;
  326. #if WASM_ENABLE_LIBC_WASI != 0
  327. #if WASM_ENABLE_UVWASI == 0
  328. typedef struct WASIContext {
  329. struct fd_table *curfds;
  330. struct fd_prestats *prestats;
  331. struct argv_environ_values *argv_environ;
  332. struct addr_pool *addr_pool;
  333. char *argv_buf;
  334. char **argv_list;
  335. char *env_buf;
  336. char **env_list;
  337. } WASIContext;
  338. #else
  339. typedef uvwasi_t WASIContext;
  340. #endif
  341. #endif
  342. #if WASM_ENABLE_MULTI_MODULE != 0
  343. typedef struct WASMRegisteredModule {
  344. bh_list_link l;
  345. /* point to a string pool */
  346. const char *module_name;
  347. WASMModuleCommon *module;
  348. /* to store the original module file buffer address */
  349. uint8 *orig_file_buf;
  350. uint32 orig_file_buf_size;
  351. } WASMRegisteredModule;
  352. #endif
  353. typedef struct WASMMemoryInstanceCommon {
  354. uint32 module_type;
  355. /* The following uint8[1] member is a dummy just to indicate
  356. some module_type dependent members follow.
  357. Typically it should be accessed by casting to the corresponding
  358. actual module_type dependent structure, not via this member. */
  359. uint8 memory_inst_data[1];
  360. } WASMMemoryInstanceCommon;
  361. typedef package_type_t PackageType;
  362. typedef wasm_section_t WASMSection, AOTSection;
  363. typedef struct wasm_frame_t {
  364. /* wasm_instance_t */
  365. void *instance;
  366. uint32 module_offset;
  367. uint32 func_index;
  368. uint32 func_offset;
  369. } WASMCApiFrame;
  370. /* See wasm_export.h for description */
  371. WASM_RUNTIME_API_EXTERN bool
  372. wasm_runtime_init(void);
  373. /* See wasm_export.h for description */
  374. WASM_RUNTIME_API_EXTERN bool
  375. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  376. /* See wasm_export.h for description */
  377. WASM_RUNTIME_API_EXTERN void
  378. wasm_runtime_destroy(void);
  379. /* See wasm_export.h for description */
  380. WASM_RUNTIME_API_EXTERN PackageType
  381. get_package_type(const uint8 *buf, uint32 size);
  382. /* See wasm_export.h for description */
  383. WASM_RUNTIME_API_EXTERN bool
  384. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size);
  385. /* See wasm_export.h for description */
  386. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  387. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  388. uint32 error_buf_size);
  389. /* See wasm_export.h for description */
  390. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  391. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  392. char *error_buf, uint32 error_buf_size);
  393. /* See wasm_export.h for description */
  394. WASM_RUNTIME_API_EXTERN void
  395. wasm_runtime_unload(WASMModuleCommon *module);
  396. /* Internal API */
  397. WASMModuleInstanceCommon *
  398. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  399. uint32 stack_size, uint32 heap_size,
  400. char *error_buf, uint32 error_buf_size);
  401. /* Internal API */
  402. void
  403. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  404. bool is_sub_inst);
  405. /* See wasm_export.h for description */
  406. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  407. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
  408. uint32 heap_size, char *error_buf,
  409. uint32 error_buf_size);
  410. /* See wasm_export.h for description */
  411. WASM_RUNTIME_API_EXTERN void
  412. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  413. /* See wasm_export.h for description */
  414. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  415. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  416. const char *name, const char *signature);
  417. /* Internal API */
  418. WASMType *
  419. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  420. uint32 module_type);
  421. /* See wasm_export.h for description */
  422. WASM_RUNTIME_API_EXTERN uint32
  423. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  424. WASMModuleInstanceCommon *const module_inst);
  425. /* See wasm_export.h for description */
  426. WASM_RUNTIME_API_EXTERN uint32
  427. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  428. WASMModuleInstanceCommon *const module_inst);
  429. /* See wasm_export.h for description */
  430. WASM_RUNTIME_API_EXTERN void
  431. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  432. WASMModuleInstanceCommon *const module_inst,
  433. wasm_valkind_t *param_types);
  434. /* See wasm_export.h for description */
  435. WASM_RUNTIME_API_EXTERN void
  436. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  437. WASMModuleInstanceCommon *const module_inst,
  438. wasm_valkind_t *result_types);
  439. /* See wasm_export.h for description */
  440. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  441. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  442. uint32 stack_size);
  443. /* See wasm_export.h for description */
  444. WASM_RUNTIME_API_EXTERN void
  445. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  446. /* See wasm_export.h for description */
  447. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  448. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  449. /* See wasm_export.h for description */
  450. WASM_RUNTIME_API_EXTERN void *
  451. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  452. /* See wasm_export.h for description */
  453. WASM_RUNTIME_API_EXTERN void
  454. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  455. /* See wasm_export.h for description */
  456. WASM_RUNTIME_API_EXTERN void *
  457. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  458. /* See wasm_export.h for description */
  459. WASM_RUNTIME_API_EXTERN bool
  460. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  461. WASMFunctionInstanceCommon *function, uint32 argc,
  462. uint32 argv[]);
  463. WASM_RUNTIME_API_EXTERN bool
  464. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  465. WASMFunctionInstanceCommon *function,
  466. uint32 num_results, wasm_val_t *results,
  467. uint32 num_args, wasm_val_t *args);
  468. WASM_RUNTIME_API_EXTERN bool
  469. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  470. WASMFunctionInstanceCommon *function,
  471. uint32 num_results, wasm_val_t *results,
  472. uint32 num_args, ...);
  473. #if WASM_ENABLE_DEBUG_INTERP != 0
  474. /* See wasm_export.h for description */
  475. WASM_RUNTIME_API_EXTERN uint32
  476. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
  477. #endif
  478. /**
  479. * Call a function reference of a given WASM runtime instance with
  480. * arguments.
  481. *
  482. * @param exec_env the execution environment to call the function
  483. * which must be created from wasm_create_exec_env()
  484. * @param element_indices the function ference indicies, usually
  485. * prvovided by the caller of a registed native function
  486. * @param argc the number of arguments
  487. * @param argv the arguments. If the function method has return value,
  488. * the first (or first two in case 64-bit return value) element of
  489. * argv stores the return value of the called WASM function after this
  490. * function returns.
  491. *
  492. * @return true if success, false otherwise and exception will be thrown,
  493. * the caller can call wasm_runtime_get_exception to get exception info.
  494. */
  495. bool
  496. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
  497. uint32 argc, uint32 argv[]);
  498. bool
  499. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  500. WASMExecEnv *
  501. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  502. /* See wasm_export.h for description */
  503. WASM_RUNTIME_API_EXTERN bool
  504. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  505. char *argv[]);
  506. /* See wasm_export.h for description */
  507. WASM_RUNTIME_API_EXTERN bool
  508. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  509. const char *name, int32 argc, char *argv[]);
  510. /* See wasm_export.h for description */
  511. WASM_RUNTIME_API_EXTERN void
  512. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  513. const char *exception);
  514. /* See wasm_export.h for description */
  515. WASM_RUNTIME_API_EXTERN const char *
  516. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  517. /* See wasm_export.h for description */
  518. WASM_RUNTIME_API_EXTERN void
  519. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  520. /* Internal API */
  521. void
  522. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  523. void *custom_data);
  524. /* See wasm_export.h for description */
  525. WASM_RUNTIME_API_EXTERN void
  526. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  527. void *custom_data);
  528. /* See wasm_export.h for description */
  529. WASM_RUNTIME_API_EXTERN void *
  530. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  531. /* See wasm_export.h for description */
  532. WASM_RUNTIME_API_EXTERN uint32
  533. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  534. void **p_native_addr);
  535. /* See wasm_export.h for description */
  536. WASM_RUNTIME_API_EXTERN void
  537. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  538. /* See wasm_export.h for description */
  539. WASM_RUNTIME_API_EXTERN uint32
  540. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  541. const char *src, uint32 size);
  542. /* See wasm_export.h for description */
  543. WASM_RUNTIME_API_EXTERN bool
  544. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  545. uint32 app_offset, uint32 size);
  546. /* See wasm_export.h for description */
  547. WASM_RUNTIME_API_EXTERN bool
  548. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  549. uint32 app_str_offset);
  550. /* See wasm_export.h for description */
  551. WASM_RUNTIME_API_EXTERN bool
  552. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  553. void *native_ptr, uint32 size);
  554. /* See wasm_export.h for description */
  555. WASM_RUNTIME_API_EXTERN void *
  556. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  557. uint32 app_offset);
  558. /* See wasm_export.h for description */
  559. WASM_RUNTIME_API_EXTERN uint32
  560. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  561. void *native_ptr);
  562. /* See wasm_export.h for description */
  563. WASM_RUNTIME_API_EXTERN bool
  564. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  565. uint32 app_offset, uint32 *p_app_start_offset,
  566. uint32 *p_app_end_offset);
  567. /* See wasm_export.h for description */
  568. WASM_RUNTIME_API_EXTERN bool
  569. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  570. uint8 *native_ptr,
  571. uint8 **p_native_start_addr,
  572. uint8 **p_native_end_addr);
  573. uint32
  574. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst);
  575. void
  576. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  577. uint32 temp_ret);
  578. uint32
  579. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst);
  580. void
  581. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  582. uint32 llvm_stack);
  583. #if WASM_ENABLE_MULTI_MODULE != 0
  584. WASM_RUNTIME_API_EXTERN void
  585. wasm_runtime_set_module_reader(const module_reader reader,
  586. const module_destroyer destroyer);
  587. module_reader
  588. wasm_runtime_get_module_reader();
  589. module_destroyer
  590. wasm_runtime_get_module_destroyer();
  591. bool
  592. wasm_runtime_register_module_internal(const char *module_name,
  593. WASMModuleCommon *module,
  594. uint8 *orig_file_buf,
  595. uint32 orig_file_buf_size,
  596. char *error_buf, uint32 error_buf_size);
  597. void
  598. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  599. bool
  600. wasm_runtime_is_module_registered(const char *module_name);
  601. bool
  602. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  603. uint32 error_buf_size);
  604. void
  605. wasm_runtime_delete_loading_module(const char *module_name);
  606. bool
  607. wasm_runtime_is_loading_module(const char *module_name);
  608. void
  609. wasm_runtime_destroy_loading_module_list();
  610. #endif /* WASM_ENALBE_MULTI_MODULE */
  611. bool
  612. wasm_runtime_is_built_in_module(const char *module_name);
  613. #if WASM_ENABLE_THREAD_MGR != 0
  614. bool
  615. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  616. uint32 *size);
  617. bool
  618. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  619. uint32 size);
  620. #endif
  621. #if WASM_ENABLE_LIBC_WASI != 0
  622. WASM_RUNTIME_API_EXTERN void
  623. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  624. uint32 dir_count, const char *map_dir_list[],
  625. uint32 map_dir_count, const char *env_list[],
  626. uint32 env_count, char *argv[], int argc,
  627. int stdinfd, int stdoutfd, int stderrfd);
  628. /* See wasm_export.h for description */
  629. WASM_RUNTIME_API_EXTERN void
  630. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  631. uint32 dir_count, const char *map_dir_list[],
  632. uint32 map_dir_count, const char *env_list[],
  633. uint32 env_count, char *argv[], int argc);
  634. /* See wasm_export.h for description */
  635. WASM_RUNTIME_API_EXTERN bool
  636. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  637. /* See wasm_export.h for description */
  638. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  639. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  640. bool
  641. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  642. const char *dir_list[], uint32 dir_count,
  643. const char *map_dir_list[], uint32 map_dir_count,
  644. const char *env[], uint32 env_count,
  645. const char *addr_pool[], uint32 addr_pool_size,
  646. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  647. int stderrfd, char *error_buf, uint32 error_buf_size);
  648. void
  649. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  650. void
  651. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  652. WASIContext *wasi_ctx);
  653. WASIContext *
  654. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  655. WASM_RUNTIME_API_EXTERN void
  656. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  657. uint32 addr_pool_size);
  658. #endif /* end of WASM_ENABLE_LIBC_WASI */
  659. #if WASM_ENABLE_REF_TYPES != 0
  660. /* See wasm_export.h for description */
  661. WASM_RUNTIME_API_EXTERN bool
  662. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  663. uint32 *p_externref_idx);
  664. /* See wasm_export.h for description */
  665. WASM_RUNTIME_API_EXTERN bool
  666. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  667. /* See wasm_export.h for description */
  668. WASM_RUNTIME_API_EXTERN bool
  669. wasm_externref_retain(uint32 externref_idx);
  670. /**
  671. * Reclaim the externref objects/indexes which are not used by
  672. * module instance
  673. */
  674. void
  675. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  676. /**
  677. * Cleanup the externref objects/indexes of the module instance
  678. */
  679. void
  680. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  681. #endif /* end of WASM_ENABLE_REF_TYPES */
  682. /* Get module of the current exec_env */
  683. WASMModuleCommon *
  684. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  685. /**
  686. * Enlarge wasm memory data space.
  687. *
  688. * @param module the wasm module instance
  689. * @param inc_page_count denote the page number to increase
  690. * @return return true if enlarge successfully, false otherwise
  691. */
  692. bool
  693. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
  694. uint32 inc_page_count);
  695. /* See wasm_export.h for description */
  696. WASM_RUNTIME_API_EXTERN bool
  697. wasm_runtime_register_natives(const char *module_name,
  698. NativeSymbol *native_symbols,
  699. uint32 n_native_symbols);
  700. /* See wasm_export.h for description */
  701. WASM_RUNTIME_API_EXTERN bool
  702. wasm_runtime_register_natives_raw(const char *module_name,
  703. NativeSymbol *native_symbols,
  704. uint32 n_native_symbols);
  705. bool
  706. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  707. const WASMType *func_type, const char *signature,
  708. void *attachment, uint32 *argv, uint32 argc,
  709. uint32 *ret);
  710. bool
  711. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  712. const WASMType *func_type, const char *signature,
  713. void *attachment, uint32 *argv, uint32 argc,
  714. uint32 *ret);
  715. void
  716. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  717. void
  718. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  719. void
  720. wasm_runtime_dump_module_inst_mem_consumption(
  721. const WASMModuleInstanceCommon *module_inst);
  722. void
  723. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  724. bool
  725. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  726. const WASMExport *export_, WASMType **out);
  727. bool
  728. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  729. const WASMExport *export_,
  730. uint8 *out_val_type, bool *out_mutability);
  731. bool
  732. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  733. const WASMExport *export_,
  734. uint32 *out_min_page, uint32 *out_max_page);
  735. bool
  736. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  737. const WASMExport *export_,
  738. uint8 *out_elem_type, uint32 *out_min_size,
  739. uint32 *out_max_size);
  740. bool
  741. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  742. void *func_ptr, WASMType *func_type,
  743. uint32 argc, uint32 *argv, bool with_env,
  744. void *wasm_c_api_env);
  745. void
  746. wasm_runtime_show_app_heap_corrupted_prompt();
  747. #ifdef __cplusplus
  748. }
  749. #endif
  750. #endif /* end of _WASM_COMMON_H */