Debian Codegraph @

arch/BPF/BPFDisassembler.c

raw symbols SCIP
1 /* Capstone Disassembly Engine */cxx . . $ `<file>/arch/BPF/BPFDisassembler.c`/
2 /* BPF Backend by david942j <[email protected]>, 2019 */
3
4 #ifdef CAPSTONE_HAS_BPFcxx . . $ `<built-in>:428:9`!
5
6 #include <string.h>
7 #include <stddef.h> // offsetof macro
8
9 #include "BPFConstants.h"cxx . . $ `<file>/arch/BPF/BPFConstants.h`/
10 #include "BPFDisassembler.h"cxx . . $ `<file>/arch/BPF/BPFDisassembler.h`/
11 #include "BPFMapping.h"cxx . . $ `<file>/arch/BPF/BPFMapping.h`/
12 #include "../../cs_priv.h"cxx . . $ `<file>/cs_priv.h`/
13
14 static uint16_tcxx . . $ uint16_t# read_u16cxx . . $ read_u16(4456cafafc814993).(cs_structcxx . . $ cs_struct# *ud, const uint8_tcxx . . $ uint8_t# *code)
15 {
16 if (MODE_IS_BIG_ENDIANcxx . . $ `cs_priv.h:89:9`!(ud->mode))
17 return (((uint16_tcxx . . $ uint16_t#)code[0] << 8) | code[1]);
18 else
19 return (((uint16_tcxx . . $ uint16_t#)code[1] << 8) | code[0]);
20 }
21
22 static uint32_tcxx . . $ uint32_t# read_u32cxx . . $ read_u32(50e34a1cf9e10eba).(cs_structcxx . . $ cs_struct# *ud, const uint8_tcxx . . $ uint8_t# *code)
23 {
24 if (MODE_IS_BIG_ENDIANcxx . . $ `cs_priv.h:89:9`!(ud->mode))
25 return ((uint32_tcxx . . $ uint32_t#)read_u16cxx . . $ read_u16(4456cafafc814993).(ud, code) << 16) | read_u16cxx . . $ read_u16(4456cafafc814993).(ud, code + 2);
26 else
27 return ((uint32_tcxx . . $ uint32_t#)read_u16cxx . . $ read_u16(4456cafafc814993).(ud, code + 2) << 16) | read_u16cxx . . $ read_u16(4456cafafc814993).(ud, code);
28 }
29
30 ///< Malloc bpf_internal, also checks if code_len is large enough.
31 static bpf_internalcxx . . $ bpf_internal# *alloc_bpf_internalcxx . . $ alloc_bpf_internal(5429cc4cecc7a4db).(size_t code_len)
32 {
33 bpf_internalcxx . . $ bpf_internal# *bpf;
34
35 if (code_len < 8)
36 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
37 bpf = cs_mem_malloccxx . . $ cs_mem_malloc.(sizeof(bpf_internalcxx . . $ bpf_internal#));
38 if (bpf == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
39 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
40 /* default value */
41 bpf->insn_sizecxx . . $ bpf_internal#insn_size. = 8;
42 return bpf;
43 }
44
45 ///< Fetch a cBPF structure from code
46 static bpf_internalcxx . . $ bpf_internal#* fetch_cbpfcxx . . $ fetch_cbpf(497e4d5e24392ec5).(cs_structcxx . . $ cs_struct# *ud, const uint8_tcxx . . $ uint8_t# *code,
47 size_t code_len)
48 {
49 bpf_internalcxx . . $ bpf_internal# *bpf;
50
51 bpf = alloc_bpf_internalcxx . . $ alloc_bpf_internal(5429cc4cecc7a4db).(code_len);
52 if (bpf == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
53 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
54
55 bpf->opcxx . . $ bpf_internal#op. = read_u16cxx . . $ read_u16(4456cafafc814993).(ud, code);
56 bpf->jtcxx . . $ bpf_internal#jt. = code[2];
57 bpf->jfcxx . . $ bpf_internal#jf. = code[3];
58 bpf->kcxx . . $ bpf_internal#k. = read_u32cxx . . $ read_u32(50e34a1cf9e10eba).(ud, code + 4);
59 return bpf;
60 }
61
62 ///< Fetch an eBPF structure from code
63 static bpf_internalcxx . . $ bpf_internal#* fetch_ebpfcxx . . $ fetch_ebpf(497e4d5e24392ec5).(cs_structcxx . . $ cs_struct# *ud, const uint8_tcxx . . $ uint8_t# *code,
64 size_t code_len)
65 {
66 bpf_internalcxx . . $ bpf_internal# *bpf;
67
68 bpf = alloc_bpf_internalcxx . . $ alloc_bpf_internal(5429cc4cecc7a4db).(code_len);
69 if (bpf == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
70 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
71
72 bpf->opcxx . . $ bpf_internal#op. = (uint16_tcxx . . $ uint16_t#)code[0];
73 bpf->dstcxx . . $ bpf_internal#dst. = code[1] & 0xf;
74 bpf->srccxx . . $ bpf_internal#src. = (code[1] & 0xf0) >> 4;
75
76 // eBPF has one 16-byte instruction: BPF_LD | BPF_DW | BPF_IMM,
77 // in this case imm is combined with the next block's imm.
78 if (bpf->opcxx . . $ bpf_internal#op. == (BPF_CLASS_LDcxx . . $ `arch/BPF/BPFConstants.h:12:9`! | BPF_SIZE_DWcxx . . $ `arch/BPF/BPFConstants.h:71:9`! | BPF_MODE_IMMcxx . . $ `arch/BPF/BPFConstants.h:75:9`!)) {
79 if (code_len < 16) {
80 cs_mem_freecxx . . $ cs_mem_free.(bpf);
81 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
82 }
83 bpf->kcxx . . $ bpf_internal#k. = read_u32cxx . . $ read_u32(50e34a1cf9e10eba).(ud, code + 4) | (((uint64_tcxx . . $ uint64_t#)read_u32cxx . . $ read_u32(50e34a1cf9e10eba).(ud, code + 12)) << 32);
84 bpf->insn_sizecxx . . $ bpf_internal#insn_size. = 16;
85 }
86 else {
87 bpf->offsetcxx . . $ bpf_internal#offset. = read_u16cxx . . $ read_u16(4456cafafc814993).(ud, code + 2);
88 bpf->kcxx . . $ bpf_internal#k. = read_u32cxx . . $ read_u32(50e34a1cf9e10eba).(ud, code + 4);
89 }
90 return bpf;
91 }
92
93 #define CHECK_READABLE_REGcxx . . $ `arch/BPF/BPFDisassembler.c:93:9`!(ud, reg) do { \
94 if (! ((reg) >= BPF_REG_R0 && (reg) <= BPF_REG_R10)) \
95 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; \
96 } while (0)
97
98 #define CHECK_WRITABLE_REGcxx . . $ `arch/BPF/BPFDisassembler.c:98:9`!(ud, reg) do { \
99 if (! ((reg) >= BPF_REG_R0 && (reg) < BPF_REG_R10)) \
100 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; \
101 } while (0)
102
103 #define CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, r) do { \
104 CHECK_READABLE_REGcxx . . $ `arch/BPF/BPFDisassembler.c:93:9`!(ud, r + BPF_REG_R0); \
105 MCOperand_CreateReg0(MI, r + BPF_REG_R0); \
106 } while (0)
107
108 #define CHECK_WRITABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:108:9`!(ud, MI, r) do { \
109 CHECK_WRITABLE_REGcxx . . $ `arch/BPF/BPFDisassembler.c:98:9`!(ud, r + BPF_REG_R0); \
110 MCOperand_CreateReg0(MI, r + BPF_REG_R0); \
111 } while (0)
112
113 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! decodeLoadcxx . . $ decodeLoad(7577f0663b5458a2).(cs_structcxx . . $ cs_struct# *ud, MCInstcxx . . $ MCInst# *MI, bpf_internalcxx . . $ bpf_internal# *bpf)
114 {
115 if (!EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(ud)) {
116 /*
117 * +-----+-----------+--------------------+
118 * | ldb | [k] | [x+k] |
119 * | ldh | [k] | [x+k] |
120 * +-----+-----------+--------------------+
121 */
122 if (BPF_SIZEcxx . . $ `arch/BPF/BPFConstants.h:66:9`!(bpf->op) == BPF_SIZE_DWcxx . . $ `arch/BPF/BPFConstants.h:71:9`!)
123 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
124 if (BPF_SIZEcxx . . $ `arch/BPF/BPFConstants.h:66:9`!(bpf->op) == BPF_SIZE_Bcxx . . $ `arch/BPF/BPFConstants.h:70:9`! || BPF_SIZEcxx . . $ `arch/BPF/BPFConstants.h:66:9`!(bpf->op) == BPF_SIZE_Hcxx . . $ `arch/BPF/BPFConstants.h:69:9`!) {
125 /* no ldx */
126 if (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(bpf->op) != BPF_CLASS_LDcxx . . $ `arch/BPF/BPFConstants.h:12:9`!)
127 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
128 /* can only be BPF_ABS and BPF_IND */
129 if (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(bpf->op) == BPF_MODE_ABScxx . . $ `arch/BPF/BPFConstants.h:76:9`!) {
130 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
131 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
132 }
133 else if (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(bpf->op) == BPF_MODE_INDcxx . . $ `arch/BPF/BPFConstants.h:77:9`!) {
134 MCOperand_CreateReg0cxx . . $ MCOperand_CreateReg0(1d3fdd902ae43da2).(MI, BPF_REG_Xcxx . . $ BPF_REG_X.);
135 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
136 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
137 }
138 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
139 }
140 /*
141 * +-----+----+------+------+-----+-------+
142 * | ld | #k | #len | M[k] | [k] | [x+k] |
143 * +-----+----+------+------+-----+-------+
144 * | ldx | #k | #len | M[k] | 4*([k]&0xf) |
145 * +-----+----+------+------+-------------+
146 */
147 switch (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(bpf->op)) {
148 default:
149 break;
150 case BPF_MODE_IMMcxx . . $ `arch/BPF/BPFConstants.h:75:9`!:
151 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
152 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
153 case BPF_MODE_LENcxx . . $ `arch/BPF/BPFConstants.h:79:9`!:
154 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
155 case BPF_MODE_MEMcxx . . $ `arch/BPF/BPFConstants.h:78:9`!:
156 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
157 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
158 }
159 if (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(bpf->op) == BPF_CLASS_LDcxx . . $ `arch/BPF/BPFConstants.h:12:9`!) {
160 if (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(bpf->op) == BPF_MODE_ABScxx . . $ `arch/BPF/BPFConstants.h:76:9`!) {
161 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
162 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
163 }
164 else if (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(bpf->op) == BPF_MODE_INDcxx . . $ `arch/BPF/BPFConstants.h:77:9`!) {
165 MCOperand_CreateReg0cxx . . $ MCOperand_CreateReg0(1d3fdd902ae43da2).(MI, BPF_REG_Xcxx . . $ BPF_REG_X.);
166 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
167 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
168 }
169 }
170 else { /* LDX */
171 if (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(bpf->op) == BPF_MODE_MSHcxx . . $ `arch/BPF/BPFConstants.h:80:9`!) {
172 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
173 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
174 }
175 }
176 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
177 }
178
179 /* eBPF mode */
180 /*
181 * - IMM: lddw dst, imm64
182 * - ABS: ld{w,h,b,dw} [k]
183 * - IND: ld{w,h,b,dw} [src+k]
184 * - MEM: ldx{w,h,b,dw} dst, [src+off]
185 */
186 if (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(bpf->op) == BPF_CLASS_LDcxx . . $ `arch/BPF/BPFConstants.h:12:9`!) {
187 switch (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(bpf->op)) {
188 case BPF_MODE_IMMcxx . . $ `arch/BPF/BPFConstants.h:75:9`!:
189 if (bpf->opcxx . . $ bpf_internal#op. != (BPF_CLASS_LDcxx . . $ `arch/BPF/BPFConstants.h:12:9`! | BPF_SIZE_DWcxx . . $ `arch/BPF/BPFConstants.h:71:9`! | BPF_MODE_IMMcxx . . $ `arch/BPF/BPFConstants.h:75:9`!))
190 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
191 CHECK_WRITABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:108:9`!(ud, MI, bpf->dst);
192 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
193 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
194 case BPF_MODE_ABScxx . . $ `arch/BPF/BPFConstants.h:76:9`!:
195 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
196 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
197 case BPF_MODE_INDcxx . . $ `arch/BPF/BPFConstants.h:77:9`!:
198 CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, bpf->src);
199 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
200 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
201 }
202 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
203
204 }
205 /* LDX */
206 if (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(bpf->op) == BPF_MODE_MEMcxx . . $ `arch/BPF/BPFConstants.h:78:9`!) {
207 CHECK_WRITABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:108:9`!(ud, MI, bpf->dst);
208 CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, bpf->src);
209 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->offsetcxx . . $ bpf_internal#offset.);
210 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
211 }
212 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
213 }
214
215 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! decodeStorecxx . . $ decodeStore(7577f0663b5458a2).(cs_structcxx . . $ cs_struct# *ud, MCInstcxx . . $ MCInst# *MI, bpf_internalcxx . . $ bpf_internal# *bpf)
216 {
217 /* in cBPF, only BPF_ST* | BPF_MEM | BPF_W is valid
218 * while in eBPF:
219 * - BPF_STX | BPF_XADD | BPF_{W,DW}
220 * - BPF_ST* | BPF_MEM | BPF_{W,H,B,DW}
221 * are valid
222 */
223 if (!EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(ud)) {
224 /* can only store to M[] */
225 if (bpf->opcxx . . $ bpf_internal#op. != (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(bpf->op) | BPF_MODE_MEMcxx . . $ `arch/BPF/BPFConstants.h:78:9`! | BPF_SIZE_Wcxx . . $ `arch/BPF/BPFConstants.h:68:9`!))
226 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
227 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
228 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
229 }
230
231 /* eBPF */
232
233 if (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(bpf->op) == BPF_MODE_XADDcxx . . $ `arch/BPF/BPFConstants.h:81:9`!) {
234 if (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(bpf->op) != BPF_CLASS_STXcxx . . $ `arch/BPF/BPFConstants.h:15:9`!)
235 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
236 if (BPF_SIZEcxx . . $ `arch/BPF/BPFConstants.h:66:9`!(bpf->op) != BPF_SIZE_Wcxx . . $ `arch/BPF/BPFConstants.h:68:9`! && BPF_SIZEcxx . . $ `arch/BPF/BPFConstants.h:66:9`!(bpf->op) != BPF_SIZE_DWcxx . . $ `arch/BPF/BPFConstants.h:71:9`!)
237 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
238 /* xadd [dst + off], src */
239 CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, bpf->dst);
240 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->offsetcxx . . $ bpf_internal#offset.);
241 CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, bpf->src);
242 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
243 }
244
245 if (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(bpf->op) != BPF_MODE_MEMcxx . . $ `arch/BPF/BPFConstants.h:78:9`!)
246 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
247
248 /* st [dst + off], src */
249 CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, bpf->dst);
250 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->offsetcxx . . $ bpf_internal#offset.);
251 if (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(bpf->op) == BPF_CLASS_STcxx . . $ `arch/BPF/BPFConstants.h:14:9`!)
252 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
253 else
254 CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, bpf->src);
255 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
256 }
257
258 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! decodeALUcxx . . $ decodeALU(7577f0663b5458a2).(cs_structcxx . . $ cs_struct# *ud, MCInstcxx . . $ MCInst# *MI, bpf_internalcxx . . $ bpf_internal# *bpf)
259 {
260 /* Set MI->Operands */
261
262 /* cBPF */
263 if (!EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(ud)) {
264 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) > BPF_ALU_XORcxx . . $ `arch/BPF/BPFConstants.h:35:9`!)
265 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
266 /* cBPF's NEG has no operands */
267 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) == BPF_ALU_NEGcxx . . $ `arch/BPF/BPFConstants.h:33:9`!)
268 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
269 if (BPF_SRCcxx . . $ `arch/BPF/BPFConstants.h:56:9`!(bpf->op) == BPF_SRC_Kcxx . . $ `arch/BPF/BPFConstants.h:59:9`!)
270 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
271 else /* BPF_SRC_X */
272 MCOperand_CreateReg0cxx . . $ MCOperand_CreateReg0(1d3fdd902ae43da2).(MI, BPF_REG_Xcxx . . $ BPF_REG_X.);
273 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
274 }
275
276 /* eBPF */
277
278 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) > BPF_ALU_ENDcxx . . $ `arch/BPF/BPFConstants.h:38:9`!)
279 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
280 /* ALU64 class doesn't have ENDian */
281 /* ENDian's imm must be one of 16, 32, 64 */
282 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) == BPF_ALU_ENDcxx . . $ `arch/BPF/BPFConstants.h:38:9`!) {
283 if (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(bpf->op) == BPF_CLASS_ALU64cxx . . $ `arch/BPF/BPFConstants.h:20:9`!)
284 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
285 if (bpf->kcxx . . $ bpf_internal#k. != 16 && bpf->kcxx . . $ bpf_internal#k. != 32 && bpf->kcxx . . $ bpf_internal#k. != 64)
286 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
287 }
288
289 /* - op dst, imm
290 * - op dst, src
291 * - neg dst
292 * - le<imm> dst
293 */
294 /* every ALU instructions have dst op */
295 CHECK_WRITABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:108:9`!(ud, MI, bpf->dst);
296
297 /* special cases */
298 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) == BPF_ALU_NEGcxx . . $ `arch/BPF/BPFConstants.h:33:9`!)
299 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
300 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) == BPF_ALU_ENDcxx . . $ `arch/BPF/BPFConstants.h:38:9`!) {
301 /* bpf->k must be one of 16, 32, 64 */
302 MCInst_setOpcodecxx . . $ MCInst_setOpcode(1d3fdd902ae43da2).(MI, MCInst_getOpcodecxx . . $ MCInst_getOpcode(66549afbf0596d6c).(MI) | ((uint32_tcxx . . $ uint32_t#)bpf->kcxx . . $ bpf_internal#k. << 4));
303 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
304 }
305
306 /* normal cases */
307 if (BPF_SRCcxx . . $ `arch/BPF/BPFConstants.h:56:9`!(bpf->op) == BPF_SRC_Kcxx . . $ `arch/BPF/BPFConstants.h:59:9`!) {
308 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
309 }
310 else { /* BPF_SRC_X */
311 CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, bpf->src);
312 }
313 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
314 }
315
316 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! decodeJumpcxx . . $ decodeJump(7577f0663b5458a2).(cs_structcxx . . $ cs_struct# *ud, MCInstcxx . . $ MCInst# *MI, bpf_internalcxx . . $ bpf_internal# *bpf)
317 {
318 /* cBPF and eBPF are very different in class jump */
319 if (!EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(ud)) {
320 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) > BPF_JUMP_JSETcxx . . $ `arch/BPF/BPFConstants.h:45:9`!)
321 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
322
323 /* ja is a special case of jumps */
324 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) == BPF_JUMP_JAcxx . . $ `arch/BPF/BPFConstants.h:41:9`!) {
325 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
326 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
327 }
328
329 if (BPF_SRCcxx . . $ `arch/BPF/BPFConstants.h:56:9`!(bpf->op) == BPF_SRC_Kcxx . . $ `arch/BPF/BPFConstants.h:59:9`!)
330 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
331 else /* BPF_SRC_X */
332 MCOperand_CreateReg0cxx . . $ MCOperand_CreateReg0(1d3fdd902ae43da2).(MI, BPF_REG_Xcxx . . $ BPF_REG_X.);
333 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->jtcxx . . $ bpf_internal#jt.);
334 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->jfcxx . . $ bpf_internal#jf.);
335 }
336 else {
337 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) > BPF_JUMP_JSLEcxx . . $ `arch/BPF/BPFConstants.h:54:9`!)
338 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
339
340 /* No operands for exit */
341 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) == BPF_JUMP_EXITcxx . . $ `arch/BPF/BPFConstants.h:50:9`!)
342 return bpf->opcxx . . $ bpf_internal#op. == (BPF_CLASS_JMPcxx . . $ `arch/BPF/BPFConstants.h:17:9`! | BPF_JUMP_EXITcxx . . $ `arch/BPF/BPFConstants.h:50:9`!);
343 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) == BPF_JUMP_CALLcxx . . $ `arch/BPF/BPFConstants.h:49:9`!) {
344 if (bpf->opcxx . . $ bpf_internal#op. == (BPF_CLASS_JMPcxx . . $ `arch/BPF/BPFConstants.h:17:9`! | BPF_JUMP_CALLcxx . . $ `arch/BPF/BPFConstants.h:49:9`!)) {
345 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
346 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
347 }
348 if (bpf->opcxx . . $ bpf_internal#op. == (BPF_CLASS_JMPcxx . . $ `arch/BPF/BPFConstants.h:17:9`! | BPF_JUMP_CALLcxx . . $ `arch/BPF/BPFConstants.h:49:9`! | BPF_SRC_Xcxx . . $ `arch/BPF/BPFConstants.h:60:9`!)) {
349 CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, bpf->k);
350 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
351 }
352 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
353 }
354
355 /* ja is a special case of jumps */
356 if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(bpf->op) == BPF_JUMP_JAcxx . . $ `arch/BPF/BPFConstants.h:41:9`!) {
357 if (BPF_SRCcxx . . $ `arch/BPF/BPFConstants.h:56:9`!(bpf->op) != BPF_SRC_Kcxx . . $ `arch/BPF/BPFConstants.h:59:9`!)
358 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
359 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->offsetcxx . . $ bpf_internal#offset.);
360 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
361 }
362
363 /* <j> dst, src, +off */
364 CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, bpf->dst);
365 if (BPF_SRCcxx . . $ `arch/BPF/BPFConstants.h:56:9`!(bpf->op) == BPF_SRC_Kcxx . . $ `arch/BPF/BPFConstants.h:59:9`!)
366 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
367 else
368 CHECK_READABLE_AND_PUSHcxx . . $ `arch/BPF/BPFDisassembler.c:103:9`!(ud, MI, bpf->src);
369 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->offsetcxx . . $ bpf_internal#offset.);
370 }
371 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
372 }
373
374 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! decodeReturncxx . . $ decodeReturn(7577f0663b5458a2).(cs_structcxx . . $ cs_struct# *ud, MCInstcxx . . $ MCInst# *MI, bpf_internalcxx . . $ bpf_internal# *bpf)
375 {
376 /* Here only handles the BPF_RET class in cBPF */
377 switch (BPF_RVALcxx . . $ `arch/BPF/BPFConstants.h:57:9`!(bpf->op)) {
378 case BPF_SRC_Kcxx . . $ `arch/BPF/BPFConstants.h:59:9`!:
379 MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MI, bpf->kcxx . . $ bpf_internal#k.);
380 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
381 case BPF_SRC_Xcxx . . $ `arch/BPF/BPFConstants.h:60:9`!:
382 MCOperand_CreateReg0cxx . . $ MCOperand_CreateReg0(1d3fdd902ae43da2).(MI, BPF_REG_Xcxx . . $ BPF_REG_X.);
383 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
384 case BPF_SRC_Acxx . . $ `arch/BPF/BPFConstants.h:61:9`!:
385 MCOperand_CreateReg0cxx . . $ MCOperand_CreateReg0(1d3fdd902ae43da2).(MI, BPF_REG_Acxx . . $ BPF_REG_A.);
386 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
387 }
388 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
389 }
390
391 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! decodeMISCcxx . . $ decodeMISC(7577f0663b5458a2).(cs_structcxx . . $ cs_struct# *ud, MCInstcxx . . $ MCInst# *MI, bpf_internalcxx . . $ bpf_internal# *bpf)
392 {
393 uint16_tcxx . . $ uint16_t# op = bpf->opcxx . . $ bpf_internal#op. ^ BPF_CLASS_MISCcxx . . $ `arch/BPF/BPFConstants.h:19:9`!;
394 return op == BPF_MISCOP_TAXcxx . . $ `arch/BPF/BPFConstants.h:85:9`! || op == BPF_MISCOP_TXAcxx . . $ `arch/BPF/BPFConstants.h:86:9`!;
395 }
396
397 ///< 1. Check if the instruction is valid
398 ///< 2. Set MI->opcode
399 ///< 3. Set MI->Operands
400 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! getInstructioncxx . . $ getInstruction(7577f0663b5458a2).(cs_structcxx . . $ cs_struct# *ud, MCInstcxx . . $ MCInst# *MI, bpf_internalcxx . . $ bpf_internal# *bpf)
401 {
402 cs_detailcxx . . $ cs_detail# *detail;
403
404 detail = MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.;
405 // initialize detail
406 if (detail) {
407 memsetcxx . . $ memset(d1de1e4fb215f1c6).(detail, 0, offsetofcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:431:9`!(cs_detail, bpf) + sizeof(cs_bpfcxx . . $ cs_bpf#));
408 }
409
410 MCInst_clearcxx . . $ MCInst_clear(94c8a3269074abe9).(MI);
411 MCInst_setOpcodecxx . . $ MCInst_setOpcode(1d3fdd902ae43da2).(MI, bpf->opcxx . . $ bpf_internal#op.);
412
413 switch (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(bpf->op)) {
414 default: /* should never happen */
415 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
416 case BPF_CLASS_LDcxx . . $ `arch/BPF/BPFConstants.h:12:9`!:
417 case BPF_CLASS_LDXcxx . . $ `arch/BPF/BPFConstants.h:13:9`!:
418 return decodeLoadcxx . . $ decodeLoad(7577f0663b5458a2).(ud, MI, bpf);
419 case BPF_CLASS_STcxx . . $ `arch/BPF/BPFConstants.h:14:9`!:
420 case BPF_CLASS_STXcxx . . $ `arch/BPF/BPFConstants.h:15:9`!:
421 return decodeStorecxx . . $ decodeStore(7577f0663b5458a2).(ud, MI, bpf);
422 case BPF_CLASS_ALUcxx . . $ `arch/BPF/BPFConstants.h:16:9`!:
423 return decodeALUcxx . . $ decodeALU(7577f0663b5458a2).(ud, MI, bpf);
424 case BPF_CLASS_JMPcxx . . $ `arch/BPF/BPFConstants.h:17:9`!:
425 return decodeJumpcxx . . $ decodeJump(7577f0663b5458a2).(ud, MI, bpf);
426 case BPF_CLASS_RETcxx . . $ `arch/BPF/BPFConstants.h:18:9`!:
427 /* eBPF doesn't have this class */
428 if (EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(ud))
429 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
430 return decodeReturncxx . . $ decodeReturn(7577f0663b5458a2).(ud, MI, bpf);
431 case BPF_CLASS_MISCcxx . . $ `arch/BPF/BPFConstants.h:19:9`!:
432 /* case BPF_CLASS_ALU64: */
433 if (EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(ud))
434 return decodeALUcxx . . $ decodeALU(7577f0663b5458a2).(ud, MI, bpf);
435 else
436 return decodeMISCcxx . . $ decodeMISC(7577f0663b5458a2).(ud, MI, bpf);
437 }
438 }
439
440 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! BPF_getInstructioncxx . . $ BPF_getInstruction(fe94ce91eeab61e9).(cshcxx . . $ csh# ud, const uint8_tcxx . . $ uint8_t# *code, size_t code_len,
441 MCInstcxx . . $ MCInst# *instr, uint16_tcxx . . $ uint16_t# *size, uint64_tcxx . . $ uint64_t# address, void *info)
442 {
443 cs_structcxx . . $ cs_struct# *cs;
444 bpf_internalcxx . . $ bpf_internal# *bpf;
445
446 cs = (cs_structcxx . . $ cs_struct#*)ud;
447 if (EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(cs))
448 bpf = fetch_ebpfcxx . . $ fetch_ebpf(497e4d5e24392ec5).(cs, code, code_len);
449 else
450 bpf = fetch_cbpfcxx . . $ fetch_cbpf(497e4d5e24392ec5).(cs, code, code_len);
451 if (bpf == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
452 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
453 if (!getInstructioncxx . . $ getInstruction(7577f0663b5458a2).(cs, instr, bpf)) {
454 cs_mem_freecxx . . $ cs_mem_free.(bpf);
455 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
456 }
457
458 *size = bpf->insn_sizecxx . . $ bpf_internal#insn_size.;
459 cs_mem_freecxx . . $ cs_mem_free.(bpf);
460
461 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
462 }
463
464 #endif