/src/capstonenext/arch/BPF/BPFInstPrinter.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Capstone Disassembly Engine */ |
2 | | /* BPF Backend by david942j <david942j@gmail.com>, 2019 */ |
3 | | /* SPDX-FileCopyrightText: 2024 Roee Toledano <roeetoledano10@gmail.com> */ |
4 | | /* SPDX-License-Identifier: BSD-3 */ |
5 | | |
6 | | #include <capstone/platform.h> |
7 | | |
8 | | #include "BPFConstants.h" |
9 | | #include "BPFInstPrinter.h" |
10 | | #include "BPFMapping.h" |
11 | | #include "../../Mapping.h" |
12 | | |
13 | | static cs_bpf_op *expand_bpf_operands(cs_bpf *bpf) |
14 | 18.5k | { |
15 | 18.5k | assert(bpf->op_count < 3); |
16 | 18.5k | return &bpf->operands[bpf->op_count++]; |
17 | 18.5k | } |
18 | | |
19 | | static void push_op_reg(cs_bpf *bpf, bpf_op_type val, uint8_t ac_mode) |
20 | 12.5k | { |
21 | 12.5k | cs_bpf_op *op = expand_bpf_operands(bpf); |
22 | | |
23 | 12.5k | op->type = BPF_OP_REG; |
24 | 12.5k | op->reg = val; |
25 | 12.5k | op->access = ac_mode; |
26 | 12.5k | } |
27 | | |
28 | | static void push_op_imm(cs_bpf *bpf, uint64_t val, const bool is_signed) |
29 | 9.65k | { |
30 | 9.65k | cs_bpf_op *op = expand_bpf_operands(bpf); |
31 | | |
32 | 9.65k | op->type = BPF_OP_IMM; |
33 | 9.65k | op->imm = val; |
34 | 9.65k | op->is_signed = is_signed; |
35 | 9.65k | } |
36 | | |
37 | | static void push_op_off(cs_bpf *bpf, uint32_t val, const bool is_signed) |
38 | 5.36k | { |
39 | 5.36k | cs_bpf_op *op = expand_bpf_operands(bpf); |
40 | | |
41 | 5.36k | op->type = BPF_OP_OFF; |
42 | 5.36k | op->off = val; |
43 | 5.36k | op->is_signed = is_signed; |
44 | 5.36k | } |
45 | | |
46 | | static void push_op_mem(cs_bpf *bpf, bpf_reg reg, uint32_t val, |
47 | | const bool is_signed, const bool is_pkt) |
48 | 6.83k | { |
49 | 6.83k | cs_bpf_op *op = expand_bpf_operands(bpf); |
50 | | |
51 | 6.83k | op->type = BPF_OP_MEM; |
52 | 6.83k | op->mem.base = reg; |
53 | 6.83k | op->mem.disp = val; |
54 | 6.83k | op->is_signed = is_signed; |
55 | 6.83k | op->is_pkt = is_pkt; |
56 | 6.83k | } |
57 | | |
58 | | static void push_op_mmem(cs_bpf *bpf, uint32_t val) |
59 | 399 | { |
60 | 399 | cs_bpf_op *op = expand_bpf_operands(bpf); |
61 | | |
62 | 399 | op->type = BPF_OP_MMEM; |
63 | 399 | op->mmem = val; |
64 | 399 | } |
65 | | |
66 | | static void push_op_msh(cs_bpf *bpf, uint32_t val) |
67 | 121 | { |
68 | 121 | cs_bpf_op *op = expand_bpf_operands(bpf); |
69 | | |
70 | 121 | op->type = BPF_OP_MSH; |
71 | 121 | op->msh = val; |
72 | 121 | } |
73 | | |
74 | | static void push_op_ext(cs_bpf *bpf, bpf_ext_type val) |
75 | 213 | { |
76 | 213 | cs_bpf_op *op = expand_bpf_operands(bpf); |
77 | | |
78 | 213 | op->type = BPF_OP_EXT; |
79 | 213 | op->ext = val; |
80 | 213 | } |
81 | | |
82 | | static void convert_operands(MCInst *MI, cs_bpf *bpf) |
83 | 10.6k | { |
84 | 10.6k | unsigned opcode = MCInst_getOpcode(MI); |
85 | 10.6k | unsigned mc_op_count = MCInst_getNumOperands(MI); |
86 | 10.6k | MCOperand *op; |
87 | 10.6k | MCOperand *op2; |
88 | | |
89 | 10.6k | bpf->op_count = 0; |
90 | 10.6k | if (BPF_CLASS(opcode) == BPF_CLASS_LD || |
91 | 10.6k | BPF_CLASS(opcode) == BPF_CLASS_LDX) { |
92 | 3.66k | switch (BPF_MODE(opcode)) { |
93 | 583 | case BPF_MODE_IMM: |
94 | 583 | if (EBPF_MODE(MI->csh->mode)) { |
95 | 123 | push_op_reg(bpf, |
96 | 123 | MCOperand_getReg( |
97 | 123 | MCInst_getOperand(MI, 0)), |
98 | 123 | CS_AC_WRITE); |
99 | 123 | push_op_imm(bpf, |
100 | 123 | MCOperand_getImm( |
101 | 123 | MCInst_getOperand(MI, 1)), |
102 | 123 | false); |
103 | 460 | } else { |
104 | 460 | push_op_imm(bpf, |
105 | 460 | MCOperand_getImm( |
106 | 460 | MCInst_getOperand(MI, 0)), |
107 | 460 | false); |
108 | 460 | } |
109 | 583 | break; |
110 | 706 | case BPF_MODE_ABS: |
111 | 706 | op = MCInst_getOperand(MI, 0); |
112 | 706 | push_op_mem(bpf, BPF_REG_INVALID, |
113 | 706 | (uint32_t)MCOperand_getImm(op), |
114 | 706 | EBPF_MODE(MI->csh->mode), true); |
115 | 706 | break; |
116 | 828 | case BPF_MODE_IND: |
117 | 828 | op = MCInst_getOperand(MI, 0); |
118 | 828 | if (EBPF_MODE(MI->csh->mode)) |
119 | 415 | push_op_mem(bpf, MCOperand_getReg(op), 0x0, |
120 | 415 | true, true); |
121 | 413 | else { |
122 | 413 | op2 = MCInst_getOperand(MI, 1); |
123 | 413 | push_op_mem(bpf, MCOperand_getReg(op), |
124 | 413 | (uint32_t)MCOperand_getImm(op2), |
125 | 413 | false, true); |
126 | 413 | } |
127 | 828 | break; |
128 | 1.34k | case BPF_MODE_MEM: |
129 | 1.34k | if (EBPF_MODE(MI->csh->mode)) { |
130 | | /* ldx{w,h,b,dw} dst, [src+off] */ |
131 | 1.17k | push_op_reg(bpf, |
132 | 1.17k | MCOperand_getReg( |
133 | 1.17k | MCInst_getOperand(MI, 0)), |
134 | 1.17k | CS_AC_WRITE); |
135 | 1.17k | op = MCInst_getOperand(MI, 1); |
136 | 1.17k | op2 = MCInst_getOperand(MI, 2); |
137 | 1.17k | push_op_mem(bpf, MCOperand_getReg(op), |
138 | 1.17k | (uint32_t)MCOperand_getImm(op2), |
139 | 1.17k | true, false); |
140 | 1.17k | } else { |
141 | 177 | push_op_mmem(bpf, |
142 | 177 | (uint32_t)MCOperand_getImm( |
143 | 177 | MCInst_getOperand(MI, 0))); |
144 | 177 | } |
145 | 1.34k | break; |
146 | 142 | case BPF_MODE_LEN: |
147 | 142 | push_op_ext(bpf, BPF_EXT_LEN); |
148 | 142 | break; |
149 | 56 | case BPF_MODE_MSH: |
150 | 56 | op = MCInst_getOperand(MI, 0); |
151 | 56 | push_op_msh(bpf, (uint32_t)MCOperand_getImm(op)); |
152 | 56 | break; |
153 | | /* case BPF_MODE_XADD: // not exists */ |
154 | 3.66k | } |
155 | 3.66k | return; |
156 | 3.66k | } |
157 | 6.94k | if (BPF_CLASS(opcode) == BPF_CLASS_ST || |
158 | 6.94k | BPF_CLASS(opcode) == BPF_CLASS_STX) { |
159 | 744 | if (!EBPF_MODE(MI->csh->mode)) { |
160 | | // cBPF has only one case - st* M[k] |
161 | 56 | push_op_mmem(bpf, (uint32_t)MCOperand_getImm( |
162 | 56 | MCInst_getOperand(MI, 0))); |
163 | 56 | return; |
164 | 56 | } |
165 | | /* eBPF has two cases: |
166 | | * - st [dst + off], src |
167 | | * - xadd [dst + off], src |
168 | | * they have same form of operands. |
169 | | */ |
170 | 688 | op = MCInst_getOperand(MI, 0); |
171 | 688 | op2 = MCInst_getOperand(MI, 1); |
172 | 688 | push_op_mem(bpf, MCOperand_getReg(op), |
173 | 688 | (uint32_t)MCOperand_getImm(op2), true, false); |
174 | | |
175 | 688 | op = MCInst_getOperand(MI, 2); |
176 | 688 | if (MCOperand_isImm(op)) |
177 | 285 | push_op_imm(bpf, MCOperand_getImm(op), false); |
178 | 403 | else if (MCOperand_isReg(op)) |
179 | 403 | push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ); |
180 | 688 | return; |
181 | 744 | } |
182 | | |
183 | 6.20k | { |
184 | 6.20k | const bool is_jmp32 = EBPF_MODE(MI->csh->mode) && |
185 | 6.20k | (BPF_CLASS(opcode) == BPF_CLASS_JMP32); |
186 | 6.20k | if (BPF_CLASS(opcode) == BPF_CLASS_JMP || is_jmp32) { |
187 | 10.7k | for (size_t i = 0; i < mc_op_count; i++) { |
188 | 7.73k | op = MCInst_getOperand(MI, i); |
189 | 7.73k | if (MCOperand_isImm(op)) { |
190 | | /* Decide if we're using IMM or OFF here (and if OFF, then signed or unsigned): |
191 | | * |
192 | | * 1. any jump/jump32 + signed off (not including exit/call and ja on jump32) // eBPF |
193 | | * 2. exit/call/ja + k // eBPF |
194 | | * 3. ja + unsigned off // cBPF (cBPF programs can only jump forwards) |
195 | | * 4. any jump {x,k}, +jt, +jf // cBPF |
196 | | * */ |
197 | | |
198 | 5.44k | if ((BPF_OP(opcode) == BPF_JUMP_JA && |
199 | 5.44k | !is_jmp32) || |
200 | 5.44k | (!EBPF_MODE(MI->csh->mode) && |
201 | 5.19k | i >= 1) || |
202 | 5.44k | (EBPF_MODE(MI->csh->mode) && |
203 | 4.70k | i == 2)) |
204 | 2.88k | push_op_off( |
205 | 2.88k | bpf, |
206 | 2.88k | MCOperand_getImm(op), |
207 | 2.88k | EBPF_MODE( |
208 | 2.88k | MI->csh->mode)); |
209 | 2.56k | else |
210 | 2.56k | push_op_imm( |
211 | 2.56k | bpf, |
212 | 2.56k | MCOperand_getImm(op), |
213 | 2.56k | true); |
214 | 5.44k | } else if (MCOperand_isReg(op)) { |
215 | 2.28k | push_op_reg(bpf, MCOperand_getReg(op), |
216 | 2.28k | CS_AC_READ); |
217 | 2.28k | } |
218 | 7.73k | } |
219 | 2.98k | return; |
220 | 2.98k | } |
221 | 6.20k | } |
222 | | |
223 | 3.22k | if (!EBPF_MODE(MI->csh->mode)) { |
224 | | /* In cBPF mode, all registers in operands are accessed as read */ |
225 | 2.97k | for (size_t i = 0; i < mc_op_count; i++) { |
226 | 1.37k | op = MCInst_getOperand(MI, i); |
227 | 1.37k | if (MCOperand_isImm(op)) |
228 | 471 | push_op_imm(bpf, MCOperand_getImm(op), false); |
229 | 906 | else if (MCOperand_isReg(op)) |
230 | 906 | push_op_reg(bpf, MCOperand_getReg(op), |
231 | 906 | CS_AC_READ); |
232 | 1.37k | } |
233 | 1.60k | return; |
234 | 1.60k | } |
235 | | |
236 | | /* remain cases are: eBPF mode && ALU */ |
237 | | /* if (BPF_CLASS(opcode) == BPF_CLASS_ALU || BPF_CLASS(opcode) == BPF_CLASS_ALU64) */ |
238 | | |
239 | | /* We have three types: |
240 | | * 1. {l,b}e dst // dst = byteswap(dst) |
241 | | * 2. neg dst // dst = -dst |
242 | | * 3. <op> dst, {src_reg, imm} // dst = dst <op> src |
243 | | * so we can simply check the number of operands, |
244 | | * exactly one operand means we are in case 1. and 2., |
245 | | * otherwise in case 3. |
246 | | */ |
247 | 1.61k | if (mc_op_count == 1) { |
248 | 224 | op = MCInst_getOperand(MI, 0); |
249 | 224 | push_op_reg(bpf, MCOperand_getReg(op), |
250 | 224 | CS_AC_READ | CS_AC_WRITE); |
251 | 1.39k | } else { // if (mc_op_count == 2) |
252 | 1.39k | op = MCInst_getOperand(MI, 0); |
253 | 1.39k | push_op_reg(bpf, MCOperand_getReg(op), |
254 | 1.39k | CS_AC_READ | CS_AC_WRITE); |
255 | | |
256 | 1.39k | op = MCInst_getOperand(MI, 1); |
257 | 1.39k | if (MCOperand_isImm(op)) |
258 | 1.21k | push_op_imm(bpf, MCOperand_getImm(op), false); |
259 | 176 | else if (MCOperand_isReg(op)) |
260 | 176 | push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ); |
261 | 1.39k | } |
262 | 1.61k | } |
263 | | |
264 | | static void print_operand(MCInst *MI, struct SStream *O, const cs_bpf_op *op) |
265 | 18.5k | { |
266 | 18.5k | switch (op->type) { |
267 | 0 | case BPF_OP_INVALID: |
268 | 0 | SStream_concat(O, "invalid"); |
269 | 0 | break; |
270 | 6.68k | case BPF_OP_REG: |
271 | 6.68k | SStream_concat(O, BPF_reg_name((csh)MI->csh, op->reg)); |
272 | 6.68k | break; |
273 | 5.11k | case BPF_OP_IMM: |
274 | 5.11k | if (op->is_signed) |
275 | 2.56k | printInt32Hex(O, op->imm); |
276 | 2.55k | else |
277 | 2.55k | SStream_concat(O, "0x%" PRIx64, op->imm); |
278 | 5.11k | break; |
279 | 2.88k | case BPF_OP_OFF: |
280 | 2.88k | if (op->is_signed) |
281 | 2.29k | printInt16HexOffset(O, op->off); |
282 | 587 | else |
283 | 587 | SStream_concat(O, "+0x%" PRIx32, op->off); |
284 | 2.88k | break; |
285 | 3.39k | case BPF_OP_MEM: |
286 | 3.39k | SStream_concat(O, "["); |
287 | | |
288 | 3.39k | if (op->is_pkt && EBPF_MODE(MI->csh->mode)) { |
289 | 679 | SStream_concat(O, "skb"); |
290 | | |
291 | 679 | if (op->mem.base != BPF_REG_INVALID) |
292 | 415 | SStream_concat(O, "+%s", |
293 | 415 | BPF_reg_name((csh)MI->csh, |
294 | 415 | op->mem.base)); |
295 | 264 | else { |
296 | 264 | if (op->is_signed) |
297 | 264 | printInt32HexOffset(O, op->mem.disp); |
298 | 0 | else |
299 | 0 | SStream_concat(O, "+0x%" PRIx32, |
300 | 0 | op->mem.disp); |
301 | 264 | } |
302 | 2.71k | } else { |
303 | 2.71k | if (op->mem.base != BPF_REG_INVALID) |
304 | 2.27k | SStream_concat(O, BPF_reg_name((csh)MI->csh, |
305 | 2.27k | op->mem.base)); |
306 | 2.71k | if (op->mem.disp != 0) { |
307 | 2.62k | if (op->mem.base != BPF_REG_INVALID) { |
308 | | // if operation is signed, then it always uses off, not k |
309 | 2.23k | if (op->is_signed) |
310 | 1.82k | printInt16HexOffset( |
311 | 1.82k | O, op->mem.disp); |
312 | 412 | else if (op->is_pkt) |
313 | 412 | SStream_concat(O, "+0x%" PRIx32, |
314 | 412 | op->mem.disp); |
315 | 0 | else |
316 | 0 | SStream_concat(O, "+0x%" PRIx16, |
317 | 0 | op->mem.disp); |
318 | 2.23k | } else |
319 | 389 | SStream_concat(O, "0x%" PRIx32, |
320 | 389 | op->mem.disp); |
321 | 2.62k | } |
322 | | |
323 | 2.71k | if (op->mem.base == BPF_REG_INVALID && |
324 | 2.71k | op->mem.disp == 0) |
325 | 53 | SStream_concat(O, "0x0"); |
326 | 2.71k | } |
327 | | |
328 | 3.39k | SStream_concat(O, "]"); |
329 | 3.39k | break; |
330 | 233 | case BPF_OP_MMEM: |
331 | 233 | SStream_concat(O, "m[0x%x]", op->mmem); |
332 | 233 | break; |
333 | 56 | case BPF_OP_MSH: |
334 | 56 | SStream_concat(O, "4*([0x%x]&0xf)", op->msh); |
335 | 56 | break; |
336 | 142 | case BPF_OP_EXT: |
337 | 142 | switch (op->ext) { |
338 | 142 | case BPF_EXT_LEN: |
339 | 142 | SStream_concat(O, "#len"); |
340 | 142 | break; |
341 | 142 | } |
342 | 142 | break; |
343 | 18.5k | } |
344 | 18.5k | } |
345 | | |
346 | | /* |
347 | | * 1. human readable mnemonic |
348 | | * 2. set pubOpcode (BPF_INSN_*) |
349 | | * 3. set detail->bpf.operands |
350 | | * */ |
351 | | void BPF_printInst(MCInst *MI, struct SStream *O, void *PrinterInfo) |
352 | 21.7k | { |
353 | 21.7k | cs_bpf bpf = { 0 }; |
354 | | |
355 | | /* set pubOpcode as instruction id */ |
356 | 21.7k | SStream_concat(O, BPF_insn_name((csh)MI->csh, MCInst_getOpcodePub(MI))); |
357 | 21.7k | convert_operands(MI, &bpf); |
358 | 56.8k | for (size_t i = 0; i < bpf.op_count; i++) { |
359 | 35.1k | if (i == 0) |
360 | 20.7k | SStream_concat(O, "\t"); |
361 | 14.3k | else |
362 | 14.3k | SStream_concat(O, ", "); |
363 | 35.1k | print_operand(MI, O, &bpf.operands[i]); |
364 | 35.1k | } |
365 | | |
366 | 21.7k | #ifndef CAPSTONE_DIET |
367 | 21.7k | if (detail_is_set(MI)) { |
368 | 21.7k | MI->flat_insn->detail->bpf = bpf; |
369 | 21.7k | } |
370 | 21.7k | #endif |
371 | 21.7k | } |