wasm_runtime_common.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. /* Internal use for setting default running mode */
  25. #define Mode_Default 0
  26. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  27. #define PUT_I64_TO_ADDR(addr, value) \
  28. do { \
  29. *(int64 *)(addr) = (int64)(value); \
  30. } while (0)
  31. #define PUT_F64_TO_ADDR(addr, value) \
  32. do { \
  33. *(float64 *)(addr) = (float64)(value); \
  34. } while (0)
  35. #define GET_I64_FROM_ADDR(addr) (*(int64 *)(addr))
  36. #define GET_F64_FROM_ADDR(addr) (*(float64 *)(addr))
  37. /* For STORE opcodes */
  38. #define STORE_I64 PUT_I64_TO_ADDR
  39. #define STORE_U32(addr, value) \
  40. do { \
  41. *(uint32 *)(addr) = (uint32)(value); \
  42. } while (0)
  43. #define STORE_U16(addr, value) \
  44. do { \
  45. *(uint16 *)(addr) = (uint16)(value); \
  46. } while (0)
  47. /* For LOAD opcodes */
  48. #define LOAD_I64(addr) (*(int64 *)(addr))
  49. #define LOAD_F64(addr) (*(float64 *)(addr))
  50. #define LOAD_I32(addr) (*(int32 *)(addr))
  51. #define LOAD_U32(addr) (*(uint32 *)(addr))
  52. #define LOAD_I16(addr) (*(int16 *)(addr))
  53. #define LOAD_U16(addr) (*(uint16 *)(addr))
  54. #define STORE_PTR(addr, ptr) \
  55. do { \
  56. *(void **)addr = (void *)ptr; \
  57. } while (0)
  58. #else /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
  59. #define PUT_I64_TO_ADDR(addr, value) \
  60. do { \
  61. uint32 *addr_u32 = (uint32 *)(addr); \
  62. union { \
  63. int64 val; \
  64. uint32 parts[2]; \
  65. } u; \
  66. u.val = (int64)(value); \
  67. addr_u32[0] = u.parts[0]; \
  68. addr_u32[1] = u.parts[1]; \
  69. } while (0)
  70. #define PUT_F64_TO_ADDR(addr, value) \
  71. do { \
  72. uint32 *addr_u32 = (uint32 *)(addr); \
  73. union { \
  74. float64 val; \
  75. uint32 parts[2]; \
  76. } u; \
  77. u.val = (value); \
  78. addr_u32[0] = u.parts[0]; \
  79. addr_u32[1] = u.parts[1]; \
  80. } while (0)
  81. static inline int64
  82. GET_I64_FROM_ADDR(uint32 *addr)
  83. {
  84. union {
  85. int64 val;
  86. uint32 parts[2];
  87. } u;
  88. u.parts[0] = addr[0];
  89. u.parts[1] = addr[1];
  90. return u.val;
  91. }
  92. static inline float64
  93. GET_F64_FROM_ADDR(uint32 *addr)
  94. {
  95. union {
  96. float64 val;
  97. uint32 parts[2];
  98. } u;
  99. u.parts[0] = addr[0];
  100. u.parts[1] = addr[1];
  101. return u.val;
  102. }
  103. /* For STORE opcodes */
  104. #define STORE_I64(addr, value) \
  105. do { \
  106. uintptr_t addr_ = (uintptr_t)(addr); \
  107. union { \
  108. int64 val; \
  109. uint32 u32[2]; \
  110. uint16 u16[4]; \
  111. uint8 u8[8]; \
  112. } u; \
  113. if ((addr_ & (uintptr_t)7) == 0) \
  114. *(int64 *)(addr) = (int64)(value); \
  115. else { \
  116. u.val = (int64)(value); \
  117. if ((addr_ & (uintptr_t)3) == 0) { \
  118. ((uint32 *)(addr))[0] = u.u32[0]; \
  119. ((uint32 *)(addr))[1] = u.u32[1]; \
  120. } \
  121. else if ((addr_ & (uintptr_t)1) == 0) { \
  122. ((uint16 *)(addr))[0] = u.u16[0]; \
  123. ((uint16 *)(addr))[1] = u.u16[1]; \
  124. ((uint16 *)(addr))[2] = u.u16[2]; \
  125. ((uint16 *)(addr))[3] = u.u16[3]; \
  126. } \
  127. else { \
  128. int32 t; \
  129. for (t = 0; t < 8; t++) \
  130. ((uint8 *)(addr))[t] = u.u8[t]; \
  131. } \
  132. } \
  133. } while (0)
  134. #define STORE_U32(addr, value) \
  135. do { \
  136. uintptr_t addr_ = (uintptr_t)(addr); \
  137. union { \
  138. uint32 val; \
  139. uint16 u16[2]; \
  140. uint8 u8[4]; \
  141. } u; \
  142. if ((addr_ & (uintptr_t)3) == 0) \
  143. *(uint32 *)(addr) = (uint32)(value); \
  144. else { \
  145. u.val = (uint32)(value); \
  146. if ((addr_ & (uintptr_t)1) == 0) { \
  147. ((uint16 *)(addr))[0] = u.u16[0]; \
  148. ((uint16 *)(addr))[1] = u.u16[1]; \
  149. } \
  150. else { \
  151. ((uint8 *)(addr))[0] = u.u8[0]; \
  152. ((uint8 *)(addr))[1] = u.u8[1]; \
  153. ((uint8 *)(addr))[2] = u.u8[2]; \
  154. ((uint8 *)(addr))[3] = u.u8[3]; \
  155. } \
  156. } \
  157. } while (0)
  158. #define STORE_U16(addr, value) \
  159. do { \
  160. union { \
  161. uint16 val; \
  162. uint8 u8[2]; \
  163. } u; \
  164. u.val = (uint16)(value); \
  165. ((uint8 *)(addr))[0] = u.u8[0]; \
  166. ((uint8 *)(addr))[1] = u.u8[1]; \
  167. } while (0)
  168. /* For LOAD opcodes */
  169. static inline int64
  170. LOAD_I64(void *addr)
  171. {
  172. uintptr_t addr1 = (uintptr_t)addr;
  173. union {
  174. int64 val;
  175. uint32 u32[2];
  176. uint16 u16[4];
  177. uint8 u8[8];
  178. } u;
  179. if ((addr1 & (uintptr_t)7) == 0)
  180. return *(int64 *)addr;
  181. if ((addr1 & (uintptr_t)3) == 0) {
  182. u.u32[0] = ((uint32 *)addr)[0];
  183. u.u32[1] = ((uint32 *)addr)[1];
  184. }
  185. else if ((addr1 & (uintptr_t)1) == 0) {
  186. u.u16[0] = ((uint16 *)addr)[0];
  187. u.u16[1] = ((uint16 *)addr)[1];
  188. u.u16[2] = ((uint16 *)addr)[2];
  189. u.u16[3] = ((uint16 *)addr)[3];
  190. }
  191. else {
  192. int32 t;
  193. for (t = 0; t < 8; t++)
  194. u.u8[t] = ((uint8 *)addr)[t];
  195. }
  196. return u.val;
  197. }
  198. static inline float64
  199. LOAD_F64(void *addr)
  200. {
  201. uintptr_t addr1 = (uintptr_t)addr;
  202. union {
  203. float64 val;
  204. uint32 u32[2];
  205. uint16 u16[4];
  206. uint8 u8[8];
  207. } u;
  208. if ((addr1 & (uintptr_t)7) == 0)
  209. return *(float64 *)addr;
  210. if ((addr1 & (uintptr_t)3) == 0) {
  211. u.u32[0] = ((uint32 *)addr)[0];
  212. u.u32[1] = ((uint32 *)addr)[1];
  213. }
  214. else if ((addr1 & (uintptr_t)1) == 0) {
  215. u.u16[0] = ((uint16 *)addr)[0];
  216. u.u16[1] = ((uint16 *)addr)[1];
  217. u.u16[2] = ((uint16 *)addr)[2];
  218. u.u16[3] = ((uint16 *)addr)[3];
  219. }
  220. else {
  221. int32 t;
  222. for (t = 0; t < 8; t++)
  223. u.u8[t] = ((uint8 *)addr)[t];
  224. }
  225. return u.val;
  226. }
  227. static inline int32
  228. LOAD_I32(void *addr)
  229. {
  230. uintptr_t addr1 = (uintptr_t)addr;
  231. union {
  232. int32 val;
  233. uint16 u16[2];
  234. uint8 u8[4];
  235. } u;
  236. if ((addr1 & (uintptr_t)3) == 0)
  237. return *(int32 *)addr;
  238. if ((addr1 & (uintptr_t)1) == 0) {
  239. u.u16[0] = ((uint16 *)addr)[0];
  240. u.u16[1] = ((uint16 *)addr)[1];
  241. }
  242. else {
  243. u.u8[0] = ((uint8 *)addr)[0];
  244. u.u8[1] = ((uint8 *)addr)[1];
  245. u.u8[2] = ((uint8 *)addr)[2];
  246. u.u8[3] = ((uint8 *)addr)[3];
  247. }
  248. return u.val;
  249. }
  250. static inline int16
  251. LOAD_I16(void *addr)
  252. {
  253. uintptr_t addr1 = (uintptr_t)addr;
  254. union {
  255. int16 val;
  256. uint8 u8[2];
  257. } u;
  258. if ((addr1 & (uintptr_t)1)) {
  259. u.u8[0] = ((uint8 *)addr)[0];
  260. u.u8[1] = ((uint8 *)addr)[1];
  261. return u.val;
  262. }
  263. return *(int16 *)addr;
  264. }
  265. #define LOAD_U32(addr) ((uint32)LOAD_I32(addr))
  266. #define LOAD_U16(addr) ((uint16)LOAD_I16(addr))
  267. #if UINTPTR_MAX == UINT32_MAX
  268. #define STORE_PTR(addr, ptr) STORE_U32(addr, (uintptr_t)ptr)
  269. #elif UINTPTR_MAX == UINT64_MAX
  270. #define STORE_PTR(addr, ptr) STORE_I64(addr, (uintptr_t)ptr)
  271. #endif
  272. #endif /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
  273. typedef struct WASMModuleCommon {
  274. /* Module type, for module loaded from WASM bytecode binary,
  275. this field is Wasm_Module_Bytecode, and this structure should
  276. be treated as WASMModule structure;
  277. for module loaded from AOT binary, this field is
  278. Wasm_Module_AoT, and this structure should be treated as
  279. AOTModule structure. */
  280. uint32 module_type;
  281. /* The following uint8[1] member is a dummy just to indicate
  282. some module_type dependent members follow.
  283. Typically it should be accessed by casting to the corresponding
  284. actual module_type dependent structure, not via this member. */
  285. uint8 module_data[1];
  286. } WASMModuleCommon;
  287. typedef struct WASMModuleInstanceCommon {
  288. /* Module instance type, for module instance loaded from WASM
  289. bytecode binary, this field is Wasm_Module_Bytecode, and this
  290. structure should be treated as WASMModuleInstance structure;
  291. for module instance loaded from AOT binary, this field is
  292. Wasm_Module_AoT, and this structure should be treated as
  293. AOTModuleInstance structure. */
  294. uint32 module_type;
  295. /* The following uint8[1] member is a dummy just to indicate
  296. some module_type dependent members follow.
  297. Typically it should be accessed by casting to the corresponding
  298. actual module_type dependent structure, not via this member. */
  299. uint8 module_inst_data[1];
  300. } WASMModuleInstanceCommon;
  301. typedef struct WASMModuleMemConsumption {
  302. uint32 total_size;
  303. uint32 module_struct_size;
  304. uint32 types_size;
  305. uint32 imports_size;
  306. uint32 functions_size;
  307. uint32 tables_size;
  308. uint32 memories_size;
  309. uint32 globals_size;
  310. uint32 exports_size;
  311. uint32 table_segs_size;
  312. uint32 data_segs_size;
  313. uint32 const_strs_size;
  314. #if WASM_ENABLE_AOT != 0
  315. uint32 aot_code_size;
  316. #endif
  317. } WASMModuleMemConsumption;
  318. typedef struct WASMModuleInstMemConsumption {
  319. uint32 total_size;
  320. uint32 module_inst_struct_size;
  321. uint32 memories_size;
  322. uint32 app_heap_size;
  323. uint32 tables_size;
  324. uint32 globals_size;
  325. uint32 functions_size;
  326. uint32 exports_size;
  327. } WASMModuleInstMemConsumption;
  328. #if WASM_ENABLE_LIBC_WASI != 0
  329. #if WASM_ENABLE_UVWASI == 0
  330. typedef struct WASIContext {
  331. struct fd_table *curfds;
  332. struct fd_prestats *prestats;
  333. struct argv_environ_values *argv_environ;
  334. struct addr_pool *addr_pool;
  335. char *ns_lookup_buf;
  336. char **ns_lookup_list;
  337. char *argv_buf;
  338. char **argv_list;
  339. char *env_buf;
  340. char **env_list;
  341. uint32_t exit_code;
  342. } WASIContext;
  343. #else
  344. typedef struct WASIContext {
  345. uvwasi_t uvwasi;
  346. uint32_t exit_code;
  347. } WASIContext;
  348. #endif
  349. #endif
  350. #if WASM_ENABLE_MULTI_MODULE != 0
  351. typedef struct WASMRegisteredModule {
  352. bh_list_link l;
  353. /* point to a string pool */
  354. const char *module_name;
  355. WASMModuleCommon *module;
  356. /* to store the original module file buffer address */
  357. uint8 *orig_file_buf;
  358. uint32 orig_file_buf_size;
  359. } WASMRegisteredModule;
  360. #endif
  361. typedef struct WASMMemoryInstanceCommon {
  362. uint32 module_type;
  363. /* The following uint8[1] member is a dummy just to indicate
  364. some module_type dependent members follow.
  365. Typically it should be accessed by casting to the corresponding
  366. actual module_type dependent structure, not via this member. */
  367. uint8 memory_inst_data[1];
  368. } WASMMemoryInstanceCommon;
  369. typedef package_type_t PackageType;
  370. typedef wasm_section_t WASMSection, AOTSection;
  371. typedef struct wasm_frame_t {
  372. /* wasm_instance_t */
  373. void *instance;
  374. uint32 module_offset;
  375. uint32 func_index;
  376. uint32 func_offset;
  377. const char *func_name_wp;
  378. } WASMCApiFrame;
  379. #ifdef WASM_ENABLE_JIT
  380. typedef struct LLVMJITOptions {
  381. uint32 opt_level;
  382. uint32 size_level;
  383. } LLVMJITOptions;
  384. #endif
  385. #ifdef OS_ENABLE_HW_BOUND_CHECK
  386. /* Signal info passing to interp/aot signal handler */
  387. typedef struct WASMSignalInfo {
  388. WASMExecEnv *exec_env_tls;
  389. #ifndef BH_PLATFORM_WINDOWS
  390. void *sig_addr;
  391. #else
  392. EXCEPTION_POINTERS *exce_info;
  393. #endif
  394. } WASMSignalInfo;
  395. /* Set exec_env of thread local storage */
  396. void
  397. wasm_runtime_set_exec_env_tls(WASMExecEnv *exec_env);
  398. /* Get exec_env of thread local storage */
  399. WASMExecEnv *
  400. wasm_runtime_get_exec_env_tls(void);
  401. #endif
  402. /* See wasm_export.h for description */
  403. WASM_RUNTIME_API_EXTERN bool
  404. wasm_runtime_init(void);
  405. /* Internal API */
  406. RunningMode
  407. wasm_runtime_get_default_running_mode(void);
  408. #if WASM_ENABLE_JIT != 0
  409. /* Internal API */
  410. LLVMJITOptions
  411. wasm_runtime_get_llvm_jit_options(void);
  412. #endif
  413. /* See wasm_export.h for description */
  414. WASM_RUNTIME_API_EXTERN bool
  415. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  416. /* See wasm_export.h for description */
  417. WASM_RUNTIME_API_EXTERN bool
  418. wasm_runtime_is_running_mode_supported(RunningMode running_mode);
  419. /* See wasm_export.h for description */
  420. WASM_RUNTIME_API_EXTERN bool
  421. wasm_runtime_set_default_running_mode(RunningMode running_mode);
  422. /* See wasm_export.h for description */
  423. WASM_RUNTIME_API_EXTERN void
  424. wasm_runtime_destroy(void);
  425. /* See wasm_export.h for description */
  426. WASM_RUNTIME_API_EXTERN PackageType
  427. get_package_type(const uint8 *buf, uint32 size);
  428. /* See wasm_export.h for description */
  429. WASM_RUNTIME_API_EXTERN bool
  430. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size);
  431. /* See wasm_export.h for description */
  432. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  433. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  434. uint32 error_buf_size);
  435. /* See wasm_export.h for description */
  436. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  437. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  438. char *error_buf, uint32 error_buf_size);
  439. /* See wasm_export.h for description */
  440. WASM_RUNTIME_API_EXTERN void
  441. wasm_runtime_unload(WASMModuleCommon *module);
  442. /* Internal API */
  443. WASMModuleInstanceCommon *
  444. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  445. WASMExecEnv *exec_env_main, uint32 stack_size,
  446. uint32 heap_size, char *error_buf,
  447. uint32 error_buf_size);
  448. /* Internal API */
  449. void
  450. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  451. bool is_sub_inst);
  452. /* See wasm_export.h for description */
  453. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  454. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 default_stack_size,
  455. uint32 host_managed_heap_size, char *error_buf,
  456. uint32 error_buf_size);
  457. /* See wasm_export.h for description */
  458. WASM_RUNTIME_API_EXTERN bool
  459. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  460. RunningMode running_mode);
  461. /* See wasm_export.h for description */
  462. WASM_RUNTIME_API_EXTERN RunningMode
  463. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
  464. /* See wasm_export.h for description */
  465. WASM_RUNTIME_API_EXTERN void
  466. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  467. /* See wasm_export.h for description */
  468. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  469. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst);
  470. /* See wasm_export.h for description */
  471. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  472. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  473. const char *name, const char *signature);
  474. /* Internal API */
  475. WASMType *
  476. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  477. uint32 module_type);
  478. /* See wasm_export.h for description */
  479. WASM_RUNTIME_API_EXTERN uint32
  480. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  481. WASMModuleInstanceCommon *const module_inst);
  482. /* See wasm_export.h for description */
  483. WASM_RUNTIME_API_EXTERN uint32
  484. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  485. WASMModuleInstanceCommon *const module_inst);
  486. /* See wasm_export.h for description */
  487. WASM_RUNTIME_API_EXTERN void
  488. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  489. WASMModuleInstanceCommon *const module_inst,
  490. wasm_valkind_t *param_types);
  491. /* See wasm_export.h for description */
  492. WASM_RUNTIME_API_EXTERN void
  493. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  494. WASMModuleInstanceCommon *const module_inst,
  495. wasm_valkind_t *result_types);
  496. /* See wasm_export.h for description */
  497. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  498. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  499. uint32 stack_size);
  500. /* See wasm_export.h for description */
  501. WASM_RUNTIME_API_EXTERN void
  502. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  503. /* See wasm_export.h for description */
  504. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  505. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  506. /* See wasm_export.h for description */
  507. WASM_RUNTIME_API_EXTERN void
  508. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  509. WASMModuleInstanceCommon *const module_inst);
  510. /* See wasm_export.h for description */
  511. WASM_RUNTIME_API_EXTERN void *
  512. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  513. /* See wasm_export.h for description */
  514. WASM_RUNTIME_API_EXTERN void
  515. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  516. /* See wasm_export.h for description */
  517. WASM_RUNTIME_API_EXTERN void *
  518. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  519. #ifdef OS_ENABLE_HW_BOUND_CHECK
  520. /* Access exception check guard page to trigger the signal handler */
  521. void
  522. wasm_runtime_access_exce_check_guard_page();
  523. #endif
  524. /* See wasm_export.h for description */
  525. WASM_RUNTIME_API_EXTERN bool
  526. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  527. WASMFunctionInstanceCommon *function, uint32 argc,
  528. uint32 argv[]);
  529. WASM_RUNTIME_API_EXTERN bool
  530. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  531. WASMFunctionInstanceCommon *function,
  532. uint32 num_results, wasm_val_t *results,
  533. uint32 num_args, wasm_val_t *args);
  534. WASM_RUNTIME_API_EXTERN bool
  535. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  536. WASMFunctionInstanceCommon *function,
  537. uint32 num_results, wasm_val_t *results,
  538. uint32 num_args, ...);
  539. /* See wasm_export.h for description */
  540. WASM_RUNTIME_API_EXTERN bool
  541. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  542. uint32 argc, uint32 argv[]);
  543. #if WASM_ENABLE_DEBUG_INTERP != 0
  544. /* See wasm_export.h for description */
  545. WASM_RUNTIME_API_EXTERN uint32
  546. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
  547. int32_t port);
  548. /* See wasm_export.h for description */
  549. WASM_RUNTIME_API_EXTERN uint32
  550. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
  551. #endif
  552. bool
  553. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  554. /* See wasm_export.h for description */
  555. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  556. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  557. /* See wasm_export.h for description */
  558. WASM_RUNTIME_API_EXTERN bool
  559. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  560. char *argv[]);
  561. /* See wasm_export.h for description */
  562. WASM_RUNTIME_API_EXTERN bool
  563. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  564. const char *name, int32 argc, char *argv[]);
  565. /* See wasm_export.h for description */
  566. WASM_RUNTIME_API_EXTERN void
  567. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  568. const char *exception);
  569. /* See wasm_export.h for description */
  570. WASM_RUNTIME_API_EXTERN const char *
  571. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  572. /* See wasm_export.h for description */
  573. WASM_RUNTIME_API_EXTERN void
  574. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  575. /* Internal API */
  576. void
  577. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  578. void *custom_data);
  579. /* See wasm_export.h for description */
  580. WASM_RUNTIME_API_EXTERN void
  581. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  582. void *custom_data);
  583. /* See wasm_export.h for description */
  584. WASM_RUNTIME_API_EXTERN void *
  585. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  586. /* Internal API */
  587. uint32
  588. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  589. WASMExecEnv *exec_env, uint32 size,
  590. void **p_native_addr);
  591. /* Internal API */
  592. uint32
  593. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  594. WASMExecEnv *exec_env, uint32 ptr,
  595. uint32 size, void **p_native_addr);
  596. /* Internal API */
  597. void
  598. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  599. WASMExecEnv *exec_env, uint32 ptr);
  600. /* See wasm_export.h for description */
  601. WASM_RUNTIME_API_EXTERN uint32
  602. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  603. void **p_native_addr);
  604. /* See wasm_export.h for description */
  605. WASM_RUNTIME_API_EXTERN void
  606. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  607. /* See wasm_export.h for description */
  608. WASM_RUNTIME_API_EXTERN uint32
  609. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  610. const char *src, uint32 size);
  611. /* See wasm_export.h for description */
  612. WASM_RUNTIME_API_EXTERN bool
  613. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  614. uint32 app_offset, uint32 size);
  615. /* See wasm_export.h for description */
  616. WASM_RUNTIME_API_EXTERN bool
  617. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  618. uint32 app_str_offset);
  619. /* See wasm_export.h for description */
  620. WASM_RUNTIME_API_EXTERN bool
  621. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  622. void *native_ptr, uint32 size);
  623. /* See wasm_export.h for description */
  624. WASM_RUNTIME_API_EXTERN void *
  625. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  626. uint32 app_offset);
  627. /* See wasm_export.h for description */
  628. WASM_RUNTIME_API_EXTERN uint32
  629. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  630. void *native_ptr);
  631. /* See wasm_export.h for description */
  632. WASM_RUNTIME_API_EXTERN bool
  633. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  634. uint32 app_offset, uint32 *p_app_start_offset,
  635. uint32 *p_app_end_offset);
  636. /* See wasm_export.h for description */
  637. WASM_RUNTIME_API_EXTERN bool
  638. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  639. uint8 *native_ptr,
  640. uint8 **p_native_start_addr,
  641. uint8 **p_native_end_addr);
  642. /* See wasm_export.h for description */
  643. WASM_RUNTIME_API_EXTERN const uint8 *
  644. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  645. const char *name, uint32 *len);
  646. #if WASM_ENABLE_MULTI_MODULE != 0
  647. WASM_RUNTIME_API_EXTERN void
  648. wasm_runtime_set_module_reader(const module_reader reader,
  649. const module_destroyer destroyer);
  650. module_reader
  651. wasm_runtime_get_module_reader();
  652. module_destroyer
  653. wasm_runtime_get_module_destroyer();
  654. bool
  655. wasm_runtime_register_module_internal(const char *module_name,
  656. WASMModuleCommon *module,
  657. uint8 *orig_file_buf,
  658. uint32 orig_file_buf_size,
  659. char *error_buf, uint32 error_buf_size);
  660. void
  661. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  662. bool
  663. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  664. uint32 error_buf_size);
  665. void
  666. wasm_runtime_delete_loading_module(const char *module_name);
  667. bool
  668. wasm_runtime_is_loading_module(const char *module_name);
  669. void
  670. wasm_runtime_destroy_loading_module_list();
  671. #endif /* WASM_ENALBE_MULTI_MODULE */
  672. bool
  673. wasm_runtime_is_built_in_module(const char *module_name);
  674. #if WASM_ENABLE_THREAD_MGR != 0
  675. bool
  676. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  677. uint32 *size);
  678. bool
  679. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  680. uint32 size);
  681. #endif
  682. #if WASM_ENABLE_LIBC_WASI != 0
  683. WASM_RUNTIME_API_EXTERN void
  684. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  685. uint32 dir_count, const char *map_dir_list[],
  686. uint32 map_dir_count, const char *env_list[],
  687. uint32 env_count, char *argv[], int argc,
  688. int stdinfd, int stdoutfd, int stderrfd);
  689. /* See wasm_export.h for description */
  690. WASM_RUNTIME_API_EXTERN void
  691. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  692. uint32 dir_count, const char *map_dir_list[],
  693. uint32 map_dir_count, const char *env_list[],
  694. uint32 env_count, char *argv[], int argc);
  695. /* See wasm_export.h for description */
  696. WASM_RUNTIME_API_EXTERN bool
  697. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  698. /* See wasm_export.h for description */
  699. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  700. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  701. /* See wasm_export.h for description */
  702. WASM_RUNTIME_API_EXTERN uint32_t
  703. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst);
  704. bool
  705. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  706. const char *dir_list[], uint32 dir_count,
  707. const char *map_dir_list[], uint32 map_dir_count,
  708. const char *env[], uint32 env_count,
  709. const char *addr_pool[], uint32 addr_pool_size,
  710. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  711. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  712. int stderrfd, char *error_buf, uint32 error_buf_size);
  713. void
  714. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  715. void
  716. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  717. WASIContext *wasi_ctx);
  718. WASIContext *
  719. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  720. WASM_RUNTIME_API_EXTERN void
  721. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  722. uint32 addr_pool_size);
  723. WASM_RUNTIME_API_EXTERN void
  724. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  725. const char *ns_lookup_pool[],
  726. uint32 ns_lookup_pool_size);
  727. #endif /* end of WASM_ENABLE_LIBC_WASI */
  728. #if WASM_ENABLE_REF_TYPES != 0
  729. /* See wasm_export.h for description */
  730. WASM_RUNTIME_API_EXTERN bool
  731. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  732. uint32 *p_externref_idx);
  733. /* See wasm_export.h for description */
  734. WASM_RUNTIME_API_EXTERN bool
  735. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  736. /* See wasm_export.h for description */
  737. WASM_RUNTIME_API_EXTERN bool
  738. wasm_externref_retain(uint32 externref_idx);
  739. /**
  740. * Reclaim the externref objects/indexes which are not used by
  741. * module instance
  742. */
  743. void
  744. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  745. /**
  746. * Cleanup the externref objects/indexes of the module instance
  747. */
  748. void
  749. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  750. #endif /* end of WASM_ENABLE_REF_TYPES */
  751. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  752. /**
  753. * @brief Internal implementation for dumping or printing callstack line
  754. *
  755. * @note if dump_or_print is true, then print to stdout directly;
  756. * if dump_or_print is false, but *buf is NULL, then return the length of the
  757. * line;
  758. * if dump_or_print is false, and *buf is not NULL, then dump content to
  759. * the memory pointed by *buf, and adjust *buf and *len according to actual
  760. * bytes dumped, and return the actual dumped length
  761. *
  762. * @param line_buf current line to dump or print
  763. * @param dump_or_print whether to print to stdout or dump to buf
  764. * @param buf [INOUT] pointer to the buffer
  765. * @param len [INOUT] pointer to remaining length
  766. * @return bytes printed to stdout or dumped to buf
  767. */
  768. uint32
  769. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  770. char **buf, uint32 *len);
  771. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 */
  772. /* Get module of the current exec_env */
  773. WASMModuleCommon *
  774. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  775. /* See wasm_export.h for description */
  776. WASM_RUNTIME_API_EXTERN bool
  777. wasm_runtime_register_natives(const char *module_name,
  778. NativeSymbol *native_symbols,
  779. uint32 n_native_symbols);
  780. /* See wasm_export.h for description */
  781. WASM_RUNTIME_API_EXTERN bool
  782. wasm_runtime_register_natives_raw(const char *module_name,
  783. NativeSymbol *native_symbols,
  784. uint32 n_native_symbols);
  785. /* See wasm_export.h for description */
  786. WASM_RUNTIME_API_EXTERN bool
  787. wasm_runtime_unregister_natives(const char *module_name,
  788. NativeSymbol *native_symbols);
  789. bool
  790. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  791. const WASMType *func_type, const char *signature,
  792. void *attachment, uint32 *argv, uint32 argc,
  793. uint32 *ret);
  794. bool
  795. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  796. const WASMType *func_type, const char *signature,
  797. void *attachment, uint32 *argv, uint32 argc,
  798. uint32 *ret);
  799. void
  800. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  801. void
  802. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  803. void
  804. wasm_runtime_dump_module_inst_mem_consumption(
  805. const WASMModuleInstanceCommon *module_inst);
  806. void
  807. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  808. bool
  809. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  810. uint32 table_idx, uint8 *out_elem_type,
  811. uint32 *out_min_size, uint32 *out_max_size);
  812. bool
  813. wasm_runtime_get_table_inst_elem_type(
  814. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  815. uint8 *out_elem_type, uint32 *out_min_size, uint32 *out_max_size);
  816. bool
  817. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  818. const WASMExport *export_, WASMType **out);
  819. bool
  820. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  821. const WASMExport *export_,
  822. uint8 *out_val_type, bool *out_mutability);
  823. bool
  824. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  825. const WASMExport *export_,
  826. uint32 *out_min_page, uint32 *out_max_page);
  827. bool
  828. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  829. const WASMExport *export_,
  830. uint8 *out_elem_type, uint32 *out_min_size,
  831. uint32 *out_max_size);
  832. bool
  833. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  834. void *func_ptr, WASMType *func_type,
  835. uint32 argc, uint32 *argv, bool with_env,
  836. void *wasm_c_api_env);
  837. void
  838. wasm_runtime_show_app_heap_corrupted_prompt();
  839. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  840. void
  841. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list);
  842. #endif
  843. WASM_RUNTIME_API_EXTERN bool
  844. wasm_runtime_is_import_func_linked(const char *module_name,
  845. const char *func_name);
  846. WASM_RUNTIME_API_EXTERN bool
  847. wasm_runtime_is_import_global_linked(const char *module_name,
  848. const char *global_name);
  849. #ifdef __cplusplus
  850. }
  851. #endif
  852. #endif /* end of _WASM_COMMON_H */