LCOV - code coverage report
Current view: top level - flamenco/runtime/tests/harness - fd_txn_harness.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 368 0.0 %
Date: 2025-08-05 05:04:49 Functions: 0 5 0.0 %

          Line data    Source code
       1             : #include "fd_txn_harness.h"
       2             : #include "fd_harness_common.h"
       3             : 
       4             : static void
       5             : fd_runtime_fuzz_txn_ctx_destroy( fd_runtime_fuzz_runner_t * runner,
       6           0 :                                  fd_exec_slot_ctx_t *       slot_ctx ) {
       7           0 :   if( !slot_ctx ) return; // This shouldn't be false either
       8           0 :   fd_funk_txn_t *       funk_txn  = slot_ctx->funk_txn;
       9             : 
      10           0 :   fd_funk_txn_cancel( runner->funk, funk_txn, 1 );
      11           0 : }
      12             : 
      13             : /* Creates transaction execution context for a single test case. Returns a
      14             :    a parsed txn descriptor on success and NULL on failure. */
      15             : static fd_txn_p_t *
      16             : fd_runtime_fuzz_txn_ctx_create( fd_runtime_fuzz_runner_t *         runner,
      17             :                                 fd_exec_slot_ctx_t *               slot_ctx,
      18           0 :                                 fd_exec_test_txn_context_t const * test_ctx ) {
      19           0 :   fd_funk_t * funk = runner->funk;
      20             : 
      21             :   /* Generate unique ID for funk txn */
      22             : 
      23           0 :   fd_funk_txn_xid_t xid[1] = {0};
      24           0 :   xid[0] = fd_funk_generate_xid();
      25             : 
      26             :   /* Create temporary funk transaction and spad contexts */
      27             : 
      28           0 :   fd_funk_txn_start_write( funk );
      29           0 :   fd_funk_txn_t * funk_txn = fd_funk_txn_prepare( funk, NULL, xid, 1 );
      30           0 :   fd_funk_txn_end_write( funk );
      31             : 
      32             :   /* Allocate contexts */
      33           0 :   assert( slot_ctx  );
      34             : 
      35             :   /* Set up slot context */
      36             : 
      37           0 :   slot_ctx->funk_txn = funk_txn;
      38           0 :   slot_ctx->funk     = funk;
      39             : 
      40           0 :   slot_ctx->banks = runner->banks;
      41           0 :   slot_ctx->bank  = runner->bank;
      42           0 :   fd_banks_clear_bank( slot_ctx->banks, slot_ctx->bank );
      43             : 
      44             :   /* Restore feature flags */
      45             : 
      46           0 :   fd_exec_test_feature_set_t const * feature_set = &test_ctx->epoch_ctx.features;
      47           0 :   fd_features_t * features_bm = fd_bank_features_modify( slot_ctx->bank );
      48           0 :   if( !fd_runtime_fuzz_restore_features( features_bm, feature_set ) ) {
      49           0 :     return NULL;
      50           0 :   }
      51             : 
      52             :   /* Default slot */
      53           0 :   ulong slot = test_ctx->slot_ctx.slot ? test_ctx->slot_ctx.slot : 10; // Arbitrary default > 0
      54             : 
      55             :   /* Set slot bank variables (defaults obtained from GenesisConfig::default() in Agave) */
      56           0 :   slot_ctx->bank->slot_ = slot;
      57             : 
      58             :   /* Initialize builtin accounts */
      59           0 :   fd_builtin_programs_init( slot_ctx );
      60             : 
      61             :   /* Load account states into funk (note this is different from the account keys):
      62             :     Account state = accounts to populate Funk
      63             :     Account keys = account keys that the transaction needs */
      64           0 :   for( ulong i = 0; i < test_ctx->account_shared_data_count; i++ ) {
      65             :     /* Load the accounts into the account manager
      66             :        Borrowed accounts get reset anyways - we just need to load the account somewhere */
      67           0 :     FD_TXN_ACCOUNT_DECL( acc );
      68           0 :     fd_runtime_fuzz_load_account( acc, funk, funk_txn, &test_ctx->account_shared_data[i], 1 );
      69           0 :   }
      70             : 
      71             :   /* Setup Bank manager */
      72             : 
      73           0 :   fd_bank_parent_slot_set( slot_ctx->bank, fd_bank_slot_get( slot_ctx->bank ) - 1UL );
      74             : 
      75           0 :   fd_bank_lamports_per_signature_set( slot_ctx->bank, 5000UL );
      76             : 
      77           0 :   fd_bank_prev_lamports_per_signature_set( slot_ctx->bank, 5000UL );
      78             : 
      79           0 :   fd_fee_rate_governor_t * fee_rate_governor = fd_bank_fee_rate_governor_modify( slot_ctx->bank );
      80           0 :   fee_rate_governor->burn_percent                  = 50;
      81           0 :   fee_rate_governor->min_lamports_per_signature    = 0;
      82           0 :   fee_rate_governor->max_lamports_per_signature    = 0;
      83           0 :   fee_rate_governor->target_lamports_per_signature = 10000;
      84           0 :   fee_rate_governor->target_signatures_per_slot    = 20000;
      85             : 
      86           0 :   fd_bank_ticks_per_slot_set( slot_ctx->bank, 64 );
      87             : 
      88             :   /* Set epoch bank variables if not present (defaults obtained from GenesisConfig::default() in Agave) */
      89           0 :   fd_epoch_schedule_t default_epoch_schedule = {
      90           0 :     .slots_per_epoch             = 432000,
      91           0 :     .leader_schedule_slot_offset = 432000,
      92           0 :     .warmup                      = 1,
      93           0 :     .first_normal_epoch          = 14,
      94           0 :     .first_normal_slot           = 524256
      95           0 :   };
      96           0 :   fd_rent_t default_rent = {
      97           0 :     .lamports_per_uint8_year = 3480,
      98           0 :     .exemption_threshold     = 2.0,
      99           0 :     .burn_percent            = 50
     100           0 :   };
     101           0 :   fd_bank_epoch_schedule_set( slot_ctx->bank, default_epoch_schedule );
     102             : 
     103           0 :   fd_bank_rent_set( slot_ctx->bank, default_rent );
     104             : 
     105           0 :   fd_bank_slots_per_year_set( slot_ctx->bank, SECONDS_PER_YEAR * (1000000000.0 / (double)6250000) / (double)(fd_bank_ticks_per_slot_get( slot_ctx->bank )) );
     106             : 
     107             :   // Override default values if provided
     108           0 :   fd_epoch_schedule_t epoch_schedule[1];
     109           0 :   if( fd_sysvar_epoch_schedule_read( funk, funk_txn, epoch_schedule ) ) {
     110           0 :     fd_bank_epoch_schedule_set( slot_ctx->bank, *epoch_schedule );
     111           0 :   }
     112             : 
     113           0 :   fd_rent_t const * rent = fd_sysvar_rent_read( funk, funk_txn, runner->spad );
     114           0 :   if( rent ) {
     115           0 :     fd_bank_rent_set( slot_ctx->bank, *rent );
     116           0 :   }
     117             : 
     118             :   /* Provide default slot hashes of size 1 if not provided */
     119           0 :   fd_slot_hashes_global_t * slot_hashes = fd_sysvar_slot_hashes_read( funk, funk_txn, runner->spad );
     120           0 :   if( !slot_hashes ) {
     121           0 :     FD_SPAD_FRAME_BEGIN( runner->spad ) {
     122             :       /* The offseted gaddr aware types need the memory for the entire
     123             :          struct to be allocated out of a contiguous memory region. */
     124           0 :       fd_slot_hash_t * slot_hashes                          = NULL;
     125           0 :       void * mem                                            = fd_spad_alloc( runner->spad, FD_SYSVAR_SLOT_HASHES_ALIGN, fd_sysvar_slot_hashes_footprint( 1UL ) );
     126           0 :       fd_slot_hashes_global_t * default_slot_hashes_global  = fd_sysvar_slot_hashes_join( fd_sysvar_slot_hashes_new( mem, 1UL ), &slot_hashes );
     127             : 
     128           0 :       fd_slot_hash_t * dummy_elem = deq_fd_slot_hash_t_push_tail_nocopy( slot_hashes );
     129           0 :       memset( dummy_elem, 0, sizeof(fd_slot_hash_t) );
     130             : 
     131           0 :       fd_sysvar_slot_hashes_write( slot_ctx, default_slot_hashes_global );
     132             : 
     133           0 :       fd_sysvar_slot_hashes_delete( fd_sysvar_slot_hashes_leave( default_slot_hashes_global, slot_hashes ) );
     134           0 :     } FD_SPAD_FRAME_END;
     135           0 :   }
     136             : 
     137             :   /* Provide default stake history if not provided */
     138           0 :   fd_stake_history_t * stake_history = fd_sysvar_stake_history_read( funk, funk_txn, runner->spad );
     139           0 :   if( !stake_history ) {
     140             :     // Provide a 0-set default entry
     141           0 :     fd_epoch_stake_history_entry_pair_t entry = {0};
     142           0 :     fd_sysvar_stake_history_init( slot_ctx );
     143           0 :     fd_sysvar_stake_history_update( slot_ctx, &entry, runner->spad );
     144           0 :   }
     145             : 
     146             :   /* Provide default last restart slot sysvar if not provided */
     147           0 :   FD_TXN_ACCOUNT_DECL( acc );
     148           0 :   int err = fd_txn_account_init_from_funk_readonly( acc, &fd_sysvar_last_restart_slot_id, funk, funk_txn );
     149           0 :   if( err==FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT ) {
     150           0 :     fd_sysvar_last_restart_slot_init( slot_ctx );
     151           0 :   }
     152             : 
     153             :   /* Provide a default clock if not present */
     154           0 :   fd_sol_sysvar_clock_t clock_[1];
     155           0 :   fd_sol_sysvar_clock_t const * clock = fd_sysvar_clock_read( funk, funk_txn, clock_ );
     156           0 :   if( !clock ) {
     157           0 :     fd_sysvar_clock_init( slot_ctx );
     158           0 :     fd_sysvar_clock_update( slot_ctx, runner->spad );
     159           0 :   }
     160             : 
     161             :   /* Epoch schedule and rent get set from the epoch bank */
     162           0 :   fd_sysvar_epoch_schedule_init( slot_ctx );
     163           0 :   fd_sysvar_rent_init( slot_ctx );
     164             : 
     165             :   /* Set the epoch rewards sysvar if partition epoch rewards feature is enabled
     166             : 
     167             :      TODO: The init parameters are not exactly conformant with Agave's epoch rewards sysvar. We should
     168             :      be calling `fd_begin_partitioned_rewards` with the same parameters as Agave. However,
     169             :      we just need the `active` field to be conformant due to a single Stake program check.
     170             :      THIS MAY CHANGE IN THE FUTURE. If there are other parts of transaction execution that use
     171             :      the epoch rewards sysvar, we may need to update this.
     172             :   */
     173           0 :   fd_sysvar_epoch_rewards_t epoch_rewards[1];
     174           0 :   if( !fd_sysvar_epoch_rewards_read( funk, funk_txn, epoch_rewards ) ) {
     175           0 :     fd_hash_t last_hash = {0};
     176           0 :     if( test_ctx->blockhash_queue_count > 0 ) last_hash = FD_LOAD( fd_hash_t, test_ctx->blockhash_queue[0]->bytes );
     177           0 :     fd_sysvar_epoch_rewards_init( slot_ctx, 0UL, 2UL, 1UL, 0UL, 0UL, &last_hash );
     178           0 :   }
     179             : 
     180             :   /* Blockhash queue is given in txn message. We need to populate the following two fields:
     181             :      - block_hash_queue
     182             :      - recent_block_hashes */
     183           0 :   ulong num_blockhashes = test_ctx->blockhash_queue_count;
     184             : 
     185             :   /* Blockhash queue init */
     186           0 :   ulong blockhash_seed; FD_TEST( fd_rng_secure( &blockhash_seed, sizeof(ulong) ) );
     187           0 :   fd_blockhashes_t * blockhashes = fd_blockhashes_init( fd_bank_block_hash_queue_modify( slot_ctx->bank ), blockhash_seed );
     188             : 
     189             :   // Save lamports per signature for most recent blockhash, if sysvar cache contains recent block hashes
     190           0 :   fd_recent_block_hashes_t const * rbh_sysvar = fd_sysvar_recent_hashes_read( funk, funk_txn, runner->spad );
     191           0 :   fd_recent_block_hashes_t rbh[1];
     192           0 :   if( rbh_sysvar ) {
     193           0 :     rbh->hashes = rbh_sysvar->hashes;
     194           0 :   }
     195             : 
     196           0 :   if( rbh_sysvar && !deq_fd_block_block_hash_entry_t_empty( rbh->hashes ) ) {
     197           0 :     fd_block_block_hash_entry_t const * last = deq_fd_block_block_hash_entry_t_peek_head_const( rbh->hashes );
     198           0 :     if( last && last->fee_calculator.lamports_per_signature!=0UL ) {
     199           0 :       fd_bank_lamports_per_signature_set( slot_ctx->bank, last->fee_calculator.lamports_per_signature );
     200           0 :       fd_bank_prev_lamports_per_signature_set( slot_ctx->bank, last->fee_calculator.lamports_per_signature );
     201           0 :     }
     202           0 :   }
     203             : 
     204             :   // Blockhash_queue[end] = last (latest) hash
     205             :   // Blockhash_queue[0] = genesis hash
     206           0 :   if( num_blockhashes > 0 ) {
     207           0 :     fd_hash_t * genesis_hash = fd_bank_genesis_hash_modify( slot_ctx->bank );
     208           0 :     memcpy( genesis_hash->hash, test_ctx->blockhash_queue[0]->bytes, sizeof(fd_hash_t) );
     209             : 
     210           0 :     for( ulong i = 0; i < num_blockhashes; ++i ) {
     211           0 :       fd_hash_t blockhash = FD_LOAD( fd_hash_t, test_ctx->blockhash_queue[i]->bytes );
     212             :       /* Drop duplicate blockhashes */
     213           0 :       if( FD_UNLIKELY( fd_blockhash_map_idx_remove( blockhashes->map, &blockhash, ULONG_MAX, blockhashes->d.deque )!=ULONG_MAX ) ) {
     214           0 :         FD_LOG_WARNING(( "Fuzz input has a duplicate blockhash %s at index %lu",
     215           0 :                          FD_BASE58_ENC_32_ALLOCA( blockhash.hash ), i ));
     216           0 :       }
     217             :       // Recent block hashes cap is 150 (actually 151), while blockhash queue capacity is 300 (actually 301)
     218           0 :       fd_bank_poh_set( slot_ctx->bank, blockhash );
     219           0 :       fd_sysvar_recent_hashes_update( slot_ctx );
     220           0 :     }
     221           0 :   } else {
     222             :     // Add a default empty blockhash and use it as genesis
     223           0 :     num_blockhashes = 1;
     224           0 :     *fd_bank_genesis_hash_modify( slot_ctx->bank ) = (fd_hash_t){0};
     225           0 :     fd_bank_poh_set( slot_ctx->bank, (fd_hash_t){0} );
     226           0 :     fd_sysvar_recent_hashes_update( slot_ctx );
     227           0 :   }
     228             : 
     229             :   /* Restore sysvars from account context */
     230           0 :   fd_sysvar_cache_restore_fuzz( slot_ctx );
     231             : 
     232             :   /* Refresh the program cache */
     233           0 :   fd_runtime_fuzz_refresh_program_cache( slot_ctx, test_ctx->account_shared_data, test_ctx->account_shared_data_count, runner->spad );
     234             : 
     235             :   /* Create the raw txn (https://solana.com/docs/core/transactions#transaction-size) */
     236           0 :   uchar * txn_raw_begin = fd_spad_alloc( runner->spad, alignof(uchar), 1232 );
     237           0 :   ushort instr_count, addr_table_cnt;
     238           0 :   ulong msg_sz = fd_runtime_fuzz_serialize_txn( txn_raw_begin, &test_ctx->tx, &instr_count, &addr_table_cnt );
     239           0 :   if( FD_UNLIKELY( msg_sz==ULONG_MAX ) ) {
     240           0 :     return NULL;
     241           0 :   }
     242             : 
     243             :   /* Set up txn descriptor from raw data */
     244           0 :   fd_txn_t * txn_descriptor = (fd_txn_t *) fd_spad_alloc( runner->spad, fd_txn_align(), fd_txn_footprint( instr_count, addr_table_cnt ) );
     245           0 :   if( FD_UNLIKELY( !fd_txn_parse( txn_raw_begin, msg_sz, txn_descriptor, NULL ) ) ) {
     246           0 :     return NULL;
     247           0 :   }
     248             : 
     249             :   /* Run txn preparation phases and execution
     250             :      NOTE: This should be modified accordingly if transaction setup logic changes */
     251           0 :   fd_txn_p_t * txn = fd_spad_alloc( runner->spad, alignof(fd_txn_p_t), sizeof(fd_txn_p_t) );
     252           0 :   memcpy( txn->payload, txn_raw_begin, msg_sz );
     253           0 :   txn->payload_sz = msg_sz;
     254           0 :   txn->flags = FD_TXN_P_FLAGS_SANITIZE_SUCCESS;
     255           0 :   memcpy( txn->_, txn_descriptor, fd_txn_footprint( instr_count, addr_table_cnt ) );
     256             : 
     257           0 :   return txn;
     258           0 : }
     259             : 
     260             : /* Takes in a parsed txn descriptor to be executed against the runtime.
     261             :    Returns the task info. */
     262             : static fd_execute_txn_task_info_t *
     263             : fd_runtime_fuzz_txn_ctx_exec( fd_runtime_fuzz_runner_t * runner,
     264             :                               fd_exec_slot_ctx_t *       slot_ctx,
     265           0 :                               fd_txn_p_t *               txn ) {
     266           0 :   fd_execute_txn_task_info_t * task_info = fd_spad_alloc( runner->spad, alignof(fd_execute_txn_task_info_t), sizeof(fd_execute_txn_task_info_t) );
     267           0 :   memset( task_info, 0, sizeof(fd_execute_txn_task_info_t) );
     268           0 :   task_info->txn     = txn;
     269           0 :   task_info->txn_ctx = fd_spad_alloc( runner->spad, FD_EXEC_TXN_CTX_ALIGN, FD_EXEC_TXN_CTX_FOOTPRINT );
     270             : 
     271           0 :   fd_runtime_prepare_txns_start( slot_ctx, task_info, txn, 1UL, runner->spad );
     272             : 
     273             :   /* Setup the spad for account allocation */
     274           0 :   task_info->txn_ctx->spad      = runner->spad;
     275           0 :   task_info->txn_ctx->spad_wksp = fd_wksp_containing( runner->spad );
     276             : 
     277           0 :   fd_runtime_pre_execute_check( task_info );
     278             : 
     279           0 :   if( task_info->txn->flags & FD_TXN_P_FLAGS_SANITIZE_SUCCESS ) {
     280           0 :       task_info->txn->flags |= FD_TXN_P_FLAGS_EXECUTE_SUCCESS;
     281           0 :       task_info->exec_res    = fd_execute_txn( task_info );
     282           0 :   }
     283             : 
     284           0 :   fd_bank_execution_fees_set( slot_ctx->bank, fd_bank_execution_fees_get( slot_ctx->bank ) + task_info->txn_ctx->execution_fee );
     285             : 
     286           0 :   fd_bank_priority_fees_set( slot_ctx->bank, fd_bank_priority_fees_get( slot_ctx->bank ) + task_info->txn_ctx->priority_fee );
     287             : 
     288           0 :   return task_info;
     289           0 : }
     290             : 
     291             : ulong
     292             : fd_runtime_fuzz_serialize_txn( uchar *                                      txn_raw_begin,
     293             :                                fd_exec_test_sanitized_transaction_t const * tx,
     294             :                                ushort *                                     out_instr_cnt,
     295           0 :                                ushort *                                     out_addr_table_cnt ) {
     296           0 :   uchar * txn_raw_cur_ptr = txn_raw_begin;
     297             : 
     298             :   /* Compact array of signatures (https://solana.com/docs/core/transactions#transaction)
     299             :      Note that although documentation interchangably refers to the signature cnt as a compact-u16
     300             :      and a u8, the max signature cnt is capped at 48 (due to txn size limits), so u8 and compact-u16
     301             :      is represented the same way anyways and can be parsed identically. */
     302             :   // Note: always create a valid txn with 1+ signatures, add an empty signature if none is provided
     303           0 :   uchar signature_cnt = fd_uchar_max( 1, (uchar) tx->signatures_count );
     304           0 :   FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &signature_cnt, sizeof(uchar) );
     305           0 :   for( uchar i = 0; i < signature_cnt; ++i ) {
     306           0 :     fd_signature_t sig = {0};
     307           0 :     if( tx->signatures && tx->signatures[i] ) sig = FD_LOAD( fd_signature_t, tx->signatures[i]->bytes );
     308           0 :     FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &sig, FD_TXN_SIGNATURE_SZ );
     309           0 :   }
     310             : 
     311             :   /* Message */
     312             :   /* For v0 transactions, the highest bit of the num_required_signatures is set, and an extra byte is used for the version.
     313             :      https://solanacookbook.com/guides/versioned-transactions.html#versioned-transactions-transactionv0
     314             : 
     315             :      We will always create a transaction with at least 1 signature, and cap the signature count to 127 to avoid
     316             :      collisions with the header_b0 tag. */
     317           0 :   uchar num_required_signatures = fd_uchar_max( 1, fd_uchar_min( 127, (uchar) tx->message.header.num_required_signatures ) );
     318           0 :   if( !tx->message.is_legacy ) {
     319           0 :     uchar header_b0 = (uchar) 0x80UL;
     320           0 :     FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &header_b0, sizeof(uchar) );
     321           0 :   }
     322             : 
     323             :   /* Header (3 bytes) (https://solana.com/docs/core/transactions#message-header) */
     324           0 :   FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &num_required_signatures, sizeof(uchar) );
     325           0 :   FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &tx->message.header.num_readonly_signed_accounts, sizeof(uchar) );
     326           0 :   FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &tx->message.header.num_readonly_unsigned_accounts, sizeof(uchar) );
     327             : 
     328             :   /* Compact array of account addresses (https://solana.com/docs/core/transactions#compact-array-format) */
     329             :   // Array length is a compact u16
     330           0 :   ushort num_acct_keys = (ushort) tx->message.account_keys_count;
     331           0 :   FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, num_acct_keys );
     332           0 :   for( ushort i = 0; i < num_acct_keys; ++i ) {
     333           0 :     FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, tx->message.account_keys[i]->bytes, sizeof(fd_pubkey_t) );
     334           0 :   }
     335             : 
     336             :   /* Recent blockhash (32 bytes) (https://solana.com/docs/core/transactions#recent-blockhash) */
     337             :   // Note: add an empty blockhash if none is provided
     338           0 :   fd_hash_t msg_rbh = {0};
     339           0 :   if( tx->message.recent_blockhash ) msg_rbh = FD_LOAD( fd_hash_t, tx->message.recent_blockhash->bytes );
     340           0 :   FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &msg_rbh, sizeof(fd_hash_t) );
     341             : 
     342             :   /* Compact array of instructions (https://solana.com/docs/core/transactions#array-of-instructions) */
     343             :   // Instruction count is a compact u16
     344           0 :   ushort instr_count = (ushort) tx->message.instructions_count;
     345           0 :   FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, instr_count );
     346           0 :   for( ushort i = 0; i < instr_count; ++i ) {
     347             :     // Program ID index
     348           0 :     uchar program_id_index = (uchar) tx->message.instructions[i].program_id_index;
     349           0 :     FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &program_id_index, sizeof(uchar) );
     350             : 
     351             :     // Compact array of account addresses
     352           0 :     ushort acct_count = (ushort) tx->message.instructions[i].accounts_count;
     353           0 :     FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, acct_count );
     354           0 :     for( ushort j = 0; j < acct_count; ++j ) {
     355           0 :       uchar account_index = (uchar) tx->message.instructions[i].accounts[j];
     356           0 :       FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &account_index, sizeof(uchar) );
     357           0 :     }
     358             : 
     359             :     // Compact array of 8-bit data
     360           0 :     pb_bytes_array_t * data = tx->message.instructions[i].data;
     361           0 :     ushort data_len;
     362           0 :     if( data ) {
     363           0 :       data_len = (ushort) data->size;
     364           0 :       FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, data_len );
     365           0 :       FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, data->bytes, data_len );
     366           0 :     } else {
     367           0 :       data_len = 0;
     368           0 :       FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, data_len );
     369           0 :     }
     370           0 :   }
     371             : 
     372             :   /* Address table lookups (N/A for legacy transactions) */
     373           0 :   ushort addr_table_cnt = 0;
     374           0 :   if( !tx->message.is_legacy ) {
     375             :     /* Compact array of address table lookups (https://solanacookbook.com/guides/versioned-transactions.html#compact-array-of-address-table-lookups) */
     376             :     // NOTE: The diagram is slightly wrong - the account key is a 32 byte pubkey, not a u8
     377           0 :     addr_table_cnt = (ushort) tx->message.address_table_lookups_count;
     378           0 :     FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, addr_table_cnt );
     379           0 :     for( ushort i = 0; i < addr_table_cnt; ++i ) {
     380             :       // Account key
     381           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) );
     382             : 
     383             :       // Compact array of writable indexes
     384           0 :       ushort writable_count = (ushort) tx->message.address_table_lookups[i].writable_indexes_count;
     385           0 :       FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, writable_count );
     386           0 :       for( ushort j = 0; j < writable_count; ++j ) {
     387           0 :         uchar writable_index = (uchar) tx->message.address_table_lookups[i].writable_indexes[j];
     388           0 :         FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &writable_index, sizeof(uchar) );
     389           0 :       }
     390             : 
     391             :       // Compact array of readonly indexes
     392           0 :       ushort readonly_count = (ushort) tx->message.address_table_lookups[i].readonly_indexes_count;
     393           0 :       FD_CHECKED_ADD_CU16_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, readonly_count );
     394           0 :       for( ushort j = 0; j < readonly_count; ++j ) {
     395           0 :         uchar readonly_index = (uchar) tx->message.address_table_lookups[i].readonly_indexes[j];
     396           0 :         FD_CHECKED_ADD_TO_TXN_DATA( txn_raw_begin, &txn_raw_cur_ptr, &readonly_index, sizeof(uchar) );
     397           0 :       }
     398           0 :     }
     399           0 :   }
     400             : 
     401           0 :   *out_instr_cnt = instr_count;
     402           0 :   *out_addr_table_cnt = addr_table_cnt;
     403           0 :   return (ulong)(txn_raw_cur_ptr - txn_raw_begin);
     404           0 : }
     405             : 
     406             : ulong
     407             : fd_runtime_fuzz_txn_run( fd_runtime_fuzz_runner_t * runner,
     408             :                          void const *               input_,
     409             :                          void **                    output_,
     410             :                          void *                     output_buf,
     411           0 :                          ulong                      output_bufsz ) {
     412           0 :   fd_exec_test_txn_context_t const * input  = fd_type_pun_const( input_ );
     413           0 :   fd_exec_test_txn_result_t **       output = fd_type_pun( output_ );
     414             : 
     415           0 :   FD_SPAD_FRAME_BEGIN( runner->spad ) {
     416             : 
     417             :     /* Initialize memory */
     418           0 :     uchar *               slot_ctx_mem = fd_spad_alloc( runner->spad, FD_EXEC_SLOT_CTX_ALIGN,  FD_EXEC_SLOT_CTX_FOOTPRINT  );
     419           0 :     fd_exec_slot_ctx_t *  slot_ctx     = fd_exec_slot_ctx_join( fd_exec_slot_ctx_new( slot_ctx_mem ) );
     420             : 
     421             :     /* Setup the transaction context */
     422           0 :     fd_txn_p_t * txn = fd_runtime_fuzz_txn_ctx_create( runner, slot_ctx, input );
     423           0 :     if( txn==NULL ) {
     424           0 :       fd_runtime_fuzz_txn_ctx_destroy( runner, slot_ctx );
     425           0 :       return 0;
     426           0 :     }
     427             : 
     428             :     /* Execute the transaction against the runtime */
     429           0 :     fd_execute_txn_task_info_t * task_info = fd_runtime_fuzz_txn_ctx_exec( runner, slot_ctx, txn );
     430           0 :     fd_exec_txn_ctx_t *          txn_ctx   = task_info->txn_ctx;
     431             : 
     432           0 :     int exec_res = task_info->exec_res;
     433             : 
     434             :     /* Start saving txn exec results */
     435           0 :     FD_SCRATCH_ALLOC_INIT( l, output_buf );
     436           0 :     ulong output_end = (ulong)output_buf + output_bufsz;
     437             : 
     438           0 :     fd_exec_test_txn_result_t * txn_result =
     439           0 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_txn_result_t),
     440           0 :                                   sizeof (fd_exec_test_txn_result_t) );
     441           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     442           0 :       abort();
     443           0 :     }
     444           0 :     fd_memset( txn_result, 0, sizeof(fd_exec_test_txn_result_t) );
     445             : 
     446             :     /* Capture basic results fields */
     447           0 :     txn_result->executed                          = task_info->txn->flags & FD_TXN_P_FLAGS_EXECUTE_SUCCESS;
     448           0 :     txn_result->sanitization_error                = !( task_info->txn->flags & FD_TXN_P_FLAGS_SANITIZE_SUCCESS );
     449           0 :     txn_result->has_resulting_state               = false;
     450           0 :     txn_result->resulting_state.acct_states_count = 0;
     451           0 :     txn_result->is_ok                             = !exec_res;
     452           0 :     txn_result->status                            = (uint32_t) -exec_res;
     453           0 :     txn_result->instruction_error                 = 0;
     454           0 :     txn_result->instruction_error_index           = 0;
     455           0 :     txn_result->custom_error                      = 0;
     456           0 :     txn_result->has_fee_details                   = false;
     457           0 :     txn_result->loaded_accounts_data_size         = txn_ctx->loaded_accounts_data_size;
     458             : 
     459           0 :     if( txn_result->sanitization_error ) {
     460             :       /* Collect fees for transactions that failed to load */
     461           0 :       if( task_info->txn->flags & FD_TXN_P_FLAGS_FEES_ONLY ) {
     462           0 :         txn_result->has_fee_details                = true;
     463           0 :         txn_result->fee_details.prioritization_fee = txn_ctx->priority_fee;
     464           0 :         txn_result->fee_details.transaction_fee    = txn_ctx->execution_fee;
     465           0 :       }
     466             : 
     467           0 :       if( exec_res==FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR ) {
     468           0 :         txn_result->instruction_error       = (uint32_t) -txn_ctx->exec_err;
     469           0 :         txn_result->instruction_error_index = (uint32_t) txn_ctx->instr_err_idx;
     470           0 :         if( txn_ctx->exec_err==FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR ) {
     471           0 :           txn_result->custom_error = txn_ctx->custom_err;
     472           0 :         }
     473           0 :       }
     474             : 
     475           0 :       ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     476           0 :       fd_runtime_fuzz_txn_ctx_destroy( runner, slot_ctx );
     477             : 
     478           0 :       *output = txn_result;
     479           0 :       return actual_end - (ulong)output_buf;
     480             : 
     481           0 :     } else {
     482             :       /* Capture the instruction error code */
     483           0 :       if( exec_res==FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR ) {
     484           0 :         int instr_err_idx                   = txn_ctx->instr_err_idx;
     485           0 :         int program_id_idx                  = txn_ctx->instr_infos[instr_err_idx].program_id;
     486             : 
     487           0 :         txn_result->instruction_error       = (uint32_t) -txn_ctx->exec_err;
     488           0 :         txn_result->instruction_error_index = (uint32_t) instr_err_idx;
     489             : 
     490             :         /* If the exec err was a custom instr error and came from a precompile instruction, don't capture the custom error code. */
     491           0 :         if( txn_ctx->exec_err==FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR &&
     492           0 :             fd_executor_lookup_native_precompile_program( &txn_ctx->accounts[ program_id_idx ] )==NULL ) {
     493           0 :           txn_result->custom_error = txn_ctx->custom_err;
     494           0 :         }
     495           0 :       }
     496           0 :     }
     497             : 
     498           0 :     txn_result->has_fee_details                = true;
     499           0 :     txn_result->fee_details.transaction_fee    = txn_ctx->execution_fee;
     500           0 :     txn_result->fee_details.prioritization_fee = txn_ctx->priority_fee;
     501           0 :     txn_result->executed_units                 = txn_ctx->compute_budget_details.compute_unit_limit - txn_ctx->compute_budget_details.compute_meter;
     502             : 
     503             : 
     504             :     /* Rent is only collected on successfully loaded transactions */
     505           0 :     txn_result->rent                           = txn_ctx->collected_rent;
     506             : 
     507           0 :     if( txn_ctx->return_data.len > 0 ) {
     508           0 :       txn_result->return_data = FD_SCRATCH_ALLOC_APPEND( l, alignof(pb_bytes_array_t),
     509           0 :                                       PB_BYTES_ARRAY_T_ALLOCSIZE( txn_ctx->return_data.len ) );
     510           0 :       if( FD_UNLIKELY( _l > output_end ) ) {
     511           0 :         abort();
     512           0 :       }
     513             : 
     514           0 :       txn_result->return_data->size = (pb_size_t)txn_ctx->return_data.len;
     515           0 :       fd_memcpy( txn_result->return_data->bytes, txn_ctx->return_data.data, txn_ctx->return_data.len );
     516           0 :     }
     517             : 
     518             :     /* Allocate space for captured accounts */
     519           0 :     ulong modified_acct_cnt = txn_ctx->accounts_cnt;
     520             : 
     521           0 :     txn_result->has_resulting_state         = true;
     522           0 :     txn_result->resulting_state.acct_states =
     523           0 :       FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_acct_state_t),
     524           0 :                                   sizeof (fd_exec_test_acct_state_t) * modified_acct_cnt );
     525           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     526           0 :       abort();
     527           0 :     }
     528             : 
     529             :     /* If the transaction is a fees-only transaction, we have to create rollback accounts to iterate over and save. */
     530           0 :     fd_txn_account_t * accounts_to_save = txn_ctx->accounts;
     531           0 :     ulong              accounts_cnt     = txn_ctx->accounts_cnt;
     532           0 :     if( task_info->txn->flags & FD_TXN_P_FLAGS_FEES_ONLY ) {
     533           0 :       accounts_to_save = fd_spad_alloc( runner->spad, alignof(fd_txn_account_t), sizeof(fd_txn_account_t) * 2 );
     534           0 :       accounts_cnt     = 0UL;
     535             : 
     536           0 :       if( FD_LIKELY( txn_ctx->nonce_account_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) ) {
     537           0 :         accounts_to_save[accounts_cnt++] = *txn_ctx->rollback_fee_payer_account;
     538           0 :       }
     539             : 
     540           0 :       if( txn_ctx->nonce_account_idx_in_txn!=ULONG_MAX ) {
     541           0 :         accounts_to_save[accounts_cnt++] = *txn_ctx->rollback_nonce_account;
     542           0 :       }
     543           0 :     }
     544             : 
     545             :     /* Capture borrowed accounts */
     546           0 :     for( ulong j=0UL; j<accounts_cnt; j++ ) {
     547           0 :       fd_txn_account_t * acc = &accounts_to_save[j];
     548             : 
     549           0 :       if( !( fd_exec_txn_ctx_account_is_writable_idx( txn_ctx, (ushort)j ) || j==FD_FEE_PAYER_TXN_IDX ) ) continue;
     550           0 :       assert( acc->vt->is_mutable( acc ) );
     551             : 
     552           0 :       ulong modified_idx = txn_result->resulting_state.acct_states_count;
     553           0 :       assert( modified_idx < modified_acct_cnt );
     554             : 
     555           0 :       fd_exec_test_acct_state_t * out_acct = &txn_result->resulting_state.acct_states[ modified_idx ];
     556           0 :       memset( out_acct, 0, sizeof(fd_exec_test_acct_state_t) );
     557             :       /* Copy over account content */
     558             : 
     559           0 :       memcpy( out_acct->address, acc->pubkey, sizeof(fd_pubkey_t) );
     560             : 
     561           0 :       out_acct->lamports = acc->vt->get_lamports( acc );
     562             : 
     563           0 :       if( acc->vt->get_data_len( acc ) > 0 ) {
     564           0 :         out_acct->data =
     565           0 :           FD_SCRATCH_ALLOC_APPEND( l, alignof(pb_bytes_array_t),
     566           0 :                                       PB_BYTES_ARRAY_T_ALLOCSIZE( acc->vt->get_data_len( acc ) ) );
     567           0 :         if( FD_UNLIKELY( _l > output_end ) ) {
     568           0 :           abort();
     569           0 :         }
     570           0 :         out_acct->data->size = (pb_size_t)acc->vt->get_data_len( acc );
     571           0 :         fd_memcpy( out_acct->data->bytes, acc->vt->get_data( acc ), acc->vt->get_data_len( acc ) );
     572           0 :       }
     573             : 
     574           0 :       out_acct->executable = acc->vt->is_executable( acc );
     575           0 :       out_acct->rent_epoch = acc->vt->get_rent_epoch( acc );
     576           0 :       memcpy( out_acct->owner, acc->vt->get_owner( acc ), sizeof(fd_pubkey_t) );
     577             : 
     578           0 :       txn_result->resulting_state.acct_states_count++;
     579           0 :     }
     580             : 
     581           0 :     ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     582           0 :     fd_runtime_fuzz_txn_ctx_destroy( runner, slot_ctx );
     583             : 
     584           0 :     *output = txn_result;
     585           0 :     return actual_end - (ulong)output_buf;
     586           0 :   } FD_SPAD_FRAME_END;
     587           0 : }

Generated by: LCOV version 1.14