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