Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change visibility of svm-internal structs and APIs #4449

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 21 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,7 +39,8 @@ 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),
}
Expand All @@ -48,6 +53,7 @@ impl BuiltinCost {
}
}

#[cfg(feature = "svm-internal")]
fn core_bpf_migration_feature(&self) -> Option<&Pubkey> {
match self {
BuiltinCost::Migrating(MigratingBuiltinCost {
Expand All @@ -58,6 +64,7 @@ impl BuiltinCost {
}
}

#[cfg(feature = "svm-internal")]
fn position(&self) -> Option<usize> {
match self {
BuiltinCost::Migrating(MigratingBuiltinCost { position, .. }) => Some(*position),
Expand Down Expand Up @@ -109,7 +116,8 @@ static_assertions::const_assert_eq!(
TOTAL_COUNT_BUILTINS
);

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 +223,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 +266,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 = []
57 changes: 35 additions & 22 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 @@ -42,19 +44,20 @@ use {
verifier::RequisiteVerifier,
vm::{ContextObject, EbpfVm},
},
solana_sdk_ids::{
bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable, loader_v4, native_loader,
},
solana_sdk_ids::{bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable, native_loader},
solana_system_interface::{instruction as system_instruction, MAX_PERMITTED_DATA_LENGTH},
solana_transaction_context::{IndexOfAccount, InstructionContext, TransactionContext},
solana_type_overrides::sync::{atomic::Ordering, Arc},
std::{cell::RefCell, mem, rc::Rc},
syscalls::{create_program_runtime_environment_v1, morph_into_deployment_environment_v1},
syscalls::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 +109,7 @@ 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(
pub fn deploy_program(
log_collector: Option<Rc<RefCell<LogCollector>>>,
program_cache_for_tx_batch: &mut ProgramCacheForTxBatch,
program_runtime_environment: ProgramRuntimeEnvironment,
Expand Down Expand Up @@ -181,7 +184,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(
$invoke_context.get_log_collector(),
$invoke_context.program_cache_for_tx_batch,
environments.program_runtime_v1.clone(),
Expand Down Expand Up @@ -220,13 +223,6 @@ fn write_program_data(
Ok(())
}

pub fn check_loader_id(id: &Pubkey) -> bool {
bpf_loader::check_id(id)
|| bpf_loader_deprecated::check_id(id)
|| bpf_loader_upgradeable::check_id(id)
|| loader_v4::check_id(id)
}

/// Only used in macro, do not use directly!
pub fn calculate_heap_cost(heap_size: u32, heap_cost: u64) -> u64 {
const KIBIBYTE: u64 = 1024;
Expand All @@ -242,7 +238,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 +394,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 +1368,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 +1570,27 @@ pub fn execute<'a, 'b: 'a>(
execute_or_deserialize_result
}

pub mod test_utils {
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
mod test_utils {
#[cfg(feature = "svm-internal")]
use {
super::*, solana_account::ReadableAccount, solana_program::loader_v4::LoaderV4State,
super::*, crate::syscalls::create_program_runtime_environment_v1,
solana_account::ReadableAccount, solana_program::loader_v4,
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(feature = "svm-internal")]
fn check_loader_id(id: &Pubkey) -> bool {
bpf_loader::check_id(id)
|| bpf_loader_deprecated::check_id(id)
|| bpf_loader_upgradeable::check_id(id)
|| loader_v4::check_id(id)
}

#[cfg(feature = "svm-internal")]
#[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
12 changes: 6 additions & 6 deletions programs/bpf_loader/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum SerializeAccount<'a> {
}

struct Serializer {
pub buffer: AlignedMemory<HOST_ALIGN>,
buffer: AlignedMemory<HOST_ALIGN>,
regions: Vec<MemoryRegion>,
vaddr: u64,
region_start: usize,
Expand All @@ -54,7 +54,7 @@ impl Serializer {
self.buffer.fill_write(num, value)
}

pub fn write<T: Pod>(&mut self, value: T) -> u64 {
fn write<T: Pod>(&mut self, value: T) -> u64 {
self.debug_assert_alignment::<T>();
let vaddr = self
.vaddr
Expand Down Expand Up @@ -249,7 +249,7 @@ pub fn serialize_parameters(
}
}

pub fn deserialize_parameters(
pub(crate) fn deserialize_parameters(
transaction_context: &TransactionContext,
instruction_context: &InstructionContext,
copy_account_data: bool,
Expand Down Expand Up @@ -358,7 +358,7 @@ fn serialize_parameters_unaligned(
Ok((mem, regions, accounts_metadata))
}

pub fn deserialize_parameters_unaligned<I: IntoIterator<Item = usize>>(
fn deserialize_parameters_unaligned<I: IntoIterator<Item = usize>>(
transaction_context: &TransactionContext,
instruction_context: &InstructionContext,
copy_account_data: bool,
Expand Down Expand Up @@ -498,7 +498,7 @@ fn serialize_parameters_aligned(
Ok((mem, regions, accounts_metadata))
}

pub fn deserialize_parameters_aligned<I: IntoIterator<Item = usize>>(
fn deserialize_parameters_aligned<I: IntoIterator<Item = usize>>(
transaction_context: &TransactionContext,
instruction_context: &InstructionContext,
copy_account_data: bool,
Expand Down Expand Up @@ -1029,7 +1029,7 @@ mod tests {

// the old bpf_loader in-program deserializer bpf_loader::id()
#[deny(unsafe_op_in_unsafe_fn)]
pub unsafe fn deserialize_unaligned<'a>(
unsafe fn deserialize_unaligned<'a>(
input: *mut u8,
) -> (&'a Pubkey, Vec<AccountInfo<'a>>, &'a [u8]) {
// this boring boilerplate struct is needed until inline const...
Expand Down
Loading
Loading