Coverage Report

Created: 2023-09-25 06:24

/src/capstonev5/arch/BPF/BPFInstPrinter.c
Line
Count
Source (jump to first uncovered line)
1
/* Capstone Disassembly Engine */
2
/* BPF Backend by david942j <david942j@gmail.com>, 2019 */
3
4
#include <capstone/platform.h>
5
6
#include "BPFConstants.h"
7
#include "BPFInstPrinter.h"
8
#include "BPFMapping.h"
9
10
static cs_bpf_op *expand_bpf_operands(cs_bpf *bpf)
11
8.46k
{
12
  /* assert(bpf->op_count < 3); */
13
8.46k
  return &bpf->operands[bpf->op_count++];
14
8.46k
}
15
16
static void push_op_reg(cs_bpf *bpf, bpf_op_type val, uint8_t ac_mode)
17
3.14k
{
18
3.14k
  cs_bpf_op *op = expand_bpf_operands(bpf);
19
20
3.14k
  op->type = BPF_OP_REG;
21
3.14k
  op->reg = val;
22
3.14k
  op->access = ac_mode;
23
3.14k
}
24
25
static void push_op_imm(cs_bpf *bpf, uint64_t val)
26
2.66k
{
27
2.66k
  cs_bpf_op *op = expand_bpf_operands(bpf);
28
29
2.66k
  op->type = BPF_OP_IMM;
30
2.66k
  op->imm = val;
31
2.66k
}
32
33
static void push_op_off(cs_bpf *bpf, uint32_t val)
34
1.19k
{
35
1.19k
  cs_bpf_op *op = expand_bpf_operands(bpf);
36
37
1.19k
  op->type = BPF_OP_OFF;
38
1.19k
  op->off = val;
39
1.19k
}
40
41
static void push_op_mem(cs_bpf *bpf, bpf_reg reg, uint32_t val)
42
1.23k
{
43
1.23k
  cs_bpf_op *op = expand_bpf_operands(bpf);
44
45
1.23k
  op->type = BPF_OP_MEM;
46
1.23k
  op->mem.base = reg;
47
1.23k
  op->mem.disp = val;
48
1.23k
}
49
50
static void push_op_mmem(cs_bpf *bpf, uint32_t val)
51
63
{
52
63
  cs_bpf_op *op = expand_bpf_operands(bpf);
53
54
63
  op->type = BPF_OP_MMEM;
55
63
  op->mmem = val;
56
63
}
57
58
static void push_op_msh(cs_bpf *bpf, uint32_t val)
59
22
{
60
22
  cs_bpf_op *op = expand_bpf_operands(bpf);
61
62
22
  op->type = BPF_OP_MSH;
63
22
  op->msh = val;
64
22
}
65
66
static void push_op_ext(cs_bpf *bpf, bpf_ext_type val)
67
151
{
68
151
  cs_bpf_op *op = expand_bpf_operands(bpf);
69
70
151
  op->type = BPF_OP_EXT;
71
151
  op->ext = val;
72
151
}
73
74
static void convert_operands(MCInst *MI, cs_bpf *bpf)
75
5.50k
{
76
5.50k
  unsigned opcode = MCInst_getOpcode(MI);
77
5.50k
  unsigned mc_op_count = MCInst_getNumOperands(MI);
78
5.50k
  MCOperand *op;
79
5.50k
  MCOperand *op2;
80
5.50k
  unsigned i;
81
82
5.50k
  bpf->op_count = 0;
83
5.50k
  if (BPF_CLASS(opcode) == BPF_CLASS_LD || BPF_CLASS(opcode) == BPF_CLASS_LDX) {
84
1.55k
    switch (BPF_MODE(opcode)) {
85
617
    case BPF_MODE_IMM:
86
617
      if (EBPF_MODE(MI->csh)) {
87
302
        push_op_reg(bpf, MCOperand_getReg(MCInst_getOperand(MI, 0)), CS_AC_WRITE);
88
302
        push_op_imm(bpf, MCOperand_getImm(MCInst_getOperand(MI, 1)));
89
315
      } else {
90
315
        push_op_imm(bpf, MCOperand_getImm(MCInst_getOperand(MI, 0)));
91
315
      }
92
617
      break;
93
353
    case BPF_MODE_ABS:
94
353
      op = MCInst_getOperand(MI, 0);
95
353
      push_op_mem(bpf, BPF_REG_INVALID, (uint32_t)MCOperand_getImm(op));
96
353
      break;
97
222
    case BPF_MODE_IND:
98
222
      op = MCInst_getOperand(MI, 0);
99
222
      op2 = MCInst_getOperand(MI, 1);
100
222
      push_op_mem(bpf, MCOperand_getReg(op), (uint32_t)MCOperand_getImm(op2));
101
222
      break;
102
190
    case BPF_MODE_MEM:
103
190
      if (EBPF_MODE(MI->csh)) {
104
        /* ldx{w,h,b,dw} dst, [src+off] */
105
138
        push_op_reg(bpf, MCOperand_getReg(MCInst_getOperand(MI, 0)), CS_AC_WRITE);
106
138
        op = MCInst_getOperand(MI, 1);
107
138
        op2 = MCInst_getOperand(MI, 2);
108
138
        push_op_mem(bpf, MCOperand_getReg(op), (uint32_t)MCOperand_getImm(op2));
109
138
      }
110
52
      else {
111
52
        push_op_mmem(bpf, (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, 0)));
112
52
      }
113
190
      break;
114
151
    case BPF_MODE_LEN:
115
151
      push_op_ext(bpf, BPF_EXT_LEN);
116
151
      break;
117
22
    case BPF_MODE_MSH:
118
22
      op = MCInst_getOperand(MI, 0);
119
22
      push_op_msh(bpf, (uint32_t)MCOperand_getImm(op));
120
22
      break;
121
    /* case BPF_MODE_XADD: // not exists */
122
1.55k
    }
123
1.55k
    return;
124
1.55k
  }
125
3.94k
  if (BPF_CLASS(opcode) == BPF_CLASS_ST || BPF_CLASS(opcode) == BPF_CLASS_STX) {
126
530
    if (!EBPF_MODE(MI->csh)) {
127
      // cBPF has only one case - st* M[k]
128
11
      push_op_mmem(bpf, (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, 0)));
129
11
      return;
130
11
    }
131
    /* eBPF has two cases:
132
     * - st [dst + off], src
133
     * - xadd [dst + off], src
134
     * they have same form of operands.
135
     */
136
519
    op = MCInst_getOperand(MI, 0);
137
519
    op2 = MCInst_getOperand(MI, 1);
138
519
    push_op_mem(bpf, MCOperand_getReg(op), (uint32_t)MCOperand_getImm(op2));
139
519
    op = MCInst_getOperand(MI, 2);
140
519
    if (MCOperand_isImm(op))
141
277
      push_op_imm(bpf, MCOperand_getImm(op));
142
242
    else if (MCOperand_isReg(op))
143
242
      push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
144
519
    return;
145
530
  }
146
147
3.41k
  if (BPF_CLASS(opcode) == BPF_CLASS_JMP) {
148
3.70k
    for (i = 0; i < mc_op_count; i++) {
149
2.42k
      op = MCInst_getOperand(MI, i);
150
2.42k
      if (MCOperand_isImm(op)) {
151
        /* decide the imm is BPF_OP_IMM or BPF_OP_OFF type here */
152
        /*
153
         * 1. ja +off
154
         * 2. j {x,k}, +jt, +jf // cBPF
155
         * 3. j dst_reg, {src_reg, k}, +off // eBPF
156
         */
157
1.81k
        if (BPF_OP(opcode) == BPF_JUMP_JA ||
158
1.81k
            (!EBPF_MODE(MI->csh) && i >= 1) ||
159
1.81k
            (EBPF_MODE(MI->csh) && i == 2))
160
1.19k
          push_op_off(bpf, (uint32_t)MCOperand_getImm(op));
161
622
        else
162
622
          push_op_imm(bpf, MCOperand_getImm(op));
163
1.81k
      }
164
605
      else if (MCOperand_isReg(op)) {
165
605
        push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
166
605
      }
167
2.42k
    }
168
1.28k
    return;
169
1.28k
  }
170
171
2.13k
  if (!EBPF_MODE(MI->csh)) {
172
    /* In cBPF mode, all registers in operands are accessed as read */
173
1.42k
    for (i = 0; i < mc_op_count; i++) {
174
627
      op = MCInst_getOperand(MI, i);
175
627
      if (MCOperand_isImm(op))
176
225
        push_op_imm(bpf, MCOperand_getImm(op));
177
402
      else if (MCOperand_isReg(op))
178
402
        push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
179
627
    }
180
797
    return;
181
797
  }
182
183
  /* remain cases are: eBPF mode && ALU */
184
  /* if (BPF_CLASS(opcode) == BPF_CLASS_ALU || BPF_CLASS(opcode) == BPF_CLASS_ALU64) */
185
186
  /* We have three types:
187
   * 1. {l,b}e dst               // dst = byteswap(dst)
188
   * 2. neg dst                  // dst = -dst
189
   * 3. <op> dst, {src_reg, imm} // dst = dst <op> src
190
   * so we can simply check the number of operands,
191
   * exactly one operand means we are in case 1. and 2.,
192
   * otherwise in case 3.
193
   */
194
1.34k
  if (mc_op_count == 1) {
195
304
    op = MCInst_getOperand(MI, 0);
196
304
    push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ | CS_AC_WRITE);
197
304
  }
198
1.03k
  else { // if (mc_op_count == 2)
199
1.03k
    op = MCInst_getOperand(MI, 0);
200
1.03k
    push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ | CS_AC_WRITE);
201
202
1.03k
    op = MCInst_getOperand(MI, 1);
203
1.03k
    if (MCOperand_isImm(op))
204
923
      push_op_imm(bpf, MCOperand_getImm(op));
205
113
    else if (MCOperand_isReg(op))
206
113
      push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
207
1.03k
  }
208
1.34k
}
209
210
static void print_operand(MCInst *MI, struct SStream *O, const cs_bpf_op *op)
211
8.46k
{
212
8.46k
  switch (op->type) {
213
0
  case BPF_OP_INVALID:
214
0
    SStream_concat(O, "invalid");
215
0
    break;
216
3.14k
  case BPF_OP_REG:
217
3.14k
    SStream_concat(O, BPF_reg_name((csh)MI->csh, op->reg));
218
3.14k
    break;
219
2.66k
  case BPF_OP_IMM:
220
2.66k
    SStream_concat(O, "0x%" PRIx64, op->imm);
221
2.66k
    break;
222
1.19k
  case BPF_OP_OFF:
223
1.19k
    SStream_concat(O, "+0x%x", op->off);
224
1.19k
    break;
225
1.23k
  case BPF_OP_MEM:
226
1.23k
    SStream_concat(O, "[");
227
1.23k
    if (op->mem.base != BPF_REG_INVALID)
228
879
      SStream_concat(O, BPF_reg_name((csh)MI->csh, op->mem.base));
229
1.23k
    if (op->mem.disp != 0) {
230
1.18k
      if (op->mem.base != BPF_REG_INVALID)
231
842
        SStream_concat(O, "+");
232
1.18k
      SStream_concat(O, "0x%x", op->mem.disp);
233
1.18k
    }
234
1.23k
    if (op->mem.base == BPF_REG_INVALID && op->mem.disp == 0) // special case
235
12
      SStream_concat(O, "0x0");
236
1.23k
    SStream_concat(O, "]");
237
1.23k
    break;
238
63
  case BPF_OP_MMEM:
239
63
    SStream_concat(O, "m[0x%x]", op->mmem);
240
63
    break;
241
22
  case BPF_OP_MSH:
242
22
    SStream_concat(O, "4*([0x%x]&0xf)", op->msh);
243
22
    break;
244
151
  case BPF_OP_EXT:
245
151
    switch (op->ext) {
246
151
    case BPF_EXT_LEN:
247
151
      SStream_concat(O, "#len");
248
151
      break;
249
151
    }
250
151
    break;
251
8.46k
  }
252
8.46k
}
253
254
/*
255
 * 1. human readable mnemonic
256
 * 2. set pubOpcode (BPF_INSN_*)
257
 * 3. set detail->bpf.operands
258
 * */
259
void BPF_printInst(MCInst *MI, struct SStream *O, void *PrinterInfo)
260
5.50k
{
261
5.50k
  int i;
262
5.50k
  cs_insn insn;
263
5.50k
  cs_bpf bpf;
264
265
5.50k
  insn.detail = NULL;
266
  /* set pubOpcode as instruction id */
267
5.50k
  BPF_get_insn_id((cs_struct*)MI->csh, &insn, MCInst_getOpcode(MI));
268
5.50k
  MCInst_setOpcodePub(MI, insn.id);
269
270
5.50k
  SStream_concat(O, BPF_insn_name((csh)MI->csh, insn.id));
271
5.50k
  convert_operands(MI, &bpf);
272
13.9k
  for (i = 0; i < bpf.op_count; i++) {
273
8.46k
    if (i == 0)
274
5.05k
      SStream_concat(O, "\t");
275
3.40k
    else
276
3.40k
      SStream_concat(O, ", ");
277
8.46k
    print_operand(MI, O, &bpf.operands[i]);
278
8.46k
  }
279
280
5.50k
#ifndef CAPSTONE_DIET
281
5.50k
  if (MI->flat_insn->detail) {
282
5.50k
    MI->flat_insn->detail->bpf = bpf;
283
5.50k
  }
284
5.50k
#endif
285
5.50k
}