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 "context/fd_exec_instr_ctx.h" 7 : #include "sysvar/fd_sysvar_rent.h" 8 : #include "program/fd_program_util.h" 9 : 10 288 : #define MAX_PERMITTED_DATA_LENGTH (FD_RUNTIME_ACC_SZ_MAX) /* 10MiB */ 11 : #define MAX_PERMITTED_ACCOUNT_DATA_ALLOCS_PER_TXN (10L<<21) /* 20MiB */ 12 : 13 : /* Kept equal to the canonical constant in fd_runtime_const.h, which the 14 : bpf loader serialization buffer bound relies on. */ 15 : FD_STATIC_ASSERT( MAX_PERMITTED_ACCOUNT_DATA_ALLOCS_PER_TXN==FD_RUNTIME_ACC_DATA_GROWTH_MAX_PER_TXN, account_data_growth_cap ); 16 : 17 : /* TODO: Not all Agave Borrowed Account API functions are implemented here */ 18 : 19 : /* TODO: check that borrow is active when calling these APIs */ 20 : 21 : struct fd_borrowed_account { 22 : fd_acc_t * acc; 23 : fd_exec_instr_ctx_t const * instr_ctx; 24 : 25 : /* index_in_instruction will be USHORT_MAX for borrowed program accounts because 26 : they are not stored in the list of instruction accounts in the instruction context */ 27 : ushort index_in_instruction; 28 : 29 : ulong * refcnt; 30 : 31 : /* The transaction's touched flag for this account. */ 32 : uchar * touched; 33 : }; 34 : 35 : typedef struct fd_borrowed_account fd_borrowed_account_t; 36 : 37 : /* prevents borrowed accounts from going out of scope without releasing a borrow */ 38 : /* TODO: Remove ... */ 39 247047 : #define fd_guarded_borrowed_account_t __attribute__((cleanup(fd_borrowed_account_drop))) fd_borrowed_account_t 40 : 41 : FD_PROTOTYPES_BEGIN 42 : 43 : static inline void 44 : fd_borrowed_account_init( fd_borrowed_account_t * borrowed_acct, 45 : fd_acc_t * acc, 46 : fd_exec_instr_ctx_t const * instr_ctx, 47 : ushort index_in_instruction, 48 : ulong * refcnt, 49 247544 : uchar * touched ) { 50 247544 : borrowed_acct->acc = acc; 51 247544 : borrowed_acct->instr_ctx = instr_ctx; 52 247544 : borrowed_acct->index_in_instruction = index_in_instruction; 53 247544 : borrowed_acct->refcnt = refcnt; 54 247544 : borrowed_acct->touched = touched; 55 247544 : } 56 : 57 : /* Drop mirrors the behavior of rust's std::mem::drop on mutable borrows. 58 : Releases the acquired write on the borrowed account object. */ 59 : 60 : static inline void 61 260272 : fd_borrowed_account_drop( fd_borrowed_account_t * borrowed_acct ) { 62 260272 : if( FD_LIKELY( borrowed_acct->refcnt ) ) *borrowed_acct->refcnt = 0; 63 260272 : borrowed_acct->refcnt = NULL; 64 260272 : } 65 : 66 : /* Getters */ 67 : 68 : /* fd_borrowed_account_get_data mirrors Agave function 69 : solana_sdk::transaction_context::BorrowedAccount::get_data. 70 : 71 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L817 */ 72 : 73 : static inline uchar const * 74 191138 : fd_borrowed_account_get_data( fd_borrowed_account_t const * borrowed_acct ) { 75 191138 : return borrowed_acct->acc->data; 76 191138 : } 77 : 78 : static inline ulong 79 208251 : fd_borrowed_account_get_data_len( fd_borrowed_account_t const * borrowed_acct ) { 80 208251 : return borrowed_acct->acc->data_len; 81 208251 : } 82 : 83 : /* fd_borrowed_account_get_data_mut mirrors Agave function 84 : solana_sdk::transaction_context::BorrowedAccount::get_data_mut. 85 : 86 : Returns a writable slice of the account data (transaction wide). 87 : Assumes the caller already holds the account's mutable borrow. 88 : Returns 0 on success or an FD_EXECUTOR_INSTR_ERR_{...} code if the 89 : data cannot be changed. 90 : 91 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L823 */ 92 : 93 : int 94 : fd_borrowed_account_get_data_mut( fd_borrowed_account_t * borrowed_acct, 95 : uchar * * data_out, 96 : ulong * dlen_out ); 97 : 98 : static inline fd_pubkey_t const * 99 46489 : fd_borrowed_account_get_owner( fd_borrowed_account_t const * borrowed_acct ) { 100 46489 : return (fd_pubkey_t const *)borrowed_acct->acc->owner; 101 46489 : } 102 : 103 : /* fd_borrowed_account_get_lamports mirrors Agave function 104 : solana_sdk::transaction_context::BorrowedAccount::get_lamports. 105 : 106 : Returns the current number of lamports in the account. 107 : 108 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L767 */ 109 : 110 : static inline ulong 111 38214 : fd_borrowed_account_get_lamports( fd_borrowed_account_t const * borrowed_acct ) { 112 38214 : return borrowed_acct->acc->lamports; 113 38214 : } 114 : 115 : /* Setters */ 116 : 117 : /* fd_borrowed_account_set_owner mirrors Agave function 118 : solana_sdk::transaction_context::BorrowedAccount::set_owner. 119 : 120 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L739 */ 121 : 122 : int 123 : fd_borrowed_account_set_owner( fd_borrowed_account_t * borrowed_acct, 124 : fd_pubkey_t const * owner ); 125 : 126 : /* fd_borrowed_account_set_lamports mirrors Agave function 127 : solana_sdk::transaction_context::BorrowedAccount::set_lamports. 128 : 129 : Runs through a sequence of permission checks, then sets the account 130 : balance. Does not update global capitalization. On success, returns 131 : 0 and updates acc->lamports. On failure, returns an 132 : FD_EXECUTOR_INSTR_ERR_{...} code. Assumes the caller already holds 133 : the account's mutable borrow. 134 : 135 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L773 */ 136 : 137 : int 138 : fd_borrowed_account_set_lamports( fd_borrowed_account_t * borrowed_acct, 139 : ulong lamports ); 140 : 141 : /* fd_borrowed_account_set_data_from_slice mirrors Agave function 142 : solana_transaction_context::instruction_accounts::BorrowedInstructionAccount::set_data_from_slice. 143 : 144 : Firedancer account storage is preallocated, so the destination 145 : account must already have enough space to fit data. Assumes the 146 : caller already holds the account's mutable borrow. 147 : 148 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/transaction-context/src/instruction_accounts.rs#L177-L192 */ 149 : 150 : int 151 : fd_borrowed_account_set_data_from_slice( fd_borrowed_account_t * borrowed_acct, 152 : uchar const * data, 153 : ulong data_sz ); 154 : 155 : /* fd_borrowed_account_set_data_length mirrors Agave function 156 : solana_transaction_context::instruction_accounts::BorrowedInstructionAccount::set_data_length. 157 : 158 : Assumes the caller already holds the account's mutable borrow. 159 : Returns 0 on success. 160 : 161 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/transaction-context/src/instruction_accounts.rs#L194-L207 */ 162 : 163 : int 164 : fd_borrowed_account_set_data_length( fd_borrowed_account_t * borrowed_acct, 165 : ulong new_len ); 166 : 167 : /* fd_borrowed_account_set_executable mirrors Agave function 168 : solana_sdk::transaction_context::BorrowedAccount::set_executable. 169 : 170 : Returns FD_EXECUTOR_INSTR_SUCCESS if the set is successful. 171 : 172 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1001 */ 173 : 174 : int 175 : fd_borrowed_account_set_executable( fd_borrowed_account_t * borrowed_acct, 176 : int is_executable ); 177 : 178 : /* Operators */ 179 : 180 : /* fd_borrowed_account_checked_add_lamports mirrors Agave function 181 : solana_sdk::transaction_context::BorrowedAccount::checked_add_lamports. 182 : 183 : Does not update global capitalization. Returns 0 on 184 : success or an FD_EXECUTOR_INSTR_ERR_{...} code on failure. 185 : Gracefully handles overflow. 186 : 187 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L797 */ 188 : 189 : static inline int 190 : fd_borrowed_account_checked_add_lamports( fd_borrowed_account_t * borrowed_acct, 191 2245 : ulong lamports ) { 192 2245 : ulong balance_post = 0UL; 193 2245 : int err = fd_ulong_checked_add( borrowed_acct->acc->lamports, 194 2245 : lamports, 195 2245 : &balance_post ); 196 2245 : if( FD_UNLIKELY( err ) ) { 197 9 : return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW; 198 9 : } 199 : 200 2236 : return fd_borrowed_account_set_lamports( borrowed_acct, balance_post ); 201 2245 : } 202 : 203 : /* fd_borrowed_account_checked_sub_lamports mirrors Agave function 204 : solana_sdk::transaction_context::BorrowedAccount::checked_sub_lamports. 205 : 206 : Does not update global capitalization. Returns 0 on 207 : success or an FD_EXECUTOR_INSTR_ERR_{...} code on failure. 208 : Gracefully handles underflow. 209 : 210 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L807 */ 211 : 212 : static inline int 213 : fd_borrowed_account_checked_sub_lamports( fd_borrowed_account_t * borrowed_acct, 214 1618 : ulong lamports ) { 215 1618 : ulong balance_post = 0UL; 216 1618 : int err = fd_ulong_checked_sub( borrowed_acct->acc->lamports, 217 1618 : lamports, 218 1618 : &balance_post ); 219 1618 : if( FD_UNLIKELY( err ) ) { 220 0 : return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW; 221 0 : } 222 : 223 1618 : return fd_borrowed_account_set_lamports( borrowed_acct, balance_post ); 224 1618 : } 225 : 226 : /* fd_borrowed_account_update_accounts_resize_delta mirrors Agave function 227 : solana_sdk::transaction_context::BorrowedAccount::update_accounts_resize_delta. 228 : 229 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1123 */ 230 : 231 : int 232 : fd_borrowed_account_update_accounts_resize_delta( fd_borrowed_account_t * borrowed_acct, 233 : ulong new_len, 234 : int * err ); 235 : 236 : /* Accessors */ 237 : 238 : /* fd_borrowed_account_is_rent_exempt_at_data_length mirrors Agave function 239 : solana_sdk::transaction_context::BorrowedAccount::is_rent_exempt_at_data_length. 240 : 241 : Returns 1 if an account is rent exempt at its current data length. 242 : 243 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L987 */ 244 : 245 : static inline int 246 0 : fd_borrowed_account_is_rent_exempt_at_data_length( fd_borrowed_account_t const * borrowed_acct ) { 247 0 : if( FD_UNLIKELY( !borrowed_acct->acc ) ) FD_LOG_ERR(( "account is not setup" )); 248 0 : 249 0 : /* TODO: Add an is_exempt rent API to better match Agave and clean up code 250 0 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L990 */ 251 0 : fd_rent_t const * rent = &borrowed_acct->instr_ctx->bank->f.rent; 252 0 : ulong min_balance = fd_rent_exempt_minimum_balance( rent, borrowed_acct->acc->data_len ); 253 0 : return borrowed_acct->acc->lamports>=min_balance; 254 0 : } 255 : 256 : /* fd_borrowed_account_is_executable mirrors Agave function 257 : solana_sdk::transaction_context::BorrowedAccount::is_executable. 258 : 259 : Returns 1 if the given account has the 260 : executable flag set. Otherwise, returns 0. 261 : 262 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L995 */ 263 : 264 : FD_FN_PURE static inline int 265 2815 : fd_borrowed_account_is_executable( fd_borrowed_account_t const * borrowed_acct ) { 266 2815 : return borrowed_acct->acc->executable; 267 2815 : } 268 : 269 : /* fd_borrowed_account_is_signer mirrors the Agave function 270 : solana_sdk::transaction_context::BorrowedAccount::is_signer. 271 : 272 : Returns 1 if the account is a signer of the current instruction. 273 : Otherwise, returns 0, including when the account is not an 274 : instruction account (index_in_instruction out of bounds). 275 : 276 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1039 */ 277 : 278 : static inline int 279 0 : fd_borrowed_account_is_signer( fd_borrowed_account_t const * borrowed_acct ) { 280 0 : fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx; 281 0 : fd_instr_info_t const * instr = instr_ctx->instr; 282 0 : 283 0 : if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) { 284 0 : return 0; 285 0 : } 286 0 : 287 0 : return fd_instr_acc_is_signer_idx( instr, borrowed_acct->index_in_instruction, NULL ); 288 0 : } 289 : 290 : /* fd_borrowed_account_is_writable mirrors the Agave function 291 : solana_sdk::transaction_context::BorrowedAccount::is_writable. 292 : 293 : Returns 1 if the account is writable in the current instruction. 294 : Otherwise, returns 0, including when the account is not an 295 : instruction account (index_in_instruction out of bounds). 296 : 297 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1052 */ 298 : 299 : static inline int 300 79661 : fd_borrowed_account_is_writable( fd_borrowed_account_t const * borrowed_acct ) { 301 79661 : fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx; 302 79661 : fd_instr_info_t const * instr = instr_ctx->instr; 303 : 304 79661 : if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) { 305 0 : return 0; 306 0 : } 307 : 308 79661 : return fd_instr_acc_is_writable_idx( instr, borrowed_acct->index_in_instruction ); 309 79661 : } 310 : 311 : /* fd_borrowed_account_is_owned_by_current_program mirrors Agave's 312 : solana_sdk::transaction_context::BorrowedAccount::is_owned_by_current_program. 313 : 314 : Returns 1 if the given 315 : account is owned by the program invoked in the current instruction. 316 : Otherwise, returns 0. 317 : 318 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1065 */ 319 : 320 : FD_FN_PURE static inline int 321 58789 : fd_borrowed_account_is_owned_by_current_program( fd_borrowed_account_t const * borrowed_acct ) { 322 58789 : fd_pubkey_t const * program_id_pubkey = NULL; 323 58789 : int err = fd_exec_instr_ctx_get_last_program_key( borrowed_acct->instr_ctx, &program_id_pubkey ); 324 58789 : if( FD_UNLIKELY( err ) ) { 325 0 : return 0; 326 0 : } 327 : 328 58789 : return !memcmp( program_id_pubkey->key, borrowed_acct->acc->owner, sizeof(fd_pubkey_t) ); 329 58789 : } 330 : 331 : /* fd_borrowed_account_can_data_be_changed mirrors Agave function 332 : solana_sdk::transaction_context::BorrowedAccount::can_data_be_changed. 333 : 334 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1074 */ 335 : 336 : static inline int 337 : fd_borrowed_account_can_data_be_changed( fd_borrowed_account_t const * borrowed_acct, 338 71295 : int * err ) { 339 : /* Only writable accounts can be changed 340 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1080 */ 341 71295 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( borrowed_acct ) ) ) { 342 18637 : *err = FD_EXECUTOR_INSTR_ERR_READONLY_DATA_MODIFIED; 343 18637 : return 0; 344 18637 : } 345 : 346 : /* And only if we are the owner 347 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1084 */ 348 52658 : if( FD_UNLIKELY( !fd_borrowed_account_is_owned_by_current_program( borrowed_acct ) ) ) { 349 46701 : *err = FD_EXECUTOR_INSTR_ERR_EXTERNAL_DATA_MODIFIED; 350 46701 : return 0; 351 46701 : } 352 : 353 5957 : *err = FD_EXECUTOR_INSTR_SUCCESS; 354 5957 : return 1; 355 52658 : } 356 : 357 : /* fd_borrowed_account_can_data_be_resized mirrors Agave function 358 : solana_transaction_context::instruction_accounts::BorrowedInstructionAccount::can_data_be_resized. 359 : 360 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/transaction-context/src/instruction_accounts.rs#L351-L357 */ 361 : 362 : int 363 : fd_borrowed_account_can_data_be_resized( fd_borrowed_account_t const * borrowed_acct, 364 : ulong new_length, 365 : int * err ); 366 : 367 : /* fd_borrowed_account_is_zeroed returns 1 if every byte of the account 368 : data is zero (or the account has no data) and 0 otherwise. */ 369 : 370 : FD_FN_PURE int 371 : fd_borrowed_account_is_zeroed( fd_borrowed_account_t const * borrowed_acct ); 372 : 373 : FD_PROTOTYPES_END 374 : 375 : #endif /* HEADER_fd_src_flamenco_runtime_fd_borrowed_account_h */