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