Skip to content

Commit 766cd68

Browse files
authored
Change visibility of svm-internal structs and APIs (anza-xyz#4449)
1 parent eb41682 commit 766cd68

File tree

15 files changed

+128
-58
lines changed

15 files changed

+128
-58
lines changed

Cargo.lock

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builtins-default-costs/Cargo.toml

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ edition = { workspace = true }
1313
ahash = { workspace = true }
1414
lazy_static = { workspace = true }
1515
log = { workspace = true }
16+
qualifier_attr = { workspace = true }
1617
solana-address-lookup-table-program = { workspace = true }
17-
solana-bpf-loader-program = { workspace = true }
18-
solana-compute-budget-program = { workspace = true }
18+
solana-bpf-loader-program = { workspace = true, features = ["svm-internal"] }
19+
solana-compute-budget-program = { workspace = true, features = ["svm-internal"] }
1920
solana-config-program = { workspace = true }
2021
solana-feature-set = { workspace = true }
2122
solana-frozen-abi = { workspace = true, optional = true, features = [
2223
"frozen-abi",
2324
] }
24-
solana-loader-v4-program = { workspace = true }
25+
solana-loader-v4-program = { workspace = true, features = ["svm-internal"] }
2526
solana-pubkey = { workspace = true }
2627
solana-sdk-ids = { workspace = true }
2728
solana-stake-program = { workspace = true }
@@ -35,6 +36,7 @@ name = "solana_builtins_default_costs"
3536

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

4042
[package.metadata.docs.rs]
@@ -46,6 +48,7 @@ frozen-abi = [
4648
"solana-vote-program/frozen-abi",
4749
]
4850
dev-context-only-utils = []
51+
svm-internal = []
4952

5053
[lints]
5154
workspace = true

builtins-default-costs/src/lib.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
22
#![allow(clippy::arithmetic_side_effects)]
3+
#[cfg(feature = "svm-internal")]
4+
use qualifier_attr::qualifiers;
35
use {
46
ahash::AHashMap,
57
lazy_static::lazy_static,
@@ -13,7 +15,8 @@ use {
1315
};
1416

1517
#[derive(Clone)]
16-
pub struct MigratingBuiltinCost {
18+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
19+
struct MigratingBuiltinCost {
1720
native_cost: u64,
1821
core_bpf_migration_feature: Pubkey,
1922
// encoding positional information explicitly for migration feature item,
@@ -24,7 +27,8 @@ pub struct MigratingBuiltinCost {
2427
}
2528

2629
#[derive(Clone)]
27-
pub struct NotMigratingBuiltinCost {
30+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
31+
struct NotMigratingBuiltinCost {
2832
native_cost: u64,
2933
}
3034

@@ -35,7 +39,8 @@ pub struct NotMigratingBuiltinCost {
3539
/// When migration completed, eg the feature gate is enabled everywhere, please
3640
/// remove that builtin entry from MIGRATING_BUILTINS_COSTS.
3741
#[derive(Clone)]
38-
pub enum BuiltinCost {
42+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
43+
enum BuiltinCost {
3944
Migrating(MigratingBuiltinCost),
4045
NotMigrating(NotMigratingBuiltinCost),
4146
}
@@ -48,6 +53,7 @@ impl BuiltinCost {
4853
}
4954
}
5055

56+
#[cfg(feature = "svm-internal")]
5157
fn core_bpf_migration_feature(&self) -> Option<&Pubkey> {
5258
match self {
5359
BuiltinCost::Migrating(MigratingBuiltinCost {
@@ -58,6 +64,7 @@ impl BuiltinCost {
5864
}
5965
}
6066

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

112-
pub const MIGRATING_BUILTINS_COSTS: &[(Pubkey, BuiltinCost)] = &[
119+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
120+
const MIGRATING_BUILTINS_COSTS: &[(Pubkey, BuiltinCost)] = &[
113121
(
114122
stake::id(),
115123
BuiltinCost::Migrating(MigratingBuiltinCost {
@@ -215,13 +223,17 @@ pub fn get_builtin_instruction_cost<'a>(
215223
.map(|builtin_cost| builtin_cost.native_cost())
216224
}
217225

218-
pub enum BuiltinMigrationFeatureIndex {
226+
#[cfg(feature = "svm-internal")]
227+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
228+
enum BuiltinMigrationFeatureIndex {
219229
NotBuiltin,
220230
BuiltinNoMigrationFeature,
221231
BuiltinWithMigrationFeature(usize),
222232
}
223233

224-
pub fn get_builtin_migration_feature_index(program_id: &Pubkey) -> BuiltinMigrationFeatureIndex {
234+
#[cfg(feature = "svm-internal")]
235+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
236+
fn get_builtin_migration_feature_index(program_id: &Pubkey) -> BuiltinMigrationFeatureIndex {
225237
BUILTIN_INSTRUCTION_COSTS.get(program_id).map_or(
226238
BuiltinMigrationFeatureIndex::NotBuiltin,
227239
|builtin_cost| {
@@ -254,7 +266,9 @@ const _: () = validate_position(MIGRATING_BUILTINS_COSTS);
254266

255267
/// Helper function to return ref of migration feature Pubkey at position `index`
256268
/// from MIGRATING_BUILTINS_COSTS
257-
pub fn get_migration_feature_id(index: usize) -> &'static Pubkey {
269+
#[cfg(feature = "svm-internal")]
270+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
271+
pub(crate) fn get_migration_feature_id(index: usize) -> &'static Pubkey {
258272
MIGRATING_BUILTINS_COSTS
259273
.get(index)
260274
.expect("valid index of MIGRATING_BUILTINS_COSTS")

compute-budget-instruction/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ edition = { workspace = true }
1212
[dependencies]
1313
log = { workspace = true }
1414
solana-borsh = { workspace = true }
15-
solana-builtins-default-costs = { workspace = true }
15+
solana-builtins-default-costs = { workspace = true, features = ["svm-internal"] }
1616
solana-compute-budget = { workspace = true }
1717
solana-compute-budget-interface = { workspace = true }
1818
solana-feature-set = { workspace = true }
@@ -32,7 +32,7 @@ name = "solana_compute_budget_instruction"
3232
bincode = { workspace = true }
3333
criterion = { workspace = true }
3434
rand = { workspace = true }
35-
solana-builtins-default-costs = { workspace = true, features = ["dev-context-only-utils"] }
35+
solana-builtins-default-costs = { workspace = true, features = ["dev-context-only-utils", "svm-internal"] }
3636
solana-hash = { workspace = true }
3737
solana-keypair = { workspace = true }
3838
solana-message = { workspace = true }

programs/bpf_loader/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ edition = { workspace = true }
1313
bincode = { workspace = true }
1414
byteorder = { workspace = true }
1515
libsecp256k1 = { workspace = true }
16+
qualifier_attr = { workspace = true }
1617
scopeguard = { workspace = true }
1718
solana-account = { workspace = true }
1819
solana-account-info = { workspace = true }
@@ -54,6 +55,7 @@ thiserror = { workspace = true }
5455
assert_matches = { workspace = true }
5556
criterion = { workspace = true }
5657
rand = { workspace = true }
58+
solana-bpf-loader-program = { path = ".", features = ["svm-internal"] }
5759
solana-epoch-rewards = { workspace = true }
5860
solana-epoch-schedule = { workspace = true }
5961
solana-fee-calculator = { workspace = true }
@@ -87,3 +89,4 @@ shuttle-test = [
8789
"solana-program-runtime/shuttle-test",
8890
"solana-sbpf/shuttle-test"
8991
]
92+
svm-internal = []

programs/bpf_loader/src/lib.rs

+35-22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
pub mod serialization;
55
pub mod syscalls;
66

7+
#[cfg(feature = "svm-internal")]
8+
use qualifier_attr::qualifiers;
79
use {
810
solana_account::WritableAccount,
911
solana_bincode::limited_deserialize,
@@ -42,19 +44,20 @@ use {
4244
verifier::RequisiteVerifier,
4345
vm::{ContextObject, EbpfVm},
4446
},
45-
solana_sdk_ids::{
46-
bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable, loader_v4, native_loader,
47-
},
47+
solana_sdk_ids::{bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable, native_loader},
4848
solana_system_interface::{instruction as system_instruction, MAX_PERMITTED_DATA_LENGTH},
4949
solana_transaction_context::{IndexOfAccount, InstructionContext, TransactionContext},
5050
solana_type_overrides::sync::{atomic::Ordering, Arc},
5151
std::{cell::RefCell, mem, rc::Rc},
52-
syscalls::{create_program_runtime_environment_v1, morph_into_deployment_environment_v1},
52+
syscalls::morph_into_deployment_environment_v1,
5353
};
5454

55-
pub const DEFAULT_LOADER_COMPUTE_UNITS: u64 = 570;
56-
pub const DEPRECATED_LOADER_COMPUTE_UNITS: u64 = 1_140;
57-
pub const UPGRADEABLE_LOADER_COMPUTE_UNITS: u64 = 2_370;
55+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
56+
const DEFAULT_LOADER_COMPUTE_UNITS: u64 = 570;
57+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
58+
const DEPRECATED_LOADER_COMPUTE_UNITS: u64 = 1_140;
59+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
60+
const UPGRADEABLE_LOADER_COMPUTE_UNITS: u64 = 2_370;
5861

5962
thread_local! {
6063
pub static MEMORY_POOL: RefCell<VmMemoryPool> = RefCell::new(VmMemoryPool::new());
@@ -106,7 +109,7 @@ pub fn load_program_from_bytes(
106109
/// Directly deploy a program using a provided invoke context.
107110
/// This function should only be invoked from the runtime, since it does not
108111
/// provide any account loads or checks.
109-
pub fn deploy_program_internal(
112+
pub fn deploy_program(
110113
log_collector: Option<Rc<RefCell<LogCollector>>>,
111114
program_cache_for_tx_batch: &mut ProgramCacheForTxBatch,
112115
program_runtime_environment: ProgramRuntimeEnvironment,
@@ -181,7 +184,7 @@ macro_rules! deploy_program {
181184
// This will never fail since the epoch schedule is already configured.
182185
InstructionError::ProgramEnvironmentSetupFailure
183186
})?;
184-
let load_program_metrics = deploy_program_internal(
187+
let load_program_metrics = $crate::deploy_program(
185188
$invoke_context.get_log_collector(),
186189
$invoke_context.program_cache_for_tx_batch,
187190
environments.program_runtime_v1.clone(),
@@ -220,13 +223,6 @@ fn write_program_data(
220223
Ok(())
221224
}
222225

223-
pub fn check_loader_id(id: &Pubkey) -> bool {
224-
bpf_loader::check_id(id)
225-
|| bpf_loader_deprecated::check_id(id)
226-
|| bpf_loader_upgradeable::check_id(id)
227-
|| loader_v4::check_id(id)
228-
}
229-
230226
/// Only used in macro, do not use directly!
231227
pub fn calculate_heap_cost(heap_size: u32, heap_cost: u64) -> u64 {
232228
const KIBIBYTE: u64 = 1024;
@@ -242,7 +238,8 @@ pub fn calculate_heap_cost(heap_size: u32, heap_cost: u64) -> u64 {
242238
}
243239

244240
/// Only used in macro, do not use directly!
245-
pub fn create_vm<'a, 'b>(
241+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
242+
fn create_vm<'a, 'b>(
246243
program: &'a Executable<InvokeContext<'b>>,
247244
regions: Vec<MemoryRegion>,
248245
accounts_metadata: Vec<SerializedAccountMetadata>,
@@ -397,7 +394,8 @@ declare_builtin_function!(
397394
}
398395
);
399396

400-
pub fn process_instruction_inner(
397+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
398+
pub(crate) fn process_instruction_inner(
401399
invoke_context: &mut InvokeContext,
402400
) -> Result<u64, Box<dyn std::error::Error>> {
403401
let log_collector = invoke_context.get_log_collector();
@@ -1370,7 +1368,8 @@ fn common_close_account(
13701368
Ok(())
13711369
}
13721370

1373-
pub fn execute<'a, 'b: 'a>(
1371+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
1372+
fn execute<'a, 'b: 'a>(
13741373
executable: &'a Executable<InvokeContext<'static>>,
13751374
invoke_context: &'a mut InvokeContext<'b>,
13761375
) -> Result<(), Box<dyn std::error::Error>> {
@@ -1571,13 +1570,27 @@ pub fn execute<'a, 'b: 'a>(
15711570
execute_or_deserialize_result
15721571
}
15731572

1574-
pub mod test_utils {
1573+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
1574+
mod test_utils {
1575+
#[cfg(feature = "svm-internal")]
15751576
use {
1576-
super::*, solana_account::ReadableAccount, solana_program::loader_v4::LoaderV4State,
1577+
super::*, crate::syscalls::create_program_runtime_environment_v1,
1578+
solana_account::ReadableAccount, solana_program::loader_v4,
1579+
solana_program::loader_v4::LoaderV4State,
15771580
solana_program_runtime::loaded_programs::DELAY_VISIBILITY_SLOT_OFFSET,
15781581
};
15791582

1580-
pub fn load_all_invoked_programs(invoke_context: &mut InvokeContext) {
1583+
#[cfg(feature = "svm-internal")]
1584+
fn check_loader_id(id: &Pubkey) -> bool {
1585+
bpf_loader::check_id(id)
1586+
|| bpf_loader_deprecated::check_id(id)
1587+
|| bpf_loader_upgradeable::check_id(id)
1588+
|| loader_v4::check_id(id)
1589+
}
1590+
1591+
#[cfg(feature = "svm-internal")]
1592+
#[cfg_attr(feature = "svm-internal", qualifiers(pub))]
1593+
fn load_all_invoked_programs(invoke_context: &mut InvokeContext) {
15811594
let mut load_program_metrics = LoadProgramMetrics::default();
15821595
let program_runtime_environment = create_program_runtime_environment_v1(
15831596
invoke_context.get_feature_set(),

programs/bpf_loader/src/serialization.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ enum SerializeAccount<'a> {
3030
}
3131

3232
struct Serializer {
33-
pub buffer: AlignedMemory<HOST_ALIGN>,
33+
buffer: AlignedMemory<HOST_ALIGN>,
3434
regions: Vec<MemoryRegion>,
3535
vaddr: u64,
3636
region_start: usize,
@@ -54,7 +54,7 @@ impl Serializer {
5454
self.buffer.fill_write(num, value)
5555
}
5656

57-
pub fn write<T: Pod>(&mut self, value: T) -> u64 {
57+
fn write<T: Pod>(&mut self, value: T) -> u64 {
5858
self.debug_assert_alignment::<T>();
5959
let vaddr = self
6060
.vaddr
@@ -249,7 +249,7 @@ pub fn serialize_parameters(
249249
}
250250
}
251251

252-
pub fn deserialize_parameters(
252+
pub(crate) fn deserialize_parameters(
253253
transaction_context: &TransactionContext,
254254
instruction_context: &InstructionContext,
255255
copy_account_data: bool,
@@ -358,7 +358,7 @@ fn serialize_parameters_unaligned(
358358
Ok((mem, regions, accounts_metadata))
359359
}
360360

361-
pub fn deserialize_parameters_unaligned<I: IntoIterator<Item = usize>>(
361+
fn deserialize_parameters_unaligned<I: IntoIterator<Item = usize>>(
362362
transaction_context: &TransactionContext,
363363
instruction_context: &InstructionContext,
364364
copy_account_data: bool,
@@ -498,7 +498,7 @@ fn serialize_parameters_aligned(
498498
Ok((mem, regions, accounts_metadata))
499499
}
500500

501-
pub fn deserialize_parameters_aligned<I: IntoIterator<Item = usize>>(
501+
fn deserialize_parameters_aligned<I: IntoIterator<Item = usize>>(
502502
transaction_context: &TransactionContext,
503503
instruction_context: &InstructionContext,
504504
copy_account_data: bool,
@@ -1029,7 +1029,7 @@ mod tests {
10291029

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

0 commit comments

Comments
 (0)