wasm_runtime_common.h 37 KB

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