Coverage Report

Created: 2026-09-01 07:20

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