solana/runtime/src/native_loader.rs

159 lines
5.6 KiB
Rust
Raw Normal View History

//! Native loader
use crate::process_instruction::{InvokeContext, LoaderEntrypoint};
#[cfg(unix)]
use libloading::os::unix::*;
#[cfg(windows)]
use libloading::os::windows::*;
2018-12-14 20:39:10 -08:00
use log::*;
use num_derive::{FromPrimitive, ToPrimitive};
2020-01-28 17:03:20 -08:00
use solana_sdk::{
account::{next_keyed_account, KeyedAccount},
decode_error::DecodeError,
entrypoint_native::ProgramEntrypoint,
instruction::InstructionError,
pubkey::Pubkey,
2020-01-28 17:03:20 -08:00
};
2020-04-15 09:41:29 -07:00
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")]
2020-04-15 09:41:29 -07:00
InvalidEntrypointName = 0x0aaa_0001,
#[error("Entrypoint was not found in the module")]
2020-04-15 09:41:29 -07:00
EntrypointNotFound = 0x0aaa_0002,
#[error("Failed to load the module")]
2020-04-15 09:41:29 -07:00
FailedToLoad = 0x0aaa_0003,
}
impl<T> DecodeError<T> for NativeLoaderError {
fn type_of() -> &'static str {
"NativeLoaderError"
}
}
2018-12-10 09:31:17 -08:00
/// Dynamic link library prefixes
#[cfg(unix)]
2020-04-15 09:41:29 -07:00
const PLATFORM_FILE_PREFIX: &str = "lib";
#[cfg(windows)]
2020-04-15 09:41:29 -07:00
const PLATFORM_FILE_PREFIX: &str = "";
/// Dynamic link library file extension specific to the platform
#[cfg(any(target_os = "macos", target_os = "ios"))]
2020-04-15 09:41:29 -07:00
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"))))]
2020-04-15 09:41:29 -07:00
const PLATFORM_FILE_EXTENSION: &str = "so";
/// Dynamic link library file extension specific to the platform
#[cfg(windows)]
2020-04-15 09:41:29 -07:00
const PLATFORM_FILE_EXTENSION: &str = "dll";
2020-04-15 09:41:29 -07:00
pub type ProgramSymbolCache = RwLock<HashMap<String, Symbol<ProgramEntrypoint>>>;
pub type LoaderSymbolCache = RwLock<HashMap<String, Symbol<LoaderEntrypoint>>>;
2020-04-15 09:41:29 -07:00
#[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,
)
}));
let library_file_name = PathBuf::from(PLATFORM_FILE_PREFIX.to_string() + name)
.with_extension(PLATFORM_FILE_EXTENSION);
2020-04-15 09:41:29 -07:00
// 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)
}
}
2020-04-15 09:41:29 -07:00
#[cfg(windows)]
fn library_open(path: &PathBuf) -> Result<Library, libloading::Error> {
2020-04-15 09:41:29 -07:00
Library::new(path)
}
2019-06-04 21:49:05 -07:00
2020-04-15 09:41:29 -07:00
#[cfg(not(windows))]
fn library_open(path: &PathBuf) -> Result<Library, libloading::Error> {
2020-04-15 09:41:29 -07:00
// Linux tls bug can cause crash on dlclose(), workaround by never unloading
Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW)
}
2019-06-04 21:49:05 -07:00
fn get_entrypoint<T>(
2020-04-15 09:41:29 -07:00
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) => {
2020-07-23 12:11:01 -07:00
panic!("Unable to find program entrypoint in {:?}: {:?})", name, e);
2020-04-15 09:41:29 -07:00
}
}
}
Err(e) => {
2020-07-23 12:11:01 -07:00
panic!("Failed to load: {:?}", e);
2020-04-15 09:41:29 -07:00
}
}
}
}
2020-04-15 09:41:29 -07:00
pub fn process_instruction(
&self,
_program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
2020-04-28 14:33:56 -07:00
invoke_context: &dyn InvokeContext,
2020-04-15 09:41:29 -07:00
) -> 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;
let name = match str::from_utf8(name_vec) {
Ok(v) => v,
Err(e) => {
2020-07-23 12:11:01 -07:00
panic!("Invalid UTF-8 sequence: {}", e);
2020-04-15 09:41:29 -07:00
}
};
trace!("Call native {:?}", name);
if name.ends_with("loader_program") {
let entrypoint =
Self::get_entrypoint::<LoaderEntrypoint>(name, &self.loader_symbol_cache)?;
2020-04-28 14:33:56 -07:00
unsafe {
entrypoint(
program.unsigned_key(),
params,
instruction_data,
invoke_context,
)
}
2020-04-15 09:41:29 -07:00
} else {
let entrypoint =
Self::get_entrypoint::<ProgramEntrypoint>(name, &self.program_symbol_cache)?;
2020-04-15 09:41:29 -07:00
unsafe { entrypoint(program.unsigned_key(), params, instruction_data) }
}
}
}