Coverage Report

Created: 2025-10-14 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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.6k
{
15
35.6k
  assert(bpf->op_count < 3);
16
35.6k
  return &bpf->operands[bpf->op_count++];
17
35.6k
}
18
19
static void push_op_reg(cs_bpf *bpf, bpf_op_type val, uint8_t ac_mode)
20
12.6k
{
21
12.6k
  cs_bpf_op *op = expand_bpf_operands(bpf);
22
23
12.6k
  op->type = BPF_OP_REG;
24
12.6k
  op->reg = val;
25
12.6k
  op->access = ac_mode;
26
12.6k
}
27
28
static void push_op_imm(cs_bpf *bpf, uint64_t val, const bool is_signed)
29
9.91k
{
30
9.91k
  cs_bpf_op *op = expand_bpf_operands(bpf);
31
32
9.91k
  op->type = BPF_OP_IMM;
33
9.91k
  op->imm = val;
34
9.91k
  op->is_signed = is_signed;
35
9.91k
}
36
37
static void push_op_off(cs_bpf *bpf, uint32_t val, const bool is_signed)
38
5.80k
{
39
5.80k
  cs_bpf_op *op = expand_bpf_operands(bpf);
40
41
5.80k
  op->type = BPF_OP_OFF;
42
5.80k
  op->off = val;
43
5.80k
  op->is_signed = is_signed;
44
5.80k
}
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
6.44k
{
49
6.44k
  cs_bpf_op *op = expand_bpf_operands(bpf);
50
51
6.44k
  op->type = BPF_OP_MEM;
52
6.44k
  op->mem.base = reg;
53
6.44k
  op->mem.disp = val;
54
6.44k
  op->is_signed = is_signed;
55
6.44k
  op->is_pkt = is_pkt;
56
6.44k
}
57
58
static void push_op_mmem(cs_bpf *bpf, uint32_t val)
59
332
{
60
332
  cs_bpf_op *op = expand_bpf_operands(bpf);
61
62
332
  op->type = BPF_OP_MMEM;
63
332
  op->mmem = val;
64
332
}
65
66
static void push_op_msh(cs_bpf *bpf, uint32_t val)
67
203
{
68
203
  cs_bpf_op *op = expand_bpf_operands(bpf);
69
70
203
  op->type = BPF_OP_MSH;
71
203
  op->msh = val;
72
203
}
73
74
static void push_op_ext(cs_bpf *bpf, bpf_ext_type val)
75
328
{
76
328
  cs_bpf_op *op = expand_bpf_operands(bpf);
77
78
328
  op->type = BPF_OP_EXT;
79
328
  op->ext = val;
80
328
}
81
82
static void convert_operands(MCInst *MI, cs_bpf *bpf)
83
20.5k
{
84
20.5k
  unsigned opcode = MCInst_getOpcode(MI);
85
20.5k
  unsigned mc_op_count = MCInst_getNumOperands(MI);
86
20.5k
  MCOperand *op;
87
20.5k
  MCOperand *op2;
88
89
20.5k
  bpf->op_count = 0;
90
20.5k
  if (BPF_CLASS(opcode) == BPF_CLASS_LD ||
91
16.7k
      BPF_CLASS(opcode) == BPF_CLASS_LDX) {
92
6.08k
    switch (BPF_MODE(opcode)) {
93
685
    case BPF_MODE_IMM:
94
685
      if (EBPF_MODE(MI->csh->mode)) {
95
199
        push_op_reg(bpf,
96
199
              MCOperand_getReg(
97
199
                MCInst_getOperand(MI, 0)),
98
199
              CS_AC_WRITE);
99
199
        push_op_imm(bpf,
100
199
              MCOperand_getImm(
101
199
                MCInst_getOperand(MI, 1)),
102
199
              false);
103
486
      } else {
104
486
        push_op_imm(bpf,
105
486
              MCOperand_getImm(
106
486
                MCInst_getOperand(MI, 0)),
107
486
              false);
108
486
      }
109
685
      break;
110
1.71k
    case BPF_MODE_ABS:
111
1.71k
      op = MCInst_getOperand(MI, 0);
112
1.71k
      push_op_mem(bpf, BPF_REG_INVALID,
113
1.71k
            (uint32_t)MCOperand_getImm(op),
114
1.71k
            EBPF_MODE(MI->csh->mode), true);
115
1.71k
      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
544
        push_op_mem(bpf, MCOperand_getReg(op), 0x0,
120
544
              true, true);
121
663
      else {
122
663
        op2 = MCInst_getOperand(MI, 1);
123
663
        push_op_mem(bpf, MCOperand_getReg(op),
124
663
              (uint32_t)MCOperand_getImm(op2),
125
663
              false, true);
126
663
      }
127
1.20k
      break;
128
1.95k
    case BPF_MODE_MEM:
129
1.95k
      if (EBPF_MODE(MI->csh->mode)) {
130
        /* ldx{w,h,b,dw} dst, [src+off] */
131
1.67k
        push_op_reg(bpf,
132
1.67k
              MCOperand_getReg(
133
1.67k
                MCInst_getOperand(MI, 0)),
134
1.67k
              CS_AC_WRITE);
135
1.67k
        op = MCInst_getOperand(MI, 1);
136
1.67k
        op2 = MCInst_getOperand(MI, 2);
137
1.67k
        push_op_mem(bpf, MCOperand_getReg(op),
138
1.67k
              (uint32_t)MCOperand_getImm(op2),
139
1.67k
              true, false);
140
1.67k
      } else {
141
275
        push_op_mmem(bpf,
142
275
               (uint32_t)MCOperand_getImm(
143
275
                 MCInst_getOperand(MI, 0)));
144
275
      }
145
1.95k
      break;
146
328
    case BPF_MODE_LEN:
147
328
      push_op_ext(bpf, BPF_EXT_LEN);
148
328
      break;
149
203
    case BPF_MODE_MSH:
150
203
      op = MCInst_getOperand(MI, 0);
151
203
      push_op_msh(bpf, (uint32_t)MCOperand_getImm(op));
152
203
      break;
153
      /* case BPF_MODE_XADD: // not exists */
154
6.08k
    }
155
6.08k
    return;
156
6.08k
  }
157
14.4k
  if (BPF_CLASS(opcode) == BPF_CLASS_ST ||
158
13.4k
      BPF_CLASS(opcode) == BPF_CLASS_STX) {
159
1.90k
    if (!EBPF_MODE(MI->csh->mode)) {
160
      // cBPF has only one case - st* M[k]
161
57
      push_op_mmem(bpf, (uint32_t)MCOperand_getImm(
162
57
              MCInst_getOperand(MI, 0)));
163
57
      return;
164
57
    }
165
    /* eBPF has two cases:
166
     * - st [dst + off], src
167
     * - xadd [dst + off], src
168
     * they have same form of operands.
169
     */
170
1.84k
    op = MCInst_getOperand(MI, 0);
171
1.84k
    op2 = MCInst_getOperand(MI, 1);
172
1.84k
    push_op_mem(bpf, MCOperand_getReg(op),
173
1.84k
          (uint32_t)MCOperand_getImm(op2), true, false);
174
175
1.84k
    op = MCInst_getOperand(MI, 2);
176
1.84k
    if (MCOperand_isImm(op))
177
908
      push_op_imm(bpf, MCOperand_getImm(op), false);
178
940
    else if (MCOperand_isReg(op))
179
940
      push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
180
1.84k
    return;
181
1.90k
  }
182
183
12.5k
  {
184
12.5k
    const bool is_jmp32 = EBPF_MODE(MI->csh->mode) &&
185
8.37k
              (BPF_CLASS(opcode) == BPF_CLASS_JMP32);
186
12.5k
    if (BPF_CLASS(opcode) == BPF_CLASS_JMP || is_jmp32) {
187
20.8k
      for (size_t i = 0; i < mc_op_count; i++) {
188
14.9k
        op = MCInst_getOperand(MI, i);
189
14.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.2k
          if ((BPF_OP(opcode) == BPF_JUMP_JA &&
199
703
               !is_jmp32) ||
200
9.76k
              (!EBPF_MODE(MI->csh->mode) &&
201
1.60k
               i >= 1) ||
202
8.47k
              (EBPF_MODE(MI->csh->mode) &&
203
8.16k
               i == 2))
204
5.80k
            push_op_off(
205
5.80k
              bpf,
206
5.80k
              MCOperand_getImm(op),
207
5.80k
              EBPF_MODE(
208
5.80k
                MI->csh->mode));
209
4.44k
          else
210
4.44k
            push_op_imm(
211
4.44k
              bpf,
212
4.44k
              MCOperand_getImm(op),
213
4.44k
              true);
214
10.2k
        } else if (MCOperand_isReg(op)) {
215
4.71k
          push_op_reg(bpf, MCOperand_getReg(op),
216
4.71k
                CS_AC_READ);
217
4.71k
        }
218
14.9k
      }
219
5.83k
      return;
220
5.83k
    }
221
12.5k
  }
222
223
6.68k
  if (!EBPF_MODE(MI->csh->mode)) {
224
    /* In cBPF mode, all registers in operands are accessed as read */
225
5.93k
    for (size_t i = 0; i < mc_op_count; i++) {
226
2.65k
      op = MCInst_getOperand(MI, i);
227
2.65k
      if (MCOperand_isImm(op))
228
1.44k
        push_op_imm(bpf, MCOperand_getImm(op), false);
229
1.20k
      else if (MCOperand_isReg(op))
230
1.20k
        push_op_reg(bpf, MCOperand_getReg(op),
231
1.20k
              CS_AC_READ);
232
2.65k
    }
233
3.27k
    return;
234
3.27k
  }
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.40k
  if (mc_op_count == 1) {
248
495
    op = MCInst_getOperand(MI, 0);
249
495
    push_op_reg(bpf, MCOperand_getReg(op),
250
495
          CS_AC_READ | CS_AC_WRITE);
251
2.91k
  } else { // if (mc_op_count == 2)
252
2.91k
    op = MCInst_getOperand(MI, 0);
253
2.91k
    push_op_reg(bpf, MCOperand_getReg(op),
254
2.91k
          CS_AC_READ | CS_AC_WRITE);
255
256
2.91k
    op = MCInst_getOperand(MI, 1);
257
2.91k
    if (MCOperand_isImm(op))
258
2.42k
      push_op_imm(bpf, MCOperand_getImm(op), false);
259
485
    else if (MCOperand_isReg(op))
260
485
      push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
261
2.91k
  }
262
3.40k
}
263
264
static void print_operand(MCInst *MI, struct SStream *O, const cs_bpf_op *op)
265
35.6k
{
266
35.6k
  switch (op->type) {
267
0
  case BPF_OP_INVALID:
268
0
    SStream_concat(O, "invalid");
269
0
    break;
270
12.6k
  case BPF_OP_REG:
271
12.6k
    SStream_concat(O, BPF_reg_name((csh)MI->csh, op->reg));
272
12.6k
    break;
273
9.91k
  case BPF_OP_IMM:
274
9.91k
    if (op->is_signed)
275
4.44k
      printInt32Hex(O, op->imm);
276
5.46k
    else
277
5.46k
      SStream_concat(O, "0x%" PRIx64, op->imm);
278
9.91k
    break;
279
5.80k
  case BPF_OP_OFF:
280
5.80k
    if (op->is_signed)
281
4.29k
      printInt16HexOffset(O, op->off);
282
1.50k
    else
283
1.50k
      SStream_concat(O, "+0x%" PRIx32, op->off);
284
5.80k
    break;
285
6.44k
  case BPF_OP_MEM:
286
6.44k
    SStream_concat(O, "[");
287
288
6.44k
    if (op->is_pkt && EBPF_MODE(MI->csh->mode)) {
289
1.42k
      SStream_concat(O, "skb");
290
291
1.42k
      if (op->mem.base != BPF_REG_INVALID)
292
544
        SStream_concat(O, "+%s",
293
544
                 BPF_reg_name((csh)MI->csh,
294
544
                  op->mem.base));
295
881
      else {
296
881
        if (op->is_signed)
297
881
          printInt32HexOffset(O, op->mem.disp);
298
0
        else
299
0
          SStream_concat(O, "+0x%" PRIx32,
300
0
                   op->mem.disp);
301
881
      }
302
5.01k
    } else {
303
5.01k
      if (op->mem.base != BPF_REG_INVALID)
304
4.18k
        SStream_concat(O, BPF_reg_name((csh)MI->csh,
305
4.18k
                     op->mem.base));
306
5.01k
      if (op->mem.disp != 0) {
307
4.87k
        if (op->mem.base != BPF_REG_INVALID) {
308
          // if operation is signed, then it always uses off, not k
309
4.12k
          if (op->is_signed)
310
3.45k
            printInt16HexOffset(
311
3.45k
              O, op->mem.disp);
312
663
          else if (op->is_pkt)
313
663
            SStream_concat(O, "+0x%" PRIx32,
314
663
                     op->mem.disp);
315
0
          else
316
0
            SStream_concat(O, "+0x%" PRIx16,
317
0
                     op->mem.disp);
318
4.12k
        } else
319
756
          SStream_concat(O, "0x%" PRIx32,
320
756
                   op->mem.disp);
321
4.87k
      }
322
323
5.01k
      if (op->mem.base == BPF_REG_INVALID &&
324
829
          op->mem.disp == 0)
325
73
        SStream_concat(O, "0x0");
326
5.01k
    }
327
328
6.44k
    SStream_concat(O, "]");
329
6.44k
    break;
330
332
  case BPF_OP_MMEM:
331
332
    SStream_concat(O, "m[0x%x]", op->mmem);
332
332
    break;
333
203
  case BPF_OP_MSH:
334
203
    SStream_concat(O, "4*([0x%x]&0xf)", op->msh);
335
203
    break;
336
328
  case BPF_OP_EXT:
337
328
    switch (op->ext) {
338
328
    case BPF_EXT_LEN:
339
328
      SStream_concat(O, "#len");
340
328
      break;
341
328
    }
342
328
    break;
343
35.6k
  }
344
35.6k
}
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
20.5k
{
353
20.5k
  cs_bpf bpf = { 0 };
354
355
  /* set pubOpcode as instruction id */
356
20.5k
  SStream_concat(O, BPF_insn_name((csh)MI->csh, MCInst_getOpcodePub(MI)));
357
20.5k
  convert_operands(MI, &bpf);
358
56.1k
  for (size_t i = 0; i < bpf.op_count; i++) {
359
35.6k
    if (i == 0)
360
19.6k
      SStream_concat(O, "\t");
361
15.9k
    else
362
15.9k
      SStream_concat(O, ", ");
363
35.6k
    print_operand(MI, O, &bpf.operands[i]);
364
35.6k
  }
365
366
20.5k
#ifndef CAPSTONE_DIET
367
20.5k
  if (detail_is_set(MI)) {
368
20.5k
    MI->flat_insn->detail->bpf = bpf;
369
20.5k
  }
370
20.5k
#endif
371
20.5k
}