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