Debian Codegraph @

tests/test_bpf.c

raw symbols SCIP
1 /* Capstone Disassembly Engine */cxx . . $ `<file>/tests/test_bpf.c`/
2 /* By david942j <[email protected]>, 2019 */
3
4 #include <capstone/capstone.h>cxx . . $ `<file>/include/capstone/capstone.h`/
5 #include <capstone/platform.h>cxx . . $ `<file>/include/capstone/platform.h`/
6
7 static cshcxx . . $ csh# handlecxx . . $ handle.;
8
9 struct platformcxx . . $ platform# {
10 cs_archcxx . . $ cs_arch# archcxx . . $ platform#arch.;
11 cs_modecxx . . $ cs_mode# modecxx . . $ platform#mode.;
12 const unsigned char *codecxx . . $ platform#code.;
13 size_tcxx . . $ size_t# sizecxx . . $ platform#size.;
14 const char *commentcxx . . $ platform#comment.;
15 };
16
17 static void print_string_hexcxx . . $ print_string_hex(432b8f53fadfa4c1).(const char *comment, const unsigned char *str, size_tcxx . . $ size_t# len)
18 {
19 const unsigned char *c;
20
21 printfcxx . . $ printf(581fddd72a8db8fc).("%s", comment);
22 for (c = str; c < str + len; c++) {
23 printfcxx . . $ printf(581fddd72a8db8fc).(" 0x%02x", *c & 0xff);
24 }
25
26 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
27 }
28
29 static const char * ext_namecxx . . $ ext_name.[] = {
30 [BPF_EXT_LENcxx . . $ BPF_EXT_LEN.] = "#len",
31 };
32
33 static void print_insn_detailcxx . . $ print_insn_detail(2888c3e5b0984e8d).(cshcxx . . $ csh# cs_handle, cs_insncxx . . $ cs_insn# *ins)
34 {
35 cs_bpfcxx . . $ cs_bpf# *bpf;
36 cs_regscxx . . $ cs_regs# regs_read, regs_write;
37 uint8_tcxx . . $ uint8_t# regs_read_count, regs_write_count;
38 unsigned i;
39
40 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
41 if (ins->detailcxx . . $ cs_insn#detail. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
42 return;
43
44 if (ins->detailcxx . . $ cs_insn#detail.->groups_countcxx . . $ cs_detail#groups_count.) {
45 int j;
46
47 printfcxx . . $ printf(581fddd72a8db8fc).("\tGroups:");
48 for(j = 0; j < ins->detailcxx . . $ cs_insn#detail.->groups_countcxx . . $ cs_detail#groups_count.; j++)
49 printfcxx . . $ printf(581fddd72a8db8fc).(" %s", cs_group_namecxx . . $ cs_group_name(efd1348d9ae5172e).(handlecxx . . $ handle., ins->detailcxx . . $ cs_insn#detail.->groupscxx . . $ cs_detail#groups.[j]));
50 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
51 }
52
53 bpf = &(ins->detailcxx . . $ cs_insn#detail.->bpfcxx . . $ cs_detail#$anonymous_type_0#bpf.);
54
55 printfcxx . . $ printf(581fddd72a8db8fc).("\tOperand count: %u\n", bpf->op_countcxx . . $ cs_bpf#op_count.);
56 for (i = 0; i < bpf->op_countcxx . . $ cs_bpf#op_count.; i++) {
57 cs_bpf_opcxx . . $ cs_bpf_op# *op = &(bpf->operandscxx . . $ cs_bpf#operands.[i]);
58 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: ", i);
59 switch (op->typecxx . . $ cs_bpf_op#type.) {
60 case BPF_OP_INVALIDcxx . . $ BPF_OP_INVALID.:
61 printfcxx . . $ printf(581fddd72a8db8fc).("INVALID\n");
62 break;
63 case BPF_OP_REGcxx . . $ BPF_OP_REG.:
64 printfcxx . . $ printf(581fddd72a8db8fc).("REG = %s\n", cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->regcxx . . $ cs_bpf_op#$anonymous_type_0#reg.));
65 break;
66 case BPF_OP_IMMcxx . . $ BPF_OP_IMM.:
67 printfcxx . . $ printf(581fddd72a8db8fc).("IMM = 0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! "\n", op->immcxx . . $ cs_bpf_op#$anonymous_type_0#imm.);
68 break;
69 case BPF_OP_OFFcxx . . $ BPF_OP_OFF.:
70 printfcxx . . $ printf(581fddd72a8db8fc).("OFF = +0x%x\n", op->offcxx . . $ cs_bpf_op#$anonymous_type_0#off.);
71 break;
72 case BPF_OP_MEMcxx . . $ BPF_OP_MEM.:
73 printfcxx . . $ printf(581fddd72a8db8fc).("MEM\n");
74 if (op->memcxx . . $ cs_bpf_op#$anonymous_type_0#mem..basecxx . . $ bpf_op_mem#base. != BPF_REG_INVALIDcxx . . $ BPF_REG_INVALID.)
75 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.base: REG = %s\n",
76 i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->memcxx . . $ cs_bpf_op#$anonymous_type_0#mem..basecxx . . $ bpf_op_mem#base.));
77 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->memcxx . . $ cs_bpf_op#$anonymous_type_0#mem..dispcxx . . $ bpf_op_mem#disp.);
78 break;
79 case BPF_OP_MMEMcxx . . $ BPF_OP_MMEM.:
80 printfcxx . . $ printf(581fddd72a8db8fc).("MMEM = M[0x%x]\n", op->mmemcxx . . $ cs_bpf_op#$anonymous_type_0#mmem.);
81 break;
82 case BPF_OP_MSHcxx . . $ BPF_OP_MSH.:
83 printfcxx . . $ printf(581fddd72a8db8fc).("MSH = 4*([0x%x]&0xf)\n", op->mshcxx . . $ cs_bpf_op#$anonymous_type_0#msh.);
84 break;
85 case BPF_OP_EXTcxx . . $ BPF_OP_EXT.:
86 printfcxx . . $ printf(581fddd72a8db8fc).("EXT = %s\n", ext_namecxx . . $ ext_name.[op->extcxx . . $ cs_bpf_op#$anonymous_type_0#ext.]);
87 break;
88 }
89 }
90
91 /* print all registers that are involved in this instruction */
92 if (!cs_regs_accesscxx . . $ cs_regs_access(4779ec53ff278361).(cs_handle, ins,
93 regs_read, &regs_read_count,
94 regs_write, &regs_write_count)) {
95 if (regs_read_count) {
96 printfcxx . . $ printf(581fddd72a8db8fc).("\tRegisters read:");
97 for(i = 0; i < regs_read_count; i++)
98 printfcxx . . $ printf(581fddd72a8db8fc).(" %s", cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(cs_handle, regs_read[i]));
99 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
100 }
101
102 if (regs_write_count) {
103 printfcxx . . $ printf(581fddd72a8db8fc).("\tRegisters modified:");
104 for(i = 0; i < regs_write_count; i++)
105 printfcxx . . $ printf(581fddd72a8db8fc).(" %s", cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(cs_handle, regs_write[i]));
106 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
107 }
108 }
109 putscxx . . $ puts(5c4306d8553b3696).("");
110 }
111
112 static void testcxx . . $ test(d878ad36e69247c7).()
113 {
114 #define CBPF_CODEcxx . . $ `tests/test_bpf.c:114:9`! "\x94\x09\x00\x00\x37\x13\x03\x00" \
115 "\x87\x00\x00\x00\x00\x00\x00\x00" \
116 "\x07\x00\x00\x00\x00\x00\x00\x00" \
117 "\x16\x00\x00\x00\x00\x00\x00\x00" \
118 "\x80\x00\x00\x00\x00\x00\x00\x00"
119
120 #define EBPF_CODEcxx . . $ `tests/test_bpf.c:120:9`! "\x97\x09\x00\x00\x37\x13\x03\x00" \
121 "\xdc\x02\x00\x00\x20\x00\x00\x00" \
122 "\x30\x00\x00\x00\x00\x00\x00\x00" \
123 "\xdb\x3a\x00\x01\x00\x00\x00\x00" \
124 "\x84\x02\x00\x00\x00\x00\x00\x00" \
125 "\x6d\x33\x17\x02\x00\x00\x00\x00"
126 struct platformcxx . . $ platform# platforms[] = {
127 {
128 CS_ARCH_BPFcxx . . $ CS_ARCH_BPF.,
129 CS_MODE_LITTLE_ENDIANcxx . . $ CS_MODE_LITTLE_ENDIAN. | CS_MODE_BPF_CLASSICcxx . . $ CS_MODE_BPF_CLASSIC.,
130 (unsigned char *)CBPF_CODEcxx . . $ `tests/test_bpf.c:114:9`!,
131 sizeof(CBPF_CODEcxx . . $ `tests/test_bpf.c:114:9`!) - 1,
132 "cBPF Le"
133 },
134 {
135 CS_ARCH_BPFcxx . . $ CS_ARCH_BPF.,
136 CS_MODE_LITTLE_ENDIANcxx . . $ CS_MODE_LITTLE_ENDIAN. | CS_MODE_BPF_EXTENDEDcxx . . $ CS_MODE_BPF_EXTENDED.,
137 (unsigned char *)EBPF_CODEcxx . . $ `tests/test_bpf.c:120:9`!,
138 sizeof(EBPF_CODEcxx . . $ `tests/test_bpf.c:120:9`!) - 1,
139 "eBPF Le"
140 },
141 };
142 uint64_tcxx . . $ uint64_t# address = 0x0;
143 cs_insncxx . . $ cs_insn# *insn;
144 int i;
145 size_tcxx . . $ size_t# count;
146
147 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
148 cs_errcxx . . $ cs_err# err = cs_opencxx . . $ cs_open(b2cb849d8ee082cd).(platforms[i].archcxx . . $ platform#arch., platforms[i].modecxx . . $ platform#mode., &handlecxx . . $ handle.);
149 if (err) {
150 printfcxx . . $ printf(581fddd72a8db8fc).("Failed on cs_open() with error returned: %u\n", err);
151 abortcxx . . $ abort(8df32a62017fbbf6).();
152 }
153
154 cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handlecxx . . $ handle., CS_OPT_DETAILcxx . . $ CS_OPT_DETAIL., CS_OPT_ONcxx . . $ CS_OPT_ON.);
155
156 count = cs_disasmcxx . . $ cs_disasm(d552efbd9b22be5).(handlecxx . . $ handle., platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size., address, 0, &insn);
157 if (count) {
158 size_tcxx . . $ size_t# j;
159 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
160 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
161 print_string_hexcxx . . $ print_string_hex(432b8f53fadfa4c1).("Code:", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
162 printfcxx . . $ printf(581fddd72a8db8fc).("Disasm:\n");
163
164 for (j = 0; j < count; j++) {
165 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.);
166 print_insn_detailcxx . . $ print_insn_detail(2888c3e5b0984e8d).(handlecxx . . $ handle., &insn[j]);
167 }
168
169 // free memory allocated by cs_disasm()
170 cs_freecxx . . $ cs_free(d0ae7023b33cf0c8).(insn, count);
171 } else {
172 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
173 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
174 print_string_hexcxx . . $ print_string_hex(432b8f53fadfa4c1).("Code:", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
175 printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Failed to disasm given code!\n");
176 abortcxx . . $ abort(8df32a62017fbbf6).();
177 }
178
179 cs_closecxx . . $ cs_close(b9ab39fbab147149).(&handlecxx . . $ handle.);
180 }
181 }
182
183 int maincxx . . $ main(d1b40a211a1a4e4c).()
184 {
185 testcxx . . $ test(d878ad36e69247c7).();
186 return 0;
187 }