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

          Line data    Source code
       1             : #include "fd_solfuzz.h"
       2             : #include "fd_solfuzz_private.h"
       3             : #include "fd_txn_harness.h"
       4             : #include "../fd_runtime.h"
       5             : #include "../fd_executor.h"
       6             : #include "../fd_txn_account.h"
       7             : #include "../program/fd_builtin_programs.h"
       8             : #include "../sysvar/fd_sysvar_clock.h"
       9             : #include "../sysvar/fd_sysvar_epoch_schedule.h"
      10             : #include "../sysvar/fd_sysvar_recent_hashes.h"
      11             : #include "../sysvar/fd_sysvar_rent.h"
      12             : #include "../sysvar/fd_sysvar_slot_hashes.h"
      13             : #include "../sysvar/fd_sysvar_stake_history.h"
      14             : #include "../../accdb/fd_accdb_impl_v1.h"
      15             : #include "../../log_collector/fd_log_collector.h"
      16             : #include <assert.h>
      17             : 
      18             : /* Macros to append data to construct a serialized transaction
      19             :    without exceeding bounds */
      20           0 : #define FD_CHECKED_ADD_TO_TXN_DATA( _begin, _cur_data, _to_add, _sz ) __extension__({ \
      21           0 :    if( FD_UNLIKELY( (*_cur_data)+_sz>_begin+FD_TXN_MTU ) ) return ULONG_MAX;          \
      22           0 :    fd_memcpy( *_cur_data, _to_add, _sz );                                             \
      23           0 :    *_cur_data += _sz;                                                                 \
      24           0 : })
      25             : 
      26           0 : #define FD_CHECKED_ADD_CU16_TO_TXN_DATA( _begin, _cur_data, _to_add ) __extension__({ \
      27           0 :    do {                                                                               \
      28           0 :       uchar _buf[3];                                                                  \
      29           0 :       fd_bincode_encode_ctx_t _encode_ctx = { .data = _buf, .dataend = _buf+3 };      \
      30           0 :       fd_bincode_compact_u16_encode( &_to_add, &_encode_ctx );                        \
      31           0 :       ulong _sz = (ulong) ((uchar *)_encode_ctx.data - _buf );                        \
      32           0 :       FD_CHECKED_ADD_TO_TXN_DATA( _begin, _cur_data, _buf, _sz );                     \
      33           0 :    } while(0);                                                                        \
      34           0 : })
      35             : 
      36             : static void
      37           0 : fd_solfuzz_txn_ctx_destroy( fd_solfuzz_runner_t * runner ) {
      38           0 :   fd_accdb_clear( runner->accdb_admin );
      39           0 :   fd_progcache_clear( runner->progcache_admin );
      40             : 
      41             :   /* In order to check for leaks in the workspace, we need to compact the
      42             :      allocators. Without doing this, empty superblocks may be retained
      43             :      by the fd_alloc instance, which mean we cannot check for leaks. */
      44           0 :   fd_alloc_compact( runner->accdb_admin->funk->alloc );
      45           0 :   fd_alloc_compact( runner->progcache_admin->funk->alloc );
      46           0 : }
      47             : 
      48             : /* Creates transaction execution context for a single test case.
      49             :    Returns a parsed txn descriptor on success and NULL on failure. */
      50             : static fd_txn_p_t *
      51             : fd_solfuzz_pb_txn_ctx_create( fd_solfuzz_runner_t *              runner,
      52           0 :                               fd_exec_test_txn_context_t const * test_ctx ) {
      53           0 :   fd_accdb_user_t * accdb = runner->accdb;
      54           0 :   fd_funk_t *       funk  = fd_accdb_user_v1_funk( runner->accdb );
      55             : 
      56             :   /* Default slot */
      57           0 :   ulong slot = test_ctx->slot_ctx.slot ? test_ctx->slot_ctx.slot : 10; // Arbitrary default > 0
      58             : 
      59             :   /* Set up the funk transaction */
      60           0 :   fd_funk_txn_xid_t xid = { .ul = { slot, runner->bank->idx } };
      61           0 :   fd_funk_txn_xid_t parent_xid; fd_funk_txn_xid_set_root( &parent_xid );
      62           0 :   fd_accdb_attach_child        ( runner->accdb_admin,     &parent_xid, &xid );
      63           0 :   fd_progcache_txn_attach_child( runner->progcache_admin, &parent_xid, &xid );
      64             : 
      65             :   /* Set up slot context */
      66           0 :   fd_banks_clear_bank( runner->banks, runner->bank );
      67             : 
      68             :   /* Restore feature flags */
      69           0 :   fd_exec_test_feature_set_t const * feature_set = &test_ctx->epoch_ctx.features;
      70           0 :   fd_features_t * features_bm = fd_bank_features_modify( runner->bank );
      71           0 :   if( !fd_solfuzz_pb_restore_features( features_bm, feature_set ) ) {
      72           0 :     return NULL;
      73           0 :   }
      74             : 
      75             :   /* Set bank variables (defaults obtained from GenesisConfig::default
      76             :      in Agave) */
      77             : 
      78           0 :   fd_bank_slot_set( runner->bank, slot );
      79           0 :   fd_bank_parent_slot_set( runner->bank, fd_bank_slot_get( runner->bank ) - 1UL );
      80             : 
      81             :   /* Initialize builtin accounts */
      82           0 :   fd_builtin_programs_init( runner->bank, accdb, &xid, NULL );
      83             : 
      84             :   /* Load account states into funk (note this is different from the account keys):
      85             :     Account state = accounts to populate Funk
      86             :     Account keys = account keys that the transaction needs */
      87           0 :   for( ulong i = 0; i < test_ctx->account_shared_data_count; i++ ) {
      88             :     /* Load the accounts into the account manager
      89             :        Borrowed accounts get reset anyways - we just need to load the account somewhere */
      90           0 :     fd_solfuzz_pb_load_account( runner->runtime, accdb, &xid, &test_ctx->account_shared_data[i], 1, i, NULL );
      91           0 :   }
      92             : 
      93             :   /* Setup Bank manager */
      94           0 :   fd_fee_rate_governor_t * fee_rate_governor = fd_bank_fee_rate_governor_modify( runner->bank );
      95           0 :   fee_rate_governor->burn_percent                  = 50;
      96           0 :   fee_rate_governor->min_lamports_per_signature    = 0;
      97           0 :   fee_rate_governor->max_lamports_per_signature    = 0;
      98           0 :   fee_rate_governor->target_lamports_per_signature = 10000;
      99           0 :   fee_rate_governor->target_signatures_per_slot    = 20000;
     100             : 
     101             :   /* https://github.com/anza-xyz/agave/blob/v3.0.3/runtime/src/bank.rs#L1249-L1251 */
     102           0 :   fd_runtime_new_fee_rate_governor_derived( runner->bank, fd_bank_parent_signature_cnt_get( runner->bank ) );
     103             : 
     104           0 :   fd_bank_ticks_per_slot_set( runner->bank, 64 );
     105             : 
     106           0 :   fd_bank_slots_per_year_set( runner->bank, SECONDS_PER_YEAR * (1000000000.0 / (double)6250000) / (double)(fd_bank_ticks_per_slot_get( runner->bank )) );
     107             : 
     108             :   /* Ensure the presence of */
     109           0 :   fd_epoch_schedule_t epoch_schedule_[1];
     110           0 :   fd_epoch_schedule_t * epoch_schedule = fd_sysvar_epoch_schedule_read( funk, &xid, epoch_schedule_ );
     111           0 :   FD_TEST( epoch_schedule );
     112           0 :   fd_bank_epoch_schedule_set( runner->bank, *epoch_schedule );
     113             : 
     114           0 :   fd_rent_t rent[1];
     115           0 :   FD_TEST( fd_sysvar_rent_read( funk, &xid, rent ) );
     116           0 :   fd_bank_rent_set( runner->bank, *rent );
     117             : 
     118           0 :   uchar __attribute__((aligned(FD_SLOT_HASHES_GLOBAL_ALIGN))) slot_hashes_mem[ FD_SYSVAR_SLOT_HASHES_FOOTPRINT ];
     119           0 :   fd_slot_hashes_global_t * slot_hashes = fd_sysvar_slot_hashes_read( funk, &xid, slot_hashes_mem );
     120           0 :   FD_TEST( slot_hashes );
     121             : 
     122           0 :   fd_stake_history_t stake_history_[1];
     123           0 :   fd_stake_history_t * stake_history = fd_sysvar_stake_history_read( funk, &xid, stake_history_ );
     124           0 :   FD_TEST( stake_history );
     125             : 
     126           0 :   fd_sol_sysvar_clock_t clock_[1];
     127           0 :   fd_sol_sysvar_clock_t const * clock = fd_sysvar_clock_read( runner->accdb, &xid, clock_ );
     128           0 :   FD_TEST( clock );
     129             : 
     130             :   /* Setup vote states dummy account */
     131           0 :   fd_vote_states_t * vote_states = fd_vote_states_join( fd_vote_states_new( fd_bank_vote_states_locking_modify( runner->bank ), 64UL, 999UL ) );
     132           0 :   if( FD_UNLIKELY( !vote_states ) ) {
     133           0 :     fd_bank_vote_states_end_locking_modify( runner->bank );
     134           0 :     return NULL;
     135           0 :   }
     136           0 :   fd_bank_vote_states_end_locking_modify( runner->bank );
     137             : 
     138             :   /* Setup vote states dummy account */
     139           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 ), FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_TRANSACTION, 999UL ) );
     140           0 :   if( FD_UNLIKELY( !vote_states_prev ) ) {
     141           0 :     fd_bank_vote_states_prev_end_locking_modify( runner->bank );
     142           0 :     return NULL;
     143           0 :   }
     144           0 :   fd_bank_vote_states_prev_end_locking_modify( runner->bank );
     145             : 
     146             :   /* Setup vote states dummy account */
     147           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 ), FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_TRANSACTION, 999UL ) );
     148           0 :   if( FD_UNLIKELY( !vote_states_prev_prev ) ) {
     149           0 :     fd_bank_vote_states_prev_prev_end_locking_modify( runner->bank );
     150           0 :     return NULL;
     151           0 :   }
     152           0 :   fd_bank_vote_states_prev_prev_end_locking_modify( runner->bank );
     153             : 
     154             :   /* Epoch schedule and rent get set from the epoch bank */
     155           0 :   fd_sysvar_epoch_schedule_init( runner->bank, runner->accdb, &xid, NULL );
     156           0 :   fd_sysvar_rent_init( runner->bank, runner->accdb, &xid, NULL );
     157             : 
     158             :   /* Blockhash queue is given in txn message. We need to populate the following two fields:
     159             :      - block_hash_queue
     160             :      - recent_block_hashes */
     161           0 :   ulong num_blockhashes = test_ctx->blockhash_queue_count;
     162             : 
     163             :   /* Blockhash queue init */
     164           0 :   ulong blockhash_seed; FD_TEST( fd_rng_secure( &blockhash_seed, sizeof(ulong) ) );
     165           0 :   fd_blockhashes_t * blockhashes = fd_blockhashes_init( fd_bank_block_hash_queue_modify( runner->bank ), blockhash_seed );
     166             : 
     167             :   // Save lamports per signature for most recent blockhash, if sysvar cache contains recent block hashes
     168           0 :   uchar __attribute__((aligned(FD_SYSVAR_RECENT_HASHES_ALIGN))) rbh_mem[FD_SYSVAR_RECENT_HASHES_FOOTPRINT];
     169           0 :   fd_recent_block_hashes_t const * rbh_sysvar = fd_sysvar_recent_hashes_read( funk, &xid, rbh_mem );
     170           0 :   fd_recent_block_hashes_t rbh[1];
     171           0 :   if( rbh_sysvar ) {
     172           0 :     rbh->hashes = rbh_sysvar->hashes;
     173           0 :   }
     174             : 
     175           0 :   if( rbh_sysvar && !deq_fd_block_block_hash_entry_t_empty( rbh->hashes ) ) {
     176           0 :     fd_block_block_hash_entry_t const * last = deq_fd_block_block_hash_entry_t_peek_head_const( rbh->hashes );
     177           0 :     if( last && last->fee_calculator.lamports_per_signature!=0UL ) {
     178           0 :       fd_bank_rbh_lamports_per_sig_set( runner->bank, last->fee_calculator.lamports_per_signature );
     179           0 :     }
     180           0 :   }
     181             : 
     182             :   // Blockhash_queue[end] = last (latest) hash
     183             :   // Blockhash_queue[0] = genesis hash
     184           0 :   if( num_blockhashes > 0 ) {
     185           0 :     fd_hash_t * genesis_hash = fd_bank_genesis_hash_modify( runner->bank );
     186           0 :     memcpy( genesis_hash->hash, test_ctx->blockhash_queue[0]->bytes, sizeof(fd_hash_t) );
     187             : 
     188           0 :     for( ulong i = 0; i < num_blockhashes; ++i ) {
     189           0 :       fd_hash_t blockhash = FD_LOAD( fd_hash_t, test_ctx->blockhash_queue[i]->bytes );
     190             :       /* Drop duplicate blockhashes */
     191           0 :       if( FD_UNLIKELY( fd_blockhash_map_idx_remove( blockhashes->map, &blockhash, ULONG_MAX, blockhashes->d.deque )!=ULONG_MAX ) ) {
     192           0 :         FD_LOG_WARNING(( "Fuzz input has a duplicate blockhash %s at index %lu",
     193           0 :                          FD_BASE58_ENC_32_ALLOCA( blockhash.hash ), i ));
     194           0 :       }
     195             :       // Recent block hashes cap is 150 (actually 151), while blockhash queue capacity is 300 (actually 301)
     196           0 :       fd_bank_poh_set( runner->bank, blockhash );
     197           0 :       fd_sysvar_recent_hashes_update( runner->bank, runner->accdb, &xid, NULL );
     198           0 :     }
     199           0 :   } else {
     200             :     // Add a default empty blockhash and use it as genesis
     201           0 :     num_blockhashes = 1;
     202           0 :     *fd_bank_genesis_hash_modify( runner->bank ) = (fd_hash_t){0};
     203           0 :     fd_bank_poh_set( runner->bank, (fd_hash_t){0} );
     204           0 :     fd_sysvar_recent_hashes_update( runner->bank, runner->accdb, &xid, NULL );
     205           0 :   }
     206             : 
     207             :   /* Restore sysvars from account context */
     208           0 :   fd_sysvar_cache_restore_fuzz( runner->bank, funk, &xid );
     209             : 
     210             :   /* Create the raw txn (https://solana.com/docs/core/transactions#transaction-size) */
     211           0 :   fd_txn_p_t * txn    = fd_spad_alloc( runner->spad, alignof(fd_txn_p_t), sizeof(fd_txn_p_t) );
     212           0 :   ulong        msg_sz = fd_solfuzz_pb_txn_serialize( txn->payload, &test_ctx->tx );
     213           0 :   if( FD_UNLIKELY( msg_sz==ULONG_MAX ) ) {
     214           0 :     return NULL;
     215           0 :   }
     216             : 
     217             :   /* Set up txn descriptor from raw data */
     218           0 :   if( FD_UNLIKELY( !fd_txn_parse( txn->payload, msg_sz, TXN( txn ), NULL ) ) ) {
     219           0 :     return NULL;
     220           0 :   }
     221             : 
     222           0 :   txn->payload_sz = msg_sz;
     223             : 
     224           0 :   return txn;
     225           0 : }
     226             : 
     227             : ulong
     228             : fd_solfuzz_pb_txn_serialize( uchar *                                      txn_raw_begin,
     229           0 :                              fd_exec_test_sanitized_transaction_t const * tx ) {
     230           0 :   uchar * txn_raw_cur_ptr = txn_raw_begin;
     231             : 
     232             :   /* Compact array of signatures (https://solana.com/docs/core/transactions#transaction)
     233             :      Note that although documentation interchangably refers to the signature cnt as a compact-u16
     234             :      and a u8, the max signature cnt is capped at 48 (due to txn size limits), so u8 and compact-u16
     235             :      is represented the same way anyways and can be parsed identically. */
     236             :   // Note: always create a valid txn with 1+ signatures, add an empty signature if none is provided
     237           0 :   uchar signature_cnt = fd_uchar_max( 1, (uchar) tx->signatures_count );
     238           0 :   FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &signature_cnt, sizeof(uchar) );
     239           0 :   for( uchar i = 0; i < signature_cnt; ++i ) {
     240           0 :     fd_signature_t sig = {0};
     241           0 :     if( tx->signatures && tx->signatures[i] ) sig = FD_LOAD( fd_signature_t, tx->signatures[i]->bytes );
     242           0 :     FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &sig, FD_TXN_SIGNATURE_SZ );
     243           0 :   }
     244             : 
     245             :   /* Message */
     246             :   /* For v0 transactions, the highest bit of the num_required_signatures is set, and an extra byte is used for the version.
     247             :      https://solanacookbook.com/guides/versioned-transactions.html#versioned-transactions-transactionv0
     248             : 
     249             :      We will always create a transaction with at least 1 signature, and cap the signature count to 127 to avoid
     250             :      collisions with the header_b0 tag. */
     251           0 :   uchar num_required_signatures = fd_uchar_max( 1, fd_uchar_min( 127, (uchar) tx->message.header.num_required_signatures ) );
     252           0 :   if( !tx->message.is_legacy ) {
     253           0 :     uchar header_b0 = (uchar) 0x80UL;
     254           0 :     FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &header_b0, sizeof(uchar) );
     255           0 :   }
     256             : 
     257             :   /* Header (3 bytes) (https://solana.com/docs/core/transactions#message-header) */
     258           0 :   FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &num_required_signatures, sizeof(uchar) );
     259           0 :   FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &tx->message.header.num_readonly_signed_accounts, sizeof(uchar) );
     260           0 :   FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &tx->message.header.num_readonly_unsigned_accounts, sizeof(uchar) );
     261             : 
     262             :   /* Compact array of account addresses (https://solana.com/docs/core/transactions#compact-array-format) */
     263             :   // Array length is a compact u16
     264           0 :   ushort num_acct_keys = (ushort) tx->message.account_keys_count;
     265           0 :   FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, num_acct_keys );
     266           0 :   for( ushort i = 0; i < num_acct_keys; ++i ) {
     267           0 :     FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, tx->message.account_keys[i]->bytes, sizeof(fd_pubkey_t) );
     268           0 :   }
     269             : 
     270             :   /* Recent blockhash (32 bytes) (https://solana.com/docs/core/transactions#recent-blockhash) */
     271             :   // Note: add an empty blockhash if none is provided
     272           0 :   fd_hash_t msg_rbh = {0};
     273           0 :   if( tx->message.recent_blockhash ) msg_rbh = FD_LOAD( fd_hash_t, tx->message.recent_blockhash->bytes );
     274           0 :   FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &msg_rbh, sizeof(fd_hash_t) );
     275             : 
     276             :   /* Compact array of instructions (https://solana.com/docs/core/transactions#array-of-instructions) */
     277             :   // Instruction count is a compact u16
     278           0 :   ushort instr_count = (ushort) tx->message.instructions_count;
     279           0 :   FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, instr_count );
     280           0 :   for( ushort i = 0; i < instr_count; ++i ) {
     281             :     // Program ID index
     282           0 :     uchar program_id_index = (uchar) tx->message.instructions[i].program_id_index;
     283           0 :     FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &program_id_index, sizeof(uchar) );
     284             : 
     285             :     // Compact array of account addresses
     286           0 :     ushort acct_count = (ushort) tx->message.instructions[i].accounts_count;
     287           0 :     FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, acct_count );
     288           0 :     for( ushort j = 0; j < acct_count; ++j ) {
     289           0 :       uchar account_index = (uchar) tx->message.instructions[i].accounts[j];
     290           0 :       FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &account_index, sizeof(uchar) );
     291           0 :     }
     292             : 
     293             :     // Compact array of 8-bit data
     294           0 :     pb_bytes_array_t * data = tx->message.instructions[i].data;
     295           0 :     ushort data_len;
     296           0 :     if( data ) {
     297           0 :       data_len = (ushort) data->size;
     298           0 :       FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, data_len );
     299           0 :       FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, data->bytes, data_len );
     300           0 :     } else {
     301           0 :       data_len = 0;
     302           0 :       FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, data_len );
     303           0 :     }
     304           0 :   }
     305             : 
     306             :   /* Address table lookups (N/A for legacy transactions) */
     307           0 :   ushort addr_table_cnt = 0;
     308           0 :   if( !tx->message.is_legacy ) {
     309             :     /* Compact array of address table lookups (https://solanacookbook.com/guides/versioned-transactions.html#compact-array-of-address-table-lookups) */
     310             :     // NOTE: The diagram is slightly wrong - the account key is a 32 byte pubkey, not a u8
     311           0 :     addr_table_cnt = (ushort) tx->message.address_table_lookups_count;
     312           0 :     FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, addr_table_cnt );
     313           0 :     for( ushort i = 0; i < addr_table_cnt; ++i ) {
     314             :       // Account key
     315           0 :       FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, tx->message.address_table_lookups[i].account_key, sizeof(fd_pubkey_t) );
     316             : 
     317             :       // Compact array of writable indexes
     318           0 :       ushort writable_count = (ushort) tx->message.address_table_lookups[i].writable_indexes_count;
     319           0 :       FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, writable_count );
     320           0 :       for( ushort j = 0; j < writable_count; ++j ) {
     321           0 :         uchar writable_index = (uchar) tx->message.address_table_lookups[i].writable_indexes[j];
     322           0 :         FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &writable_index, sizeof(uchar) );
     323           0 :       }
     324             : 
     325             :       // Compact array of readonly indexes
     326           0 :       ushort readonly_count = (ushort) tx->message.address_table_lookups[i].readonly_indexes_count;
     327           0 :       FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, readonly_count );
     328           0 :       for( ushort j = 0; j < readonly_count; ++j ) {
     329           0 :         uchar readonly_index = (uchar) tx->message.address_table_lookups[i].readonly_indexes[j];
     330           0 :         FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &readonly_index, sizeof(uchar) );
     331           0 :       }
     332           0 :     }
     333           0 :   }
     334             : 
     335           0 :   return (ulong)(txn_raw_cur_ptr - txn_raw_begin);
     336           0 : }
     337             : 
     338             : void
     339             : fd_solfuzz_txn_ctx_exec( fd_solfuzz_runner_t * runner,
     340             :                          fd_runtime_t *        runtime,
     341             :                          fd_txn_in_t const *   txn_in,
     342             :                          int *                 exec_res,
     343           0 :                          fd_txn_out_t *        txn_out ) {
     344             : 
     345           0 :   txn_out->err.is_committable = 1;
     346             : 
     347           0 :   runtime->log.enable_vm_tracing = runner->enable_vm_tracing;
     348           0 :   uchar * tracing_mem = NULL;
     349           0 :   if( runner->enable_vm_tracing ) {
     350           0 :     tracing_mem = fd_spad_alloc_check( runner->spad, FD_RUNTIME_VM_TRACE_STATIC_ALIGN, FD_RUNTIME_VM_TRACE_STATIC_FOOTPRINT * FD_MAX_INSTRUCTION_STACK_DEPTH );
     351           0 :   }
     352             : 
     353           0 :   runtime->accdb           = runner->accdb;
     354           0 :   runtime->funk            = fd_accdb_user_v1_funk( runner->accdb );
     355           0 :   runtime->progcache       = runner->progcache;
     356           0 :   runtime->status_cache    = NULL;
     357           0 :   runtime->log.tracing_mem = tracing_mem;
     358           0 :   runtime->log.dumping_mem = NULL;
     359           0 :   runtime->log.capture_ctx = NULL;
     360             : 
     361           0 :   fd_runtime_prepare_and_execute_txn( runtime, runner->bank, txn_in, txn_out );
     362           0 :   *exec_res = txn_out->err.txn_err;
     363           0 : }
     364             : 
     365             : ulong
     366             : fd_solfuzz_pb_txn_run( fd_solfuzz_runner_t * runner,
     367             :                        void const *          input_,
     368             :                        void **               output_,
     369             :                        void *                output_buf,
     370           0 :                        ulong                 output_bufsz ) {
     371           0 :   fd_exec_test_txn_context_t const * input  = fd_type_pun_const( input_ );
     372           0 :   fd_exec_test_txn_result_t **       output = fd_type_pun( output_ );
     373             : 
     374           0 :   FD_SPAD_FRAME_BEGIN( runner->spad ) {
     375             : 
     376             :     /* Setup the transaction context */
     377           0 :     fd_txn_p_t * txn = fd_solfuzz_pb_txn_ctx_create( runner, input );
     378           0 :     if( FD_UNLIKELY( txn==NULL ) ) {
     379           0 :       fd_solfuzz_txn_ctx_destroy( runner );
     380           0 :       return 0UL;
     381           0 :     }
     382             : 
     383             :     /* Execute the transaction against the runtime */
     384           0 :     int exec_res = 0;
     385           0 :     fd_runtime_t *       runtime = runner->runtime;
     386           0 :     fd_txn_in_t *        txn_in  = fd_spad_alloc( runner->spad, alignof(fd_txn_in_t), sizeof(fd_txn_in_t) );
     387           0 :     fd_txn_out_t *       txn_out = fd_spad_alloc( runner->spad, alignof(fd_txn_out_t), sizeof(fd_txn_out_t) );
     388           0 :     fd_log_collector_t * log     = fd_spad_alloc( runner->spad, alignof(fd_log_collector_t), sizeof(fd_log_collector_t) );
     389           0 :     runtime->log.log_collector = log;
     390           0 :     txn_in->txn = txn;
     391           0 :     txn_in->exec_accounts = runner->exec_accounts;
     392           0 :     txn_in->bundle.is_bundle = 0;
     393           0 :     fd_solfuzz_txn_ctx_exec( runner, runtime, txn_in, &exec_res, txn_out );
     394             : 
     395             :     /* Start saving txn exec results */
     396           0 :     FD_SCRATCH_ALLOC_INIT( l, output_buf );
     397           0 :     ulong output_end = (ulong)output_buf + output_bufsz;
     398             : 
     399           0 :     fd_exec_test_txn_result_t * txn_result =
     400           0 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_txn_result_t),
     401           0 :                                   sizeof (fd_exec_test_txn_result_t) );
     402           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     403           0 :       abort();
     404           0 :     }
     405           0 :     fd_memset( txn_result, 0, sizeof(fd_exec_test_txn_result_t) );
     406             : 
     407             :     /* Map the nonce errors into the agave expected ones. */
     408           0 :     if( FD_UNLIKELY( exec_res==FD_RUNTIME_TXN_ERR_BLOCKHASH_NONCE_ALREADY_ADVANCED ||
     409           0 :                      exec_res==FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_ADVANCE_NONCE_INSTR ||
     410           0 :                      exec_res==FD_RUNTIME_TXN_ERR_BLOCKHASH_FAIL_WRONG_NONCE )) {
     411           0 :       exec_res = FD_RUNTIME_TXN_ERR_BLOCKHASH_NOT_FOUND;
     412           0 :     }
     413             : 
     414             :     /* Capture basic results fields */
     415           0 :     txn_result->executed                          = txn_out->err.is_committable;
     416           0 :     txn_result->sanitization_error                = !txn_out->err.is_committable;
     417           0 :     txn_result->has_resulting_state               = false;
     418           0 :     txn_result->resulting_state.acct_states_count = 0;
     419           0 :     txn_result->is_ok                             = !exec_res;
     420           0 :     txn_result->status                            = (uint32_t) -exec_res;
     421           0 :     txn_result->instruction_error                 = 0;
     422           0 :     txn_result->instruction_error_index           = 0;
     423           0 :     txn_result->custom_error                      = 0;
     424           0 :     txn_result->has_fee_details                   = false;
     425           0 :     txn_result->loaded_accounts_data_size         = txn_out->details.loaded_accounts_data_size;
     426             : 
     427           0 :     if( txn_result->sanitization_error ) {
     428             :       /* Collect fees for transactions that failed to load */
     429           0 :       if( txn_out->err.is_fees_only ) {
     430           0 :         txn_result->has_fee_details                = true;
     431           0 :         txn_result->fee_details.prioritization_fee = txn_out->details.priority_fee;
     432           0 :         txn_result->fee_details.transaction_fee    = txn_out->details.execution_fee;
     433           0 :       }
     434             : 
     435           0 :       if( exec_res==FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR ) {
     436           0 :         txn_result->instruction_error       = (uint32_t) -txn_out->err.exec_err;
     437           0 :         txn_result->instruction_error_index = (uint32_t) txn_out->err.exec_err_idx;
     438           0 :         if( txn_out->err.exec_err==FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR ) {
     439           0 :           txn_result->custom_error = txn_out->err.custom_err;
     440           0 :         }
     441           0 :       }
     442             : 
     443           0 :       ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     444           0 :       fd_solfuzz_txn_ctx_destroy( runner );
     445             : 
     446           0 :       *output = txn_result;
     447           0 :       return actual_end - (ulong)output_buf;
     448             : 
     449           0 :     } else {
     450             :       /* Capture the instruction error code */
     451           0 :       if( exec_res==FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR ) {
     452           0 :         fd_txn_t const * txn            = TXN( txn_in->txn );
     453           0 :         int              instr_err_idx  = txn_out->err.exec_err_idx;
     454           0 :         int              program_id_idx = txn->instr[instr_err_idx].program_id;
     455             : 
     456           0 :         txn_result->instruction_error       = (uint32_t) -txn_out->err.exec_err;
     457           0 :         txn_result->instruction_error_index = (uint32_t) instr_err_idx;
     458             : 
     459             :         /* If the exec err was a custom instr error and came from a precompile instruction, don't capture the custom error code. */
     460           0 :         if( txn_out->err.exec_err==FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR &&
     461           0 :             fd_executor_lookup_native_precompile_program( &txn_out->accounts.keys[ program_id_idx ] )==NULL ) {
     462           0 :           txn_result->custom_error = txn_out->err.custom_err;
     463           0 :         }
     464           0 :       }
     465           0 :     }
     466             : 
     467           0 :     txn_result->has_fee_details                = true;
     468           0 :     txn_result->fee_details.transaction_fee    = txn_out->details.execution_fee;
     469           0 :     txn_result->fee_details.prioritization_fee = txn_out->details.priority_fee;
     470           0 :     txn_result->executed_units                 = txn_out->details.compute_budget.compute_unit_limit - txn_out->details.compute_budget.compute_meter;
     471             : 
     472             : 
     473             :     /* Rent is no longer collected */
     474           0 :     txn_result->rent                           = 0UL;
     475             : 
     476           0 :     if( txn_out->details.return_data.len > 0 ) {
     477           0 :       txn_result->return_data = FD_SCRATCH_ALLOC_APPEND( l, alignof(pb_bytes_array_t),
     478           0 :                                       PB_BYTES_ARRAY_T_ALLOCSIZE( txn_out->details.return_data.len ) );
     479           0 :       if( FD_UNLIKELY( _l > output_end ) ) {
     480           0 :         abort();
     481           0 :       }
     482             : 
     483           0 :       txn_result->return_data->size = (pb_size_t)txn_out->details.return_data.len;
     484           0 :       fd_memcpy( txn_result->return_data->bytes, txn_out->details.return_data.data, txn_out->details.return_data.len );
     485           0 :     }
     486             : 
     487             :     /* Allocate space for captured accounts */
     488           0 :     ulong modified_acct_cnt = txn_out->accounts.cnt;
     489             : 
     490           0 :     txn_result->has_resulting_state         = true;
     491           0 :     txn_result->resulting_state.acct_states =
     492           0 :       FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_acct_state_t),
     493           0 :                                   sizeof (fd_exec_test_acct_state_t) * modified_acct_cnt );
     494           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     495           0 :       abort();
     496           0 :     }
     497             : 
     498             :     /* If the transaction is a fees-only transaction, we have to create rollback accounts to iterate over and save. */
     499           0 :     fd_account_meta_t * * accounts_to_save = txn_out->accounts.metas;
     500           0 :     ulong                 accounts_cnt     = txn_out->accounts.cnt;
     501           0 :     if( txn_out->err.is_fees_only ) {
     502           0 :       accounts_to_save = fd_spad_alloc( runner->spad, alignof(fd_txn_account_t), sizeof(fd_txn_account_t) * 2 );
     503           0 :       accounts_cnt     = 0UL;
     504             : 
     505           0 :       if( FD_LIKELY( txn_out->accounts.nonce_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) ) {
     506           0 :         accounts_to_save[accounts_cnt++] = txn_out->accounts.rollback_fee_payer;
     507           0 :       }
     508             : 
     509           0 :       if( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) {
     510           0 :         accounts_to_save[accounts_cnt++] = txn_out->accounts.rollback_nonce;
     511           0 :       }
     512           0 :     }
     513             : 
     514             :     /* Capture borrowed accounts */
     515           0 :     for( ulong j=0UL; j<accounts_cnt; j++ ) {
     516           0 :       fd_account_meta_t * meta = accounts_to_save[j];
     517           0 :       fd_pubkey_t * pubkey     = &txn_out->accounts.keys[j];
     518             : 
     519           0 :       if( !( fd_runtime_account_is_writable_idx( txn_in, txn_out, runner->bank, (ushort)j ) || j==FD_FEE_PAYER_TXN_IDX ) ) continue;
     520             : 
     521           0 :       ulong modified_idx = txn_result->resulting_state.acct_states_count;
     522           0 :       assert( modified_idx < modified_acct_cnt );
     523             : 
     524           0 :       fd_exec_test_acct_state_t * out_acct = &txn_result->resulting_state.acct_states[ modified_idx ];
     525           0 :       memset( out_acct, 0, sizeof(fd_exec_test_acct_state_t) );
     526             :       /* Copy over account content */
     527             : 
     528           0 :       memcpy( out_acct->address, pubkey, sizeof(fd_pubkey_t) );
     529             : 
     530           0 :       out_acct->lamports = meta->lamports;
     531             : 
     532           0 :       if( meta->dlen>0UL ) {
     533           0 :         out_acct->data =
     534           0 :           FD_SCRATCH_ALLOC_APPEND( l, alignof(pb_bytes_array_t),
     535           0 :                                       PB_BYTES_ARRAY_T_ALLOCSIZE( meta->dlen ) );
     536           0 :         if( FD_UNLIKELY( _l > output_end ) ) {
     537           0 :           abort();
     538           0 :         }
     539           0 :         out_acct->data->size = (pb_size_t)meta->dlen;
     540           0 :         fd_memcpy( out_acct->data->bytes, fd_account_data( meta ), meta->dlen );
     541           0 :       }
     542             : 
     543           0 :       out_acct->executable = meta->executable;
     544           0 :       memcpy( out_acct->owner, meta->owner, sizeof(fd_pubkey_t) );
     545             : 
     546           0 :       txn_result->resulting_state.acct_states_count++;
     547           0 :     }
     548             : 
     549           0 :     ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     550           0 :     fd_solfuzz_txn_ctx_destroy( runner );
     551             : 
     552           0 :     *output = txn_result;
     553           0 :     return actual_end - (ulong)output_buf;
     554           0 :   } FD_SPAD_FRAME_END;
     555           0 : }

Generated by: LCOV version 1.14