Add native loader entry points (#9275)

This commit is contained in:
Jack May
2020-04-03 17:40:59 -07:00
committed by GitHub
parent c1441a2a8f
commit ed86d8d1fc
24 changed files with 297 additions and 154 deletions

View File

@ -27,7 +27,6 @@ rand = "0.6.5"
rayon = "1.3.0"
serde = { version = "1.0.105", features = ["rc"] }
serde_derive = "1.0.103"
solana-bpf-loader-program = { path = "../programs/bpf_loader", version = "1.2.0" }
solana-logger = { path = "../logger", version = "1.2.0" }
solana-measure = { path = "../measure", version = "1.2.0" }
solana-metrics = { path = "../metrics", version = "1.2.0" }

View File

@ -11,6 +11,7 @@ use solana_sdk::{
clock::MAX_RECENT_BLOCKHASHES,
genesis_config::create_genesis_config,
instruction::InstructionError,
native_program_info,
pubkey::Pubkey,
signature::{Keypair, Signer},
transaction::Transaction,
@ -124,7 +125,7 @@ fn do_bench_transactions(
let mut bank = Bank::new(&genesis_config);
bank.add_instruction_processor(Pubkey::new(&BUILTIN_PROGRAM_ID), process_instruction);
bank.register_native_instruction_processor(
"solana_noop_program",
&native_program_info!("solana_noop_program"),
&Pubkey::new(&NOOP_PROGRAM_ID),
);
let bank = Arc::new(bank);

View File

@ -39,7 +39,8 @@ use solana_sdk::{
hard_forks::HardForks,
hash::{extend_and_hash, hashv, Hash},
inflation::Inflation,
native_loader, nonce,
native_loader::{self, create_loadable_account},
native_program_info, nonce,
pubkey::Pubkey,
signature::{Keypair, Signature},
slot_hashes::SlotHashes,
@ -898,14 +899,18 @@ impl Bank {
);
// Add additional native programs specified in the genesis config
for (name, program_id) in &genesis_config.native_instruction_processors {
self.register_native_instruction_processor(name, program_id);
for (info, program_id) in &genesis_config.native_instruction_processors {
self.register_native_instruction_processor(&info, program_id);
}
}
pub fn register_native_instruction_processor(&self, name: &str, program_id: &Pubkey) {
debug!("Adding native program {} under {:?}", name, program_id);
let account = native_loader::create_loadable_account(name);
pub fn register_native_instruction_processor(
&self,
info: &native_loader::Info,
program_id: &Pubkey,
) {
debug!("Adding {:?} under {:?}", info, program_id);
let account = create_loadable_account(info);
self.store_account(program_id, &account);
}
@ -2155,7 +2160,7 @@ impl Bank {
assert_eq!(program_account.owner, solana_sdk::native_loader::id());
} else {
// Register a bogus executable account, which will be loaded and ignored.
self.register_native_instruction_processor("", &program_id);
self.register_native_instruction_processor(&native_program_info!(""), &program_id);
}
}

View File

@ -136,7 +136,6 @@ pub fn create_genesis_config_with_leader_ex(
// Bare minimum program set
let native_instruction_processors = vec![
solana_system_program(),
solana_bpf_loader_program!(),
solana_vote_program!(),
solana_stake_program!(),
];

View File

@ -30,9 +30,6 @@ extern crate solana_vote_program;
#[macro_use]
extern crate solana_stake_program;
#[macro_use]
extern crate solana_bpf_loader_program;
#[macro_use]
extern crate serde_derive;

View File

@ -1,22 +1,18 @@
use crate::{native_loader, rent_collector::RentCollector, system_instruction_processor};
use crate::{
native_loader::NativeLoader, rent_collector::RentCollector, system_instruction_processor,
};
use serde::{Deserialize, Serialize};
use solana_sdk::{
account::{create_keyed_readonly_accounts, Account, KeyedAccount},
clock::Epoch,
entrypoint_native,
instruction::{CompiledInstruction, InstructionError},
message::Message,
native_loader::id as native_loader_id,
native_loader,
pubkey::Pubkey,
system_program,
transaction::TransactionError,
};
use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::RwLock};
#[cfg(unix)]
use libloading::os::unix::*;
#[cfg(windows)]
use libloading::os::windows::*;
use std::{cell::RefCell, rc::Rc};
// The relevant state of an account before an Instruction executes, used
// to verify account integrity after the Instruction completes
@ -159,14 +155,13 @@ impl PreAccount {
}
pub type ProcessInstruction = fn(&Pubkey, &[KeyedAccount], &[u8]) -> Result<(), InstructionError>;
pub type SymbolCache = RwLock<HashMap<Vec<u8>, Symbol<entrypoint_native::Entrypoint>>>;
#[derive(Serialize, Deserialize)]
pub struct MessageProcessor {
#[serde(skip)]
instruction_processors: Vec<(Pubkey, ProcessInstruction)>,
#[serde(skip)]
symbol_cache: SymbolCache,
native_loader: NativeLoader,
}
impl Default for MessageProcessor {
fn default() -> Self {
@ -177,7 +172,7 @@ impl Default for MessageProcessor {
Self {
instruction_processors,
symbol_cache: RwLock::new(HashMap::new()),
native_loader: NativeLoader::default(),
}
}
}
@ -218,10 +213,10 @@ impl MessageProcessor {
})
.collect();
keyed_accounts.append(&mut keyed_accounts2);
assert!(keyed_accounts[0].executable()?, "account not executable");
let root_program_id = keyed_accounts[0].unsigned_key();
for (id, process_instruction) in &self.instruction_processors {
let root_program_id = keyed_accounts[0].unsigned_key();
if id == root_program_id {
return process_instruction(
&root_program_id,
@ -231,12 +226,15 @@ impl MessageProcessor {
}
}
native_loader::invoke_entrypoint(
&native_loader_id(),
&keyed_accounts,
&instruction.data,
&self.symbol_cache,
)
if native_loader::check_id(&keyed_accounts[0].owner()?) {
self.native_loader.process_instruction(
&native_loader::id(),
&keyed_accounts,
&instruction.data,
)
} else {
Err(InstructionError::UnsupportedProgramId)
}
}
/// Record the initial state of the accounts so that they can be compared
@ -372,6 +370,7 @@ mod tests {
instruction::{AccountMeta, Instruction, InstructionError},
message::Message,
native_loader::create_loadable_account,
native_program_info,
};
#[test]
@ -917,7 +916,9 @@ mod tests {
accounts.push(account);
let mut loaders: Vec<Vec<(Pubkey, RefCell<Account>)>> = Vec::new();
let account = RefCell::new(create_loadable_account("mock_system_program"));
let account = RefCell::new(create_loadable_account(&native_program_info!(
"mock_system_program"
)));
loaders.push(vec![(mock_system_program_id, account)]);
let from_pubkey = Pubkey::new_rand();
@ -1040,7 +1041,9 @@ mod tests {
accounts.push(account);
let mut loaders: Vec<Vec<(Pubkey, RefCell<Account>)>> = Vec::new();
let account = RefCell::new(create_loadable_account("mock_system_program"));
let account = RefCell::new(create_loadable_account(&native_program_info!(
"mock_system_program"
)));
loaders.push(vec![(mock_program_id, account)]);
let from_pubkey = Pubkey::new_rand();

View File

@ -1,29 +1,30 @@
//! Native loader
use crate::message_processor::SymbolCache;
#[cfg(unix)]
use libloading::os::unix::*;
#[cfg(windows)]
use libloading::os::windows::*;
use log::*;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive;
use solana_sdk::{
account::KeyedAccount,
entrypoint_native,
entrypoint_native::{LoaderEntrypoint, ProgramEntrypoint},
instruction::InstructionError,
native_loader::Kind,
program_utils::{next_keyed_account, DecodeError},
pubkey::Pubkey,
};
use std::{env, path::PathBuf, str};
use std::{collections::HashMap, env, 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")]
InvalidEntrypointName,
InvalidEntrypointName = 0x0aaa_0001,
#[error("Entrypoint was not found in the module")]
EntrypointNotFound,
EntrypointNotFound = 0x0aaa_0002,
#[error("Failed to load the module")]
FailedToLoad,
FailedToLoad = 0x0aaa_0003,
}
impl<T> DecodeError<T> for NativeLoaderError {
fn type_of() -> &'static str {
@ -33,103 +34,130 @@ impl<T> DecodeError<T> for NativeLoaderError {
/// Dynamic link library prefixes
#[cfg(unix)]
const PLATFORM_FILE_PREFIX_NATIVE: &str = "lib";
const PLATFORM_FILE_PREFIX: &str = "lib";
#[cfg(windows)]
const PLATFORM_FILE_PREFIX_NATIVE: &str = "";
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_NATIVE: &str = "dylib";
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_NATIVE: &str = "so";
const PLATFORM_FILE_EXTENSION: &str = "so";
/// Dynamic link library file extension specific to the platform
#[cfg(windows)]
const PLATFORM_FILE_EXTENSION_NATIVE: &str = "dll";
const PLATFORM_FILE_EXTENSION: &str = "dll";
fn create_path(name: &str) -> PathBuf {
let current_exe = env::current_exe()
.unwrap_or_else(|e| panic!("create_path(\"{}\"): current exe not found: {:?}", name, e));
let current_exe_directory = PathBuf::from(current_exe.parent().unwrap_or_else(|| {
panic!(
"create_path(\"{}\"): no parent directory of {:?}",
name, current_exe,
)
}));
pub type ProgramSymbolCache = RwLock<HashMap<String, Symbol<ProgramEntrypoint>>>;
pub type LoaderSymbolCache = RwLock<HashMap<String, Symbol<LoaderEntrypoint>>>;
let library_file_name = PathBuf::from(PLATFORM_FILE_PREFIX_NATIVE.to_string() + name)
.with_extension(PLATFORM_FILE_EXTENSION_NATIVE);
// 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() {
file_path
} else {
// `cargo build` places dependent libraries in the deps/ subdirectory
current_exe_directory.join("deps").join(library_file_name)
}
#[derive(Debug, Default)]
pub struct NativeLoader {
program_symbol_cache: ProgramSymbolCache,
loader_symbol_cache: LoaderSymbolCache,
}
impl NativeLoader {
fn create_path(name: &str) -> PathBuf {
let current_exe = env::current_exe().unwrap_or_else(|e| {
panic!("create_path(\"{}\"): current exe not found: {:?}", name, e)
});
let current_exe_directory = PathBuf::from(current_exe.parent().unwrap_or_else(|| {
panic!(
"create_path(\"{}\"): no parent directory of {:?}",
name, current_exe,
)
}));
#[cfg(windows)]
fn library_open(path: &PathBuf) -> std::io::Result<Library> {
Library::new(path)
}
let library_file_name = PathBuf::from(PLATFORM_FILE_PREFIX.to_string() + name)
.with_extension(PLATFORM_FILE_EXTENSION);
#[cfg(not(windows))]
fn library_open(path: &PathBuf) -> std::io::Result<Library> {
// Linux tls bug can cause crash on dlclose(), workaround by never unloading
Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW)
}
pub fn invoke_entrypoint(
_program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
symbol_cache: &SymbolCache,
) -> Result<(), InstructionError> {
let mut keyed_accounts_iter = keyed_accounts.iter();
let program = next_keyed_account(&mut keyed_accounts_iter)?;
let params = keyed_accounts_iter.as_slice();
let name_vec = &program.try_account_ref()?.data;
if let Some(entrypoint) = symbol_cache.read().unwrap().get(name_vec) {
unsafe {
return entrypoint(program.unsigned_key(), params, instruction_data);
// 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() {
file_path
} else {
// `cargo build` places dependent libraries in the deps/ subdirectory
current_exe_directory.join("deps").join(library_file_name)
}
}
let name = match str::from_utf8(name_vec) {
Ok(v) => v,
Err(e) => {
warn!("Invalid UTF-8 sequence: {}", e);
return Err(NativeLoaderError::InvalidEntrypointName.into());
}
};
trace!("Call native {:?}", name);
let path = create_path(&name);
match library_open(&path) {
Ok(library) => unsafe {
let entrypoint: Symbol<entrypoint_native::Entrypoint> =
match library.get(name.as_bytes()) {
Ok(s) => s,
Err(e) => {
warn!(
"Unable to find entrypoint {:?} (error: {:?})",
name.as_bytes(),
e
);
return Err(NativeLoaderError::EntrypointNotFound.into());
#[cfg(windows)]
fn library_open(path: &PathBuf) -> std::io::Result<Library> {
Library::new(path)
}
#[cfg(not(windows))]
fn library_open(path: &PathBuf) -> std::io::Result<Library> {
// Linux tls bug can cause crash on dlclose(), workaround by never unloading
Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW)
}
fn invoke_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) => {
warn!("Unable to find program entrypoint in {:?}: {:?})", name, e);
Err(NativeLoaderError::EntrypointNotFound.into())
}
}
};
let ret = entrypoint(program.unsigned_key(), params, instruction_data);
symbol_cache
.write()
.unwrap()
.insert(name_vec.to_vec(), entrypoint);
ret
},
Err(e) => {
warn!("Failed to load: {:?}", e);
Err(NativeLoaderError::FailedToLoad.into())
}
Err(e) => {
warn!("Failed to load: {:?}", e);
Err(NativeLoaderError::FailedToLoad.into())
}
}
}
}
pub fn process_instruction(
&self,
_program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
) -> Result<(), InstructionError> {
let mut keyed_accounts_iter = keyed_accounts.iter();
let program = next_keyed_account(&mut keyed_accounts_iter)?;
let params = keyed_accounts_iter.as_slice();
let data = &program.try_account_ref()?.data;
let program_kind = FromPrimitive::from_u8(data[0]);
let name = match str::from_utf8(&data[1..]) {
Ok(v) => v,
Err(e) => {
warn!("Invalid UTF-8 sequence: {}", e);
return Err(NativeLoaderError::InvalidEntrypointName.into());
}
};
trace!("Call native {:?}: {:?}", program_kind, name);
match program_kind {
Some(Kind::Program) => {
let entrypoint =
Self::invoke_entrypoint::<ProgramEntrypoint>(name, &self.program_symbol_cache)?;
unsafe { entrypoint(program.unsigned_key(), params, instruction_data) }
}
Some(Kind::Loader) => {
let entrypoint =
Self::invoke_entrypoint::<LoaderEntrypoint>(name, &self.loader_symbol_cache)?;
unsafe { entrypoint(program.unsigned_key(), params, instruction_data) }
}
None => {
warn!("Invalid native type: {:?}", data[0]);
Err(NativeLoaderError::FailedToLoad.into())
}
}
}
}

View File

@ -2,7 +2,8 @@ use solana_runtime::{
bank::Bank, bank_client::BankClient, loader_utils::create_invoke_instruction,
};
use solana_sdk::{
client::SyncClient, genesis_config::create_genesis_config, pubkey::Pubkey, signature::Signer,
client::SyncClient, genesis_config::create_genesis_config, native_program_info, pubkey::Pubkey,
signature::Signer,
};
#[test]
@ -12,7 +13,10 @@ fn test_program_native_noop() {
let (genesis_config, alice_keypair) = create_genesis_config(50);
let program_id = Pubkey::new_rand();
let bank = Bank::new(&genesis_config);
bank.register_native_instruction_processor("solana_noop_program", &program_id);
bank.register_native_instruction_processor(
&native_program_info!("solana_noop_program"),
&program_id,
);
// Call user program
let instruction = create_invoke_instruction(alice_keypair.pubkey(), program_id, &1u8);