wasm_runtime_common.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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 *ns_lookup_buf;
  334. char **ns_lookup_list;
  335. char *argv_buf;
  336. char **argv_list;
  337. char *env_buf;
  338. char **env_list;
  339. uint32_t exit_code;
  340. } WASIContext;
  341. #else
  342. typedef struct WASIContext {
  343. uvwasi_t uvwasi;
  344. uint32_t exit_code;
  345. } WASIContext;
  346. #endif
  347. #endif
  348. #if WASM_ENABLE_MULTI_MODULE != 0
  349. typedef struct WASMRegisteredModule {
  350. bh_list_link l;
  351. /* point to a string pool */
  352. const char *module_name;
  353. WASMModuleCommon *module;
  354. /* to store the original module file buffer address */
  355. uint8 *orig_file_buf;
  356. uint32 orig_file_buf_size;
  357. } WASMRegisteredModule;
  358. #endif
  359. typedef struct WASMMemoryInstanceCommon {
  360. uint32 module_type;
  361. /* The following uint8[1] member is a dummy just to indicate
  362. some module_type dependent members follow.
  363. Typically it should be accessed by casting to the corresponding
  364. actual module_type dependent structure, not via this member. */
  365. uint8 memory_inst_data[1];
  366. } WASMMemoryInstanceCommon;
  367. typedef package_type_t PackageType;
  368. typedef wasm_section_t WASMSection, AOTSection;
  369. typedef struct wasm_frame_t {
  370. /* wasm_instance_t */
  371. void *instance;
  372. uint32 module_offset;
  373. uint32 func_index;
  374. uint32 func_offset;
  375. const char *func_name_wp;
  376. } WASMCApiFrame;
  377. #ifdef OS_ENABLE_HW_BOUND_CHECK
  378. /* Signal info passing to interp/aot signal handler */
  379. typedef struct WASMSignalInfo {
  380. WASMExecEnv *exec_env_tls;
  381. #ifndef BH_PLATFORM_WINDOWS
  382. void *sig_addr;
  383. #else
  384. EXCEPTION_POINTERS *exce_info;
  385. #endif
  386. } WASMSignalInfo;
  387. /* Set exec_env of thread local storage */
  388. void
  389. wasm_runtime_set_exec_env_tls(WASMExecEnv *exec_env);
  390. /* Get exec_env of thread local storage */
  391. WASMExecEnv *
  392. wasm_runtime_get_exec_env_tls(void);
  393. #endif
  394. /* See wasm_export.h for description */
  395. WASM_RUNTIME_API_EXTERN bool
  396. wasm_runtime_init(void);
  397. /* See wasm_export.h for description */
  398. WASM_RUNTIME_API_EXTERN bool
  399. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  400. /* See wasm_export.h for description */
  401. WASM_RUNTIME_API_EXTERN void
  402. wasm_runtime_destroy(void);
  403. /* See wasm_export.h for description */
  404. WASM_RUNTIME_API_EXTERN PackageType
  405. get_package_type(const uint8 *buf, uint32 size);
  406. /* See wasm_export.h for description */
  407. WASM_RUNTIME_API_EXTERN bool
  408. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size);
  409. /* See wasm_export.h for description */
  410. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  411. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  412. uint32 error_buf_size);
  413. /* See wasm_export.h for description */
  414. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  415. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  416. char *error_buf, uint32 error_buf_size);
  417. /* See wasm_export.h for description */
  418. WASM_RUNTIME_API_EXTERN void
  419. wasm_runtime_unload(WASMModuleCommon *module);
  420. /* Internal API */
  421. WASMModuleInstanceCommon *
  422. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  423. uint32 stack_size, uint32 heap_size,
  424. char *error_buf, uint32 error_buf_size);
  425. /* Internal API */
  426. void
  427. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  428. bool is_sub_inst);
  429. /* See wasm_export.h for description */
  430. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  431. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
  432. uint32 heap_size, char *error_buf,
  433. uint32 error_buf_size);
  434. /* See wasm_export.h for description */
  435. WASM_RUNTIME_API_EXTERN void
  436. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  437. /* See wasm_export.h for description */
  438. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  439. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst);
  440. /* See wasm_export.h for description */
  441. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  442. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  443. const char *name, const char *signature);
  444. /* Internal API */
  445. WASMType *
  446. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  447. uint32 module_type);
  448. /* See wasm_export.h for description */
  449. WASM_RUNTIME_API_EXTERN uint32
  450. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  451. WASMModuleInstanceCommon *const module_inst);
  452. /* See wasm_export.h for description */
  453. WASM_RUNTIME_API_EXTERN uint32
  454. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  455. WASMModuleInstanceCommon *const module_inst);
  456. /* See wasm_export.h for description */
  457. WASM_RUNTIME_API_EXTERN void
  458. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  459. WASMModuleInstanceCommon *const module_inst,
  460. wasm_valkind_t *param_types);
  461. /* See wasm_export.h for description */
  462. WASM_RUNTIME_API_EXTERN void
  463. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  464. WASMModuleInstanceCommon *const module_inst,
  465. wasm_valkind_t *result_types);
  466. /* See wasm_export.h for description */
  467. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  468. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  469. uint32 stack_size);
  470. /* See wasm_export.h for description */
  471. WASM_RUNTIME_API_EXTERN void
  472. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  473. /* See wasm_export.h for description */
  474. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  475. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  476. /* See wasm_export.h for description */
  477. WASM_RUNTIME_API_EXTERN void
  478. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  479. WASMModuleInstanceCommon *const module_inst);
  480. /* See wasm_export.h for description */
  481. WASM_RUNTIME_API_EXTERN void *
  482. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  483. /* See wasm_export.h for description */
  484. WASM_RUNTIME_API_EXTERN void
  485. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  486. /* See wasm_export.h for description */
  487. WASM_RUNTIME_API_EXTERN void *
  488. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  489. /* See wasm_export.h for description */
  490. WASM_RUNTIME_API_EXTERN bool
  491. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  492. WASMFunctionInstanceCommon *function, uint32 argc,
  493. uint32 argv[]);
  494. WASM_RUNTIME_API_EXTERN bool
  495. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  496. WASMFunctionInstanceCommon *function,
  497. uint32 num_results, wasm_val_t *results,
  498. uint32 num_args, wasm_val_t *args);
  499. WASM_RUNTIME_API_EXTERN bool
  500. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  501. WASMFunctionInstanceCommon *function,
  502. uint32 num_results, wasm_val_t *results,
  503. uint32 num_args, ...);
  504. #if WASM_ENABLE_DEBUG_INTERP != 0
  505. /* See wasm_export.h for description */
  506. WASM_RUNTIME_API_EXTERN uint32
  507. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
  508. int32_t port);
  509. /* See wasm_export.h for description */
  510. WASM_RUNTIME_API_EXTERN uint32
  511. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
  512. #endif
  513. /**
  514. * Call a function reference of a given WASM runtime instance with
  515. * arguments.
  516. *
  517. * @param exec_env the execution environment to call the function
  518. * which must be created from wasm_create_exec_env()
  519. * @param element_indices the function ference indicies, usually
  520. * prvovided by the caller of a registed native function
  521. * @param argc the number of arguments
  522. * @param argv the arguments. If the function method has return value,
  523. * the first (or first two in case 64-bit return value) element of
  524. * argv stores the return value of the called WASM function after this
  525. * function returns.
  526. *
  527. * @return true if success, false otherwise and exception will be thrown,
  528. * the caller can call wasm_runtime_get_exception to get exception info.
  529. */
  530. bool
  531. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
  532. uint32 argc, uint32 argv[]);
  533. bool
  534. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  535. /* See wasm_export.h for description */
  536. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  537. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  538. /* See wasm_export.h for description */
  539. WASM_RUNTIME_API_EXTERN bool
  540. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  541. char *argv[]);
  542. /* See wasm_export.h for description */
  543. WASM_RUNTIME_API_EXTERN bool
  544. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  545. const char *name, int32 argc, char *argv[]);
  546. /* See wasm_export.h for description */
  547. WASM_RUNTIME_API_EXTERN void
  548. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  549. const char *exception);
  550. /* See wasm_export.h for description */
  551. WASM_RUNTIME_API_EXTERN const char *
  552. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  553. /* See wasm_export.h for description */
  554. WASM_RUNTIME_API_EXTERN void
  555. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  556. /* Internal API */
  557. void
  558. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  559. void *custom_data);
  560. /* See wasm_export.h for description */
  561. WASM_RUNTIME_API_EXTERN void
  562. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  563. void *custom_data);
  564. /* See wasm_export.h for description */
  565. WASM_RUNTIME_API_EXTERN void *
  566. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  567. /* See wasm_export.h for description */
  568. WASM_RUNTIME_API_EXTERN uint32
  569. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  570. void **p_native_addr);
  571. /* See wasm_export.h for description */
  572. WASM_RUNTIME_API_EXTERN void
  573. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  574. /* See wasm_export.h for description */
  575. WASM_RUNTIME_API_EXTERN uint32
  576. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  577. const char *src, uint32 size);
  578. /* See wasm_export.h for description */
  579. WASM_RUNTIME_API_EXTERN bool
  580. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  581. uint32 app_offset, uint32 size);
  582. /* See wasm_export.h for description */
  583. WASM_RUNTIME_API_EXTERN bool
  584. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  585. uint32 app_str_offset);
  586. /* See wasm_export.h for description */
  587. WASM_RUNTIME_API_EXTERN bool
  588. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  589. void *native_ptr, uint32 size);
  590. /* See wasm_export.h for description */
  591. WASM_RUNTIME_API_EXTERN void *
  592. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  593. uint32 app_offset);
  594. /* See wasm_export.h for description */
  595. WASM_RUNTIME_API_EXTERN uint32
  596. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  597. void *native_ptr);
  598. /* See wasm_export.h for description */
  599. WASM_RUNTIME_API_EXTERN bool
  600. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  601. uint32 app_offset, uint32 *p_app_start_offset,
  602. uint32 *p_app_end_offset);
  603. /* See wasm_export.h for description */
  604. WASM_RUNTIME_API_EXTERN bool
  605. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  606. uint8 *native_ptr,
  607. uint8 **p_native_start_addr,
  608. uint8 **p_native_end_addr);
  609. /* See wasm_export.h for description */
  610. WASM_RUNTIME_API_EXTERN const uint8 *
  611. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  612. const char *name, uint32 *len);
  613. #if WASM_ENABLE_MULTI_MODULE != 0
  614. WASM_RUNTIME_API_EXTERN void
  615. wasm_runtime_set_module_reader(const module_reader reader,
  616. const module_destroyer destroyer);
  617. module_reader
  618. wasm_runtime_get_module_reader();
  619. module_destroyer
  620. wasm_runtime_get_module_destroyer();
  621. bool
  622. wasm_runtime_register_module_internal(const char *module_name,
  623. WASMModuleCommon *module,
  624. uint8 *orig_file_buf,
  625. uint32 orig_file_buf_size,
  626. char *error_buf, uint32 error_buf_size);
  627. void
  628. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  629. bool
  630. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  631. uint32 error_buf_size);
  632. void
  633. wasm_runtime_delete_loading_module(const char *module_name);
  634. bool
  635. wasm_runtime_is_loading_module(const char *module_name);
  636. void
  637. wasm_runtime_destroy_loading_module_list();
  638. #endif /* WASM_ENALBE_MULTI_MODULE */
  639. bool
  640. wasm_runtime_is_built_in_module(const char *module_name);
  641. #if WASM_ENABLE_THREAD_MGR != 0
  642. bool
  643. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  644. uint32 *size);
  645. bool
  646. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  647. uint32 size);
  648. #endif
  649. #if WASM_ENABLE_LIBC_WASI != 0
  650. WASM_RUNTIME_API_EXTERN void
  651. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  652. uint32 dir_count, const char *map_dir_list[],
  653. uint32 map_dir_count, const char *env_list[],
  654. uint32 env_count, char *argv[], int argc,
  655. int stdinfd, int stdoutfd, int stderrfd);
  656. /* See wasm_export.h for description */
  657. WASM_RUNTIME_API_EXTERN void
  658. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  659. uint32 dir_count, const char *map_dir_list[],
  660. uint32 map_dir_count, const char *env_list[],
  661. uint32 env_count, char *argv[], int argc);
  662. /* See wasm_export.h for description */
  663. WASM_RUNTIME_API_EXTERN bool
  664. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  665. /* See wasm_export.h for description */
  666. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  667. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  668. /* See wasm_export.h for description */
  669. WASM_RUNTIME_API_EXTERN uint32_t
  670. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst);
  671. bool
  672. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  673. const char *dir_list[], uint32 dir_count,
  674. const char *map_dir_list[], uint32 map_dir_count,
  675. const char *env[], uint32 env_count,
  676. const char *addr_pool[], uint32 addr_pool_size,
  677. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  678. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  679. int stderrfd, char *error_buf, uint32 error_buf_size);
  680. void
  681. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  682. void
  683. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  684. WASIContext *wasi_ctx);
  685. WASIContext *
  686. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  687. WASM_RUNTIME_API_EXTERN void
  688. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  689. uint32 addr_pool_size);
  690. WASM_RUNTIME_API_EXTERN void
  691. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  692. const char *ns_lookup_pool[],
  693. uint32 ns_lookup_pool_size);
  694. #endif /* end of WASM_ENABLE_LIBC_WASI */
  695. #if WASM_ENABLE_REF_TYPES != 0
  696. /* See wasm_export.h for description */
  697. WASM_RUNTIME_API_EXTERN bool
  698. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  699. uint32 *p_externref_idx);
  700. /* See wasm_export.h for description */
  701. WASM_RUNTIME_API_EXTERN bool
  702. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  703. /* See wasm_export.h for description */
  704. WASM_RUNTIME_API_EXTERN bool
  705. wasm_externref_retain(uint32 externref_idx);
  706. /**
  707. * Reclaim the externref objects/indexes which are not used by
  708. * module instance
  709. */
  710. void
  711. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  712. /**
  713. * Cleanup the externref objects/indexes of the module instance
  714. */
  715. void
  716. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  717. #endif /* end of WASM_ENABLE_REF_TYPES */
  718. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  719. /**
  720. * @brief Internal implementation for dumping or printing callstack line
  721. *
  722. * @note if dump_or_print is true, then print to stdout directly;
  723. * if dump_or_print is false, but *buf is NULL, then return the length of the
  724. * line;
  725. * if dump_or_print is false, and *buf is not NULL, then dump content to
  726. * the memory pointed by *buf, and adjust *buf and *len according to actual
  727. * bytes dumped, and return the actual dumped length
  728. *
  729. * @param line_buf current line to dump or print
  730. * @param dump_or_print whether to print to stdout or dump to buf
  731. * @param buf [INOUT] pointer to the buffer
  732. * @param len [INOUT] pointer to remaining length
  733. * @return bytes printed to stdout or dumped to buf
  734. */
  735. uint32
  736. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  737. char **buf, uint32 *len);
  738. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 */
  739. /* Get module of the current exec_env */
  740. WASMModuleCommon *
  741. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  742. /* See wasm_export.h for description */
  743. WASM_RUNTIME_API_EXTERN bool
  744. wasm_runtime_register_natives(const char *module_name,
  745. NativeSymbol *native_symbols,
  746. uint32 n_native_symbols);
  747. /* See wasm_export.h for description */
  748. WASM_RUNTIME_API_EXTERN bool
  749. wasm_runtime_register_natives_raw(const char *module_name,
  750. NativeSymbol *native_symbols,
  751. uint32 n_native_symbols);
  752. /* See wasm_export.h for description */
  753. WASM_RUNTIME_API_EXTERN bool
  754. wasm_runtime_unregister_natives(const char *module_name,
  755. NativeSymbol *native_symbols);
  756. bool
  757. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  758. const WASMType *func_type, const char *signature,
  759. void *attachment, uint32 *argv, uint32 argc,
  760. uint32 *ret);
  761. bool
  762. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  763. const WASMType *func_type, const char *signature,
  764. void *attachment, uint32 *argv, uint32 argc,
  765. uint32 *ret);
  766. void
  767. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  768. void
  769. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  770. void
  771. wasm_runtime_dump_module_inst_mem_consumption(
  772. const WASMModuleInstanceCommon *module_inst);
  773. void
  774. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  775. bool
  776. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  777. uint32 table_idx, uint8 *out_elem_type,
  778. uint32 *out_min_size, uint32 *out_max_size);
  779. bool
  780. wasm_runtime_get_table_inst_elem_type(
  781. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  782. uint8 *out_elem_type, uint32 *out_min_size, uint32 *out_max_size);
  783. bool
  784. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  785. const WASMExport *export_, WASMType **out);
  786. bool
  787. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  788. const WASMExport *export_,
  789. uint8 *out_val_type, bool *out_mutability);
  790. bool
  791. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  792. const WASMExport *export_,
  793. uint32 *out_min_page, uint32 *out_max_page);
  794. bool
  795. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  796. const WASMExport *export_,
  797. uint8 *out_elem_type, uint32 *out_min_size,
  798. uint32 *out_max_size);
  799. bool
  800. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  801. void *func_ptr, WASMType *func_type,
  802. uint32 argc, uint32 *argv, bool with_env,
  803. void *wasm_c_api_env);
  804. void
  805. wasm_runtime_show_app_heap_corrupted_prompt();
  806. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  807. void
  808. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list);
  809. #endif
  810. #ifdef __cplusplus
  811. }
  812. #endif
  813. #endif /* end of _WASM_COMMON_H */