Add solana-program-runtime crate (#19438)
This commit is contained in:
33
program-runtime/Cargo.toml
Normal file
33
program-runtime/Cargo.toml
Normal file
@ -0,0 +1,33 @@
|
||||
[package]
|
||||
name = "solana-program-runtime"
|
||||
version = "1.8.0"
|
||||
description = "Solana program runtime"
|
||||
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
|
||||
repository = "https://github.com/solana-labs/solana"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://solana.com/"
|
||||
documentation = "https://docs.rs/solana-program-runtime"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2.101"
|
||||
libloading = "0.7.0"
|
||||
log = "0.4.14"
|
||||
num-derive = { version = "0.3" }
|
||||
num-traits = { version = "0.2" }
|
||||
serde = { version = "1.0.129", features = ["derive", "rc"] }
|
||||
solana-frozen-abi = { path = "../frozen-abi", version = "=1.8.0" }
|
||||
solana-frozen-abi-macro = { path = "../frozen-abi/macro", version = "=1.8.0" }
|
||||
solana-logger = { path = "../logger", version = "=1.8.0" }
|
||||
solana-sdk = { path = "../sdk", version = "=1.8.0" }
|
||||
thiserror = "1.0"
|
||||
|
||||
[lib]
|
||||
crate-type = ["lib"]
|
||||
name = "solana_program_runtime"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[build-dependencies]
|
||||
rustc_version = "0.4"
|
102
program-runtime/benches/instruction_processor.rs
Normal file
102
program-runtime/benches/instruction_processor.rs
Normal file
@ -0,0 +1,102 @@
|
||||
#![feature(test)]
|
||||
|
||||
extern crate test;
|
||||
|
||||
use log::*;
|
||||
use solana_program_runtime::{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));
|
||||
}
|
1
program-runtime/build.rs
Symbolic link
1
program-runtime/build.rs
Symbolic link
@ -0,0 +1 @@
|
||||
../frozen-abi/build.rs
|
1229
program-runtime/src/instruction_processor.rs
Normal file
1229
program-runtime/src/instruction_processor.rs
Normal file
File diff suppressed because it is too large
Load Diff
8
program-runtime/src/lib.rs
Normal file
8
program-runtime/src/lib.rs
Normal file
@ -0,0 +1,8 @@
|
||||
#![cfg_attr(RUSTC_WITH_SPECIALIZATION, feature(min_specialization))]
|
||||
#![allow(clippy::integer_arithmetic)]
|
||||
|
||||
mod instruction_processor;
|
||||
mod native_loader;
|
||||
|
||||
pub use instruction_processor::*;
|
||||
pub use native_loader::*;
|
193
program-runtime/src/native_loader.rs
Normal file
193
program-runtime/src/native_loader.rs
Normal file
@ -0,0 +1,193 @@
|
||||
//! Native loader
|
||||
#[cfg(unix)]
|
||||
use libloading::os::unix::*;
|
||||
#[cfg(windows)]
|
||||
use libloading::os::windows::*;
|
||||
use log::*;
|
||||
use num_derive::{FromPrimitive, ToPrimitive};
|
||||
use serde::Serialize;
|
||||
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,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user