Coverage Report

Created: 2026-09-28 06:34

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
22.2k
{
15
22.2k
  assert(bpf->op_count < 3);
16
22.2k
  return &bpf->operands[bpf->op_count++];
17
22.2k
}
18
19
static void push_op_reg(cs_bpf *bpf, bpf_op_type val, uint8_t ac_mode)
20
7.67k
{
21
7.67k
  cs_bpf_op *op = expand_bpf_operands(bpf);
22
23
7.67k
  op->type = BPF_OP_REG;
24
7.67k
  op->reg = val;
25
7.67k
  op->access = ac_mode;
26
7.67k
}
27
28
static void push_op_imm(cs_bpf *bpf, uint64_t val, const bool is_signed)
29
7.39k
{
30
7.39k
  cs_bpf_op *op = expand_bpf_operands(bpf);
31
32
7.39k
  op->type = BPF_OP_IMM;
33
7.39k
  op->imm = val;
34
7.39k
  op->is_signed = is_signed;
35
7.39k
}
36
37
static void push_op_off(cs_bpf *bpf, uint32_t val, const bool is_signed)
38
2.92k
{
39
2.92k
  cs_bpf_op *op = expand_bpf_operands(bpf);
40
41
2.92k
  op->type = BPF_OP_OFF;
42
2.92k
  op->off = val;
43
2.92k
  op->is_signed = is_signed;
44
2.92k
}
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
3.76k
{
49
3.76k
  cs_bpf_op *op = expand_bpf_operands(bpf);
50
51
3.76k
  op->type = BPF_OP_MEM;
52
3.76k
  op->mem.base = reg;
53
3.76k
  op->mem.disp = val;
54
3.76k
  op->is_signed = is_signed;
55
3.76k
  op->is_pkt = is_pkt;
56
3.76k
}
57
58
static void push_op_mmem(cs_bpf *bpf, uint32_t val)
59
264
{
60
264
  cs_bpf_op *op = expand_bpf_operands(bpf);
61
62
264
  op->type = BPF_OP_MMEM;
63
264
  op->mmem = val;
64
264
}
65
66
static void push_op_msh(cs_bpf *bpf, uint32_t val)
67
217
{
68
217
  cs_bpf_op *op = expand_bpf_operands(bpf);
69
70
217
  op->type = BPF_OP_MSH;
71
217
  op->msh = val;
72
217
}
73
74
static void push_op_ext(cs_bpf *bpf, bpf_ext_type val)
75
34
{
76
34
  cs_bpf_op *op = expand_bpf_operands(bpf);
77
78
34
  op->type = BPF_OP_EXT;
79
34
  op->ext = val;
80
34
}
81
82
static void convert_operands(MCInst *MI, cs_bpf *bpf)
83
13.0k
{
84
13.0k
  unsigned opcode = MCInst_getOpcode(MI);
85
13.0k
  unsigned mc_op_count = MCInst_getNumOperands(MI);
86
13.0k
  MCOperand *op;
87
13.0k
  MCOperand *op2;
88
89
13.0k
  bpf->op_count = 0;
90
13.0k
  if (BPF_CLASS(opcode) == BPF_CLASS_LD ||
91
9.79k
      BPF_CLASS(opcode) == BPF_CLASS_LDX) {
92
4.11k
    switch (BPF_MODE(opcode)) {
93
1.49k
    case BPF_MODE_IMM:
94
1.49k
      if (EBPF_MODE(MI->csh->mode)) {
95
320
        push_op_reg(bpf,
96
320
              MCOperand_getReg(
97
320
                MCInst_getOperand(MI, 0)),
98
320
              CS_AC_WRITE);
99
320
        push_op_imm(bpf,
100
320
              MCOperand_getImm(
101
320
                MCInst_getOperand(MI, 1)),
102
320
              false);
103
1.17k
      } else {
104
1.17k
        push_op_imm(bpf,
105
1.17k
              MCOperand_getImm(
106
1.17k
                MCInst_getOperand(MI, 0)),
107
1.17k
              false);
108
1.17k
      }
109
1.49k
      break;
110
1.07k
    case BPF_MODE_ABS:
111
1.07k
      op = MCInst_getOperand(MI, 0);
112
1.07k
      push_op_mem(bpf, BPF_REG_INVALID,
113
1.07k
            (uint32_t)MCOperand_getImm(op),
114
1.07k
            EBPF_MODE(MI->csh->mode), true);
115
1.07k
      break;
116
705
    case BPF_MODE_IND:
117
705
      op = MCInst_getOperand(MI, 0);
118
705
      if (EBPF_MODE(MI->csh->mode))
119
572
        push_op_mem(bpf, MCOperand_getReg(op), 0x0,
120
572
              true, true);
121
133
      else {
122
133
        op2 = MCInst_getOperand(MI, 1);
123
133
        push_op_mem(bpf, MCOperand_getReg(op),
124
133
              (uint32_t)MCOperand_getImm(op2),
125
133
              false, true);
126
133
      }
127
705
      break;
128
587
    case BPF_MODE_MEM:
129
587
      if (EBPF_MODE(MI->csh->mode)) {
130
        /* ldx{w,h,b,dw} dst, [src+off] */
131
474
        push_op_reg(bpf,
132
474
              MCOperand_getReg(
133
474
                MCInst_getOperand(MI, 0)),
134
474
              CS_AC_WRITE);
135
474
        op = MCInst_getOperand(MI, 1);
136
474
        op2 = MCInst_getOperand(MI, 2);
137
474
        push_op_mem(bpf, MCOperand_getReg(op),
138
474
              (uint32_t)MCOperand_getImm(op2),
139
474
              true, false);
140
474
      } else {
141
113
        push_op_mmem(bpf,
142
113
               (uint32_t)MCOperand_getImm(
143
113
                 MCInst_getOperand(MI, 0)));
144
113
      }
145
587
      break;
146
34
    case BPF_MODE_LEN:
147
34
      push_op_ext(bpf, BPF_EXT_LEN);
148
34
      break;
149
217
    case BPF_MODE_MSH:
150
217
      op = MCInst_getOperand(MI, 0);
151
217
      push_op_msh(bpf, (uint32_t)MCOperand_getImm(op));
152
217
      break;
153
      /* case BPF_MODE_XADD: // not exists */
154
4.11k
    }
155
4.11k
    return;
156
4.11k
  }
157
8.94k
  if (BPF_CLASS(opcode) == BPF_CLASS_ST ||
158
7.91k
      BPF_CLASS(opcode) == BPF_CLASS_STX) {
159
1.66k
    if (!EBPF_MODE(MI->csh->mode)) {
160
      // cBPF has only one case - st* M[k]
161
151
      push_op_mmem(bpf, (uint32_t)MCOperand_getImm(
162
151
              MCInst_getOperand(MI, 0)));
163
151
      return;
164
151
    }
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.51k
    op = MCInst_getOperand(MI, 0);
171
1.51k
    op2 = MCInst_getOperand(MI, 1);
172
1.51k
    push_op_mem(bpf, MCOperand_getReg(op),
173
1.51k
          (uint32_t)MCOperand_getImm(op2), true, false);
174
175
1.51k
    op = MCInst_getOperand(MI, 2);
176
1.51k
    if (MCOperand_isImm(op))
177
915
      push_op_imm(bpf, MCOperand_getImm(op), false);
178
598
    else if (MCOperand_isReg(op))
179
598
      push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
180
1.51k
    return;
181
1.66k
  }
182
183
7.28k
  {
184
7.28k
    const bool is_jmp32 = EBPF_MODE(MI->csh->mode) &&
185
5.33k
              (BPF_CLASS(opcode) == BPF_CLASS_JMP32);
186
7.28k
    if (BPF_CLASS(opcode) == BPF_CLASS_JMP || is_jmp32) {
187
10.8k
      for (size_t i = 0; i < mc_op_count; i++) {
188
7.88k
        op = MCInst_getOperand(MI, i);
189
7.88k
        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
5.29k
          if ((BPF_OP(opcode) == BPF_JUMP_JA &&
199
293
               !is_jmp32) ||
200
5.19k
              (!EBPF_MODE(MI->csh->mode) &&
201
770
               i >= 1) ||
202
4.49k
              (EBPF_MODE(MI->csh->mode) &&
203
4.42k
               i == 2))
204
2.92k
            push_op_off(
205
2.92k
              bpf,
206
2.92k
              MCOperand_getImm(op),
207
2.92k
              EBPF_MODE(
208
2.92k
                MI->csh->mode));
209
2.37k
          else
210
2.37k
            push_op_imm(
211
2.37k
              bpf,
212
2.37k
              MCOperand_getImm(op),
213
2.37k
              true);
214
5.29k
        } else if (MCOperand_isReg(op)) {
215
2.58k
          push_op_reg(bpf, MCOperand_getReg(op),
216
2.58k
                CS_AC_READ);
217
2.58k
        }
218
7.88k
      }
219
3.00k
      return;
220
3.00k
    }
221
7.28k
  }
222
223
4.28k
  if (!EBPF_MODE(MI->csh->mode)) {
224
    /* In cBPF mode, all registers in operands are accessed as read */
225
2.88k
    for (size_t i = 0; i < mc_op_count; i++) {
226
1.30k
      op = MCInst_getOperand(MI, i);
227
1.30k
      if (MCOperand_isImm(op))
228
671
        push_op_imm(bpf, MCOperand_getImm(op), false);
229
634
      else if (MCOperand_isReg(op))
230
634
        push_op_reg(bpf, MCOperand_getReg(op),
231
634
              CS_AC_READ);
232
1.30k
    }
233
1.58k
    return;
234
1.58k
  }
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
2.69k
  if (mc_op_count == 1) {
248
399
    op = MCInst_getOperand(MI, 0);
249
399
    push_op_reg(bpf, MCOperand_getReg(op),
250
399
          CS_AC_READ | CS_AC_WRITE);
251
2.30k
  } else { // if (mc_op_count == 2)
252
2.30k
    op = MCInst_getOperand(MI, 0);
253
2.30k
    push_op_reg(bpf, MCOperand_getReg(op),
254
2.30k
          CS_AC_READ | CS_AC_WRITE);
255
256
2.30k
    op = MCInst_getOperand(MI, 1);
257
2.30k
    if (MCOperand_isImm(op))
258
1.93k
      push_op_imm(bpf, MCOperand_getImm(op), false);
259
362
    else if (MCOperand_isReg(op))
260
362
      push_op_reg(bpf, MCOperand_getReg(op), CS_AC_READ);
261
2.30k
  }
262
2.69k
}
263
264
static void print_operand(MCInst *MI, struct SStream *O, const cs_bpf_op *op)
265
22.2k
{
266
22.2k
  switch (op->type) {
267
0
  case BPF_OP_INVALID:
268
0
    SStream_concat(O, "invalid");
269
0
    break;
270
7.67k
  case BPF_OP_REG:
271
7.67k
    SStream_concat(O, BPF_reg_name((csh)MI->csh, op->reg));
272
7.67k
    break;
273
7.39k
  case BPF_OP_IMM:
274
7.39k
    if (op->is_signed)
275
2.37k
      printInt32Hex(O, op->imm);
276
5.01k
    else
277
5.01k
      SStream_concat(O, "0x%" PRIx64, op->imm);
278
7.39k
    break;
279
2.92k
  case BPF_OP_OFF:
280
2.92k
    if (op->is_signed)
281
2.20k
      printInt16HexOffset(O, op->off);
282
718
    else
283
718
      SStream_concat(O, "+0x%" PRIx32, op->off);
284
2.92k
    break;
285
3.76k
  case BPF_OP_MEM:
286
3.76k
    SStream_concat(O, "[");
287
288
3.76k
    if (op->is_pkt && EBPF_MODE(MI->csh->mode)) {
289
1.14k
      SStream_concat(O, "skb");
290
291
1.14k
      if (op->mem.base != BPF_REG_INVALID)
292
572
        SStream_concat(O, "+%s",
293
572
                 BPF_reg_name((csh)MI->csh,
294
572
                  op->mem.base));
295
572
      else {
296
572
        if (op->is_signed)
297
572
          printInt32HexOffset(O, op->mem.disp);
298
0
        else
299
0
          SStream_concat(O, "+0x%" PRIx32,
300
0
                   op->mem.disp);
301
572
      }
302
2.62k
    } else {
303
2.62k
      if (op->mem.base != BPF_REG_INVALID)
304
2.12k
        SStream_concat(O, BPF_reg_name((csh)MI->csh,
305
2.12k
                     op->mem.base));
306
2.62k
      if (op->mem.disp != 0) {
307
2.38k
        if (op->mem.base != BPF_REG_INVALID) {
308
          // if operation is signed, then it always uses off, not k
309
1.94k
          if (op->is_signed)
310
1.80k
            printInt16HexOffset(
311
1.80k
              O, op->mem.disp);
312
133
          else if (op->is_pkt)
313
133
            SStream_concat(O, "+0x%" PRIx32,
314
133
                     op->mem.disp);
315
0
          else
316
0
            SStream_concat(O, "+0x%" PRIx16,
317
0
                     op->mem.disp);
318
1.94k
        } else
319
445
          SStream_concat(O, "0x%" PRIx32,
320
445
                   op->mem.disp);
321
2.38k
      }
322
323
2.62k
      if (op->mem.base == BPF_REG_INVALID &&
324
505
          op->mem.disp == 0)
325
60
        SStream_concat(O, "0x0");
326
2.62k
    }
327
328
3.76k
    SStream_concat(O, "]");
329
3.76k
    break;
330
264
  case BPF_OP_MMEM:
331
264
    SStream_concat(O, "m[0x%x]", op->mmem);
332
264
    break;
333
217
  case BPF_OP_MSH:
334
217
    SStream_concat(O, "4*([0x%x]&0xf)", op->msh);
335
217
    break;
336
34
  case BPF_OP_EXT:
337
34
    switch (op->ext) {
338
34
    case BPF_EXT_LEN:
339
34
      SStream_concat(O, "#len");
340
34
      break;
341
34
    }
342
34
    break;
343
22.2k
  }
344
22.2k
}
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
13.0k
{
353
13.0k
  cs_bpf bpf = { 0 };
354
355
  /* set pubOpcode as instruction id */
356
13.0k
  SStream_concat(O, BPF_insn_name((csh)MI->csh, MCInst_getOpcodePub(MI)));
357
13.0k
  convert_operands(MI, &bpf);
358
35.3k
  for (size_t i = 0; i < bpf.op_count; i++) {
359
22.2k
    if (i == 0)
360
12.7k
      SStream_concat(O, "\t");
361
9.54k
    else
362
9.54k
      SStream_concat(O, ", ");
363
22.2k
    print_operand(MI, O, &bpf.operands[i]);
364
22.2k
  }
365
366
13.0k
#ifndef CAPSTONE_DIET
367
13.0k
  if (detail_is_set(MI)) {
368
13.0k
    MI->flat_insn->detail->bpf = bpf;
369
13.0k
  }
370
13.0k
#endif
371
13.0k
}