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