Skip to content

Commit

Permalink
Change visibility of svm-internal structs and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
pgarg66 committed Jan 14, 2025
1 parent d3dc3d0 commit 1923f3b
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 50 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions builtins-default-costs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ edition = { workspace = true }
ahash = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }
qualifier_attr = { workspace = true }
solana-address-lookup-table-program = { workspace = true }
solana-bpf-loader-program = { workspace = true }
solana-compute-budget-program = { workspace = true }
solana-bpf-loader-program = { workspace = true, features = ["svm-internal"] }
solana-compute-budget-program = { workspace = true, features = ["svm-internal"] }
solana-config-program = { workspace = true }
solana-feature-set = { workspace = true }
solana-frozen-abi = { workspace = true, optional = true, features = [
"frozen-abi",
] }
solana-loader-v4-program = { workspace = true }
solana-loader-v4-program = { workspace = true, features = ["svm-internal"] }
solana-pubkey = { workspace = true }
solana-sdk-ids = { workspace = true }
solana-stake-program = { workspace = true }
Expand All @@ -35,6 +36,7 @@ name = "solana_builtins_default_costs"

[dev-dependencies]
rand = "0.8.5"
solana-builtins-default-costs = { path = ".", features = ["svm-internal"] }
static_assertions = { workspace = true }

[package.metadata.docs.rs]
Expand All @@ -46,6 +48,7 @@ frozen-abi = [
"solana-vote-program/frozen-abi",
]
dev-context-only-utils = []
svm-internal = []

[lints]
workspace = true
32 changes: 25 additions & 7 deletions builtins-default-costs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
#![allow(clippy::arithmetic_side_effects)]
#[cfg(feature = "svm-internal")]
use qualifier_attr::qualifiers;
use {
ahash::AHashMap,
lazy_static::lazy_static,
Expand All @@ -13,7 +15,8 @@ use {
};

#[derive(Clone)]
pub struct MigratingBuiltinCost {
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
struct MigratingBuiltinCost {
native_cost: u64,
core_bpf_migration_feature: Pubkey,
// encoding positional information explicitly for migration feature item,
Expand All @@ -24,7 +27,8 @@ pub struct MigratingBuiltinCost {
}

#[derive(Clone)]
pub struct NotMigratingBuiltinCost {
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
struct NotMigratingBuiltinCost {
native_cost: u64,
}

Expand All @@ -35,19 +39,23 @@ pub struct NotMigratingBuiltinCost {
/// When migration completed, eg the feature gate is enabled everywhere, please
/// remove that builtin entry from MIGRATING_BUILTINS_COSTS.
#[derive(Clone)]
pub enum BuiltinCost {
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
enum BuiltinCost {
Migrating(MigratingBuiltinCost),
NotMigrating(NotMigratingBuiltinCost),
}

impl BuiltinCost {
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
fn native_cost(&self) -> u64 {
match self {
BuiltinCost::Migrating(MigratingBuiltinCost { native_cost, .. }) => *native_cost,
BuiltinCost::NotMigrating(NotMigratingBuiltinCost { native_cost }) => *native_cost,
}
}

#[cfg(feature = "svm-internal")]
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
fn core_bpf_migration_feature(&self) -> Option<&Pubkey> {
match self {
BuiltinCost::Migrating(MigratingBuiltinCost {
Expand All @@ -58,13 +66,16 @@ impl BuiltinCost {
}
}

#[cfg(feature = "svm-internal")]
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
fn position(&self) -> Option<usize> {
match self {
BuiltinCost::Migrating(MigratingBuiltinCost { position, .. }) => Some(*position),
BuiltinCost::NotMigrating(_) => None,
}
}

#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
fn has_migrated(&self, feature_set: &FeatureSet) -> bool {
match self {
BuiltinCost::Migrating(MigratingBuiltinCost {
Expand Down Expand Up @@ -109,7 +120,8 @@ static_assertions::const_assert_eq!(
TOTAL_COUNT_BUILTS
);

pub const MIGRATING_BUILTINS_COSTS: &[(Pubkey, BuiltinCost)] = &[
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
const MIGRATING_BUILTINS_COSTS: &[(Pubkey, BuiltinCost)] = &[
(
stake::id(),
BuiltinCost::Migrating(MigratingBuiltinCost {
Expand Down Expand Up @@ -215,13 +227,17 @@ pub fn get_builtin_instruction_cost<'a>(
.map(|builtin_cost| builtin_cost.native_cost())
}

pub enum BuiltinMigrationFeatureIndex {
#[cfg(feature = "svm-internal")]
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
enum BuiltinMigrationFeatureIndex {
NotBuiltin,
BuiltinNoMigrationFeature,
BuiltinWithMigrationFeature(usize),
}

pub fn get_builtin_migration_feature_index(program_id: &Pubkey) -> BuiltinMigrationFeatureIndex {
#[cfg(feature = "svm-internal")]
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
fn get_builtin_migration_feature_index(program_id: &Pubkey) -> BuiltinMigrationFeatureIndex {
BUILTIN_INSTRUCTION_COSTS.get(program_id).map_or(
BuiltinMigrationFeatureIndex::NotBuiltin,
|builtin_cost| {
Expand Down Expand Up @@ -254,7 +270,9 @@ const _: () = validate_position(MIGRATING_BUILTINS_COSTS);

/// Helper function to return ref of migration feature Pubkey at position `index`
/// from MIGRATING_BUILTINS_COSTS
pub fn get_migration_feature_id(index: usize) -> &'static Pubkey {
#[cfg(feature = "svm-internal")]
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
pub(crate) fn get_migration_feature_id(index: usize) -> &'static Pubkey {
MIGRATING_BUILTINS_COSTS
.get(index)
.expect("valid index of MIGRATING_BUILTINS_COSTS")
Expand Down
4 changes: 2 additions & 2 deletions compute-budget-instruction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ edition = { workspace = true }
[dependencies]
log = { workspace = true }
solana-borsh = { workspace = true }
solana-builtins-default-costs = { workspace = true }
solana-builtins-default-costs = { workspace = true, features = ["svm-internal"] }
solana-compute-budget = { workspace = true }
solana-compute-budget-interface = { workspace = true }
solana-feature-set = { workspace = true }
Expand All @@ -32,7 +32,7 @@ name = "solana_compute_budget_instruction"
bincode = { workspace = true }
criterion = { workspace = true }
rand = { workspace = true }
solana-builtins-default-costs = { workspace = true, features = ["dev-context-only-utils"] }
solana-builtins-default-costs = { workspace = true, features = ["dev-context-only-utils", "svm-internal"] }
solana-hash = { workspace = true }
solana-keypair = { workspace = true }
solana-message = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions programs/bpf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition = { workspace = true }
bincode = { workspace = true }
byteorder = { workspace = true }
libsecp256k1 = { workspace = true }
qualifier_attr = { workspace = true }
scopeguard = { workspace = true }
solana-account = { workspace = true }
solana-account-info = { workspace = true }
Expand Down Expand Up @@ -54,6 +55,7 @@ thiserror = { workspace = true }
assert_matches = { workspace = true }
criterion = { workspace = true }
rand = { workspace = true }
solana-bpf-loader-program = { path = ".", features = ["svm-internal"] }
solana-epoch-rewards = { workspace = true }
solana-epoch-schedule = { workspace = true }
solana-fee-calculator = { workspace = true }
Expand Down Expand Up @@ -87,3 +89,4 @@ shuttle-test = [
"solana-program-runtime/shuttle-test",
"solana-sbpf/shuttle-test"
]
svm-internal = []
59 changes: 45 additions & 14 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
pub mod serialization;
pub mod syscalls;

#[cfg(feature = "svm-internal")]
use qualifier_attr::qualifiers;
use {
solana_account::WritableAccount,
solana_bincode::limited_deserialize,
Expand Down Expand Up @@ -52,9 +54,12 @@ use {
syscalls::{create_program_runtime_environment_v1, morph_into_deployment_environment_v1},
};

pub const DEFAULT_LOADER_COMPUTE_UNITS: u64 = 570;
pub const DEPRECATED_LOADER_COMPUTE_UNITS: u64 = 1_140;
pub const UPGRADEABLE_LOADER_COMPUTE_UNITS: u64 = 2_370;
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
const DEFAULT_LOADER_COMPUTE_UNITS: u64 = 570;
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
const DEPRECATED_LOADER_COMPUTE_UNITS: u64 = 1_140;
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
const UPGRADEABLE_LOADER_COMPUTE_UNITS: u64 = 2_370;

thread_local! {
pub static MEMORY_POOL: RefCell<VmMemoryPool> = RefCell::new(VmMemoryPool::new());
Expand Down Expand Up @@ -106,7 +111,8 @@ pub fn load_program_from_bytes(
/// Directly deploy a program using a provided invoke context.
/// This function should only be invoked from the runtime, since it does not
/// provide any account loads or checks.
pub fn deploy_program_internal(
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
fn deploy_program_internal(
log_collector: Option<Rc<RefCell<LogCollector>>>,
program_cache_for_tx_batch: &mut ProgramCacheForTxBatch,
program_runtime_environment: ProgramRuntimeEnvironment,
Expand Down Expand Up @@ -181,7 +187,7 @@ macro_rules! deploy_program {
// This will never fail since the epoch schedule is already configured.
InstructionError::ProgramEnvironmentSetupFailure
})?;
let load_program_metrics = deploy_program_internal(
let load_program_metrics = $crate::deploy_program_internal(
$invoke_context.get_log_collector(),
$invoke_context.program_cache_for_tx_batch,
environments.program_runtime_v1.clone(),
Expand All @@ -195,6 +201,27 @@ macro_rules! deploy_program {
};
}

// This function/wrapper is added specifically for non "svm-internal" users. It enables the reducing
// of public visibility of some internal APIs.
pub fn deploy_program(
invoke_context: &mut InvokeContext,
program_id: &Pubkey,
loader_key: &Pubkey,
account_size: usize,
programdata: &[u8],
deployment_slot: Slot,
) -> Result<(), InstructionError> {
deploy_program!(
invoke_context,
program_id,
loader_key,
account_size,
programdata,
deployment_slot
);
Ok(())
}

fn write_program_data(
program_data_offset: usize,
bytes: &[u8],
Expand All @@ -220,7 +247,7 @@ fn write_program_data(
Ok(())
}

pub fn check_loader_id(id: &Pubkey) -> bool {
fn check_loader_id(id: &Pubkey) -> bool {
bpf_loader::check_id(id)
|| bpf_loader_deprecated::check_id(id)
|| bpf_loader_upgradeable::check_id(id)
Expand All @@ -242,7 +269,8 @@ pub fn calculate_heap_cost(heap_size: u32, heap_cost: u64) -> u64 {
}

/// Only used in macro, do not use directly!
pub fn create_vm<'a, 'b>(
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
fn create_vm<'a, 'b>(
program: &'a Executable<InvokeContext<'b>>,
regions: Vec<MemoryRegion>,
accounts_metadata: Vec<SerializedAccountMetadata>,
Expand Down Expand Up @@ -397,7 +425,8 @@ declare_builtin_function!(
}
);

pub fn process_instruction_inner(
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
pub(crate) fn process_instruction_inner(
invoke_context: &mut InvokeContext,
) -> Result<u64, Box<dyn std::error::Error>> {
let log_collector = invoke_context.get_log_collector();
Expand Down Expand Up @@ -1370,7 +1399,8 @@ fn common_close_account(
Ok(())
}

pub fn execute<'a, 'b: 'a>(
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
fn execute<'a, 'b: 'a>(
executable: &'a Executable<InvokeContext<'static>>,
invoke_context: &'a mut InvokeContext<'b>,
) -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -1571,13 +1601,15 @@ pub fn execute<'a, 'b: 'a>(
execute_or_deserialize_result
}

pub mod test_utils {
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
mod test_utils {
use {
super::*, solana_account::ReadableAccount, solana_program::loader_v4::LoaderV4State,
solana_program_runtime::loaded_programs::DELAY_VISIBILITY_SLOT_OFFSET,
};

pub fn load_all_invoked_programs(invoke_context: &mut InvokeContext) {
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
fn load_all_invoked_programs(invoke_context: &mut InvokeContext) {
let mut load_program_metrics = LoadProgramMetrics::default();
let program_runtime_environment = create_program_runtime_environment_v1(
invoke_context.get_feature_set(),
Expand Down Expand Up @@ -3833,15 +3865,14 @@ mod tests {
let mut file = File::open("test_elfs/out/sbpfv3_return_ok.so").expect("file open failed");
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
deploy_program!(
deploy_program(
invoke_context,
&program_id,
&bpf_loader_upgradeable::id(),
elf.len(),
&elf,
2_u64,
);
Ok(())
)
}

#[test]
Expand Down
Loading

0 comments on commit 1923f3b

Please sign in to comment.