wasm.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. typedef struct BrTableCache {
  286. struct BrTableCache *next;
  287. /* Address of br_table opcode */
  288. uint8 *br_table_op_addr;
  289. uint32 br_count;
  290. uint32 br_depths[1];
  291. } BrTableCache;
  292. #if WASM_ENABLE_DEBUG_INTERP != 0
  293. typedef struct WASMFastOPCodeNode {
  294. struct WASMFastOPCodeNode *next;
  295. uint64 offset;
  296. uint8 orig_op;
  297. } WASMFastOPCodeNode;
  298. #endif
  299. struct WASMModule {
  300. /* Module type, for module loaded from WASM bytecode binary,
  301. this field is Wasm_Module_Bytecode;
  302. for module loaded from AOT file, this field is
  303. Wasm_Module_AoT, and this structure should be treated as
  304. AOTModule structure. */
  305. uint32 module_type;
  306. uint32 type_count;
  307. uint32 import_count;
  308. uint32 function_count;
  309. uint32 table_count;
  310. uint32 memory_count;
  311. uint32 global_count;
  312. uint32 export_count;
  313. uint32 table_seg_count;
  314. /* data seg count read from data segment section */
  315. uint32 data_seg_count;
  316. #if WASM_ENABLE_BULK_MEMORY != 0
  317. /* data count read from datacount section */
  318. uint32 data_seg_count1;
  319. #endif
  320. uint32 import_function_count;
  321. uint32 import_table_count;
  322. uint32 import_memory_count;
  323. uint32 import_global_count;
  324. WASMImport *import_functions;
  325. WASMImport *import_tables;
  326. WASMImport *import_memories;
  327. WASMImport *import_globals;
  328. WASMType **types;
  329. WASMImport *imports;
  330. WASMFunction **functions;
  331. WASMTable *tables;
  332. WASMMemory *memories;
  333. WASMGlobal *globals;
  334. WASMExport *exports;
  335. WASMTableSeg *table_segments;
  336. WASMDataSeg **data_segments;
  337. uint32 start_function;
  338. /* the index of auxiliary __data_end global,
  339. -1 means unexported */
  340. uint32 aux_data_end_global_index;
  341. /* auxiliary __data_end exported by wasm app */
  342. uint32 aux_data_end;
  343. /* the index of auxiliary __heap_base global,
  344. -1 means unexported */
  345. uint32 aux_heap_base_global_index;
  346. /* auxiliary __heap_base exported by wasm app */
  347. uint32 aux_heap_base;
  348. /* the index of auxiliary stack top global,
  349. -1 means unexported */
  350. uint32 aux_stack_top_global_index;
  351. /* auxiliary stack bottom resolved */
  352. uint32 aux_stack_bottom;
  353. /* auxiliary stack size resolved */
  354. uint32 aux_stack_size;
  355. /* the index of malloc/free function,
  356. -1 means unexported */
  357. uint32 malloc_function;
  358. uint32 free_function;
  359. /* the index of __retain function,
  360. -1 means unexported */
  361. uint32 retain_function;
  362. /* Whether there is possible memory grow, e.g. memory.grow opcode */
  363. bool possible_memory_grow;
  364. StringList const_str_list;
  365. #if WASM_ENABLE_FAST_INTERP == 0
  366. bh_list br_table_cache_list_head;
  367. bh_list *br_table_cache_list;
  368. #endif
  369. #if WASM_ENABLE_LIBC_WASI != 0
  370. WASIArguments wasi_args;
  371. bool import_wasi_api;
  372. #endif
  373. #if WASM_ENABLE_MULTI_MODULE != 0
  374. /* TODO: add mutex for mutli-thread? */
  375. bh_list import_module_list_head;
  376. bh_list *import_module_list;
  377. #endif
  378. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  379. bh_list fast_opcode_list;
  380. uint8 *buf_code;
  381. uint8 *load_addr;
  382. uint64 load_size;
  383. uint64 buf_code_size;
  384. #endif
  385. #if WASM_ENABLE_DEBUG_INTERP != 0
  386. /**
  387. * Count how many instances reference this module. When source
  388. * debugging feature enabled, the debugger may modify the code
  389. * section of the module, so we need to report a warning if user
  390. * create several instances based on the same module
  391. *
  392. * Sub_instances created by lib-pthread or spawn API will not
  393. * influence or check the ref count
  394. */
  395. uint32 ref_count;
  396. korp_mutex ref_count_lock;
  397. #endif
  398. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  399. const uint8 *name_section_buf;
  400. const uint8 *name_section_buf_end;
  401. #endif
  402. };
  403. typedef struct BlockType {
  404. /* Block type may be expressed in one of two forms:
  405. * either by the type of the single return value or
  406. * by a type index of module.
  407. */
  408. union {
  409. uint8 value_type;
  410. WASMType *type;
  411. } u;
  412. bool is_value_type;
  413. } BlockType;
  414. typedef struct WASMBranchBlock {
  415. uint8 *begin_addr;
  416. uint8 *target_addr;
  417. uint32 *frame_sp;
  418. uint32 cell_num;
  419. } WASMBranchBlock;
  420. /* Execution environment, e.g. stack info */
  421. /**
  422. * Align an unsigned value on a alignment boundary.
  423. *
  424. * @param v the value to be aligned
  425. * @param b the alignment boundary (2, 4, 8, ...)
  426. *
  427. * @return the aligned value
  428. */
  429. inline static unsigned
  430. align_uint(unsigned v, unsigned b)
  431. {
  432. unsigned m = b - 1;
  433. return (v + m) & ~m;
  434. }
  435. /**
  436. * Return the hash value of c string.
  437. */
  438. inline static uint32
  439. wasm_string_hash(const char *str)
  440. {
  441. unsigned h = (unsigned)strlen(str);
  442. const uint8 *p = (uint8 *)str;
  443. const uint8 *end = p + h;
  444. while (p != end)
  445. h = ((h << 5) - h) + *p++;
  446. return h;
  447. }
  448. /**
  449. * Whether two c strings are equal.
  450. */
  451. inline static bool
  452. wasm_string_equal(const char *s1, const char *s2)
  453. {
  454. return strcmp(s1, s2) == 0 ? true : false;
  455. }
  456. /**
  457. * Return the byte size of value type.
  458. *
  459. */
  460. inline static uint32
  461. wasm_value_type_size(uint8 value_type)
  462. {
  463. switch (value_type) {
  464. case VALUE_TYPE_I32:
  465. case VALUE_TYPE_F32:
  466. #if WASM_ENABLE_REF_TYPES != 0
  467. case VALUE_TYPE_FUNCREF:
  468. case VALUE_TYPE_EXTERNREF:
  469. #endif
  470. return sizeof(int32);
  471. case VALUE_TYPE_I64:
  472. case VALUE_TYPE_F64:
  473. return sizeof(int64);
  474. #if WASM_ENABLE_SIMD != 0
  475. case VALUE_TYPE_V128:
  476. return sizeof(int64) * 2;
  477. #endif
  478. case VALUE_TYPE_VOID:
  479. return 0;
  480. default:
  481. bh_assert(0);
  482. }
  483. return 0;
  484. }
  485. inline static uint16
  486. wasm_value_type_cell_num(uint8 value_type)
  487. {
  488. return wasm_value_type_size(value_type) / 4;
  489. }
  490. inline static uint32
  491. wasm_get_cell_num(const uint8 *types, uint32 type_count)
  492. {
  493. uint32 cell_num = 0;
  494. uint32 i;
  495. for (i = 0; i < type_count; i++)
  496. cell_num += wasm_value_type_cell_num(types[i]);
  497. return cell_num;
  498. }
  499. #if WASM_ENABLE_REF_TYPES != 0
  500. inline static uint16
  501. wasm_value_type_cell_num_outside(uint8 value_type)
  502. {
  503. if (VALUE_TYPE_EXTERNREF == value_type) {
  504. return sizeof(uintptr_t) / sizeof(uint32);
  505. }
  506. else {
  507. return wasm_value_type_cell_num(value_type);
  508. }
  509. }
  510. #endif
  511. inline static bool
  512. wasm_type_equal(const WASMType *type1, const WASMType *type2)
  513. {
  514. return (type1->param_count == type2->param_count
  515. && type1->result_count == type2->result_count
  516. && memcmp(type1->types, type2->types,
  517. (uint32)(type1->param_count + type1->result_count))
  518. == 0)
  519. ? true
  520. : false;
  521. }
  522. inline static uint32
  523. wasm_get_smallest_type_idx(WASMType **types, uint32 type_count,
  524. uint32 cur_type_idx)
  525. {
  526. uint32 i;
  527. for (i = 0; i < cur_type_idx; i++) {
  528. if (wasm_type_equal(types[cur_type_idx], types[i]))
  529. return i;
  530. }
  531. return cur_type_idx;
  532. }
  533. static inline uint32
  534. block_type_get_param_types(BlockType *block_type, uint8 **p_param_types)
  535. {
  536. uint32 param_count = 0;
  537. if (!block_type->is_value_type) {
  538. WASMType *wasm_type = block_type->u.type;
  539. *p_param_types = wasm_type->types;
  540. param_count = wasm_type->param_count;
  541. }
  542. else {
  543. *p_param_types = NULL;
  544. param_count = 0;
  545. }
  546. return param_count;
  547. }
  548. static inline uint32
  549. block_type_get_result_types(BlockType *block_type, uint8 **p_result_types)
  550. {
  551. uint32 result_count = 0;
  552. if (block_type->is_value_type) {
  553. if (block_type->u.value_type != VALUE_TYPE_VOID) {
  554. *p_result_types = &block_type->u.value_type;
  555. result_count = 1;
  556. }
  557. }
  558. else {
  559. WASMType *wasm_type = block_type->u.type;
  560. *p_result_types = wasm_type->types + wasm_type->param_count;
  561. result_count = wasm_type->result_count;
  562. }
  563. return result_count;
  564. }
  565. #ifdef __cplusplus
  566. } /* end of extern "C" */
  567. #endif
  568. #endif /* end of _WASM_H_ */