wasm_runtime_common.h 37 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 "posix.h"
  16. #else
  17. #include "uvwasi.h"
  18. #endif
  19. #endif
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* Internal use for setting default running mode */
  24. #define Mode_Default 0
  25. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  26. #define PUT_I64_TO_ADDR(addr, value) \
  27. do { \
  28. *(int64 *)(addr) = (int64)(value); \
  29. } while (0)
  30. #define PUT_F64_TO_ADDR(addr, value) \
  31. do { \
  32. *(float64 *)(addr) = (float64)(value); \
  33. } while (0)
  34. #define GET_I64_FROM_ADDR(addr) (*(int64 *)(addr))
  35. #define GET_F64_FROM_ADDR(addr) (*(float64 *)(addr))
  36. /* For STORE opcodes */
  37. #define STORE_I64 PUT_I64_TO_ADDR
  38. #define STORE_U32(addr, value) \
  39. do { \
  40. *(uint32 *)(addr) = (uint32)(value); \
  41. } while (0)
  42. #define STORE_U16(addr, value) \
  43. do { \
  44. *(uint16 *)(addr) = (uint16)(value); \
  45. } while (0)
  46. /* For LOAD opcodes */
  47. #define LOAD_I64(addr) (*(int64 *)(addr))
  48. #define LOAD_F64(addr) (*(float64 *)(addr))
  49. #define LOAD_I32(addr) (*(int32 *)(addr))
  50. #define LOAD_U32(addr) (*(uint32 *)(addr))
  51. #define LOAD_I16(addr) (*(int16 *)(addr))
  52. #define LOAD_U16(addr) (*(uint16 *)(addr))
  53. #define STORE_PTR(addr, ptr) \
  54. do { \
  55. *(void **)addr = (void *)ptr; \
  56. } while (0)
  57. #else /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
  58. #define PUT_I64_TO_ADDR(addr, value) \
  59. do { \
  60. uint32 *addr_u32 = (uint32 *)(addr); \
  61. union { \
  62. int64 val; \
  63. uint32 parts[2]; \
  64. } u; \
  65. u.val = (int64)(value); \
  66. addr_u32[0] = u.parts[0]; \
  67. addr_u32[1] = u.parts[1]; \
  68. } while (0)
  69. #define PUT_F64_TO_ADDR(addr, value) \
  70. do { \
  71. uint32 *addr_u32 = (uint32 *)(addr); \
  72. union { \
  73. float64 val; \
  74. uint32 parts[2]; \
  75. } u; \
  76. u.val = (value); \
  77. addr_u32[0] = u.parts[0]; \
  78. addr_u32[1] = u.parts[1]; \
  79. } while (0)
  80. static inline int64
  81. GET_I64_FROM_ADDR(uint32 *addr)
  82. {
  83. union {
  84. int64 val;
  85. uint32 parts[2];
  86. } u;
  87. u.parts[0] = addr[0];
  88. u.parts[1] = addr[1];
  89. return u.val;
  90. }
  91. static inline float64
  92. GET_F64_FROM_ADDR(uint32 *addr)
  93. {
  94. union {
  95. float64 val;
  96. uint32 parts[2];
  97. } u;
  98. u.parts[0] = addr[0];
  99. u.parts[1] = addr[1];
  100. return u.val;
  101. }
  102. /* For STORE opcodes */
  103. #define STORE_I64(addr, value) \
  104. do { \
  105. uintptr_t addr_ = (uintptr_t)(addr); \
  106. union { \
  107. int64 val; \
  108. uint32 u32[2]; \
  109. uint16 u16[4]; \
  110. uint8 u8[8]; \
  111. } u; \
  112. if ((addr_ & (uintptr_t)7) == 0) \
  113. *(int64 *)(addr) = (int64)(value); \
  114. else { \
  115. u.val = (int64)(value); \
  116. if ((addr_ & (uintptr_t)3) == 0) { \
  117. ((uint32 *)(addr))[0] = u.u32[0]; \
  118. ((uint32 *)(addr))[1] = u.u32[1]; \
  119. } \
  120. else if ((addr_ & (uintptr_t)1) == 0) { \
  121. ((uint16 *)(addr))[0] = u.u16[0]; \
  122. ((uint16 *)(addr))[1] = u.u16[1]; \
  123. ((uint16 *)(addr))[2] = u.u16[2]; \
  124. ((uint16 *)(addr))[3] = u.u16[3]; \
  125. } \
  126. else { \
  127. int32 t; \
  128. for (t = 0; t < 8; t++) \
  129. ((uint8 *)(addr))[t] = u.u8[t]; \
  130. } \
  131. } \
  132. } while (0)
  133. #define STORE_U32(addr, value) \
  134. do { \
  135. uintptr_t addr_ = (uintptr_t)(addr); \
  136. union { \
  137. uint32 val; \
  138. uint16 u16[2]; \
  139. uint8 u8[4]; \
  140. } u; \
  141. if ((addr_ & (uintptr_t)3) == 0) \
  142. *(uint32 *)(addr) = (uint32)(value); \
  143. else { \
  144. u.val = (uint32)(value); \
  145. if ((addr_ & (uintptr_t)1) == 0) { \
  146. ((uint16 *)(addr))[0] = u.u16[0]; \
  147. ((uint16 *)(addr))[1] = u.u16[1]; \
  148. } \
  149. else { \
  150. ((uint8 *)(addr))[0] = u.u8[0]; \
  151. ((uint8 *)(addr))[1] = u.u8[1]; \
  152. ((uint8 *)(addr))[2] = u.u8[2]; \
  153. ((uint8 *)(addr))[3] = u.u8[3]; \
  154. } \
  155. } \
  156. } while (0)
  157. #define STORE_U16(addr, value) \
  158. do { \
  159. union { \
  160. uint16 val; \
  161. uint8 u8[2]; \
  162. } u; \
  163. u.val = (uint16)(value); \
  164. ((uint8 *)(addr))[0] = u.u8[0]; \
  165. ((uint8 *)(addr))[1] = u.u8[1]; \
  166. } while (0)
  167. /* For LOAD opcodes */
  168. static inline int64
  169. LOAD_I64(void *addr)
  170. {
  171. uintptr_t addr1 = (uintptr_t)addr;
  172. union {
  173. int64 val;
  174. uint32 u32[2];
  175. uint16 u16[4];
  176. uint8 u8[8];
  177. } u;
  178. if ((addr1 & (uintptr_t)7) == 0)
  179. return *(int64 *)addr;
  180. if ((addr1 & (uintptr_t)3) == 0) {
  181. u.u32[0] = ((uint32 *)addr)[0];
  182. u.u32[1] = ((uint32 *)addr)[1];
  183. }
  184. else if ((addr1 & (uintptr_t)1) == 0) {
  185. u.u16[0] = ((uint16 *)addr)[0];
  186. u.u16[1] = ((uint16 *)addr)[1];
  187. u.u16[2] = ((uint16 *)addr)[2];
  188. u.u16[3] = ((uint16 *)addr)[3];
  189. }
  190. else {
  191. int32 t;
  192. for (t = 0; t < 8; t++)
  193. u.u8[t] = ((uint8 *)addr)[t];
  194. }
  195. return u.val;
  196. }
  197. static inline float64
  198. LOAD_F64(void *addr)
  199. {
  200. uintptr_t addr1 = (uintptr_t)addr;
  201. union {
  202. float64 val;
  203. uint32 u32[2];
  204. uint16 u16[4];
  205. uint8 u8[8];
  206. } u;
  207. if ((addr1 & (uintptr_t)7) == 0)
  208. return *(float64 *)addr;
  209. if ((addr1 & (uintptr_t)3) == 0) {
  210. u.u32[0] = ((uint32 *)addr)[0];
  211. u.u32[1] = ((uint32 *)addr)[1];
  212. }
  213. else if ((addr1 & (uintptr_t)1) == 0) {
  214. u.u16[0] = ((uint16 *)addr)[0];
  215. u.u16[1] = ((uint16 *)addr)[1];
  216. u.u16[2] = ((uint16 *)addr)[2];
  217. u.u16[3] = ((uint16 *)addr)[3];
  218. }
  219. else {
  220. int32 t;
  221. for (t = 0; t < 8; t++)
  222. u.u8[t] = ((uint8 *)addr)[t];
  223. }
  224. return u.val;
  225. }
  226. static inline int32
  227. LOAD_I32(void *addr)
  228. {
  229. uintptr_t addr1 = (uintptr_t)addr;
  230. union {
  231. int32 val;
  232. uint16 u16[2];
  233. uint8 u8[4];
  234. } u;
  235. if ((addr1 & (uintptr_t)3) == 0)
  236. return *(int32 *)addr;
  237. if ((addr1 & (uintptr_t)1) == 0) {
  238. u.u16[0] = ((uint16 *)addr)[0];
  239. u.u16[1] = ((uint16 *)addr)[1];
  240. }
  241. else {
  242. u.u8[0] = ((uint8 *)addr)[0];
  243. u.u8[1] = ((uint8 *)addr)[1];
  244. u.u8[2] = ((uint8 *)addr)[2];
  245. u.u8[3] = ((uint8 *)addr)[3];
  246. }
  247. return u.val;
  248. }
  249. static inline int16
  250. LOAD_I16(void *addr)
  251. {
  252. uintptr_t addr1 = (uintptr_t)addr;
  253. union {
  254. int16 val;
  255. uint8 u8[2];
  256. } u;
  257. if ((addr1 & (uintptr_t)1)) {
  258. u.u8[0] = ((uint8 *)addr)[0];
  259. u.u8[1] = ((uint8 *)addr)[1];
  260. return u.val;
  261. }
  262. return *(int16 *)addr;
  263. }
  264. #define LOAD_U32(addr) ((uint32)LOAD_I32(addr))
  265. #define LOAD_U16(addr) ((uint16)LOAD_I16(addr))
  266. #if UINTPTR_MAX == UINT32_MAX
  267. #define STORE_PTR(addr, ptr) STORE_U32(addr, (uintptr_t)ptr)
  268. #elif UINTPTR_MAX == UINT64_MAX
  269. #define STORE_PTR(addr, ptr) STORE_I64(addr, (uintptr_t)ptr)
  270. #endif
  271. #endif /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
  272. typedef struct WASMModuleCommon {
  273. /* Module type, for module loaded from WASM bytecode binary,
  274. this field is Wasm_Module_Bytecode, and this structure should
  275. be treated as WASMModule structure;
  276. for module loaded from AOT binary, this field is
  277. Wasm_Module_AoT, and this structure should be treated as
  278. AOTModule structure. */
  279. uint32 module_type;
  280. /* The following uint8[1] member is a dummy just to indicate
  281. some module_type dependent members follow.
  282. Typically it should be accessed by casting to the corresponding
  283. actual module_type dependent structure, not via this member. */
  284. uint8 module_data[1];
  285. } WASMModuleCommon;
  286. typedef struct WASMModuleInstanceCommon {
  287. /* Module instance type, for module instance loaded from WASM
  288. bytecode binary, this field is Wasm_Module_Bytecode, and this
  289. structure should be treated as WASMModuleInstance structure;
  290. for module instance loaded from AOT binary, this field is
  291. Wasm_Module_AoT, and this structure should be treated as
  292. AOTModuleInstance structure. */
  293. uint32 module_type;
  294. /* The following uint8[1] member is a dummy just to indicate
  295. some module_type dependent members follow.
  296. Typically it should be accessed by casting to the corresponding
  297. actual module_type dependent structure, not via this member. */
  298. uint8 module_inst_data[1];
  299. } WASMModuleInstanceCommon;
  300. typedef struct WASMModuleMemConsumption {
  301. uint32 total_size;
  302. uint32 module_struct_size;
  303. uint32 types_size;
  304. uint32 imports_size;
  305. uint32 functions_size;
  306. uint32 tables_size;
  307. uint32 memories_size;
  308. uint32 globals_size;
  309. uint32 exports_size;
  310. uint32 table_segs_size;
  311. uint32 data_segs_size;
  312. uint32 const_strs_size;
  313. #if WASM_ENABLE_AOT != 0
  314. uint32 aot_code_size;
  315. #endif
  316. } WASMModuleMemConsumption;
  317. typedef struct WASMModuleInstMemConsumption {
  318. uint32 total_size;
  319. uint32 module_inst_struct_size;
  320. uint32 memories_size;
  321. uint32 app_heap_size;
  322. uint32 tables_size;
  323. uint32 globals_size;
  324. uint32 functions_size;
  325. uint32 exports_size;
  326. } WASMModuleInstMemConsumption;
  327. #if WASM_ENABLE_LIBC_WASI != 0
  328. #if WASM_ENABLE_UVWASI == 0
  329. typedef struct WASIContext {
  330. struct fd_table *curfds;
  331. struct fd_prestats *prestats;
  332. struct argv_environ_values *argv_environ;
  333. struct addr_pool *addr_pool;
  334. char *ns_lookup_buf;
  335. char **ns_lookup_list;
  336. char *argv_buf;
  337. char **argv_list;
  338. char *env_buf;
  339. char **env_list;
  340. uint32_t exit_code;
  341. } WASIContext;
  342. #else
  343. typedef struct WASIContext {
  344. uvwasi_t uvwasi;
  345. uint32_t exit_code;
  346. } WASIContext;
  347. #endif
  348. #endif
  349. #if WASM_ENABLE_MULTI_MODULE != 0
  350. typedef struct WASMRegisteredModule {
  351. bh_list_link l;
  352. /* point to a string pool */
  353. const char *module_name;
  354. WASMModuleCommon *module;
  355. /* to store the original module file buffer address */
  356. uint8 *orig_file_buf;
  357. uint32 orig_file_buf_size;
  358. } WASMRegisteredModule;
  359. #endif
  360. typedef struct WASMMemoryInstanceCommon {
  361. uint32 module_type;
  362. /* The following uint8[1] member is a dummy just to indicate
  363. some module_type dependent members follow.
  364. Typically it should be accessed by casting to the corresponding
  365. actual module_type dependent structure, not via this member. */
  366. uint8 memory_inst_data[1];
  367. } WASMMemoryInstanceCommon;
  368. typedef package_type_t PackageType;
  369. typedef wasm_section_t WASMSection, AOTSection;
  370. typedef struct wasm_frame_t {
  371. /* wasm_instance_t */
  372. void *instance;
  373. uint32 module_offset;
  374. uint32 func_index;
  375. uint32 func_offset;
  376. const char *func_name_wp;
  377. } WASMCApiFrame;
  378. #ifdef WASM_ENABLE_JIT
  379. typedef struct LLVMJITOptions {
  380. uint32 opt_level;
  381. uint32 size_level;
  382. uint32 segue_flags;
  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,
  445. WASMModuleInstanceCommon *parent,
  446. WASMExecEnv *exec_env_main, uint32 stack_size,
  447. uint32 heap_size, char *error_buf,
  448. uint32 error_buf_size);
  449. /* Internal API */
  450. void
  451. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  452. bool is_sub_inst);
  453. /* See wasm_export.h for description */
  454. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  455. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 default_stack_size,
  456. uint32 host_managed_heap_size, char *error_buf,
  457. uint32 error_buf_size);
  458. /* See wasm_export.h for description */
  459. WASM_RUNTIME_API_EXTERN bool
  460. wasm_runtime_set_running_mode(wasm_module_inst_t module_inst,
  461. RunningMode running_mode);
  462. /* See wasm_export.h for description */
  463. WASM_RUNTIME_API_EXTERN RunningMode
  464. wasm_runtime_get_running_mode(wasm_module_inst_t module_inst);
  465. /* See wasm_export.h for description */
  466. WASM_RUNTIME_API_EXTERN void
  467. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  468. /* See wasm_export.h for description */
  469. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  470. wasm_runtime_get_module(WASMModuleInstanceCommon *module_inst);
  471. /* See wasm_export.h for description */
  472. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  473. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  474. const char *name, const char *signature);
  475. /* Internal API */
  476. WASMType *
  477. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  478. uint32 module_type);
  479. /* See wasm_export.h for description */
  480. WASM_RUNTIME_API_EXTERN uint32
  481. wasm_func_get_param_count(WASMFunctionInstanceCommon *const func_inst,
  482. WASMModuleInstanceCommon *const module_inst);
  483. /* See wasm_export.h for description */
  484. WASM_RUNTIME_API_EXTERN uint32
  485. wasm_func_get_result_count(WASMFunctionInstanceCommon *const func_inst,
  486. WASMModuleInstanceCommon *const module_inst);
  487. /* See wasm_export.h for description */
  488. WASM_RUNTIME_API_EXTERN void
  489. wasm_func_get_param_types(WASMFunctionInstanceCommon *const func_inst,
  490. WASMModuleInstanceCommon *const module_inst,
  491. wasm_valkind_t *param_types);
  492. /* See wasm_export.h for description */
  493. WASM_RUNTIME_API_EXTERN void
  494. wasm_func_get_result_types(WASMFunctionInstanceCommon *const func_inst,
  495. WASMModuleInstanceCommon *const module_inst,
  496. wasm_valkind_t *result_types);
  497. /* See wasm_export.h for description */
  498. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  499. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  500. uint32 stack_size);
  501. /* See wasm_export.h for description */
  502. WASM_RUNTIME_API_EXTERN void
  503. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  504. /* See wasm_export.h for description */
  505. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  506. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  507. /* See wasm_export.h for description */
  508. WASM_RUNTIME_API_EXTERN void
  509. wasm_runtime_set_module_inst(WASMExecEnv *exec_env,
  510. WASMModuleInstanceCommon *const module_inst);
  511. /* See wasm_export.h for description */
  512. WASM_RUNTIME_API_EXTERN void *
  513. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  514. /* See wasm_export.h for description */
  515. WASM_RUNTIME_API_EXTERN void
  516. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  517. /* See wasm_export.h for description */
  518. WASM_RUNTIME_API_EXTERN void *
  519. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  520. #if WASM_CONFIGUABLE_BOUNDS_CHECKS != 0
  521. /* See wasm_export.h for description */
  522. WASM_RUNTIME_API_EXTERN void
  523. wasm_runtime_set_bounds_checks(WASMModuleInstanceCommon *module_inst,
  524. bool enable);
  525. /* See wasm_export.h for description */
  526. WASM_RUNTIME_API_EXTERN bool
  527. wasm_runtime_is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst);
  528. #endif
  529. #ifdef OS_ENABLE_HW_BOUND_CHECK
  530. /* Access exception check guard page to trigger the signal handler */
  531. void
  532. wasm_runtime_access_exce_check_guard_page();
  533. #endif
  534. /* See wasm_export.h for description */
  535. WASM_RUNTIME_API_EXTERN bool
  536. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  537. WASMFunctionInstanceCommon *function, uint32 argc,
  538. uint32 argv[]);
  539. WASM_RUNTIME_API_EXTERN bool
  540. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  541. WASMFunctionInstanceCommon *function,
  542. uint32 num_results, wasm_val_t *results,
  543. uint32 num_args, wasm_val_t *args);
  544. WASM_RUNTIME_API_EXTERN bool
  545. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  546. WASMFunctionInstanceCommon *function,
  547. uint32 num_results, wasm_val_t *results,
  548. uint32 num_args, ...);
  549. /* See wasm_export.h for description */
  550. WASM_RUNTIME_API_EXTERN bool
  551. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
  552. uint32 argc, uint32 argv[]);
  553. #if WASM_ENABLE_DEBUG_INTERP != 0
  554. /* See wasm_export.h for description */
  555. WASM_RUNTIME_API_EXTERN uint32
  556. wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
  557. int32_t port);
  558. /* See wasm_export.h for description */
  559. WASM_RUNTIME_API_EXTERN uint32
  560. wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
  561. #endif
  562. bool
  563. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  564. /* See wasm_export.h for description */
  565. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  566. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  567. /* See wasm_export.h for description */
  568. WASM_RUNTIME_API_EXTERN bool
  569. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  570. char *argv[]);
  571. /* See wasm_export.h for description */
  572. WASM_RUNTIME_API_EXTERN bool
  573. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  574. const char *name, int32 argc, char *argv[]);
  575. /* See wasm_export.h for description */
  576. WASM_RUNTIME_API_EXTERN void
  577. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  578. const char *exception);
  579. /* See wasm_export.h for description */
  580. WASM_RUNTIME_API_EXTERN const char *
  581. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  582. /* See wasm_export.h for description */
  583. WASM_RUNTIME_API_EXTERN void
  584. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  585. /* See wasm_export.h for description */
  586. WASM_RUNTIME_API_EXTERN void
  587. wasm_runtime_terminate(WASMModuleInstanceCommon *module);
  588. /* Internal API */
  589. void
  590. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  591. void *custom_data);
  592. /* See wasm_export.h for description */
  593. WASM_RUNTIME_API_EXTERN void
  594. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  595. void *custom_data);
  596. /* See wasm_export.h for description */
  597. WASM_RUNTIME_API_EXTERN void *
  598. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  599. /* Internal API */
  600. uint32
  601. wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
  602. WASMExecEnv *exec_env, uint32 size,
  603. void **p_native_addr);
  604. /* Internal API */
  605. uint32
  606. wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
  607. WASMExecEnv *exec_env, uint32 ptr,
  608. uint32 size, void **p_native_addr);
  609. /* Internal API */
  610. void
  611. wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
  612. WASMExecEnv *exec_env, uint32 ptr);
  613. /* See wasm_export.h for description */
  614. WASM_RUNTIME_API_EXTERN uint32
  615. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  616. void **p_native_addr);
  617. /* See wasm_export.h for description */
  618. WASM_RUNTIME_API_EXTERN void
  619. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  620. /* See wasm_export.h for description */
  621. WASM_RUNTIME_API_EXTERN uint32
  622. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  623. const char *src, uint32 size);
  624. /* See wasm_export.h for description */
  625. WASM_RUNTIME_API_EXTERN bool
  626. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  627. uint32 app_offset, uint32 size);
  628. /* See wasm_export.h for description */
  629. WASM_RUNTIME_API_EXTERN bool
  630. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  631. uint32 app_str_offset);
  632. /* See wasm_export.h for description */
  633. WASM_RUNTIME_API_EXTERN bool
  634. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  635. void *native_ptr, uint32 size);
  636. /* See wasm_export.h for description */
  637. WASM_RUNTIME_API_EXTERN void *
  638. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  639. uint32 app_offset);
  640. /* See wasm_export.h for description */
  641. WASM_RUNTIME_API_EXTERN uint32
  642. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  643. void *native_ptr);
  644. /* See wasm_export.h for description */
  645. WASM_RUNTIME_API_EXTERN bool
  646. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  647. uint32 app_offset, uint32 *p_app_start_offset,
  648. uint32 *p_app_end_offset);
  649. /* See wasm_export.h for description */
  650. WASM_RUNTIME_API_EXTERN bool
  651. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  652. uint8 *native_ptr,
  653. uint8 **p_native_start_addr,
  654. uint8 **p_native_end_addr);
  655. /* See wasm_export.h for description */
  656. WASM_RUNTIME_API_EXTERN const uint8 *
  657. wasm_runtime_get_custom_section(WASMModuleCommon *const module_comm,
  658. const char *name, uint32 *len);
  659. #if WASM_ENABLE_MULTI_MODULE != 0
  660. WASM_RUNTIME_API_EXTERN void
  661. wasm_runtime_set_module_reader(const module_reader reader,
  662. const module_destroyer destroyer);
  663. module_reader
  664. wasm_runtime_get_module_reader();
  665. module_destroyer
  666. wasm_runtime_get_module_destroyer();
  667. bool
  668. wasm_runtime_register_module_internal(const char *module_name,
  669. WASMModuleCommon *module,
  670. uint8 *orig_file_buf,
  671. uint32 orig_file_buf_size,
  672. char *error_buf, uint32 error_buf_size);
  673. void
  674. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  675. WASMModuleCommon *
  676. wasm_runtime_find_module_registered(const char *module_name);
  677. bool
  678. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  679. uint32 error_buf_size);
  680. void
  681. wasm_runtime_delete_loading_module(const char *module_name);
  682. bool
  683. wasm_runtime_is_loading_module(const char *module_name);
  684. void
  685. wasm_runtime_destroy_loading_module_list();
  686. WASMModuleCommon *
  687. wasm_runtime_search_sub_module(const WASMModuleCommon *parent_module,
  688. const char *sub_module_name);
  689. bool
  690. wasm_runtime_register_sub_module(const WASMModuleCommon *parent_module,
  691. const char *sub_module_name,
  692. WASMModuleCommon *sub_module);
  693. WASMModuleCommon *
  694. wasm_runtime_load_depended_module(const WASMModuleCommon *parent_module,
  695. const char *sub_module_name, char *error_buf,
  696. uint32 error_buf_size);
  697. bool
  698. wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
  699. WASMModuleInstanceCommon *module_inst,
  700. uint32 stack_size, uint32 heap_size,
  701. char *error_buf, uint32 error_buf_size);
  702. void
  703. wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst);
  704. #endif
  705. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  706. WASMExport *
  707. loader_find_export(const WASMModuleCommon *module, const char *module_name,
  708. const char *field_name, uint8 export_kind, char *error_buf,
  709. uint32 error_buf_size);
  710. #endif /* WASM_ENALBE_MULTI_MODULE */
  711. bool
  712. wasm_runtime_is_built_in_module(const char *module_name);
  713. #if WASM_ENABLE_THREAD_MGR != 0
  714. bool
  715. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  716. uint32 *size);
  717. bool
  718. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  719. uint32 size);
  720. #endif
  721. #if WASM_ENABLE_LIBC_WASI != 0
  722. WASM_RUNTIME_API_EXTERN void
  723. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  724. uint32 dir_count, const char *map_dir_list[],
  725. uint32 map_dir_count, const char *env_list[],
  726. uint32 env_count, char *argv[], int argc,
  727. int64 stdinfd, int64 stdoutfd, int64 stderrfd);
  728. /* See wasm_export.h for description */
  729. WASM_RUNTIME_API_EXTERN void
  730. wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
  731. uint32 dir_count, const char *map_dir_list[],
  732. uint32 map_dir_count, const char *env_list[],
  733. uint32 env_count, char *argv[], int argc);
  734. /* See wasm_export.h for description */
  735. WASM_RUNTIME_API_EXTERN bool
  736. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  737. /* See wasm_export.h for description */
  738. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  739. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  740. /* See wasm_export.h for description */
  741. WASM_RUNTIME_API_EXTERN uint32_t
  742. wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst);
  743. bool
  744. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  745. const char *dir_list[], uint32 dir_count,
  746. const char *map_dir_list[], uint32 map_dir_count,
  747. const char *env[], uint32 env_count,
  748. const char *addr_pool[], uint32 addr_pool_size,
  749. const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
  750. char *argv[], uint32 argc, os_raw_file_handle stdinfd,
  751. os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
  752. 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 */