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 126 : #define FD_RENT_STATE_UNINITIALIZED (0U)
33 2484 : #define FD_RENT_STATE_RENT_PAYING (1U)
34 3990 : #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 8298 : #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 8298 : #define MAP_PERFECT_KEYS_EQUAL(k1,k2) (!memcmp( (k1), (k2), 32UL ))
45 :
46 8298 : #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 8298 : #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 2598 : fd_executor_pubkey_is_bpf_loader( fd_pubkey_t const * pubkey ) {
67 2598 : fd_native_prog_info_t const null_function = {0};
68 2598 : return fd_native_program_fn_lookup_tbl_query( pubkey, &null_function )->is_bpf_loader;
69 2598 : }
70 :
71 : uchar
72 : fd_executor_program_is_active( fd_bank_t * bank,
73 2850 : fd_pubkey_t const * pubkey ) {
74 2850 : fd_native_prog_info_t const null_function = { .feature_enable_offset = ULONG_MAX };
75 2850 : ulong feature_offset = fd_native_program_fn_lookup_tbl_query( pubkey, &null_function )->feature_enable_offset;
76 :
77 2850 : return feature_offset==ULONG_MAX ||
78 2850 : FD_FEATURE_ACTIVE_BANK_OFFSET( bank, feature_offset );
79 2850 : }
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 2850 : uchar * is_precompile ) {
92 : /* First lookup to see if the program key is a precompile */
93 2850 : *is_precompile = 0;
94 2850 : *native_prog_fn = fd_executor_lookup_native_precompile_program( (fd_pubkey_t*)meta->pubkey );
95 2850 : 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 2850 : int is_native_program = !memcmp( meta->owner, fd_solana_native_loader_id.key, 32UL );
103 :
104 2850 : if( FD_UNLIKELY( !is_native_program ) ) {
105 2526 : 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 2526 : }
109 :
110 2850 : 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 2850 : uchar has_migrated;
117 2850 : 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 2850 : 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 2850 : fd_native_prog_info_t const null_function = {0};
133 2850 : *native_prog_fn = fd_native_program_fn_lookup_tbl_query( lookup_pubkey, &null_function )->fn;
134 2850 : return 0;
135 2850 : }
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 1167 : fd_rent_state_t const * post_rent_state ) {
141 1167 : switch( post_rent_state->discriminant ) {
142 36 : case FD_RENT_STATE_UNINITIALIZED:
143 1134 : case FD_RENT_STATE_RENT_EXEMPT: {
144 1134 : return 1;
145 36 : }
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 1167 : }
165 1167 : }
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 666 : fd_rent_state_t const * post_rent_state ) {
172 666 : if( FD_UNLIKELY( memcmp( pubkey, fd_sysvar_incinerator_id.key, sizeof(fd_pubkey_t) ) &&
173 666 : !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 666 : return FD_RUNTIME_EXECUTE_SUCCESS;
177 666 : }
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 2334 : ulong min_balance ) {
184 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L85-L86 */
185 2334 : 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 2253 : if( FD_LIKELY( account_lamports>=min_balance ) ) {
189 2169 : return (fd_rent_state_t){ .discriminant = FD_RENT_STATE_RENT_EXEMPT };
190 2169 : }
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 2253 : }
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 1170 : int disallow_rent_paying ) {
206 1170 : fd_rent_state_t rent_state = get_account_rent_state( account_lamports, account_size, min_balance );
207 1170 : 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 1170 : return rent_state;
211 1170 : }
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 1170 : 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 1170 : if( !relax_rent_exempt_criteria ) {
223 1164 : return get_account_rent_state( account_lamports, account_size, min_balance );
224 1164 : }
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 504 : 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 504 : 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 504 : fd_rent_state_t pre_state = get_pre_exec_account_rent_state(
256 504 : pre_exec_balance,
257 504 : data_size,
258 504 : rent_min_balance,
259 504 : relax_post_exec_min_balance_check
260 504 : );
261 :
262 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L163-L170 */
263 504 : fd_rent_state_t post_state = get_post_exec_account_rent_state(
264 504 : post_exec_balance,
265 504 : data_size,
266 504 : rent_min_balance,
267 504 : &pre_state,
268 504 : pre_exec_balance,
269 504 : relax_post_exec_min_balance_check
270 504 : );
271 :
272 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/rent_calculator.rs#L172-L174 */
273 504 : 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 492 : return FD_RUNTIME_EXECUTE_SUCCESS;
278 504 : }
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 594 : 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 594 : 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 483 : int system_account_kind = fd_get_system_account_kind( acc );
292 483 : 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 483 : ulong min_balance = 0UL;
298 483 : 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 483 : 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 483 : 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 483 : 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 483 : 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 483 : return fd_executor_check_static_account_rent_state_transition(
320 483 : pre_balance,
321 483 : post_balance,
322 483 : acc->data_len,
323 483 : rent,
324 483 : relax_post_exec_min_balance_check
325 483 : );
326 483 : }
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 597 : fd_txn_out_t * txn_out ) {
333 597 : if( FD_UNLIKELY( !status_cache ) ) {
334 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
335 0 : }
336 :
337 597 : 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 576 : fd_blake3_t b3[1];
349 576 : fd_blake3_init( b3 );
350 576 : fd_blake3_append( b3, "solana-tx-message-v1", 20UL );
351 576 : 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 576 : fd_blake3_fini( b3, &txn_out->details.blake_txn_msg_hash );
353 :
354 576 : fd_hash_t * blockhash = (fd_hash_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->recent_blockhash_off);
355 576 : int found = fd_txncache_query( status_cache, bank->txncache_fork_id, blockhash->uc, txn_out->details.blake_txn_msg_hash.uc );
356 576 : if( FD_UNLIKELY( found ) ) return FD_RUNTIME_TXN_ERR_ALREADY_PROCESSED;
357 :
358 576 : 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 237 : for( ulong i=0UL; i<txn_in->bundle.prev_txn_cnt; i++ ) {
365 96 : fd_txn_out_t const * prev_txn_out = txn_in->bundle.prev_txn_outs[i];
366 96 : 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 96 : }
370 144 : }
371 :
372 573 : return FD_RUNTIME_EXECUTE_SUCCESS;
373 576 : }
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 606 : 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 606 : int err = fd_sanitize_compute_unit_limits( txn_out );
383 606 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
384 0 : return err;
385 0 : }
386 :
387 : /* https://github.com/anza-xyz/agave/blob/v3.1.8/runtime/src/bank/check_transactions.rs#L124-L131 */
388 606 : err = fd_check_transaction_age( bank, txn_in, txn_out );
389 606 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
390 9 : return err;
391 9 : }
392 :
393 597 : return FD_RUNTIME_EXECUTE_SUCCESS;
394 606 : }
395 :
396 : /* https://github.com/anza-xyz/agave/blob/v2.0.9/runtime/src/bank.rs#L3239-L3251 */
397 : static inline ulong
398 648 : get_transaction_account_lock_limit( void ) {
399 648 : return 64UL;
400 648 : }
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 606 : 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 606 : int err = fd_executor_check_transaction_age_and_compute_budget_limits( bank, txn_in, txn_out );
410 606 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
411 9 : return err;
412 9 : }
413 :
414 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank/check_transactions.rs#L74 */
415 597 : err = fd_executor_check_status_cache( runtime->status_cache, bank, txn_in, txn_out );
416 597 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
417 3 : return err;
418 3 : }
419 :
420 594 : return FD_RUNTIME_EXECUTE_SUCCESS;
421 597 : }
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 15 : fd_txn_out_t * txn_out ) {
428 15 : fd_compute_budget_details_t * details = &txn_out->details.compute_budget;
429 15 : fd_txn_t const * txn = TXN( txn_in->txn );
430 :
431 15 : ulong priority_fee, cu_limit, loaded, heap;
432 15 : uint config_mask = fd_uint_load_4( (uchar const *)txn_in->txn->payload + 4 );
433 15 : fd_txn_parse_v1_config( config_mask,
434 15 : (uchar const *)txn_in->txn->payload + txn->v1_txn_config_values_off,
435 15 : &priority_fee, &cu_limit, &loaded, &heap );
436 :
437 :
438 15 : details->is_v1 = 1;
439 15 : details->priority_fee_lamports = priority_fee;
440 15 : details->has_compute_units_limit_update = 1;
441 15 : details->compute_unit_limit = cu_limit;
442 15 : details->loaded_accounts_data_size_limit = fd_ulong_min( FD_VM_LOADED_ACCOUNTS_DATA_SIZE_LIMIT, loaded );
443 15 : details->has_loaded_accounts_data_size_limit_update = 0;
444 15 : details->heap_size = heap;
445 15 : details->has_requested_heap_size = 0;
446 :
447 15 : return FD_RUNTIME_EXECUTE_SUCCESS;
448 15 : }
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 630 : fd_txn_out_t * txn_out ) {
465 630 : 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 630 : if( FD_UNLIKELY( TXN( txn_in->txn )->transaction_version==FD_TXN_V1 &&
469 630 : !FD_FEATURE_ACTIVE_BANK( bank, enable_tx_v1 ) ) ) {
470 12 : return FD_RUNTIME_TXN_ERR_UNSUPPORTED_VERSION;
471 12 : }
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 618 : if( TXN( txn_in->txn )->transaction_version==FD_TXN_V1 ) {
475 15 : fd_compute_budget_count_instrs( bank, txn_in, &txn_out->details.compute_budget ); /* telemetry only, v1 sets the CU limit */
476 15 : err = fd_executor_sanitize_txn_v1_config( txn_in, txn_out );
477 603 : } else {
478 603 : err = fd_executor_compute_budget_program_execute_instructions( bank, txn_in, txn_out );
479 603 : }
480 618 : if( FD_UNLIKELY( err ) ) return err;
481 :
482 618 : return FD_RUNTIME_EXECUTE_SUCCESS;
483 618 : }
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 1659 : 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 1659 : 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 0 : int err = fd_sysvar_instructions_serialize_account( txn_in, txn_out, txn_idx );
525 0 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
526 0 : *out_loaded_sz = 0UL;
527 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
528 0 : }
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 1659 : 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 1521 : 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 1521 : *out_loaded_sz = fd_ulong_sat_add( base_account_size, acc->data_len );
545 1521 : return FD_RUNTIME_EXECUTE_SUCCESS;
546 1521 : }
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 138 : *out_loaded_sz = 0UL;
556 138 : return FD_RUNTIME_EXECUTE_SUCCESS;
557 1659 : }
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 2676 : ulong data_size_delta ) {
563 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L500-L503 */
564 2676 : 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 2676 : txn_out->details.loaded_accounts_data_size = fd_ulong_sat_add( txn_out->details.loaded_accounts_data_size, data_size_delta );
570 :
571 2676 : if( FD_UNLIKELY( txn_out->details.loaded_accounts_data_size>txn_out->details.compute_budget.loaded_accounts_data_size_limit ) ) {
572 9 : return FD_RUNTIME_TXN_ERR_MAX_LOADED_ACCOUNTS_DATA_SIZE_EXCEEDED;
573 9 : }
574 :
575 2667 : return FD_RUNTIME_EXECUTE_SUCCESS;
576 2676 : }
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 2142 : 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 2142 : int err = fd_increase_calculated_data_size( txn_out, loaded_acc_size );
589 2142 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
590 9 : return err;
591 9 : }
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 2133 : if( FD_LIKELY( memcmp( acc->owner, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
601 1989 : return FD_RUNTIME_EXECUTE_SUCCESS;
602 1989 : }
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 144 : fd_bpf_state_t loader_state[1];
607 144 : err = fd_bpf_loader_program_get_state( acc, loader_state );
608 144 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
609 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
610 0 : }
611 :
612 : /* Make sure the account is a v3 program */
613 144 : if( loader_state->discriminant!=FD_BPF_STATE_PROGRAM ) {
614 48 : return FD_RUNTIME_EXECUTE_SUCCESS;
615 48 : }
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 279 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
621 207 : if( FD_UNLIKELY( !memcmp( &txn_out->accounts.keys[i], &loader_state->inner.program.programdata_address, sizeof(fd_pubkey_t) ) ) ) {
622 24 : return FD_RUNTIME_EXECUTE_SUCCESS;
623 24 : }
624 207 : }
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 72 : for( ushort i=0; i<*additional_loaded_account_keys_cnt; i++ ) {
629 0 : 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 0 : }
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 72 : fd_acc_t const * programdata_ref = NULL;
640 72 : ushort pd_idx = USHORT_MAX;
641 72 : for( ushort i=0; i<txn_out->accounts.executable_cnt; i++ ) {
642 39 : fd_acc_t const * exe = txn_out->accounts.executable[ i ];
643 39 : if( !memcmp( exe->pubkey, &loader_state->inner.program.programdata_address, 32UL ) ) {
644 39 : programdata_ref = exe;
645 39 : pd_idx = i;
646 39 : break;
647 39 : }
648 39 : }
649 72 : ulong programdata_sz;
650 72 : 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 39 : ulong cur = txn_out->accounts.executable_cur_len[ pd_idx ];
657 39 : if( cur!=ULONG_MAX ) {
658 24 : if( FD_UNLIKELY( !txn_out->accounts.executable_cur_lamports[ pd_idx ] ) ) return FD_RUNTIME_EXECUTE_SUCCESS;
659 21 : programdata_sz = cur;
660 21 : } else {
661 15 : if( FD_UNLIKELY( !programdata_ref->lamports ) ) return FD_RUNTIME_EXECUTE_SUCCESS;
662 12 : programdata_sz = programdata_ref->data_len;
663 12 : }
664 39 : } 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 33 : int found = 0;
673 33 : programdata_sz = 0UL;
674 33 : for( ushort i=0; i<txn_out->accounts.executable_skipped_cnt; i++ ) {
675 18 : if( !memcmp( txn_out->accounts.executable_skipped_key[ i ].uc, &loader_state->inner.program.programdata_address, 32UL ) ) {
676 18 : programdata_sz = txn_out->accounts.executable_skipped_len[ i ];
677 18 : found = 1;
678 18 : break;
679 18 : }
680 18 : }
681 33 : if( FD_UNLIKELY( !found ) ) return FD_RUNTIME_EXECUTE_SUCCESS;
682 33 : }
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 51 : ulong programdata_size_delta = fd_ulong_sat_add( FD_TRANSACTION_ACCOUNT_BASE_SIZE, programdata_sz );
687 51 : err = fd_increase_calculated_data_size( txn_out, programdata_size_delta );
688 51 : 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 51 : fd_memcpy(
695 51 : &additional_loaded_account_keys[(*additional_loaded_account_keys_cnt)++],
696 51 : &loader_state->inner.program.programdata_address,
697 51 : sizeof(fd_pubkey_t) );
698 :
699 51 : return FD_RUNTIME_EXECUTE_SUCCESS;
700 51 : }
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 483 : 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 483 : fd_pubkey_t additional_loaded_account_keys[ FD_TXN_ACCT_ADDR_MAX ];
724 483 : 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 483 : ulong aluts_size = fd_ulong_sat_mul( TXN( txn_in->txn )->addr_table_lookup_cnt,
729 483 : FD_ADDRESS_LOOKUP_TABLE_BASE_SIZE );
730 483 : int err = fd_increase_calculated_data_size( txn_out, aluts_size );
731 483 : 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 2616 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
737 2142 : fd_acc_t * acc = txn_out->accounts.account[ i ];
738 :
739 2142 : uchar unknown_acc = !!( !fd_runtime_get_account_at_index( txn_in, txn_out, i, fd_runtime_account_check_exists ) ||
740 2142 : 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 2142 : if( FD_UNLIKELY( i==FD_FEE_PAYER_TXN_IDX ) ) {
747 483 : ulong loaded_acc_size = fd_ulong_sat_add( FD_TRANSACTION_ACCOUNT_BASE_SIZE,
748 483 : acc->data_len );
749 483 : int err = fd_collect_loaded_account(
750 483 : txn_out,
751 483 : acc,
752 483 : loaded_acc_size,
753 483 : additional_loaded_account_keys,
754 483 : &additional_loaded_account_keys_cnt );
755 483 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
756 0 : return err;
757 0 : }
758 483 : continue;
759 483 : }
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 1659 : ulong loaded_acc_size = 0UL;
764 1659 : int err = load_transaction_account( bank, txn_in, txn_out, &txn_out->accounts.keys[i], acc, unknown_acc, i, &loaded_acc_size );
765 1659 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
766 0 : return err;
767 0 : }
768 1659 : err = fd_collect_loaded_account(
769 1659 : txn_out,
770 1659 : acc,
771 1659 : loaded_acc_size,
772 1659 : additional_loaded_account_keys,
773 1659 : &additional_loaded_account_keys_cnt );
774 1659 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
775 9 : return err;
776 9 : }
777 1659 : }
778 :
779 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L662-L686 */
780 474 : ushort instr_cnt = TXN( txn_in->txn )->instr_cnt;
781 864 : for( ushort i=0; i<instr_cnt; i++ ) {
782 408 : 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 408 : fd_acc_t * ref = fd_runtime_get_account_at_index(
787 408 : txn_in, txn_out, instr->program_id, fd_runtime_account_check_exists );
788 408 : if( FD_UNLIKELY( !ref ) ) return FD_RUNTIME_TXN_ERR_PROGRAM_ACCOUNT_NOT_FOUND;
789 :
790 390 : 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 390 : if( FD_UNLIKELY( memcmp( ref->owner, &fd_solana_native_loader_id, 32UL ) &&
794 390 : !fd_executor_pubkey_is_bpf_loader( (fd_pubkey_t*)ref->owner ) ) ) {
795 0 : return FD_RUNTIME_TXN_ERR_INVALID_PROGRAM_FOR_EXECUTION;
796 0 : }
797 390 : }
798 :
799 456 : return FD_RUNTIME_EXECUTE_SUCCESS;
800 474 : }
801 :
802 : /* https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118 */
803 : int
804 648 : 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 648 : ulong tx_account_lock_limit = get_transaction_account_lock_limit();
808 648 : 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 3171 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
815 8274 : for( ushort j=(ushort)(i+1U); j<txn_out->accounts.cnt; j++ ) {
816 5751 : if( FD_UNLIKELY( !memcmp( &txn_out->accounts.keys[i], &txn_out->accounts.keys[j], sizeof(fd_pubkey_t) ) ) ) {
817 9 : return FD_RUNTIME_TXN_ERR_ACCOUNT_LOADED_TWICE;
818 9 : }
819 5751 : }
820 2532 : }
821 :
822 : /* https://github.com/anza-xyz/agave/blob/v2.2.17/accounts-db/src/account_locks.rs#L124-L126 */
823 639 : return FD_RUNTIME_EXECUTE_SUCCESS;
824 648 : }
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 594 : fd_get_prioritization_fee( fd_compute_budget_details_t const * compute_budget_details ) {
829 594 : if( compute_budget_details->is_v1 ) {
830 3 : return compute_budget_details->priority_fee_lamports;
831 3 : }
832 591 : uint128 micro_lamport_fee = fd_uint128_sat_mul( compute_budget_details->compute_unit_price, compute_budget_details->compute_unit_limit );
833 591 : uint128 fee = fd_uint128_sat_add( micro_lamport_fee, MICRO_LAMPORTS_PER_LAMPORT-1UL ) / MICRO_LAMPORTS_PER_LAMPORT;
834 591 : return fee>(uint128)ULONG_MAX ? ULONG_MAX : (ulong)fee;
835 594 : }
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 594 : 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 594 : ulong num_signatures = txn_descriptor->signature_cnt;
849 1017 : for (ushort i=0; i<txn_descriptor->instr_cnt; ++i ) {
850 423 : fd_txn_instr_t const * txn_instr = &txn_descriptor->instr[i];
851 423 : fd_pubkey_t * program_id = &txn_out->accounts.keys[txn_instr->program_id];
852 423 : if( !memcmp(program_id->uc, fd_solana_keccak_secp_256k_program_id.key, sizeof(fd_pubkey_t)) ||
853 423 : !memcmp(program_id->uc, fd_solana_ed25519_sig_verify_program_id.key, sizeof(fd_pubkey_t)) ||
854 423 : !memcmp(program_id->uc, fd_solana_secp256r1_program_id.key, sizeof(fd_pubkey_t)) ) {
855 0 : if( !txn_instr->data_sz ) {
856 0 : continue;
857 0 : }
858 0 : uchar const * data = payload + txn_instr->data_off;
859 0 : num_signatures = fd_ulong_sat_add(num_signatures, (ulong)(data[0]));
860 0 : }
861 423 : }
862 594 : *ret_execution_fee = FD_RUNTIME_FEE_STRUCTURE_LAMPORTS_PER_SIGNATURE * num_signatures;
863 594 : *ret_priority_fee = fd_get_prioritization_fee( &txn_out->details.compute_budget );
864 594 : }
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 483 : fd_executor_create_rollback_fee_payer_account( fd_txn_out_t * txn_out ) {
890 483 : fd_acc_t const * fee_payer = txn_out->accounts.account[ FD_FEE_PAYER_TXN_IDX ];
891 483 : txn_out->accounts.fee_payer_rollback_lamports = fee_payer->lamports;
892 483 : }
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 594 : 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 594 : 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 594 : if( FD_UNLIKELY( !ref ) ) return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
902 :
903 594 : 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 594 : ulong execution_fee = 0UL;
908 594 : ulong priority_fee = 0UL;
909 :
910 594 : fd_executor_calculate_fee( txn_out, TXN( txn_in->txn ), txn_in->txn->payload, &execution_fee, &priority_fee );
911 594 : 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 594 : int err = fd_validate_fee_payer(
916 594 : fee_payer,
917 594 : &bank->f.rent,
918 594 : total_fee,
919 594 : FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check )
920 594 : );
921 594 : 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 483 : fd_executor_create_rollback_fee_payer_account( txn_out );
926 :
927 483 : txn_out->details.execution_fee = execution_fee;
928 483 : txn_out->details.priority_fee = priority_fee;
929 :
930 483 : return FD_RUNTIME_EXECUTE_SUCCESS;
931 594 : }
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 651 : fd_txn_out_t * txn_out ) {
942 651 : 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 270 : fd_sysvar_cache_t const * sysvar_cache = &bank->f.sysvar_cache;
945 270 : fd_slot_hashes_t slot_hashes_view[1];
946 270 : 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 270 : FD_TEST( bank->parent_accdb_fork_id.val!=USHORT_MAX );
968 270 : fd_acct_addr_t * accts_alt = fd_type_pun( &txn_out->accounts.keys[txn_out->accounts.cnt] );
969 270 : int err = fd_runtime_load_txn_address_lookup_tables( TXN( txn_in->txn ),
970 270 : txn_in->txn->payload,
971 270 : runtime->accdb,
972 270 : bank->parent_accdb_fork_id,
973 270 : bank->f.slot,
974 270 : slot_hashes_view,
975 270 : accts_alt );
976 270 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
977 267 : txn_out->accounts.cnt += TXN( txn_in->txn )->addr_table_adtl_cnt;
978 :
979 267 : }
980 648 : return FD_RUNTIME_EXECUTE_SUCCESS;
981 651 : }
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 3201 : 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 3201 : ulong starting_lamports_h = 0UL;
992 3201 : ulong starting_lamports_l = 0UL;
993 3201 : int err = fd_instr_info_sum_account_lamports( instr,
994 3201 : txn_out,
995 3201 : &starting_lamports_h,
996 3201 : &starting_lamports_l );
997 3201 : if( FD_UNLIKELY( err ) ) {
998 0 : return err;
999 0 : }
1000 3201 : instr->starting_lamports_h = starting_lamports_h;
1001 3201 : 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 3201 : if( runtime->instr.stack_sz>0 ) {
1006 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/sdk/src/transaction_context.rs#L330 */
1007 2817 : 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 2817 : ulong original_caller_lamport_sum_h = caller_instruction_context->instr->starting_lamports_h;
1011 2817 : 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 2817 : ulong current_caller_lamport_sum_h = 0UL;
1015 2817 : ulong current_caller_lamport_sum_l = 0UL;
1016 2817 : int err = fd_instr_info_sum_account_lamports( caller_instruction_context->instr,
1017 2817 : caller_instruction_context->txn_out,
1018 2817 : ¤t_caller_lamport_sum_h,
1019 2817 : ¤t_caller_lamport_sum_l );
1020 2817 : 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 2817 : if( FD_UNLIKELY( current_caller_lamport_sum_h!=original_caller_lamport_sum_h ||
1026 2817 : current_caller_lamport_sum_l!=original_caller_lamport_sum_l ) ) {
1027 303 : return FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR;
1028 303 : }
1029 2817 : }
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 2898 : 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 2898 : if( FD_UNLIKELY( runtime->instr.stack_sz>=FD_MAX_INSTRUCTION_STACK_DEPTH ) ) {
1041 48 : return FD_EXECUTOR_INSTR_ERR_CALL_DEPTH;
1042 48 : }
1043 2850 : 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 2850 : ulong idx = fd_runtime_find_index_of_account( txn_out, &fd_sysvar_instructions_id );
1048 2850 : 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 0 : fd_acc_t * ref = fd_runtime_get_account_at_index( txn_in, txn_out, (ushort)idx, NULL );
1051 0 : 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 0 : 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 0 : fd_sysvar_instructions_update_current_instr_idx( txn_out->accounts.account[ idx ], (ushort)runtime->instr.current_idx );
1060 0 : }
1061 :
1062 2850 : return FD_EXECUTOR_INSTR_SUCCESS;
1063 2850 : }
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 3201 : 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 3201 : fd_pubkey_t const * program_id_pubkey = NULL;
1086 3201 : int err = fd_runtime_get_key_of_account_at_index( txn_out,
1087 3201 : instr->program_id,
1088 3201 : &program_id_pubkey );
1089 3201 : 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 3201 : 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 2817 : uchar contains = 0;
1097 2817 : uchar is_last = 0;
1098 :
1099 : // Checks all previous instructions in the stack for reentrancy
1100 5826 : for( uchar level=0; level<runtime->instr.stack_sz; level++ ) {
1101 3009 : 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 3009 : if( instr->program_id == instr_ctx->instr->program_id ) {
1104 : // Reentrancy not allowed unless caller is calling itself
1105 2997 : if( level == runtime->instr.stack_sz-1 ) {
1106 2805 : is_last = 1;
1107 2805 : }
1108 2997 : contains = 1;
1109 2997 : }
1110 3009 : }
1111 : /* https://github.com/anza-xyz/agave/blob/v2.0.0/program-runtime/src/invoke_context.rs#L282-L285 */
1112 2817 : if( FD_UNLIKELY( contains && !is_last ) ) {
1113 0 : return FD_EXECUTOR_INSTR_ERR_REENTRANCY_NOT_ALLOWED;
1114 0 : }
1115 2817 : }
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 3201 : return fd_txn_ctx_push( runtime, txn_in, txn_out, instr );
1119 3201 : }
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 2850 : 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 2850 : if( FD_UNLIKELY( runtime->instr.stack_sz==0 ) ) {
1132 0 : return FD_EXECUTOR_INSTR_ERR_CALL_DEPTH;
1133 0 : }
1134 2850 : 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 7296 : for( ushort i=0; i<instr->acct_cnt; i++ ) {
1139 4446 : ushort idx_in_txn = instr->accounts[ i ].index_in_transaction;
1140 4446 : fd_acc_t const * acc = txn_out->accounts.account[ idx_in_txn ];
1141 4446 : ulong refcnt = runtime->accounts.refcnt[idx_in_txn];
1142 4446 : if( FD_UNLIKELY( acc->executable && refcnt ) ) return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_OUTSTANDING;
1143 4446 : }
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 2850 : ulong ending_lamports_h = 0UL;
1148 2850 : ulong ending_lamports_l = 0UL;
1149 2850 : int err = fd_instr_info_sum_account_lamports( instr,
1150 2850 : txn_out,
1151 2850 : &ending_lamports_h,
1152 2850 : &ending_lamports_l );
1153 2850 : if( FD_UNLIKELY( err ) ) {
1154 0 : return err;
1155 0 : }
1156 2850 : if( FD_UNLIKELY( ending_lamports_l != instr->starting_lamports_l || ending_lamports_h != instr->starting_lamports_h ) ) {
1157 0 : return FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR;
1158 0 : }
1159 :
1160 2850 : return FD_EXECUTOR_INSTR_SUCCESS;
1161 2850 : }
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 2850 : int instr_exec_result ) {
1170 2850 : 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 2850 : if( FD_UNLIKELY( instr_exec_result==FD_EXECUTOR_INSTR_SUCCESS && stack_pop_err ) ) {
1174 0 : FD_TXN_PREPARE_ERR_OVERWRITE( instr_ctx->txn_out );
1175 0 : FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, stack_pop_err, instr_ctx->txn_out->err.exec_err_idx );
1176 0 : instr_exec_result = stack_pop_err;
1177 0 : }
1178 :
1179 2850 : return instr_exec_result;
1180 2850 : }
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 3201 : fd_instr_info_t * instr ) {
1188 3201 : if( FD_UNLIKELY( runtime->instr.stack_sz ) ) runtime->metrics.cpi_cum++;
1189 384 : else runtime->metrics.instr_cum++;
1190 :
1191 3201 : fd_sysvar_cache_t const * sysvar_cache = &bank->f.sysvar_cache;
1192 3201 : int instr_exec_result = fd_instr_stack_push( runtime, txn_in, txn_out, instr );
1193 3201 : if( FD_UNLIKELY( instr_exec_result ) ) {
1194 351 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1195 351 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1196 351 : return instr_exec_result;
1197 351 : }
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 2850 : fd_exec_instr_ctx_t * ctx = &runtime->instr.stack[ runtime->instr.stack_sz - 1 ];
1202 2850 : *ctx = (fd_exec_instr_ctx_t) {
1203 2850 : .instr = instr,
1204 2850 : .sysvar_cache = sysvar_cache,
1205 2850 : .runtime = runtime,
1206 2850 : .txn_in = txn_in,
1207 2850 : .txn_out = txn_out,
1208 2850 : .bank = bank,
1209 2850 : };
1210 :
1211 2850 : if( FD_UNLIKELY( !runtime->log.log_collector->disabled ) ) {
1212 2454 : fd_base58_encode_32( txn_out->accounts.keys[ instr->program_id ].uc, NULL, ctx->program_id_base58 );
1213 2454 : }
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 2850 : fd_exec_instr_fn_t native_prog_fn;
1218 2850 : uchar is_precompile;
1219 2850 : int err = fd_executor_lookup_native_program( txn_out->accounts.account[ instr->program_id ],
1220 2850 : bank,
1221 2850 : &native_prog_fn,
1222 2850 : &is_precompile );
1223 :
1224 2850 : if( FD_UNLIKELY( err ) ) {
1225 0 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1226 0 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, err, txn_out->err.exec_err_idx );
1227 0 : return fd_execute_instr_end( ctx, instr, err );
1228 0 : }
1229 :
1230 2850 : 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 2850 : 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 2850 : if( FD_LIKELY( !is_precompile ) ) {
1239 2850 : txn_out->details.return_data.len = 0;
1240 2850 : txn_out->details.return_data.program_id = (fd_pubkey_t){0};
1241 2850 : }
1242 :
1243 : /* Execute the native program. */
1244 2850 : instr_exec_result = native_prog_fn( ctx );
1245 2850 : } 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 2850 : if( FD_LIKELY( instr_exec_result==FD_EXECUTOR_INSTR_SUCCESS ) ) {
1254 : /* Log success */
1255 2496 : if( FD_LIKELY( !is_precompile ) ) fd_log_collector_program_success( ctx );
1256 2496 : } 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 354 : if( !txn_out->err.exec_err ) {
1267 273 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1268 273 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1269 273 : if( FD_LIKELY( !is_precompile ) ) fd_log_collector_program_failure( ctx );
1270 273 : } else {
1271 81 : if( FD_LIKELY( !is_precompile ) ) fd_log_collector_program_failure( ctx );
1272 81 : FD_TXN_PREPARE_ERR_OVERWRITE( txn_out );
1273 81 : FD_TXN_ERR_FOR_LOG_INSTR( txn_out, instr_exec_result, txn_out->err.exec_err_idx );
1274 81 : }
1275 354 : }
1276 :
1277 2850 : return fd_execute_instr_end( ctx, instr, instr_exec_result );
1278 2850 : }
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 159 : 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 1128 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
1297 969 : txn_out->accounts.is_writable[ i ] = (uchar)fd_runtime_account_is_writable_idx( txn_in, txn_out, i );
1298 969 : txn_out->accounts.account_acquired[ i ] = 0U;
1299 969 : runtime->accounts.refcnt[ i ] = 0UL;
1300 :
1301 969 : fd_acc_t * acc = txn_out->accounts.account[ i ];
1302 969 : 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 969 : if( FD_UNLIKELY( !acc->lamports ) ) {
1310 75 : acc->data_len = 0UL;
1311 75 : acc->executable = 0;
1312 75 : memset( acc->owner, 0, 32UL );
1313 75 : }
1314 :
1315 969 : txn_out->accounts.starting_lamports[ i ] = acc->lamports;
1316 969 : txn_out->accounts.starting_data_len[ i ] = acc->data_len;
1317 969 : 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 969 : int found = 0;
1322 1692 : for( ulong p=txn_in->bundle.prev_txn_cnt; p>0UL && !found; p-- ) {
1323 723 : fd_txn_out_t * prev_txn = txn_in->bundle.prev_txn_outs[ p-1UL ];
1324 4893 : for( ushort k=0; k<prev_txn->accounts.cnt; k++ ) {
1325 : /* Only match if the txn owns the reference to the account. */
1326 4650 : if( !prev_txn->accounts.account_acquired[ k ] ||
1327 4650 : !fd_pubkey_eq( &prev_txn->accounts.keys[ k ], &txn_out->accounts.keys[ i ] ) ) continue;
1328 480 : 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 480 : if( txn_out->accounts.is_writable[ i ] ) {
1335 123 : txn_out->accounts.stake_update[ i ] |= prev_txn->accounts.stake_update[ k ]; prev_txn->accounts.stake_update[ k ] = 0;
1336 123 : txn_out->accounts.vote_update [ i ] |= prev_txn->accounts.vote_update [ k ]; prev_txn->accounts.vote_update [ k ] = 0;
1337 123 : txn_out->accounts.touched [ i ] |= prev_txn->accounts.touched [ k ];
1338 123 : prev_txn->accounts.account_acquired[ k ] = 0U;
1339 123 : txn_out->accounts.account_acquired[ i ] = 1U;
1340 123 : }
1341 480 : break;
1342 4650 : }
1343 723 : }
1344 :
1345 969 : if( !found ) txn_out->accounts.account_acquired[ i ] = 1U;
1346 969 : }
1347 :
1348 159 : txn_out->accounts.is_setup = 1;
1349 159 : txn_out->accounts.nonce_idx_in_txn = ULONG_MAX;
1350 159 : }
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 447 : fd_txn_out_t * txn_out ) {
1357 :
1358 447 : ulong acquire_idx[ MAX_TX_ACCOUNT_LOCKS ];
1359 447 : int acquire_writable[ MAX_TX_ACCOUNT_LOCKS ];
1360 447 : uchar const * acquire_pubkeys[ MAX_TX_ACCOUNT_LOCKS ];
1361 447 : ulong acquire_cnt = 0UL;
1362 :
1363 447 : runtime->accounts.account_cnt = 0UL;
1364 447 : runtime->accounts.executable_cnt = 0UL;
1365 :
1366 447 : txn_out->accounts.cnt = (uchar)TXN( txn_in->txn )->acct_addr_cnt;
1367 447 : fd_pubkey_t * tx_accs = (fd_pubkey_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->acct_addr_off);
1368 1908 : for( ulong i=0UL; i<TXN( txn_in->txn )->acct_addr_cnt; i++ ) txn_out->accounts.keys[ i ] = tx_accs[ i ];
1369 :
1370 447 : txn_out->accounts.executable_cnt = 0UL;
1371 :
1372 447 : int err = fd_executor_setup_txn_alut_account_keys( runtime, bank, txn_in, txn_out );
1373 447 : 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 447 : err = fd_executor_validate_account_locks( txn_out );
1378 447 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
1379 :
1380 : /* Resolve all transaction accounts and queue them for acquisition. */
1381 :
1382 1908 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
1383 1461 : txn_out->accounts.is_writable[ i ] = (uchar)fd_runtime_account_is_writable_idx( txn_in, txn_out, i );
1384 1461 : txn_out->accounts.account_acquired[ i ] = 0U;
1385 1461 : txn_out->accounts.account[ i ] = NULL;
1386 1461 : runtime->accounts.refcnt[ i ] = 0UL;
1387 :
1388 1461 : FD_TEST( runtime->accounts.account_cnt+acquire_cnt<FD_PACK_MAX_TXN_PER_BUNDLE*MAX_TX_ACCOUNT_LOCKS );
1389 1461 : acquire_idx[ acquire_cnt ] = i;
1390 1461 : acquire_pubkeys[ acquire_cnt ] = txn_out->accounts.keys[ i ].uc;
1391 1461 : acquire_writable[ acquire_cnt ] = (int)txn_out->accounts.is_writable[ i ];
1392 1461 : acquire_cnt++;
1393 1461 : }
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 447 : if( FD_LIKELY( acquire_cnt ) ) {
1411 447 : fd_acc_t * acquire_base = &runtime->accounts.account[ runtime->accounts.account_cnt ];
1412 447 : fd_accdb_acquire_a( runtime->accdb, bank->accdb_fork_id, acquire_cnt, acquire_pubkeys, acquire_writable, acquire_base );
1413 :
1414 1908 : for( ulong i=0UL; i<acquire_cnt; i++ ) {
1415 1461 : ulong txn_idx = acquire_idx[ i ];
1416 1461 : txn_out->accounts.account[ txn_idx ] = &acquire_base[ i ];
1417 1461 : txn_out->accounts.account_acquired[ txn_idx ] = 1U;
1418 1461 : txn_out->accounts.starting_lamports[ txn_idx ] = acquire_base[ i ].prior_lamports;
1419 1461 : txn_out->accounts.starting_data_len[ txn_idx ] = acquire_base[ i ].prior_data_len;
1420 1461 : memcpy( &txn_out->accounts.starting_owner[ txn_idx ], acquire_base[ i ].owner, sizeof(fd_pubkey_t) );
1421 1461 : }
1422 447 : runtime->accounts.account_cnt += acquire_cnt;
1423 447 : }
1424 :
1425 447 : ushort executable_account_cnt = 0;
1426 447 : ushort executable_acquire_cnt = 0;
1427 447 : ushort executable_acquire_idx[ MAX_TX_ACCOUNT_LOCKS ];
1428 447 : fd_pubkey_t programdata_keys[ MAX_TX_ACCOUNT_LOCKS ];
1429 447 : uchar const * pubkeys[ MAX_TX_ACCOUNT_LOCKS ];
1430 1908 : for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
1431 1461 : fd_acc_t * acc = txn_out->accounts.account[ i ];
1432 1461 : if( FD_UNLIKELY( memcmp( acc->owner, fd_solana_bpf_loader_upgradeable_program_id.key, 32UL ) ) ) continue;
1433 93 : fd_bpf_state_t program_loader_state[1];
1434 93 : err = fd_bpf_loader_program_get_state( txn_out->accounts.account[ i ], program_loader_state );
1435 93 : if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) continue;
1436 93 : if( FD_UNLIKELY( program_loader_state->discriminant!=FD_BPF_STATE_PROGRAM ) ) continue;
1437 :
1438 60 : 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 60 : if( FD_UNLIKELY( fd_runtime_find_index_of_account( txn_out, programdata_key )!=ULONG_MAX ) ) continue;
1451 :
1452 45 : FD_TEST( bank->parent_accdb_fork_id.val!=USHORT_MAX );
1453 45 : if( FD_UNLIKELY( !fd_accdb_exists( runtime->accdb, bank->parent_accdb_fork_id, programdata_key->uc ) ) ) {
1454 18 : int skip_pd = 0;
1455 18 : ulong skip_len = 0UL;
1456 18 : 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 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 ) {
1462 9 : ushort s = txn_out->accounts.executable_skipped_cnt++;
1463 9 : txn_out->accounts.executable_skipped_key[ s ] = *programdata_key;
1464 9 : txn_out->accounts.executable_skipped_len[ s ] = skip_len;
1465 9 : }
1466 18 : continue;
1467 18 : }
1468 :
1469 27 : 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 27 : programdata_keys[ executable_acquire_cnt ] = *programdata_key;
1473 27 : pubkeys[ executable_acquire_cnt ] = programdata_keys[ executable_acquire_cnt ].uc;
1474 27 : executable_acquire_cnt++;
1475 27 : executable_account_cnt++;
1476 27 : }
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 447 : FD_TEST( runtime->accounts.executable_cnt+executable_acquire_cnt<=FD_PACK_MAX_TXN_PER_BUNDLE*MAX_TX_ACCOUNT_LOCKS );
1482 447 : fd_acc_t * acquire_base = &runtime->accounts.executable[ runtime->accounts.executable_cnt ];
1483 447 : fd_accdb_acquire_b( runtime->accdb, bank->parent_accdb_fork_id, acquire_cnt, executable_acquire_cnt, pubkeys, acquire_base );
1484 447 : int acquired_from_parent = bank->parent_accdb_fork_id.val!=bank->accdb_fork_id.val;
1485 474 : for( ushort i=0; i<executable_acquire_cnt; i++ ) {
1486 27 : ushort exe_idx = executable_acquire_idx[ i ];
1487 27 : txn_out->accounts.executable[ exe_idx ] = &acquire_base[ i ];
1488 27 : txn_out->accounts.executable_from_parent[ exe_idx ] = acquired_from_parent;
1489 27 : int pd = 0;
1490 27 : ulong len = ULONG_MAX;
1491 27 : ulong lamports = 0UL;
1492 27 : fd_accdb_probe_pd_this_fork( runtime->accdb, bank->accdb_fork_id, pubkeys[ i ], &pd, &len, &lamports );
1493 27 : txn_out->accounts.executable_pd_write[ exe_idx ] = pd;
1494 27 : txn_out->accounts.executable_cur_len[ exe_idx ] = len;
1495 27 : txn_out->accounts.executable_cur_lamports[ exe_idx ] = lamports;
1496 27 : }
1497 447 : runtime->accounts.executable_cnt += executable_acquire_cnt;
1498 :
1499 :
1500 447 : txn_out->accounts.is_setup = 1;
1501 447 : txn_out->accounts.nonce_idx_in_txn = ULONG_MAX;
1502 447 : txn_out->accounts.executable_cnt = executable_account_cnt;
1503 447 : return FD_RUNTIME_EXECUTE_SUCCESS;
1504 447 : }
1505 :
1506 : int
1507 : fd_executor_txn_verify( fd_txn_p_t * txn_p,
1508 : fd_sha512_t * shas[ FD_TXN_SIG_MAX ],
1509 3 : fd_ed25519_cache_t * cache ) {
1510 3 : fd_txn_t * txn = TXN( txn_p );
1511 :
1512 3 : uchar * signatures = txn_p->payload + txn->signature_off;
1513 3 : uchar * pubkeys = txn_p->payload + txn->acct_addr_off;
1514 3 : uchar * msg = txn_p->payload + txn->message_off;
1515 3 : ulong msg_sz = fd_txn_msg_sz( txn, txn_p->payload_sz );
1516 :
1517 3 : int res = cache ? fd_ed25519_verify_batch_single_msg_cached( msg, msg_sz, signatures, pubkeys, shas, txn->signature_cnt, cache ) :
1518 3 : fd_ed25519_verify_batch_single_msg ( msg, msg_sz, signatures, pubkeys, shas, txn->signature_cnt );
1519 3 : if( FD_UNLIKELY( res!=FD_ED25519_SUCCESS ) ) return FD_RUNTIME_TXN_ERR_SIGNATURE_FAILURE;
1520 :
1521 0 : return FD_RUNTIME_EXECUTE_SUCCESS;
1522 3 : }
1523 :
1524 : static int
1525 : fd_executor_txn_check( fd_bank_t * bank,
1526 303 : fd_txn_out_t * txn_out ) {
1527 303 : int err = 0;
1528 303 : ulong starting_lamports_l = 0UL;
1529 303 : ulong starting_lamports_h = 0UL;
1530 303 : ulong ending_lamports_l = 0UL;
1531 303 : ulong ending_lamports_h = 0UL;
1532 :
1533 : /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L63 */
1534 1908 : for( ulong i=0UL; i<txn_out->accounts.cnt; i++ ) {
1535 1605 : fd_acc_t * acc = txn_out->accounts.account[ i ];
1536 1605 : 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 666 : ulong starting_lamports = i!=FD_FEE_PAYER_TXN_IDX ? txn_out->accounts.starting_lamports[ i ]
1541 666 : : 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 666 : 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 666 : fd_uwide_inc( &ending_lamports_h, &ending_lamports_l, ending_lamports_h, ending_lamports_l, acc->lamports );
1558 666 : 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 666 : fd_rent_state_t pre_state = get_pre_exec_account_rent_state(
1564 666 : starting_lamports,
1565 666 : txn_out->accounts.starting_data_len[ i ],
1566 666 : fd_rent_exempt_minimum_balance( &bank->f.rent, txn_out->accounts.starting_data_len[ i ] ),
1567 666 : FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check )
1568 666 : );
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 666 : int relax_rent_exempt_criteria = FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check ) &&
1573 666 : txn_out->accounts.starting_data_len[ i ]>=acc->data_len &&
1574 666 : pre_state.discriminant==FD_RENT_STATE_RENT_EXEMPT &&
1575 666 : !memcmp( &txn_out->accounts.starting_owner[ i ], acc->owner, sizeof(fd_pubkey_t) );
1576 666 : fd_rent_state_t post_state = get_post_exec_account_rent_state(
1577 666 : acc->lamports,
1578 666 : acc->data_len,
1579 666 : fd_rent_exempt_minimum_balance( &bank->f.rent, acc->data_len ),
1580 666 : &pre_state,
1581 666 : starting_lamports,
1582 666 : relax_rent_exempt_criteria
1583 666 : );
1584 :
1585 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/svm/src/transaction_account_state_info.rs#L105-L125 */
1586 666 : err = fd_executor_check_rent_state_with_account( &txn_out->accounts.keys[ i ], &pre_state, &post_state );
1587 666 : if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
1588 :
1589 666 : if ( !memcmp( acc->owner, &fd_solana_stake_program_id, sizeof(fd_pubkey_t) ) ) txn_out->accounts.stake_update[ i ] = 1;
1590 657 : else if( !memcmp( acc->owner, &fd_solana_vote_program_id, sizeof(fd_pubkey_t) ) ) txn_out->accounts.vote_update[ i ] = 1;
1591 666 : }
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 303 : 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 303 : 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 303 : return FD_RUNTIME_EXECUTE_SUCCESS;
1604 303 : }
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 456 : fd_txn_out_t * txn_out ) {
1612 :
1613 456 : bool dump_insn = runtime->log.dump_proto_ctx &&
1614 456 : bank->f.slot>=runtime->log.dump_proto_ctx->dump_proto_start_slot &&
1615 456 : runtime->log.dump_proto_ctx->dump_instr_to_pb;
1616 :
1617 456 : if( FD_UNLIKELY( runtime->log.log_collector ) ) fd_log_collector_init( runtime->log.log_collector, runtime->log.enable_log_collector );
1618 :
1619 456 : fd_txn_t const * txn = TXN( txn_in->txn );
1620 687 : for( ushort i=0; i<txn->instr_cnt; i++ ) {
1621 : /* Set up the instr info for the current instruction */
1622 384 : fd_instr_info_t * instr_info = &runtime->instr.trace[ runtime->instr.trace_length++ ];
1623 384 : fd_instr_info_init_from_txn_instr( instr_info, txn_in, txn_out, &txn->instr[ i ] );
1624 :
1625 384 : 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 384 : runtime->instr.current_idx = i;
1632 :
1633 : /* Execute the current instruction */
1634 384 : int instr_exec_result = fd_execute_instr( runtime, bank, txn_in, txn_out, instr_info );
1635 384 : if( FD_UNLIKELY( instr_exec_result!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
1636 153 : if( FD_UNLIKELY( txn_out->err.exec_err_idx==UINT_MAX ) ) txn_out->err.exec_err_idx = i;
1637 153 : return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
1638 153 : }
1639 384 : }
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 303 : return fd_executor_txn_check( bank, txn_out );
1644 456 : }
1645 :
1646 : int
1647 : fd_executor_consume_cus( fd_txn_out_t * txn_out,
1648 2793 : ulong cus ) {
1649 2793 : ulong new_cus = txn_out->details.compute_budget.compute_meter - cus;
1650 2793 : int underflow = (txn_out->details.compute_budget.compute_meter < cus);
1651 2793 : if( FD_UNLIKELY( underflow ) ) {
1652 0 : txn_out->details.compute_budget.compute_meter = 0UL;
1653 0 : return FD_EXECUTOR_INSTR_ERR_COMPUTE_BUDGET_EXCEEDED;
1654 0 : }
1655 2793 : txn_out->details.compute_budget.compute_meter = new_cus;
1656 2793 : return FD_EXECUTOR_INSTR_SUCCESS;
1657 2793 : }
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 144 : fd_executor_instr_strerror( int err ) {
1665 :
1666 144 : 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 48 : 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 96 : 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 144 : }
1717 :
1718 0 : return "";
1719 144 : }
|