LCOV - code coverage report
Current view: top level - flamenco/vm - fd_vm_private.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 144 204 70.6 %
Date: 2025-12-29 05:22:51 Functions: 32 546 5.9 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_flamenco_vm_fd_vm_private_h
       2             : #define HEADER_fd_src_flamenco_vm_fd_vm_private_h
       3             : 
       4             : #include "fd_vm.h"
       5             : 
       6             : #include "../runtime/fd_runtime_const.h"
       7             : #include "../runtime/fd_runtime.h"
       8             : #include "fd_vm_base.h"
       9             : 
      10             : /* FD_VM_ALIGN_RUST_{} define the alignments for relevant rust types.
      11             :    Alignments are derived with std::mem::align_of::<T>() and are enforced
      12             :    by the VM (with the exception of v1 loader).
      13             : 
      14             :    In our implementation, when calling FD_VM_MEM_HADDR_ST / FD_VM_MEM_HADDR_LD,
      15             :    we need to make sure we're passing the correct alignment based on the Rust
      16             :    type in the corresponding mapping in Agave.
      17             : 
      18             :    FD_VM_ALIGN_RUST_{} has been generated with this Rust code:
      19             :    ```rust
      20             :       pub type Epoch = u64;
      21             :       pub struct Pubkey(pub [u8; 32]);
      22             :       pub struct AccountMeta {
      23             :           pub lamports: u64,
      24             :           pub rent_epoch: Epoch,
      25             :           pub owner: Pubkey,
      26             :           pub executable: bool,
      27             :       }
      28             : 
      29             :       pub struct PodScalar(pub [u8; 32]);
      30             : 
      31             :       fn main() {
      32             :           println!("u8: {}", std::mem::align_of::<u8>());
      33             :           println!("u32: {}", std::mem::align_of::<u32>());
      34             :           println!("u64: {}", std::mem::align_of::<u64>());
      35             :           println!("u128: {}", std::mem::align_of::<u128>());
      36             :           println!("&[u8]: {}", std::mem::align_of::<&[u8]>());
      37             :           println!("AccountMeta: {}", std::mem::align_of::<AccountMeta>());
      38             :           println!("PodScalar: {}", std::mem::align_of::<PodScalar>());
      39             :           println!("Pubkey: {}", std::mem::align_of::<Pubkey>());
      40             :       }
      41             :     ``` */
      42             : 
      43          93 : #define FD_VM_ALIGN_RUST_U8                       (1UL)
      44             : #define FD_VM_ALIGN_RUST_U32                      (4UL)
      45          15 : #define FD_VM_ALIGN_RUST_I32                      (4UL)
      46             : #define FD_VM_ALIGN_RUST_U64                      (8UL)
      47             : #define FD_VM_ALIGN_RUST_U128                    (16UL)
      48             : #define FD_VM_ALIGN_RUST_SLICE_U8_REF             (8UL)
      49          18 : #define FD_VM_ALIGN_RUST_POD_U8_ARRAY             (1UL)
      50           0 : #define FD_VM_ALIGN_RUST_PUBKEY                   (1UL)
      51           0 : #define FD_VM_ALIGN_RUST_SYSVAR_CLOCK             (8UL)
      52           0 : #define FD_VM_ALIGN_RUST_SYSVAR_EPOCH_SCHEDULE    (8UL)
      53           0 : #define FD_VM_ALIGN_RUST_SYSVAR_RENT              (8UL)
      54           0 : #define FD_VM_ALIGN_RUST_SYSVAR_LAST_RESTART_SLOT (8UL)
      55             : #define FD_VM_ALIGN_RUST_SYSVAR_EPOCH_REWARDS    (16UL)
      56             : #define FD_VM_ALIGN_RUST_STABLE_INSTRUCTION       (8UL)
      57             : 
      58             : /* fd_vm_vec_t is the in-memory representation of a vector descriptor.
      59             :    Equal in layout to the Rust slice header &[_] and various vector
      60             :    types in the C version of the syscall API. */
      61             : /* FIXME: WHEN IS VADDR NULL AND/OR SZ 0 OKAY? */
      62             : /* FIXME: MOVE FD_VM_RUST_VEC_T FROM SYSCALL/FD_VM_CPI.H HERE TOO? */
      63             : 
      64             : #define FD_VM_VEC_ALIGN FD_VM_ALIGN_RUST_SLICE_U8_REF
      65             : #define FD_VM_VEC_SIZE  (16UL)
      66             : 
      67             : struct __attribute__((packed)) fd_vm_vec {
      68             :   ulong addr; /* FIXME: NAME -> VADDR */
      69             :   ulong len;  /* FIXME: NAME -> SZ */
      70             : };
      71             : 
      72             : typedef struct fd_vm_vec fd_vm_vec_t;
      73             : 
      74             : FD_STATIC_ASSERT( sizeof(fd_vm_vec_t)==FD_VM_VEC_SIZE, fd_vm_vec size mismatch );
      75             : 
      76             : /* SBPF version and features
      77             :    https://github.com/anza-xyz/sbpf/blob/v0.12.2/src/program.rs#L28
      78             :    Note: SIMDs enable or disable features, e.g. BPF instructions.
      79             :    If we have macros with names ENABLE vs DISABLE, we have the advantage that
      80             :    the condition is always pretty clear: sbpf_version <= activation_version,
      81             :    but the disadvantage of inconsistent names.
      82             :    Viceversa, calling everything ENABLE has the risk to invert a <= with a >=
      83             :    and create a huge mess.
      84             :    We define both, so hopefully it's foolproof. */
      85             : 
      86             : #define FD_VM_SBPF_REJECT_RODATA_STACK_OVERLAP(v)  ( v != FD_SBPF_V0 )
      87             : #define FD_VM_SBPF_ENABLE_ELF_VADDR(v)             ( v != FD_SBPF_V0 )
      88             : /* SIMD-0166 */
      89   805499499 : #define FD_VM_SBPF_DYNAMIC_STACK_FRAMES(v)         ( v >= FD_SBPF_V1 )
      90             : /* SIMD-0173 */
      91        7884 : #define FD_VM_SBPF_CALLX_USES_SRC_REG(v)           ( v >= FD_SBPF_V2 )
      92             : #define FD_VM_SBPF_DISABLE_LDDW(v)                 ( v >= FD_SBPF_V2 )
      93       78000 : #define FD_VM_SBPF_ENABLE_LDDW(v)                  ( v <  FD_SBPF_V2 )
      94             : #define FD_VM_SBPF_DISABLE_LE(v)                   ( v >= FD_SBPF_V2 )
      95       39000 : #define FD_VM_SBPF_ENABLE_LE(v)                    ( v <  FD_SBPF_V2 )
      96      936000 : #define FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(v)       ( v >= FD_SBPF_V2 )
      97             : /* SIMD-0174 */
      98     1053000 : #define FD_VM_SBPF_ENABLE_PQR(v)                   ( v >= FD_SBPF_V2 )
      99             : #define FD_VM_SBPF_DISABLE_NEG(v)                  ( v >= FD_SBPF_V2 )
     100       39000 : #define FD_VM_SBPF_ENABLE_NEG(v)                   ( v <  FD_SBPF_V2 )
     101       62232 : #define FD_VM_SBPF_SWAP_SUB_REG_IMM_OPERANDS(v)    ( v >= FD_SBPF_V2 )
     102      124464 : #define FD_VM_SBPF_EXPLICIT_SIGN_EXT(v)            ( v >= FD_SBPF_V2 )
     103             : /* SIMD-0178 + SIMD-0179 */
     104      156000 : #define FD_VM_SBPF_STATIC_SYSCALLS(v)              ( v >= FD_SBPF_V3 )
     105             : /* SIMD-0189 */
     106             : #define FD_VM_SBPF_ENABLE_LOWER_BYTECODE_VADDR(v)  ( v >= FD_SBPF_V3 )
     107             : /* enable_strict_elf_headers is defined in fd_sbpf_loader.h because it's needed
     108             :    by the ELF loader, not really by the VM
     109             :    #define FD_VM_SBPF_ENABLE_STRICTER_ELF_HEADERS(v)  ( v >= FD_SBPF_V3 ) */
     110             : 
     111        3483 : #define FD_VM_OFFSET_MASK (0xffffffffUL)
     112             : 
     113             : /* https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L32 */
     114           0 : #define FD_MAX_ACCOUNT_DATA_GROWTH_PER_TRANSACTION (FD_RUNTIME_ACC_SZ_MAX * 2UL)
     115             : 
     116             : FD_PROTOTYPES_BEGIN
     117             : 
     118             : /* Error logging handholding assertions */
     119             : 
     120             : #ifdef FD_RUNTIME_ERR_HANDHOLDING
     121             : /* Asserts that the error and error kind are populated (non-zero) */
     122             : #define FD_VM_TEST_ERR_EXISTS( vm )                                       \
     123             :     FD_TEST( vm->instr_ctx->txn_out->err.exec_err );                      \
     124             :     FD_TEST( vm->instr_ctx->txn_out->err.exec_err_kind )
     125             : 
     126             : /* Used prior to a FD_VM_ERR_FOR_LOG_INSTR call to deliberately
     127             :    bypass overwrite handholding checks.
     128             :    Only use this if you know what you're doing. */
     129             : #define FD_VM_PREPARE_ERR_OVERWRITE( vm )                                 \
     130             :    vm->instr_ctx->txn_out->err.exec_err = 0;                              \
     131             :    vm->instr_ctx->txn_out->err.exec_err_kind = 0
     132             : 
     133             : /* Asserts that the error and error kind are not populated (zero) */
     134             : #define FD_VM_TEST_ERR_OVERWRITE( vm )                                    \
     135             :     FD_TEST( !vm->instr_ctx->txn_out->err.exec_err );                     \
     136             :     FD_TEST( !vm->instr_ctx->txn_out->err.exec_err_kind )
     137             : #else
     138           0 : #define FD_VM_TEST_ERR_EXISTS( vm ) ( ( void )0 )
     139           0 : #define FD_VM_PREPARE_ERR_OVERWRITE( vm ) ( ( void )0 )
     140          93 : #define FD_VM_TEST_ERR_OVERWRITE( vm ) ( ( void )0 )
     141             : #endif
     142             : 
     143             : /* Log error within the instr_ctx to match Agave/Rust error. */
     144             : 
     145          57 : #define FD_VM_ERR_FOR_LOG_EBPF( vm, err_ ) (__extension__({                \
     146          57 :     FD_VM_TEST_ERR_OVERWRITE( vm );                                        \
     147          57 :     vm->instr_ctx->txn_out->err.exec_err = err_;                           \
     148          57 :     vm->instr_ctx->txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_EBPF; \
     149          57 :   }))
     150             : 
     151          36 : #define FD_VM_ERR_FOR_LOG_SYSCALL( vm, err_ ) (__extension__({                \
     152          36 :     FD_VM_TEST_ERR_OVERWRITE( vm );                                           \
     153          36 :     vm->instr_ctx->txn_out->err.exec_err = err_;                              \
     154          36 :     vm->instr_ctx->txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_SYSCALL; \
     155          36 :   }))
     156             : 
     157           0 : #define FD_VM_ERR_FOR_LOG_INSTR( vm, err_ ) (__extension__({                \
     158           0 :     FD_VM_TEST_ERR_OVERWRITE( vm );                                         \
     159           0 :     vm->instr_ctx->txn_out->err.exec_err = err_;                            \
     160           0 :     vm->instr_ctx->txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_INSTR; \
     161           0 :   }))
     162             : 
     163        3441 : #define FD_VADDR_TO_REGION( _vaddr ) fd_ulong_min( (_vaddr) >> FD_VM_MEM_MAP_REGION_VIRT_ADDR_BITS, FD_VM_HIGH_REGION )
     164             : 
     165             : /* fd_vm_instr APIs ***************************************************/
     166             : 
     167             : /* FIXME: MIGRATE FD_SBPF_INSTR_T STUFF TO THIS API */
     168             : 
     169             : /* fd_vm_instr returns the SBPF instruction word corresponding to the
     170             :    given fields. */
     171             : 
     172             : FD_FN_CONST static inline ulong
     173             : fd_vm_instr( ulong opcode, /* Assumed valid */
     174             :              ulong dst,    /* Assumed in [0,FD_VM_REG_CNT) */
     175             :              ulong src,    /* Assumed in [0,FD_VM_REG_CNT) */
     176             :              short offset,
     177       16587 :              uint  imm ) {
     178       16587 :   return opcode | (dst<<8) | (src<<12) | (((ulong)(ushort)offset)<<16) | (((ulong)imm)<<32);
     179       16587 : }
     180             : 
     181             : /* fd_vm_instr_* return the SBPF instruction field for the given word.
     182             :    fd_vm_instr_{normal,mem}_* only apply to {normal,mem} opclass
     183             :    instructions. */
     184             : 
     185      381843 : FD_FN_CONST static inline ulong fd_vm_instr_opcode( ulong instr ) { return   instr      & 255UL;       } /* In [0,256) */
     186      381843 : FD_FN_CONST static inline ulong fd_vm_instr_dst   ( ulong instr ) { return ((instr>> 8) &  15UL);      } /* In [0,16)  */
     187      381843 : FD_FN_CONST static inline ulong fd_vm_instr_src   ( ulong instr ) { return ((instr>>12) &  15UL);      } /* In [0,16)  */
     188      381843 : FD_FN_CONST static inline ulong fd_vm_instr_offset( ulong instr ) { return (ulong)(long)(short)(ushort)(instr>>16); }
     189      381960 : FD_FN_CONST static inline uint  fd_vm_instr_imm   ( ulong instr ) { return (uint)(instr>>32);          }
     190             : 
     191           0 : FD_FN_CONST static inline ulong fd_vm_instr_opclass       ( ulong instr ) { return  instr      & 7UL; } /* In [0,8)  */
     192           0 : FD_FN_CONST static inline ulong fd_vm_instr_normal_opsrc  ( ulong instr ) { return (instr>>3) &  1UL; } /* In [0,2)  */
     193           0 : FD_FN_CONST static inline ulong fd_vm_instr_normal_opmode ( ulong instr ) { return (instr>>4) & 15UL; } /* In [0,16) */
     194           0 : FD_FN_CONST static inline ulong fd_vm_instr_mem_opsize    ( ulong instr ) { return (instr>>3) &  3UL; } /* In [0,4)  */
     195           0 : FD_FN_CONST static inline ulong fd_vm_instr_mem_opaddrmode( ulong instr ) { return (instr>>5) &  7UL; } /* In [0,16) */
     196             : 
     197             : /* fd_vm_mem API ******************************************************/
     198             : 
     199             : /* fd_vm_mem APIs support the fast mapping of virtual address ranges to
     200             :    host address ranges.  Since the SBPF virtual address space consists
     201             :    of 4 consecutive 4GiB regions and the mapable size of each region is
     202             :    less than 4 GiB (as implied by FD_VM_MEM_MAP_REGION_SZ==2^32-1 and
     203             :    that Solana protocol limits are much smaller still), it is impossible
     204             :    for a valid virtual address range to span multiple regions. */
     205             : 
     206             : /* fd_vm_mem_cfg configures the vm's tlb arrays.  Assumes vm is valid
     207             :    and vm already has configured the rodata, stack, heap and input
     208             :    regions.  Returns vm. */
     209             : 
     210             : static inline fd_vm_t *
     211        8106 : fd_vm_mem_cfg( fd_vm_t * vm ) {
     212        8106 :   vm->region_haddr[0] = 0UL;                                vm->region_ld_sz[0]                  = (uint)0UL;             vm->region_st_sz[0]                  = (uint)0UL;
     213        8106 :   vm->region_haddr[FD_VM_PROG_REGION]  = (ulong)vm->rodata; vm->region_ld_sz[FD_VM_PROG_REGION]  = (uint)vm->rodata_sz;   vm->region_st_sz[FD_VM_PROG_REGION]  = (uint)0UL;
     214        8106 :   vm->region_haddr[FD_VM_STACK_REGION] = (ulong)vm->stack;  vm->region_ld_sz[FD_VM_STACK_REGION] = (uint)FD_VM_STACK_MAX; vm->region_st_sz[FD_VM_STACK_REGION] = (uint)FD_VM_STACK_MAX;
     215        8106 :   vm->region_haddr[FD_VM_HEAP_REGION]  = (ulong)vm->heap;   vm->region_ld_sz[FD_VM_HEAP_REGION]  = (uint)vm->heap_max;    vm->region_st_sz[FD_VM_HEAP_REGION]  = (uint)vm->heap_max;
     216        8106 :   vm->region_haddr[5]                  = 0UL;               vm->region_ld_sz[5]                  = (uint)0UL;             vm->region_st_sz[5]                  = (uint)0UL;
     217        8106 :   if( vm->direct_mapping || !vm->input_mem_regions_cnt ) {
     218             :     /* When direct mapping is enabled, we don't use these fields because
     219             :        the load and stores are fragmented. */
     220         450 :     vm->region_haddr[FD_VM_INPUT_REGION] = 0UL;
     221         450 :     vm->region_ld_sz[FD_VM_INPUT_REGION] = 0U;
     222         450 :     vm->region_st_sz[FD_VM_INPUT_REGION] = 0U;
     223        7656 :   } else {
     224        7656 :     vm->region_haddr[FD_VM_INPUT_REGION] = vm->input_mem_regions[0].haddr;
     225        7656 :     vm->region_ld_sz[FD_VM_INPUT_REGION] = vm->input_mem_regions[0].region_sz;
     226        7656 :     vm->region_st_sz[FD_VM_INPUT_REGION] = vm->input_mem_regions[0].region_sz;
     227        7656 :   }
     228        8106 :   return vm;
     229        8106 : }
     230             : 
     231             : /* Simplified version of Agave's `generate_access_violation()` function
     232             :    that simply returns either FD_VM_ERR_EBPF_ACCESS_VIOLATION or
     233             :    FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION. This has no consensus
     234             :    effects and is purely for logging purposes for fuzzing. Returns
     235             :    FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION if the provided vaddr is in the
     236             :    stack (0x200000000) and FD_VM_ERR_EBPF_ACCESS_VIOLATION otherwise.
     237             : 
     238             :    https://github.com/anza-xyz/sbpf/blob/v0.11.1/src/memory_region.rs#L834-L869 */
     239             : static FD_FN_PURE inline int
     240         255 : fd_vm_generate_access_violation( ulong vaddr, ulong sbpf_version ) {
     241             :   /* rel_offset can be negative because there is an edge case where the
     242             :      first "frame" right before the stack region should also throw a
     243             :      stack access violation. */
     244         255 :   long rel_offset = fd_long_sat_sub( (long)vaddr, (long)FD_VM_MEM_MAP_STACK_REGION_START );
     245         255 :   long stack_frame = rel_offset / (long)FD_VM_STACK_FRAME_SZ;
     246         255 :   if( !fd_sbpf_dynamic_stack_frames_enabled( sbpf_version ) &&
     247         255 :       stack_frame>=-1L && stack_frame<=(long)FD_VM_MAX_CALL_DEPTH ) {
     248           0 :     return FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION;
     249           0 :   }
     250         255 :   return FD_VM_ERR_EBPF_ACCESS_VIOLATION;
     251         255 : }
     252             : 
     253             : /* fd_vm_mem_haddr translates the vaddr range [vaddr,vaddr+sz) (in
     254             :    infinite precision math) into the non-wrapping haddr range
     255             :    [haddr,haddr+sz).  On success, returns haddr and every byte in the
     256             :    haddr range is a valid address.  On failure, returns sentinel and
     257             :    there was at least one byte in the virtual address range that did not
     258             :    have a corresponding byte in the host address range.
     259             : 
     260             :    IMPORTANT SAFETY TIP!  When sz==0, the return value currently is
     261             :    arbitrary.  This is often fine as there should be no
     262             :    actual accesses to a sz==0 region.  However, this also means that
     263             :    testing return for sentinel is insufficient to tell if mapping
     264             :    failed.  That is, assuming sentinel is a location that could never
     265             :    happen on success:
     266             : 
     267             :      sz!=0 and ret!=sentinel -> success
     268             :      sz!=0 and ret==sentinel -> failure
     269             :      sz==0 -> ignore ret, application specific handling
     270             : 
     271             :    With ~O(2) extra fast branchless instructions, the below could be
     272             :    tweaked in the sz==0 case to return NULL or return a non-NULL
     273             :    sentinel value.  What is most optimal practically depends on how
     274             :    empty ranges and NULL vaddr handling is defined in the application.
     275             : 
     276             :    Requires ~O(10) fast branchless assembly instructions with 2 L1 cache
     277             :    hit loads and pretty good ILP.
     278             : 
     279             :    fd_vm_mem_haddr_fast is when the vaddr is for use when it is already
     280             :    known that the vaddr region has a valid mapping.
     281             : 
     282             :    These assumptions don't hold if direct mapping is enabled since input
     283             :    region lookups become O(log(n)). */
     284             : 
     285             : 
     286             : /* fd_vm_get_input_mem_region_idx returns the index into the input memory
     287             :    region array with the largest region offset that is <= the offset that
     288             :    is passed in.  This function makes NO guarantees about the input being
     289             :    a valid input region offset; the caller is responsible for safely handling
     290             :    it. */
     291             : static inline ulong
     292         348 : fd_vm_get_input_mem_region_idx( fd_vm_t const * vm, ulong offset ) {
     293         348 :   uint left  = 0U;
     294         348 :   uint right = vm->input_mem_regions_cnt - 1U;
     295         348 :   uint mid   = 0U;
     296             : 
     297         576 :   while( left<right ) {
     298         228 :     mid = (left+right) / 2U;
     299         228 :     if( offset>=vm->input_mem_regions[ mid ].vaddr_offset+vm->input_mem_regions[ mid ].address_space_reserved ) {
     300          51 :       left = mid + 1U;
     301         177 :     } else {
     302         177 :       right = mid;
     303         177 :     }
     304         228 :   }
     305         348 :   return left;
     306         348 : }
     307             : 
     308             : /* If the region is an account, handle the resizing logic. This logic
     309             :    corresponds to
     310             :    solana_transaction_context::TransactionContext::access_violation_handler
     311             : 
     312             :    https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L510-L581 */
     313             : static inline void
     314             : fd_vm_handle_input_mem_region_oob( fd_vm_t const * vm,
     315             :                                    ulong           offset,
     316             :                                    ulong           sz,
     317             :                                    ulong           region_idx,
     318         120 :                                    uchar           write ) {
     319             :   /* If stricter_abi_and_runtime_constraints is not enabled, we don't need to
     320             :      do anything */
     321         120 :   if( FD_UNLIKELY( !vm->stricter_abi_and_runtime_constraints ) ) {
     322         102 :     return;
     323         102 :   }
     324             : 
     325             :   /* If the access is not a write, we don't need to do anything
     326             :      https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L523-L525 */
     327          18 :   if( FD_UNLIKELY( !write ) ) {
     328           0 :     return;
     329           0 :   }
     330             : 
     331          18 :   fd_vm_input_region_t * region = &vm->input_mem_regions[ region_idx ];
     332             :   /* If the region is not writable, we don't need to do anything
     333             :      https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L526-L529 */
     334          18 :   if( FD_UNLIKELY( !region->is_writable ) ) {
     335           0 :     return;
     336           0 :   }
     337             : 
     338             :   /* Calculate the requested length
     339             :      https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L532-L535 */
     340          18 :   ulong requested_len = fd_ulong_sat_sub( fd_ulong_sat_add( offset, sz ), region->vaddr_offset );
     341          18 :   if( FD_UNLIKELY( requested_len > region->address_space_reserved ) ) {
     342          18 :     return;
     343          18 :   }
     344             : 
     345             :   /* Calculate the remaining allowed growth
     346             :      https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L549-L551 */
     347           0 :   ulong remaining_allowed_growth = fd_ulong_sat_sub(
     348           0 :     FD_MAX_ACCOUNT_DATA_GROWTH_PER_TRANSACTION,
     349           0 :     vm->instr_ctx->txn_out->details.accounts_resize_delta );
     350             : 
     351             :   /* If the requested length is greater than the size of the region,
     352             :      resize the region
     353             :      https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L553-L571 */
     354           0 :   if( FD_UNLIKELY( requested_len > region->region_sz ) ) {
     355             :     /* Calculate the new region size
     356             :        https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L558-L560 */
     357           0 :     ulong new_region_sz = fd_ulong_min(
     358           0 :       fd_ulong_min( region->address_space_reserved, FD_RUNTIME_ACC_SZ_MAX ),
     359           0 :       fd_ulong_sat_add( region->region_sz, remaining_allowed_growth ) );
     360             : 
     361             :     /* Resize the account and the region
     362             :        https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L569-L570 */
     363           0 :     if( FD_UNLIKELY( new_region_sz > region->region_sz ) ) {
     364           0 :       vm->instr_ctx->txn_out->details.accounts_resize_delta = fd_ulong_sat_sub(
     365           0 :         fd_ulong_sat_add( vm->instr_ctx->txn_out->details.accounts_resize_delta, new_region_sz ),
     366           0 :         region->region_sz );
     367             : 
     368           0 :       fd_account_meta_resize( vm->acc_region_metas[ region->acc_region_meta_idx ].meta, new_region_sz );
     369           0 :       region->region_sz = (uint)new_region_sz;
     370           0 :     }
     371           0 :   }
     372           0 : }
     373             : 
     374             : /* fd_vm_find_input_mem_region returns the translated haddr for a given
     375             :    offset into the input region.  If an offset/sz is invalid or if an
     376             :    illegal write is performed, the sentinel value is returned. If the offset
     377             :    provided is too large, it will choose the upper-most region as the
     378             :    region_idx. However, it will get caught for being too large of an access
     379             :    in the multi-region checks. */
     380             : static inline ulong
     381             : fd_vm_find_input_mem_region( fd_vm_t const * vm,
     382             :                              ulong           offset,
     383             :                              ulong           sz,
     384             :                              uchar           write,
     385         348 :                              ulong           sentinel ) {
     386         348 :   if( FD_UNLIKELY( vm->input_mem_regions_cnt==0 ) ) {
     387           0 :     return sentinel; /* Access is too large */
     388           0 :   }
     389             : 
     390             :   /* Binary search to find the correct memory region.  If direct mapping is not
     391             :      enabled, then there is only 1 memory region which spans the input region. */
     392         348 :   ulong region_idx = fd_vm_get_input_mem_region_idx( vm, offset );
     393         348 :   if( FD_UNLIKELY( region_idx>=vm->input_mem_regions_cnt ) ) {
     394           0 :     return sentinel; /* Region not found */
     395           0 :   }
     396             : 
     397         348 :   ulong bytes_in_region = fd_ulong_sat_sub( vm->input_mem_regions[ region_idx ].region_sz,
     398         348 :                                             fd_ulong_sat_sub( offset, vm->input_mem_regions[ region_idx ].vaddr_offset ) );
     399             : 
     400             :   /* If the access is out of bounds, invoke the callback to handle the out of bounds access.
     401             :      This potentially resizes the region if necessary. */
     402         348 :   if( FD_UNLIKELY( sz>bytes_in_region ) ) {
     403         120 :     fd_vm_handle_input_mem_region_oob( vm, offset, sz, region_idx, write );
     404         120 :   }
     405             : 
     406             :   /* After potentially resizing, re-check the bounds */
     407         348 :   bytes_in_region = fd_ulong_sat_sub( vm->input_mem_regions[ region_idx ].region_sz,
     408         348 :                                       fd_ulong_sat_sub( offset, vm->input_mem_regions[ region_idx ].vaddr_offset ) );
     409             :   /* If the access is still out of bounds, return the sentinel */
     410         348 :   if( FD_UNLIKELY( sz>bytes_in_region ) ) {
     411         120 :     return sentinel;
     412         120 :   }
     413             : 
     414         228 :   if( FD_UNLIKELY( write && vm->input_mem_regions[ region_idx ].is_writable==0U ) ) {
     415           0 :     return sentinel; /* Illegal write */
     416           0 :   }
     417             : 
     418         228 :   ulong start_region_idx = region_idx;
     419             : 
     420         228 :   ulong adjusted_haddr = vm->input_mem_regions[ start_region_idx ].haddr + offset - vm->input_mem_regions[ start_region_idx ].vaddr_offset;
     421         228 :   return adjusted_haddr;
     422         228 : }
     423             : 
     424             : 
     425             : static inline ulong
     426             : fd_vm_mem_haddr( fd_vm_t const * vm,
     427             :                  ulong           vaddr,
     428             :                  ulong           sz,
     429             :                  ulong const *   vm_region_haddr, /* indexed [0,6) */
     430             :                  uint  const *   vm_region_sz,    /* indexed [0,6) */
     431             :                  uchar           write,           /* 1 if the access is a write, 0 if it is a read */
     432        3441 :                  ulong           sentinel ) {
     433        3441 :   ulong region = FD_VADDR_TO_REGION( vaddr );
     434        3441 :   ulong offset = vaddr & FD_VM_OFFSET_MASK;
     435             : 
     436             :   /* Stack memory regions have 4kB unmapped "gaps" in-between each frame, which only exist if...
     437             :           - dynamic stack frames are not enabled (!(SBPF version >= SBPF_V1))
     438             :      https://github.com/anza-xyz/agave/blob/v2.2.12/programs/bpf_loader/src/lib.rs#L344-L351
     439             :     */
     440        3441 :   if( FD_UNLIKELY( region==FD_VM_STACK_REGION &&
     441        3441 :                    !fd_sbpf_dynamic_stack_frames_enabled( vm->sbpf_version ) ) ) {
     442             :     /* If an access starts in a gap region, that is an access violation */
     443           0 :     if( FD_UNLIKELY( !!(vaddr & 0x1000) ) ) {
     444           0 :       return sentinel;
     445           0 :     }
     446             : 
     447             :     /* To account for the fact that we have gaps in the virtual address space but not in the
     448             :        physical address space, we need to subtract from the offset the size of all the virtual
     449             :        gap frames underneath it.
     450             : 
     451             :        https://github.com/solana-labs/rbpf/blob/b503a1867a9cfa13f93b4d99679a17fe219831de/src/memory_region.rs#L147-L149 */
     452           0 :     ulong gap_mask = 0xFFFFFFFFFFFFF000;
     453           0 :     offset = ( ( offset & gap_mask ) >> 1 ) | ( offset & ~gap_mask );
     454           0 :   }
     455             : 
     456        3441 :   ulong region_sz = (ulong)vm_region_sz[ region ];
     457        3441 :   ulong sz_max    = region_sz - fd_ulong_min( offset, region_sz );
     458             : 
     459             :   /* If the region is an account, handle the resizing logic. This logic corresponds to
     460             :      solana_transaction_context::TransactionContext::access_violation_handler
     461             : 
     462             :      https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L510-L581 */
     463        3441 :   if( region==FD_VM_INPUT_REGION ) {
     464         348 :     return fd_vm_find_input_mem_region( vm, offset, sz, write, sentinel );
     465         348 :   }
     466             : 
     467             : # ifdef FD_VM_INTERP_MEM_TRACING_ENABLED
     468             :   if ( FD_LIKELY( sz<=sz_max ) ) {
     469             :     fd_vm_trace_event_mem( vm->trace, write, vaddr, sz, vm_region_haddr[ region ] + offset );
     470             :   }
     471             : # endif
     472        3093 :   return fd_ulong_if( sz<=sz_max, vm_region_haddr[ region ] + offset, sentinel );
     473        3441 : }
     474             : 
     475             : static inline ulong
     476             : fd_vm_mem_haddr_fast( fd_vm_t const * vm,
     477             :                       ulong           vaddr,
     478           0 :                       ulong   const * vm_region_haddr ) { /* indexed [0,6) */
     479           0 :   ulong region   = FD_VADDR_TO_REGION( vaddr );
     480           0 :   ulong offset   = vaddr & FD_VM_OFFSET_MASK;
     481           0 :   if( FD_UNLIKELY( region==FD_VM_INPUT_REGION ) ) {
     482           0 :     return fd_vm_find_input_mem_region( vm, offset, 1UL, 0, 0UL );
     483           0 :   }
     484           0 :   return vm_region_haddr[ region ] + offset;
     485           0 : }
     486             : 
     487          54 : FD_FN_PURE static inline ulong fd_vm_mem_ld_1( ulong haddr ) {
     488          54 :   return (ulong)*(uchar const *)haddr;
     489          54 : }
     490             : 
     491          60 : FD_FN_PURE static inline ulong fd_vm_mem_ld_2( ulong haddr ) {
     492          60 :   ushort t;
     493          60 :   memcpy( &t, (void const *)haddr, sizeof(ushort) );
     494          60 :   return (ulong)t;
     495          60 : }
     496             : 
     497          60 : FD_FN_PURE static inline ulong fd_vm_mem_ld_4( ulong haddr ) {
     498          60 :   uint t;
     499          60 :   memcpy( &t, (void const *)haddr, sizeof(uint) );
     500          60 :   return (ulong)t;
     501          60 : }
     502             : 
     503          42 : FD_FN_PURE static inline ulong fd_vm_mem_ld_8( ulong haddr ) {
     504          42 :   ulong t;
     505          42 :   memcpy( &t, (void const *)haddr, sizeof(ulong) );
     506          42 :   return t;
     507          42 : }
     508             : 
     509           6 : static inline void fd_vm_mem_st_1( ulong haddr, uchar val ) {
     510           6 :   *(uchar *)haddr = val;
     511           6 : }
     512             : 
     513             : static inline void fd_vm_mem_st_2( ulong  haddr,
     514           6 :                                    ushort val ) {
     515           6 :   memcpy( (void *)haddr, &val, sizeof(ushort) );
     516           6 : }
     517             : 
     518             : static inline void fd_vm_mem_st_4( ulong haddr,
     519           6 :                                    uint  val ) {
     520           6 :   memcpy( (void *)haddr, &val, sizeof(uint) );
     521           6 : }
     522             : 
     523             : static inline void fd_vm_mem_st_8( ulong haddr,
     524           6 :                                    ulong val ) {
     525           6 :   memcpy( (void *)haddr, &val, sizeof(ulong) );
     526           6 : }
     527             : 
     528             : FD_PROTOTYPES_END
     529             : 
     530             : #endif /* HEADER_fd_src_flamenco_vm_fd_vm_private_h */

Generated by: LCOV version 1.14