wasm.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_H_
  6. #define _WASM_H_
  7. #include "bh_platform.h"
  8. #include "bh_hashmap.h"
  9. #include "bh_assert.h"
  10. #include "wasm_multimodules_program.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /** Value Type */
  15. #define VALUE_TYPE_I32 0x7F
  16. #define VALUE_TYPE_I64 0X7E
  17. #define VALUE_TYPE_F32 0x7D
  18. #define VALUE_TYPE_F64 0x7C
  19. #define VALUE_TYPE_V128 0x7B
  20. #define VALUE_TYPE_FUNCREF 0x70
  21. #define VALUE_TYPE_EXTERNREF 0x6F
  22. #define VALUE_TYPE_VOID 0x40
  23. /* Used by AOT */
  24. #define VALUE_TYPE_I1 0x41
  25. /* Used by loader to represent any type of i32/i64/f32/f64 */
  26. #define VALUE_TYPE_ANY 0x42
  27. #define DEFAULT_NUM_BYTES_PER_PAGE 65536
  28. #define NULL_REF (0xFFFFFFFF)
  29. #define TABLE_MAX_SIZE (1024)
  30. #define INIT_EXPR_TYPE_I32_CONST 0x41
  31. #define INIT_EXPR_TYPE_I64_CONST 0x42
  32. #define INIT_EXPR_TYPE_F32_CONST 0x43
  33. #define INIT_EXPR_TYPE_F64_CONST 0x44
  34. #define INIT_EXPR_TYPE_V128_CONST 0xFD
  35. /* = WASM_OP_REF_FUNC */
  36. #define INIT_EXPR_TYPE_FUNCREF_CONST 0xD2
  37. /* = WASM_OP_REF_NULL */
  38. #define INIT_EXPR_TYPE_REFNULL_CONST 0xD0
  39. #define INIT_EXPR_TYPE_GET_GLOBAL 0x23
  40. #define INIT_EXPR_TYPE_ERROR 0xff
  41. #define WASM_MAGIC_NUMBER 0x6d736100
  42. #define WASM_CURRENT_VERSION 1
  43. #define SECTION_TYPE_USER 0
  44. #define SECTION_TYPE_TYPE 1
  45. #define SECTION_TYPE_IMPORT 2
  46. #define SECTION_TYPE_FUNC 3
  47. #define SECTION_TYPE_TABLE 4
  48. #define SECTION_TYPE_MEMORY 5
  49. #define SECTION_TYPE_GLOBAL 6
  50. #define SECTION_TYPE_EXPORT 7
  51. #define SECTION_TYPE_START 8
  52. #define SECTION_TYPE_ELEM 9
  53. #define SECTION_TYPE_CODE 10
  54. #define SECTION_TYPE_DATA 11
  55. #if WASM_ENABLE_BULK_MEMORY != 0
  56. #define SECTION_TYPE_DATACOUNT 12
  57. #endif
  58. #define SUB_SECTION_TYPE_MODULE 0
  59. #define SUB_SECTION_TYPE_FUNC 1
  60. #define SUB_SECTION_TYPE_LOCAL 2
  61. #define IMPORT_KIND_FUNC 0
  62. #define IMPORT_KIND_TABLE 1
  63. #define IMPORT_KIND_MEMORY 2
  64. #define IMPORT_KIND_GLOBAL 3
  65. #define EXPORT_KIND_FUNC 0
  66. #define EXPORT_KIND_TABLE 1
  67. #define EXPORT_KIND_MEMORY 2
  68. #define EXPORT_KIND_GLOBAL 3
  69. #define LABEL_TYPE_BLOCK 0
  70. #define LABEL_TYPE_LOOP 1
  71. #define LABEL_TYPE_IF 2
  72. #define LABEL_TYPE_FUNCTION 3
  73. typedef struct WASMModule WASMModule;
  74. typedef struct WASMFunction WASMFunction;
  75. typedef struct WASMGlobal WASMGlobal;
  76. typedef union V128 {
  77. int8 i8x16[16];
  78. int16 i16x8[8];
  79. int32 i32x8[4];
  80. int64 i64x2[2];
  81. float32 f32x4[4];
  82. float64 f64x2[2];
  83. } V128;
  84. typedef union WASMValue {
  85. int32 i32;
  86. uint32 u32;
  87. uint32 global_index;
  88. uint32 ref_index;
  89. int64 i64;
  90. uint64 u64;
  91. float32 f32;
  92. float64 f64;
  93. uintptr_t addr;
  94. V128 v128;
  95. } WASMValue;
  96. typedef struct InitializerExpression {
  97. /* type of INIT_EXPR_TYPE_XXX */
  98. /* it actually is instr, in some places, requires constant only */
  99. uint8 init_expr_type;
  100. WASMValue u;
  101. } InitializerExpression;
  102. typedef struct WASMType {
  103. uint16 param_count;
  104. uint16 result_count;
  105. uint16 param_cell_num;
  106. uint16 ret_cell_num;
  107. /* types of params and results */
  108. uint8 types[1];
  109. } WASMType;
  110. typedef struct WASMTable {
  111. uint8 elem_type;
  112. uint32 flags;
  113. uint32 init_size;
  114. /* specified if (flags & 1), else it is 0x10000 */
  115. uint32 max_size;
  116. bool possible_grow;
  117. } WASMTable;
  118. typedef struct WASMMemory {
  119. uint32 flags;
  120. uint32 num_bytes_per_page;
  121. uint32 init_page_count;
  122. uint32 max_page_count;
  123. } WASMMemory;
  124. typedef struct WASMTableImport {
  125. const char *module_name;
  126. char *field_name;
  127. uint8 elem_type;
  128. uint32 flags;
  129. uint32 init_size;
  130. /* specified if (flags & 1), else it is 0x10000 */
  131. uint32 max_size;
  132. bool possible_grow;
  133. #if WASM_ENABLE_MULTI_MODULE != 0
  134. WASMModule *import_module;
  135. WASMTable *import_table_linked;
  136. #endif
  137. } WASMTableImport;
  138. typedef struct WASMMemoryImport {
  139. const char *module_name;
  140. char *field_name;
  141. uint32 flags;
  142. uint32 num_bytes_per_page;
  143. uint32 init_page_count;
  144. uint32 max_page_count;
  145. #if WASM_ENABLE_MULTI_MODULE != 0
  146. WASMModule *import_module;
  147. WASMMemory *import_memory_linked;
  148. #endif
  149. } WASMMemoryImport;
  150. typedef struct WASMFunctionImport {
  151. const ConstStrDescription *module_name;
  152. const ConstStrDescription *field_name;
  153. /* function type */
  154. WASMType *func_type;
  155. /* native function pointer after linked */
  156. void *func_ptr_linked;
  157. /* signature from registered native symbols */
  158. const char *signature;
  159. /* attachment */
  160. void *attachment;
  161. bool call_conv_raw;
  162. #if WASM_ENABLE_MULTI_MODULE != 0
  163. WASMModule *import_module;
  164. WASMFunction *import_func_linked;
  165. #endif
  166. bool call_conv_wasm_c_api;
  167. bool wasm_c_api_with_env;
  168. } WASMFunctionImport;
  169. typedef struct WASMGlobalImport {
  170. const ConstStrDescription *module_name;
  171. const ConstStrDescription *field_name;
  172. uint8 type;
  173. bool is_mutable;
  174. /* global data after linked */
  175. WASMValue global_data_linked;
  176. bool is_linked;
  177. #if WASM_ENABLE_MULTI_MODULE != 0
  178. /* imported function pointer after linked */
  179. /* TODO: remove if not needed */
  180. WASMModule *import_module;
  181. WASMGlobal *import_global_linked;
  182. #endif
  183. } WASMGlobalImport;
  184. typedef struct WASMImport {
  185. uint8 kind;
  186. union {
  187. WASMFunctionImport function;
  188. WASMTableImport table;
  189. WASMMemoryImport memory;
  190. WASMGlobalImport global;
  191. struct {
  192. const ConstStrDescription *module_name;
  193. const ConstStrDescription *field_name;
  194. } names;
  195. } u;
  196. } WASMImport;
  197. struct WASMFunction {
  198. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  199. const char *field_name;
  200. #endif
  201. /* the type of function */
  202. WASMType *func_type;
  203. uint32 local_count;
  204. uint8 *local_types;
  205. /* cell num of local variables */
  206. uint16 local_cell_num;
  207. /* offset of each local, including function parameters
  208. and local variables */
  209. uint16 *local_offsets;
  210. uint32 max_stack_cell_num;
  211. uint32 max_block_num;
  212. /* Whether function has opcode memory.grow */
  213. bool has_op_memory_grow;
  214. /* Whether function has opcode call or
  215. call_indirect */
  216. bool has_op_func_call;
  217. uint32 code_size;
  218. uint8 *code;
  219. #if WASM_ENABLE_FAST_INTERP != 0
  220. uint32 code_compiled_size;
  221. uint8 *code_compiled;
  222. uint8 *consts;
  223. uint32 const_cell_num;
  224. #endif
  225. };
  226. struct WASMGlobal {
  227. uint8 type;
  228. bool is_mutable;
  229. InitializerExpression init_expr;
  230. };
  231. typedef struct WASMExport {
  232. const char *name;
  233. uint8 kind;
  234. uint32 index;
  235. } WASMExport;
  236. typedef struct WASMTableSeg {
  237. /* 0 to 7 */
  238. uint32 mode;
  239. /* funcref or externref, elemkind will be considered as funcref */
  240. uint32 elem_type;
  241. bool is_dropped;
  242. /* optional, only for active */
  243. uint32 table_index;
  244. InitializerExpression base_offset;
  245. uint32 function_count;
  246. uint32 *func_indexes;
  247. } WASMTableSeg;
  248. typedef struct WASMDataSeg {
  249. uint32 memory_index;
  250. InitializerExpression base_offset;
  251. uint32 data_length;
  252. #if WASM_ENABLE_BULK_MEMORY != 0
  253. bool is_passive;
  254. #endif
  255. uint8 *data;
  256. } WASMDataSeg;
  257. typedef struct WASMDylibEntry {
  258. uint32 dylib_name_len;
  259. const char * dylib_name_str;
  260. } WASMDylibEntry;
  261. typedef struct WASMDylinkSection {
  262. uint32 memory_size;
  263. uint32 memory_alignment;
  264. uint32 table_size;
  265. uint32 table_alignment;
  266. uint32 needed_dylib_count;
  267. const ConstStrDescription * needed_dylib_entries[0];
  268. } WASMDylinkSection;
  269. typedef struct BlockAddr {
  270. const uint8 *start_addr;
  271. uint8 *else_addr;
  272. uint8 *end_addr;
  273. } BlockAddr;
  274. #if WASM_ENABLE_LIBC_WASI != 0
  275. typedef struct WASIArguments {
  276. const char **dir_list;
  277. uint32 dir_count;
  278. const char **map_dir_list;
  279. uint32 map_dir_count;
  280. const char **env;
  281. uint32 env_count;
  282. char **argv;
  283. uint32 argc;
  284. int stdio[3];
  285. } WASIArguments;
  286. #endif
  287. typedef struct StringNode {
  288. struct StringNode *next;
  289. char *str;
  290. } StringNode, *StringList;
  291. #if WASM_ENABLE_DEBUG_INTERP != 0
  292. typedef struct WASMFastOPCodeNode {
  293. struct WASMFastOPCodeNode *next;
  294. uint64 offset;
  295. uint8 orig_op;
  296. } WASMFastOPCodeNode;
  297. #endif
  298. struct WASMModule {
  299. /* Module type, for module loaded from WASM bytecode binary,
  300. this field is Wasm_Module_Bytecode;
  301. for module loaded from AOT file, this field is
  302. Wasm_Module_AoT, and this structure should be treated as
  303. AOTModule structure. */
  304. uint32 module_type;
  305. uint32 type_count;
  306. uint32 import_count;
  307. uint32 function_count;
  308. uint32 table_count;
  309. uint32 memory_count;
  310. uint32 global_count;
  311. uint32 export_count;
  312. uint32 table_seg_count;
  313. /* data seg count read from data segment section */
  314. uint32 data_seg_count;
  315. #if WASM_ENABLE_BULK_MEMORY != 0
  316. /* data count read from datacount section */
  317. uint32 data_seg_count1;
  318. #endif
  319. uint32 import_function_count;
  320. uint32 import_table_count;
  321. uint32 import_memory_count;
  322. uint32 import_global_count;
  323. WASMImport *import_functions;
  324. WASMImport *import_tables;
  325. WASMImport *import_memories;
  326. WASMImport *import_globals;
  327. uint32 export_func_count;
  328. uint32 export_global_count;
  329. uint32 export_mem_count;
  330. uint32 export_tab_count;
  331. WASMType **types;
  332. WASMImport *imports;
  333. WASMFunction **functions;
  334. WASMTable *tables;
  335. WASMMemory *memories;
  336. WASMGlobal *globals;
  337. WASMExport *exports;
  338. WASMTableSeg *table_segments;
  339. WASMDataSeg *data_segments;
  340. WASMDylinkSection *dylink_section;
  341. uint32 start_function;
  342. /* the index of auxiliary __data_end global,
  343. -1 means unexported */
  344. uint32 aux_data_end_global_index;
  345. /* auxiliary __data_end exported by wasm app */
  346. uint32 aux_data_end;
  347. /* the index of auxiliary __heap_base global,
  348. -1 means unexported */
  349. uint32 aux_heap_base_global_index;
  350. /* auxiliary __heap_base exported by wasm app */
  351. uint32 aux_heap_base;
  352. /* the index of auxiliary stack top global,
  353. -1 means unexported */
  354. uint32 aux_stack_top_global_index;
  355. /* auxiliary stack bottom resolved */
  356. uint32 aux_stack_bottom;
  357. /* auxiliary stack size resolved */
  358. uint32 aux_stack_size;
  359. /* the index of malloc/free function,
  360. -1 means unexported */
  361. uint32 malloc_function;
  362. uint32 free_function;
  363. /* the index of __retain function,
  364. -1 means unexported */
  365. uint32 retain_function;
  366. /* Whether there is possible memory grow, e.g. memory.grow opcode */
  367. bool possible_memory_grow;
  368. WASMRuntime * runtime;
  369. const uint8 * file_buf;
  370. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  371. /* directly implicit dependency modules of current module, e.g. dylink section */
  372. HashMap *implicit_dependency_modules_hmap;
  373. uint32 ref_cnt;
  374. #endif
  375. const ConstStrDescription * module_name;
  376. #if WASM_ENABLE_LIBC_WASI != 0
  377. WASIArguments wasi_args;
  378. bool is_wasi_module;
  379. #endif
  380. #if WASM_ENABLE_MULTI_MODULE != 0
  381. /* TODO: add mutex for mutli-thread? */
  382. bh_list import_module_list_head;
  383. bh_list *import_module_list;
  384. #endif
  385. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  386. bh_list fast_opcode_list;
  387. uint8 *buf_code;
  388. uint8 *load_addr;
  389. uint64 load_size;
  390. uint64 buf_code_size;
  391. #endif
  392. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  393. const uint8 *name_section_buf;
  394. const uint8 *name_section_buf_end;
  395. #endif
  396. };
  397. typedef struct BlockType {
  398. /* Block type may be expressed in one of two forms:
  399. * either by the type of the single return value or
  400. * by a type index of module.
  401. */
  402. union {
  403. uint8 value_type;
  404. WASMType *type;
  405. } u;
  406. bool is_value_type;
  407. } BlockType;
  408. typedef struct WASMBranchBlock {
  409. uint8 *begin_addr;
  410. uint8 *target_addr;
  411. uint32 *frame_sp;
  412. uint32 cell_num;
  413. } WASMBranchBlock;
  414. /* Execution environment, e.g. stack info */
  415. /**
  416. * Align an unsigned value on a alignment boundary.
  417. *
  418. * @param v the value to be aligned
  419. * @param b the alignment boundary (2, 4, 8, ...)
  420. *
  421. * @return the aligned value
  422. */
  423. inline static unsigned
  424. align_uint(unsigned v, unsigned b)
  425. {
  426. unsigned m = b - 1;
  427. return (v + m) & ~m;
  428. }
  429. /**
  430. * Return the hash value of c string.
  431. */
  432. inline static uint32
  433. wasm_string_hash(const char *str)
  434. {
  435. unsigned h = (unsigned)strlen(str);
  436. const uint8 *p = (uint8 *)str;
  437. const uint8 *end = p + h;
  438. while (p != end)
  439. h = ((h << 5) - h) + *p++;
  440. return h;
  441. }
  442. /**
  443. * Whether two c strings are equal.
  444. */
  445. inline static bool
  446. wasm_string_equal(const char *s1, const char *s2)
  447. {
  448. return strcmp(s1, s2) == 0 ? true : false;
  449. }
  450. /**
  451. * Return the byte size of value type.
  452. *
  453. */
  454. inline static uint32
  455. wasm_value_type_size(uint8 value_type)
  456. {
  457. switch (value_type) {
  458. case VALUE_TYPE_I32:
  459. case VALUE_TYPE_F32:
  460. #if WASM_ENABLE_REF_TYPES != 0
  461. case VALUE_TYPE_FUNCREF:
  462. case VALUE_TYPE_EXTERNREF:
  463. #endif
  464. return sizeof(int32);
  465. case VALUE_TYPE_I64:
  466. case VALUE_TYPE_F64:
  467. return sizeof(int64);
  468. #if WASM_ENABLE_SIMD != 0
  469. case VALUE_TYPE_V128:
  470. return sizeof(int64) * 2;
  471. #endif
  472. default:
  473. bh_assert(0);
  474. }
  475. return 0;
  476. }
  477. inline static uint16
  478. wasm_value_type_cell_num(uint8 value_type)
  479. {
  480. if (value_type == VALUE_TYPE_VOID)
  481. return 0;
  482. else if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32
  483. #if WASM_ENABLE_REF_TYPES != 0
  484. || value_type == VALUE_TYPE_FUNCREF
  485. || value_type == VALUE_TYPE_EXTERNREF
  486. #endif
  487. )
  488. return 1;
  489. else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64)
  490. return 2;
  491. #if WASM_ENABLE_SIMD != 0
  492. else if (value_type == VALUE_TYPE_V128)
  493. return 4;
  494. #endif
  495. else {
  496. bh_assert(0);
  497. }
  498. return 0;
  499. }
  500. inline static uint32
  501. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  502. {
  503. uint32 cell_num = 0;
  504. uint32 i;
  505. for (i = 0; i < type_count; i++)
  506. cell_num += wasm_value_type_cell_num(types[i]);
  507. return cell_num;
  508. }
  509. inline static bool
  510. wasm_type_equal(const WASMType *type1, const WASMType *type2)
  511. {
  512. return (type1->param_count == type2->param_count
  513. && type1->result_count == type2->result_count
  514. && memcmp(type1->types, type2->types,
  515. (uint32)(type1->param_count + type1->result_count))
  516. == 0)
  517. ? true
  518. : false;
  519. }
  520. inline static uint32
  521. wasm_get_smallest_type_idx(WASMType **types, uint32 type_count,
  522. uint32 cur_type_idx)
  523. {
  524. uint32 i;
  525. for (i = 0; i < cur_type_idx; i++) {
  526. if (wasm_type_equal(types[cur_type_idx], types[i]))
  527. return i;
  528. }
  529. return cur_type_idx;
  530. }
  531. static inline uint32
  532. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
  533. {
  534. uint32 param_count = 0;
  535. if (!block_type->is_value_type) {
  536. WASMType *wasm_type = block_type->u.type;
  537. *p_param_types = wasm_type->types;
  538. param_count = wasm_type->param_count;
  539. }
  540. else {
  541. *p_param_types = NULL;
  542. param_count = 0;
  543. }
  544. return param_count;
  545. }
  546. static inline uint32
  547. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
  548. {
  549. uint32 result_count = 0;
  550. if (block_type->is_value_type) {
  551. if (block_type->u.value_type != VALUE_TYPE_VOID) {
  552. *p_result_types = &block_type->u.value_type;
  553. result_count = 1;
  554. }
  555. }
  556. else {
  557. WASMType *wasm_type = block_type->u.type;
  558. *p_result_types = wasm_type->types + wasm_type->param_count;
  559. result_count = wasm_type->result_count;
  560. }
  561. return result_count;
  562. }
  563. #ifdef __cplusplus
  564. } /* end of extern "C" */
  565. #endif
  566. #endif /* end of _WASM_H_ */