From b3b946f22ec7949436d316ed820b989c626baeca Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Wed, 23 Feb 2022 19:44:43 -0700 Subject: [PATCH] followup safety checks for #23295 --- runtime/src/builtins.rs | 121 ++++++++++++++++++++++++---------------- sdk/src/lib.rs | 8 +++ 2 files changed, 80 insertions(+), 49 deletions(-) diff --git a/runtime/src/builtins.rs b/runtime/src/builtins.rs index 81ef383b342c21..a7229cb288abc2 100644 --- a/runtime/src/builtins.rs +++ b/runtime/src/builtins.rs @@ -1,5 +1,7 @@ #[cfg(RUSTC_WITH_SPECIALIZATION)] use solana_frozen_abi::abi_example::AbiExample; +#[allow(deprecated)] +use solana_sdk::AutoTraitBreakSendSync; use { crate::system_instruction_processor, solana_program_runtime::{ @@ -108,7 +110,7 @@ pub enum BuiltinAction { /// State transition enum used for adding and removing builtin programs through /// feature activations. #[derive(Debug, Clone, AbiExample)] -pub enum BuiltinFeatureTransition { +enum InnerBuiltinFeatureTransition { /// Add a builtin program if a feature is activated. Add { builtin: Builtin, @@ -123,6 +125,13 @@ pub enum BuiltinFeatureTransition { }, } +#[allow(deprecated)] +#[cfg(debug_assertions)] +impl AutoTraitBreakSendSync for InnerBuiltinFeatureTransition {} + +#[derive(Clone, Debug)] +pub struct BuiltinFeatureTransition(InnerBuiltinFeatureTransition); + // https://github.com/solana-labs/solana/pull/23233 added `BuiltinFeatureTransition` // to `Bank` which triggers https://github.com/rust-lang/rust/issues/92987 while // attempting to resolve `Sync` on `BankRc` in `AccountsBackgroundService::new` ala, @@ -142,14 +151,38 @@ unsafe impl Send for BuiltinFeatureTransition {} unsafe impl Sync for BuiltinFeatureTransition {} impl BuiltinFeatureTransition { + pub fn new_add( + name: &str, + id: Pubkey, + process_instruction_with_context: ProcessInstructionWithContext, + feature_id: Pubkey, + ) -> Self { + Self(InnerBuiltinFeatureTransition::Add { + builtin: Builtin::new(name, id, process_instruction_with_context), + feature_id, + }) + } + pub fn new_remove_or_retain( + name: &str, + id: Pubkey, + process_instruction_with_context: ProcessInstructionWithContext, + addition_feature_id: Pubkey, + removal_feature_id: Pubkey, + ) -> Self { + Self(InnerBuiltinFeatureTransition::RemoveOrRetain { + previously_added_builtin: Builtin::new(name, id, process_instruction_with_context), + addition_feature_id, + removal_feature_id, + }) + } pub fn to_action( &self, should_apply_action_for_feature: &impl Fn(&Pubkey) -> bool, ) -> Option { - match self { - Self::Add { + match &self.0 { + InnerBuiltinFeatureTransition::Add { builtin, - feature_id, + ref feature_id, } => { if should_apply_action_for_feature(feature_id) { Some(BuiltinAction::Add(builtin.clone())) @@ -157,10 +190,10 @@ impl BuiltinFeatureTransition { None } } - Self::RemoveOrRetain { + InnerBuiltinFeatureTransition::RemoveOrRetain { previously_added_builtin, - addition_feature_id, - removal_feature_id, + ref addition_feature_id, + ref removal_feature_id, } => { if should_apply_action_for_feature(removal_feature_id) { Some(BuiltinAction::Remove(previously_added_builtin.id)) @@ -213,48 +246,38 @@ fn dummy_process_instruction( /// Dynamic feature transitions for builtin programs fn builtin_feature_transitions() -> Vec { vec![ - BuiltinFeatureTransition::Add { - builtin: Builtin::new( - "compute_budget_program", - solana_sdk::compute_budget::id(), - solana_compute_budget_program::process_instruction, - ), - feature_id: feature_set::add_compute_budget_program::id(), - }, - BuiltinFeatureTransition::RemoveOrRetain { - previously_added_builtin: Builtin::new( - "secp256k1_program", - solana_sdk::secp256k1_program::id(), - dummy_process_instruction, - ), - addition_feature_id: feature_set::secp256k1_program_enabled::id(), - removal_feature_id: feature_set::prevent_calling_precompiles_as_programs::id(), - }, - BuiltinFeatureTransition::RemoveOrRetain { - previously_added_builtin: Builtin::new( - "ed25519_program", - solana_sdk::ed25519_program::id(), - dummy_process_instruction, - ), - addition_feature_id: feature_set::ed25519_program_enabled::id(), - removal_feature_id: feature_set::prevent_calling_precompiles_as_programs::id(), - }, - BuiltinFeatureTransition::Add { - builtin: Builtin::new( - "address_lookup_table_program", - solana_address_lookup_table_program::id(), - solana_address_lookup_table_program::processor::process_instruction, - ), - feature_id: feature_set::versioned_tx_message_enabled::id(), - }, - BuiltinFeatureTransition::Add { - builtin: Builtin::new( - "zk_token_proof_program", - solana_zk_token_sdk::zk_token_proof_program::id(), - with_program_logging!(solana_zk_token_proof_program::process_instruction), - ), - feature_id: feature_set::zk_token_sdk_enabled::id(), - }, + BuiltinFeatureTransition::new_add( + "compute_budget_program", + solana_sdk::compute_budget::id(), + solana_compute_budget_program::process_instruction, + feature_set::add_compute_budget_program::id(), + ), + BuiltinFeatureTransition::new_remove_or_retain( + "secp256k1_program", + solana_sdk::secp256k1_program::id(), + dummy_process_instruction, + feature_set::secp256k1_program_enabled::id(), + feature_set::prevent_calling_precompiles_as_programs::id(), + ), + BuiltinFeatureTransition::new_remove_or_retain( + "ed25519_program", + solana_sdk::ed25519_program::id(), + dummy_process_instruction, + feature_set::ed25519_program_enabled::id(), + feature_set::prevent_calling_precompiles_as_programs::id(), + ), + BuiltinFeatureTransition::new_add( + "address_lookup_table_program", + solana_address_lookup_table_program::id(), + solana_address_lookup_table_program::processor::process_instruction, + feature_set::versioned_tx_message_enabled::id(), + ), + BuiltinFeatureTransition::new_add( + "zk_token_proof_program", + solana_zk_token_sdk::zk_token_proof_program::id(), + with_program_logging!(solana_zk_token_proof_program::process_instruction), + feature_set::zk_token_sdk_enabled::id(), + ), ] } diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index bc3c65dcbd289a..019cd40ed149d6 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -97,6 +97,14 @@ pub use solana_sdk_macro::pubkeys; #[rustversion::since(1.46.0)] pub use solana_sdk_macro::respan; +#[deprecated( + since = "1.9.0", + note = "use only to break https://github.com/rust-lang/rust/issues/92987. remove when we move to Rust 1.60.0" +)] +#[doc(hidden)] +#[cfg(debug_assertions)] +pub trait AutoTraitBreakSendSync: Send + Sync {} + // Unused `solana_sdk::program_stubs!()` macro retained for source backwards compatibility with older programs #[macro_export] #[deprecated(