LCOV - code coverage report
Current view: top level - flamenco/runtime/tests - fd_instr_harness.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 340 0.0 %
Date: 2025-12-17 05:06:24 Functions: 0 3 0.0 %

          Line data    Source code
       1             : #undef FD_SPAD_USE_HANDHOLDING
       2             : #define FD_SPAD_USE_HANDHOLDING 1
       3             : 
       4             : #include "fd_solfuzz_private.h"
       5             : #include "fd_instr_harness.h"
       6             : #include "../fd_executor.h"
       7             : #include "../fd_runtime.h"
       8             : #include "../program/fd_bpf_loader_program.h"
       9             : #include "../sysvar/fd_sysvar.h"
      10             : #include "../sysvar/fd_sysvar_clock.h"
      11             : #include "../sysvar/fd_sysvar_epoch_schedule.h"
      12             : #include "../sysvar/fd_sysvar_recent_hashes.h"
      13             : #include "../sysvar/fd_sysvar_rent.h"
      14             : #include "../sysvar/fd_sysvar_last_restart_slot.h"
      15             : #include "../fd_system_ids.h"
      16             : #include "../../accdb/fd_accdb_impl_v1.h"
      17             : #include "../../log_collector/fd_log_collector.h"
      18             : #include <assert.h>
      19             : 
      20             : int
      21             : fd_solfuzz_pb_instr_ctx_create( fd_solfuzz_runner_t *                runner,
      22             :                                 fd_exec_instr_ctx_t *                ctx,
      23             :                                 fd_exec_test_instr_context_t const * test_ctx,
      24           0 :                                 bool                                 is_syscall ) {
      25             : 
      26           0 :   memset( ctx, 0, sizeof(fd_exec_instr_ctx_t) );
      27             : 
      28           0 :   fd_funk_t * funk = fd_accdb_user_v1_funk( runner->accdb );
      29             : 
      30             :   /* Generate unique ID for funk txn */
      31             : 
      32           0 :   fd_funk_txn_xid_t xid[1] = {{ .ul={ LONG_MAX, LONG_MAX } }};
      33             : 
      34             :   /* Create temporary funk transaction and txn / slot / epoch contexts */
      35             : 
      36           0 :   fd_funk_txn_xid_t parent_xid; fd_funk_txn_xid_set_root( &parent_xid );
      37           0 :   fd_accdb_attach_child        ( runner->accdb_admin,     &parent_xid, xid );
      38           0 :   fd_progcache_txn_attach_child( runner->progcache_admin, &parent_xid, xid );
      39             : 
      40           0 :   fd_txn_in_t *  txn_in  = fd_spad_alloc( runner->spad, alignof(fd_txn_in_t), sizeof(fd_txn_in_t) );
      41           0 :   fd_txn_out_t * txn_out = fd_spad_alloc( runner->spad, alignof(fd_txn_out_t), sizeof(fd_txn_out_t) );
      42             : 
      43           0 :   fd_log_collector_t * log = fd_spad_alloc( runner->spad, alignof(fd_log_collector_t), sizeof(fd_log_collector_t) );
      44             : 
      45           0 :   fd_runtime_t * runtime = runner->runtime;
      46             : 
      47           0 :   runtime->log.log_collector = log;
      48             : 
      49           0 :   ctx->txn_out = txn_out;
      50           0 :   ctx->txn_in  = txn_in;
      51             : 
      52           0 :   txn_in->exec_accounts = runner->exec_accounts;
      53             : 
      54           0 :   memset( txn_out->accounts.metas, 0, sizeof(fd_account_meta_t) * MAX_TX_ACCOUNT_LOCKS );
      55             : 
      56             :   /* Bank manager */
      57           0 :   fd_banks_clear_bank( runner->banks, runner->bank );
      58             : 
      59           0 :   fd_features_t * features = fd_bank_features_modify( runner->bank );
      60           0 :   fd_exec_test_feature_set_t const * feature_set = &test_ctx->epoch_context.features;
      61           0 :   if( !fd_solfuzz_pb_restore_features( features, feature_set ) ) {
      62           0 :     return 0;
      63           0 :   }
      64             : 
      65             :   /* Setup vote states accounts */
      66           0 :   fd_vote_states_t * vote_states = fd_vote_states_join( fd_vote_states_new( fd_bank_vote_states_locking_modify( runner->bank ), 4UL, 999UL ) );
      67           0 :   if( FD_UNLIKELY( !vote_states ) ) FD_LOG_ERR(( "fd_vote_states_new failed" ));
      68           0 :   fd_bank_vote_states_end_locking_modify( runner->bank );
      69             : 
      70           0 :   fd_vote_states_t * vote_states_prev = fd_vote_states_join( fd_vote_states_new( fd_bank_vote_states_prev_locking_modify( runner->bank ), 4UL, 999UL ) );
      71           0 :   if( FD_UNLIKELY( !vote_states_prev ) ) FD_LOG_ERR(( "fd_vote_states_new for prev failed" ));
      72           0 :   fd_bank_vote_states_prev_end_locking_modify( runner->bank );
      73             : 
      74           0 :   fd_vote_states_t * vote_states_prev_prev = fd_vote_states_join( fd_vote_states_new( fd_bank_vote_states_prev_prev_locking_modify( runner->bank ), 4UL, 999UL ) );
      75           0 :   if( FD_UNLIKELY( !vote_states_prev_prev ) ) FD_LOG_ERR(( "fd_vote_staets_new for prev2 failed" ));
      76           0 :   fd_bank_vote_states_prev_prev_end_locking_modify( runner->bank );
      77             : 
      78             :   /* Blockhash queue init */
      79             : 
      80           0 :   ulong blockhash_seed; FD_TEST( fd_rng_secure( &blockhash_seed, sizeof(ulong) ) );
      81           0 :   fd_blockhashes_t * blockhashes = fd_blockhashes_init( fd_bank_block_hash_queue_modify( runner->bank ), blockhash_seed );
      82           0 :   fd_memset( fd_blockhash_deq_push_tail_nocopy( blockhashes->d.deque ), 0, sizeof(fd_hash_t) );
      83             : 
      84             :   /* Set up mock txn descriptor and payload
      85             :      FIXME: More fields may need to be initialized. This seems to be
      86             :      the minimal set of fields needed to retain full context for
      87             :      precompile execution. */
      88           0 :   fd_txn_p_t * txn            = fd_spad_alloc_check( runner->spad, alignof(fd_txn_p_t), sizeof(fd_txn_p_t) );
      89           0 :   fd_txn_t *   txn_descriptor = TXN( txn );
      90           0 :   if( test_ctx->data ) {
      91           0 :     memcpy( txn->payload, test_ctx->data->bytes, test_ctx->data->size );
      92           0 :     txn->payload_sz = test_ctx->data->size;
      93           0 :   } else {
      94           0 :     txn->payload_sz = 0;
      95           0 :   }
      96           0 :   txn_descriptor->transaction_version = FD_TXN_VLEGACY;
      97           0 :   txn_descriptor->acct_addr_cnt       = (ushort)test_ctx->accounts_count;
      98           0 :   txn_descriptor->instr_cnt           = 1;
      99           0 :   txn_descriptor->instr[0]            = (fd_txn_instr_t) {
     100           0 :     .acct_cnt = (ushort)test_ctx->accounts_count,
     101           0 :     .data_off = 0,
     102           0 :     .data_sz  = (ushort)txn->payload_sz,
     103           0 :   };
     104             : 
     105           0 :   runtime->funk                     = funk;
     106           0 :   runtime->log.enable_log_collector = 0;
     107             : 
     108           0 :   fd_compute_budget_details_new( &txn_out->details.compute_budget );
     109           0 :   runtime->instr.stack_sz            = 0;
     110           0 :   txn_out->accounts.cnt     = 0UL;
     111           0 :   runtime->accounts.executable_cnt   = 0UL;
     112             : 
     113           0 :   txn_out->details.programs_to_reverify_cnt       = 0UL;
     114           0 :   txn_out->details.loaded_accounts_data_size      = 0UL;
     115           0 :   txn_out->details.loaded_accounts_data_size_cost = 0UL;
     116           0 :   txn_out->details.accounts_resize_delta          = 0UL;
     117             : 
     118           0 :   memset( txn_out->details.return_data.program_id.key, 0, sizeof(fd_pubkey_t) );
     119           0 :   txn_out->details.return_data.len = 0;
     120             : 
     121           0 :   runtime->log.capture_ctx   = NULL;
     122             : 
     123           0 :   runtime->instr.trace_length = 1UL;
     124             : 
     125           0 :   txn_out->err.exec_err       = 0;
     126           0 :   txn_out->err.exec_err_kind  = FD_EXECUTOR_ERR_KIND_NONE;
     127           0 :   runtime->instr.current_idx  = 0;
     128             : 
     129           0 :   txn_in->txn                                                = txn;
     130           0 :   txn_out->details.compute_budget.compute_unit_limit = test_ctx->cu_avail;
     131           0 :   txn_out->details.compute_budget.compute_meter      = test_ctx->cu_avail;
     132           0 :   runtime->log.enable_vm_tracing                             = runner->enable_vm_tracing;
     133           0 :   runtime->log.tracing_mem                                   = runner->enable_vm_tracing ?
     134           0 :                                                                fd_spad_alloc_check( runner->spad, FD_RUNTIME_VM_TRACE_STATIC_ALIGN, FD_RUNTIME_VM_TRACE_STATIC_FOOTPRINT * FD_MAX_INSTRUCTION_STACK_DEPTH ) :
     135           0 :                                                                NULL;
     136             : 
     137             :   /* Set up instruction context */
     138           0 :   fd_instr_info_t * info = &runtime->instr.trace[ 0UL ];
     139           0 :   memset( info, 0, sizeof(fd_instr_info_t) );
     140           0 :   info->stack_height = 1;
     141             : 
     142           0 :   if( test_ctx->data ) {
     143           0 :     if( FD_UNLIKELY( test_ctx->data->size>FD_INSTR_DATA_MAX ) ) {
     144           0 :       FD_LOG_ERR(( "invariant violation: instr data sz is too large %u > %lu", test_ctx->data->size, FD_INSTR_DATA_MAX ));
     145           0 :     }
     146           0 :     info->data_sz = (ushort)test_ctx->data->size;
     147           0 :     memcpy( info->data, test_ctx->data->bytes, info->data_sz );
     148           0 :   }
     149             : 
     150             :   /* Prepare borrowed account table (correctly handles aliasing) */
     151             : 
     152           0 :   if( FD_UNLIKELY( test_ctx->accounts_count > MAX_TX_ACCOUNT_LOCKS ) ) {
     153           0 :     FD_LOG_NOTICE(( "too many accounts" ));
     154           0 :     return 0;
     155           0 :   }
     156             : 
     157             :   /* Load accounts into database */
     158             : 
     159           0 :   fd_txn_account_t accts[MAX_TX_ACCOUNT_LOCKS] = {0};
     160           0 :   txn_out->accounts.cnt = test_ctx->accounts_count;
     161             : 
     162           0 :   int has_program_id = 0;
     163             : 
     164           0 :   for( ulong j=0UL; j < test_ctx->accounts_count; j++ ) {
     165           0 :     fd_pubkey_t * acc_key = (fd_pubkey_t *)test_ctx->accounts[j].address;
     166             : 
     167           0 :     memcpy(  &(txn_out->accounts.keys[j]), test_ctx->accounts[j].address, sizeof(fd_pubkey_t) );
     168           0 :     if( !fd_solfuzz_pb_load_account( runtime, runner->accdb, xid, &test_ctx->accounts[j], 0, j, &accts[j].meta ) ) {
     169           0 :       return 0;
     170           0 :     }
     171           0 :     runtime->accounts.refcnt[j] = 0UL;
     172             : 
     173           0 :     fd_txn_account_t * acc = &accts[j];
     174           0 :     if( fd_txn_account_get_meta( acc ) ) {
     175           0 :       uchar *             data     = fd_spad_alloc( runner->spad, FD_ACCOUNT_REC_ALIGN, FD_ACC_TOT_SZ_MAX );
     176           0 :       ulong               dlen     = fd_txn_account_get_data_len( acc );
     177           0 :       fd_account_meta_t * meta     = (fd_account_meta_t *)data;
     178           0 :       fd_memcpy( data, fd_txn_account_get_meta( acc ), sizeof(fd_account_meta_t)+dlen );
     179           0 :       if( FD_UNLIKELY( !fd_txn_account_join( fd_txn_account_new( acc, acc_key, meta, 0 ) ) ) ) {
     180           0 :         FD_LOG_CRIT(( "Failed to join and new a txn account" ));
     181           0 :       }
     182           0 :     }
     183           0 :     txn_out->accounts.metas[j] = accts[j].meta;
     184             : 
     185           0 :     if( !memcmp( accts[j].pubkey, test_ctx->program_id, sizeof(fd_pubkey_t) ) ) {
     186           0 :       has_program_id = 1;
     187           0 :       info->program_id = (uchar)txn_out->accounts.cnt;
     188           0 :     }
     189             : 
     190             :     /* Since the instructions sysvar is set as mutable at the txn level, we need to make it mutable here as well. */
     191           0 :     if( !memcmp( accts[j].pubkey, &fd_sysvar_instructions_id, sizeof(fd_pubkey_t) ) ) {
     192           0 :       fd_txn_account_set_mutable( acc );
     193           0 :     }
     194           0 :   }
     195             : 
     196             :   /* If the program id is not in the set of accounts it must be added to the set of accounts. */
     197           0 :   if( FD_UNLIKELY( !has_program_id ) ) {
     198           0 :     fd_txn_account_t * program_acc = &accts[ test_ctx->accounts_count ];
     199           0 :     fd_pubkey_t *      program_key = &txn_out->accounts.keys[ txn_out->accounts.cnt ];
     200           0 :     memcpy( program_key, test_ctx->program_id, sizeof(fd_pubkey_t) );
     201             : 
     202           0 :     fd_account_meta_t * meta = fd_spad_alloc( runner->spad, alignof(fd_account_meta_t), sizeof(fd_account_meta_t) );
     203           0 :     fd_account_meta_init( meta );
     204             : 
     205           0 :     if( FD_UNLIKELY( !fd_txn_account_join( fd_txn_account_new(
     206           0 :           program_acc,
     207           0 :           program_key,
     208           0 :           meta,
     209           0 :           1 ) ) ) ) {
     210           0 :       FD_LOG_CRIT(( "Failed to join and new a txn account" ));
     211           0 :     }
     212             : 
     213           0 :     txn_out->accounts.metas[test_ctx->accounts_count] = meta;
     214             : 
     215           0 :     info->program_id = (uchar)txn_out->accounts.cnt;
     216           0 :     txn_out->accounts.cnt++;
     217           0 :   }
     218             : 
     219             :   /* Load in executable accounts */
     220           0 :   for( ulong i = 0; i < txn_out->accounts.cnt; i++ ) {
     221           0 :     fd_pubkey_t * acc_key = (fd_pubkey_t *)test_ctx->accounts[i].address;
     222             : 
     223           0 :     fd_txn_account_t * acc = &accts[i];
     224           0 :     if ( !fd_executor_pubkey_is_bpf_loader( fd_txn_account_get_owner( acc ) ) ) {
     225           0 :       continue;
     226           0 :     }
     227             : 
     228           0 :     fd_account_meta_t const * meta = fd_txn_account_get_meta( acc );
     229           0 :     if( meta == NULL ) {
     230           0 :       uchar * mem = fd_spad_alloc( runner->spad, FD_TXN_ACCOUNT_ALIGN, sizeof(fd_account_meta_t) );
     231           0 :       fd_account_meta_t * meta = (fd_account_meta_t *)mem;
     232           0 :       memset( meta, 0, sizeof(fd_account_meta_t) );
     233           0 :       if( FD_UNLIKELY( !fd_txn_account_join( fd_txn_account_new( acc, acc_key, meta, 0 ) ) ) ) {
     234           0 :         FD_LOG_CRIT(( "Failed to join and new a txn account" ));
     235           0 :       }
     236           0 :       continue;
     237           0 :     }
     238             : 
     239           0 :     if( FD_UNLIKELY( !memcmp( meta->owner, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
     240           0 :       fd_bpf_upgradeable_loader_state_t program_loader_state[1];
     241           0 :       int err = fd_bpf_loader_program_get_state( meta, program_loader_state );
     242           0 :       if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
     243           0 :         continue;
     244           0 :       }
     245             : 
     246           0 :       if( !fd_bpf_upgradeable_loader_state_is_program( program_loader_state ) ) {
     247           0 :         continue;
     248           0 :       }
     249             : 
     250           0 :       fd_pubkey_t * programdata_acc = &program_loader_state->inner.program.programdata_address;
     251             : 
     252           0 :       fd_account_meta_t const * meta = fd_funk_get_acc_meta_readonly(
     253           0 :           runtime->funk,
     254           0 :           xid,
     255           0 :           programdata_acc,
     256           0 :           NULL,
     257           0 :           &err,
     258           0 :           NULL );
     259             : 
     260           0 :       if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS ) ) {
     261           0 :         continue;
     262           0 :       }
     263           0 :       runtime->accounts.executable_pubkeys[runtime->accounts.executable_cnt] = *programdata_acc;
     264           0 :       runtime->accounts.executables_meta[runtime->accounts.executable_cnt]   = (fd_account_meta_t *)meta;
     265           0 :       runtime->accounts.executable_cnt++;
     266           0 :     }
     267           0 :   }
     268             : 
     269             :   /* Set slot bank variables and ensure all relevant sysvars are present */
     270           0 :   fd_sol_sysvar_last_restart_slot_t last_restart_slot_[1];
     271           0 :   FD_TEST( fd_sysvar_last_restart_slot_read( funk, xid, last_restart_slot_ ) );
     272             : 
     273           0 :   fd_sol_sysvar_clock_t clock_[1];
     274           0 :   fd_sol_sysvar_clock_t * clock = fd_sysvar_clock_read( runner->accdb, xid, clock_ );
     275           0 :   FD_TEST( clock );
     276           0 :   fd_bank_slot_set( runner->bank, clock->slot );
     277             : 
     278           0 :   fd_epoch_schedule_t epoch_schedule_[1];
     279           0 :   fd_epoch_schedule_t * epoch_schedule = fd_sysvar_epoch_schedule_read( funk, xid, epoch_schedule_ );
     280           0 :   FD_TEST( epoch_schedule );
     281           0 :   fd_bank_epoch_schedule_set( runner->bank, *epoch_schedule );
     282             : 
     283             :   /* Override epoch bank rent setting */
     284           0 :   fd_rent_t rent[1];
     285           0 :   FD_TEST( fd_sysvar_rent_read( funk, xid, rent ) );
     286           0 :   fd_bank_rent_set( runner->bank, *rent );
     287             : 
     288             :   /* Override most recent blockhash if given */
     289           0 :   uchar __attribute__((aligned(FD_SYSVAR_RECENT_HASHES_ALIGN))) rbh_mem[FD_SYSVAR_RECENT_HASHES_FOOTPRINT];
     290           0 :   fd_recent_block_hashes_t const * rbh = fd_sysvar_recent_hashes_read( funk, xid, rbh_mem );
     291           0 :   FD_TEST( rbh );
     292           0 :   if( !deq_fd_block_block_hash_entry_t_empty( rbh->hashes ) ) {
     293           0 :     fd_block_block_hash_entry_t const * last = deq_fd_block_block_hash_entry_t_peek_tail_const( rbh->hashes );
     294           0 :     if( last ) {
     295           0 :       fd_blockhashes_t * blockhashes = fd_bank_block_hash_queue_modify( runner->bank );
     296           0 :       fd_blockhashes_pop_new( blockhashes );
     297           0 :       fd_blockhash_info_t * info = fd_blockhashes_push_new( blockhashes, &last->blockhash );
     298           0 :       info->fee_calculator = last->fee_calculator;
     299             : 
     300           0 :       fd_bank_rbh_lamports_per_sig_set( runner->bank, last->fee_calculator.lamports_per_signature );
     301           0 :     }
     302           0 :   }
     303             : 
     304           0 :   fd_funk_txn_xid_t exec_xid[1] = {{ .ul={ fd_bank_slot_get( runner->bank ), runner->bank->idx } }};
     305           0 :   fd_accdb_attach_child        ( runner->accdb_admin,     xid, exec_xid );
     306           0 :   fd_progcache_txn_attach_child( runner->progcache_admin, xid, exec_xid );
     307             : 
     308             :   /* Load instruction accounts */
     309             : 
     310           0 :   if( FD_UNLIKELY( test_ctx->instr_accounts_count > MAX_TX_ACCOUNT_LOCKS ) ) {
     311           0 :     FD_LOG_NOTICE(( "too many instruction accounts" ));
     312           0 :     return 0;
     313           0 :   }
     314             : 
     315             :   /* Restore sysvar cache */
     316           0 :   fd_sysvar_cache_restore_fuzz( runner->bank, funk, xid );
     317           0 :   ctx->sysvar_cache = fd_bank_sysvar_cache_modify( runner->bank );
     318           0 :   ctx->runtime = runtime;
     319             : 
     320           0 :   uchar acc_idx_seen[ FD_INSTR_ACCT_MAX ] = {0};
     321           0 :   for( ulong j=0UL; j < test_ctx->instr_accounts_count; j++ ) {
     322           0 :     uint index = test_ctx->instr_accounts[j].index;
     323           0 :     if( index >= test_ctx->accounts_count ) {
     324           0 :       FD_LOG_NOTICE( ( "instruction account index out of range (%u > %u)", index, test_ctx->instr_accounts_count ) );
     325           0 :       return 0;
     326           0 :     }
     327             : 
     328           0 :     fd_txn_account_t * acc = &accts[ index ];
     329             : 
     330             :     /* Setup instruction accounts */
     331           0 :     fd_instr_info_setup_instr_account( info,
     332           0 :                                        acc_idx_seen,
     333           0 :                                        (ushort)index,
     334           0 :                                        (ushort)j,
     335           0 :                                        (ushort)j,
     336           0 :                                        test_ctx->instr_accounts[j].is_writable,
     337           0 :                                        test_ctx->instr_accounts[j].is_signer );
     338             : 
     339           0 :     if( test_ctx->instr_accounts[j].is_writable ) {
     340           0 :       fd_txn_account_set_mutable( acc );
     341           0 :     }
     342           0 :   }
     343           0 :   info->acct_cnt = (uchar)test_ctx->instr_accounts_count;
     344             : 
     345             :   /* The remaining checks enforce that the program is in the accounts list. */
     346           0 :   bool found_program_id = false;
     347           0 :   for( uint i = 0; i < test_ctx->accounts_count; i++ ) {
     348           0 :     if( 0 == memcmp( test_ctx->accounts[i].address, test_ctx->program_id, sizeof(fd_pubkey_t) ) ) {
     349           0 :       info->program_id = (uchar) i;
     350           0 :       found_program_id = true;
     351           0 :       break;
     352           0 :     }
     353           0 :   }
     354             : 
     355             :   /* Early returning only happens in instruction execution. */
     356           0 :   if( !is_syscall && !found_program_id ) {
     357           0 :     FD_LOG_NOTICE(( " Unable to find program_id in accounts" ));
     358           0 :     return 0;
     359           0 :   }
     360             : 
     361           0 :   ctx->instr              = info;
     362           0 :   ctx->runtime->progcache = runner->progcache;
     363           0 :   ctx->runtime->accdb     = runner->accdb;
     364             : 
     365           0 :   runtime->log.enable_log_collector = 0;
     366             : 
     367           0 :   fd_log_collector_init( ctx->runtime->log.log_collector, 1 );
     368           0 :   fd_base58_encode_32( txn_out->accounts.keys[ ctx->instr->program_id ].uc, NULL, ctx->program_id_base58 );
     369             : 
     370           0 :   return 1;
     371           0 : }
     372             : 
     373             : void
     374             : fd_solfuzz_pb_instr_ctx_destroy( fd_solfuzz_runner_t * runner,
     375           0 :                                  fd_exec_instr_ctx_t * ctx ) {
     376           0 :   if( !ctx ) return;
     377           0 :   fd_accdb_clear( runner->accdb_admin );
     378           0 :   fd_progcache_clear( runner->progcache_admin );
     379             : 
     380             :   /* In order to check for leaks in the workspace, we need to compact the
     381             :      allocators. Without doing this, empty superblocks may be retained
     382             :      by the fd_alloc instance, which mean we cannot check for leaks. */
     383           0 :   fd_alloc_compact( runner->accdb_admin->funk->alloc );
     384           0 :   fd_alloc_compact( runner->progcache_admin->funk->alloc );
     385           0 : }
     386             : 
     387             : ulong
     388             : fd_solfuzz_pb_instr_run( fd_solfuzz_runner_t * runner,
     389             :                          void const *          input_,
     390             :                          void **               output_,
     391             :                          void *                output_buf,
     392           0 :                          ulong                 output_bufsz ) {
     393           0 :   fd_exec_test_instr_context_t const * input  = fd_type_pun_const( input_ );
     394           0 :   fd_exec_test_instr_effects_t **      output = fd_type_pun( output_ );
     395             : 
     396             :   /* Convert the Protobuf inputs to a fd_exec context */
     397           0 :   fd_exec_instr_ctx_t ctx[1];
     398           0 :   if( !fd_solfuzz_pb_instr_ctx_create( runner, ctx, input, false ) ) {
     399           0 :     fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     400           0 :     return 0UL;
     401           0 :   }
     402             : 
     403           0 :   fd_instr_info_t * instr = (fd_instr_info_t *) ctx->instr;
     404             : 
     405             :   /* Execute the test */
     406           0 :   int exec_result = fd_execute_instr( ctx->runtime, runner->bank, ctx->txn_in, ctx->txn_out, instr );
     407             : 
     408             :   /* Allocate space to capture outputs */
     409             : 
     410           0 :   ulong output_end = (ulong)output_buf + output_bufsz;
     411           0 :   FD_SCRATCH_ALLOC_INIT( l, output_buf );
     412             : 
     413           0 :   fd_exec_test_instr_effects_t * effects =
     414           0 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_instr_effects_t),
     415           0 :                                 sizeof (fd_exec_test_instr_effects_t) );
     416           0 :   if( FD_UNLIKELY( _l > output_end ) ) {
     417           0 :     fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     418           0 :     return 0UL;
     419           0 :   }
     420           0 :   fd_memset( effects, 0, sizeof(fd_exec_test_instr_effects_t) );
     421             : 
     422             :   /* Capture error code */
     423             : 
     424           0 :   effects->result   = -exec_result;
     425           0 :   effects->cu_avail = ctx->txn_out->details.compute_budget.compute_meter;
     426             : 
     427             :   /* Don't capture custom error codes if the program is a precompile */
     428           0 :   if( FD_LIKELY( effects->result ) ) {
     429           0 :     int program_id_idx = ctx->instr[ 0UL ].program_id;
     430           0 :     if( exec_result==FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR &&
     431           0 :         fd_executor_lookup_native_precompile_program( &ctx->txn_out->accounts.keys[ program_id_idx ] )==NULL ) {
     432           0 :       effects->custom_err = ctx->txn_out->err.custom_err;
     433           0 :     }
     434           0 :   }
     435             : 
     436             :   /* Allocate space for captured accounts */
     437           0 :   ulong modified_acct_cnt = ctx->txn_out->accounts.cnt;
     438             : 
     439           0 :   fd_exec_test_acct_state_t * modified_accts =
     440           0 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_acct_state_t),
     441           0 :                                 sizeof (fd_exec_test_acct_state_t) * modified_acct_cnt );
     442           0 :   if( FD_UNLIKELY( _l > output_end ) ) {
     443           0 :     fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     444           0 :     return 0;
     445           0 :   }
     446           0 :   effects->modified_accounts       = modified_accts;
     447           0 :   effects->modified_accounts_count = 0UL;
     448             : 
     449             :   /* Capture borrowed accounts */
     450             : 
     451           0 :   for( ulong j=0UL; j < ctx->txn_out->accounts.cnt; j++ ) {
     452           0 :     fd_pubkey_t * acc_key = &ctx->txn_out->accounts.keys[j];
     453           0 :     fd_account_meta_t * acc = ctx->txn_out->accounts.metas[j];
     454           0 :     if( !acc ) {
     455           0 :       continue;
     456           0 :     }
     457             : 
     458           0 :     ulong modified_idx = effects->modified_accounts_count;
     459           0 :     assert( modified_idx < modified_acct_cnt );
     460             : 
     461           0 :     fd_exec_test_acct_state_t * out_acct = &effects->modified_accounts[ modified_idx ];
     462           0 :     memset( out_acct, 0, sizeof(fd_exec_test_acct_state_t) );
     463             :     /* Copy over account content */
     464             : 
     465           0 :     memcpy( out_acct->address, acc_key, sizeof(fd_pubkey_t) );
     466           0 :     out_acct->lamports = acc->lamports;
     467           0 :     if( acc->dlen>0UL ) {
     468           0 :       out_acct->data =
     469           0 :         FD_SCRATCH_ALLOC_APPEND( l, alignof(pb_bytes_array_t),
     470           0 :                                     PB_BYTES_ARRAY_T_ALLOCSIZE( acc->dlen ) );
     471           0 :       if( FD_UNLIKELY( _l > output_end ) ) {
     472           0 :         fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     473           0 :         return 0UL;
     474           0 :       }
     475           0 :       out_acct->data->size = (pb_size_t)acc->dlen;
     476           0 :       fd_memcpy( out_acct->data->bytes, fd_account_data( acc ), acc->dlen );
     477           0 :     }
     478             : 
     479           0 :     out_acct->executable = acc->executable;
     480           0 :     memcpy( out_acct->owner, acc->owner, sizeof(fd_pubkey_t) );
     481             : 
     482           0 :     effects->modified_accounts_count++;
     483           0 :   }
     484             : 
     485             :   /* Capture return data */
     486           0 :   fd_txn_return_data_t * return_data = &ctx->txn_out->details.return_data;
     487           0 :   if( return_data->len>0UL ) {
     488           0 :     effects->return_data = FD_SCRATCH_ALLOC_APPEND(l, alignof(pb_bytes_array_t),
     489           0 :                                 PB_BYTES_ARRAY_T_ALLOCSIZE( return_data->len ) );
     490           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     491           0 :       fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     492           0 :       return 0UL;
     493           0 :     }
     494           0 :     effects->return_data->size = (pb_size_t)return_data->len;
     495           0 :     fd_memcpy( effects->return_data->bytes, return_data->data, return_data->len );
     496           0 :   }
     497             : 
     498           0 :   ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     499           0 :   fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     500             : 
     501           0 :   *output = effects;
     502           0 :   return actual_end - (ulong)output_buf;
     503           0 : }

Generated by: LCOV version 1.14