LCOV - code coverage report
Current view: top level - flamenco/vm - fd_vm_interp_core.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 620 705 87.9 %
Date: 2025-10-27 04:40:00 Functions: 0 0 -

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

Generated by: LCOV version 1.14