Line data Source code
1 : #include "fd_executor.h"
2 : #include "fd_bank.h"
3 : #include "fd_runtime.h"
4 : #include "fd_runtime_err.h"
5 : #include "fd_acc_pool.h"
6 :
7 : #include "fd_system_ids.h"
8 : #include "program/fd_bpf_loader_program.h"
9 : #include "program/fd_loader_v4_program.h"
10 : #include "program/fd_compute_budget_program.h"
11 : #include "program/fd_precompiles.h"
12 : #include "program/fd_system_program.h"
13 : #include "program/fd_builtin_programs.h"
14 : #include "program/fd_vote_program.h"
15 : #include "program/fd_zk_elgamal_proof_program.h"
16 : #include "sysvar/fd_sysvar_cache.h"
17 : #include "sysvar/fd_sysvar_epoch_schedule.h"
18 : #include "sysvar/fd_sysvar_instructions.h"
19 : #include "sysvar/fd_sysvar_rent.h"
20 : #include "sysvar/fd_sysvar_slot_history.h"
21 : #include "tests/fd_dump_pb.h"
22 :
23 : #include "../accdb/fd_accdb_sync.h"
24 : #include "../log_collector/fd_log_collector.h"
25 :
26 : #include "../../ballet/base58/fd_base58.h"
27 :
28 : #include "../../util/bits/fd_uwide.h"
29 :
30 : #include <assert.h>
31 : #include <math.h>
32 : #include <stdio.h> /* snprintf(3) */
33 : #include <fcntl.h> /* openat(2) */
34 : #include <unistd.h> /* write(3) */
35 : #include <time.h>
36 :
37 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/rent_state.rs#L5-L15 */
38 : struct fd_rent_state {
39 : uint discriminant;
40 : ulong lamports;
41 : ulong data_size;
42 : };
43 : typedef struct fd_rent_state fd_rent_state_t;
44 :
45 0 : #define FD_RENT_STATE_UNINITIALIZED (0U)
46 0 : #define FD_RENT_STATE_RENT_PAYING (1U)
47 387 : #define FD_RENT_STATE_RENT_EXEMPT (2U)
48 :
49 : #define MAP_PERFECT_NAME fd_native_program_fn_lookup_tbl
50 : #define MAP_PERFECT_LG_TBL_SZ 3
51 : #define MAP_PERFECT_T fd_native_prog_info_t
52 258 : #define MAP_PERFECT_HASH_C 1069U
53 : #define MAP_PERFECT_KEY key.uc
54 : #define MAP_PERFECT_KEY_T fd_pubkey_t const *
55 : #define MAP_PERFECT_ZERO_KEY (0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0)
56 : #define MAP_PERFECT_COMPLEX_KEY 1
57 258 : #define MAP_PERFECT_KEYS_EQUAL(k1,k2) (!memcmp( (k1), (k2), 32UL ))
58 :
59 258 : #define PERFECT_HASH( u ) (((MAP_PERFECT_HASH_C*(u))>>29)&0x7U)
60 :
61 : #define MAP_PERFECT_HASH_PP( a00,a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,a12,a13,a14,a15, \
62 : a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31) \
63 : PERFECT_HASH( (a08 | (a09<<8) | (a10<<16) | (a11<<24)) )
64 258 : #define MAP_PERFECT_HASH_R( ptr ) PERFECT_HASH( fd_uint_load_4( (uchar const *)ptr + 8UL ) )
65 :
66 : #define MAP_PERFECT_0 ( VOTE_PROG_ID ), .fn = fd_vote_program_execute, .is_bpf_loader = 0, .feature_enable_offset = ULONG_MAX
67 : #define MAP_PERFECT_1 ( SYS_PROG_ID ), .fn = fd_system_program_execute, .is_bpf_loader = 0, .feature_enable_offset = ULONG_MAX
68 : #define MAP_PERFECT_2 ( COMPUTE_BUDGET_PROG_ID ), .fn = fd_compute_budget_program_execute, .is_bpf_loader = 0, .feature_enable_offset = ULONG_MAX
69 : #define MAP_PERFECT_3 ( ZK_EL_GAMAL_PROG_ID ), .fn = fd_executor_zk_elgamal_proof_program_execute, .is_bpf_loader = 0, .feature_enable_offset = ULONG_MAX
70 : #define MAP_PERFECT_4 ( BPF_LOADER_1_PROG_ID ), .fn = fd_bpf_loader_program_execute, .is_bpf_loader = 1, .feature_enable_offset = ULONG_MAX
71 : #define MAP_PERFECT_5 ( BPF_LOADER_2_PROG_ID ), .fn = fd_bpf_loader_program_execute, .is_bpf_loader = 1, .feature_enable_offset = ULONG_MAX
72 : #define MAP_PERFECT_6 ( BPF_UPGRADEABLE_PROG_ID ), .fn = fd_bpf_loader_program_execute, .is_bpf_loader = 1, .feature_enable_offset = ULONG_MAX
73 : #define MAP_PERFECT_7 ( LOADER_V4_PROG_ID ), .fn = fd_loader_v4_program_execute, .is_bpf_loader = 1, .feature_enable_offset = offsetof( fd_features_t, enable_loader_v4 )
74 :
75 : #include "../../util/tmpl/fd_map_perfect.c"
76 : #undef PERFECT_HASH
77 :
78 : uchar
79 6 : fd_executor_pubkey_is_bpf_loader( fd_pubkey_t const * pubkey ) {
80 6 : fd_native_prog_info_t const null_function = {0};
81 6 : return fd_native_program_fn_lookup_tbl_query( pubkey, &null_function )->is_bpf_loader;
82 6 : }
83 :
84 : uchar
85 : fd_executor_program_is_active( fd_bank_t * bank,
86 126 : fd_pubkey_t const * pubkey ) {
87 126 : fd_native_prog_info_t const null_function = {0};
88 126 : ulong feature_offset = fd_native_program_fn_lookup_tbl_query( pubkey, &null_function )->feature_enable_offset;
89 :
90 126 : return feature_offset==ULONG_MAX ||
91 126 : FD_FEATURE_ACTIVE_BANK_OFFSET( bank, feature_offset );
92 126 : }
93 :
94 : /* fd_executor_lookup_native_program returns the appropriate instruction processor for the given
95 : native program ID. Returns NULL if given ID is not a recognized native program.
96 : https://github.com/anza-xyz/agave/blob/v2.2.6/program-runtime/src/invoke_context.rs#L520-L544 */
97 : static int
98 : fd_executor_lookup_native_program( fd_pubkey_t const * pubkey,
99 : fd_account_meta_t const * meta,
100 : fd_bank_t * bank,
101 : fd_exec_instr_fn_t * native_prog_fn,
102 126 : uchar * is_precompile ) {
103 : /* First lookup to see if the program key is a precompile */
104 126 : *is_precompile = 0;
105 126 : *native_prog_fn = fd_executor_lookup_native_precompile_program( pubkey );
106 126 : if( FD_UNLIKELY( *native_prog_fn!=NULL ) ) {
107 0 : *is_precompile = 1;
108 0 : return 0;
109 0 : }
110 :
111 126 : fd_pubkey_t const * owner = (fd_pubkey_t const *)meta->owner;
112 :
113 : /* Native programs should be owned by the native loader...
114 : This will not be the case though once core programs are migrated to BPF. */
115 126 : int is_native_program = !memcmp( owner, fd_solana_native_loader_id.key, sizeof(fd_pubkey_t) );
116 :
117 126 : if( !is_native_program ) {
118 6 : if( FD_UNLIKELY( !fd_executor_pubkey_is_bpf_loader( owner ) ) ) {
119 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
120 0 : }
121 6 : }
122 :
123 126 : fd_pubkey_t const * lookup_pubkey = is_native_program ? pubkey : owner;
124 :
125 : /* Migrated programs must be executed via the corresponding BPF
126 : loader(s), not natively. This check is performed at the transaction
127 : level, but we re-check to please the instruction level (and below)
128 : fuzzers. */
129 126 : uchar has_migrated;
130 126 : if( FD_UNLIKELY( fd_is_migrating_builtin_program( bank, lookup_pubkey, &has_migrated ) && has_migrated ) ) {
131 0 : *native_prog_fn = NULL;
132 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
133 0 : }
134 :
135 : /* We perform feature gate checks here to emulate the absence of
136 : a native program in Agave's ProgramCache when the program's feature
137 : gate is not activated.
138 : https://github.com/anza-xyz/agave/blob/v3.0.3/program-runtime/src/invoke_context.rs#L546-L549 */
139 :
140 126 : if( FD_UNLIKELY( !fd_executor_program_is_active( bank, lookup_pubkey ) ) ) {
141 0 : *native_prog_fn = NULL;
142 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
143 0 : }
144 :
145 126 : fd_native_prog_info_t const null_function = {0};
146 126 : *native_prog_fn = fd_native_program_fn_lookup_tbl_query( lookup_pubkey, &null_function )->fn;
147 126 : return 0;
148 126 : }
149 :
150 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L117-L136 */
151 : static uchar
152 : fd_executor_rent_transition_allowed( fd_rent_state_t const * pre_rent_state,
153 129 : fd_rent_state_t const * post_rent_state ) {
154 129 : switch( post_rent_state->discriminant ) {
155 0 : case FD_RENT_STATE_UNINITIALIZED:
156 129 : case FD_RENT_STATE_RENT_EXEMPT: {
157 129 : return 1;
158 0 : }
159 0 : case FD_RENT_STATE_RENT_PAYING: {
160 0 : switch( pre_rent_state->discriminant ) {
161 0 : case FD_RENT_STATE_UNINITIALIZED:
162 0 : case FD_RENT_STATE_RENT_EXEMPT: {
163 0 : return 0;
164 0 : }
165 0 : case FD_RENT_STATE_RENT_PAYING: {
166 0 : return post_rent_state->data_size==pre_rent_state->data_size &&
167 0 : post_rent_state->lamports<=pre_rent_state->lamports;
168 0 : }
169 0 : default: {
170 0 : FD_LOG_CRIT(( "unexpected pre-rent state discriminant %u", pre_rent_state->discriminant ));
171 0 : }
172 0 : }
173 0 : }
174 0 : default: {
175 0 : FD_LOG_CRIT(( "unexpected post-rent state discriminant %u", post_rent_state->discriminant ));
176 0 : }
177 129 : }
178 129 : }
179 :
180 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L61-L77 */
181 : static int
182 : fd_executor_check_rent_state_with_account( fd_pubkey_t const * pubkey,
183 : fd_rent_state_t const * pre_rent_state,
184 129 : fd_rent_state_t const * post_rent_state ) {
185 129 : if( FD_UNLIKELY( memcmp( pubkey, fd_sysvar_incinerator_id.key, sizeof(fd_pubkey_t) ) &&
186 129 : !fd_executor_rent_transition_allowed( pre_rent_state, post_rent_state ) ) ) {
187 0 : return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_RENT;
188 0 : }
189 129 : return FD_RUNTIME_EXECUTE_SUCCESS;
190 129 : }
191 :
192 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L87-L101 */
193 : fd_rent_state_t
194 258 : fd_executor_get_account_rent_state( fd_account_meta_t const * meta, fd_rent_t const * rent ) {
195 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L88-L89 */
196 258 : if( meta->lamports==0UL ) {
197 0 : return (fd_rent_state_t){
198 0 : .discriminant = FD_RENT_STATE_UNINITIALIZED
199 0 : };
200 0 : }
201 :
202 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L90-L94 */
203 258 : if( meta->lamports>=fd_rent_exempt_minimum_balance( rent, meta->dlen ) ) {
204 258 : return (fd_rent_state_t){
205 258 : .discriminant = FD_RENT_STATE_RENT_EXEMPT
206 258 : };
207 258 : }
208 :
209 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L95-L99 */
210 0 : return (fd_rent_state_t){
211 0 : .discriminant = FD_RENT_STATE_RENT_PAYING,
212 0 : .lamports = meta->lamports,
213 0 : .data_size = meta->dlen
214 0 : };
215 258 : }
216 :
217 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L293-L342 */
218 : static int
219 : fd_validate_fee_payer( fd_pubkey_t const * pubkey,
220 : fd_account_meta_t * meta,
221 : fd_rent_t const * rent,
222 141 : ulong fee ) {
223 :
224 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L301-L304 */
225 141 : if( FD_UNLIKELY( meta->lamports==0UL ) ) {
226 0 : FD_BASE58_ENCODE_32_BYTES( pubkey->uc, pubkey_b58 );
227 0 : FD_LOG_DEBUG(( "Fee payer doesn't exist %s", pubkey_b58 ));
228 0 : return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
229 0 : }
230 :
231 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L305-L308 */
232 141 : int system_account_kind = fd_get_system_account_kind( meta );
233 141 : if( FD_UNLIKELY( system_account_kind==FD_SYSTEM_PROGRAM_NONCE_ACCOUNT_KIND_UNKNOWN ) ) {
234 12 : return FD_RUNTIME_TXN_ERR_INVALID_ACCOUNT_FOR_FEE;
235 12 : }
236 :
237 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L309-L318 */
238 129 : ulong min_balance = 0UL;
239 129 : if( system_account_kind==FD_SYSTEM_PROGRAM_NONCE_ACCOUNT_KIND_NONCE ) {
240 0 : min_balance = fd_rent_exempt_minimum_balance( rent, FD_SYSTEM_PROGRAM_NONCE_DLEN );
241 0 : }
242 :
243 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L320-L327 */
244 129 : if( FD_UNLIKELY( min_balance>meta->lamports || fee>meta->lamports-min_balance ) ) {
245 0 : return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_FEE;
246 0 : }
247 :
248 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L329 */
249 129 : fd_rent_state_t payer_pre_rent_state = fd_executor_get_account_rent_state( meta, rent );
250 :
251 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L330-L332 */
252 129 : int err = fd_account_meta_checked_sub_lamports( meta, fee );
253 129 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
254 0 : return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_FEE;
255 0 : }
256 :
257 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L334 */
258 129 : fd_rent_state_t payer_post_rent_state = fd_executor_get_account_rent_state( meta, rent );
259 :
260 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L335-L342 */
261 129 : return fd_executor_check_rent_state_with_account( pubkey, &payer_pre_rent_state, &payer_post_rent_state );
262 129 : }
263 :
264 : static int
265 : fd_executor_check_status_cache( fd_txncache_t * status_cache,
266 : fd_bank_t * bank,
267 : fd_txn_in_t const * txn_in,
268 141 : fd_txn_out_t * txn_out ) {
269 141 : if( FD_UNLIKELY( !status_cache ) ) {
270 141 : return FD_RUNTIME_EXECUTE_SUCCESS;
271 141 : }
272 :
273 0 : if( FD_UNLIKELY( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) ) {
274 : /* In Agave, durable nonce transactions are inserted to the status
275 : cache the same as any others, but this is only to serve RPC
276 : requests, they do not need to be in there for correctness as the
277 : nonce mechanism itself prevents double spend. We skip this logic
278 : entirely to simplify and improve performance of the txn cache. */
279 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
280 0 : }
281 :
282 : /* Compute the blake3 hash of the transaction message
283 : https://github.com/anza-xyz/agave/blob/v2.1.7/sdk/program/src/message/versions/mod.rs#L159-L167 */
284 0 : fd_blake3_t b3[1];
285 0 : fd_blake3_init( b3 );
286 0 : fd_blake3_append( b3, "solana-tx-message-v1", 20UL );
287 0 : fd_blake3_append( b3, ((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->message_off),(ulong)( txn_in->txn->payload_sz - TXN( txn_in->txn )->message_off ) );
288 0 : fd_blake3_fini( b3, &txn_out->details.blake_txn_msg_hash );
289 :
290 0 : fd_hash_t * blockhash = (fd_hash_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->recent_blockhash_off);
291 0 : int found = fd_txncache_query( status_cache, bank->data->txncache_fork_id, blockhash->uc, txn_out->details.blake_txn_msg_hash.uc );
292 0 : if( FD_UNLIKELY( found ) ) return FD_RUNTIME_TXN_ERR_ALREADY_PROCESSED;
293 :
294 0 : if( FD_UNLIKELY( txn_in->bundle.is_bundle ) ) {
295 : /* It is possible for users to send transactions in a bundle with
296 : identical transaction message hashes. The message hashes are
297 : only added to the txncache when transactions are committed. So
298 : message hashes must be checked against the txncache AND previous
299 : transactions in the bundle. */
300 0 : for( ulong i=0UL; i<txn_in->bundle.prev_txn_cnt; i++ ) {
301 0 : fd_txn_out_t const * prev_txn_out = txn_in->bundle.prev_txn_outs[i];
302 0 : if( FD_UNLIKELY( !memcmp( &prev_txn_out->details.blake_txn_msg_hash, &txn_out->details.blake_txn_msg_hash, sizeof(fd_hash_t) ) ) ) {
303 0 : return FD_RUNTIME_TXN_ERR_ALREADY_PROCESSED;
304 0 : }
305 0 : }
306 0 : }
307 :
308 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
309 0 : }
310 :
311 : /* https://github.com/anza-xyz/agave/blob/v3.1.8/runtime/src/bank/check_transactions.rs#L71-L136 */
312 : static int
313 : fd_executor_check_transaction_age_and_compute_budget_limits( fd_runtime_t * runtime,
314 : fd_bank_t * bank,
315 : fd_txn_in_t const * txn_in,
316 141 : fd_txn_out_t * txn_out ) {
317 :
318 : /* https://github.com/anza-xyz/agave/blob/v3.1.8/runtime/src/bank/check_transactions.rs#L94-L123 */
319 141 : int err = fd_sanitize_compute_unit_limits( txn_out );
320 141 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
321 0 : return err;
322 0 : }
323 :
324 : /* https://github.com/anza-xyz/agave/blob/v3.1.8/runtime/src/bank/check_transactions.rs#L124-L131 */
325 141 : err = fd_check_transaction_age( runtime, bank, txn_in, txn_out );
326 141 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
327 0 : return err;
328 0 : }
329 :
330 141 : return FD_RUNTIME_EXECUTE_SUCCESS;
331 141 : }
332 :
333 : /* https://github.com/anza-xyz/agave/blob/v2.0.9/runtime/src/bank.rs#L3239-L3251 */
334 : static inline ulong
335 141 : get_transaction_account_lock_limit( fd_bank_t * bank ) {
336 141 : return fd_ulong_if( FD_FEATURE_ACTIVE_BANK( bank, increase_tx_account_lock_limit ), MAX_TX_ACCOUNT_LOCKS, 64UL );
337 141 : }
338 :
339 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank/check_transactions.rs#L61-L75 */
340 : int
341 : fd_executor_check_transactions( fd_runtime_t * runtime,
342 : fd_bank_t * bank,
343 : fd_txn_in_t const * txn_in,
344 141 : fd_txn_out_t * txn_out ) {
345 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank/check_transactions.rs#L68-L73 */
346 141 : int err = fd_executor_check_transaction_age_and_compute_budget_limits( runtime, bank, txn_in, txn_out );
347 141 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
348 0 : return err;
349 0 : }
350 :
351 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank/check_transactions.rs#L74 */
352 141 : err = fd_executor_check_status_cache( runtime->status_cache, bank, txn_in, txn_out );
353 141 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
354 0 : return err;
355 0 : }
356 :
357 141 : return FD_RUNTIME_EXECUTE_SUCCESS;
358 141 : }
359 :
360 : /* `verify_transaction()` is the first function called in the
361 : transaction execution pipeline. It is responsible for deserializing
362 : the transaction, verifying the message hash (sigverify), verifying
363 : the precompiles, and processing compute budget instructions. We
364 : leave sigverify out for now to easily bypass this function's
365 : checks for fuzzing.
366 :
367 : TODO: Maybe support adding sigverify in here, and toggling it
368 : on/off with a flag.
369 :
370 : https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L5725-L5753 */
371 : int
372 : fd_executor_verify_transaction( fd_bank_t const * bank,
373 : fd_txn_in_t const * txn_in,
374 144 : fd_txn_out_t * txn_out ) {
375 144 : int err = FD_RUNTIME_EXECUTE_SUCCESS;
376 :
377 : /* SIMD-0406: enforce limit on number of instruction accounts.
378 :
379 : TODO: when limit_instruction_accounts is activated everywhere,
380 : remove this and make the transaction parser check stricter.
381 :
382 : https://github.com/anza-xyz/agave/blob/v4.0.0-alpha.0/runtime-transaction/src/runtime_transaction/sdk_transactions.rs#L93-L99 */
383 144 : if( FD_UNLIKELY( FD_FEATURE_ACTIVE_BANK( bank, limit_instruction_accounts ) ) ) {
384 18 : fd_txn_t const * txn = TXN( txn_in->txn );
385 39 : for( ushort i=0; i<txn->instr_cnt; i++ ) {
386 24 : if( FD_UNLIKELY( txn->instr[i].acct_cnt > FD_BPF_INSTR_ACCT_MAX ) ) {
387 3 : return FD_RUNTIME_TXN_ERR_SANITIZE_FAILURE;
388 3 : }
389 24 : }
390 18 : }
391 :
392 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L566-L569 */
393 141 : err = fd_executor_compute_budget_program_execute_instructions( bank, txn_in, txn_out );
394 141 : if( FD_UNLIKELY( err ) ) return err;
395 :
396 141 : return FD_RUNTIME_EXECUTE_SUCCESS;
397 141 : }
398 :
399 : /* https://github.com/anza-xyz/agave/blob/v2.0.9/svm/src/account_loader.rs#L410-427 */
400 : static int
401 : accumulate_and_check_loaded_account_data_size( ulong acc_size,
402 : ulong requested_loaded_accounts_data_size,
403 138 : ulong * accumulated_account_size ) {
404 138 : *accumulated_account_size = fd_ulong_sat_add( *accumulated_account_size, acc_size );
405 138 : if( FD_UNLIKELY( *accumulated_account_size>requested_loaded_accounts_data_size ) ) {
406 0 : return FD_RUNTIME_TXN_ERR_MAX_LOADED_ACCOUNTS_DATA_SIZE_EXCEEDED;
407 0 : }
408 138 : return FD_RUNTIME_EXECUTE_SUCCESS;
409 138 : }
410 :
411 : /* This function contains special casing for loading and collecting rent from
412 : each transaction account. The logic is as follows:
413 : 1. If the account is the instructions sysvar, then load in the compiled
414 : instructions from the transactions into the sysvar's data.
415 : 2. If the account is a fee payer, then it is already loaded.
416 : 3. Otherwise load in the account from the accounts DB. If the account is
417 : writable and exists, try to collect rent from it.
418 :
419 : Returns the loaded transaction account size, which is the value that
420 : must be used when accumulating and checking against the
421 : transactions's loaded account data size limit.
422 :
423 : Agave relies on this function to actually load accounts from their
424 : accounts db. However, since our accounts model is slightly different,
425 : our account loading logic is handled earlier in the transaction
426 : execution pipeline within `fd_executor_setup_accounts_for_txn()`.
427 : Therefore, the name of this function is slightly misleading - we
428 : don't actually load accounts here, but we still need to collect
429 : rent from writable accounts and accumulate the transaction's
430 : total loaded account size.
431 :
432 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L199-L228 */
433 : static ulong
434 : load_transaction_account( fd_runtime_t * runtime,
435 : fd_bank_t * bank,
436 : fd_txn_in_t const * txn_in,
437 : fd_txn_out_t * txn_out,
438 : fd_pubkey_t const * pubkey,
439 : fd_account_meta_t * meta,
440 : uchar unknown_acc,
441 258 : ulong txn_idx ) {
442 :
443 : /* Handling the sysvar instructions account explictly.
444 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L817-L824 */
445 258 : if( FD_UNLIKELY( !memcmp( pubkey, fd_sysvar_instructions_id.key, sizeof(fd_pubkey_t) ) ) ) {
446 : /* The sysvar instructions account cannot be "loaded" since it's
447 : constructed by the SVM and modified within each transaction's
448 : instruction execution only, so it incurs a loaded size cost
449 : of 0. */
450 0 : fd_sysvar_instructions_serialize_account( runtime, txn_in, txn_out, txn_idx );
451 0 : return 0UL;
452 0 : }
453 :
454 : /* This next block calls `account_loader::load_transaction_account()`
455 : which loads the account from the accounts db. If the account exists
456 : and is writable, collect rent from it.
457 :
458 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L828-L835 */
459 258 : if( FD_LIKELY( !unknown_acc ) ) {
460 : /* SIMD-0186 introduces a base account size of 64 bytes for all
461 : transaction counts that exist prior to the transaction's
462 : execution.
463 :
464 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L204-L208 */
465 240 : ulong base_account_size = FD_FEATURE_ACTIVE_BANK( bank, formalize_loaded_transaction_data_size ) ? FD_TRANSACTION_ACCOUNT_BASE_SIZE : 0UL;
466 :
467 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L828-L835 */
468 240 : return fd_ulong_sat_add( base_account_size, meta->dlen );
469 240 : }
470 :
471 : /* The rest of this function is a no-op for us since we already set up
472 : the transaction accounts for unknown accounts within
473 : `fd_executor_setup_accounts_for_txn()`. We also do not need to
474 : add a base cost to the loaded account size because the SIMD
475 : states that accounts that do not exist prior to the transaction's
476 : execution should not incur a loaded size cost.
477 : https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L566-L577 */
478 18 : return 0UL;
479 258 : }
480 :
481 : /* This big function contains a lot of logic and special casing for loading transaction accounts.
482 : Because of the `enable_transaction_loading_failure_fees` feature, it is imperative that we
483 : are conformant with Agave's logic here and reject / accept transactions here where they do.
484 :
485 : In the firedancer client only some of these steps are necessary because
486 : all of the accounts are loaded in from the accounts db into borrowed
487 : accounts already.
488 :
489 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L691-L807 */
490 : static int
491 : fd_executor_load_transaction_accounts_old( fd_runtime_t * runtime,
492 : fd_bank_t * bank,
493 : fd_txn_in_t const * txn_in,
494 57 : fd_txn_out_t * txn_out ) {
495 57 : ulong requested_loaded_accounts_data_size = txn_out->details.compute_budget.loaded_accounts_data_size_limit;
496 :
497 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L429-L443 */
498 195 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
499 138 : fd_account_meta_t * meta = txn_out->accounts.account[i].meta;
500 138 : uchar unknown_acc = !!(fd_runtime_get_account_at_index( txn_in, txn_out, i, fd_runtime_account_check_exists ) ||
501 138 : meta->lamports==0UL);
502 :
503 : /* Collect the fee payer account separately (since it was already)
504 : loaded during fee payer validation.
505 :
506 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L727-L729 */
507 138 : if( FD_UNLIKELY( i==FD_FEE_PAYER_TXN_IDX ) ) {
508 : /* Note that the dlen for most fee payers is 0, but we want to
509 : consider the case where the fee payer is a nonce account.
510 : We also don't need to add a base account size to this value
511 : because this branch would only be taken BEFORE SIMD-0186
512 : is enabled. */
513 57 : int err = accumulate_and_check_loaded_account_data_size( meta->dlen,
514 57 : requested_loaded_accounts_data_size,
515 57 : &txn_out->details.loaded_accounts_data_size );
516 57 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
517 0 : return err;
518 0 : }
519 57 : continue;
520 57 : }
521 :
522 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L733-L740 */
523 81 : ulong loaded_acc_size = load_transaction_account( runtime, bank, txn_in, txn_out, &txn_out->accounts.keys[i], meta, unknown_acc, i );
524 81 : int err = accumulate_and_check_loaded_account_data_size( loaded_acc_size,
525 81 : requested_loaded_accounts_data_size,
526 81 : &txn_out->details.loaded_accounts_data_size );
527 :
528 81 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
529 0 : return err;
530 0 : }
531 81 : }
532 :
533 : /* TODO: Consider using a hash set (if its more performant) */
534 57 : ushort instr_cnt = TXN( txn_in->txn )->instr_cnt;
535 57 : fd_pubkey_t validated_loaders[instr_cnt];
536 57 : ushort validated_loaders_cnt = 0;
537 57 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
538 :
539 : /* The logic below handles special casing with loading instruction accounts.
540 : https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L445-L525 */
541 72 : for( ushort i=0; i<instr_cnt; i++ ) {
542 15 : fd_txn_instr_t const * instr = &TXN( txn_in->txn )->instr[i];
543 :
544 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L449-L451 */
545 15 : if( FD_UNLIKELY( !memcmp( txn_out->accounts.keys[ instr->program_id ].key, fd_solana_native_loader_id.key, sizeof(fd_pubkey_t) ) ) ) {
546 0 : continue;
547 0 : }
548 :
549 : /* Mimicking `load_account()` here with 0-lamport check as well.
550 : https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L455-L462 */
551 15 : fd_account_meta_t * program_meta = txn_out->accounts.account[instr->program_id].meta;
552 15 : int err = fd_runtime_get_account_at_index( txn_in,
553 15 : txn_out,
554 15 : instr->program_id,
555 15 : fd_runtime_account_check_exists );
556 15 : if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS || program_meta->lamports==0UL ) ) {
557 0 : return FD_RUNTIME_TXN_ERR_PROGRAM_ACCOUNT_NOT_FOUND;
558 0 : }
559 :
560 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L474-L477 */
561 15 : if( !memcmp( program_meta->owner, fd_solana_native_loader_id.key, sizeof(fd_pubkey_t) ) ) {
562 15 : continue;
563 15 : }
564 :
565 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L479-L522 */
566 0 : uchar loader_seen = 0;
567 0 : for( ushort j=0; j<validated_loaders_cnt; j++ ) {
568 0 : if( !memcmp( validated_loaders[j].key, program_meta->owner, sizeof(fd_pubkey_t) ) ) {
569 : /* If the owner account has already been seen, skip the owner checks
570 : and do not acccumulate the account size. */
571 0 : loader_seen = 1;
572 0 : break;
573 0 : }
574 0 : }
575 0 : if( loader_seen ) continue;
576 :
577 : /* The agave client does checks on the program account's owners as well.
578 : However, it is important to not do these checks multiple times as the
579 : total size of accounts and their owners are accumulated: duplicate owners
580 : should be avoided.
581 : https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L496-L517 */
582 :
583 0 : fd_accdb_ro_t owner_ro[1];
584 0 : fd_pubkey_t const * owner_pubkey = (fd_pubkey_t const *)program_meta->owner;
585 0 : if( FD_UNLIKELY( !fd_accdb_open_ro( runtime->accdb, owner_ro, &xid, owner_pubkey ) ) ) {
586 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L520 */
587 0 : return FD_RUNTIME_TXN_ERR_PROGRAM_ACCOUNT_NOT_FOUND;
588 0 : }
589 0 : ulong const owner_sz = fd_accdb_ref_data_sz( owner_ro );
590 0 : fd_pubkey_t const owner_owner = FD_LOAD( fd_pubkey_t, fd_accdb_ref_owner( owner_ro ) );
591 0 : fd_accdb_close_ro( runtime->accdb, owner_ro );
592 :
593 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L502-L510 */
594 0 : if( FD_UNLIKELY( !fd_pubkey_eq( &owner_owner, &fd_solana_native_loader_id ) ) ) {
595 0 : return FD_RUNTIME_TXN_ERR_INVALID_PROGRAM_FOR_EXECUTION;
596 0 : }
597 :
598 : /* Count the owner's data in the loaded account size for program accounts.
599 : However, it is important to not double count repeated owners.
600 : https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L511-L517 */
601 0 : err = accumulate_and_check_loaded_account_data_size( owner_sz,
602 0 : requested_loaded_accounts_data_size,
603 0 : &txn_out->details.loaded_accounts_data_size );
604 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
605 0 : return err;
606 0 : }
607 :
608 0 : fd_memcpy( validated_loaders[ validated_loaders_cnt++ ].key, owner_pubkey, sizeof(fd_pubkey_t) );
609 0 : }
610 :
611 57 : return FD_RUNTIME_EXECUTE_SUCCESS;
612 57 : }
613 :
614 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L494-L515 */
615 : static int
616 : fd_increase_calculated_data_size( fd_txn_out_t * txn_out,
617 321 : ulong data_size_delta ) {
618 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L500-L503 */
619 321 : if( FD_UNLIKELY( data_size_delta>UINT_MAX ) ) {
620 0 : return FD_RUNTIME_TXN_ERR_MAX_LOADED_ACCOUNTS_DATA_SIZE_EXCEEDED;
621 0 : }
622 :
623 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L505-L507 */
624 321 : txn_out->details.loaded_accounts_data_size = fd_ulong_sat_add( txn_out->details.loaded_accounts_data_size, data_size_delta );
625 :
626 321 : if( FD_UNLIKELY( txn_out->details.loaded_accounts_data_size>txn_out->details.compute_budget.loaded_accounts_data_size_limit ) ) {
627 0 : return FD_RUNTIME_TXN_ERR_MAX_LOADED_ACCOUNTS_DATA_SIZE_EXCEEDED;
628 0 : }
629 :
630 321 : return FD_RUNTIME_EXECUTE_SUCCESS;
631 321 : }
632 :
633 : /* This function is represented as a closure in Agave.
634 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L578-L640 */
635 : static int
636 : fd_collect_loaded_account( fd_runtime_t * runtime,
637 : fd_txn_out_t * txn_out,
638 : fd_account_meta_t const * account_meta,
639 : ulong loaded_acc_size,
640 : fd_pubkey_t * additional_loaded_account_keys,
641 249 : ulong * additional_loaded_account_keys_cnt ) {
642 :
643 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L586-L590 */
644 249 : int err = fd_increase_calculated_data_size( txn_out, loaded_acc_size );
645 249 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
646 0 : return err;
647 0 : }
648 :
649 : /* The remainder of this function is a deep-nested set of if
650 : statements. I've inverted the logic to make it easier to read.
651 : The purpose of the following code is to ensure that loader v3
652 : programdata accounts are accounted for exactly once in the account
653 : loading logic.
654 :
655 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L611 */
656 249 : if( FD_LIKELY( memcmp( account_meta->owner, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
657 249 : return FD_RUNTIME_EXECUTE_SUCCESS;
658 249 : }
659 :
660 : /* Try to read the program state
661 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L612-L634 */
662 0 : fd_bpf_upgradeable_loader_state_t loader_state[1];
663 0 : err = fd_bpf_loader_program_get_state( account_meta, loader_state );
664 0 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
665 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
666 0 : }
667 :
668 : /* Make sure the account is a v3 program */
669 0 : if( !fd_bpf_upgradeable_loader_state_is_program( loader_state ) ) {
670 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
671 0 : }
672 :
673 : /* Iterate through the account keys and make sure the programdata
674 : account is not present so it doesn't get loaded twice.
675 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L617 */
676 0 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
677 0 : if( FD_UNLIKELY( !memcmp( &txn_out->accounts.keys[i], &loader_state->inner.program.programdata_address, sizeof(fd_pubkey_t) ) ) ) {
678 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
679 0 : }
680 0 : }
681 :
682 : /* Check that the programdata account has not been already counted
683 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L618 */
684 0 : for( ushort i=0; i<*additional_loaded_account_keys_cnt; i++ ) {
685 0 : if( FD_UNLIKELY( !memcmp( &additional_loaded_account_keys[i], &loader_state->inner.program.programdata_address, sizeof(fd_pubkey_t) ) ) ) {
686 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
687 0 : }
688 0 : }
689 :
690 : /* Programdata account size check */
691 0 : fd_accdb_ro_t const * programdata_ref = NULL;
692 0 : for( ushort i=0; i<runtime->accounts.executable_cnt; i++ ) {
693 0 : fd_accdb_ro_t const * ro = &runtime->accounts.executable[i];
694 0 : if( fd_pubkey_eq( fd_accdb_ref_address( ro ), &loader_state->inner.program.programdata_address ) ) {
695 0 : programdata_ref = ro;
696 0 : break;
697 0 : }
698 0 : }
699 0 : if( FD_UNLIKELY( !programdata_ref ) ) return FD_RUNTIME_EXECUTE_SUCCESS;
700 0 : ulong programdata_sz = fd_accdb_ref_data_sz( programdata_ref );
701 :
702 : /* Try to accumulate the programdata's data size
703 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L625-L630 */
704 0 : ulong programdata_size_delta = fd_ulong_sat_add( FD_TRANSACTION_ACCOUNT_BASE_SIZE, programdata_sz );
705 0 : err = fd_increase_calculated_data_size( txn_out, programdata_size_delta );
706 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
707 0 : return err;
708 0 : }
709 :
710 : /* Add the programdata account to the list of loaded programdata accounts
711 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L631 */
712 0 : fd_memcpy(
713 0 : &additional_loaded_account_keys[(*additional_loaded_account_keys_cnt)++],
714 0 : &loader_state->inner.program.programdata_address,
715 0 : sizeof(fd_pubkey_t) );
716 :
717 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
718 0 : }
719 :
720 : /* Simplified transaction loading logic for SIMD-0186 which does the
721 : following:
722 : - Calculates the loaded data size for each address lookup table
723 : - Calculates the loaded data size for each transaction account
724 : - Calculates the loaded data size for each v3 programdata account
725 : not directly referenced in the transaction accounts
726 : - Collects rent from all referenced transaction accounts (excluding
727 : the fee payer)
728 : - Validates that each program invoked in a top-level instruction
729 : exists, is executable, and is owned by either the native loader
730 : or a bpf loader
731 :
732 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L550-L689 */
733 : static int
734 : fd_executor_load_transaction_accounts_simd_186( fd_runtime_t * runtime,
735 : fd_bank_t * bank,
736 : fd_txn_in_t const * txn_in,
737 72 : fd_txn_out_t * txn_out ) {
738 : /* Programdata accounts that are loaded by this transaction.
739 : We keep track of these to ensure they are not counted twice.
740 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L559 */
741 72 : fd_pubkey_t additional_loaded_account_keys[ FD_TXN_ACCT_ADDR_MAX ] = { 0 };
742 72 : ulong additional_loaded_account_keys_cnt = 0UL;
743 :
744 : /* Charge a base fee for each address lookup table.
745 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L570-L576 */
746 72 : ulong aluts_size = fd_ulong_sat_mul( TXN( txn_in->txn )->addr_table_lookup_cnt,
747 72 : FD_ADDRESS_LOOKUP_TABLE_BASE_SIZE );
748 72 : int err = fd_increase_calculated_data_size( txn_out, aluts_size );
749 72 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
750 0 : return err;
751 0 : }
752 :
753 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L642-L660 */
754 321 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
755 249 : fd_account_meta_t * meta = txn_out->accounts.account[i].meta;
756 :
757 249 : uchar unknown_acc = !!(fd_runtime_get_account_at_index( txn_in, txn_out, i, fd_runtime_account_check_exists ) ||
758 249 : meta->lamports==0UL);
759 :
760 : /* Collect the fee payer account separately (since it was already)
761 : loaded during fee payer validation.
762 :
763 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L644-L648 */
764 249 : if( FD_UNLIKELY( i==FD_FEE_PAYER_TXN_IDX ) ) {
765 : /* Note that the dlen for most fee payers is 0, but we want to
766 : consider the case where the fee payer is a nonce account.
767 : We also must add a base account size to this value
768 : because this branch would only be taken AFTER SIMD-0186
769 : is enabled. */
770 72 : ulong loaded_acc_size = fd_ulong_sat_add( FD_TRANSACTION_ACCOUNT_BASE_SIZE,
771 72 : meta->dlen );
772 72 : int err = fd_collect_loaded_account(
773 72 : runtime,
774 72 : txn_out,
775 72 : meta,
776 72 : loaded_acc_size,
777 72 : additional_loaded_account_keys,
778 72 : &additional_loaded_account_keys_cnt );
779 72 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
780 0 : return err;
781 0 : }
782 72 : continue;
783 72 : }
784 :
785 : /* Load and collect any remaining accounts
786 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L652-L659 */
787 177 : ulong loaded_acc_size = load_transaction_account( runtime, bank, txn_in, txn_out, &txn_out->accounts.keys[i], meta, unknown_acc, i );
788 177 : int err = fd_collect_loaded_account(
789 177 : runtime,
790 177 : txn_out,
791 177 : meta,
792 177 : loaded_acc_size,
793 177 : additional_loaded_account_keys,
794 177 : &additional_loaded_account_keys_cnt );
795 177 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
796 0 : return err;
797 0 : }
798 177 : }
799 :
800 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L662-L686 */
801 72 : ushort instr_cnt = TXN( txn_in->txn )->instr_cnt;
802 156 : for( ushort i=0; i<instr_cnt; i++ ) {
803 84 : fd_txn_instr_t const * instr = &TXN( txn_in->txn )->instr[i];
804 :
805 : /* Mimicking `load_account()` here with 0-lamport check as well.
806 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L663-L666 */
807 84 : fd_account_meta_t * program_meta = txn_out->accounts.account[instr->program_id].meta;
808 84 : int err = fd_runtime_get_account_at_index( txn_in,
809 84 : txn_out,
810 84 : instr->program_id,
811 84 : fd_runtime_account_check_exists );
812 84 : if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS || program_meta->lamports==0UL ) ) {
813 0 : return FD_RUNTIME_TXN_ERR_PROGRAM_ACCOUNT_NOT_FOUND;
814 0 : }
815 :
816 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L677-L681 */
817 84 : fd_pubkey_t const * owner_id = (fd_pubkey_t const *)program_meta->owner;
818 84 : if( FD_UNLIKELY( memcmp( owner_id->key, fd_solana_native_loader_id.key, sizeof(fd_pubkey_t) ) &&
819 84 : !fd_executor_pubkey_is_bpf_loader( owner_id ) ) ) {
820 0 : return FD_RUNTIME_TXN_ERR_INVALID_PROGRAM_FOR_EXECUTION;
821 0 : }
822 84 : }
823 :
824 72 : return FD_RUNTIME_EXECUTE_SUCCESS;
825 72 : }
826 :
827 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L518-L548 */
828 : int
829 : fd_executor_load_transaction_accounts( fd_runtime_t * runtime,
830 : fd_bank_t * bank,
831 : fd_txn_in_t const * txn_in,
832 129 : fd_txn_out_t * txn_out ) {
833 129 : if( FD_FEATURE_ACTIVE_BANK( bank, formalize_loaded_transaction_data_size ) ) {
834 72 : return fd_executor_load_transaction_accounts_simd_186( runtime, bank, txn_in, txn_out );
835 72 : } else {
836 57 : return fd_executor_load_transaction_accounts_old( runtime, bank, txn_in, txn_out );
837 57 : }
838 129 : }
839 :
840 : /* https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118 */
841 : int
842 : fd_executor_validate_account_locks( fd_bank_t * bank,
843 141 : fd_txn_out_t const * txn_out ) {
844 : /* Ensure the number of account keys does not exceed the transaction lock limit
845 : https://github.com/anza-xyz/agave/blob/v2.2.17/accounts-db/src/account_locks.rs#L121 */
846 141 : ulong tx_account_lock_limit = get_transaction_account_lock_limit( bank );
847 141 : if( FD_UNLIKELY( txn_out->accounts.cnt>tx_account_lock_limit ) ) {
848 0 : return FD_RUNTIME_TXN_ERR_TOO_MANY_ACCOUNT_LOCKS;
849 0 : }
850 :
851 : /* Duplicate account check
852 : https://github.com/anza-xyz/agave/blob/v2.2.17/accounts-db/src/account_locks.rs#L123 */
853 564 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
854 1044 : for( ushort j=(ushort)(i+1U); j<txn_out->accounts.cnt; j++ ) {
855 621 : if( FD_UNLIKELY( !memcmp( &txn_out->accounts.keys[i], &txn_out->accounts.keys[j], sizeof(fd_pubkey_t) ) ) ) {
856 0 : return FD_RUNTIME_TXN_ERR_ACCOUNT_LOADED_TWICE;
857 0 : }
858 621 : }
859 423 : }
860 :
861 : /* https://github.com/anza-xyz/agave/blob/v2.2.17/accounts-db/src/account_locks.rs#L124-L126 */
862 141 : return FD_RUNTIME_EXECUTE_SUCCESS;
863 141 : }
864 :
865 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/compute-budget/src/compute_budget_limits.rs#L62-L70 */
866 : static ulong
867 141 : fd_get_prioritization_fee( fd_compute_budget_details_t const * compute_budget_details ) {
868 141 : uint128 micro_lamport_fee = fd_uint128_sat_mul( compute_budget_details->compute_unit_price, compute_budget_details->compute_unit_limit );
869 141 : uint128 fee = fd_uint128_sat_add( micro_lamport_fee, MICRO_LAMPORTS_PER_LAMPORT-1UL ) / MICRO_LAMPORTS_PER_LAMPORT;
870 141 : return fee>(uint128)ULONG_MAX ? ULONG_MAX : (ulong)fee;
871 141 : }
872 :
873 : static void
874 : fd_executor_calculate_fee( fd_txn_out_t * txn_out,
875 : fd_txn_t const * txn_descriptor,
876 : uchar const * payload,
877 : ulong * ret_execution_fee,
878 141 : ulong * ret_priority_fee ) {
879 : /* The execution fee is just the signature fee. The priority fee
880 : is calculated based on the compute budget details.
881 : https://github.com/anza-xyz/agave/blob/v3.0.3/fee/src/lib.rs#L65-L84 */
882 :
883 : // let signature_fee = Self::get_num_signatures_in_message(message) .saturating_mul(fee_structure.lamports_per_signature);
884 141 : ulong num_signatures = txn_descriptor->signature_cnt;
885 252 : for (ushort i=0; i<txn_descriptor->instr_cnt; ++i ) {
886 111 : fd_txn_instr_t const * txn_instr = &txn_descriptor->instr[i];
887 111 : fd_pubkey_t * program_id = &txn_out->accounts.keys[txn_instr->program_id];
888 111 : if( !memcmp(program_id->uc, fd_solana_keccak_secp_256k_program_id.key, sizeof(fd_pubkey_t)) ||
889 111 : !memcmp(program_id->uc, fd_solana_ed25519_sig_verify_program_id.key, sizeof(fd_pubkey_t)) ||
890 111 : !memcmp(program_id->uc, fd_solana_secp256r1_program_id.key, sizeof(fd_pubkey_t)) ) {
891 0 : if( !txn_instr->data_sz ) {
892 0 : continue;
893 0 : }
894 0 : uchar const * data = payload + txn_instr->data_off;
895 0 : num_signatures = fd_ulong_sat_add(num_signatures, (ulong)(data[0]));
896 0 : }
897 111 : }
898 141 : *ret_execution_fee = FD_RUNTIME_FEE_STRUCTURE_LAMPORTS_PER_SIGNATURE * num_signatures;
899 141 : *ret_priority_fee = fd_get_prioritization_fee( &txn_out->details.compute_budget );
900 141 : }
901 :
902 : /* This function creates a rollback account for just the fee payer. Although Agave
903 : also sets up rollback accounts for both the fee payer and nonce account here,
904 : we already set up the rollback nonce account in earlier sanitization checks. Here
905 : we have to capture the entire fee payer record so that if the transaction fails,
906 : the fee payer state can be rolled back to it's state pre-transaction, and then debited
907 : any transaction fees.
908 :
909 : Our implementation is slightly different than Agave's in several ways:
910 : 1. The rollback nonce account has already been set up when checking the transaction age
911 : 2. When the nonce and fee payer accounts are the same...
912 : - Agave copies the data from the rollback nonce account into the rollback fee payer account,
913 : and then uses that new fee payer account as the rollback account.
914 : - We simply set the rent epoch and lamports of the rollback nonce account (since the other fields
915 : of the account do not change)
916 :
917 : https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/rollback_accounts.rs#L34-L77 */
918 : static void
919 : fd_executor_create_rollback_fee_payer_account( fd_runtime_t * runtime,
920 : fd_bank_t * bank,
921 : fd_txn_in_t const * txn_in,
922 : fd_txn_out_t * txn_out,
923 129 : ulong total_fee ) {
924 129 : fd_pubkey_t * fee_payer_key = &txn_out->accounts.keys[FD_FEE_PAYER_TXN_IDX];
925 :
926 : /* When setting the data of the rollback fee payer, there is an edge
927 : case where the fee payer is the nonce account. In this case, we
928 : can just deduct fees from the nonce account and return, because
929 : we save the nonce account in the commit phase anyways. */
930 129 : if( FD_UNLIKELY( txn_out->accounts.nonce_idx_in_txn==FD_FEE_PAYER_TXN_IDX ) ) {
931 0 : txn_out->accounts.rollback_fee_payer = txn_out->accounts.rollback_nonce;
932 129 : } else {
933 129 : uchar * fee_payer_data = txn_out->accounts.rollback_fee_payer_mem;
934 129 : txn_out->accounts.rollback_fee_payer = fd_type_pun( fee_payer_data );
935 :
936 129 : fd_account_meta_t const * meta = NULL;
937 129 : if( FD_UNLIKELY( txn_in->bundle.is_bundle ) ) {
938 42 : int is_found = 0;
939 78 : for( ulong i=txn_in->bundle.prev_txn_cnt; i>0UL && !is_found; i-- ) {;
940 36 : fd_txn_out_t const * prev_txn_out = txn_in->bundle.prev_txn_outs[ i-1 ];
941 36 : for( ushort j=0UL; j<prev_txn_out->accounts.cnt; j++ ) {
942 36 : if( fd_pubkey_eq( &prev_txn_out->accounts.keys[ j ], fee_payer_key ) && prev_txn_out->accounts.is_writable[j] ) {
943 36 : meta = prev_txn_out->accounts.account[j].meta;
944 36 : is_found = 1;
945 36 : break;
946 36 : }
947 36 : }
948 36 : }
949 42 : }
950 :
951 129 : if( meta ) {
952 : /* Account modified in a previous transaction */
953 36 : fd_memcpy( fee_payer_data, (uchar *)meta, sizeof(fd_account_meta_t) + meta->dlen );
954 93 : } else {
955 : /* Copy from account database */
956 93 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
957 93 : fd_accdb_ro_t fee_payer_ro[1];
958 93 : if( FD_UNLIKELY( !fd_accdb_open_ro( runtime->accdb, fee_payer_ro, &xid, fee_payer_key ) ) ) {
959 0 : FD_BASE58_ENCODE_32_BYTES( fee_payer_key->uc, fee_payer_key_b58 );
960 0 : FD_LOG_CRIT(( "accdb query for fee payer account failed: xid=%lu:%lu address=%s", xid.ul[0], xid.ul[1], fee_payer_key_b58 ));
961 0 : }
962 93 : fd_memcpy( fee_payer_data,
963 93 : fee_payer_ro->meta,
964 93 : sizeof(fd_account_meta_t) );
965 93 : fd_memcpy( fee_payer_data+sizeof(fd_account_meta_t),
966 93 : fd_accdb_ref_data_const( fee_payer_ro ),
967 93 : fd_accdb_ref_data_sz ( fee_payer_ro ) );
968 93 : fd_accdb_close_ro( runtime->accdb, fee_payer_ro );
969 93 : }
970 129 : }
971 :
972 : /* Deduct the transaction fees from the rollback account. Because of prior checks, this should never fail. */
973 129 : if( FD_UNLIKELY( fd_account_meta_checked_sub_lamports( txn_out->accounts.rollback_fee_payer, total_fee ) ) ) {
974 0 : FD_LOG_ERR(( "fd_executor_create_rollback_fee_payer_account(): failed to deduct fees from rollback account" ));
975 0 : }
976 129 : }
977 :
978 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L557-L634 */
979 : int
980 : fd_executor_validate_transaction_fee_payer( fd_runtime_t * runtime,
981 : fd_bank_t * bank,
982 : fd_txn_in_t const * txn_in,
983 141 : fd_txn_out_t * txn_out ) {
984 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L574-L580 */
985 141 : int err = fd_runtime_get_account_at_index( txn_in,
986 141 : txn_out,
987 141 : FD_FEE_PAYER_TXN_IDX,
988 141 : fd_runtime_account_check_fee_payer_writable );
989 141 : if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS ) ) {
990 0 : FD_BASE58_ENCODE_32_BYTES( txn_out->accounts.keys[FD_FEE_PAYER_TXN_IDX].uc, pubkey_b58 );
991 0 : FD_LOG_DEBUG(( "Fee payer isn't writable %s", pubkey_b58 ));
992 0 : return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
993 0 : }
994 :
995 141 : fd_pubkey_t * fee_payer_key = &txn_out->accounts.keys[FD_FEE_PAYER_TXN_IDX];
996 141 : fd_account_meta_t * fee_payer_meta = txn_out->accounts.account[FD_FEE_PAYER_TXN_IDX].meta;
997 :
998 : /* Calculate transaction fees
999 : https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L597-L606 */
1000 141 : ulong execution_fee = 0UL;
1001 141 : ulong priority_fee = 0UL;
1002 :
1003 141 : fd_executor_calculate_fee( txn_out, TXN( txn_in->txn ), txn_in->txn->payload, &execution_fee, &priority_fee );
1004 141 : ulong total_fee = fd_ulong_sat_add( execution_fee, priority_fee );
1005 :
1006 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L609-L616 */
1007 141 : err = fd_validate_fee_payer( fee_payer_key, fee_payer_meta, fd_bank_rent_query( bank ), total_fee );
1008 141 : if( FD_UNLIKELY( err ) ) {
1009 12 : return err;
1010 12 : }
1011 :
1012 : /* Create the rollback fee payer account
1013 : https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L620-L626 */
1014 129 : fd_executor_create_rollback_fee_payer_account( runtime, bank, txn_in, txn_out, total_fee );
1015 :
1016 : /* Set the starting lamports (to avoid unbalanced lamports issues in instruction execution) */
1017 129 : runtime->accounts.starting_lamports[FD_FEE_PAYER_TXN_IDX] = fee_payer_meta->lamports;
1018 :
1019 129 : txn_out->details.execution_fee = execution_fee;
1020 129 : txn_out->details.priority_fee = priority_fee;
1021 :
1022 129 : return FD_RUNTIME_EXECUTE_SUCCESS;
1023 141 : }
1024 :
1025 : /* Simply unpacks the account keys from the serialized transaction and
1026 : sets them in the txn_out. */
1027 : void
1028 : fd_executor_setup_txn_account_keys( fd_txn_in_t const * txn_in,
1029 144 : fd_txn_out_t * txn_out ) {
1030 144 : txn_out->accounts.cnt = (uchar)TXN( txn_in->txn )->acct_addr_cnt;
1031 144 : fd_pubkey_t * tx_accs = (fd_pubkey_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->acct_addr_off);
1032 :
1033 : // Set up accounts in the transaction body and perform checks
1034 573 : for( ulong i = 0UL; i < TXN( txn_in->txn )->acct_addr_cnt; i++ ) {
1035 429 : txn_out->accounts.keys[i] = tx_accs[i];
1036 429 : }
1037 144 : }
1038 :
1039 : /* Resolves any address lookup tables referenced in the transaction and adds
1040 : them to the transaction's account keys. Returns 0 on success or if the transaction
1041 : is a legacy transaction, and an FD_RUNTIME_TXN_ERR_* on failure. */
1042 : int
1043 : fd_executor_setup_txn_alut_account_keys( fd_runtime_t * runtime,
1044 : fd_bank_t * bank,
1045 : fd_txn_in_t const * txn_in,
1046 141 : fd_txn_out_t * txn_out ) {
1047 141 : if( TXN( txn_in->txn )->transaction_version == FD_TXN_V0 ) {
1048 : /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/runtime/src/bank/address_lookup_table.rs#L44-L48 */
1049 69 : fd_sysvar_cache_t const * sysvar_cache = fd_bank_sysvar_cache_query( bank );
1050 69 : fd_slot_hash_t const * slot_hashes = fd_sysvar_cache_slot_hashes_join_const( sysvar_cache );
1051 69 : if( FD_UNLIKELY( !slot_hashes ) ) {
1052 0 : FD_LOG_DEBUG(( "fd_executor_setup_txn_alut_account_keys(): failed to get slot hashes" ));
1053 0 : return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
1054 0 : }
1055 69 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
1056 69 : fd_acct_addr_t * accts_alt = (fd_acct_addr_t *) fd_type_pun( &txn_out->accounts.keys[txn_out->accounts.cnt] );
1057 69 : int err = fd_runtime_load_txn_address_lookup_tables( TXN( txn_in->txn ),
1058 69 : txn_in->txn->payload,
1059 69 : runtime->accdb,
1060 69 : &xid,
1061 69 : fd_bank_slot_get( bank ),
1062 69 : slot_hashes,
1063 69 : accts_alt );
1064 69 : fd_sysvar_cache_slot_hashes_leave_const( sysvar_cache, slot_hashes );
1065 69 : txn_out->accounts.cnt += TXN( txn_in->txn )->addr_table_adtl_cnt;
1066 69 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
1067 :
1068 69 : }
1069 141 : return FD_RUNTIME_EXECUTE_SUCCESS;
1070 141 : }
1071 :
1072 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L319-L357 */
1073 : static inline int
1074 : fd_txn_ctx_push( fd_runtime_t * runtime,
1075 : fd_txn_in_t const * txn_in,
1076 : fd_txn_out_t * txn_out,
1077 126 : fd_instr_info_t * instr ) {
1078 : /* Earlier checks in the permalink are redundant since Agave maintains instr stack and trace accounts separately
1079 : https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L327-L328 */
1080 126 : ulong starting_lamports_h = 0UL;
1081 126 : ulong starting_lamports_l = 0UL;
1082 126 : int err = fd_instr_info_sum_account_lamports( instr,
1083 126 : txn_out,
1084 126 : &starting_lamports_h,
1085 126 : &starting_lamports_l );
1086 126 : if( FD_UNLIKELY( err ) ) {
1087 0 : return err;
1088 0 : }
1089 126 : instr->starting_lamports_h = starting_lamports_h;
1090 126 : instr->starting_lamports_l = starting_lamports_l;
1091 :
1092 : /* Check that the caller's lamport sum has not changed.
1093 : https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L329-L340 */
1094 126 : if( runtime->instr.stack_sz>0 ) {
1095 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L330 */
1096 0 : fd_exec_instr_ctx_t const * caller_instruction_context = &runtime->instr.stack[ runtime->instr.stack_sz-1 ];
1097 :
1098 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L331-L332 */
1099 0 : ulong original_caller_lamport_sum_h = caller_instruction_context->instr->starting_lamports_h;
1100 0 : ulong original_caller_lamport_sum_l = caller_instruction_context->instr->starting_lamports_l;
1101 :
1102 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L333-L334 */
1103 0 : ulong current_caller_lamport_sum_h = 0UL;
1104 0 : ulong current_caller_lamport_sum_l = 0UL;
1105 0 : int err = fd_instr_info_sum_account_lamports( caller_instruction_context->instr,
1106 0 : caller_instruction_context->txn_out,
1107 0 : ¤t_caller_lamport_sum_h,
1108 0 : ¤t_caller_lamport_sum_l );
1109 0 : if( FD_UNLIKELY( err ) ) {
1110 0 : return err;
1111 0 : }
1112 :
1113 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L335-L339 */
1114 0 : if( FD_UNLIKELY( current_caller_lamport_sum_h!=original_caller_lamport_sum_h ||
1115 0 : current_caller_lamport_sum_l!=original_caller_lamport_sum_l ) ) {
1116 0 : return FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR;
1117 0 : }
1118 0 : }
1119 :
1120 : /* Note that we don't update the trace length here - since the caller
1121 : allocates out of the trace array, they are also responsible for
1122 : incrementing the trace length variable.
1123 : https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L347-L351 */
1124 126 : if( FD_UNLIKELY( runtime->instr.trace_length>FD_MAX_INSTRUCTION_TRACE_LENGTH ) ) {
1125 0 : return FD_EXECUTOR_INSTR_ERR_MAX_INSN_TRACE_LENS_EXCEEDED;
1126 0 : }
1127 :
1128 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L352-L356 */
1129 126 : if( FD_UNLIKELY( runtime->instr.stack_sz>=FD_MAX_INSTRUCTION_STACK_DEPTH ) ) {
1130 0 : return FD_EXECUTOR_INSTR_ERR_CALL_DEPTH;
1131 0 : }
1132 126 : runtime->instr.stack_sz++;
1133 :
1134 : /* A beloved refactor moves sysvar instructions updating to the instruction level as of v2.2.12...
1135 : https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L396-L407 */
1136 126 : int idx = fd_runtime_find_index_of_account( txn_out, &fd_sysvar_instructions_id );
1137 126 : if( FD_UNLIKELY( idx!=-1 ) ) {
1138 : /* https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L397-L400 */
1139 0 : err = fd_runtime_get_account_at_index( txn_in, txn_out, (ushort)idx, NULL );
1140 0 : if( FD_UNLIKELY( err ) ) {
1141 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
1142 0 : }
1143 :
1144 0 : ulong refcnt = runtime->accounts.refcnt[idx];
1145 : /* https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L401-L402 */
1146 0 : if( FD_UNLIKELY( refcnt!=0UL ) ) {
1147 0 : return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED;
1148 0 : }
1149 0 : refcnt++;
1150 :
1151 : /* https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L403-L406 */
1152 0 : fd_sysvar_instructions_update_current_instr_idx( txn_out->accounts.account[idx].meta, (ushort)runtime->instr.current_idx );
1153 0 : refcnt--;
1154 0 : }
1155 :
1156 126 : return FD_EXECUTOR_INSTR_SUCCESS;
1157 126 : }
1158 :
1159 : /* Pushes a new instruction onto the instruction stack and trace. This check loops through all instructions in the current call stack
1160 : and checks for reentrancy violations. If successful, simply increments the instruction stack and trace size and returns. It is
1161 : the responsibility of the caller to populate the newly pushed instruction fields, which are undefined otherwise.
1162 :
1163 : https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L246-L290 */
1164 : int
1165 : fd_instr_stack_push( fd_runtime_t * runtime,
1166 : fd_txn_in_t const * txn_in,
1167 : fd_txn_out_t * txn_out,
1168 126 : fd_instr_info_t * instr ) {
1169 : /* Agave keeps a vector of vectors called program_indices that stores the program_id index for each instruction within the transaction.
1170 : https://github.com/anza-xyz/agave/blob/v2.1.7/svm/src/account_loader.rs#L347-L402
1171 : If and only if the program_id is the native loader, then the vector for respective specific instruction (account_indices) is empty.
1172 : https://github.com/anza-xyz/agave/blob/v2.1.7/svm/src/account_loader.rs#L350-L358
1173 : While trying to push a new instruction onto the instruction stack, if the vector for the respective instruction is empty, Agave throws UnsupportedProgramId
1174 : https://github.com/anza-xyz/agave/blob/v2.1.7/program-runtime/src/invoke_context.rs#L253-L255
1175 : The only way for the vector to be empty is if the program_id is the native loader, so we can a program_id check here
1176 : */
1177 :
1178 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/program-runtime/src/invoke_context.rs#L250-L252 */
1179 126 : fd_pubkey_t const * program_id_pubkey = NULL;
1180 126 : int err = fd_runtime_get_key_of_account_at_index( txn_out,
1181 126 : instr->program_id,
1182 126 : &program_id_pubkey );
1183 126 : if( FD_UNLIKELY( err ) ) {
1184 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
1185 0 : }
1186 :
1187 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L256-L286 */
1188 126 : if( runtime->instr.stack_sz ) {
1189 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L261-L285 */
1190 0 : uchar contains = 0;
1191 0 : uchar is_last = 0;
1192 :
1193 : // Checks all previous instructions in the stack for reentrancy
1194 0 : for( uchar level=0; level<runtime->instr.stack_sz; level++ ) {
1195 0 : fd_exec_instr_ctx_t * instr_ctx = &runtime->instr.stack[level];
1196 : // Optimization: compare program id index instead of pubkey since account keys are unique
1197 0 : if( instr->program_id == instr_ctx->instr->program_id ) {
1198 : // Reentrancy not allowed unless caller is calling itself
1199 0 : if( level == runtime->instr.stack_sz-1 ) {
1200 0 : is_last = 1;
1201 0 : }
1202 0 : contains = 1;
1203 0 : }
1204 0 : }
1205 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L282-L285 */
1206 0 : if( FD_UNLIKELY( contains && !is_last ) ) {
1207 0 : return FD_EXECUTOR_INSTR_ERR_REENTRANCY_NOT_ALLOWED;
1208 0 : }
1209 0 : }
1210 : /* "Push" a new instruction onto the stack by simply incrementing the stack and trace size counters
1211 : https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L289 */
1212 126 : return fd_txn_ctx_push( runtime, txn_in, txn_out, instr );
1213 126 : }
1214 :
1215 : /* Pops an instruction from the instruction stack. Agave's implementation performs instruction balancing checks every time pop is called,
1216 : but error codes returned from `pop` are only used if the program's execution was successful. Therefore, we can optimize our code by only
1217 : checking for unbalanced instructions if the program execution was successful within fd_execute_instr.
1218 :
1219 : https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L293-L298 */
1220 : int
1221 : fd_instr_stack_pop( fd_runtime_t * runtime,
1222 : fd_txn_out_t * txn_out,
1223 126 : fd_instr_info_t const * instr ) {
1224 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L362-L364 */
1225 126 : if( FD_UNLIKELY( runtime->instr.stack_sz==0 ) ) {
1226 0 : return FD_EXECUTOR_INSTR_ERR_CALL_DEPTH;
1227 0 : }
1228 126 : runtime->instr.stack_sz--;
1229 :
1230 : /* Verify all executable accounts have no outstanding refs
1231 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L367-L371 */
1232 5892 : for( ushort i=0; i<instr->acct_cnt; i++ ) {
1233 5766 : ushort idx_in_txn = instr->accounts[i].index_in_transaction;
1234 5766 : fd_account_meta_t const * meta = txn_out->accounts.account[ idx_in_txn ].meta;
1235 5766 : ulong refcnt = runtime->accounts.refcnt[idx_in_txn];
1236 5766 : if( FD_UNLIKELY( meta->executable && refcnt!=0UL ) ) {
1237 0 : return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_OUTSTANDING;
1238 0 : }
1239 5766 : }
1240 :
1241 : /* Verify lamports are balanced before and after instruction
1242 : https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L366-L380 */
1243 126 : ulong ending_lamports_h = 0UL;
1244 126 : ulong ending_lamports_l = 0UL;
1245 126 : int err = fd_instr_info_sum_account_lamports( instr,
1246 126 : txn_out,
1247 126 : &ending_lamports_h,
1248 126 : &ending_lamports_l );
1249 126 : if( FD_UNLIKELY( err ) ) {
1250 0 : return err;
1251 0 : }
1252 126 : if( FD_UNLIKELY( ending_lamports_l != instr->starting_lamports_l || ending_lamports_h != instr->starting_lamports_h ) ) {
1253 0 : return FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR;
1254 0 : }
1255 :
1256 126 : return FD_EXECUTOR_INSTR_SUCCESS;
1257 126 : }
1258 :
1259 : /* This function mimics Agave's `.and(self.pop())` functionality,
1260 : where we always pop the instruction stack no matter what the error code is.
1261 : https://github.com/anza-xyz/agave/blob/v2.2.12/program-runtime/src/invoke_context.rs#L480 */
1262 : static inline int
1263 : fd_execute_instr_end( fd_exec_instr_ctx_t * instr_ctx,
1264 : fd_instr_info_t * instr,
1265 126 : int instr_exec_result ) {
1266 126 : int stack_pop_err = fd_instr_stack_pop( instr_ctx->runtime, instr_ctx->txn_out, instr );
1267 :
1268 : /* Only report the stack pop error on success */
1269 126 : if( FD_UNLIKELY( instr_exec_result==FD_EXECUTOR_INSTR_SUCCESS && stack_pop_err ) ) {
1270 0 : FD_TXN_PREPARE_ERR_OVERWRITE( instr_ctx->txn_out );
1271 0 : FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, stack_pop_err, instr_ctx->txn_out->err.exec_err_idx );
1272 0 : instr_exec_result = stack_pop_err;
1273 0 : }
1274 :
1275 126 : return instr_exec_result;
1276 126 : }
1277 :
1278 : int
1279 : fd_execute_instr( fd_runtime_t * runtime,
1280 : fd_bank_t * bank,
1281 : fd_txn_in_t const * txn_in,
1282 : fd_txn_out_t * txn_out,
1283 126 : fd_instr_info_t * instr ) {
1284 126 : fd_sysvar_cache_t const * sysvar_cache = fd_bank_sysvar_cache_query( bank );
1285 126 : int instr_exec_result = fd_instr_stack_push( runtime, txn_in, txn_out, instr );
1286 126 : if( FD_UNLIKELY( instr_exec_result ) ) {
1287 0 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1288 0 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1289 0 : return instr_exec_result;
1290 0 : }
1291 :
1292 : /* `process_executable_chain()`
1293 : https://github.com/anza-xyz/agave/blob/v2.2.12/program-runtime/src/invoke_context.rs#L512-L619 */
1294 126 : fd_exec_instr_ctx_t * ctx = &runtime->instr.stack[ runtime->instr.stack_sz - 1 ];
1295 126 : *ctx = (fd_exec_instr_ctx_t) {
1296 126 : .instr = instr,
1297 126 : .sysvar_cache = sysvar_cache,
1298 126 : .runtime = runtime,
1299 126 : .txn_in = txn_in,
1300 126 : .txn_out = txn_out,
1301 126 : .bank = bank,
1302 126 : };
1303 126 : fd_base58_encode_32( txn_out->accounts.keys[ instr->program_id ].uc, NULL, ctx->program_id_base58 );
1304 :
1305 : /* Look up the native program. We check for precompiles within the lookup function as well.
1306 : https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/message_processor.rs#L88 */
1307 126 : fd_exec_instr_fn_t native_prog_fn;
1308 126 : uchar is_precompile;
1309 126 : int err = fd_executor_lookup_native_program( &txn_out->accounts.keys[ instr->program_id ],
1310 126 : txn_out->accounts.account[ instr->program_id ].meta,
1311 126 : bank,
1312 126 : &native_prog_fn,
1313 126 : &is_precompile );
1314 :
1315 126 : if( FD_UNLIKELY( err ) ) {
1316 0 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1317 0 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, err, txn_out->err.exec_err_idx );
1318 0 : return fd_execute_instr_end( ctx, instr, err );
1319 0 : }
1320 :
1321 126 : if( FD_LIKELY( native_prog_fn!=NULL ) ) {
1322 : /* If this branch is taken, we've found an entrypoint to execute. */
1323 126 : fd_log_collector_program_invoke( ctx );
1324 :
1325 : /* Only reset the return data when executing a native builtin program (not a precompile)
1326 : https://github.com/anza-xyz/agave/blob/v2.1.6/program-runtime/src/invoke_context.rs#L536-L537 */
1327 126 : if( FD_LIKELY( !is_precompile ) ) {
1328 126 : txn_out->details.return_data.len = 0;
1329 126 : }
1330 :
1331 : /* Execute the native program. */
1332 126 : instr_exec_result = native_prog_fn( ctx );
1333 126 : } else {
1334 : /* Unknown program. In this case specifically, we should not log the program id. */
1335 0 : instr_exec_result = FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
1336 0 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1337 0 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1338 0 : return fd_execute_instr_end( ctx, instr, instr_exec_result );
1339 0 : }
1340 :
1341 126 : if( FD_LIKELY( instr_exec_result==FD_EXECUTOR_INSTR_SUCCESS ) ) {
1342 : /* Log success */
1343 96 : fd_log_collector_program_success( ctx );
1344 96 : } else {
1345 : /* Log failure cases.
1346 : We assume that the correct type of error is stored in ctx.
1347 : Syscalls are expected to log when the error is generated, while
1348 : native programs will be logged here.
1349 : (This is because syscall errors often carry data with them.)
1350 :
1351 : TODO: This hackily handles cases where the exec_err and exec_err_kind
1352 : is not set yet. We should change our native programs to set
1353 : this in their respective processors. */
1354 30 : if( !txn_out->err.exec_err ) {
1355 30 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1356 30 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1357 30 : fd_log_collector_program_failure( ctx );
1358 30 : } else {
1359 0 : fd_log_collector_program_failure( ctx );
1360 0 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1361 0 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1362 0 : }
1363 30 : }
1364 :
1365 126 : return fd_execute_instr_end( ctx, instr, instr_exec_result );
1366 126 : }
1367 :
1368 : static void
1369 : fd_executor_reclaim_account( fd_account_meta_t * meta,
1370 189 : ulong slot ) {
1371 189 : if( FD_UNLIKELY( meta->lamports==0UL ) ) {
1372 12 : memset( meta, 0, sizeof(fd_account_meta_t) );
1373 12 : }
1374 189 : meta->slot = slot;
1375 189 : }
1376 :
1377 : static void
1378 : fd_executor_setup_txn_account( fd_runtime_t * runtime,
1379 : fd_bank_t * bank,
1380 : fd_txn_in_t const * txn_in,
1381 : fd_txn_out_t * txn_out,
1382 : ushort idx,
1383 : uchar * * writable_accs_mem,
1384 423 : ulong * writable_accs_idx_out ) {
1385 : /* To setup a transaction account, we need to first retrieve a
1386 : read-only handle to the account from the database. */
1387 :
1388 423 : fd_pubkey_t * address = &txn_out->accounts.keys[ idx ];
1389 423 : fd_accdb_rw_t * ref_slot = &txn_out->accounts.account[ idx ];
1390 :
1391 423 : fd_accdb_rw_t * account = NULL;
1392 423 : int is_found_in_bundle = 0;
1393 423 : if( txn_in->bundle.is_bundle ) {
1394 : /* If we are in a bundle, that means that the latest version of an
1395 : account may be a transaction account from a previous transaction
1396 : and not in the accounts database. This means we have to
1397 : reference the previous transaction's account. Because we are in
1398 : a bundle, we know that the transaction accounts for all previous
1399 : bundle transactions are valid. We will also assume that the
1400 : transactions are in execution order.
1401 :
1402 : TODO: This lookup can be made more performant by using a map
1403 : from pubkey to the bundle transaction index and only inserting
1404 : or updating when the account is writable. */
1405 :
1406 162 : for( ulong i=txn_in->bundle.prev_txn_cnt; i>0UL && !is_found_in_bundle; i-- ) {
1407 78 : fd_txn_out_t * prev_txn_out = txn_in->bundle.prev_txn_outs[ i-1 ];
1408 126 : for( ushort j=0UL; j<prev_txn_out->accounts.cnt; j++ ) {
1409 120 : if( fd_pubkey_eq( &prev_txn_out->accounts.keys[ j ], address ) && prev_txn_out->accounts.is_writable[j] ) {
1410 : /* Found the account in a previous transaction. Move ownership
1411 : of reference from previous transaction to this one. */
1412 72 : fd_memcpy( ref_slot, prev_txn_out->accounts.account[ j ].ref, sizeof(fd_accdb_rw_t) );
1413 72 : account = ref_slot;
1414 72 : is_found_in_bundle = 1;
1415 :
1416 : /* If the account being carried forward from the previous txn
1417 : had queued an update to the vote/stakes caches and is
1418 : writable in the new transaction, unmark the update to avoid
1419 : double-counting the update. */
1420 72 : if( FD_UNLIKELY( txn_out->accounts.is_writable[ idx ] &&
1421 72 : (prev_txn_out->accounts.stake_update[ j ] || prev_txn_out->accounts.vote_update[ j ]) ) ) {
1422 0 : prev_txn_out->accounts.stake_update[ j ] = 0;
1423 0 : prev_txn_out->accounts.vote_update[ j ] = 0;
1424 0 : }
1425 :
1426 72 : break;
1427 72 : }
1428 120 : }
1429 78 : }
1430 84 : }
1431 :
1432 423 : if( FD_LIKELY( !account ) ) {
1433 351 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
1434 351 : account = (fd_accdb_rw_t *)fd_accdb_open_ro( runtime->accdb, ref_slot->ro, &xid, address );
1435 : /* creates a database reference, which is explicitly dropped here
1436 : or in commit/cancel */
1437 351 : }
1438 :
1439 423 : if( txn_out->accounts.is_writable[ idx ] ) {
1440 : /* If the account is writable or a fee payer, then we need to create
1441 : staging regions for the account. If the account exists, copy the
1442 : account data into the staging area; otherwise, initialize a new
1443 : metadata. */
1444 270 : uchar * new_raw_data = writable_accs_mem[ *writable_accs_idx_out ];
1445 270 : ulong dlen = !!account ? fd_accdb_ref_data_sz( (fd_accdb_ro_t *)account ) : 0UL;
1446 270 : (*writable_accs_idx_out)++;
1447 :
1448 270 : if( FD_LIKELY( account ) ) {
1449 : /* Create copy of account, release reference of original */
1450 267 : fd_memcpy( new_raw_data, account->meta, sizeof(fd_account_meta_t)+dlen );
1451 267 : fd_accdb_close_ro( runtime->accdb, (fd_accdb_ro_t *)account );
1452 267 : } else {
1453 : /* Account did not exist, set up metadata */
1454 3 : fd_account_meta_init( (fd_account_meta_t *)new_raw_data );
1455 3 : }
1456 :
1457 270 : account = fd_accdb_rw_init_nodb(
1458 270 : (fd_accdb_rw_t *)ref_slot,
1459 270 : address,
1460 270 : (fd_account_meta_t *)new_raw_data,
1461 270 : FD_RUNTIME_ACC_SZ_MAX
1462 270 : );
1463 :
1464 270 : } else {
1465 : /* If the account is not writable, then we can simply initialize
1466 : the txn account with the read-only accountsdb record. However,
1467 : if the account does not exist, we need to initialize a new
1468 : metadata. */
1469 153 : if( FD_UNLIKELY( fd_pubkey_eq( address, &fd_sysvar_instructions_id ) ) ) {
1470 0 : fd_account_meta_t * meta = fd_account_meta_init( (void *)runtime->accounts.sysvar_instructions_mem );
1471 0 : account = (fd_accdb_rw_t *)fd_accdb_ro_init_nodb( (fd_accdb_ro_t *)ref_slot, address, meta );
1472 153 : } else if( FD_LIKELY( account && !is_found_in_bundle ) ) {
1473 : /* transfer ownership of reference to runtime struct
1474 : account is freed in cancel/commit */
1475 132 : } else if( FD_LIKELY( account && is_found_in_bundle ) ) {
1476 : /* If the account is found in the bundle and marked read-only we
1477 : just need to initialize a reference to the account that doesn't
1478 : reference the database. */
1479 9 : account = (fd_accdb_rw_t *)fd_accdb_ro_init_nodb( (fd_accdb_ro_t *)ref_slot, address, account->meta );
1480 12 : } else {
1481 12 : account = (fd_accdb_rw_t *)fd_accdb_ro_init_nodb( (fd_accdb_ro_t *)ref_slot, address, &FD_ACCOUNT_META_DEFAULT );
1482 12 : }
1483 153 : }
1484 :
1485 423 : runtime->accounts.starting_lamports[idx] = fd_accdb_ref_lamports( account->ro );
1486 423 : runtime->accounts.starting_dlen[idx] = fd_accdb_ref_data_sz ( account->ro );
1487 423 : runtime->accounts.refcnt[idx] = 0UL;
1488 423 : }
1489 :
1490 : static void
1491 : fd_executor_setup_executable_account( fd_runtime_t * runtime,
1492 : fd_bank_t * bank,
1493 : fd_account_meta_t const * program_meta,
1494 0 : ushort * executable_idx ) {
1495 0 : fd_bpf_upgradeable_loader_state_t program_loader_state[1];
1496 0 : int err = fd_bpf_loader_program_get_state( program_meta, program_loader_state );
1497 0 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1498 0 : return;
1499 0 : }
1500 :
1501 0 : if( !fd_bpf_upgradeable_loader_state_is_program( program_loader_state ) ) {
1502 0 : return;
1503 0 : }
1504 :
1505 : /* Attempt to load the program data account from funk. This prevents any unknown program
1506 : data accounts from getting loaded into the executable accounts list. If such a program is
1507 : invoked, the call will fail at the instruction execution level since the programdata
1508 : account will not exist within the executable accounts list. */
1509 0 : fd_pubkey_t * programdata_acc = &program_loader_state->inner.program.programdata_address;
1510 0 : fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
1511 :
1512 0 : fd_accdb_ro_t * ro = &runtime->accounts.executable[ *executable_idx ];
1513 0 : ro = fd_accdb_open_ro( runtime->accdb, ro, &xid, programdata_acc );
1514 0 : if( FD_LIKELY( ro ) ) (*executable_idx)++;
1515 0 : }
1516 :
1517 : void
1518 : fd_executor_setup_accounts_for_txn( fd_runtime_t * runtime,
1519 : fd_bank_t * bank,
1520 : fd_txn_in_t const * txn_in,
1521 141 : fd_txn_out_t * txn_out ) {
1522 :
1523 : /* At this point, the total number of writable accounts in the
1524 : transaction is known. We can now attempt to get the required
1525 : amount of memory from the account memory pool. */
1526 :
1527 141 : ushort writable_account_cnt = 0U;
1528 564 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
1529 423 : if( fd_runtime_account_is_writable_idx( txn_in, txn_out, i ) ) {
1530 270 : txn_out->accounts.is_writable[ i ] = 1;
1531 270 : writable_account_cnt++;
1532 270 : } else {
1533 153 : txn_out->accounts.is_writable[ i ] = 0;
1534 153 : }
1535 423 : }
1536 :
1537 : /* At this point we know which accounts are writable, but we don't
1538 : know if we will need to create an account for the rollback fee
1539 : payer or nonce account. To avoid a potential deadlock, we want to
1540 : request the worst-case number of accounts (# writable accounts + 2
1541 : rollback accounts) for the transaction in one call to
1542 : fd_acc_pool_acquire. */
1543 :
1544 141 : ulong writable_accs_idx = 0UL;
1545 141 : uchar * writable_accs_mem[ MAX_TX_ACCOUNT_LOCKS + 2UL ];
1546 141 : fd_acc_pool_acquire( runtime->acc_pool, writable_account_cnt + 2UL, writable_accs_mem );
1547 141 : txn_out->accounts.rollback_fee_payer_mem = writable_accs_mem[ writable_account_cnt ];
1548 141 : txn_out->accounts.rollback_nonce_mem = writable_accs_mem[ writable_account_cnt+1UL ];
1549 :
1550 141 : ushort executable_idx = 0U;
1551 564 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
1552 423 : fd_executor_setup_txn_account( runtime, bank, txn_in, txn_out, i, writable_accs_mem, &writable_accs_idx );
1553 423 : fd_account_meta_t * meta = txn_out->accounts.account[ i ].meta;
1554 :
1555 423 : if( FD_UNLIKELY( meta && memcmp( meta->owner, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) == 0 ) ) {
1556 0 : fd_executor_setup_executable_account( runtime, bank, meta, &executable_idx );
1557 0 : }
1558 423 : }
1559 :
1560 141 : txn_out->accounts.is_setup = 1;
1561 141 : txn_out->accounts.nonce_idx_in_txn = ULONG_MAX;
1562 141 : runtime->accounts.executable_cnt = executable_idx;
1563 141 : }
1564 :
1565 : int
1566 : fd_executor_txn_verify( fd_txn_p_t * txn_p,
1567 0 : fd_sha512_t * shas[ FD_TXN_ACTUAL_SIG_MAX ] ) {
1568 0 : fd_txn_t * txn = TXN( txn_p );
1569 :
1570 0 : uchar * signatures = txn_p->payload + txn->signature_off;
1571 0 : uchar * pubkeys = txn_p->payload + txn->acct_addr_off;
1572 0 : uchar * msg = txn_p->payload + txn->message_off;
1573 0 : ulong msg_sz = txn_p->payload_sz - txn->message_off;
1574 :
1575 0 : int res = fd_ed25519_verify_batch_single_msg( msg, msg_sz, signatures, pubkeys, shas, txn->signature_cnt );
1576 0 : if( FD_UNLIKELY( res != FD_ED25519_SUCCESS ) ) {
1577 0 : return FD_RUNTIME_TXN_ERR_SIGNATURE_FAILURE;
1578 0 : }
1579 :
1580 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
1581 0 : }
1582 :
1583 : static int
1584 : fd_executor_txn_check( fd_runtime_t * runtime,
1585 : fd_bank_t * bank,
1586 99 : fd_txn_out_t * txn_out ) {
1587 99 : fd_rent_t const * rent = fd_bank_rent_query( bank );
1588 :
1589 99 : ulong starting_lamports_l = 0UL;
1590 99 : ulong starting_lamports_h = 0UL;
1591 99 : ulong ending_lamports_l = 0UL;
1592 99 : ulong ending_lamports_h = 0UL;
1593 :
1594 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L63 */
1595 369 : for( ulong i=0UL; i<txn_out->accounts.cnt; i++ ) {
1596 270 : if( !txn_out->accounts.is_writable[i] ) continue;
1597 :
1598 189 : ulong starting_lamports = runtime->accounts.starting_lamports[i];
1599 189 : ulong starting_dlen = runtime->accounts.starting_dlen[i];
1600 189 : fd_account_meta_t * meta = txn_out->accounts.account[i].meta;
1601 189 : fd_pubkey_t * pubkey = &txn_out->accounts.keys[i];
1602 :
1603 189 : fd_uwide_inc( &ending_lamports_h, &ending_lamports_l, ending_lamports_h, ending_lamports_l, meta->lamports );
1604 189 : fd_uwide_inc( &starting_lamports_h, &starting_lamports_l, starting_lamports_h, starting_lamports_l, starting_lamports );
1605 :
1606 : /* Rent states are defined as followed:
1607 : - lamports == 0 -> Uninitialized
1608 : - 0 < lamports < rent_exempt_minimum -> RentPaying
1609 : - lamports >= rent_exempt_minimum -> RentExempt
1610 : In Agave, 'self' refers to our 'after' state. */
1611 189 : uchar after_uninitialized = meta->lamports==0UL;
1612 189 : uchar after_rent_exempt = meta->lamports>=fd_rent_exempt_minimum_balance( rent, meta->dlen );
1613 :
1614 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L96 */
1615 189 : if( FD_LIKELY( memcmp( pubkey, fd_sysvar_incinerator_id.key, sizeof(fd_pubkey_t) ) ) ) {
1616 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L44 */
1617 189 : if( after_uninitialized || after_rent_exempt ) {
1618 : // no-op
1619 186 : } else {
1620 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L45-L59 */
1621 3 : uchar before_uninitialized = starting_lamports==0UL;
1622 3 : uchar before_rent_exempt = starting_lamports>=fd_rent_exempt_minimum_balance( rent, starting_dlen );
1623 :
1624 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L50 */
1625 3 : if( FD_UNLIKELY( before_uninitialized || before_rent_exempt ) ) {
1626 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L104 */
1627 0 : return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_RENT;
1628 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L56 */
1629 3 : } else if( (meta->dlen==starting_dlen) && meta->lamports<=starting_lamports ) {
1630 : // no-op
1631 3 : } else {
1632 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L104 */
1633 0 : return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_RENT;
1634 0 : }
1635 3 : }
1636 189 : }
1637 :
1638 189 : if ( !memcmp( meta->owner, &fd_solana_stake_program_id, sizeof(fd_pubkey_t) ) ) txn_out->accounts.stake_update[i] = 1;
1639 189 : else if( !memcmp( meta->owner, &fd_solana_vote_program_id, sizeof(fd_pubkey_t) ) ) txn_out->accounts.vote_update[i] = 1;
1640 :
1641 189 : if( FD_LIKELY( !runtime->fuzz.enabled ) ) {
1642 189 : fd_executor_reclaim_account( meta, fd_bank_slot_get( bank ) );
1643 189 : }
1644 189 : }
1645 :
1646 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/transaction_processor.rs#L839-L845 */
1647 99 : if( FD_UNLIKELY( ending_lamports_l!=starting_lamports_l || ending_lamports_h!=starting_lamports_h ) ) {
1648 0 : return FD_RUNTIME_TXN_ERR_UNBALANCED_TRANSACTION;
1649 0 : }
1650 :
1651 99 : return FD_RUNTIME_EXECUTE_SUCCESS;
1652 99 : }
1653 :
1654 :
1655 : int
1656 : fd_execute_txn( fd_runtime_t * runtime,
1657 : fd_bank_t * bank,
1658 : fd_txn_in_t const * txn_in,
1659 129 : fd_txn_out_t * txn_out ) {
1660 129 : fd_accdb_user_t * accdb = runtime->accdb;
1661 :
1662 129 : bool dump_insn = runtime->log.dump_proto_ctx &&
1663 129 : fd_bank_slot_get( bank )>=runtime->log.dump_proto_ctx->dump_proto_start_slot &&
1664 129 : runtime->log.dump_proto_ctx->dump_instr_to_pb;
1665 129 : (void)dump_insn;
1666 :
1667 129 : fd_txn_t const * txn = TXN( txn_in->txn );
1668 :
1669 : /* Initialize log collection. */
1670 129 : if( !!runtime->log.log_collector ) fd_log_collector_init( runtime->log.log_collector, runtime->log.enable_log_collector );
1671 :
1672 195 : for( ushort i=0; i<TXN( txn_in->txn )->instr_cnt; i++ ) {
1673 : /* Set up the instr info for the current instruction */
1674 96 : fd_instr_info_t * instr_info = &runtime->instr.trace[runtime->instr.trace_length++];
1675 96 : fd_instr_info_init_from_txn_instr(
1676 96 : instr_info,
1677 96 : txn_in,
1678 96 : txn_out,
1679 96 : &txn->instr[i]
1680 96 : );
1681 :
1682 96 : # if FD_HAS_FLATCC
1683 96 : if( FD_UNLIKELY( dump_insn ) ) {
1684 : // Capture the input and convert it into a Protobuf message
1685 0 : fd_dump_instr_to_protobuf( runtime, bank, txn_in, txn_out, instr_info, i );
1686 0 : }
1687 96 : # endif
1688 :
1689 : /* Update the current executing instruction index */
1690 96 : runtime->instr.current_idx = i;
1691 :
1692 : /* Execute the current instruction */
1693 96 : ulong account_refs_pre = accdb->base.ro_active + accdb->base.rw_active;
1694 96 : int instr_exec_result = fd_execute_instr( runtime, bank, txn_in, txn_out, instr_info );
1695 96 : ulong account_refs_post = accdb->base.ro_active + accdb->base.rw_active;
1696 96 : if( FD_UNLIKELY( account_refs_post != account_refs_pre ) ) {
1697 0 : FD_BASE58_ENCODE_64_BYTES( fd_txn_get_signatures( txn, txn_in->txn->payload )[0], txn_b58 );
1698 0 : FD_LOG_CRIT(( "fd_execute_instr(txn=%s,instr_idx=%u) leaked %lu account references",
1699 0 : txn_b58, i, account_refs_post-account_refs_pre ));
1700 0 : }
1701 96 : if( FD_UNLIKELY( instr_exec_result!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1702 30 : if( txn_out->err.exec_err_idx==INT_MAX ) {
1703 30 : txn_out->err.exec_err_idx = i;
1704 30 : }
1705 30 : return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
1706 30 : }
1707 96 : }
1708 :
1709 : /* TODO: This function needs to be split out of fd_execute_txn and be placed
1710 : into the replay tile once it is implemented. */
1711 99 : return fd_executor_txn_check( runtime, bank, txn_out );
1712 129 : }
1713 :
1714 : int
1715 : fd_executor_consume_cus( fd_txn_out_t * txn_out,
1716 126 : ulong cus ) {
1717 126 : ulong new_cus = txn_out->details.compute_budget.compute_meter - cus;
1718 126 : int underflow = (txn_out->details.compute_budget.compute_meter < cus);
1719 126 : if( FD_UNLIKELY( underflow ) ) {
1720 0 : txn_out->details.compute_budget.compute_meter = 0UL;
1721 0 : return FD_EXECUTOR_INSTR_ERR_COMPUTE_BUDGET_EXCEEDED;
1722 0 : }
1723 126 : txn_out->details.compute_budget.compute_meter = new_cus;
1724 126 : return FD_EXECUTOR_INSTR_SUCCESS;
1725 126 : }
1726 :
1727 : /* fd_executor_instr_strerror() returns the error message corresponding to err,
1728 : intended to be logged by log_collector, or an empty string if the error code
1729 : should be omitted in logs for whatever reason. Omitted examples are success,
1730 : fatal (placeholder just in firedancer), custom error.
1731 : See also fd_log_collector_program_failure(). */
1732 : FD_FN_CONST char const *
1733 0 : fd_executor_instr_strerror( int err ) {
1734 :
1735 0 : switch( err ) {
1736 0 : case FD_EXECUTOR_INSTR_SUCCESS : return ""; // not used
1737 0 : case FD_EXECUTOR_INSTR_ERR_FATAL : return ""; // not used
1738 0 : case FD_EXECUTOR_INSTR_ERR_GENERIC_ERR : return "generic instruction error";
1739 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_ARG : return "invalid program argument";
1740 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA : return "invalid instruction data";
1741 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA : return "invalid account data for instruction";
1742 0 : case FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL : return "account data too small for instruction";
1743 0 : case FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS : return "insufficient funds for instruction";
1744 0 : case FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID : return "incorrect program id for instruction";
1745 0 : case FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE : return "missing required signature for instruction";
1746 0 : case FD_EXECUTOR_INSTR_ERR_ACC_ALREADY_INITIALIZED : return "instruction requires an uninitialized account";
1747 0 : case FD_EXECUTOR_INSTR_ERR_UNINITIALIZED_ACCOUNT : return "instruction requires an initialized account";
1748 0 : case FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR : return "sum of account balances before and after instruction do not match";
1749 0 : case FD_EXECUTOR_INSTR_ERR_MODIFIED_PROGRAM_ID : return "instruction illegally modified the program id of an account";
1750 0 : case FD_EXECUTOR_INSTR_ERR_EXTERNAL_ACCOUNT_LAMPORT_SPEND : return "instruction spent from the balance of an account it does not own";
1751 0 : case FD_EXECUTOR_INSTR_ERR_EXTERNAL_DATA_MODIFIED : return "instruction modified data of an account it does not own";
1752 0 : case FD_EXECUTOR_INSTR_ERR_READONLY_LAMPORT_CHANGE : return "instruction changed the balance of a read-only account";
1753 0 : case FD_EXECUTOR_INSTR_ERR_READONLY_DATA_MODIFIED : return "instruction modified data of a read-only account";
1754 0 : case FD_EXECUTOR_INSTR_ERR_DUPLICATE_ACCOUNT_IDX : return "instruction contains duplicate accounts";
1755 0 : case FD_EXECUTOR_INSTR_ERR_EXECUTABLE_MODIFIED : return "instruction changed executable bit of an account";
1756 0 : case FD_EXECUTOR_INSTR_ERR_RENT_EPOCH_MODIFIED : return "instruction modified rent epoch of an account";
1757 0 : case FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS : return "insufficient account keys for instruction";
1758 0 : case FD_EXECUTOR_INSTR_ERR_ACC_DATA_SIZE_CHANGED : return "program other than the account's owner changed the size of the account data";
1759 0 : case FD_EXECUTOR_INSTR_ERR_ACC_NOT_EXECUTABLE : return "instruction expected an executable account";
1760 0 : case FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED : return "instruction tries to borrow reference for an account which is already borrowed";
1761 0 : case FD_EXECUTOR_INSTR_ERR_ACC_BORROW_OUTSTANDING : return "instruction left account with an outstanding borrowed reference";
1762 0 : case FD_EXECUTOR_INSTR_ERR_DUPLICATE_ACCOUNT_OUT_OF_SYNC : return "instruction modifications of multiply-passed account differ";
1763 0 : case FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR : return ""; // custom handling via txn_ctx->err.custom_err
1764 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_ERR : return "program returned invalid error code";
1765 0 : case FD_EXECUTOR_INSTR_ERR_EXECUTABLE_DATA_MODIFIED : return "instruction changed executable accounts data";
1766 0 : case FD_EXECUTOR_INSTR_ERR_EXECUTABLE_LAMPORT_CHANGE : return "instruction changed the balance of an executable account";
1767 0 : case FD_EXECUTOR_INSTR_ERR_EXECUTABLE_ACCOUNT_NOT_RENT_EXEMPT : return "executable accounts must be rent exempt";
1768 0 : case FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID : return "Unsupported program id";
1769 0 : case FD_EXECUTOR_INSTR_ERR_CALL_DEPTH : return "Cross-program invocation call depth too deep";
1770 0 : case FD_EXECUTOR_INSTR_ERR_MISSING_ACC : return "An account required by the instruction is missing";
1771 0 : case FD_EXECUTOR_INSTR_ERR_REENTRANCY_NOT_ALLOWED : return "Cross-program invocation reentrancy not allowed for this instruction";
1772 0 : case FD_EXECUTOR_INSTR_ERR_MAX_SEED_LENGTH_EXCEEDED : return "Length of the seed is too long for address generation";
1773 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_SEEDS : return "Provided seeds do not result in a valid address";
1774 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC : return "Failed to reallocate account data";
1775 0 : case FD_EXECUTOR_INSTR_ERR_COMPUTE_BUDGET_EXCEEDED : return "Computational budget exceeded";
1776 0 : case FD_EXECUTOR_INSTR_ERR_PRIVILEGE_ESCALATION : return "Cross-program invocation with unauthorized signer or writable account";
1777 0 : case FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE : return "Failed to create program execution environment";
1778 0 : case FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPLETE : return "Program failed to complete";
1779 0 : case FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPILE : return "Program failed to compile";
1780 0 : case FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE : return "Account is immutable";
1781 0 : case FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY : return "Incorrect authority provided";
1782 0 : case FD_EXECUTOR_INSTR_ERR_BORSH_IO_ERROR : return "Failed to serialize or deserialize account data"; // truncated
1783 0 : case FD_EXECUTOR_INSTR_ERR_ACC_NOT_RENT_EXEMPT : return "An account does not have enough lamports to be rent-exempt";
1784 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER : return "Invalid account owner";
1785 0 : case FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW : return "Program arithmetic overflowed";
1786 0 : case FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR : return "Unsupported sysvar";
1787 0 : case FD_EXECUTOR_INSTR_ERR_ILLEGAL_OWNER : return "Provided owner is not allowed";
1788 0 : case FD_EXECUTOR_INSTR_ERR_MAX_ACCS_DATA_ALLOCS_EXCEEDED : return "Accounts data allocations exceeded the maximum allowed per transaction";
1789 0 : case FD_EXECUTOR_INSTR_ERR_MAX_ACCS_EXCEEDED : return "Max accounts exceeded";
1790 0 : case FD_EXECUTOR_INSTR_ERR_MAX_INSN_TRACE_LENS_EXCEEDED : return "Max instruction trace length exceeded";
1791 0 : case FD_EXECUTOR_INSTR_ERR_BUILTINS_MUST_CONSUME_CUS : return "Builtin programs must consume compute units";
1792 0 : default: break;
1793 0 : }
1794 :
1795 0 : return "";
1796 0 : }
|