/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.8k  | { | 
15  | 35.8k  |   assert(bpf->op_count < 3);  | 
16  | 35.8k  |   return &bpf->operands[bpf->op_count++];  | 
17  | 35.8k  | }  | 
18  |  |  | 
19  |  | static void push_op_reg(cs_bpf *bpf, bpf_op_type val, uint8_t ac_mode)  | 
20  | 20.7k  | { | 
21  | 20.7k  |   cs_bpf_op *op = expand_bpf_operands(bpf);  | 
22  |  |  | 
23  | 20.7k  |   op->type = BPF_OP_REG;  | 
24  | 20.7k  |   op->reg = val;  | 
25  | 20.7k  |   op->access = ac_mode;  | 
26  | 20.7k  | }  | 
27  |  |  | 
28  |  | static void push_op_imm(cs_bpf *bpf, uint64_t val, const bool is_signed)  | 
29  | 15.4k  | { | 
30  | 15.4k  |   cs_bpf_op *op = expand_bpf_operands(bpf);  | 
31  |  |  | 
32  | 15.4k  |   op->type = BPF_OP_IMM;  | 
33  | 15.4k  |   op->imm = val;  | 
34  | 15.4k  |   op->is_signed = is_signed;  | 
35  | 15.4k  | }  | 
36  |  |  | 
37  |  | static void push_op_off(cs_bpf *bpf, uint32_t val, const bool is_signed)  | 
38  | 9.29k  | { | 
39  | 9.29k  |   cs_bpf_op *op = expand_bpf_operands(bpf);  | 
40  |  |  | 
41  | 9.29k  |   op->type = BPF_OP_OFF;  | 
42  | 9.29k  |   op->off = val;  | 
43  | 9.29k  |   op->is_signed = is_signed;  | 
44  | 9.29k  | }  | 
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  | 10.1k  | { | 
49  | 10.1k  |   cs_bpf_op *op = expand_bpf_operands(bpf);  | 
50  |  |  | 
51  | 10.1k  |   op->type = BPF_OP_MEM;  | 
52  | 10.1k  |   op->mem.base = reg;  | 
53  | 10.1k  |   op->mem.disp = val;  | 
54  | 10.1k  |   op->is_signed = is_signed;  | 
55  | 10.1k  |   op->is_pkt = is_pkt;  | 
56  | 10.1k  | }  | 
57  |  |  | 
58  |  | static void push_op_mmem(cs_bpf *bpf, uint32_t val)  | 
59  | 1.08k  | { | 
60  | 1.08k  |   cs_bpf_op *op = expand_bpf_operands(bpf);  | 
61  |  |  | 
62  | 1.08k  |   op->type = BPF_OP_MMEM;  | 
63  | 1.08k  |   op->mmem = val;  | 
64  | 1.08k  | }  | 
65  |  |  | 
66  |  | static void push_op_msh(cs_bpf *bpf, uint32_t val)  | 
67  | 394  | { | 
68  | 394  |   cs_bpf_op *op = expand_bpf_operands(bpf);  | 
69  |  |  | 
70  | 394  |   op->type = BPF_OP_MSH;  | 
71  | 394  |   op->msh = val;  | 
72  | 394  | }  | 
73  |  |  | 
74  |  | static void push_op_ext(cs_bpf *bpf, bpf_ext_type val)  | 
75  | 474  | { | 
76  | 474  |   cs_bpf_op *op = expand_bpf_operands(bpf);  | 
77  |  |  | 
78  | 474  |   op->type = BPF_OP_EXT;  | 
79  | 474  |   op->ext = val;  | 
80  | 474  | }  | 
81  |  |  | 
82  |  | static void convert_operands(MCInst *MI, cs_bpf *bpf)  | 
83  | 19.8k  | { | 
84  | 19.8k  |   unsigned opcode = MCInst_getOpcode(MI);  | 
85  | 19.8k  |   unsigned mc_op_count = MCInst_getNumOperands(MI);  | 
86  | 19.8k  |   MCOperand *op;  | 
87  | 19.8k  |   MCOperand *op2;  | 
88  |  |  | 
89  | 19.8k  |   bpf->op_count = 0;  | 
90  | 19.8k  |   if (BPF_CLASS(opcode) == BPF_CLASS_LD ||  | 
91  | 16.3k  |       BPF_CLASS(opcode) == BPF_CLASS_LDX) { | 
92  | 5.08k  |     switch (BPF_MODE(opcode)) { | 
93  | 681  |     case BPF_MODE_IMM:  | 
94  | 681  |       if (EBPF_MODE(MI->csh->mode)) { | 
95  | 202  |         push_op_reg(bpf,  | 
96  | 202  |               MCOperand_getReg(  | 
97  | 202  |                 MCInst_getOperand(MI, 0)),  | 
98  | 202  |               CS_AC_WRITE);  | 
99  | 202  |         push_op_imm(bpf,  | 
100  | 202  |               MCOperand_getImm(  | 
101  | 202  |                 MCInst_getOperand(MI, 1)),  | 
102  | 202  |               false);  | 
103  | 479  |       } else { | 
104  | 479  |         push_op_imm(bpf,  | 
105  | 479  |               MCOperand_getImm(  | 
106  | 479  |                 MCInst_getOperand(MI, 0)),  | 
107  | 479  |               false);  | 
108  | 479  |       }  | 
109  | 681  |       break;  | 
110  | 1.63k  |     case BPF_MODE_ABS:  | 
111  | 1.63k  |       op = MCInst_getOperand(MI, 0);  | 
112  | 1.63k  |       push_op_mem(bpf, BPF_REG_INVALID,  | 
113  | 1.63k  |             (uint32_t)MCOperand_getImm(op),  | 
114  | 1.63k  |             EBPF_MODE(MI->csh->mode), true);  | 
115  | 1.63k  |       break;  | 
116  | 1.20k  |     case BPF_MODE_IND:  | 
117  | 1.20k  |       op = MCInst_getOperand(MI, 0);  | 
118  | 1.20k  |       if (EBPF_MODE(MI->csh->mode))  | 
119  | 735  |         push_op_mem(bpf, MCOperand_getReg(op), 0x0,  | 
120  | 735  |               true, true);  | 
121  | 466  |       else { | 
122  | 466  |         op2 = MCInst_getOperand(MI, 1);  | 
123  | 466  |         push_op_mem(bpf, MCOperand_getReg(op),  | 
124  | 466  |               (uint32_t)MCOperand_getImm(op2),  | 
125  | 466  |               false, true);  | 
126  | 466  |       }  | 
127  | 1.20k  |       break;  | 
128  | 1.16k  |     case BPF_MODE_MEM:  | 
129  | 1.16k  |       if (EBPF_MODE(MI->csh->mode)) { | 
130  |  |         /* ldx{w,h,b,dw} dst, [src+off] */ | 
131  | 968  |         push_op_reg(bpf,  | 
132  | 968  |               MCOperand_getReg(  | 
133  | 968  |                 MCInst_getOperand(MI, 0)),  | 
134  | 968  |               CS_AC_WRITE);  | 
135  | 968  |         op = MCInst_getOperand(MI, 1);  | 
136  | 968  |         op2 = MCInst_getOperand(MI, 2);  | 
137  | 968  |         push_op_mem(bpf, MCOperand_getReg(op),  | 
138  | 968  |               (uint32_t)MCOperand_getImm(op2),  | 
139  | 968  |               true, false);  | 
140  | 968  |       } else { | 
141  | 197  |         push_op_mmem(bpf,  | 
142  | 197  |                (uint32_t)MCOperand_getImm(  | 
143  | 197  |                  MCInst_getOperand(MI, 0)));  | 
144  | 197  |       }  | 
145  | 1.16k  |       break;  | 
146  | 212  |     case BPF_MODE_LEN:  | 
147  | 212  |       push_op_ext(bpf, BPF_EXT_LEN);  | 
148  | 212  |       break;  | 
149  | 194  |     case BPF_MODE_MSH:  | 
150  | 194  |       op = MCInst_getOperand(MI, 0);  | 
151  | 194  |       push_op_msh(bpf, (uint32_t)MCOperand_getImm(op));  | 
152  | 194  |       break;  | 
153  |  |       /* case BPF_MODE_XADD: // not exists */  | 
154  | 5.08k  |     }  | 
155  | 5.08k  |     return;  | 
156  | 5.08k  |   }  | 
157  | 14.7k  |   if (BPF_CLASS(opcode) == BPF_CLASS_ST ||  | 
158  | 13.8k  |       BPF_CLASS(opcode) == BPF_CLASS_STX) { | 
159  | 2.36k  |     if (!EBPF_MODE(MI->csh->mode)) { | 
160  |  |       // cBPF has only one case - st* M[k]  | 
161  | 102  |       push_op_mmem(bpf, (uint32_t)MCOperand_getImm(  | 
162  | 102  |               MCInst_getOperand(MI, 0)));  | 
163  | 102  |       return;  | 
164  | 102  |     }  | 
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.26k  |     op = MCInst_getOperand(MI, 0);  | 
171  | 2.26k  |     op2 = MCInst_getOperand(MI, 1);  | 
172  | 2.26k  |     push_op_mem(bpf, MCOperand_getReg(op),  | 
173  | 2.26k  |           (uint32_t)MCOperand_getImm(op2), true, false);  | 
174  |  |  | 
175  | 2.26k  |     op = MCInst_getOperand(MI, 2);  | 
176  | 2.26k  |     if (MCOperand_isImm(op))  | 
177  | 837  |       push_op_imm(bpf, MCOperand_getImm(op), false);  | 
178  | 1.43k  |     else if (MCOperand_isReg(op))  | 
179  | 1.43k  |       push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);  | 
180  | 2.26k  |     return;  | 
181  | 2.36k  |   }  | 
182  |  |  | 
183  | 12.4k  |   { | 
184  | 12.4k  |     const bool is_jmp32 = EBPF_MODE(MI->csh->mode) &&  | 
185  | 8.88k  |               (BPF_CLASS(opcode) == BPF_CLASS_JMP32);  | 
186  | 12.4k  |     if (BPF_CLASS(opcode) == BPF_CLASS_JMP || is_jmp32) { | 
187  | 22.0k  |       for (size_t i = 0; i < mc_op_count; i++) { | 
188  | 15.9k  |         op = MCInst_getOperand(MI, i);  | 
189  | 15.9k  |         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.9k  |           if ((BPF_OP(opcode) == BPF_JUMP_JA &&  | 
199  | 661  |                !is_jmp32) ||  | 
200  | 10.5k  |               (!EBPF_MODE(MI->csh->mode) &&  | 
201  | 1.94k  |                i >= 1) ||  | 
202  | 8.90k  |               (EBPF_MODE(MI->csh->mode) &&  | 
203  | 8.58k  |                i == 2))  | 
204  | 6.29k  |             push_op_off(  | 
205  | 6.29k  |               bpf,  | 
206  | 6.29k  |               MCOperand_getImm(op),  | 
207  | 6.29k  |               EBPF_MODE(  | 
208  | 6.29k  |                 MI->csh->mode));  | 
209  | 4.68k  |           else  | 
210  | 4.68k  |             push_op_imm(  | 
211  | 4.68k  |               bpf,  | 
212  | 4.68k  |               MCOperand_getImm(op),  | 
213  | 4.68k  |               true);  | 
214  | 10.9k  |         } else if (MCOperand_isReg(op)) { | 
215  | 4.98k  |           push_op_reg(bpf, MCOperand_getReg(op),  | 
216  | 4.98k  |                 CS_AC_READ);  | 
217  | 4.98k  |         }  | 
218  | 15.9k  |       }  | 
219  | 6.09k  |       return;  | 
220  | 6.09k  |     }  | 
221  | 12.4k  |   }  | 
222  |  |  | 
223  | 6.30k  |   if (!EBPF_MODE(MI->csh->mode)) { | 
224  |  |     /* In cBPF mode, all registers in operands are accessed as read */  | 
225  | 4.51k  |     for (size_t i = 0; i < mc_op_count; i++) { | 
226  | 2.01k  |       op = MCInst_getOperand(MI, i);  | 
227  | 2.01k  |       if (MCOperand_isImm(op))  | 
228  | 1.03k  |         push_op_imm(bpf, MCOperand_getImm(op), false);  | 
229  | 988  |       else if (MCOperand_isReg(op))  | 
230  | 988  |         push_op_reg(bpf, MCOperand_getReg(op),  | 
231  | 988  |               CS_AC_READ);  | 
232  | 2.01k  |     }  | 
233  | 2.49k  |     return;  | 
234  | 2.49k  |   }  | 
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.81k  |   if (mc_op_count == 1) { | 
248  | 618  |     op = MCInst_getOperand(MI, 0);  | 
249  | 618  |     push_op_reg(bpf, MCOperand_getReg(op),  | 
250  | 618  |           CS_AC_READ | CS_AC_WRITE);  | 
251  | 3.19k  |   } else { // if (mc_op_count == 2) | 
252  | 3.19k  |     op = MCInst_getOperand(MI, 0);  | 
253  | 3.19k  |     push_op_reg(bpf, MCOperand_getReg(op),  | 
254  | 3.19k  |           CS_AC_READ | CS_AC_WRITE);  | 
255  |  |  | 
256  | 3.19k  |     op = MCInst_getOperand(MI, 1);  | 
257  | 3.19k  |     if (MCOperand_isImm(op))  | 
258  | 2.80k  |       push_op_imm(bpf, MCOperand_getImm(op), false);  | 
259  | 388  |     else if (MCOperand_isReg(op))  | 
260  | 388  |       push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);  | 
261  | 3.19k  |   }  | 
262  | 3.81k  | }  | 
263  |  |  | 
264  |  | static void print_operand(MCInst *MI, struct SStream *O, const cs_bpf_op *op)  | 
265  | 35.8k  | { | 
266  | 35.8k  |   switch (op->type) { | 
267  | 0  |   case BPF_OP_INVALID:  | 
268  | 0  |     SStream_concat(O, "invalid");  | 
269  | 0  |     break;  | 
270  | 12.7k  |   case BPF_OP_REG:  | 
271  | 12.7k  |     SStream_concat(O, BPF_reg_name((csh)MI->csh, op->reg));  | 
272  | 12.7k  |     break;  | 
273  | 10.0k  |   case BPF_OP_IMM:  | 
274  | 10.0k  |     if (op->is_signed)  | 
275  | 4.68k  |       printInt32Hex(O, op->imm);  | 
276  | 5.35k  |     else  | 
277  | 5.35k  |       SStream_concat(O, "0x%" PRIx64, op->imm);  | 
278  | 10.0k  |     break;  | 
279  | 6.29k  |   case BPF_OP_OFF:  | 
280  | 6.29k  |     if (op->is_signed)  | 
281  | 4.45k  |       printInt16HexOffset(O, op->off);  | 
282  | 1.84k  |     else  | 
283  | 1.84k  |       SStream_concat(O, "+0x%" PRIx32, op->off);  | 
284  | 6.29k  |     break;  | 
285  | 6.06k  |   case BPF_OP_MEM:  | 
286  | 6.06k  |     SStream_concat(O, "[");  | 
287  |  |  | 
288  | 6.06k  |     if (op->is_pkt && EBPF_MODE(MI->csh->mode)) { | 
289  | 1.75k  |       SStream_concat(O, "skb");  | 
290  |  |  | 
291  | 1.75k  |       if (op->mem.base != BPF_REG_INVALID)  | 
292  | 735  |         SStream_concat(O, "+%s",  | 
293  | 735  |                  BPF_reg_name((csh)MI->csh,  | 
294  | 735  |                   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  | 4.31k  |     } else { | 
303  | 4.31k  |       if (op->mem.base != BPF_REG_INVALID)  | 
304  | 3.70k  |         SStream_concat(O, BPF_reg_name((csh)MI->csh,  | 
305  | 3.70k  |                      op->mem.base));  | 
306  | 4.31k  |       if (op->mem.disp != 0) { | 
307  | 4.08k  |         if (op->mem.base != BPF_REG_INVALID) { | 
308  |  |           // if operation is signed, then it always uses off, not k  | 
309  | 3.54k  |           if (op->is_signed)  | 
310  | 3.07k  |             printInt16HexOffset(  | 
311  | 3.07k  |               O, op->mem.disp);  | 
312  | 466  |           else if (op->is_pkt)  | 
313  | 466  |             SStream_concat(O, "+0x%" PRIx32,  | 
314  | 466  |                      op->mem.disp);  | 
315  | 0  |           else  | 
316  | 0  |             SStream_concat(O, "+0x%" PRIx16,  | 
317  | 0  |                      op->mem.disp);  | 
318  | 3.54k  |         } else  | 
319  | 539  |           SStream_concat(O, "0x%" PRIx32,  | 
320  | 539  |                    op->mem.disp);  | 
321  | 4.08k  |       }  | 
322  |  |  | 
323  | 4.31k  |       if (op->mem.base == BPF_REG_INVALID &&  | 
324  | 615  |           op->mem.disp == 0)  | 
325  | 76  |         SStream_concat(O, "0x0");  | 
326  | 4.31k  |     }  | 
327  |  |  | 
328  | 6.06k  |     SStream_concat(O, "]");  | 
329  | 6.06k  |     break;  | 
330  | 299  |   case BPF_OP_MMEM:  | 
331  | 299  |     SStream_concat(O, "m[0x%x]", op->mmem);  | 
332  | 299  |     break;  | 
333  | 194  |   case BPF_OP_MSH:  | 
334  | 194  |     SStream_concat(O, "4*([0x%x]&0xf)", op->msh);  | 
335  | 194  |     break;  | 
336  | 212  |   case BPF_OP_EXT:  | 
337  | 212  |     switch (op->ext) { | 
338  | 212  |     case BPF_EXT_LEN:  | 
339  | 212  |       SStream_concat(O, "#len");  | 
340  | 212  |       break;  | 
341  | 212  |     }  | 
342  | 212  |     break;  | 
343  | 35.8k  |   }  | 
344  | 35.8k  | }  | 
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.2k  | { | 
353  | 34.2k  |   cs_bpf bpf = { 0 }; | 
354  |  |  | 
355  |  |   /* set pubOpcode as instruction id */  | 
356  | 34.2k  |   SStream_concat(O, BPF_insn_name((csh)MI->csh, MCInst_getOpcodePub(MI)));  | 
357  | 34.2k  |   convert_operands(MI, &bpf);  | 
358  | 91.8k  |   for (size_t i = 0; i < bpf.op_count; i++) { | 
359  | 57.6k  |     if (i == 0)  | 
360  | 32.6k  |       SStream_concat(O, "\t");  | 
361  | 24.9k  |     else  | 
362  | 24.9k  |       SStream_concat(O, ", ");  | 
363  | 57.6k  |     print_operand(MI, O, &bpf.operands[i]);  | 
364  | 57.6k  |   }  | 
365  |  |  | 
366  | 34.2k  | #ifndef CAPSTONE_DIET  | 
367  | 34.2k  |   if (detail_is_set(MI)) { | 
368  | 34.2k  |     MI->flat_insn->detail->bpf = bpf;  | 
369  | 34.2k  |   }  | 
370  | 34.2k  | #endif  | 
371  | 34.2k  | }  |