Line data Source code
1 : /* This is the VM SBPF interpreter core. The caller unpacks the VM
2 : state and then just lets execution continue into this (or jumps to
3 : interp_exec) to start running. The VM will run until it halts or
4 : faults. On normal termination, it will branch to interp_halt to
5 : exit. Each fault has its own exit label to allow the caller to
6 : handle individually. */
7 :
8 36 : # if defined(__GNUC__) /* -Wpedantic rejects labels as values and rejects goto *expr */
9 36 : # pragma GCC diagnostic push
10 36 : # pragma GCC diagnostic ignored "-Wpedantic"
11 36 : # endif
12 :
13 36 : # if defined(__clang__) /* Clang is differently picky about labels as values and goto *expr */
14 36 : # pragma clang diagnostic push
15 36 : # pragma clang diagnostic ignored "-Wpedantic"
16 36 : # pragma clang diagnostic ignored "-Wgnu-label-as-value"
17 36 : # endif
18 :
19 : /* Include the jump table */
20 :
21 9987 : # include "fd_vm_interp_jump_table.c"
22 :
23 : /* Per-instruction VM state. High register pressure here, so only
24 : declare locals for essentials. Evaluate the rest (e.g. instruction
25 : dst/src/off/imm fields) just in time. */
26 :
27 36 : ulong pc = vm->pc;
28 36 : ulong cu = vm->cu;
29 :
30 36 : void const * const * const version_interp_jump_table = interp_jump_table[ vm->sbpf_version ];
31 :
32 36 : ulong instr;
33 :
34 6850449 : #define reg (vm->reg)
35 188751 : #define text_cnt (vm->text_cnt)
36 :
37 6803685 : #define INS_DST fd_vm_instr_dst ( instr )
38 38826 : #define INS_SRC fd_vm_instr_src ( instr )
39 6803685 : #define REG_DST (reg[ INS_DST ])
40 38409 : #define REG_SRC (reg[ INS_SRC ])
41 215424 : #define INS_OFF fd_vm_instr_offset( instr )
42 25548 : #define INS_IMM fd_vm_instr_imm ( instr )
43 36 : #define INS_SIMM ((ulong)(long)(int)(instr >> 32))
44 :
45 36 : #define ULONG_SHL_WRAP(a,b) ( ( a )<<( ( b )&63 ) )
46 36 : #define ULONG_SHR_WRAP(a,b) ( ( a )>>( ( b )&63 ) )
47 36 : #define UINT_SHL_WRAP(a,b) ( ( a )<<( ( b )&31 ) )
48 36 : #define UINT_SHR_WRAP(a,b) ( ( a )>>( ( b )&31 ) )
49 36 : #define INT_SHR_WRAP(a,b) ( ( a )>>( ( b )&31 ) )
50 36 : #define LONG_SHR_WRAP(a,b) ( ( a )>>( ( b )&63 ) )
51 :
52 36 : # define INTERP_SYSCALL_EXEC_DUMP \
53 : /* Dumping for debugging purposes */ \
54 228 : if( FD_UNLIKELY( vm->dump_syscall_to_pb ) ) { \
55 0 : fd_dump_vm_syscall_to_protobuf( vm, syscall->name ); \
56 0 : }
57 :
58 36 : # define INTERP_SYSCALL_EXEC \
59 : /* Setup */ \
60 228 : ulong _ic_save = vm->ic; \
61 228 : ulong _fc_save = vm->frame_cnt; \
62 228 : vm->pc = pc; \
63 228 : vm->cu = cu; \
64 228 : INTERP_SYSCALL_EXEC_DUMP \
65 : /* Execution */ \
66 228 : err = syscall->func( vm, reg[1], reg[2], reg[3], reg[4], reg[5] ); \
67 228 : vm->ic = _ic_save; \
68 228 : vm->frame_cnt = _fc_save; \
69 : /* Error handling */ \
70 228 : ulong cu_req = vm->cu; \
71 228 : cu = fd_ulong_min( cu_req, cu ); \
72 228 : if( FD_UNLIKELY( err ) ) { \
73 0 : if( err==FD_VM_SYSCALL_ERR_COMPUTE_BUDGET_EXCEEDED ) cu = 0UL; \
74 0 : FD_VM_TEST_ERR_EXISTS( vm ); \
75 0 : goto sigsyscall; \
76 0 : }
77 :
78 : /* All handlers share one indirect jump (interp_dispatch). This
79 : concentrates the indirect target predictor's capacity on a single
80 : branch. The text limit check stays in each handler (not taken), so
81 : reaching the dispatch costs one direct jump. */
82 : # ifndef FD_VM_INTERP_EXE_TRACING_ENABLED
83 : # define INTERP_NEXT \
84 13588773 : if( FD_UNLIKELY( pc>=block_text_limit ) ) goto sigtext_or_sigcost; /* Note: untaken branches don't consume BTB */ \
85 13588773 : goto interp_dispatch
86 : # else
87 372 : # define INTERP_NEXT goto interp_exec
88 : # endif
89 :
90 6803751 : # define INTERP_INSTR_BEGIN(opcode) interp_##opcode:
91 10956414 : # define INTERP_INSTR_END pc++; INTERP_NEXT
92 :
93 : # ifndef FD_VM_INTERP_EXE_TRACING_ENABLED
94 :
95 : /* CU metering:
96 : The production path maintains `pc_bias = pc0 + ic_correction` as a
97 : single live register (for 2-word LDDW). */
98 : ulong pc_bias = pc;
99 9918 : ulong block_text_limit = fd_ulong_min( text_cnt, pc+cu+cu+1UL );
100 :
101 24546 : # define INTERP_LDDW_EXTRA pc_bias++
102 :
103 : # define INTERP_BRANCH_BEGIN(opcode) \
104 186561 : interp_##opcode: { \
105 186561 : ulong _insns = pc - pc_bias + 1UL; \
106 186561 : vm->ic += _insns; \
107 186561 : if( FD_UNLIKELY( _insns>cu ) ) goto sigcost; /* Note: untaken branches don't consume BTB */ \
108 186561 : cu -= _insns; \
109 185748 : }
110 :
111 : # define INTERP_BRANCH_END \
112 2632689 : pc++; \
113 2632689 : pc_bias = pc; \
114 2632689 : block_text_limit = fd_ulong_min( text_cnt, pc+cu+cu+1UL ); \
115 2632731 : INTERP_NEXT
116 :
117 : # define INTERP_FAULT \
118 1719 : do { \
119 1719 : ulong _insns = pc - pc_bias + 1UL; \
120 1719 : vm->ic += _insns; \
121 1719 : if( FD_UNLIKELY( _insns > cu ) ) err = FD_VM_ERR_EBPF_EXCEEDED_MAX_INSTRUCTIONS; \
122 1719 : cu -= fd_ulong_min( _insns, cu ); \
123 1719 : } while(0)
124 :
125 : # else /* FD_VM_INTERP_EXE_TRACING_ENABLED */
126 :
127 : /* Unlike prod, use two live registers to accurately report CU for
128 : each instruction. */
129 : ulong pc0 = pc;
130 : ulong ic_correction = 0UL;
131 69 : ulong block_text_limit = fd_ulong_min( text_cnt, pc0+cu+cu+1UL );
132 :
133 24 : # define INTERP_LDDW_EXTRA ic_correction++
134 :
135 : # define INTERP_BRANCH_BEGIN(opcode) \
136 141 : interp_##opcode: \
137 141 : ic_correction = pc - pc0 + 1UL - ic_correction; \
138 141 : vm->ic += ic_correction; \
139 141 : if( FD_UNLIKELY( ic_correction>cu ) ) goto sigcost; /* Note: untaken branches don't consume BTB */ \
140 141 : cu -= ic_correction; \
141 141 : ic_correction = 0UL;
142 :
143 : # define INTERP_BRANCH_END \
144 63 : pc++; \
145 63 : pc0 = pc; \
146 78 : block_text_limit = fd_ulong_min( text_cnt, pc0+cu+cu+1UL ); \
147 63 : goto interp_exec
148 :
149 : # define INTERP_FAULT \
150 3 : do { \
151 3 : ic_correction = pc - pc0 + 1UL - ic_correction; \
152 3 : vm->ic += ic_correction; \
153 3 : if( FD_UNLIKELY( ic_correction > cu ) ) err = FD_VM_ERR_EBPF_EXCEEDED_MAX_INSTRUCTIONS; \
154 3 : cu -= fd_ulong_min( ic_correction, cu ); \
155 3 : } while(0)
156 :
157 : # endif
158 :
159 36 : # define INTERP_STACK_PUSH \
160 735 : do { \
161 735 : ulong _fc = vm->frame_cnt; \
162 735 : fd_vm_shadow_t * _sh = &vm->shadow[ _fc ]; \
163 735 : _sh->r6 = reg[6]; \
164 735 : _sh->r7 = reg[7]; \
165 735 : _sh->r8 = reg[8]; \
166 735 : _sh->r9 = reg[9]; \
167 735 : _sh->r10 = reg[10]; \
168 735 : _sh->pc = pc; \
169 735 : vm->frame_cnt = ++_fc; \
170 735 : if( FD_UNLIKELY( _fc>=FD_VM_STACK_FRAME_MAX ) ) goto sigstack; /* Note: untaken branches don't consume BTB */ \
171 735 : reg[10] += vm->stack_frame_sz; \
172 732 : } while(0)
173 :
174 : /* Memory load/store templates. Regions 0..4 share a unified 5-slot
175 : inline TLB (slot 4 caches the active MRU input sub-region via
176 : region_voff[4] and pre-biased region_haddr[4] = haddr - voff);
177 : non-NULL haddr is guaranteed on the fast path. */
178 :
179 36 : # define INTERP_LD(opcode, sz, ld_fn) \
180 20391 : INTERP_INSTR_BEGIN(opcode) { \
181 20391 : ulong _vaddr = REG_SRC + INS_OFF; \
182 20391 : ulong _region = _vaddr >> 32; \
183 20391 : ulong _offset = (ulong)(uint)_vaddr; \
184 20391 : ulong _haddr; \
185 20391 : if( FD_LIKELY( _region<5UL && (ulong)((uint)_offset - vm->region_voff[ _region ])+(sz)<=(ulong)vm->region_ld_sz[ _region ] ) ) { \
186 19881 : _haddr = vm->region_haddr[ _region ] + _offset; \
187 19881 : } else { \
188 510 : _haddr = fd_vm_mem_haddr_ld_slow( vm, _vaddr, (sz) ); \
189 510 : if( FD_UNLIKELY( !_haddr ) ) goto sigsegv; \
190 510 : } \
191 20391 : REG_DST = ld_fn( _haddr ); \
192 137031 : } INTERP_INSTR_END
193 :
194 36 : # define INTERP_ST_IMM(opcode, sz, st_fn, val_expr) \
195 39 : INTERP_INSTR_BEGIN(opcode) { \
196 39 : ulong _vaddr = REG_DST + INS_OFF; \
197 39 : ulong _region = _vaddr >> 32; \
198 39 : ulong _offset = (ulong)(uint)_vaddr; \
199 39 : ulong _haddr; \
200 39 : if( FD_LIKELY( _region<5UL && (ulong)((uint)_offset - vm->region_voff[ _region ])+(sz)<=(ulong)vm->region_st_sz[ _region ] ) ) { \
201 0 : _haddr = vm->region_haddr[ _region ] + _offset; \
202 39 : } else { \
203 39 : _haddr = fd_vm_mem_haddr_st_slow( vm, _vaddr, (sz) ); \
204 39 : if( FD_UNLIKELY( !_haddr ) ) goto sigsegv; \
205 39 : } \
206 39 : st_fn( _haddr, (val_expr) ); \
207 4071 : } INTERP_INSTR_END
208 :
209 36 : # define INTERP_ST_REG(opcode, sz, st_fn, cast_t) \
210 17961 : INTERP_INSTR_BEGIN(opcode) { \
211 17961 : ulong _vaddr = REG_DST + INS_OFF; \
212 17961 : ulong _region = _vaddr >> 32; \
213 17961 : ulong _offset = (ulong)(uint)_vaddr; \
214 17961 : ulong _haddr; \
215 17961 : if( FD_LIKELY( _region<5UL && (ulong)((uint)_offset - vm->region_voff[ _region ])+(sz)<=(ulong)vm->region_st_sz[ _region ] ) ) { \
216 17373 : _haddr = vm->region_haddr[ _region ] + _offset; \
217 17373 : } else { \
218 588 : _haddr = fd_vm_mem_haddr_st_slow( vm, _vaddr, (sz) ); \
219 588 : if( FD_UNLIKELY( !_haddr ) ) goto sigsegv; \
220 588 : } \
221 17961 : st_fn( _haddr, (cast_t)REG_SRC ); \
222 41871 : } INTERP_INSTR_END
223 :
224 : /* Instruction that executes stmt without changing control flow */
225 36 : # define INTERP_ALU(opcode, stmt) \
226 10748871 : INTERP_INSTR_BEGIN(opcode) { ulong * _d = ®_DST; stmt; } INTERP_INSTR_END
227 : /* Instruction that jumps to the immediate offset with cond is true */
228 36 : # define INTERP_JMP(opcode, cond) \
229 2631333 : INTERP_BRANCH_BEGIN(opcode) pc += fd_ulong_if( (cond), INS_OFF, 0UL ); INTERP_BRANCH_END
230 :
231 36 : goto interp_exec;
232 10131 : interp_exec:
233 10131 : if( FD_UNLIKELY( pc>=block_text_limit ) ) goto sigtext_or_sigcost;
234 :
235 : # ifdef FD_VM_INTERP_EXE_TRACING_ENABLED
236 210 : fd_vm_trace_event_exe( vm->trace, pc, vm->ic + ( pc - pc0 - ic_correction ), cu, reg, text + pc, text_cnt - pc, ic_correction, vm->frame_cnt );
237 : # else
238 6991422 : interp_dispatch:
239 6991422 : # endif
240 :
241 6991422 : instr = text[ pc ]; /* Guaranteed in-bounds */
242 6991422 : goto *version_interp_jump_table[ instr & 255UL ]; /* Guaranteed in-bounds */
243 :
244 : /* 0x00 - 0x0f ******************************************************/
245 :
246 6991632 : INTERP_ALU( 0x04, *_d = (ulong)(uint)( (int)*_d + (int)INS_IMM ) ); /* ADD_IMM */
247 137574 : INTERP_ALU( 0x04depr, *_d = (ulong)(long)( (int)*_d + (int)INS_IMM ) ); /* ADD_IMM (depr) */
248 137574 : INTERP_JMP( 0x05, 1 ); /* JA */
249 196494 : INTERP_ALU( 0x07, *_d += INS_SIMM ); /* ADD64_IMM */
250 196494 : INTERP_ALU( 0x0c, *_d = (ulong)(uint)( (int)*_d + (int)REG_SRC ) ); /* ADD_REG */
251 136992 : INTERP_ALU( 0x0cdepr, *_d = (ulong)(long)( (int)*_d + (int)REG_SRC ) ); /* ADD_REG (depr) */
252 153873 : INTERP_ALU( 0x0f, *_d += REG_SRC ); /* ADD64_REG */
253 :
254 : /* 0x10 - 0x1f ******************************************************/
255 :
256 153873 : INTERP_ALU( 0x14, *_d = (ulong)(uint)( (int)INS_IMM - (int)*_d ) ); /* SUB_IMM */
257 137067 : INTERP_ALU( 0x14depr, *_d = (ulong)(long)( (int)*_d - (int)INS_IMM ) ); /* SUB_IMM (depr) */
258 137067 : INTERP_JMP( 0x15, REG_DST==INS_SIMM ); /* JEQ_IMM */
259 15618 : INTERP_ALU( 0x17, *_d = INS_SIMM - *_d ); /* SUB64_IMM */
260 136101 : INTERP_ALU( 0x17depr, *_d -= INS_SIMM ); /* SUB64_IMM(depr) */
261 :
262 136101 : INTERP_INSTR_BEGIN(0x18) { /* LDDW */
263 24570 : ulong * _d = ®_DST;
264 24570 : ulong lo = (ulong)INS_IMM;
265 24570 : pc++;
266 24570 : INTERP_LDDW_EXTRA;
267 24570 : *_d = lo | (text[ pc ] & 0xFFFFFFFF00000000UL);
268 24570 : } INTERP_INSTR_END;
269 :
270 24570 : INTERP_ALU( 0x1c, *_d = (ulong)(uint)( (int)*_d - (int)REG_SRC ) ); /* SUB_REG */
271 137220 : INTERP_ALU( 0x1cdepr, *_d = (ulong)(long)( (int)*_d - (int)REG_SRC ) ); /* SUB_REG (depr) */
272 137220 : INTERP_JMP( 0x1d, REG_DST==REG_SRC ); /* JEQ_REG */
273 180246 : INTERP_ALU( 0x1f, *_d -= REG_SRC ); /* SUB64_REG */
274 :
275 : /* 0x20 - 0x2f ******************************************************/
276 :
277 180246 : INTERP_ALU ( 0x24, *_d = (ulong)(long)( (int)*_d * (int)INS_IMM ) ); /* MUL_IMM */
278 137010 : INTERP_JMP ( 0x25, REG_DST>INS_SIMM ); /* JGT_IMM */
279 3990 : INTERP_ST_IMM( 0x27, sizeof(uchar), fd_vm_mem_st_1, (uchar)INS_IMM ); /* STB */
280 3 : INTERP_LD ( 0x2c, sizeof(uchar), fd_vm_mem_ld_1 ); /* LDXB */
281 15756 : INTERP_JMP ( 0x2d, REG_DST>REG_SRC ); /* JGT_REG */
282 15711 : INTERP_ST_REG( 0x2f, sizeof(uchar), fd_vm_mem_st_1, uchar ); /* STXB */
283 15711 : INTERP_ALU ( 0x27depr, *_d = (ulong)( (long)*_d * (long)INS_SIMM ) ); /* MUL64_IMM */
284 150450 : INTERP_ALU ( 0x2cdepr, *_d = (ulong)(long)( (int)*_d * (int)REG_SRC ) ); /* MUL_REG */
285 137421 : INTERP_ALU ( 0x2fdepr, *_d *= REG_SRC ); /* MUL64_REG */
286 :
287 : /* 0x30 - 0x3f ******************************************************/
288 :
289 166824 : INTERP_ALU ( 0x34, *_d = (ulong)((uint)*_d / INS_IMM) ); /* DIV_IMM */
290 136575 : INTERP_JMP ( 0x35, REG_DST>=INS_SIMM ); /* JGE_IMM */
291 6057 : INTERP_ALU ( 0x36, *_d = (ulong)(( (uint128)*_d * (uint128)(ulong)INS_IMM )>>64) ); /* UHMUL64_IMM */
292 3 : INTERP_ST_IMM( 0x37, sizeof(ushort), fd_vm_mem_st_2, (ushort)INS_IMM ); /* STH */
293 3 : INTERP_LD ( 0x3c, sizeof(ushort), fd_vm_mem_ld_2 ); /* LDXH */
294 60 : INTERP_JMP ( 0x3d, REG_DST>=REG_SRC ); /* JGE_REG */
295 36930 : INTERP_ST_REG( 0x3f, sizeof(ushort), fd_vm_mem_st_2, ushort ); /* STXH */
296 3 : INTERP_ALU ( 0x3e, *_d = (ulong)(( (uint128)*_d * (uint128)REG_SRC ) >> 64) ); /* UHMUL64_REG */
297 3 : INTERP_ALU ( 0x37depr, *_d /= INS_SIMM ); /* DIV64_IMM */
298 151485 : INTERP_ALU ( 0x3cdepr, uint rsrc = (uint)REG_SRC; /* DIV_REG */
299 42 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
300 42 : *_d = (ulong)((uint)*_d / rsrc) );
301 42 : INTERP_ALU ( 0x3fdepr, ulong rsrc = REG_SRC; /* DIV64_REG */
302 30060 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
303 30060 : *_d /= rsrc );
304 :
305 : /* 0x40 - 0x4f ******************************************************/
306 :
307 136560 : INTERP_ALU( 0x44, *_d = (ulong)( (uint)*_d | INS_IMM ) ); /* OR_IMM */
308 136560 : INTERP_JMP( 0x45, !!(REG_DST & INS_SIMM) ); /* JSET_IMM */
309 1248 : INTERP_ALU( 0x46, *_d = (ulong)( (uint)*_d / INS_IMM ) ); /* UDIV32_IMM */
310 136830 : INTERP_ALU( 0x47, *_d |= INS_SIMM ); /* OR64_IMM */
311 136830 : INTERP_ALU( 0x4c, *_d = (ulong)(uint)( *_d | REG_SRC ) ); /* OR_REG */
312 136665 : INTERP_JMP( 0x4d, !!(REG_DST & REG_SRC) ); /* JSET_REG */
313 651 : INTERP_ALU( 0x4e, uint rsrc = (uint)REG_SRC; /* UDIV32_REG */
314 36 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
315 36 : *_d = (ulong)( (uint)*_d / rsrc ) );
316 137349 : INTERP_ALU( 0x4f, *_d |= REG_SRC ); /* OR64_REG */
317 :
318 : /* 0x50 - 0x5f ******************************************************/
319 :
320 137349 : INTERP_ALU( 0x54, *_d = (ulong)( (uint)*_d & INS_IMM ) ); /* AND_IMM */
321 137244 : INTERP_JMP( 0x55, REG_DST!=INS_SIMM ); /* JNE_IMM */
322 31737 : INTERP_ALU( 0x56, *_d /= (ulong)INS_IMM ); /* UDIV64_IMM */
323 137226 : INTERP_ALU( 0x57, *_d &= INS_SIMM ); /* AND64_IMM */
324 137226 : INTERP_ALU( 0x5c, *_d = (ulong)(uint)( *_d & REG_SRC ) ); /* AND_REG */
325 135882 : INTERP_JMP( 0x5d, REG_DST!=REG_SRC ); /* JNE_REG */
326 651 : INTERP_ALU( 0x5e, ulong rsrc = REG_SRC; /* UDIV64_REG */
327 36 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
328 36 : *_d /= rsrc );
329 137262 : INTERP_ALU( 0x5f, *_d &= REG_SRC ); /* AND64_REG */
330 :
331 : /* 0x60 - 0x6f ******************************************************/
332 :
333 137262 : INTERP_ALU( 0x64, *_d = (ulong)UINT_SHL_WRAP ( (uint)*_d, INS_IMM ) ); /* LSH_IMM */
334 136833 : INTERP_JMP( 0x65, (long)REG_DST>(long)INS_SIMM ); /* JSGT_IMM */
335 3690 : INTERP_ALU( 0x66, *_d = (ulong)( (uint)*_d % INS_IMM ) ); /* UREM32_IMM */
336 149859 : INTERP_ALU( 0x67, *_d = ULONG_SHL_WRAP( *_d, INS_IMM ) ); /* LSH64_IMM */
337 149859 : INTERP_ALU( 0x6c, *_d = (ulong)UINT_SHL_WRAP ( (uint)*_d, REG_SRC ) ); /* LSH_REG */
338 137754 : INTERP_JMP( 0x6d, (long)REG_DST>(long)REG_SRC ); /* JSGT_REG */
339 3636 : INTERP_ALU( 0x6e, uint rsrc = (uint)REG_SRC; /* UREM32_REG */
340 36 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
341 36 : *_d = (ulong)( (uint)*_d % rsrc ) );
342 137838 : INTERP_ALU( 0x6f, *_d = ULONG_SHL_WRAP( *_d, REG_SRC ) ); /* LSH64_REG */
343 :
344 : /* 0x70 - 0x7f ******************************************************/
345 :
346 137838 : INTERP_ALU( 0x74, *_d = (ulong)UINT_SHR_WRAP ( (uint)*_d, INS_IMM ) ); /* RSH_IMM */
347 137682 : INTERP_JMP( 0x75, (long)REG_DST>=(long)INS_SIMM ); /* JSGE_IMM */
348 6642 : INTERP_ALU( 0x76, *_d %= (ulong)INS_IMM ); /* UREM64_IMM */
349 136944 : INTERP_ALU( 0x77, *_d = ULONG_SHR_WRAP( *_d, INS_IMM ) ); /* RSH64_IMM */
350 137442 : INTERP_ALU( 0x7c, *_d = (ulong)UINT_SHR_WRAP ( (uint)*_d, (uint)REG_SRC ) ); /* RSH_REG */
351 137442 : INTERP_JMP( 0x7d, (long)REG_DST>=(long)REG_SRC ); /* JSGE_REG */
352 5520 : INTERP_ALU( 0x7e, ulong rsrc = REG_SRC; /* UREM64_REG */
353 36 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
354 36 : *_d %= rsrc );
355 136323 : INTERP_ALU( 0x7f, *_d = ULONG_SHR_WRAP( *_d, REG_SRC ) ); /* RSH64_REG */
356 :
357 : /* 0x80-0x8f ********************************************************/
358 :
359 136446 : INTERP_ALU( 0x84, *_d = (ulong)( -(uint)*_d ) ); /* NEG */
360 :
361 136446 : INTERP_BRANCH_BEGIN(0x85) { /* CALL_IMM */
362 468 : uint imm = INS_IMM;
363 468 : fd_vm_syscall_t const * syscall = fd_vm_syscall_query( imm, vm->syscall_set );
364 468 : if( FD_LIKELY( !syscall ) ) {
365 420 : ulong target_pc;
366 420 : if( FD_UNLIKELY( imm==0x71e3cf81U ) ) { /* "entrypoint" */
367 0 : target_pc = vm->entry_pc;
368 420 : } else {
369 420 : target_pc = (ulong)fd_pchash_inverse( imm );
370 420 : if( FD_UNLIKELY( target_pc>=text_cnt || !fd_sbpf_calldests_test( vm->calldests, target_pc ) ) ) {
371 12 : goto sigillbr;
372 12 : }
373 420 : }
374 408 : INTERP_STACK_PUSH;
375 408 : pc = target_pc - 1UL;
376 408 : } else {
377 48 : INTERP_SYSCALL_EXEC;
378 48 : }
379 468 : }
380 456 : INTERP_BRANCH_END;
381 :
382 : /* SIMD-0178: Static syscalls (SBPF V3+)
383 : https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/interpreter.rs#L542-L577 */
384 450 : INTERP_BRANCH_BEGIN(0x85_static) { /* CALL_IMM (static syscalls) */
385 417 : ulong src = INS_SRC;
386 417 : uint imm = INS_IMM;
387 417 : if( src == 1UL ) {
388 222 : long target_pc_l = fd_long_sat_add( (long)pc, fd_long_sat_add( (long)(int)imm, 1L ) );
389 222 : if( FD_UNLIKELY( target_pc_l<0L || (ulong)target_pc_l>=text_cnt ) ) goto sigillbr;
390 210 : INTERP_STACK_PUSH;
391 207 : pc = (ulong)target_pc_l - 1UL;
392 207 : } else if( src == 0UL ) {
393 189 : fd_vm_syscall_t const * syscall = fd_vm_syscall_query( imm, vm->syscall_set );
394 189 : if( FD_UNLIKELY( !syscall ) ) goto sigillbr;
395 360 : INTERP_SYSCALL_EXEC;
396 360 : } else {
397 6 : goto sigillbr;
398 6 : }
399 417 : }
400 387 : INTERP_BRANCH_END;
401 :
402 387 : INTERP_ALU ( 0x86, *_d = (ulong)( (uint)*_d * INS_IMM ) ); /* LMUL32_IMM */
403 39 : INTERP_ST_IMM( 0x87, sizeof(uint), fd_vm_mem_st_4, INS_IMM ); /* STW */
404 3 : INTERP_ALU ( 0x87depr, *_d = -*_d ); /* NEG64 */
405 137022 : INTERP_LD ( 0x8c, sizeof(uint), fd_vm_mem_ld_4 ); /* LDXW */
406 :
407 117 : INTERP_BRANCH_BEGIN(0x8d) { /* CALL_REG */
408 117 : ulong rsrc = REG_SRC;
409 117 : ulong rdst = REG_DST;
410 117 : INTERP_STACK_PUSH;
411 :
412 117 : ulong sbpf_ver = vm->sbpf_version;
413 117 : ulong vaddr = fd_sbpf_callx_uses_src_reg_enabled( sbpf_ver ) ? rsrc
414 117 : : fd_sbpf_callx_uses_dst_reg_enabled( sbpf_ver ) ? rdst
415 96 : : reg[ INS_IMM & 15U ];
416 :
417 117 : ulong region = vaddr >> 32;
418 117 : ulong target_pc = ((vaddr & FD_VM_OFFSET_MASK) - vm->text_off) / 8UL;
419 117 : if( FD_UNLIKELY( (region!=1UL) | (target_pc>=text_cnt) ) ) goto sigtextbr;
420 75 : pc = target_pc - 1UL;
421 75 : } INTERP_BRANCH_END;
422 :
423 75 : INTERP_ALU ( 0x8e, *_d = (ulong)( (uint)*_d * (uint)REG_SRC ) ); /* LMUL32_REG */
424 33 : INTERP_ST_REG( 0x8f, sizeof(uint), fd_vm_mem_st_4, uint ); /* STXW */
425 :
426 : /* 0x90 - 0x9f ******************************************************/
427 :
428 136725 : INTERP_ALU( 0x94, *_d = (ulong)( (uint)*_d % INS_IMM ) ); /* MOD_IMM */
429 :
430 136725 : INTERP_BRANCH_BEGIN(0x95) { /* EXIT */
431 7854 : ulong _fc = vm->frame_cnt;
432 7854 : if( FD_UNLIKELY( !_fc ) ) goto sigexit;
433 501 : _fc--;
434 501 : vm->frame_cnt = _fc;
435 501 : fd_vm_shadow_t const * sh = &vm->shadow[ _fc ];
436 501 : reg[6] = sh->r6;
437 501 : reg[7] = sh->r7;
438 501 : reg[8] = sh->r8;
439 501 : reg[9] = sh->r9;
440 501 : reg[10] = sh->r10;
441 501 : pc = sh->pc;
442 501 : }
443 501 : INTERP_BRANCH_END;
444 :
445 501 : INTERP_ALU ( 0x96, *_d *= INS_SIMM ); /* LMUL64_IMM */
446 39 : INTERP_ST_IMM( 0x97, sizeof(ulong), fd_vm_mem_st_8, INS_SIMM ); /* STQ */
447 3 : INTERP_LD ( 0x9c, sizeof(ulong), fd_vm_mem_ld_8 ); /* LDXQ */
448 4332 : INTERP_ALU ( 0x9e, *_d *= REG_SRC ); /* LMUL64_REG */
449 2184 : INTERP_ST_REG( 0x9f, sizeof(ulong), fd_vm_mem_st_8, ulong ); /* STXQ */
450 2184 : INTERP_ALU ( 0x97depr, *_d %= INS_SIMM ); /* MOD64_IMM */
451 137019 : INTERP_ALU ( 0x9cdepr, uint rsrc = (uint)REG_SRC; /* MOD_REG */
452 42 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
453 42 : *_d = (ulong)( (uint)*_d % rsrc ) );
454 42 : INTERP_ALU ( 0x9fdepr, ulong rsrc = REG_SRC; /* MOD64_REG */
455 42 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
456 42 : *_d %= rsrc );
457 :
458 : /* 0xa0 - 0xaf ******************************************************/
459 :
460 136353 : INTERP_ALU( 0xa4, *_d = (ulong)( (uint)*_d ^ INS_IMM ) ); /* XOR_IMM */
461 136353 : INTERP_JMP( 0xa5, REG_DST<INS_SIMM ); /* JLT_IMM */
462 137409 : INTERP_ALU( 0xa7, *_d ^= INS_SIMM ); /* XOR64_IMM */
463 137409 : INTERP_ALU( 0xac, *_d = (ulong)(uint)( *_d ^ REG_SRC ) ); /* XOR_REG */
464 136023 : INTERP_JMP( 0xad, REG_DST<REG_SRC ); /* JLT_REG */
465 138081 : INTERP_ALU( 0xaf, *_d ^= REG_SRC ); /* XOR64_REG */
466 :
467 : /* 0xb0 - 0xbf ******************************************************/
468 :
469 138081 : INTERP_ALU( 0xb4, *_d = (ulong)INS_IMM ); /* MOV_IMM */
470 137544 : INTERP_JMP( 0xb5, REG_DST<=INS_SIMM ); /* JLE_IMM */
471 6066 : INTERP_ALU( 0xb6, *_d = (ulong)(( (int128)(long)*_d * (int128)(long)INS_SIMM ) >> 64) ); /* SHMUL64_IMM */
472 260217 : INTERP_ALU( 0xb7, *_d = INS_SIMM ); /* MOV64_IMM */
473 260217 : INTERP_ALU( 0xbc, *_d = (ulong)(long)(int)REG_SRC ); /* MOV_REG */
474 137271 : INTERP_ALU( 0xbcdepr, *_d = (ulong)(uint) REG_SRC ); /* MOV_REG (depr) */
475 137271 : INTERP_JMP( 0xbd, REG_DST<=REG_SRC ); /* JLE_REG */
476 4863 : INTERP_ALU( 0xbe, *_d = (ulong)(( (int128)(long)*_d * (int128)(long)REG_SRC ) >> 64) ); /* SHMUL64_REG */
477 230529 : INTERP_ALU( 0xbf, *_d = REG_SRC ); /* MOV64_REG */
478 :
479 : /* 0xc0 - 0xcf ******************************************************/
480 :
481 230529 : INTERP_ALU( 0xc4, *_d = (ulong)(uint)INT_SHR_WRAP ( (int) *_d, INS_IMM ) ); /* ARSH_IMM */
482 137271 : INTERP_JMP( 0xc5, (long)REG_DST<(long)INS_SIMM ); /* JSLT_IMM */
483 3072 : INTERP_ALU( 0xc6, int rdst = (int)*_d; /* SDIV32_IMM */
484 39 : int simm = (int)INS_IMM;
485 39 : if( FD_UNLIKELY( (rdst==INT_MIN) & (simm==-1) ) ) goto sigfpeof;
486 39 : *_d = (ulong)(uint)( rdst / simm ) );
487 136947 : INTERP_ALU( 0xc7, *_d = (ulong) LONG_SHR_WRAP( (long)*_d, INS_IMM ) ); /* ARSH64_IMM */
488 137304 : INTERP_ALU( 0xcc, *_d = (ulong)(uint)INT_SHR_WRAP ( (int) *_d, (uint)REG_SRC ) ); /* ARSH_REG */
489 137304 : INTERP_JMP( 0xcd, (long)REG_DST<(long)REG_SRC ); /* JSLT_REG */
490 3078 : INTERP_ALU( 0xce, int rsrc = (int)REG_SRC; /* SDIV32_REG */
491 36 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
492 36 : int rdst = (int)*_d;
493 36 : if( FD_UNLIKELY( (rdst==INT_MIN) & (rsrc==-1) ) ) goto sigfpeof;
494 36 : *_d = (ulong)(uint)( rdst / rsrc ) );
495 136167 : INTERP_ALU( 0xcf, *_d = (ulong) LONG_SHR_WRAP( (long)*_d, REG_SRC ) ); /* ARSH64_REG */
496 :
497 : /* 0xd0 - 0xdf ******************************************************/
498 :
499 136167 : INTERP_ALU( 0xd4, switch( INS_IMM ) { /* END_LE */
500 15 : case 16U: *_d = (ushort)*_d; break;
501 15 : case 32U: *_d = (uint) *_d; break;
502 15 : case 64U: break;
503 15 : default: goto siginv; } );
504 :
505 2442 : INTERP_JMP( 0xd5, (long)REG_DST<=(long)INS_SIMM ); /* JSLE_IMM */
506 :
507 2442 : INTERP_ALU( 0xd6, long rdst = (long)*_d; /* SDIV64_IMM */
508 39 : long simm = (long)INS_SIMM;
509 39 : if( FD_UNLIKELY( (rdst==LONG_MIN) & (simm==-1L) ) ) goto sigfpeof;
510 39 : *_d = (ulong)( rdst / simm ) );
511 :
512 60 : INTERP_ALU( 0xdc, ulong rdst = *_d;
513 60 : switch( INS_IMM ) { /* END_BE */
514 60 : case 16U: *_d = (ulong)fd_ushort_bswap( (ushort)rdst ); break;
515 60 : case 32U: *_d = (ulong)fd_uint_bswap ( (uint) rdst ); break;
516 60 : case 64U: *_d = fd_ulong_bswap ( rdst ); break;
517 60 : default: goto siginv; } );
518 :
519 1830 : INTERP_JMP( 0xdd, (long)REG_DST<=(long)REG_SRC ); /* JSLE_REG */
520 :
521 1830 : INTERP_ALU( 0xde, long rsrc = (long)REG_SRC; /* SDIV64_REG */
522 36 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
523 36 : long rdst = (long)*_d;
524 36 : if( FD_UNLIKELY( (rdst==LONG_MIN) & (rsrc==-1L) ) ) goto sigfpeof;
525 36 : *_d = (ulong)( rdst / rsrc ) );
526 :
527 : /* 0xe0 - 0xef ******************************************************/
528 :
529 39 : INTERP_ALU( 0xe6, int rdst = (int)*_d; /* SREM32_IMM */
530 39 : int simm = (int)INS_IMM;
531 39 : if( FD_UNLIKELY( (rdst==INT_MIN) & (simm==-1) ) ) goto sigfpeof;
532 39 : *_d = (ulong)(uint)( rdst % simm ) );
533 :
534 39 : INTERP_ALU( 0xee, int rsrc = (int)REG_SRC; /* SREM32_REG */
535 36 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
536 36 : int rdst = (int)*_d;
537 36 : if( FD_UNLIKELY( (rdst==INT_MIN) & (rsrc==-1) ) ) goto sigfpeof;
538 36 : *_d = (ulong)(uint)( rdst % rsrc ) );
539 :
540 : /* 0xf0 - 0xff ******************************************************/
541 :
542 39 : INTERP_ALU( 0xf6, long rdst = (long)*_d; /* SREM64_IMM */
543 39 : long simm = (long)INS_SIMM;
544 39 : if( FD_UNLIKELY( (rdst==LONG_MIN) & (simm==-1L) ) ) goto sigfpeof;
545 39 : *_d = (ulong)( rdst % simm ) );
546 :
547 39 : INTERP_ALU( 0xf7, *_d |= (instr & 0xFFFFFFFF00000000UL) ); /* HOR64 */
548 :
549 36 : INTERP_ALU( 0xfe, long rsrc = (long)REG_SRC; /* SREM64_REG */
550 36 : if( FD_UNLIKELY( !rsrc ) ) goto sigfpe;
551 36 : long rdst = (long)*_d;
552 36 : if( FD_UNLIKELY( (rdst==LONG_MIN) & (rsrc==-1L) ) ) goto sigfpeof;
553 36 : *_d = (ulong)( rdst % rsrc ) );
554 :
555 : /* SIMD-0377: JMP32
556 : https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/interpreter.rs#L480-L501
557 :
558 : 0x16 - 0xde ******************************************************/
559 :
560 1530 : INTERP_JMP( 0x16_jmp32, (uint)REG_DST== INS_IMM ); /* JEQ32_IMM */
561 1530 : INTERP_JMP( 0x1e_jmp32, (uint)REG_DST==(uint)REG_SRC ); /* JEQ32_REG */
562 930 : INTERP_JMP( 0x26_jmp32, (uint)REG_DST> INS_IMM ); /* JGT32_IMM */
563 930 : INTERP_JMP( 0x2e_jmp32, (uint)REG_DST> (uint)REG_SRC ); /* JGT32_REG */
564 918 : INTERP_JMP( 0x36_jmp32, (uint)REG_DST>= INS_IMM ); /* JGE32_IMM */
565 918 : INTERP_JMP( 0x3e_jmp32, (uint)REG_DST>=(uint)REG_SRC ); /* JGE32_REG */
566 930 : INTERP_JMP( 0x46_jmp32, !!((uint)REG_DST & INS_IMM) ); /* JSET32_IMM */
567 930 : INTERP_JMP( 0x4e_jmp32, !!((uint)REG_DST &(uint)REG_SRC) ); /* JSET32_REG */
568 321 : INTERP_JMP( 0x56_jmp32, (uint)REG_DST!= INS_IMM ); /* JNE32_IMM */
569 321 : INTERP_JMP( 0x5e_jmp32, (uint)REG_DST!=(uint)REG_SRC ); /* JNE32_REG */
570 1236 : INTERP_JMP( 0x66_jmp32, (int) REG_DST> (int) INS_IMM ); /* JSGT32_IMM */
571 1236 : INTERP_JMP( 0x6e_jmp32, (int) REG_DST> (int) REG_SRC ); /* JSGT32_REG */
572 621 : INTERP_JMP( 0x76_jmp32, (int) REG_DST>=(int) INS_IMM ); /* JSGE32_IMM */
573 621 : INTERP_JMP( 0x7e_jmp32, (int) REG_DST>=(int) REG_SRC ); /* JSGE32_REG */
574 621 : INTERP_JMP( 0xa6_jmp32, (uint)REG_DST< INS_IMM ); /* JLT32_IMM */
575 621 : INTERP_JMP( 0xae_jmp32, (uint)REG_DST< (uint)REG_SRC ); /* JLT32_REG */
576 615 : INTERP_JMP( 0xb6_jmp32, (uint)REG_DST<= INS_IMM ); /* JLE32_IMM */
577 615 : INTERP_JMP( 0xbe_jmp32, (uint)REG_DST<=(uint)REG_SRC ); /* JLE32_REG */
578 324 : INTERP_JMP( 0xc6_jmp32, (int) REG_DST< (int) INS_IMM ); /* JSLT32_IMM */
579 324 : INTERP_JMP( 0xce_jmp32, (int) REG_DST< (int) REG_SRC ); /* JSLT32_REG */
580 615 : INTERP_JMP( 0xd6_jmp32, (int) REG_DST<=(int) INS_IMM ); /* JSLE32_IMM */
581 615 : INTERP_JMP( 0xde_jmp32, (int) REG_DST<=(int) REG_SRC ); /* JSLE32_REG */
582 :
583 315 : sigtext_or_sigcost:
584 102 : if( pc<text_cnt ) goto sigcost; else goto sigtext;
585 87 : sigtext: err = FD_VM_ERR_EBPF_EXECUTION_OVERRUN; INTERP_FAULT; goto interp_halt;
586 42 : sigtextbr: err = FD_VM_ERR_EBPF_CALL_OUTSIDE_TEXT_SEGMENT; /* ic current */ /* cu current */ goto interp_halt;
587 3 : sigstack: err = FD_VM_ERR_EBPF_CALL_DEPTH_EXCEEDED; /* ic current */ /* cu current */ goto interp_halt;
588 1179 : sigill: err = FD_VM_ERR_EBPF_UNSUPPORTED_INSTRUCTION; INTERP_FAULT; goto interp_halt;
589 39 : sigillbr: err = FD_VM_ERR_EBPF_UNSUPPORTED_INSTRUCTION; /* ic current */ /* cu current */ goto interp_halt;
590 18 : siginv: err = FD_VM_ERR_EBPF_INVALID_INSTRUCTION; INTERP_FAULT; goto interp_halt;
591 270 : sigsegv: err = fd_vm_generate_access_violation( vm->segv_vaddr, vm->sbpf_version ); INTERP_FAULT; goto interp_halt;
592 828 : sigcost: err = FD_VM_ERR_EBPF_EXCEEDED_MAX_INSTRUCTIONS; /* ic current */ cu = 0UL; goto interp_halt;
593 0 : sigsyscall: err = FD_VM_ERR_EBPF_SYSCALL_ERROR; /* ic current */ /* cu current */ goto interp_halt;
594 132 : sigfpe: err = FD_VM_ERR_EBPF_DIVIDE_BY_ZERO; INTERP_FAULT; goto interp_halt;
595 36 : sigfpeof: err = FD_VM_ERR_EBPF_DIVIDE_OVERFLOW; INTERP_FAULT; goto interp_halt;
596 7353 : sigexit: /* err current */ /* ic current */ /* cu current */ goto interp_halt;
597 :
598 9987 : interp_halt:
599 :
600 : /* Pack the unpacked execution state into vm to give a precise view of
601 : the execution when the vm halted. */
602 :
603 9987 : vm->pc = pc;
604 9987 : vm->cu = cu;
605 :
606 9987 : # undef text_cnt
607 9987 : # undef reg
608 :
609 9987 : # undef INTERP_JMP
610 9987 : # undef INTERP_ALU
611 9987 : # undef INTERP_ST_REG
612 9987 : # undef INTERP_ST_IMM
613 9987 : # undef INTERP_LD
614 9987 : # undef INTERP_STACK_PUSH
615 9987 : # undef INTERP_FAULT
616 9987 : # undef INTERP_BRANCH_END
617 9987 : # undef INTERP_BRANCH_BEGIN
618 9987 : # undef INTERP_LDDW_EXTRA
619 9987 : # undef INTERP_INSTR_END
620 9987 : # undef INTERP_INSTR_BEGIN
621 9987 : # undef INTERP_NEXT
622 9987 : # undef INTERP_SYSCALL_EXEC
623 9987 : # undef INTERP_SYSCALL_EXEC_DUMP
624 9987 : # undef LONG_SHR_WRAP
625 9987 : # undef INT_SHR_WRAP
626 9987 : # undef UINT_SHR_WRAP
627 9987 : # undef UINT_SHL_WRAP
628 9987 : # undef ULONG_SHR_WRAP
629 9987 : # undef ULONG_SHL_WRAP
630 9987 : # undef INS_SIMM
631 9987 : # undef INS_IMM
632 9987 : # undef INS_OFF
633 9987 : # undef REG_SRC
634 9987 : # undef REG_DST
635 9987 : # undef INS_SRC
636 9987 : # undef INS_DST
637 :
638 9987 : # if defined(__clang__)
639 9987 : # pragma clang diagnostic pop
640 9987 : # endif
641 :
642 9987 : # if defined(__GNUC__)
643 9987 : # pragma GCC diagnostic pop
644 9987 : # endif
|