wasm_runtime_common.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. uint32 segue_flags;
  384. } LLVMJITOptions;
  385. #endif
  386. #ifdef OS_ENABLE_HW_BOUND_CHECK
  387. /* Signal info passing to interp/aot signal handler */
  388. typedef struct WASMSignalInfo {
  389. WASMExecEnv *exec_env_tls;
  390. #ifndef BH_PLATFORM_WINDOWS
  391. void *sig_addr;
  392. #else
  393. EXCEPTION_POINTERS *exce_info;
  394. #endif
  395. } WASMSignalInfo;
  396. /* Set exec_env of thread local storage */
  397. void
  398. wasm_runtime_set_exec_env_tls(WASMExecEnv *exec_env);
  399. /* Get exec_env of thread local storage */
  400. WASMExecEnv *
  401. wasm_runtime_get_exec_env_tls(void);
  402. #endif
  403. /* See wasm_export.h for description */
  404. WASM_RUNTIME_API_EXTERN bool
  405. wasm_runtime_init(void);
  406. /* Internal API */
  407. RunningMode
  408. wasm_runtime_get_default_running_mode(void);
  409. #if WASM_ENABLE_JIT != 0
  410. /* Internal API */
  411. LLVMJITOptions
  412. wasm_runtime_get_llvm_jit_options(void);
  413. #endif
  414. /* See wasm_export.h for description */
  415. WASM_RUNTIME_API_EXTERN bool
  416. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  417. /* See wasm_export.h for description */
  418. WASM_RUNTIME_API_EXTERN bool
  419. wasm_runtime_is_running_mode_supported(RunningMode running_mode);
  420. /* See wasm_export.h for description */
  421. WASM_RUNTIME_API_EXTERN bool
  422. wasm_runtime_set_default_running_mode(RunningMode running_mode);
  423. /* See wasm_export.h for description */
  424. WASM_RUNTIME_API_EXTERN void
  425. wasm_runtime_destroy(void);
  426. /* See wasm_export.h for description */
  427. WASM_RUNTIME_API_EXTERN PackageType
  428. get_package_type(const uint8 *buf, uint32 size);
  429. /* See wasm_export.h for description */
  430. WASM_RUNTIME_API_EXTERN bool
  431. wasm_runtime_is_xip_file(const uint8 *buf, uint32 size);
  432. /* See wasm_export.h for description */
  433. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  434. wasm_runtime_load(uint8 *buf, uint32 size, char *error_buf,
  435. uint32 error_buf_size);
  436. /* See wasm_export.h for description */
  437. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  438. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  439. char *error_buf, uint32 error_buf_size);
  440. /* See wasm_export.h for description */
  441. WASM_RUNTIME_API_EXTERN void
  442. wasm_runtime_unload(WASMModuleCommon *module);
  443. /* Internal API */
  444. WASMModuleInstanceCommon *
  445. wasm_runtime_instantiate_internal(WASMModuleCommon *module,
  446. WASMModuleInstanceCommon *parent,
  447. WASMExecEnv *exec_env_main, uint32 stack_size,
  448. uint32 heap_size, char *error_buf,
  449. uint32 error_buf_size);
  450. /* Internal API */
  451. void
  452. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  453. bool is_sub_inst);
  454. /* See wasm_export.h for description */
  455. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  456. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 default_stack_size,
  457. uint32 host_managed_heap_size, char *error_buf,
  458. uint32 error_buf_size);
  459. /* See wasm_export.h for description */
  460. WASM_RUNTIME_API_EXTERN bool
  461. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  462. RunningMode running_mode);
  463. /* See wasm_export.h for description */
  464. WASM_RUNTIME_API_EXTERN RunningMode
  465. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
  466. /* See wasm_export.h for description */
  467. WASM_RUNTIME_API_EXTERN void
  468. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  469. /* See wasm_export.h for description */
  470. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  471. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst);
  472. /* See wasm_export.h for description */
  473. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  474. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  475. const char *name, const char *signature);
  476. /* Internal API */
  477. WASMType *
  478. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  479. uint32 module_type);
  480. /* See wasm_export.h for description */
  481. WASM_RUNTIME_API_EXTERN uint32
  482. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  483. WASMModuleInstanceCommon *const module_inst);
  484. /* See wasm_export.h for description */
  485. WASM_RUNTIME_API_EXTERN uint32
  486. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  487. WASMModuleInstanceCommon *const module_inst);
  488. /* See wasm_export.h for description */
  489. WASM_RUNTIME_API_EXTERN void
  490. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  491. WASMModuleInstanceCommon *const module_inst,
  492. wasm_valkind_t *param_types);
  493. /* See wasm_export.h for description */
  494. WASM_RUNTIME_API_EXTERN void
  495. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  496. WASMModuleInstanceCommon *const module_inst,
  497. wasm_valkind_t *result_types);
  498. /* See wasm_export.h for description */
  499. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  500. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  501. uint32 stack_size);
  502. /* See wasm_export.h for description */
  503. WASM_RUNTIME_API_EXTERN void
  504. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  505. /* See wasm_export.h for description */
  506. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  507. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  508. /* See wasm_export.h for description */
  509. WASM_RUNTIME_API_EXTERN void
  510. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  511. WASMModuleInstanceCommon *const module_inst);
  512. /* See wasm_export.h for description */
  513. WASM_RUNTIME_API_EXTERN void *
  514. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  515. /* See wasm_export.h for description */
  516. WASM_RUNTIME_API_EXTERN void
  517. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  518. /* See wasm_export.h for description */
  519. WASM_RUNTIME_API_EXTERN void *
  520. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  521. #if WASM_CONFIGUABLE_BOUNDS_CHECKS != 0
  522. /* See wasm_export.h for description */
  523. WASM_RUNTIME_API_EXTERN void
  524. wasm_runtime_set_bounds_checks(WASMModuleInstanceCommon *module_inst,
  525. bool enable);
  526. /* See wasm_export.h for description */
  527. WASM_RUNTIME_API_EXTERN bool
  528. wasm_runtime_is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst);
  529. #endif
  530. #ifdef OS_ENABLE_HW_BOUND_CHECK
  531. /* Access exception check guard page to trigger the signal handler */
  532. void
  533. wasm_runtime_access_exce_check_guard_page();
  534. #endif
  535. /* See wasm_export.h for description */
  536. WASM_RUNTIME_API_EXTERN bool
  537. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  538. WASMFunctionInstanceCommon *function, uint32 argc,
  539. uint32 argv[]);
  540. WASM_RUNTIME_API_EXTERN bool
  541. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  542. WASMFunctionInstanceCommon *function,
  543. uint32 num_results, wasm_val_t *results,
  544. uint32 num_args, wasm_val_t *args);
  545. WASM_RUNTIME_API_EXTERN bool
  546. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  547. WASMFunctionInstanceCommon *function,
  548. uint32 num_results, wasm_val_t *results,
  549. uint32 num_args, ...);
  550. /* See wasm_export.h for description */
  551. WASM_RUNTIME_API_EXTERN bool
  552. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  553. uint32 argc, uint32 argv[]);
  554. #if WASM_ENABLE_DEBUG_INTERP != 0
  555. /* See wasm_export.h for description */
  556. WASM_RUNTIME_API_EXTERN uint32
  557. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
  558. int32_t port);
  559. /* See wasm_export.h for description */
  560. WASM_RUNTIME_API_EXTERN uint32
  561. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
  562. #endif
  563. bool
  564. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  565. /* See wasm_export.h for description */
  566. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  567. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  568. /* See wasm_export.h for description */
  569. WASM_RUNTIME_API_EXTERN bool
  570. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  571. char *argv[]);
  572. /* See wasm_export.h for description */
  573. WASM_RUNTIME_API_EXTERN bool
  574. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  575. const char *name, int32 argc, char *argv[]);
  576. /* See wasm_export.h for description */
  577. WASM_RUNTIME_API_EXTERN void
  578. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  579. const char *exception);
  580. /* See wasm_export.h for description */
  581. WASM_RUNTIME_API_EXTERN const char *
  582. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  583. /* See wasm_export.h for description */
  584. WASM_RUNTIME_API_EXTERN void
  585. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  586. /* See wasm_export.h for description */
  587. WASM_RUNTIME_API_EXTERN void
  588. wasm_runtime_terminate(WASMModuleInstanceCommon *module);
  589. /* Internal API */
  590. void
  591. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  592. void *custom_data);
  593. /* See wasm_export.h for description */
  594. WASM_RUNTIME_API_EXTERN void
  595. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  596. void *custom_data);
  597. /* See wasm_export.h for description */
  598. WASM_RUNTIME_API_EXTERN void *
  599. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  600. /* Internal API */
  601. uint32
  602. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  603. WASMExecEnv *exec_env, uint32 size,
  604. void **p_native_addr);
  605. /* Internal API */
  606. uint32
  607. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  608. WASMExecEnv *exec_env, uint32 ptr,
  609. uint32 size, void **p_native_addr);
  610. /* Internal API */
  611. void
  612. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  613. WASMExecEnv *exec_env, uint32 ptr);
  614. /* See wasm_export.h for description */
  615. WASM_RUNTIME_API_EXTERN uint32
  616. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  617. void **p_native_addr);
  618. /* See wasm_export.h for description */
  619. WASM_RUNTIME_API_EXTERN void
  620. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  621. /* See wasm_export.h for description */
  622. WASM_RUNTIME_API_EXTERN uint32
  623. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  624. const char *src, uint32 size);
  625. /* See wasm_export.h for description */
  626. WASM_RUNTIME_API_EXTERN bool
  627. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  628. uint32 app_offset, uint32 size);
  629. /* See wasm_export.h for description */
  630. WASM_RUNTIME_API_EXTERN bool
  631. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  632. uint32 app_str_offset);
  633. /* See wasm_export.h for description */
  634. WASM_RUNTIME_API_EXTERN bool
  635. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  636. void *native_ptr, uint32 size);
  637. /* See wasm_export.h for description */
  638. WASM_RUNTIME_API_EXTERN void *
  639. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  640. uint32 app_offset);
  641. /* See wasm_export.h for description */
  642. WASM_RUNTIME_API_EXTERN uint32
  643. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  644. void *native_ptr);
  645. /* See wasm_export.h for description */
  646. WASM_RUNTIME_API_EXTERN bool
  647. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  648. uint32 app_offset, uint32 *p_app_start_offset,
  649. uint32 *p_app_end_offset);
  650. /* See wasm_export.h for description */
  651. WASM_RUNTIME_API_EXTERN bool
  652. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  653. uint8 *native_ptr,
  654. uint8 **p_native_start_addr,
  655. uint8 **p_native_end_addr);
  656. /* See wasm_export.h for description */
  657. WASM_RUNTIME_API_EXTERN const uint8 *
  658. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  659. const char *name, uint32 *len);
  660. #if WASM_ENABLE_MULTI_MODULE != 0
  661. WASM_RUNTIME_API_EXTERN void
  662. wasm_runtime_set_module_reader(const module_reader reader,
  663. const module_destroyer destroyer);
  664. module_reader
  665. wasm_runtime_get_module_reader();
  666. module_destroyer
  667. wasm_runtime_get_module_destroyer();
  668. bool
  669. wasm_runtime_register_module_internal(const char *module_name,
  670. WASMModuleCommon *module,
  671. uint8 *orig_file_buf,
  672. uint32 orig_file_buf_size,
  673. char *error_buf, uint32 error_buf_size);
  674. void
  675. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  676. WASMModuleCommon *
  677. wasm_runtime_find_module_registered(const char *module_name);
  678. bool
  679. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  680. uint32 error_buf_size);
  681. void
  682. wasm_runtime_delete_loading_module(const char *module_name);
  683. bool
  684. wasm_runtime_is_loading_module(const char *module_name);
  685. void
  686. wasm_runtime_destroy_loading_module_list();
  687. WASMModuleCommon *
  688. wasm_runtime_search_sub_module(const WASMModuleCommon *parent_module,
  689. const char *sub_module_name);
  690. bool
  691. wasm_runtime_register_sub_module(const WASMModuleCommon *parent_module,
  692. const char *sub_module_name,
  693. WASMModuleCommon *sub_module);
  694. WASMModuleCommon *
  695. wasm_runtime_load_depended_module(const WASMModuleCommon *parent_module,
  696. const char *sub_module_name, char *error_buf,
  697. uint32 error_buf_size);
  698. bool
  699. wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
  700. WASMModuleInstanceCommon *module_inst,
  701. uint32 stack_size, uint32 heap_size,
  702. char *error_buf, uint32 error_buf_size);
  703. void
  704. wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst);
  705. #endif
  706. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  707. WASMExport *
  708. loader_find_export(const WASMModuleCommon *module, const char *module_name,
  709. const char *field_name, uint8 export_kind, char *error_buf,
  710. uint32 error_buf_size);
  711. #endif /* WASM_ENALBE_MULTI_MODULE */
  712. bool
  713. wasm_runtime_is_built_in_module(const char *module_name);
  714. #if WASM_ENABLE_THREAD_MGR != 0
  715. bool
  716. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  717. uint32 *size);
  718. bool
  719. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  720. uint32 size);
  721. #endif
  722. #if WASM_ENABLE_LIBC_WASI != 0
  723. WASM_RUNTIME_API_EXTERN void
  724. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  725. uint32 dir_count, const char *map_dir_list[],
  726. uint32 map_dir_count, const char *env_list[],
  727. uint32 env_count, char *argv[], int argc,
  728. int stdinfd, int stdoutfd, int stderrfd);
  729. /* See wasm_export.h for description */
  730. WASM_RUNTIME_API_EXTERN void
  731. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  732. uint32 dir_count, const char *map_dir_list[],
  733. uint32 map_dir_count, const char *env_list[],
  734. uint32 env_count, char *argv[], int argc);
  735. /* See wasm_export.h for description */
  736. WASM_RUNTIME_API_EXTERN bool
  737. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  738. /* See wasm_export.h for description */
  739. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  740. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  741. /* See wasm_export.h for description */
  742. WASM_RUNTIME_API_EXTERN uint32_t
  743. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst);
  744. bool
  745. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  746. const char *dir_list[], uint32 dir_count,
  747. const char *map_dir_list[], uint32 map_dir_count,
  748. const char *env[], uint32 env_count,
  749. const char *addr_pool[], uint32 addr_pool_size,
  750. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  751. char *argv[], uint32 argc, int stdinfd, int stdoutfd,
  752. int stderrfd, char *error_buf, uint32 error_buf_size);
  753. void
  754. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  755. void
  756. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  757. WASIContext *wasi_ctx);
  758. WASIContext *
  759. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  760. WASM_RUNTIME_API_EXTERN void
  761. wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
  762. uint32 addr_pool_size);
  763. WASM_RUNTIME_API_EXTERN void
  764. wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
  765. const char *ns_lookup_pool[],
  766. uint32 ns_lookup_pool_size);
  767. #endif /* end of WASM_ENABLE_LIBC_WASI */
  768. #if WASM_ENABLE_REF_TYPES != 0
  769. /* See wasm_export.h for description */
  770. WASM_RUNTIME_API_EXTERN bool
  771. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  772. uint32 *p_externref_idx);
  773. /* See wasm_export.h for description */
  774. WASM_RUNTIME_API_EXTERN bool
  775. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  776. /* See wasm_export.h for description */
  777. WASM_RUNTIME_API_EXTERN bool
  778. wasm_externref_retain(uint32 externref_idx);
  779. /**
  780. * Reclaim the externref objects/indexes which are not used by
  781. * module instance
  782. */
  783. void
  784. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  785. /**
  786. * Cleanup the externref objects/indexes of the module instance
  787. */
  788. void
  789. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  790. #endif /* end of WASM_ENABLE_REF_TYPES */
  791. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  792. /**
  793. * @brief Internal implementation for dumping or printing callstack line
  794. *
  795. * @note if dump_or_print is true, then print to stdout directly;
  796. * if dump_or_print is false, but *buf is NULL, then return the length of the
  797. * line;
  798. * if dump_or_print is false, and *buf is not NULL, then dump content to
  799. * the memory pointed by *buf, and adjust *buf and *len according to actual
  800. * bytes dumped, and return the actual dumped length
  801. *
  802. * @param line_buf current line to dump or print
  803. * @param dump_or_print whether to print to stdout or dump to buf
  804. * @param buf [INOUT] pointer to the buffer
  805. * @param len [INOUT] pointer to remaining length
  806. * @return bytes printed to stdout or dumped to buf
  807. */
  808. uint32
  809. wasm_runtime_dump_line_buf_impl(const char *line_buf, bool dump_or_print,
  810. char **buf, uint32 *len);
  811. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 */
  812. /* Get module of the current exec_env */
  813. WASMModuleCommon *
  814. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  815. /* See wasm_export.h for description */
  816. WASM_RUNTIME_API_EXTERN bool
  817. wasm_runtime_register_natives(const char *module_name,
  818. NativeSymbol *native_symbols,
  819. uint32 n_native_symbols);
  820. /* See wasm_export.h for description */
  821. WASM_RUNTIME_API_EXTERN bool
  822. wasm_runtime_register_natives_raw(const char *module_name,
  823. NativeSymbol *native_symbols,
  824. uint32 n_native_symbols);
  825. /* See wasm_export.h for description */
  826. WASM_RUNTIME_API_EXTERN bool
  827. wasm_runtime_unregister_natives(const char *module_name,
  828. NativeSymbol *native_symbols);
  829. /* See wasm_export.h for description */
  830. WASM_RUNTIME_API_EXTERN void *
  831. wasm_runtime_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
  832. void *ctx));
  833. /* See wasm_export.h for description */
  834. WASM_RUNTIME_API_EXTERN void
  835. wasm_runtime_destroy_context_key(void *key);
  836. /* See wasm_export.h for description */
  837. WASM_RUNTIME_API_EXTERN void
  838. wasm_runtime_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx);
  839. /* See wasm_export.h for description */
  840. WASM_RUNTIME_API_EXTERN void
  841. wasm_runtime_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
  842. void *ctx);
  843. /* See wasm_export.h for description */
  844. WASM_RUNTIME_API_EXTERN void *
  845. wasm_runtime_get_context(WASMModuleInstanceCommon *inst, void *key);
  846. bool
  847. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  848. const WASMType *func_type, const char *signature,
  849. void *attachment, uint32 *argv, uint32 argc,
  850. uint32 *ret);
  851. bool
  852. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  853. const WASMType *func_type, const char *signature,
  854. void *attachment, uint32 *argv, uint32 argc,
  855. uint32 *ret);
  856. void
  857. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  858. void
  859. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  860. void
  861. wasm_runtime_dump_module_inst_mem_consumption(
  862. const WASMModuleInstanceCommon *module_inst);
  863. void
  864. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  865. bool
  866. wasm_runtime_get_table_elem_type(const WASMModuleCommon *module_comm,
  867. uint32 table_idx, uint8 *out_elem_type,
  868. uint32 *out_min_size, uint32 *out_max_size);
  869. bool
  870. wasm_runtime_get_table_inst_elem_type(
  871. const WASMModuleInstanceCommon *module_inst_comm, uint32 table_idx,
  872. uint8 *out_elem_type, uint32 *out_min_size, uint32 *out_max_size);
  873. bool
  874. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  875. const WASMExport *export_, WASMType **out);
  876. bool
  877. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  878. const WASMExport *export_,
  879. uint8 *out_val_type, bool *out_mutability);
  880. bool
  881. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  882. const WASMExport *export_,
  883. uint32 *out_min_page, uint32 *out_max_page);
  884. bool
  885. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  886. const WASMExport *export_,
  887. uint8 *out_elem_type, uint32 *out_min_size,
  888. uint32 *out_max_size);
  889. bool
  890. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  891. void *func_ptr, WASMType *func_type,
  892. uint32 argc, uint32 *argv, bool with_env,
  893. void *wasm_c_api_env);
  894. void
  895. wasm_runtime_show_app_heap_corrupted_prompt();
  896. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  897. void
  898. wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list);
  899. #endif
  900. WASM_RUNTIME_API_EXTERN bool
  901. wasm_runtime_is_import_func_linked(const char *module_name,
  902. const char *func_name);
  903. WASM_RUNTIME_API_EXTERN bool
  904. wasm_runtime_is_import_global_linked(const char *module_name,
  905. const char *global_name);
  906. WASM_RUNTIME_API_EXTERN bool
  907. wasm_runtime_begin_blocking_op(WASMExecEnv *exec_env);
  908. WASM_RUNTIME_API_EXTERN void
  909. wasm_runtime_end_blocking_op(WASMExecEnv *exec_env);
  910. void
  911. wasm_runtime_interrupt_blocking_op(WASMExecEnv *exec_env);
  912. #ifdef __cplusplus
  913. }
  914. #endif
  915. #endif /* end of _WASM_COMMON_H */