Add solana-program-runtime crate (#19438)

This commit is contained in:
Justin Starry
2021-08-26 17:30:36 -07:00
committed by GitHub
parent 02b050e0f5
commit 2d7f036afd
22 changed files with 1361 additions and 1213 deletions

View File

@ -23,8 +23,6 @@ flate2 = "1.0.20"
fnv = "1.0.7"
itertools = "0.10.1"
lazy_static = "1.4.0"
libc = "0.2.101"
libloading = "0.7.0"
log = "0.4.14"
memmap2 = "0.3.1"
num-derive = { version = "0.3" }
@ -43,6 +41,7 @@ solana-frozen-abi-macro = { path = "../frozen-abi/macro", version = "=1.8.0" }
solana-logger = { path = "../logger", version = "=1.8.0" }
solana-measure = { path = "../measure", version = "=1.8.0" }
solana-metrics = { path = "../metrics", version = "=1.8.0" }
solana-program-runtime = { path = "../program-runtime", version = "=1.8.0" }
solana-rayon-threadlimit = { path = "../rayon-threadlimit", version = "=1.8.0" }
solana-sdk = { path = "../sdk", version = "=1.8.0" }
solana-secp256k1-program = { path = "../programs/secp256k1", version = "=1.8.0" }

View File

@ -1,102 +0,0 @@
#![feature(test)]
extern crate test;
use log::*;
use solana_runtime::message_processor::{ExecuteDetailsTimings, PreAccount};
use solana_sdk::{account::AccountSharedData, pubkey, rent::Rent};
use test::Bencher;
#[bench]
fn bench_verify_account_changes_data(bencher: &mut Bencher) {
solana_logger::setup();
let owner = pubkey::new_rand();
let non_owner = pubkey::new_rand();
let pre = PreAccount::new(
&pubkey::new_rand(),
&AccountSharedData::new(0, BUFSIZE, &owner),
);
let post = AccountSharedData::new(0, BUFSIZE, &owner);
assert_eq!(
pre.verify(
&owner,
false,
&Rent::default(),
&post,
&mut ExecuteDetailsTimings::default(),
false,
true,
),
Ok(())
);
// this one should be faster
bencher.iter(|| {
pre.verify(
&owner,
false,
&Rent::default(),
&post,
&mut ExecuteDetailsTimings::default(),
false,
true,
)
.unwrap();
});
let summary = bencher.bench(|_bencher| {}).unwrap();
info!("data no change by owner: {} ns/iter", summary.median);
let pre_data = vec![BUFSIZE];
let post_data = vec![BUFSIZE];
bencher.iter(|| pre_data == post_data);
let summary = bencher.bench(|_bencher| {}).unwrap();
info!("data compare {} ns/iter", summary.median);
let pre = PreAccount::new(
&pubkey::new_rand(),
&AccountSharedData::new(0, BUFSIZE, &owner),
);
bencher.iter(|| {
pre.verify(
&non_owner,
false,
&Rent::default(),
&post,
&mut ExecuteDetailsTimings::default(),
false,
true,
)
.unwrap();
});
let summary = bencher.bench(|_bencher| {}).unwrap();
info!("data no change by non owner: {} ns/iter", summary.median);
}
const BUFSIZE: usize = 1024 * 1024 + 127;
static BUF0: [u8; BUFSIZE] = [0; BUFSIZE];
static BUF1: [u8; BUFSIZE] = [1; BUFSIZE];
#[bench]
fn bench_is_zeroed(bencher: &mut Bencher) {
bencher.iter(|| {
PreAccount::is_zeroed(&BUF0);
});
}
#[bench]
fn bench_is_zeroed_not(bencher: &mut Bencher) {
bencher.iter(|| {
PreAccount::is_zeroed(&BUF1);
});
}
#[bench]
fn bench_is_zeroed_by_iter(bencher: &mut Bencher) {
bencher.iter(|| BUF0.iter().all(|item| *item == 0));
}
#[bench]
fn bench_is_zeroed_not_by_iter(bencher: &mut Bencher) {
bencher.iter(|| BUF1.iter().all(|item| *item == 0));
}

View File

@ -50,7 +50,7 @@ use crate::{
inline_spl_token_v2_0,
instruction_recorder::InstructionRecorder,
log_collector::LogCollector,
message_processor::{ExecuteDetailsTimings, Executors, MessageProcessor},
message_processor::MessageProcessor,
rent_collector::RentCollector,
stake_weighted_timestamp::{
calculate_stake_weighted_timestamp, MaxAllowableDrift, MAX_ALLOWABLE_DRIFT_PERCENTAGE,
@ -68,6 +68,7 @@ use log::*;
use rayon::ThreadPool;
use solana_measure::measure::Measure;
use solana_metrics::{datapoint_debug, inc_new_counter_debug, inc_new_counter_info};
use solana_program_runtime::{ExecuteDetailsTimings, Executors};
#[allow(deprecated)]
use solana_sdk::recent_blockhashes_account;
use solana_sdk::{
@ -5766,10 +5767,10 @@ pub(crate) mod tests {
create_genesis_config_with_leader, create_genesis_config_with_vote_accounts,
GenesisConfigInfo, ValidatorVoteKeypairs,
},
native_loader::NativeLoaderError,
status_cache::MAX_CACHE_ENTRIES,
};
use crossbeam_channel::{bounded, unbounded};
use solana_program_runtime::NativeLoaderError;
#[allow(deprecated)]
use solana_sdk::sysvar::fees::Fees;
use solana_sdk::{

View File

@ -25,7 +25,6 @@ pub mod instruction_recorder;
pub mod loader_utils;
pub mod log_collector;
pub mod message_processor;
mod native_loader;
pub mod neon_evm_program;
pub mod non_circulating_supply;
mod pubkey_bins;

File diff suppressed because it is too large Load Diff

View File

@ -1,192 +0,0 @@
//! Native loader
#[cfg(unix)]
use libloading::os::unix::*;
#[cfg(windows)]
use libloading::os::windows::*;
use log::*;
use num_derive::{FromPrimitive, ToPrimitive};
use solana_sdk::{
account::ReadableAccount,
decode_error::DecodeError,
entrypoint_native::ProgramEntrypoint,
instruction::InstructionError,
keyed_account::keyed_account_at_index,
native_loader,
process_instruction::{InvokeContext, LoaderEntrypoint},
pubkey::Pubkey,
};
use std::{
collections::HashMap,
env,
path::{Path, PathBuf},
str,
sync::RwLock,
};
use thiserror::Error;
#[derive(Error, Debug, Serialize, Clone, PartialEq, FromPrimitive, ToPrimitive)]
pub enum NativeLoaderError {
#[error("Entrypoint name in the account data is not a valid UTF-8 string")]
InvalidAccountData = 0x0aaa_0001,
#[error("Entrypoint was not found in the module")]
EntrypointNotFound = 0x0aaa_0002,
#[error("Failed to load the module")]
FailedToLoad = 0x0aaa_0003,
}
impl<T> DecodeError<T> for NativeLoaderError {
fn type_of() -> &'static str {
"NativeLoaderError"
}
}
/// Dynamic link library prefixes
#[cfg(unix)]
const PLATFORM_FILE_PREFIX: &str = "lib";
#[cfg(windows)]
const PLATFORM_FILE_PREFIX: &str = "";
/// Dynamic link library file extension specific to the platform
#[cfg(any(target_os = "macos", target_os = "ios"))]
const PLATFORM_FILE_EXTENSION: &str = "dylib";
/// Dynamic link library file extension specific to the platform
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
const PLATFORM_FILE_EXTENSION: &str = "so";
/// Dynamic link library file extension specific to the platform
#[cfg(windows)]
const PLATFORM_FILE_EXTENSION: &str = "dll";
pub type ProgramSymbolCache = RwLock<HashMap<String, Symbol<ProgramEntrypoint>>>;
pub type LoaderSymbolCache = RwLock<HashMap<String, Symbol<LoaderEntrypoint>>>;
#[derive(Debug, Default)]
pub struct NativeLoader {
program_symbol_cache: ProgramSymbolCache,
loader_symbol_cache: LoaderSymbolCache,
}
impl NativeLoader {
fn create_path(name: &str) -> Result<PathBuf, InstructionError> {
let current_exe = env::current_exe().map_err(|e| {
error!("create_path(\"{}\"): current exe not found: {:?}", name, e);
InstructionError::from(NativeLoaderError::EntrypointNotFound)
})?;
let current_exe_directory = PathBuf::from(current_exe.parent().ok_or_else(|| {
error!(
"create_path(\"{}\"): no parent directory of {:?}",
name, current_exe
);
InstructionError::from(NativeLoaderError::FailedToLoad)
})?);
let library_file_name = PathBuf::from(PLATFORM_FILE_PREFIX.to_string() + name)
.with_extension(PLATFORM_FILE_EXTENSION);
// Check the current_exe directory for the library as `cargo tests` are run
// from the deps/ subdirectory
let file_path = current_exe_directory.join(&library_file_name);
if file_path.exists() {
Ok(file_path)
} else {
// `cargo build` places dependent libraries in the deps/ subdirectory
Ok(current_exe_directory.join("deps").join(library_file_name))
}
}
#[cfg(windows)]
fn library_open(path: &Path) -> Result<Library, libloading::Error> {
unsafe { Library::new(path) }
}
#[cfg(not(windows))]
fn library_open(path: &Path) -> Result<Library, libloading::Error> {
unsafe {
// Linux tls bug can cause crash on dlclose(), workaround by never unloading
Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW)
}
}
fn get_entrypoint<T>(
name: &str,
cache: &RwLock<HashMap<String, Symbol<T>>>,
) -> Result<Symbol<T>, InstructionError> {
let mut cache = cache.write().unwrap();
if let Some(entrypoint) = cache.get(name) {
Ok(entrypoint.clone())
} else {
match Self::library_open(&Self::create_path(name)?) {
Ok(library) => {
let result = unsafe { library.get::<T>(name.as_bytes()) };
match result {
Ok(entrypoint) => {
cache.insert(name.to_string(), entrypoint.clone());
Ok(entrypoint)
}
Err(e) => {
error!("Unable to find program entrypoint in {:?}: {:?})", name, e);
Err(NativeLoaderError::EntrypointNotFound.into())
}
}
}
Err(e) => {
error!("Failed to load: {:?}", e);
Err(NativeLoaderError::FailedToLoad.into())
}
}
}
}
pub fn process_instruction(
&self,
program_id: &Pubkey,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
let (program_id, name_vec) = {
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let program = keyed_account_at_index(keyed_accounts, 0)?;
if native_loader::id() != *program_id {
error!("Program id mismatch");
return Err(InstructionError::IncorrectProgramId);
}
if program.owner()? != *program_id {
error!("Executable account now owned by loader");
return Err(InstructionError::IncorrectProgramId);
}
// TODO: Remove these two copies (* deref is also a copy)
// Both could be avoided as we know that the first KeyedAccount
// still exists even after invoke_context.remove_first_keyed_account() is called
(
*program.unsigned_key(),
&program.try_account_ref()?.data().to_vec(),
)
};
let name = match str::from_utf8(name_vec) {
Ok(v) => v,
Err(e) => {
error!("Invalid UTF-8 sequence: {}", e);
return Err(NativeLoaderError::InvalidAccountData.into());
}
};
if name.is_empty() || name.starts_with('\0') {
error!("Empty name string");
return Err(NativeLoaderError::InvalidAccountData.into());
}
trace!("Call native {:?}", name);
invoke_context.remove_first_keyed_account()?;
if name.ends_with("loader_program") {
let entrypoint =
Self::get_entrypoint::<LoaderEntrypoint>(name, &self.loader_symbol_cache)?;
unsafe { entrypoint(&program_id, instruction_data, invoke_context) }
} else {
let entrypoint =
Self::get_entrypoint::<ProgramEntrypoint>(name, &self.program_symbol_cache)?;
unsafe {
entrypoint(
&program_id,
invoke_context.get_keyed_accounts()?,
instruction_data,
)
}
}
}
}

View File

@ -11,7 +11,6 @@ use {
blockhash_queue::BlockhashQueue,
epoch_stakes::EpochStakes,
hardened_unpack::UnpackedAppendVecMap,
message_processor::MessageProcessor,
rent_collector::RentCollector,
serde_snapshot::future::SerializableStorage,
stakes::Stakes,
@ -21,6 +20,7 @@ use {
log::*,
rayon::prelude::*,
serde::{de::DeserializeOwned, Deserialize, Serialize},
solana_program_runtime::InstructionProcessor,
solana_sdk::{
clock::{Epoch, Slot, UnixTimestamp},
epoch_schedule::EpochSchedule,

View File

@ -78,7 +78,7 @@ pub(crate) struct DeserializableVersionedBank {
pub(crate) unused_accounts: UnusedAccounts,
pub(crate) epoch_stakes: HashMap<Epoch, EpochStakes>,
pub(crate) is_delta: bool,
pub(crate) message_processor: MessageProcessor,
pub(crate) message_processor: InstructionProcessor,
}
impl From<DeserializableVersionedBank> for BankFieldsToDeserialize {
@ -155,7 +155,7 @@ pub(crate) struct SerializableVersionedBank<'a> {
pub(crate) unused_accounts: UnusedAccounts,
pub(crate) epoch_stakes: &'a HashMap<Epoch, EpochStakes>,
pub(crate) is_delta: bool,
pub(crate) message_processor: MessageProcessor,
pub(crate) message_processor: InstructionProcessor,
}
impl<'a> From<crate::bank::BankFieldsToSerialize<'a>> for SerializableVersionedBank<'a> {

View File

@ -308,7 +308,7 @@ mod test_bank_serialize {
// This some what long test harness is required to freeze the ABI of
// Bank's serialization due to versioned nature
#[frozen_abi(digest = "7XCv7DU27QC6iNJ1WYkXY3X4bKu8j6CxAn6morP2u4hu")]
#[frozen_abi(digest = "A9KFf8kLJczP3AMbFXRrqzmruoqMjooTPzdvEwZZ4EP7")]
#[derive(Serialize, AbiExample)]
pub struct BankAbiTestWrapperFuture {
#[serde(serialize_with = "wrapper_future")]