wasm_runtime_common.h 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. #include "wasm_multimodules_program.h"
  14. #if WASM_ENABLE_LIBC_WASI != 0
  15. #if WASM_ENABLE_UVWASI == 0
  16. #include "wasmtime_ssp.h"
  17. #include "posix.h"
  18. #else
  19. #include "uvwasi.h"
  20. #endif
  21. #endif
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  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 addr1 = (uintptr_t)(addr); \
  106. union { \
  107. int64 val; \
  108. uint32 u32[2]; \
  109. uint16 u16[4]; \
  110. uint8 u8[8]; \
  111. } u; \
  112. if ((addr1 & (uintptr_t)7) == 0) \
  113. *(int64 *)(addr) = (int64)(value); \
  114. else { \
  115. u.val = (int64)(value); \
  116. if ((addr1 & (uintptr_t)3) == 0) { \
  117. ((uint32 *)(addr))[0] = u.u32[0]; \
  118. ((uint32 *)(addr))[1] = u.u32[1]; \
  119. } \
  120. else if ((addr1 & (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 addr1 = (uintptr_t)(addr); \
  136. union { \
  137. uint32 val; \
  138. uint16 u16[2]; \
  139. uint8 u8[4]; \
  140. } u; \
  141. if ((addr1 & (uintptr_t)3) == 0) \
  142. *(uint32 *)(addr) = (uint32)(value); \
  143. else { \
  144. u.val = (uint32)(value); \
  145. if ((addr1 & (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. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  273. typedef struct DependencyModuleInitGlobals {
  274. uint32 memory_base;
  275. uint32 actual_memory_base;
  276. uint32 table_base;
  277. uint32 table_alignment;
  278. uint32 table_size;
  279. uint32 stack_pointer;
  280. } DependencyModuleInitGlobals;
  281. typedef struct WASMModuleInstanceHead {
  282. uint32 module_type;
  283. // unique instance id in program scope, used to alloc table space currently.
  284. uint32 inst_id;
  285. WASMRuntime * runtime;
  286. WASMProgramInstance * program;
  287. HashMap * local_implicit_dependency_modules_name_hmap;
  288. DependencyModuleInitGlobals init_globals;
  289. // explicit ref count, updated by dlopen/dlclose
  290. uint32 exp_ref_cnt;
  291. // implicit ref count, updated according to needed library entries
  292. uint32 imp_ref_cnt;
  293. #ifdef TARGET_32
  294. uint32 padding_runtime;
  295. uint32 padding_program;
  296. uint32 padding_dep_hmap;
  297. #endif
  298. } WASMModuleInstanceHead;
  299. #endif
  300. typedef struct WASMProgramCommon {
  301. uint8 module_data[1];
  302. } WASMProgramCommon;
  303. typedef struct WASMModuleCommon {
  304. /* Module type, for module loaded from WASM bytecode binary,
  305. this field is Wasm_Module_Bytecode, and this structure should
  306. be treated as WASMModule structure;
  307. for module loaded from AOT binary, this field is
  308. Wasm_Module_AoT, and this structure should be treated as
  309. AOTModule structure. */
  310. uint32 module_type;
  311. uint8 module_data[1];
  312. } WASMModuleCommon;
  313. typedef struct WASMModuleInstanceCommon {
  314. /* Module instance type, for module instance loaded from WASM
  315. bytecode binary, this field is Wasm_Module_Bytecode, and this
  316. structure should be treated as WASMModuleInstance structure;
  317. for module instance loaded from AOT binary, this field is
  318. Wasm_Module_AoT, and this structure should be treated as
  319. AOTModuleInstance structure. */
  320. uint32 module_type;
  321. uint8 module_inst_data[1];
  322. } WASMModuleInstanceCommon;
  323. typedef struct WASMModuleMemConsumption {
  324. uint32 total_size;
  325. uint32 module_struct_size;
  326. uint32 types_size;
  327. uint32 imports_size;
  328. uint32 functions_size;
  329. uint32 tables_size;
  330. uint32 memories_size;
  331. uint32 globals_size;
  332. uint32 exports_size;
  333. uint32 table_segs_size;
  334. uint32 data_segs_size;
  335. uint32 const_strs_size;
  336. #if WASM_ENABLE_AOT != 0
  337. uint32 aot_code_size;
  338. #endif
  339. } WASMModuleMemConsumption;
  340. typedef struct WASMModuleInstMemConsumption {
  341. uint32 total_size;
  342. uint32 module_inst_struct_size;
  343. uint32 memories_size;
  344. uint32 app_heap_size;
  345. uint32 tables_size;
  346. uint32 globals_size;
  347. uint32 functions_size;
  348. uint32 exports_size;
  349. } WASMModuleInstMemConsumption;
  350. #if WASM_ENABLE_LIBC_WASI != 0
  351. #if WASM_ENABLE_UVWASI == 0
  352. typedef struct WASIContext {
  353. struct fd_table *curfds;
  354. struct fd_prestats *prestats;
  355. struct argv_environ_values *argv_environ;
  356. char *argv_buf;
  357. char **argv_list;
  358. char *env_buf;
  359. char **env_list;
  360. } WASIContext;
  361. #else
  362. typedef uvwasi_t WASIContext;
  363. #endif
  364. #endif
  365. #if WASM_ENABLE_MULTI_MODULE != 0
  366. typedef struct WASMRegisteredModule {
  367. bh_list_link l;
  368. /* point to a string pool */
  369. const char *module_name;
  370. WASMModuleCommon *module;
  371. /* to store the original module file buffer address */
  372. uint8 *orig_file_buf;
  373. uint32 orig_file_buf_size;
  374. } WASMRegisteredModule;
  375. #endif
  376. typedef struct WASMMemoryInstanceCommon {
  377. uint32 module_type;
  378. uint8 memory_inst_data[1];
  379. } WASMMemoryInstanceCommon;
  380. typedef package_type_t PackageType;
  381. typedef wasm_section_t WASMSection, AOTSection;
  382. typedef struct wasm_frame_t {
  383. /* wasm_instance_t */
  384. void *instance;
  385. uint32 module_offset;
  386. uint32 func_index;
  387. uint32 func_offset;
  388. } WASMCApiFrame;
  389. /* See wasm_export.h for description */
  390. WASM_RUNTIME_API_EXTERN bool
  391. wasm_runtime_init(void);
  392. /* See wasm_export.h for description */
  393. WASM_RUNTIME_API_EXTERN bool
  394. wasm_runtime_full_init(RuntimeInitArgs *init_args);
  395. /* See wasm_export.h for description */
  396. WASM_RUNTIME_API_EXTERN void
  397. wasm_runtime_destroy(void);
  398. /* See wasm_export.h for description */
  399. WASM_RUNTIME_API_EXTERN PackageType
  400. get_package_type(const uint8 *buf, uint32 size);
  401. /* See wasm_export.h for description */
  402. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  403. wasm_runtime_load(const uint8 *buf, uint32 size, char *error_buf,
  404. uint32 error_buf_size);
  405. /* See wasm_export.h for description */
  406. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  407. wasm_runtime_load2(const char * name, const uint8 *buf, uint32 size,
  408. char *error_buf, uint32 error_buf_size);
  409. /* See wasm_export.h for description */
  410. WASM_RUNTIME_API_EXTERN WASMModuleCommon *
  411. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  412. char *error_buf, uint32 error_buf_size);
  413. /* See wasm_export.h for description */
  414. WASM_RUNTIME_API_EXTERN void
  415. wasm_runtime_unload(WASMModuleCommon *module);
  416. WASM_RUNTIME_API_EXTERN WASMProgramCommon *
  417. wasm_runtime_create_program(WASMModuleCommon * module, uint32 stack_size,
  418. uint32 heap_size, uint32 dlopen_mode,
  419. char * error_buf, uint32 error_buf_size);
  420. WASM_RUNTIME_API_EXTERN void
  421. wasm_runtime_destroy_program(WASMProgramCommon* program);
  422. /* Internal API */
  423. /**
  424. * load and instantiate a wasm module and its dependencies at runtime.
  425. *
  426. */
  427. //uint32
  428. //wasm_runtime_open_dependencies(wasm_module_inst_t module_inst,
  429. // const char * path, bool is_aot);
  430. uint32
  431. wasm_runtime_lookup_symbol_from_module(wasm_module_inst_t caller_module,
  432. uint32 module_slot, const char * symbol);
  433. /* Internal API */
  434. WASMModuleInstanceCommon *
  435. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  436. uint32 stack_size, uint32 heap_size,
  437. char *error_buf, uint32 error_buf_size);
  438. /* Internal API */
  439. void
  440. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  441. bool is_sub_inst);
  442. WASMModuleCommon *
  443. load_dependency_module_internal(module_reader reader, module_destroyer destroyer,
  444. const char *sub_module_name, const uint32 name_len,
  445. const package_type_t module_type, char *error_buf,
  446. uint32 error_buf_size);
  447. inline static uint32
  448. const_str_hash(ConstStrDescription * key)
  449. {
  450. if (key->hash)
  451. return key->hash;
  452. /* take FNV-1a as hash function*/
  453. uint32 hash = 2166136261u;
  454. for (uint32 i = 0; i < key->len; i++) {
  455. hash ^= (uint8)key->str[i];
  456. hash *= 16777619;
  457. }
  458. key->hash = hash;
  459. return hash;
  460. }
  461. inline static bool
  462. const_str_equal(const ConstStrDescription * key1, const ConstStrDescription * key2)
  463. {
  464. if ((key1->hash != key2->hash) || (key1->len != key2->len))
  465. return false;
  466. return ((strncmp(key1->str, key2->str, key1->len) == 0) ? true : false);
  467. }
  468. inline static void
  469. const_str_destroy_key(void *key)
  470. {
  471. if (!key)
  472. return;
  473. if (!((ConstStrDescription*)key)->is_sys_symbol)
  474. wasm_runtime_free(key);
  475. }
  476. inline static void
  477. const_str_destroy_value(void *value)
  478. {
  479. if (!value)
  480. return;
  481. wasm_runtime_free(value);
  482. }
  483. inline static void
  484. const_str_destroy_module(void *module)
  485. {
  486. if (!module)
  487. return;
  488. wasm_runtime_unload((WASMModuleCommon*)module);
  489. }
  490. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  491. inline static void
  492. const_str_destroy_module_inst(void *module_inst)
  493. {
  494. WASMModuleInstanceHead * inst_head = NULL;
  495. bool is_sub_inst = true;
  496. if (!module_inst)
  497. return;
  498. inst_head = (WASMModuleInstanceHead *)module_inst;
  499. if (inst_head->program->root_module_inst == module_inst)
  500. is_sub_inst = false;
  501. wasm_runtime_deinstantiate_internal((WASMModuleInstanceCommon *)module_inst, is_sub_inst);
  502. }
  503. #endif
  504. /* See wasm_export.h for description */
  505. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  506. wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
  507. uint32 heap_size, char *error_buf,
  508. uint32 error_buf_size);
  509. /* See wasm_export.h for description */
  510. WASM_RUNTIME_API_EXTERN void
  511. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);
  512. /* See wasm_export.h for description */
  513. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  514. wasm_runtime_lookup_function(WASMModuleInstanceCommon *const module_inst,
  515. const char *name, const char *signature);
  516. /* Internal API */
  517. WASMType *
  518. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  519. uint32 module_type);
  520. /* See wasm_export.h for description */
  521. WASM_RUNTIME_API_EXTERN WASMExecEnv *
  522. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  523. uint32 stack_size);
  524. /* See wasm_export.h for description */
  525. WASM_RUNTIME_API_EXTERN void
  526. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env);
  527. /* See wasm_export.h for description */
  528. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  529. wasm_runtime_get_module_inst(WASMExecEnv *exec_env);
  530. WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
  531. wasm_runtime_get_root_module_inst(WASMExecEnv *exec_env);
  532. /* See wasm_export.h for description */
  533. WASM_RUNTIME_API_EXTERN void *
  534. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env);
  535. /* See wasm_export.h for description */
  536. WASM_RUNTIME_API_EXTERN void
  537. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
  538. /* See wasm_export.h for description */
  539. WASM_RUNTIME_API_EXTERN void *
  540. wasm_runtime_get_user_data(WASMExecEnv *exec_env);
  541. /* See wasm_export.h for description */
  542. WASM_RUNTIME_API_EXTERN bool
  543. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  544. WASMFunctionInstanceCommon *function, uint32 argc,
  545. uint32 argv[]);
  546. WASM_RUNTIME_API_EXTERN bool
  547. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  548. WASMFunctionInstanceCommon *function,
  549. uint32 num_results, wasm_val_t *results,
  550. uint32 num_args, wasm_val_t *args);
  551. WASM_RUNTIME_API_EXTERN bool
  552. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  553. WASMFunctionInstanceCommon *function,
  554. uint32 num_results, wasm_val_t *results,
  555. uint32 num_args, ...);
  556. /**
  557. * Call a function reference of a given WASM runtime instance with
  558. * arguments.
  559. *
  560. * @param exec_env the execution environment to call the function
  561. * which must be created from wasm_create_exec_env()
  562. * @param element_indices the function ference indicies, usually
  563. * prvovided by the caller of a registed native function
  564. * @param argc the number of arguments
  565. * @param argv the arguments. If the function method has return value,
  566. * the first (or first two in case 64-bit return value) element of
  567. * argv stores the return value of the called WASM function after this
  568. * function returns.
  569. *
  570. * @return true if success, false otherwise and exception will be thrown,
  571. * the caller can call wasm_runtime_get_exception to get exception info.
  572. */
  573. bool
  574. wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
  575. uint32 argc, uint32 argv[]);
  576. bool
  577. wasm_runtime_create_exec_env_and_call_wasm(
  578. WASMModuleInstanceCommon *module_inst, WASMFunctionInstanceCommon *function,
  579. uint32 argc, uint32 argv[]);
  580. bool
  581. wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  582. WASMExecEnv *
  583. wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
  584. /* See wasm_export.h for description */
  585. WASM_RUNTIME_API_EXTERN bool
  586. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
  587. char *argv[]);
  588. WASM_RUNTIME_API_EXTERN bool
  589. wasm_application_execute_program_main(WASMProgramCommon *program,
  590. int32 argc, char *argv[]);
  591. /* See wasm_export.h for description */
  592. WASM_RUNTIME_API_EXTERN bool
  593. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  594. const char *name, int32 argc, char *argv[]);
  595. WASM_RUNTIME_API_EXTERN bool
  596. wasm_application_execute_program_func(WASMProgramCommon *program,
  597. const char *name, int32 argc, char *argv[]);
  598. /* See wasm_export.h for description */
  599. WASM_RUNTIME_API_EXTERN void
  600. wasm_runtime_set_exception(WASMModuleInstanceCommon *module,
  601. const char *exception);
  602. /* See wasm_export.h for description */
  603. WASM_RUNTIME_API_EXTERN const char *
  604. wasm_runtime_get_exception(WASMModuleInstanceCommon *module);
  605. /* See wasm_export.h for description */
  606. WASM_RUNTIME_API_EXTERN const char *
  607. wasm_runtime_get_program_exception(WASMProgramCommon *progam);
  608. /* See wasm_export.h for description */
  609. WASM_RUNTIME_API_EXTERN void
  610. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst);
  611. /* Internal API */
  612. void
  613. wasm_runtime_set_custom_data_internal(WASMModuleInstanceCommon *module_inst,
  614. void *custom_data);
  615. /* See wasm_export.h for description */
  616. WASM_RUNTIME_API_EXTERN void
  617. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  618. void *custom_data);
  619. /* See wasm_export.h for description */
  620. WASM_RUNTIME_API_EXTERN void *
  621. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
  622. /* See wasm_export.h for description */
  623. WASM_RUNTIME_API_EXTERN uint32
  624. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  625. void **p_native_addr);
  626. /* See wasm_export.h for description */
  627. WASM_RUNTIME_API_EXTERN void
  628. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr);
  629. /* See wasm_export.h for description */
  630. WASM_RUNTIME_API_EXTERN uint32
  631. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  632. const char *src, uint32 size);
  633. /* See wasm_export.h for description */
  634. WASM_RUNTIME_API_EXTERN bool
  635. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  636. uint32 app_offset, uint32 size);
  637. /* See wasm_export.h for description */
  638. WASM_RUNTIME_API_EXTERN bool
  639. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  640. uint32 app_str_offset);
  641. /* See wasm_export.h for description */
  642. WASM_RUNTIME_API_EXTERN bool
  643. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  644. void *native_ptr, uint32 size);
  645. /* See wasm_export.h for description */
  646. WASM_RUNTIME_API_EXTERN void *
  647. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  648. uint32 app_offset);
  649. /* See wasm_export.h for description */
  650. WASM_RUNTIME_API_EXTERN uint32
  651. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  652. void *native_ptr);
  653. /* See wasm_export.h for description */
  654. WASM_RUNTIME_API_EXTERN bool
  655. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  656. uint32 app_offset, uint32 *p_app_start_offset,
  657. uint32 *p_app_end_offset);
  658. /* See wasm_export.h for description */
  659. WASM_RUNTIME_API_EXTERN bool
  660. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  661. uint8 *native_ptr,
  662. uint8 **p_native_start_addr,
  663. uint8 **p_native_end_addr);
  664. uint32
  665. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst);
  666. void
  667. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  668. uint32 temp_ret);
  669. uint32
  670. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst);
  671. void
  672. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  673. uint32 llvm_stack);
  674. WASM_RUNTIME_API_EXTERN void
  675. wasm_runtime_set_module_reader(const module_reader reader,
  676. const module_destroyer destroyer);
  677. module_reader
  678. wasm_runtime_get_module_reader();
  679. module_destroyer
  680. wasm_runtime_get_module_destroyer();
  681. #if WASM_ENABLE_MULTI_MODULE != 0
  682. bool
  683. wasm_runtime_register_module_internal(const char *module_name,
  684. WASMModuleCommon *module,
  685. uint8 *orig_file_buf,
  686. uint32 orig_file_buf_size,
  687. char *error_buf, uint32 error_buf_size);
  688. void
  689. wasm_runtime_unregister_module(const WASMModuleCommon *module);
  690. bool
  691. wasm_runtime_is_module_registered(const char *module_name);
  692. bool
  693. wasm_runtime_add_loading_module(const char *module_name, char *error_buf,
  694. uint32 error_buf_size);
  695. void
  696. wasm_runtime_delete_loading_module(const char *module_name);
  697. bool
  698. wasm_runtime_is_loading_module(const char *module_name);
  699. void
  700. wasm_runtime_destroy_loading_module_list();
  701. #endif /* WASM_ENALBE_MULTI_MODULE */
  702. bool
  703. wasm_runtime_is_built_in_module(const char *module_name);
  704. bool
  705. wasm_runtime_is_built_in_module_new(WASMRuntime * runtime, const ConstStrDescription *module_name);
  706. #if WASM_ENABLE_THREAD_MGR != 0
  707. bool
  708. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset,
  709. uint32 *size);
  710. bool
  711. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env, uint32 start_offset,
  712. uint32 size);
  713. #endif
  714. #if WASM_ENABLE_LIBC_WASI != 0
  715. WASM_RUNTIME_API_EXTERN void
  716. wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
  717. uint32 dir_count, const char *map_dir_list[],
  718. uint32 map_dir_count, const char *env_list[],
  719. uint32 env_count, char *argv[], int argc,
  720. int stdinfd, int stdoutfd, int stderrfd);
  721. /* See wasm_export.h for description */
  722. WASM_RUNTIME_API_EXTERN void
  723. wasm_runtime_set_wasi_args(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. /* See wasm_export.h for description */
  728. WASM_RUNTIME_API_EXTERN bool
  729. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
  730. /* See wasm_export.h for description */
  731. WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
  732. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
  733. bool
  734. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  735. const char *dir_list[], uint32 dir_count,
  736. const char *map_dir_list[], uint32 map_dir_count,
  737. const char *env[], uint32 env_count, char *argv[],
  738. uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
  739. char *error_buf, uint32 error_buf_size);
  740. void
  741. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
  742. void
  743. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  744. WASIContext *wasi_ctx);
  745. WASIContext *
  746. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
  747. #endif /* end of WASM_ENABLE_LIBC_WASI */
  748. #if WASM_ENABLE_REF_TYPES != 0
  749. /* See wasm_export.h for description */
  750. WASM_RUNTIME_API_EXTERN bool
  751. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst, void *extern_obj,
  752. uint32 *p_externref_idx);
  753. /* See wasm_export.h for description */
  754. WASM_RUNTIME_API_EXTERN bool
  755. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj);
  756. /* See wasm_export.h for description */
  757. WASM_RUNTIME_API_EXTERN bool
  758. wasm_externref_retain(uint32 externref_idx);
  759. /**
  760. * Reclaim the externref objects/indexes which are not used by
  761. * module instance
  762. */
  763. void
  764. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst);
  765. /**
  766. * Cleanup the externref objects/indexes of the module instance
  767. */
  768. void
  769. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst);
  770. #endif /* end of WASM_ENABLE_REF_TYPES */
  771. /* Get module of the current exec_env */
  772. WASMModuleCommon *
  773. wasm_exec_env_get_module(WASMExecEnv *exec_env);
  774. /**
  775. * Enlarge wasm memory data space.
  776. *
  777. * @param module the wasm module instance
  778. * @param inc_page_count denote the page number to increase
  779. * @return return true if enlarge successfully, false otherwise
  780. */
  781. bool
  782. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
  783. uint32 inc_page_count);
  784. /* See wasm_export.h for description */
  785. WASM_RUNTIME_API_EXTERN bool
  786. wasm_runtime_register_natives(const char *module_name,
  787. NativeSymbol *native_symbols,
  788. uint32 n_native_symbols);
  789. /* See wasm_export.h for description */
  790. WASM_RUNTIME_API_EXTERN bool
  791. wasm_runtime_register_natives_raw(const char *module_name,
  792. NativeSymbol *native_symbols,
  793. uint32 n_native_symbols);
  794. bool
  795. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  796. const WASMType *func_type, const char *signature,
  797. void *attachment, uint32 *argv, uint32 argc,
  798. uint32 *ret);
  799. bool
  800. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  801. const WASMType *func_type, const char *signature,
  802. void *attachment, uint32 *argv, uint32 argc,
  803. uint32 *ret);
  804. void
  805. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2);
  806. void
  807. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module);
  808. void
  809. wasm_runtime_dump_module_inst_mem_consumption(
  810. const WASMModuleInstanceCommon *module_inst);
  811. void
  812. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env);
  813. #if WASM_ENABLE_REF_TYPES != 0
  814. void
  815. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  816. WASMFunctionInstanceCommon *function);
  817. void
  818. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  819. WASMFunctionInstanceCommon *function,
  820. bool ret, uint32 *argv);
  821. #endif
  822. bool
  823. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  824. const WASMExport *export_, WASMType **out);
  825. bool
  826. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  827. const WASMExport *export_,
  828. uint8 *out_val_type, bool *out_mutability);
  829. bool
  830. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  831. const WASMExport *export_,
  832. uint32 *out_min_page, uint32 *out_max_page);
  833. bool
  834. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  835. const WASMExport *export_,
  836. uint8 *out_elem_type, uint32 *out_min_size,
  837. uint32 *out_max_size);
  838. bool
  839. wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
  840. void *func_ptr, WASMType *func_type,
  841. uint32 argc, uint32 *argv, bool with_env,
  842. void *wasm_c_api_env);
  843. void
  844. wasm_runtime_show_app_heap_corrupted_prompt();
  845. #ifdef __cplusplus
  846. }
  847. #endif
  848. #endif /* end of _WASM_COMMON_H */