Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_runtime_native_program_util_h 2 : #define HEADER_fd_src_flamenco_runtime_native_program_util_h 3 : 4 : #include "../../fd_flamenco_base.h" 5 : #include "../fd_executor_err.h" 6 : #include "../fd_borrowed_account.h" 7 : 8 : FD_PROTOTYPES_BEGIN 9 : 10 : /**********************************************************************/ 11 : /* mod instruction */ 12 : /**********************************************************************/ 13 : 14 : // https://github.com/firedancer-io/solana/blob/v1.17/sdk/program/src/instruction.rs#L519 15 : static inline int 16 9546 : fd_ulong_checked_add( ulong a, ulong b, ulong * out ) { 17 9546 : int cf = __builtin_uaddl_overflow( a, b, out ); 18 9546 : return fd_int_if( cf, FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS, FD_EXECUTOR_INSTR_SUCCESS ); 19 9546 : } 20 : 21 : // https://github.com/firedancer-io/solana/blob/v1.17/sdk/program/src/instruction.rs#L519 22 : static inline int FD_FN_UNUSED 23 173307 : fd_ulong_checked_sub( ulong a, ulong b, ulong * out ) { 24 173307 : int cf = __builtin_usubl_overflow( a, b, out ); 25 173307 : return fd_int_if( cf, FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS, FD_EXECUTOR_INSTR_SUCCESS ); 26 173307 : } 27 : 28 : static inline ulong 29 4557 : fd_ulong_checked_add_expect( ulong a, ulong b, char const * expect ) { 30 4557 : ulong out = ULONG_MAX; 31 4557 : if( FD_UNLIKELY( fd_ulong_checked_add( a, b, &out ) ) ) { FD_LOG_ERR(( "%s", expect )); } 32 4557 : return out; 33 4557 : } 34 : 35 : static inline ulong 36 156870 : fd_ulong_checked_sub_expect( ulong a, ulong b, char const * expect ) { 37 156870 : ulong out = ULONG_MAX; 38 156870 : if( FD_UNLIKELY( fd_ulong_checked_sub( a, b, &out ) ) ) { FD_LOG_ERR(( "%s", expect )); } 39 156870 : return out; 40 156870 : } 41 : 42 : /**********************************************************************/ 43 : /* impl BorrowedAccount */ 44 : /**********************************************************************/ 45 : 46 : // https://github.com/firedancer-io/solana/blob/da470eef4652b3b22598a1f379cacfe82bd5928d/sdk/src/transaction_context.rs#L841 47 : static inline int 48 : fd_borrowed_account_checked_add_lamports( fd_borrowed_account_t * self, 49 0 : ulong lamports ) { 50 0 : ulong temp; 51 0 : int rc = fd_int_if( __builtin_uaddl_overflow( self->meta->info.lamports, lamports, &temp ), 52 0 : FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW, 53 0 : FD_EXECUTOR_INSTR_SUCCESS ); 54 : 55 0 : if (FD_EXECUTOR_INSTR_SUCCESS != rc) 56 0 : return rc; 57 : 58 0 : self->meta->info.lamports = temp; 59 0 : return rc; 60 0 : } 61 : 62 : FD_PROTOTYPES_END 63 : 64 : #endif /* HEADER_fd_src_flamenco_runtime_native_program_util_h */