Coverage Report

Created: 2025-11-24 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonev5/arch/BPF/BPFInstPrinter.c
Line
Count
Source
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
4.20k
{
12
  /* assert(bpf->op_count < 3); */
13
4.20k
  return &bpf->operands[bpf->op_count++];
14
4.20k
}
15
16
static void push_op_reg(cs_bpf *bpf, bpf_op_type val, uint8_t ac_mode)
17
14.5k
{
18
14.5k
  cs_bpf_op *op = expand_bpf_operands(bpf);
19
20
14.5k
  op->type = BPF_OP_REG;
21
14.5k
  op->reg = val;
22
14.5k
  op->access = ac_mode;
23
14.5k
}
24
25
static void push_op_imm(cs_bpf *bpf, uint64_t val)
26
11.3k
{
27
11.3k
  cs_bpf_op *op = expand_bpf_operands(bpf);
28
29
11.3k
  op->type = BPF_OP_IMM;
30
11.3k
  op->imm = val;
31
11.3k
}
32
33
static void push_op_off(cs_bpf *bpf, uint32_t val)
34
6.45k
{
35
6.45k
  cs_bpf_op *op = expand_bpf_operands(bpf);
36
37
6.45k
  op->type = BPF_OP_OFF;
38
6.45k
  op->off = val;
39
6.45k
}
40
41
static void push_op_mem(cs_bpf *bpf, bpf_reg reg, uint32_t val)
42
6.05k
{
43
6.05k
  cs_bpf_op *op = expand_bpf_operands(bpf);
44
45
6.05k
  op->type = BPF_OP_MEM;
46
6.05k
  op->mem.base = reg;
47
6.05k
  op->mem.disp = val;
48
6.05k
}
49
50
static void push_op_mmem(cs_bpf *bpf, uint32_t val)
51
570
{
52
570
  cs_bpf_op *op = expand_bpf_operands(bpf);
53
54
570
  op->type = BPF_OP_MMEM;
55
570
  op->mmem = val;
56
570
}
57
58
static void push_op_msh(cs_bpf *bpf, uint32_t val)
59
325
{
60
325
  cs_bpf_op *op = expand_bpf_operands(bpf);
61
62
325
  op->type = BPF_OP_MSH;
63
325
  op->msh = val;
64
325
}
65
66
static void push_op_ext(cs_bpf *bpf, bpf_ext_type val)
67
229
{
68
229
  cs_bpf_op *op = expand_bpf_operands(bpf);
69
70
229
  op->type = BPF_OP_EXT;
71
229
  op->ext = val;
72
229
}
73
74
static void convert_operands(MCInst *MI, cs_bpf *bpf)
75
2.95k
{
76
2.95k
  unsigned opcode = MCInst_getOpcode(MI);
77
2.95k
  unsigned mc_op_count = MCInst_getNumOperands(MI);
78
2.95k
  MCOperand *op;
79
2.95k
  MCOperand *op2;
80
2.95k
  unsigned i;
81
82
2.95k
  bpf->op_count = 0;
83
2.95k
  if (BPF_CLASS(opcode) == BPF_CLASS_LD || BPF_CLASS(opcode) == BPF_CLASS_LDX) {
84
422
    switch (BPF_MODE(opcode)) {
85
34
    case BPF_MODE_IMM:
86
34
      if (EBPF_MODE(MI->csh)) {
87
2
        push_op_reg(bpf, MCOperand_getReg(MCInst_getOperand(MI, 0)), CS_AC_WRITE);
88
2
        push_op_imm(bpf, MCOperand_getImm(MCInst_getOperand(MI, 1)));
89
32
      } else {
90
32
        push_op_imm(bpf, MCOperand_getImm(MCInst_getOperand(MI, 0)));
91
32
      }
92
34
      break;
93
33
    case BPF_MODE_ABS:
94
33
      op = MCInst_getOperand(MI, 0);
95
33
      push_op_mem(bpf, BPF_REG_INVALID, (uint32_t)MCOperand_getImm(op));
96
33
      break;
97
26
    case BPF_MODE_IND:
98
26
      op = MCInst_getOperand(MI, 0);
99
26
      op2 = MCInst_getOperand(MI, 1);
100
26
      push_op_mem(bpf, MCOperand_getReg(op), (uint32_t)MCOperand_getImm(op2));
101
26
      break;
102
165
    case BPF_MODE_MEM:
103
165
      if (EBPF_MODE(MI->csh)) {
104
        /* ldx{w,h,b,dw} dst, [src+off] */
105
149
        push_op_reg(bpf, MCOperand_getReg(MCInst_getOperand(MI, 0)), CS_AC_WRITE);
106
149
        op = MCInst_getOperand(MI, 1);
107
149
        op2 = MCInst_getOperand(MI, 2);
108
149
        push_op_mem(bpf, MCOperand_getReg(op), (uint32_t)MCOperand_getImm(op2));
109
149
      }
110
16
      else {
111
16
        push_op_mmem(bpf, (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, 0)));
112
16
      }
113
165
      break;
114
34
    case BPF_MODE_LEN:
115
34
      push_op_ext(bpf, BPF_EXT_LEN);
116
34
      break;
117
130
    case BPF_MODE_MSH:
118
130
      op = MCInst_getOperand(MI, 0);
119
130
      push_op_msh(bpf, (uint32_t)MCOperand_getImm(op));
120
130
      break;
121
    /* case BPF_MODE_XADD: // not exists */
122
422
    }
123
422
    return;
124
422
  }
125
2.53k
  if (BPF_CLASS(opcode) == BPF_CLASS_ST || BPF_CLASS(opcode) == BPF_CLASS_STX) {
126
322
    if (!EBPF_MODE(MI->csh)) {
127
      // cBPF has only one case - st* M[k]
128
78
      push_op_mmem(bpf, (uint32_t)MCOperand_getImm(MCInst_getOperand(MI, 0)));
129
78
      return;
130
78
    }
131
    /* eBPF has two cases:
132
     * - st [dst + off], src
133
     * - xadd [dst + off], src
134
     * they have same form of operands.
135
     */
136
244
    op = MCInst_getOperand(MI, 0);
137
244
    op2 = MCInst_getOperand(MI, 1);
138
244
    push_op_mem(bpf, MCOperand_getReg(op), (uint32_t)MCOperand_getImm(op2));
139
244
    op = MCInst_getOperand(MI, 2);
140
244
    if (MCOperand_isImm(op))
141
118
      push_op_imm(bpf, MCOperand_getImm(op));
142
126
    else if (MCOperand_isReg(op))
143
126
      push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
144
244
    return;
145
322
  }
146
147
2.21k
  if (BPF_CLASS(opcode) == BPF_CLASS_JMP) {
148
1.27k
    for (i = 0; i < mc_op_count; i++) {
149
868
      op = MCInst_getOperand(MI, i);
150
868
      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
508
        if (BPF_OP(opcode) == BPF_JUMP_JA ||
158
469
            (!EBPF_MODE(MI->csh) && i >= 1) ||
159
377
            (EBPF_MODE(MI->csh) && i == 2))
160
316
          push_op_off(bpf, (uint32_t)MCOperand_getImm(op));
161
192
        else
162
192
          push_op_imm(bpf, MCOperand_getImm(op));
163
508
      }
164
360
      else if (MCOperand_isReg(op)) {
165
360
        push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
166
360
      }
167
868
    }
168
407
    return;
169
407
  }
170
171
1.80k
  if (!EBPF_MODE(MI->csh)) {
172
    /* In cBPF mode, all registers in operands are accessed as read */
173
1.82k
    for (i = 0; i < mc_op_count; i++) {
174
835
      op = MCInst_getOperand(MI, i);
175
835
      if (MCOperand_isImm(op))
176
406
        push_op_imm(bpf, MCOperand_getImm(op));
177
429
      else if (MCOperand_isReg(op))
178
429
        push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
179
835
    }
180
986
    return;
181
986
  }
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
822
  if (mc_op_count == 1) {
195
278
    op = MCInst_getOperand(MI, 0);
196
278
    push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ | CS_AC_WRITE);
197
278
  }
198
544
  else { // if (mc_op_count == 2)
199
544
    op = MCInst_getOperand(MI, 0);
200
544
    push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ | CS_AC_WRITE);
201
202
544
    op = MCInst_getOperand(MI, 1);
203
544
    if (MCOperand_isImm(op))
204
534
      push_op_imm(bpf, MCOperand_getImm(op));
205
10
    else if (MCOperand_isReg(op))
206
10
      push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
207
544
  }
208
822
}
209
210
static void print_operand(MCInst *MI, struct SStream *O, const cs_bpf_op *op)
211
4.20k
{
212
4.20k
  switch (op->type) {
213
0
  case BPF_OP_INVALID:
214
0
    SStream_concat(O, "invalid");
215
0
    break;
216
1.89k
  case BPF_OP_REG:
217
1.89k
    SStream_concat(O, BPF_reg_name((csh)MI->csh, op->reg));
218
1.89k
    break;
219
1.28k
  case BPF_OP_IMM:
220
1.28k
    SStream_concat(O, "0x%" PRIx64, op->imm);
221
1.28k
    break;
222
316
  case BPF_OP_OFF:
223
316
    SStream_concat(O, "+0x%x", op->off);
224
316
    break;
225
452
  case BPF_OP_MEM:
226
452
    SStream_concat(O, "[");
227
452
    if (op->mem.base != BPF_REG_INVALID)
228
419
      SStream_concat(O, BPF_reg_name((csh)MI->csh, op->mem.base));
229
452
    if (op->mem.disp != 0) {
230
448
      if (op->mem.base != BPF_REG_INVALID)
231
416
        SStream_concat(O, "+");
232
448
      SStream_concat(O, "0x%x", op->mem.disp);
233
448
    }
234
452
    if (op->mem.base == BPF_REG_INVALID && op->mem.disp == 0) // special case
235
1
      SStream_concat(O, "0x0");
236
452
    SStream_concat(O, "]");
237
452
    break;
238
94
  case BPF_OP_MMEM:
239
94
    SStream_concat(O, "m[0x%x]", op->mmem);
240
94
    break;
241
130
  case BPF_OP_MSH:
242
130
    SStream_concat(O, "4*([0x%x]&0xf)", op->msh);
243
130
    break;
244
34
  case BPF_OP_EXT:
245
34
    switch (op->ext) {
246
34
    case BPF_EXT_LEN:
247
34
      SStream_concat(O, "#len");
248
34
      break;
249
34
    }
250
34
    break;
251
4.20k
  }
252
4.20k
}
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
22.8k
{
261
22.8k
  int i;
262
22.8k
  cs_insn insn;
263
22.8k
  cs_bpf bpf;
264
265
22.8k
  insn.detail = NULL;
266
  /* set pubOpcode as instruction id */
267
22.8k
  BPF_get_insn_id((cs_struct*)MI->csh, &insn, MCInst_getOpcode(MI));
268
22.8k
  MCInst_setOpcodePub(MI, insn.id);
269
270
22.8k
  SStream_concat(O, BPF_insn_name((csh)MI->csh, insn.id));
271
22.8k
  convert_operands(MI, &bpf);
272
62.3k
  for (i = 0; i < bpf.op_count; i++) {
273
39.5k
    if (i == 0)
274
22.1k
      SStream_concat(O, "\t");
275
17.3k
    else
276
17.3k
      SStream_concat(O, ", ");
277
39.5k
    print_operand(MI, O, &bpf.operands[i]);
278
39.5k
  }
279
280
22.8k
#ifndef CAPSTONE_DIET
281
22.8k
  if (MI->flat_insn->detail) {
282
22.8k
    MI->flat_insn->detail->bpf = bpf;
283
22.8k
  }
284
22.8k
#endif
285
22.8k
}