Line data    Source code 
       1             : #include "fd_bpf_loader_program.h"
       2             : 
       3             : /* For additional context see https://solana.com/docs/programs/deploying#state-accounts */
       4             : 
       5             : #include "../../progcache/fd_prog_load.h"
       6             : #include "../fd_pubkey_utils.h"
       7             : #include "../../../ballet/sbpf/fd_sbpf_loader.h"
       8             : #include "../sysvar/fd_sysvar.h"
       9             : #include "fd_bpf_loader_serialization.h"
      10             : #include "fd_builtin_programs.h"
      11             : #include "fd_native_cpi.h"
      12             : #include "../fd_borrowed_account.h"
      13             : #include "../fd_system_ids.h"
      14             : 
      15             : /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/sdk/program/src/program_error.rs#L290-L335 */
      16             : static inline int
      17             : program_error_to_instr_error( ulong  err,
      18           0 :                               uint * custom_err ) {
      19           0 :   switch( err ) {
      20           0 :     case CUSTOM_ZERO:
      21           0 :       *custom_err = 0;
      22           0 :       return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
      23           0 :     case INVALID_ARGUMENT:
      24           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
      25           0 :     case INVALID_INSTRUCTION_DATA:
      26           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
      27           0 :     case INVALID_ACCOUNT_DATA:
      28           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
      29           0 :     case ACCOUNT_DATA_TOO_SMALL:
      30           0 :       return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
      31           0 :     case INSUFFICIENT_FUNDS:
      32           0 :       return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
      33           0 :     case INCORRECT_PROGRAM_ID:
      34           0 :       return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
      35           0 :     case MISSING_REQUIRED_SIGNATURES:
      36           0 :       return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
      37           0 :     case ACCOUNT_ALREADY_INITIALIZED:
      38           0 :       return FD_EXECUTOR_INSTR_ERR_ACC_ALREADY_INITIALIZED;
      39           0 :     case UNINITIALIZED_ACCOUNT:
      40           0 :       return FD_EXECUTOR_INSTR_ERR_UNINITIALIZED_ACCOUNT;
      41           0 :     case NOT_ENOUGH_ACCOUNT_KEYS:
      42           0 :       return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
      43           0 :     case ACCOUNT_BORROW_FAILED:
      44           0 :       return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED;
      45           0 :     case MAX_SEED_LENGTH_EXCEEDED:
      46           0 :       return FD_EXECUTOR_INSTR_ERR_MAX_SEED_LENGTH_EXCEEDED;
      47           0 :     case INVALID_SEEDS:
      48           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_SEEDS;
      49           0 :     case BORSH_IO_ERROR:
      50           0 :       return FD_EXECUTOR_INSTR_ERR_BORSH_IO_ERROR;
      51           0 :     case ACCOUNT_NOT_RENT_EXEMPT:
      52           0 :       return FD_EXECUTOR_INSTR_ERR_ACC_NOT_RENT_EXEMPT;
      53           0 :     case UNSUPPORTED_SYSVAR:
      54           0 :       return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
      55           0 :     case ILLEGAL_OWNER:
      56           0 :       return FD_EXECUTOR_INSTR_ERR_ILLEGAL_OWNER;
      57           0 :     case MAX_ACCOUNTS_DATA_ALLOCATIONS_EXCEEDED:
      58           0 :       return FD_EXECUTOR_INSTR_ERR_MAX_ACCS_DATA_ALLOCS_EXCEEDED;
      59           0 :     case INVALID_ACCOUNT_DATA_REALLOC:
      60           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
      61           0 :     case MAX_INSTRUCTION_TRACE_LENGTH_EXCEEDED:
      62           0 :       return FD_EXECUTOR_INSTR_ERR_MAX_INSN_TRACE_LENS_EXCEEDED;
      63           0 :     case BUILTIN_PROGRAMS_MUST_CONSUME_COMPUTE_UNITS:
      64           0 :       return FD_EXECUTOR_INSTR_ERR_BUILTINS_MUST_CONSUME_CUS;
      65           0 :     case INVALID_ACCOUNT_OWNER:
      66           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER;
      67           0 :     case ARITHMETIC_OVERFLOW:
      68           0 :       return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
      69           0 :     case IMMUTABLE:
      70           0 :       return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
      71           0 :     case INCORRECT_AUTHORITY:
      72           0 :       return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
      73           0 :     default:
      74           0 :       if( err>>BUILTIN_BIT_SHIFT == 0 ) {
      75           0 :         *custom_err = (uint)err;
      76           0 :         return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
      77           0 :       }
      78           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ERR;
      79           0 :   }
      80           0 : }
      81             : 
      82             : /* https://github.com/anza-xyz/agave/blob/9b22f28104ec5fd606e4bb39442a7600b38bb671/programs/bpf_loader/src/lib.rs#L216-L229 */
      83             : static ulong
      84           0 : calculate_heap_cost( ulong heap_size, ulong heap_cost ) {
      85           0 :   #define KIBIBYTE_MUL_PAGES       (1024UL * 32UL)
      86           0 :   #define KIBIBYTE_MUL_PAGES_SUB_1 (KIBIBYTE_MUL_PAGES - 1UL)
      87             : 
      88           0 :   heap_size = fd_ulong_sat_add( heap_size, KIBIBYTE_MUL_PAGES_SUB_1 );
      89             : 
      90           0 :   heap_size = fd_ulong_sat_mul( fd_ulong_sat_sub( heap_size / KIBIBYTE_MUL_PAGES, 1UL ), heap_cost );
      91           0 :   return heap_size;
      92             : 
      93           0 :   #undef KIBIBYTE_MUL_PAGES
      94           0 :   #undef KIBIBYTE_MUL_PAGES_SUB_1
      95           0 : }
      96             : 
      97             : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L105-L171
      98             : 
      99             :    Our arguments to deploy_program are different from the Agave version because
     100             :    we handle the caching of deployed programs differently. In Firedancer we
     101             :    lack the concept of ProgramCacheEntryType entirely.
     102             :    https://github.com/anza-xyz/agave/blob/114d94a25e9631f9bf6349c4b833d7900ef1fb1c/program-runtime/src/loaded_programs.rs#L158
     103             : 
     104             :    In Agave there is a separate caching structure that is used to store the
     105             :    deployed programs. In Firedancer the deployed, validated program is stored as
     106             :   metadata for the account in the funk record.
     107             : 
     108             :    See https://github.com/firedancer-io/firedancer/blob/9c1df680b3f38bebb0597e089766ec58f3b41e85/src/flamenco/runtime/program/fd_bpf_loader_v3_program.c#L1640
     109             :    for how we handle the concept of 'LoadedProgramType::DelayVisibility' in Firedancer.
     110             : 
     111             :    As a concrete example, our version of deploy_program does not have the
     112             :    'account_size' argument because we do not update the funk record here.
     113             : 
     114             :    The spad used for allocations can be either scoped to the executor or the
     115             :    runtime depending on where it is called from. If a program is deployed from
     116             :    the v3 contract, then the executor spad should be used. */
     117             : int
     118             : fd_deploy_program( fd_exec_instr_ctx_t * instr_ctx,
     119             :                    fd_pubkey_t const *   program_key,
     120             :                    uchar const *         programdata,
     121             :                    ulong                 programdata_size,
     122           0 :                    fd_spad_t *           spad ) {
     123           0 :   int deploy_mode                          = 1;
     124           0 :   int direct_mapping                       = FD_FEATURE_ACTIVE_BANK( instr_ctx->txn_ctx->bank, account_data_direct_mapping );
     125           0 :   int stricter_abi_and_runtime_constraints = FD_FEATURE_ACTIVE_BANK( instr_ctx->txn_ctx->bank, stricter_abi_and_runtime_constraints );
     126             : 
     127           0 :   fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_new( fd_spad_alloc( spad,
     128           0 :                                                                        fd_sbpf_syscalls_align(),
     129           0 :                                                                        fd_sbpf_syscalls_footprint() ) );
     130           0 :   if( FD_UNLIKELY( !syscalls ) ) {
     131             :     //TODO: full log including err
     132           0 :     fd_log_collector_msg_literal( instr_ctx, "Failed to register syscalls" );
     133           0 :     return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
     134           0 :   }
     135             : 
     136           0 :   fd_vm_syscall_register_slot( syscalls,
     137           0 :                                instr_ctx->txn_ctx->slot,
     138           0 :                                &instr_ctx->txn_ctx->features,
     139           0 :                                1 );
     140             : 
     141             :   /* Load executable */
     142           0 :   fd_sbpf_elf_info_t elf_info[ 1UL ];
     143           0 :   fd_prog_versions_t versions = fd_prog_versions( &instr_ctx->txn_ctx->features, instr_ctx->txn_ctx->slot );
     144             : 
     145           0 :   fd_sbpf_loader_config_t config = { 0 };
     146           0 :   config.elf_deploy_checks = deploy_mode;
     147           0 :   config.sbpf_min_version = versions.min_sbpf_version;
     148           0 :   config.sbpf_max_version = versions.max_sbpf_version;
     149             : 
     150           0 :   if( FD_UNLIKELY( fd_sbpf_elf_peek( elf_info, programdata, programdata_size, &config )<0 ) ) {
     151             :     //TODO: actual log, this is a custom Firedancer msg
     152           0 :     fd_log_collector_msg_literal( instr_ctx, "Failed to load or verify Elf" );
     153           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
     154           0 :   }
     155             : 
     156             :   /* Allocate rodata segment */
     157           0 :   void * rodata = fd_spad_alloc( spad, FD_SBPF_PROG_RODATA_ALIGN, elf_info->bin_sz );
     158           0 :   if( FD_UNLIKELY( !rodata ) ) {
     159           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
     160           0 :   }
     161             : 
     162             :   /* Allocate program buffer */
     163           0 :   ulong  prog_align        = fd_sbpf_program_align();
     164           0 :   ulong  prog_footprint    = fd_sbpf_program_footprint( elf_info );
     165           0 :   fd_sbpf_program_t * prog = fd_sbpf_program_new( fd_spad_alloc( spad, prog_align, prog_footprint ), elf_info, rodata );
     166           0 :   if( FD_UNLIKELY( !prog ) ) {
     167           0 :     FD_LOG_ERR(( "fd_sbpf_program_new() failed" ));
     168           0 :   }
     169             : 
     170             :   /* Load program */
     171           0 :   void * scratch = fd_spad_alloc( spad, 1UL, programdata_size );
     172           0 :   int err = fd_sbpf_program_load( prog, programdata, programdata_size, syscalls, &config, scratch, programdata_size );
     173           0 :   if( FD_UNLIKELY( err ) ) {
     174           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
     175           0 :   }
     176             : 
     177             :   /* Validate the program */
     178           0 :   fd_vm_t _vm[ 1UL ];
     179           0 :   fd_vm_t * vm = fd_vm_join( fd_vm_new( _vm ) );
     180             : 
     181           0 :   vm = fd_vm_init(
     182           0 :     /* vm                                   */ vm,
     183           0 :     /* instr_ctx                            */ instr_ctx,
     184           0 :     /* heap_max                             */ instr_ctx->txn_ctx->compute_budget_details.heap_size,
     185           0 :     /* entry_cu                             */ instr_ctx->txn_ctx->compute_budget_details.compute_meter,
     186           0 :     /* rodata                               */ prog->rodata,
     187           0 :     /* rodata_sz                            */ prog->rodata_sz,
     188           0 :     /* text                                 */ prog->text,
     189           0 :     /* text_cnt                             */ prog->info.text_cnt,
     190           0 :     /* text_off                             */ prog->info.text_off, /* FIXME: What if text_off is not multiple of 8 */
     191           0 :     /* text_sz                              */ prog->info.text_sz,
     192           0 :     /* entry_pc                             */ prog->entry_pc,
     193           0 :     /* calldests                            */ prog->calldests,
     194           0 :     /* sbpf_version                         */ elf_info->sbpf_version,
     195           0 :     /* syscalls                             */ syscalls,
     196           0 :     /* trace                                */ NULL,
     197           0 :     /* sha                                  */ NULL,
     198           0 :     /* mem_regions                          */ NULL,
     199           0 :     /* mem_regions_cnt                      */ 0,
     200           0 :     /* mem_region_accs                      */ NULL,
     201           0 :     /* is_deprecated                        */ 0,
     202           0 :     /* direct mapping                       */ direct_mapping,
     203           0 :     /* stricter_abi_and_runtime_constraints */ stricter_abi_and_runtime_constraints,
     204           0 :     /* dump_syscall_to_pb                   */ 0 );
     205           0 :   if ( FD_UNLIKELY( vm == NULL ) ) {
     206           0 :     FD_LOG_WARNING(( "NULL vm" ));
     207           0 :     return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
     208           0 :   }
     209             : 
     210           0 :   int validate_result = fd_vm_validate( vm );
     211           0 :   if( FD_UNLIKELY( validate_result!=FD_VM_SUCCESS ) ) {
     212           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
     213           0 :   }
     214             : 
     215             :   /* Queue the program for reverification */
     216           0 :   instr_ctx->txn_ctx->programs_to_reverify[instr_ctx->txn_ctx->programs_to_reverify_cnt++] = *program_key;
     217             : 
     218           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     219           0 : }
     220             : 
     221             : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L195-L218 */
     222             : static int
     223             : write_program_data( fd_exec_instr_ctx_t *   instr_ctx,
     224             :                     ushort                  instr_acc_idx,
     225             :                     ulong                   program_data_offset,
     226             :                     uchar *                 bytes,
     227           0 :                     ulong                   bytes_len ) {
     228           0 :   int err;
     229             : 
     230             :   /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L202 */
     231           0 :   fd_guarded_borrowed_account_t program = {0};
     232           0 :   FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, instr_acc_idx, &program );
     233             : 
     234             :   /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L203 */
     235           0 :   uchar * data = NULL;
     236           0 :   ulong   dlen = 0UL;
     237           0 :   err = fd_borrowed_account_get_data_mut( &program, &data, &dlen );
     238           0 :   if( FD_UNLIKELY( err ) ) {
     239           0 :     return err;
     240           0 :   }
     241             : 
     242           0 :   ulong write_offset = fd_ulong_sat_add( program_data_offset, bytes_len );
     243           0 :   if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &program )<write_offset ) ) {
     244             :     /* Max msg_sz: 24 - 6 + 2*20 = 58 < 127 => we can use printf */
     245           0 :     fd_log_collector_printf_dangerous_max_127( instr_ctx,
     246           0 :       "Write overflow %lu < %lu", fd_borrowed_account_get_data_len( &program ), write_offset );
     247           0 :     return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
     248           0 :   }
     249             : 
     250           0 :   if( FD_UNLIKELY( program_data_offset>dlen ) ) {
     251           0 :     return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
     252           0 :   }
     253             : 
     254           0 :   if( FD_LIKELY( bytes_len ) ) {
     255           0 :     fd_memcpy( data+program_data_offset, bytes, bytes_len );
     256           0 :   }
     257             : 
     258           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     259           0 : }
     260             : 
     261             : fd_bpf_upgradeable_loader_state_t *
     262             : fd_bpf_loader_program_get_state( fd_txn_account_t const * acct,
     263             :                                  fd_spad_t *              spad,
     264           0 :                                  int *                    opt_err ) {
     265           0 :   int err;
     266           0 :   fd_bpf_upgradeable_loader_state_t * res = fd_bincode_decode_spad(
     267           0 :       bpf_upgradeable_loader_state,
     268           0 :       spad,
     269           0 :       fd_txn_account_get_data( acct ),
     270           0 :       fd_txn_account_get_data_len( acct ),
     271           0 :       &err );
     272             : 
     273           0 :   if( opt_err ) {
     274           0 :     *opt_err = FD_UNLIKELY( err ) ? FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA : FD_EXECUTOR_INSTR_SUCCESS;
     275           0 :   }
     276             : 
     277           0 :   return FD_UNLIKELY( err ) ? NULL : res;
     278           0 : }
     279             : 
     280             : /* Mirrors solana_sdk::transaction_context::BorrowedAccount::set_state()
     281             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L973 */
     282             : int
     283             : fd_bpf_loader_v3_program_set_state( fd_borrowed_account_t * borrowed_acct,
     284           0 :                                     fd_bpf_upgradeable_loader_state_t * state ) {
     285           0 :   ulong state_size = fd_bpf_upgradeable_loader_state_size( state );
     286             : 
     287           0 :   uchar * data = NULL;
     288           0 :   ulong   dlen = 0UL;
     289             : 
     290           0 :   int err = fd_borrowed_account_get_data_mut( borrowed_acct, &data, &dlen );
     291           0 :   if( FD_UNLIKELY( err ) ) {
     292           0 :     return err;
     293           0 :   }
     294             : 
     295           0 :   if( FD_UNLIKELY( state_size>dlen ) ) {
     296           0 :     return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
     297           0 :   }
     298             : 
     299           0 :   fd_bincode_encode_ctx_t ctx = {
     300           0 :     .data    = data,
     301           0 :     .dataend = data + state_size
     302           0 :   };
     303             : 
     304           0 :   err = fd_bpf_upgradeable_loader_state_encode( state, &ctx );
     305           0 :   if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
     306           0 :     return FD_EXECUTOR_INSTR_ERR_GENERIC_ERR;
     307           0 :   }
     308             : 
     309           0 :   return FD_BINCODE_SUCCESS;
     310           0 : }
     311             : 
     312             : /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1299-L1331 */
     313             : static int
     314             : common_close_account( fd_pubkey_t * authority_address,
     315             :                       fd_exec_instr_ctx_t * instr_ctx,
     316           0 :                       fd_bpf_upgradeable_loader_state_t * state ) {
     317           0 :   int err;
     318             : 
     319             :   /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L1307 */
     320           0 :   if( FD_UNLIKELY( !authority_address ) ) {
     321           0 :     return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
     322           0 :   }
     323             : 
     324             :   /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L1312-L1313 */
     325           0 :   fd_pubkey_t const * acc_key = NULL;
     326           0 :   err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 2UL, &acc_key );
     327           0 :   if( FD_UNLIKELY( err ) ) {
     328           0 :     return err;
     329           0 :   }
     330             : 
     331           0 :   if( FD_UNLIKELY( memcmp( authority_address, acc_key, sizeof(fd_pubkey_t) ) ) ) {
     332           0 :     return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
     333           0 :   }
     334             : 
     335             :   /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L1319-L1322 */
     336           0 :   if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 2UL, &err ) ) ) {
     337             :     /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
     338           0 :     if( FD_UNLIKELY( !!err ) ) return err;
     339           0 :     return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
     340           0 :   }
     341             : 
     342             :   /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1324 */
     343           0 :   fd_guarded_borrowed_account_t close_account = {0};
     344           0 :   FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &close_account );
     345             : 
     346             :   /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1326 */
     347           0 :   fd_guarded_borrowed_account_t recipient_account = {0};
     348           0 :   FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 1UL, &recipient_account );
     349             : 
     350           0 :   err = fd_borrowed_account_checked_add_lamports( &recipient_account,
     351           0 :                                                   fd_borrowed_account_get_lamports( &close_account ) );
     352           0 :   if( FD_UNLIKELY( err ) ) {
     353           0 :     return err;
     354           0 :   }
     355             : 
     356           0 :   err = fd_borrowed_account_set_lamports( &close_account, 0UL );
     357           0 :   if( FD_UNLIKELY( err ) ) {
     358           0 :     return err;
     359           0 :   }
     360             : 
     361           0 :   state->discriminant = fd_bpf_upgradeable_loader_state_enum_uninitialized;
     362           0 :   err = fd_bpf_loader_v3_program_set_state( &close_account, state );
     363           0 :   if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
     364           0 :     return err;
     365           0 :   }
     366             : 
     367           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     368           0 : }
     369             : 
     370             : 
     371             : /* Every loader-owned BPF program goes through this function, which goes into the VM.
     372             : 
     373             :    https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1332-L1501 */
     374             : int
     375             : fd_bpf_execute( fd_exec_instr_ctx_t *      instr_ctx,
     376             :                 fd_progcache_rec_t const * cache_entry,
     377           0 :                 uchar                      is_deprecated ) {
     378             : 
     379           0 :   int err                       = FD_EXECUTOR_INSTR_SUCCESS;
     380           0 :   fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_new( fd_spad_alloc( instr_ctx->txn_ctx->spad,
     381           0 :                                                                        fd_sbpf_syscalls_align(),
     382           0 :                                                                        fd_sbpf_syscalls_footprint() ) );
     383           0 :   if( FD_UNLIKELY( !syscalls ) ) {
     384           0 :     FD_LOG_CRIT(( "Unable to allocate syscalls" ));
     385           0 :   }
     386             : 
     387             :   /* TODO do we really need to re-do this on every instruction? */
     388           0 :   fd_vm_syscall_register_slot( syscalls,
     389           0 :                                instr_ctx->txn_ctx->slot,
     390           0 :                                &instr_ctx->txn_ctx->features,
     391           0 :                                0 );
     392             : 
     393             :   /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1362-L1368 */
     394           0 :   ulong                   input_sz                             = 0UL;
     395           0 :   ulong                   pre_lens[256]                        = {0};
     396           0 :   fd_vm_input_region_t    input_mem_regions[1000]              = {0}; /* We can have a max of (3 * num accounts + 1) regions */
     397           0 :   fd_vm_acc_region_meta_t acc_region_metas[256]                = {0}; /* instr acc idx to idx */
     398           0 :   uint                    input_mem_regions_cnt                = 0U;
     399           0 :   int                     direct_mapping                       = FD_FEATURE_ACTIVE_BANK( instr_ctx->txn_ctx->bank, bpf_account_data_direct_mapping );
     400           0 :   int                     stricter_abi_and_runtime_constraints = FD_FEATURE_ACTIVE_BANK( instr_ctx->txn_ctx->bank, stricter_abi_and_runtime_constraints );
     401             : 
     402           0 :   uchar * input = NULL;
     403           0 :   err = fd_bpf_loader_input_serialize_parameters( instr_ctx, &input_sz, pre_lens,
     404           0 :                                                   input_mem_regions, &input_mem_regions_cnt,
     405           0 :                                                   acc_region_metas, stricter_abi_and_runtime_constraints, direct_mapping, is_deprecated, &input );
     406           0 :   if( FD_UNLIKELY( err ) ) {
     407           0 :     return err;
     408           0 :   }
     409             : 
     410           0 :   if( FD_UNLIKELY( input==NULL ) ) {
     411           0 :     return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
     412           0 :   }
     413             : 
     414           0 :   fd_sha256_t _sha[1];
     415           0 :   fd_sha256_t * sha = fd_sha256_join( fd_sha256_new( _sha ) );
     416             : 
     417           0 :   fd_vm_t _vm[1];
     418           0 :   fd_vm_t * vm = fd_vm_join( fd_vm_new( _vm ) );
     419             : 
     420           0 :   ulong pre_insn_cus = instr_ctx->txn_ctx->compute_budget_details.compute_meter;
     421           0 :   ulong heap_size    = instr_ctx->txn_ctx->compute_budget_details.heap_size;
     422             : 
     423             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L275-L278 */
     424           0 :   ulong heap_cost = calculate_heap_cost( heap_size, FD_VM_HEAP_COST );
     425           0 :   int heap_cost_result = fd_exec_consume_cus( instr_ctx->txn_ctx, heap_cost );
     426           0 :   if( FD_UNLIKELY( heap_cost_result ) ) {
     427           0 :     return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
     428           0 :   }
     429             : 
     430             :   /* For dumping syscalls for seed corpora */
     431           0 :   int dump_syscall_to_pb = instr_ctx->txn_ctx->capture_ctx &&
     432           0 :                            instr_ctx->txn_ctx->slot >= instr_ctx->txn_ctx->capture_ctx->dump_proto_start_slot &&
     433           0 :                            instr_ctx->txn_ctx->capture_ctx->dump_syscall_to_pb;
     434             : 
     435             :   /* TODO: (topointon): correctly set check_size in vm setup */
     436           0 :   vm = fd_vm_init(
     437           0 :     /* vm                                   */ vm,
     438           0 :     /* instr_ctx                            */ instr_ctx,
     439           0 :     /* heap_max                             */ heap_size,
     440           0 :     /* entry_cu                             */ instr_ctx->txn_ctx->compute_budget_details.compute_meter,
     441           0 :     /* rodata                               */ fd_progcache_rec_rodata( cache_entry ),
     442           0 :     /* rodata_sz                            */ cache_entry->rodata_sz,
     443           0 :     /* text (note: text_off is byte offset) */ (ulong *)((ulong)fd_progcache_rec_rodata( cache_entry ) + (ulong)cache_entry->text_off),
     444           0 :     /* text_cnt                             */ cache_entry->text_cnt,
     445           0 :     /* text_off                             */ cache_entry->text_off,
     446           0 :     /* text_sz                              */ cache_entry->text_sz,
     447           0 :     /* entry_pc                             */ cache_entry->entry_pc,
     448           0 :     /* calldests                            */ fd_progcache_rec_calldests( cache_entry ),
     449           0 :     /* sbpf_version                         */ cache_entry->sbpf_version,
     450           0 :     /* syscalls                             */ syscalls,
     451           0 :     /* trace                                */ NULL,
     452           0 :     /* sha                                  */ sha,
     453           0 :     /* input_mem_regions                    */ input_mem_regions,
     454           0 :     /* input_mem_regions_cnt                */ input_mem_regions_cnt,
     455           0 :     /* acc_region_metas                     */ acc_region_metas,
     456           0 :     /* is_deprecated                        */ is_deprecated,
     457           0 :     /* direct_mapping                       */ direct_mapping,
     458           0 :     /* stricter_abi_and_runtime_constraints */ stricter_abi_and_runtime_constraints,
     459           0 :     /* dump_syscall_to_pb    */                dump_syscall_to_pb );
     460           0 :   if( FD_UNLIKELY( !vm ) ) {
     461             :     /* We throw an error here because it could be the case that the given heap_size > HEAP_MAX.
     462             :        In this case, Agave fails the transaction but does not error out.
     463             : 
     464             :        https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1396 */
     465           0 :     FD_LOG_WARNING(( "null vm" ));
     466           0 :     return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
     467           0 :   }
     468             : 
     469           0 :   if( FD_UNLIKELY( instr_ctx->txn_ctx->fuzz_config.enable_vm_tracing ) ) {
     470           0 :     ulong event_max      = FD_RUNTIME_VM_TRACE_EVENT_MAX;
     471           0 :     ulong event_data_max = FD_RUNTIME_VM_TRACE_EVENT_DATA_MAX;
     472           0 :     vm->trace = fd_vm_trace_join( fd_vm_trace_new( fd_spad_alloc(
     473           0 :     instr_ctx->txn_ctx->spad, fd_vm_trace_align(), fd_vm_trace_footprint( event_max, event_data_max ) ), event_max, event_data_max ) );
     474           0 :     if( FD_UNLIKELY( !vm->trace ) ) FD_LOG_ERR(( "unable to create trace; make sure you've compiled with sufficient spad size " ));
     475           0 :   }
     476             : 
     477           0 :   int exec_err = fd_vm_exec( vm );
     478           0 :   instr_ctx->txn_ctx->compute_budget_details.compute_meter = vm->cu;
     479             : 
     480           0 :   if( FD_UNLIKELY( vm->trace ) ) {
     481           0 :     err = fd_vm_trace_printf( vm->trace, vm->syscalls );
     482           0 :     if( FD_UNLIKELY( err ) ) {
     483           0 :       FD_LOG_WARNING(( "fd_vm_trace_printf failed (%i-%s)", err, fd_vm_strerror( err ) ));
     484           0 :     }
     485           0 :   }
     486             : 
     487             :   /* Log consumed compute units and return data.
     488             :      https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/lib.rs#L1418-L1429 */
     489           0 :   fd_log_collector_program_consumed( instr_ctx, pre_insn_cus-vm->cu, pre_insn_cus );
     490           0 :   if( FD_UNLIKELY( instr_ctx->txn_ctx->return_data.len ) ) {
     491           0 :     fd_log_collector_program_return( instr_ctx );
     492           0 :   }
     493             : 
     494             :   /* We have a big error-matching arm here
     495             :      https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1674-L1744 */
     496             : 
     497             :   /* Handle non-zero return status with successful VM execution. This is
     498             :      the Ok(status) case, hence exec_err must be 0 for this case to be hit.
     499             :      https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1675-L1678 */
     500           0 :   if( FD_LIKELY( !exec_err ) ) {
     501           0 :     ulong status = vm->reg[0];
     502           0 :     if( FD_UNLIKELY( status ) ) {
     503           0 :       err = program_error_to_instr_error( status, &instr_ctx->txn_ctx->custom_err );
     504           0 :       FD_VM_PREPARE_ERR_OVERWRITE( vm );
     505           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     506           0 :       return err;
     507           0 :     }
     508           0 :   } else {
     509             :     /* https://github.com/anza-xyz/agave/blob/v2.1.13/programs/bpf_loader/src/lib.rs#L1434-L1439 */
     510             :     /* (SIMD-182) Consume ALL requested CUs on non-Syscall errors */
     511           0 :     if( FD_FEATURE_ACTIVE_BANK( instr_ctx->txn_ctx->bank, deplete_cu_meter_on_vm_failure ) &&
     512           0 :         exec_err!=FD_VM_ERR_EBPF_SYSCALL_ERROR ) {
     513           0 :       instr_ctx->txn_ctx->compute_budget_details.compute_meter = 0UL;
     514           0 :     }
     515             : 
     516             :     /* Direct mapping access violation case
     517             :        Edge case with error codes: if direct mapping is enabled, the EBPF error is an access violation,
     518             :        and the access type was a store, a different error code is returned to give developers more insight
     519             :        as to what caused the error.
     520             :        https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1556-L1618 */
     521           0 :     if( FD_UNLIKELY( stricter_abi_and_runtime_constraints &&
     522           0 :                      ( exec_err==FD_VM_ERR_EBPF_ACCESS_VIOLATION || instr_ctx->txn_ctx->exec_err==FD_VM_ERR_EBPF_ACCESS_VIOLATION ) &&
     523           0 :                      vm->segv_vaddr!=ULONG_MAX ) ) {
     524             : 
     525             :       /* vaddrs start at 0xFFFFFFFF + 1, so anything below it would not correspond to any account metadata. */
     526           0 :       if( FD_UNLIKELY( vm->segv_vaddr>>32UL==0UL ) ) {
     527           0 :         return FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPLETE;
     528           0 :       }
     529             : 
     530             :       /* If the vaddr doesn't live in the input region, then we don't need to
     531             :          bother trying to iterate through all of the borrowed accounts. */
     532           0 :       if( FD_VADDR_TO_REGION( vm->segv_vaddr )!=FD_VM_INPUT_REGION ) {
     533           0 :         return FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPLETE;
     534           0 :       }
     535             : 
     536             :       /* If the vaddr of the access violation falls within the bounds of a
     537             :          serialized account vaddr range, then try to retrieve a more specific
     538             :          vm error based on the account's accesss permissions. */
     539           0 :       for( ushort i=0UL; i<instr_ctx->instr->acct_cnt; i++ ) {
     540             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1455 */
     541             : 
     542             :         /* Find the input memory region that corresponds to the access
     543             :            https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1566-L1617 */
     544           0 :         ulong idx = acc_region_metas[i].region_idx;
     545           0 :         fd_vm_input_region_t const * input_mem_region = &input_mem_regions[idx];
     546           0 :         fd_vm_acc_region_meta_t const * acc_region_meta = &acc_region_metas[i];
     547             : 
     548             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1484-L1492 */
     549           0 :         ulong region_data_vaddr_start = FD_VM_MEM_MAP_INPUT_REGION_START + input_mem_region->vaddr_offset + input_mem_region->region_sz;
     550           0 :         ulong region_data_vaddr_end   = fd_ulong_sat_add( region_data_vaddr_start, acc_region_meta->original_data_len );
     551           0 :         if( FD_LIKELY( !is_deprecated ) ) {
     552           0 :           region_data_vaddr_end       = fd_ulong_sat_add( region_data_vaddr_end, MAX_PERMITTED_DATA_INCREASE );
     553           0 :         }
     554             : 
     555           0 :         if( vm->segv_vaddr >= region_data_vaddr_start && vm->segv_vaddr <= region_data_vaddr_end ) {
     556             : 
     557             :           /* https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1575-L1616 */
     558           0 :           fd_guarded_borrowed_account_t instr_acc = {0};
     559           0 :           FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, i, &instr_acc );
     560             : 
     561             :           /* https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1581-L1616 */
     562           0 :           if( fd_ulong_sat_add( vm->segv_vaddr, vm->segv_access_len ) <= region_data_vaddr_end ) {
     563             :             /* https://github.com/anza-xyz/agave/blob/v3.0.4/programs/bpf_loader/src/lib.rs#L1592-L1601 */
     564           0 :             if( vm->segv_access_type == FD_VM_ACCESS_TYPE_ST ) {
     565           0 :               int borrow_err = FD_EXECUTOR_INSTR_SUCCESS;
     566           0 :               if( !fd_borrowed_account_can_data_be_changed( &instr_acc, &borrow_err ) || borrow_err != FD_EXECUTOR_INSTR_SUCCESS ) {
     567           0 :                 return borrow_err;
     568           0 :               } else {
     569           0 :                 return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
     570           0 :               }
     571           0 :             } else if ( vm->segv_access_type == FD_VM_ACCESS_TYPE_LD ) {
     572           0 :               int borrow_err = FD_EXECUTOR_INSTR_SUCCESS;
     573           0 :               if( !fd_borrowed_account_can_data_be_changed( &instr_acc, &borrow_err ) || borrow_err != FD_EXECUTOR_INSTR_SUCCESS ) {
     574           0 :                 return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
     575           0 :               } else {
     576           0 :                 return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
     577           0 :               }
     578           0 :             }
     579           0 :           }
     580           0 :         }
     581           0 :       }
     582           0 :     }
     583             : 
     584             :     /* The error kind should have been set in the VM. Match it and set
     585             :        the error code accordingly. There are no direct permalinks here -
     586             :        this is all a result of Agave's complex nested error-code handling
     587             :        and our design decisions for making our error codes match. */
     588             : 
     589             :     /* Instr error case. Set the error kind and return the instruction error */
     590           0 :     if( instr_ctx->txn_ctx->exec_err_kind==FD_EXECUTOR_ERR_KIND_INSTR ) {
     591           0 :       err = instr_ctx->txn_ctx->exec_err;
     592           0 :       FD_VM_PREPARE_ERR_OVERWRITE( vm );
     593           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     594           0 :       return err;
     595           0 :     }
     596             : 
     597             :     /* Syscall error case. The VM would have also set the syscall error
     598             :        code in the txn_ctx exec_err. */
     599           0 :     if( instr_ctx->txn_ctx->exec_err_kind==FD_EXECUTOR_ERR_KIND_SYSCALL ) {
     600           0 :       err = instr_ctx->txn_ctx->exec_err;
     601           0 :       FD_VM_PREPARE_ERR_OVERWRITE( vm );
     602           0 :       FD_VM_ERR_FOR_LOG_SYSCALL( vm, err );
     603           0 :       return FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPLETE;
     604           0 :     }
     605             : 
     606             :     /* An access violation that takes place inside a syscall will
     607             :        cause `exec_res` to be set to EbpfError::SyscallError,
     608             :        but the `txn_ctx->exec_err_kind` will be set to EBPF and
     609             :        `txn_ctx->exec_err` will be set to the EBPF error. In this
     610             :        specific case, there is nothing to do since the error and error
     611             :        kind area already set correctly. Otherwise, we need to log the
     612             :        EBPF error. */
     613           0 :     if( exec_err!=FD_VM_ERR_EBPF_SYSCALL_ERROR ) {
     614           0 :       FD_VM_PREPARE_ERR_OVERWRITE( vm );
     615           0 :       FD_VM_ERR_FOR_LOG_EBPF( vm, exec_err );
     616           0 :     }
     617             : 
     618           0 :     return FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPLETE;
     619           0 :   }
     620             : 
     621           0 :   err = fd_bpf_loader_input_deserialize_parameters(
     622           0 :     instr_ctx, pre_lens, input, input_sz, stricter_abi_and_runtime_constraints, direct_mapping, is_deprecated );
     623           0 :   if( FD_UNLIKELY( err ) ) {
     624           0 :     return err;
     625           0 :   }
     626             : 
     627           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     628           0 : }
     629             : 
     630             : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1358-L1539 */
     631             : static int
     632             : common_extend_program( fd_exec_instr_ctx_t * instr_ctx,
     633             :                        uint                  additional_bytes,
     634           0 :                        uchar                 check_authority ) {
     635           0 :   int err;
     636             : 
     637             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1366 */
     638           0 :   fd_pubkey_t const * program_id = NULL;
     639           0 :   err = fd_exec_instr_ctx_get_last_program_key( instr_ctx, &program_id );
     640           0 :   if( FD_UNLIKELY( err ) ) {
     641           0 :     return err;
     642           0 :   }
     643             : 
     644             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1368-L1370 */
     645           0 :   #define PROGRAM_DATA_ACCOUNT_INDEX (0)
     646           0 :   #define PROGRAM_ACCOUNT_INDEX      (1)
     647           0 :   #define AUTHORITY_ACCOUNT_INDEX    (2)
     648             : 
     649             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1371-L1372 */
     650           0 :   uchar optional_payer_account_index = check_authority ? 4 : 3;
     651             : 
     652             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1374-L1377 */
     653           0 :   if( FD_UNLIKELY( additional_bytes==0U ) ) {
     654           0 :     fd_log_collector_msg_literal( instr_ctx, "Additional bytes must be greater than 0" );
     655           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
     656           0 :   }
     657             : 
     658             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1379-L1381 */
     659           0 :   fd_guarded_borrowed_account_t programdata_account = {0};
     660           0 :   FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, PROGRAM_DATA_ACCOUNT_INDEX, &programdata_account );
     661           0 :   fd_pubkey_t * programdata_key = programdata_account.acct->pubkey;
     662             : 
     663             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1383-L1386 */
     664           0 :   if( FD_UNLIKELY( memcmp( program_id, fd_borrowed_account_get_owner( &programdata_account ), sizeof(fd_pubkey_t) ) ) ) {
     665           0 :     fd_log_collector_msg_literal( instr_ctx, "ProgramData owner is invalid" );
     666           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER;
     667           0 :   }
     668             : 
     669             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1387-L1390 */
     670           0 :   if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &programdata_account ) ) ) {
     671           0 :     fd_log_collector_msg_literal( instr_ctx, "ProgramData is not writable" );
     672           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
     673           0 :   }
     674             : 
     675             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1392-L1393 */
     676           0 :   fd_guarded_borrowed_account_t program_account = {0};
     677           0 :   FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, PROGRAM_ACCOUNT_INDEX, &program_account );
     678             : 
     679             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1394-L1397 */
     680           0 :   if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &program_account ) ) ) {
     681           0 :     fd_log_collector_msg_literal( instr_ctx, "Program account is not writable" );
     682           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
     683           0 :   }
     684             : 
     685             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1398-L1401 */
     686           0 :   if( FD_UNLIKELY( memcmp( program_id, fd_borrowed_account_get_owner( &program_account ), sizeof(fd_pubkey_t) ) ) ) {
     687           0 :     fd_log_collector_msg_literal( instr_ctx, "Program account not owned by loader" );
     688           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER;
     689           0 :   }
     690             : 
     691             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1403-L1419 */
     692           0 :   fd_bpf_upgradeable_loader_state_t * program_state = fd_bpf_loader_program_get_state( program_account.acct, instr_ctx->txn_ctx->spad, &err );
     693           0 :   if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
     694           0 :     return err;
     695           0 :   }
     696           0 :   if( fd_bpf_upgradeable_loader_state_is_program( program_state ) ) {
     697           0 :     if( FD_UNLIKELY( memcmp( &program_state->inner.program.programdata_address, programdata_key, sizeof(fd_pubkey_t) ) ) ) {
     698           0 :       fd_log_collector_msg_literal( instr_ctx, "Program account does not match ProgramData account" );
     699           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
     700           0 :     }
     701           0 :   } else {
     702           0 :     fd_log_collector_msg_literal( instr_ctx, "Invalid Program account" );
     703           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
     704           0 :   }
     705             : 
     706             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1420 */
     707           0 :   fd_borrowed_account_drop( &program_account );
     708             : 
     709             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1422-L1432 */
     710           0 :   ulong old_len = fd_borrowed_account_get_data_len( &programdata_account );
     711           0 :   ulong new_len = fd_ulong_sat_add( old_len, additional_bytes );
     712           0 :   if( FD_UNLIKELY( new_len>MAX_PERMITTED_DATA_LENGTH ) ) {
     713             :     /* Max msg_sz: 85 - 6 + 2*20 = 119 < 127 => we can use printf */
     714           0 :     fd_log_collector_printf_dangerous_max_127( instr_ctx,
     715           0 :       "Extended ProgramData length of %lu bytes exceeds max account data length of %lu bytes", new_len, MAX_PERMITTED_DATA_LENGTH );
     716           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
     717           0 :   }
     718             : 
     719             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1434-L1437 */
     720           0 :   fd_sol_sysvar_clock_t clock[1];
     721           0 :   if( FD_UNLIKELY( !fd_sysvar_cache_clock_read( instr_ctx->sysvar_cache, clock ) ) ) {
     722           0 :     return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
     723           0 :   }
     724           0 :   ulong clock_slot = clock->slot;
     725             : 
     726             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1439-L1478 */
     727           0 :   fd_pubkey_t * upgrade_authority_address = NULL;
     728           0 :   fd_bpf_upgradeable_loader_state_t * programdata_state = fd_bpf_loader_program_get_state( programdata_account.acct, instr_ctx->txn_ctx->spad, &err );
     729           0 :   if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
     730           0 :     return err;
     731           0 :   }
     732           0 :   if( fd_bpf_upgradeable_loader_state_is_program_data( programdata_state ) ) {
     733             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1444-L1447 */
     734           0 :     if( FD_UNLIKELY( clock_slot==programdata_state->inner.program_data.slot ) ) {
     735           0 :       fd_log_collector_msg_literal( instr_ctx, "Program was extended in this block already" );
     736           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
     737           0 :     }
     738             : 
     739             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1449-L1455 */
     740           0 :     if( FD_UNLIKELY( !programdata_state->inner.program_data.has_upgrade_authority_address ) ) {
     741           0 :       fd_log_collector_msg_literal( instr_ctx, "Cannot extend ProgramData accounts that are not upgradeable" );
     742           0 :       return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
     743           0 :     }
     744             : 
     745             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1457-L1472 */
     746           0 :     if( check_authority ) {
     747             :       /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1458-L1463 */
     748           0 :       fd_pubkey_t const * authority_key = NULL;
     749           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, AUTHORITY_ACCOUNT_INDEX, &authority_key );
     750           0 :       if( FD_UNLIKELY( err ) ) {
     751           0 :         return err;
     752           0 :       }
     753             : 
     754             :       /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1464-L1467 */
     755           0 :       if( FD_UNLIKELY( !fd_pubkey_eq( &programdata_state->inner.program_data.upgrade_authority_address, authority_key ) ) ) {
     756           0 :         fd_log_collector_msg_literal( instr_ctx, "Incorrect upgrade authority provided" );
     757           0 :         return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
     758           0 :       }
     759             : 
     760             :       /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1468-L1471 */
     761           0 :       if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, AUTHORITY_ACCOUNT_INDEX, &err ) ) ) {
     762             :         /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
     763           0 :         if( FD_UNLIKELY( !!err ) ) return err;
     764           0 :         fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
     765           0 :         return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
     766           0 :       }
     767           0 :     }
     768             : 
     769             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1474 */
     770           0 :     fd_bpf_upgradeable_loader_state_program_data_t * pd = &programdata_state->inner.program_data;
     771           0 :     upgrade_authority_address = pd->has_upgrade_authority_address ? &pd->upgrade_authority_address : NULL;
     772           0 :   } else {
     773             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1476-L1477 */
     774           0 :     fd_log_collector_msg_literal( instr_ctx, "ProgramData state is invalid" );
     775           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
     776           0 :   }
     777             : 
     778             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1480-L1485 */
     779           0 :   fd_rent_t const * rent             = fd_bank_rent_query( instr_ctx->txn_ctx->bank );
     780           0 :   ulong             balance          = fd_borrowed_account_get_lamports( &programdata_account );
     781           0 :   ulong             min_balance      = fd_ulong_max( fd_rent_exempt_minimum_balance( rent, new_len ), 1UL );
     782           0 :   ulong             required_payment = fd_ulong_sat_sub( min_balance, balance );
     783             : 
     784             :   /* Borrowed accounts need to be dropped before native invocations. Note:
     785             :      the programdata account is manually released and acquired within the
     786             :      extend instruction to preserve the local variable scoping to maintain
     787             :      readability. The scoped macro still successfully handles the case of
     788             :      freeing a write lock in case of an early termination. */
     789             : 
     790             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1488 */
     791           0 :   fd_borrowed_account_drop( &programdata_account );
     792             : 
     793             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1492-L1502 */
     794           0 :   if( FD_UNLIKELY( required_payment>0UL ) ) {
     795             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1493-L1496 */
     796           0 :     fd_pubkey_t const * payer_key = NULL;
     797           0 :     err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, optional_payer_account_index, &payer_key );
     798           0 :     if( FD_UNLIKELY( err ) ) {
     799           0 :       return err;
     800           0 :     }
     801             : 
     802             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1498-L1501 */
     803           0 :     uchar instr_data[FD_TXN_MTU];
     804           0 :     fd_system_program_instruction_t instr = {
     805           0 :       .discriminant = fd_system_program_instruction_enum_transfer,
     806           0 :       .inner = {
     807           0 :         .transfer = required_payment
     808           0 :       }
     809           0 :     };
     810             : 
     811           0 :     fd_bincode_encode_ctx_t encode_ctx = {
     812           0 :       .data    = instr_data,
     813           0 :       .dataend = instr_data + FD_TXN_MTU
     814           0 :     };
     815             : 
     816             :     // This should never fail.
     817           0 :     int err = fd_system_program_instruction_encode( &instr, &encode_ctx );
     818           0 :     if( FD_UNLIKELY( err ) ) {
     819           0 :       return FD_EXECUTOR_INSTR_ERR_FATAL;
     820           0 :     }
     821             : 
     822           0 :     fd_vm_rust_account_meta_t * acct_metas = (fd_vm_rust_account_meta_t *)
     823           0 :                                               fd_spad_alloc( instr_ctx->txn_ctx->spad,
     824           0 :                                                              FD_VM_RUST_ACCOUNT_META_ALIGN,
     825           0 :                                                              2UL * sizeof(fd_vm_rust_account_meta_t) );
     826           0 :     fd_native_cpi_create_account_meta( payer_key,       1UL, 1UL, &acct_metas[ 0UL ] );
     827           0 :     fd_native_cpi_create_account_meta( programdata_key, 0UL, 1UL, &acct_metas[ 1UL ] );
     828             : 
     829           0 :     err = fd_native_cpi_native_invoke( instr_ctx,
     830           0 :                                         &fd_solana_system_program_id,
     831           0 :                                         instr_data,
     832           0 :                                         FD_TXN_MTU,
     833           0 :                                         acct_metas,
     834           0 :                                         2UL,
     835           0 :                                         NULL,
     836           0 :                                         0UL );
     837           0 :     if( FD_UNLIKELY( err ) ) {
     838           0 :       return err;
     839           0 :     }
     840           0 :   }
     841             : 
     842             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1506-L1507 */
     843           0 :   FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, PROGRAM_DATA_ACCOUNT_INDEX, &programdata_account );
     844             : 
     845             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1508 */
     846           0 :   err = fd_borrowed_account_set_data_length( &programdata_account, new_len );
     847           0 :   if( FD_UNLIKELY( err ) ) {
     848           0 :     return err;
     849           0 :   }
     850             : 
     851             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1510 */
     852           0 :   ulong programdata_data_offset = PROGRAMDATA_METADATA_SIZE;
     853             : 
     854             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1517-L1520 */
     855           0 :   if( FD_UNLIKELY( programdata_data_offset>fd_borrowed_account_get_data_len( &programdata_account ) ) ) {
     856           0 :     return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
     857           0 :   }
     858           0 :   uchar const * programdata_data = fd_borrowed_account_get_data( &programdata_account ) + programdata_data_offset;
     859           0 :   ulong         programdata_size = new_len - PROGRAMDATA_METADATA_SIZE;
     860             : 
     861             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1512-L1522 */
     862           0 :   err = fd_deploy_program( instr_ctx, program_account.acct->pubkey, programdata_data, programdata_size, instr_ctx->txn_ctx->spad );
     863           0 :   if( FD_UNLIKELY( err ) ) {
     864           0 :     return err;
     865           0 :   }
     866             : 
     867             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1523 */
     868           0 :   fd_borrowed_account_drop( &programdata_account );
     869             : 
     870             :   /* Setting the discriminant and upgrade authority address here can likely
     871             :      be a no-op because these values shouldn't change. These can probably be
     872             :      removed, but can help to mirror against Agave client's implementation.
     873             :      The set_state function also contains an ownership check. */
     874             : 
     875             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1525-L1526 */
     876           0 :   FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &programdata_account );
     877             : 
     878             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1527-L1530 */
     879           0 :   programdata_state->discriminant            = fd_bpf_upgradeable_loader_state_enum_program_data;
     880           0 :   programdata_state->inner.program_data.slot = clock_slot;
     881           0 :   programdata_state->inner.program_data.has_upgrade_authority_address = !!upgrade_authority_address;
     882           0 :   if( upgrade_authority_address ) programdata_state->inner.program_data.upgrade_authority_address = *upgrade_authority_address;
     883             : 
     884           0 :   err = fd_bpf_loader_v3_program_set_state( &programdata_account, programdata_state );
     885           0 :   if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
     886           0 :     return err;
     887           0 :   }
     888             : 
     889             :   /* Max msg_sz: 41 - 2 + 20 = 57 < 127 => we can use printf
     890             :      https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1532-L1536 */
     891           0 :   fd_log_collector_printf_dangerous_max_127( instr_ctx,
     892           0 :     "Extended ProgramData account by %u bytes", additional_bytes );
     893             : 
     894             :   /* programdata account is dropped when it goes out of scope */
     895             : 
     896           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     897             : 
     898           0 :   #undef PROGRAM_DATA_ACCOUNT_INDEX
     899           0 :   #undef PROGRAM_ACCOUNT_INDEX
     900           0 :   #undef AUTHORITY_ACCOUNT_INDEX
     901           0 : }
     902             : 
     903             : /* https://github.com/anza-xyz/agave/blob/77daab497df191ef485a7ad36ed291c1874596e5/programs/bpf_loader/src/lib.rs#L566-L1444 */
     904             : static int
     905           0 : process_loader_upgradeable_instruction( fd_exec_instr_ctx_t * instr_ctx ) {
     906           0 :   uchar const * data = instr_ctx->instr->data;
     907           0 :   fd_spad_t *   spad = instr_ctx->txn_ctx->spad;
     908             : 
     909           0 :   int err;
     910           0 :   fd_bpf_upgradeable_loader_program_instruction_t * instruction =
     911           0 :     fd_bincode_decode_spad(
     912           0 :       bpf_upgradeable_loader_program_instruction, spad,
     913           0 :       data,
     914           0 :       instr_ctx->instr->data_sz>FD_TXN_MTU ? FD_TXN_MTU: instr_ctx->instr->data_sz,
     915           0 :       &err );
     916           0 :   if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
     917           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
     918           0 :   }
     919             : 
     920             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/programs/bpf_loader/src/lib.rs#L510 */
     921           0 :   fd_pubkey_t const * program_id = NULL;
     922           0 :   err = fd_exec_instr_ctx_get_last_program_key( instr_ctx, &program_id );
     923           0 :   if( FD_UNLIKELY( err ) ) {
     924           0 :     return err;
     925           0 :   }
     926             : 
     927           0 :   switch( instruction->discriminant ) {
     928             :     /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L476-L493 */
     929           0 :     case fd_bpf_upgradeable_loader_program_instruction_enum_initialize_buffer: {
     930           0 :       if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 2U ) ) ) {
     931           0 :         return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
     932           0 :       }
     933             : 
     934             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L479 */
     935           0 :       fd_guarded_borrowed_account_t buffer = {0};
     936           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &buffer );
     937           0 :       fd_bpf_upgradeable_loader_state_t * buffer_state = fd_bpf_loader_program_get_state( buffer.acct,
     938           0 :                                                                                           spad,
     939           0 :                                                                                           &err );
     940           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
     941           0 :         return err;
     942           0 :       }
     943             : 
     944           0 :       if( FD_UNLIKELY( !fd_bpf_upgradeable_loader_state_is_uninitialized( buffer_state ) ) ) {
     945           0 :         fd_log_collector_msg_literal( instr_ctx, "Buffer account is already initialized" );
     946           0 :         return FD_EXECUTOR_INSTR_ERR_ACC_ALREADY_INITIALIZED;
     947           0 :       }
     948             : 
     949             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L487-L489 */
     950           0 :       fd_pubkey_t const * authority_key = NULL;
     951           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &authority_key );
     952           0 :       if( FD_UNLIKELY( err ) ) {
     953           0 :         return err;
     954           0 :       }
     955             : 
     956           0 :       buffer_state->discriminant                       = fd_bpf_upgradeable_loader_state_enum_buffer;
     957           0 :       buffer_state->inner.buffer.has_authority_address = 1;
     958           0 :       buffer_state->inner.buffer.authority_address     = *authority_key;
     959             : 
     960           0 :       err = fd_bpf_loader_v3_program_set_state( &buffer, buffer_state );
     961           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
     962           0 :         return err;
     963           0 :       }
     964             : 
     965             :       /* implicit drop of buffer account */
     966             : 
     967           0 :       break;
     968           0 :     }
     969             :     /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L494-L525 */
     970           0 :     case fd_bpf_upgradeable_loader_program_instruction_enum_write: {
     971           0 :       if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 2U ) ) ) {
     972           0 :         return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
     973           0 :       }
     974             : 
     975             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L497 */
     976           0 :       fd_guarded_borrowed_account_t buffer = {0};
     977           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &buffer );
     978             : 
     979           0 :        fd_bpf_upgradeable_loader_state_t * loader_state = fd_bpf_loader_program_get_state( buffer.acct,
     980           0 :                                                                                            spad,
     981           0 :                                                                                            &err );
     982           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
     983           0 :         return err;
     984           0 :       }
     985             : 
     986           0 :       if( fd_bpf_upgradeable_loader_state_is_buffer( loader_state ) ) {
     987           0 :         if( FD_UNLIKELY( !loader_state->inner.buffer.has_authority_address ) ) {
     988           0 :           fd_log_collector_msg_literal( instr_ctx, "Buffer is immutable" );
     989           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
     990           0 :         }
     991             : 
     992             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L505-L507 */
     993           0 :         fd_pubkey_t const * authority_key = NULL;
     994           0 :         err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &authority_key );
     995           0 :         if( FD_UNLIKELY( err ) ) {
     996           0 :           return err;
     997           0 :         }
     998             : 
     999           0 :         if( FD_UNLIKELY( !fd_pubkey_eq( &loader_state->inner.buffer.authority_address, authority_key ) ) ) {
    1000           0 :           fd_log_collector_msg_literal( instr_ctx, "Incorrect buffer authority provided" );
    1001           0 :           return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
    1002           0 :         }
    1003           0 :         if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 1UL, &err ) ) ) {
    1004             :           /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
    1005           0 :           if( FD_UNLIKELY( !!err ) ) return err;
    1006           0 :           fd_log_collector_msg_literal( instr_ctx, "Buffer authority did not sign" );
    1007           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    1008           0 :         }
    1009           0 :       } else {
    1010           0 :         fd_log_collector_msg_literal( instr_ctx, "Invalid Buffer account" );
    1011           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    1012           0 :       }
    1013             : 
    1014             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L520 */
    1015           0 :       fd_borrowed_account_drop( &buffer );
    1016             : 
    1017           0 :       ulong program_data_offset = fd_ulong_sat_add( BUFFER_METADATA_SIZE, instruction->inner.write.offset );
    1018           0 :       err = write_program_data( instr_ctx,
    1019           0 :                                 0UL,
    1020           0 :                                 program_data_offset,
    1021           0 :                                 instruction->inner.write.bytes,
    1022           0 :                                 instruction->inner.write.bytes_len );
    1023           0 :       if( FD_UNLIKELY( err ) ) {
    1024           0 :         return err;
    1025           0 :       }
    1026             : 
    1027           0 :       break;
    1028           0 :     }
    1029             :     /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L526-L702 */
    1030           0 :     case fd_bpf_upgradeable_loader_program_instruction_enum_deploy_with_max_data_len: {
    1031             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L527-L541 */
    1032           0 :       if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 4U ) ) ) {
    1033           0 :         return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
    1034           0 :       }
    1035             : 
    1036             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L529-L534 */
    1037           0 :       fd_pubkey_t const * payer_key       = NULL;
    1038           0 :       fd_pubkey_t const * programdata_key = NULL;
    1039             : 
    1040           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 0UL, &payer_key );
    1041           0 :       if( FD_UNLIKELY( err ) ) {
    1042           0 :         return err;
    1043           0 :       }
    1044             : 
    1045           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &programdata_key );
    1046           0 :       if( FD_UNLIKELY( err ) ) {
    1047           0 :         return err;
    1048           0 :       }
    1049             : 
    1050             :       /* rent is accessed directly from the epoch bank and the clock from the
    1051             :         slot context. However, a check must be done to make sure that the
    1052             :         sysvars are correctly included in the set of transaction accounts. */
    1053           0 :       err = fd_sysvar_instr_acct_check( instr_ctx, 4UL, &fd_sysvar_rent_id );
    1054           0 :       if( FD_UNLIKELY( err ) ) {
    1055           0 :         return err;
    1056           0 :       }
    1057           0 :       err = fd_sysvar_instr_acct_check( instr_ctx, 5UL, &fd_sysvar_clock_id );
    1058           0 :       if( FD_UNLIKELY( err ) ) {
    1059           0 :         return err;
    1060           0 :       }
    1061             : 
    1062           0 :       fd_sol_sysvar_clock_t clock_;
    1063           0 :       fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( instr_ctx->sysvar_cache, &clock_ );
    1064           0 :       if( FD_UNLIKELY( !clock ) ) {
    1065           0 :         return FD_EXECUTOR_INSTR_ERR_GENERIC_ERR;
    1066           0 :       }
    1067             : 
    1068             :       /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L538 */
    1069           0 :       if( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 8U ) ) {
    1070           0 :         return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
    1071           0 :       }
    1072             : 
    1073             :       /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L539-L541 */
    1074           0 :       fd_pubkey_t const * authority_key = NULL;
    1075           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 7UL, &authority_key );
    1076           0 :       if( FD_UNLIKELY( err ) ) return err;
    1077             : 
    1078             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L542-L560 */
    1079             :       /* Verify Program account */
    1080             : 
    1081           0 :       fd_bpf_upgradeable_loader_state_t * loader_state   = NULL;
    1082           0 :       fd_pubkey_t *                       new_program_id = NULL;
    1083           0 :       fd_rent_t const *                   rent           = fd_bank_rent_query( instr_ctx->txn_ctx->bank );
    1084             : 
    1085             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L545 */
    1086           0 :       fd_guarded_borrowed_account_t program = {0};
    1087           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &program );
    1088             : 
    1089           0 :       loader_state = fd_bpf_loader_program_get_state( program.acct,
    1090           0 :                                                       spad,
    1091           0 :                                                       &err );
    1092             : 
    1093           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1094           0 :         return err;
    1095           0 :       }
    1096           0 :       if( FD_UNLIKELY( !fd_bpf_upgradeable_loader_state_is_uninitialized( loader_state ) ) ) {
    1097           0 :         fd_log_collector_msg_literal( instr_ctx, "Program account already initialized" );
    1098           0 :         return FD_EXECUTOR_INSTR_ERR_ACC_ALREADY_INITIALIZED;
    1099           0 :       }
    1100           0 :       if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &program )<SIZE_OF_PROGRAM ) ) {
    1101           0 :         fd_log_collector_msg_literal( instr_ctx, "Program account too small" );
    1102           0 :         return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
    1103           0 :       }
    1104           0 :       if( FD_UNLIKELY( fd_borrowed_account_get_lamports( &program )<
    1105           0 :                        fd_rent_exempt_minimum_balance( rent, fd_borrowed_account_get_data_len( &program ) ) ) ) {
    1106           0 :         fd_log_collector_msg_literal( instr_ctx, "Program account not rent-exempt" );
    1107           0 :         return FD_EXECUTOR_INSTR_ERR_EXECUTABLE_ACCOUNT_NOT_RENT_EXEMPT;
    1108           0 :       }
    1109           0 :       new_program_id = program.acct->pubkey;
    1110             : 
    1111             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L560 */
    1112           0 :       fd_borrowed_account_drop( &program );
    1113             : 
    1114             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L561-L600 */
    1115             :       /* Verify Buffer account */
    1116             : 
    1117           0 :       fd_pubkey_t * buffer_key       = NULL;
    1118           0 :       ulong buffer_data_offset       = 0UL;
    1119           0 :       ulong buffer_data_len          = 0UL;
    1120           0 :       ulong programdata_len          = 0UL;
    1121             : 
    1122             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L564-L565 */
    1123           0 :       fd_guarded_borrowed_account_t buffer = {0};
    1124           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 3UL, &buffer );
    1125             : 
    1126           0 :       fd_bpf_upgradeable_loader_state_t * buffer_state = fd_bpf_loader_program_get_state( buffer.acct, spad, &err );
    1127           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1128           0 :         return err;
    1129           0 :       }
    1130             : 
    1131           0 :       if( fd_bpf_upgradeable_loader_state_is_buffer( buffer_state ) ) {
    1132           0 :         if( FD_UNLIKELY( (authority_key==NULL) != (!buffer_state->inner.buffer.has_authority_address) ||
    1133           0 :             (authority_key!=NULL && !fd_pubkey_eq( &buffer_state->inner.buffer.authority_address, authority_key ) ) ) ) {
    1134           0 :           fd_log_collector_msg_literal( instr_ctx, "Buffer and upgrade authority don't match" );
    1135           0 :           return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
    1136           0 :         }
    1137           0 :         if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 7UL, &err ) ) ) {
    1138             :           /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
    1139           0 :           if( FD_UNLIKELY( !!err ) ) return err;
    1140           0 :           fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
    1141           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    1142           0 :         }
    1143           0 :       } else {
    1144           0 :         fd_log_collector_msg_literal( instr_ctx, "Invalid Buffer account" );
    1145           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1146           0 :       }
    1147           0 :       buffer_key         = buffer.acct->pubkey;
    1148           0 :       buffer_data_offset = BUFFER_METADATA_SIZE;
    1149           0 :       buffer_data_len    = fd_ulong_sat_sub( fd_borrowed_account_get_data_len( &buffer ), buffer_data_offset );
    1150             :       /* UpgradeableLoaderState::size_of_program_data( max_data_len ) */
    1151           0 :       programdata_len    = fd_ulong_sat_add( PROGRAMDATA_METADATA_SIZE,
    1152           0 :                                              instruction->inner.deploy_with_max_data_len.max_data_len );
    1153             : 
    1154           0 :       if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &buffer )<BUFFER_METADATA_SIZE || buffer_data_len==0UL ) ) {
    1155           0 :         fd_log_collector_msg_literal( instr_ctx, "Buffer account too small" );
    1156           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    1157           0 :       }
    1158             : 
    1159           0 :       if( FD_UNLIKELY( instruction->inner.deploy_with_max_data_len.max_data_len<buffer_data_len ) ) {
    1160           0 :         fd_log_collector_msg_literal( instr_ctx, "Max data length is too small to hold Buffer data" );
    1161           0 :         return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
    1162           0 :       }
    1163             : 
    1164           0 :       if( FD_UNLIKELY( programdata_len>MAX_PERMITTED_DATA_LENGTH ) ) {
    1165           0 :         fd_log_collector_msg_literal( instr_ctx, "Max data length is too large" );
    1166           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1167           0 :       }
    1168             : 
    1169             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L590 */
    1170           0 :       fd_borrowed_account_drop( &buffer );
    1171             : 
    1172             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L602-L608 */
    1173             :       /* Create ProgramData account */
    1174             : 
    1175           0 :       fd_pubkey_t derived_address[ 1UL ];
    1176           0 :       uchar const * seeds[ 1UL ];
    1177           0 :       seeds[ 0UL ]    = (uchar const *)new_program_id;
    1178           0 :       ulong seed_sz   = sizeof(fd_pubkey_t);
    1179           0 :       uchar bump_seed = 0;
    1180           0 :       err = fd_pubkey_find_program_address( program_id, 1UL, seeds, &seed_sz, derived_address,
    1181           0 :                                             &bump_seed, &instr_ctx->txn_ctx->custom_err );
    1182           0 :       if( FD_UNLIKELY( err ) ) {
    1183             :         /* TODO: We should handle these errors more gracefully instead of just killing the client (e.g. excluding the transaction
    1184             :            from the block). */
    1185           0 :         FD_LOG_ERR(( "Unable to find a viable program address bump seed" )); // Solana panics, error code is undefined
    1186           0 :         return err;
    1187           0 :       }
    1188           0 :       if( FD_UNLIKELY( memcmp( derived_address, programdata_key, sizeof(fd_pubkey_t) ) ) ) {
    1189           0 :         fd_log_collector_msg_literal( instr_ctx, "ProgramData address is not derived" );
    1190           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1191           0 :       }
    1192             : 
    1193             :       /* Drain the Buffer account to payer before paying for programdata account in a local scope
    1194             :          https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L612-L628 */
    1195             : 
    1196           0 :       do {
    1197             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L615 */
    1198           0 :         fd_guarded_borrowed_account_t payer = {0};
    1199           0 :         FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &payer );
    1200             : 
    1201             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L613 */
    1202           0 :         fd_guarded_borrowed_account_t buffer = {0};
    1203           0 :         FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 3UL, &buffer );
    1204             : 
    1205           0 :         err = fd_borrowed_account_checked_add_lamports( &payer, fd_borrowed_account_get_lamports( &buffer ) );
    1206           0 :         if( FD_UNLIKELY( err ) ) {
    1207           0 :           return err;
    1208           0 :         }
    1209           0 :         err = fd_borrowed_account_set_lamports( &buffer, 0UL );
    1210           0 :         if( FD_UNLIKELY( err ) ) {
    1211           0 :           return err;
    1212           0 :         }
    1213           0 :       } while (0);
    1214             : 
    1215             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L628-L642 */
    1216             :       /* Pass an extra account to avoid the overly strict unbalanced instruction error */
    1217             :       /* Invoke the system program to create the new account */
    1218           0 :       uchar instr_data[FD_TXN_MTU];
    1219           0 :       fd_system_program_instruction_create_account_t create_acct = {
    1220           0 :         .lamports = fd_rent_exempt_minimum_balance( rent, programdata_len ),
    1221           0 :         .space    = programdata_len,
    1222           0 :         .owner    = *program_id,
    1223           0 :       };
    1224           0 :       if( !create_acct.lamports ) {
    1225           0 :         create_acct.lamports = 1UL;
    1226           0 :       }
    1227             : 
    1228           0 :       fd_system_program_instruction_t instr = {
    1229           0 :         .discriminant = fd_system_program_instruction_enum_create_account,
    1230           0 :         .inner = {
    1231           0 :           .create_account = create_acct,
    1232           0 :         }
    1233           0 :       };
    1234             : 
    1235           0 :       fd_bincode_encode_ctx_t encode_ctx = {
    1236           0 :         .data    = instr_data,
    1237           0 :         .dataend = instr_data + FD_TXN_MTU
    1238           0 :       };
    1239             : 
    1240             :       // This should never fail.
    1241           0 :       int err = fd_system_program_instruction_encode( &instr, &encode_ctx );
    1242           0 :       if( FD_UNLIKELY( err ) ) {
    1243           0 :         return FD_EXECUTOR_INSTR_ERR_FATAL;
    1244           0 :       }
    1245             : 
    1246           0 :       fd_vm_rust_account_meta_t * acct_metas = (fd_vm_rust_account_meta_t*)
    1247           0 :                                                 fd_spad_alloc( instr_ctx->txn_ctx->spad,
    1248           0 :                                                                FD_VM_RUST_ACCOUNT_META_ALIGN,
    1249           0 :                                                                3UL * sizeof(fd_vm_rust_account_meta_t) );
    1250           0 :       fd_native_cpi_create_account_meta( payer_key,       1U, 1U, &acct_metas[ 0UL ] );
    1251           0 :       fd_native_cpi_create_account_meta( programdata_key, 1U, 1U, &acct_metas[ 1UL ] );
    1252           0 :       fd_native_cpi_create_account_meta( buffer_key,      0U, 1U, &acct_metas[ 2UL ] );
    1253             : 
    1254             :       /* caller_program_id == program_id */
    1255           0 :       fd_pubkey_t signers[ 1UL ];
    1256           0 :       err = fd_pubkey_derive_pda( program_id, 1UL, seeds, &seed_sz, &bump_seed, signers, &instr_ctx->txn_ctx->custom_err );
    1257           0 :       if( FD_UNLIKELY( err ) ) {
    1258           0 :         return err;
    1259           0 :       }
    1260           0 :       err = fd_native_cpi_native_invoke( instr_ctx,
    1261           0 :                                          &fd_solana_system_program_id,
    1262           0 :                                          instr_data,
    1263           0 :                                          FD_TXN_MTU,
    1264           0 :                                          acct_metas,
    1265           0 :                                          3UL,
    1266           0 :                                          signers,
    1267           0 :                                          1UL );
    1268           0 :       if( FD_UNLIKELY( err ) ) {
    1269           0 :         return err;
    1270           0 :       }
    1271             : 
    1272             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L644-L665 */
    1273             :       /* Load and verify the program bits */
    1274             : 
    1275             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L648-L649 */
    1276           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 3UL, &buffer );
    1277             : 
    1278           0 :       if( FD_UNLIKELY( buffer_data_offset>fd_borrowed_account_get_data_len( &buffer ) ) ) {
    1279           0 :         return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
    1280           0 :       }
    1281             : 
    1282           0 :       const uchar * buffer_data = fd_borrowed_account_get_data( &buffer ) + buffer_data_offset;
    1283             : 
    1284           0 :       err = fd_deploy_program( instr_ctx, program.acct->pubkey, buffer_data, buffer_data_len, instr_ctx->txn_ctx->spad );
    1285           0 :       if( FD_UNLIKELY( err ) ) {
    1286           0 :         return err;
    1287           0 :       }
    1288             : 
    1289             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L657 */
    1290           0 :       fd_borrowed_account_drop( &buffer );
    1291             : 
    1292             :       /* Update the ProgramData account and record the program bits in a local scope
    1293             :          https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L669-L691 */
    1294           0 :       do {
    1295             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L670-L671 */
    1296           0 :         fd_guarded_borrowed_account_t programdata = {0};
    1297           0 :         FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 1UL, &programdata );
    1298             : 
    1299           0 :         fd_bpf_upgradeable_loader_state_t programdata_loader_state = {
    1300           0 :           .discriminant = fd_bpf_upgradeable_loader_state_enum_program_data,
    1301           0 :           .inner.program_data = {
    1302           0 :             .slot                          = clock->slot,
    1303           0 :             .has_upgrade_authority_address = !!authority_key,
    1304           0 :             .upgrade_authority_address     = authority_key ? *authority_key : (fd_pubkey_t){{0}},
    1305           0 :           },
    1306           0 :         };
    1307           0 :         err = fd_bpf_loader_v3_program_set_state( &programdata, &programdata_loader_state );
    1308           0 :         if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1309           0 :           return err;
    1310           0 :         }
    1311             : 
    1312             :         /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L675-L689 */
    1313           0 :         if( FD_UNLIKELY( PROGRAMDATA_METADATA_SIZE+buffer_data_len>fd_borrowed_account_get_data_len( &programdata ) ) ) {
    1314           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
    1315           0 :         }
    1316           0 :         if( FD_UNLIKELY( buffer_data_offset>fd_borrowed_account_get_data_len( &buffer ) ) ) {
    1317           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
    1318           0 :         }
    1319             : 
    1320           0 :         uchar * programdata_data = NULL;
    1321           0 :         ulong   programdata_dlen = 0UL;
    1322           0 :         err = fd_borrowed_account_get_data_mut( &programdata, &programdata_data, &programdata_dlen );
    1323           0 :         if( FD_UNLIKELY( err ) ) {
    1324           0 :           return err;
    1325           0 :         }
    1326             : 
    1327           0 :         uchar *   dst_slice = programdata_data + PROGRAMDATA_METADATA_SIZE;
    1328           0 :         ulong dst_slice_len = buffer_data_len;
    1329             : 
    1330             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L683-L684 */
    1331           0 :         fd_guarded_borrowed_account_t buffer = {0};
    1332           0 :         FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 3UL, &buffer );
    1333             : 
    1334           0 :         if( FD_UNLIKELY( buffer_data_offset>fd_borrowed_account_get_data_len( &buffer ) ) ) {
    1335           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
    1336           0 :         }
    1337           0 :         const uchar * src_slice = fd_borrowed_account_get_data( &buffer ) + buffer_data_offset;
    1338           0 :         fd_memcpy( dst_slice, src_slice, dst_slice_len );
    1339             :         /* Update buffer data length.
    1340             :           BUFFER_METADATA_SIZE == UpgradeableLoaderState::size_of_buffer(0) */
    1341           0 :         err = fd_borrowed_account_set_data_length( &buffer, BUFFER_METADATA_SIZE );
    1342           0 :         if( FD_UNLIKELY( err ) ) {
    1343           0 :           return err;
    1344           0 :         }
    1345           0 :       } while(0);
    1346             : 
    1347             :       /* Max msg_sz: 19 - 2 + 45 = 62 < 127 => we can use printf */
    1348           0 :       fd_log_collector_printf_dangerous_max_127( instr_ctx, "Deployed program %s", FD_BASE58_ENC_32_ALLOCA( program_id ) );
    1349             : 
    1350             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L692-L699 */
    1351             : 
    1352             :       /* Update the Program account
    1353             :          https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L694-L695 */
    1354           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &program );
    1355             : 
    1356           0 :       loader_state->discriminant = fd_bpf_upgradeable_loader_state_enum_program;
    1357           0 :       loader_state->inner.program.programdata_address =  *programdata_key;
    1358           0 :       err = fd_bpf_loader_v3_program_set_state( &program, loader_state );
    1359           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1360           0 :         return err;
    1361           0 :       }
    1362           0 :       err = fd_borrowed_account_set_executable( &program, 1 );
    1363           0 :       if( FD_UNLIKELY( err ) ) {
    1364           0 :         return err;
    1365           0 :       }
    1366             : 
    1367           0 :       FD_LOG_INFO(( "Program deployed %s", FD_BASE58_ENC_32_ALLOCA( program.acct->pubkey ) ));
    1368             : 
    1369             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L700 */
    1370           0 :       fd_borrowed_account_drop( &program );
    1371             : 
    1372           0 :       break;
    1373           0 :     }
    1374             :     /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L703-L891 */
    1375           0 :     case fd_bpf_upgradeable_loader_program_instruction_enum_upgrade: {
    1376             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L704-L714 */
    1377           0 :       if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 3U ) ) ) {
    1378           0 :         return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
    1379           0 :       }
    1380             : 
    1381             :       /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L706-L708 */
    1382           0 :       fd_pubkey_t const * programdata_key = NULL;
    1383           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 0UL, &programdata_key );
    1384           0 :       if( FD_UNLIKELY( err ) ) {
    1385           0 :         return err;
    1386           0 :       }
    1387             : 
    1388             :       /* rent is accessed directly from the epoch bank and the clock from the
    1389             :         slot context. However, a check must be done to make sure that the
    1390             :         sysvars are correctly included in the set of transaction accounts. */
    1391           0 :       err = fd_sysvar_instr_acct_check( instr_ctx, 4UL, &fd_sysvar_rent_id );
    1392           0 :       if( FD_UNLIKELY( err ) ) {
    1393           0 :         return err;
    1394           0 :       }
    1395           0 :       err = fd_sysvar_instr_acct_check( instr_ctx, 5UL, &fd_sysvar_clock_id );
    1396           0 :       if( FD_UNLIKELY( err ) ) {
    1397           0 :         return err;
    1398           0 :       }
    1399             : 
    1400           0 :       if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 7U ) ) ) {
    1401           0 :         return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
    1402           0 :       }
    1403             : 
    1404             :       /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L713-L715 */
    1405           0 :       fd_pubkey_t const * authority_key = NULL;
    1406           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 6UL, &authority_key );
    1407           0 :       if( FD_UNLIKELY( err ) ) return err;
    1408             : 
    1409             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L716-L745 */
    1410             :       /* Verify Program account */
    1411             : 
    1412             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L719-L720 */
    1413           0 :       fd_guarded_borrowed_account_t program = {0};
    1414           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 1UL, &program );
    1415             : 
    1416             :       /* https://github.com/anza-xyz/agave/blob/89872fdb074e6658646b2b57a299984f0059cc84/programs/bpf_loader/src/lib.rs#L758-L765 */
    1417           0 :       if( FD_UNLIKELY( !FD_FEATURE_ACTIVE_BANK( instr_ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) &&
    1418           0 :                        !fd_borrowed_account_is_executable( &program ) ) ) {
    1419           0 :         fd_log_collector_msg_literal( instr_ctx, "Program account not executable" );
    1420           0 :         return FD_EXECUTOR_INSTR_ERR_ACC_NOT_EXECUTABLE;
    1421           0 :       }
    1422           0 :       if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &program ) ) ) {
    1423           0 :         fd_log_collector_msg_literal( instr_ctx, "Program account not writeable" );
    1424           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1425           0 :       }
    1426           0 :       if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &program ), program_id, sizeof(fd_pubkey_t) ) ) ) {
    1427           0 :         fd_log_collector_msg_literal( instr_ctx, "Program account not owned by loader" );
    1428           0 :         return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
    1429           0 :       }
    1430           0 :       fd_bpf_upgradeable_loader_state_t * program_state = fd_bpf_loader_program_get_state( program.acct, spad, &err );
    1431           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1432           0 :         return err;
    1433           0 :       }
    1434           0 :       if( FD_UNLIKELY( fd_bpf_upgradeable_loader_state_is_program( program_state ) ) ) {
    1435           0 :         if( FD_UNLIKELY( memcmp( &program_state->inner.program.programdata_address, programdata_key, sizeof(fd_pubkey_t) ) ) ) {
    1436           0 :           fd_log_collector_msg_literal( instr_ctx, "Program and ProgramData account mismatch" );
    1437           0 :           return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1438           0 :         }
    1439           0 :       } else {
    1440           0 :         fd_log_collector_msg_literal( instr_ctx, "Invalid Program account" );
    1441           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    1442           0 :       }
    1443             : 
    1444             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L746 */
    1445           0 :       fd_borrowed_account_drop( &program );
    1446             : 
    1447             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L747-L773 */
    1448             :       /* Verify Buffer account */
    1449             : 
    1450           0 :       ulong buffer_lamports    = 0UL;
    1451           0 :       ulong buffer_data_offset = 0UL;
    1452           0 :       ulong buffer_data_len    = 0UL;
    1453             : 
    1454             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L750-L751 */
    1455           0 :       fd_guarded_borrowed_account_t buffer = {0};
    1456           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &buffer );
    1457             : 
    1458           0 :       fd_bpf_upgradeable_loader_state_t * buffer_state = fd_bpf_loader_program_get_state( buffer.acct, spad, &err );
    1459           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1460           0 :         return err;
    1461           0 :       }
    1462           0 :       if( fd_bpf_upgradeable_loader_state_is_buffer( buffer_state ) ) {
    1463           0 :         if( FD_UNLIKELY( (authority_key==NULL) != (!buffer_state->inner.buffer.has_authority_address) ||
    1464           0 :             (authority_key!=NULL && !fd_pubkey_eq( &buffer_state->inner.buffer.authority_address, authority_key ) ) ) ) {
    1465           0 :           fd_log_collector_msg_literal( instr_ctx, "Buffer and upgrade authority don't match" );
    1466           0 :           return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
    1467           0 :         }
    1468           0 :         if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 6UL, &err ) ) ) {
    1469             :           /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
    1470           0 :           if( FD_UNLIKELY( !!err ) ) return err;
    1471           0 :           fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
    1472           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    1473           0 :         }
    1474           0 :       } else {
    1475           0 :         fd_log_collector_msg_literal( instr_ctx, "Invalid Buffer account" );
    1476           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1477           0 :       }
    1478           0 :       buffer_lamports    = fd_borrowed_account_get_lamports( &buffer );
    1479           0 :       buffer_data_offset = BUFFER_METADATA_SIZE;
    1480           0 :       buffer_data_len    = fd_ulong_sat_sub( fd_borrowed_account_get_data_len( &buffer ), buffer_data_offset );
    1481           0 :       if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &buffer )<BUFFER_METADATA_SIZE || buffer_data_len==0UL ) ) {
    1482           0 :         fd_log_collector_msg_literal( instr_ctx, "Buffer account too small" );
    1483           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    1484           0 :       }
    1485             : 
    1486             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L774 */
    1487           0 :       fd_borrowed_account_drop( &buffer );
    1488             : 
    1489             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L775-L823 */
    1490             :       /* Verify ProgramData account */
    1491             : 
    1492           0 :       ulong                               programdata_data_offset      = PROGRAMDATA_METADATA_SIZE;
    1493           0 :       fd_bpf_upgradeable_loader_state_t * programdata_state            = NULL;
    1494           0 :       ulong                               programdata_balance_required = 0UL;
    1495             : 
    1496             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L778-L779 */
    1497           0 :       fd_guarded_borrowed_account_t programdata = {0};
    1498           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &programdata );
    1499             : 
    1500           0 :       fd_rent_t const * rent = fd_bank_rent_query( instr_ctx->txn_ctx->bank );
    1501             : 
    1502           0 :       programdata_balance_required = fd_ulong_max( 1UL, fd_rent_exempt_minimum_balance( rent, fd_borrowed_account_get_data_len( &programdata ) ) );
    1503             : 
    1504           0 :       if( FD_UNLIKELY( fd_borrowed_account_get_data_len( &programdata )<fd_ulong_sat_add( PROGRAMDATA_METADATA_SIZE, buffer_data_len ) ) ) {
    1505           0 :         fd_log_collector_msg_literal( instr_ctx, "ProgramData account not large enough" );
    1506           0 :         return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
    1507           0 :       }
    1508           0 :       if( FD_UNLIKELY( fd_ulong_sat_add( fd_borrowed_account_get_lamports( &programdata ), buffer_lamports )<programdata_balance_required ) ) {
    1509           0 :         fd_log_collector_msg_literal( instr_ctx, "Buffer account balance too low to fund upgrade" );
    1510           0 :         return FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS;
    1511           0 :       }
    1512           0 :       programdata_state = fd_bpf_loader_program_get_state( programdata.acct, spad, &err );
    1513           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1514           0 :         return err;
    1515           0 :       }
    1516             : 
    1517           0 :       fd_sol_sysvar_clock_t clock_;
    1518           0 :       fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( instr_ctx->sysvar_cache, &clock_ );
    1519           0 :       if( FD_UNLIKELY( !clock ) ) {
    1520           0 :         return FD_EXECUTOR_INSTR_ERR_GENERIC_ERR;
    1521           0 :       }
    1522             : 
    1523           0 :       if( fd_bpf_upgradeable_loader_state_is_program_data( programdata_state ) ) {
    1524           0 :         if( FD_UNLIKELY( clock->slot==programdata_state->inner.program_data.slot ) ) {
    1525           0 :           fd_log_collector_msg_literal( instr_ctx, "Program was deployed in this block already" );
    1526           0 :           return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1527           0 :         }
    1528           0 :         if( FD_UNLIKELY( !programdata_state->inner.program_data.has_upgrade_authority_address ) ) {
    1529           0 :           fd_log_collector_msg_literal( instr_ctx, "Prrogram not upgradeable" );
    1530           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
    1531           0 :         }
    1532           0 :         if( FD_UNLIKELY( !fd_pubkey_eq( &programdata_state->inner.program_data.upgrade_authority_address, authority_key ) ) ) {
    1533           0 :           fd_log_collector_msg_literal( instr_ctx, "Incorrect upgrade authority provided" );
    1534           0 :           return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
    1535           0 :         }
    1536           0 :         if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 6UL, &err ) ) ) {
    1537             :           /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
    1538           0 :           if( FD_UNLIKELY( !!err ) ) return err;
    1539           0 :           fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
    1540           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    1541           0 :         }
    1542           0 :       } else {
    1543           0 :         fd_log_collector_msg_literal( instr_ctx, "Invalid ProgramData account" );
    1544           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    1545           0 :       }
    1546             : 
    1547             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L824 */
    1548           0 :       fd_borrowed_account_drop( &programdata );
    1549             : 
    1550             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L825-L845 */
    1551             :       /* Load and verify the program bits */
    1552             : 
    1553             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L827-L828 */
    1554           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &buffer );
    1555             : 
    1556           0 :       if( FD_UNLIKELY( buffer_data_offset>fd_borrowed_account_get_data_len( &buffer ) ) ) {
    1557           0 :         return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
    1558           0 :       }
    1559             : 
    1560           0 :       const uchar * buffer_data = fd_borrowed_account_get_data( &buffer ) + buffer_data_offset;
    1561           0 :       err = fd_deploy_program( instr_ctx, program.acct->pubkey, buffer_data, buffer_data_len, instr_ctx->txn_ctx->spad );
    1562           0 :       if( FD_UNLIKELY( err ) ) {
    1563           0 :         return err;
    1564           0 :       }
    1565             : 
    1566             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L836 */
    1567           0 :       fd_borrowed_account_drop( &buffer );
    1568             : 
    1569             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L849-L850 */
    1570           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &programdata );
    1571             : 
    1572             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L846-L874 */
    1573             :       /* Update the ProgramData account, record the upgraded data, and zero the rest in a local scope */
    1574           0 :       do {
    1575           0 :         programdata_state->discriminant                                     = fd_bpf_upgradeable_loader_state_enum_program_data;
    1576           0 :         programdata_state->inner.program_data.slot                          = clock->slot;
    1577           0 :         programdata_state->inner.program_data.has_upgrade_authority_address = 1;
    1578           0 :         programdata_state->inner.program_data.upgrade_authority_address     = *authority_key;
    1579           0 :         err = fd_bpf_loader_v3_program_set_state( &programdata, programdata_state );
    1580           0 :         if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1581           0 :           return err;
    1582           0 :         }
    1583             : 
    1584             :         /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L846-L875 */
    1585             :         /* We want to copy over the data and zero out the rest */
    1586           0 :         if( FD_UNLIKELY( programdata_data_offset+buffer_data_len>fd_borrowed_account_get_data_len( &programdata ) ) ) {
    1587           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
    1588           0 :         }
    1589             : 
    1590           0 :         uchar * programdata_data = NULL;
    1591           0 :         ulong   programdata_dlen = 0UL;
    1592           0 :         err = fd_borrowed_account_get_data_mut( &programdata, &programdata_data, &programdata_dlen );
    1593           0 :         if( FD_UNLIKELY( err ) ) {
    1594           0 :           return err;
    1595           0 :         }
    1596           0 :         uchar * dst_slice     = programdata_data + programdata_data_offset;
    1597           0 :         ulong   dst_slice_len = buffer_data_len;
    1598             : 
    1599             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L863-L864 */
    1600           0 :         fd_guarded_borrowed_account_t buffer = {0};
    1601           0 :         FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &buffer );
    1602             : 
    1603           0 :         if( FD_UNLIKELY( buffer_data_offset>fd_borrowed_account_get_data_len( &buffer ) ) ){
    1604           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
    1605           0 :         }
    1606             : 
    1607           0 :         const uchar * src_slice = fd_borrowed_account_get_data( &buffer ) + buffer_data_offset;
    1608           0 :         fd_memcpy( dst_slice, src_slice, dst_slice_len );
    1609           0 :         fd_memset( dst_slice + dst_slice_len, 0, fd_borrowed_account_get_data_len( &programdata ) - programdata_data_offset - dst_slice_len );
    1610             : 
    1611             :         /* implicit drop of buffer */
    1612           0 :       } while (0);
    1613             : 
    1614             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L876-L891 */
    1615             :       /* Fund ProgramData to rent-exemption, spill the rest */
    1616             : 
    1617             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L878-L879 */
    1618           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 2UL, &buffer );
    1619             : 
    1620             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L880-L881 */
    1621           0 :       fd_guarded_borrowed_account_t spill = {0};
    1622           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 3UL, &spill );
    1623             : 
    1624           0 :       ulong spill_addend = fd_ulong_sat_sub( fd_ulong_sat_add( fd_borrowed_account_get_lamports( &programdata ), buffer_lamports ),
    1625           0 :                                             programdata_balance_required );
    1626           0 :       err = fd_borrowed_account_checked_add_lamports( &spill, spill_addend );
    1627           0 :       if( FD_UNLIKELY( err ) ) {
    1628           0 :         return err;
    1629           0 :       }
    1630           0 :       err = fd_borrowed_account_set_lamports( &buffer, 0UL );
    1631           0 :       if( FD_UNLIKELY( err ) ) {
    1632           0 :         return err;
    1633           0 :       }
    1634           0 :       err = fd_borrowed_account_set_lamports( &programdata, programdata_balance_required );
    1635           0 :       if( FD_UNLIKELY( err ) ) {
    1636           0 :         return err;
    1637           0 :       }
    1638             : 
    1639             :       /* Buffer account set_data_length */
    1640           0 :       err = fd_borrowed_account_set_data_length( &buffer, BUFFER_METADATA_SIZE );
    1641           0 :       if( FD_UNLIKELY( err ) ) {
    1642           0 :         return err;
    1643           0 :       }
    1644             : 
    1645             :       /* buffer is dropped when it goes out of scope */
    1646             :       /* spill is dropped when it goes out of scope */
    1647             :       /* programdata is dropped when it goes out of scope */
    1648             : 
    1649             :       /* Max msg_sz: 19 - 2 + 45 = 62 < 127 => we can use printf */
    1650             :       //TODO: this is likely the incorrect program_id, do we have new_program_id?
    1651           0 :       fd_log_collector_printf_dangerous_max_127( instr_ctx, "Upgraded program %s", FD_BASE58_ENC_32_ALLOCA( program_id ) );
    1652             : 
    1653           0 :       break;
    1654           0 :     }
    1655             :     /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L893-L957 */
    1656           0 :     case fd_bpf_upgradeable_loader_program_instruction_enum_set_authority: {
    1657           0 :       int err;
    1658           0 :       if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 2U ) ) ) {
    1659           0 :         return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
    1660           0 :       }
    1661             : 
    1662             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L896-L897 */
    1663           0 :       fd_guarded_borrowed_account_t account = {0};
    1664           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &account );
    1665             : 
    1666             :       /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L898-L900 */
    1667           0 :       fd_pubkey_t const * present_authority_key = NULL;
    1668           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &present_authority_key );
    1669           0 :       if( FD_UNLIKELY( err ) ) {
    1670           0 :         return err;
    1671           0 :       }
    1672             : 
    1673             :       /* Don't check the error here because the new_authority key is allowed to be NULL until further checks.
    1674             :          https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L901-L906 */
    1675           0 :       fd_pubkey_t const * new_authority = NULL;
    1676           0 :       fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 2UL, &new_authority );
    1677             : 
    1678           0 :       fd_bpf_upgradeable_loader_state_t * account_state = fd_bpf_loader_program_get_state( account.acct,
    1679           0 :                                                                                            spad,
    1680           0 :                                                                                            &err );
    1681           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1682           0 :         return err;
    1683           0 :       }
    1684             : 
    1685           0 :       if( fd_bpf_upgradeable_loader_state_is_buffer( account_state ) ) {
    1686           0 :         if( FD_UNLIKELY( !new_authority ) ) {
    1687           0 :           fd_log_collector_msg_literal( instr_ctx, "Buffer authority is not optional" );
    1688           0 :           return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
    1689           0 :         }
    1690           0 :         if( FD_UNLIKELY( !account_state->inner.buffer.has_authority_address ) ) {
    1691           0 :           fd_log_collector_msg_literal( instr_ctx, "Buffer is immutable" );
    1692           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
    1693           0 :         }
    1694           0 :         if( FD_UNLIKELY( !fd_pubkey_eq( &account_state->inner.buffer.authority_address, present_authority_key ) ) ) {
    1695           0 :           fd_log_collector_msg_literal( instr_ctx, "Incorrect buffer authority provided" );
    1696           0 :           return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
    1697           0 :         }
    1698           0 :         if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 1UL, &err ) ) ) {
    1699             :           /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
    1700           0 :           if( FD_UNLIKELY( !!err ) ) return err;
    1701           0 :           fd_log_collector_msg_literal( instr_ctx, "Buffer authority did not sign" );
    1702           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    1703           0 :         }
    1704             : 
    1705             :         /* copy in the authority public key into the authority address.
    1706             :            https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L926-L928 */
    1707           0 :         account_state->inner.buffer.has_authority_address = !!new_authority;
    1708           0 :         if( new_authority ) {
    1709           0 :           account_state->inner.buffer.authority_address = *new_authority;
    1710           0 :         }
    1711             : 
    1712           0 :         err = fd_bpf_loader_v3_program_set_state( &account, account_state );
    1713           0 :         if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1714           0 :           return err;
    1715           0 :         }
    1716           0 :       } else if( fd_bpf_upgradeable_loader_state_is_program_data( account_state ) ) {
    1717           0 :         if( FD_UNLIKELY( !account_state->inner.program_data.has_upgrade_authority_address ) ) {
    1718           0 :           fd_log_collector_msg_literal( instr_ctx, "Program not upgradeable" );
    1719           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
    1720           0 :         }
    1721           0 :         if( FD_UNLIKELY( !fd_pubkey_eq( &account_state->inner.program_data.upgrade_authority_address, present_authority_key ) ) ) {
    1722           0 :           fd_log_collector_msg_literal( instr_ctx, "Incorrect upgrade authority provided" );
    1723           0 :           return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
    1724           0 :         }
    1725           0 :         if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 1UL, &err ) ) ) {
    1726             :           /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
    1727           0 :           if( FD_UNLIKELY( !!err ) ) return err;
    1728           0 :           fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
    1729           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    1730           0 :         }
    1731             : 
    1732             :         /* copy in the authority public key into the upgrade authority address.
    1733             :            https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L946-L949 */
    1734           0 :         account_state->inner.program_data.has_upgrade_authority_address = !!new_authority;
    1735           0 :         if( new_authority ) {
    1736           0 :           account_state->inner.program_data.upgrade_authority_address = *new_authority;
    1737           0 :         }
    1738             : 
    1739           0 :         err = fd_bpf_loader_v3_program_set_state( &account, account_state );
    1740           0 :         if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1741           0 :           return err;
    1742           0 :         }
    1743           0 :       } else {
    1744           0 :         fd_log_collector_msg_literal( instr_ctx, "Account does not support authorities" );
    1745           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1746           0 :       }
    1747             : 
    1748             :       /* Max msg_sz: 16 - 2 + 45 = 59 < 127 => we can use printf */
    1749           0 :       if( new_authority ) {
    1750           0 :         fd_log_collector_printf_dangerous_max_127( instr_ctx, "New authority Some(%s)", FD_BASE58_ENC_32_ALLOCA( new_authority ) );
    1751           0 :       } else {
    1752           0 :         fd_log_collector_printf_dangerous_max_127( instr_ctx, "New authority None" );
    1753           0 :       }
    1754             : 
    1755             :       /* implicit drop of account */
    1756             : 
    1757           0 :       break;
    1758           0 :     }
    1759             :     /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L958-L1030 */
    1760           0 :     case fd_bpf_upgradeable_loader_program_instruction_enum_set_authority_checked: {
    1761           0 :       int err;
    1762           0 :       if( !FD_FEATURE_ACTIVE_BANK( instr_ctx->txn_ctx->bank, enable_bpf_loader_set_authority_checked_ix ) ) {
    1763           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
    1764           0 :       }
    1765             : 
    1766           0 :       if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 3U ) ) ) {
    1767           0 :         return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
    1768           0 :       }
    1769             : 
    1770             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L968-L969 */
    1771           0 :       fd_guarded_borrowed_account_t account = {0};
    1772           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &account );
    1773             : 
    1774             :       /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L970-L975 */
    1775           0 :       fd_pubkey_t const * present_authority_key = NULL;
    1776           0 :       fd_pubkey_t const * new_authority_key     = NULL;
    1777             : 
    1778           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &present_authority_key );
    1779           0 :       if( FD_UNLIKELY( err ) ) return err;
    1780             : 
    1781           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 2UL, &new_authority_key );
    1782           0 :       if( FD_UNLIKELY( err ) ) return err;
    1783             : 
    1784           0 :       fd_bpf_upgradeable_loader_state_t * account_state = fd_bpf_loader_program_get_state( account.acct, spad, &err );
    1785           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1786           0 :         return err;
    1787           0 :       }
    1788             : 
    1789           0 :       if( fd_bpf_upgradeable_loader_state_is_buffer( account_state ) ) {
    1790           0 :         if( FD_UNLIKELY( !account_state->inner.buffer.has_authority_address ) ) {
    1791           0 :           fd_log_collector_msg_literal( instr_ctx, "Buffer is immutable" );
    1792           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
    1793           0 :         }
    1794           0 :         if( FD_UNLIKELY( !fd_pubkey_eq( &account_state->inner.buffer.authority_address, present_authority_key ) ) ) {
    1795           0 :           fd_log_collector_msg_literal( instr_ctx, "Incorrect buffer authority provided" );
    1796           0 :           return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
    1797           0 :         }
    1798           0 :         int instr_err_code = 0;
    1799           0 :         if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 1UL, &instr_err_code ) ) ) {
    1800             :           /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
    1801           0 :           if( FD_UNLIKELY( !!instr_err_code ) ) return instr_err_code;
    1802           0 :           fd_log_collector_msg_literal( instr_ctx, "Buffer authority did not sign" );
    1803           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    1804           0 :         }
    1805           0 :         instr_err_code = 0;
    1806           0 :         if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 2UL, &instr_err_code ) ) ) {
    1807             :           /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
    1808           0 :           if( FD_UNLIKELY( !!instr_err_code ) ) return instr_err_code;
    1809           0 :           fd_log_collector_msg_literal( instr_ctx, "New authority did not sign" );
    1810           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    1811           0 :         }
    1812           0 :         account_state->inner.buffer.has_authority_address = 1;
    1813           0 :         account_state->inner.buffer.authority_address     = *new_authority_key;
    1814           0 :         err = fd_bpf_loader_v3_program_set_state( &account, account_state );
    1815           0 :         if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1816           0 :           return err;
    1817           0 :         }
    1818           0 :       } else if( fd_bpf_upgradeable_loader_state_is_program_data( account_state ) ) {
    1819           0 :         if( FD_UNLIKELY( !account_state->inner.program_data.has_upgrade_authority_address ) ) {
    1820           0 :           fd_log_collector_msg_literal( instr_ctx, "Program not upgradeable" );
    1821           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE;
    1822           0 :         }
    1823           0 :         if( FD_UNLIKELY( !fd_pubkey_eq( &account_state->inner.program_data.upgrade_authority_address, present_authority_key ) ) ) {
    1824           0 :           fd_log_collector_msg_literal( instr_ctx, "Incorrect upgrade authority provided" );
    1825           0 :           return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
    1826           0 :         }
    1827           0 :         int instr_err_code = 0;
    1828           0 :         if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 1UL, &instr_err_code ) ) ) {
    1829             :           /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
    1830           0 :           if( FD_UNLIKELY( !!instr_err_code ) ) return instr_err_code;
    1831           0 :           fd_log_collector_msg_literal( instr_ctx, "Upgrade authority did not sign" );
    1832           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    1833           0 :         }
    1834           0 :         instr_err_code = 0;
    1835           0 :         if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( instr_ctx->instr, 2UL, &instr_err_code ) ) ) {
    1836             :           /* https://github.com/anza-xyz/agave/blob/v3.0.3/transaction-context/src/lib.rs#L789 */
    1837           0 :           if( FD_UNLIKELY( !!instr_err_code ) ) return instr_err_code;
    1838           0 :           fd_log_collector_msg_literal( instr_ctx, "New authority did not sign" );
    1839           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    1840           0 :         }
    1841           0 :         account_state->inner.program_data.has_upgrade_authority_address = 1;
    1842           0 :         account_state->inner.program_data.upgrade_authority_address     = *new_authority_key;
    1843           0 :         err = fd_bpf_loader_v3_program_set_state( &account, account_state );
    1844           0 :         if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1845           0 :           return err;
    1846           0 :         }
    1847           0 :       } else {
    1848           0 :         fd_log_collector_msg_literal( instr_ctx, "Account does not support authorities" );
    1849           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1850           0 :       }
    1851             : 
    1852             :       /* Max msg_sz: 16 - 2 + 45 = 59 < 127 => we can use printf */
    1853           0 :       fd_log_collector_printf_dangerous_max_127( instr_ctx, "New authority %s", FD_BASE58_ENC_32_ALLOCA( new_authority_key ) );
    1854             : 
    1855             :       /* implicit drop of account */
    1856             : 
    1857           0 :       break;
    1858           0 :     }
    1859             :     /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1031-L1134 */
    1860           0 :     case fd_bpf_upgradeable_loader_program_instruction_enum_close: {
    1861           0 :       int err;
    1862             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1032-L1046 */
    1863           0 :       if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 2U ) ) ) {
    1864           0 :         return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
    1865           0 :       }
    1866             : 
    1867             :       /* It's safe to directly access the instruction accounts because we already checked for two
    1868             :          instruction accounts previously.
    1869             :          https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L1034-L1035 */
    1870           0 :       if( FD_UNLIKELY( instr_ctx->instr->accounts[ 0UL ].index_in_transaction ==
    1871           0 :                        instr_ctx->instr->accounts[ 1UL ].index_in_transaction ) ) {
    1872           0 :         fd_log_collector_msg_literal( instr_ctx, "Recipient is the same as the account being closed" );
    1873           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1874           0 :       }
    1875             : 
    1876             :       /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1043-L1044 */
    1877           0 :       fd_guarded_borrowed_account_t close_account = {0};
    1878           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &close_account );
    1879             : 
    1880           0 :       fd_pubkey_t * close_key = close_account.acct->pubkey;
    1881           0 :       fd_bpf_upgradeable_loader_state_t * close_account_state = fd_bpf_loader_program_get_state( close_account.acct, spad, &err );
    1882           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1883           0 :         return err;
    1884           0 :       }
    1885             :       /* Close account set data length */
    1886           0 :       err = fd_borrowed_account_set_data_length( &close_account, SIZE_OF_UNINITIALIZED );
    1887           0 :       if( FD_UNLIKELY( err ) ) {
    1888           0 :         return err;
    1889           0 :       }
    1890             : 
    1891             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1049-L1056 */
    1892           0 :       if( fd_bpf_upgradeable_loader_state_is_uninitialized( close_account_state ) ) {
    1893             : 
    1894             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1050-L1051 */
    1895           0 :         fd_guarded_borrowed_account_t recipient_account = {0};
    1896           0 :         FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 1UL, &recipient_account );
    1897             : 
    1898           0 :         err = fd_borrowed_account_checked_add_lamports( &recipient_account, fd_borrowed_account_get_lamports( &close_account ) );
    1899           0 :         if( FD_UNLIKELY( err ) ) {
    1900           0 :           return err;
    1901           0 :         }
    1902           0 :         err = fd_borrowed_account_set_lamports( &close_account, 0UL );
    1903           0 :         if( FD_UNLIKELY( err ) ) {
    1904           0 :           return err;
    1905           0 :         }
    1906             :         /* Max msg_sz: 23 - 2 + 45 = 66 < 127 => we can use printf */
    1907           0 :         fd_log_collector_printf_dangerous_max_127( instr_ctx,
    1908           0 :           "Closed Uninitialized %s", FD_BASE58_ENC_32_ALLOCA( close_key ) );
    1909             : 
    1910             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1057-L1068 */
    1911           0 :       } else if( fd_bpf_upgradeable_loader_state_is_buffer( close_account_state ) ) {
    1912             : 
    1913             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1059 */
    1914           0 :         fd_borrowed_account_drop( &close_account );
    1915             : 
    1916           0 :         if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 3U ) ) ) {
    1917           0 :           return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
    1918           0 :         }
    1919             : 
    1920           0 :         fd_bpf_upgradeable_loader_state_buffer_t * state_buf = &close_account_state->inner.buffer;
    1921           0 :         err = common_close_account(
    1922           0 :             state_buf->has_authority_address ? &state_buf->authority_address : NULL,
    1923           0 :             instr_ctx,
    1924           0 :             close_account_state );
    1925           0 :         if( FD_UNLIKELY( err ) ) return err;
    1926             : 
    1927             :         /* Max msg_sz: 16 - 2 + 45 = 63 < 127 => we can use printf */
    1928           0 :         fd_log_collector_printf_dangerous_max_127( instr_ctx,
    1929           0 :           "Closed Buffer %s", FD_BASE58_ENC_32_ALLOCA( close_key ) );
    1930             : 
    1931             :       /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1069-L1129 */
    1932           0 :       } else if( fd_bpf_upgradeable_loader_state_is_program_data( close_account_state ) ) {
    1933           0 :         int err;
    1934           0 :         if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 4U ) ) ) {
    1935           0 :           return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
    1936           0 :         }
    1937             : 
    1938             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1074 */
    1939           0 :         fd_borrowed_account_drop( &close_account );
    1940             : 
    1941             :         /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1075-L1076 */
    1942           0 :         fd_guarded_borrowed_account_t program_account = {0};
    1943           0 :         FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK(instr_ctx, 3UL, &program_account );
    1944             : 
    1945           0 :         if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &program_account ) ) ) {
    1946           0 :           fd_log_collector_msg_literal( instr_ctx, "Program account is not writable" );
    1947           0 :           return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1948           0 :         }
    1949           0 :         if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &program_account ), program_id, sizeof(fd_pubkey_t) ) ) ) {
    1950           0 :           fd_log_collector_msg_literal( instr_ctx, "Program account not owned by loader" );
    1951           0 :           return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
    1952           0 :         }
    1953           0 :         fd_sol_sysvar_clock_t clock_;
    1954           0 :         fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( instr_ctx->sysvar_cache, &clock_ );
    1955           0 :         if( FD_UNLIKELY( !clock ) ) {
    1956           0 :           return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
    1957           0 :         }
    1958           0 :         if( FD_UNLIKELY( clock->slot==close_account_state->inner.program_data.slot ) ) {
    1959           0 :           fd_log_collector_msg_literal( instr_ctx,"Program was deployed in this block already" );
    1960           0 :           return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1961           0 :         }
    1962             : 
    1963           0 :         fd_bpf_upgradeable_loader_state_t * program_state = fd_bpf_loader_program_get_state( program_account.acct, spad, &err );
    1964           0 :         if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    1965           0 :           return err;
    1966           0 :         }
    1967           0 :         if( fd_bpf_upgradeable_loader_state_is_program( program_state ) ) {
    1968           0 :           if( FD_UNLIKELY( memcmp( &program_state->inner.program.programdata_address, close_key, sizeof(fd_pubkey_t) ) ) ) {
    1969           0 :             fd_log_collector_msg_literal( instr_ctx,"Program account does not match ProgramData account" );
    1970           0 :             return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1971           0 :           }
    1972             : 
    1973             :           /* https://github.com/anza-xyz/agave/blob/v2.1.4/programs/bpf_loader/src/lib.rs#L1105 */
    1974           0 :           fd_borrowed_account_drop( &program_account );
    1975             : 
    1976           0 :           fd_bpf_upgradeable_loader_state_program_data_t * pd = &close_account_state->inner.program_data;
    1977           0 :           err = common_close_account(
    1978           0 :               pd->has_upgrade_authority_address ? &pd->upgrade_authority_address : NULL,
    1979           0 :               instr_ctx,
    1980           0 :               close_account_state );
    1981           0 :           if( FD_UNLIKELY( err ) ) return err;
    1982             : 
    1983             :           /* The Agave client updates the account state upon closing an account
    1984             :              in their loaded program cache. Checking for a program can be
    1985             :              checked by checking to see if the programdata account's loader state
    1986             :              is unitialized. The firedancer implementation also removes closed
    1987             :              accounts from the loaded program cache at the end of a slot. Closed
    1988             :              accounts are not checked from the cache, instead the account state
    1989             :              is looked up. */
    1990             : 
    1991           0 :         } else {
    1992           0 :           fd_log_collector_msg_literal( instr_ctx, "Invalid program account" );
    1993           0 :           return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    1994           0 :         }
    1995             : 
    1996             :         /* Max msg_sz: 17 - 2 + 45 = 60 < 127 => we can use printf */
    1997           0 :         fd_log_collector_printf_dangerous_max_127( instr_ctx,
    1998           0 :           "Closed Program %s", FD_BASE58_ENC_32_ALLOCA( close_key ) );
    1999             : 
    2000             :         /* program account is dropped when it goes out of scope */
    2001           0 :       } else {
    2002           0 :         fd_log_collector_msg_literal( instr_ctx, "Account does not support closing" );
    2003           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    2004           0 :       }
    2005             : 
    2006             :       /* implicit drop of close account */
    2007           0 :       break;
    2008           0 :     }
    2009             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1158-L1170 */
    2010           0 :     case fd_bpf_upgradeable_loader_program_instruction_enum_extend_program: {
    2011           0 :       if( FD_UNLIKELY( FD_FEATURE_ACTIVE_BANK( instr_ctx->txn_ctx->bank, enable_extend_program_checked ) ) ) {
    2012           0 :         fd_log_collector_msg_literal( instr_ctx, "ExtendProgram was superseded by ExtendProgramChecked" );
    2013           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
    2014           0 :       }
    2015           0 :       err = common_extend_program( instr_ctx, instruction->inner.extend_program.additional_bytes, 0 );
    2016           0 :       if( FD_UNLIKELY( err ) ) {
    2017           0 :         return err;
    2018           0 :       }
    2019             : 
    2020           0 :       break;
    2021           0 :     }
    2022             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1171-L1179 */
    2023           0 :     case fd_bpf_upgradeable_loader_program_instruction_enum_extend_program_checked: {
    2024           0 :       if( FD_UNLIKELY( !FD_FEATURE_ACTIVE_BANK( instr_ctx->txn_ctx->bank, enable_extend_program_checked ) ) ) {
    2025           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
    2026           0 :       }
    2027           0 :       err = common_extend_program( instr_ctx, instruction->inner.extend_program_checked.additional_bytes, 1 );
    2028           0 :       if( FD_UNLIKELY( err ) ) {
    2029           0 :         return err;
    2030           0 :       }
    2031             : 
    2032           0 :       break;
    2033           0 :     }
    2034             :     /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1338-L1508 */
    2035           0 :     case fd_bpf_upgradeable_loader_program_instruction_enum_migrate: {
    2036             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1339-L1344 */
    2037           0 :       if( FD_UNLIKELY( !FD_FEATURE_ACTIVE_BANK( instr_ctx->txn_ctx->bank, enable_loader_v4 ) ) ) {
    2038           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
    2039           0 :       }
    2040             : 
    2041             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1346 */
    2042           0 :       if( FD_UNLIKELY( fd_exec_instr_ctx_check_num_insn_accounts( instr_ctx, 3U ) ) ) {
    2043           0 :         return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
    2044           0 :       }
    2045             : 
    2046             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1347-L1349 */
    2047           0 :       fd_pubkey_t const * programdata_address = NULL;
    2048           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 0UL, &programdata_address );
    2049           0 :       if( FD_UNLIKELY( err ) ) {
    2050           0 :         return err;
    2051           0 :       }
    2052             : 
    2053             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1350-L1352 */
    2054           0 :       fd_pubkey_t const * program_address = NULL;
    2055           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 1UL, &program_address );
    2056           0 :       if( FD_UNLIKELY( err ) ) {
    2057           0 :         return err;
    2058           0 :       }
    2059             : 
    2060             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1353-L1355 */
    2061           0 :       fd_pubkey_t const * provided_authority_address = NULL;
    2062           0 :       err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, 2UL, &provided_authority_address );
    2063           0 :       if( FD_UNLIKELY( err ) ) {
    2064           0 :         return err;
    2065           0 :       }
    2066             : 
    2067             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1356-L1359 */
    2068           0 :       fd_sol_sysvar_clock_t clock_;
    2069           0 :       fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock_read( instr_ctx->sysvar_cache, &clock_ );
    2070           0 :       if( FD_UNLIKELY( !clock ) ) {
    2071           0 :         return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
    2072           0 :       }
    2073           0 :       ulong clock_slot = clock->slot;
    2074             : 
    2075             :       /* Verify ProgramData account
    2076             :          https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1362-L1363 */
    2077           0 :       fd_guarded_borrowed_account_t programdata = {0};
    2078           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 0UL, &programdata );
    2079             : 
    2080             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1364-L1367 */
    2081           0 :       if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &programdata ) ) ) {
    2082           0 :         fd_log_collector_msg_literal( instr_ctx, "ProgramData account not writeable" );
    2083           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    2084           0 :       }
    2085             : 
    2086             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1368-L1387 */
    2087           0 :       ulong         program_len               = 0UL;
    2088           0 :       fd_pubkey_t * upgrade_authority_address = NULL;
    2089           0 :       fd_bpf_upgradeable_loader_state_t * programdata_state = fd_bpf_loader_program_get_state( programdata.acct, spad, &err );
    2090           0 :       if( FD_LIKELY( err==FD_BINCODE_SUCCESS && fd_bpf_upgradeable_loader_state_is_program_data( programdata_state ) ) ) {
    2091             : 
    2092             :         /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1374-L1377 */
    2093           0 :         if( FD_UNLIKELY( clock_slot==programdata_state->inner.program_data.slot ) ) {
    2094           0 :           fd_log_collector_msg_literal( instr_ctx, "Program was deployed in this block already" );
    2095           0 :           return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    2096           0 :         }
    2097             : 
    2098             :         /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1378-L1384 */
    2099           0 :         program_len = fd_ulong_sat_sub( fd_borrowed_account_get_data_len( &programdata ), PROGRAMDATA_METADATA_SIZE );
    2100           0 :         fd_bpf_upgradeable_loader_state_program_data_t * pd = &programdata_state->inner.program_data;
    2101           0 :         upgrade_authority_address = pd->has_upgrade_authority_address ? &programdata_state->inner.program_data.upgrade_authority_address : NULL;
    2102           0 :       }
    2103             : 
    2104             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1388 */
    2105           0 :       ulong programdata_funds = fd_borrowed_account_get_lamports( &programdata );
    2106             : 
    2107             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1389 */
    2108           0 :       fd_borrowed_account_drop( &programdata );
    2109             : 
    2110             :       /* Verify authority signature
    2111             :          https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1391-L1398 */
    2112           0 :       fd_pubkey_t const * authority_key_to_compare = upgrade_authority_address ? upgrade_authority_address : program_address;
    2113           0 :       if( FD_UNLIKELY( memcmp( fd_solana_migration_authority.key, provided_authority_address->key, sizeof(fd_pubkey_t) ) &&
    2114           0 :                        memcmp( authority_key_to_compare->key, provided_authority_address->key, sizeof(fd_pubkey_t) ) ) ) {
    2115           0 :         fd_log_collector_msg_literal( instr_ctx, "Incorrect migration authority provided" );
    2116           0 :         return FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY;
    2117           0 :       }
    2118             : 
    2119             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1399-L1402 */
    2120           0 :       if( FD_UNLIKELY( !instr_ctx->instr->accounts[ 2UL ].is_signer ) ) {
    2121           0 :         fd_log_collector_msg_literal( instr_ctx, "Migration authority did not sign" );
    2122           0 :         return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
    2123           0 :       }
    2124             : 
    2125             :       /* Verify Program account
    2126             :          https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1404-L1406 */
    2127           0 :       fd_guarded_borrowed_account_t program = {0};
    2128           0 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( instr_ctx, 1UL, &program );
    2129             : 
    2130             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1407-L1410 */
    2131           0 :       if( FD_UNLIKELY( !fd_borrowed_account_is_writable( &program ) ) ) {
    2132           0 :         fd_log_collector_msg_literal( instr_ctx, "Program account not writeable" );
    2133           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    2134           0 :       }
    2135             : 
    2136             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1411-L1414 */
    2137           0 :       if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &program ), program_id, sizeof(fd_pubkey_t) ) ) ) {
    2138           0 :         fd_log_collector_msg_literal( instr_ctx, "Program account not owned by loader" );
    2139           0 :         return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
    2140           0 :       }
    2141             : 
    2142             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1415-L1426 */
    2143           0 :       fd_bpf_upgradeable_loader_state_t * program_state = fd_bpf_loader_program_get_state( program.acct, spad, &err );
    2144           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    2145           0 :         return err;
    2146           0 :       }
    2147             : 
    2148           0 :       if( FD_LIKELY( fd_bpf_upgradeable_loader_state_is_program( program_state ) ) ) {
    2149           0 :         if( FD_UNLIKELY( memcmp( programdata_address->key, program_state->inner.program.programdata_address.key, sizeof(fd_pubkey_t) ) ) ) {
    2150           0 :           fd_log_collector_msg_literal( instr_ctx, "Program and ProgramData account mismatch" );
    2151           0 :           return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
    2152           0 :         }
    2153           0 :       } else {
    2154           0 :         fd_log_collector_msg_literal( instr_ctx, "Invalid Program account" );
    2155           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2156           0 :       }
    2157             : 
    2158             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1427 */
    2159           0 :       err = fd_borrowed_account_set_data_from_slice( &program, NULL, 0UL );
    2160           0 :       if( FD_UNLIKELY( err ) ) {
    2161           0 :         return err;
    2162           0 :       }
    2163             : 
    2164             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1428 */
    2165           0 :       err = fd_borrowed_account_checked_add_lamports( &program , programdata_funds );
    2166           0 :       if( FD_UNLIKELY( err ) ) {
    2167           0 :         return err;
    2168           0 :       }
    2169             : 
    2170             :       /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/lib.rs#L1268 */
    2171           0 :       err = fd_borrowed_account_set_owner( &program, &fd_solana_bpf_loader_v4_program_id );
    2172           0 :       if( FD_UNLIKELY( err ) ) {
    2173           0 :         return err;
    2174           0 :       }
    2175             : 
    2176             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1434 */
    2177           0 :       fd_borrowed_account_drop( &program );
    2178             : 
    2179             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1436-L1437 */
    2180           0 :       err = fd_exec_instr_ctx_try_borrow_instr_account( instr_ctx , 0U, &programdata );
    2181           0 :       if( FD_UNLIKELY( err ) ) {
    2182           0 :         return err;
    2183           0 :       }
    2184             : 
    2185             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1438 */
    2186           0 :       err = fd_borrowed_account_set_lamports( &programdata, 0UL );
    2187           0 :       if( FD_UNLIKELY( err ) ) {
    2188           0 :         return err;
    2189           0 :       }
    2190             : 
    2191             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1439 */
    2192           0 :       fd_borrowed_account_drop( &programdata );
    2193             : 
    2194           0 :       FD_SPAD_FRAME_BEGIN( instr_ctx->txn_ctx->spad ) {
    2195           0 :         uchar                              instr_data[FD_TXN_MTU];
    2196           0 :         fd_loader_v4_program_instruction_t instr      = {0};
    2197           0 :         fd_bincode_encode_ctx_t            encode_ctx = {0};
    2198           0 :         fd_vm_rust_account_meta_t *        acct_metas = fd_spad_alloc( instr_ctx->txn_ctx->spad,
    2199           0 :                                                                        FD_VM_RUST_ACCOUNT_META_ALIGN,
    2200           0 :                                                                        3UL * sizeof(fd_vm_rust_account_meta_t) );
    2201             : 
    2202             :         /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1441-L1484 */
    2203           0 :         if( FD_LIKELY( program_len>0UL ) ) {
    2204             : 
    2205             :           /* Set program length
    2206             :              https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1442-L1451 */
    2207           0 :           fd_native_cpi_create_account_meta( program_address,            0, 1, &acct_metas[0] );
    2208           0 :           fd_native_cpi_create_account_meta( provided_authority_address, 1, 0, &acct_metas[1] );
    2209           0 :           fd_native_cpi_create_account_meta( program_address,            0, 1, &acct_metas[2] );
    2210             : 
    2211           0 :           instr = (fd_loader_v4_program_instruction_t) {
    2212           0 :             .discriminant = fd_loader_v4_program_instruction_enum_set_program_length,
    2213           0 :             .inner = {
    2214           0 :               .set_program_length = {
    2215           0 :                 .new_size = (uint)program_len
    2216           0 :               }
    2217           0 :             }
    2218           0 :           };
    2219             : 
    2220           0 :           encode_ctx = (fd_bincode_encode_ctx_t) {
    2221           0 :             .data    = instr_data,
    2222           0 :             .dataend = instr_data + FD_TXN_MTU
    2223           0 :           };
    2224             : 
    2225             :           // This should never fail.
    2226           0 :           err = fd_loader_v4_program_instruction_encode( &instr, &encode_ctx );
    2227           0 :           if( FD_UNLIKELY( err ) ) {
    2228           0 :             return FD_EXECUTOR_INSTR_ERR_FATAL;
    2229           0 :           }
    2230             : 
    2231           0 :           err = fd_native_cpi_native_invoke( instr_ctx,
    2232           0 :                                              &fd_solana_bpf_loader_v4_program_id,
    2233           0 :                                              instr_data,
    2234           0 :                                              FD_TXN_MTU,
    2235           0 :                                              acct_metas,
    2236           0 :                                              3UL,
    2237           0 :                                              NULL,
    2238           0 :                                              0UL );
    2239           0 :           if( FD_UNLIKELY( err ) ) {
    2240           0 :             return err;
    2241           0 :           }
    2242             : 
    2243             :           /* Copy
    2244             :              https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1453-L1464 */
    2245           0 :           fd_native_cpi_create_account_meta( program_address,            0, 1, &acct_metas[0] );
    2246           0 :           fd_native_cpi_create_account_meta( provided_authority_address, 1, 0, &acct_metas[1] );
    2247           0 :           fd_native_cpi_create_account_meta( programdata_address,        0, 0, &acct_metas[2] );
    2248             : 
    2249           0 :           instr = (fd_loader_v4_program_instruction_t) {
    2250           0 :             .discriminant = fd_loader_v4_program_instruction_enum_copy,
    2251           0 :             .inner = {
    2252           0 :               .copy = {
    2253           0 :                 .destination_offset = 0U,
    2254           0 :                 .source_offset      = 0U,
    2255           0 :                 .length             = (uint)program_len
    2256           0 :               }
    2257           0 :             }
    2258           0 :           };
    2259             : 
    2260           0 :           encode_ctx = (fd_bincode_encode_ctx_t) {
    2261           0 :             .data    = instr_data,
    2262           0 :             .dataend = instr_data + FD_TXN_MTU
    2263           0 :           };
    2264             : 
    2265             :           // This should never fail.
    2266           0 :           err = fd_loader_v4_program_instruction_encode( &instr, &encode_ctx );
    2267           0 :           if( FD_UNLIKELY( err ) ) {
    2268           0 :             return FD_EXECUTOR_INSTR_ERR_FATAL;
    2269           0 :           }
    2270             : 
    2271           0 :           err = fd_native_cpi_native_invoke( instr_ctx,
    2272           0 :                                              &fd_solana_bpf_loader_v4_program_id,
    2273           0 :                                              instr_data,
    2274           0 :                                              FD_TXN_MTU,
    2275           0 :                                              acct_metas,
    2276           0 :                                              3UL,
    2277           0 :                                              NULL,
    2278           0 :                                              0UL );
    2279           0 :           if( FD_UNLIKELY( err ) ) {
    2280           0 :             return err;
    2281           0 :           }
    2282             : 
    2283             :           /* Deploy
    2284             :              https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1466-L1473 */
    2285           0 :           fd_native_cpi_create_account_meta( program_address,            0, 1, &acct_metas[0] );
    2286           0 :           fd_native_cpi_create_account_meta( provided_authority_address, 1, 0, &acct_metas[1] );
    2287             : 
    2288           0 :           instr = (fd_loader_v4_program_instruction_t) {
    2289           0 :             .discriminant = fd_loader_v4_program_instruction_enum_deploy,
    2290           0 :           };
    2291             : 
    2292           0 :           encode_ctx = (fd_bincode_encode_ctx_t) {
    2293           0 :             .data    = instr_data,
    2294           0 :             .dataend = instr_data + FD_TXN_MTU
    2295           0 :           };
    2296             : 
    2297             :           // This should never fail.
    2298           0 :           err = fd_loader_v4_program_instruction_encode( &instr, &encode_ctx );
    2299           0 :           if( FD_UNLIKELY( err ) ) {
    2300           0 :             return FD_EXECUTOR_INSTR_ERR_FATAL;
    2301           0 :           }
    2302             : 
    2303           0 :           err = fd_native_cpi_native_invoke( instr_ctx,
    2304           0 :                                             &fd_solana_bpf_loader_v4_program_id,
    2305           0 :                                             instr_data,
    2306           0 :                                             FD_TXN_MTU,
    2307           0 :                                             acct_metas,
    2308           0 :                                             2UL,
    2309           0 :                                             NULL,
    2310           0 :                                             0UL );
    2311           0 :           if( FD_UNLIKELY( err ) ) {
    2312           0 :             return err;
    2313           0 :           }
    2314             : 
    2315             :           /* Finalize (if no upgrade authority address provided)
    2316             :              https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1475-L1484 */
    2317           0 :           if( upgrade_authority_address==NULL ) {
    2318           0 :             fd_native_cpi_create_account_meta( program_address,            0, 1, &acct_metas[0] );
    2319           0 :             fd_native_cpi_create_account_meta( provided_authority_address, 1, 0, &acct_metas[1] );
    2320           0 :             fd_native_cpi_create_account_meta( program_address,            0, 0, &acct_metas[2] );
    2321             : 
    2322           0 :             instr = (fd_loader_v4_program_instruction_t) {
    2323           0 :               .discriminant = fd_loader_v4_program_instruction_enum_finalize,
    2324           0 :             };
    2325             : 
    2326           0 :             encode_ctx = (fd_bincode_encode_ctx_t) {
    2327           0 :               .data    = instr_data,
    2328           0 :               .dataend = instr_data + FD_TXN_MTU
    2329           0 :             };
    2330             : 
    2331             :             // This should never fail.
    2332           0 :             err = fd_loader_v4_program_instruction_encode( &instr, &encode_ctx );
    2333           0 :             if( FD_UNLIKELY( err ) ) {
    2334           0 :               return FD_EXECUTOR_INSTR_ERR_FATAL;
    2335           0 :             }
    2336             : 
    2337           0 :             err = fd_native_cpi_native_invoke( instr_ctx,
    2338           0 :                                               &fd_solana_bpf_loader_v4_program_id,
    2339           0 :                                               instr_data,
    2340           0 :                                               FD_TXN_MTU,
    2341           0 :                                               acct_metas,
    2342           0 :                                               3UL,
    2343           0 :                                               NULL,
    2344           0 :                                               0UL );
    2345           0 :             if( FD_UNLIKELY( err ) ) {
    2346           0 :               return err;
    2347           0 :             }
    2348           0 :           } else if( !memcmp( fd_solana_migration_authority.key, provided_authority_address->key, sizeof(fd_pubkey_t) ) ) {
    2349             : 
    2350             :             /* Transfer authority
    2351             :                https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1486-L1494 */
    2352           0 :             fd_native_cpi_create_account_meta( program_address,            0, 1, &acct_metas[0] );
    2353           0 :             fd_native_cpi_create_account_meta( provided_authority_address, 1, 0, &acct_metas[1] );
    2354           0 :             fd_native_cpi_create_account_meta( upgrade_authority_address,  1, 0, &acct_metas[2] );
    2355             : 
    2356           0 :             instr = (fd_loader_v4_program_instruction_t) {
    2357           0 :               .discriminant = fd_loader_v4_program_instruction_enum_transfer_authority,
    2358           0 :             };
    2359             : 
    2360           0 :             encode_ctx = (fd_bincode_encode_ctx_t) {
    2361           0 :               .data    = instr_data,
    2362           0 :               .dataend = instr_data + FD_TXN_MTU
    2363           0 :             };
    2364             : 
    2365             :             // This should never fail.
    2366           0 :             err = fd_loader_v4_program_instruction_encode( &instr, &encode_ctx );
    2367           0 :             if( FD_UNLIKELY( err ) ) {
    2368           0 :               return FD_EXECUTOR_INSTR_ERR_FATAL;
    2369           0 :             }
    2370             : 
    2371           0 :             err = fd_native_cpi_native_invoke( instr_ctx,
    2372           0 :                                               &fd_solana_bpf_loader_v4_program_id,
    2373           0 :                                               instr_data,
    2374           0 :                                               FD_TXN_MTU,
    2375           0 :                                               acct_metas,
    2376           0 :                                               3UL,
    2377           0 :                                               NULL,
    2378           0 :                                               0UL );
    2379           0 :             if( FD_UNLIKELY( err ) ) {
    2380           0 :               return err;
    2381           0 :             }
    2382           0 :           }
    2383           0 :         }
    2384           0 :       } FD_SPAD_FRAME_END;
    2385             : 
    2386             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1500-L1501 */
    2387           0 :       err = fd_exec_instr_ctx_try_borrow_instr_account( instr_ctx , 0U, &programdata );
    2388           0 :       if( FD_UNLIKELY( err ) ) {
    2389           0 :         return err;
    2390           0 :       }
    2391             : 
    2392             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1502 */
    2393           0 :       err = fd_borrowed_account_set_data_from_slice( &programdata, NULL, 0UL );
    2394           0 :       if( FD_UNLIKELY( err ) ) {
    2395           0 :         return err;
    2396           0 :       }
    2397             : 
    2398             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1504 */
    2399           0 :       fd_borrowed_account_drop( &programdata );
    2400             : 
    2401             :       /* https://github.com/anza-xyz/agave/blob/v2.2.6/programs/bpf_loader/src/lib.rs#L1506 */
    2402           0 :       fd_log_collector_printf_dangerous_max_127( instr_ctx, "Migrated program %s", FD_BASE58_ENC_32_ALLOCA( program_address ) );
    2403             : 
    2404           0 :       break;
    2405           0 :     }
    2406           0 :     default: {
    2407           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2408           0 :     }
    2409           0 :   }
    2410           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
    2411           0 : }
    2412             : 
    2413             : /* process_instruction_inner() */
    2414             : /* https://github.com/anza-xyz/agave/blob/77daab497df191ef485a7ad36ed291c1874596e5/programs/bpf_loader/src/lib.rs#L394-L564 */
    2415             : int
    2416           0 : fd_bpf_loader_program_execute( fd_exec_instr_ctx_t * ctx ) {
    2417           0 :   FD_SPAD_FRAME_BEGIN( ctx->txn_ctx->spad ) {
    2418             :     /* https://github.com/anza-xyz/agave/blob/77daab497df191ef485a7ad36ed291c1874596e5/programs/bpf_loader/src/lib.rs#L491-L529 */
    2419             : 
    2420             :     /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L403-L404 */
    2421           0 :     fd_guarded_borrowed_account_t program_account = {0};
    2422           0 :     int err = fd_exec_instr_ctx_try_borrow_last_program_account( ctx, &program_account );
    2423           0 :     if( FD_UNLIKELY( err ) ) {
    2424           0 :       return err;
    2425           0 :     }
    2426             : 
    2427             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/programs/bpf_loader/src/lib.rs#L409 */
    2428           0 :     fd_pubkey_t const * program_id = NULL;
    2429           0 :     err = fd_exec_instr_ctx_get_last_program_key( ctx, &program_id );
    2430           0 :     if( FD_UNLIKELY( err ) ) {
    2431           0 :       return err;
    2432           0 :     }
    2433             : 
    2434             :     /* Program management instruction */
    2435           0 :     if( FD_UNLIKELY( !memcmp( &fd_solana_native_loader_id, fd_borrowed_account_get_owner( &program_account ), sizeof(fd_pubkey_t) ) ) ) {
    2436             :       /* https://github.com/anza-xyz/agave/blob/v2.2.3/programs/bpf_loader/src/lib.rs#L416 */
    2437           0 :       fd_borrowed_account_drop( &program_account );
    2438             : 
    2439           0 :       if( FD_UNLIKELY( !memcmp( &fd_solana_bpf_loader_upgradeable_program_id, program_id, sizeof(fd_pubkey_t) ) ) ) {
    2440           0 :         FD_EXEC_CU_UPDATE( ctx, UPGRADEABLE_LOADER_COMPUTE_UNITS );
    2441           0 :         return process_loader_upgradeable_instruction( ctx );
    2442           0 :       } else if( FD_UNLIKELY( !memcmp( &fd_solana_bpf_loader_program_id, program_id, sizeof(fd_pubkey_t) ) ) ) {
    2443           0 :         FD_EXEC_CU_UPDATE( ctx, DEFAULT_LOADER_COMPUTE_UNITS );
    2444           0 :         fd_log_collector_msg_literal( ctx, "BPF loader management instructions are no longer supported" );
    2445           0 :         return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2446           0 :       } else if( FD_UNLIKELY( !memcmp( &fd_solana_bpf_loader_deprecated_program_id, program_id, sizeof(fd_pubkey_t) ) ) ) {
    2447           0 :         FD_EXEC_CU_UPDATE( ctx, DEPRECATED_LOADER_COMPUTE_UNITS );
    2448           0 :         fd_log_collector_msg_literal( ctx, "Deprecated loader is no longer supported" );
    2449           0 :         return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2450           0 :       } else {
    2451           0 :         fd_log_collector_msg_literal( ctx, "Invalid BPF loader id" );
    2452             :         /* https://github.com/anza-xyz/agave/blob/89872fdb074e6658646b2b57a299984f0059cc84/programs/bpf_loader/src/lib.rs#L429-L436 */
    2453           0 :         if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
    2454           0 :           return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2455           0 :         }
    2456           0 :         return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
    2457           0 :       }
    2458           0 :     }
    2459             : 
    2460             :     /* https://github.com/anza-xyz/agave/blob/89872fdb074e6658646b2b57a299984f0059cc84/programs/bpf_loader/src/lib.rs#L445-L452 */
    2461             :     /* Program invocation. Any invalid programs will be caught here or at the program load. */
    2462           0 :     if( FD_UNLIKELY( !FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) &&
    2463           0 :                      !fd_borrowed_account_is_executable( &program_account ) ) ) {
    2464           0 :       fd_log_collector_msg_literal( ctx, "Program is not executable" );
    2465           0 :       return FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID;
    2466           0 :     }
    2467             : 
    2468             :     /* https://github.com/anza-xyz/agave/blob/77daab497df191ef485a7ad36ed291c1874596e5/programs/bpf_loader/src/lib.rs#L551-L563 */
    2469             :     /* The Agave client stores a loaded program type state in its implementation
    2470             :       of the loaded program cache. It checks to see if an account is able to be
    2471             :       executed. It is possible for a program to be in the DelayVisibility state or
    2472             :       Closed state but it won't be reflected in the Firedancer cache. Program
    2473             :       accounts that are in this state should exit with an invalid account data
    2474             :       error. For programs that are recently deployed or upgraded, they should not
    2475             :       be allowed to be executed for the remainder of the slot. For closed
    2476             :       accounts, they're uninitialized and shouldn't be executed as well.
    2477             : 
    2478             :       For the former case the slot that the
    2479             :       program was last updated in is in the program data account.
    2480             :       This means that if the slot in the program data account is greater than or
    2481             :       equal to the current execution slot, then the account is in a
    2482             :       'LoadedProgramType::DelayVisiblity' state.
    2483             : 
    2484             :       The latter case as described above is a tombstone account which is in a Closed
    2485             :       state. This occurs when a program data account is closed. However, our cache
    2486             :       does not track this. Instead, this can be checked for by seeing if the program
    2487             :       account's respective program data account is uninitialized. This should only
    2488             :       happen when the account is closed.
    2489             : 
    2490             :       Every error that comes out of this block is mapped to an InvalidAccountData instruction error in Agave. */
    2491             : 
    2492           0 :     fd_account_meta_t const * metadata = fd_borrowed_account_get_acc_meta( &program_account );
    2493           0 :     uchar is_deprecated = !memcmp( metadata->owner, &fd_solana_bpf_loader_deprecated_program_id, sizeof(fd_pubkey_t) );
    2494             : 
    2495           0 :     if( !memcmp( metadata->owner, &fd_solana_bpf_loader_upgradeable_program_id, sizeof(fd_pubkey_t) ) ) {
    2496           0 :       fd_bpf_upgradeable_loader_state_t * program_account_state = fd_bpf_loader_program_get_state( program_account.acct, ctx->txn_ctx->spad, &err );
    2497           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    2498           0 :         fd_log_collector_msg_literal( ctx, "Program is not deployed" );
    2499           0 :         if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
    2500           0 :           return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2501           0 :         }
    2502           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2503           0 :       }
    2504             : 
    2505             :       /* https://github.com/anza-xyz/agave/blob/v2.0.9/svm/src/program_loader.rs#L96-L98
    2506             :          Program account and program data account discriminants get checked when loading in program accounts
    2507             :          into the program cache. If the discriminants are incorrect, the program is marked as closed. */
    2508           0 :       if( FD_UNLIKELY( !fd_bpf_upgradeable_loader_state_is_program( program_account_state ) ) ) {
    2509             :         /* https://github.com/anza-xyz/agave/tree/v3.0.5/programs/bpf_loader/src/lib.rs#L424-L433
    2510             :            Agave's program cache will add any non-migrating built-ins as built-in
    2511             :            accounts, even though they might be owned by the BPF loader. In these
    2512             :            cases, Agave does not log this message. Meanwhile, non-migrating
    2513             :            built-in programs do not use the BPF loader, by definition. */
    2514           0 :         if( !fd_is_non_migrating_builtin_program( program_id ) ) {
    2515           0 :           fd_log_collector_msg_literal( ctx, "Program is not deployed" );
    2516           0 :         }
    2517           0 :         if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
    2518           0 :           return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2519           0 :         }
    2520           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2521           0 :       }
    2522             : 
    2523           0 :       fd_txn_account_t * program_data_account = NULL;
    2524           0 :       fd_pubkey_t *      programdata_pubkey   = (fd_pubkey_t *)&program_account_state->inner.program.programdata_address;
    2525           0 :       err = fd_exec_txn_ctx_get_executable_account( ctx->txn_ctx,
    2526           0 :                                                     programdata_pubkey,
    2527           0 :                                                     &program_data_account,
    2528           0 :                                                     fd_txn_account_check_exists );
    2529           0 :       if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS ) ) {
    2530           0 :         fd_log_collector_msg_literal( ctx, "Program is not deployed" );
    2531           0 :         if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
    2532           0 :           return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2533           0 :         }
    2534           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2535           0 :       }
    2536             : 
    2537           0 :       if( FD_UNLIKELY( fd_txn_account_get_data_len( program_data_account )<PROGRAMDATA_METADATA_SIZE ) ) {
    2538           0 :         fd_log_collector_msg_literal( ctx, "Program is not deployed" );
    2539           0 :         if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
    2540           0 :           return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2541           0 :         }
    2542           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2543           0 :       }
    2544             : 
    2545           0 :       fd_bpf_upgradeable_loader_state_t * program_data_account_state = fd_bpf_loader_program_get_state( program_data_account,
    2546           0 :                                                                                                         ctx->txn_ctx->spad,
    2547           0 :                                                                                                         &err );
    2548           0 :       if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
    2549           0 :         fd_log_collector_msg_literal( ctx, "Program is not deployed" );
    2550           0 :         if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
    2551           0 :           return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2552           0 :         }
    2553           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2554           0 :       }
    2555             : 
    2556             :       /* https://github.com/anza-xyz/agave/blob/v2.0.9/svm/src/program_loader.rs#L100-L104
    2557             :          Same as above comment. Program data discriminant must be set correctly. */
    2558           0 :       if( FD_UNLIKELY( !fd_bpf_upgradeable_loader_state_is_program_data( program_data_account_state ) ) ) {
    2559             :         /* The account is closed. */
    2560           0 :         fd_log_collector_msg_literal( ctx, "Program is not deployed" );
    2561           0 :         if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
    2562           0 :           return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2563           0 :         }
    2564           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2565           0 :       }
    2566             : 
    2567           0 :       ulong program_data_slot = program_data_account_state->inner.program_data.slot;
    2568           0 :       if( FD_UNLIKELY( program_data_slot>=ctx->txn_ctx->slot ) ) {
    2569             :         /* The account was likely just deployed or upgraded. Corresponds to
    2570             :           'LoadedProgramType::DelayVisibility' */
    2571           0 :         fd_log_collector_msg_literal( ctx, "Program is not deployed" );
    2572           0 :         if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
    2573           0 :           return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2574           0 :         }
    2575           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2576           0 :       }
    2577           0 :     }
    2578             : 
    2579           0 :     fd_prog_load_env_t load_env[1]; fd_prog_load_env_from_bank( load_env, ctx->txn_ctx->bank );
    2580           0 :     fd_progcache_rec_t const * cache_entry =
    2581           0 :         fd_progcache_pull( ctx->txn_ctx->progcache,
    2582           0 :                            ctx->txn_ctx->funk,
    2583           0 :                            ctx->txn_ctx->xid,
    2584           0 :                            program_id,
    2585           0 :                            load_env );
    2586           0 :     if( FD_UNLIKELY( !cache_entry ) ) {
    2587           0 :       fd_log_collector_msg_literal( ctx, "Program is not cached" );
    2588             : 
    2589             :       /* https://github.com/anza-xyz/agave/blob/89872fdb074e6658646b2b57a299984f0059cc84/programs/bpf_loader/src/lib.rs#L460-L467 */
    2590           0 :       if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
    2591           0 :         return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2592           0 :       }
    2593           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2594           0 :     }
    2595             : 
    2596             :     /* The program may be in the cache but could have failed verification in the current epoch. */
    2597           0 :     if( FD_UNLIKELY( cache_entry->executable==0 ) ) {
    2598           0 :       fd_log_collector_msg_literal( ctx, "Program is not deployed" );
    2599           0 :       if( FD_FEATURE_ACTIVE_BANK( ctx->txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
    2600           0 :         return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    2601           0 :       }
    2602           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
    2603           0 :     }
    2604             : 
    2605             :     /* https://github.com/anza-xyz/agave/blob/v2.1.14/programs/bpf_loader/src/lib.rs#L446 */
    2606           0 :     fd_borrowed_account_drop( &program_account );
    2607             : 
    2608           0 :     return fd_bpf_execute( ctx, cache_entry, is_deprecated );
    2609           0 :   } FD_SPAD_FRAME_END;
    2610           0 : }
    2611             : 
    2612             : 
    2613             : /* Public APIs */
    2614             : 
    2615             : int
    2616             : fd_directly_invoke_loader_v3_deploy( fd_bank_t *               bank,
    2617             :                                      void *                    accdb_shfunk,
    2618             :                                      fd_funk_txn_xid_t const * xid,
    2619             :                                      fd_pubkey_t const *       program_key,
    2620             :                                      uchar const *             elf,
    2621           0 :                                      ulong                     elf_sz ) {
    2622             :   /* FIXME: Breaking this until exec spad is replaced. */
    2623           0 :   FD_LOG_ERR(( "fd_directly_invoke_loader_v3_deploy is not implemented" ));
    2624           0 :   return 0;
    2625             : 
    2626             :   /* Set up a dummy instr and txn context */
    2627           0 :   fd_exec_txn_ctx_t * txn_ctx = fd_exec_txn_ctx_join( fd_exec_txn_ctx_new( NULL ), NULL, NULL );
    2628             : 
    2629           0 :   fd_exec_txn_ctx_setup( bank,
    2630           0 :                          accdb_shfunk,
    2631           0 :                          NULL,
    2632           0 :                          xid,
    2633           0 :                          NULL,
    2634           0 :                          txn_ctx,
    2635           0 :                          NULL,
    2636           0 :                          NULL,
    2637           0 :                          0UL );
    2638             : 
    2639           0 :   fd_exec_txn_ctx_setup_basic( txn_ctx );
    2640           0 :   txn_ctx->instr_stack_sz = 1;
    2641           0 :   fd_exec_instr_ctx_t * instr_ctx = &txn_ctx->instr_stack[0];
    2642           0 :   *instr_ctx = (fd_exec_instr_ctx_t) {
    2643           0 :     .instr     = NULL,
    2644           0 :     .txn_ctx   = txn_ctx,
    2645           0 :   };
    2646             : 
    2647             :   /* Important note: this function is called at the epoch boundary and
    2648             :      does not do anything with the `programs_to_reverify` field in the
    2649             :      transaction context. This is fine though because when this function
    2650             :      is called, the program will not exist in the cache yet (because it
    2651             :      does not exist on-chain as a BPF program yet). There is no queueing
    2652             :      needed because the next time the program is invoked, the program
    2653             :      cache updating logic will see that the cache entry is missing and
    2654             :      will insert it then. */
    2655             :   return fd_deploy_program( instr_ctx, program_key, elf, elf_sz, NULL );
    2656           0 : }
       |