LCOV - code coverage report
Current view: top level - flamenco/runtime/program - fd_bpf_loader_program.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 1771 0.0 %
Date: 2025-12-04 04:56:06 Functions: 0 12 0.0 %

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

Generated by: LCOV version 1.14