LCOV - code coverage report
Current view: top level - flamenco/vm/syscall - fd_vm_syscall_runtime.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 308 0.0 %
Date: 2025-09-17 04:38:03 Functions: 0 11 0.0 %

          Line data    Source code
       1             : #include "fd_vm_syscall.h"
       2             : #include "../../runtime/program/fd_vote_program.h"
       3             : #include "../../runtime/context/fd_exec_txn_ctx.h"
       4             : #include "../../runtime/context/fd_exec_instr_ctx.h"
       5             : #include "../../runtime/fd_system_ids.h"
       6             : #include "fd_vm_syscall_macros.h"
       7             : 
       8             : /* FIXME: In the original version of this code, there was an FD_TEST
       9             :    to check if the VM was attached to an instruction context (that
      10             :    would have crashed anyway because of pointer chasing).  If the VM
      11             :    is being run outside the Solana runtime, it should never invoke
      12             :    this syscall in the first place.  So we treat this as a SIGCALL in
      13             :    a non-crashing way for the time being. */
      14             : 
      15             : int
      16             : fd_vm_syscall_sol_get_clock_sysvar( /**/            void *  _vm,
      17             :                                     /**/            ulong   out_vaddr,
      18             :                                     FD_PARAM_UNUSED ulong   r2,
      19             :                                     FD_PARAM_UNUSED ulong   r3,
      20             :                                     FD_PARAM_UNUSED ulong   r4,
      21             :                                     FD_PARAM_UNUSED ulong   r5,
      22           0 :                                     /**/            ulong * _ret ) {
      23           0 :   fd_vm_t * vm = _vm;
      24           0 :   fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
      25           0 :   if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
      26             : 
      27           0 :   FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, sizeof(fd_sol_sysvar_clock_t) ) );
      28             : 
      29           0 :   fd_vm_haddr_query_t var_query = {
      30           0 :     .vaddr    = out_vaddr,
      31           0 :     .align    = FD_VM_ALIGN_RUST_SYSVAR_CLOCK,
      32           0 :     .sz       = sizeof(fd_sol_sysvar_clock_t),
      33           0 :     .is_slice = 0,
      34           0 :   };
      35             : 
      36           0 :   fd_vm_haddr_query_t * queries[] = { &var_query };
      37           0 :   FD_VM_TRANSLATE_MUT( vm, queries );
      38             : 
      39           0 :   fd_sol_sysvar_clock_t clock = fd_sysvar_cache_clock_read_nofail( instr_ctx->sysvar_cache );
      40           0 :   memcpy( var_query.haddr, &clock, sizeof(fd_sol_sysvar_clock_t) );
      41             : 
      42           0 :   *_ret = 0UL;
      43           0 :   return FD_VM_SUCCESS;
      44           0 : }
      45             : 
      46             : int
      47             : fd_vm_syscall_sol_get_epoch_schedule_sysvar( /**/            void *  _vm,
      48             :                                              /**/            ulong   out_vaddr,
      49             :                                              FD_PARAM_UNUSED ulong   r2,
      50             :                                              FD_PARAM_UNUSED ulong   r3,
      51             :                                              FD_PARAM_UNUSED ulong   r4,
      52             :                                              FD_PARAM_UNUSED ulong   r5,
      53           0 :                                              /**/            ulong * _ret ) {
      54           0 :   fd_vm_t * vm = _vm;
      55           0 :   fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
      56           0 :   if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
      57             : 
      58           0 :   FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, sizeof(fd_epoch_schedule_t) ) );
      59             : 
      60           0 :   fd_vm_haddr_query_t var_query = {
      61           0 :     .vaddr    = out_vaddr,
      62           0 :     .align    = FD_VM_ALIGN_RUST_SYSVAR_EPOCH_SCHEDULE,
      63           0 :     .sz       = sizeof(fd_epoch_schedule_t),
      64           0 :     .is_slice = 0,
      65           0 :   };
      66             : 
      67           0 :   fd_vm_haddr_query_t * queries[] = { &var_query };
      68           0 :   FD_VM_TRANSLATE_MUT( vm, queries );
      69             : 
      70           0 :   fd_epoch_schedule_t schedule;
      71           0 :   if( FD_UNLIKELY( !fd_sysvar_cache_epoch_schedule_read( instr_ctx->sysvar_cache, &schedule ) ) ) {
      72           0 :     FD_TXN_ERR_FOR_LOG_INSTR( vm->instr_ctx->txn_ctx, FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR, vm->instr_ctx->txn_ctx->instr_err_idx );
      73           0 :     return FD_VM_ERR_INVAL;
      74           0 :   }
      75           0 :   memcpy( var_query.haddr, &schedule, sizeof(fd_epoch_schedule_t) );
      76             : 
      77           0 :   *_ret = 0UL;
      78           0 :   return FD_VM_SUCCESS;
      79           0 : }
      80             : 
      81             : int
      82             : fd_vm_syscall_sol_get_rent_sysvar( /**/            void *  _vm,
      83             :                                    /**/            ulong   out_vaddr,
      84             :                                    FD_PARAM_UNUSED ulong   r2,
      85             :                                    FD_PARAM_UNUSED ulong   r3,
      86             :                                    FD_PARAM_UNUSED ulong   r4,
      87             :                                    FD_PARAM_UNUSED ulong   r5,
      88           0 :                                    /**/            ulong * _ret ) {
      89           0 :   fd_vm_t * vm = _vm;
      90             : 
      91             :   /* Unreachable in a real SVM, used for testing */
      92             : 
      93           0 :   fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
      94           0 :   if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
      95             : 
      96           0 :   FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, sizeof(fd_rent_t) ) );
      97             : 
      98           0 :   fd_vm_haddr_query_t var_query = {
      99           0 :     .vaddr    = out_vaddr,
     100           0 :     .align    = FD_VM_ALIGN_RUST_SYSVAR_RENT,
     101           0 :     .sz       = sizeof(fd_rent_t),
     102           0 :     .is_slice = 0,
     103           0 :   };
     104             : 
     105           0 :   fd_vm_haddr_query_t * queries[] = { &var_query };
     106           0 :   FD_VM_TRANSLATE_MUT( vm, queries );
     107             : 
     108           0 :   fd_rent_t rent = fd_sysvar_cache_rent_read_nofail( instr_ctx->sysvar_cache );
     109           0 :   memcpy( var_query.haddr, &rent, sizeof(fd_rent_t) );
     110             : 
     111           0 :   *_ret = 0UL;
     112           0 :   return FD_VM_SUCCESS;
     113           0 : }
     114             : 
     115             : /* https://github.com/anza-xyz/agave/blob/v2.3.2/programs/bpf_loader/src/syscalls/sysvar.rs#L149 */
     116             : int
     117             : fd_vm_syscall_sol_get_last_restart_slot_sysvar( /**/            void *  _vm,
     118             :                                                 /**/            ulong   out_vaddr,
     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 = _vm;
     125           0 :   fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
     126           0 :   if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
     127             : 
     128           0 :   FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, sizeof(fd_sol_sysvar_last_restart_slot_t) ) );
     129             : 
     130           0 :   fd_vm_haddr_query_t var_query = {
     131           0 :     .vaddr    = out_vaddr,
     132           0 :     .align    = FD_VM_ALIGN_RUST_SYSVAR_LAST_RESTART_SLOT,
     133           0 :     .sz       = sizeof(fd_sol_sysvar_last_restart_slot_t),
     134           0 :     .is_slice = 0,
     135           0 :   };
     136             : 
     137           0 :   fd_vm_haddr_query_t * queries[] = { &var_query };
     138           0 :   FD_VM_TRANSLATE_MUT( vm, queries );
     139             : 
     140           0 :   fd_sol_sysvar_last_restart_slot_t last_restart_slot;
     141           0 :   if( FD_UNLIKELY( !fd_sysvar_cache_last_restart_slot_read( vm->instr_ctx->sysvar_cache, &last_restart_slot ) ) ) {
     142           0 :     FD_TXN_ERR_FOR_LOG_INSTR( vm->instr_ctx->txn_ctx, FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR, vm->instr_ctx->txn_ctx->instr_err_idx );
     143           0 :     return FD_VM_ERR_INVAL;
     144           0 :   }
     145             : 
     146           0 :   memcpy( var_query.haddr, &last_restart_slot, sizeof(fd_sol_sysvar_last_restart_slot_t) );
     147             : 
     148           0 :   *_ret = 0UL;
     149           0 :   return FD_VM_SUCCESS;
     150           0 : }
     151             : 
     152             : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L167-L232 */
     153             : int
     154             : fd_vm_syscall_sol_get_sysvar( /**/            void *  _vm,
     155             :                               /**/            ulong   sysvar_id_vaddr,
     156             :                               /**/            ulong   out_vaddr,
     157             :                               /**/            ulong   offset,
     158             :                               /**/            ulong   sz,
     159             :                               FD_PARAM_UNUSED ulong   r5,
     160           0 :                               /**/            ulong * _ret ) {
     161           0 :   fd_vm_t * vm = _vm;
     162           0 :   fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
     163           0 :   if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
     164             : 
     165             :   /* sysvar_id_cost seems to just always be 32 / 250 = 0...
     166             :      https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L190-L197 */
     167           0 :   ulong sysvar_buf_cost = sz / FD_VM_CPI_BYTES_PER_UNIT;
     168           0 :   FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, fd_ulong_max( sysvar_buf_cost, FD_VM_MEM_OP_BASE_COST ) ) );
     169             : 
     170             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/sysvar.rs#L207-L211 */
     171           0 :   fd_vm_haddr_query_t var_query = {
     172           0 :     .vaddr    = out_vaddr,
     173           0 :     .align    = FD_VM_ALIGN_RUST_U8,
     174           0 :     .sz       = sz,
     175           0 :     .is_slice = 1,
     176           0 :   };
     177             : 
     178           0 :   fd_vm_haddr_query_t * queries[] = { &var_query };
     179           0 :   FD_VM_TRANSLATE_MUT( vm, queries );
     180             : 
     181             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L199-L200 */
     182           0 :   const fd_pubkey_t * sysvar_id = FD_VM_MEM_HADDR_LD( vm, sysvar_id_vaddr, FD_VM_ALIGN_RUST_PUBKEY, FD_PUBKEY_FOOTPRINT );
     183             : 
     184             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L205-L208 */
     185           0 :   ulong offset_length;
     186           0 :   int err = fd_int_if( __builtin_uaddl_overflow( offset, sz, &offset_length ), FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW, FD_EXECUTOR_INSTR_SUCCESS );
     187           0 :   if( FD_UNLIKELY( err ) ) {
     188           0 :     FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     189           0 :     return FD_VM_SYSCALL_ERR_ABORT;
     190           0 :   }
     191             : 
     192             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L210-L213
     193             :      We don't need this, we already checked we can store in out_vaddr with requested sz. */
     194             : 
     195             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L215-L221 */
     196           0 :   if( FD_UNLIKELY( memcmp( sysvar_id->uc, fd_sysvar_clock_id.uc,             FD_PUBKEY_FOOTPRINT ) &&
     197           0 :                    memcmp( sysvar_id->uc, fd_sysvar_epoch_schedule_id.uc,    FD_PUBKEY_FOOTPRINT ) &&
     198           0 :                    memcmp( sysvar_id->uc, fd_sysvar_epoch_rewards_id.uc,     FD_PUBKEY_FOOTPRINT ) &&
     199           0 :                    memcmp( sysvar_id->uc, fd_sysvar_rent_id.uc,              FD_PUBKEY_FOOTPRINT ) &&
     200           0 :                    memcmp( sysvar_id->uc, fd_sysvar_slot_hashes_id.uc,       FD_PUBKEY_FOOTPRINT ) &&
     201           0 :                    memcmp( sysvar_id->uc, fd_sysvar_stake_history_id.uc,     FD_PUBKEY_FOOTPRINT ) &&
     202           0 :                    memcmp( sysvar_id->uc, fd_sysvar_last_restart_slot_id.uc, FD_PUBKEY_FOOTPRINT ) ) ) {
     203           0 :     *_ret = 2UL;
     204           0 :     return FD_VM_SUCCESS;
     205           0 :   }
     206             : 
     207           0 :   ulong         sysvar_buf_len;
     208           0 :   uchar const * sysvar_buf =
     209           0 :     fd_sysvar_cache_data_query( vm->instr_ctx->sysvar_cache, sysvar_id, &sysvar_buf_len );
     210           0 :   if( FD_UNLIKELY( !sysvar_buf ) ) {
     211           0 :     *_ret = 2UL;
     212           0 :     return FD_VM_SUCCESS;
     213           0 :   }
     214             : 
     215             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L223-L228
     216             :      Note the length check is at the very end to fail after performing sufficient checks. */
     217             : 
     218           0 :   if( FD_UNLIKELY( offset_length>sysvar_buf_len ) ) {
     219           0 :     *_ret = 1UL;
     220           0 :     return FD_VM_SUCCESS;
     221           0 :   }
     222             : 
     223           0 :   if( FD_UNLIKELY( sz==0UL ) ) {
     224           0 :     *_ret = 0UL;
     225           0 :     return FD_VM_SUCCESS;
     226           0 :   }
     227             : 
     228           0 :   fd_memcpy( var_query.haddr, sysvar_buf + offset, sz );
     229           0 :   *_ret = 0;
     230           0 :   return FD_VM_SUCCESS;
     231           0 : }
     232             : 
     233             : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2043-L2118 */
     234             : int
     235             : fd_vm_syscall_sol_get_epoch_stake( /**/            void *  _vm,
     236             :                                    /**/            ulong   var_addr,
     237             :                                    FD_PARAM_UNUSED ulong   r2,
     238             :                                    FD_PARAM_UNUSED ulong   r3,
     239             :                                    FD_PARAM_UNUSED ulong   r4,
     240             :                                    FD_PARAM_UNUSED ulong   r5,
     241           0 :                                    /**/            ulong * _ret ) {
     242           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     243             : 
     244             :   /* Var addr of 0 returns the total active stake on the cluster.
     245             : 
     246             :      https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2057-L2075 */
     247           0 :   if( FD_UNLIKELY( var_addr==0UL ) ) {
     248             :     /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2065-L2066 */
     249           0 :     FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
     250             : 
     251             :     /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2074 */
     252           0 :     *_ret = fd_bank_total_epoch_stake_get( vm->instr_ctx->txn_ctx->bank );
     253           0 :     return FD_VM_SUCCESS;
     254           0 :   }
     255             : 
     256             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2083-L2091
     257             :      FD_PUBKEY_FOOTPRINT/FD_VM_CPI_BYTES_PER_UNIT is always 32/250 = 0,
     258             :      so we can omit it */
     259             : 
     260           0 :   FD_VM_CU_UPDATE( vm, FD_VM_MEM_OP_BASE_COST + FD_VM_SYSCALL_BASE_COST );
     261             : 
     262             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2103-L2104 */
     263           0 :   const fd_pubkey_t * vote_address = FD_VM_MEM_HADDR_LD( vm, var_addr, FD_VM_ALIGN_RUST_PUBKEY, FD_PUBKEY_FOOTPRINT );
     264             : 
     265             :   /* https://github.com/anza-xyz/agave/blob/v2.2.14/runtime/src/bank.rs#L6954 */
     266           0 :   fd_vote_states_t const *    vote_states    = fd_bank_vote_states_prev_locking_query( vm->instr_ctx->txn_ctx->bank );
     267           0 :   fd_vote_state_ele_t const * vote_state_ele = fd_vote_states_query_const( vote_states, vote_address );
     268           0 :   *_ret = vote_state_ele ? vote_state_ele->stake : 0UL;
     269           0 :   fd_bank_vote_states_prev_end_locking_query( vm->instr_ctx->txn_ctx->bank );
     270             : 
     271           0 :   return FD_VM_SUCCESS;
     272           0 : }
     273             : 
     274             : int
     275             : fd_vm_syscall_sol_get_stack_height( /**/            void *  _vm,
     276             :                                     FD_PARAM_UNUSED ulong   r1,
     277             :                                     FD_PARAM_UNUSED ulong   r2,
     278             :                                     FD_PARAM_UNUSED ulong   r3,
     279             :                                     FD_PARAM_UNUSED ulong   r4,
     280             :                                     FD_PARAM_UNUSED ulong   r5,
     281           0 :                                     /**/            ulong * _ret ) {
     282             :   /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L1547 */
     283           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     284             : 
     285           0 :   FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
     286             : 
     287           0 :   *_ret = vm->instr_ctx->txn_ctx->instr_stack_sz;
     288           0 :   return FD_VM_SUCCESS;
     289           0 : }
     290             : 
     291             : int
     292             : fd_vm_syscall_sol_get_return_data( /**/            void *  _vm,
     293             :                                    /**/            ulong   return_data_vaddr,
     294             :                                    /**/            ulong   sz,
     295             :                                    /**/            ulong   program_id_vaddr,
     296             :                                    FD_PARAM_UNUSED ulong   r4,
     297             :                                    FD_PARAM_UNUSED ulong   r5,
     298           0 :                                    /**/            ulong * _ret ) {
     299           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     300             : 
     301             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1465 */
     302           0 :   FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
     303             : 
     304             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1467 */
     305           0 :   fd_txn_return_data_t const * return_data = &vm->instr_ctx->txn_ctx->return_data;
     306             : 
     307             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1468 */
     308           0 :   ulong length = fd_ulong_min( return_data->len, sz );
     309             : 
     310             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1469-L1492 */
     311           0 :   if( FD_LIKELY( length ) ) {
     312             : 
     313             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1470-L1474 */
     314           0 :     FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( length, sizeof(fd_pubkey_t) ) / FD_VM_CPI_BYTES_PER_UNIT );
     315             : 
     316             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1476-L1481 */
     317           0 :     fd_vm_haddr_query_t return_data_query = {
     318           0 :       .vaddr    = return_data_vaddr,
     319           0 :       .align    = FD_VM_ALIGN_RUST_U8,
     320           0 :       .sz       = length,
     321           0 :       .is_slice = 1
     322           0 :     };
     323             : 
     324           0 :     fd_vm_haddr_query_t program_id_query = {
     325           0 :       .vaddr    = program_id_vaddr,
     326           0 :       .align    = FD_VM_ALIGN_RUST_PUBKEY,
     327           0 :       .sz       = sizeof(fd_pubkey_t),
     328           0 :       .is_slice = 0
     329           0 :     };
     330             : 
     331           0 :     fd_vm_haddr_query_t * queries[] = { &return_data_query, &program_id_query };
     332           0 :     FD_VM_TRANSLATE_MUT( vm, queries );
     333             : 
     334             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1490-L1491 */
     335           0 :     memcpy( return_data_query.haddr, return_data->data, length );
     336           0 :     memcpy( program_id_query.haddr, &return_data->program_id, sizeof(fd_pubkey_t) );
     337           0 :   }
     338             : 
     339             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1495 */
     340           0 :   *_ret = return_data->len;
     341           0 :   return FD_VM_SUCCESS;
     342           0 : }
     343             : 
     344             : int
     345             : fd_vm_syscall_sol_set_return_data( /**/            void *  _vm,
     346             :                                    /**/            ulong   src_vaddr,
     347             :                                    /**/            ulong   src_sz,
     348             :                                    FD_PARAM_UNUSED ulong   r3,
     349             :                                    FD_PARAM_UNUSED ulong   r4,
     350             :                                    FD_PARAM_UNUSED ulong   r5,
     351           0 :                                    /**/            ulong * _ret ) {
     352             :   /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L1297 */
     353           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     354             : 
     355             :   /* In the original version of this code, there was an FD_TEST
     356             :      to check if the VM was attached to an instruction context (that
     357             :      would have crashed anyway because of pointer chasing).  If the VM
     358             :      is being run outside the Solana runtime, it should never invoke
     359             :      this syscall in the first place.  So we treat this as a SIGCALL in
     360             :      a non-crashing way for the time being. */
     361           0 :   fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
     362           0 :   if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
     363             : 
     364           0 :   FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSCALL_BASE_COST, src_sz / FD_VM_CPI_BYTES_PER_UNIT ) );
     365             : 
     366             :   /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L1316 */
     367           0 :   if( FD_UNLIKELY( src_sz>FD_VM_RETURN_DATA_MAX ) ) {
     368             :     /* TODO: this is a bit annoying, we may want to unify return codes...
     369             :        - FD_VM_SYSCALL_ERR_RETURN_DATA_TOO_LARGE is Agave's return code,
     370             :          also used for logging */
     371           0 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_RETURN_DATA_TOO_LARGE );
     372           0 :     return FD_VM_SYSCALL_ERR_RETURN_DATA_TOO_LARGE;
     373           0 :   }
     374             : 
     375             :   /* src_sz == 0 is ok */
     376           0 :   void const * src = FD_VM_MEM_SLICE_HADDR_LD( vm, src_vaddr, FD_VM_ALIGN_RUST_U8, src_sz );
     377             : 
     378             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/programs/bpf_loader/src/syscalls/mod.rs#L1480-L1484 */
     379           0 :   fd_pubkey_t const * program_id = NULL;
     380           0 :   int err = fd_exec_instr_ctx_get_last_program_key( vm->instr_ctx, &program_id );
     381           0 :   if( FD_UNLIKELY( err ) ) {
     382           0 :     FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     383           0 :     return err;
     384           0 :   }
     385             : 
     386           0 :   fd_txn_return_data_t * return_data = &instr_ctx->txn_ctx->return_data;
     387             : 
     388           0 :   return_data->len = src_sz;
     389           0 :   if( FD_LIKELY( src_sz!=0UL ) ) {
     390           0 :     fd_memcpy( return_data->data, src, src_sz );
     391           0 :   }
     392           0 :   return_data->program_id = *program_id;
     393             : 
     394           0 :   *_ret = 0;
     395           0 :   return FD_VM_SUCCESS;
     396           0 : }
     397             : 
     398             : /* Used to query and convey information about the sibling instruction
     399             :    https://github.com/anza-xyz/agave/blob/70089cce5119c9afaeb2986e2ecaa6d4505ec15d/sdk/program/src/instruction.rs#L676
     400             : 
     401             :     */
     402             : struct fd_vm_syscall_processed_sibling_instruction {
     403             :   /* Length of the instruction data */
     404             :   ulong data_len;
     405             :   /* Number of accounts */
     406             :   ulong accounts_len;
     407             : };
     408             : typedef struct fd_vm_syscall_processed_sibling_instruction fd_vm_syscall_processed_sibling_instruction_t;
     409             : 
     410           0 : #define FD_VM_SYSCALL_PROCESSED_SIBLING_INSTRUCTION_SIZE  (16UL)
     411           0 : #define FD_VM_SYSCALL_PROCESSED_SIBLING_INSTRUCTION_ALIGN (8UL )
     412             : 
     413             : /* https://github.com/anza-xyz/agave/blob/70089cce5119c9afaeb2986e2ecaa6d4505ec15d/programs/bpf_loader/src/syscalls/mod.rs#L1402 */
     414             : int
     415             : fd_vm_syscall_sol_get_processed_sibling_instruction(
     416             :     void * _vm,
     417             :     ulong index,
     418             :     ulong result_meta_vaddr,
     419             :     ulong result_program_id_vaddr,
     420             :     ulong result_data_vaddr,
     421             :     ulong result_accounts_vaddr,
     422             :     ulong * _ret
     423           0 : ) {
     424           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     425             : 
     426             :   /* Consume base compute cost
     427             :      https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1513 */
     428           0 :   FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
     429             : 
     430             :   /* Get the current instruction stack height.  This value is 1-indexed
     431             :      (top level instruction has a stack height of 1).
     432             :     https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1517 */
     433           0 :   ulong stack_height = vm->instr_ctx->txn_ctx->instr_stack_sz;
     434             : 
     435             :   /* Reverse iterate through the instruction trace, ignoring anything except instructions on the same level.
     436             :      https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1518-L1522 */
     437           0 :   ulong instruction_trace_length = vm->instr_ctx->txn_ctx->instr_trace_length;
     438           0 :   ulong reverse_index_at_stack_height = 0UL;
     439           0 :   fd_exec_instr_trace_entry_t * found_instruction_context = NULL;
     440           0 :   for( ulong index_in_trace=instruction_trace_length; index_in_trace>0UL; index_in_trace-- ) {
     441             : 
     442             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1524-L1526
     443             :        This error can never happen */
     444             : 
     445             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1527-L1529 */
     446           0 :     fd_exec_instr_trace_entry_t * instruction_context = &vm->instr_ctx->txn_ctx->instr_trace[ index_in_trace-1UL ];
     447           0 :     if( FD_LIKELY( instruction_context->stack_height<stack_height ) ) {
     448           0 :       break;
     449           0 :     }
     450             : 
     451             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1530-L1536 */
     452           0 :     if( FD_UNLIKELY( instruction_context->stack_height==stack_height ) ) {
     453           0 :       if( FD_UNLIKELY( fd_ulong_sat_add( index, 1UL )==reverse_index_at_stack_height ) ) {
     454           0 :         found_instruction_context = instruction_context;
     455           0 :         break;
     456           0 :       }
     457           0 :       reverse_index_at_stack_height = fd_ulong_sat_add( reverse_index_at_stack_height, 1UL );
     458           0 :     }
     459           0 :   }
     460             : 
     461             :   /* If we have found an entry, then copy the instruction into the
     462             :      result addresses.
     463             :      https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1539-L1588
     464             :    */
     465           0 :   if( FD_LIKELY( found_instruction_context != NULL ) ) {
     466           0 :     fd_instr_info_t * instr_info = found_instruction_context->instr_info;
     467             : 
     468           0 :     fd_vm_haddr_query_t result_header_query = {
     469           0 :       .vaddr    = result_meta_vaddr,
     470           0 :       .align    = FD_VM_SYSCALL_PROCESSED_SIBLING_INSTRUCTION_ALIGN,
     471           0 :       .sz       = FD_VM_SYSCALL_PROCESSED_SIBLING_INSTRUCTION_SIZE,
     472           0 :       .is_slice = 0,
     473           0 :     };
     474             : 
     475           0 :     fd_vm_haddr_query_t * queries[] = { &result_header_query };
     476           0 :     FD_VM_TRANSLATE_MUT( vm, queries );
     477             : 
     478           0 :     fd_vm_syscall_processed_sibling_instruction_t * result_header = result_header_query.haddr;
     479             : 
     480             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1546-L1583 */
     481           0 :     if( result_header->data_len==instr_info->data_sz && result_header->accounts_len==instr_info->acct_cnt ) {
     482           0 :       fd_vm_haddr_query_t program_id_query = {
     483           0 :         .vaddr    = result_program_id_vaddr,
     484           0 :         .align    = FD_VM_ALIGN_RUST_PUBKEY,
     485           0 :         .sz       = sizeof(fd_pubkey_t),
     486           0 :         .is_slice = 0,
     487           0 :       };
     488             : 
     489           0 :       fd_vm_haddr_query_t data_query = {
     490           0 :         .vaddr    = result_data_vaddr,
     491           0 :         .align    = FD_VM_ALIGN_RUST_U8,
     492           0 :         .sz       = result_header->data_len,
     493           0 :         .is_slice = 1,
     494           0 :       };
     495             : 
     496           0 :       fd_vm_haddr_query_t accounts_query = {
     497           0 :         .vaddr    = result_accounts_vaddr,
     498           0 :         .align    = FD_VM_RUST_ACCOUNT_META_ALIGN,
     499           0 :         .sz       = fd_ulong_sat_mul( result_header->accounts_len, FD_VM_RUST_ACCOUNT_META_SIZE ),
     500           0 :         .is_slice = 1,
     501           0 :       };
     502             : 
     503           0 :       fd_vm_haddr_query_t * queries[] = { &program_id_query, &data_query, &accounts_query, &result_header_query };
     504           0 :       FD_VM_TRANSLATE_MUT( vm, queries );
     505             : 
     506           0 :       fd_pubkey_t *               program_id = program_id_query.haddr;
     507           0 :       uchar *                     data       = data_query.haddr;
     508           0 :       fd_vm_rust_account_meta_t * accounts   = accounts_query.haddr;
     509             : 
     510             :       /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1561-L1562 */
     511           0 :       fd_pubkey_t const * instr_ctx_program_id = NULL;
     512           0 :       int err = fd_exec_txn_ctx_get_key_of_account_at_index( vm->instr_ctx->txn_ctx,
     513           0 :                                                              instr_info->program_id,
     514           0 :                                                              &instr_ctx_program_id );
     515           0 :       if( FD_UNLIKELY( err ) ) {
     516           0 :         FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     517           0 :         return err;
     518           0 :       }
     519           0 :       fd_memcpy( program_id, instr_ctx_program_id, sizeof(fd_pubkey_t) );
     520             : 
     521             :       /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1563 */
     522           0 :       fd_memcpy( data, instr_info->data, instr_info->data_sz );
     523             : 
     524             :       /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1564-L1581 */
     525           0 :       for( ushort i=0; i<instr_info->acct_cnt; i++ ) {
     526           0 :         fd_pubkey_t const * account_key;
     527           0 :         ushort txn_idx = instr_info->accounts[ i ].index_in_transaction;
     528           0 :         err            = fd_exec_txn_ctx_get_key_of_account_at_index( vm->instr_ctx->txn_ctx, txn_idx, &account_key );
     529           0 :         if( FD_UNLIKELY( err ) ) {
     530           0 :           FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     531           0 :           return err;
     532           0 :         }
     533             : 
     534           0 :         fd_memcpy( accounts[ i ].pubkey, account_key, sizeof(fd_pubkey_t) );
     535           0 :         accounts[ i ].is_signer   = !!(instr_info->accounts[ i ].is_signer );
     536           0 :         accounts[ i ].is_writable = !!(instr_info->accounts[ i ].is_writable );
     537           0 :       }
     538           0 :     } else {
     539             :       /* Copy the actual metadata into the result meta struct
     540             :          https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1584-L1586 */
     541           0 :       result_header->data_len     = instr_info->data_sz;
     542           0 :       result_header->accounts_len = instr_info->acct_cnt;
     543           0 :     }
     544             : 
     545             :     /* Return true as we found a sibling instruction
     546             :        https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1588 */
     547           0 :     *_ret = 1UL;
     548           0 :     return FD_VM_SUCCESS;
     549           0 :   }
     550             : 
     551             :   /* Return false if we didn't find a sibling instruction
     552             :      https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1590 */
     553           0 :   *_ret = 0UL;
     554           0 :   return FD_VM_SUCCESS;
     555           0 : }
     556             : 
     557             : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/sysvar.rs#L80 */
     558             : int
     559             : fd_vm_syscall_sol_get_epoch_rewards_sysvar( /**/            void *  _vm,
     560             :                                             /**/            ulong   out_vaddr,
     561             :                                             FD_PARAM_UNUSED ulong   r2,
     562             :                                             FD_PARAM_UNUSED ulong   r3,
     563             :                                             FD_PARAM_UNUSED ulong   r4,
     564             :                                             FD_PARAM_UNUSED ulong   r5,
     565           0 :                                             /**/            ulong * _ret ) {
     566           0 :   fd_vm_t * vm = _vm;
     567           0 :   fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
     568           0 :   if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
     569             : 
     570           0 :   FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, sizeof(fd_sysvar_epoch_rewards_t) ) );
     571             : 
     572           0 :   uchar * out = FD_VM_MEM_HADDR_ST( vm, out_vaddr, FD_VM_ALIGN_RUST_SYSVAR_EPOCH_REWARDS, sizeof(fd_sysvar_epoch_rewards_t) );
     573             : 
     574           0 :   fd_sysvar_epoch_rewards_t epoch_rewards;
     575           0 :   if( FD_UNLIKELY( !fd_sysvar_cache_epoch_rewards_read( instr_ctx->sysvar_cache, &epoch_rewards ) ) ) {
     576           0 :     FD_TXN_ERR_FOR_LOG_INSTR( vm->instr_ctx->txn_ctx, FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR, vm->instr_ctx->txn_ctx->instr_err_idx );
     577           0 :     return FD_VM_ERR_INVAL;
     578           0 :   }
     579           0 :   memcpy( out, &epoch_rewards, sizeof(fd_sysvar_epoch_rewards_t) );
     580           0 :   memset( out+81, 0, 7 ); /* padding */
     581             : 
     582           0 :   *_ret = 0UL;
     583           0 :   return FD_VM_SUCCESS;
     584           0 : }

Generated by: LCOV version 1.14