LCOV - code coverage report
Current view: top level - flamenco/runtime/program - fd_builtin_programs.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 29 82 35.4 %
Date: 2026-03-19 18:19:27 Functions: 6 9 66.7 %

          Line data    Source code
       1             : #include "fd_builtin_programs.h"
       2             : #include "fd_precompiles.h"
       3             : #include "../fd_system_ids.h"
       4             : #include "../fd_system_ids_pp.h"
       5             : #include "../../accdb/fd_accdb_sync.h"
       6             : 
       7             : #define BUILTIN_PROGRAM(program_id, name, feature_offset, migration_config) \
       8             :     {                                                                       \
       9             :         program_id,                                                         \
      10             :         name,                                                               \
      11             :         feature_offset,                                                     \
      12             :         migration_config                                                    \
      13             :     }
      14             : 
      15             : #define STATELESS_BUILTIN(program_id, migration_config) \
      16             :     {                                                   \
      17             :         program_id,                                     \
      18             :         migration_config                                \
      19             :     }
      20             : 
      21             : #define CORE_BPF_MIGRATION_CONFIG(source_buffer_address, upgrade_authority_address, enable_feature_offset, builtin_program_id) \
      22             :     {                                                                                                                          \
      23             :         source_buffer_address,                                                                                                 \
      24             :         upgrade_authority_address,                                                                                             \
      25             :         enable_feature_offset,                                                                                                 \
      26             :         builtin_program_id                                                                                                     \
      27             :     }
      28             : 
      29             : /* Core BPF migration configs */
      30             : static const fd_core_bpf_migration_config_t STATELESS_TO_CORE_BPF_FEATURE_GATE_PROGRAM_CONFIG = {
      31             :     &fd_solana_feature_program_buffer_address,
      32             :     NULL,
      33             :     offsetof(fd_features_t, migrate_feature_gate_program_to_core_bpf),
      34             :     FD_CORE_BPF_MIGRATION_TARGET_STATELESS,
      35             :     &fd_solana_feature_program_id,
      36             :     NULL
      37             : };
      38             : static const fd_core_bpf_migration_config_t * const MIGRATE_STATELESS_TO_CORE_BPF_FEATURE_GATE_PROGRAM_CONFIG = &STATELESS_TO_CORE_BPF_FEATURE_GATE_PROGRAM_CONFIG;
      39             : 
      40             : /* FIXME: update to correct hash when slashing program is finalized */
      41             : /* 9260b9ac8dfa1a6ed1022380a713bec7b75979ae136e91f9a86795b51c6c489f */
      42             : #define SLASHING_PROG_HASH_SIMD_204 0x92U,0x60U,0xb9U,0xacU,0x8dU,0xfaU,0x1aU,0x6eU,0xd1U,0x02U,0x23U,0x80U,0xa7U,0x13U,0xbeU,0xc7U, \
      43             :                                     0xb7U,0x59U,0x79U,0xaeU,0x13U,0x6eU,0x91U,0xf9U,0xa8U,0x67U,0x95U,0xb5U,0x1cU,0x6cU,0x48U,0x9fU
      44             : const fd_hash_t fd_solana_slashing_program_verified_build_hash_simd_204 = { .uc = { SLASHING_PROG_HASH_SIMD_204 } };
      45             : static const fd_core_bpf_migration_config_t STATELESS_TO_CORE_BPF_SLASHING_PROGRAM_CONFIG = {
      46             :     &fd_solana_slashing_program_buffer_address,
      47             :     NULL,
      48             :     offsetof(fd_features_t, enshrine_slashing_program),
      49             :     FD_CORE_BPF_MIGRATION_TARGET_STATELESS,
      50             :     &fd_solana_slashing_program_id,
      51             :     &fd_solana_slashing_program_verified_build_hash_simd_204
      52             : };
      53             : static const fd_core_bpf_migration_config_t * const MIGRATE_STATELESS_TO_CORE_BPF_SLASHING_PROGRAM_CONFIG = &STATELESS_TO_CORE_BPF_SLASHING_PROGRAM_CONFIG;
      54             : 
      55             : #define SYSTEM_PROGRAM_BUILTIN            BUILTIN_PROGRAM(&fd_solana_system_program_id,                 "system_program",                        NO_ENABLE_FEATURE_ID,                                      NULL)
      56             : #define VOTE_PROGRAM_BUILTIN              BUILTIN_PROGRAM(&fd_solana_vote_program_id,                   "vote_program",                          NO_ENABLE_FEATURE_ID,                                      NULL)
      57             : #define LOADER_V4_BUILTIN                 BUILTIN_PROGRAM(&fd_solana_bpf_loader_v4_program_id,          "loader_v4",                             offsetof(fd_features_t, enable_loader_v4),                 NULL)
      58             : #define BPF_LOADER_DEPRECATED_BUILTIN     BUILTIN_PROGRAM(&fd_solana_bpf_loader_deprecated_program_id,  "solana_bpf_loader_deprecated_program",  NO_ENABLE_FEATURE_ID,                                      NULL)
      59             : #define BPF_LOADER_BUILTIN                BUILTIN_PROGRAM(&fd_solana_bpf_loader_program_id,             "solana_bpf_loader_program",             NO_ENABLE_FEATURE_ID,                                      NULL)
      60             : #define BPF_LOADER_UPGRADEABLE_BUILTIN    BUILTIN_PROGRAM(&fd_solana_bpf_loader_upgradeable_program_id, "solana_bpf_loader_upgradeable_program", NO_ENABLE_FEATURE_ID,                                      NULL)
      61             : #define COMPUTE_BUDGET_PROGRAM_BUILTIN    BUILTIN_PROGRAM(&fd_solana_compute_budget_program_id,         "compute_budget_program",                NO_ENABLE_FEATURE_ID,                                      NULL)
      62             : #define ZK_TOKEN_PROOF_PROGRAM_BUILTIN    BUILTIN_PROGRAM(&fd_solana_zk_token_proof_program_id,         "zk_token_proof_program",                offsetof(fd_features_t, zk_token_sdk_enabled),             NULL)
      63             : #define ZK_ELGAMAL_PROOF_PROGRAM_BUILTIN  BUILTIN_PROGRAM(&fd_solana_zk_elgamal_proof_program_id,       "zk_elgamal_proof_program",              offsetof(fd_features_t, zk_elgamal_proof_program_enabled), NULL)
      64             : 
      65             : #define FEATURE_PROGRAM_BUILTIN           STATELESS_BUILTIN(&fd_solana_feature_program_id,  MIGRATE_STATELESS_TO_CORE_BPF_FEATURE_GATE_PROGRAM_CONFIG)
      66             : #define SLASHING_PROGRAM_BUILTIN          STATELESS_BUILTIN(&fd_solana_slashing_program_id, MIGRATE_STATELESS_TO_CORE_BPF_SLASHING_PROGRAM_CONFIG)
      67             : 
      68             : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank/builtins/mod.rs#L133-L143 */
      69             : static const fd_stateless_builtin_program_t stateless_programs_builtins[] = {
      70             :     FEATURE_PROGRAM_BUILTIN,
      71             :     SLASHING_PROGRAM_BUILTIN
      72             : };
      73           6 : #define STATELESS_BUILTINS_COUNT (sizeof(stateless_programs_builtins) / sizeof(fd_stateless_builtin_program_t))
      74             : 
      75             : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank/builtins/mod.rs#L34-L131 */
      76             : static fd_builtin_program_t const builtin_programs[] = {
      77             :     SYSTEM_PROGRAM_BUILTIN,
      78             :     VOTE_PROGRAM_BUILTIN,
      79             :     LOADER_V4_BUILTIN,
      80             :     BPF_LOADER_DEPRECATED_BUILTIN,
      81             :     BPF_LOADER_BUILTIN,
      82             :     BPF_LOADER_UPGRADEABLE_BUILTIN,
      83             :     COMPUTE_BUDGET_PROGRAM_BUILTIN,
      84             :     ZK_TOKEN_PROOF_PROGRAM_BUILTIN,
      85             :     ZK_ELGAMAL_PROOF_PROGRAM_BUILTIN
      86             : };
      87          20 : #define BUILTIN_PROGRAMS_COUNT (sizeof(builtin_programs) / sizeof(fd_builtin_program_t))
      88             : 
      89             : /* Used by the compute budget program to determine how many CUs to deduct by default
      90             :    https://github.com/anza-xyz/agave/blob/v2.1.13/builtins-default-costs/src/lib.rs#L113-L139 */
      91             : static fd_core_bpf_migration_config_t const * migrating_builtins[] = {
      92             :   /* Any future migrating builtins should be added here. Intentionally
      93             :      not cleaned up to support future migrations. */
      94             :   NULL,
      95             : };
      96       54094 : #define MIGRATING_BUILTINS_COUNT (sizeof(migrating_builtins) / sizeof(fd_core_bpf_migration_config_t const *))
      97             : 
      98             : /* https://github.com/anza-xyz/agave/blob/v2.1.13/builtins-default-costs/src/lib.rs#L141-L193 */
      99             : #define MAP_PERFECT_NAME fd_non_migrating_builtins_tbl
     100             : #define MAP_PERFECT_LG_TBL_SZ 4
     101             : #define MAP_PERFECT_T fd_pubkey_t
     102        8083 : #define MAP_PERFECT_HASH_C 146U
     103             : #define MAP_PERFECT_KEY uc
     104             : #define MAP_PERFECT_KEY_T fd_pubkey_t const *
     105             : #define MAP_PERFECT_ZERO_KEY  (0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0)
     106             : #define MAP_PERFECT_COMPLEX_KEY 1
     107        8083 : #define MAP_PERFECT_KEYS_EQUAL(k1,k2) (!memcmp( (k1), (k2), 32UL ))
     108             : 
     109        8083 : #define PERFECT_HASH( u ) (((MAP_PERFECT_HASH_C*(u))>>28)&0x0FU)
     110             : 
     111             : #define MAP_PERFECT_HASH_PP( a00,a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,a12,a13,a14,a15, \
     112             :                              a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31) \
     113             :                                           PERFECT_HASH( (a08 | (a09<<8) | (a10<<16) | (a11<<24)) )
     114        8083 : #define MAP_PERFECT_HASH_R( ptr ) PERFECT_HASH( fd_uint_load_4( (uchar const *)ptr->uc + 8UL ) )
     115             : 
     116             : #define MAP_PERFECT_0      ( VOTE_PROG_ID            ),
     117             : #define MAP_PERFECT_1      ( SYS_PROG_ID             ),
     118             : #define MAP_PERFECT_2      ( COMPUTE_BUDGET_PROG_ID  ),
     119             : #define MAP_PERFECT_3      ( BPF_UPGRADEABLE_PROG_ID ),
     120             : #define MAP_PERFECT_4      ( BPF_LOADER_1_PROG_ID    ),
     121             : #define MAP_PERFECT_5      ( BPF_LOADER_2_PROG_ID    ),
     122             : #define MAP_PERFECT_6      ( LOADER_V4_PROG_ID       ),
     123             : #define MAP_PERFECT_7      ( KECCAK_SECP_PROG_ID     ),
     124             : #define MAP_PERFECT_8      ( ED25519_SV_PROG_ID      ),
     125             : 
     126             : #include "../../../util/tmpl/fd_map_perfect.c"
     127             : #undef PERFECT_HASH
     128             : 
     129             : // https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L4944
     130             : static int
     131             : fd_builtin_is_bpf( fd_accdb_user_t *         accdb,
     132             :                    fd_funk_txn_xid_t const * xid,
     133           0 :                    fd_pubkey_t const  *      pubkey ) {
     134           0 :   fd_accdb_ro_t ro[1];
     135           0 :   if( !fd_accdb_open_ro( accdb, ro, xid, pubkey ) ) {
     136           0 :     return 0;
     137           0 :   }
     138           0 :   int is_bpf = memcmp( fd_accdb_ref_owner( ro ), &fd_solana_bpf_loader_upgradeable_program_id, sizeof(fd_solana_bpf_loader_upgradeable_program_id) )==0;
     139           0 :   fd_accdb_close_ro( accdb, ro );
     140           0 :   return is_bpf;
     141           0 : }
     142             : 
     143             : 
     144             : /* BuiltIn programs need "bogus" executable accounts to exist.
     145             :    These are loaded and ignored during execution.
     146             : 
     147             :    Bogus accounts are marked as "executable", but their data is a
     148             :    hardcoded ASCII string. */
     149             : 
     150             : /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/src/native_loader.rs#L19 */
     151             : void
     152             : fd_write_builtin_account( fd_bank_t  *              bank,
     153             :                           fd_accdb_user_t *         accdb,
     154             :                           fd_funk_txn_xid_t const * xid,
     155             :                           fd_capture_ctx_t *        capture_ctx,
     156             :                           fd_pubkey_t const         pubkey,
     157             :                           void const *              data,
     158           0 :                           ulong                     sz ) {
     159             : 
     160           0 :   fd_accdb_rw_t rw[1];
     161           0 :   fd_accdb_open_rw( accdb, rw, xid, &pubkey, sz, FD_ACCDB_FLAG_CREATE );
     162             : 
     163           0 :   fd_lthash_value_t prev_hash[1];
     164           0 :   fd_hashes_account_lthash(
     165           0 :     &pubkey,
     166           0 :     rw->meta,
     167           0 :     fd_accdb_ref_data_const( rw->ro ),
     168           0 :     prev_hash );
     169             : 
     170           0 :   fd_accdb_ref_data_set( accdb, rw, data, sz );
     171           0 :   fd_accdb_ref_lamports_set( rw, 1UL );
     172           0 :   fd_accdb_ref_exec_bit_set( rw, 1 );
     173           0 :   fd_accdb_ref_owner_set( rw, &fd_solana_native_loader_id );
     174             : 
     175           0 :   fd_hashes_update_lthash( &pubkey, rw->meta, prev_hash, bank, capture_ctx );
     176           0 :   fd_bank_capitalization_set( bank, fd_bank_capitalization_get( bank ) + 1UL );
     177           0 :   fd_accdb_close_rw( accdb, rw );
     178           0 : }
     179             : 
     180             : void
     181             : fd_builtin_programs_init( fd_bank_t *               bank,
     182             :                           fd_accdb_user_t *         accdb,
     183             :                           fd_funk_txn_xid_t const * xid,
     184           0 :                           fd_capture_ctx_t *        capture_ctx ) {
     185             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/builtins/src/lib.rs#L52 */
     186           0 :   fd_builtin_program_t const * builtins = fd_builtins();
     187             : 
     188           0 :   for( ulong i=0UL; i<fd_num_builtins(); i++ ) {
     189             :     /** https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L4949 */
     190           0 :     if( fd_bank_slot_get( bank )==0UL && builtins[i].enable_feature_offset==NO_ENABLE_FEATURE_ID && !fd_builtin_is_bpf( accdb, xid, builtins[i].pubkey ) ) {
     191           0 :       fd_write_builtin_account( bank, accdb, xid, capture_ctx, *builtins[i].pubkey, builtins[i].data, strlen( builtins[i].data ) );
     192           0 :     } else if( builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_query( bank ), builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
     193           0 :       continue;
     194           0 :     } else if( builtins[i].enable_feature_offset!=NO_ENABLE_FEATURE_ID && !FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_query( bank ), builtins[i].enable_feature_offset ) ) {
     195           0 :       continue;
     196           0 :     } else {
     197           0 :       fd_write_builtin_account( bank, accdb, xid, capture_ctx, *builtins[i].pubkey, builtins[i].data, strlen(builtins[i].data) );
     198           0 :     }
     199           0 :   }
     200             : 
     201             :   /* Precompiles have empty account data */
     202           0 :   fd_write_builtin_account( bank, accdb, xid, capture_ctx, fd_solana_keccak_secp_256k_program_id, "", 0 );
     203           0 :   fd_write_builtin_account( bank, accdb, xid, capture_ctx, fd_solana_ed25519_sig_verify_program_id, "", 0 );
     204           0 :   if( FD_FEATURE_ACTIVE_BANK( bank, enable_secp256r1_precompile ) ) {
     205           0 :     fd_write_builtin_account( bank, accdb, xid, capture_ctx, fd_solana_secp256r1_program_id, "", 0 );
     206           0 :   }
     207           0 : }
     208             : 
     209             : fd_builtin_program_t const *
     210           2 : fd_builtins( void ) {
     211           2 :   return builtin_programs;
     212           2 : }
     213             : 
     214             : ulong
     215          20 : fd_num_builtins( void ) {
     216          20 :   return BUILTIN_PROGRAMS_COUNT;
     217          20 : }
     218             : 
     219             : fd_stateless_builtin_program_t const *
     220           2 : fd_stateless_builtins( void ) {
     221           2 :   return stateless_programs_builtins;
     222           2 : }
     223             : 
     224             : ulong
     225           6 : fd_num_stateless_builtins( void ) {
     226           6 :   return STATELESS_BUILTINS_COUNT;
     227           6 : }
     228             : 
     229             : uchar
     230             : fd_is_migrating_builtin_program( fd_bank_t const *   bank,
     231             :                                  fd_pubkey_t const * pubkey,
     232       27047 :                                  uchar *             migrated_yet ) {
     233       27047 :   *migrated_yet = 0;
     234             : 
     235       54094 :   for( ulong i=0; i<MIGRATING_BUILTINS_COUNT; i++ ) {
     236       27047 :     fd_core_bpf_migration_config_t const * config = migrating_builtins[i];
     237       27047 :     if( !config ) continue;
     238             : 
     239           0 :     if( !memcmp( pubkey->uc, config->builtin_program_id->key, sizeof(fd_pubkey_t) ) ) {
     240           0 :       if( config->enable_feature_offset!=NO_ENABLE_FEATURE_ID &&
     241           0 :         FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_query( bank ), config->enable_feature_offset ) ) {
     242             :         /* The program has been migrated to BPF. */
     243           0 :         *migrated_yet = 1;
     244           0 :       }
     245             : 
     246           0 :       return 1;
     247           0 :     }
     248           0 :   }
     249             : 
     250             :   /* No migration config exists for this program */
     251       27047 :   return 0;
     252       27047 : }
     253             : 
     254             : FD_FN_PURE uchar
     255        8083 : fd_is_non_migrating_builtin_program( fd_pubkey_t const * pubkey ) {
     256        8083 :   return !!( fd_non_migrating_builtins_tbl_contains( pubkey ) );
     257        8083 : }

Generated by: LCOV version 1.14