LCOV - code coverage report
Current view: top level - flamenco/runtime/program - fd_builtin_programs.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 111 0.0 %
Date: 2025-07-01 05:00:49 Functions: 0 11 0.0 %

          Line data    Source code
       1             : #include "fd_builtin_programs.h"
       2             : #include "fd_precompiles.h"
       3             : #include "../fd_runtime.h"
       4             : #include "../fd_acc_mgr.h"
       5             : #include "../fd_system_ids.h"
       6             : #include "../fd_system_ids_pp.h"
       7             : 
       8             : #define BUILTIN_PROGRAM(program_id, name, feature_offset, migration_config) \
       9             :     {                                                                       \
      10             :         program_id,                                                         \
      11             :         name,                                                               \
      12             :         feature_offset,                                                     \
      13             :         migration_config                                                    \
      14             :     }
      15             : 
      16             : #define STATELESS_BUILTIN(program_id, migration_config) \
      17             :     {                                                   \
      18             :         program_id,                                     \
      19             :         migration_config                                \
      20             :     }
      21             : 
      22             : #define CORE_BPF_MIGRATION_CONFIG(source_buffer_address, upgrade_authority_address, enable_feature_offset, builtin_program_id) \
      23             :     {                                                                                                                          \
      24             :         source_buffer_address,                                                                                                 \
      25             :         upgrade_authority_address,                                                                                             \
      26             :         enable_feature_offset,                                                                                                 \
      27             :         builtin_program_id                                                                                                     \
      28             :     }
      29             : 
      30             : #define PRECOMPILE(program_id, feature_offset, verify_fn) \
      31             :     {                                                     \
      32             :         program_id,                                       \
      33             :         feature_offset,                                   \
      34             :         verify_fn                                         \
      35             :     }
      36             : 
      37             : #define NO_CORE_BPF_MIGRATION_CONFIG NULL
      38             : 
      39             : #define DEFINE_CORE_BPF_MIGRATION_CONFIG(name, buffer_address, feature_offset, program_id) \
      40             :     static const fd_core_bpf_migration_config_t name = {                                   \
      41             :         buffer_address,                                                                    \
      42             :         NULL,                                                                              \
      43             :         offsetof(fd_features_t, feature_offset),                                           \
      44             :         program_id                                                                         \
      45             :     };                                                                                     \
      46             :     static const fd_core_bpf_migration_config_t * const MIGRATE_##name = &name
      47             : 
      48             : DEFINE_CORE_BPF_MIGRATION_CONFIG(BUILTIN_TO_CORE_BPF_STAKE_PROGRAM_CONFIG,                &fd_solana_stake_program_buffer_address,                migrate_stake_program_to_core_bpf,                &fd_solana_stake_program_id);
      49             : DEFINE_CORE_BPF_MIGRATION_CONFIG(BUILTIN_TO_CORE_BPF_CONFIG_PROGRAM_CONFIG,               &fd_solana_config_program_buffer_address,               migrate_config_program_to_core_bpf,               &fd_solana_config_program_id);
      50             : DEFINE_CORE_BPF_MIGRATION_CONFIG(BUILTIN_TO_CORE_BPF_ADDRESS_LOOKUP_TABLE_PROGRAM_CONFIG, &fd_solana_address_lookup_table_program_buffer_address, migrate_address_lookup_table_program_to_core_bpf, &fd_solana_address_lookup_table_program_id);
      51             : DEFINE_CORE_BPF_MIGRATION_CONFIG(STATELESS_TO_CORE_BPF_FEATURE_GATE_PROGRAM_CONFIG,       &fd_solana_feature_program_buffer_address,              migrate_feature_gate_program_to_core_bpf,         &fd_solana_feature_program_id);
      52             : 
      53             : #define SYSTEM_PROGRAM_BUILTIN                BUILTIN_PROGRAM(&fd_solana_system_program_id,                 "system_program",                        NO_ENABLE_FEATURE_ID,                                             NO_CORE_BPF_MIGRATION_CONFIG)
      54             : #define VOTE_PROGRAM_BUILTIN                  BUILTIN_PROGRAM(&fd_solana_vote_program_id,                   "vote_program",                          NO_ENABLE_FEATURE_ID,                                             NO_CORE_BPF_MIGRATION_CONFIG)
      55             : #define STAKE_PROGRAM_BUILTIN                 BUILTIN_PROGRAM(&fd_solana_stake_program_id,                  "stake_program",                         NO_ENABLE_FEATURE_ID,                                             MIGRATE_BUILTIN_TO_CORE_BPF_STAKE_PROGRAM_CONFIG)
      56             : #define CONFIG_PROGRAM_BUILTIN                BUILTIN_PROGRAM(&fd_solana_config_program_id,                 "config_program",                        NO_ENABLE_FEATURE_ID,                                             MIGRATE_BUILTIN_TO_CORE_BPF_CONFIG_PROGRAM_CONFIG)
      57             : #define LOADER_V4_BUILTIN                     BUILTIN_PROGRAM(&fd_solana_bpf_loader_v4_program_id,          "loader_v4",                             offsetof(fd_features_t, enable_loader_v4),                        NO_CORE_BPF_MIGRATION_CONFIG)
      58             : #define ADDRESS_LOOKUP_TABLE_PROGRAM_BUILTIN  BUILTIN_PROGRAM(&fd_solana_address_lookup_table_program_id,   "address_lookup_table_program",          NO_ENABLE_FEATURE_ID,                                             MIGRATE_BUILTIN_TO_CORE_BPF_ADDRESS_LOOKUP_TABLE_PROGRAM_CONFIG)
      59             : #define BPF_LOADER_DEPRECATED_BUILTIN         BUILTIN_PROGRAM(&fd_solana_bpf_loader_deprecated_program_id,  "solana_bpf_loader_deprecated_program",  NO_ENABLE_FEATURE_ID,                                             NO_CORE_BPF_MIGRATION_CONFIG)
      60             : #define BPF_LOADER_BUILTIN                    BUILTIN_PROGRAM(&fd_solana_bpf_loader_program_id,             "solana_bpf_loader_program",             NO_ENABLE_FEATURE_ID,                                             NO_CORE_BPF_MIGRATION_CONFIG)
      61             : #define BPF_LOADER_UPGRADEABLE_BUILTIN        BUILTIN_PROGRAM(&fd_solana_bpf_loader_upgradeable_program_id, "solana_bpf_loader_upgradeable_program", NO_ENABLE_FEATURE_ID,                                             NO_CORE_BPF_MIGRATION_CONFIG)
      62             : #define COMPUTE_BUDGET_PROGRAM_BUILTIN        BUILTIN_PROGRAM(&fd_solana_compute_budget_program_id,         "compute_budget_program",                NO_ENABLE_FEATURE_ID,                                             NO_CORE_BPF_MIGRATION_CONFIG)
      63             : #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),                    NO_CORE_BPF_MIGRATION_CONFIG)
      64             : #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),        NO_CORE_BPF_MIGRATION_CONFIG)
      65             : 
      66             : #define FEATURE_PROGRAM_BUILTIN               STATELESS_BUILTIN(&fd_solana_feature_program_id, MIGRATE_STATELESS_TO_CORE_BPF_FEATURE_GATE_PROGRAM_CONFIG)
      67             : 
      68             : #define SECP256R1_PROGRAM_PRECOMPILE          PRECOMPILE(&fd_solana_secp256r1_program_id,          offsetof(fd_features_t, enable_secp256r1_precompile), fd_precompile_secp256r1_verify)
      69             : #define KECCAK_SECP_PROGRAM_PRECOMPILE        PRECOMPILE(&fd_solana_keccak_secp_256k_program_id,   NO_ENABLE_FEATURE_ID,                                 fd_precompile_secp256k1_verify)
      70             : #define ED25519_SV_PROGRAM_PRECOMPILE         PRECOMPILE(&fd_solana_ed25519_sig_verify_program_id, NO_ENABLE_FEATURE_ID,                                 fd_precompile_ed25519_verify)
      71             : 
      72             : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank/builtins/mod.rs#L133-L143 */
      73             : static const fd_stateless_builtin_program_t stateless_programs_builtins[] = {
      74             :     FEATURE_PROGRAM_BUILTIN
      75             : };
      76           0 : #define STATELESS_BUILTINS_COUNT (sizeof(stateless_programs_builtins) / sizeof(fd_stateless_builtin_program_t))
      77             : 
      78             : static const fd_precompile_program_t precompiles[] = {
      79             :     SECP256R1_PROGRAM_PRECOMPILE,
      80             :     KECCAK_SECP_PROGRAM_PRECOMPILE,
      81             :     ED25519_SV_PROGRAM_PRECOMPILE
      82             : };
      83           0 : #define PRECOMPILE_PROGRAMS_COUNT (sizeof(precompiles) / sizeof(fd_precompile_program_t))
      84             : 
      85             : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank/builtins/mod.rs#L34-L131 */
      86             : static fd_builtin_program_t const builtin_programs[] = {
      87             :     SYSTEM_PROGRAM_BUILTIN,
      88             :     VOTE_PROGRAM_BUILTIN,
      89             :     STAKE_PROGRAM_BUILTIN,
      90             :     CONFIG_PROGRAM_BUILTIN,
      91             :     LOADER_V4_BUILTIN,
      92             :     ADDRESS_LOOKUP_TABLE_PROGRAM_BUILTIN,
      93             :     BPF_LOADER_DEPRECATED_BUILTIN,
      94             :     BPF_LOADER_BUILTIN,
      95             :     BPF_LOADER_UPGRADEABLE_BUILTIN,
      96             :     COMPUTE_BUDGET_PROGRAM_BUILTIN,
      97             :     ZK_TOKEN_PROOF_PROGRAM_BUILTIN,
      98             :     ZK_ELGAMAL_PROOF_PROGRAM_BUILTIN
      99             : };
     100           0 : #define BUILTIN_PROGRAMS_COUNT (sizeof(builtin_programs) / sizeof(fd_builtin_program_t))
     101             : 
     102             : /* Used by the compute budget program to determine how many CUs to deduct by default
     103             :    https://github.com/anza-xyz/agave/blob/v2.1.13/builtins-default-costs/src/lib.rs#L113-L139 */
     104             : static fd_core_bpf_migration_config_t const * migrating_builtins[] = {
     105             :     MIGRATE_BUILTIN_TO_CORE_BPF_STAKE_PROGRAM_CONFIG,
     106             :     MIGRATE_BUILTIN_TO_CORE_BPF_CONFIG_PROGRAM_CONFIG,
     107             :     MIGRATE_BUILTIN_TO_CORE_BPF_ADDRESS_LOOKUP_TABLE_PROGRAM_CONFIG,
     108             : };
     109           0 : #define MIGRATING_BUILTINS_COUNT (sizeof(migrating_builtins) / sizeof(fd_core_bpf_migration_config_t const *))
     110             : 
     111             : /* Using MAP_PERFECT instead of a list for optimization
     112             :    https://github.com/anza-xyz/agave/blob/v2.1.13/builtins-default-costs/src/lib.rs#L141-L193 */
     113             : #define MAP_PERFECT_NAME fd_non_migrating_builtins_tbl
     114             : #define MAP_PERFECT_LG_TBL_SZ 4
     115             : #define MAP_PERFECT_T fd_pubkey_t
     116           0 : #define MAP_PERFECT_HASH_C 146U
     117             : #define MAP_PERFECT_KEY uc
     118             : #define MAP_PERFECT_KEY_T fd_pubkey_t const *
     119             : #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)
     120             : #define MAP_PERFECT_COMPLEX_KEY 1
     121           0 : #define MAP_PERFECT_KEYS_EQUAL(k1,k2) (!memcmp( (k1), (k2), 32UL ))
     122             : 
     123           0 : #define PERFECT_HASH( u ) (((MAP_PERFECT_HASH_C*(u))>>28)&0x0FU)
     124             : 
     125             : #define MAP_PERFECT_HASH_PP( a00,a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,a12,a13,a14,a15, \
     126             :                              a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31) \
     127             :                                           PERFECT_HASH( (a08 | (a09<<8) | (a10<<16) | (a11<<24)) )
     128           0 : #define MAP_PERFECT_HASH_R( ptr ) PERFECT_HASH( fd_uint_load_4( (uchar const *)ptr->uc + 8UL ) )
     129             : 
     130             : #define MAP_PERFECT_0      ( VOTE_PROG_ID            ),
     131             : #define MAP_PERFECT_1      ( SYS_PROG_ID             ),
     132             : #define MAP_PERFECT_2      ( COMPUTE_BUDGET_PROG_ID  ),
     133             : #define MAP_PERFECT_3      ( BPF_UPGRADEABLE_PROG_ID ),
     134             : #define MAP_PERFECT_4      ( BPF_LOADER_1_PROG_ID    ),
     135             : #define MAP_PERFECT_5      ( BPF_LOADER_2_PROG_ID    ),
     136             : #define MAP_PERFECT_6      ( LOADER_V4_PROG_ID       ),
     137             : #define MAP_PERFECT_7      ( KECCAK_SECP_PROG_ID     ),
     138             : #define MAP_PERFECT_8      ( ED25519_SV_PROG_ID      ),
     139             : 
     140             : #include "../../../util/tmpl/fd_map_perfect.c"
     141             : #undef PERFECT_HASH
     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_exec_slot_ctx_t * slot_ctx,
     153             :                           fd_pubkey_t const    pubkey,
     154             :                           char const *         data,
     155           0 :                           ulong                sz ) {
     156             : 
     157           0 :   fd_funk_t *         funk = slot_ctx->funk;
     158           0 :   fd_funk_txn_t *     txn  = slot_ctx->funk_txn;
     159           0 :   FD_TXN_ACCOUNT_DECL( rec );
     160             : 
     161           0 :   int err = fd_txn_account_init_from_funk_mutable( rec, &pubkey, funk, txn, 1, sz );
     162           0 :   FD_TEST( !err );
     163             : 
     164           0 :   rec->vt->set_data( rec, data, sz );
     165           0 :   rec->vt->set_lamports( rec, 1UL );
     166           0 :   rec->vt->set_rent_epoch( rec, 0UL );
     167           0 :   rec->vt->set_executable( rec, 1 );
     168           0 :   rec->vt->set_owner( rec, &fd_solana_native_loader_id );
     169             : 
     170           0 :   fd_txn_account_mutable_fini( rec, funk, txn );
     171             : 
     172           0 :   fd_bank_capitalization_set( slot_ctx->bank, fd_bank_capitalization_get( slot_ctx->bank ) + 1UL );
     173             : 
     174             :   // err = fd_acc_mgr_commit( acc_mgr, rec, slot_ctx );
     175           0 :   FD_TEST( !err );
     176           0 : }
     177             : 
     178             : /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/runtime/src/inline_spl_token.rs#L74 */
     179             : /* TODO: move this somewhere more appropiate */
     180             : static void
     181           0 : write_inline_spl_native_mint_program_account( fd_exec_slot_ctx_t * slot_ctx ) {
     182             : 
     183           0 :   if( true ) {
     184             :     /* FIXME: This is a hack that corresponds to the cluster type field
     185             :        in Agave. This needs to get implemented properly in Firedancer. */
     186           0 :     return;
     187           0 :   }
     188             : 
     189           0 :   fd_funk_t *         funk = slot_ctx->funk;
     190           0 :   fd_funk_txn_t *     txn  = slot_ctx->funk_txn;
     191           0 :   fd_pubkey_t const * key  = (fd_pubkey_t const *)&fd_solana_spl_native_mint_id;
     192           0 :   FD_TXN_ACCOUNT_DECL( rec );
     193             : 
     194             :   /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/runtime/src/inline_spl_token.rs#L86-L90 */
     195           0 :   static uchar const data[] = {
     196           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,
     197           0 :       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     198           0 :       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
     199             : 
     200           0 :   int err = fd_txn_account_init_from_funk_mutable( rec, key, funk, txn, 1, sizeof(data) );
     201           0 :   FD_TEST( !err );
     202             : 
     203           0 :   rec->vt->set_lamports( rec, 1000000000UL );
     204           0 :   rec->vt->set_rent_epoch( rec, 1UL );
     205           0 :   rec->vt->set_executable( rec, 0 );
     206           0 :   rec->vt->set_owner( rec, &fd_solana_spl_token_id );
     207           0 :   rec->vt->set_data( rec, data, sizeof(data) );
     208             : 
     209           0 :   fd_txn_account_mutable_fini( rec, funk, txn );
     210             : 
     211           0 :   FD_TEST( !err );
     212           0 : }
     213             : 
     214           0 : void fd_builtin_programs_init( fd_exec_slot_ctx_t * slot_ctx ) {
     215             :   // https://github.com/anza-xyz/agave/blob/v2.0.1/runtime/src/bank/builtins/mod.rs#L33
     216           0 :   fd_builtin_program_t const * builtins = fd_builtins();
     217           0 :   for( ulong i=0UL; i<fd_num_builtins(); i++ ) {
     218           0 :     if( builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( slot_ctx->slot, fd_bank_features_get( slot_ctx->bank ), builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
     219           0 :       continue;
     220           0 :     } else if( builtins[i].enable_feature_offset!=NO_ENABLE_FEATURE_ID && !FD_FEATURE_ACTIVE_OFFSET( slot_ctx->slot, fd_bank_features_get( slot_ctx->bank ), builtins[i].enable_feature_offset ) ) {
     221           0 :       continue;
     222           0 :     } else {
     223           0 :       fd_write_builtin_account( slot_ctx, *builtins[i].pubkey, builtins[i].data, strlen(builtins[i].data) );
     224           0 :     }
     225           0 :   }
     226             : 
     227             :   //TODO: remove when no longer necessary
     228           0 :   if( FD_FEATURE_ACTIVE_BANK( slot_ctx->bank, zk_token_sdk_enabled ) ) {
     229           0 :     fd_write_builtin_account( slot_ctx, fd_solana_zk_token_proof_program_id, "zk_token_proof_program", 22UL );
     230           0 :   }
     231             : 
     232           0 :   if( FD_FEATURE_ACTIVE_BANK( slot_ctx->bank, zk_elgamal_proof_program_enabled ) ) {
     233           0 :     fd_write_builtin_account( slot_ctx, fd_solana_zk_elgamal_proof_program_id, "zk_elgamal_proof_program", 24UL );
     234           0 :   }
     235             : 
     236             :   /* Precompiles have empty account data */
     237           0 :   if( fd_bank_cluster_version_get( slot_ctx->bank ).major == 1 ) {
     238           0 :     char data[1] = {1};
     239           0 :     fd_write_builtin_account( slot_ctx, fd_solana_keccak_secp_256k_program_id, data, 1 );
     240           0 :     fd_write_builtin_account( slot_ctx, fd_solana_ed25519_sig_verify_program_id, data, 1 );
     241           0 :     if( FD_FEATURE_ACTIVE_BANK( slot_ctx->bank, enable_secp256r1_precompile ) )
     242           0 :       fd_write_builtin_account( slot_ctx, fd_solana_secp256r1_program_id, data, 1 );
     243           0 :   } else {
     244           0 :     fd_write_builtin_account( slot_ctx, fd_solana_keccak_secp_256k_program_id, "", 0 );
     245           0 :     fd_write_builtin_account( slot_ctx, fd_solana_ed25519_sig_verify_program_id, "", 0 );
     246           0 :     if( FD_FEATURE_ACTIVE_BANK( slot_ctx->bank, enable_secp256r1_precompile ) )
     247           0 :       fd_write_builtin_account( slot_ctx, fd_solana_secp256r1_program_id, "", 0 );
     248           0 :   }
     249             : 
     250             :   /* Inline SPL token mint program ("inlined to avoid an external dependency on the spl-token crate") */
     251           0 :   write_inline_spl_native_mint_program_account( slot_ctx );
     252           0 : }
     253             : 
     254             : fd_builtin_program_t const *
     255           0 : fd_builtins( void ) {
     256           0 :   return builtin_programs;
     257           0 : }
     258             : 
     259             : ulong
     260           0 : fd_num_builtins( void ) {
     261           0 :   return BUILTIN_PROGRAMS_COUNT;
     262           0 : }
     263             : 
     264             : fd_stateless_builtin_program_t const *
     265           0 : fd_stateless_builtins( void ) {
     266           0 :   return stateless_programs_builtins;
     267           0 : }
     268             : 
     269             : ulong
     270           0 : fd_num_stateless_builtins( void ) {
     271           0 :   return STATELESS_BUILTINS_COUNT;
     272           0 : }
     273             : 
     274             : fd_precompile_program_t const *
     275           0 : fd_precompiles( void ) {
     276           0 :   return precompiles;
     277           0 : }
     278             : 
     279             : ulong
     280           0 : fd_num_precompiles( void ) {
     281           0 :   return PRECOMPILE_PROGRAMS_COUNT;
     282           0 : }
     283             : 
     284             : uchar
     285             : fd_is_migrating_builtin_program( fd_exec_txn_ctx_t const * txn_ctx,
     286             :                                  fd_pubkey_t const *       pubkey,
     287           0 :                                  uchar *                   migrated_yet ) {
     288           0 :   *migrated_yet = 0;
     289             : 
     290           0 :   for( ulong i=0; i<MIGRATING_BUILTINS_COUNT; i++ ) {
     291           0 :     fd_core_bpf_migration_config_t const * config = migrating_builtins[i];
     292           0 :     if( !memcmp( pubkey->uc, config->builtin_program_id->key, sizeof(fd_pubkey_t) ) ) {
     293           0 :       if( config->enable_feature_offset!=NO_ENABLE_FEATURE_ID &&
     294           0 :         FD_FEATURE_ACTIVE_OFFSET( txn_ctx->slot, txn_ctx->features, config->enable_feature_offset ) ) {
     295             :         /* The program has been migrated to BPF. */
     296           0 :         *migrated_yet = 1;
     297           0 :       }
     298             : 
     299           0 :       return 1;
     300           0 :     }
     301           0 :   }
     302             : 
     303             :   /* No migration config exists for this program */
     304           0 :   return 0;
     305           0 : }
     306             : 
     307             : FD_FN_PURE uchar
     308           0 : fd_is_non_migrating_builtin_program( fd_pubkey_t const * pubkey ) {
     309           0 :   return !!( fd_non_migrating_builtins_tbl_contains( pubkey ) );
     310           0 : }

Generated by: LCOV version 1.14