Add CPI support for upgradeable loader (#14193)
This commit is contained in:
7
programs/bpf/Cargo.lock
generated
7
programs/bpf/Cargo.lock
generated
@ -1985,6 +1985,13 @@ dependencies = [
|
|||||||
"solana-program",
|
"solana-program",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "solana-bpf-rust-invoke-and-return"
|
||||||
|
version = "1.6.0"
|
||||||
|
dependencies = [
|
||||||
|
"solana-program",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "solana-bpf-rust-invoked"
|
name = "solana-bpf-rust-invoked"
|
||||||
version = "1.6.0"
|
version = "1.6.0"
|
||||||
|
@ -51,6 +51,7 @@ members = [
|
|||||||
"rust/invoke",
|
"rust/invoke",
|
||||||
"rust/invoke_and_error",
|
"rust/invoke_and_error",
|
||||||
"rust/invoke_and_ok",
|
"rust/invoke_and_ok",
|
||||||
|
"rust/invoke_and_return",
|
||||||
"rust/invoked",
|
"rust/invoked",
|
||||||
"rust/iter",
|
"rust/iter",
|
||||||
"rust/many_args",
|
"rust/many_args",
|
||||||
|
@ -72,6 +72,7 @@ fn main() {
|
|||||||
"invoke",
|
"invoke",
|
||||||
"invoke_and_error",
|
"invoke_and_error",
|
||||||
"invoke_and_ok",
|
"invoke_and_ok",
|
||||||
|
"invoke_and_return",
|
||||||
"invoked",
|
"invoked",
|
||||||
"iter",
|
"iter",
|
||||||
"many_args",
|
"many_args",
|
||||||
|
18
programs/bpf/rust/invoke_and_return/Cargo.toml
Normal file
18
programs/bpf/rust/invoke_and_return/Cargo.toml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
[package]
|
||||||
|
name = "solana-bpf-rust-invoke-and-return"
|
||||||
|
version = "1.6.0"
|
||||||
|
description = "Solana BPF test program written in Rust"
|
||||||
|
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
|
||||||
|
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.6.0" }
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
crate-type = ["cdylib"]
|
||||||
|
|
||||||
|
[package.metadata.docs.rs]
|
||||||
|
targets = ["x86_64-unknown-linux-gnu"]
|
2
programs/bpf/rust/invoke_and_return/Xargo.toml
Normal file
2
programs/bpf/rust/invoke_and_return/Xargo.toml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[target.bpfel-unknown-unknown.dependencies.std]
|
||||||
|
features = []
|
36
programs/bpf/rust/invoke_and_return/src/lib.rs
Normal file
36
programs/bpf/rust/invoke_and_return/src/lib.rs
Normal file
@ -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)
|
||||||
|
}
|
@ -21,7 +21,9 @@ use solana_runtime::{
|
|||||||
};
|
};
|
||||||
use solana_sdk::{
|
use solana_sdk::{
|
||||||
account::Account,
|
account::Account,
|
||||||
|
account_utils::StateMut,
|
||||||
bpf_loader, bpf_loader_deprecated,
|
bpf_loader, bpf_loader_deprecated,
|
||||||
|
bpf_loader_upgradeable::UpgradeableLoaderState,
|
||||||
client::SyncClient,
|
client::SyncClient,
|
||||||
clock::{DEFAULT_SLOTS_PER_EPOCH, MAX_PROCESSING_AGE},
|
clock::{DEFAULT_SLOTS_PER_EPOCH, MAX_PROCESSING_AGE},
|
||||||
entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS},
|
entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS},
|
||||||
@ -1557,3 +1559,58 @@ fn test_program_bpf_upgrade() {
|
|||||||
TransactionError::InstructionError(0, InstructionError::Custom(42))
|
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))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
@ -12,7 +12,9 @@ use solana_runtime::message_processor::MessageProcessor;
|
|||||||
use solana_sdk::{
|
use solana_sdk::{
|
||||||
account::Account,
|
account::Account,
|
||||||
account_info::AccountInfo,
|
account_info::AccountInfo,
|
||||||
|
account_utils::StateMut,
|
||||||
bpf_loader_deprecated,
|
bpf_loader_deprecated,
|
||||||
|
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
|
||||||
entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS},
|
entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS},
|
||||||
feature_set::{
|
feature_set::{
|
||||||
pubkey_log_syscall_enabled, ristretto_mul_syscall_enabled, sha256_syscall_enabled,
|
pubkey_log_syscall_enabled, ristretto_mul_syscall_enabled, sha256_syscall_enabled,
|
||||||
@ -1367,7 +1369,7 @@ fn call<'a>(
|
|||||||
.get_callers_keyed_accounts()
|
.get_callers_keyed_accounts()
|
||||||
.iter()
|
.iter()
|
||||||
.collect::<Vec<&KeyedAccount>>();
|
.collect::<Vec<&KeyedAccount>>();
|
||||||
let (message, callee_program_id, callee_program_id_index) =
|
let (message, callee_program_id) =
|
||||||
MessageProcessor::create_message(&instruction, &keyed_account_refs, &signers)
|
MessageProcessor::create_message(&instruction, &keyed_account_refs, &signers)
|
||||||
.map_err(SyscallError::InstructionError)?;
|
.map_err(SyscallError::InstructionError)?;
|
||||||
let (accounts, account_refs) = syscall.translate_accounts(
|
let (accounts, account_refs) = syscall.translate_accounts(
|
||||||
@ -1380,17 +1382,40 @@ fn call<'a>(
|
|||||||
// Process instruction
|
// Process instruction
|
||||||
|
|
||||||
invoke_context.record_instruction(&instruction);
|
invoke_context.record_instruction(&instruction);
|
||||||
|
|
||||||
let program_account =
|
let program_account =
|
||||||
(**accounts
|
invoke_context
|
||||||
.get(callee_program_id_index)
|
.get_account(&callee_program_id)
|
||||||
.ok_or(SyscallError::InstructionError(
|
.ok_or(SyscallError::InstructionError(
|
||||||
InstructionError::MissingAccount,
|
InstructionError::MissingAccount,
|
||||||
))?)
|
))?;
|
||||||
.clone();
|
if !program_account.executable {
|
||||||
if !program_account.borrow().executable {
|
|
||||||
return Err(SyscallError::InstructionError(InstructionError::AccountNotExecutable).into());
|
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)]
|
#[allow(clippy::deref_addrof)]
|
||||||
match MessageProcessor::process_cross_program_instruction(
|
match MessageProcessor::process_cross_program_instruction(
|
||||||
|
@ -6,6 +6,8 @@ use log::*;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use solana_sdk::{
|
use solana_sdk::{
|
||||||
account::Account,
|
account::Account,
|
||||||
|
account_utils::StateMut,
|
||||||
|
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
|
||||||
clock::Epoch,
|
clock::Epoch,
|
||||||
feature_set::{instructions_sysvar_enabled, FeatureSet},
|
feature_set::{instructions_sysvar_enabled, FeatureSet},
|
||||||
instruction::{CompiledInstruction, Instruction, InstructionError},
|
instruction::{CompiledInstruction, Instruction, InstructionError},
|
||||||
@ -310,6 +312,21 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
|
|||||||
fn is_feature_active(&self, feature_id: &Pubkey) -> bool {
|
fn is_feature_active(&self, feature_id: &Pubkey) -> bool {
|
||||||
self.feature_set.is_active(feature_id)
|
self.feature_set.is_active(feature_id)
|
||||||
}
|
}
|
||||||
|
fn get_account(&self, pubkey: &Pubkey) -> Option<Account> {
|
||||||
|
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 {
|
pub struct ThisLogger {
|
||||||
log_collector: Option<Rc<LogCollector>>,
|
log_collector: Option<Rc<LogCollector>>,
|
||||||
@ -541,7 +558,7 @@ impl MessageProcessor {
|
|||||||
instruction: &Instruction,
|
instruction: &Instruction,
|
||||||
keyed_accounts: &[&KeyedAccount],
|
keyed_accounts: &[&KeyedAccount],
|
||||||
signers: &[Pubkey],
|
signers: &[Pubkey],
|
||||||
) -> Result<(Message, Pubkey, usize), InstructionError> {
|
) -> Result<(Message, Pubkey), InstructionError> {
|
||||||
// Check for privilege escalation
|
// Check for privilege escalation
|
||||||
for account in instruction.accounts.iter() {
|
for account in instruction.accounts.iter() {
|
||||||
let keyed_account = keyed_accounts
|
let keyed_account = keyed_accounts
|
||||||
@ -584,10 +601,7 @@ impl MessageProcessor {
|
|||||||
let id = *message
|
let id = *message
|
||||||
.program_id(0)
|
.program_id(0)
|
||||||
.ok_or(InstructionError::MissingAccount)?;
|
.ok_or(InstructionError::MissingAccount)?;
|
||||||
let index = message
|
Ok((message, id))
|
||||||
.program_index(0)
|
|
||||||
.ok_or(InstructionError::MissingAccount)?;
|
|
||||||
Ok((message, id, index))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Entrypoint for a cross-program invocation from a native program
|
/// Entrypoint for a cross-program invocation from a native program
|
||||||
@ -605,7 +619,7 @@ impl MessageProcessor {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|seeds| Pubkey::create_program_address(&seeds, caller_program_id))
|
.map(|seeds| Pubkey::create_program_address(&seeds, caller_program_id))
|
||||||
.collect::<Result<Vec<_>, solana_sdk::pubkey::PubkeyError>>()?;
|
.collect::<Result<Vec<_>, 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)?;
|
Self::create_message(&instruction, &keyed_accounts, &signers)?;
|
||||||
let mut accounts = vec![];
|
let mut accounts = vec![];
|
||||||
let mut account_refs = vec![];
|
let mut account_refs = vec![];
|
||||||
@ -623,14 +637,33 @@ impl MessageProcessor {
|
|||||||
// Process instruction
|
// Process instruction
|
||||||
|
|
||||||
invoke_context.record_instruction(&instruction);
|
invoke_context.record_instruction(&instruction);
|
||||||
let program_account = (**accounts
|
|
||||||
.get(callee_program_id_index)
|
let program_account = invoke_context
|
||||||
.ok_or(InstructionError::MissingAccount)?)
|
.get_account(&callee_program_id)
|
||||||
.clone();
|
.ok_or(InstructionError::MissingAccount)?;
|
||||||
if !program_account.borrow().executable {
|
if !program_account.executable {
|
||||||
return Err(InstructionError::AccountNotExecutable);
|
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(
|
MessageProcessor::process_cross_program_instruction(
|
||||||
&message,
|
&message,
|
||||||
|
@ -61,6 +61,8 @@ pub trait InvokeContext {
|
|||||||
fn record_instruction(&self, instruction: &Instruction);
|
fn record_instruction(&self, instruction: &Instruction);
|
||||||
/// Get the bank's active feature set
|
/// Get the bank's active feature set
|
||||||
fn is_feature_active(&self, feature_id: &Pubkey) -> bool;
|
fn is_feature_active(&self, feature_id: &Pubkey) -> bool;
|
||||||
|
/// Get an account from a pre-account
|
||||||
|
fn get_account(&self, pubkey: &Pubkey) -> Option<Account>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, AbiExample)]
|
#[derive(Clone, Copy, Debug, AbiExample)]
|
||||||
@ -340,4 +342,7 @@ impl InvokeContext for MockInvokeContext {
|
|||||||
fn is_feature_active(&self, _feature_id: &Pubkey) -> bool {
|
fn is_feature_active(&self, _feature_id: &Pubkey) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
fn get_account(&self, _pubkey: &Pubkey) -> Option<Account> {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user