LCOV - code coverage report
Current view: top level - flamenco/vm/syscall - fd_vm_syscall_util.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 281 404 69.6 %
Date: 2025-03-20 12:08:36 Functions: 8 13 61.5 %

          Line data    Source code
       1             : #include "fd_vm_syscall.h"
       2             : 
       3             : #include "../../../ballet/base64/fd_base64.h"
       4             : #include "../../../ballet/utf8/fd_utf8.h"
       5             : #include "../../runtime/sysvar/fd_sysvar.h"
       6             : #include "../../runtime/sysvar/fd_sysvar_clock.h"
       7             : #include "../../runtime/sysvar/fd_sysvar_epoch_schedule.h"
       8             : #include "../../runtime/sysvar/fd_sysvar_fees.h"
       9             : #include "../../runtime/context/fd_exec_txn_ctx.h"
      10             : #include "../../runtime/context/fd_exec_instr_ctx.h"
      11             : 
      12             : int
      13             : fd_vm_syscall_abort( /**/            void *  _vm,
      14             :                      FD_PARAM_UNUSED ulong   r1,
      15             :                      FD_PARAM_UNUSED ulong   r2,
      16             :                      FD_PARAM_UNUSED ulong   r3,
      17             :                      FD_PARAM_UNUSED ulong   r4,
      18             :                      FD_PARAM_UNUSED ulong   r5,
      19           0 :                      FD_PARAM_UNUSED ulong * _ret ) {
      20             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/mod.rs#L630 */
      21           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
      22           0 :   FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_ABORT );
      23           0 :   return FD_VM_SYSCALL_ERR_ABORT;
      24           0 : }
      25             : 
      26             : /* FD_TRANSLATE_STRING returns a read only pointer to the host address of
      27             :    a valid utf8 string, or it errors.
      28             : 
      29             :    Analogous of Agave's translate_string_and_do().
      30             :    https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/mod.rs#L601
      31             : 
      32             :    As of v0.2.6, the only two usages are in syscall panic and syscall log. */
      33           9 : #define FD_TRANSLATE_STRING( vm, vaddr, msg_sz ) (__extension__({                          \
      34           9 :     char const * msg = FD_VM_MEM_SLICE_HADDR_LD( vm, vaddr, FD_VM_ALIGN_RUST_U8, msg_sz ); \
      35           9 :     if( FD_UNLIKELY( !fd_utf8_verify( msg, msg_sz ) ) ) {                                  \
      36           0 :       FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_STRING );                   \
      37           0 :       return FD_VM_SYSCALL_ERR_INVALID_STRING;                                             \
      38           0 :     }                                                                                      \
      39           9 :     msg;                                                                                   \
      40           9 : }))
      41             : 
      42             : int
      43             : fd_vm_syscall_sol_panic( /**/            void *  _vm,
      44             :                          /**/            ulong   file_vaddr,
      45             :                          /**/            ulong   file_sz,
      46             :                          /**/            ulong   line,
      47             :                          /**/            ulong   column,
      48             :                          FD_PARAM_UNUSED ulong   r5,
      49           0 :                          FD_PARAM_UNUSED ulong * _ret ) {
      50           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
      51             : 
      52             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/mod.rs#L637
      53             : 
      54             :      Note: this syscall is not used by the Rust SDK, only by the C SDK.
      55             :      Rust transforms `panic!()` into a log, followed by an abort.
      56             :      It's unclear if this syscall actually makes any sense... */
      57           0 :   FD_VM_CU_UPDATE( vm, file_sz );
      58             : 
      59             :   /* Validate string */
      60           0 :   FD_TRANSLATE_STRING( vm, file_vaddr, file_sz );
      61             : 
      62             :   /* Note: we truncate the log, ignoring file, line, column.
      63             :      As mentioned above, it's unclear if anyone is even using this syscall,
      64             :      so dealing with the complexity of Agave's log is a waste of time. */
      65           0 :   (void)line;
      66           0 :   (void)column;
      67             : 
      68           0 :   FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_PANIC );
      69           0 :   return FD_VM_SYSCALL_ERR_PANIC;
      70           0 : }
      71             : 
      72             : int
      73             : fd_vm_syscall_sol_log( /**/            void *  _vm,
      74             :                        /**/            ulong   msg_vaddr,
      75             :                        /**/            ulong   msg_sz,
      76             :                        FD_PARAM_UNUSED ulong   r2,
      77             :                        FD_PARAM_UNUSED ulong   r3,
      78             :                        FD_PARAM_UNUSED ulong   r4,
      79           9 :                        /**/            ulong * _ret ) {
      80           9 :   fd_vm_t * vm = (fd_vm_t *)_vm;
      81             : 
      82             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L5 */
      83             : 
      84           9 :   FD_VM_CU_UPDATE( vm, fd_ulong_max( msg_sz, FD_VM_SYSCALL_BASE_COST ) );
      85             : 
      86             :   /* Note: when msg_sz==0, msg can be undefined. fd_log_collector_program_log() handles it.
      87             :      FIXME: Macro invocation in function invocation? */
      88           9 :   fd_log_collector_program_log( vm->instr_ctx, FD_TRANSLATE_STRING( vm, msg_vaddr, msg_sz ), msg_sz );
      89             : 
      90           0 :   *_ret = 0UL;
      91           9 :   return FD_VM_SUCCESS;
      92          18 : }
      93             : 
      94             : int
      95             : fd_vm_syscall_sol_log_64( void *  _vm,
      96             :                           ulong   r1,
      97             :                           ulong   r2,
      98             :                           ulong   r3,
      99             :                           ulong   r4,
     100             :                           ulong   r5,
     101           3 :                           ulong * _ret ) {
     102           3 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     103             : 
     104             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L37 */
     105             : 
     106           3 :   FD_VM_CU_UPDATE( vm, FD_VM_LOG_64_UNITS );
     107             : 
     108             :   /* Max msg_sz: 46 - 15 + 16*5 = 111 < 127 => we can use printf */
     109           0 :   fd_log_collector_printf_dangerous_max_127( vm->instr_ctx,
     110           3 :     "Program log: 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx", r1, r2, r3, r4, r5 );
     111             : 
     112           3 :   *_ret = 0UL;
     113           3 :   return FD_VM_SUCCESS;
     114           3 : }
     115             : 
     116             : int
     117             : fd_vm_syscall_sol_log_compute_units( /**/            void *  _vm,
     118             :                                      FD_PARAM_UNUSED ulong   r1,
     119             :                                      FD_PARAM_UNUSED ulong   r2,
     120             :                                      FD_PARAM_UNUSED ulong   r3,
     121             :                                      FD_PARAM_UNUSED ulong   r4,
     122             :                                      FD_PARAM_UNUSED ulong   r5,
     123           0 :                                      /**/            ulong * _ret ) {
     124           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     125             : 
     126             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L60 */
     127             : 
     128           0 :   FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
     129             : 
     130             :   /* Max msg_sz: 40 - 3 + 20 = 57 < 127 => we can use printf */
     131           0 :   fd_log_collector_printf_dangerous_max_127( vm->instr_ctx,
     132           0 :     "Program consumption: %lu units remaining", vm->cu );
     133             : 
     134           0 :   *_ret = 0UL;
     135           0 :   return FD_VM_SUCCESS;
     136           0 : }
     137             : 
     138             : int
     139             : fd_vm_syscall_sol_log_pubkey( /**/            void *  _vm,
     140             :                               /**/            ulong   pubkey_vaddr,
     141             :                               FD_PARAM_UNUSED ulong   r2,
     142             :                               FD_PARAM_UNUSED ulong   r3,
     143             :                               FD_PARAM_UNUSED ulong   r4,
     144             :                               FD_PARAM_UNUSED ulong   r5,
     145           0 :                               /**/            ulong * _ret ) {
     146           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     147             : 
     148             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L84 */
     149             : 
     150           0 :   FD_VM_CU_UPDATE( vm, FD_VM_LOG_PUBKEY_UNITS );
     151             : 
     152           0 :   void const * pubkey = FD_VM_MEM_HADDR_LD( vm, pubkey_vaddr, FD_VM_ALIGN_RUST_PUBKEY, sizeof(fd_pubkey_t) );
     153             : 
     154           0 :   char msg[ FD_BASE58_ENCODED_32_SZ ]; ulong msg_sz;
     155           0 :   if( FD_UNLIKELY( fd_base58_encode_32( pubkey, &msg_sz, msg )==NULL ) ) {
     156           0 :     return FD_VM_SYSCALL_ERR_INVALID_STRING;
     157           0 :   }
     158             : 
     159           0 :   fd_log_collector_program_log( vm->instr_ctx, msg, msg_sz );
     160             : 
     161           0 :   *_ret = 0UL;
     162           0 :   return FD_VM_SUCCESS;
     163           0 : }
     164             : 
     165             : int
     166             : fd_vm_syscall_sol_log_data( /**/            void *  _vm,
     167             :                             /**/            ulong   slice_vaddr,
     168             :                             /**/            ulong   slice_cnt,
     169             :                             FD_PARAM_UNUSED ulong   r3,
     170             :                             FD_PARAM_UNUSED ulong   r4,
     171             :                             FD_PARAM_UNUSED ulong   r5,
     172           3 :                             /**/            ulong * _ret ) {
     173           3 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     174             : 
     175             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L109
     176             : 
     177             :      Note: this is implemented following Agave's perverse behavior.
     178             :      We need to loop the slice multiple times to match the exact error,
     179             :      first compute budget, then memory mapping.
     180             :      And finally we can loop to log. */
     181             : 
     182             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L121 */
     183             : 
     184           3 :   FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
     185             : 
     186             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L123-L128 */
     187             : 
     188           6 :   fd_vm_vec_t const * slice = (fd_vm_vec_t const *)FD_VM_MEM_SLICE_HADDR_LD( vm, slice_vaddr, FD_VM_ALIGN_RUST_SLICE_U8_REF,
     189           6 :     fd_ulong_sat_mul( slice_cnt, sizeof(fd_vm_vec_t) ) );
     190             : 
     191             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L130-L135 */
     192             : 
     193           3 :   FD_VM_CU_UPDATE( vm, fd_ulong_sat_mul( FD_VM_SYSCALL_BASE_COST, slice_cnt ) );
     194             : 
     195             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L136-L141 */
     196             : 
     197          18 :   for( ulong i=0UL; i<slice_cnt; i++ ) {
     198          15 :     FD_VM_CU_UPDATE( vm, slice[i].len );
     199          15 :   }
     200             : 
     201             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L145-L152 */
     202             : 
     203           3 :   ulong msg_sz = 14UL; /* "Program data: ", with space */
     204          18 :   for( ulong i=0UL; i<slice_cnt; i++ ) {
     205          15 :     ulong cur_len = slice[i].len;
     206             :     /* This fails the syscall in case of memory mapping issues */
     207          30 :     FD_VM_MEM_SLICE_HADDR_LD( vm, slice[i].addr, FD_VM_ALIGN_RUST_U8, cur_len );
     208             :     /* Every buffer will be base64 encoded + space separated */
     209           0 :     msg_sz += (slice[i].len + 2)/3*4 + (i > 0);
     210          30 :   }
     211             : 
     212             :   /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L156 */
     213             : 
     214           3 :   char msg[ FD_LOG_COLLECTOR_MAX ];
     215           3 :   ulong bytes_written = fd_log_collector_check_and_truncate( &vm->instr_ctx->txn_ctx->log_collector, msg_sz );
     216           3 :   if( FD_LIKELY( bytes_written < ULONG_MAX ) ) {
     217           3 :     fd_memcpy( msg, "Program data: ", 14 );
     218           3 :     char * buf = msg + 14;
     219             : 
     220          18 :     for( ulong i=0UL; i<slice_cnt; i++ ) {
     221          15 :       ulong cur_len = slice[i].len;
     222          30 :       void const * bytes = FD_VM_MEM_SLICE_HADDR_LD( vm, slice[i].addr, FD_VM_ALIGN_RUST_U8, cur_len );
     223             : 
     224          15 :       if( i ) { *buf = ' '; ++buf; } /* skip first */
     225          30 :       buf += fd_base64_encode( buf, bytes, cur_len );
     226          30 :     }
     227           3 :     FD_TEST( (ulong)(buf-msg)==msg_sz );
     228             : 
     229           3 :     fd_log_collector_msg( vm->instr_ctx, msg, msg_sz );
     230           3 :   }
     231             : 
     232           3 :   *_ret = 0;
     233           3 :   return FD_VM_SUCCESS;
     234           3 : }
     235             : 
     236             : int
     237             : fd_vm_syscall_sol_alloc_free( /**/            void *  _vm,
     238             :                               /**/            ulong   sz,
     239             :                               /**/            ulong   free_vaddr,
     240             :                               FD_PARAM_UNUSED ulong   r3,
     241             :                               FD_PARAM_UNUSED ulong   r4,
     242             :                               FD_PARAM_UNUSED ulong   r5,
     243           0 :                               /**/            ulong * _ret ) {
     244           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     245             : 
     246             :   /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L666 */
     247             : 
     248             :   /* This syscall is ... uh ... problematic.  But the community has
     249             :      already recognized this and deprecated it:
     250             : 
     251             :      https://github.com/solana-labs/solana/blob/v1.17.23/sdk/src/feature_set.rs#L846
     252             : 
     253             :      Unfortunately, old code never dies so, practically, this will need
     254             :      to be supported until the heat death of the universe.
     255             : 
     256             :      The most serious issue is that there is nothing to stop VM code
     257             :      making a decision based on the _location_ of the returned
     258             :      allocation.  If different validator implementations use different
     259             :      allocator algorithms, though each implementation would behave
     260             :      functionally correct in isolation, the VM code that uses it would
     261             :      actually break consensus.
     262             : 
     263             :      As a result, every validator needs to use a bit-for-bit identical
     264             :      allocation algorithm.  Fortunately, Solana is just using a basic
     265             :      bump allocator:
     266             : 
     267             :      https://github.com/solana-labs/solana/blob/v1.17.23/program-runtime/src/invoke_context.rs#L122-L148
     268             : 
     269             :      vm->heap_{sz,max} and the below replicate this exactly.
     270             : 
     271             :      Another major issue is that this alloc doesn't always conform
     272             :      typical malloc/free semantics (e.g. C/C++ requires malloc to have
     273             :      an alignment safe for primitive types ... 8 for the Solana machine
     274             :      model).  This is clearly to support backward compat with older VM
     275             :      code (though ideally a malloc syscall should have behaved like ...
     276             :      well ... malloc from day 1).  So the alignment behavior below is a
     277             :      bug-for-bug replication of that:
     278             : 
     279             :      https://github.com/solana-labs/solana/blob/v1.17.23/programs/bpf_loader/src/syscalls/mod.rs#L645-L681
     280             :      https://github.com/solana-labs/solana/blob/v1.17.23/sdk/program/src/entrypoint.rs#L265-L266
     281             : 
     282             :      More generally and already ranted about elsewhere, any code that
     283             :      uses malloc/free style dynamic allocation is inherently broken.  So
     284             :      this syscall should have never existed in the first place ... it
     285             :      just feeds the trolls.  The above is just additional implementation
     286             :      horror because people consistent think malloc/free is much simpler
     287             :      than it actually is.  This is also an example of how quickly
     288             :      mistakes fossilize and become a thorn-in-the-side forever.
     289             : 
     290             :      IMPORTANT SAFETY TIP!  heap_start must be non zero and both
     291             :      heap_start and heap_end should have an alignment of at least 8.
     292             :      This existing runtime policies around heap implicitly satisfy this.
     293             : 
     294             :      IMPORTANT SAFETY TIP!  The specification for Rust's align_offset
     295             :      doesn't seem to provide a strong guarantee that it will return the
     296             :      minimal positive offset necessary to align pointers.  It is
     297             :      possible for a "conforming" Rust compiler to break consensus by
     298             :      using a different align_offset implementation that aligned pointer
     299             :      between different compilations of the Solana validator and the
     300             :      below. */
     301             : 
     302             :   /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L676-L680 */
     303             : 
     304           0 :   ulong align = fd_vm_is_check_align_enabled( vm ) ? 8UL : FD_VM_ALIGN_RUST_U8;
     305             : 
     306             :   /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L681-L683
     307             :      Nothing to do. This section can't error, see:
     308             :      https://doc.rust-lang.org/1.81.0/src/core/alloc/layout.rs.html#70
     309             :      https://doc.rust-lang.org/1.81.0/src/core/alloc/layout.rs.html#100 */
     310             : 
     311             : 
     312             :   /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L684
     313             :      Nothing to do.
     314             :      TODO: unclear if it throw InstructionError::CallDepth
     315             :      https://github.com/anza-xyz/agave/blob/v2.0.8/program-runtime/src/invoke_context.rs#L662 */
     316             : 
     317             :   /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L685-L693 */
     318             : 
     319             :   /* Non-zero free address implies that this is a free() call.  Since
     320             :      this is a bump allocator, free is a no-op. */
     321           0 :   if( FD_UNLIKELY( free_vaddr ) ) {
     322           0 :     *_ret = 0UL;
     323           0 :     return FD_VM_SUCCESS;
     324           0 :   }
     325             : 
     326             : 
     327           0 :   ulong heap_sz    = fd_ulong_align_up( vm->heap_sz, align                           );
     328           0 :   ulong heap_vaddr = fd_ulong_sat_add ( heap_sz,     FD_VM_MEM_MAP_HEAP_REGION_START );
     329           0 :   /**/  heap_sz    = fd_ulong_sat_add ( heap_sz,     sz                              );
     330             : 
     331           0 :   if( FD_UNLIKELY( heap_sz > vm->heap_max ) ) { /* Not enough free memory */
     332           0 :     *_ret = 0UL;
     333           0 :     return FD_VM_SUCCESS;
     334           0 :   }
     335             : 
     336           0 :   vm->heap_sz = heap_sz;
     337             : 
     338           0 :   *_ret = heap_vaddr;
     339           0 :   return FD_VM_SUCCESS;
     340           0 : }
     341             : 
     342             : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mem_ops.rs#L145 */
     343             : int
     344             : fd_vm_memmove( fd_vm_t * vm,
     345             :                ulong     dst_vaddr,
     346             :                ulong     src_vaddr,
     347          66 :                ulong     sz ) {
     348          66 :   if( FD_UNLIKELY( !sz ) ) {
     349           0 :     return FD_VM_SUCCESS;
     350           0 :   }
     351             : 
     352          66 :   if( !vm->direct_mapping ) {
     353          24 :     void *       dst = FD_VM_MEM_HADDR_ST( vm, dst_vaddr, FD_VM_ALIGN_RUST_U8, sz );
     354          48 :     void const * src = FD_VM_MEM_HADDR_LD( vm, src_vaddr, FD_VM_ALIGN_RUST_U8, sz );
     355           0 :     memmove( dst, src, sz );
     356          48 :   } else {
     357             :     /* If the src and dst vaddrs overlap and src_vaddr < dst_vaddr, Agave iterates through input regions backwards
     358             :        to maintain correct memmove behavior for overlapping cases. Although this logic should only apply to the src and dst
     359             :        vaddrs being in the input data region (since that is the only possible case you could have overlapping, chunked-up memmoves),
     360             :        Agave will iterate backwards in ANY region. If it eventually reaches the end of a region after iterating backwards and
     361             :        hits an access violation, the bytes from [region_begin, start_vaddr] will still be written to, causing fuzzing mismatches.
     362             :        In this case, if we didn't have the reverse flag, we would have thrown an access violation before any bytes were copied.
     363             :        The same logic applies to memmoves that go past the high end of a region - reverse iteration logic would throw an access
     364             :        violation before any bytes were copied, while the current logic would copy the bytes until the end of the region.
     365             :        https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mem_ops.rs#L184 */
     366          42 :     uchar reverse = !!( dst_vaddr >= src_vaddr && dst_vaddr - src_vaddr < sz );
     367             : 
     368             :     /* In reverse calculations, start from the rightmost vaddr that will be accessed (note the - 1). */
     369          42 :     ulong dst_vaddr_begin = reverse ? fd_ulong_sat_add( dst_vaddr, sz - 1UL ) : dst_vaddr;
     370          42 :     ulong src_vaddr_begin = reverse ? fd_ulong_sat_add( src_vaddr, sz - 1UL ) : src_vaddr;
     371             : 
     372             :     /* Find the correct src and dst haddrs to start operating from. If the src or dst vaddrs
     373             :        belong to the input data region (4), keep track of region statistics to memmove in chunks. */
     374          42 :     ulong   dst_region                  = FD_VADDR_TO_REGION( dst_vaddr_begin );
     375          42 :     uchar   dst_is_input_mem_region     = ( dst_region==FD_VM_INPUT_REGION );
     376          42 :     ulong   dst_offset                  = dst_vaddr_begin & FD_VM_OFFSET_MASK;
     377          42 :     ulong   dst_region_idx              = 0UL;
     378          42 :     ulong   dst_bytes_rem_in_cur_region;
     379          42 :     uchar * dst_haddr;
     380          42 :     if( dst_is_input_mem_region ) {
     381          18 :       FD_VM_MEM_HADDR_AND_REGION_IDX_FROM_INPUT_REGION_CHECKED( vm, dst_offset, dst_region_idx, dst_haddr );
     382          18 :       if( FD_UNLIKELY( !vm->input_mem_regions[ dst_region_idx ].is_writable ) ) {
     383           0 :         FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
     384           0 :         return FD_VM_SYSCALL_ERR_SEGFAULT;
     385           0 :       }
     386          18 :       if( FD_UNLIKELY( reverse ) ) {
     387             :         /* Bytes remaining between region begin and current position (+ 1 for inclusive region beginning). */
     388           6 :         dst_bytes_rem_in_cur_region = fd_ulong_sat_sub( dst_offset + 1UL, vm->input_mem_regions[ dst_region_idx ].vaddr_offset );
     389          12 :       } else {
     390             :         /* Bytes remaining between current position and region end. */
     391          12 :         dst_bytes_rem_in_cur_region = fd_ulong_sat_sub( vm->input_mem_regions[ dst_region_idx ].region_sz, ( dst_offset - vm->input_mem_regions[ dst_region_idx ].vaddr_offset ) );
     392          12 :       }
     393          24 :     } else {
     394          24 :       dst_haddr = (uchar*)FD_VM_MEM_HADDR_ST_NO_SZ_CHECK( vm, dst_vaddr_begin, FD_VM_ALIGN_RUST_U8 );
     395             : 
     396          18 :       if( FD_UNLIKELY( reverse ) ) {
     397             :         /* Bytes remaining is minimum of the offset from the beginning of the current
     398             :            region (+1 for inclusive region beginning) and the number of storable bytes in the region. */
     399           3 :         dst_bytes_rem_in_cur_region = fd_ulong_min( vm->region_st_sz[ dst_region ], dst_offset + 1UL );
     400             : 
     401          15 :       } else {
     402             :         /* Bytes remaining is the number of writable bytes left in the region */
     403          15 :         dst_bytes_rem_in_cur_region = fd_ulong_sat_sub( vm->region_st_sz[ dst_region ], dst_offset );
     404          15 :       }
     405          18 :     }
     406             : 
     407             :     /* Logic for src vaddr translation is similar to above excluding any writable checks. */
     408          36 :     ulong   src_region                  = FD_VADDR_TO_REGION( src_vaddr_begin );
     409          36 :     uchar   src_is_input_mem_region     = ( src_region==FD_VM_INPUT_REGION );
     410          36 :     ulong   src_offset                  = src_vaddr_begin & FD_VM_OFFSET_MASK;
     411          36 :     ulong   src_region_idx              = 0UL;
     412          36 :     ulong   src_bytes_rem_in_cur_region;
     413          36 :     uchar * src_haddr;
     414          36 :     if( src_is_input_mem_region ) {
     415          18 :       FD_VM_MEM_HADDR_AND_REGION_IDX_FROM_INPUT_REGION_CHECKED( vm, src_offset, src_region_idx, src_haddr );
     416          18 :       if( FD_UNLIKELY( reverse ) ) {
     417           6 :         src_bytes_rem_in_cur_region = fd_ulong_sat_sub( src_offset + 1UL, vm->input_mem_regions[ src_region_idx ].vaddr_offset );
     418          12 :       } else {
     419          12 :         src_bytes_rem_in_cur_region = fd_ulong_sat_sub( vm->input_mem_regions[ src_region_idx ].region_sz, ( src_offset - vm->input_mem_regions[ src_region_idx ].vaddr_offset ) );
     420          12 :       }
     421          18 :     } else {
     422          54 :       src_haddr = (uchar*)FD_VM_MEM_HADDR_LD_NO_SZ_CHECK( vm, src_vaddr_begin, FD_VM_ALIGN_RUST_U8 );
     423             : 
     424          18 :       if( FD_UNLIKELY( reverse ) ) {
     425           3 :         src_bytes_rem_in_cur_region = fd_ulong_min( vm->region_ld_sz[ src_region ], src_offset + 1UL );
     426             : 
     427          15 :       } else {
     428          15 :         src_bytes_rem_in_cur_region = fd_ulong_sat_sub( vm->region_ld_sz[ src_region ], src_offset );
     429          15 :       }
     430          54 :     }
     431             : 
     432             :     /* Short circuit: if the number of copyable bytes stays within all memory regions,
     433             :        just memmove and return. This is a majority case in mainnet, devnet, and testnet.
     434             :        Someone would have to be very crafty and clever to construct a transaction that
     435             :        deploys and invokes a custom program that does not fall into this branch. */
     436          36 :     if( FD_LIKELY( sz<=dst_bytes_rem_in_cur_region && sz<=src_bytes_rem_in_cur_region ) ) {
     437          21 :       if( FD_UNLIKELY( reverse ) ) {
     438             :         /* In the reverse iteration case, the haddrs point to the end of the region here. Since the
     439             :            above checks guarantee that there are enough bytes left in the src and dst regions to do
     440             :            a direct memmove, we can just subtract (sz-1) from the haddrs, memmove, and return. */
     441           3 :         memmove( dst_haddr - sz + 1UL, src_haddr - sz + 1UL, sz );
     442          18 :       } else {
     443             :         /* In normal iteration, the haddrs correspond to the correct starting point for the memcpy,
     444             :            so no further translation has to be done. */
     445          18 :         memmove( dst_haddr, src_haddr, sz );
     446          18 :       }
     447          21 :       return FD_VM_SUCCESS;
     448          21 :     }
     449             : 
     450             :     /* Copy over the bytes from each region in chunks. */
     451          57 :     while( sz>0UL ) {
     452             :       /* End of region case */
     453          45 :       if( FD_UNLIKELY( src_bytes_rem_in_cur_region==0UL ) ) {
     454             :         /* Same as above, except no writable checks. */
     455          30 :         if( FD_LIKELY( !reverse &&
     456          30 :                         src_is_input_mem_region &&
     457          30 :                         src_region_idx+1UL<vm->input_mem_regions_cnt ) ) {
     458          12 :           if( FD_UNLIKELY( vm->input_mem_regions[ src_region_idx+1UL ].is_acct_data != vm->input_mem_regions[ src_region_idx ].is_acct_data ) ) {
     459           0 :             FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
     460           0 :             return FD_VM_SYSCALL_ERR_SEGFAULT;
     461           0 :           }
     462          12 :           src_region_idx++;
     463          12 :           src_haddr = (uchar*)vm->input_mem_regions[ src_region_idx ].haddr;
     464          18 :         } else if( FD_LIKELY( reverse && src_region_idx>0UL ) ) {
     465          15 :           if( FD_UNLIKELY( vm->input_mem_regions[ src_region_idx-1UL ].is_acct_data != vm->input_mem_regions[ src_region_idx ].is_acct_data ) ) {
     466           0 :             FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
     467           0 :             return FD_VM_SYSCALL_ERR_SEGFAULT;
     468           0 :           }
     469          15 :           src_region_idx--;
     470          15 :           src_haddr = (uchar*)vm->input_mem_regions[ src_region_idx ].haddr + vm->input_mem_regions[ src_region_idx ].region_sz - 1UL;
     471          15 :         } else {
     472           3 :           FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
     473           3 :           return FD_VM_SYSCALL_ERR_SEGFAULT;
     474           3 :         }
     475          27 :         src_bytes_rem_in_cur_region = vm->input_mem_regions[ src_region_idx ].region_sz;
     476          27 :       }
     477          42 :       if( FD_UNLIKELY( dst_bytes_rem_in_cur_region==0UL ) ) {
     478             :         /* Only proceed if:
     479             :             - We are in the input memory region
     480             :             - There are remaining input memory regions to copy from (for both regular and reverse iteration orders)
     481             :             - The next input memory region is writable
     482             :            Fail otherwise. */
     483           9 :         if( FD_LIKELY( !reverse &&
     484           9 :                         dst_is_input_mem_region &&
     485           9 :                         dst_region_idx+1UL<vm->input_mem_regions_cnt &&
     486           9 :                         vm->input_mem_regions[ dst_region_idx+1UL ].is_writable ) ) {
     487           6 :           if( FD_UNLIKELY( vm->input_mem_regions[ dst_region_idx+1UL ].is_acct_data != vm->input_mem_regions[ dst_region_idx ].is_acct_data ) ) {
     488           0 :             FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
     489           0 :             return FD_VM_SYSCALL_ERR_SEGFAULT;
     490           0 :           }
     491             :           /* In normal iteration, we move the haddr to the beginning of the next region. */
     492           6 :           dst_region_idx++;
     493           6 :           dst_haddr = (uchar*)vm->input_mem_regions[ dst_region_idx ].haddr;
     494           6 :         } else if( FD_LIKELY( reverse &&
     495           3 :                               dst_region_idx>0UL &&
     496           3 :                               vm->input_mem_regions[ dst_region_idx-1UL ].is_writable ) ) {
     497           3 :           if( FD_UNLIKELY( vm->input_mem_regions[ dst_region_idx-1UL ].is_acct_data != vm->input_mem_regions[ dst_region_idx ].is_acct_data ) ) {
     498           0 :             FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
     499           0 :             return FD_VM_SYSCALL_ERR_SEGFAULT;
     500           0 :           }
     501             :           /* Note that when reverse iterating, we set the haddr to the END of the PREVIOUS region. */
     502           3 :           dst_region_idx--;
     503           3 :           dst_haddr = (uchar*)vm->input_mem_regions[ dst_region_idx ].haddr + vm->input_mem_regions[ dst_region_idx ].region_sz - 1UL;
     504           3 :         } else {
     505           0 :           FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
     506           0 :           return FD_VM_SYSCALL_ERR_SEGFAULT;
     507           0 :         }
     508           9 :         dst_bytes_rem_in_cur_region = vm->input_mem_regions[ dst_region_idx ].region_sz;
     509           9 :       }
     510             : 
     511             :       /* Number of bytes to operate on in this iteration is the min of:
     512             :          - number of bytes left to copy
     513             :          - bytes left in the current src region
     514             :          - bytes left in the current dst region */
     515          42 :       ulong num_bytes_to_copy = fd_ulong_min( sz, fd_ulong_min( src_bytes_rem_in_cur_region, dst_bytes_rem_in_cur_region ) );
     516          42 :       if( FD_UNLIKELY( reverse ) ) {
     517          21 :         memmove( dst_haddr - num_bytes_to_copy + 1UL, src_haddr - num_bytes_to_copy + 1UL, num_bytes_to_copy );
     518          21 :         dst_haddr -= num_bytes_to_copy;
     519          21 :         src_haddr -= num_bytes_to_copy;
     520          21 :       } else {
     521          21 :         memmove( dst_haddr, src_haddr, num_bytes_to_copy );
     522          21 :         dst_haddr += num_bytes_to_copy;
     523          21 :         src_haddr += num_bytes_to_copy;
     524          21 :       }
     525             : 
     526             :       /* Update size trackers */
     527          42 :       sz                          -= num_bytes_to_copy;
     528          42 :       src_bytes_rem_in_cur_region -= num_bytes_to_copy;
     529          42 :       dst_bytes_rem_in_cur_region -= num_bytes_to_copy;
     530          42 :     }
     531          15 :   }
     532             : 
     533          27 :   return FD_VM_SUCCESS;
     534          66 : }
     535             : 
     536             : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mem_ops.rs#L41 */
     537             : int
     538             : fd_vm_syscall_sol_memmove( /**/            void *  _vm,
     539             :                            /**/            ulong   dst_vaddr,
     540             :                            /**/            ulong   src_vaddr,
     541             :                            /**/            ulong   sz,
     542             :                            FD_PARAM_UNUSED ulong   r4,
     543             :                            FD_PARAM_UNUSED ulong   r5,
     544          36 :                            /**/            ulong * _ret ) {
     545          36 :   *_ret = 0;
     546          36 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     547             : 
     548          36 :   FD_VM_CU_MEM_OP_UPDATE( vm, sz );
     549             : 
     550             :   /* No overlap check for memmove. */
     551           0 :   return fd_vm_memmove( vm, dst_vaddr, src_vaddr, sz );
     552          36 : }
     553             : 
     554             : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mem_ops.rs#L18 */
     555             : int
     556             : fd_vm_syscall_sol_memcpy( /**/            void *  _vm,
     557             :                           /**/            ulong   dst_vaddr,
     558             :                           /**/            ulong   src_vaddr,
     559             :                           /**/            ulong   sz,
     560             :                           FD_PARAM_UNUSED ulong   r4,
     561             :                           FD_PARAM_UNUSED ulong   r5,
     562          48 :                           /**/            ulong * _ret ) {
     563          48 :   *_ret = 0;
     564          48 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     565             : 
     566          48 :   FD_VM_CU_MEM_OP_UPDATE( vm, sz );
     567             : 
     568             :   /* Exact same as memmove, except also check overlap.
     569             :      https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/mem_ops.rs#L31 */
     570          48 :   FD_VM_MEM_CHECK_NON_OVERLAPPING( vm, src_vaddr, sz, dst_vaddr, sz );
     571             : 
     572          30 :   return fd_vm_memmove( vm, dst_vaddr, src_vaddr, sz );
     573          48 : }
     574             : 
     575             : int
     576             : fd_vm_syscall_sol_memcmp( /**/            void *  _vm,
     577             :                           /**/            ulong   m0_vaddr,
     578             :                           /**/            ulong   m1_vaddr,
     579             :                           /**/            ulong   sz,
     580             :                           /**/            ulong   out_vaddr,
     581             :                           FD_PARAM_UNUSED ulong   r5,
     582          15 :                           /**/            ulong * _ret ) {
     583          15 :   *_ret = 0;
     584          15 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     585             : 
     586             :   /* https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/mem_ops.rs#L59 */
     587             : 
     588          15 :   FD_VM_CU_MEM_OP_UPDATE( vm, sz );
     589             : 
     590             :   /* Note: though this behaves like a normal C-style memcmp, we can't
     591             :      use the compilers / libc memcmp directly because the specification
     592             :      doesn't provide strong enough guarantees about the return value (it
     593             :      only promises the sign). */
     594             : 
     595          15 :   if( !vm->direct_mapping ) {
     596           0 :     uchar const * m0 = (uchar const *)FD_VM_MEM_SLICE_HADDR_LD( vm, m0_vaddr, FD_VM_ALIGN_RUST_U8, sz );
     597           0 :     uchar const * m1 = (uchar const *)FD_VM_MEM_SLICE_HADDR_LD( vm, m1_vaddr, FD_VM_ALIGN_RUST_U8, sz );
     598             : 
     599             :     /* Silly that this doesn't use r0 to return ... slower, more edge
     600             :       case, different from libc style memcmp, harder to callers to use,
     601             :       etc ... probably too late to do anything about it now ... sigh */
     602             : 
     603           0 :     void * _out = FD_VM_MEM_HADDR_ST( vm, out_vaddr, FD_VM_ALIGN_RUST_I32, 4UL );
     604             : 
     605           0 :     int out = 0;
     606           0 :     for( ulong i=0UL; i<sz; i++ ) {
     607           0 :       int i0 = (int)m0[i];
     608           0 :       int i1 = (int)m1[i];
     609           0 :       if( i0!=i1 ) {
     610           0 :         out = i0 - i1;
     611           0 :         break;
     612           0 :       }
     613           0 :     }
     614             : 
     615           0 :     fd_memcpy( _out, &out, 4UL ); /* Sigh ... see note above (and might be unaligned ... double sigh) */
     616             : 
     617           0 :     return FD_VM_SUCCESS;
     618          15 :   } else {
     619             :     /* In the case that direct mapping is enabled, the behavior for memcmps
     620             :        differ significantly from the non-dm case. The key difference is that
     621             :        invalid loads will instantly lead to errors in the non-dm case. However,
     622             :        when direct mapping is enabled, we will first try to memcmp the largest
     623             :        size valid chunk first, and will exit successfully if a difference is
     624             :        found without aborting from the VM. A chunk is defined as the largest
     625             :        valid vaddr range in both memory regions that doesn't span multiple
     626             :        regions.
     627             : 
     628             :        Example:
     629             :        fd_vm_syscall_sol_memcmp( vm, m0_addr : 0x4000, m1_vaddr : 0x2000, 0x200, ... );
     630             :        m0's region: m0_addr 0x4000 -> 0x4000 + 0x50  (region sz 0x50)
     631             :        m1's region: m1_addr 0x2000 -> 0x2000 + 0x100 (region sz 0x100)
     632             :        sz: 0x200
     633             : 
     634             :        Case 1: 0x4000 -> 0x4050 does     have the same bytes as 0x2000 -> 0x2050
     635             :        Case 2: 0x4000 -> 0x4050 does NOT have the same bytes as 0x2000 -> 0x2050
     636             : 
     637             :        Pre-DM:
     638             :        This will fail out before any bytes are compared because the memory
     639             :        translation is done first.
     640             : 
     641             :        Post-DM:
     642             :        For case 1, the memcmp will return an error and the VM will exit because
     643             :        the memcmp will eventually try to access 0x4051 which is invalid. First
     644             :        0x50 bytes are compared, but the next chunk will lead to an invalid
     645             :        access.
     646             : 
     647             :        For case 2, the memcmp will first translate the first 0x50 bytes and will
     648             :        see that the bytes are not the same. This will lead to the syscall
     649             :        exiting out successfully without detecting the access violation.
     650             : 
     651             :       https://github.com/anza-xyz/agave/blob/v2.0.10/programs/bpf_loader/src/syscalls/mem_ops.rs#L213
     652             :        */
     653             : 
     654          15 :     void * _out = FD_VM_MEM_HADDR_ST( vm, out_vaddr, FD_VM_ALIGN_RUST_I32, 4UL );
     655           0 :     int     out = 0;
     656             : 
     657             :     /* Lookup host address chunks. Try to do a standard memcpy if the regions
     658             :        do not cross memory regions. The translation logic is different if the
     659             :        the virtual address region is the input region vs. not. See the comment
     660             :        in fd_bpf_loader_serialization for more details on how the input
     661             :        region is different from other regions. The input data region will try
     662             :        to lookup the number of remaining bytes in the specific data region. If
     663             :        the memory access is not in the input data region, assume the bytes in
     664             :        the current region are bound by the size of the remaining bytes in the
     665             :        region. */
     666             : 
     667          12 :     ulong   m0_region              = FD_VADDR_TO_REGION( m0_vaddr );
     668          12 :     ulong   m0_offset              = m0_vaddr & FD_VM_OFFSET_MASK;
     669          12 :     ulong   m0_region_idx          = 0UL;
     670          12 :     ulong   m0_bytes_in_cur_region = sz;
     671          12 :     uchar * m0_haddr               = NULL;
     672          12 :     if( m0_region==FD_VM_INPUT_REGION ) {
     673           6 :       m0_region_idx          = fd_vm_get_input_mem_region_idx( vm, m0_offset );
     674           6 :       m0_haddr               = (uchar*)(vm->input_mem_regions[ m0_region_idx ].haddr + m0_offset - vm->input_mem_regions[ m0_region_idx ].vaddr_offset);
     675           6 :       m0_bytes_in_cur_region = fd_ulong_min( sz, fd_ulong_sat_sub( vm->input_mem_regions[ m0_region_idx ].region_sz,
     676           6 :                                                                         ((ulong)m0_haddr - vm->input_mem_regions[ m0_region_idx ].haddr) ) );
     677           6 :     } else {
     678             :       /* We can safely load a slice of 1 byte here because we know that we will
     679             :          not ever read more than the number of bytes that are left in the
     680             :          region. */
     681           6 :       m0_bytes_in_cur_region = fd_ulong_min( sz, vm->region_ld_sz[ m0_region ] - m0_offset );
     682          12 :       m0_haddr               = (uchar *)FD_VM_MEM_SLICE_HADDR_LD_SZ_UNCHECKED( vm, m0_vaddr, FD_VM_ALIGN_RUST_U8 );
     683          12 :     }
     684             : 
     685          12 :     ulong   m1_region              = FD_VADDR_TO_REGION( m1_vaddr );
     686          12 :     ulong   m1_offset              = m1_vaddr & FD_VM_OFFSET_MASK;
     687          12 :     ulong   m1_region_idx          = 0UL;
     688          12 :     ulong   m1_bytes_in_cur_region = sz;
     689          12 :     uchar * m1_haddr               = NULL;
     690          12 :     if( m1_region==FD_VM_INPUT_REGION ) {
     691           6 :       m1_region_idx          = fd_vm_get_input_mem_region_idx( vm, m1_offset );
     692           6 :       m1_haddr               = (uchar*)(vm->input_mem_regions[ m1_region_idx ].haddr + m1_offset - vm->input_mem_regions[ m1_region_idx ].vaddr_offset);
     693           6 :       m1_bytes_in_cur_region = fd_ulong_min( sz, fd_ulong_sat_sub( vm->input_mem_regions[ m1_region_idx ].region_sz,
     694           6 :                                                                         ((ulong)m1_haddr - vm->input_mem_regions[ m1_region_idx ].haddr) ) );
     695           6 :     } else {
     696           6 :       m1_bytes_in_cur_region = fd_ulong_min( sz, vm->region_ld_sz[ m1_region ] - m1_offset );
     697          12 :       m1_haddr               = (uchar *)FD_VM_MEM_SLICE_HADDR_LD_SZ_UNCHECKED( vm, m1_vaddr, FD_VM_ALIGN_RUST_U8 );
     698          12 :     }
     699             : 
     700             :     /* Case where the operation spans multiple regions. Copy over the bytes
     701             :        from each region while iterating to the next one. */
     702             :     /* TODO: An optimization would be to memcmp chunks at once */
     703          12 :     ulong m0_idx = 0UL;
     704          12 :     ulong m1_idx = 0UL;
     705         612 :     for( ulong i=0UL; i<sz; i++ ) {
     706         609 :       if( FD_UNLIKELY( !m0_bytes_in_cur_region ) ) {
     707             :         /* If the memory is not in the input region or it is the last input
     708             :            memory region, that means that if we don't exit now we will have
     709             :            an access violation. */
     710           6 :         if( FD_UNLIKELY( m0_region!=FD_VM_INPUT_REGION || ++m0_region_idx>=vm->input_mem_regions_cnt ) ) {
     711           0 :           FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
     712           0 :           return FD_VM_SYSCALL_ERR_SEGFAULT;
     713           0 :         }
     714           6 :         if( FD_UNLIKELY( vm->input_mem_regions[ m0_region_idx-1UL ].is_acct_data != vm->input_mem_regions[ m0_region_idx ].is_acct_data ) ) {
     715           0 :           FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
     716           0 :           return FD_VM_SYSCALL_ERR_SEGFAULT;
     717           0 :         }
     718             :         /* Otherwise, query the next input region. */
     719           6 :         m0_haddr = (uchar*)vm->input_mem_regions[ m0_region_idx ].haddr;
     720           6 :         m0_idx = 0UL;
     721           6 :         m0_bytes_in_cur_region = vm->input_mem_regions[ m0_region_idx ].region_sz;
     722           6 :       }
     723         609 :       if( FD_UNLIKELY( !m1_bytes_in_cur_region ) ) {
     724           0 :         if( FD_UNLIKELY( m1_region!=FD_VM_INPUT_REGION || ++m1_region_idx>=vm->input_mem_regions_cnt ) ) {
     725           0 :           FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
     726           0 :           return FD_VM_SYSCALL_ERR_SEGFAULT;
     727           0 :         }
     728           0 :         if( FD_UNLIKELY( vm->input_mem_regions[ m1_region_idx-1UL ].is_acct_data != vm->input_mem_regions[ m1_region_idx ].is_acct_data ) ) {
     729           0 :           FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
     730           0 :           return FD_VM_SYSCALL_ERR_SEGFAULT;
     731           0 :         }
     732           0 :         m1_haddr = (uchar*)vm->input_mem_regions[ m1_region_idx ].haddr;
     733           0 :         m1_idx = 0UL;
     734           0 :         m1_bytes_in_cur_region = vm->input_mem_regions[ m1_region_idx ].region_sz;
     735           0 :       }
     736             : 
     737         609 :       int i0 = (int)m0_haddr[ m0_idx ];
     738         609 :       int i1 = (int)m1_haddr[ m1_idx ];
     739         609 :       if( i0!=i1 ) {
     740           9 :         out = i0 - i1;
     741           9 :         break;
     742           9 :       }
     743             : 
     744         600 :       m0_bytes_in_cur_region--;
     745         600 :       m1_bytes_in_cur_region--;
     746         600 :       m0_idx++;
     747         600 :       m1_idx++;
     748         600 :     }
     749          12 :     fd_memcpy( _out, &out, 4UL ); /* Sigh ... see note above (and might be unaligned ... double sigh) */
     750          12 :     return FD_VM_SUCCESS;
     751          12 :   }
     752          15 : }
     753             : 
     754             : int
     755             : fd_vm_syscall_sol_memset( /**/            void *  _vm,
     756             :                           /**/            ulong   dst_vaddr,
     757             :                           /**/            ulong   c,
     758             :                           /**/            ulong   sz,
     759             :                           FD_PARAM_UNUSED ulong   r4,
     760             :                           FD_PARAM_UNUSED ulong   r5,
     761          27 :                           /**/            ulong * _ret ) {
     762          27 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     763          27 :   *_ret = 0;
     764             : 
     765             :   /* https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/mem_ops.rs#L115 */
     766             : 
     767          27 :   FD_VM_CU_MEM_OP_UPDATE( vm, sz );
     768             : 
     769          27 :   if( FD_UNLIKELY( !sz ) ) {
     770           0 :     return FD_VM_SUCCESS;
     771           0 :   }
     772             : 
     773          27 :   ulong   region = FD_VADDR_TO_REGION( dst_vaddr );
     774          27 :   ulong   offset = dst_vaddr & FD_VM_OFFSET_MASK;
     775          27 :   uchar * haddr;
     776             : 
     777          27 :   int b = (int)(c & 255UL);
     778             : 
     779          27 :   if( !vm->direct_mapping ) {
     780           9 :     haddr = FD_VM_MEM_HADDR_ST( vm, dst_vaddr, FD_VM_ALIGN_RUST_U8, sz );
     781           0 :     fd_memset( haddr, b, sz );
     782          18 :   } else if( region!=FD_VM_INPUT_REGION ) {
     783             :     /* Here we special case non-input region memsets: we try to memset
     784             :        as many bytes as possible until it reaches an unwritable section.
     785             :        This is done in order to ensure error-code conformance with
     786             :        Agave. */
     787           9 :     haddr = (uchar*)FD_VM_MEM_HADDR_ST_FAST( vm, dst_vaddr );
     788           9 :     ulong bytes_in_cur_region = fd_ulong_sat_sub( vm->region_st_sz[ region ], offset );
     789           9 :     ulong bytes_to_set        = fd_ulong_min( sz, bytes_in_cur_region );
     790           9 :     fd_memset( haddr, b, bytes_to_set );
     791           9 :     if( FD_UNLIKELY( bytes_to_set<sz ) ) {
     792           3 :       FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
     793           3 :       return FD_VM_SYSCALL_ERR_SEGFAULT;
     794           3 :     }
     795           9 :   } else {
     796             :     /* In this case, we are in the input region AND direct mapping is
     797             :        enabled. Get the haddr and input region and check if it's
     798             :        writable. This means that we may potentially iterate over
     799             :        multiple regions. */
     800           9 :     ulong region_idx;
     801           9 :     FD_VM_MEM_HADDR_AND_REGION_IDX_FROM_INPUT_REGION_CHECKED( vm, offset, region_idx, haddr );
     802           0 :     ulong offset_in_cur_region = offset - vm->input_mem_regions[ region_idx ].vaddr_offset;
     803           9 :     ulong bytes_in_cur_region  = fd_ulong_sat_sub( vm->input_mem_regions[ region_idx ].region_sz, offset_in_cur_region );
     804             : 
     805             :     /* Check that current region is writable */
     806           9 :     if( FD_UNLIKELY( !vm->input_mem_regions[ region_idx ].is_writable && sz ) ) {
     807           0 :       FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
     808           0 :       return FD_VM_SYSCALL_ERR_SEGFAULT;
     809           0 :     }
     810             : 
     811             :     /* Memset goes into multiple regions. */
     812          30 :     while( sz>0UL ) {
     813             : 
     814             :       /* Memset bytes */
     815          30 :       ulong num_bytes_to_set = fd_ulong_min( sz, bytes_in_cur_region );
     816          30 :       fd_memset( haddr, b, num_bytes_to_set );
     817          30 :       sz -= num_bytes_to_set;
     818             : 
     819             :       /* If no more regions left, break. */
     820          30 :       if( ++region_idx==vm->input_mem_regions_cnt ) {
     821           6 :         break;
     822           6 :       }
     823             : 
     824             :       /* Check that new region is writable. */
     825          24 :       if( FD_UNLIKELY( !vm->input_mem_regions[ region_idx ].is_writable ) ) {
     826           3 :         break;
     827           3 :       }
     828             : 
     829             :       /* If new region crosses into/out of account region, error out. */
     830          21 :       if( FD_UNLIKELY( vm->input_mem_regions[ region_idx ].is_acct_data !=
     831          21 :                        vm->input_mem_regions[ region_idx-1UL ].is_acct_data && sz ) ) {
     832           0 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
     833           0 :         return FD_VM_SYSCALL_ERR_SEGFAULT;
     834           0 :       }
     835             : 
     836             :       /* Move haddr to next region. */
     837          21 :       haddr               = (uchar*)vm->input_mem_regions[ region_idx ].haddr;
     838          21 :       bytes_in_cur_region = vm->input_mem_regions[ region_idx ].region_sz;
     839          21 :     }
     840             : 
     841             :     /* If we were not able to successfully set all the bytes, throw an error. */
     842           9 :     if( FD_UNLIKELY( sz>0 ) ) {
     843           3 :       FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
     844           3 :       return FD_VM_SYSCALL_ERR_SEGFAULT;
     845           3 :     }
     846           9 :   }
     847          18 :   return FD_VM_SUCCESS;
     848          27 : }

Generated by: LCOV version 1.14