From a57758e9c9ab1aba56c6fc4bdbeb830a0907446e Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 18 Dec 2020 11:23:00 -0800 Subject: [PATCH] Add CPI support for upgradeable loader (bp #14193) (#14199) --- programs/bpf/Cargo.lock | 7 +++ programs/bpf/Cargo.toml | 1 + programs/bpf/build.rs | 1 + .../bpf/rust/invoke_and_return/Cargo.toml | 18 ++++++ .../bpf/rust/invoke_and_return/Xargo.toml | 2 + .../bpf/rust/invoke_and_return/src/lib.rs | 36 ++++++++++++ programs/bpf/tests/programs.rs | 57 +++++++++++++++++++ programs/bpf_loader/src/syscalls.rs | 39 ++++++++++--- runtime/src/message_processor.rs | 57 +++++++++++++++---- sdk/src/process_instruction.rs | 5 ++ 10 files changed, 204 insertions(+), 19 deletions(-) create mode 100644 programs/bpf/rust/invoke_and_return/Cargo.toml create mode 100644 programs/bpf/rust/invoke_and_return/Xargo.toml create mode 100644 programs/bpf/rust/invoke_and_return/src/lib.rs diff --git a/programs/bpf/Cargo.lock b/programs/bpf/Cargo.lock index 6cbad7e774..e2e4ba2967 100644 --- a/programs/bpf/Cargo.lock +++ b/programs/bpf/Cargo.lock @@ -1985,6 +1985,13 @@ dependencies = [ "solana-program", ] +[[package]] +name = "solana-bpf-rust-invoke-and-return" +version = "1.6.0" +dependencies = [ + "solana-program", +] + [[package]] name = "solana-bpf-rust-invoked" version = "1.5.0" diff --git a/programs/bpf/Cargo.toml b/programs/bpf/Cargo.toml index f266f4cebf..d4d0fa1f21 100644 --- a/programs/bpf/Cargo.toml +++ b/programs/bpf/Cargo.toml @@ -51,6 +51,7 @@ members = [ "rust/invoke", "rust/invoke_and_error", "rust/invoke_and_ok", + "rust/invoke_and_return", "rust/invoked", "rust/iter", "rust/many_args", diff --git a/programs/bpf/build.rs b/programs/bpf/build.rs index 971b21f0d3..f7fa029c80 100644 --- a/programs/bpf/build.rs +++ b/programs/bpf/build.rs @@ -72,6 +72,7 @@ fn main() { "invoke", "invoke_and_error", "invoke_and_ok", + "invoke_and_return", "invoked", "iter", "many_args", diff --git a/programs/bpf/rust/invoke_and_return/Cargo.toml b/programs/bpf/rust/invoke_and_return/Cargo.toml new file mode 100644 index 0000000000..f3d20d85cf --- /dev/null +++ b/programs/bpf/rust/invoke_and_return/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "solana-bpf-rust-invoke-and-return" +version = "1.5.0" +description = "Solana BPF test program written in Rust" +authors = ["Solana Maintainers "] +repository = "https://github.com/solana-labs/solana" +license = "Apache-2.0" +homepage = "https://solana.com/" +edition = "2018" + +[dependencies] +solana-program = { path = "../../../../sdk/program", version = "1.5.0" } + +[lib] +crate-type = ["cdylib"] + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] diff --git a/programs/bpf/rust/invoke_and_return/Xargo.toml b/programs/bpf/rust/invoke_and_return/Xargo.toml new file mode 100644 index 0000000000..1744f098ae --- /dev/null +++ b/programs/bpf/rust/invoke_and_return/Xargo.toml @@ -0,0 +1,2 @@ +[target.bpfel-unknown-unknown.dependencies.std] +features = [] \ No newline at end of file diff --git a/programs/bpf/rust/invoke_and_return/src/lib.rs b/programs/bpf/rust/invoke_and_return/src/lib.rs new file mode 100644 index 0000000000..8855bfcead --- /dev/null +++ b/programs/bpf/rust/invoke_and_return/src/lib.rs @@ -0,0 +1,36 @@ +//! @brief Invokes an instruction and returns an error, the instruction invoked +//! uses the instruction data provided and all the accounts + +use solana_program::{ + account_info::AccountInfo, bpf_loader_upgradeable, entrypoint, entrypoint::ProgramResult, + instruction::AccountMeta, instruction::Instruction, program::invoke, pubkey::Pubkey, +}; + +entrypoint!(process_instruction); +#[allow(clippy::unnecessary_wraps)] +fn process_instruction( + _program_id: &Pubkey, + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> ProgramResult { + let to_call = accounts[0].key; + let infos = accounts; + let last = if bpf_loader_upgradeable::check_id(accounts[0].owner) { + accounts.len() - 1 + } else { + accounts.len() + }; + let instruction = Instruction { + accounts: accounts[1..last] + .iter() + .map(|acc| AccountMeta { + pubkey: *acc.key, + is_signer: acc.is_signer, + is_writable: acc.is_writable, + }) + .collect(), + data: instruction_data.to_owned(), + program_id: *to_call, + }; + invoke(&instruction, &infos) +} diff --git a/programs/bpf/tests/programs.rs b/programs/bpf/tests/programs.rs index f56cc255a6..67dab91925 100644 --- a/programs/bpf/tests/programs.rs +++ b/programs/bpf/tests/programs.rs @@ -21,7 +21,9 @@ use solana_runtime::{ }; use solana_sdk::{ account::Account, + account_utils::StateMut, bpf_loader, bpf_loader_deprecated, + bpf_loader_upgradeable::UpgradeableLoaderState, client::SyncClient, clock::{DEFAULT_SLOTS_PER_EPOCH, MAX_PROCESSING_AGE}, entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS}, @@ -1557,3 +1559,58 @@ fn test_program_bpf_upgrade() { TransactionError::InstructionError(0, InstructionError::Custom(42)) ); } + +#[cfg(feature = "bpf_rust")] +#[test] +fn test_program_bpf_invoke_upgradeable() { + solana_logger::setup(); + + let GenesisConfigInfo { + genesis_config, + mint_keypair, + .. + } = create_genesis_config(50); + let mut bank = Bank::new(&genesis_config); + let (name, id, entrypoint) = solana_bpf_loader_program!(); + bank.add_builtin(&name, id, entrypoint); + let (name, id, entrypoint) = solana_bpf_loader_upgradeable_program!(); + bank.add_builtin(&name, id, entrypoint); + let bank_client = BankClient::new(bank); + let invoke_and_return = load_bpf_program( + &bank_client, + &bpf_loader::id(), + &mint_keypair, + "solana_bpf_rust_invoke_and_return", + ); + + // deploy upgrade program + let (program_id, _) = + load_upgradeable_bpf_program(&bank_client, &mint_keypair, "solana_bpf_rust_upgradeable"); + + let data = bank_client.get_account(&program_id).unwrap().unwrap(); + let programdata_address = if let UpgradeableLoaderState::Program { + programdata_address, + } = data.state().unwrap() + { + programdata_address + } else { + panic!("Not a program"); + }; + + // call invoker program to invoke the upgradeable program + let instruction = Instruction::new( + invoke_and_return, + &[0], + vec![ + AccountMeta::new(program_id, false), + AccountMeta::new(clock::id(), false), + AccountMeta::new(fees::id(), false), + AccountMeta::new(programdata_address, false), + ], + ); + let result = bank_client.send_and_confirm_instruction(&mint_keypair, instruction); + assert_eq!( + result.unwrap_err().unwrap(), + TransactionError::InstructionError(0, InstructionError::Custom(42)) + ); +} diff --git a/programs/bpf_loader/src/syscalls.rs b/programs/bpf_loader/src/syscalls.rs index ee19cc56e6..f034841828 100644 --- a/programs/bpf_loader/src/syscalls.rs +++ b/programs/bpf_loader/src/syscalls.rs @@ -12,7 +12,9 @@ use solana_runtime::message_processor::MessageProcessor; use solana_sdk::{ account::Account, account_info::AccountInfo, + account_utils::StateMut, bpf_loader_deprecated, + bpf_loader_upgradeable::{self, UpgradeableLoaderState}, entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS}, feature_set::{ pubkey_log_syscall_enabled, ristretto_mul_syscall_enabled, sha256_syscall_enabled, @@ -1367,7 +1369,7 @@ fn call<'a>( .get_callers_keyed_accounts() .iter() .collect::>(); - let (message, callee_program_id, callee_program_id_index) = + let (message, callee_program_id) = MessageProcessor::create_message(&instruction, &keyed_account_refs, &signers) .map_err(SyscallError::InstructionError)?; let (accounts, account_refs) = syscall.translate_accounts( @@ -1380,17 +1382,40 @@ fn call<'a>( // Process instruction invoke_context.record_instruction(&instruction); + let program_account = - (**accounts - .get(callee_program_id_index) + invoke_context + .get_account(&callee_program_id) .ok_or(SyscallError::InstructionError( InstructionError::MissingAccount, - ))?) - .clone(); - if !program_account.borrow().executable { + ))?; + if !program_account.executable { return Err(SyscallError::InstructionError(InstructionError::AccountNotExecutable).into()); } - let executable_accounts = vec![(callee_program_id, program_account)]; + let programdata_executable = if program_account.owner == bpf_loader_upgradeable::id() { + if let UpgradeableLoaderState::Program { + programdata_address, + } = program_account + .state() + .map_err(SyscallError::InstructionError)? + { + if let Some(account) = invoke_context.get_account(&programdata_address) { + Some((programdata_address, RefCell::new(account))) + } else { + return Err( + SyscallError::InstructionError(InstructionError::MissingAccount).into(), + ); + } + } else { + return Err(SyscallError::InstructionError(InstructionError::MissingAccount).into()); + } + } else { + None + }; + let mut executable_accounts = vec![(callee_program_id, RefCell::new(program_account))]; + if let Some(programdata) = programdata_executable { + executable_accounts.push(programdata); + } #[allow(clippy::deref_addrof)] match MessageProcessor::process_cross_program_instruction( diff --git a/runtime/src/message_processor.rs b/runtime/src/message_processor.rs index ea9eb7a1fe..1f0808c416 100644 --- a/runtime/src/message_processor.rs +++ b/runtime/src/message_processor.rs @@ -6,6 +6,8 @@ use log::*; use serde::{Deserialize, Serialize}; use solana_sdk::{ account::Account, + account_utils::StateMut, + bpf_loader_upgradeable::{self, UpgradeableLoaderState}, clock::Epoch, feature_set::{instructions_sysvar_enabled, FeatureSet}, instruction::{CompiledInstruction, Instruction, InstructionError}, @@ -310,6 +312,21 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> { fn is_feature_active(&self, feature_id: &Pubkey) -> bool { self.feature_set.is_active(feature_id) } + fn get_account(&self, pubkey: &Pubkey) -> Option { + self.pre_accounts.iter().find_map(|pre| { + if pre.key == *pubkey { + Some(Account { + lamports: pre.lamports, + data: pre.data.clone(), + owner: pre.owner, + executable: pre.is_executable, + rent_epoch: pre.rent_epoch, + }) + } else { + None + } + }) + } } pub struct ThisLogger { log_collector: Option>, @@ -541,7 +558,7 @@ impl MessageProcessor { instruction: &Instruction, keyed_accounts: &[&KeyedAccount], signers: &[Pubkey], - ) -> Result<(Message, Pubkey, usize), InstructionError> { + ) -> Result<(Message, Pubkey), InstructionError> { // Check for privilege escalation for account in instruction.accounts.iter() { let keyed_account = keyed_accounts @@ -584,10 +601,7 @@ impl MessageProcessor { let id = *message .program_id(0) .ok_or(InstructionError::MissingAccount)?; - let index = message - .program_index(0) - .ok_or(InstructionError::MissingAccount)?; - Ok((message, id, index)) + Ok((message, id)) } /// Entrypoint for a cross-program invocation from a native program @@ -605,7 +619,7 @@ impl MessageProcessor { .iter() .map(|seeds| Pubkey::create_program_address(&seeds, caller_program_id)) .collect::, solana_sdk::pubkey::PubkeyError>>()?; - let (message, callee_program_id, callee_program_id_index) = + let (message, callee_program_id) = Self::create_message(&instruction, &keyed_accounts, &signers)?; let mut accounts = vec![]; let mut account_refs = vec![]; @@ -623,14 +637,33 @@ impl MessageProcessor { // Process instruction invoke_context.record_instruction(&instruction); - let program_account = (**accounts - .get(callee_program_id_index) - .ok_or(InstructionError::MissingAccount)?) - .clone(); - if !program_account.borrow().executable { + + let program_account = invoke_context + .get_account(&callee_program_id) + .ok_or(InstructionError::MissingAccount)?; + if !program_account.executable { return Err(InstructionError::AccountNotExecutable); } - let executable_accounts = vec![(callee_program_id, program_account)]; + let programdata_executable = if program_account.owner == bpf_loader_upgradeable::id() { + if let UpgradeableLoaderState::Program { + programdata_address, + } = program_account.state()? + { + if let Some(account) = invoke_context.get_account(&programdata_address) { + Some((programdata_address, RefCell::new(account))) + } else { + return Err(InstructionError::MissingAccount); + } + } else { + return Err(InstructionError::MissingAccount); + } + } else { + None + }; + let mut executable_accounts = vec![(callee_program_id, RefCell::new(program_account))]; + if let Some(programdata) = programdata_executable { + executable_accounts.push(programdata); + } MessageProcessor::process_cross_program_instruction( &message, diff --git a/sdk/src/process_instruction.rs b/sdk/src/process_instruction.rs index c5e0f93749..5347019bed 100644 --- a/sdk/src/process_instruction.rs +++ b/sdk/src/process_instruction.rs @@ -61,6 +61,8 @@ pub trait InvokeContext { fn record_instruction(&self, instruction: &Instruction); /// Get the bank's active feature set fn is_feature_active(&self, feature_id: &Pubkey) -> bool; + /// Get an account from a pre-account + fn get_account(&self, pubkey: &Pubkey) -> Option; } #[derive(Clone, Copy, Debug, AbiExample)] @@ -340,4 +342,7 @@ impl InvokeContext for MockInvokeContext { fn is_feature_active(&self, _feature_id: &Pubkey) -> bool { true } + fn get_account(&self, _pubkey: &Pubkey) -> Option { + None + } }