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