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 : /* FIXME: SIGILLS FOR VARIOUS THINGS THAT HAVE UNNECESSARY BITS IN IMM
9 : SET? (LIKE WIDE SHIFTS?) */
10 :
11 0 : # if defined(__GNUC__) /* -Wpedantic rejects labels as values and rejects goto *expr */
12 0 : # pragma GCC diagnostic push
13 0 : # pragma GCC diagnostic ignored "-Wpedantic"
14 0 : # endif
15 :
16 0 : # if defined(__clang__) /* Clang is differently picky about labels as values and goto *expr */
17 0 : # pragma clang diagnostic push
18 0 : # pragma clang diagnostic ignored "-Wpedantic"
19 0 : # pragma clang diagnostic ignored "-Wgnu-label-as-value"
20 0 : # endif
21 :
22 : /* Include the jump table */
23 :
24 11355 : # include "fd_vm_interp_jump_table.c"
25 :
26 : /* Update the jump table based on SBPF version */
27 :
28 0 : ulong sbpf_version = vm->sbpf_version;
29 :
30 : /* Unpack the VM state */
31 :
32 0 : ulong pc = vm->pc;
33 0 : ulong ic = vm->ic;
34 0 : ulong cu = vm->cu;
35 0 : ulong frame_cnt = vm->frame_cnt;
36 :
37 0 : void const * const * const version_interp_jump_table = interp_jump_table[ sbpf_version ];
38 :
39 : /* FD_VM_INTERP_INSTR_EXEC loads the first word of the instruction at
40 : pc, parses it, fetches the associated register values and then
41 : jumps to the code that executes the instruction. On normal
42 : instruction execution, the pc will be updated and
43 : FD_VM_INTERP_INSTR_EXEC will be invoked again to do the next
44 : instruction. After a normal halt, this will branch to interp_halt.
45 : Otherwise, it will branch to the appropriate normal termination. */
46 :
47 0 : ulong instr;
48 0 : ulong opcode;
49 0 : ulong dst;
50 0 : ulong src;
51 0 : ulong offset; /* offset is 16-bit but always sign extended, so we handle cast once */
52 0 : uint imm;
53 0 : ulong reg_dst;
54 0 : ulong reg_src;
55 :
56 : /* These mimic the exact Rust semantics for wrapping_shl and wrapping_shr. */
57 :
58 : /* u64::wrapping_shl: a.unchecked_shl(b & (64 - 1))
59 :
60 : https://doc.rust-lang.org/std/primitive.u64.html#method.wrapping_shl
61 : */
62 11004 : #define FD_RUST_ULONG_WRAPPING_SHL( a, b ) ((a) << ( (b) & ( 63 ) ))
63 :
64 : /* u64::wrapping_shr: a.unchecked_shr(b & (64 - 1))
65 :
66 : https://doc.rust-lang.org/std/primitive.u64.html#method.wrapping_shr
67 : */
68 126 : #define FD_RUST_ULONG_WRAPPING_SHR( a, b ) ((a) >> ( (b) & ( 63 ) ))
69 :
70 : /* u32::wrapping_shl: a.unchecked_shl(b & (32 - 1))
71 :
72 : https://doc.rust-lang.org/std/primitive.u32.html#method.wrapping_shl
73 : */
74 900 : #define FD_RUST_UINT_WRAPPING_SHL( a, b ) ((a) << ( (b) & ( 31 ) ))
75 :
76 : /* u32::wrapping_shr: a.unchecked_shr(b & (32 - 1))
77 :
78 : https://doc.rust-lang.org/std/primitive.u32.html#method.wrapping_shr
79 : */
80 18 : #define FD_RUST_UINT_WRAPPING_SHR( a, b ) ((a) >> ( (b) & ( 31 ) ))
81 :
82 : /* i32::wrapping_shr: a.unchecked_shr(b & (32 - 1))
83 :
84 : https://doc.rust-lang.org/std/primitive.i32.html#method.wrapping_shr
85 : */
86 243 : #define FD_RUST_INT_WRAPPING_SHR( a, b ) ((a) >> ( (b) & ( 31 ) ))
87 :
88 : /* i64::wrapping_shr: a.unchecked_shr(b & (64 - 1))
89 :
90 : https://doc.rust-lang.org/std/primitive.i64.html#method.wrapping_shr
91 : */
92 423 : #define FD_RUST_LONG_WRAPPING_SHR( a, b ) ((a) >> ( (b) & ( 63 ) ))
93 :
94 :
95 0 : # define FD_VM_INTERP_INSTR_EXEC \
96 664047 : if( FD_UNLIKELY( pc>=block_text_limit ) ) goto sigtext_or_sigcost; /* Note: untaken branches don't consume BTB */ \
97 664047 : instr = text[ pc ]; /* Guaranteed in-bounds */ \
98 663903 : opcode = fd_vm_instr_opcode( instr ); /* in [0,256) even if malformed */ \
99 663903 : dst = fd_vm_instr_dst ( instr ); /* in [0, 16) even if malformed */ \
100 663903 : src = fd_vm_instr_src ( instr ); /* in [0, 16) even if malformed */ \
101 663903 : offset = fd_vm_instr_offset( instr ); /* in [-2^15,2^15) even if malformed */ \
102 663903 : imm = fd_vm_instr_imm ( instr ); /* in [0,2^32) even if malformed */ \
103 663903 : reg_dst = reg[ dst ]; /* Guaranteed in-bounds */ \
104 663903 : reg_src = reg[ src ]; /* Guaranteed in-bounds */ \
105 663903 : goto *version_interp_jump_table[ opcode ] /* Guaranteed in-bounds */
106 :
107 : /* FD_VM_INTERP_SYSCALL_EXEC
108 : (macro to handle the logic of 0x85 pre- and post- SIMD-0178: static syscalls)
109 :
110 : Setup.
111 : Update the vm with the current vm execution state for the
112 : syscall. Note that BRANCH_BEGIN has pc at the syscall and
113 : already updated ic and cu to reflect all instructions up to
114 : and including the syscall instruction itself.
115 :
116 : Execution.
117 : Do the syscall. The return value is placed into vm->reg[0].
118 : At this point, vm->cu is positive.
119 :
120 : Error handling.
121 : If we trust syscall implementations to handle the vm state
122 : correctly, the below could be implemented as unpacking the vm
123 : state and jumping to sigsys on error. But we provide some
124 : extra protection to make various strong guarantees:
125 :
126 : - We do not let the syscall modify pc currently as nothing
127 : requires this and it reduces risk of a syscall bug mucking
128 : up the interpreter. If there ever was a syscall that
129 : needed to modify the pc (e.g. a syscall that has execution
130 : resume from a different location than the instruction
131 : following the syscall), do "pc = vm->pc" below.
132 :
133 : - We do not let the syscall modify ic currently as nothing
134 : requires this and it keeps the ic precise. If a future
135 : syscall needs this, do "ic = vm->ic" below.
136 :
137 : - We do not let the syscall increase cu as nothing requires
138 : this and it guarantees the interpreter will halt in a
139 : reasonable finite amount of time. If a future syscall
140 : needs this, do "cu = vm->cu" below.
141 :
142 : - A syscall that returns SIGCOST is always treated as though
143 : it also zerod cu.
144 :
145 : At this point, vm->cu is whatever the syscall tried to set
146 : and cu is positive.
147 :
148 : Exit
149 : At this point, cu is positive and err is clear.
150 : */
151 :
152 0 : # define FD_VM_INTERP_SYSCALL_EXEC_DUMP \
153 : /* Dumping for debugging purposes */ \
154 42 : if( FD_UNLIKELY( vm->dump_syscall_to_pb ) ) { \
155 0 : fd_dump_vm_syscall_to_protobuf( vm, syscall->name ); \
156 0 : }
157 :
158 0 : # define FD_VM_INTERP_SYSCALL_EXEC \
159 : /* Setup */ \
160 42 : vm->pc = pc; \
161 42 : vm->ic = ic; \
162 42 : vm->cu = cu; \
163 42 : vm->frame_cnt = frame_cnt; \
164 42 : FD_VM_INTERP_SYSCALL_EXEC_DUMP \
165 : /* Execution */ \
166 42 : err = syscall->func( vm, reg[1], reg[2], reg[3], reg[4], reg[5] ); \
167 : /* Error handling */ \
168 42 : ulong cu_req = vm->cu; \
169 42 : cu = fd_ulong_min( cu_req, cu ); \
170 42 : if( FD_UNLIKELY( err ) ) { \
171 0 : if( err==FD_VM_SYSCALL_ERR_COMPUTE_BUDGET_EXCEEDED ) cu = 0UL; /* cmov */ \
172 0 : FD_VM_TEST_ERR_EXISTS( vm ); \
173 0 : goto sigsyscall; \
174 0 : } \
175 : /* Exit */
176 :
177 :
178 : /* FD_VM_INTERP_INSTR_BEGIN / FD_VM_INTERP_INSTR_END bracket opcode's
179 : implementation for an opcode that does not branch. On entry, the
180 : instruction word has been unpacked into dst / src / offset / imm
181 : and reg[dst] / reg[src] has been prefetched into reg_dst / reg_src. */
182 :
183 480939 : # define FD_VM_INTERP_INSTR_BEGIN(opcode) interp_##opcode:
184 :
185 : # ifndef FD_VM_INTERP_EXE_TRACING_ENABLED /* Non-tracing path only, ~0.3% faster in some benchmarks, slower in others but more code footprint */
186 480537 : # define FD_VM_INTERP_INSTR_END pc++; FD_VM_INTERP_INSTR_EXEC
187 : # else /* Use this version when tracing or optimizing code footprint */
188 0 : # define FD_VM_INTERP_INSTR_END pc++; goto interp_exec
189 : # endif
190 :
191 : /* Instead of doing a lot of compute budget calcs and tests every
192 : instruction, we note that the program counter increases
193 : monotonically after a branch (or a program start) until the next
194 : branch (or program termination). We save the program counter of
195 : the start of such a segment in pc0. Whenever we encounter a branch
196 : (or a program termination) at pc, we know we processed pc-pc0+1
197 : text words (including the text word for the branch instruction
198 : itself as all branch instructions are single word).
199 :
200 : Each instruction costs 1 cu (syscalls can cost extra on top of
201 : this that is accounted separately in CALL_IMM below). Since there
202 : could have been multiword instructions in this segment, at start of
203 : such a segment, we zero out the accumulator ic_correction and have
204 : every multiword instruction in the segment accumulate the number of
205 : extra text words it has to this variable. (Sigh ... it would be a
206 : lot simpler to bill based on text words processed but this would be
207 : very difficult to make this protocol change at this point.)
208 :
209 : When we encounter a branch at pc, the number of instructions
210 : processed (and thus the number of compute units to bill for that
211 : segment) is thus:
212 :
213 : pc - pc0 + 1 - ic_correction */
214 :
215 0 : ulong pc0 = pc;
216 0 : ulong ic_correction = 0UL;
217 :
218 : /* Compute the text limit for a basic block. This bounds the number
219 : of instructions that the basic block can execute linearly.
220 :
221 : There are two factors: the remaining CU budget as of the start of
222 : the basic block, and the text_cnt. The tightest bound is the
223 : lesser of the two.
224 :
225 : We use 2*(the remaining CU budget) because each LDDW instruction
226 : costs 1 CU, but occupies two text words, so in the worst case (a
227 : block full of LDDW instructions) the block text limit should be
228 : doubled.
229 :
230 : It is safe to not use saturating addition here. Both addends are
231 : bounded far below ULONG_MAX before any block executes:
232 :
233 : pc0 <= text_cnt <= 1310720 (see fd_sbpf_loader.h)
234 : cu <= 1400000 (FD_MAX_COMPUTE_UNIT_LIMIT)
235 :
236 : pc0 + cu + cu + 1 < 1310720 + 1400000 + 1400000 + 1 < ULONG_MAX */
237 :
238 183480 : # define FD_VM_INTERP_BLOCK_TEXT_LIMIT fd_ulong_min( text_cnt, pc0+cu+cu+1UL )
239 :
240 11355 : ulong block_text_limit = FD_VM_INTERP_BLOCK_TEXT_LIMIT;
241 :
242 0 : # define FD_VM_INTERP_BRANCH_BEGIN(opcode) \
243 181785 : interp_##opcode: \
244 : /* Bill linear text segment and this branch instruction as per the above */ \
245 181785 : ic_correction = pc - pc0 + 1UL - ic_correction; \
246 181785 : ic += ic_correction; \
247 181785 : if( FD_UNLIKELY( ic_correction>cu ) ) goto sigcost; /* Note: untaken branches don't consume BTB */ \
248 181785 : cu -= ic_correction; \
249 : /* At this point, cu>=0 */ \
250 180978 : ic_correction = 0UL;
251 :
252 : /* FIXME: debatable if it is better to do pc++ here or have the
253 : instruction implementations do it in their code path. */
254 :
255 : # ifndef FD_VM_INTERP_EXE_TRACING_ENABLED /* Non-tracing path only, ~4% faster in some benchmarks, slower in others but more code footprint */
256 : # define FD_VM_INTERP_BRANCH_END \
257 172125 : pc++; \
258 172125 : pc0 = pc; /* Start a new linear segment */ \
259 172125 : block_text_limit = FD_VM_INTERP_BLOCK_TEXT_LIMIT; \
260 172155 : FD_VM_INTERP_INSTR_EXEC
261 : # else /* Use this version when tracing or optimizing code footprint */
262 : # define FD_VM_INTERP_BRANCH_END \
263 0 : pc++; \
264 0 : pc0 = pc; /* Start a new linear segment */ \
265 0 : block_text_limit = FD_VM_INTERP_BLOCK_TEXT_LIMIT; \
266 : /* FIXME: TEST sigsplit HERE */ \
267 0 : goto interp_exec
268 : # endif
269 :
270 : /* FD_VM_INTERP_STACK_PUSH pushes reg[6:9] onto the shadow stack and
271 : advances reg[10] to a new user stack frame. If there are no more
272 : stack frames available, will do a SIGSTACK. */
273 :
274 : /* FIXME: double check faulting is desired on stack overflow. */
275 :
276 : /* FIXME: a pre-belt-sanding FIXME implied the TLB should be updated
277 : to prevent byte code from accessing the stack outside its current
278 : stack frame. But this would break the common practice of a
279 : function passing a pointer to something on its stack into a
280 : function that it calls:
281 :
282 : void foo( ... ) {
283 : ...
284 : int ret;
285 : bar( &ret );
286 : ...
287 : }
288 :
289 : So this probably shouldn't be done. But, if it is in fact
290 : necessary, the TLB updates would be here and in pop. */
291 :
292 : /* FIXME: unvalidated code mucking with r10 */
293 :
294 0 : # define FD_VM_INTERP_STACK_PUSH \
295 645 : shadow[ frame_cnt ].r6 = reg[6]; \
296 645 : shadow[ frame_cnt ].r7 = reg[7]; \
297 645 : shadow[ frame_cnt ].r8 = reg[8]; \
298 645 : shadow[ frame_cnt ].r9 = reg[9]; \
299 645 : shadow[ frame_cnt ].r10 = reg[10]; \
300 645 : shadow[ frame_cnt ].pc = pc; \
301 645 : if( FD_UNLIKELY( ++frame_cnt>=frame_max ) ) goto sigstack; /* Note: untaken branches don't consume BTB */ \
302 645 : reg[10] += vm->stack_frame_sz * vm->stack_push_frame_count; \
303 : /* We subtract the heap cost in the BPF loader */
304 :
305 0 : goto interp_exec; /* Silly but to avoid unused label warning in some configurations */
306 11355 : interp_exec:
307 :
308 : # ifdef FD_VM_INTERP_EXE_TRACING_ENABLED
309 : /* Note: when tracing or optimizing for code footprint, all
310 : instruction execution starts here such that this is only point
311 : where exe tracing diagnostics are needed. */
312 0 : if( FD_UNLIKELY( pc>=block_text_limit ) ) goto sigtext_or_sigcost;
313 0 : fd_vm_trace_event_exe( vm->trace, pc, ic + ( pc - pc0 - ic_correction ), cu, reg, vm->text + pc, vm->text_cnt - pc, ic_correction, frame_cnt );
314 0 : # endif
315 :
316 11355 : FD_VM_INTERP_INSTR_EXEC;
317 :
318 : /* 0x00 - 0x0f ******************************************************/
319 :
320 11355 : FD_VM_INTERP_INSTR_BEGIN(0x04) /* FD_SBPF_OP_ADD_IMM */
321 36 : reg[ dst ] = (ulong)(uint)( (int)reg_dst + (int)imm );
322 36 : FD_VM_INTERP_INSTR_END;
323 :
324 45 : FD_VM_INTERP_INSTR_BEGIN(0x04depr) /* FD_SBPF_OP_ADD_IMM deprecated SIMD-0174 */
325 45 : reg[ dst ] = (ulong)(long)( (int)reg_dst + (int)imm );
326 45 : FD_VM_INTERP_INSTR_END;
327 :
328 11847 : FD_VM_INTERP_BRANCH_BEGIN(0x05) /* FD_SBPF_OP_JA */
329 11841 : pc += offset;
330 11841 : FD_VM_INTERP_BRANCH_END;
331 :
332 55062 : FD_VM_INTERP_INSTR_BEGIN(0x07) /* FD_SBPF_OP_ADD64_IMM */
333 55062 : reg[ dst ] = reg_dst + (ulong)(long)(int)imm;
334 55062 : FD_VM_INTERP_INSTR_END;
335 :
336 33 : FD_VM_INTERP_INSTR_BEGIN(0x0c) /* FD_SBPF_OP_ADD_REG */
337 33 : reg[ dst ] = (ulong)(uint)( (int)reg_dst + (int)reg_src );
338 33 : FD_VM_INTERP_INSTR_END;
339 :
340 39 : FD_VM_INTERP_INSTR_BEGIN(0x0cdepr) /* FD_SBPF_OP_ADD_REG deprecated SIMD-0174 */
341 39 : reg[ dst ] = (ulong)(long)( (int)reg_dst + (int)reg_src );
342 39 : FD_VM_INTERP_INSTR_END;
343 :
344 14268 : FD_VM_INTERP_INSTR_BEGIN(0x0f) /* FD_SBPF_OP_ADD64_REG */
345 14268 : reg[ dst ] = reg_dst + reg_src;
346 14268 : FD_VM_INTERP_INSTR_END;
347 :
348 : /* 0x10 - 0x1f ******************************************************/
349 :
350 36 : FD_VM_INTERP_INSTR_BEGIN(0x14) /* FD_SBPF_OP_SUB_IMM */
351 36 : reg[ dst ] = (ulong)(uint)( (int)imm - (int)reg_dst );
352 36 : FD_VM_INTERP_INSTR_END;
353 :
354 39 : FD_VM_INTERP_INSTR_BEGIN(0x14depr) /* FD_SBPF_OP_SUB_IMM deprecated SIMD-0174 */
355 39 : reg[ dst ] = (ulong)(long)( (int)reg_dst - (int)imm );
356 39 : FD_VM_INTERP_INSTR_END;
357 :
358 13194 : FD_VM_INTERP_BRANCH_BEGIN(0x15) /* FD_SBPF_OP_JEQ_IMM */
359 13182 : pc += fd_ulong_if( reg_dst==(ulong)(long)(int)imm, offset, 0UL );
360 13182 : FD_VM_INTERP_BRANCH_END;
361 :
362 33 : FD_VM_INTERP_INSTR_BEGIN(0x17) /* FD_SBPF_OP_SUB64_IMM */
363 33 : reg[ dst ] = (ulong)(long)(int)imm - reg_dst;
364 33 : FD_VM_INTERP_INSTR_END;
365 :
366 36 : FD_VM_INTERP_INSTR_BEGIN(0x17depr) /* FD_SBPF_OP_SUB64_IMM deprecated SIMD-0174 */
367 36 : reg[ dst ] = reg_dst - (ulong)(long)(int)imm;
368 36 : FD_VM_INTERP_INSTR_END;
369 :
370 24459 : FD_VM_INTERP_INSTR_BEGIN(0x18) /* FD_SBPF_OP_LDQ */
371 24459 : pc++;
372 24459 : ic_correction++;
373 : /* No need to check pc because it's already checked during validation.
374 : if( FD_UNLIKELY( pc>=text_cnt ) ) goto sigsplit; // Note: untaken branches don't consume BTB */
375 24459 : reg[ dst ] = (ulong)((ulong)imm | ((ulong)fd_vm_instr_imm( text[ pc ] ) << 32));
376 24459 : FD_VM_INTERP_INSTR_END;
377 :
378 36 : FD_VM_INTERP_INSTR_BEGIN(0x1c) /* FD_SBPF_OP_SUB_REG */
379 36 : reg[ dst ] = (ulong)(uint)( (int)reg_dst - (int)reg_src );
380 36 : FD_VM_INTERP_INSTR_END;
381 :
382 39 : FD_VM_INTERP_INSTR_BEGIN(0x1cdepr) /* FD_SBPF_OP_SUB_REG deprecated SIMD-0174 */
383 39 : reg[ dst ] = (ulong)(long)( (int)reg_dst - (int)reg_src );
384 39 : FD_VM_INTERP_INSTR_END;
385 :
386 1998 : FD_VM_INTERP_BRANCH_BEGIN(0x1d) /* FD_SBPF_OP_JEQ_REG */
387 1992 : pc += fd_ulong_if( reg_dst==reg_src, offset, 0UL );
388 1992 : FD_VM_INTERP_BRANCH_END;
389 :
390 41193 : FD_VM_INTERP_INSTR_BEGIN(0x1f) /* FD_SBPF_OP_SUB64_REG */
391 41193 : reg[ dst ] = reg_dst - reg_src;
392 41193 : FD_VM_INTERP_INSTR_END;
393 :
394 : /* 0x20 - 0x2f ******************************************************/
395 :
396 42 : FD_VM_INTERP_INSTR_BEGIN(0x24) /* FD_SBPF_OP_MUL_IMM */
397 42 : reg[ dst ] = (ulong)(long)( (int)reg_dst * (int)imm );
398 42 : FD_VM_INTERP_INSTR_END;
399 :
400 3876 : FD_VM_INTERP_BRANCH_BEGIN(0x25) /* FD_SBPF_OP_JGT_IMM */
401 3846 : pc += fd_ulong_if( reg_dst>(ulong)(long)(int)imm, offset, 0UL );
402 3846 : FD_VM_INTERP_BRANCH_END;
403 :
404 9 : FD_VM_INTERP_INSTR_BEGIN(0x27) { /* FD_SBPF_OP_STB */
405 9 : ulong vaddr = reg_dst + offset;
406 9 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uchar), region_haddr, region_st_sz, 1, 0UL );
407 9 : if( FD_UNLIKELY( !haddr ) ) {
408 6 : vm->segv_vaddr = vaddr;
409 6 : vm->segv_access_type = FD_VM_ACCESS_TYPE_ST;
410 6 : vm->segv_access_len = 1UL;
411 6 : goto sigsegv;
412 6 : } /* Note: untaken branches don't consume BTB */
413 3 : fd_vm_mem_st_1( haddr, (uchar)imm );
414 3 : }
415 3 : FD_VM_INTERP_INSTR_END;
416 :
417 13158 : FD_VM_INTERP_INSTR_BEGIN(0x2c) { /* FD_SBPF_OP_LDXB */
418 13158 : ulong vaddr = reg_src + offset;
419 13158 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uchar), region_haddr, region_ld_sz, 0, 0UL );
420 13158 : if( FD_UNLIKELY( !haddr ) ) {
421 24 : vm->segv_vaddr = vaddr;
422 24 : vm->segv_access_type = FD_VM_ACCESS_TYPE_LD;
423 24 : vm->segv_access_len = 1UL;
424 24 : goto sigsegv;
425 24 : } /* Note: untaken branches don't consume BTB */
426 13134 : reg[ dst ] = fd_vm_mem_ld_1( haddr );
427 13134 : }
428 13134 : FD_VM_INTERP_INSTR_END;
429 :
430 4587 : FD_VM_INTERP_BRANCH_BEGIN(0x2d) /* FD_SBPF_OP_JGT_REG */
431 4557 : pc += fd_ulong_if( reg_dst>reg_src, offset, 0UL );
432 4557 : FD_VM_INTERP_BRANCH_END;
433 :
434 14409 : FD_VM_INTERP_INSTR_BEGIN(0x2f) { /* FD_SBPF_OP_STXB */
435 14409 : ulong vaddr = reg_dst + offset;
436 14409 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uchar), region_haddr, region_st_sz, 1, 0UL );
437 14409 : if( FD_UNLIKELY( !haddr ) ) {
438 30 : vm->segv_vaddr = vaddr;
439 30 : vm->segv_access_type = FD_VM_ACCESS_TYPE_ST;
440 30 : vm->segv_access_len = 1UL;
441 30 : goto sigsegv;
442 30 : } /* Note: untaken branches don't consume BTB */ /* FIXME: sigrdonly */
443 14379 : fd_vm_mem_st_1( haddr, (uchar)reg_src );
444 14379 : }
445 14379 : FD_VM_INTERP_INSTR_END;
446 :
447 11022 : FD_VM_INTERP_INSTR_BEGIN(0x27depr) /* FD_SBPF_OP_MUL64_IMM */
448 11022 : reg[ dst ] = (ulong)( (long)reg_dst * (long)(int)imm );
449 11022 : FD_VM_INTERP_INSTR_END;
450 :
451 39 : FD_VM_INTERP_INSTR_BEGIN(0x2cdepr) /* FD_SBPF_OP_MUL_REG */
452 39 : reg[ dst ] = (ulong)(long)( (int)reg_dst * (int)reg_src );
453 39 : FD_VM_INTERP_INSTR_END;
454 :
455 30093 : FD_VM_INTERP_INSTR_BEGIN(0x2fdepr) /* FD_SBPF_OP_MUL64_REG */
456 30093 : reg[ dst ] = reg_dst * reg_src;
457 30093 : FD_VM_INTERP_INSTR_END;
458 :
459 : /* 0x30 - 0x3f ******************************************************/
460 :
461 42 : FD_VM_INTERP_INSTR_BEGIN(0x34) /* FD_SBPF_OP_DIV_IMM */
462 42 : /* FIXME: convert to a multiply at validation time (usually probably
463 42 : not worth it) */
464 42 : reg[ dst ] = (ulong)((uint)reg_dst / imm);
465 42 : FD_VM_INTERP_INSTR_END;
466 :
467 6123 : FD_VM_INTERP_BRANCH_BEGIN(0x35) /* FD_SBPF_OP_JGE_IMM */
468 6063 : pc += fd_ulong_if( reg_dst>=(ulong)(long)(int)imm, offset, 0UL );
469 6063 : FD_VM_INTERP_BRANCH_END;
470 :
471 3 : FD_VM_INTERP_INSTR_BEGIN(0x36) /* FD_SBPF_OP_UHMUL64_IMM */
472 3 : reg[ dst ] = (ulong)(( (uint128)reg_dst * (uint128)(ulong)imm ) >> 64 );
473 3 : FD_VM_INTERP_INSTR_END;
474 :
475 9 : FD_VM_INTERP_INSTR_BEGIN(0x37) { /* FD_SBPF_OP_STH */
476 9 : ulong vaddr = reg_dst + offset;
477 9 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ushort), region_haddr, region_st_sz, 1, 0UL );
478 9 : int sigsegv = !haddr;
479 9 : if( FD_UNLIKELY( sigsegv ) ) {
480 6 : vm->segv_vaddr = vaddr;
481 6 : vm->segv_access_type = FD_VM_ACCESS_TYPE_ST;
482 6 : vm->segv_access_len = 2UL;
483 6 : goto sigsegv;
484 6 : } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */
485 3 : fd_vm_mem_st_2( haddr, (ushort)imm );
486 3 : }
487 3 : FD_VM_INTERP_INSTR_END;
488 :
489 96 : FD_VM_INTERP_INSTR_BEGIN(0x3c) { /* FD_SBPF_OP_LDXH */
490 96 : ulong vaddr = reg_src + offset;
491 96 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ushort), region_haddr, region_ld_sz, 0, 0UL );
492 96 : int sigsegv = !haddr;
493 96 : if( FD_UNLIKELY( sigsegv ) ) {
494 36 : vm->segv_vaddr = vaddr;
495 36 : vm->segv_access_type = FD_VM_ACCESS_TYPE_LD;
496 36 : vm->segv_access_len = 2UL;
497 36 : goto sigsegv; /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */
498 36 : }
499 60 : reg[ dst ] = fd_vm_mem_ld_2( haddr );
500 60 : }
501 60 : FD_VM_INTERP_INSTR_END;
502 :
503 36747 : FD_VM_INTERP_BRANCH_BEGIN(0x3d) /* FD_SBPF_OP_JGE_REG */
504 36693 : pc += fd_ulong_if( reg_dst>=reg_src, offset, 0UL );
505 36693 : FD_VM_INTERP_BRANCH_END;
506 :
507 9 : FD_VM_INTERP_INSTR_BEGIN(0x3f) { /* FD_SBPF_OP_STXH */
508 9 : ulong vaddr = reg_dst + offset;
509 9 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ushort), region_haddr, region_st_sz, 1, 0UL );
510 9 : int sigsegv = !haddr;
511 9 : if( FD_UNLIKELY( sigsegv ) ) {
512 6 : vm->segv_vaddr = vaddr;
513 6 : vm->segv_access_type = FD_VM_ACCESS_TYPE_ST;
514 6 : vm->segv_access_len = 2UL;
515 6 : goto sigsegv;
516 6 : } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */
517 3 : fd_vm_mem_st_2( haddr, (ushort)reg_src );
518 3 : }
519 3 : FD_VM_INTERP_INSTR_END;
520 :
521 3 : FD_VM_INTERP_INSTR_BEGIN(0x3e) /* FD_SBPF_OP_UHMUL64_REG */
522 3 : reg[ dst ] = (ulong)(( (uint128)reg_dst * (uint128)reg_src ) >> 64 );
523 3 : FD_VM_INTERP_INSTR_END;
524 :
525 10965 : FD_VM_INTERP_INSTR_BEGIN(0x37depr) /* FD_SBPF_OP_DIV64_IMM */
526 10965 : reg[ dst ] = reg_dst / (ulong)(long)(int)imm;
527 10965 : FD_VM_INTERP_INSTR_END;
528 :
529 57 : FD_VM_INTERP_INSTR_BEGIN(0x3cdepr) /* FD_SBPF_OP_DIV_REG */
530 57 : if( FD_UNLIKELY( !(uint)reg_src ) ) goto sigfpe;
531 42 : reg[ dst ] = (ulong)((uint)reg_dst / (uint)reg_src);
532 42 : FD_VM_INTERP_INSTR_END;
533 :
534 30072 : FD_VM_INTERP_INSTR_BEGIN(0x3fdepr) /* FD_SBPF_OP_DIV64_REG */
535 30072 : if( FD_UNLIKELY( !reg_src ) ) goto sigfpe;
536 30060 : reg[ dst ] = reg_dst / reg_src;
537 30060 : FD_VM_INTERP_INSTR_END;
538 :
539 : /* 0x40 - 0x4f ******************************************************/
540 :
541 51 : FD_VM_INTERP_INSTR_BEGIN(0x44) /* FD_SBPF_OP_OR_IMM */
542 51 : reg[ dst ] = (ulong)( (uint)reg_dst | imm );
543 51 : FD_VM_INTERP_INSTR_END;
544 :
545 1266 : FD_VM_INTERP_BRANCH_BEGIN(0x45) /* FD_SBPF_OP_JSET_IMM */
546 1254 : pc += fd_ulong_if( !!(reg_dst & (ulong)(long)(int)imm), offset, 0UL );
547 1254 : FD_VM_INTERP_BRANCH_END;
548 :
549 39 : FD_VM_INTERP_INSTR_BEGIN(0x46) /* FD_SBPF_OP_UDIV32_IMM */
550 39 : reg[ dst ] = (ulong)( (uint)reg_dst / (uint)imm );
551 39 : FD_VM_INTERP_INSTR_END;
552 :
553 51 : FD_VM_INTERP_INSTR_BEGIN(0x47) /* FD_SBPF_OP_OR64_IMM */
554 51 : reg[ dst ] = reg_dst | (ulong)(long)(int)imm;
555 51 : FD_VM_INTERP_INSTR_END;
556 :
557 57 : FD_VM_INTERP_INSTR_BEGIN(0x4c) /* FD_SBPF_OP_OR_REG */
558 57 : reg[ dst ] = (ulong)(uint)( reg_dst | reg_src );
559 57 : FD_VM_INTERP_INSTR_END;
560 :
561 663 : FD_VM_INTERP_BRANCH_BEGIN(0x4d) /* FD_SBPF_OP_JSET_REG */
562 657 : pc += fd_ulong_if( !!(reg_dst & reg_src), offset, 0UL );
563 657 : FD_VM_INTERP_BRANCH_END;
564 :
565 48 : FD_VM_INTERP_INSTR_BEGIN(0x4e) /* FD_SBPF_OP_UDIV32_REG */
566 48 : if( FD_UNLIKELY( !(uint)reg_src ) ) goto sigfpe;
567 36 : reg[ dst ] = (ulong)( (uint)reg_dst / (uint)reg_src );
568 36 : FD_VM_INTERP_INSTR_END;
569 :
570 102 : FD_VM_INTERP_INSTR_BEGIN(0x4f) /* FD_SBPF_OP_OR64_REG */
571 102 : reg[ dst ] = reg_dst | reg_src;
572 102 : FD_VM_INTERP_INSTR_END;
573 :
574 : /* 0x50 - 0x5f ******************************************************/
575 :
576 54 : FD_VM_INTERP_INSTR_BEGIN(0x54) /* FD_SBPF_OP_AND_IMM */
577 54 : reg[ dst ] = (ulong)( (uint)reg_dst & imm );
578 54 : FD_VM_INTERP_INSTR_END;
579 :
580 31554 : FD_VM_INTERP_BRANCH_BEGIN(0x55) /* FD_SBPF_OP_JNE_IMM */
581 31548 : pc += fd_ulong_if( reg_dst!=(ulong)(long)(int)imm, offset, 0UL );
582 31548 : FD_VM_INTERP_BRANCH_END;
583 :
584 39 : FD_VM_INTERP_INSTR_BEGIN(0x56) /* FD_SBPF_OP_UDIV64_IMM */
585 39 : reg[ dst ] = reg_dst / (ulong)imm;
586 39 : FD_VM_INTERP_INSTR_END;
587 :
588 228 : FD_VM_INTERP_INSTR_BEGIN(0x57) /* FD_SBPF_OP_AND64_IMM */
589 228 : reg[ dst ] = reg_dst & (ulong)(long)(int)imm;
590 228 : FD_VM_INTERP_INSTR_END;
591 :
592 60 : FD_VM_INTERP_INSTR_BEGIN(0x5c) /* FD_SBPF_OP_AND_REG */
593 60 : reg[ dst ] = (ulong)(uint)( reg_dst & reg_src );
594 60 : FD_VM_INTERP_INSTR_END;
595 :
596 657 : FD_VM_INTERP_BRANCH_BEGIN(0x5d) /* FD_SBPF_OP_JNE_REG */
597 651 : pc += fd_ulong_if( reg_dst!=reg_src, offset, 0UL );
598 651 : FD_VM_INTERP_BRANCH_END;
599 :
600 45 : FD_VM_INTERP_INSTR_BEGIN(0x5e) /* FD_SBPF_OP_UDIV64_REG */
601 45 : if( FD_UNLIKELY( !reg_src ) ) goto sigfpe;
602 36 : reg[ dst ] = reg_dst / reg_src;
603 36 : FD_VM_INTERP_INSTR_END;
604 :
605 138 : FD_VM_INTERP_INSTR_BEGIN(0x5f) /* FD_SBPF_OP_AND64_REG */
606 138 : reg[ dst ] = reg_dst & reg_src;
607 138 : FD_VM_INTERP_INSTR_END;
608 :
609 : /* 0x60 - 0x6f ******************************************************/
610 :
611 453 : FD_VM_INTERP_INSTR_BEGIN(0x64) /* FD_SBPF_OP_LSH_IMM */
612 453 : /* https://github.com/solana-labs/rbpf/blob/8d36530b7071060e2837ebb26f25590db6816048/src/interpreter.rs#L291 */
613 453 : reg[ dst ] = (ulong)( FD_RUST_UINT_WRAPPING_SHL( (uint)reg_dst, (uint)imm ) );
614 453 : FD_VM_INTERP_INSTR_END;
615 :
616 3717 : FD_VM_INTERP_BRANCH_BEGIN(0x65) /* FD_SBPF_OP_JSGT_IMM */
617 3681 : pc += fd_ulong_if( (long)reg_dst>(long)(int)imm, offset, 0UL );
618 3681 : FD_VM_INTERP_BRANCH_END;
619 :
620 39 : FD_VM_INTERP_INSTR_BEGIN(0x66) /* FD_SBPF_OP_UREM32_IMM */
621 39 : reg[ dst ] = (ulong)( (uint)reg_dst % (uint)imm );
622 39 : FD_VM_INTERP_INSTR_END;
623 :
624 10995 : FD_VM_INTERP_INSTR_BEGIN(0x67) /* FD_SBPF_OP_LSH64_IMM */
625 10995 : /* https://github.com/solana-labs/rbpf/blob/8d36530b7071060e2837ebb26f25590db6816048/src/interpreter.rs#L376 */
626 10995 : reg[ dst ] = FD_RUST_ULONG_WRAPPING_SHL( reg_dst, imm );
627 10995 : FD_VM_INTERP_INSTR_END;
628 :
629 447 : FD_VM_INTERP_INSTR_BEGIN(0x6c) /* FD_SBPF_OP_LSH_REG */
630 447 : /* https://github.com/solana-labs/rbpf/blob/8d36530b7071060e2837ebb26f25590db6816048/src/interpreter.rs#L292 */
631 447 : reg[ dst ] = (ulong)( FD_RUST_UINT_WRAPPING_SHL( (uint)reg_dst, reg_src ) );
632 447 : FD_VM_INTERP_INSTR_END;
633 :
634 3573 : FD_VM_INTERP_BRANCH_BEGIN(0x6d) /* FD_SBPF_OP_JSGT_REG */
635 3543 : pc += fd_ulong_if( (long)reg_dst>(long)reg_src, offset, 0UL );
636 3543 : FD_VM_INTERP_BRANCH_END;
637 :
638 48 : FD_VM_INTERP_INSTR_BEGIN(0x6e) /* FD_SBPF_OP_UREM32_REG */
639 48 : if( FD_UNLIKELY( !(uint)reg_src ) ) goto sigfpe;
640 36 : reg[ dst ] = (ulong)( (uint)reg_dst % (uint)reg_src );
641 36 : FD_VM_INTERP_INSTR_END;
642 :
643 9 : FD_VM_INTERP_INSTR_BEGIN(0x6f) /* FD_SBPF_OP_LSH64_REG */
644 9 : /* https://github.com/solana-labs/rbpf/blob/8d36530b7071060e2837ebb26f25590db6816048/src/interpreter.rs#L377 */
645 9 : reg[ dst ] = FD_RUST_ULONG_WRAPPING_SHL( reg_dst, reg_src );
646 9 : FD_VM_INTERP_INSTR_END;
647 :
648 : /* 0x70 - 0x7f ******************************************************/
649 :
650 9 : FD_VM_INTERP_INSTR_BEGIN(0x74) /* FD_SBPF_OP_RSH_IMM */
651 9 : /* https://github.com/solana-labs/rbpf/blob/8d36530b7071060e2837ebb26f25590db6816048/src/interpreter.rs#L293 */
652 9 : reg[ dst ] = (ulong)( FD_RUST_UINT_WRAPPING_SHR( (uint)reg_dst, imm ) );
653 9 : FD_VM_INTERP_INSTR_END;
654 :
655 6714 : FD_VM_INTERP_BRANCH_BEGIN(0x75) /* FD_SBPF_OP_JSGE_IMM */
656 6648 : pc += fd_ulong_if( (long)reg_dst>=(long)(int)imm, offset, 0UL );
657 6648 : FD_VM_INTERP_BRANCH_END;
658 :
659 39 : FD_VM_INTERP_INSTR_BEGIN(0x76) /* FD_SBPF_OP_UREM64_IMM */
660 39 : reg[ dst ] = reg_dst % (ulong)imm;
661 39 : FD_VM_INTERP_INSTR_END;
662 :
663 117 : FD_VM_INTERP_INSTR_BEGIN(0x77) /* FD_SBPF_OP_RSH64_IMM */
664 117 : /* https://github.com/solana-labs/rbpf/blob/8d36530b7071060e2837ebb26f25590db6816048/src/interpreter.rs#L378 */
665 117 : reg[ dst ] = FD_RUST_ULONG_WRAPPING_SHR( reg_dst, imm );
666 117 : FD_VM_INTERP_INSTR_END;
667 :
668 9 : FD_VM_INTERP_INSTR_BEGIN(0x7c) /* FD_SBPF_OP_RSH_REG */
669 9 : /* https://github.com/solana-labs/rbpf/blob/8d36530b7071060e2837ebb26f25590db6816048/src/interpreter.rs#L294 */
670 9 : reg[ dst ] = (ulong)( FD_RUST_UINT_WRAPPING_SHR( (uint)reg_dst, (uint)reg_src ) );
671 9 : FD_VM_INTERP_INSTR_END;
672 :
673 5568 : FD_VM_INTERP_BRANCH_BEGIN(0x7d) /* FD_SBPF_OP_JSGE_REG */
674 5514 : pc += fd_ulong_if( (long)reg_dst>=(long)reg_src, offset, 0UL );
675 5514 : FD_VM_INTERP_BRANCH_END;
676 :
677 45 : FD_VM_INTERP_INSTR_BEGIN(0x7e) /* FD_SBPF_OP_UREM64_REG */
678 45 : if( FD_UNLIKELY( !reg_src ) ) goto sigfpe;
679 36 : reg[ dst ] = reg_dst % reg_src;
680 36 : FD_VM_INTERP_INSTR_END;
681 :
682 9 : FD_VM_INTERP_INSTR_BEGIN(0x7f) /* FD_SBPF_OP_RSH64_REG */
683 9 : /* https://github.com/solana-labs/rbpf/blob/8d36530b7071060e2837ebb26f25590db6816048/src/interpreter.rs#L379 */
684 9 : reg[ dst ] = FD_RUST_ULONG_WRAPPING_SHR( reg_dst, reg_src );
685 9 : FD_VM_INTERP_INSTR_END;
686 :
687 : /* 0x80-0x8f ********************************************************/
688 :
689 3 : FD_VM_INTERP_INSTR_BEGIN(0x84) /* FD_SBPF_OP_NEG */
690 3 : reg[ dst ] = (ulong)( -(uint)reg_dst );
691 3 : FD_VM_INTERP_INSTR_END;
692 :
693 369 : FD_VM_INTERP_BRANCH_BEGIN(0x85) { /* FD_SBPF_OP_CALL_IMM */
694 :
695 369 : fd_sbpf_syscalls_t const * syscall = imm!=fd_sbpf_syscalls_key_null() ? fd_sbpf_syscalls_query_const( syscalls, (ulong)imm, NULL ) : NULL;
696 369 : if( FD_UNLIKELY( !syscall ) ) { /* Optimize for the syscall case */
697 :
698 : /* Note we do the stack push before updating the pc(*). This implies
699 : that the call stack frame gets allocated _before_ checking if the
700 : call target is valid. It would be fine to switch the order
701 : though such would change the precise faulting semantics of
702 : sigtextbr and sigstack.
703 :
704 : (*)but after checking calldests, see point below. */
705 :
706 : /* Agave's order of checks
707 : (https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/interpreter.rs#L565-L572):
708 : 1. Lookup imm hash in FunctionRegistry (calldests_test is our equivalent)
709 : 2. Push stack frame
710 : 3. Check PC
711 : 4. Update PC
712 :
713 : Following this precisely is impossible as our PC check also
714 : serves as a bounds check for the calldests_test call. So we
715 : have to perform step 3 before step 1. The following
716 : is a best-effort implementation that should match the VM state
717 : in all ways except error code. */
718 :
719 : /* Special case to handle entrypoint.
720 : ebpf::hash_symbol_name(b"entrypoint") = 0x71e3cf81, and
721 : fd_pchash_inverse( 0x71e3cf81U ) = 0xb00c380U */
722 336 : if( FD_UNLIKELY( imm==0x71e3cf81U ) ) {
723 0 : FD_VM_INTERP_STACK_PUSH;
724 0 : pc = entry_pc - 1;
725 336 : } else {
726 336 : ulong target_pc = (ulong)fd_pchash_inverse( imm );
727 336 : if( FD_UNLIKELY( target_pc>=text_cnt ) ) {
728 6 : goto sigillbr; /* different return between 0x85 and 0x8d */
729 6 : }
730 330 : if( FD_UNLIKELY( !fd_sbpf_calldests_test( calldests, target_pc ) ) ) {
731 0 : goto sigillbr;
732 0 : }
733 660 : FD_VM_INTERP_STACK_PUSH;
734 660 : pc = target_pc - 1;
735 660 : }
736 :
737 336 : } else {
738 :
739 33 : FD_VM_INTERP_SYSCALL_EXEC;
740 :
741 33 : }
742 369 : } FD_VM_INTERP_BRANCH_END;
743 :
744 : /* SIMD-0178: Static syscalls (SBPF V3+)
745 : https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/interpreter.rs#L542-L577 */
746 240 : FD_VM_INTERP_BRANCH_BEGIN(0x85_static) { /* FD_SBPF_OP_CALL_IMM (static syscalls) */
747 :
748 240 : if( src == 0 ) {
749 : /* External syscall
750 : https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/interpreter.rs#L545-L553 */
751 15 : fd_sbpf_syscalls_t const * syscall = imm!=fd_sbpf_syscalls_key_null() ? fd_sbpf_syscalls_query_const( syscalls, (ulong)imm, NULL ) : NULL;
752 15 : if( FD_UNLIKELY( !syscall ) ) goto sigillbr;
753 18 : FD_VM_INTERP_SYSCALL_EXEC;
754 225 : } else if( src == 1 ) {
755 : /* Internal call
756 : https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/interpreter.rs#L555-L563
757 : https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/program.rs#L97-L103 */
758 219 : long target_pc_l = fd_long_sat_add( (long)pc, fd_long_sat_add( (long)(int)imm, 1L ) );
759 219 : if( FD_UNLIKELY( target_pc_l<0L || (ulong)target_pc_l>=text_cnt ) ) goto sigillbr;
760 417 : FD_VM_INTERP_STACK_PUSH;
761 417 : pc = (ulong)target_pc_l - 1;
762 417 : } else {
763 : /* https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/interpreter.rs#L574-L576 */
764 6 : goto sigillbr;
765 6 : }
766 :
767 240 : } FD_VM_INTERP_BRANCH_END;
768 :
769 39 : FD_VM_INTERP_INSTR_BEGIN(0x86) /* FD_SBPF_OP_LMUL32_IMM */
770 39 : reg[ dst ] = (ulong)( (uint)reg_dst * imm );
771 39 : FD_VM_INTERP_INSTR_END;
772 :
773 9 : FD_VM_INTERP_INSTR_BEGIN(0x87) { /* FD_SBPF_OP_STW */
774 9 : ulong vaddr = reg_dst + offset;
775 9 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uint), region_haddr, region_st_sz, 1, 0UL );
776 9 : int sigsegv = !haddr;
777 9 : if( FD_UNLIKELY( sigsegv ) ) {
778 6 : vm->segv_vaddr = vaddr;
779 6 : vm->segv_access_type = FD_VM_ACCESS_TYPE_ST;
780 6 : vm->segv_access_len = 4UL;
781 6 : goto sigsegv;
782 6 : } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */
783 3 : fd_vm_mem_st_4( haddr, imm );
784 3 : } FD_VM_INTERP_INSTR_END;
785 :
786 48 : FD_VM_INTERP_INSTR_BEGIN(0x87depr) /* FD_SBPF_OP_NEG64 deprecated */
787 48 : reg[ dst ] = -reg_dst;
788 48 : FD_VM_INTERP_INSTR_END;
789 :
790 108 : FD_VM_INTERP_INSTR_BEGIN(0x8c) { /* FD_SBPF_OP_LDXW */
791 108 : ulong vaddr = reg_src + offset;
792 108 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uint), region_haddr, region_ld_sz, 0, 0UL );
793 108 : int sigsegv = !haddr;
794 108 : if( FD_UNLIKELY( sigsegv ) ) {
795 48 : vm->segv_vaddr = vaddr;
796 48 : vm->segv_access_type = FD_VM_ACCESS_TYPE_LD;
797 48 : vm->segv_access_len = 4UL;
798 48 : goto sigsegv; /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */
799 48 : }
800 60 : reg[ dst ] = fd_vm_mem_ld_4( haddr );
801 60 : }
802 60 : FD_VM_INTERP_INSTR_END;
803 :
804 105 : FD_VM_INTERP_BRANCH_BEGIN(0x8d) { /* FD_SBPF_OP_CALL_REG */
805 :
806 105 : FD_VM_INTERP_STACK_PUSH;
807 :
808 : /* https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/interpreter.rs#L528-L540 */
809 105 : ulong vaddr = fd_sbpf_callx_uses_src_reg_enabled( sbpf_version ) ? reg_src
810 105 : : fd_sbpf_callx_uses_dst_reg_enabled( sbpf_version ) ? reg[ dst ]
811 84 : : reg[ imm & 15U ];
812 :
813 : /* Notes: Agave checks region and target_pc before updating the pc.
814 : To match their state, we do the same, even though we could simply
815 : update the pc and let BRANCH_END fail.
816 : Also, Agave doesn't check alignment. */
817 :
818 105 : ulong region = vaddr >> 32;
819 : /* ulong align = vaddr & 7UL; */
820 105 : ulong target_pc = ((vaddr & FD_VM_OFFSET_MASK) - vm->text_off) / 8UL;
821 105 : if( FD_UNLIKELY( (region!=1UL) | (target_pc>=text_cnt) ) ) goto sigtextbr; /* Note: untaken branches don't consume BTB */
822 63 : pc = target_pc - 1;
823 :
824 63 : } FD_VM_INTERP_BRANCH_END;
825 :
826 33 : FD_VM_INTERP_INSTR_BEGIN(0x8e) /* FD_SBPF_OP_LMUL32_REG */
827 33 : reg[ dst ] = (ulong)( (uint)reg_dst * (uint)reg_src );
828 33 : FD_VM_INTERP_INSTR_END;
829 :
830 9 : FD_VM_INTERP_INSTR_BEGIN(0x8f) { /* FD_SBPF_OP_STXW */
831 9 : ulong vaddr = reg_dst + offset;
832 9 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(uint), region_haddr, region_st_sz, 1, 0UL );
833 9 : int sigsegv = !haddr;
834 9 : if( FD_UNLIKELY( sigsegv ) ) {
835 6 : vm->segv_vaddr = vaddr;
836 6 : vm->segv_access_type = FD_VM_ACCESS_TYPE_ST;
837 6 : vm->segv_access_len = 4UL;
838 6 : goto sigsegv;
839 6 : } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */
840 3 : fd_vm_mem_st_4( haddr, (uint)reg_src );
841 3 : }
842 3 : FD_VM_INTERP_INSTR_END;
843 :
844 : /* 0x90 - 0x9f ******************************************************/
845 :
846 42 : FD_VM_INTERP_INSTR_BEGIN(0x94) /* FD_SBPF_OP_MOD_IMM */
847 42 : reg[ dst ] = (ulong)( (uint)reg_dst % imm );
848 42 : FD_VM_INTERP_INSTR_END;
849 :
850 9201 : FD_VM_INTERP_BRANCH_BEGIN(0x95) /* FD_SBPF_OP_EXIT */
851 9192 : /* Agave JIT VM exit implementation analysis below.
852 9192 :
853 9192 : Agave references:
854 9192 : https://github.com/solana-labs/rbpf/blob/v0.8.5/src/interpreter.rs#L503-L509
855 9192 : https://github.com/solana-labs/rbpf/blob/v0.8.5/src/jit.rs#L697-L702 */
856 9192 : if( FD_UNLIKELY( !frame_cnt ) ) goto sigexit; /* Exit program */
857 411 : frame_cnt--;
858 411 : reg[6] = shadow[ frame_cnt ].r6;
859 411 : reg[7] = shadow[ frame_cnt ].r7;
860 411 : reg[8] = shadow[ frame_cnt ].r8;
861 411 : reg[9] = shadow[ frame_cnt ].r9;
862 411 : reg[10] = shadow[ frame_cnt ].r10;
863 411 : pc = shadow[ frame_cnt ].pc;
864 411 : FD_VM_INTERP_BRANCH_END;
865 :
866 39 : FD_VM_INTERP_INSTR_BEGIN(0x96) /* FD_SBPF_OP_LMUL64_IMM */
867 39 : reg[ dst ] = reg_dst * (ulong)(long)(int)imm;
868 39 : FD_VM_INTERP_INSTR_END;
869 :
870 9 : FD_VM_INTERP_INSTR_BEGIN(0x97) { /* FD_SBPF_OP_STQ */
871 9 : ulong vaddr = reg_dst + offset;
872 9 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ulong), region_haddr, region_st_sz, 1, 0UL );
873 9 : int sigsegv = !haddr;
874 9 : if( FD_UNLIKELY( sigsegv ) ) {
875 6 : vm->segv_vaddr = vaddr;
876 6 : vm->segv_access_type = FD_VM_ACCESS_TYPE_ST;
877 6 : vm->segv_access_len = 8UL;
878 6 : goto sigsegv;
879 6 : } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */
880 3 : fd_vm_mem_st_8( haddr, (ulong)(long)(int)imm );
881 3 : }
882 3 : FD_VM_INTERP_INSTR_END;
883 :
884 3564 : FD_VM_INTERP_INSTR_BEGIN(0x9c) { /* FD_SBPF_OP_LDXQ */
885 3564 : ulong vaddr = reg_src + offset;
886 3564 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ulong), region_haddr, region_ld_sz, 0, 0UL );
887 3564 : int sigsegv = !haddr;
888 3564 : if( FD_UNLIKELY( sigsegv ) ) {
889 48 : vm->segv_vaddr = vaddr;
890 48 : vm->segv_access_type = FD_VM_ACCESS_TYPE_LD;
891 48 : vm->segv_access_len = 8UL;
892 48 : goto sigsegv; /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */
893 48 : }
894 3516 : reg[ dst ] = fd_vm_mem_ld_8( haddr );
895 3516 : }
896 3516 : FD_VM_INTERP_INSTR_END;
897 :
898 57 : FD_VM_INTERP_INSTR_BEGIN(0x9e) /* FD_SBPF_OP_LMUL64_REG */
899 57 : reg[ dst ] = reg_dst * reg_src;
900 57 : FD_VM_INTERP_INSTR_END;
901 :
902 2319 : FD_VM_INTERP_INSTR_BEGIN(0x9f) { /* FD_SBPF_OP_STXQ */
903 2319 : ulong vaddr = reg_dst + offset;
904 2319 : ulong haddr = fd_vm_mem_haddr( vm, vaddr, sizeof(ulong), region_haddr, region_st_sz, 1, 0UL );
905 2319 : int sigsegv = !haddr;
906 2319 : if( FD_UNLIKELY( sigsegv ) ) {
907 6 : vm->segv_vaddr = vaddr;
908 6 : vm->segv_access_type = FD_VM_ACCESS_TYPE_ST;
909 6 : vm->segv_access_len = 8UL;
910 6 : goto sigsegv;
911 6 : } /* Note: untaken branches don't consume BTB */ /* FIXME: sigbus */
912 2313 : fd_vm_mem_st_8( haddr, reg_src );
913 2313 : }
914 2313 : FD_VM_INTERP_INSTR_END;
915 :
916 42 : FD_VM_INTERP_INSTR_BEGIN(0x97depr) /* FD_SBPF_OP_MOD64_IMM */
917 42 : reg[ dst ] = reg_dst % (ulong)(long)(int)imm;
918 42 : FD_VM_INTERP_INSTR_END;
919 :
920 57 : FD_VM_INTERP_INSTR_BEGIN(0x9cdepr) /* FD_SBPF_OP_MOD_REG */
921 57 : if( FD_UNLIKELY( !(uint)reg_src ) ) goto sigfpe;
922 42 : reg[ dst ] = (ulong)( ((uint)reg_dst % (uint)reg_src) );
923 42 : FD_VM_INTERP_INSTR_END;
924 :
925 54 : FD_VM_INTERP_INSTR_BEGIN(0x9fdepr) /* FD_SBPF_OP_MOD64_REG */
926 54 : if( FD_UNLIKELY( !reg_src ) ) goto sigfpe;
927 42 : reg[ dst ] = reg_dst % reg_src;
928 42 : FD_VM_INTERP_INSTR_END;
929 :
930 : /* 0xa0 - 0xaf ******************************************************/
931 :
932 9 : FD_VM_INTERP_INSTR_BEGIN(0xa4) /* FD_SBPF_OP_XOR_IMM */
933 9 : reg[ dst ] = (ulong)( (uint)reg_dst ^ imm );
934 9 : FD_VM_INTERP_INSTR_END;
935 :
936 3126 : FD_VM_INTERP_BRANCH_BEGIN(0xa5) /* FD_SBPF_OP_JLT_IMM */
937 3096 : pc += fd_ulong_if( reg_dst<(ulong)(long)(int)imm, offset, 0UL );
938 3096 : FD_VM_INTERP_BRANCH_END;
939 :
940 39 : FD_VM_INTERP_INSTR_BEGIN(0xa7) /* FD_SBPF_OP_XOR64_IMM */
941 39 : reg[ dst ] = reg_dst ^ (ulong)(long)(int)imm;
942 39 : FD_VM_INTERP_INSTR_END;
943 :
944 9 : FD_VM_INTERP_INSTR_BEGIN(0xac) /* FD_SBPF_OP_XOR_REG */
945 9 : reg[ dst ] = (ulong)(uint)( reg_dst ^ reg_src );
946 9 : FD_VM_INTERP_INSTR_END;
947 :
948 2517 : FD_VM_INTERP_BRANCH_BEGIN(0xad) /* FD_SBPF_OP_JLT_REG */
949 2493 : pc += fd_ulong_if( reg_dst<reg_src, offset, 0UL );
950 2493 : FD_VM_INTERP_BRANCH_END;
951 :
952 21 : FD_VM_INTERP_INSTR_BEGIN(0xaf) /* FD_SBPF_OP_XOR64_REG */
953 21 : reg[ dst ] = reg_dst ^ reg_src;
954 21 : FD_VM_INTERP_INSTR_END;
955 :
956 : /* 0xb0 - 0xbf ******************************************************/
957 :
958 330 : FD_VM_INTERP_INSTR_BEGIN(0xb4) /* FD_SBPF_OP_MOV_IMM */
959 330 : reg[ dst ] = (ulong)imm;
960 330 : FD_VM_INTERP_INSTR_END;
961 :
962 6126 : FD_VM_INTERP_BRANCH_BEGIN(0xb5) /* FD_SBPF_OP_JLE_IMM */
963 6066 : pc += fd_ulong_if( reg_dst<=(ulong)(long)(int)imm, offset, 0UL );
964 6066 : FD_VM_INTERP_BRANCH_END;
965 :
966 3 : FD_VM_INTERP_INSTR_BEGIN(0xb6) /* FD_SBPF_OP_SHMUL64_IMM */
967 3 : reg[ dst ] = (ulong)(( (int128)(long)reg_dst * (int128)(long)(int)imm ) >> 64 );
968 3 : FD_VM_INTERP_INSTR_END;
969 :
970 126420 : FD_VM_INTERP_INSTR_BEGIN(0xb7) /* FD_SBPF_OP_MOV64_IMM */
971 126420 : reg[ dst ] = (ulong)(long)(int)imm;
972 126420 : FD_VM_INTERP_INSTR_END;
973 :
974 3 : FD_VM_INTERP_INSTR_BEGIN(0xbc) /* FD_SBPF_OP_MOV_REG */
975 3 : reg[ dst ] = (ulong)(long)(int)reg_src;
976 3 : FD_VM_INTERP_INSTR_END;
977 :
978 15 : FD_VM_INTERP_INSTR_BEGIN(0xbcdepr) /* FD_SBPF_OP_MOV_REG deprecated SIMD-1074 */
979 15 : reg[ dst ] = (ulong)(uint)reg_src;
980 15 : FD_VM_INTERP_INSTR_END;
981 :
982 4917 : FD_VM_INTERP_BRANCH_BEGIN(0xbd) /* FD_SBPF_OP_JLE_REG */
983 4869 : pc += fd_ulong_if( reg_dst<=reg_src, offset, 0UL );
984 4869 : FD_VM_INTERP_BRANCH_END;
985 :
986 3 : FD_VM_INTERP_INSTR_BEGIN(0xbe) /* FD_SBPF_OP_SHMUL64_REG */
987 3 : reg[ dst ] = (ulong)(( (int128)(long)reg_dst * (int128)(long)reg_src ) >> 64 );
988 3 : FD_VM_INTERP_INSTR_END;
989 :
990 87984 : FD_VM_INTERP_INSTR_BEGIN(0xbf) /* FD_SBPF_OP_MOV64_REG */
991 87984 : reg[ dst ] = reg_src;
992 87984 : FD_VM_INTERP_INSTR_END;
993 :
994 : /* 0xc0 - 0xcf ******************************************************/
995 :
996 147 : FD_VM_INTERP_INSTR_BEGIN(0xc4) /* FD_SBPF_OP_ARSH_IMM */
997 147 : reg[ dst ] = (ulong)(uint)( FD_RUST_INT_WRAPPING_SHR( (int)reg_dst, imm ) );
998 147 : FD_VM_INTERP_INSTR_END;
999 :
1000 3102 : FD_VM_INTERP_BRANCH_BEGIN(0xc5) /* FD_SBPF_OP_JSLT_IMM */ /* FIXME: CHECK IMM SIGN EXTENSION */
1001 3072 : pc += fd_ulong_if( (long)reg_dst<(long)(int)imm, offset, 0UL );
1002 3072 : FD_VM_INTERP_BRANCH_END;
1003 :
1004 45 : FD_VM_INTERP_INSTR_BEGIN(0xc6) /* FD_SBPF_OP_SDIV32_IMM */
1005 45 : if( FD_UNLIKELY( ((int)reg_dst==INT_MIN) & ((int)imm==-1) ) ) goto sigfpeof;
1006 39 : reg[ dst ] = (ulong)(uint)( (int)reg_dst / (int)imm );
1007 39 : FD_VM_INTERP_INSTR_END;
1008 :
1009 330 : FD_VM_INTERP_INSTR_BEGIN(0xc7) /* FD_SBPF_OP_ARSH64_IMM */
1010 330 : reg[ dst ] = (ulong)( FD_RUST_LONG_WRAPPING_SHR( (long)reg_dst, imm ) );
1011 330 : FD_VM_INTERP_INSTR_END;
1012 :
1013 96 : FD_VM_INTERP_INSTR_BEGIN(0xcc) /* FD_SBPF_OP_ARSH_REG */
1014 96 : reg[ dst ] = (ulong)(uint)( FD_RUST_INT_WRAPPING_SHR( (int)reg_dst, (uint)reg_src ) );
1015 96 : FD_VM_INTERP_INSTR_END;
1016 :
1017 3108 : FD_VM_INTERP_BRANCH_BEGIN(0xcd) /* FD_SBPF_OP_JSLT_REG */
1018 3078 : pc += fd_ulong_if( (long)reg_dst<(long)reg_src, offset, 0UL );
1019 3078 : FD_VM_INTERP_BRANCH_END;
1020 :
1021 54 : FD_VM_INTERP_INSTR_BEGIN(0xce) /* FD_SBPF_OP_SDIV32_REG */
1022 54 : if( FD_UNLIKELY( !(int)reg_src ) ) goto sigfpe;
1023 42 : if( FD_UNLIKELY( ((int)reg_dst==INT_MIN) & ((int)reg_src==-1) ) ) goto sigfpeof;
1024 36 : reg[ dst ] = (ulong)(uint)( (int)reg_dst / (int)reg_src );
1025 36 : FD_VM_INTERP_INSTR_END;
1026 :
1027 93 : FD_VM_INTERP_INSTR_BEGIN(0xcf) /* FD_SBPF_OP_ARSH64_REG */
1028 93 : reg[ dst ] = (ulong)( FD_RUST_LONG_WRAPPING_SHR( (long)reg_dst, reg_src ) );
1029 93 : FD_VM_INTERP_INSTR_END;
1030 :
1031 : /* 0xd0 - 0xdf ******************************************************/
1032 :
1033 21 : FD_VM_INTERP_INSTR_BEGIN(0xd4) /* FD_SBPF_OP_END_LE */
1034 21 : switch( imm ) {
1035 9 : case 16U: reg[ dst ] = (ushort)reg_dst; break;
1036 3 : case 32U: reg[ dst ] = (uint) reg_dst; break;
1037 3 : case 64U: break;
1038 6 : default: goto siginv;
1039 21 : }
1040 15 : FD_VM_INTERP_INSTR_END;
1041 :
1042 2460 : FD_VM_INTERP_BRANCH_BEGIN(0xd5) /* FD_SBPF_OP_JSLE_IMM */
1043 2436 : pc += fd_ulong_if( (long)reg_dst<=(long)(int)imm, offset, 0UL );
1044 2436 : FD_VM_INTERP_BRANCH_END;
1045 :
1046 42 : FD_VM_INTERP_INSTR_BEGIN(0xd6) /* FD_SBPF_OP_SDIV64_IMM */
1047 42 : if( FD_UNLIKELY( ((long)reg_dst==LONG_MIN) & ((long)(int)imm==-1L) ) ) goto sigfpeof;
1048 39 : reg[ dst ] = (ulong)( (long)reg_dst / (long)(int)imm );
1049 39 : FD_VM_INTERP_INSTR_END;
1050 :
1051 72 : FD_VM_INTERP_INSTR_BEGIN(0xdc) /* FD_SBPF_OP_END_BE */
1052 72 : switch( imm ) {
1053 39 : case 16U: reg[ dst ] = (ulong)fd_ushort_bswap( (ushort)reg_dst ); break;
1054 12 : case 32U: reg[ dst ] = (ulong)fd_uint_bswap ( (uint) reg_dst ); break;
1055 9 : case 64U: reg[ dst ] = fd_ulong_bswap ( (ulong) reg_dst ); break;
1056 12 : default: goto siginv;
1057 72 : }
1058 60 : FD_VM_INTERP_INSTR_END;
1059 :
1060 1854 : FD_VM_INTERP_BRANCH_BEGIN(0xdd) /* FD_SBPF_OP_JSLE_REG */
1061 1836 : pc += fd_ulong_if( (long)reg_dst<=(long)reg_src, offset, 0UL );
1062 1836 : FD_VM_INTERP_BRANCH_END;
1063 :
1064 48 : FD_VM_INTERP_INSTR_BEGIN(0xde) /* FD_SBPF_OP_SDIV64_REG */
1065 48 : if( FD_UNLIKELY( !reg_src ) ) goto sigfpe;
1066 39 : if( FD_UNLIKELY( ((long)reg_dst==LONG_MIN) & ((long)reg_src==-1L) ) ) goto sigfpeof;
1067 36 : reg[ dst ] = (ulong)( (long)reg_dst / (long)reg_src );
1068 36 : FD_VM_INTERP_INSTR_END;
1069 :
1070 : /* 0xe0 - 0xef ******************************************************/
1071 :
1072 45 : FD_VM_INTERP_INSTR_BEGIN(0xe6) /* FD_SBPF_OP_SREM32_IMM */
1073 45 : if( FD_UNLIKELY( ((int)reg_dst==INT_MIN) & ((int)imm==-1) ) ) goto sigfpeof;
1074 39 : reg[ dst ] = (ulong)(uint)( (int)reg_dst % (int)imm );
1075 39 : FD_VM_INTERP_INSTR_END;
1076 :
1077 54 : FD_VM_INTERP_INSTR_BEGIN(0xee) /* FD_SBPF_OP_SREM32_REG */
1078 54 : if( FD_UNLIKELY( !(int)reg_src ) ) goto sigfpe;
1079 42 : if( FD_UNLIKELY( ((int)reg_dst==INT_MIN) & ((int)reg_src==-1) ) ) goto sigfpeof;
1080 36 : reg[ dst ] = (ulong)(uint)( (int)reg_dst % (int)reg_src );
1081 36 : FD_VM_INTERP_INSTR_END;
1082 :
1083 : /* 0xf0 - 0xff ******************************************************/
1084 :
1085 42 : FD_VM_INTERP_INSTR_BEGIN(0xf6) /* FD_SBPF_OP_SREM64_IMM */
1086 42 : if( FD_UNLIKELY( ((long)reg_dst==LONG_MIN) & ((long)(int)imm==-1L) ) ) goto sigfpeof;
1087 39 : reg[ dst ] = (ulong)( (long)reg_dst % (long)(int)imm );
1088 39 : FD_VM_INTERP_INSTR_END;
1089 :
1090 21 : FD_VM_INTERP_INSTR_BEGIN(0xf7) /* FD_SBPF_OP_HOR64 */
1091 21 : reg[ dst ] = reg_dst | (((ulong)imm) << 32);
1092 21 : FD_VM_INTERP_INSTR_END;
1093 :
1094 48 : FD_VM_INTERP_INSTR_BEGIN(0xfe) /* FD_SBPF_OP_SREM64_REG */
1095 48 : if( FD_UNLIKELY( !reg_src ) ) goto sigfpe;
1096 39 : if( FD_UNLIKELY( ((long)reg_dst==LONG_MIN) & ((long)reg_src==-1L) ) ) goto sigfpeof;
1097 36 : reg[ dst ] = (ulong)( (long)reg_dst % (long)reg_src );
1098 36 : FD_VM_INTERP_INSTR_END;
1099 :
1100 : /* SIMD-0377: JMP32
1101 : https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/interpreter.rs#L480-L501
1102 :
1103 : 0x16 - 0xde ******************************************************/
1104 :
1105 1548 : FD_VM_INTERP_BRANCH_BEGIN(0x16_jmp32) /* FD_SBPF_OP_JEQ32_IMM */
1106 1533 : pc += fd_ulong_if( (uint)reg_dst==(uint)imm, offset, 0UL );
1107 1533 : FD_VM_INTERP_BRANCH_END;
1108 :
1109 630 : FD_VM_INTERP_BRANCH_BEGIN(0x1e_jmp32) /* FD_SBPF_OP_JEQ32_REG */
1110 624 : pc += fd_ulong_if( (uint)reg_dst==(uint)reg_src, offset, 0UL );
1111 624 : FD_VM_INTERP_BRANCH_END;
1112 :
1113 942 : FD_VM_INTERP_BRANCH_BEGIN(0x26_jmp32) /* FD_SBPF_OP_JGT32_IMM */
1114 933 : pc += fd_ulong_if( (uint)reg_dst>(uint)imm, offset, 0UL );
1115 933 : FD_VM_INTERP_BRANCH_END;
1116 :
1117 321 : FD_VM_INTERP_BRANCH_BEGIN(0x2e_jmp32) /* FD_SBPF_OP_JGT32_REG */
1118 318 : pc += fd_ulong_if( (uint)reg_dst>(uint)reg_src, offset, 0UL );
1119 318 : FD_VM_INTERP_BRANCH_END;
1120 :
1121 927 : FD_VM_INTERP_BRANCH_BEGIN(0x36_jmp32) /* FD_SBPF_OP_JGE32_IMM */
1122 918 : pc += fd_ulong_if( (uint)reg_dst>=(uint)imm, offset, 0UL );
1123 918 : FD_VM_INTERP_BRANCH_END;
1124 :
1125 318 : FD_VM_INTERP_BRANCH_BEGIN(0x3e_jmp32) /* FD_SBPF_OP_JGE32_REG */
1126 315 : pc += fd_ulong_if( (uint)reg_dst>=(uint)reg_src, offset, 0UL );
1127 315 : FD_VM_INTERP_BRANCH_END;
1128 :
1129 939 : FD_VM_INTERP_BRANCH_BEGIN(0x46_jmp32) /* FD_SBPF_OP_JSET32_IMM */
1130 930 : pc += fd_ulong_if( !!((uint)reg_dst & (uint)imm), offset, 0UL );
1131 930 : FD_VM_INTERP_BRANCH_END;
1132 :
1133 324 : FD_VM_INTERP_BRANCH_BEGIN(0x4e_jmp32) /* FD_SBPF_OP_JSET32_REG */
1134 321 : pc += fd_ulong_if( !!((uint)reg_dst & (uint)reg_src), offset, 0UL );
1135 321 : FD_VM_INTERP_BRANCH_END;
1136 :
1137 321 : FD_VM_INTERP_BRANCH_BEGIN(0x56_jmp32) /* FD_SBPF_OP_JNE32_IMM */
1138 318 : pc += fd_ulong_if( (uint)reg_dst!=(uint)imm, offset, 0UL );
1139 318 : FD_VM_INTERP_BRANCH_END;
1140 :
1141 324 : FD_VM_INTERP_BRANCH_BEGIN(0x5e_jmp32) /* FD_SBPF_OP_JNE32_REG */
1142 321 : pc += fd_ulong_if( (uint)reg_dst!=(uint)reg_src, offset, 0UL );
1143 321 : FD_VM_INTERP_BRANCH_END;
1144 :
1145 1248 : FD_VM_INTERP_BRANCH_BEGIN(0x66_jmp32) /* FD_SBPF_OP_JSGT32_IMM */
1146 1236 : pc += fd_ulong_if( (int)reg_dst>(int)imm, offset, 0UL );
1147 1236 : FD_VM_INTERP_BRANCH_END;
1148 :
1149 321 : FD_VM_INTERP_BRANCH_BEGIN(0x6e_jmp32) /* FD_SBPF_OP_JSGT32_REG */
1150 318 : pc += fd_ulong_if( (int)reg_dst>(int)reg_src, offset, 0UL );
1151 318 : FD_VM_INTERP_BRANCH_END;
1152 :
1153 627 : FD_VM_INTERP_BRANCH_BEGIN(0x76_jmp32) /* FD_SBPF_OP_JSGE32_IMM */
1154 621 : pc += fd_ulong_if( (int)reg_dst>=(int)imm, offset, 0UL );
1155 621 : FD_VM_INTERP_BRANCH_END;
1156 :
1157 318 : FD_VM_INTERP_BRANCH_BEGIN(0x7e_jmp32) /* FD_SBPF_OP_JSGE32_REG */
1158 315 : pc += fd_ulong_if( (int)reg_dst>=(int)reg_src, offset, 0UL );
1159 315 : FD_VM_INTERP_BRANCH_END;
1160 :
1161 627 : FD_VM_INTERP_BRANCH_BEGIN(0xa6_jmp32) /* FD_SBPF_OP_JLT32_IMM */
1162 621 : pc += fd_ulong_if( (uint)reg_dst<(uint)imm, offset, 0UL );
1163 621 : FD_VM_INTERP_BRANCH_END;
1164 :
1165 318 : FD_VM_INTERP_BRANCH_BEGIN(0xae_jmp32) /* FD_SBPF_OP_JLT32_REG */
1166 315 : pc += fd_ulong_if( (uint)reg_dst<(uint)reg_src, offset, 0UL );
1167 315 : FD_VM_INTERP_BRANCH_END;
1168 :
1169 621 : FD_VM_INTERP_BRANCH_BEGIN(0xb6_jmp32) /* FD_SBPF_OP_JLE32_IMM */
1170 615 : pc += fd_ulong_if( (uint)reg_dst<=(uint)imm, offset, 0UL );
1171 615 : FD_VM_INTERP_BRANCH_END;
1172 :
1173 318 : FD_VM_INTERP_BRANCH_BEGIN(0xbe_jmp32) /* FD_SBPF_OP_JLE32_REG */
1174 315 : pc += fd_ulong_if( (uint)reg_dst<=(uint)reg_src, offset, 0UL );
1175 315 : FD_VM_INTERP_BRANCH_END;
1176 :
1177 327 : FD_VM_INTERP_BRANCH_BEGIN(0xc6_jmp32) /* FD_SBPF_OP_JSLT32_IMM */
1178 324 : pc += fd_ulong_if( (int)reg_dst<(int)imm, offset, 0UL );
1179 324 : FD_VM_INTERP_BRANCH_END;
1180 :
1181 318 : FD_VM_INTERP_BRANCH_BEGIN(0xce_jmp32) /* FD_SBPF_OP_JSLT32_REG */
1182 315 : pc += fd_ulong_if( (int)reg_dst<(int)reg_src, offset, 0UL );
1183 315 : FD_VM_INTERP_BRANCH_END;
1184 :
1185 621 : FD_VM_INTERP_BRANCH_BEGIN(0xd6_jmp32) /* FD_SBPF_OP_JSLE32_IMM */
1186 615 : pc += fd_ulong_if( (int)reg_dst<=(int)imm, offset, 0UL );
1187 615 : FD_VM_INTERP_BRANCH_END;
1188 :
1189 318 : FD_VM_INTERP_BRANCH_BEGIN(0xde_jmp32) /* FD_SBPF_OP_JSLE32_REG */
1190 315 : pc += fd_ulong_if( (int)reg_dst<=(int)reg_src, offset, 0UL );
1191 315 : FD_VM_INTERP_BRANCH_END;
1192 :
1193 : /* FIXME: sigbus/sigrdonly are mapped to sigsegv for simplicity
1194 : currently but could be enabled if desired. */
1195 :
1196 : /* Note: sigtextbr is for sigtext errors that occur on branching
1197 : instructions (i.e., prefixed with FD_VM_INTERP_BRANCH_BEGIN).
1198 : We skip a repeat ic accumulation in FD_VM_INTERP_FAULT */
1199 :
1200 : /* FD_VM_INTERP_FAULT accumulates to ic and cu all non-faulting
1201 : instructions preceding a fault generated by a non-branching
1202 : instruction. When a non-branching instruction faults, pc is at the
1203 : instruction and the number of non-branching instructions that have
1204 : not yet been reflected in ic and cu is:
1205 :
1206 : pc - pc0 + 1 - ic_correction
1207 :
1208 : as per the accounting described above. +1 to include the faulting
1209 : instruction itself.
1210 :
1211 : Note that, for a sigtext caused by a branch instruction, pc0==pc
1212 : (from the BRANCH_END) and ic_correction==0 (from the BRANCH_BEGIN)
1213 : such that the below does not change the already current values in
1214 : ic and cu. Thus it also "does the right thing" in both the
1215 : non-branching and branching cases for sigtext. The same applies to
1216 : sigsplit. */
1217 :
1218 0 : #define FD_VM_INTERP_FAULT \
1219 1668 : ic_correction = pc - pc0 + 1UL - ic_correction; \
1220 1668 : ic += ic_correction; \
1221 1668 : if ( FD_UNLIKELY( ic_correction > cu ) ) err = FD_VM_ERR_EBPF_EXCEEDED_MAX_INSTRUCTIONS; \
1222 1668 : cu -= fd_ulong_min( ic_correction, cu )
1223 :
1224 96 : sigtext_or_sigcost:
1225 : /* If the block text limit is exceeded, sigtext_or_sigcost will be
1226 : thrown. This could either be because the pc has exceeded the text
1227 : section, or we have exhausted our CU budget. The block text limit
1228 : is used to combine both of these checks into one, primarily for
1229 : performance reasons. We then disambiguate the scenarios in this
1230 : signal handler, as hitting either of these cases is rare. */
1231 96 : if( pc<text_cnt ) goto sigcost; else goto sigtext;
1232 87 : sigtext: err = FD_VM_ERR_EBPF_EXECUTION_OVERRUN; FD_VM_INTERP_FAULT; goto interp_halt;
1233 42 : sigtextbr: err = FD_VM_ERR_EBPF_CALL_OUTSIDE_TEXT_SEGMENT; /* ic current */ /* cu current */ goto interp_halt;
1234 3 : sigstack: err = FD_VM_ERR_EBPF_CALL_DEPTH_EXCEEDED; /* ic current */ /* cu current */ goto interp_halt;
1235 1179 : sigill: err = FD_VM_ERR_EBPF_UNSUPPORTED_INSTRUCTION; FD_VM_INTERP_FAULT; goto interp_halt;
1236 27 : sigillbr: err = FD_VM_ERR_EBPF_UNSUPPORTED_INSTRUCTION; /* ic current */ /* cu current */ goto interp_halt;
1237 18 : siginv: err = FD_VM_ERR_EBPF_INVALID_INSTRUCTION; /* ic current */ /* cu current */ goto interp_halt;
1238 228 : sigsegv: err = fd_vm_generate_access_violation( vm->segv_vaddr, vm->sbpf_version ); FD_VM_INTERP_FAULT; goto interp_halt;
1239 816 : sigcost: err = FD_VM_ERR_EBPF_EXCEEDED_MAX_INSTRUCTIONS; /* ic current */ cu = 0UL; goto interp_halt;
1240 0 : sigsyscall: err = FD_VM_ERR_EBPF_SYSCALL_ERROR; /* ic current */ /* cu current */ goto interp_halt;
1241 138 : sigfpe: err = FD_VM_ERR_EBPF_DIVIDE_BY_ZERO; FD_VM_INTERP_FAULT; goto interp_halt;
1242 36 : sigfpeof: err = FD_VM_ERR_EBPF_DIVIDE_OVERFLOW; FD_VM_INTERP_FAULT; goto interp_halt;
1243 8781 : sigexit: /* err current */ /* ic current */ /* cu current */ goto interp_halt;
1244 :
1245 0 : #undef FD_VM_INTERP_FAULT
1246 :
1247 11355 : interp_halt:
1248 :
1249 : /* Pack the unpacked execution state into vm to give a precise view of
1250 : the execution when the vm halted. */
1251 :
1252 11355 : vm->pc = pc;
1253 11355 : vm->ic = ic;
1254 11355 : vm->cu = cu;
1255 11355 : vm->frame_cnt = frame_cnt;
1256 :
1257 11355 : # undef FD_VM_INTERP_STACK_PUSH
1258 :
1259 11355 : # undef FD_VM_INTERP_BRANCH_END
1260 11355 : # undef FD_VM_INTERP_BRANCH_BEGIN
1261 :
1262 11355 : # undef FD_VM_INTERP_BLOCK_TEXT_LIMIT
1263 :
1264 11355 : # undef FD_VM_INTERP_INSTR_END
1265 11355 : # undef FD_VM_INTERP_INSTR_BEGIN
1266 11355 : # undef FD_VM_INTERP_INSTR_EXEC
1267 :
1268 11355 : # if defined(__clang__)
1269 11355 : # pragma clang diagnostic pop
1270 11355 : # endif
1271 :
1272 11355 : # if defined(__GNUC__)
1273 11355 : # pragma GCC diagnostic pop
1274 11355 : # endif
1275 :
1276 : /* Agave/JIT CU model analysis (and why we are conformant!):
1277 :
1278 : The Agave JIT employs a similar strategy of accumulating instructions
1279 : in a linear run and processing them at the start of a new linear
1280 : run/branch (side note: the JIT treats the LDQ instruction as a "branch"
1281 : that jumps pc + 2).
1282 :
1283 : In what is assumed to be an act of register conservation, the JIT
1284 : uses a catch-all "instruction meter" (IM) register (REGISTER_INSTRUCTION_METER)
1285 : that represents two different interpretations of the question
1286 : "how many instructions can I execute?".
1287 :
1288 : The IM, depending on where we are in the execution, either represents:
1289 : 1. IM => The number of instructions remaining before exhausting CU
1290 : budget. This is analogous to vm->cu in our interpreter.
1291 : 2. IM' => The last pc you can execute in the current linear run before
1292 : exhausting CU budget. Mathematically, IM' = IM + pc0
1293 : where pc0, just like our definition, is the start of the linear run.
1294 :
1295 : Note: IM' can go past the actual basic block/segment. In-fact,
1296 : it typically does, and implies we can execute the full block without
1297 : exhausting CU budget (reminder that LDQ is treated as a branch).
1298 :
1299 : By default, the IM' form is used during execution. The IM form is used:
1300 : - (transiently) during the processing of a branch instruction
1301 : - in post-VM cleanup (updates EbpfVm::previous_instruction_meter).
1302 :
1303 : When a branch instruction is encountered, the JIT checks
1304 : for CU exhaustion with pc > IM', and throws an exception if so. This is valid,
1305 : because as described above, IM' is the largest PC you can reach.
1306 :
1307 : If we haven't exhausted our CU limit, it updates IM':
1308 : 1. IM = IM' - (pc + 1) # Note that IM' at this point is IM + pc0',
1309 : # where pc0' is the start of the current linear run.
1310 : 2. IM' = IM + pc0 # pc0 is the start of the new linear run (typically the target pc)
1311 :
1312 : Code (that does the above in one ALU instruction):
1313 : https://github.com/solana-labs/rbpf/blob/v0.8.5/src/jit.rs#L891
1314 :
1315 :
1316 : ### How does this relate to our interpreter?
1317 :
1318 : This process is similar to FD_VM_INTERP_BRANCH_BEGIN.
1319 : We just deal with the IM form throughout (with vm->cu and ic_correction).
1320 : If we break down step 1 from above with what we know about IM and IM',
1321 : we get the following:
1322 : 1. IM = IM' - (pc + 1)
1323 : IM = (IM + pc0') - (pc + 1)
1324 : IM = IM + (pc0' - (pc + 1))
1325 : IM = IM - ((pc + 1) - pc0')
1326 : IM = IM - ic_correction
1327 : Here, ((pc + 1) - pc0') is the number of instructions executed in the current
1328 : linear run. This is the same as our ic_correction(*) in FD_VM_INTERP_BRANCH_BEGIN.
1329 :
1330 : If we replace IM with cu, this effectively becomes the
1331 : cu -= ic_correction
1332 : line in FD_VM_INTERP_BRANCH_BEGIN.
1333 :
1334 : (*) Note: ic_correction (also) takes two forms. It is either the instruction
1335 : accumulator or the number of instructions executed in the current linear run.
1336 : It (transiently) takes the latter form during FD_VM_INTERP_BRANCH_BEGIN and
1337 : FD_VM_INTERP_FAULT, and the former form otherwise.
1338 : */
1339 :
1340 : /* (WIP) Precise faulting and the Agave JIT:
1341 :
1342 : Since the cost model is a part of consensus, we need to conform with the Agave/JIT
1343 : cost model 1:1. To achieve that, our faulting model also needs to match precisely. This
1344 : section covers the various faults that the respective VMs implement and how they match.
1345 :
1346 : # Normal VM exit (sigexit):
1347 : VM exit instruction entrypoint: https://github.com/solana-labs/rbpf/blob/12237895305ab38514be865ebed6268553e4f589/src/jit.rs#L698-L708
1348 :
1349 : Pseudocode (with FD semantics):
1350 : ```
1351 : # pc is at the exit instruction
1352 : # pc0 is the start of the current linear run
1353 : if (frame_cnt == 0) {
1354 : goto sigexit;
1355 : }
1356 : ...
1357 :
1358 : sigexit:
1359 : if IM' <= pc {
1360 : goto sigcost;
1361 : } else {
1362 : goto interp_halt;
1363 : }
1364 : ```
1365 :
1366 : Breaking down the IM' < pc check:
1367 : - IM' = IM + pc0
1368 : - pc = ic + pc0, where (ic + 1) is the number of instructions executed in the current linear run
1369 :
1370 : IM' <= pc
1371 : IM + pc0 <= ic + pc0
1372 : IM <= ic
1373 : IM <= pc - pc0
1374 : IM < pc - pc0 + 1 # all unsigned integers
1375 : IM < ic_correction
1376 :
1377 : This is analogous to the ic_correction>cu check in VM_INTERP_BRANCH_BEGIN.
1378 :
1379 : # (TODO) Text Overrun (sigtext/sigsplit):
1380 :
1381 : */
|