Debian Codegraph @

arch/BPF/BPFInstPrinter.c

raw symbols SCIP
1 /* Capstone Disassembly Engine */
2 /* BPF Backend by david942j <[email protected]>, 2019 */
3
4 #include <capstone/platform.h>
5
6 #include "BPFConstants.h"
7 #include "BPFInstPrinter.h"
8 #include "BPFMapping.h"
9
10 static cs_bpf_op *expand_bpf_operands(cs_bpf *bpf)
11 {
12 /* assert(bpf->op_count < 3); */
13 return &bpf->operands[bpf->op_count++];
14 }
15
16 static void push_op_reg(cs_bpf *bpf, bpf_op_type val, uint8_t ac_mode)
17 {
18 cs_bpf_op *op = expand_bpf_operands(bpf);
19
20 op->type = BPF_OP_REG;
21 op->reg = val;
22 op->access = ac_mode;
23 }
24
25 static void push_op_imm(cs_bpf *bpf, uint64_t val)
26 {
27 cs_bpf_op *op = expand_bpf_operands(bpf);
28
29 op->type = BPF_OP_IMM;
30 op->imm = val;
31 }
32
33 static void push_op_off(cs_bpf *bpf, uint32_t val)
34 {
35 cs_bpf_op *op = expand_bpf_operands(bpf);
36
37 op->type = BPF_OP_OFF;
38 op->off = val;
39 }
40
41 static void push_op_mem(cs_bpf *bpf, bpf_reg reg, uint32_t val)
42 {
43 cs_bpf_op *op = expand_bpf_operands(bpf);
44
45 op->type = BPF_OP_MEM;
46 op->mem.base = reg;
47 op->mem.disp = val;
48 }
49
50 static void push_op_mmem(cs_bpf *bpf, uint32_t val)
51 {
52 cs_bpf_op *op = expand_bpf_operands(bpf);
53
54 op->type = BPF_OP_MMEM;
55 op->mmem = val;
56 }
57
58 static void push_op_msh(cs_bpf *bpf, uint32_t val)
59 {
60 cs_bpf_op *op = expand_bpf_operands(bpf);
61
62 op->type = BPF_OP_MSH;
63 op->msh = val;
64 }
65
66 static void push_op_ext(cs_bpf *bpf, bpf_ext_type val)
67 {
68 cs_bpf_op *op = expand_bpf_operands(bpf);
69
70 op->type = BPF_OP_EXT;
71 op->ext = val;
72 }
73
74 static void convert_operands(MCInst *MI, cs_bpf *bpf)
75 {
76 unsigned opcode = MCInst_getOpcode(MI);
77 unsigned mc_op_count = MCInst_getNumOperands(MI);
78 MCOperand *op;
79 MCOperand *op2;
80 unsigned i;
81
82 bpf->op_count = 0;
83 if (BPF_CLASS(opcode) == BPF_CLASS_LD || BPF_CLASS(opcode) == BPF_CLASS_LDX) {
84 switch (BPF_MODE(opcode)) {
85 case BPF_MODE_IMM:
86 if (EBPF_MODE(MI->csh)) {
87 push_op_reg(bpf, MCOperand_getReg(MCInst_getOperand(MI, 0)), CS_AC_WRITE);
88 push_op_imm(bpf, MCOperand_getImm(MCInst_getOperand(MI, 1)));
89 } else {
90 push_op_imm(bpf, MCOperand_getImm(MCInst_getOperand(MI, 0)));
91 }
92 break;
93 case BPF_MODE_ABS:
94 op = MCInst_getOperand(MI, 0);
95 push_op_mem(bpf, BPF_REG_INVALID, (uint32_t)MCOperand_getImm(op));
96 break;
97 case BPF_MODE_IND:
98 op = MCInst_getOperand(MI, 0);
99 op2 = MCInst_getOperand(MI, 1);
100 push_op_mem(bpf, MCOperand_getReg(op), (uint32_t)MCOperand_getImm(op2));
101 break;
102 case BPF_MODE_MEM:
103 if (EBPF_MODE(MI->csh)) {
104 /* ldx{w,h,b,dw} dst, [src+off] */
105 push_op_reg(bpf, MCOperand_getReg(MCInst_getOperand(MI, 0)), CS_AC_WRITE);
106 op = MCInst_getOperand(MI, 1);
107 op2 = MCInst_getOperand(MI, 2);
108 push_op_mem(bpf, MCOperand_getReg(op), (uint32_t)MCOperand_getImm(op2));
109 }
110 else {
111 push_op_mmem(bpf, (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, 0)));
112 }
113 break;
114 case BPF_MODE_LEN:
115 push_op_ext(bpf, BPF_EXT_LEN);
116 break;
117 case BPF_MODE_MSH:
118 op = MCInst_getOperand(MI, 0);
119 push_op_msh(bpf, (uint32_t)MCOperand_getImm(op));
120 break;
121 /* case BPF_MODE_XADD: // not exists */
122 }
123 return;
124 }
125 if (BPF_CLASS(opcode) == BPF_CLASS_ST || BPF_CLASS(opcode) == BPF_CLASS_STX) {
126 if (!EBPF_MODE(MI->csh)) {
127 // cBPF has only one case - st* M[k]
128 push_op_mmem(bpf, (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, 0)));
129 return;
130 }
131 /* eBPF has two cases:
132 * - st [dst + off], src
133 * - xadd [dst + off], src
134 * they have same form of operands.
135 */
136 op = MCInst_getOperand(MI, 0);
137 op2 = MCInst_getOperand(MI, 1);
138 push_op_mem(bpf, MCOperand_getReg(op), (uint32_t)MCOperand_getImm(op2));
139 op = MCInst_getOperand(MI, 2);
140 if (MCOperand_isImm(op))
141 push_op_imm(bpf, MCOperand_getImm(op));
142 else if (MCOperand_isReg(op))
143 push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
144 return;
145 }
146
147 if (BPF_CLASS(opcode) == BPF_CLASS_JMP) {
148 for (i = 0; i < mc_op_count; i++) {
149 op = MCInst_getOperand(MI, i);
150 if (MCOperand_isImm(op)) {
151 /* decide the imm is BPF_OP_IMM or BPF_OP_OFF type here */
152 /*
153 * 1. ja +off
154 * 2. j {x,k}, +jt, +jf // cBPF
155 * 3. j dst_reg, {src_reg, k}, +off // eBPF
156 */
157 if (BPF_OP(opcode) == BPF_JUMP_JA ||
158 (!EBPF_MODE(MI->csh) && i >= 1) ||
159 (EBPF_MODE(MI->csh) && i == 2))
160 push_op_off(bpf, (uint32_t)MCOperand_getImm(op));
161 else
162 push_op_imm(bpf, MCOperand_getImm(op));
163 }
164 else if (MCOperand_isReg(op)) {
165 push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
166 }
167 }
168 return;
169 }
170
171 if (!EBPF_MODE(MI->csh)) {
172 /* In cBPF mode, all registers in operands are accessed as read */
173 for (i = 0; i < mc_op_count; i++) {
174 op = MCInst_getOperand(MI, i);
175 if (MCOperand_isImm(op))
176 push_op_imm(bpf, MCOperand_getImm(op));
177 else if (MCOperand_isReg(op))
178 push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
179 }
180 return;
181 }
182
183 /* remain cases are: eBPF mode && ALU */
184 /* if (BPF_CLASS(opcode) == BPF_CLASS_ALU || BPF_CLASS(opcode) == BPF_CLASS_ALU64) */
185
186 /* We have three types:
187 * 1. {l,b}e dst // dst = byteswap(dst)
188 * 2. neg dst // dst = -dst
189 * 3. <op> dst, {src_reg, imm} // dst = dst <op> src
190 * so we can simply check the number of operands,
191 * exactly one operand means we are in case 1. and 2.,
192 * otherwise in case 3.
193 */
194 if (mc_op_count == 1) {
195 op = MCInst_getOperand(MI, 0);
196 push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ | CS_AC_WRITE);
197 }
198 else { // if (mc_op_count == 2)
199 op = MCInst_getOperand(MI, 0);
200 push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ | CS_AC_WRITE);
201
202 op = MCInst_getOperand(MI, 1);
203 if (MCOperand_isImm(op))
204 push_op_imm(bpf, MCOperand_getImm(op));
205 else if (MCOperand_isReg(op))
206 push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
207 }
208 }
209
210 static void print_operand(MCInst *MI, struct SStream *O, const cs_bpf_op *op)
211 {
212 switch (op->type) {
213 case BPF_OP_INVALID:
214 SStream_concat(O, "invalid");
215 break;
216 case BPF_OP_REG:
217 SStream_concat(O, BPF_reg_name((csh)MI->csh, op->reg));
218 break;
219 case BPF_OP_IMM:
220 SStream_concat(O, "0x%" PRIx64, op->imm);
221 break;
222 case BPF_OP_OFF:
223 SStream_concat(O, "+0x%x", op->off);
224 break;
225 case BPF_OP_MEM:
226 SStream_concat(O, "[");
227 if (op->mem.base != BPF_REG_INVALID)
228 SStream_concat(O, BPF_reg_name((csh)MI->csh, op->mem.base));
229 if (op->mem.disp != 0) {
230 if (op->mem.base != BPF_REG_INVALID)
231 SStream_concat(O, "+");
232 SStream_concat(O, "0x%x", op->mem.disp);
233 }
234 if (op->mem.base == BPF_REG_INVALID && op->mem.disp == 0) // special case
235 SStream_concat(O, "0x0");
236 SStream_concat(O, "]");
237 break;
238 case BPF_OP_MMEM:
239 SStream_concat(O, "m[0x%x]", op->mmem);
240 break;
241 case BPF_OP_MSH:
242 SStream_concat(O, "4*([0x%x]&0xf)", op->msh);
243 break;
244 case BPF_OP_EXT:
245 switch (op->ext) {
246 case BPF_EXT_LEN:
247 SStream_concat(O, "#len");
248 break;
249 }
250 break;
251 }
252 }
253
254 /*
255 * 1. human readable mnemonic
256 * 2. set pubOpcode (BPF_INSN_*)
257 * 3. set detail->bpf.operands
258 * */
259 void BPF_printInst(MCInst *MI, struct SStream *O, void *PrinterInfo)
260 {
261 int i;
262 cs_insn insn;
263 cs_bpf bpf;
264
265 insn.detail = NULL;
266 /* set pubOpcode as instruction id */
267 BPF_get_insn_id((cs_struct*)MI->csh, &insn, MCInst_getOpcode(MI));
268 MCInst_setOpcodePub(MI, insn.id);
269
270 SStream_concat(O, BPF_insn_name((csh)MI->csh, insn.id));
271 convert_operands(MI, &bpf);
272 for (i = 0; i < bpf.op_count; i++) {
273 if (i == 0)
274 SStream_concat(O, "\t");
275 else
276 SStream_concat(O, ", ");
277 print_operand(MI, O, &bpf.operands[i]);
278 }
279
280 #ifndef CAPSTONE_DIET
281 if (MI->flat_insn->detail) {
282 MI->flat_insn->detail->bpf = bpf;
283 }
284 #endif
285 }