Debian Codegraph @

tests/test_wasm.c

raw symbols SCIP
1 /* Capstone Disassembler Engine */cxx . . $ `<file>/tests/test_wasm.c`/
2 /* By Spike <[email protected]>, 2018 */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include <capstone/platform.h>cxx . . $ `<file>/include/capstone/platform.h`/
8 #include <capstone/capstone.h>cxx . . $ `<file>/include/capstone/capstone.h`/
9
10 static cshcxx . . $ csh# handlecxx . . $ handle.;
11
12 struct platformcxx . . $ platform# {
13 cs_archcxx . . $ cs_arch# archcxx . . $ platform#arch.;
14 cs_modecxx . . $ cs_mode# modecxx . . $ platform#mode.;
15 unsigned char *codecxx . . $ platform#code.;
16 size_t sizecxx . . $ platform#size.;
17 const char *commentcxx . . $ platform#comment.;
18 };
19
20 static void print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).(const char *comment, unsigned char *str, size_t len)
21 {
22 unsigned char *c;
23
24 printfcxx . . $ printf(581fddd72a8db8fc).("%s", comment);
25 for (c = str; c < str + len; c++) {
26 printfcxx . . $ printf(581fddd72a8db8fc).("0x%02x ", *c & 0xff);
27 }
28
29 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
30 }
31
32 static void print_insn_detailcxx . . $ print_insn_detail(2888c3e5b0984e8d).(cshcxx . . $ csh# cs_handle, cs_insncxx . . $ cs_insn# *ins)
33 {
34 cs_wasmcxx . . $ cs_wasm# *wasm;
35
36 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
37 if (ins->detailcxx . . $ cs_insn#detail. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
38 return;
39
40 if (ins->detailcxx . . $ cs_insn#detail.->groups_countcxx . . $ cs_detail#groups_count.) {
41 int j;
42
43 printfcxx . . $ printf(581fddd72a8db8fc).("\tGroups: ");
44 for(j = 0; j < ins->detailcxx . . $ cs_insn#detail.->groups_countcxx . . $ cs_detail#groups_count.; j++) {
45 printfcxx . . $ printf(581fddd72a8db8fc).("%s ", cs_group_namecxx . . $ cs_group_name(efd1348d9ae5172e).(handlecxx . . $ handle., ins->detailcxx . . $ cs_insn#detail.->groupscxx . . $ cs_detail#groups.[j]));
46 }
47 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
48 }
49
50 wasm = &(ins->detailcxx . . $ cs_insn#detail.->wasmcxx . . $ cs_detail#$anonymous_type_0#wasm.);
51
52 if (wasm->op_countcxx . . $ cs_wasm#op_count. > 0) {
53 unsigned int i;
54
55 printfcxx . . $ printf(581fddd72a8db8fc).("\tOperand count: %u\n", wasm->op_countcxx . . $ cs_wasm#op_count.);
56
57 for (i = 0; i < wasm->op_countcxx . . $ cs_wasm#op_count.; i++) {
58 switch (wasm->operandscxx . . $ cs_wasm#operands.[i].typecxx . . $ cs_wasm_op#type.) {
59 default:
60 break;
61 case WASM_OP_INT7cxx . . $ WASM_OP_INT7.:
62 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] type: int7\n", i);
63 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] value: %d\n", i, wasm->operandscxx . . $ cs_wasm#operands.[i].int7cxx . . $ cs_wasm_op#$anonymous_type_0#int7.);
64 break;
65 case WASM_OP_UINT32cxx . . $ WASM_OP_UINT32.:
66 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] type: uint32\n", i);
67 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] value: 0x%x\n", i, wasm->operandscxx . . $ cs_wasm#operands.[i].uint32cxx . . $ cs_wasm_op#$anonymous_type_0#uint32.);
68 break;
69 case WASM_OP_UINT64cxx . . $ WASM_OP_UINT64.:
70 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] type: uint64\n", i);
71 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] value: 0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! "\n", i, wasm->operandscxx . . $ cs_wasm#operands.[i].uint64cxx . . $ cs_wasm_op#$anonymous_type_0#uint64.);
72 break;
73 case WASM_OP_VARUINT32cxx . . $ WASM_OP_VARUINT32.:
74 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] type: varuint32\n", i);
75 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] value: 0x%x\n", i, wasm->operandscxx . . $ cs_wasm#operands.[i].varuint32cxx . . $ cs_wasm_op#$anonymous_type_0#varuint32.);
76 break;
77 case WASM_OP_VARUINT64cxx . . $ WASM_OP_VARUINT64.:
78 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] type: varuint64\n", i);
79 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] value: 0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! "\n", i, wasm->operandscxx . . $ cs_wasm#operands.[i].varuint64cxx . . $ cs_wasm_op#$anonymous_type_0#varuint64.);
80 break;
81 }
82 printfcxx . . $ printf(581fddd72a8db8fc).("\t\tOperand[%u] size: %u\n", i, wasm->operandscxx . . $ cs_wasm#operands.[i].sizecxx . . $ cs_wasm_op#size.);
83 }
84 }
85 }
86
87 static void testcxx . . $ test(d878ad36e69247c7).()
88 {
89 #define WASM_CODEcxx . . $ `tests/test_wasm.c:89:9`! "\x20\x00\x20\x01\x41\x20\x10\xc9\x01\x45\x0b"
90 struct platformcxx . . $ platform# platforms[] = {
91 {
92 CS_ARCH_WASMcxx . . $ CS_ARCH_WASM.,
93 0,
94 (unsigned char *)WASM_CODEcxx . . $ `tests/test_wasm.c:89:9`!,
95 sizeof(WASM_CODEcxx . . $ `tests/test_wasm.c:89:9`!) - 1,
96 "WASM"
97 },
98 };
99
100 uint64_tcxx . . $ uint64_t# address = 0xffff;
101 cs_insncxx . . $ cs_insn# *insn;
102 size_t count;
103 int i;
104
105 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
106 cs_errcxx . . $ cs_err# err = cs_opencxx . . $ cs_open(b2cb849d8ee082cd).(platforms[i].archcxx . . $ platform#arch., platforms[i].modecxx . . $ platform#mode., &handlecxx . . $ handle.);
107 if (err) {
108 printfcxx . . $ printf(581fddd72a8db8fc).("Failed on cs_open() with error returned: %u\n", err);
109 abortcxx . . $ abort(8df32a62017fbbf6).();
110 }
111
112 cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handlecxx . . $ handle., CS_OPT_DETAILcxx . . $ CS_OPT_DETAIL., CS_OPT_ONcxx . . $ CS_OPT_ON.);
113
114 count = cs_disasmcxx . . $ cs_disasm(d552efbd9b22be5).(handlecxx . . $ handle., platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size., address, 0, &insn);
115 if (count) {
116 size_t j;
117 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
118 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
119 print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).("Code: ", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
120 printfcxx . . $ printf(581fddd72a8db8fc).("Disasm:\n");
121
122 for (j = 0; j < count; j++) {
123 printfcxx . . $ printf(581fddd72a8db8fc).("0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! ":\t%s\t%s\n", insn[j].addresscxx . . $ cs_insn#address., insn[j].mnemoniccxx . . $ cs_insn#mnemonic., insn[j].op_strcxx . . $ cs_insn#op_str.);
124 print_insn_detailcxx . . $ print_insn_detail(2888c3e5b0984e8d).(handlecxx . . $ handle., &insn[j]);
125 }
126 printfcxx . . $ printf(581fddd72a8db8fc).("0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! ":\n", insn[j-1].addresscxx . . $ cs_insn#address. + insn[j-1].sizecxx . . $ cs_insn#size.);
127
128 // free memory allocated by cs_disasm()
129 cs_freecxx . . $ cs_free(d0ae7023b33cf0c8).(insn, count);
130 } else {
131 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
132 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
133 print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).("Code: ", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
134 printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Failed to disasm given code!\n");
135 abortcxx . . $ abort(8df32a62017fbbf6).();
136 }
137
138 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
139
140 cs_closecxx . . $ cs_close(b9ab39fbab147149).(&handlecxx . . $ handle.);
141 }
142 }
143
144 int maincxx . . $ main(d1b40a211a1a4e4c).()
145 {
146 testcxx . . $ test(d878ad36e69247c7).();
147 return 0;
148 }
149