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