/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.3k | { |
15 | 35.3k | assert(bpf->op_count < 3); |
16 | 35.3k | return &bpf->operands[bpf->op_count++]; |
17 | 35.3k | } |
18 | | |
19 | | static void push_op_reg(cs_bpf *bpf, bpf_op_type val, uint8_t ac_mode) |
20 | 14.5k | { |
21 | 14.5k | cs_bpf_op *op = expand_bpf_operands(bpf); |
22 | | |
23 | 14.5k | op->type = BPF_OP_REG; |
24 | 14.5k | op->reg = val; |
25 | 14.5k | op->access = ac_mode; |
26 | 14.5k | } |
27 | | |
28 | | static void push_op_imm(cs_bpf *bpf, uint64_t val, const bool is_signed) |
29 | 11.3k | { |
30 | 11.3k | cs_bpf_op *op = expand_bpf_operands(bpf); |
31 | | |
32 | 11.3k | op->type = BPF_OP_IMM; |
33 | 11.3k | op->imm = val; |
34 | 11.3k | op->is_signed = is_signed; |
35 | 11.3k | } |
36 | | |
37 | | static void push_op_off(cs_bpf *bpf, uint32_t val, const bool is_signed) |
38 | 6.45k | { |
39 | 6.45k | cs_bpf_op *op = expand_bpf_operands(bpf); |
40 | | |
41 | 6.45k | op->type = BPF_OP_OFF; |
42 | 6.45k | op->off = val; |
43 | 6.45k | op->is_signed = is_signed; |
44 | 6.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 | 6.05k | { |
49 | 6.05k | cs_bpf_op *op = expand_bpf_operands(bpf); |
50 | | |
51 | 6.05k | op->type = BPF_OP_MEM; |
52 | 6.05k | op->mem.base = reg; |
53 | 6.05k | op->mem.disp = val; |
54 | 6.05k | op->is_signed = is_signed; |
55 | 6.05k | op->is_pkt = is_pkt; |
56 | 6.05k | } |
57 | | |
58 | | static void push_op_mmem(cs_bpf *bpf, uint32_t val) |
59 | 570 | { |
60 | 570 | cs_bpf_op *op = expand_bpf_operands(bpf); |
61 | | |
62 | 570 | op->type = BPF_OP_MMEM; |
63 | 570 | op->mmem = val; |
64 | 570 | } |
65 | | |
66 | | static void push_op_msh(cs_bpf *bpf, uint32_t val) |
67 | 325 | { |
68 | 325 | cs_bpf_op *op = expand_bpf_operands(bpf); |
69 | | |
70 | 325 | op->type = BPF_OP_MSH; |
71 | 325 | op->msh = val; |
72 | 325 | } |
73 | | |
74 | | static void push_op_ext(cs_bpf *bpf, bpf_ext_type val) |
75 | 229 | { |
76 | 229 | cs_bpf_op *op = expand_bpf_operands(bpf); |
77 | | |
78 | 229 | op->type = BPF_OP_EXT; |
79 | 229 | op->ext = val; |
80 | 229 | } |
81 | | |
82 | | static void convert_operands(MCInst *MI, cs_bpf *bpf) |
83 | 19.9k | { |
84 | 19.9k | unsigned opcode = MCInst_getOpcode(MI); |
85 | 19.9k | unsigned mc_op_count = MCInst_getNumOperands(MI); |
86 | 19.9k | MCOperand *op; |
87 | 19.9k | MCOperand *op2; |
88 | | |
89 | 19.9k | bpf->op_count = 0; |
90 | 19.9k | if (BPF_CLASS(opcode) == BPF_CLASS_LD || |
91 | 15.7k | BPF_CLASS(opcode) == BPF_CLASS_LDX) { |
92 | 5.34k | switch (BPF_MODE(opcode)) { |
93 | 952 | case BPF_MODE_IMM: |
94 | 952 | if (EBPF_MODE(MI->csh->mode)) { |
95 | 205 | push_op_reg(bpf, |
96 | 205 | MCOperand_getReg( |
97 | 205 | MCInst_getOperand(MI, 0)), |
98 | 205 | CS_AC_WRITE); |
99 | 205 | push_op_imm(bpf, |
100 | 205 | MCOperand_getImm( |
101 | 205 | MCInst_getOperand(MI, 1)), |
102 | 205 | false); |
103 | 747 | } else { |
104 | 747 | push_op_imm(bpf, |
105 | 747 | MCOperand_getImm( |
106 | 747 | MCInst_getOperand(MI, 0)), |
107 | 747 | false); |
108 | 747 | } |
109 | 952 | break; |
110 | 1.83k | case BPF_MODE_ABS: |
111 | 1.83k | op = MCInst_getOperand(MI, 0); |
112 | 1.83k | push_op_mem(bpf, BPF_REG_INVALID, |
113 | 1.83k | (uint32_t)MCOperand_getImm(op), |
114 | 1.83k | EBPF_MODE(MI->csh->mode), true); |
115 | 1.83k | break; |
116 | 1.12k | case BPF_MODE_IND: |
117 | 1.12k | op = MCInst_getOperand(MI, 0); |
118 | 1.12k | if (EBPF_MODE(MI->csh->mode)) |
119 | 722 | push_op_mem(bpf, MCOperand_getReg(op), 0x0, |
120 | 722 | true, true); |
121 | 407 | else { |
122 | 407 | op2 = MCInst_getOperand(MI, 1); |
123 | 407 | push_op_mem(bpf, MCOperand_getReg(op), |
124 | 407 | (uint32_t)MCOperand_getImm(op2), |
125 | 407 | false, true); |
126 | 407 | } |
127 | 1.12k | break; |
128 | 1.03k | case BPF_MODE_MEM: |
129 | 1.03k | if (EBPF_MODE(MI->csh->mode)) { |
130 | | /* ldx{w,h,b,dw} dst, [src+off] */ |
131 | 823 | push_op_reg(bpf, |
132 | 823 | MCOperand_getReg( |
133 | 823 | MCInst_getOperand(MI, 0)), |
134 | 823 | CS_AC_WRITE); |
135 | 823 | op = MCInst_getOperand(MI, 1); |
136 | 823 | op2 = MCInst_getOperand(MI, 2); |
137 | 823 | push_op_mem(bpf, MCOperand_getReg(op), |
138 | 823 | (uint32_t)MCOperand_getImm(op2), |
139 | 823 | true, false); |
140 | 823 | } else { |
141 | 214 | push_op_mmem(bpf, |
142 | 214 | (uint32_t)MCOperand_getImm( |
143 | 214 | MCInst_getOperand(MI, 0))); |
144 | 214 | } |
145 | 1.03k | break; |
146 | 195 | case BPF_MODE_LEN: |
147 | 195 | push_op_ext(bpf, BPF_EXT_LEN); |
148 | 195 | break; |
149 | 195 | case BPF_MODE_MSH: |
150 | 195 | op = MCInst_getOperand(MI, 0); |
151 | 195 | push_op_msh(bpf, (uint32_t)MCOperand_getImm(op)); |
152 | 195 | break; |
153 | | /* case BPF_MODE_XADD: // not exists */ |
154 | 5.34k | } |
155 | 5.34k | return; |
156 | 5.34k | } |
157 | 14.5k | if (BPF_CLASS(opcode) == BPF_CLASS_ST || |
158 | 13.6k | BPF_CLASS(opcode) == BPF_CLASS_STX) { |
159 | 2.07k | if (!EBPF_MODE(MI->csh->mode)) { |
160 | | // cBPF has only one case - st* M[k] |
161 | 262 | push_op_mmem(bpf, (uint32_t)MCOperand_getImm( |
162 | 262 | MCInst_getOperand(MI, 0))); |
163 | 262 | return; |
164 | 262 | } |
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.81k | op = MCInst_getOperand(MI, 0); |
171 | 1.81k | op2 = MCInst_getOperand(MI, 1); |
172 | 1.81k | push_op_mem(bpf, MCOperand_getReg(op), |
173 | 1.81k | (uint32_t)MCOperand_getImm(op2), true, false); |
174 | | |
175 | 1.81k | op = MCInst_getOperand(MI, 2); |
176 | 1.81k | if (MCOperand_isImm(op)) |
177 | 879 | push_op_imm(bpf, MCOperand_getImm(op), false); |
178 | 935 | else if (MCOperand_isReg(op)) |
179 | 935 | push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ); |
180 | 1.81k | return; |
181 | 2.07k | } |
182 | | |
183 | 12.4k | { |
184 | 12.4k | const bool is_jmp32 = EBPF_MODE(MI->csh->mode) && |
185 | 9.15k | (BPF_CLASS(opcode) == BPF_CLASS_JMP32); |
186 | 12.4k | if (BPF_CLASS(opcode) == BPF_CLASS_JMP || is_jmp32) { |
187 | 21.9k | for (size_t i = 0; i < mc_op_count; i++) { |
188 | 15.8k | op = MCInst_getOperand(MI, i); |
189 | 15.8k | 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.8k | if ((BPF_OP(opcode) == BPF_JUMP_JA && |
199 | 601 | !is_jmp32) || |
200 | 10.4k | (!EBPF_MODE(MI->csh->mode) && |
201 | 1.79k | i >= 1) || |
202 | 8.99k | (EBPF_MODE(MI->csh->mode) && |
203 | 8.64k | i == 2)) |
204 | 6.14k | push_op_off( |
205 | 6.14k | bpf, |
206 | 6.14k | MCOperand_getImm(op), |
207 | 6.14k | EBPF_MODE( |
208 | 6.14k | MI->csh->mode)); |
209 | 4.69k | else |
210 | 4.69k | push_op_imm( |
211 | 4.69k | bpf, |
212 | 4.69k | MCOperand_getImm(op), |
213 | 4.69k | true); |
214 | 10.8k | } else if (MCOperand_isReg(op)) { |
215 | 5.03k | push_op_reg(bpf, MCOperand_getReg(op), |
216 | 5.03k | CS_AC_READ); |
217 | 5.03k | } |
218 | 15.8k | } |
219 | 6.07k | return; |
220 | 6.07k | } |
221 | 12.4k | } |
222 | | |
223 | 6.40k | if (!EBPF_MODE(MI->csh->mode)) { |
224 | | /* In cBPF mode, all registers in operands are accessed as read */ |
225 | 4.49k | for (size_t i = 0; i < mc_op_count; i++) { |
226 | 2.09k | op = MCInst_getOperand(MI, i); |
227 | 2.09k | if (MCOperand_isImm(op)) |
228 | 898 | push_op_imm(bpf, MCOperand_getImm(op), false); |
229 | 1.19k | else if (MCOperand_isReg(op)) |
230 | 1.19k | push_op_reg(bpf, MCOperand_getReg(op), |
231 | 1.19k | CS_AC_READ); |
232 | 2.09k | } |
233 | 2.40k | return; |
234 | 2.40k | } |
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.00k | if (mc_op_count == 1) { |
248 | 894 | op = MCInst_getOperand(MI, 0); |
249 | 894 | push_op_reg(bpf, MCOperand_getReg(op), |
250 | 894 | CS_AC_READ | CS_AC_WRITE); |
251 | 3.10k | } else { // if (mc_op_count == 2) |
252 | 3.10k | op = MCInst_getOperand(MI, 0); |
253 | 3.10k | push_op_reg(bpf, MCOperand_getReg(op), |
254 | 3.10k | CS_AC_READ | CS_AC_WRITE); |
255 | | |
256 | 3.10k | op = MCInst_getOperand(MI, 1); |
257 | 3.10k | if (MCOperand_isImm(op)) |
258 | 2.67k | push_op_imm(bpf, MCOperand_getImm(op), false); |
259 | 427 | else if (MCOperand_isReg(op)) |
260 | 427 | push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ); |
261 | 3.10k | } |
262 | 4.00k | } |
263 | | |
264 | | static void print_operand(MCInst *MI, struct SStream *O, const cs_bpf_op *op) |
265 | 35.3k | { |
266 | 35.3k | 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.1k | case BPF_OP_IMM: |
274 | 10.1k | if (op->is_signed) |
275 | 4.69k | printInt32Hex(O, op->imm); |
276 | 5.40k | else |
277 | 5.40k | SStream_concat(O, "0x%" PRIx64, op->imm); |
278 | 10.1k | break; |
279 | 6.14k | case BPF_OP_OFF: |
280 | 6.14k | if (op->is_signed) |
281 | 4.49k | printInt16HexOffset(O, op->off); |
282 | 1.64k | else |
283 | 1.64k | SStream_concat(O, "+0x%" PRIx32, op->off); |
284 | 6.14k | break; |
285 | 5.60k | case BPF_OP_MEM: |
286 | 5.60k | SStream_concat(O, "["); |
287 | | |
288 | 5.60k | if (op->is_pkt && EBPF_MODE(MI->csh->mode)) { |
289 | 1.73k | SStream_concat(O, "skb"); |
290 | | |
291 | 1.73k | if (op->mem.base != BPF_REG_INVALID) |
292 | 722 | SStream_concat(O, "+%s", |
293 | 722 | BPF_reg_name((csh)MI->csh, |
294 | 722 | op->mem.base)); |
295 | 1.01k | else { |
296 | 1.01k | if (op->is_signed) |
297 | 1.01k | printInt32HexOffset(O, op->mem.disp); |
298 | 0 | else |
299 | 0 | SStream_concat(O, "+0x%" PRIx32, |
300 | 0 | op->mem.disp); |
301 | 1.01k | } |
302 | 3.86k | } else { |
303 | 3.86k | if (op->mem.base != BPF_REG_INVALID) |
304 | 3.04k | SStream_concat(O, BPF_reg_name((csh)MI->csh, |
305 | 3.04k | op->mem.base)); |
306 | 3.86k | if (op->mem.disp != 0) { |
307 | 3.50k | if (op->mem.base != BPF_REG_INVALID) { |
308 | | // if operation is signed, then it always uses off, not k |
309 | 2.90k | if (op->is_signed) |
310 | 2.49k | printInt16HexOffset( |
311 | 2.49k | O, op->mem.disp); |
312 | 407 | else if (op->is_pkt) |
313 | 407 | SStream_concat(O, "+0x%" PRIx32, |
314 | 407 | op->mem.disp); |
315 | 0 | else |
316 | 0 | SStream_concat(O, "+0x%" PRIx16, |
317 | 0 | op->mem.disp); |
318 | 2.90k | } else |
319 | 606 | SStream_concat(O, "0x%" PRIx32, |
320 | 606 | op->mem.disp); |
321 | 3.50k | } |
322 | | |
323 | 3.86k | if (op->mem.base == BPF_REG_INVALID && |
324 | 820 | op->mem.disp == 0) |
325 | 214 | SStream_concat(O, "0x0"); |
326 | 3.86k | } |
327 | | |
328 | 5.60k | SStream_concat(O, "]"); |
329 | 5.60k | break; |
330 | 476 | case BPF_OP_MMEM: |
331 | 476 | SStream_concat(O, "m[0x%x]", op->mmem); |
332 | 476 | break; |
333 | 195 | case BPF_OP_MSH: |
334 | 195 | SStream_concat(O, "4*([0x%x]&0xf)", op->msh); |
335 | 195 | break; |
336 | 195 | case BPF_OP_EXT: |
337 | 195 | switch (op->ext) { |
338 | 195 | case BPF_EXT_LEN: |
339 | 195 | SStream_concat(O, "#len"); |
340 | 195 | break; |
341 | 195 | } |
342 | 195 | break; |
343 | 35.3k | } |
344 | 35.3k | } |
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 | 22.8k | { |
353 | 22.8k | cs_bpf bpf = { 0 }; |
354 | | |
355 | | /* set pubOpcode as instruction id */ |
356 | 22.8k | SStream_concat(O, BPF_insn_name((csh)MI->csh, MCInst_getOpcodePub(MI))); |
357 | 22.8k | convert_operands(MI, &bpf); |
358 | 62.3k | for (size_t i = 0; i < bpf.op_count; i++) { |
359 | 39.5k | if (i == 0) |
360 | 22.1k | SStream_concat(O, "\t"); |
361 | 17.3k | else |
362 | 17.3k | SStream_concat(O, ", "); |
363 | 39.5k | print_operand(MI, O, &bpf.operands[i]); |
364 | 39.5k | } |
365 | | |
366 | 22.8k | #ifndef CAPSTONE_DIET |
367 | 22.8k | if (detail_is_set(MI)) { |
368 | 22.8k | MI->flat_insn->detail->bpf = bpf; |
369 | 22.8k | } |
370 | 22.8k | #endif |
371 | 22.8k | } |