Line data Source code
1 : #include "fd_exec_txn_ctx.h"
2 : #include "../fd_acc_mgr.h"
3 : #include "../fd_executor.h"
4 : #include "../../vm/fd_vm.h"
5 : #include "../fd_system_ids.h"
6 :
7 : void *
8 0 : fd_exec_txn_ctx_new( void * mem ) {
9 0 : if( FD_UNLIKELY( !mem ) ) {
10 0 : FD_LOG_WARNING(( "NULL mem" ));
11 0 : return NULL;
12 0 : }
13 :
14 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, FD_EXEC_TXN_CTX_ALIGN ) ) ) {
15 0 : FD_LOG_WARNING(( "misaligned mem" ));
16 0 : return NULL;
17 0 : }
18 :
19 0 : fd_exec_txn_ctx_t * self = (fd_exec_txn_ctx_t *) mem;
20 0 : fd_memset( self, 0, sizeof(fd_exec_txn_ctx_t) );
21 :
22 0 : FD_COMPILER_MFENCE();
23 0 : self->magic = FD_EXEC_TXN_CTX_MAGIC;
24 0 : FD_COMPILER_MFENCE();
25 :
26 0 : return mem;
27 0 : }
28 :
29 : fd_exec_txn_ctx_t *
30 : fd_exec_txn_ctx_join( void * mem,
31 : fd_spad_t * spad,
32 0 : fd_wksp_t * spad_wksp ) {
33 0 : if( FD_UNLIKELY( !mem ) ) {
34 0 : FD_LOG_WARNING(( "NULL block" ));
35 0 : return NULL;
36 0 : }
37 :
38 0 : fd_exec_txn_ctx_t * ctx = (fd_exec_txn_ctx_t *) mem;
39 :
40 0 : if( FD_UNLIKELY( ctx->magic!=FD_EXEC_TXN_CTX_MAGIC ) ) {
41 0 : FD_LOG_WARNING(( "bad magic" ));
42 0 : return NULL;
43 0 : }
44 :
45 : /* Rejoin the wksp */
46 0 : ctx->spad = spad;
47 0 : ctx->spad_wksp = spad_wksp;
48 :
49 0 : return ctx;
50 0 : }
51 :
52 : void *
53 0 : fd_exec_txn_ctx_leave( fd_exec_txn_ctx_t * ctx) {
54 0 : if( FD_UNLIKELY( !ctx ) ) {
55 0 : FD_LOG_WARNING(( "NULL block" ));
56 0 : return NULL;
57 0 : }
58 :
59 0 : if( FD_UNLIKELY( ctx->magic!=FD_EXEC_TXN_CTX_MAGIC ) ) {
60 0 : FD_LOG_WARNING(( "bad magic" ));
61 0 : return NULL;
62 0 : }
63 :
64 0 : return (void *) ctx;
65 0 : }
66 :
67 : void *
68 0 : fd_exec_txn_ctx_delete( void * mem ) {
69 0 : if( FD_UNLIKELY( !mem ) ) {
70 0 : FD_LOG_WARNING(( "NULL mem" ));
71 0 : return NULL;
72 0 : }
73 :
74 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, FD_EXEC_TXN_CTX_ALIGN) ) ) {
75 0 : FD_LOG_WARNING(( "misaligned mem" ));
76 0 : return NULL;
77 0 : }
78 :
79 0 : fd_exec_txn_ctx_t * hdr = (fd_exec_txn_ctx_t *)mem;
80 0 : if( FD_UNLIKELY( hdr->magic!=FD_EXEC_TXN_CTX_MAGIC ) ) {
81 0 : FD_LOG_WARNING(( "bad magic" ));
82 0 : return NULL;
83 0 : }
84 :
85 0 : FD_COMPILER_MFENCE();
86 0 : FD_VOLATILE( hdr->magic ) = 0UL;
87 0 : FD_COMPILER_MFENCE();
88 :
89 0 : return mem;
90 0 : }
91 :
92 : int
93 : fd_exec_txn_ctx_get_account_at_index( fd_exec_txn_ctx_t * ctx,
94 : ushort idx,
95 : fd_txn_account_t * * account,
96 0 : fd_txn_account_condition_fn_t * condition ) {
97 0 : if( FD_UNLIKELY( idx>=ctx->accounts_cnt ) ) {
98 0 : return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
99 0 : }
100 :
101 0 : fd_txn_account_t * txn_account = &ctx->accounts[idx];
102 0 : *account = txn_account;
103 :
104 0 : if( FD_LIKELY( condition != NULL ) ) {
105 0 : if( FD_UNLIKELY( !condition( *account, ctx, idx ) ) ) {
106 0 : return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
107 0 : }
108 0 : }
109 :
110 0 : return FD_ACC_MGR_SUCCESS;
111 0 : }
112 :
113 : int
114 : fd_exec_txn_ctx_get_account_with_key( fd_exec_txn_ctx_t * ctx,
115 : fd_pubkey_t const * pubkey,
116 : fd_txn_account_t * * account,
117 0 : fd_txn_account_condition_fn_t * condition ) {
118 0 : int index = fd_exec_txn_ctx_find_index_of_account( ctx, pubkey );
119 0 : if( FD_UNLIKELY( index==-1 ) ) {
120 0 : return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
121 0 : }
122 :
123 0 : return fd_exec_txn_ctx_get_account_at_index( ctx,
124 0 : (uchar)index,
125 0 : account,
126 0 : condition );
127 0 : }
128 :
129 : int
130 : fd_exec_txn_ctx_get_executable_account( fd_exec_txn_ctx_t * ctx,
131 : fd_pubkey_t const * pubkey,
132 : fd_txn_account_t * * account,
133 0 : fd_txn_account_condition_fn_t * condition ) {
134 : /* First try to fetch the executable account from the existing borrowed accounts.
135 : If the pubkey is in the account keys, then we want to re-use that
136 : borrowed account since it reflects changes from prior instructions. Referencing the
137 : read-only executable accounts list is incorrect behavior when the program
138 : data account is written to in a prior instruction (e.g. program upgrade + invoke within the same txn) */
139 0 : int err = fd_exec_txn_ctx_get_account_with_key( ctx, pubkey, account, condition );
140 0 : if( FD_UNLIKELY( err==FD_ACC_MGR_SUCCESS ) ) {
141 0 : return FD_ACC_MGR_SUCCESS;
142 0 : }
143 :
144 0 : for( ushort i=0; i<ctx->executable_cnt; i++ ) {
145 0 : if( memcmp( pubkey->uc, ctx->executable_accounts[i].pubkey->uc, sizeof(fd_pubkey_t) )==0 ) {
146 0 : fd_txn_account_t * txn_account = &ctx->executable_accounts[i];
147 0 : *account = txn_account;
148 :
149 0 : if( FD_LIKELY( condition != NULL ) ) {
150 0 : if( FD_UNLIKELY( !condition( *account, ctx, i ) ) ) {
151 0 : return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
152 0 : }
153 0 : }
154 :
155 0 : return FD_ACC_MGR_SUCCESS;
156 0 : }
157 0 : }
158 :
159 0 : return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
160 0 : }
161 :
162 : int
163 : fd_exec_txn_ctx_get_key_of_account_at_index( fd_exec_txn_ctx_t * ctx,
164 : ushort idx,
165 0 : fd_pubkey_t const * * key ) {
166 : /* Return a NotEnoughAccountKeys error if idx is out of bounds.
167 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L218 */
168 0 : if( FD_UNLIKELY( idx>=ctx->accounts_cnt ) ) {
169 0 : return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
170 0 : }
171 :
172 0 : *key = &ctx->account_keys[ idx ];
173 0 : return FD_EXECUTOR_INSTR_SUCCESS;
174 0 : }
175 :
176 : void
177 0 : fd_exec_txn_ctx_setup_basic( fd_exec_txn_ctx_t * ctx ) {
178 0 : fd_compute_budget_details_new( &ctx->compute_budget_details );
179 :
180 0 : ctx->custom_err = 0U;
181 0 : ctx->instr_stack_sz = 0;
182 0 : ctx->accounts_cnt = 0UL;
183 0 : ctx->executable_cnt = 0UL;
184 0 : ctx->programs_to_reverify_cnt = 0UL;
185 :
186 0 : ctx->paid_fees = 0UL;
187 0 : ctx->loaded_accounts_data_size = 0UL;
188 0 : ctx->accounts_resize_delta = 0UL;
189 0 : ctx->collected_rent = 0UL;
190 :
191 0 : ctx->num_instructions = 0;
192 0 : memset( ctx->return_data.program_id.key, 0, sizeof(fd_pubkey_t) );
193 0 : ctx->return_data.len = 0;
194 :
195 0 : ctx->dirty_vote_acc = 0;
196 0 : ctx->failed_instr = NULL;
197 0 : ctx->instr_err_idx = INT_MAX;
198 0 : ctx->capture_ctx = NULL;
199 :
200 0 : ctx->instr_info_cnt = 0UL;
201 0 : ctx->cpi_instr_info_cnt = 0UL;
202 0 : ctx->instr_trace_length = 0UL;
203 :
204 0 : ctx->exec_err = 0;
205 0 : ctx->exec_err_kind = FD_EXECUTOR_ERR_KIND_NONE;
206 0 : ctx->current_instr_idx = 0;
207 0 : }
208 :
209 : void
210 0 : fd_exec_txn_ctx_teardown( fd_exec_txn_ctx_t * ctx ) {
211 0 : (void)ctx;
212 0 : }
213 :
214 : void
215 0 : fd_exec_txn_ctx_reset_return_data( fd_exec_txn_ctx_t * txn_ctx ) {
216 0 : txn_ctx->return_data.len = 0;
217 0 : }
218 :
219 : /* https://github.com/anza-xyz/agave/blob/v2.1.1/sdk/program/src/message/versions/v0/loaded.rs#L162 */
220 : int
221 : fd_txn_account_is_demotion( const int idx,
222 : const fd_txn_t * txn_descriptor,
223 0 : const uint bpf_upgradeable_in_txn ) {
224 0 : uint is_program = 0U;
225 0 : for( ulong j=0UL; j<txn_descriptor->instr_cnt; j++ ) {
226 0 : if( txn_descriptor->instr[j].program_id == idx ) {
227 0 : is_program = 1U;
228 0 : break;
229 0 : }
230 0 : }
231 :
232 0 : return (is_program && !bpf_upgradeable_in_txn);
233 0 : }
234 :
235 : uint
236 : fd_txn_account_has_bpf_loader_upgradeable( const fd_pubkey_t * account_keys,
237 0 : const ulong accounts_cnt ) {
238 0 : for( ulong j=0; j<accounts_cnt; j++ ) {
239 0 : const fd_pubkey_t * acc = &account_keys[j];
240 0 : if ( memcmp( acc->uc, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) == 0 ) {
241 0 : return 1U;
242 0 : }
243 0 : }
244 0 : return 0U;
245 0 : }
246 :
247 : /* This function aims to mimic the writable accounts check to populate the writable accounts cache, used
248 : to determine if accounts are writable or not.
249 :
250 : https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L38-L47 */
251 : int
252 0 : fd_exec_txn_ctx_account_is_writable_idx( fd_exec_txn_ctx_t const * txn_ctx, ushort idx ) {
253 0 : uint bpf_upgradeable = fd_txn_account_has_bpf_loader_upgradeable( txn_ctx->account_keys, txn_ctx->accounts_cnt );
254 0 : return fd_exec_txn_account_is_writable_idx_flat( txn_ctx->slot,
255 0 : idx,
256 0 : &txn_ctx->account_keys[idx],
257 0 : TXN( &txn_ctx->txn ),
258 0 : &txn_ctx->features,
259 0 : bpf_upgradeable );
260 0 : }
261 :
262 : int
263 : fd_exec_txn_account_is_writable_idx_flat( const ulong slot,
264 : const ushort idx,
265 : const fd_pubkey_t * addr_at_idx,
266 : const fd_txn_t * txn_descriptor,
267 : const fd_features_t * features,
268 0 : const uint bpf_upgradeable_in_txn ) {
269 : /* https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L43 */
270 0 : if( !fd_txn_is_writable( txn_descriptor, idx ) ) {
271 0 : return 0;
272 0 : }
273 :
274 : /* See comments in fd_system_ids.h.
275 : https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L44 */
276 0 : if( fd_pubkey_is_active_reserved_key( addr_at_idx ) ||
277 0 : fd_pubkey_is_pending_reserved_key( addr_at_idx ) ||
278 0 : ( FD_FEATURE_ACTIVE( slot, features, enable_secp256r1_precompile ) &&
279 0 : fd_pubkey_is_secp256r1_key( addr_at_idx ) ) ) {
280 :
281 0 : return 0;
282 0 : }
283 :
284 0 : if( fd_txn_account_is_demotion( idx, txn_descriptor, bpf_upgradeable_in_txn ) ) {
285 0 : return 0;
286 0 : }
287 :
288 0 : return 1;
289 0 : }
290 :
291 : /* Account pre-condition filtering functions */
292 :
293 : int
294 : fd_txn_account_check_exists( fd_txn_account_t * acc,
295 : fd_exec_txn_ctx_t const * ctx,
296 0 : ushort idx ) {
297 0 : (void) ctx;
298 0 : (void) idx;
299 0 : return fd_account_meta_exists( fd_txn_account_get_meta( acc ) );
300 0 : }
301 :
302 : int
303 : fd_txn_account_check_is_writable( fd_txn_account_t * acc,
304 : fd_exec_txn_ctx_t const * ctx,
305 0 : ushort idx ) {
306 0 : (void) acc;
307 0 : return fd_exec_txn_ctx_account_is_writable_idx( ctx, idx );
308 0 : }
309 :
310 : int
311 : fd_txn_account_check_fee_payer_writable( fd_txn_account_t * acc,
312 : fd_exec_txn_ctx_t const * ctx,
313 0 : ushort idx ) {
314 0 : (void) acc;
315 0 : return fd_txn_is_writable( TXN( &ctx->txn ), idx );
316 0 : }
317 :
318 : int
319 : fd_txn_account_check_borrow_mut( fd_txn_account_t * acc,
320 : fd_exec_txn_ctx_t const * ctx,
321 0 : ushort idx ) {
322 0 : (void) ctx;
323 0 : (void) idx;
324 0 : return fd_txn_account_is_mutable( acc ) && fd_txn_account_try_borrow_mut( acc );
325 0 : }
|