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