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-09-17 04:28:31 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 "../fd_accdb_svm.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         846 : #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       42390 : #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        5742 : #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         360 : #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         360 : #define MAP_PERFECT_KEYS_EQUAL(k1,k2) (!memcmp( (k1), (k2), 32UL ))
     108             : 
     109         360 : #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         360 : #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_t *         accdb,
     132             :                    fd_accdb_fork_id_t   fork_id,
     133           0 :                    fd_pubkey_t const  * pubkey ) {
     134           0 :   fd_acc_t acc = fd_accdb_read_one( accdb, fork_id, pubkey->uc );
     135           0 :   if( FD_UNLIKELY( !acc.lamports ) ) {
     136           0 :     fd_accdb_unread_one( accdb, &acc );
     137           0 :     return 0;
     138           0 :   }
     139             : 
     140           0 :   int is_bpf = !memcmp( acc.owner, fd_solana_bpf_loader_upgradeable_program_id.uc, 32UL );
     141           0 :   fd_accdb_unread_one( accdb, &acc );
     142           0 :   return is_bpf;
     143           0 : }
     144             : 
     145             : /* BuiltIn programs need "bogus" executable accounts to exist.
     146             :    These are loaded and ignored during execution.
     147             : 
     148             :    Bogus accounts are marked as "executable", but their data is a
     149             :    hardcoded ASCII string. */
     150             : 
     151             : /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/src/native_loader.rs#L19 */
     152             : void
     153             : fd_write_builtin_account( fd_bank_t  *       bank,
     154             :                           fd_accdb_t *       accdb,
     155             :                           fd_capture_ctx_t * capture_ctx,
     156             :                           fd_pubkey_t const  pubkey,
     157             :                           void const *       data,
     158           0 :                           ulong              sz ) {
     159           0 :   fd_accdb_svm_write(
     160           0 :       bank, accdb, capture_ctx,
     161           0 :       &pubkey,
     162           0 :       &fd_solana_native_loader_id, /* owner */
     163           0 :       data, sz,                    /* data */
     164           0 :       1UL,                         /* lamports_min */
     165           0 :       1                            /* exec_bit */
     166           0 :  , 0 );
     167           0 : }
     168             : 
     169             : void
     170             : fd_builtin_programs_init( fd_bank_t *        bank,
     171             :                           fd_accdb_t *       accdb,
     172           0 :                           fd_capture_ctx_t * capture_ctx ) {
     173             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/builtins/src/lib.rs#L52 */
     174           0 :   fd_builtin_program_t const * builtins = fd_builtins();
     175             : 
     176           0 :   for( ulong i=0UL; i<fd_num_builtins(); i++ ) {
     177             :     /** https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L4949 */
     178           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 ) ) {
     179           0 :       fd_write_builtin_account( bank, accdb, capture_ctx, *builtins[i].pubkey, builtins[i].data, strlen( builtins[i].data ) );
     180           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 ) ) {
     181           0 :       continue;
     182           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 ) ) {
     183           0 :       continue;
     184           0 :     } else {
     185           0 :       fd_write_builtin_account( bank, accdb, capture_ctx, *builtins[i].pubkey, builtins[i].data, strlen(builtins[i].data) );
     186           0 :     }
     187           0 :   }
     188             : 
     189             :   /* Precompiles have empty account data */
     190           0 :   fd_write_builtin_account( bank, accdb, capture_ctx, fd_solana_keccak_secp_256k_program_id, "", 0 );
     191           0 :   fd_write_builtin_account( bank, accdb, capture_ctx, fd_solana_ed25519_sig_verify_program_id, "", 0 );
     192           0 :   fd_write_builtin_account( bank, accdb, capture_ctx, fd_solana_secp256r1_program_id, "", 0 );
     193           0 : }
     194             : 
     195             : fd_builtin_program_t const *
     196        4239 : fd_builtins( void ) {
     197        4239 :   return builtin_programs;
     198        4239 : }
     199             : 
     200             : ulong
     201       42390 : fd_num_builtins( void ) {
     202       42390 :   return BUILTIN_PROGRAMS_COUNT;
     203       42390 : }
     204             : 
     205             : fd_stateless_builtin_program_t const *
     206         282 : fd_stateless_builtins( void ) {
     207         282 :   return stateless_programs_builtins;
     208         282 : }
     209             : 
     210             : ulong
     211         846 : fd_num_stateless_builtins( void ) {
     212         846 :   return STATELESS_BUILTINS_COUNT;
     213         846 : }
     214             : 
     215             : uchar
     216             : fd_is_migrating_builtin_program( fd_bank_t const *   bank,
     217             :                                  fd_pubkey_t const * pubkey,
     218        2871 :                                  uchar *             migrated_yet ) {
     219        2871 :   *migrated_yet = 0;
     220             : 
     221        5742 :   for( ulong i=0; i<MIGRATING_BUILTINS_COUNT; i++ ) {
     222        2871 :     fd_core_bpf_migration_config_t const * config = migrating_builtins[i];
     223        2871 :     if( !config ) continue;
     224             : 
     225           0 :     if( !memcmp( pubkey->uc, config->builtin_program_id->key, sizeof(fd_pubkey_t) ) ) {
     226           0 :       if( config->enable_feature_offset!=NO_ENABLE_FEATURE_ID &&
     227           0 :         FD_FEATURE_ACTIVE_OFFSET( bank->f.slot, &bank->f.features, config->enable_feature_offset ) ) {
     228             :         /* The program has been migrated to BPF. */
     229           0 :         *migrated_yet = 1;
     230           0 :       }
     231             : 
     232           0 :       return 1;
     233           0 :     }
     234           0 :   }
     235             : 
     236             :   /* No migration config exists for this program */
     237        2871 :   return 0;
     238        2871 : }
     239             : 
     240             : FD_FN_PURE uchar
     241         360 : fd_is_non_migrating_builtin_program( fd_pubkey_t const * pubkey ) {
     242         360 :   return !!( fd_non_migrating_builtins_tbl_contains( pubkey ) );
     243         360 : }

Generated by: LCOV version 1.14