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