Debian Codegraph @

tests/test_bpf.c

raw symbols SCIP
1 /* Capstone Disassembly Engine */
2 /* By david942j <[email protected]>, 2019 */
3
4 #include <capstone/capstone.h>
5 #include <capstone/platform.h>
6
7 static csh handle;
8
9 struct platform {
10 cs_arch arch;
11 cs_mode mode;
12 const unsigned char *code;
13 size_t size;
14 const char *comment;
15 };
16
17 static void print_string_hex(const char *comment, const unsigned char *str, size_t len)
18 {
19 const unsigned char *c;
20
21 printf("%s", comment);
22 for (c = str; c < str + len; c++) {
23 printf(" 0x%02x", *c & 0xff);
24 }
25
26 printf("\n");
27 }
28
29 static const char * ext_name[] = {
30 [BPF_EXT_LEN] = "#len",
31 };
32
33 static void print_insn_detail(csh cs_handle, cs_insn *ins)
34 {
35 cs_bpf *bpf;
36 cs_regs regs_read, regs_write;
37 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->detail == NULL)
42 return;
43
44 if (ins->detail->groups_count) {
45 int j;
46
47 printf("\tGroups:");
48 for(j = 0; j < ins->detail->groups_count; j++)
49 printf(" %s", cs_group_name(handle, ins->detail->groups[j]));
50 printf("\n");
51 }
52
53 bpf = &(ins->detail->bpf);
54
55 printf("\tOperand count: %u\n", bpf->op_count);
56 for (i = 0; i < bpf->op_count; i++) {
57 cs_bpf_op *op = &(bpf->operands[i]);
58 printf("\t\toperands[%u].type: ", i);
59 switch (op->type) {
60 case BPF_OP_INVALID:
61 printf("INVALID\n");
62 break;
63 case BPF_OP_REG:
64 printf("REG = %s\n", cs_reg_name(handle, op->reg));
65 break;
66 case BPF_OP_IMM:
67 printf("IMM = 0x%" PRIx64 "\n", op->imm);
68 break;
69 case BPF_OP_OFF:
70 printf("OFF = +0x%x\n", op->off);
71 break;
72 case BPF_OP_MEM:
73 printf("MEM\n");
74 if (op->mem.base != BPF_REG_INVALID)
75 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
76 i, cs_reg_name(handle, op->mem.base));
77 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
78 break;
79 case BPF_OP_MMEM:
80 printf("MMEM = M[0x%x]\n", op->mmem);
81 break;
82 case BPF_OP_MSH:
83 printf("MSH = 4*([0x%x]&0xf)\n", op->msh);
84 break;
85 case BPF_OP_EXT:
86 printf("EXT = %s\n", ext_name[op->ext]);
87 break;
88 }
89 }
90
91 /* print all registers that are involved in this instruction */
92 if (!cs_regs_access(cs_handle, ins,
93 regs_read, &regs_read_count,
94 regs_write, &regs_write_count)) {
95 if (regs_read_count) {
96 printf("\tRegisters read:");
97 for(i = 0; i < regs_read_count; i++)
98 printf(" %s", cs_reg_name(cs_handle, regs_read[i]));
99 printf("\n");
100 }
101
102 if (regs_write_count) {
103 printf("\tRegisters modified:");
104 for(i = 0; i < regs_write_count; i++)
105 printf(" %s", cs_reg_name(cs_handle, regs_write[i]));
106 printf("\n");
107 }
108 }
109 puts("");
110 }
111
112 static void test()
113 {
114 #define CBPF_CODE "\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_CODE "\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 platform platforms[] = {
127 {
128 CS_ARCH_BPF,
129 CS_MODE_LITTLE_ENDIAN | CS_MODE_BPF_CLASSIC,
130 (unsigned char *)CBPF_CODE,
131 sizeof(CBPF_CODE) - 1,
132 "cBPF Le"
133 },
134 {
135 CS_ARCH_BPF,
136 CS_MODE_LITTLE_ENDIAN | CS_MODE_BPF_EXTENDED,
137 (unsigned char *)EBPF_CODE,
138 sizeof(EBPF_CODE) - 1,
139 "eBPF Le"
140 },
141 };
142 uint64_t address = 0x0;
143 cs_insn *insn;
144 int i;
145 size_t count;
146
147 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
148 cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
149 if (err) {
150 printf("Failed on cs_open() with error returned: %u\n", err);
151 abort();
152 }
153
154 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
155
156 count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
157 if (count) {
158 size_t j;
159 printf("****************\n");
160 printf("Platform: %s\n", platforms[i].comment);
161 print_string_hex("Code:", platforms[i].code, platforms[i].size);
162 printf("Disasm:\n");
163
164 for (j = 0; j < count; j++) {
165 printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
166 print_insn_detail(handle, &insn[j]);
167 }
168
169 // free memory allocated by cs_disasm()
170 cs_free(insn, count);
171 } else {
172 printf("****************\n");
173 printf("Platform: %s\n", platforms[i].comment);
174 print_string_hex("Code:", platforms[i].code, platforms[i].size);
175 printf("ERROR: Failed to disasm given code!\n");
176 abort();
177 }
178
179 cs_close(&handle);
180 }
181 }
182
183 int main()
184 {
185 test();
186 return 0;
187 }