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

Generated by: LCOV version 1.14