wasm.h 15 KB

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