Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_runtime_fd_borrowed_account_h 2 : #define HEADER_fd_src_flamenco_runtime_fd_borrowed_account_h 3 : 4 : #include "fd_executor_err.h" 5 : #include "fd_system_ids.h" 6 : #include "fd_runtime.h" 7 : #include "context/fd_exec_txn_ctx.h" 8 : #include "sysvar/fd_sysvar_rent.h" 9 : 10 : /* FD_ACC_SZ_MAX is the hardcoded size limit of a Solana account. */ 11 : 12 0 : #define MAX_PERMITTED_DATA_LENGTH (10UL<<20) /* 10MiB */ 13 : #define MAX_PERMITTED_ACCOUNT_DATA_ALLOCS_PER_TXN (10L<<21 ) /* 20MiB */ 14 : 15 : /* TODO: Not all Agave Borrowed Account API functions are implemented here */ 16 : 17 : /* TODO: check that borrow is active when calling these APIs */ 18 : 19 : struct fd_borrowed_account { 20 : ulong magic; 21 : fd_txn_account_t * acct; 22 : fd_exec_instr_ctx_t const * instr_ctx; 23 : 24 : /* index_in_instruction will be USHORT_MAX for borrowed program accounts because 25 : they are not stored in the list of instruction accounts in the instruction context */ 26 : ushort index_in_instruction; 27 : }; 28 : typedef struct fd_borrowed_account fd_borrowed_account_t; 29 : 30 0 : #define FD_BORROWED_ACCOUNT_MAGIC (0xFDB07703ACC736C0UL) /* FD BORROW ACCT MGC version 0 */ 31 : /* prevents borrowed accounts from going out of scope without releasing a borrow */ 32 0 : #define fd_guarded_borrowed_account_t __attribute__((cleanup(fd_borrowed_account_destroy))) fd_borrowed_account_t 33 : 34 : FD_PROTOTYPES_BEGIN 35 : 36 : /* Constructor */ 37 : 38 : static inline void 39 : fd_borrowed_account_init( fd_borrowed_account_t * borrowed_acct, 40 : fd_txn_account_t * acct, 41 : fd_exec_instr_ctx_t const * instr_ctx, 42 0 : ushort index_in_instruction ) { 43 0 : borrowed_acct->acct = acct; 44 0 : borrowed_acct->instr_ctx = instr_ctx; 45 0 : borrowed_acct->index_in_instruction = index_in_instruction; 46 : 47 0 : FD_COMPILER_MFENCE(); 48 0 : borrowed_acct->magic = FD_BORROWED_ACCOUNT_MAGIC; 49 0 : FD_COMPILER_MFENCE(); 50 0 : } 51 : 52 : /* Drop mirrors the behavior of rust's std::mem::drop on mutable borrows. 53 : Releases the acquired write on the borrowed account object. */ 54 : 55 : static inline void 56 0 : fd_borrowed_account_drop( fd_borrowed_account_t * borrowed_acct ) { 57 0 : borrowed_acct->acct->vt->drop( borrowed_acct->acct ); 58 0 : } 59 : 60 : /* Destructor */ 61 : 62 : static inline void 63 0 : fd_borrowed_account_destroy( fd_borrowed_account_t * borrowed_acct ) { 64 0 : if( FD_LIKELY( borrowed_acct->magic == FD_BORROWED_ACCOUNT_MAGIC ) ) { 65 0 : fd_borrowed_account_drop( borrowed_acct ); 66 0 : } 67 : 68 0 : FD_COMPILER_MFENCE(); 69 0 : FD_VOLATILE( borrowed_acct->magic ) = 0UL; 70 0 : FD_COMPILER_MFENCE(); 71 : 72 0 : borrowed_acct = NULL; 73 0 : } 74 : 75 : /* Getters */ 76 : 77 : /* fd_borrowed_account_get_data mirrors Agave function 78 : solana_sdk::transaction_context::BorrowedAccount::get_data. 79 : 80 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L817 */ 81 : 82 : static inline uchar const * 83 0 : fd_borrowed_account_get_data( fd_borrowed_account_t const * borrowed_acct ) { 84 0 : return borrowed_acct->acct->vt->get_data( borrowed_acct->acct ); 85 0 : } 86 : 87 : static inline ulong 88 0 : fd_borrowed_account_get_data_len( fd_borrowed_account_t const * borrowed_acct ) { 89 0 : return borrowed_acct->acct->vt->get_data_len( borrowed_acct->acct ); 90 0 : } 91 : 92 : /* fd_borrowed_account_get_data_mut mirrors Agave function 93 : solana_sdk::transaction_context::BorrowedAccount::get_data_mut. 94 : 95 : Returns a writable slice of the account data (transaction wide). 96 : Acquires a writable handle. This function assumes that the relevant 97 : borrowed has already acquired exclusive write access. 98 : 99 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L823 */ 100 : 101 : int 102 : fd_borrowed_account_get_data_mut( fd_borrowed_account_t * borrowed_acct, 103 : uchar * * data_out, 104 : ulong * dlen_out ); 105 : 106 : static inline fd_pubkey_t const * 107 0 : fd_borrowed_account_get_owner( fd_borrowed_account_t const * borrowed_acct ) { 108 0 : return borrowed_acct->acct->vt->get_owner( borrowed_acct->acct ); 109 0 : } 110 : 111 : /* fd_borrowed_account_get_lamports mirrors Agave function 112 : solana_sdk::transaction_context::BorrowedAccount::get_lamports. 113 : 114 : Returns current number of lamports in account. Well behaved if meta 115 : is NULL. 116 : 117 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L767 */ 118 : 119 : static inline ulong 120 0 : fd_borrowed_account_get_lamports( fd_borrowed_account_t const * borrowed_acct ) { 121 0 : return borrowed_acct->acct->vt->get_lamports( borrowed_acct->acct ); 122 0 : } 123 : 124 : /* fd_borrowed_account_get_rent_epoch mirrors Agave function 125 : solana_sdk::transaction_context::BorrowedAccount::get_rent_epoch. 126 : 127 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1034 */ 128 : 129 : static inline ulong 130 0 : fd_borrowed_account_get_rent_epoch( fd_borrowed_account_t const * borrowed_acct ) { 131 0 : return borrowed_acct->acct->vt->get_rent_epoch( borrowed_acct->acct ); 132 0 : } 133 : 134 : static inline fd_account_meta_t const * 135 0 : fd_borrowed_account_get_acc_meta( fd_borrowed_account_t const * borrowed_acct ) { 136 0 : return borrowed_acct->acct->vt->get_meta( borrowed_acct->acct ); 137 0 : } 138 : 139 : /* Setters */ 140 : 141 : /* fd_borrowed_account_set_owner mirrors Agave function 142 : solana_sdk::transaction_context::BorrowedAccount::set_owner. 143 : 144 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L739 */ 145 : 146 : int 147 : fd_borrowed_account_set_owner( fd_borrowed_account_t * borrowed_acct, 148 : fd_pubkey_t const * owner ); 149 : 150 : /* fd_borrowed_account_set_lamports mirrors Agave function 151 : solana_sdk::transaction_context::BorrowedAccount::set_lamports. 152 : 153 : Runs through a sequence of permission checks, then sets the account 154 : balance. Does not update global capitalization. On success, returns 155 : 0 and updates meta->lamports. On failure, returns an 156 : FD_EXECUTOR_INSTR_ERR_{...} code. Acquires a writable handle. 157 : 158 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L773 */ 159 : 160 : int 161 : fd_borrowed_account_set_lamports( fd_borrowed_account_t * borrowed_acct, 162 : ulong lamports ); 163 : 164 : /* fd_borrowed_account_set_data_from_slice mirrors Agave function 165 : solana_sdk::transaction_context::BorrowedAccount::set_data_from_slice. 166 : 167 : In the firedancer client, it also mirrors the Agave function 168 : solana_sdk::transaction_context::BorrowedAccount::set_data. 169 : Assumes that destination account already has enough space to fit 170 : data. Acquires a writable handle. 171 : 172 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L864 */ 173 : 174 : int 175 : fd_borrowed_account_set_data_from_slice( fd_borrowed_account_t * borrowed_acct, 176 : uchar const * data, 177 : ulong data_sz ); 178 : 179 : /* fd_borrowed_account_set_data_length mirrors Agave function 180 : solana_sdk::transaction_context::BorrowedAccount::set_data_length. 181 : 182 : Acquires a writable handle. Returns 0 on success. 183 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L882 */ 184 : 185 : int 186 : fd_borrowed_account_set_data_length( fd_borrowed_account_t * borrowed_acct, 187 : ulong new_len ); 188 : 189 : /* fd_borrowed_account_set_executable mirrors Agave function 190 : solana_sdk::transaction_context::BorrowedAccount::set_executable. 191 : 192 : Returns FD_EXECUTOR_INSTR_SUCCESS if the set is successful. 193 : 194 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L10015 */ 195 : 196 : int 197 : fd_borrowed_account_set_executable( fd_borrowed_account_t * borrowed_acct, 198 : int is_executable ); 199 : 200 : /* Operators */ 201 : 202 : /* fd_borrowed_account_checked_add_lamports mirros Agave function 203 : solana_sdk::transaction_context::BorrowedAccount::checked_add_lamports. 204 : 205 : Does not update global capitalization. Returns 0 on 206 : success or an FD_EXECUTOR_INSTR_ERR_{...} code on failure. 207 : Gracefully handles underflow. 208 : 209 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L797 */ 210 : 211 : static inline int 212 : fd_borrowed_account_checked_add_lamports( fd_borrowed_account_t * borrowed_acct, 213 0 : ulong lamports ) { 214 0 : ulong balance_post = 0UL; 215 0 : int err = fd_ulong_checked_add( borrowed_acct->acct->vt->get_lamports( borrowed_acct->acct ), 216 0 : lamports, 217 0 : &balance_post ); 218 0 : if( FD_UNLIKELY( err ) ) { 219 0 : return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW; 220 0 : } 221 : 222 0 : return fd_borrowed_account_set_lamports( borrowed_acct, balance_post ); 223 0 : } 224 : 225 : /* fd_borrowed_account_checked_sub_lamports mirrors Agave function 226 : solana_sdk::transaction_context::BorrowedAccount::checked_sub_lamports. 227 : 228 : Does not update global capitalization. Returns 0 on 229 : success or an FD_EXECUTOR_INSTR_ERR_{...} code on failure. 230 : Gracefully handles underflow. 231 : 232 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L807 */ 233 : 234 : static inline int 235 : fd_borrowed_account_checked_sub_lamports( fd_borrowed_account_t * borrowed_acct, 236 0 : ulong lamports ) { 237 0 : ulong balance_post = 0UL; 238 0 : int err = fd_ulong_checked_sub( borrowed_acct->acct->vt->get_lamports( borrowed_acct->acct ), 239 0 : lamports, 240 0 : &balance_post ); 241 0 : if( FD_UNLIKELY( err ) ) { 242 0 : return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW; 243 0 : } 244 : 245 0 : return fd_borrowed_account_set_lamports( borrowed_acct, balance_post ); 246 0 : } 247 : 248 : /* fd_borrowed_account_update_acounts_resize_delta mirrors Agave function 249 : solana_sdk::transaction_context:BorrowedAccount::update_accounts_resize_delta. 250 : 251 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1123 */ 252 : 253 : int 254 : fd_borrowed_account_update_accounts_resize_delta( fd_borrowed_account_t * borrowed_acct, 255 : ulong new_len, 256 : int * err ); 257 : 258 : /* Accessors */ 259 : 260 : /* fd_borrowed_account_is_rent_exempt_at_data_length mirrors Agave function 261 : solana_sdk::transaction_context::BorrowedAccount::is_rent_exempt_at_data_length. 262 : 263 : Returns 1 if an account is rent exempt at it's current data length. 264 : 265 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L987 */ 266 : 267 : static inline int 268 0 : fd_borrowed_account_is_rent_exempt_at_data_length( fd_borrowed_account_t const * borrowed_acct ) { 269 0 : fd_txn_account_t * acct = borrowed_acct->acct; 270 0 : if( FD_UNLIKELY( ( acct->vt->get_meta( acct ) == NULL ) ) ) FD_LOG_ERR(( "account is not setup" )); 271 0 : 272 0 : /* TODO: Add an is_exempt rent API to better match Agave and clean up code 273 0 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L990 */ 274 0 : fd_rent_t const * rent = fd_bank_rent_query( borrowed_acct->instr_ctx->txn_ctx->bank ); 275 0 : ulong min_balance = fd_rent_exempt_minimum_balance( rent, acct->vt->get_data_len( acct ) ); 276 0 : return acct->vt->get_lamports( acct ) >= min_balance; 277 0 : } 278 : 279 : /* fd_borrowed_account_is_executable mirrors Agave function 280 : solana_sdk::transaction_context::BorrowedAccount::is_executable. 281 : 282 : Returns 1 if the given account has the 283 : executable flag set. Otherwise, returns 0. 284 : 285 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L995 */ 286 : 287 : FD_FN_PURE static inline int 288 0 : fd_borrowed_account_is_executable( fd_borrowed_account_t const * borrowed_acct ) { 289 0 : return borrowed_acct->acct->vt->is_executable( borrowed_acct->acct ); 290 0 : } 291 : 292 : /* fd_borrowed_account_is_executable_internal is a private function for deprecating the `is_executable` flag. 293 : It returns true if the `remove_accounts_executable_flag_checks` feature is inactive AND fd_account_is_executable 294 : return true. This is newly used in account modification logic to eventually allow "executable" accounts to be 295 : modified. 296 : 297 : https://github.com/anza-xyz/agave/blob/89872fdb074e6658646b2b57a299984f0059cc84/sdk/transaction-context/src/lib.rs#L1052-L1060 */ 298 : 299 : FD_FN_PURE static inline int 300 0 : fd_borrowed_account_is_executable_internal( fd_borrowed_account_t const * borrowed_acct ) { 301 0 : return !FD_FEATURE_ACTIVE( borrowed_acct->instr_ctx->txn_ctx->slot, &borrowed_acct->instr_ctx->txn_ctx->features, remove_accounts_executable_flag_checks ) && 302 0 : fd_borrowed_account_is_executable( borrowed_acct ); 303 0 : } 304 : 305 : FD_FN_PURE static inline int 306 0 : fd_borrowed_account_is_mutable( fd_borrowed_account_t const * borrowed_acct ) { 307 0 : return borrowed_acct->acct->vt->is_mutable( borrowed_acct->acct ); 308 0 : } 309 : 310 : /* fd_borrowed_account_is_signer mirrors the Agave function 311 : solana_sdk::transaction_context::BorrowedAccount::is_signer. 312 : Returns 1 if the account is a signer or is writable and 0 otherwise. 313 : 314 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1039 */ 315 : 316 : static inline int 317 0 : fd_borrowed_account_is_signer( fd_borrowed_account_t const * borrowed_acct ) { 318 0 : fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx; 319 0 : fd_instr_info_t const * instr = instr_ctx->instr; 320 : 321 0 : if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) { 322 0 : return 0; 323 0 : } 324 : 325 0 : return fd_instr_acc_is_signer_idx( instr, borrowed_acct->index_in_instruction ); 326 0 : } 327 : 328 : /* fd_borrowed_account_is_writer mirrors the Agave function 329 : solana_sdk::transaction_context::BorrowedAccount::is_writer. 330 : Returns 1 if the account is a signer or is writable and 0 otherwise. 331 : 332 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1052 */ 333 : 334 : static inline int 335 0 : fd_borrowed_account_is_writable( fd_borrowed_account_t const * borrowed_acct ) { 336 0 : fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx; 337 0 : fd_instr_info_t const * instr = instr_ctx->instr; 338 : 339 0 : if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) { 340 0 : return 0; 341 0 : } 342 : 343 0 : return fd_instr_acc_is_writable_idx( instr, borrowed_acct->index_in_instruction ); 344 0 : } 345 : 346 : /* fd_borrowed_account_is_owned_by_current_program mirrors Agave's 347 : solana_sdk::transaction_context::BorrowedAccount::is_owned_by_current_program. 348 : 349 : Returns 1 if the given 350 : account is owned by the program invoked in the current instruction. 351 : Otherwise, returns 0. 352 : 353 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1065 */ 354 : 355 : FD_FN_PURE static inline int 356 0 : fd_borrowed_account_is_owned_by_current_program( fd_borrowed_account_t const * borrowed_acct ) { 357 0 : fd_pubkey_t const * program_id_pubkey = NULL; 358 0 : int err = fd_exec_instr_ctx_get_last_program_key( borrowed_acct->instr_ctx, &program_id_pubkey ); 359 0 : if( FD_UNLIKELY( err ) ) { 360 0 : return 0; 361 0 : } 362 : 363 0 : return memcmp( program_id_pubkey->key, 364 0 : borrowed_acct->acct->vt->get_owner( borrowed_acct->acct ), sizeof(fd_pubkey_t) ) == 0; 365 0 : } 366 : 367 : /* fd_borrowed_account_can_data_be changed mirrors Agave function 368 : solana_sdk::transaction_context::BorrowedAccount::can_data_be_changed. 369 : 370 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1074 */ 371 : 372 : static inline int 373 : fd_borrowed_account_can_data_be_changed( fd_borrowed_account_t const * borrowed_acct, 374 0 : int * err ) { 375 : /* Only non-executable accounts data can be changed 376 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1076 */ 377 0 : if( FD_UNLIKELY( fd_borrowed_account_is_executable_internal( borrowed_acct ) ) ) { 378 0 : *err = FD_EXECUTOR_INSTR_ERR_EXECUTABLE_DATA_MODIFIED; 379 0 : return 0; 380 0 : } 381 : 382 : /* And only if the account is writable 383 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1080 */ 384 0 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( borrowed_acct ) ) ) { 385 0 : *err = FD_EXECUTOR_INSTR_ERR_READONLY_DATA_MODIFIED; 386 0 : return 0; 387 0 : } 388 : 389 : /* And only if we are the owner 390 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1084 */ 391 0 : if( FD_UNLIKELY( !fd_borrowed_account_is_owned_by_current_program( borrowed_acct ) ) ) { 392 0 : *err = FD_EXECUTOR_INSTR_ERR_EXTERNAL_DATA_MODIFIED; 393 0 : return 0; 394 0 : } 395 : 396 0 : *err = FD_EXECUTOR_INSTR_SUCCESS; 397 0 : return 1; 398 0 : } 399 : 400 : /* fd_borrowed_account_can_data_be_resized mirrors Agave function 401 : solana_sdk::transaction_context::BorrowedAccount::can_data_be_resized 402 : 403 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1092 */ 404 : 405 : static inline int 406 : fd_borrowed_account_can_data_be_resized( fd_borrowed_account_t const * borrowed_acct, 407 : ulong new_length, 408 0 : int * err ) { 409 0 : fd_txn_account_t * acct = borrowed_acct->acct; 410 : 411 : /* Only the owner can change the length of the data 412 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1095 */ 413 0 : if( FD_UNLIKELY( ( acct->vt->get_data_len( acct ) != new_length ) & 414 0 : ( !fd_borrowed_account_is_owned_by_current_program( borrowed_acct ) ) ) ) { 415 0 : *err = FD_EXECUTOR_INSTR_ERR_ACC_DATA_SIZE_CHANGED; 416 0 : return 0; 417 0 : } 418 : 419 : /* The new length can not exceed the maximum permitted length 420 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1099 */ 421 0 : if( FD_UNLIKELY( new_length>MAX_PERMITTED_DATA_LENGTH ) ) { 422 0 : *err = FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC; 423 0 : return 0; 424 0 : } 425 : 426 : /* The resize can not exceed the per-transaction maximum 427 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1104-L1108 */ 428 0 : long length_delta = fd_long_sat_sub( (long)new_length, (long)acct->vt->get_data_len( acct ) ); 429 0 : long new_accounts_resize_delta = fd_long_sat_add( (long)borrowed_acct->instr_ctx->txn_ctx->accounts_resize_delta, length_delta ); 430 0 : if( FD_UNLIKELY( new_accounts_resize_delta > MAX_PERMITTED_ACCOUNT_DATA_ALLOCS_PER_TXN ) ) { 431 0 : *err = FD_EXECUTOR_INSTR_ERR_MAX_ACCS_DATA_ALLOCS_EXCEEDED; 432 0 : return 0; 433 0 : } 434 : 435 0 : *err = FD_EXECUTOR_INSTR_SUCCESS; 436 0 : return 1; 437 0 : } 438 : 439 : FD_FN_PURE static inline int 440 0 : fd_borrowed_account_is_zeroed( fd_borrowed_account_t const * borrowed_acct ) { 441 0 : fd_txn_account_t * acct = borrowed_acct->acct; 442 : /* TODO: optimize this */ 443 0 : uchar const * data = acct->vt->get_data( acct ); 444 0 : for( ulong i=0UL; i < acct->vt->get_data_len( acct ); i++ ) 445 0 : if( data[i] != 0 ) 446 0 : return 0; 447 0 : return 1; 448 0 : } 449 : 450 : FD_PROTOTYPES_END 451 : 452 : #endif /* HEADER_fd_src_flamenco_runtime_fd_borrowed_account_h */