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