Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_runtime_context_fd_exec_txn_ctx_h 2 : #define HEADER_fd_src_flamenco_runtime_context_fd_exec_txn_ctx_h 3 : 4 : #include "fd_exec_instr_ctx.h" 5 : #include "../../log_collector/fd_log_collector_base.h" 6 : #include "../../../ballet/txn/fd_txn.h" 7 : #include "../../features/fd_features.h" 8 : #include "../fd_txncache.h" 9 : #include "../fd_bank_hash_cmp.h" 10 : #include "../../../funk/fd_funk.h" 11 : #include "../fd_compute_budget_details.h" 12 : #include "../../../disco/pack/fd_microblock.h" 13 : 14 : /* Return data for syscalls */ 15 : 16 : struct fd_txn_return_data { 17 : fd_pubkey_t program_id; 18 : ulong len; 19 : uchar data[1024]; 20 : }; 21 : 22 : typedef struct fd_txn_return_data fd_txn_return_data_t; 23 : 24 : /* fd_exec_txn_ctx_t is the context needed to execute a transaction. */ 25 : 26 : /* An entry in the instruction trace */ 27 : struct fd_exec_instr_trace_entry { 28 : /* Metadata about the instruction */ 29 : fd_instr_info_t * instr_info; 30 : /* Stack height when this instruction was pushed onto the stack (including itself) 31 : https://github.com/anza-xyz/agave/blob/d87e23d8d91c32d5f2be2bb3557c730bee1e9434/sdk/src/transaction_context.rs#L475-L480 */ 32 : ulong stack_height; 33 : }; 34 : typedef struct fd_exec_instr_trace_entry fd_exec_instr_trace_entry_t; 35 : 36 : /* https://github.com/anza-xyz/agave/blob/0d34a1a160129c4293dac248e14231e9e773b4ce/program-runtime/src/compute_budget.rs#L139 */ 37 : #define FD_MAX_INSTRUCTION_TRACE_LENGTH (64UL) 38 : /* https://github.com/anza-xyz/agave/blob/f70ab5598ccd86b216c3928e4397bf4a5b58d723/compute-budget/src/compute_budget.rs#L13 */ 39 12 : #define FD_MAX_INSTRUCTION_STACK_DEPTH (5UL) 40 : 41 : struct fd_exec_txn_ctx { 42 : ulong magic; /* ==FD_EXEC_TXN_CTX_MAGIC */ 43 : 44 : /* TODO: These are fields borrowed from the slot and epoch ctx. This 45 : could be refactored even further. Currently these fields are not 46 : all valid local joins within the scope of txn execution. */ 47 : 48 : uint flags; 49 : 50 : fd_bank_t * bank; 51 : 52 : /* All pointers starting here are valid local joins in txn execution. */ 53 : fd_features_t features; 54 : fd_txncache_t * status_cache; 55 : int enable_exec_recording; 56 : fd_bank_hash_cmp_t * bank_hash_cmp; 57 : fd_funk_txn_t * funk_txn; 58 : fd_funk_t funk[1]; 59 : ulong slot; 60 : ulong bank_idx; 61 : fd_txn_p_t txn; 62 : 63 : fd_spad_t * spad; /* Sized out to handle the worst case footprint of single transaction execution. */ 64 : fd_wksp_t * spad_wksp; /* Workspace for the spad. */ 65 : 66 : fd_compute_budget_details_t compute_budget_details; /* Compute budget details */ 67 : 68 : /* Fields below here are not guaranteed to be local joins in txn execution. */ 69 : 70 : ulong paid_fees; 71 : ulong loaded_accounts_data_size; /* The actual transaction loaded data size */ 72 : uint custom_err; /* When a custom error is returned, this is where the numeric value gets stashed */ 73 : uchar instr_stack_sz; /* Current depth of the instruction execution stack. */ 74 : fd_exec_instr_ctx_t instr_stack[FD_MAX_INSTRUCTION_STACK_DEPTH]; /* Instruction execution stack. */ 75 : fd_exec_instr_ctx_t * failed_instr; 76 : int instr_err_idx; 77 : /* During sanitization, v0 transactions are allowed to have up to 256 accounts: 78 : https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/sdk/program/src/message/versions/v0/mod.rs#L139 79 : Nonetheless, when Agave prepares a sanitized batch for execution and tries to lock accounts, a lower limit is enforced: 80 : https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118 81 : That is the limit we are going to use here. */ 82 : ulong accounts_cnt; /* Number of account pubkeys accessed by this transaction. */ 83 : fd_pubkey_t account_keys[ MAX_TX_ACCOUNT_LOCKS ]; /* Array of account pubkeys accessed by this transaction. */ 84 : ulong executable_cnt; /* Number of BPF upgradeable loader accounts. */ 85 : fd_txn_account_t executable_accounts[ MAX_TX_ACCOUNT_LOCKS ]; /* Array of BPF upgradeable loader program data accounts */ 86 : fd_txn_account_t accounts[ MAX_TX_ACCOUNT_LOCKS ]; /* Array of borrowed accounts accessed by this transaction. */ 87 : 88 : /* When a program is deployed or upgraded, we must queue it to be 89 : updated in the program cache (if it exists already) so that 90 : the cache entry's ELF / sBPF information can be updated for future 91 : executions. We keep an array of pubkeys for the transaction to 92 : track which programs need to be reverified. The actual queueing 93 : for reverification is done in the transaction finalization step. */ 94 : uchar programs_to_reverify_cnt; 95 : fd_pubkey_t programs_to_reverify[ MAX_TX_ACCOUNT_LOCKS ]; 96 : 97 : /* The next three fields describe Agave's "rollback" accounts, which 98 : are copies of the fee payer and (if applicable) nonce account. If the 99 : transaction fails to load, the fee payer is still debited the transaction fee, 100 : and the nonce account is advanced. The fee payer must also be rolled back to it's 101 : state pre-transaction, plus debited any transaction fees. 102 : 103 : This is a bit of a misnomer but Agave calls it "rollback". 104 : This is the account state that the nonce account should be in when 105 : the txn fails. 106 : It will advance the nonce account, rather than "roll back". 107 : */ 108 : fd_txn_account_t rollback_nonce_account[ 1 ]; 109 : ulong nonce_account_idx_in_txn; /* If the transaction has a nonce account that must be advanced, this would be !=ULONG_MAX. */ 110 : fd_txn_account_t rollback_fee_payer_account[ 1 ]; 111 : 112 : uint num_instructions; /* Counter for number of instructions in txn */ 113 : fd_txn_return_data_t return_data; /* Data returned from `return_data` syscalls */ 114 : ulong accounts_resize_delta; /* Transaction level tracking for account resizing */ 115 : fd_hash_t blake_txn_msg_hash; /* Hash of raw transaction message used by the status cache */ 116 : ulong execution_fee; /* Execution fee paid by the fee payer in the transaction */ 117 : ulong priority_fee; /* Priority fee paid by the fee payer in the transaction */ 118 : ulong collected_rent; /* Rent collected from accounts in this transaction */ 119 : 120 : uchar dirty_vote_acc : 1; /* 1 if this transaction maybe modified a vote account */ 121 : 122 : fd_capture_ctx_t * capture_ctx; 123 : 124 : /* The instr_infos for the entire transaction are allocated at the start of 125 : the transaction. However, this must preserve a different counter because 126 : the top level instructions must get set up at once. The instruction 127 : error check on a maximum instruction size can be done on the 128 : instr_info_cnt instead of the instr_trace_length because it is a proxy 129 : for the trace_length: the instr_info_cnt gets incremented faster than 130 : the instr_trace_length because it counts all of the top level instructions 131 : first. */ 132 : fd_instr_info_t instr_infos[FD_MAX_INSTRUCTION_TRACE_LENGTH]; 133 : ulong instr_info_cnt; 134 : 135 : /* These instr infos are statically allocated at the beginning of a transaction 136 : and are only written to / referred to within the VM. It's kept 137 : at the transaction level because syscalls like `GetProcessedSiblingInstruction()` 138 : may refer to instructions processed earlier in the transaction. */ 139 : fd_instr_info_t cpi_instr_infos[FD_MAX_INSTRUCTION_TRACE_LENGTH]; 140 : ulong cpi_instr_info_cnt; 141 : 142 : /* Each instr info within `instr_trace` may refer to an `instr_infos` or `cpi_instr_infos` 143 : entry. */ 144 : fd_exec_instr_trace_entry_t instr_trace[FD_MAX_INSTRUCTION_TRACE_LENGTH]; /* Instruction trace */ 145 : ulong instr_trace_length; /* Number of instructions in the trace */ 146 : 147 : fd_log_collector_t log_collector; /* Log collector instance */ 148 : 149 : /* Execution error and type, to match Agave. */ 150 : int exec_err; 151 : int exec_err_kind; 152 : 153 : /* The current instruction index being executed */ 154 : int current_instr_idx; 155 : 156 : /* fuzzing options */ 157 : struct { 158 : int enable_vm_tracing; 159 : } fuzz_config; 160 : }; 161 : 162 0 : #define FD_EXEC_TXN_CTX_ALIGN (alignof(fd_exec_txn_ctx_t)) 163 0 : #define FD_EXEC_TXN_CTX_FOOTPRINT ( sizeof(fd_exec_txn_ctx_t)) 164 0 : #define FD_EXEC_TXN_CTX_MAGIC (0x9AD93EE71469F4D7UL ) /* random */ 165 : 166 : FD_PROTOTYPES_BEGIN 167 : 168 : /* Error logging handholding assertions */ 169 : 170 : #ifdef FD_RUNTIME_ERR_HANDHOLDING 171 : 172 : /* Asserts that the error and error kind are not populated (zero) */ 173 : #define FD_TXN_TEST_ERR_OVERWRITE( txn_ctx ) \ 174 : FD_TEST( !txn_ctx->exec_err ); \ 175 : FD_TEST( !txn_ctx->exec_err_kind ) 176 : 177 : /* Used prior to a FD_TXN_ERR_FOR_LOG_INSTR call to deliberately 178 : bypass overwrite handholding checks. 179 : Only use this if you know what you're doing. */ 180 : #define FD_TXN_PREPARE_ERR_OVERWRITE( txn_ctx ) \ 181 : txn_ctx->exec_err = 0; \ 182 : txn_ctx->exec_err_kind = 0 183 : 184 : #else 185 : 186 0 : #define FD_TXN_TEST_ERR_OVERWRITE( txn_ctx ) ( ( void )0 ) 187 0 : #define FD_TXN_PREPARE_ERR_OVERWRITE( txn_ctx ) ( ( void )0 ) 188 : 189 : #endif 190 : 191 0 : #define FD_TXN_ERR_FOR_LOG_INSTR( txn_ctx, err, idx ) (__extension__({ \ 192 0 : FD_TXN_TEST_ERR_OVERWRITE( txn_ctx ); \ 193 0 : txn_ctx->exec_err = err; \ 194 0 : txn_ctx->exec_err_kind = FD_EXECUTOR_ERR_KIND_INSTR; \ 195 0 : txn_ctx->instr_err_idx = idx; \ 196 0 : })) 197 : 198 : void * 199 : fd_exec_txn_ctx_new( void * mem ); 200 : 201 : fd_exec_txn_ctx_t * 202 : fd_exec_txn_ctx_join( void * mem, fd_spad_t * spad, fd_wksp_t * spad_wksp ); 203 : 204 : void * 205 : fd_exec_txn_ctx_leave( fd_exec_txn_ctx_t * ctx ); 206 : 207 : void * 208 : fd_exec_txn_ctx_delete( void * mem ); 209 : 210 : /* Sets up a basic transaction ctx without a txn descriptor or txn raw. Useful 211 : for mocking transaction context objects for instructions. */ 212 : void 213 : fd_exec_txn_ctx_setup_basic( fd_exec_txn_ctx_t * ctx ); 214 : 215 : void 216 : fd_exec_txn_ctx_teardown( fd_exec_txn_ctx_t * txn_ctx ); 217 : 218 : /* Mirrors Agave function solana_sdk::transaction_context::find_index_of_account 219 : 220 : Backward scan over transaction accounts. 221 : Returns -1 if not found. 222 : 223 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L233-L238 */ 224 : 225 : static inline int 226 : fd_exec_txn_ctx_find_index_of_account( fd_exec_txn_ctx_t const * ctx, 227 0 : fd_pubkey_t const * pubkey ) { 228 0 : for( ulong i=ctx->accounts_cnt; i>0UL; i-- ) { 229 0 : if( 0==memcmp( pubkey, &ctx->account_keys[ i-1UL ], sizeof(fd_pubkey_t) ) ) { 230 0 : return (int)(i-1UL); 231 0 : } 232 0 : } 233 0 : return -1; 234 0 : } 235 : 236 : typedef int fd_txn_account_condition_fn_t ( fd_txn_account_t * acc, 237 : fd_exec_txn_ctx_t const * ctx, 238 : ushort idx ); 239 : 240 : /* Mirrors Agave function solana_sdk::transaction_context::get_account_at_index 241 : 242 : Takes a function pointer to a condition function to check pre-conditions on the 243 : obtained account. If the condition function is NULL, the account is returned without 244 : any pre-condition checks. 245 : 246 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L223-L230 */ 247 : 248 : int 249 : fd_exec_txn_ctx_get_account_at_index( fd_exec_txn_ctx_t * ctx, 250 : ushort idx, 251 : fd_txn_account_t * * account, 252 : fd_txn_account_condition_fn_t * condition ); 253 : 254 : /* A wrapper around fd_exec_txn_ctx_get_account_at_index that obtains an 255 : account from the transaction context by its pubkey. */ 256 : 257 : int 258 : fd_exec_txn_ctx_get_account_with_key( fd_exec_txn_ctx_t * ctx, 259 : fd_pubkey_t const * pubkey, 260 : fd_txn_account_t * * account, 261 : fd_txn_account_condition_fn_t * condition ); 262 : 263 : /* Gets an executable (program data) account via its pubkey. */ 264 : 265 : int 266 : fd_exec_txn_ctx_get_executable_account( fd_exec_txn_ctx_t * ctx, 267 : fd_pubkey_t const * pubkey, 268 : fd_txn_account_t * * account, 269 : fd_txn_account_condition_fn_t * condition ); 270 : 271 : /* Mirrors Agave function solana_sdk::transaction_context::get_key_of_account_at_index 272 : 273 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L212 */ 274 : 275 : int 276 : fd_exec_txn_ctx_get_key_of_account_at_index( fd_exec_txn_ctx_t * ctx, 277 : ushort idx, 278 : fd_pubkey_t const * * key ); 279 : 280 : void 281 : fd_exec_txn_ctx_reset_return_data( fd_exec_txn_ctx_t * ctx ); 282 : 283 : /* In agave, the writable accounts cache is populated by this below function. 284 : This cache is then referenced to determine if a transaction account is 285 : writable or not. 286 : 287 : The overall logic is as follows: an account can be passed 288 : in as writable based on the signature and readonly signature as they are 289 : passed in by the transaction message. However, the account's writable 290 : status will be demoted if either of the two conditions are met: 291 : 1. If the account is in the set of reserved pubkeys 292 : 2. If the account is the program id AND the upgradeable loader account is in 293 : the set of transaction accounts. */ 294 : /* https://github.com/anza-xyz/agave/blob/v2.1.1/sdk/program/src/message/versions/v0/loaded.rs#L137-L150 */ 295 : 296 : int 297 : fd_exec_txn_ctx_account_is_writable_idx( fd_exec_txn_ctx_t const * txn_ctx, ushort idx ); 298 : 299 : /* This flat function does the same as the function above, but uses the 300 : exact arguments needed instead of the full fd_exec_txn_ctx_t */ 301 : 302 : int 303 : fd_exec_txn_account_is_writable_idx_flat( const ulong slot, 304 : const ushort idx, 305 : const fd_pubkey_t * addr_at_idx, 306 : const fd_txn_t * txn_descriptor, 307 : const fd_features_t * features, 308 : const uint bpf_upgradeable_in_txn ); 309 : 310 : /* The bpf_upgradeable_in_txn argument of the above function can be 311 : obtained by the function below */ 312 : uint 313 : fd_txn_account_has_bpf_loader_upgradeable( const fd_pubkey_t * account_keys, 314 : const ulong accounts_cnt ); 315 : 316 : 317 : /* Account pre-condition filtering functions 318 : 319 : Used to filter accounts based on pre-conditions such as existence, is_writable, etc. 320 : when obtaining accounts from the transaction context. Passed as a function pointer. */ 321 : 322 : int 323 : fd_txn_account_check_exists( fd_txn_account_t * acc, 324 : fd_exec_txn_ctx_t const * ctx, 325 : ushort idx ); 326 : 327 : int 328 : fd_txn_account_check_is_writable( fd_txn_account_t * acc, 329 : fd_exec_txn_ctx_t const * ctx, 330 : ushort idx ); 331 : 332 : /* The fee payer is a valid modifiable account if it is passed in as writable 333 : in the message via a valid signature. We ignore if the account has been 334 : demoted or not (see fd_exec_txn_ctx_account_is_writable_idx) for more details. 335 : Agave and Firedancer will reject the fee payer if the transaction message 336 : doesn't have a writable signature. */ 337 : 338 : int 339 : fd_txn_account_check_fee_payer_writable( fd_txn_account_t * acc, 340 : fd_exec_txn_ctx_t const * ctx, 341 : ushort idx ); 342 : 343 : /* Checks if the account is mutable and borrows the account mutably. 344 : 345 : The borrow is an acquired write on the account object. 346 : The caller is responsible for releasing the write via 347 : fd_txn_account_release_write. 348 : 349 : TODO: Agave doesn't need to check if the account is mutable 350 : because it uses Writable/Readable traits for accounts. We 351 : should have a similar concept to abstract away fd_txn_account_t's 352 : const_meta and meta fields. */ 353 : 354 : int 355 : fd_txn_account_check_borrow_mut( fd_txn_account_t * acc, 356 : fd_exec_txn_ctx_t const * ctx, 357 : ushort idx ); 358 : 359 : 360 : FD_PROTOTYPES_END 361 : 362 : #endif /* HEADER_fd_src_flamenco_runtime_context_fd_exec_txn_ctx_h */