Coverage Report

Created: 2025-11-16 06:46

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