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