LCOV - code coverage report
Current view: top level - flamenco/runtime/tests - fd_instr_harness.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 329 351 93.7 %
Date: 2026-10-22 18:10:43 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include "fd_solfuzz_private.h"
       2             : #include "fd_instr_harness.h"
       3             : #include "../fd_executor.h"
       4             : #include "../fd_runtime.h"
       5             : #include "../program/fd_bpf_loader_program.h"
       6             : #include "../program/fd_precompiles.h"
       7             : #include "../fd_system_ids.h"
       8             : #include "../../progcache/fd_progcache_admin.h"
       9             : #include "../../alpenglow/fd_alpenglow.h"
      10             : #include "../../log_collector/fd_log_collector.h"
      11             : 
      12             : void
      13             : fd_solfuzz_pb_instr_ctx_create( fd_solfuzz_runner_t *                runner,
      14             :                                 fd_exec_instr_ctx_t *                ctx,
      15       55383 :                                 fd_exec_test_instr_context_t const * test_ctx ) {
      16             : 
      17       55383 :   memset( ctx, 0, sizeof(fd_exec_instr_ctx_t) );
      18             : 
      19             :   /* Create temporary fork for account loading */
      20             : 
      21       55383 :   runner->bank->progcache_fork_id    = fd_progcache_attach_child( runner->progcache->join, fd_progcache_fork_id_initial() );
      22       55383 :   runner->bank->accdb_fork_id        = fd_accdb_attach_child( runner->accdb, runner->root_fork_id );
      23       55383 :   runner->bank->parent_accdb_fork_id = runner->bank->accdb_fork_id;
      24             : 
      25       55383 :   fd_txn_in_t *  txn_in  = fd_spad_alloc( runner->spad, alignof(fd_txn_in_t), sizeof(fd_txn_in_t) );
      26       55383 :   fd_txn_out_t * txn_out = fd_spad_alloc( runner->spad, alignof(fd_txn_out_t), sizeof(fd_txn_out_t) );
      27             : 
      28       55383 :   fd_log_collector_t * log = fd_spad_alloc( runner->spad, alignof(fd_log_collector_t), sizeof(fd_log_collector_t) );
      29             : 
      30       55383 :   fd_runtime_t * runtime = runner->runtime;
      31             : 
      32       55383 :   runtime->log.log_collector = log;
      33             : 
      34       55383 :   ctx->txn_out = txn_out;
      35       55383 :   ctx->txn_in  = txn_in;
      36             : 
      37       55383 :   fd_memset( txn_out->accounts.keys,       0, sizeof(fd_pubkey_t)*MAX_TX_ACCOUNT_LOCKS );
      38       55383 :   fd_memset( runtime->accounts.account,    0, sizeof(fd_acc_t)*MAX_TX_ACCOUNT_LOCKS );
      39       55383 :   fd_memset( runtime->accounts.executable, 0, sizeof(fd_acc_t)*MAX_TX_ACCOUNT_LOCKS );
      40     3588952 :   for( ulong j=0UL; j<MAX_TX_ACCOUNT_LOCKS; j++ ) {
      41     3533569 :     txn_out->accounts.account[ j ]    = &runtime->accounts.account[ j ];
      42     3533569 :     txn_out->accounts.executable[ j ] = &runtime->accounts.executable[ j ];
      43     3533569 :   }
      44       55383 :   txn_out->accounts.executable_cnt = 0UL;
      45       55383 :   fd_memset( txn_out->accounts.executable_from_parent, 0, sizeof(txn_out->accounts.executable_from_parent) );
      46       55383 :   fd_memset( txn_out->accounts.executable_pd_write,    0, sizeof(txn_out->accounts.executable_pd_write) );
      47       55383 :   txn_out->accounts.executable_skipped_cnt = 0;
      48             : 
      49             :   /* Bank manager */
      50       55383 :   fd_banks_clear_bank( runner->banks, runner->bank );
      51             : 
      52             :   /* Restore features */
      53       55383 :   FD_TEST( test_ctx->has_features );
      54       55383 :   fd_features_t * features = &runner->bank->f.features;
      55       55383 :   fd_exec_test_feature_set_t const * feature_set = &test_ctx->features;
      56       55383 :   FD_TEST( fd_solfuzz_pb_restore_features( features, feature_set ) );
      57             : 
      58             :   /* Blockhash queue init */
      59       55383 :   ulong blockhash_seed; FD_TEST( fd_rng_secure( &blockhash_seed, sizeof(ulong) ) );
      60       55383 :   fd_blockhashes_t * blockhashes = fd_blockhashes_init( &runner->bank->f.block_hash_queue, blockhash_seed );
      61       55383 :   fd_blockhash_info_t * placeholder = fd_blockhash_deq_push_tail_nocopy( blockhashes->d.deque );
      62       55383 :   fd_memset( placeholder, 0, sizeof(fd_hash_t) );
      63       55383 :   placeholder->hash_index = 0UL;
      64             : 
      65             :   /* Set up instruction context */
      66       55383 :   fd_instr_info_t * info = fd_instr_info_new( &runtime->instr.trace[ 0UL ] );
      67       55383 :   info->stack_height = 1;
      68             : 
      69             :   /* Set up mock txn descriptor and payload
      70             :      FIXME: More fields may need to be initialized. This seems to be
      71             :      the minimal set of fields needed to retain full context for
      72             :      precompile execution. */
      73       55383 :   fd_txn_p_t * txn            = fd_spad_alloc_check( runner->spad, alignof(fd_txn_p_t), sizeof(fd_txn_p_t) );
      74       55383 :   fd_txn_t *   txn_descriptor = TXN( txn );
      75             : 
      76       55383 :   txn->payload_sz = 0UL;
      77       55383 :   if( test_ctx->data ) {
      78       47835 :     if( FD_UNLIKELY( test_ctx->data->size>FD_INSTR_DATA_MAX ) ) {
      79           0 :       FD_LOG_ERR(( "invariant violation: instr data sz is too large %u > %lu", test_ctx->data->size, FD_INSTR_DATA_MAX ));
      80           0 :     }
      81       47835 :     info->data_sz = (ushort)test_ctx->data->size;
      82       47835 :     memcpy( info->data, test_ctx->data->bytes, info->data_sz );
      83             : 
      84       47835 :     ulong payload_sz = fd_ulong_min( test_ctx->data->size, sizeof(txn->payload) );
      85       47835 :     memcpy( txn->payload, test_ctx->data->bytes, payload_sz );
      86       47835 :     txn->payload_sz = (ushort)payload_sz;
      87       47835 :   }
      88       55383 :   txn_descriptor->transaction_version = FD_TXN_VLEGACY;
      89       55383 :   txn_descriptor->acct_addr_cnt       = (ushort)test_ctx->accounts_count;
      90       55383 :   txn_descriptor->instr_cnt           = 1;
      91       55383 :   txn_descriptor->instr[0]            = (fd_txn_instr_t) {
      92       55383 :     .acct_cnt = (ushort)test_ctx->accounts_count,
      93       55383 :     .data_off = 0,
      94       55383 :     .data_sz  = (ushort)txn->payload_sz,
      95       55383 :   };
      96             : 
      97       55383 :   runtime->log.enable_log_collector = 0;
      98             : 
      99       55383 :   fd_compute_budget_details_new( &txn_out->details.compute_budget );
     100       55383 :   runtime->instr.stack_sz            = 0;
     101       55383 :   txn_out->accounts.cnt     = 0UL;
     102       55383 :   txn_out->accounts.executable_cnt   = 0UL;
     103             : 
     104       55383 :   txn_out->details.loaded_accounts_data_size = 0UL;
     105       55383 :   txn_out->details.accounts_resize_delta     = 0L;
     106             : 
     107       55383 :   memset( txn_out->details.return_data.program_id.key, 0, sizeof(fd_pubkey_t) );
     108       55383 :   txn_out->details.return_data.len = 0;
     109             : 
     110       55383 :   runtime->log.capture_ctx    = NULL;
     111       55383 :   runtime->log.dump_proto_ctx = NULL;
     112       55383 :   runtime->log.txn_dump_ctx   = NULL;
     113             : 
     114       55383 :   runtime->instr.trace_length = 1UL;
     115             : 
     116       55383 :   txn_out->err.exec_err       = 0;
     117       55383 :   txn_out->err.exec_err_kind  = FD_EXECUTOR_ERR_KIND_NONE;
     118       55383 :   runtime->instr.current_idx  = 0;
     119             : 
     120       55383 :   txn_in->txn                                        = txn;
     121       55383 :   txn_out->details.compute_budget.compute_unit_limit = test_ctx->cu_avail;
     122       55383 :   txn_out->details.compute_budget.compute_meter      = test_ctx->cu_avail;
     123       55383 :   runtime->log.enable_vm_tracing                     = runner->enable_vm_tracing;
     124       55383 :   runtime->log.tracing_mem                           = runner->enable_vm_tracing ?
     125           0 :                                                        fd_spad_alloc_check( runner->spad, FD_RUNTIME_VM_TRACE_STATIC_ALIGN, FD_RUNTIME_VM_TRACE_STATIC_FOOTPRINT * FD_MAX_INSTRUCTION_STACK_DEPTH ) :
     126       55383 :                                                        NULL;
     127             : 
     128             :   /* Prepare borrowed account table (correctly handles aliasing) */
     129             : 
     130       55383 :   if( FD_UNLIKELY( test_ctx->accounts_count > MAX_TX_ACCOUNT_LOCKS ) ) {
     131           0 :     FD_LOG_ERR(( "invariant violation: too many accounts (%lu > %lu)",
     132           0 :                  (ulong)test_ctx->accounts_count, (ulong)MAX_TX_ACCOUNT_LOCKS ));
     133           0 :   }
     134             : 
     135             :   /* Load accounts from input */
     136             : 
     137             :   /* Mimic Agave's mock_compile_message: put the program + referenced
     138             :      instruction accounts in the first txn slots, the rest after. */
     139       55383 :   uchar account_in_message[ MAX_TX_ACCOUNT_LOCKS ] = {0};
     140       55383 :   uint  input_txn_idx     [ MAX_TX_ACCOUNT_LOCKS ];
     141             : 
     142      460438 :   for( ulong i=0UL; i<test_ctx->instr_accounts_count; i++ ) {
     143      405055 :     uint index = test_ctx->instr_accounts[ i ].index;
     144      405055 :     if( FD_UNLIKELY( index>=test_ctx->accounts_count ) ) {
     145           0 :       FD_LOG_ERR(( "invariant violation: instruction account index out of range (%u >= %u)",
     146           0 :                    index, test_ctx->accounts_count ));
     147           0 :     }
     148      405055 :     account_in_message[ index ] = 1;
     149      405055 :   }
     150             : 
     151       55383 :   ulong program_idx = ULONG_MAX;
     152      166612 :   for( ulong i=0UL; i<test_ctx->accounts_count; i++ ) {
     153      166612 :     if( !memcmp( test_ctx->accounts[ i ].address, test_ctx->program_id, sizeof(fd_pubkey_t) ) ) {
     154       55532 :       account_in_message[ i ] = 1;
     155       55532 :       program_idx = i;
     156       55532 :       break;
     157       55532 :     }
     158      166612 :   }
     159             : 
     160             :   /* Ensure the program id is in the set of accounts */
     161       55383 :   FD_TEST( program_idx!=ULONG_MAX );
     162             : 
     163             :   /* Compile the message by filling the transaction accounts with
     164             :      only accounts referenced by the instruction. */
     165       55383 :   uint message_account_cnt = 0U;
     166       55383 :   uint tail_txn_idx        = test_ctx->accounts_count;
     167      653021 :   for( ulong i=0UL; i<test_ctx->accounts_count; i++ ) {
     168      597638 :     input_txn_idx[ i ] = account_in_message[ i ] ? message_account_cnt++ : --tail_txn_idx;
     169      597638 :   }
     170             : 
     171       55383 :   info->program_id      = (uchar)input_txn_idx[ program_idx ];
     172       55383 :   txn_out->accounts.cnt = message_account_cnt;
     173             : 
     174       55383 :   ulong loaded_data_sz = 0UL;
     175      651072 :   for( ulong j=0UL; j < test_ctx->accounts_count; j++ ) {
     176      595689 :     if( !account_in_message[j] ) continue;
     177             : 
     178      315062 :     ulong txn_idx = input_txn_idx[j];
     179      315062 :     fd_pubkey_t * acc_key = (fd_pubkey_t *)test_ctx->accounts[j].address;
     180             : 
     181      315062 :     pb_bytes_array_t const * in_data = fd_solfuzz_acct_data( &test_ctx->accounts[j] );
     182      315062 :     uint dlen = in_data ? in_data->size : 0U;
     183      315062 :     loaded_data_sz += dlen;
     184      315062 :     if( FD_UNLIKELY( dlen>FD_RUNTIME_ACC_SZ_MAX || loaded_data_sz>FD_VM_LOADED_ACCOUNTS_DATA_SIZE_LIMIT ) ) {
     185           0 :       FD_LOG_ERR(( "account data too large (dlen=%u total=%lu)", dlen, loaded_data_sz ));
     186           0 :     }
     187      315062 :     uchar * data_buf = fd_spad_alloc( runner->spad, FD_ACCOUNT_REC_ALIGN, FD_RUNTIME_ACC_SZ_MAX );
     188      315062 :     if( dlen ) {
     189      282209 :       fd_memcpy( data_buf, in_data->bytes, dlen );
     190      282209 :     }
     191             : 
     192             :     /* Initialize entry with in-memory account data (no DB backing) */
     193      315062 :     fd_acc_t * acc = txn_out->accounts.account[txn_idx];
     194      315062 :     memcpy( acc->pubkey, acc_key->key, 32 );
     195      315062 :     memcpy( acc->owner, test_ctx->accounts[j].owner, 32 );
     196      315062 :     acc->lamports   = test_ctx->accounts[j].lamports;
     197      315062 :     acc->executable = test_ctx->accounts[j].executable;
     198      315062 :     acc->data_len   = dlen;
     199      315062 :     acc->data       = data_buf;
     200      315062 :     acc->_writable  = 1;
     201      315062 :     acc->commit     = 0;
     202             : 
     203      315062 :     txn_out->accounts.is_writable[txn_idx] = 1U;
     204      315062 :     runtime->accounts.refcnt[txn_idx] = 0UL;
     205      315062 :     txn_out->accounts.keys[txn_idx] = *acc_key;
     206      315062 :   }
     207             : 
     208             :   /* Load in executable accounts */
     209      652955 :   for( ulong i=0UL; i<test_ctx->accounts_count; i++ ) {
     210      597572 :     fd_exec_test_acct_state_t const * prog  = &test_ctx->accounts[i];
     211      597572 :     fd_pubkey_t const *               owner = fd_type_pun_const( prog->owner );
     212             : 
     213      597572 :     if( !fd_executor_pubkey_is_bpf_loader( owner ) ) {
     214      543320 :       continue;
     215      543320 :     }
     216             : 
     217       54252 :     if( FD_UNLIKELY( !memcmp( owner, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
     218       30778 :       pb_bytes_array_t const * prog_data = fd_solfuzz_acct_data( prog );
     219       30778 :       if( FD_UNLIKELY( !prog_data ) ) continue;
     220             : 
     221       28267 :       fd_bpf_state_t program_loader_state[1];
     222       28267 :       int err = fd_bpf_loader_program_get_state2( prog_data->bytes, prog_data->size, program_loader_state );
     223       28267 :       if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
     224        4031 :         continue;
     225        4031 :       }
     226             : 
     227       24236 :       if( program_loader_state->discriminant!=FD_BPF_STATE_PROGRAM ) {
     228       14231 :         continue;
     229       14231 :       }
     230             : 
     231       10005 :       fd_pubkey_t * programdata_acc = &program_loader_state->inner.program.programdata_address;
     232             : 
     233       10005 :       fd_exec_test_acct_state_t const * pd = NULL;
     234       81616 :       for( ulong j=0UL; j<test_ctx->accounts_count; j++ ) {
     235       79821 :         if( !memcmp( test_ctx->accounts[j].address, programdata_acc, sizeof(fd_pubkey_t) ) ) {
     236        8210 :           pd = &test_ctx->accounts[j];
     237        8210 :           break;
     238        8210 :         }
     239       79821 :       }
     240       10005 :       pb_bytes_array_t const * pd_data = pd ? fd_solfuzz_acct_data( pd ) : NULL;
     241       10005 :       if( FD_UNLIKELY( pd==NULL || !pd_data ) ) {
     242        1981 :         continue;
     243        1981 :       }
     244             : 
     245        8024 :       FD_TEST( txn_out->accounts.executable_cnt < MAX_TX_ACCOUNT_LOCKS );
     246        8024 :       fd_acc_t * exe = txn_out->accounts.executable[ txn_out->accounts.executable_cnt ];
     247        8024 :       memcpy( exe->pubkey, programdata_acc->key, 32 );
     248        8024 :       memcpy( exe->owner,  pd->owner, sizeof(fd_pubkey_t) );
     249             :       /* Agave loads a program into its ProgramCache from the
     250             :          programdata bytes independently of the on-chain lamports
     251             :          snapshot.  An instruction fixture may capture a programdata
     252             :          account with zero lamports (a real 0-lamport account would
     253             :          otherwise have all its other fields cleared between
     254             :          transactions), and still expect the program to execute.  The
     255             :          runtime treats an executable account as "deployed" only when it
     256             :          exists, so present the programdata as existing whenever it
     257             :          carries program data.  This executable account is read-only on
     258             :          the invoke path and its lamports are never consumed, only used
     259             :          as an existence gate. */
     260        8024 :       exe->lamports   = ( !pd->lamports && pd_data->size ) ? 1UL : pd->lamports;
     261        8024 :       exe->executable = pd->executable;
     262        8024 :       exe->data_len   = pd_data->size;
     263        8024 :       exe->data       = (uchar *)pd_data->bytes;
     264        8024 :       txn_out->accounts.executable_cnt++;
     265        8024 :     }
     266       54252 :   }
     267             : 
     268             :   /* Load instruction accounts */
     269             : 
     270       55383 :   if( FD_UNLIKELY( test_ctx->instr_accounts_count > FD_TXN_INSTR_ACCT_MAX ) ) {
     271           0 :     FD_LOG_ERR(( "invariant violation: too many instruction accounts (%lu > %lu)",
     272           0 :                  (ulong)test_ctx->instr_accounts_count, (ulong)FD_TXN_INSTR_ACCT_MAX ));
     273           0 :   }
     274             : 
     275             :   /* Restore sysvar cache */
     276       55383 :   fd_sysvar_cache_t * sysvar_cache = &runner->bank->f.sysvar_cache;
     277       55383 :   ctx->sysvar_cache = sysvar_cache;
     278      651707 :   for( ulong i=0UL; i<test_ctx->accounts_count; i++ ) {
     279      596324 :     fd_exec_test_acct_state_t const * account = &test_ctx->accounts[i];
     280      596324 :     pb_bytes_array_t const *          data    = fd_solfuzz_acct_data( account );
     281      596324 :     if( FD_UNLIKELY( !data ) ) continue;
     282             : 
     283      555796 :     fd_pubkey_t const * address = fd_type_pun_const( account->address );
     284      555796 :     fd_sysvar_cache_restore_one( sysvar_cache,
     285      555796 :                                  address,
     286      555796 :                                  account->lamports,
     287      555796 :                                  data->bytes,
     288      555796 :                                  data->size );
     289      555796 :   }
     290             : 
     291       55383 :   ctx->runtime = runtime;
     292             : 
     293       55383 :   fd_sol_sysvar_clock_t clock_[1];
     294       55383 :   fd_sol_sysvar_clock_t * clock = fd_sysvar_cache_clock_read( ctx->sysvar_cache, clock_ );
     295       55383 :   FD_TEST( clock );
     296       55383 :   runner->bank->f.slot = clock->slot;
     297             : 
     298       55383 :   runner->bank->progcache_fork_id = fd_progcache_attach_child( runner->progcache->join, runner->bank->progcache_fork_id );
     299             : 
     300       55383 :   fd_epoch_schedule_t epoch_schedule_[1];
     301       55383 :   fd_epoch_schedule_t * epoch_schedule = fd_sysvar_cache_epoch_schedule_read( ctx->sysvar_cache, epoch_schedule_ );
     302       55383 :   FD_TEST( epoch_schedule );
     303       55383 :   runner->bank->f.epoch_schedule = *epoch_schedule;
     304             : 
     305       55383 :   fd_rent_t rent_[1];
     306       55383 :   fd_rent_t * rent = fd_sysvar_cache_rent_read( ctx->sysvar_cache, rent_ );
     307       55383 :   FD_TEST( rent );
     308       55383 :   runner->bank->f.rent = *rent;
     309       55383 :   runner->bank->f.alpenglow_migration_slot = fd_alpenglow_migration_slot( runner->bank, runner->accdb );
     310             : 
     311       55675 :   if( !fd_sysvar_cache_recent_hashes_is_empty( sysvar_cache ) ) {
     312       55675 :     uchar const * rbh_data  = sysvar_cache->bin_recent_hashes;
     313       55675 :     ulong         rbh_len   = FD_LOAD( ulong, rbh_data );
     314       55675 :     ulong         entry_off = sizeof(ulong) + ((rbh_len - 1UL) * 40UL);
     315       55675 :     uchar const * entry     = rbh_data + entry_off;
     316       55675 :     FD_TEST( entry_off+40UL <= sysvar_cache->desc[ FD_SYSVAR_recent_hashes_IDX ].data_sz );
     317             : 
     318       55675 :     fd_blockhashes_t * blockhashes = &runner->bank->f.block_hash_queue;
     319       55675 :     fd_blockhashes_pop_new( blockhashes );
     320       55675 :     fd_hash_t hash = FD_LOAD( fd_hash_t, entry );
     321       55675 :     fd_blockhash_info_t * info = fd_blockhashes_push_new( blockhashes, &hash );
     322       55675 :     info->lamports_per_signature = runner->bank->f.rbh_lamports_per_sig =
     323       55675 :         FD_LOAD( ulong, entry+32UL );
     324       55675 :   }
     325             : 
     326             :   /* Agave compiles the instruction into a message, OR-ing duplicate
     327             :      accounts' writable/signer flags. */
     328       55383 :   uchar instr_is_writable[ FD_TXN_ACCT_ADDR_MAX ] = {0};
     329       55383 :   uchar instr_is_signer  [ FD_TXN_ACCT_ADDR_MAX ] = {0};
     330       55383 :   int   bpf_upgradeable_present = !memcmp( test_ctx->program_id, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) );
     331      460591 :   for( ulong j=0UL; j < test_ctx->instr_accounts_count; j++ ) {
     332      405208 :     uint index = test_ctx->instr_accounts[j].index;
     333             : 
     334      405208 :     instr_is_writable[index] = (uchar)( instr_is_writable[index] | test_ctx->instr_accounts[j].is_writable );
     335      405208 :     instr_is_signer[index]   = (uchar)( instr_is_signer[index]   | test_ctx->instr_accounts[j].is_signer );
     336      405208 :     if( !memcmp( test_ctx->accounts[index].address, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) ) {
     337        1847 :       bpf_upgradeable_present = 1;
     338        1847 :     }
     339      405208 :   }
     340             : 
     341       55383 :   uchar acc_idx_seen[ FD_TXN_ACCT_ADDR_MAX ] = {0};
     342      460390 :   for( ulong j=0UL; j < test_ctx->instr_accounts_count; j++ ) {
     343      405007 :     uint index = test_ctx->instr_accounts[j].index;
     344             : 
     345             :     /* A program account is demoted to read-only unless the upgradeable
     346             :        loader is present. */
     347      405007 :     uchar is_writable = instr_is_writable[index];
     348      405007 :     if( !bpf_upgradeable_present && !memcmp( test_ctx->accounts[index].address, test_ctx->program_id, sizeof(fd_pubkey_t) ) ) {
     349       13434 :       is_writable = 0;
     350       13434 :     }
     351             : 
     352             :     /* Setup instruction accounts */
     353      405007 :     fd_instr_info_setup_instr_account( info,
     354      405007 :                                        acc_idx_seen,
     355      405007 :                                        (ushort)input_txn_idx[index],
     356      405007 :                                        (ushort)j,
     357      405007 :                                        (ushort)j,
     358      405007 :                                        is_writable,
     359      405007 :                                        instr_is_signer[index] );
     360      405007 :   }
     361       55383 :   info->acct_cnt          = (ushort)test_ctx->instr_accounts_count;
     362             : 
     363       55383 :   ctx->instr              = info;
     364       55383 :   ctx->runtime->progcache = runner->progcache;
     365       55383 :   ctx->runtime->accdb     = runner->accdb;
     366             : 
     367       55383 :   runtime->log.enable_log_collector = 0;
     368             : 
     369       55383 :   fd_log_collector_init( ctx->runtime->log.log_collector, 0 );
     370       55383 :   fd_base58_encode_32( txn_out->accounts.keys[ ctx->instr->program_id ].uc, NULL, ctx->program_id_base58 );
     371       55383 : }
     372             : 
     373             : void
     374             : fd_solfuzz_pb_instr_ctx_destroy( fd_solfuzz_runner_t * runner,
     375       55654 :                                  fd_exec_instr_ctx_t * ctx ) {
     376       55654 :   if( !ctx ) return;
     377             : 
     378       55654 :   fd_progcache_reset( runner->progcache->join );
     379             : 
     380             :   /* Purge the fork attached in ctx_create so the accdb fork pool slot
     381             :      is released back for reuse.  Without this, repeated harness
     382             :      invocations (e.g. under a fuzzer) exhaust max_live_slots. */
     383       55654 :   fd_accdb_purge( runner->accdb, runner->bank->accdb_fork_id );
     384       55654 :   int charge_busy = 0;
     385       55654 :   fd_accdb_background( runner->accdb, &charge_busy );
     386             : 
     387       55654 : }
     388             : 
     389             : /* Txn index for addr among the compiled-message accounts [0,cnt).
     390             :    Returns ULONG_MAX if not found. */
     391             : static ulong
     392      478833 : instr_harness_idx_of( fd_txn_out_t const * txn_out, uchar const * addr ) {
     393     5049145 :   for( ulong i=0UL; i<txn_out->accounts.cnt; i++ ) {
     394     4814957 :     if( !memcmp( txn_out->accounts.keys[i].key, addr, sizeof(fd_pubkey_t) ) ) return i;
     395     4814957 :   }
     396      234188 :   return ULONG_MAX;
     397      478833 : }
     398             : 
     399             : ulong
     400             : fd_solfuzz_pb_instr_run( fd_solfuzz_runner_t * runner,
     401             :                          void const *          input_,
     402             :                          void **               output_,
     403             :                          void *                output_buf,
     404       46828 :                          ulong                 output_bufsz ) {
     405       46828 :   fd_exec_test_instr_context_t const * input  = fd_type_pun_const( input_ );
     406       46828 :   fd_exec_test_instr_effects_t **      output = fd_type_pun( output_ );
     407             : 
     408             :   /* Convert the Protobuf inputs to a fd_exec context */
     409       46828 :   fd_exec_instr_ctx_t ctx[1];
     410       46828 :   fd_solfuzz_pb_instr_ctx_create( runner, ctx, input );
     411             : 
     412       46828 :   fd_instr_info_t * instr = (fd_instr_info_t *) ctx->instr;
     413             : 
     414             :   /* Execute the test */
     415       46828 :   int exec_result = fd_execute_instr( ctx->runtime, runner->bank, ctx->txn_in, ctx->txn_out, instr );
     416             : 
     417             :   /* Allocate space to capture outputs */
     418       46828 :   ulong output_end = (ulong)output_buf + output_bufsz;
     419       46828 :   FD_SCRATCH_ALLOC_INIT( l, output_buf );
     420             : 
     421       46828 :   fd_exec_test_instr_effects_t * effects =
     422       46828 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_instr_effects_t),
     423       46828 :                                 sizeof (fd_exec_test_instr_effects_t) );
     424       46828 :   if( FD_UNLIKELY( _l > output_end ) ) {
     425           0 :     fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     426           0 :     return 0UL;
     427           0 :   }
     428       46828 :   fd_memset( effects, 0, sizeof(fd_exec_test_instr_effects_t) );
     429             : 
     430             :   /* Capture error code */
     431             : 
     432       46828 :   effects->result   = -exec_result;
     433       46828 :   effects->cu_avail = ctx->txn_out->details.compute_budget.compute_meter;
     434             : 
     435             :   /* Don't capture custom error codes if the program is a precompile */
     436       46828 :   if( FD_LIKELY( effects->result ) ) {
     437       30656 :     int program_id_idx = ctx->instr[ 0UL ].program_id;
     438       30656 :     if( exec_result==FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR &&
     439       30656 :         fd_executor_lookup_native_precompile_program( &ctx->txn_out->accounts.keys[ program_id_idx ] )==NULL ) {
     440        1197 :       effects->custom_err = ctx->txn_out->err.custom_err;
     441        1197 :     }
     442       30656 :   }
     443             : 
     444             :   /* Allocate space for captured accounts */
     445       46828 :   ulong modified_acct_cnt = input->accounts_count;
     446             : 
     447       46828 :   fd_exec_test_acct_state_t * modified_accts =
     448       46828 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_acct_state_t),
     449       46828 :                                 sizeof (fd_exec_test_acct_state_t) * modified_acct_cnt );
     450       46828 :   if( FD_UNLIKELY( _l > output_end ) ) {
     451           0 :     fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     452           0 :     return 0;
     453           0 :   }
     454       46828 :   effects->modified_accounts       = modified_accts;
     455       46828 :   effects->modified_accounts_count = 0UL;
     456             : 
     457             :   /* Capture any accounts which may have changed */
     458             : 
     459      525684 :   for( ulong j=0UL; j < input->accounts_count; j++ ) {
     460      478856 :     fd_exec_test_acct_state_t const * in_acct = &input->accounts[j];
     461             : 
     462      478856 :     ulong         lamports;
     463      478856 :     uchar const * data;
     464      478856 :     ulong         data_len;
     465      478856 :     int           executable;
     466      478856 :     uchar const * owner;
     467             : 
     468             :     /* Capture the account from the message in case it was modified,
     469             :        or just report the input account otherwise. */
     470      478856 :     ulong txn_idx = instr_harness_idx_of( ctx->txn_out, in_acct->address );
     471      478856 :     if( txn_idx!=ULONG_MAX ) {
     472      244664 :       fd_acc_t const * acc = ctx->txn_out->accounts.account[txn_idx];
     473      244664 :       lamports   = acc->lamports;
     474      244664 :       data       = acc->data;
     475      244664 :       data_len   = acc->data_len;
     476      244664 :       executable = acc->executable;
     477      244664 :       owner      = acc->owner;
     478      244664 :     } else {
     479      234192 :       pb_bytes_array_t const * in_data = fd_solfuzz_acct_data( in_acct );
     480      234192 :       lamports   = in_acct->lamports;
     481      234192 :       data       = in_data ? in_data->bytes : NULL;
     482      234192 :       data_len   = in_data ? in_data->size  : 0UL;
     483      234192 :       executable = in_acct->executable;
     484      234192 :       owner      = in_acct->owner;
     485      234192 :     }
     486             : 
     487      478856 :     ulong modified_idx = effects->modified_accounts_count;
     488      478856 :     if( FD_UNLIKELY( modified_idx >= modified_acct_cnt ) ) {
     489           0 :       FD_LOG_CRIT(( "invalid modified account index" ));
     490           0 :     }
     491             : 
     492      478856 :     fd_exec_test_acct_state_t * out_acct = &effects->modified_accounts[ modified_idx ];
     493      478856 :     memset( out_acct, 0, sizeof(fd_exec_test_acct_state_t) );
     494             : 
     495             :     /* Copy over account content */
     496      478856 :     memcpy( out_acct->address, in_acct->address, sizeof(fd_pubkey_t) );
     497      478856 :     out_acct->lamports = lamports;
     498      478856 :     fd_solfuzz_acct_set_data_hash( out_acct, data, data_len );
     499             : 
     500      478856 :     out_acct->executable = executable;
     501      478856 :     memcpy( out_acct->owner, owner, sizeof(fd_pubkey_t) );
     502             : 
     503      478856 :     effects->modified_accounts_count++;
     504      478856 :   }
     505             : 
     506       46828 :   fd_solfuzz_direct_mapping_handle_cu_exhaustion(
     507       46828 :       runner, effects->cu_avail, effects->result,
     508       46828 :       effects->modified_accounts, (pb_size_t)effects->modified_accounts_count );
     509             : 
     510             :   /* Capture return data */
     511       46828 :   fd_txn_return_data_t * return_data = &ctx->txn_out->details.return_data;
     512       46828 :   effects->return_data_hash = fd_solfuzz_hash( return_data->data, return_data->len );
     513             : 
     514       46828 :   ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     515       46828 :   fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     516             : 
     517       46828 :   *output = effects;
     518       46828 :   return actual_end - (ulong)output_buf;
     519       46828 : }

Generated by: LCOV version 1.14