Cache re-usable work performed by the loader (#12135)

This commit is contained in:
Jack May
2020-09-14 17:42:37 -07:00
committed by GitHub
parent af2262cbba
commit 3278d78f08
11 changed files with 543 additions and 249 deletions

View File

@ -1176,9 +1176,9 @@ dependencies = [
[[package]]
name = "paste"
version = "0.1.14"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3431e8f72b90f8a7af91dec890d9814000cb371258e0ec7370d93e085361f531"
checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880"
dependencies = [
"paste-impl",
"proc-macro-hack",
@ -1186,14 +1186,11 @@ dependencies = [
[[package]]
name = "paste-impl"
version = "0.1.14"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25af5fc872ba284d8d84608bf8a0fa9b5376c96c23f503b007dfd9e34dde5606"
checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6"
dependencies = [
"proc-macro-hack",
"proc-macro2 1.0.19",
"quote 1.0.6",
"syn 1.0.27",
]
[[package]]
@ -2050,9 +2047,9 @@ dependencies = [
[[package]]
name = "solana_rbpf"
version = "0.1.30"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "185f68b54660652e2244bbdef792b369f12045da856a4af75b776e6e72757831"
checksum = "962f8f04ac7239fe4dd45fa4ce706ec78b59a0da9f41def463832857e36c60b0"
dependencies = [
"byteorder 1.3.4",
"combine",

View File

@ -26,7 +26,7 @@ solana-bpf-loader-program = { path = "../bpf_loader", version = "1.4.0" }
solana-logger = { path = "../../logger", version = "1.4.0" }
solana-runtime = { path = "../../runtime", version = "1.4.0" }
solana-sdk = { path = "../../sdk", version = "1.4.0" }
solana_rbpf = "=0.1.30"
solana_rbpf = "=0.1.31"
[[bench]]
name = "bpf_loader"

View File

@ -6,8 +6,7 @@ extern crate test;
extern crate solana_bpf_loader_program;
use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
use solana_bpf_loader_program::serialization::{deserialize_parameters, serialize_parameters};
use solana_rbpf::EbpfVm;
use solana_rbpf::vm::EbpfVm;
use solana_runtime::{
bank::Bank,
bank_client::BankClient,
@ -15,11 +14,13 @@ use solana_runtime::{
loader_utils::load_program,
};
use solana_sdk::{
account::{create_keyed_readonly_accounts, Account},
bpf_loader, bpf_loader_deprecated,
account::Account,
bpf_loader,
client::SyncClient,
entrypoint::SUCCESS,
entrypoint_native::{ComputeBudget, ComputeMeter, InvokeContext, Logger, ProcessInstruction},
entrypoint_native::{
ComputeBudget, ComputeMeter, Executor, InvokeContext, Logger, ProcessInstruction,
},
instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError},
message::Message,
pubkey::Pubkey,
@ -42,10 +43,6 @@ fn create_bpf_path(name: &str) -> PathBuf {
pathbuf
}
fn empty_check(_prog: &[u8]) -> Result<(), solana_bpf_loader_program::BPFError> {
Ok(())
}
fn load_elf(name: &str) -> Result<Vec<u8>, std::io::Error> {
let path = create_bpf_path(name);
let mut file = File::open(&path).expect(&format!("Unable to open {:?}", path));
@ -71,15 +68,13 @@ const ARMSTRONG_LIMIT: u64 = 500;
const ARMSTRONG_EXPECTED: u64 = 5;
#[bench]
fn bench_program_verify(bencher: &mut Bencher) {
fn bench_program_create_executable(bencher: &mut Bencher) {
let elf = load_elf("bench_alu").unwrap();
let mut vm = EbpfVm::<solana_bpf_loader_program::BPFError>::new(None).unwrap();
vm.set_verifier(empty_check).unwrap();
vm.set_elf(&elf).unwrap();
bencher.iter(|| {
vm.set_verifier(solana_bpf_loader_program::bpf_verifier::check)
.unwrap();
let _ =
EbpfVm::<solana_bpf_loader_program::BPFError>::create_executable_from_elf(&elf, None)
.unwrap();
});
}
@ -96,8 +91,16 @@ fn bench_program_alu(bencher: &mut Bencher) {
let mut invoke_context = MockInvokeContext::default();
let elf = load_elf("bench_alu").unwrap();
let (mut vm, _) =
solana_bpf_loader_program::create_vm(&loader_id, &elf, &[], &mut invoke_context).unwrap();
let executable =
EbpfVm::<solana_bpf_loader_program::BPFError>::create_executable_from_elf(&elf, None)
.unwrap();
let (mut vm, _) = solana_bpf_loader_program::create_vm(
&loader_id,
executable.as_ref(),
&[],
&mut invoke_context,
)
.unwrap();
println!("Interpreted:");
assert_eq!(
@ -151,8 +154,6 @@ fn bench_program_alu(bencher: &mut Bencher) {
#[bench]
fn bench_program_execute_noop(bencher: &mut Bencher) {
// solana_logger::setup(); // TODO remove
let GenesisConfigInfo {
genesis_config,
mint_keypair,
@ -177,7 +178,6 @@ fn bench_program_execute_noop(bencher: &mut Bencher) {
.send_and_confirm_message(&[&mint_keypair], message.clone())
.unwrap();
println!("start bench");
bencher.iter(|| {
bank.clear_signatures();
bank_client
@ -186,62 +186,6 @@ fn bench_program_execute_noop(bencher: &mut Bencher) {
});
}
fn create_serialization_create_params() -> (Vec<u8>, Vec<(Pubkey, RefCell<Account>)>) {
let accounts = vec![
(
Pubkey::new_rand(),
RefCell::new(Account::new(0, 100, &Pubkey::new_rand())),
),
(
Pubkey::new_rand(),
RefCell::new(Account::new(0, 100, &Pubkey::new_rand())),
),
(
Pubkey::new_rand(),
RefCell::new(Account::new(0, 250, &Pubkey::new_rand())),
),
(
Pubkey::new_rand(),
RefCell::new(Account::new(0, 1000, &Pubkey::new_rand())),
),
];
(vec![0xee; 100], accounts)
}
#[bench]
fn bench_serialization_aligned(bencher: &mut Bencher) {
let (data, accounts) = create_serialization_create_params();
let keyed_accounts = create_keyed_readonly_accounts(&accounts);
bencher.iter(|| {
let buffer = serialize_parameters(
&bpf_loader_deprecated::id(),
&Pubkey::new_rand(),
&keyed_accounts,
&data,
)
.unwrap();
deserialize_parameters(&bpf_loader_deprecated::id(), &keyed_accounts, &buffer).unwrap();
});
}
#[bench]
fn bench_serialization_unaligned(bencher: &mut Bencher) {
let (data, accounts) = create_serialization_create_params();
let keyed_accounts = create_keyed_readonly_accounts(&accounts);
bencher.iter(|| {
let buffer = serialize_parameters(
&bpf_loader_deprecated::id(),
&Pubkey::new_rand(),
&keyed_accounts,
&data,
)
.unwrap();
deserialize_parameters(&bpf_loader_deprecated::id(), &keyed_accounts, &buffer).unwrap();
});
}
#[derive(Debug, Default)]
pub struct MockInvokeContext {
key: Pubkey,
@ -279,6 +223,10 @@ impl InvokeContext for MockInvokeContext {
fn get_compute_meter(&self) -> Rc<RefCell<dyn ComputeMeter>> {
Rc::new(RefCell::new(self.mock_compute_meter.clone()))
}
fn add_executor(&mut self, _pubkey: &Pubkey, _executor: Arc<dyn Executor>) {}
fn get_executor(&mut self, _pubkey: &Pubkey) -> Option<Arc<dyn Executor>> {
None
}
}
#[derive(Debug, Default, Clone)]
pub struct MockLogger {

View File

@ -7,7 +7,7 @@ use solana_bpf_loader_program::{
create_vm,
serialization::{deserialize_parameters, serialize_parameters},
};
use solana_rbpf::InstructionMeter;
use solana_rbpf::vm::{EbpfVm, InstructionMeter};
use solana_runtime::{
bank::Bank,
bank_client::BankClient,
@ -20,7 +20,9 @@ use solana_sdk::{
client::SyncClient,
clock::DEFAULT_SLOTS_PER_EPOCH,
entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS},
entrypoint_native::{ComputeBudget, ComputeMeter, InvokeContext, Logger, ProcessInstruction},
entrypoint_native::{
ComputeBudget, ComputeMeter, Executor, InvokeContext, Logger, ProcessInstruction,
},
instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError},
message::Message,
pubkey::Pubkey,
@ -67,14 +69,15 @@ fn run_program(
let path = create_bpf_path(name);
let mut file = File::open(path).unwrap();
let mut program_account = Account::default();
file.read_to_end(&mut program_account.data).unwrap();
let mut data = vec![];
file.read_to_end(&mut data).unwrap();
let loader_id = bpf_loader::id();
let mut invoke_context = MockInvokeContext::default();
let executable = EbpfVm::create_executable_from_elf(&data, None).unwrap();
let (mut vm, heap_region) = create_vm(
&loader_id,
&program_account.data,
executable.as_ref(),
parameter_accounts,
&mut invoke_context,
)
@ -631,6 +634,10 @@ impl InvokeContext for MockInvokeContext {
fn get_compute_meter(&self) -> Rc<RefCell<dyn ComputeMeter>> {
Rc::new(RefCell::new(self.compute_meter.clone()))
}
fn add_executor(&mut self, _pubkey: &Pubkey, _executor: Arc<dyn Executor>) {}
fn get_executor(&mut self, _pubkey: &Pubkey) -> Option<Arc<dyn Executor>> {
None
}
}
#[derive(Debug, Default, Clone)]

View File

@ -11,11 +11,11 @@ edition = "2018"
[dependencies]
bincode = "1.3.1"
byteorder = "1.3.4"
num-derive = { version = "0.3" }
num-traits = { version = "0.2" }
num-derive = "0.3"
num-traits = "0.2"
solana-runtime = { path = "../../runtime", version = "1.4.0" }
solana-sdk = { path = "../../sdk", version = "1.4.0" }
solana_rbpf = "=0.1.30"
solana_rbpf = "=0.1.31"
thiserror = "1.0"
[dev-dependencies]

View File

@ -12,22 +12,22 @@ use crate::{
};
use num_derive::{FromPrimitive, ToPrimitive};
use solana_rbpf::{
ebpf::{EbpfError, UserDefinedError},
error::{EbpfError, UserDefinedError},
memory_region::MemoryRegion,
EbpfVm, InstructionMeter,
vm::{EbpfVm, Executable, InstructionMeter},
};
use solana_sdk::{
account::{is_executable, next_keyed_account, KeyedAccount},
bpf_loader, bpf_loader_deprecated,
decode_error::DecodeError,
entrypoint::SUCCESS,
entrypoint_native::{ComputeMeter, InvokeContext},
entrypoint_native::{ComputeMeter, Executor, InvokeContext},
instruction::InstructionError,
loader_instruction::LoaderInstruction,
program_utils::limited_deserialize,
pubkey::Pubkey,
};
use std::{cell::RefCell, rc::Rc};
use std::{cell::RefCell, rc::Rc, sync::Arc};
use thiserror::Error;
solana_sdk::declare_builtin!(
@ -36,6 +36,7 @@ solana_sdk::declare_builtin!(
solana_bpf_loader_program::process_instruction
);
/// Errors returned by the BPFLoader if the VM fails to run the program
#[derive(Error, Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
pub enum BPFLoaderError {
#[error("failed to create virtual machine")]
@ -49,7 +50,7 @@ impl<E> DecodeError<E> for BPFLoaderError {
}
}
/// Errors returned by functions the BPF Loader registers with the vM
/// Errors returned by functions the BPF Loader registers with the VM
#[derive(Debug, Error, PartialEq)]
pub enum BPFError {
#[error("{0}")]
@ -59,39 +60,7 @@ pub enum BPFError {
}
impl UserDefinedError for BPFError {}
pub fn create_vm<'a>(
loader_id: &'a Pubkey,
prog: &'a [u8],
parameter_accounts: &'a [KeyedAccount<'a>],
invoke_context: &'a mut dyn InvokeContext,
) -> Result<(EbpfVm<'a, BPFError>, MemoryRegion), EbpfError<BPFError>> {
let mut vm = EbpfVm::new(None)?;
vm.set_verifier(bpf_verifier::check)?;
vm.set_elf(&prog)?;
let heap_region =
syscalls::register_syscalls(loader_id, &mut vm, parameter_accounts, invoke_context)?;
Ok((vm, heap_region))
}
pub fn check_elf(prog: &[u8]) -> Result<(), EbpfError<BPFError>> {
let mut vm = EbpfVm::new(None)?;
vm.set_verifier(bpf_verifier::check)?;
vm.set_elf(&prog)?;
Ok(())
}
/// Look for a duplicate account and return its position if found
pub fn is_dup(accounts: &[KeyedAccount], keyed_account: &KeyedAccount) -> (bool, usize) {
for (i, account) in accounts.iter().enumerate() {
if account == keyed_account {
return (true, i);
}
}
(false, 0)
}
/// Point all log messages to the log collector
macro_rules! log{
($logger:ident, $message:expr) => {
if let Ok(mut logger) = $logger.try_borrow_mut() {
@ -102,23 +71,42 @@ macro_rules! log{
};
($logger:ident, $fmt:expr, $($arg:tt)*) => {
if let Ok(mut logger) = $logger.try_borrow_mut() {
logger.log(&format!($fmt, $($arg)*));
if logger.log_enabled() {
logger.log(&format!($fmt, $($arg)*));
}
}
};
}
struct ThisInstructionMeter {
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
pub fn create_and_cache_executor(
program: &KeyedAccount,
invoke_context: &mut dyn InvokeContext,
) -> Result<Arc<BPFExecutor>, InstructionError> {
let executable = EbpfVm::create_executable_from_elf(
&program.try_account_ref()?.data,
Some(bpf_verifier::check),
)
.map_err(|e| {
let logger = invoke_context.get_logger();
log!(logger, "{}", e);
InstructionError::InvalidAccountData
})?;
let executor = Arc::new(BPFExecutor { executable });
invoke_context.add_executor(program.unsigned_key(), executor.clone());
Ok(executor)
}
impl InstructionMeter for ThisInstructionMeter {
fn consume(&mut self, amount: u64) {
// 1 to 1 instruction to compute unit mapping
// ignore error, Ebpf will bail if exceeded
let _ = self.compute_meter.borrow_mut().consume(amount);
}
fn get_remaining(&self) -> u64 {
self.compute_meter.borrow().get_remaining()
}
/// Create the BPF virtual machine
pub fn create_vm<'a>(
loader_id: &'a Pubkey,
executable: &'a dyn Executable<BPFError>,
parameter_accounts: &'a [KeyedAccount<'a>],
invoke_context: &'a mut dyn InvokeContext,
) -> Result<(EbpfVm<'a, BPFError>, MemoryRegion), EbpfError<BPFError>> {
let mut vm = EbpfVm::new(executable)?;
let heap_region =
syscalls::register_syscalls(loader_id, &mut vm, parameter_accounts, invoke_context)?;
Ok((vm, heap_region))
}
pub fn process_instruction(
@ -135,8 +123,82 @@ pub fn process_instruction(
log!(logger, "No account keys");
return Err(InstructionError::NotEnoughAccountKeys);
}
let program = &keyed_accounts[0];
if is_executable(keyed_accounts)? {
let executor = match invoke_context.get_executor(program.unsigned_key()) {
Some(executor) => executor,
None => create_and_cache_executor(program, invoke_context)?,
};
executor.execute(program_id, keyed_accounts, instruction_data, invoke_context)?
} else if !keyed_accounts.is_empty() {
match limited_deserialize(instruction_data)? {
LoaderInstruction::Write { offset, bytes } => {
if program.signer_key().is_none() {
log!(logger, "key[0] did not sign the transaction");
return Err(InstructionError::MissingRequiredSignature);
}
let offset = offset as usize;
let len = bytes.len();
if program.data_len()? < offset + len {
log!(
logger,
"Write overflow: {} < {}",
program.data_len()?,
offset + len
);
return Err(InstructionError::AccountDataTooSmall);
}
program.try_account_ref_mut()?.data[offset..offset + len].copy_from_slice(&bytes);
}
LoaderInstruction::Finalize => {
if program.signer_key().is_none() {
log!(logger, "key[0] did not sign the transaction");
return Err(InstructionError::MissingRequiredSignature);
}
let _ = create_and_cache_executor(program, invoke_context)?;
program.try_account_ref_mut()?.executable = true;
log!(
logger,
"Finalized account {:?}",
program.signer_key().unwrap()
);
}
}
}
Ok(())
}
/// Passed to the VM to enforce the compute budget
struct ThisInstructionMeter {
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
}
impl InstructionMeter for ThisInstructionMeter {
fn consume(&mut self, amount: u64) {
// 1 to 1 instruction to compute unit mapping
// ignore error, Ebpf will bail if exceeded
let _ = self.compute_meter.borrow_mut().consume(amount);
}
fn get_remaining(&self) -> u64 {
self.compute_meter.borrow().get_remaining()
}
}
/// BPF Loader's Executor implementation
pub struct BPFExecutor {
executable: Box<dyn Executable<BPFError>>,
}
impl Executor for BPFExecutor {
fn execute(
&self,
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
let logger = invoke_context.get_logger();
let mut keyed_accounts_iter = keyed_accounts.iter();
let program = next_keyed_account(&mut keyed_accounts_iter)?;
@ -149,10 +211,9 @@ pub fn process_instruction(
)?;
{
let compute_meter = invoke_context.get_compute_meter();
let program_account = program.try_account_ref_mut()?;
let (mut vm, heap_region) = match create_vm(
program_id,
&program_account.data,
self.executable.as_ref(),
&parameter_accounts,
invoke_context,
) {
@ -201,59 +262,15 @@ pub fn process_instruction(
}
deserialize_parameters(program_id, parameter_accounts, &parameter_bytes)?;
log!(logger, "BPF program {} success", program.unsigned_key());
} else if !keyed_accounts.is_empty() {
match limited_deserialize(instruction_data)? {
LoaderInstruction::Write { offset, bytes } => {
let mut keyed_accounts_iter = keyed_accounts.iter();
let program = next_keyed_account(&mut keyed_accounts_iter)?;
if program.signer_key().is_none() {
log!(logger, "key[0] did not sign the transaction");
return Err(InstructionError::MissingRequiredSignature);
}
let offset = offset as usize;
let len = bytes.len();
if program.data_len()? < offset + len {
log!(
logger,
"Write overflow: {} < {}",
program.data_len()?,
offset + len
);
return Err(InstructionError::AccountDataTooSmall);
}
program.try_account_ref_mut()?.data[offset..offset + len].copy_from_slice(&bytes);
}
LoaderInstruction::Finalize => {
let mut keyed_accounts_iter = keyed_accounts.iter();
let program = next_keyed_account(&mut keyed_accounts_iter)?;
if program.signer_key().is_none() {
log!(logger, "key[0] did not sign the transaction");
return Err(InstructionError::MissingRequiredSignature);
}
if let Err(e) = check_elf(&program.try_account_ref()?.data) {
log!(logger, "{}", e);
return Err(InstructionError::InvalidAccountData);
}
program.try_account_ref_mut()?.executable = true;
log!(
logger,
"Finalized account {:?}",
program.signer_key().unwrap()
);
}
}
Ok(())
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use rand::Rng;
use solana_runtime::message_processor::ThisInvokeContext;
use solana_runtime::message_processor::{Executors, ThisInvokeContext};
use solana_sdk::{
account::Account,
entrypoint_native::{ComputeBudget, Logger, ProcessInstruction},
@ -339,6 +356,10 @@ mod tests {
fn get_compute_meter(&self) -> Rc<RefCell<dyn ComputeMeter>> {
Rc::new(RefCell::new(self.compute_meter.clone()))
}
fn add_executor(&mut self, _pubkey: &Pubkey, _executor: Arc<dyn Executor>) {}
fn get_executor(&mut self, _pubkey: &Pubkey) -> Option<Arc<dyn Executor>> {
None
}
}
struct TestInstructionMeter {
@ -364,10 +385,10 @@ mod tests {
];
let input = &mut [0x00];
let mut vm = EbpfVm::<BPFError>::new(None).unwrap();
vm.set_verifier(bpf_verifier::check).unwrap();
let executable =
EbpfVm::create_executable_from_text_bytes(program, Some(bpf_verifier::check)).unwrap();
let mut vm = EbpfVm::<BPFError>::new(executable.as_ref()).unwrap();
let instruction_meter = TestInstructionMeter { remaining: 10 };
vm.set_program(program).unwrap();
vm.execute_program_metered(input, &[], &[], instruction_meter)
.unwrap();
}
@ -518,38 +539,25 @@ mod tests {
let mut keyed_accounts = vec![KeyedAccount::new(&program_key, false, &program_account)];
let mut invoke_context = MockInvokeContext::default();
// Case: Empty keyed accounts
assert_eq!(
Err(InstructionError::NotEnoughAccountKeys),
process_instruction(
&bpf_loader::id(),
&[],
&[],
&mut MockInvokeContext::default()
)
process_instruction(&bpf_loader::id(), &[], &[], &mut invoke_context)
);
// Case: Only a program account
assert_eq!(
Ok(()),
process_instruction(
&bpf_loader::id(),
&keyed_accounts,
&[],
&mut MockInvokeContext::default()
)
process_instruction(&bpf_loader::id(), &keyed_accounts, &[], &mut invoke_context)
);
// Case: Account not executable
keyed_accounts[0].account.borrow_mut().executable = false;
assert_eq!(
Err(InstructionError::InvalidInstructionData),
process_instruction(
&bpf_loader::id(),
&keyed_accounts,
&[],
&mut MockInvokeContext::default()
)
process_instruction(&bpf_loader::id(), &keyed_accounts, &[], &mut invoke_context)
);
keyed_accounts[0].account.borrow_mut().executable = true;
@ -558,12 +566,7 @@ mod tests {
keyed_accounts.push(KeyedAccount::new(&program_key, false, &parameter_account));
assert_eq!(
Ok(()),
process_instruction(
&bpf_loader::id(),
&keyed_accounts,
&[],
&mut MockInvokeContext::default()
)
process_instruction(&bpf_loader::id(), &keyed_accounts, &[], &mut invoke_context)
);
// Case: limited budget
@ -583,6 +586,7 @@ mod tests {
invoke_units: 1000,
max_invoke_depth: 2,
},
Rc::new(RefCell::new(Executors::default())),
);
assert_eq!(
Err(InstructionError::Custom(194969602)),

View File

@ -1,9 +1,10 @@
use crate::{alloc, BPFError};
use alloc::Alloc;
use solana_rbpf::{
ebpf::{EbpfError, SyscallObject, ELF_INSN_DUMP_OFFSET, MM_HEAP_START},
ebpf::{ELF_INSN_DUMP_OFFSET, MM_HEAP_START},
error::EbpfError,
memory_region::{translate_addr, MemoryRegion},
EbpfVm,
vm::{EbpfVm, SyscallObject},
};
use solana_runtime::message_processor::MessageProcessor;
use solana_sdk::{
@ -1282,7 +1283,7 @@ mod tests {
assert_eq!(
Err(EbpfError::AccessViolation(
"programs/bpf_loader/src/syscalls.rs".to_string(),
247,
248,
100,
32,
" regions: \n0x64-0x73".to_string()