Line data Source code
1 : #include "fd_executor.h"
2 : #include "fd_runtime.h"
3 : #include "fd_runtime_helpers.h"
4 : #include "fd_compute_budget_details.h"
5 :
6 : #include "tests/fd_dump_pb.h"
7 : #include "fd_system_ids.h"
8 : #include "fd_system_ids_pp.h"
9 : #include "program/fd_bpf_loader_program.h"
10 : #include "program/fd_compute_budget_program.h"
11 : #include "program/fd_precompiles.h"
12 : #include "program/fd_system_program.h"
13 : #include "program/fd_builtin_programs.h"
14 : #include "program/fd_vote_program.h"
15 : #include "program/fd_zk_elgamal_proof_program.h"
16 : #include "sysvar/fd_sysvar_instructions.h"
17 : #include "sysvar/fd_sysvar_rent.h"
18 :
19 : #include "../log_collector/fd_log_collector.h"
20 :
21 : #include "../../disco/pack/fd_pack_tip_prog_blacklist.h"
22 : #include "../../ballet/txn/fd_txn_v1.h"
23 :
24 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/rent_state.rs#L5-L15 */
25 : struct fd_rent_state {
26 : uint discriminant;
27 : ulong lamports;
28 : ulong data_size;
29 : };
30 : typedef struct fd_rent_state fd_rent_state_t;
31 :
32 180 : #define FD_RENT_STATE_UNINITIALIZED (0U)
33 4182 : #define FD_RENT_STATE_RENT_PAYING (1U)
34 6690 : #define FD_RENT_STATE_RENT_EXEMPT (2U)
35 :
36 : #define MAP_PERFECT_NAME fd_native_program_fn_lookup_tbl
37 : #define MAP_PERFECT_LG_TBL_SZ 3
38 : #define MAP_PERFECT_T fd_native_prog_info_t
39 8658 : #define MAP_PERFECT_HASH_C 1069U
40 : #define MAP_PERFECT_KEY key.uc
41 : #define MAP_PERFECT_KEY_T fd_pubkey_t const *
42 : #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)
43 : #define MAP_PERFECT_COMPLEX_KEY 1
44 8658 : #define MAP_PERFECT_KEYS_EQUAL(k1,k2) (!memcmp( (k1), (k2), 32UL ))
45 :
46 8658 : #define PERFECT_HASH( u ) (((MAP_PERFECT_HASH_C*(u))>>29)&0x7U)
47 :
48 : #define MAP_PERFECT_HASH_PP( a00,a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,a12,a13,a14,a15, \
49 : a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31) \
50 : PERFECT_HASH( (a08 | (a09<<8) | (a10<<16) | (a11<<24)) )
51 8658 : #define MAP_PERFECT_HASH_R( ptr ) PERFECT_HASH( fd_uint_load_4( (uchar const *)ptr + 8UL ) )
52 :
53 : #define MAP_PERFECT_0 ( VOTE_PROG_ID ), .fn = fd_vote_program_execute, .is_bpf_loader = 0, .feature_enable_offset = ULONG_MAX
54 : #define MAP_PERFECT_1 ( SYS_PROG_ID ), .fn = fd_system_program_execute, .is_bpf_loader = 0, .feature_enable_offset = ULONG_MAX
55 : #define MAP_PERFECT_2 ( COMPUTE_BUDGET_PROG_ID ), .fn = fd_compute_budget_program_execute, .is_bpf_loader = 0, .feature_enable_offset = ULONG_MAX
56 : #define MAP_PERFECT_3 ( ZK_EL_GAMAL_PROG_ID ), .fn = fd_executor_zk_elgamal_proof_program_execute, .is_bpf_loader = 0, .feature_enable_offset = ULONG_MAX
57 : #define MAP_PERFECT_4 ( BPF_LOADER_1_PROG_ID ), .fn = fd_bpf_loader_program_execute, .is_bpf_loader = 1, .feature_enable_offset = ULONG_MAX
58 : #define MAP_PERFECT_5 ( BPF_LOADER_2_PROG_ID ), .fn = fd_bpf_loader_program_execute, .is_bpf_loader = 1, .feature_enable_offset = ULONG_MAX
59 : #define MAP_PERFECT_6 ( BPF_UPGRADEABLE_PROG_ID ), .fn = fd_bpf_loader_program_execute, .is_bpf_loader = 1, .feature_enable_offset = ULONG_MAX
60 : #define MAP_PERFECT_7 ( LOADER_V4_PROG_ID ), .fn = NULL, .is_bpf_loader = 1, .feature_enable_offset = offsetof( fd_features_t, enable_loader_v4 )
61 :
62 : #include "../../util/tmpl/fd_map_perfect.c"
63 : #undef PERFECT_HASH
64 :
65 : uchar
66 2604 : fd_executor_pubkey_is_bpf_loader( fd_pubkey_t const * pubkey ) {
67 2604 : fd_native_prog_info_t const null_function = {0};
68 2604 : return fd_native_program_fn_lookup_tbl_query( pubkey, &null_function )->is_bpf_loader;
69 2604 : }
70 :
71 : uchar
72 : fd_executor_program_is_active( fd_bank_t * bank,
73 3027 : fd_pubkey_t const * pubkey ) {
74 3027 : fd_native_prog_info_t const null_function = { .feature_enable_offset = ULONG_MAX };
75 3027 : ulong feature_offset = fd_native_program_fn_lookup_tbl_query( pubkey, &null_function )->feature_enable_offset;
76 :
77 3027 : return feature_offset==ULONG_MAX ||
78 3027 : FD_FEATURE_ACTIVE_BANK_OFFSET( bank, feature_offset );
79 3027 : }
80 :
81 : /* fd_executor_lookup_native_program returns the appropriate instruction
82 : processor for the given native program ID. Returns NULL if given ID
83 : is not a recognized native program.
84 :
85 : https://github.com/anza-xyz/agave/blob/v2.2.6/program-runtime/src/invoke_context.rs#L520-L544 */
86 :
87 : static int
88 : fd_executor_lookup_native_program( fd_acc_t const * meta,
89 : fd_bank_t * bank,
90 : fd_exec_instr_fn_t * native_prog_fn,
91 3027 : uchar * is_precompile ) {
92 : /* First lookup to see if the program key is a precompile */
93 3027 : *is_precompile = 0;
94 3027 : *native_prog_fn = fd_executor_lookup_native_precompile_program( (fd_pubkey_t*)meta->pubkey );
95 3027 : if( FD_UNLIKELY( *native_prog_fn!=NULL ) ) {
96 0 : *is_precompile = 1;
97 0 : return 0;
98 0 : }
99 :
100 : /* Native programs should be owned by the native loader... This will
101 : not be the case though once core programs are migrated to BPF. */
102 3027 : int is_native_program = !memcmp( meta->owner, fd_solana_native_loader_id.key, 32UL );
103 :
104 3027 : if( FD_UNLIKELY( !is_native_program ) ) {
105 2529 : if( FD_UNLIKELY( !fd_executor_pubkey_is_bpf_loader( (fd_pubkey_t*)meta->owner ) ) ) {
106 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
107 0 : }
108 2529 : }
109 :
110 3027 : fd_pubkey_t const * lookup_pubkey = is_native_program ? (fd_pubkey_t*)meta->pubkey : (fd_pubkey_t*)meta->owner;
111 :
112 : /* Migrated programs must be executed via the corresponding BPF
113 : loader(s), not natively. This check is performed at the transaction
114 : level, but we re-check to please the instruction level (and below)
115 : fuzzers. */
116 3027 : uchar has_migrated;
117 3027 : if( FD_UNLIKELY( fd_is_migrating_builtin_program( bank, lookup_pubkey, &has_migrated ) && has_migrated ) ) {
118 0 : *native_prog_fn = NULL;
119 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
120 0 : }
121 :
122 : /* We perform feature gate checks here to emulate the absence of
123 : a native program in Agave's ProgramCache when the program's feature
124 : gate is not activated.
125 : https://github.com/anza-xyz/agave/blob/v3.0.3/program-runtime/src/invoke_context.rs#L546-L549 */
126 :
127 3027 : if( FD_UNLIKELY( !fd_executor_program_is_active( bank, lookup_pubkey ) ) ) {
128 0 : *native_prog_fn = NULL;
129 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
130 0 : }
131 :
132 3027 : fd_native_prog_info_t const null_function = {0};
133 3027 : *native_prog_fn = fd_native_program_fn_lookup_tbl_query( lookup_pubkey, &null_function )->fn;
134 3027 : return 0;
135 3027 : }
136 :
137 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L117-L136 */
138 : static uchar
139 : fd_executor_rent_transition_allowed( fd_rent_state_t const * pre_rent_state,
140 2016 : fd_rent_state_t const * post_rent_state ) {
141 2016 : switch( post_rent_state->discriminant ) {
142 57 : case FD_RENT_STATE_UNINITIALIZED:
143 1983 : case FD_RENT_STATE_RENT_EXEMPT: {
144 1983 : return 1;
145 57 : }
146 33 : case FD_RENT_STATE_RENT_PAYING: {
147 33 : switch( pre_rent_state->discriminant ) {
148 9 : case FD_RENT_STATE_UNINITIALIZED:
149 9 : case FD_RENT_STATE_RENT_EXEMPT: {
150 9 : return 0;
151 9 : }
152 24 : case FD_RENT_STATE_RENT_PAYING: {
153 24 : return post_rent_state->data_size==pre_rent_state->data_size &&
154 24 : post_rent_state->lamports<=pre_rent_state->lamports;
155 9 : }
156 0 : default: {
157 0 : FD_LOG_CRIT(( "unexpected pre-rent state discriminant %u", pre_rent_state->discriminant ));
158 0 : }
159 33 : }
160 33 : }
161 0 : default: {
162 0 : FD_LOG_CRIT(( "unexpected post-rent state discriminant %u", post_rent_state->discriminant ));
163 0 : }
164 2016 : }
165 2016 : }
166 :
167 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L61-L77 */
168 : static int
169 : fd_executor_check_rent_state_with_account( fd_pubkey_t const * pubkey,
170 : fd_rent_state_t const * pre_rent_state,
171 852 : fd_rent_state_t const * post_rent_state ) {
172 852 : if( FD_UNLIKELY( memcmp( pubkey, fd_sysvar_incinerator_id.key, sizeof(fd_pubkey_t) ) &&
173 852 : !fd_executor_rent_transition_allowed( pre_rent_state, post_rent_state ) ) ) {
174 0 : return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_RENT;
175 0 : }
176 852 : return FD_RUNTIME_EXECUTE_SUCCESS;
177 852 : }
178 :
179 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L75-L95 */
180 : static fd_rent_state_t
181 : get_account_rent_state( ulong account_lamports,
182 : ulong account_size,
183 4032 : ulong min_balance ) {
184 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L85-L86 */
185 4032 : if( FD_UNLIKELY( account_lamports==0UL ) ) return (fd_rent_state_t){ .discriminant = FD_RENT_STATE_UNINITIALIZED };
186 :
187 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L87-L88 */
188 3918 : if( FD_LIKELY( account_lamports>=min_balance ) ) {
189 3834 : return (fd_rent_state_t){ .discriminant = FD_RENT_STATE_RENT_EXEMPT };
190 3834 : }
191 :
192 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L89-L93 */
193 84 : return (fd_rent_state_t){
194 84 : .discriminant = FD_RENT_STATE_RENT_PAYING,
195 84 : .lamports = account_lamports,
196 84 : .data_size = account_size,
197 84 : };
198 3918 : }
199 :
200 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L97-L112 */
201 : static fd_rent_state_t
202 : get_pre_exec_account_rent_state( ulong account_lamports,
203 : ulong account_size,
204 : ulong min_balance,
205 2019 : int disallow_rent_paying ) {
206 2019 : fd_rent_state_t rent_state = get_account_rent_state( account_lamports, account_size, min_balance );
207 2019 : if( rent_state.discriminant==FD_RENT_STATE_RENT_PAYING && disallow_rent_paying ) {
208 3 : rent_state.discriminant = FD_RENT_STATE_RENT_EXEMPT;
209 3 : }
210 2019 : return rent_state;
211 2019 : }
212 :
213 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L114-L142 */
214 : static fd_rent_state_t
215 : get_post_exec_account_rent_state( ulong account_lamports,
216 : ulong account_size,
217 : ulong min_balance,
218 : fd_rent_state_t const * pre_rent_state,
219 : ulong pre_exec_balance,
220 2019 : int relax_rent_exempt_criteria ) {
221 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L127-L129 */
222 2019 : if( !relax_rent_exempt_criteria ) {
223 2013 : return get_account_rent_state( account_lamports, account_size, min_balance );
224 2013 : }
225 :
226 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L132 */
227 6 : if( FD_UNLIKELY( account_lamports==0UL ) ) {
228 0 : return (fd_rent_state_t){ .discriminant = FD_RENT_STATE_UNINITIALIZED };
229 0 : }
230 :
231 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L133 */
232 6 : if( FD_LIKELY( account_lamports>=min_balance ) ) return (fd_rent_state_t){ .discriminant = FD_RENT_STATE_RENT_EXEMPT };
233 :
234 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L134-L136 */
235 6 : if( account_lamports>=pre_exec_balance && pre_rent_state->discriminant==FD_RENT_STATE_RENT_EXEMPT ) return (fd_rent_state_t){ .discriminant = FD_RENT_STATE_RENT_EXEMPT };
236 :
237 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L137-L140 */
238 3 : return (fd_rent_state_t){
239 3 : .discriminant = FD_RENT_STATE_RENT_PAYING,
240 3 : .lamports = account_lamports,
241 3 : .data_size = account_size,
242 3 : };
243 6 : }
244 :
245 : int
246 : fd_executor_check_static_account_rent_state_transition( ulong pre_exec_balance,
247 : ulong post_exec_balance,
248 : ulong data_size,
249 : fd_rent_t const * rent,
250 1167 : int relax_post_exec_min_balance_check ) {
251 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L155 */
252 1167 : ulong rent_min_balance = fd_rent_exempt_minimum_balance( rent, data_size );
253 :
254 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L156-L162 */
255 1167 : fd_rent_state_t pre_state = get_pre_exec_account_rent_state(
256 1167 : pre_exec_balance,
257 1167 : data_size,
258 1167 : rent_min_balance,
259 1167 : relax_post_exec_min_balance_check
260 1167 : );
261 :
262 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L163-L170 */
263 1167 : fd_rent_state_t post_state = get_post_exec_account_rent_state(
264 1167 : post_exec_balance,
265 1167 : data_size,
266 1167 : rent_min_balance,
267 1167 : &pre_state,
268 1167 : pre_exec_balance,
269 1167 : relax_post_exec_min_balance_check
270 1167 : );
271 :
272 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L172-L174 */
273 1167 : if( FD_UNLIKELY( !fd_executor_rent_transition_allowed( &pre_state, &post_state ) ) ) {
274 12 : return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_RENT;
275 12 : }
276 :
277 1155 : return FD_RUNTIME_EXECUTE_SUCCESS;
278 1167 : }
279 :
280 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/account_loader.rs#L351-L404 */
281 : static int
282 : fd_validate_fee_payer( fd_acc_t * acc,
283 : fd_rent_t const * rent,
284 : ulong fee,
285 762 : int relax_post_exec_min_balance_check ) {
286 :
287 : /* https://github.com/anza-xyz/agave/blob/v4.5.0-beta.0/svm/src/transaction_processor.rs#L849-L857 */
288 762 : if( FD_UNLIKELY( !acc->lamports ) ) return FD_RUNTIME_TXN_ERR_INVALID_ACCOUNT_FOR_FEE;
289 :
290 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/account_loader.rs#L368-L371 */
291 651 : int system_account_kind = fd_get_system_account_kind( acc );
292 651 : if( FD_UNLIKELY( system_account_kind==FD_SYSTEM_PROGRAM_NONCE_ACCOUNT_KIND_UNKNOWN ) ) {
293 0 : return FD_RUNTIME_TXN_ERR_INVALID_ACCOUNT_FOR_FEE;
294 0 : }
295 :
296 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/account_loader.rs#L372-L379 */
297 651 : ulong min_balance = 0UL;
298 651 : if( FD_UNLIKELY( system_account_kind==FD_SYSTEM_PROGRAM_NONCE_ACCOUNT_KIND_NONCE ) ) {
299 3 : min_balance = fd_rent_exempt_minimum_balance( rent, FD_SYSTEM_PROGRAM_NONCE_DLEN );
300 3 : }
301 :
302 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/account_loader.rs#L381-L388 */
303 651 : if( FD_UNLIKELY( min_balance>acc->lamports || fee>acc->lamports-min_balance ) ) {
304 0 : return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_FEE;
305 0 : }
306 :
307 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/account_loader.rs#L390 */
308 651 : ulong pre_balance = acc->lamports;
309 :
310 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/account_loader.rs#L391-L393 */
311 651 : if( FD_UNLIKELY( fd_account_meta_checked_sub_lamports( acc, fee )!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
312 0 : return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_FEE;
313 0 : }
314 :
315 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/account_loader.rs#L394 */
316 651 : ulong post_balance = acc->lamports;
317 :
318 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/account_loader.rs#L396-L403 */
319 651 : return fd_executor_check_static_account_rent_state_transition(
320 651 : pre_balance,
321 651 : post_balance,
322 651 : acc->data_len,
323 651 : rent,
324 651 : relax_post_exec_min_balance_check
325 651 : );
326 651 : }
327 :
328 : static int
329 : fd_executor_check_status_cache( fd_txncache_t * status_cache,
330 : fd_bank_t * bank,
331 : fd_txn_in_t const * txn_in,
332 765 : fd_txn_out_t * txn_out ) {
333 765 : if( FD_UNLIKELY( !status_cache ) ) {
334 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
335 0 : }
336 :
337 765 : if( FD_UNLIKELY( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) ) {
338 : /* In Agave, durable nonce transactions are inserted to the status
339 : cache the same as any others, but this is only to serve RPC
340 : requests, they do not need to be in there for correctness as the
341 : nonce mechanism itself prevents double spend. We skip this logic
342 : entirely to simplify and improve performance of the txn cache. */
343 21 : return FD_RUNTIME_EXECUTE_SUCCESS;
344 21 : }
345 :
346 : /* Compute the blake3 hash of the transaction message
347 : https://github.com/anza-xyz/agave/blob/v2.1.7/sdk/program/src/message/versions/mod.rs#L159-L167 */
348 744 : fd_blake3_t b3[1];
349 744 : fd_blake3_init( b3 );
350 744 : fd_blake3_append( b3, "solana-tx-message-v1", 20UL );
351 744 : fd_blake3_append( b3, ((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->message_off), fd_txn_msg_sz( TXN( txn_in->txn ), (ulong)txn_in->txn->payload_sz ) );
352 744 : fd_blake3_fini( b3, &txn_out->details.blake_txn_msg_hash );
353 :
354 744 : fd_hash_t * blockhash = (fd_hash_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->recent_blockhash_off);
355 744 : int found = fd_txncache_query( status_cache, bank->txncache_fork_id, blockhash->uc, txn_out->details.blake_txn_msg_hash.uc );
356 744 : if( FD_UNLIKELY( found ) ) return FD_RUNTIME_TXN_ERR_ALREADY_PROCESSED;
357 :
358 744 : if( FD_UNLIKELY( txn_in->bundle.is_bundle ) ) {
359 : /* It is possible for users to send transactions in a bundle with
360 : identical transaction message hashes. The message hashes are
361 : only added to the txncache when transactions are committed. So
362 : message hashes must be checked against the txncache AND previous
363 : transactions in the bundle. */
364 255 : for( ulong i=0UL; i<txn_in->bundle.prev_txn_cnt; i++ ) {
365 102 : fd_txn_out_t const * prev_txn_out = txn_in->bundle.prev_txn_outs[i];
366 102 : if( FD_UNLIKELY( !memcmp( &prev_txn_out->details.blake_txn_msg_hash, &txn_out->details.blake_txn_msg_hash, sizeof(fd_hash_t) ) ) ) {
367 3 : return FD_RUNTIME_TXN_ERR_ALREADY_PROCESSED;
368 3 : }
369 102 : }
370 156 : }
371 :
372 741 : return FD_RUNTIME_EXECUTE_SUCCESS;
373 744 : }
374 :
375 : /* https://github.com/anza-xyz/agave/blob/v2.0.9/runtime/src/bank.rs#L3239-L3251 */
376 : static inline ulong
377 816 : get_transaction_account_lock_limit( void ) {
378 816 : return 64UL;
379 816 : }
380 :
381 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank/check_transactions.rs#L61-L75 */
382 : int
383 : fd_executor_check_transactions( fd_runtime_t * runtime,
384 : fd_bank_t * bank,
385 : fd_txn_in_t const * txn_in,
386 774 : fd_txn_out_t * txn_out ) {
387 : /* Compute unit limits are sanitized earlier by the caller.
388 : https://github.com/anza-xyz/agave/blob/v4.4.0-beta.0/runtime/src/bank/check_transactions.rs#L121-L140 */
389 774 : int err = fd_check_transaction_age( bank, txn_in, txn_out );
390 774 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
391 9 : return err;
392 9 : }
393 :
394 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank/check_transactions.rs#L74 */
395 765 : err = fd_executor_check_status_cache( runtime->status_cache, bank, txn_in, txn_out );
396 765 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
397 3 : return err;
398 3 : }
399 :
400 762 : return FD_RUNTIME_EXECUTE_SUCCESS;
401 765 : }
402 :
403 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime-transaction/src/runtime_transaction/transaction_view.rs#L98-L107
404 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime-transaction/src/transaction_meta.rs#L156-L176 */
405 : int
406 : fd_executor_sanitize_txn_v1_config( fd_txn_in_t const * txn_in,
407 15 : fd_txn_out_t * txn_out ) {
408 15 : fd_compute_budget_details_t * details = &txn_out->details.compute_budget;
409 15 : fd_txn_t const * txn = TXN( txn_in->txn );
410 :
411 15 : ulong priority_fee, cu_limit, loaded, heap;
412 15 : uint config_mask = fd_uint_load_4( (uchar const *)txn_in->txn->payload + 4 );
413 15 : fd_txn_parse_v1_config( config_mask,
414 15 : (uchar const *)txn_in->txn->payload + txn->v1_txn_config_values_off,
415 15 : &priority_fee, &cu_limit, &loaded, &heap );
416 :
417 :
418 15 : details->is_v1 = 1;
419 15 : details->priority_fee_lamports = priority_fee;
420 15 : details->has_compute_units_limit_update = 1;
421 15 : details->compute_unit_limit = cu_limit;
422 15 : details->loaded_accounts_data_size_limit = fd_ulong_min( FD_VM_LOADED_ACCOUNTS_DATA_SIZE_LIMIT, loaded );
423 15 : details->has_loaded_accounts_data_size_limit_update = 0;
424 15 : details->heap_size = heap;
425 15 : details->has_requested_heap_size = 0;
426 :
427 15 : return FD_RUNTIME_EXECUTE_SUCCESS;
428 15 : }
429 :
430 : /* fd_executor_verify_transaction checks the transaction version and
431 : processes its compute budget or v1 configuration. */
432 : int
433 : fd_executor_verify_transaction( fd_bank_t const * bank,
434 : fd_txn_in_t const * txn_in,
435 801 : fd_txn_out_t * txn_out ) {
436 801 : int err = FD_RUNTIME_EXECUTE_SUCCESS;
437 :
438 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime/src/bank.rs#L5467-L5477 */
439 801 : if( FD_UNLIKELY( TXN( txn_in->txn )->transaction_version==FD_TXN_V1 &&
440 801 : !FD_FEATURE_ACTIVE_BANK( bank, enable_tx_v1 ) ) ) {
441 12 : return FD_RUNTIME_TXN_ERR_UNSUPPORTED_VERSION;
442 12 : }
443 :
444 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime-transaction/src/runtime_transaction/transaction_view.rs#L94-L112 */
445 789 : if( TXN( txn_in->txn )->transaction_version==FD_TXN_V1 ) {
446 15 : fd_compute_budget_count_instrs( bank, txn_in, &txn_out->details.compute_budget ); /* telemetry only, v1 sets the CU limit */
447 15 : err = fd_executor_sanitize_txn_v1_config( txn_in, txn_out );
448 774 : } else {
449 774 : err = fd_executor_compute_budget_program_execute_instructions( bank, txn_in, txn_out );
450 774 : }
451 789 : if( FD_UNLIKELY( err ) ) return err;
452 :
453 789 : return FD_RUNTIME_EXECUTE_SUCCESS;
454 789 : }
455 :
456 : /* load_transaction_account serializes the instructions sysvar and
457 : reports the account's contribution to the loaded account data size.
458 : Accounts are already loaded by fd_executor_setup_accounts_for_txn().
459 :
460 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L199-L228 */
461 : static int
462 : load_transaction_account( fd_bank_t * bank,
463 : fd_txn_in_t const * txn_in,
464 : fd_txn_out_t * txn_out,
465 : fd_pubkey_t const * pubkey,
466 : fd_acc_t * acc,
467 : uchar unknown_acc,
468 : ulong txn_idx,
469 2400 : ulong * out_loaded_sz ) {
470 :
471 : /* Handling the sysvar instructions account explicitly.
472 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L817-L824 */
473 2400 : if( FD_UNLIKELY( !memcmp( pubkey, fd_sysvar_instructions_id.key, sizeof(fd_pubkey_t) ) ) ) {
474 : /* The sysvar instructions account cannot be "loaded" since it's
475 : constructed by the SVM and modified within each transaction's
476 : instruction execution only, so it incurs a loaded size cost
477 : of 0. */
478 0 : int err = fd_sysvar_instructions_serialize_account( txn_in, txn_out, txn_idx );
479 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
480 0 : *out_loaded_sz = 0UL;
481 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
482 0 : }
483 :
484 2400 : if( FD_LIKELY( !unknown_acc ) ) {
485 : /* SIMD-0186 introduces a base account size of 64 bytes for all
486 : transaction counts that exist prior to the transaction's
487 : execution.
488 :
489 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L204-L208 */
490 2235 : ulong base_account_size = FD_FEATURE_ACTIVE_BANK( bank, formalize_loaded_transaction_data_size ) ? FD_TRANSACTION_ACCOUNT_BASE_SIZE : 0UL;
491 :
492 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L828-L835 */
493 2235 : *out_loaded_sz = fd_ulong_sat_add( base_account_size, acc->data_len );
494 2235 : return FD_RUNTIME_EXECUTE_SUCCESS;
495 2235 : }
496 :
497 : /* The rest of this function is a no-op for us since we already set up
498 : the transaction accounts for unknown accounts within
499 : `fd_executor_setup_accounts_for_txn()`. We also do not need to
500 : add a base cost to the loaded account size because the SIMD
501 : states that accounts that do not exist prior to the transaction's
502 : execution should not incur a loaded size cost.
503 : https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L566-L577 */
504 165 : *out_loaded_sz = 0UL;
505 165 : return FD_RUNTIME_EXECUTE_SUCCESS;
506 2400 : }
507 :
508 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L494-L515 */
509 : static int
510 : fd_increase_calculated_data_size( fd_txn_out_t * txn_out,
511 3762 : ulong data_size_delta ) {
512 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L500-L503 */
513 3762 : if( FD_UNLIKELY( data_size_delta>UINT_MAX ) ) {
514 0 : return FD_RUNTIME_TXN_ERR_MAX_LOADED_ACCOUNTS_DATA_SIZE_EXCEEDED;
515 0 : }
516 :
517 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L505-L507 */
518 3762 : txn_out->details.loaded_accounts_data_size = fd_ulong_sat_add( txn_out->details.loaded_accounts_data_size, data_size_delta );
519 :
520 3762 : if( FD_UNLIKELY( txn_out->details.loaded_accounts_data_size>txn_out->details.compute_budget.loaded_accounts_data_size_limit ) ) {
521 3 : return FD_RUNTIME_TXN_ERR_MAX_LOADED_ACCOUNTS_DATA_SIZE_EXCEEDED;
522 3 : }
523 :
524 3759 : return FD_RUNTIME_EXECUTE_SUCCESS;
525 3762 : }
526 :
527 : /* This function is represented as a closure in Agave.
528 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L578-L640 */
529 : static int
530 : fd_collect_loaded_account( fd_txn_out_t * txn_out,
531 : fd_acc_t const * acc,
532 : ulong loaded_acc_size,
533 : int programdata_size_floor,
534 : fd_pubkey_t * additional_loaded_account_keys,
535 3042 : ulong * additional_loaded_account_keys_cnt ) {
536 :
537 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L586-L590 */
538 3042 : int err = fd_increase_calculated_data_size( txn_out, loaded_acc_size );
539 3042 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
540 0 : return err;
541 0 : }
542 :
543 : /* The remainder of this function is a deep-nested set of if
544 : statements. I've inverted the logic to make it easier to read.
545 : The purpose of the following code is to ensure that loader v3
546 : programdata accounts are accounted for exactly once in the account
547 : loading logic.
548 :
549 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L611 */
550 3042 : if( FD_LIKELY( memcmp( acc->owner, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
551 2832 : return FD_RUNTIME_EXECUTE_SUCCESS;
552 2832 : }
553 :
554 : /* Try to read the program state
555 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L612-L634 */
556 210 : fd_bpf_state_t loader_state[1];
557 210 : err = fd_bpf_loader_program_get_state( acc, loader_state );
558 210 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
559 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
560 0 : }
561 :
562 : /* Make sure the account is a v3 program */
563 210 : if( loader_state->discriminant!=FD_BPF_STATE_PROGRAM ) {
564 105 : return FD_RUNTIME_EXECUTE_SUCCESS;
565 105 : }
566 :
567 : /* Iterate through the account keys and make sure the programdata
568 : account is not present so it doesn't get loaded twice.
569 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L617 */
570 306 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
571 228 : if( FD_UNLIKELY( !memcmp( &txn_out->accounts.keys[i], &loader_state->inner.program.programdata_address, sizeof(fd_pubkey_t) ) ) ) {
572 27 : return FD_RUNTIME_EXECUTE_SUCCESS;
573 27 : }
574 228 : }
575 :
576 : /* Check that the programdata account has not been already counted
577 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L618 */
578 78 : for( ushort i=0; i<*additional_loaded_account_keys_cnt; i++ ) {
579 0 : if( FD_UNLIKELY( !memcmp( &additional_loaded_account_keys[i], &loader_state->inner.program.programdata_address, sizeof(fd_pubkey_t) ) ) ) {
580 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
581 0 : }
582 0 : }
583 :
584 : /* Programdata liveness and size. Agave reads both off the current
585 : fork, which the parent-fork executable[] copy can lag, so prefer
586 : the probe's current-fork values when it reported them. Programdata
587 : deployed this slot has no executable[] copy at all. Its size is in
588 : the skipped list. */
589 78 : fd_acc_t const * programdata_ref = NULL;
590 78 : ushort pd_idx = USHORT_MAX;
591 78 : for( ushort i=0; i<txn_out->accounts.executable_cnt; i++ ) {
592 42 : fd_acc_t const * exe = txn_out->accounts.executable[ i ];
593 42 : if( !memcmp( exe->pubkey, &loader_state->inner.program.programdata_address, 32UL ) ) {
594 42 : programdata_ref = exe;
595 42 : pd_idx = i;
596 42 : break;
597 42 : }
598 42 : }
599 78 : int programdata_live;
600 78 : ulong programdata_sz;
601 78 : if( FD_LIKELY( programdata_ref ) ) {
602 42 : ulong cur = txn_out->accounts.executable_cur_len[ pd_idx ];
603 42 : if( cur!=ULONG_MAX ) {
604 24 : programdata_live = !!txn_out->accounts.executable_cur_lamports[ pd_idx ];
605 24 : programdata_sz = cur;
606 24 : } else {
607 18 : programdata_live = !!programdata_ref->lamports;
608 18 : programdata_sz = programdata_ref->data_len;
609 18 : }
610 42 : } else {
611 : /* Membership in the skipped list is the liveness answer: producers
612 : only record a PD that is live on the current fork. Track that
613 : with an explicit flag rather than inferring it from a nonzero
614 : length, since a live PD may legitimately have zero-length data
615 : (e.g. closed in an earlier slot, then credited back to
616 : rent-exemption), which Agave still charges the 64-byte base
617 : for. */
618 36 : programdata_live = 0;
619 36 : programdata_sz = 0UL;
620 36 : for( ushort i=0; i<txn_out->accounts.executable_skipped_cnt; i++ ) {
621 21 : if( !memcmp( txn_out->accounts.executable_skipped_key[ i ].uc, &loader_state->inner.program.programdata_address, 32UL ) ) {
622 21 : programdata_live = 1;
623 21 : programdata_sz = txn_out->accounts.executable_skipped_len[ i ];
624 21 : break;
625 21 : }
626 21 : }
627 36 : }
628 :
629 : /* Agave charges 64+len for a live PD and nothing for a dead or
630 : missing one. Under loaderv3_programdata_size_floor it charges
631 : 64+max(len,45) regardless, with a dead or missing PD counting as
632 : length 0. So a PD closed in an earlier slot charges the same
633 : whether or not the reader observes a plain transfer reviving it.
634 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L625-L630 */
635 78 : ulong programdata_size_delta;
636 78 : if( programdata_size_floor ) {
637 0 : programdata_size_delta = fd_ulong_sat_add( FD_TRANSACTION_ACCOUNT_BASE_SIZE, fd_ulong_max( programdata_live ? programdata_sz : 0UL, PROGRAMDATA_METADATA_SIZE ) );
638 78 : } else {
639 78 : programdata_size_delta = programdata_live ? fd_ulong_sat_add( FD_TRANSACTION_ACCOUNT_BASE_SIZE, programdata_sz ) : 0UL;
640 78 : }
641 78 : err = fd_increase_calculated_data_size( txn_out, programdata_size_delta );
642 78 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
643 3 : return err;
644 3 : }
645 :
646 : /* Add the programdata account to the list of loaded programdata accounts
647 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L631 */
648 75 : fd_memcpy(
649 75 : &additional_loaded_account_keys[(*additional_loaded_account_keys_cnt)++],
650 75 : &loader_state->inner.program.programdata_address,
651 75 : sizeof(fd_pubkey_t) );
652 :
653 75 : return FD_RUNTIME_EXECUTE_SUCCESS;
654 78 : }
655 :
656 : /* Simplified transaction loading logic for SIMD-0186 which does the
657 : following:
658 : - Calculates the loaded data size for each address lookup table
659 : - Calculates the loaded data size for each transaction account
660 : - Calculates the loaded data size for each v3 programdata account
661 : not directly referenced in the transaction accounts
662 : - Validates that each program invoked in a top-level instruction
663 : exists and is owned by either the native loader or a bpf loader
664 :
665 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L550-L689
666 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L518-L548 */
667 : int
668 : fd_executor_load_transaction_accounts( fd_bank_t * bank,
669 : fd_txn_in_t const * txn_in,
670 642 : fd_txn_out_t * txn_out ) {
671 : /* Programdata accounts that are loaded by this transaction.
672 : We keep track of these to ensure they are not counted twice.
673 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L559 */
674 642 : fd_pubkey_t additional_loaded_account_keys[ FD_TXN_ACCT_ADDR_MAX ];
675 642 : ulong additional_loaded_account_keys_cnt = 0UL;
676 642 : int programdata_size_floor = FD_FEATURE_ACTIVE_BANK( bank, loaderv3_programdata_size_floor );
677 :
678 : /* Charge a base fee for each address lookup table.
679 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L570-L576 */
680 642 : ulong aluts_size = fd_ulong_sat_mul( TXN( txn_in->txn )->addr_table_lookup_cnt,
681 642 : FD_ADDRESS_LOOKUP_TABLE_BASE_SIZE );
682 642 : int err = fd_increase_calculated_data_size( txn_out, aluts_size );
683 642 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
684 0 : return err;
685 0 : }
686 :
687 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L642-L660 */
688 3681 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
689 3042 : fd_acc_t * acc = txn_out->accounts.account[ i ];
690 :
691 3042 : uchar unknown_acc = !!( !fd_runtime_get_account_at_index( txn_in, txn_out, i, fd_runtime_account_check_exists ) ||
692 3042 : acc->lamports==0UL);
693 :
694 : /* Collect the fee payer account separately (since it was already)
695 : loaded during fee payer validation.
696 :
697 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L644-L648 */
698 3042 : if( FD_UNLIKELY( i==FD_FEE_PAYER_TXN_IDX ) ) {
699 642 : ulong loaded_acc_size = fd_ulong_sat_add( FD_TRANSACTION_ACCOUNT_BASE_SIZE,
700 642 : acc->data_len );
701 642 : int err = fd_collect_loaded_account(
702 642 : txn_out,
703 642 : acc,
704 642 : loaded_acc_size,
705 642 : programdata_size_floor,
706 642 : additional_loaded_account_keys,
707 642 : &additional_loaded_account_keys_cnt );
708 642 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
709 0 : return err;
710 0 : }
711 642 : continue;
712 642 : }
713 :
714 : /* Load and collect any remaining accounts
715 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L652-L659 */
716 2400 : ulong loaded_acc_size = 0UL;
717 2400 : int err = load_transaction_account( bank, txn_in, txn_out, &txn_out->accounts.keys[i], acc, unknown_acc, i, &loaded_acc_size );
718 2400 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
719 0 : return err;
720 0 : }
721 2400 : err = fd_collect_loaded_account(
722 2400 : txn_out,
723 2400 : acc,
724 2400 : loaded_acc_size,
725 2400 : programdata_size_floor,
726 2400 : additional_loaded_account_keys,
727 2400 : &additional_loaded_account_keys_cnt );
728 2400 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
729 3 : return err;
730 3 : }
731 2400 : }
732 :
733 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L662-L686 */
734 639 : ushort instr_cnt = TXN( txn_in->txn )->instr_cnt;
735 1185 : for( ushort i=0; i<instr_cnt; i++ ) {
736 564 : fd_txn_instr_t const * instr = &TXN( txn_in->txn )->instr[i];
737 :
738 : /* Mimicking `load_account()` here with 0-lamport check as well.
739 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L663-L666 */
740 564 : fd_acc_t * ref = fd_runtime_get_account_at_index(
741 564 : txn_in, txn_out, instr->program_id, fd_runtime_account_check_exists );
742 564 : if( FD_UNLIKELY( !ref ) ) return FD_RUNTIME_TXN_ERR_PROGRAM_ACCOUNT_NOT_FOUND;
743 :
744 546 : if( FD_UNLIKELY( !ref->lamports ) ) return FD_RUNTIME_TXN_ERR_PROGRAM_ACCOUNT_NOT_FOUND;
745 :
746 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L677-L681 */
747 546 : if( FD_UNLIKELY( memcmp( ref->owner, &fd_solana_native_loader_id, 32UL ) &&
748 546 : !fd_executor_pubkey_is_bpf_loader( (fd_pubkey_t*)ref->owner ) ) ) {
749 0 : return FD_RUNTIME_TXN_ERR_INVALID_PROGRAM_FOR_EXECUTION;
750 0 : }
751 546 : }
752 :
753 621 : return FD_RUNTIME_EXECUTE_SUCCESS;
754 639 : }
755 :
756 : /* https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118 */
757 : int
758 816 : fd_executor_validate_account_locks( fd_txn_out_t const * txn_out ) {
759 : /* Ensure the number of account keys does not exceed the transaction lock limit
760 : https://github.com/anza-xyz/agave/blob/v2.2.17/accounts-db/src/account_locks.rs#L121 */
761 816 : ulong tx_account_lock_limit = get_transaction_account_lock_limit();
762 816 : if( FD_UNLIKELY( txn_out->accounts.cnt>tx_account_lock_limit ) ) {
763 0 : return FD_RUNTIME_TXN_ERR_TOO_MANY_ACCOUNT_LOCKS;
764 0 : }
765 :
766 : /* Duplicate account check
767 : https://github.com/anza-xyz/agave/blob/v2.2.17/accounts-db/src/account_locks.rs#L123 */
768 4260 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
769 11661 : for( ushort j=(ushort)(i+1U); j<txn_out->accounts.cnt; j++ ) {
770 8217 : if( FD_UNLIKELY( !memcmp( &txn_out->accounts.keys[i], &txn_out->accounts.keys[j], sizeof(fd_pubkey_t) ) ) ) {
771 9 : return FD_RUNTIME_TXN_ERR_ACCOUNT_LOADED_TWICE;
772 9 : }
773 8217 : }
774 3453 : }
775 :
776 : /* https://github.com/anza-xyz/agave/blob/v2.2.17/accounts-db/src/account_locks.rs#L124-L126 */
777 807 : return FD_RUNTIME_EXECUTE_SUCCESS;
778 816 : }
779 :
780 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/compute-budget/src/compute_budget_limits.rs#L62-L70 */
781 : static ulong
782 777 : fd_get_prioritization_fee( fd_compute_budget_details_t const * compute_budget_details ) {
783 777 : if( compute_budget_details->is_v1 ) {
784 3 : return compute_budget_details->priority_fee_lamports;
785 3 : }
786 774 : uint128 micro_lamport_fee = fd_uint128_sat_mul( compute_budget_details->compute_unit_price, compute_budget_details->compute_unit_limit );
787 774 : uint128 fee = fd_uint128_sat_add( micro_lamport_fee, MICRO_LAMPORTS_PER_LAMPORT-1UL ) / MICRO_LAMPORTS_PER_LAMPORT;
788 774 : return fee>(uint128)ULONG_MAX ? ULONG_MAX : (ulong)fee;
789 777 : }
790 :
791 : void
792 : fd_executor_calculate_fee( fd_txn_out_t const * txn_out,
793 : fd_txn_t const * txn_descriptor,
794 : uchar const * payload,
795 : ulong * ret_execution_fee,
796 777 : ulong * ret_priority_fee ) {
797 : /* The execution fee is just the signature fee. The priority fee
798 : is calculated based on the compute budget details.
799 : https://github.com/anza-xyz/agave/blob/v3.0.3/fee/src/lib.rs#L65-L84 */
800 :
801 : // let signature_fee = Self::get_num_signatures_in_message(message) .saturating_mul(fee_structure.lamports_per_signature);
802 777 : ulong num_signatures = txn_descriptor->signature_cnt;
803 777 : fd_pubkey_t const * accts = fd_type_pun_const( fd_txn_get_acct_addrs( txn_descriptor, payload ) );
804 1380 : for (ushort i=0; i<txn_descriptor->instr_cnt; ++i ) {
805 603 : fd_txn_instr_t const * txn_instr = &txn_descriptor->instr[i];
806 603 : fd_pubkey_t const * program_id = &accts[txn_instr->program_id];
807 603 : if( !memcmp(program_id->uc, fd_solana_keccak_secp_256k_program_id.key, sizeof(fd_pubkey_t)) ||
808 603 : !memcmp(program_id->uc, fd_solana_ed25519_sig_verify_program_id.key, sizeof(fd_pubkey_t)) ||
809 603 : !memcmp(program_id->uc, fd_solana_secp256r1_program_id.key, sizeof(fd_pubkey_t)) ) {
810 0 : if( !txn_instr->data_sz ) {
811 0 : continue;
812 0 : }
813 0 : uchar const * data = payload + txn_instr->data_off;
814 0 : num_signatures = fd_ulong_sat_add(num_signatures, (ulong)(data[0]));
815 0 : }
816 603 : }
817 777 : *ret_execution_fee = FD_RUNTIME_FEE_STRUCTURE_LAMPORTS_PER_SIGNATURE * num_signatures;
818 777 : *ret_priority_fee = fd_get_prioritization_fee( &txn_out->details.compute_budget );
819 777 : }
820 :
821 : /* Save the fee payer's lamports after fee validation for rollback.
822 : The nonce rollback account is set up earlier when checking transaction age. */
823 :
824 : static void
825 651 : fd_executor_create_rollback_fee_payer_account( fd_txn_out_t * txn_out ) {
826 651 : fd_acc_t const * fee_payer = txn_out->accounts.account[ FD_FEE_PAYER_TXN_IDX ];
827 651 : txn_out->accounts.fee_payer_rollback_lamports = fee_payer->lamports;
828 651 : }
829 :
830 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L557-L634 */
831 : int
832 : fd_executor_validate_transaction_fee_payer( fd_bank_t * bank,
833 : fd_txn_in_t const * txn_in,
834 762 : fd_txn_out_t * txn_out ) {
835 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L574-L580 */
836 762 : fd_acc_t * ref = fd_runtime_get_account_at_index( txn_in, txn_out, FD_FEE_PAYER_TXN_IDX, fd_runtime_account_check_fee_payer_writable );
837 762 : if( FD_UNLIKELY( !ref ) ) return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
838 :
839 762 : fd_acc_t * fee_payer = txn_out->accounts.account[ FD_FEE_PAYER_TXN_IDX ];
840 762 : ulong total_fee = fd_ulong_sat_add( txn_out->details.execution_fee, txn_out->details.priority_fee );
841 :
842 : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L609-L616 */
843 :
844 762 : int err = fd_validate_fee_payer(
845 762 : fee_payer,
846 762 : &bank->f.rent,
847 762 : total_fee,
848 762 : FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check )
849 762 : );
850 762 : if( FD_UNLIKELY( err ) ) return err;
851 :
852 : /* Create the rollback fee payer account
853 : https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L620-L626 */
854 651 : fd_executor_create_rollback_fee_payer_account( txn_out );
855 :
856 651 : return FD_RUNTIME_EXECUTE_SUCCESS;
857 762 : }
858 :
859 : /* Resolves any address lookup tables referenced in the transaction and
860 : adds them to the transaction's account keys. Returns 0 on success or
861 : if the transaction is a legacy transaction, and an
862 : FD_RUNTIME_TXN_ERR_* on failure. */
863 : int
864 : fd_executor_setup_txn_alut_account_keys( fd_runtime_t * runtime,
865 : fd_bank_t * bank,
866 : fd_txn_in_t const * txn_in,
867 822 : fd_txn_out_t * txn_out ) {
868 822 : if( FD_LIKELY( TXN( txn_in->txn )->transaction_version==FD_TXN_V0 ) ) {
869 : /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/runtime/src/bank/address_lookup_table.rs#L44-L48 */
870 294 : fd_slot_hashes_t slot_hashes_view[1];
871 294 : if( FD_UNLIKELY( !fd_sysvar_cache_slot_hashes_view( runtime->sysvar_cache, slot_hashes_view ) ) ) {
872 0 : FD_LOG_DEBUG(( "fd_executor_setup_txn_alut_account_keys(): failed to get slot hashes" ));
873 0 : return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
874 0 : }
875 : /* Resolve the ALT against the parent. This is because the
876 : scheduler does not treat the ALT itself as a dependency, so a
877 : transaction resolving/reading a table can be scheduled
878 : concurrently with one extending/writing it. This would violate
879 : the accdb acquire contract if they both used the same fork_id.
880 : Similar to the case of implied loader v3 ProgramData reads, we
881 : acquire from the parent.
882 :
883 : Only the fork_id changes. The slot stays this bank's slot, which
884 : is always larger than the parent's last_extended_slot, and thus
885 : revealing every address as of the end of the parent slot. The
886 : slot hashes sysvar also stays this bank's, since the deactivation
887 : window is relative to the executing slot.
888 :
889 : Unlike with implied loader v3 ProgramData, no special handling is
890 : needed here for loaded account data size on the ALT. ALT size
891 : accounting is stateless. */
892 294 : FD_TEST( bank->parent_accdb_fork_id.val!=USHORT_MAX );
893 294 : fd_acct_addr_t * accts_alt = fd_type_pun( &txn_out->accounts.keys[txn_out->accounts.cnt] );
894 294 : int err = fd_runtime_load_txn_address_lookup_tables( TXN( txn_in->txn ),
895 294 : txn_in->txn->payload,
896 294 : runtime->accdb,
897 294 : bank->parent_accdb_fork_id,
898 294 : bank->f.slot,
899 294 : slot_hashes_view,
900 294 : accts_alt );
901 294 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
902 288 : txn_out->accounts.cnt += TXN( txn_in->txn )->addr_table_adtl_cnt;
903 :
904 288 : }
905 816 : return FD_RUNTIME_EXECUTE_SUCCESS;
906 822 : }
907 :
908 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L319-L357 */
909 : static inline int
910 : fd_txn_ctx_push( fd_runtime_t * runtime,
911 : fd_txn_in_t const * txn_in,
912 : fd_txn_out_t * txn_out,
913 3378 : fd_instr_info_t * instr ) {
914 : /* Earlier checks in the permalink are redundant since Agave maintains instr stack and trace accounts separately
915 : https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L327-L328 */
916 3378 : ulong starting_lamports_h = 0UL;
917 3378 : ulong starting_lamports_l = 0UL;
918 3378 : int err = fd_instr_info_sum_account_lamports( instr,
919 3378 : txn_out,
920 3378 : &starting_lamports_h,
921 3378 : &starting_lamports_l );
922 3378 : if( FD_UNLIKELY( err ) ) {
923 0 : return err;
924 0 : }
925 3378 : instr->starting_lamports_h = starting_lamports_h;
926 3378 : instr->starting_lamports_l = starting_lamports_l;
927 :
928 : /* Check that the caller's lamport sum has not changed.
929 : https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L329-L340 */
930 3378 : if( runtime->instr.stack_sz>0 ) {
931 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L330 */
932 2838 : fd_exec_instr_ctx_t const * caller_instruction_context = &runtime->instr.stack[ runtime->instr.stack_sz-1 ];
933 :
934 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L331-L332 */
935 2838 : ulong original_caller_lamport_sum_h = caller_instruction_context->instr->starting_lamports_h;
936 2838 : ulong original_caller_lamport_sum_l = caller_instruction_context->instr->starting_lamports_l;
937 :
938 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L333-L334 */
939 2838 : ulong current_caller_lamport_sum_h = 0UL;
940 2838 : ulong current_caller_lamport_sum_l = 0UL;
941 2838 : int err = fd_instr_info_sum_account_lamports( caller_instruction_context->instr,
942 2838 : caller_instruction_context->txn_out,
943 2838 : ¤t_caller_lamport_sum_h,
944 2838 : ¤t_caller_lamport_sum_l );
945 2838 : if( FD_UNLIKELY( err ) ) {
946 0 : return err;
947 0 : }
948 :
949 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L335-L339 */
950 2838 : if( FD_UNLIKELY( current_caller_lamport_sum_h!=original_caller_lamport_sum_h ||
951 2838 : current_caller_lamport_sum_l!=original_caller_lamport_sum_l ) ) {
952 303 : return FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR;
953 303 : }
954 2838 : }
955 :
956 : /* Note that we don't update the trace length here - since the caller
957 : allocates out of the trace array, they are also responsible for
958 : incrementing the trace length variable.
959 : https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L347-L351 */
960 3075 : if( FD_UNLIKELY( runtime->instr.trace_length>FD_MAX_INSTRUCTION_TRACE_LENGTH ) ) {
961 0 : return FD_EXECUTOR_INSTR_ERR_MAX_INSN_TRACE_LENS_EXCEEDED;
962 0 : }
963 :
964 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L352-L356 */
965 3075 : if( FD_UNLIKELY( runtime->instr.stack_sz>=FD_MAX_INSTRUCTION_STACK_DEPTH ) ) {
966 48 : return FD_EXECUTOR_INSTR_ERR_CALL_DEPTH;
967 48 : }
968 3027 : runtime->instr.stack_sz++;
969 :
970 : /* A beloved refactor moves sysvar instructions updating to the instruction level as of v2.2.12...
971 : https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L396-L407 */
972 3027 : ulong idx = fd_runtime_find_index_of_account( txn_out, &fd_sysvar_instructions_id );
973 3027 : if( FD_UNLIKELY( idx!=ULONG_MAX ) ) {
974 : /* https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L397-L400 */
975 0 : fd_acc_t * ref = fd_runtime_get_account_at_index( txn_in, txn_out, (ushort)idx, NULL );
976 0 : if( FD_UNLIKELY( !ref ) ) return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
977 :
978 : /* https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L401-L402 */
979 0 : if( FD_UNLIKELY( runtime->accounts.refcnt[ idx ]!=0UL ) ) {
980 0 : return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED;
981 0 : }
982 :
983 : /* https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L403-L406 */
984 0 : fd_sysvar_instructions_update_current_instr_idx( txn_out->accounts.account[ idx ], (ushort)runtime->instr.current_idx );
985 0 : }
986 :
987 3027 : return FD_EXECUTOR_INSTR_SUCCESS;
988 3027 : }
989 :
990 : /* fd_instr_stack_push checks for reentrancy violations and pushes the
991 : already-populated instr onto the instruction stack.
992 :
993 : https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L246-L290 */
994 : int
995 : fd_instr_stack_push( fd_runtime_t * runtime,
996 : fd_txn_in_t const * txn_in,
997 : fd_txn_out_t * txn_out,
998 3378 : fd_instr_info_t * instr ) {
999 : /* Agave keeps a vector of vectors called program_indices that stores the program_id index for each instruction within the transaction.
1000 : https://github.com/anza-xyz/agave/blob/v2.1.7/svm/src/account_loader.rs#L347-L402
1001 : If and only if the program_id is the native loader, then the vector for respective specific instruction (account_indices) is empty.
1002 : https://github.com/anza-xyz/agave/blob/v2.1.7/svm/src/account_loader.rs#L350-L358
1003 : While trying to push a new instruction onto the instruction stack, if the vector for the respective instruction is empty, Agave throws UnsupportedProgramId
1004 : https://github.com/anza-xyz/agave/blob/v2.1.7/program-runtime/src/invoke_context.rs#L253-L255
1005 : 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
1006 : */
1007 :
1008 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/program-runtime/src/invoke_context.rs#L250-L252 */
1009 3378 : fd_pubkey_t const * program_id_pubkey = NULL;
1010 3378 : int err = fd_runtime_get_key_of_account_at_index( txn_out,
1011 3378 : instr->program_id,
1012 3378 : &program_id_pubkey );
1013 3378 : if( FD_UNLIKELY( err ) ) {
1014 0 : return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
1015 0 : }
1016 :
1017 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L256-L286 */
1018 3378 : if( runtime->instr.stack_sz ) {
1019 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L261-L285 */
1020 2838 : uchar contains = 0;
1021 2838 : uchar is_last = 0;
1022 :
1023 : // Checks all previous instructions in the stack for reentrancy
1024 5868 : for( uchar level=0; level<runtime->instr.stack_sz; level++ ) {
1025 3030 : fd_exec_instr_ctx_t * instr_ctx = &runtime->instr.stack[level];
1026 : // Optimization: compare program id index instead of pubkey since account keys are unique
1027 3030 : if( instr->program_id == instr_ctx->instr->program_id ) {
1028 : // Reentrancy not allowed unless caller is calling itself
1029 2997 : if( level == runtime->instr.stack_sz-1 ) {
1030 2805 : is_last = 1;
1031 2805 : }
1032 2997 : contains = 1;
1033 2997 : }
1034 3030 : }
1035 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L282-L285 */
1036 2838 : if( FD_UNLIKELY( contains && !is_last ) ) {
1037 0 : return FD_EXECUTOR_INSTR_ERR_REENTRANCY_NOT_ALLOWED;
1038 0 : }
1039 2838 : }
1040 : /* Push the instruction onto the stack
1041 : https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L289 */
1042 3378 : return fd_txn_ctx_push( runtime, txn_in, txn_out, instr );
1043 3378 : }
1044 :
1045 : /* Pops an instruction from the instruction stack. Agave's implementation performs instruction balancing checks every time pop is called,
1046 : but error codes returned from `pop` are only used if the program's execution was successful. Therefore, we can optimize our code by only
1047 : checking for unbalanced instructions if the program execution was successful within fd_execute_instr.
1048 :
1049 : https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L293-L298 */
1050 : int
1051 : fd_instr_stack_pop( fd_runtime_t * runtime,
1052 : fd_txn_out_t * txn_out,
1053 3027 : fd_instr_info_t const * instr ) {
1054 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L362-L364 */
1055 3027 : if( FD_UNLIKELY( runtime->instr.stack_sz==0 ) ) {
1056 0 : return FD_EXECUTOR_INSTR_ERR_CALL_DEPTH;
1057 0 : }
1058 3027 : runtime->instr.stack_sz--;
1059 :
1060 : /* Verify all executable accounts have no outstanding refs
1061 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L367-L371 */
1062 8031 : for( ushort i=0; i<instr->acct_cnt; i++ ) {
1063 5004 : ushort idx_in_txn = instr->accounts[ i ].index_in_transaction;
1064 5004 : fd_acc_t const * acc = txn_out->accounts.account[ idx_in_txn ];
1065 5004 : ulong refcnt = runtime->accounts.refcnt[idx_in_txn];
1066 5004 : if( FD_UNLIKELY( acc->executable && refcnt ) ) return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_OUTSTANDING;
1067 5004 : }
1068 :
1069 : /* Verify lamports are balanced before and after instruction
1070 : https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L366-L380 */
1071 3027 : ulong ending_lamports_h = 0UL;
1072 3027 : ulong ending_lamports_l = 0UL;
1073 3027 : int err = fd_instr_info_sum_account_lamports( instr,
1074 3027 : txn_out,
1075 3027 : &ending_lamports_h,
1076 3027 : &ending_lamports_l );
1077 3027 : if( FD_UNLIKELY( err ) ) {
1078 0 : return err;
1079 0 : }
1080 3027 : if( FD_UNLIKELY( ending_lamports_l != instr->starting_lamports_l || ending_lamports_h != instr->starting_lamports_h ) ) {
1081 0 : return FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR;
1082 0 : }
1083 :
1084 3027 : return FD_EXECUTOR_INSTR_SUCCESS;
1085 3027 : }
1086 :
1087 : /* This function mimics Agave's `.and(self.pop())` functionality,
1088 : where we always pop the instruction stack no matter what the error code is.
1089 : https://github.com/anza-xyz/agave/blob/v2.2.12/program-runtime/src/invoke_context.rs#L480 */
1090 : int
1091 : fd_execute_instr_end( fd_exec_instr_ctx_t * instr_ctx,
1092 : fd_instr_info_t const * instr,
1093 3027 : int instr_exec_result ) {
1094 3027 : int stack_pop_err = fd_instr_stack_pop( instr_ctx->runtime, instr_ctx->txn_out, instr );
1095 :
1096 : /* Only report the stack pop error on success */
1097 3027 : if( FD_UNLIKELY( instr_exec_result==FD_EXECUTOR_INSTR_SUCCESS && stack_pop_err ) ) {
1098 0 : FD_TXN_PREPARE_ERR_OVERWRITE( instr_ctx->txn_out );
1099 0 : FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, stack_pop_err, instr_ctx->txn_out->err.exec_err_idx );
1100 0 : instr_exec_result = stack_pop_err;
1101 0 : }
1102 :
1103 3027 : return instr_exec_result;
1104 3027 : }
1105 :
1106 : int
1107 : fd_execute_instr( fd_runtime_t * runtime,
1108 : fd_bank_t * bank,
1109 : fd_txn_in_t const * txn_in,
1110 : fd_txn_out_t * txn_out,
1111 3378 : fd_instr_info_t * instr ) {
1112 3378 : if( FD_UNLIKELY( runtime->instr.stack_sz ) ) runtime->metrics.cpi_cum++;
1113 540 : else runtime->metrics.instr_cum++;
1114 :
1115 3378 : fd_sysvar_cache_t const * sysvar_cache = runtime->sysvar_cache;
1116 3378 : int instr_exec_result = fd_instr_stack_push( runtime, txn_in, txn_out, instr );
1117 3378 : if( FD_UNLIKELY( instr_exec_result ) ) {
1118 351 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1119 351 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1120 351 : return instr_exec_result;
1121 351 : }
1122 :
1123 : /* `process_executable_chain()`
1124 : https://github.com/anza-xyz/agave/blob/v2.2.12/program-runtime/src/invoke_context.rs#L512-L619 */
1125 3027 : fd_exec_instr_ctx_t * ctx = &runtime->instr.stack[ runtime->instr.stack_sz - 1 ];
1126 3027 : *ctx = (fd_exec_instr_ctx_t) {
1127 3027 : .instr = instr,
1128 3027 : .sysvar_cache = sysvar_cache,
1129 3027 : .runtime = runtime,
1130 3027 : .txn_in = txn_in,
1131 3027 : .txn_out = txn_out,
1132 3027 : .bank = bank,
1133 3027 : };
1134 :
1135 3027 : if( FD_UNLIKELY( !runtime->log.log_collector->disabled ) ) {
1136 2454 : fd_base58_encode_32( txn_out->accounts.keys[ instr->program_id ].uc, NULL, ctx->program_id_base58 );
1137 2454 : }
1138 :
1139 : /* Look up the native program. We check for precompiles within the lookup function as well.
1140 : https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/message_processor.rs#L88 */
1141 3027 : fd_exec_instr_fn_t native_prog_fn;
1142 3027 : uchar is_precompile;
1143 3027 : int err = fd_executor_lookup_native_program( txn_out->accounts.account[ instr->program_id ],
1144 3027 : bank,
1145 3027 : &native_prog_fn,
1146 3027 : &is_precompile );
1147 :
1148 3027 : if( FD_UNLIKELY( err ) ) {
1149 0 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1150 0 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, err, txn_out->err.exec_err_idx );
1151 0 : return fd_execute_instr_end( ctx, instr, err );
1152 0 : }
1153 :
1154 3027 : if( FD_LIKELY( native_prog_fn!=NULL ) ) {
1155 : /* If this branch is taken, we've found an entrypoint to execute.
1156 : Precompiles run without program log lines.
1157 : https://github.com/anza-xyz/agave/blob/v4.3.0/program-runtime/src/invoke_context.rs#L618-L631 */
1158 3027 : if( FD_LIKELY( !is_precompile ) ) fd_log_collector_program_invoke( ctx );
1159 :
1160 : /* Only reset the return data when executing a native builtin program (not a precompile)
1161 : https://github.com/anza-xyz/agave/blob/v2.1.6/program-runtime/src/invoke_context.rs#L536-L537 */
1162 3027 : if( FD_LIKELY( !is_precompile ) ) {
1163 3027 : txn_out->details.return_data.len = 0;
1164 3027 : txn_out->details.return_data.program_id = (fd_pubkey_t){0};
1165 3027 : }
1166 :
1167 : /* Execute the native program. */
1168 3027 : instr_exec_result = native_prog_fn( ctx );
1169 3027 : } else {
1170 : /* Unknown program. In this case specifically, we should not log the program id. */
1171 0 : instr_exec_result = FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
1172 0 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1173 0 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1174 0 : return fd_execute_instr_end( ctx, instr, instr_exec_result );
1175 0 : }
1176 :
1177 3027 : if( FD_LIKELY( instr_exec_result==FD_EXECUTOR_INSTR_SUCCESS ) ) {
1178 : /* Log success */
1179 2574 : if( FD_LIKELY( !is_precompile ) ) fd_log_collector_program_success( ctx );
1180 2574 : } else {
1181 : /* Log failure cases.
1182 : We assume that the correct type of error is stored in ctx.
1183 : Syscalls are expected to log when the error is generated, while
1184 : native programs will be logged here.
1185 : (This is because syscall errors often carry data with them.)
1186 :
1187 : TODO: This hackily handles cases where the exec_err and exec_err_kind
1188 : is not set yet. We should change our native programs to set
1189 : this in their respective processors. */
1190 453 : if( !txn_out->err.exec_err ) {
1191 360 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1192 360 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1193 360 : if( FD_LIKELY( !is_precompile ) ) fd_log_collector_program_failure( ctx );
1194 360 : } else {
1195 93 : if( FD_LIKELY( !is_precompile ) ) fd_log_collector_program_failure( ctx );
1196 93 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1197 93 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1198 93 : }
1199 453 : }
1200 :
1201 3027 : return fd_execute_instr_end( ctx, instr, instr_exec_result );
1202 3027 : }
1203 :
1204 : /* fd_executor_setup_accounts_for_txn_bundle is the bundle counterpart of
1205 : fd_executor_setup_accounts_for_txn. A bundle's accounts are acquired
1206 : once, up-front, for the whole bundle by
1207 : fd_runtime_prepare_bundle_accounts, so this never acquires and never
1208 : resets the shared pool (prepare owns it): it resolves the txn's keys
1209 : and binds every account (and programdata) to the pre-acquired pool.
1210 : Each bind must succeed; a miss means prepare did not acquire an
1211 : account this txn references, which is a bug. */
1212 :
1213 : void
1214 : fd_executor_setup_accounts_for_txn_bundle( fd_runtime_t * runtime,
1215 : fd_txn_in_t const * txn_in,
1216 171 : fd_txn_out_t * txn_out ) {
1217 : /* The key list was resolved once up-front by
1218 : fd_runtime_prepare_bundle_accounts. Bind every transaction account
1219 : to the shared pre-acquired pool. */
1220 1164 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
1221 993 : txn_out->accounts.is_writable[ i ] = (uchar)fd_runtime_account_is_writable_idx( txn_in, txn_out, i );
1222 993 : txn_out->accounts.account_acquired[ i ] = 0U;
1223 993 : runtime->accounts.refcnt[ i ] = 0UL;
1224 :
1225 993 : fd_acc_t * acc = txn_out->accounts.account[ i ];
1226 993 : FD_TEST( acc );
1227 :
1228 : /* If the prior bundle txn drained the account to zero lamports,
1229 : the next txn must observe the reclaimed account, matching the
1230 : tombstone reset the accdb applies on a fresh acquire: empty
1231 : data, non-executable, system owner.
1232 : https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L199-L228 */
1233 993 : if( FD_UNLIKELY( !acc->lamports ) ) {
1234 75 : acc->data_len = 0UL;
1235 75 : acc->executable = 0;
1236 75 : memset( acc->owner, 0, 32UL );
1237 75 : }
1238 :
1239 993 : txn_out->accounts.starting_lamports[ i ] = acc->lamports;
1240 993 : txn_out->accounts.starting_data_len[ i ] = acc->data_len;
1241 993 : memcpy( &txn_out->accounts.starting_owner[ i ], acc->owner, sizeof(fd_pubkey_t) );
1242 :
1243 : /* Iterate backwards through previous bundle txns to find the relevant
1244 : account. No duplicate accounts can be loaded in a bundle. */
1245 993 : int found = 0;
1246 1728 : for( ulong p=txn_in->bundle.prev_txn_cnt; p>0UL && !found; p-- ) {
1247 735 : fd_txn_out_t * prev_txn = txn_in->bundle.prev_txn_outs[ p-1UL ];
1248 4917 : for( ushort k=0; k<prev_txn->accounts.cnt; k++ ) {
1249 : /* Only match if the txn owns the reference to the account. */
1250 4668 : if( !prev_txn->accounts.account_acquired[ k ] ||
1251 4668 : !fd_pubkey_eq( &prev_txn->accounts.keys[ k ], &txn_out->accounts.keys[ i ] ) ) continue;
1252 486 : found = 1;
1253 :
1254 : /* If this txn writes the account, transfer ownership of the accdb
1255 : ref to it and carry forward the vote and stake cache update
1256 : flags. The owner commits the account if any bundle txn up to
1257 : it touched it. */
1258 486 : if( txn_out->accounts.is_writable[ i ] ) {
1259 129 : txn_out->accounts.stake_update[ i ] |= prev_txn->accounts.stake_update[ k ]; prev_txn->accounts.stake_update[ k ] = 0;
1260 129 : txn_out->accounts.vote_update [ i ] |= prev_txn->accounts.vote_update [ k ]; prev_txn->accounts.vote_update [ k ] = 0;
1261 129 : txn_out->accounts.touched [ i ] |= prev_txn->accounts.touched [ k ];
1262 129 : prev_txn->accounts.account_acquired[ k ] = 0U;
1263 129 : txn_out->accounts.account_acquired[ i ] = 1U;
1264 129 : }
1265 486 : break;
1266 4668 : }
1267 735 : }
1268 :
1269 993 : if( !found ) txn_out->accounts.account_acquired[ i ] = 1U;
1270 993 : }
1271 :
1272 171 : txn_out->accounts.is_setup = 1;
1273 171 : txn_out->accounts.nonce_idx_in_txn = ULONG_MAX;
1274 171 : }
1275 :
1276 : /* fd_executor_txn_will_fail returns 1 if account metadata shows the
1277 : transaction will fail fee payer validation or exceed its loaded
1278 : accounts data size limit. It does not load any account data.
1279 : *out_loaded_sz is set to a lower bound of the loaded accounts data
1280 : size excluding programdata, or 0 if the fee payer is invalid. */
1281 :
1282 : static int
1283 : fd_executor_txn_will_fail( fd_runtime_t * runtime,
1284 : fd_bank_t * bank,
1285 : fd_txn_in_t const * txn_in,
1286 : fd_txn_out_t const * txn_out,
1287 603 : ulong * out_loaded_sz ) {
1288 603 : fd_accdb_t * accdb = runtime->accdb;
1289 603 : fd_accdb_fork_id_t fork_id = bank->accdb_fork_id;
1290 603 : *out_loaded_sz = 0UL;
1291 :
1292 : /* First, validate the fee payer */
1293 603 : ulong fee_payer_lamp = fd_accdb_lamports( accdb, fork_id, txn_out->accounts.keys[ FD_FEE_PAYER_TXN_IDX ].uc );
1294 603 : ulong fee_payer_dlen = fd_accdb_data_len( accdb, fork_id, txn_out->accounts.keys[ FD_FEE_PAYER_TXN_IDX ].uc );
1295 603 : ulong min_balance = fee_payer_dlen ? fd_rent_exempt_minimum_balance( &bank->f.rent, FD_SYSTEM_PROGRAM_NONCE_DLEN ) : 0UL;
1296 603 : ulong fee = fd_ulong_sat_add( txn_out->details.execution_fee, txn_out->details.priority_fee );
1297 603 : if( (fee_payer_dlen && fee_payer_dlen!=FD_SYSTEM_PROGRAM_NONCE_DLEN) ||
1298 603 : fee_payer_lamp<fd_ulong_sat_add( min_balance, fee ) ||
1299 603 : fd_executor_check_static_account_rent_state_transition( fee_payer_lamp, fee_payer_lamp-fee, fee_payer_dlen, &bank->f.rent,
1300 495 : FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check ) ) ) {
1301 108 : return 1;
1302 108 : }
1303 :
1304 : /* Then, validate the loaded accounts data size limit */
1305 495 : ulong loaded_sz = fee_payer_dlen;
1306 2142 : for( ushort i=1; i<txn_out->accounts.cnt; i++ ) {
1307 1647 : if( FD_LIKELY( !fd_pubkey_eq( &txn_out->accounts.keys[ i ], &fd_sysvar_instructions_id ) ) ) {
1308 1647 : loaded_sz += fd_accdb_data_len( accdb, fork_id, txn_out->accounts.keys[ i ].uc );
1309 1647 : }
1310 1647 : }
1311 495 : *out_loaded_sz = loaded_sz;
1312 495 : return loaded_sz>txn_out->details.compute_budget.loaded_accounts_data_size_limit &&
1313 495 : !fd_sysvar_instructions_offsets_overflow( TXN( txn_in->txn ) );
1314 603 : }
1315 :
1316 : int
1317 : fd_executor_setup_accounts_for_txn( fd_runtime_t * runtime,
1318 : fd_bank_t * bank,
1319 : fd_txn_in_t const * txn_in,
1320 606 : fd_txn_out_t * txn_out ) {
1321 :
1322 606 : ulong acquire_idx[ MAX_TX_ACCOUNT_LOCKS ];
1323 606 : int acquire_writable[ MAX_TX_ACCOUNT_LOCKS ];
1324 606 : uchar const * acquire_pubkeys[ MAX_TX_ACCOUNT_LOCKS ];
1325 606 : ulong acquire_cnt = 0UL;
1326 :
1327 606 : runtime->accounts.account_cnt = 0UL;
1328 606 : runtime->accounts.executable_cnt = 0UL;
1329 :
1330 606 : txn_out->accounts.cnt = (uchar)TXN( txn_in->txn )->acct_addr_cnt;
1331 606 : fd_pubkey_t * tx_accs = (fd_pubkey_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->acct_addr_off);
1332 2943 : for( ulong i=0UL; i<TXN( txn_in->txn )->acct_addr_cnt; i++ ) txn_out->accounts.keys[ i ] = tx_accs[ i ];
1333 :
1334 606 : txn_out->accounts.executable_cnt = 0UL;
1335 :
1336 606 : int err = fd_executor_setup_txn_alut_account_keys( runtime, bank, txn_in, txn_out );
1337 606 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
1338 :
1339 : /* Validate account locks before acquiring; the accdb acquire
1340 : hard-asserts the lock count is within bounds. */
1341 603 : err = fd_executor_validate_account_locks( txn_out );
1342 603 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
1343 :
1344 : /* If the transaction will fail, only acquire the fee payer and the
1345 : potential nonce account (first account of the first instruction). */
1346 603 : fd_txn_t const * txn = TXN( txn_in->txn );
1347 603 : ulong loaded_sz;
1348 603 : int will_fail = fd_executor_txn_will_fail( runtime, bank, txn_in, txn_out, &loaded_sz );
1349 603 : ulong nonce_idx = (txn->instr_cnt && txn->instr[ 0 ].acct_cnt) ? fd_txn_get_instr_accts( &txn->instr[ 0 ], txn_in->txn->payload )[ 0 ] : 0UL;
1350 :
1351 : /* Resolve all transaction accounts and queue them for acquisition. */
1352 :
1353 2961 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
1354 2358 : txn_out->accounts.is_writable[ i ] = (uchar)fd_runtime_account_is_writable_idx( txn_in, txn_out, i );
1355 2358 : txn_out->accounts.account_acquired[ i ] = 0U;
1356 2358 : txn_out->accounts.account[ i ] = NULL;
1357 2358 : runtime->accounts.refcnt[ i ] = 0UL;
1358 2358 : if( FD_UNLIKELY( will_fail && i!=FD_FEE_PAYER_TXN_IDX && i!=nonce_idx ) ) continue;
1359 :
1360 2226 : FD_TEST( runtime->accounts.account_cnt+acquire_cnt<FD_PACK_MAX_TXN_PER_BUNDLE*MAX_TX_ACCOUNT_LOCKS );
1361 2226 : acquire_idx[ acquire_cnt ] = i;
1362 2226 : acquire_pubkeys[ acquire_cnt ] = txn_out->accounts.keys[ i ].uc;
1363 2226 : acquire_writable[ acquire_cnt ] = (int)txn_out->accounts.is_writable[ i ];
1364 2226 : acquire_cnt++;
1365 2226 : }
1366 :
1367 : /* The hilariously poorly designed account loader semantics require a
1368 : two phase acquire ... since the programdata accounts do not need to
1369 : be declared in the transaction account keys. We first have to
1370 : acquire all accounts expressly referenced in the transaction, and
1371 : overcommit the reservations by double the number of them that could
1372 : be executable. This is because acquire must grab all locks it
1373 : needs atomically, if we went back to grab more locks later it could
1374 : deadlock with two threads both holding half the locks each and
1375 : unable to acquire.
1376 :
1377 : So first, just acquire and atomically reserve double the locks,
1378 : then figure out which accounts are executable, keep those
1379 : reservations, and release the extras back to the pool in the second
1380 : phase. */
1381 :
1382 603 : if( FD_LIKELY( acquire_cnt ) ) {
1383 603 : fd_acc_t * acquire_base = &runtime->accounts.account[ runtime->accounts.account_cnt ];
1384 603 : fd_accdb_acquire_a( runtime->accdb, bank->accdb_fork_id, acquire_cnt, acquire_pubkeys, acquire_writable, acquire_base );
1385 :
1386 2829 : for( ulong i=0UL; i<acquire_cnt; i++ ) {
1387 2226 : ulong txn_idx = acquire_idx[ i ];
1388 2226 : txn_out->accounts.account[ txn_idx ] = &acquire_base[ i ];
1389 2226 : txn_out->accounts.account_acquired[ txn_idx ] = 1U;
1390 2226 : txn_out->accounts.starting_lamports[ txn_idx ] = acquire_base[ i ].prior_lamports;
1391 2226 : txn_out->accounts.starting_data_len[ txn_idx ] = acquire_base[ i ].prior_data_len;
1392 2226 : memcpy( &txn_out->accounts.starting_owner[ txn_idx ], acquire_base[ i ].owner, sizeof(fd_pubkey_t) );
1393 2226 : }
1394 603 : runtime->accounts.account_cnt += acquire_cnt;
1395 603 : }
1396 :
1397 603 : ushort executable_account_cnt = 0;
1398 603 : ushort executable_acquire_cnt = 0;
1399 603 : ushort executable_acquire_idx[ MAX_TX_ACCOUNT_LOCKS ];
1400 603 : fd_pubkey_t programdata_keys[ MAX_TX_ACCOUNT_LOCKS ];
1401 603 : uchar const * pubkeys[ MAX_TX_ACCOUNT_LOCKS ];
1402 2961 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
1403 2358 : fd_acc_t * acc = txn_out->accounts.account[ i ];
1404 2358 : if( FD_UNLIKELY( !acc || memcmp( acc->owner, fd_solana_bpf_loader_upgradeable_program_id.key, 32UL ) ) ) continue;
1405 159 : fd_bpf_state_t program_loader_state[1];
1406 159 : err = fd_bpf_loader_program_get_state( txn_out->accounts.account[ i ], program_loader_state );
1407 159 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) continue;
1408 159 : if( FD_UNLIKELY( program_loader_state->discriminant!=FD_BPF_STATE_PROGRAM ) ) continue;
1409 :
1410 69 : fd_pubkey_t const * programdata_key = &program_loader_state->inner.program.programdata_address;
1411 :
1412 : /* If the programdata account is already one of the transaction's
1413 : declared accounts (e.g. a loader Upgrade/Extend lists it as a
1414 : writable instruction account), do NOT re-acquire it here. A
1415 : second acquire of the same (pubkey, fork) while it is held
1416 : writable from acquire_a above violates the accdb acquire contract
1417 : (a writable acquire must not overlap any other acquire of the
1418 : same account on the same fork; see fd_accdb.h). It is also
1419 : redundant: fd_runtime_get_executable_account() always prefers the
1420 : declared account over runtime->accounts.executable[], so the
1421 : read-only copy would never be read. */
1422 69 : if( FD_UNLIKELY( fd_runtime_find_index_of_account( txn_out, programdata_key )!=ULONG_MAX ) ) continue;
1423 :
1424 51 : FD_TEST( bank->parent_accdb_fork_id.val!=USHORT_MAX );
1425 51 : if( FD_UNLIKELY( !fd_accdb_exists( runtime->accdb, bank->parent_accdb_fork_id, programdata_key->uc ) ) ) {
1426 18 : int skip_pd = 0;
1427 18 : ulong skip_len = 0UL;
1428 18 : ulong skip_lamports = 0UL;
1429 : /* Record only a PD that is live on the current fork. A dead one
1430 : (created and closed within this slot) must contribute nothing,
1431 : matching Agave, whose load_account returns None for a
1432 : zero-lamport account. */
1433 18 : if( fd_accdb_probe_pd_this_fork( runtime->accdb, bank->accdb_fork_id, programdata_key->uc, &skip_pd, &skip_len, &skip_lamports ) && skip_lamports ) {
1434 9 : ushort s = txn_out->accounts.executable_skipped_cnt++;
1435 9 : txn_out->accounts.executable_skipped_key[ s ] = *programdata_key;
1436 9 : txn_out->accounts.executable_skipped_len[ s ] = skip_len;
1437 9 : }
1438 18 : continue;
1439 18 : }
1440 :
1441 33 : executable_acquire_idx[ executable_acquire_cnt ] = executable_account_cnt;
1442 : /* Keep the derived programdata address in stable storage until
1443 : fd_accdb_acquire_b() consumes the pubkey array below. */
1444 33 : programdata_keys[ executable_acquire_cnt ] = *programdata_key;
1445 33 : pubkeys[ executable_acquire_cnt ] = programdata_keys[ executable_acquire_cnt ].uc;
1446 33 : executable_acquire_cnt++;
1447 33 : executable_account_cnt++;
1448 33 : }
1449 :
1450 : /* If the programdata pushes the transaction over its loaded accounts
1451 : data size limit, the transaction fails before executing. Record
1452 : the programdata sizes for that check instead of loading them. */
1453 603 : ulong pd_sz = 0UL;
1454 636 : for( ushort i=0; i<executable_acquire_cnt; i++ ) {
1455 33 : int dup = 0;
1456 33 : for( ushort j=0; j<i; j++ ) dup |= fd_pubkey_eq( &programdata_keys[ i ], &programdata_keys[ j ] );
1457 33 : if( !dup ) pd_sz += fd_accdb_data_len( runtime->accdb, bank->accdb_fork_id, pubkeys[ i ] );
1458 33 : }
1459 603 : if( FD_UNLIKELY( loaded_sz+pd_sz>txn_out->details.compute_budget.loaded_accounts_data_size_limit ) ) {
1460 15 : for( ushort i=0; i<executable_acquire_cnt; i++ ) {
1461 3 : if( !fd_accdb_lamports( runtime->accdb, bank->accdb_fork_id, pubkeys[ i ] ) ) continue;
1462 3 : ushort s = txn_out->accounts.executable_skipped_cnt++;
1463 3 : txn_out->accounts.executable_skipped_key[ s ] = programdata_keys[ i ];
1464 3 : txn_out->accounts.executable_skipped_len[ s ] = fd_accdb_data_len( runtime->accdb, bank->accdb_fork_id, pubkeys[ i ] );
1465 3 : }
1466 12 : executable_acquire_cnt = 0;
1467 12 : executable_account_cnt = 0;
1468 12 : }
1469 :
1470 : /* acquire_b must refund exactly what acquire_a reserved. acquire_a
1471 : ran over acquire_cnt pubkeys, so the reserved count is acquire_cnt
1472 : and not txn_out->accounts.cnt. */
1473 603 : FD_TEST( runtime->accounts.executable_cnt+executable_acquire_cnt<=FD_PACK_MAX_TXN_PER_BUNDLE*MAX_TX_ACCOUNT_LOCKS );
1474 603 : fd_acc_t * acquire_base = &runtime->accounts.executable[ runtime->accounts.executable_cnt ];
1475 603 : fd_accdb_acquire_b( runtime->accdb, bank->parent_accdb_fork_id, acquire_cnt, executable_acquire_cnt, pubkeys, acquire_base );
1476 603 : int acquired_from_parent = bank->parent_accdb_fork_id.val!=bank->accdb_fork_id.val;
1477 633 : for( ushort i=0; i<executable_acquire_cnt; i++ ) {
1478 30 : ushort exe_idx = executable_acquire_idx[ i ];
1479 30 : txn_out->accounts.executable[ exe_idx ] = &acquire_base[ i ];
1480 30 : txn_out->accounts.executable_from_parent[ exe_idx ] = acquired_from_parent;
1481 30 : int pd = 0;
1482 30 : ulong len = ULONG_MAX;
1483 30 : ulong lamports = 0UL;
1484 30 : fd_accdb_probe_pd_this_fork( runtime->accdb, bank->accdb_fork_id, pubkeys[ i ], &pd, &len, &lamports );
1485 30 : txn_out->accounts.executable_pd_write[ exe_idx ] = pd;
1486 30 : txn_out->accounts.executable_cur_len[ exe_idx ] = len;
1487 30 : txn_out->accounts.executable_cur_lamports[ exe_idx ] = lamports;
1488 30 : }
1489 603 : runtime->accounts.executable_cnt += executable_acquire_cnt;
1490 :
1491 :
1492 603 : txn_out->accounts.is_setup = 1;
1493 603 : txn_out->accounts.nonce_idx_in_txn = ULONG_MAX;
1494 603 : txn_out->accounts.executable_cnt = executable_account_cnt;
1495 603 : return FD_RUNTIME_EXECUTE_SUCCESS;
1496 603 : }
1497 :
1498 : int
1499 : fd_executor_txn_verify( fd_txn_p_t * txn_p,
1500 : fd_sha512_t * shas[ FD_TXN_SIG_MAX ],
1501 3 : fd_ed25519_cache_t * cache ) {
1502 3 : fd_txn_t * txn = TXN( txn_p );
1503 :
1504 3 : uchar * signatures = txn_p->payload + txn->signature_off;
1505 3 : uchar * pubkeys = txn_p->payload + txn->acct_addr_off;
1506 3 : uchar * msg = txn_p->payload + txn->message_off;
1507 3 : ulong msg_sz = fd_txn_msg_sz( txn, txn_p->payload_sz );
1508 :
1509 3 : int res = cache ? fd_ed25519_verify_batch_single_msg_cached( msg, msg_sz, signatures, pubkeys, shas, txn->signature_cnt, cache ) :
1510 3 : fd_ed25519_verify_batch_single_msg ( msg, msg_sz, signatures, pubkeys, shas, txn->signature_cnt );
1511 3 : if( FD_UNLIKELY( res!=FD_ED25519_SUCCESS ) ) return FD_RUNTIME_TXN_ERR_SIGNATURE_FAILURE;
1512 :
1513 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
1514 3 : }
1515 :
1516 : static int
1517 : fd_executor_txn_check( fd_bank_t * bank,
1518 375 : fd_txn_out_t * txn_out ) {
1519 375 : int err = 0;
1520 375 : ulong starting_lamports_l = 0UL;
1521 375 : ulong starting_lamports_h = 0UL;
1522 375 : ulong ending_lamports_l = 0UL;
1523 375 : ulong ending_lamports_h = 0UL;
1524 :
1525 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L63 */
1526 2367 : for( ulong i=0UL; i<txn_out->accounts.cnt; i++ ) {
1527 1992 : fd_acc_t * acc = txn_out->accounts.account[ i ];
1528 1992 : if( FD_UNLIKELY( !txn_out->accounts.is_writable[ i ] ) ) continue;
1529 :
1530 : /* The fee payer's starting balance is its post-fee-deduction
1531 : balance. */
1532 852 : ulong starting_lamports = i!=FD_FEE_PAYER_TXN_IDX ? txn_out->accounts.starting_lamports[ i ]
1533 852 : : txn_out->accounts.fee_payer_rollback_lamports;
1534 :
1535 : /* Tips for bundles are collected in the bank: a user submitting a
1536 : bundle must include a instruction that transfers lamports to
1537 : a specific tip account. Tips accumulated through the slot.
1538 :
1539 : The delta must be measured against this txn's starting balance,
1540 : not acc->prior_lamports. For a tip account reused across bundle
1541 : txns, prior_lamports stays anchored to the on-chain pre-image, so
1542 : using it here would re-count the credit from earlier bundle txns
1543 : and inflate bank->f.tips. starting_lamports[] holds the carried-
1544 : forward balance (== prior_lamports for freshly acquired accounts). */
1545 852 : if( FD_UNLIKELY( fd_pack_tip_is_tip_account( fd_type_pun_const( txn_out->accounts.keys[ i ].uc ) ) ) ) {
1546 0 : txn_out->details.tips += fd_ulong_sat_sub( acc->lamports, starting_lamports );
1547 0 : }
1548 :
1549 852 : fd_uwide_inc( &ending_lamports_h, &ending_lamports_l, ending_lamports_h, ending_lamports_l, acc->lamports );
1550 852 : fd_uwide_inc( &starting_lamports_h, &starting_lamports_l, starting_lamports_h, starting_lamports_l, starting_lamports );
1551 :
1552 :
1553 : /* Get pre-exec rent states
1554 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/transaction_processor.rs#L1012-L1019 */
1555 852 : fd_rent_state_t pre_state = get_pre_exec_account_rent_state(
1556 852 : starting_lamports,
1557 852 : txn_out->accounts.starting_data_len[ i ],
1558 852 : fd_rent_exempt_minimum_balance( &bank->f.rent, txn_out->accounts.starting_data_len[ i ] ),
1559 852 : FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check )
1560 852 : );
1561 :
1562 : /* Post-exec rent states
1563 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/transaction_processor.rs#L1065-L1079 */
1564 852 : int relax_rent_exempt_criteria = FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check ) &&
1565 852 : txn_out->accounts.starting_data_len[ i ]>=acc->data_len &&
1566 852 : pre_state.discriminant==FD_RENT_STATE_RENT_EXEMPT &&
1567 852 : !memcmp( &txn_out->accounts.starting_owner[ i ], acc->owner, sizeof(fd_pubkey_t) );
1568 852 : fd_rent_state_t post_state = get_post_exec_account_rent_state(
1569 852 : acc->lamports,
1570 852 : acc->data_len,
1571 852 : fd_rent_exempt_minimum_balance( &bank->f.rent, acc->data_len ),
1572 852 : &pre_state,
1573 852 : starting_lamports,
1574 852 : relax_rent_exempt_criteria
1575 852 : );
1576 :
1577 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/transaction_account_state_info.rs#L105-L125 */
1578 852 : err = fd_executor_check_rent_state_with_account( &txn_out->accounts.keys[ i ], &pre_state, &post_state );
1579 852 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
1580 :
1581 852 : if ( !memcmp( acc->owner, &fd_solana_stake_program_id, sizeof(fd_pubkey_t) ) ) txn_out->accounts.stake_update[ i ] = 1;
1582 843 : else if( !memcmp( acc->owner, &fd_solana_vote_program_id, sizeof(fd_pubkey_t) ) ) txn_out->accounts.vote_update[ i ] = 1;
1583 852 : }
1584 :
1585 : /* The fee payer (account index 0) is debited during loading, outside
1586 : the VM, so it carries no touch flag but must still be written back.
1587 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/transaction_processor.rs#L1116-L1120 */
1588 375 : txn_out->accounts.touched[ 0 ] = 1;
1589 :
1590 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/transaction_processor.rs#L1126-L1132 */
1591 375 : if( FD_UNLIKELY( ending_lamports_l!=starting_lamports_l || ending_lamports_h!=starting_lamports_h ) ) {
1592 0 : return FD_RUNTIME_TXN_ERR_UNBALANCED_TRANSACTION;
1593 0 : }
1594 :
1595 375 : return FD_RUNTIME_EXECUTE_SUCCESS;
1596 375 : }
1597 :
1598 :
1599 : int
1600 : fd_execute_txn( fd_runtime_t * runtime,
1601 : fd_bank_t * bank,
1602 : fd_txn_in_t const * txn_in,
1603 621 : fd_txn_out_t * txn_out ) {
1604 :
1605 621 : bool dump_insn = runtime->log.dump_proto_ctx &&
1606 621 : bank->f.slot>=runtime->log.dump_proto_ctx->dump_proto_start_slot &&
1607 621 : runtime->log.dump_proto_ctx->dump_instr_to_pb;
1608 :
1609 621 : if( FD_UNLIKELY( runtime->log.log_collector ) ) fd_log_collector_init( runtime->log.log_collector, runtime->log.enable_log_collector );
1610 :
1611 621 : fd_txn_t const * txn = TXN( txn_in->txn );
1612 915 : for( ushort i=0; i<txn->instr_cnt; i++ ) {
1613 : /* Set up the instr info for the current instruction */
1614 540 : fd_instr_info_t * instr_info = &runtime->instr.trace[ runtime->instr.trace_length++ ];
1615 540 : fd_instr_info_init_from_txn_instr( instr_info, txn_in, txn_out, &txn->instr[ i ] );
1616 :
1617 540 : if( FD_UNLIKELY( dump_insn ) ) {
1618 : // Capture the input and convert it into a Protobuf message
1619 0 : fd_dump_instr_to_protobuf( runtime, bank, txn_in, txn_out, instr_info, i );
1620 0 : }
1621 :
1622 : /* Update the current executing instruction index */
1623 540 : runtime->instr.current_idx = i;
1624 :
1625 : /* Execute the current instruction */
1626 540 : int instr_exec_result = fd_execute_instr( runtime, bank, txn_in, txn_out, instr_info );
1627 540 : if( FD_UNLIKELY( instr_exec_result!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1628 246 : if( FD_UNLIKELY( txn_out->err.exec_err_idx==UINT_MAX ) ) txn_out->err.exec_err_idx = i;
1629 246 : return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
1630 246 : }
1631 540 : }
1632 :
1633 : /* TODO: This function needs to be split out of fd_execute_txn and be placed
1634 : into the replay tile once it is implemented. */
1635 375 : return fd_executor_txn_check( bank, txn_out );
1636 621 : }
1637 :
1638 : int
1639 : fd_executor_consume_cus( fd_txn_out_t * txn_out,
1640 2970 : ulong cus ) {
1641 2970 : ulong new_cus = txn_out->details.compute_budget.compute_meter - cus;
1642 2970 : int underflow = (txn_out->details.compute_budget.compute_meter < cus);
1643 2970 : if( FD_UNLIKELY( underflow ) ) {
1644 0 : txn_out->details.compute_budget.compute_meter = 0UL;
1645 0 : return FD_EXECUTOR_INSTR_ERR_COMPUTE_BUDGET_EXCEEDED;
1646 0 : }
1647 2970 : txn_out->details.compute_budget.compute_meter = new_cus;
1648 2970 : return FD_EXECUTOR_INSTR_SUCCESS;
1649 2970 : }
1650 :
1651 : /* fd_executor_instr_strerror() returns the error message corresponding to err,
1652 : intended to be logged by log_collector, or an empty string if the error code
1653 : should be omitted in logs for whatever reason. Omitted examples are success,
1654 : custom error. See also fd_log_collector_program_failure(). */
1655 : FD_FN_CONST char const *
1656 144 : fd_executor_instr_strerror( int err ) {
1657 :
1658 144 : switch( err ) {
1659 0 : case FD_EXECUTOR_INSTR_SUCCESS : return ""; // not used
1660 0 : case FD_EXECUTOR_INSTR_ERR_GENERIC_ERR : return "generic instruction error";
1661 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_ARG : return "invalid program argument";
1662 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA : return "invalid instruction data";
1663 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA : return "invalid account data for instruction";
1664 0 : case FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL : return "account data too small for instruction";
1665 0 : case FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS : return "insufficient funds for instruction";
1666 0 : case FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID : return "incorrect program id for instruction";
1667 0 : case FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE : return "missing required signature for instruction";
1668 0 : case FD_EXECUTOR_INSTR_ERR_ACC_ALREADY_INITIALIZED : return "instruction requires an uninitialized account";
1669 0 : case FD_EXECUTOR_INSTR_ERR_UNINITIALIZED_ACCOUNT : return "instruction requires an initialized account";
1670 0 : case FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR : return "sum of account balances before and after instruction do not match";
1671 0 : case FD_EXECUTOR_INSTR_ERR_MODIFIED_PROGRAM_ID : return "instruction illegally modified the program id of an account";
1672 0 : case FD_EXECUTOR_INSTR_ERR_EXTERNAL_ACCOUNT_LAMPORT_SPEND : return "instruction spent from the balance of an account it does not own";
1673 0 : case FD_EXECUTOR_INSTR_ERR_EXTERNAL_DATA_MODIFIED : return "instruction modified data of an account it does not own";
1674 0 : case FD_EXECUTOR_INSTR_ERR_READONLY_LAMPORT_CHANGE : return "instruction changed the balance of a read-only account";
1675 48 : case FD_EXECUTOR_INSTR_ERR_READONLY_DATA_MODIFIED : return "instruction modified data of a read-only account";
1676 0 : case FD_EXECUTOR_INSTR_ERR_EXECUTABLE_MODIFIED : return "instruction changed executable bit of an account";
1677 0 : case FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS : return "insufficient account keys for instruction";
1678 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";
1679 0 : case FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED : return "instruction tries to borrow reference for an account which is already borrowed";
1680 0 : case FD_EXECUTOR_INSTR_ERR_ACC_BORROW_OUTSTANDING : return "instruction left account with an outstanding borrowed reference";
1681 0 : case FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR : return ""; // custom handling via txn_ctx->err.custom_err
1682 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_ERR : return "program returned invalid error code";
1683 0 : case FD_EXECUTOR_INSTR_ERR_EXECUTABLE_ACCOUNT_NOT_RENT_EXEMPT : return "executable accounts must be rent exempt";
1684 0 : case FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID : return "Unsupported program id";
1685 0 : case FD_EXECUTOR_INSTR_ERR_CALL_DEPTH : return "Cross-program invocation call depth too deep";
1686 0 : case FD_EXECUTOR_INSTR_ERR_MISSING_ACC : return "An account required by the instruction is missing";
1687 0 : case FD_EXECUTOR_INSTR_ERR_REENTRANCY_NOT_ALLOWED : return "Cross-program invocation reentrancy not allowed for this instruction";
1688 0 : case FD_EXECUTOR_INSTR_ERR_MAX_SEED_LENGTH_EXCEEDED : return "Length of the seed is too long for address generation";
1689 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_SEEDS : return "Provided seeds do not result in a valid address";
1690 96 : case FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC : return "Failed to reallocate account data";
1691 0 : case FD_EXECUTOR_INSTR_ERR_COMPUTE_BUDGET_EXCEEDED : return "Computational budget exceeded";
1692 0 : case FD_EXECUTOR_INSTR_ERR_PRIVILEGE_ESCALATION : return "Cross-program invocation with unauthorized signer or writable account";
1693 0 : case FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE : return "Failed to create program execution environment";
1694 0 : case FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPLETE : return "Program failed to complete";
1695 0 : case FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE : return "Account is immutable";
1696 0 : case FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY : return "Incorrect authority provided";
1697 0 : case FD_EXECUTOR_INSTR_ERR_BORSH_IO_ERROR : return "Failed to serialize or deserialize account data"; // truncated
1698 0 : case FD_EXECUTOR_INSTR_ERR_ACC_NOT_RENT_EXEMPT : return "An account does not have enough lamports to be rent-exempt";
1699 0 : case FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER : return "Invalid account owner";
1700 0 : case FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW : return "Program arithmetic overflowed";
1701 0 : case FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR : return "Unsupported sysvar";
1702 0 : case FD_EXECUTOR_INSTR_ERR_ILLEGAL_OWNER : return "Provided owner is not allowed";
1703 0 : case FD_EXECUTOR_INSTR_ERR_MAX_ACCS_DATA_ALLOCS_EXCEEDED : return "Accounts data allocations exceeded the maximum allowed per transaction";
1704 0 : case FD_EXECUTOR_INSTR_ERR_MAX_ACCS_EXCEEDED : return "Max accounts exceeded";
1705 0 : case FD_EXECUTOR_INSTR_ERR_MAX_INSN_TRACE_LENS_EXCEEDED : return "Max instruction trace length exceeded";
1706 0 : case FD_EXECUTOR_INSTR_ERR_BUILTINS_MUST_CONSUME_CUS : return "Builtin programs must consume compute units";
1707 0 : default: break;
1708 144 : }
1709 :
1710 0 : return "";
1711 144 : }
|