LCOV - code coverage report
Current view: top level - flamenco/vm - fd_vm.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 282 400 70.5 %
Date: 2025-01-08 12:08:44 Functions: 12 12 100.0 %

          Line data    Source code
       1             : #include "fd_vm_private.h"
       2             : #include "../runtime/context/fd_exec_epoch_ctx.h"
       3             : #include "../runtime/context/fd_exec_slot_ctx.h"
       4             : #include "../features/fd_features.h"
       5             : 
       6             : /* fd_vm_syscall_strerror() returns the error message corresponding to err,
       7             :    intended to be logged by log_collector, or an empty string if the error code
       8             :    should be omitted in logs for whatever reason.  Omitted examples are success,
       9             :    panic (logged in place)...
      10             :    See also fd_log_collector_program_failure(). */
      11             : char const *
      12         237 : fd_vm_syscall_strerror( int err ) {
      13             : 
      14         237 :   switch( err ) {
      15             : 
      16           6 :   case FD_VM_SYSCALL_ERR_INVALID_STRING:                         return "invalid utf-8 sequence"; // truncated
      17           9 :   case FD_VM_SYSCALL_ERR_ABORT:                                  return "SBF program panicked";
      18          24 :   case FD_VM_SYSCALL_ERR_PANIC:                                  return "SBF program Panicked in..."; // truncated
      19           0 :   case FD_VM_SYSCALL_ERR_INVOKE_CONTEXT_BORROW_FAILED:           return "Cannot borrow invoke context";
      20           0 :   case FD_VM_SYSCALL_ERR_MALFORMED_SIGNER_SEED:                  return "Malformed signer seed"; // truncated
      21           6 :   case FD_VM_SYSCALL_ERR_BAD_SEEDS:                              return "Could not create program address with signer seeds"; // truncated
      22           0 :   case FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED:                  return "Program not supported by inner instructions"; // truncated
      23          24 :   case FD_VM_SYSCALL_ERR_UNALIGNED_POINTER:                      return "Unaligned pointer";
      24           0 :   case FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS:                       return "Too many signers";
      25           0 :   case FD_VM_SYSCALL_ERR_INSTRUCTION_TOO_LARGE:                  return "Instruction passed to inner instruction is too large"; // truncated
      26           0 :   case FD_VM_SYSCALL_ERR_TOO_MANY_ACCOUNTS:                      return "Too many accounts passed to inner instruction";
      27         111 :   case FD_VM_SYSCALL_ERR_COPY_OVERLAPPING:                       return "Overlapping copy";
      28           0 :   case FD_VM_SYSCALL_ERR_RETURN_DATA_TOO_LARGE:                  return "Return data too large"; // truncated
      29           9 :   case FD_VM_SYSCALL_ERR_TOO_MANY_SLICES:                        return "Hashing too many sequences";
      30          18 :   case FD_VM_SYSCALL_ERR_INVALID_LENGTH:                         return "InvalidLength";
      31           0 :   case FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_DATA_LEN_EXCEEDED:      return "Invoked an instruction with data that is too large"; // truncated
      32           0 :   case FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNTS_EXCEEDED:      return "Invoked an instruction with too many accounts"; // truncated
      33           0 :   case FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED: return "Invoked an instruction with too many account info's"; // truncated
      34          24 :   case FD_VM_SYSCALL_ERR_INVALID_ATTRIBUTE:                      return "InvalidAttribute";
      35           0 :   case FD_VM_SYSCALL_ERR_INVALID_POINTER:                        return "Invalid pointer";
      36           0 :   case FD_VM_SYSCALL_ERR_ARITHMETIC_OVERFLOW:                    return "Arithmetic overflow";
      37             : 
      38           0 :   case FD_VM_SYSCALL_ERR_INSTR_ERR:                              return "Instruction error";
      39           0 :   case FD_VM_SYSCALL_ERR_INVALID_PDA:                            return "Invalid PDA";
      40           0 :   case FD_VM_SYSCALL_ERR_COMPUTE_BUDGET_EXCEEDED:                return "Compute budget exceeded";
      41           0 :   case FD_VM_SYSCALL_ERR_SEGFAULT:                               return "Segmentation fault";
      42           0 :   case FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME:                        return "Syscall executed outside runtime";
      43             : 
      44             : 
      45           3 :   case FD_VM_SYSCALL_ERR_POSEIDON_INVALID_PARAMS:                return "Syscall error: Invalid parameters.";
      46           3 :   case FD_VM_SYSCALL_ERR_POSEIDON_INVALID_ENDIANNESS:            return "Syscall error: Invalid endianness.";
      47             : 
      48           0 :   default: break;
      49         237 :   }
      50             : 
      51           0 :   return "";
      52         237 : }
      53             : 
      54             : /* fd_vm_ebpf_strerror() returns the error message corresponding to err,
      55             :    intended to be logged by log_collector, or an empty string if the error code
      56             :    should be omitted in logs for whatever reason.
      57             :    See also fd_log_collector_program_failure(). */
      58             : char const *
      59        1206 : fd_vm_ebpf_strerror( int err ) {
      60             : 
      61        1206 :   switch( err ) {
      62             : 
      63           0 :   case FD_VM_ERR_EBPF_ELF_ERROR:                   return "ELF error"; // truncated
      64           0 :   case FD_VM_ERR_EBPF_FUNCTION_ALREADY_REGISTERED: return "function was already registered"; // truncated
      65           0 :   case FD_VM_ERR_EBPF_CALL_DEPTH_EXCEEDED:         return "exceeded max BPF to BPF call depth";
      66           0 :   case FD_VM_ERR_EBPF_EXIT_ROOT_CALL_FRAME:        return "attempted to exit root call frame";
      67           0 :   case FD_VM_ERR_EBPF_DIVIDE_BY_ZERO:              return "divide by zero at BPF instruction";
      68           0 :   case FD_VM_ERR_EBPF_DIVIDE_OVERFLOW:             return "division overflow at BPF instruction";
      69           0 :   case FD_VM_ERR_EBPF_EXECUTION_OVERRUN:           return "attempted to execute past the end of the text segment at BPF instruction";
      70           0 :   case FD_VM_ERR_EBPF_CALL_OUTSIDE_TEXT_SEGMENT:   return "callx attempted to call outside of the text segment";
      71           0 :   case FD_VM_ERR_EBPF_EXCEEDED_MAX_INSTRUCTIONS:   return "exceeded CUs meter at BPF instruction";
      72           0 :   case FD_VM_ERR_EBPF_JIT_NOT_COMPILED:            return "program has not been JIT-compiled";
      73           0 :   case FD_VM_ERR_EBPF_INVALID_VIRTUAL_ADDRESS:     return "invalid virtual address"; // truncated
      74           0 :   case FD_VM_ERR_EBPF_INVALID_MEMORY_REGION:       return "Invalid memory region at index"; // truncated
      75        1206 :   case FD_VM_ERR_EBPF_ACCESS_VIOLATION:            return "Access violation"; // truncated
      76           0 :   case FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION:      return "Access violation in stack frame"; // truncated
      77           0 :   case FD_VM_ERR_EBPF_INVALID_INSTRUCTION:         return "invalid BPF instruction";
      78           0 :   case FD_VM_ERR_EBPF_UNSUPPORTED_INSTRUCTION:     return "unsupported BPF instruction";
      79           0 :   case FD_VM_ERR_EBPF_EXHAUSTED_TEXT_SEGMENT:      return "Compilation exhausted text segment at BPF instruction"; // truncated
      80           0 :   case FD_VM_ERR_EBPF_LIBC_INVOCATION_FAILED:      return "Libc calling returned error code"; // truncated
      81           0 :   case FD_VM_ERR_EBPF_VERIFIER_ERROR:              return "Verifier error"; // truncated
      82           0 :   case FD_VM_ERR_EBPF_SYSCALL_ERROR:               return ""; // handled explicitly via fd_vm_syscall_strerror()
      83             : 
      84           0 :   default: break;
      85        1206 :   }
      86             : 
      87           0 :   return "";
      88        1206 : }
      89             : 
      90             : /* fd_vm_strerror() returns the error message corresponding to err, used internally
      91             :    for system logs, NOT for log_collector / transaction logs. */
      92             : char const *
      93          81 : fd_vm_strerror( int err ) {
      94             : 
      95          81 :   switch( err ) {
      96             : 
      97             :   /* "Standard" Firedancer error codes */
      98             : 
      99           3 :   case FD_VM_SUCCESS:   return "SUCCESS success";
     100           3 :   case FD_VM_ERR_INVAL: return "INVAL invalid request";
     101           3 :   case FD_VM_ERR_UNSUP: return "UNSUP unsupported request";
     102           3 :   case FD_VM_ERR_PERM:  return "PERM unauthorized request";
     103           3 :   case FD_VM_ERR_FULL:  return "FULL storage full";
     104           3 :   case FD_VM_ERR_EMPTY: return "EMPTY nothing to do";
     105           3 :   case FD_VM_ERR_IO:    return "IO input-output error";
     106           3 :   case FD_VM_ERR_AGAIN: return "AGAIN try again later";
     107             : 
     108             :   /* VM exec error codes */
     109             : 
     110           3 :   case FD_VM_ERR_SIGTEXT:     return "SIGTEXT illegal program counter";
     111           3 :   case FD_VM_ERR_SIGSPLIT:    return "SIGSPLIT split multiword instruction";
     112           3 :   case FD_VM_ERR_SIGCALL:     return "unsupported BPF instruction";
     113           3 :   case FD_VM_ERR_SIGSTACK:    return "SIGSTACK call depth limit exceeded";
     114           3 :   case FD_VM_ERR_SIGILL:      return "SIGILL illegal instruction";
     115           3 :   case FD_VM_ERR_SIGSEGV:     return "SIGSEGV illegal memory address";
     116           3 :   case FD_VM_ERR_SIGBUS:      return "SIGBUS misaligned memory address";
     117           3 :   case FD_VM_ERR_SIGRDONLY:   return "SIGRDONLY illegal write";
     118           3 :   case FD_VM_ERR_SIGCOST:     return "SIGCOST compute unit limit exceeded";
     119           0 :   case FD_VM_ERR_SIGFPE:      return "SIGFPE division by zero";
     120           0 :   case FD_VM_ERR_SIGFPE_OF:   return "SIGFPE division overflow";
     121           0 :   case FD_VM_ERR_SIGSYSCALL:  return "SIGSYSCALL syscall error"; 
     122           0 :   case FD_VM_ERR_SIGABORT:    return "SIGABORT abort error";
     123             : 
     124             :   /* VM validate error codes */
     125             : 
     126           3 :   case FD_VM_ERR_INVALID_OPCODE:    return "INVALID_OPCODE detected an invalid opcode";
     127           3 :   case FD_VM_ERR_INVALID_SRC_REG:   return "INVALID_SRC_REG detected an invalid source register";
     128           3 :   case FD_VM_ERR_INVALID_DST_REG:   return "INVALID_DST_REG detected an invalid destination register";
     129           3 :   case FD_VM_ERR_INF_LOOP:          return "INF_LOOP detected an infinite loop";
     130           3 :   case FD_VM_ERR_JMP_OUT_OF_BOUNDS: return "JMP_OUT_OF_BOUNDS detected an out of bounds jump";
     131           3 :   case FD_VM_ERR_JMP_TO_ADDL_IMM:   return "JMP_TO_ADDL_IMM detected a jump to an addl imm";
     132           3 :   case FD_VM_ERR_INVALID_END_IMM:   return "INVALID_END_IMM detected an invalid immediate for an endianness conversion instruction";
     133           3 :   case FD_VM_ERR_INCOMPLETE_LDQ:    return "INCOMPLETE_LDQ detected an incomplete ldq at program end";
     134           3 :   case FD_VM_ERR_LDQ_NO_ADDL_IMM:   return "LDQ_NO_ADDL_IMM detected a ldq without an addl imm following it";
     135           3 :   case FD_VM_ERR_NO_SUCH_EXT_CALL:  return "NO_SUCH_EXT_CALL detected a call imm with no function was registered for that immediate";
     136           0 :   case FD_VM_ERR_INVALID_REG:       return "INVALID_REG detected an invalid register number in a callx instruction";
     137           0 :   case FD_VM_ERR_BAD_TEXT:          return "BAD_TEXT detected a bad text section";
     138           0 :   case FD_VM_SH_OVERFLOW:           return "SH_OVERFLOW detected a shift overflow in an instruction";
     139           0 :   case FD_VM_TEXT_SZ_UNALIGNED:     return "TEXT_SZ_UNALIGNED detected an unaligned text section size (not a multiple of 8)";
     140             : 
     141           0 :   default: break;
     142          81 :   }
     143             : 
     144           0 :   return "UNKNOWN probably not a FD_VM_ERR code";
     145          81 : }
     146             : 
     147             : /* FIXME: add a pedantic version of this validation that does things
     148             :    like:
     149             :   - only 0 imms when the instruction does not use an imm
     150             :   - same as above but for src/dst reg, offset */
     151             : /* FIXME: HANDLING OF LDQ ON NEWER SBPF VERSUS OLDER SBPF (AND WITH CALL
     152             :    REG) */
     153             : /* FIXME: LINK TO SOLANA CODE VALIDATE AND DOUBLE CHECK THIS BEHAVES
     154             :    IDENTICALLY! */
     155             : 
     156             : int
     157      330351 : fd_vm_validate( fd_vm_t const * vm ) {
     158             : 
     159      330351 :   ulong sbpf_version = vm->sbpf_version;
     160             : 
     161             :   /* A mapping of all the possible 1-byte sBPF opcodes to their
     162             :      validation criteria. */
     163             : 
     164   697874592 : # define FD_VALID               ((uchar)0) /* Valid opcode */
     165      330351 : # define FD_CHECK_JMP_V3        ((uchar)1) /* Validation should check that the instruction is a valid jump (v3+) */
     166      554790 : # define FD_CHECK_END           ((uchar)2) /* Validation should check that the instruction is a valid endianness conversion */
     167    13773540 : # define FD_CHECK_ST            ((uchar)3) /* Validation should check that the instruction is a valid store */
     168     1401681 : # define FD_CHECK_LDQ           ((uchar)4) /* Validation should check that the instruction is a valid load-quad */
     169    70184115 : # define FD_CHECK_DIV           ((uchar)5) /* Validation should check that the instruction is a valid division by immediate */
     170    49307367 : # define FD_CHECK_SH32          ((uchar)6) /* Validation should check that the immediate is a valid 32-bit shift exponent */
     171    50251998 : # define FD_CHECK_SH64          ((uchar)7) /* Validation should check that the immediate is a valid 64-bit shift exponent */
     172    69437406 : # define FD_INVALID             ((uchar)8) /* The opcode is invalid */
     173      442191 : # define FD_CHECK_CALL_REG      ((uchar)9) /* Validation should check that callx has valid register number */
     174      658620 : # define FD_CHECK_CALL_REG_DEPR ((uchar)10) /* Older / deprecated FD_CHECK_CALLX */
     175      330351 : # define FD_CHECK_CALL_IMM      ((uchar)11) /* Check call against functions registry */
     176      330351 : # define FD_CHECK_SYSCALL       ((uchar)12) /* Check call against syscalls registry */
     177     5218818 : # define FD_CHECK_JMP_V0        ((uchar)13) /* Validation should check that the instruction is a valid jump (v0..v2) */
     178             : 
     179      330351 :   uchar FD_CHECK_JMP = FD_VM_SBPF_STATIC_SYSCALLS (sbpf_version) ? FD_CHECK_JMP_V3 : FD_CHECK_JMP_V0;
     180             : 
     181      330351 :   uchar validation_map[ 256 ] = {
     182             :     /* 0x00 */ FD_INVALID,    /* 0x01 */ FD_INVALID,    /* 0x02 */ FD_INVALID,    /* 0x03 */ FD_INVALID,
     183             :     /* 0x04 */ FD_VALID,      /* 0x05 */ FD_CHECK_JMP,  /* 0x06 */ FD_INVALID,    /* 0x07 */ FD_VALID,
     184             :     /* 0x08 */ FD_INVALID,    /* 0x09 */ FD_INVALID,    /* 0x0a */ FD_INVALID,    /* 0x0b */ FD_INVALID,
     185             :     /* 0x0c */ FD_VALID,      /* 0x0d */ FD_INVALID,    /* 0x0e */ FD_INVALID,    /* 0x0f */ FD_VALID,
     186             :     /* 0x10 */ FD_INVALID,    /* 0x11 */ FD_INVALID,    /* 0x12 */ FD_INVALID,    /* 0x13 */ FD_INVALID,
     187             :     /* 0x14 */ FD_VALID,      /* 0x15 */ FD_CHECK_JMP,  /* 0x16 */ FD_INVALID,    /* 0x17 */ FD_VALID,
     188             :     /* 0x18 */ FD_INVALID,    /* 0x19 */ FD_INVALID,    /* 0x1a */ FD_INVALID,    /* 0x1b */ FD_INVALID,
     189             :     /* 0x1c */ FD_VALID,      /* 0x1d */ FD_CHECK_JMP,  /* 0x1e */ FD_INVALID,    /* 0x1f */ FD_VALID,
     190             :     /* 0x20 */ FD_INVALID,    /* 0x21 */ FD_INVALID,    /* 0x22 */ FD_INVALID,    /* 0x23 */ FD_INVALID,
     191             :     /* 0x24 */ FD_INVALID,    /* 0x25 */ FD_CHECK_JMP,  /* 0x26 */ FD_INVALID,    /* 0x27 */ FD_CHECK_ST,
     192             :     /* 0x28 */ FD_INVALID,    /* 0x29 */ FD_INVALID,    /* 0x2a */ FD_INVALID,    /* 0x2b */ FD_INVALID,
     193             :     /* 0x2c */ FD_VALID,      /* 0x2d */ FD_CHECK_JMP,  /* 0x2e */ FD_INVALID,    /* 0x2f */ FD_CHECK_ST,
     194             :     /* 0x30 */ FD_INVALID,    /* 0x31 */ FD_INVALID,    /* 0x32 */ FD_INVALID,    /* 0x33 */ FD_INVALID,
     195             :     /* 0x34 */ FD_INVALID,    /* 0x35 */ FD_CHECK_JMP,  /* 0x36 */ FD_VALID,      /* 0x37 */ FD_CHECK_ST,
     196             :     /* 0x38 */ FD_INVALID,    /* 0x39 */ FD_INVALID,    /* 0x3a */ FD_INVALID,    /* 0x3b */ FD_INVALID,
     197             :     /* 0x3c */ FD_VALID,      /* 0x3d */ FD_CHECK_JMP,  /* 0x3e */ FD_VALID,      /* 0x3f */ FD_CHECK_ST,
     198             :     /* 0x40 */ FD_INVALID,    /* 0x41 */ FD_INVALID,    /* 0x42 */ FD_INVALID,    /* 0x43 */ FD_INVALID,
     199             :     /* 0x44 */ FD_VALID,      /* 0x45 */ FD_CHECK_JMP,  /* 0x46 */ FD_CHECK_DIV,  /* 0x47 */ FD_VALID,
     200             :     /* 0x48 */ FD_INVALID,    /* 0x49 */ FD_INVALID,    /* 0x4a */ FD_INVALID,    /* 0x4b */ FD_INVALID,
     201             :     /* 0x4c */ FD_VALID,      /* 0x4d */ FD_CHECK_JMP,  /* 0x4e */ FD_VALID,      /* 0x4f */ FD_VALID,
     202             :     /* 0x50 */ FD_INVALID,    /* 0x51 */ FD_INVALID,    /* 0x52 */ FD_INVALID,    /* 0x53 */ FD_INVALID,
     203             :     /* 0x54 */ FD_VALID,      /* 0x55 */ FD_CHECK_JMP,  /* 0x56 */ FD_CHECK_DIV,  /* 0x57 */ FD_VALID,
     204             :     /* 0x58 */ FD_INVALID,    /* 0x59 */ FD_INVALID,    /* 0x5a */ FD_INVALID,    /* 0x5b */ FD_INVALID,
     205             :     /* 0x5c */ FD_VALID,      /* 0x5d */ FD_CHECK_JMP,  /* 0x5e */ FD_VALID,      /* 0x5f */ FD_VALID,
     206             :     /* 0x60 */ FD_INVALID,    /* 0x61 */ FD_INVALID,    /* 0x62 */ FD_INVALID,    /* 0x63 */ FD_INVALID,
     207             :     /* 0x64 */ FD_CHECK_SH32, /* 0x65 */ FD_CHECK_JMP,  /* 0x66 */ FD_CHECK_DIV,  /* 0x67 */ FD_CHECK_SH64,
     208             :     /* 0x68 */ FD_INVALID,    /* 0x69 */ FD_INVALID,    /* 0x6a */ FD_INVALID,    /* 0x6b */ FD_INVALID,
     209             :     /* 0x6c */ FD_VALID,      /* 0x6d */ FD_CHECK_JMP,  /* 0x6e */ FD_VALID,      /* 0x6f */ FD_VALID,
     210             :     /* 0x70 */ FD_INVALID,    /* 0x71 */ FD_INVALID,    /* 0x72 */ FD_INVALID,    /* 0x73 */ FD_INVALID,
     211             :     /* 0x74 */ FD_CHECK_SH32, /* 0x75 */ FD_CHECK_JMP,  /* 0x76 */ FD_CHECK_DIV,  /* 0x77 */ FD_CHECK_SH64,
     212             :     /* 0x78 */ FD_INVALID,    /* 0x79 */ FD_INVALID,    /* 0x7a */ FD_INVALID,    /* 0x7b */ FD_INVALID,
     213             :     /* 0x7c */ FD_VALID,      /* 0x7d */ FD_CHECK_JMP,  /* 0x7e */ FD_VALID,      /* 0x7f */ FD_VALID,
     214             :     /* 0x80 */ FD_INVALID,    /* 0x81 */ FD_INVALID,    /* 0x82 */ FD_INVALID,    /* 0x83 */ FD_INVALID,
     215             :     /* 0x84 */ FD_INVALID,    /* 0x85 */ FD_CHECK_CALL_IMM,/*0x86*/FD_VALID,      /* 0x87 */ FD_CHECK_ST,
     216             :     /* 0x88 */ FD_INVALID,    /* 0x89 */ FD_INVALID,    /* 0x8a */ FD_INVALID,    /* 0x8b */ FD_INVALID,
     217             :     /* 0x8c */ FD_VALID,      /* 0x8d */ FD_CHECK_CALL_REG,/*0x8e*/FD_VALID,      /* 0x8f */ FD_CHECK_ST,
     218             :     /* 0x90 */ FD_INVALID,    /* 0x91 */ FD_INVALID,    /* 0x92 */ FD_INVALID,    /* 0x93 */ FD_INVALID,
     219             :     /* 0x94 */ FD_INVALID,    /* 0x95 */ FD_CHECK_SYSCALL,/*0x96*/ FD_VALID,      /* 0x97 */ FD_CHECK_ST,
     220             :     /* 0x98 */ FD_INVALID,    /* 0x99 */ FD_INVALID,    /* 0x9a */ FD_INVALID,    /* 0x9b */ FD_INVALID,
     221             :     /* 0x9c */ FD_VALID,      /* 0x9d */ FD_VALID,      /* 0x9e */ FD_VALID,      /* 0x9f */ FD_CHECK_ST,
     222             :     /* 0xa0 */ FD_INVALID,    /* 0xa1 */ FD_INVALID,    /* 0xa2 */ FD_INVALID,    /* 0xa3 */ FD_INVALID,
     223             :     /* 0xa4 */ FD_VALID,      /* 0xa5 */ FD_CHECK_JMP,  /* 0xa6 */ FD_INVALID,    /* 0xa7 */ FD_VALID,
     224             :     /* 0xa8 */ FD_INVALID,    /* 0xa9 */ FD_INVALID,    /* 0xaa */ FD_INVALID,    /* 0xab */ FD_INVALID,
     225             :     /* 0xac */ FD_VALID,      /* 0xad */ FD_CHECK_JMP,  /* 0xae */ FD_INVALID,    /* 0xaf */ FD_VALID,
     226             :     /* 0xb0 */ FD_INVALID,    /* 0xb1 */ FD_INVALID,    /* 0xb2 */ FD_INVALID,    /* 0xb3 */ FD_INVALID,
     227             :     /* 0xb4 */ FD_VALID,      /* 0xb5 */ FD_CHECK_JMP,  /* 0xb6 */ FD_VALID,      /* 0xb7 */ FD_VALID,
     228             :     /* 0xb8 */ FD_INVALID,    /* 0xb9 */ FD_INVALID,    /* 0xba */ FD_INVALID,    /* 0xbb */ FD_INVALID,
     229             :     /* 0xbc */ FD_VALID,      /* 0xbd */ FD_CHECK_JMP,  /* 0xbe */ FD_VALID,      /* 0xbf */ FD_VALID,
     230             :     /* 0xc0 */ FD_INVALID,    /* 0xc1 */ FD_INVALID,    /* 0xc2 */ FD_INVALID,    /* 0xc3 */ FD_INVALID,
     231             :     /* 0xc4 */ FD_CHECK_SH32, /* 0xc5 */ FD_CHECK_JMP,  /* 0xc6 */ FD_CHECK_DIV,  /* 0xc7 */ FD_CHECK_SH64,
     232             :     /* 0xc8 */ FD_INVALID,    /* 0xc9 */ FD_INVALID,    /* 0xca */ FD_INVALID,    /* 0xcb */ FD_INVALID,
     233             :     /* 0xcc */ FD_VALID,      /* 0xcd */ FD_CHECK_JMP,  /* 0xce */ FD_VALID,      /* 0xcf */ FD_VALID,
     234             :     /* 0xd0 */ FD_INVALID,    /* 0xd1 */ FD_INVALID,    /* 0xd2 */ FD_INVALID,    /* 0xd3 */ FD_INVALID,
     235             :     /* 0xd4 */ FD_INVALID,    /* 0xd5 */ FD_CHECK_JMP,  /* 0xd6 */ FD_CHECK_DIV,  /* 0xd7 */ FD_INVALID,
     236             :     /* 0xd8 */ FD_INVALID,    /* 0xd9 */ FD_INVALID,    /* 0xda */ FD_INVALID,    /* 0xdb */ FD_INVALID,
     237             :     /* 0xdc */ FD_CHECK_END,  /* 0xdd */ FD_CHECK_JMP,  /* 0xde */ FD_VALID,      /* 0xdf */ FD_INVALID,
     238             :     /* 0xe0 */ FD_INVALID,    /* 0xe1 */ FD_INVALID,    /* 0xe2 */ FD_INVALID,    /* 0xe3 */ FD_INVALID,
     239             :     /* 0xe4 */ FD_INVALID,    /* 0xe5 */ FD_INVALID,    /* 0xe6 */ FD_CHECK_DIV,  /* 0xe7 */ FD_INVALID,
     240             :     /* 0xe8 */ FD_INVALID,    /* 0xe9 */ FD_INVALID,    /* 0xea */ FD_INVALID,    /* 0xeb */ FD_INVALID,
     241             :     /* 0xec */ FD_INVALID,    /* 0xed */ FD_INVALID,    /* 0xee */ FD_VALID,      /* 0xef */ FD_INVALID,
     242             :     /* 0xf0 */ FD_INVALID,    /* 0xf1 */ FD_INVALID,    /* 0xf2 */ FD_INVALID,    /* 0xf3 */ FD_INVALID,
     243             :     /* 0xf4 */ FD_INVALID,    /* 0xf5 */ FD_INVALID,    /* 0xf6 */ FD_CHECK_DIV,  /* 0xf7 */ FD_VALID,
     244             :     /* 0xf8 */ FD_INVALID,    /* 0xf9 */ FD_INVALID,    /* 0xfa */ FD_INVALID,    /* 0xfb */ FD_INVALID,
     245             :     /* 0xfc */ FD_INVALID,    /* 0xfd */ FD_INVALID,    /* 0xfe */ FD_VALID,      /* 0xff */ FD_INVALID,
     246      330351 :   };
     247             : 
     248             :   /* SIMD-0173: LDDW */
     249      330351 :   validation_map[ 0x18 ] = FD_VM_SBPF_ENABLE_LDDW(sbpf_version) ? FD_CHECK_LDQ : FD_INVALID;
     250      330351 :   validation_map[ 0xf7 ] = FD_VM_SBPF_ENABLE_LDDW(sbpf_version) ? FD_INVALID   : FD_VALID; /* HOR64 */
     251             : 
     252             :   /* SIMD-0173: LE */
     253      330351 :   validation_map[ 0xd4 ] = FD_VM_SBPF_ENABLE_LE  (sbpf_version) ? FD_CHECK_END : FD_INVALID;
     254             : 
     255             :   /* SIMD-0173: LDXW, STW, STXW */
     256      330351 :   validation_map[ 0x61 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_VALID;
     257      330351 :   validation_map[ 0x62 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_CHECK_ST;
     258      330351 :   validation_map[ 0x63 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_CHECK_ST;
     259      330351 :   validation_map[ 0x8c ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_VALID     : FD_INVALID;
     260      330351 :   validation_map[ 0x87 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_CHECK_ST  : FD_VALID; /* VALID because it's NEG64 */
     261      330351 :   validation_map[ 0x8f ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_CHECK_ST  : FD_INVALID;
     262             : 
     263             :   /* SIMD-0173: LDXH, STH, STXH */
     264      330351 :   validation_map[ 0x69 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_VALID;
     265      330351 :   validation_map[ 0x6a ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_CHECK_ST;
     266      330351 :   validation_map[ 0x6b ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_CHECK_ST;
     267      330351 :   validation_map[ 0x3c ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_VALID     : FD_VALID;
     268      330351 :   validation_map[ 0x37 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_CHECK_ST  : FD_CHECK_DIV;
     269      330351 :   validation_map[ 0x3f ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_CHECK_ST  : FD_VALID;
     270             : 
     271             :   /* SIMD-0173: LDXB, STB, STXB */
     272      330351 :   validation_map[ 0x71 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_VALID;
     273      330351 :   validation_map[ 0x72 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_CHECK_ST;
     274      330351 :   validation_map[ 0x73 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_CHECK_ST;
     275      330351 :   validation_map[ 0x2c ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_VALID     : FD_VALID;
     276      330351 :   validation_map[ 0x27 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_CHECK_ST  : FD_VALID;
     277      330351 :   validation_map[ 0x2f ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_CHECK_ST  : FD_VALID;
     278             : 
     279             :   /* SIMD-0173: LDXDW, STDW, STXDW */
     280      330351 :   validation_map[ 0x79 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_VALID;
     281      330351 :   validation_map[ 0x7a ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_CHECK_ST;
     282      330351 :   validation_map[ 0x7b ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_INVALID   : FD_CHECK_ST;
     283      330351 :   validation_map[ 0x9c ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_VALID     : FD_VALID;
     284      330351 :   validation_map[ 0x97 ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_CHECK_ST  : FD_CHECK_DIV;
     285      330351 :   validation_map[ 0x9f ] = FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(sbpf_version) ? FD_CHECK_ST  : FD_VALID;
     286             : 
     287             :   /* SIMD-0173: CALLX */
     288      330351 :   validation_map[ 0x8d ] = FD_VM_SBPF_CALLX_USES_SRC_REG(sbpf_version) ? FD_CHECK_CALL_REG : FD_CHECK_CALL_REG_DEPR;
     289             : 
     290             :   /* SIMD-0174: MUL, DIV, MOD */
     291      330351 :   validation_map[ 0x24 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_INVALID : FD_VALID;
     292      330351 :   validation_map[ 0x34 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_INVALID : FD_CHECK_DIV;
     293      330351 :   validation_map[ 0x94 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_INVALID : FD_CHECK_DIV;
     294             :   /* note: 0x?c, 0x?7, 0x?f should not be overwritten because they're now load/store ix */
     295             : 
     296             :   /* SIMD-0174: NEG */
     297      330351 :   validation_map[ 0x84 ] = FD_VM_SBPF_ENABLE_NEG (sbpf_version) ? FD_VALID : FD_INVALID;
     298             :   /* note: 0x87 should not be overwritten because it was NEG64 and it becomes STW */
     299             : 
     300             :   /* SIMD-0174: MUL, DIV, MOD */
     301      330351 :   validation_map[ 0x36 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID; /* UHMUL64 */
     302      330351 :   validation_map[ 0x3e ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     303      330351 :   validation_map[ 0x46 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_CHECK_DIV : FD_INVALID; /* UDIV32 */
     304      330351 :   validation_map[ 0x4e ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     305      330351 :   validation_map[ 0x56 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_CHECK_DIV : FD_INVALID; /* UDIV64 */
     306      330351 :   validation_map[ 0x5e ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     307      330351 :   validation_map[ 0x66 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_CHECK_DIV : FD_INVALID; /* UREM32 */
     308      330351 :   validation_map[ 0x6e ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     309      330351 :   validation_map[ 0x76 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_CHECK_DIV : FD_INVALID; /* UREM64 */
     310      330351 :   validation_map[ 0x7e ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     311      330351 :   validation_map[ 0x86 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID; /* LMUL32 */
     312      330351 :   validation_map[ 0x8e ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     313      330351 :   validation_map[ 0x96 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID; /* LMUL64 */
     314      330351 :   validation_map[ 0x9e ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     315      330351 :   validation_map[ 0xb6 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID; /* SHMUL64 */
     316      330351 :   validation_map[ 0xbe ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     317      330351 :   validation_map[ 0xc6 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_CHECK_DIV : FD_INVALID; /* SDIV32 */
     318      330351 :   validation_map[ 0xce ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     319      330351 :   validation_map[ 0xd6 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_CHECK_DIV : FD_INVALID; /* SDIV64 */
     320      330351 :   validation_map[ 0xde ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     321      330351 :   validation_map[ 0xe6 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_CHECK_DIV : FD_INVALID; /* SREM32 */
     322      330351 :   validation_map[ 0xee ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     323      330351 :   validation_map[ 0xf6 ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_CHECK_DIV : FD_INVALID; /* SREM64 */
     324      330351 :   validation_map[ 0xfe ] = FD_VM_SBPF_ENABLE_PQR (sbpf_version) ? FD_VALID     : FD_INVALID;
     325             : 
     326             :   /* SIMD-0178: static syscalls */
     327      330351 :   validation_map[ 0x85 ] = FD_VM_SBPF_STATIC_SYSCALLS (sbpf_version) ? FD_CHECK_CALL_IMM : FD_VALID;
     328      330351 :   validation_map[ 0x95 ] = FD_VM_SBPF_STATIC_SYSCALLS (sbpf_version) ? FD_CHECK_SYSCALL : FD_VALID;
     329      330351 :   validation_map[ 0x9d ] = FD_VM_SBPF_STATIC_SYSCALLS (sbpf_version) ? FD_VALID : FD_INVALID;
     330             : 
     331             :   /* FIXME: These checks are not necessary assuming fd_vm_t is populated by metadata
     332             :      generated in fd_sbpf_elf_peek (which performs these checks). But there is no guarantee, and
     333             :      this non-guarantee is (rightfully) exploited by the fuzz harnesses.
     334             :      Agave doesn't perform these checks explicitly due to Rust's guarantees  */
     335      330351 :   if( FD_UNLIKELY( vm->text_sz / 8UL != vm->text_cnt ||
     336      330351 :                    (const uchar *)vm->text < vm->rodata ||
     337      330351 :                    (ulong)vm->text > (ulong)vm->text + vm->text_sz || /* Overflow chk */
     338      330351 :                    (const uchar *)vm->text + vm->text_sz > vm->rodata + vm->rodata_sz ) )
     339           0 :     return FD_VM_ERR_BAD_TEXT;
     340             : 
     341      330351 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( vm->text_sz, 8UL ) ) ) /* https://github.com/solana-labs/rbpf/blob/v0.8.0/src/verifier.rs#L109 */
     342         126 :     return FD_VM_TEXT_SZ_UNALIGNED;
     343             : 
     344      330225 :   if ( FD_UNLIKELY( vm->text_cnt == 0UL ) ) /* https://github.com/solana-labs/rbpf/blob/v0.8.0/src/verifier.rs#L112 */
     345           0 :     return FD_VM_ERR_EMPTY;
     346             : 
     347             :   /* FIXME: CLEAN UP LONG / ULONG TYPE CONVERSION */
     348      330225 :   ulong const * text     = vm->text;
     349      330225 :   ulong         text_cnt = vm->text_cnt;
     350   843265989 :   for( ulong i=0UL; i<text_cnt; i++ ) {
     351   843151917 :     fd_sbpf_instr_t instr = fd_sbpf_instr( text[i] );
     352             : 
     353   843151917 :     uchar validation_code = validation_map[ instr.opcode.raw ];
     354   843151917 :     switch( validation_code ) {
     355             : 
     356   669348696 :     case FD_VALID: break;
     357             : 
     358             :     /* Store ops are special because they allow dreg==r10.
     359             :        We use a special validation_code, used later in the
     360             :        "Check registers" section.
     361             :        But there's nothing to do at this time. */
     362     5845116 :     case FD_CHECK_ST: break;
     363             : 
     364     4558116 :     case FD_CHECK_JMP_V0: {
     365     4558116 :       long jmp_dst = (long)i + (long)instr.offset + 1L;
     366     4558116 :       if( FD_UNLIKELY( (jmp_dst<0) | (jmp_dst>=(long)text_cnt)                          ) ) return FD_VM_ERR_JMP_OUT_OF_BOUNDS;
     367             :       //FIXME: this shouldn't be here?
     368     4557975 :       if( FD_UNLIKELY( fd_sbpf_instr( text[ jmp_dst ] ).opcode.raw==FD_SBPF_OP_ADDL_IMM ) ) return FD_VM_ERR_JMP_TO_ADDL_IMM;
     369     4557894 :       break;
     370     4557975 :     }
     371             : 
     372     4557894 :     case FD_CHECK_JMP_V3: {
     373           0 :       long jmp_dst = (long)i + (long)instr.offset + 1L;
     374           0 :       if( FD_UNLIKELY( (jmp_dst<0) | (jmp_dst>=(long)text_cnt) ) ) return FD_VM_ERR_JMP_OUT_OF_BOUNDS;
     375           0 :       break;
     376           0 :     }
     377             : 
     378        5004 :     case FD_CHECK_END: {
     379        5004 :       if( FD_UNLIKELY( !((instr.imm==16) | (instr.imm==32) | (instr.imm==64)) ) ) return FD_VM_ERR_INVALID_END_IMM;
     380        1746 :       break;
     381        5004 :     }
     382             : 
     383             :     /* https://github.com/solana-labs/rbpf/blob/b503a1867a9cfa13f93b4d99679a17fe219831de/src/verifier.rs#L244 */
     384     1182246 :     case FD_CHECK_LDQ: {
     385             :       /* https://github.com/solana-labs/rbpf/blob/b503a1867a9cfa13f93b4d99679a17fe219831de/src/verifier.rs#L131 */
     386     1182246 :       if( FD_UNLIKELY( (i+1UL)>=text_cnt ) ) return FD_VM_ERR_INCOMPLETE_LDQ;
     387             : 
     388             :       /* https://github.com/solana-labs/rbpf/blob/b503a1867a9cfa13f93b4d99679a17fe219831de/src/verifier.rs#L137-L139 */
     389     1182240 :       fd_sbpf_instr_t addl_imm = fd_sbpf_instr( text[ i+1UL ] );
     390     1182240 :       if( FD_UNLIKELY( addl_imm.opcode.raw!=FD_SBPF_OP_ADDL_IMM ) ) return FD_VM_ERR_LDQ_NO_ADDL_IMM;
     391             : 
     392             :       /* FIXME: SET A BIT MAP HERE OF ADDL_IMM TO DENOTE * AS FORBIDDEN
     393             :          BRANCH TARGETS OF CALL_REG?? */
     394             : 
     395     1182234 :       i++; /* Skip the addl imm */
     396     1182234 :       break;
     397     1182240 :     }
     398             : 
     399    64454835 :     case FD_CHECK_DIV: {
     400    64454835 :       if( FD_UNLIKELY( instr.imm==0 ) ) return FD_VM_ERR_SIGFPE;
     401    64453587 :       break;
     402    64454835 :     }
     403             : 
     404    64453587 :     case FD_CHECK_SH32: {
     405    48316314 :       if( FD_UNLIKELY( instr.imm>=32 ) ) return FD_VM_SH_OVERFLOW;
     406    48311100 :       break;
     407    48316314 :     }
     408             : 
     409    49260945 :     case FD_CHECK_SH64: {
     410    49260945 :       if( FD_UNLIKELY( instr.imm>=64 ) ) return FD_VM_SH_OVERFLOW;
     411    49257015 :       break;
     412    49260945 :     }
     413             : 
     414             :     /* https://github.com/solana-labs/rbpf/blob/v0.8.5/src/verifier.rs#L207 */
     415    49257015 :     case FD_CHECK_CALL_REG: {
     416         924 :       if( FD_UNLIKELY( instr.src_reg > 9 ) ) {
     417         297 :         return FD_VM_ERR_INVALID_REG;
     418         297 :       }
     419         627 :       break;
     420         924 :     }
     421      108834 :     case FD_CHECK_CALL_REG_DEPR: {
     422      108834 :       if( FD_UNLIKELY( instr.imm > 9 ) ) {
     423        1449 :         return FD_VM_ERR_INVALID_REG;
     424        1449 :       }
     425      107385 :       break;
     426      108834 :     }
     427             : 
     428             :     /* https://github.com/solana-labs/rbpf/blob/4ad935be/src/verifier.rs#L411-L418 */
     429      107385 :     case FD_CHECK_CALL_IMM: {
     430           0 :       ulong target_pc = (ulong)( fd_long_sat_add( (long)i, fd_long_sat_add( (long)(int)instr.imm, 1 ) ) );
     431           0 :       if( FD_UNLIKELY( target_pc>text_cnt || !fd_sbpf_calldests_test( vm->calldests, target_pc ) ) ) {
     432           0 :         return FD_VM_INVALID_FUNCTION;
     433           0 :       }
     434           0 :       break;
     435           0 :     }
     436             : 
     437             :     /* https://github.com/solana-labs/rbpf/blob/4ad935be/src/verifier.rs#L423-L428 */
     438           0 :     case FD_CHECK_SYSCALL: {
     439           0 :       uint imm = instr.imm;
     440             :       /* check out of bound */
     441           0 :       if( FD_UNLIKELY( imm==0 || imm >= FD_VM_SBPF_STATIC_SYSCALLS_LIST_SZ ) ) {
     442           0 :         return FD_VM_INVALID_SYSCALL;
     443           0 :       }
     444           0 :       uint syscall_key = FD_VM_SBPF_STATIC_SYSCALLS_LIST[ imm ];
     445             :       /* check active syscall */
     446           0 :       fd_sbpf_syscalls_t const * syscall = fd_sbpf_syscalls_query_const( vm->syscalls, syscall_key, NULL );
     447           0 :       if( FD_UNLIKELY( !syscall ) ) {
     448           0 :         return FD_VM_INVALID_SYSCALL;
     449           0 :       }
     450           0 :       break;
     451           0 :     }
     452             : 
     453       70887 :     case FD_INVALID: default: return FD_VM_ERR_INVALID_OPCODE;
     454   843151917 :     }
     455             : 
     456             :     /* Check registers
     457             :        https://github.com/solana-labs/rbpf/blob/v0.8.5/src/verifier.rs#L177 */
     458             : 
     459             :     /* Source register */
     460   843065400 :     if( FD_UNLIKELY( instr.src_reg>10 ) ) return FD_VM_ERR_INVALID_SRC_REG;
     461             : 
     462             :     /* Special R10 register allowed for ADD64_IMM */
     463   843027318 :     if( instr.dst_reg==10U
     464   843027318 :         && FD_VM_SBPF_DYNAMIC_STACK_FRAMES( sbpf_version )
     465   843027318 :         && instr.opcode.raw == 0x07
     466   843027318 :         && ( instr.imm % FD_VM_SBPF_DYNAMIC_STACK_FRAMES_ALIGN )==0 )
     467          99 :         continue;
     468             : 
     469             :     /* Destination register. */
     470   843027219 :     if( FD_UNLIKELY( instr.dst_reg==10U && validation_code != FD_CHECK_ST ) ) return FD_VM_ERR_INVALID_DST_REG;
     471   842983125 :     if( FD_UNLIKELY( instr.dst_reg > 10U ) ) return FD_VM_ERR_INVALID_DST_REG;
     472   842983125 :   }
     473             : 
     474      114072 :   return FD_VM_SUCCESS;
     475      330225 : }
     476             : 
     477             : FD_FN_CONST ulong
     478      333126 : fd_vm_align( void ) {
     479      333126 :   return FD_VM_ALIGN;
     480      333126 : }
     481             : 
     482             : FD_FN_CONST ulong
     483      333126 : fd_vm_footprint( void ) {
     484      333126 :   return FD_VM_FOOTPRINT;
     485      333126 : }
     486             : 
     487             : void *
     488      336444 : fd_vm_new( void * shmem ) {
     489             : 
     490      336444 :   if( FD_UNLIKELY( !shmem ) ) {
     491           0 :     FD_LOG_WARNING(( "NULL shmem" ));
     492           0 :     return NULL;
     493           0 :   }
     494             : 
     495      336444 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_vm_align() ) ) ) {
     496           0 :     FD_LOG_WARNING(( "misaligned shmem" ));
     497           0 :     return NULL;
     498           0 :   }
     499             : 
     500      336444 :   fd_vm_t * vm = (fd_vm_t *)shmem;
     501      336444 :   fd_memset( vm, 0, fd_vm_footprint() );
     502             : 
     503      336444 :   FD_COMPILER_MFENCE();
     504      336444 :   FD_VOLATILE( vm->magic ) = FD_VM_MAGIC;
     505      336444 :   FD_COMPILER_MFENCE();
     506             : 
     507      336444 :   return shmem;
     508      336444 : }
     509             : 
     510             : fd_vm_t *
     511      336444 : fd_vm_join( void * shmem ) {
     512             : 
     513      336444 :   if( FD_UNLIKELY( !shmem ) ) {
     514           0 :     FD_LOG_WARNING(( "NULL shmem" ));
     515           0 :     return NULL;
     516           0 :   }
     517             : 
     518      336444 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_vm_align() ) ) ) {
     519           0 :     FD_LOG_WARNING(( "misaligned shmem" ));
     520           0 :     return NULL;
     521           0 :   }
     522             : 
     523      336444 :   fd_vm_t * vm = (fd_vm_t *)shmem;
     524             : 
     525      336444 :   if( FD_UNLIKELY( vm->magic!=FD_VM_MAGIC ) ) {
     526           0 :     FD_LOG_WARNING(( "bad magic" ));
     527           0 :     return NULL;
     528           0 :   }
     529             : 
     530      336444 :   return vm;
     531      336444 : }
     532             : 
     533             : void *
     534           9 : fd_vm_leave( fd_vm_t * vm ) {
     535             : 
     536           9 :   if( FD_UNLIKELY( !vm ) ) {
     537           0 :     FD_LOG_WARNING(( "NULL vm" ));
     538           0 :     return NULL;
     539           0 :   }
     540             : 
     541           9 :   return (void *)vm;
     542           9 : }
     543             : 
     544             : void *
     545           9 : fd_vm_delete( void * shmem ) {
     546             : 
     547           9 :   if( FD_UNLIKELY( !shmem ) ) {
     548           0 :     FD_LOG_WARNING(( "NULL shmem" ));
     549           0 :     return NULL;
     550           0 :   }
     551             : 
     552           9 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_vm_align() ) ) ) {
     553           0 :     FD_LOG_WARNING(( "misaligned shmem" ));
     554           0 :     return NULL;
     555           0 :   }
     556             : 
     557           9 :   fd_vm_t * vm = (fd_vm_t *)shmem;
     558             : 
     559           9 :   if( FD_UNLIKELY( vm->magic!=FD_VM_MAGIC ) ) {
     560           0 :     FD_LOG_WARNING(( "bad magic" ));
     561           0 :     return NULL;
     562           0 :   }
     563             : 
     564           9 :   FD_COMPILER_MFENCE();
     565           9 :   FD_VOLATILE( vm->magic ) = 0UL;
     566           9 :   FD_COMPILER_MFENCE();
     567             : 
     568           9 :   return (void *)vm;
     569           9 : }
     570             : 
     571             : fd_vm_t *
     572             : fd_vm_init(
     573             :    fd_vm_t * vm,
     574             :    fd_exec_instr_ctx_t *instr_ctx,
     575             :    ulong heap_max,
     576             :    ulong entry_cu,
     577             :    uchar const * rodata,
     578             :    ulong rodata_sz,
     579             :    ulong const * text,
     580             :    ulong text_cnt,
     581             :    ulong text_off,
     582             :    ulong text_sz,
     583             :    ulong entry_pc,
     584             :    ulong * calldests,
     585             :    ulong sbpf_version,
     586             :    fd_sbpf_syscalls_t * syscalls,
     587             :    fd_vm_trace_t * trace,
     588             :    fd_sha256_t * sha,
     589             :    fd_vm_input_region_t * mem_regions,
     590             :    uint mem_regions_cnt,
     591             :    fd_vm_acc_region_meta_t * acc_region_metas,
     592             :    uchar is_deprecated,
     593      344097 :    int direct_mapping ) {
     594             : 
     595      344097 :   if ( FD_UNLIKELY( vm == NULL ) ) {
     596           0 :     FD_LOG_WARNING(( "NULL vm" ));
     597           0 :     return NULL;
     598           0 :   }
     599             : 
     600      344097 :   if ( FD_UNLIKELY( vm->magic != FD_VM_MAGIC ) ) {
     601           0 :     FD_LOG_WARNING(( "bad magic" ));
     602           0 :     return NULL;
     603           0 :   }
     604             : 
     605      344097 :   if ( FD_UNLIKELY( instr_ctx == NULL ) ) {
     606           0 :     FD_LOG_WARNING(( "NULL instr_ctx" ));
     607           0 :     return NULL;
     608           0 :   }
     609             : 
     610      344097 :   if ( FD_UNLIKELY( heap_max > FD_VM_HEAP_MAX ) ) {
     611           0 :     FD_LOG_WARNING(( "heap_max > FD_VM_HEAP_MAX" ));
     612           0 :     return NULL;
     613           0 :   }
     614             : 
     615             :   // Set the vm fields
     616      344097 :   vm->instr_ctx = instr_ctx;
     617      344097 :   vm->heap_max = heap_max;
     618      344097 :   vm->entry_cu = entry_cu;
     619      344097 :   vm->rodata = rodata;
     620      344097 :   vm->rodata_sz = rodata_sz;
     621      344097 :   vm->text = text;
     622      344097 :   vm->text_cnt = text_cnt;
     623      344097 :   vm->text_off = text_off;
     624      344097 :   vm->text_sz = text_sz;
     625      344097 :   vm->entry_pc = entry_pc;
     626      344097 :   vm->calldests = calldests;
     627      344097 :   vm->sbpf_version = sbpf_version;
     628      344097 :   vm->syscalls = syscalls;
     629      344097 :   vm->trace = trace;
     630      344097 :   vm->sha = sha;
     631      344097 :   vm->input_mem_regions = mem_regions;
     632      344097 :   vm->input_mem_regions_cnt = mem_regions_cnt;
     633      344097 :   vm->acc_region_metas = acc_region_metas;
     634      344097 :   vm->is_deprecated = is_deprecated;
     635      344097 :   vm->direct_mapping = direct_mapping;
     636      344097 :   vm->stack_frame_size = FD_VM_STACK_FRAME_SZ + ( direct_mapping ? 0UL : FD_VM_STACK_GUARD_SZ );
     637      344097 :   vm->segv_store_vaddr = ULONG_MAX;
     638             : 
     639             :   /* Unpack the configuration */
     640      344097 :   int err = fd_vm_setup_state_for_execution( vm );
     641      344097 :   if( FD_UNLIKELY( err != FD_VM_SUCCESS ) ) {
     642           0 :     return NULL;
     643           0 :   }
     644             : 
     645      344097 :   return vm;
     646      344097 : }
     647             : 
     648             : int
     649      344097 : fd_vm_setup_state_for_execution( fd_vm_t * vm ) {
     650             : 
     651      344097 :   if ( FD_UNLIKELY( !vm ) ) {
     652           0 :     FD_LOG_WARNING(( "NULL vm" ));
     653           0 :     return FD_VM_ERR_INVAL;
     654           0 :   }
     655             : 
     656             :   /* Unpack input and rodata */
     657      344097 :   fd_vm_mem_cfg( vm );
     658             : 
     659             :   /* Initialize registers */
     660             :   /* FIXME: Zero out shadow, stack and heap here? */
     661      344097 :   fd_memset( vm->reg, 0, FD_VM_REG_MAX * sizeof(ulong) );
     662      344097 :   vm->reg[ 1] = FD_VM_MEM_MAP_INPUT_REGION_START;
     663             :   /* https://github.com/solana-labs/rbpf/blob/4ad935be45e5663be23b30cfc750b1ae1ad03c44/src/vm.rs#L326-L333 */
     664      344097 :   vm->reg[10] = FD_VM_MEM_MAP_STACK_REGION_START +
     665      344097 :     ( FD_VM_SBPF_DYNAMIC_STACK_FRAMES( vm->sbpf_version ) ? FD_VM_STACK_MAX : FD_VM_STACK_FRAME_SZ );
     666             :   /* Note: Agave uses r11 as pc, we don't */
     667             : 
     668             :   /* Set execution state */
     669      344097 :   vm->pc        = vm->entry_pc;
     670      344097 :   vm->ic        = 0UL;
     671      344097 :   vm->cu        = vm->entry_cu;
     672      344097 :   vm->frame_cnt = 0UL;
     673             : 
     674      344097 :   vm->heap_sz = 0UL;
     675             : 
     676             :   /* Do NOT reset logs */
     677             : 
     678      344097 :   return FD_VM_SUCCESS;
     679      344097 : }

Generated by: LCOV version 1.14