/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 | 35.7k | { |
15 | 35.7k | assert(bpf->op_count < 3); |
16 | 35.7k | return &bpf->operands[bpf->op_count++]; |
17 | 35.7k | } |
18 | | |
19 | | static void push_op_reg(cs_bpf *bpf, bpf_op_type val, uint8_t ac_mode) |
20 | 20.9k | { |
21 | 20.9k | cs_bpf_op *op = expand_bpf_operands(bpf); |
22 | | |
23 | 20.9k | op->type = BPF_OP_REG; |
24 | 20.9k | op->reg = val; |
25 | 20.9k | op->access = ac_mode; |
26 | 20.9k | } |
27 | | |
28 | | static void push_op_imm(cs_bpf *bpf, uint64_t val, const bool is_signed) |
29 | 16.3k | { |
30 | 16.3k | cs_bpf_op *op = expand_bpf_operands(bpf); |
31 | | |
32 | 16.3k | op->type = BPF_OP_IMM; |
33 | 16.3k | op->imm = val; |
34 | 16.3k | op->is_signed = is_signed; |
35 | 16.3k | } |
36 | | |
37 | | static void push_op_off(cs_bpf *bpf, uint32_t val, const bool is_signed) |
38 | 9.43k | { |
39 | 9.43k | cs_bpf_op *op = expand_bpf_operands(bpf); |
40 | | |
41 | 9.43k | op->type = BPF_OP_OFF; |
42 | 9.43k | op->off = val; |
43 | 9.43k | op->is_signed = is_signed; |
44 | 9.43k | } |
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 | 9.82k | { |
49 | 9.82k | cs_bpf_op *op = expand_bpf_operands(bpf); |
50 | | |
51 | 9.82k | op->type = BPF_OP_MEM; |
52 | 9.82k | op->mem.base = reg; |
53 | 9.82k | op->mem.disp = val; |
54 | 9.82k | op->is_signed = is_signed; |
55 | 9.82k | op->is_pkt = is_pkt; |
56 | 9.82k | } |
57 | | |
58 | | static void push_op_mmem(cs_bpf *bpf, uint32_t val) |
59 | 727 | { |
60 | 727 | cs_bpf_op *op = expand_bpf_operands(bpf); |
61 | | |
62 | 727 | op->type = BPF_OP_MMEM; |
63 | 727 | op->mmem = val; |
64 | 727 | } |
65 | | |
66 | | static void push_op_msh(cs_bpf *bpf, uint32_t val) |
67 | 461 | { |
68 | 461 | cs_bpf_op *op = expand_bpf_operands(bpf); |
69 | | |
70 | 461 | op->type = BPF_OP_MSH; |
71 | 461 | op->msh = val; |
72 | 461 | } |
73 | | |
74 | | static void push_op_ext(cs_bpf *bpf, bpf_ext_type val) |
75 | 408 | { |
76 | 408 | cs_bpf_op *op = expand_bpf_operands(bpf); |
77 | | |
78 | 408 | op->type = BPF_OP_EXT; |
79 | 408 | op->ext = val; |
80 | 408 | } |
81 | | |
82 | | static void convert_operands(MCInst *MI, cs_bpf *bpf) |
83 | 20.0k | { |
84 | 20.0k | unsigned opcode = MCInst_getOpcode(MI); |
85 | 20.0k | unsigned mc_op_count = MCInst_getNumOperands(MI); |
86 | 20.0k | MCOperand *op; |
87 | 20.0k | MCOperand *op2; |
88 | | |
89 | 20.0k | bpf->op_count = 0; |
90 | 20.0k | if (BPF_CLASS(opcode) == BPF_CLASS_LD || |
91 | 20.0k | BPF_CLASS(opcode) == BPF_CLASS_LDX) { |
92 | 5.57k | switch (BPF_MODE(opcode)) { |
93 | 1.01k | case BPF_MODE_IMM: |
94 | 1.01k | if (EBPF_MODE(MI->csh->mode)) { |
95 | 480 | push_op_reg(bpf, |
96 | 480 | MCOperand_getReg( |
97 | 480 | MCInst_getOperand(MI, 0)), |
98 | 480 | CS_AC_WRITE); |
99 | 480 | push_op_imm(bpf, |
100 | 480 | MCOperand_getImm( |
101 | 480 | MCInst_getOperand(MI, 1)), |
102 | 480 | false); |
103 | 533 | } else { |
104 | 533 | push_op_imm(bpf, |
105 | 533 | MCOperand_getImm( |
106 | 533 | MCInst_getOperand(MI, 0)), |
107 | 533 | false); |
108 | 533 | } |
109 | 1.01k | break; |
110 | 1.74k | case BPF_MODE_ABS: |
111 | 1.74k | op = MCInst_getOperand(MI, 0); |
112 | 1.74k | push_op_mem(bpf, BPF_REG_INVALID, |
113 | 1.74k | (uint32_t)MCOperand_getImm(op), |
114 | 1.74k | EBPF_MODE(MI->csh->mode), true); |
115 | 1.74k | break; |
116 | 1.23k | case BPF_MODE_IND: |
117 | 1.23k | op = MCInst_getOperand(MI, 0); |
118 | 1.23k | if (EBPF_MODE(MI->csh->mode)) |
119 | 683 | push_op_mem(bpf, MCOperand_getReg(op), 0x0, |
120 | 683 | true, true); |
121 | 550 | else { |
122 | 550 | op2 = MCInst_getOperand(MI, 1); |
123 | 550 | push_op_mem(bpf, MCOperand_getReg(op), |
124 | 550 | (uint32_t)MCOperand_getImm(op2), |
125 | 550 | false, true); |
126 | 550 | } |
127 | 1.23k | break; |
128 | 1.18k | case BPF_MODE_MEM: |
129 | 1.18k | if (EBPF_MODE(MI->csh->mode)) { |
130 | | /* ldx{w,h,b,dw} dst, [src+off] */ |
131 | 912 | push_op_reg(bpf, |
132 | 912 | MCOperand_getReg( |
133 | 912 | MCInst_getOperand(MI, 0)), |
134 | 912 | CS_AC_WRITE); |
135 | 912 | op = MCInst_getOperand(MI, 1); |
136 | 912 | op2 = MCInst_getOperand(MI, 2); |
137 | 912 | push_op_mem(bpf, MCOperand_getReg(op), |
138 | 912 | (uint32_t)MCOperand_getImm(op2), |
139 | 912 | true, false); |
140 | 912 | } else { |
141 | 274 | push_op_mmem(bpf, |
142 | 274 | (uint32_t)MCOperand_getImm( |
143 | 274 | MCInst_getOperand(MI, 0))); |
144 | 274 | } |
145 | 1.18k | break; |
146 | 197 | case BPF_MODE_LEN: |
147 | 197 | push_op_ext(bpf, BPF_EXT_LEN); |
148 | 197 | break; |
149 | 196 | case BPF_MODE_MSH: |
150 | 196 | op = MCInst_getOperand(MI, 0); |
151 | 196 | push_op_msh(bpf, (uint32_t)MCOperand_getImm(op)); |
152 | 196 | break; |
153 | | /* case BPF_MODE_XADD: // not exists */ |
154 | 5.57k | } |
155 | 5.57k | return; |
156 | 5.57k | } |
157 | 14.4k | if (BPF_CLASS(opcode) == BPF_CLASS_ST || |
158 | 14.4k | BPF_CLASS(opcode) == BPF_CLASS_STX) { |
159 | 1.79k | if (!EBPF_MODE(MI->csh->mode)) { |
160 | | // cBPF has only one case - st* M[k] |
161 | 98 | push_op_mmem(bpf, (uint32_t)MCOperand_getImm( |
162 | 98 | MCInst_getOperand(MI, 0))); |
163 | 98 | return; |
164 | 98 | } |
165 | | /* eBPF has two cases: |
166 | | * - st [dst + off], src |
167 | | * - xadd [dst + off], src |
168 | | * they have same form of operands. |
169 | | */ |
170 | 1.69k | op = MCInst_getOperand(MI, 0); |
171 | 1.69k | op2 = MCInst_getOperand(MI, 1); |
172 | 1.69k | push_op_mem(bpf, MCOperand_getReg(op), |
173 | 1.69k | (uint32_t)MCOperand_getImm(op2), true, false); |
174 | | |
175 | 1.69k | op = MCInst_getOperand(MI, 2); |
176 | 1.69k | if (MCOperand_isImm(op)) |
177 | 865 | push_op_imm(bpf, MCOperand_getImm(op), false); |
178 | 831 | else if (MCOperand_isReg(op)) |
179 | 831 | push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ); |
180 | 1.69k | return; |
181 | 1.79k | } |
182 | | |
183 | 12.6k | { |
184 | 12.6k | const bool is_jmp32 = EBPF_MODE(MI->csh->mode) && |
185 | 12.6k | (BPF_CLASS(opcode) == BPF_CLASS_JMP32); |
186 | 12.6k | if (BPF_CLASS(opcode) == BPF_CLASS_JMP || is_jmp32) { |
187 | 22.6k | for (size_t i = 0; i < mc_op_count; i++) { |
188 | 16.3k | op = MCInst_getOperand(MI, i); |
189 | 16.3k | 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 | 11.3k | if ((BPF_OP(opcode) == BPF_JUMP_JA && |
199 | 11.3k | !is_jmp32) || |
200 | 11.3k | (!EBPF_MODE(MI->csh->mode) && |
201 | 10.9k | i >= 1) || |
202 | 11.3k | (EBPF_MODE(MI->csh->mode) && |
203 | 9.41k | i == 2)) |
204 | 6.30k | push_op_off( |
205 | 6.30k | bpf, |
206 | 6.30k | MCOperand_getImm(op), |
207 | 6.30k | EBPF_MODE( |
208 | 6.30k | MI->csh->mode)); |
209 | 5.03k | else |
210 | 5.03k | push_op_imm( |
211 | 5.03k | bpf, |
212 | 5.03k | MCOperand_getImm(op), |
213 | 5.03k | true); |
214 | 11.3k | } else if (MCOperand_isReg(op)) { |
215 | 4.99k | push_op_reg(bpf, MCOperand_getReg(op), |
216 | 4.99k | CS_AC_READ); |
217 | 4.99k | } |
218 | 16.3k | } |
219 | 6.36k | return; |
220 | 6.36k | } |
221 | 12.6k | } |
222 | | |
223 | 6.29k | if (!EBPF_MODE(MI->csh->mode)) { |
224 | | /* In cBPF mode, all registers in operands are accessed as read */ |
225 | 5.20k | for (size_t i = 0; i < mc_op_count; i++) { |
226 | 2.44k | op = MCInst_getOperand(MI, i); |
227 | 2.44k | if (MCOperand_isImm(op)) |
228 | 1.04k | push_op_imm(bpf, MCOperand_getImm(op), false); |
229 | 1.40k | else if (MCOperand_isReg(op)) |
230 | 1.40k | push_op_reg(bpf, MCOperand_getReg(op), |
231 | 1.40k | CS_AC_READ); |
232 | 2.44k | } |
233 | 2.75k | return; |
234 | 2.75k | } |
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 | 3.54k | if (mc_op_count == 1) { |
248 | 513 | op = MCInst_getOperand(MI, 0); |
249 | 513 | push_op_reg(bpf, MCOperand_getReg(op), |
250 | 513 | CS_AC_READ | CS_AC_WRITE); |
251 | 3.02k | } else { // if (mc_op_count == 2) |
252 | 3.02k | op = MCInst_getOperand(MI, 0); |
253 | 3.02k | push_op_reg(bpf, MCOperand_getReg(op), |
254 | 3.02k | CS_AC_READ | CS_AC_WRITE); |
255 | | |
256 | 3.02k | op = MCInst_getOperand(MI, 1); |
257 | 3.02k | if (MCOperand_isImm(op)) |
258 | 2.69k | push_op_imm(bpf, MCOperand_getImm(op), false); |
259 | 334 | else if (MCOperand_isReg(op)) |
260 | 334 | push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ); |
261 | 3.02k | } |
262 | 3.54k | } |
263 | | |
264 | | static void print_operand(MCInst *MI, struct SStream *O, const cs_bpf_op *op) |
265 | 35.7k | { |
266 | 35.7k | switch (op->type) { |
267 | 0 | case BPF_OP_INVALID: |
268 | 0 | SStream_concat(O, "invalid"); |
269 | 0 | break; |
270 | 12.4k | case BPF_OP_REG: |
271 | 12.4k | SStream_concat(O, BPF_reg_name((csh)MI->csh, op->reg)); |
272 | 12.4k | break; |
273 | 10.6k | case BPF_OP_IMM: |
274 | 10.6k | if (op->is_signed) |
275 | 5.03k | printInt32Hex(O, op->imm); |
276 | 5.61k | else |
277 | 5.61k | SStream_concat(O, "0x%" PRIx64, op->imm); |
278 | 10.6k | break; |
279 | 6.30k | case BPF_OP_OFF: |
280 | 6.30k | if (op->is_signed) |
281 | 4.59k | printInt16HexOffset(O, op->off); |
282 | 1.71k | else |
283 | 1.71k | SStream_concat(O, "+0x%" PRIx32, op->off); |
284 | 6.30k | break; |
285 | 5.58k | case BPF_OP_MEM: |
286 | 5.58k | SStream_concat(O, "["); |
287 | | |
288 | 5.58k | if (op->is_pkt && EBPF_MODE(MI->csh->mode)) { |
289 | 1.66k | SStream_concat(O, "skb"); |
290 | | |
291 | 1.66k | if (op->mem.base != BPF_REG_INVALID) |
292 | 683 | SStream_concat(O, "+%s", |
293 | 683 | BPF_reg_name((csh)MI->csh, |
294 | 683 | op->mem.base)); |
295 | 977 | else { |
296 | 977 | if (op->is_signed) |
297 | 977 | printInt32HexOffset(O, op->mem.disp); |
298 | 0 | else |
299 | 0 | SStream_concat(O, "+0x%" PRIx32, |
300 | 0 | op->mem.disp); |
301 | 977 | } |
302 | 3.92k | } else { |
303 | 3.92k | if (op->mem.base != BPF_REG_INVALID) |
304 | 3.15k | SStream_concat(O, BPF_reg_name((csh)MI->csh, |
305 | 3.15k | op->mem.base)); |
306 | 3.92k | if (op->mem.disp != 0) { |
307 | 3.78k | if (op->mem.base != BPF_REG_INVALID) { |
308 | | // if operation is signed, then it always uses off, not k |
309 | 3.08k | if (op->is_signed) |
310 | 2.54k | printInt16HexOffset( |
311 | 2.54k | O, op->mem.disp); |
312 | 547 | else if (op->is_pkt) |
313 | 547 | SStream_concat(O, "+0x%" PRIx32, |
314 | 547 | op->mem.disp); |
315 | 0 | else |
316 | 0 | SStream_concat(O, "+0x%" PRIx16, |
317 | 0 | op->mem.disp); |
318 | 3.08k | } else |
319 | 699 | SStream_concat(O, "0x%" PRIx32, |
320 | 699 | op->mem.disp); |
321 | 3.78k | } |
322 | | |
323 | 3.92k | if (op->mem.base == BPF_REG_INVALID && |
324 | 3.92k | op->mem.disp == 0) |
325 | 71 | SStream_concat(O, "0x0"); |
326 | 3.92k | } |
327 | | |
328 | 5.58k | SStream_concat(O, "]"); |
329 | 5.58k | break; |
330 | 372 | case BPF_OP_MMEM: |
331 | 372 | SStream_concat(O, "m[0x%x]", op->mmem); |
332 | 372 | break; |
333 | 196 | case BPF_OP_MSH: |
334 | 196 | SStream_concat(O, "4*([0x%x]&0xf)", op->msh); |
335 | 196 | break; |
336 | 197 | case BPF_OP_EXT: |
337 | 197 | switch (op->ext) { |
338 | 197 | case BPF_EXT_LEN: |
339 | 197 | SStream_concat(O, "#len"); |
340 | 197 | break; |
341 | 197 | } |
342 | 197 | break; |
343 | 35.7k | } |
344 | 35.7k | } |
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 | 34.5k | { |
353 | 34.5k | cs_bpf bpf = { 0 }; |
354 | | |
355 | | /* set pubOpcode as instruction id */ |
356 | 34.5k | SStream_concat(O, BPF_insn_name((csh)MI->csh, MCInst_getOpcodePub(MI))); |
357 | 34.5k | convert_operands(MI, &bpf); |
358 | 92.6k | for (size_t i = 0; i < bpf.op_count; i++) { |
359 | 58.1k | if (i == 0) |
360 | 33.1k | SStream_concat(O, "\t"); |
361 | 24.9k | else |
362 | 24.9k | SStream_concat(O, ", "); |
363 | 58.1k | print_operand(MI, O, &bpf.operands[i]); |
364 | 58.1k | } |
365 | | |
366 | 34.5k | #ifndef CAPSTONE_DIET |
367 | 34.5k | if (detail_is_set(MI)) { |
368 | 34.5k | MI->flat_insn->detail->bpf = bpf; |
369 | 34.5k | } |
370 | 34.5k | #endif |
371 | 34.5k | } |