LCOV - code coverage report
Current view: top level - flamenco/runtime/program - fd_builtin_programs.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 61 80 76.2 %
Date: 2026-03-22 09:35:25 Functions: 8 9 88.9 %

          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          27 : #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        1080 : #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         252 : #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         111 : #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         111 : #define MAP_PERFECT_KEYS_EQUAL(k1,k2) (!memcmp( (k1), (k2), 32UL ))
     108             : 
     109         111 : #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         111 : #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        1158 :                           ulong                     sz ) {
     159             : 
     160        1158 :   fd_accdb_rw_t rw[1];
     161        1158 :   fd_accdb_open_rw( accdb, rw, xid, &pubkey, sz, FD_ACCDB_FLAG_CREATE );
     162             : 
     163        1158 :   fd_lthash_value_t prev_hash[1];
     164        1158 :   fd_hashes_account_lthash(
     165        1158 :     &pubkey,
     166        1158 :     rw->meta,
     167        1158 :     fd_accdb_ref_data_const( rw->ro ),
     168        1158 :     prev_hash );
     169             : 
     170        1158 :   fd_accdb_ref_data_set( accdb, rw, data, sz );
     171        1158 :   fd_accdb_ref_lamports_set( rw, 1UL );
     172        1158 :   fd_accdb_ref_exec_bit_set( rw, 1 );
     173        1158 :   fd_accdb_ref_owner_set( rw, &fd_solana_native_loader_id );
     174             : 
     175        1158 :   fd_hashes_update_lthash( &pubkey, rw->meta, prev_hash, bank, capture_ctx );
     176        1158 :   fd_bank_capitalization_set( bank, fd_bank_capitalization_get( bank ) + 1UL );
     177        1158 :   fd_accdb_close_rw( accdb, rw );
     178        1158 : }
     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          99 :                           fd_capture_ctx_t *        capture_ctx ) {
     185             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/builtins/src/lib.rs#L52 */
     186          99 :   fd_builtin_program_t const * builtins = fd_builtins();
     187             : 
     188         990 :   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         891 :     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         891 :     } 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         891 :     } 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          30 :       continue;
     196         861 :     } else {
     197         861 :       fd_write_builtin_account( bank, accdb, xid, capture_ctx, *builtins[i].pubkey, builtins[i].data, strlen(builtins[i].data) );
     198         861 :     }
     199         891 :   }
     200             : 
     201             :   /* Precompiles have empty account data */
     202          99 :   fd_write_builtin_account( bank, accdb, xid, capture_ctx, fd_solana_keccak_secp_256k_program_id, "", 0 );
     203          99 :   fd_write_builtin_account( bank, accdb, xid, capture_ctx, fd_solana_ed25519_sig_verify_program_id, "", 0 );
     204          99 :   fd_write_builtin_account( bank, accdb, xid, capture_ctx, fd_solana_secp256r1_program_id, "", 0 );
     205          99 : }
     206             : 
     207             : fd_builtin_program_t const *
     208         108 : fd_builtins( void ) {
     209         108 :   return builtin_programs;
     210         108 : }
     211             : 
     212             : ulong
     213        1080 : fd_num_builtins( void ) {
     214        1080 :   return BUILTIN_PROGRAMS_COUNT;
     215        1080 : }
     216             : 
     217             : fd_stateless_builtin_program_t const *
     218           9 : fd_stateless_builtins( void ) {
     219           9 :   return stateless_programs_builtins;
     220           9 : }
     221             : 
     222             : ulong
     223          27 : fd_num_stateless_builtins( void ) {
     224          27 :   return STATELESS_BUILTINS_COUNT;
     225          27 : }
     226             : 
     227             : uchar
     228             : fd_is_migrating_builtin_program( fd_bank_t const *   bank,
     229             :                                  fd_pubkey_t const * pubkey,
     230         126 :                                  uchar *             migrated_yet ) {
     231         126 :   *migrated_yet = 0;
     232             : 
     233         252 :   for( ulong i=0; i<MIGRATING_BUILTINS_COUNT; i++ ) {
     234         126 :     fd_core_bpf_migration_config_t const * config = migrating_builtins[i];
     235         126 :     if( !config ) continue;
     236             : 
     237           0 :     if( !memcmp( pubkey->uc, config->builtin_program_id->key, sizeof(fd_pubkey_t) ) ) {
     238           0 :       if( config->enable_feature_offset!=NO_ENABLE_FEATURE_ID &&
     239           0 :         FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_query( bank ), config->enable_feature_offset ) ) {
     240             :         /* The program has been migrated to BPF. */
     241           0 :         *migrated_yet = 1;
     242           0 :       }
     243             : 
     244           0 :       return 1;
     245           0 :     }
     246           0 :   }
     247             : 
     248             :   /* No migration config exists for this program */
     249         126 :   return 0;
     250         126 : }
     251             : 
     252             : FD_FN_PURE uchar
     253         111 : fd_is_non_migrating_builtin_program( fd_pubkey_t const * pubkey ) {
     254         111 :   return !!( fd_non_migrating_builtins_tbl_contains( pubkey ) );
     255         111 : }

Generated by: LCOV version 1.14