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