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