2018-10-23 14:44:41 -07:00
|
|
|
//! Native loader
|
2018-10-04 09:44:44 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
use libloading::os::unix::*;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use libloading::os::windows::*;
|
2018-12-14 20:39:10 -08:00
|
|
|
use log::*;
|
2020-02-14 13:58:48 -08:00
|
|
|
use num_derive::{FromPrimitive, ToPrimitive};
|
2020-01-28 17:03:20 -08:00
|
|
|
use solana_sdk::{
|
2021-03-09 16:31:33 -06:00
|
|
|
account::ReadableAccount,
|
2020-06-17 10:39:14 -07:00
|
|
|
decode_error::DecodeError,
|
2020-09-29 01:36:46 -07:00
|
|
|
entrypoint_native::ProgramEntrypoint,
|
2020-03-04 17:10:22 -08:00
|
|
|
instruction::InstructionError,
|
2021-04-19 18:48:48 +02:00
|
|
|
keyed_account::keyed_account_at_index,
|
2021-01-11 14:36:52 -08:00
|
|
|
native_loader,
|
2020-10-28 20:21:50 -07:00
|
|
|
process_instruction::{InvokeContext, LoaderEntrypoint},
|
2020-03-04 17:10:22 -08:00
|
|
|
pubkey::Pubkey,
|
2020-01-28 17:03:20 -08:00
|
|
|
};
|
2021-01-23 11:55:15 -08:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
env,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
str,
|
|
|
|
sync::RwLock,
|
|
|
|
};
|
2020-02-14 13:58:48 -08:00
|
|
|
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-10-12 13:40:04 -07:00
|
|
|
InvalidAccountData = 0x0aaa_0001,
|
2020-02-14 13:58:48 -08:00
|
|
|
#[error("Entrypoint was not found in the module")]
|
2020-04-15 09:41:29 -07:00
|
|
|
EntrypointNotFound = 0x0aaa_0002,
|
2020-02-14 13:58:48 -08:00
|
|
|
#[error("Failed to load the module")]
|
2020-04-15 09:41:29 -07:00
|
|
|
FailedToLoad = 0x0aaa_0003,
|
2020-02-14 13:58:48 -08:00
|
|
|
}
|
|
|
|
impl<T> DecodeError<T> for NativeLoaderError {
|
|
|
|
fn type_of() -> &'static str {
|
|
|
|
"NativeLoaderError"
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 22:13:44 -07:00
|
|
|
|
2018-12-10 09:31:17 -08:00
|
|
|
/// Dynamic link library prefixes
|
2018-09-23 22:13:44 -07:00
|
|
|
#[cfg(unix)]
|
2020-04-15 09:41:29 -07:00
|
|
|
const PLATFORM_FILE_PREFIX: &str = "lib";
|
2018-09-23 22:13:44 -07:00
|
|
|
#[cfg(windows)]
|
2020-04-15 09:41:29 -07:00
|
|
|
const PLATFORM_FILE_PREFIX: &str = "";
|
2018-10-04 09:44:44 -07:00
|
|
|
|
2018-09-23 22:13:44 -07:00
|
|
|
/// 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";
|
2018-09-23 22:13:44 -07:00
|
|
|
/// 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";
|
2018-09-23 22:13:44 -07:00
|
|
|
/// 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";
|
2018-10-04 09:44:44 -07:00
|
|
|
|
2020-04-15 09:41:29 -07:00
|
|
|
pub type ProgramSymbolCache = RwLock<HashMap<String, Symbol<ProgramEntrypoint>>>;
|
|
|
|
pub type LoaderSymbolCache = RwLock<HashMap<String, Symbol<LoaderEntrypoint>>>;
|
2019-05-21 21:34:51 -07:00
|
|
|
|
2020-04-15 09:41:29 -07:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct NativeLoader {
|
|
|
|
program_symbol_cache: ProgramSymbolCache,
|
|
|
|
loader_symbol_cache: LoaderSymbolCache,
|
|
|
|
}
|
|
|
|
impl NativeLoader {
|
2020-10-12 13:40:04 -07:00
|
|
|
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!(
|
2020-04-15 09:41:29 -07:00
|
|
|
"create_path(\"{}\"): no parent directory of {:?}",
|
2020-10-12 13:40:04 -07:00
|
|
|
name, current_exe
|
|
|
|
);
|
|
|
|
InstructionError::from(NativeLoaderError::FailedToLoad)
|
|
|
|
})?);
|
2020-04-15 09:41:29 -07:00
|
|
|
|
|
|
|
let library_file_name = PathBuf::from(PLATFORM_FILE_PREFIX.to_string() + name)
|
|
|
|
.with_extension(PLATFORM_FILE_EXTENSION);
|
2018-10-04 09:44:44 -07:00
|
|
|
|
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() {
|
2020-10-12 13:40:04 -07:00
|
|
|
Ok(file_path)
|
2020-04-15 09:41:29 -07:00
|
|
|
} else {
|
|
|
|
// `cargo build` places dependent libraries in the deps/ subdirectory
|
2020-10-12 13:40:04 -07:00
|
|
|
Ok(current_exe_directory.join("deps").join(library_file_name))
|
2020-04-15 09:41:29 -07:00
|
|
|
}
|
2018-12-08 10:43:02 -08:00
|
|
|
}
|
2018-10-04 09:44:44 -07:00
|
|
|
|
2020-04-15 09:41:29 -07:00
|
|
|
#[cfg(windows)]
|
2021-01-23 11:55:15 -08:00
|
|
|
fn library_open(path: &Path) -> 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))]
|
2021-01-23 11:55:15 -08:00
|
|
|
fn library_open(path: &Path) -> 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
|
|
|
|
2020-04-20 16:46:06 -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 {
|
2020-10-12 13:40:04 -07:00
|
|
|
match Self::library_open(&Self::create_path(&name)?) {
|
2020-04-15 09:41:29 -07:00
|
|
|
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-10-12 13:40:04 -07:00
|
|
|
error!("Unable to find program entrypoint in {:?}: {:?})", name, e);
|
|
|
|
Err(NativeLoaderError::EntrypointNotFound.into())
|
2020-04-15 09:41:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2020-10-12 13:40:04 -07:00
|
|
|
error!("Failed to load: {:?}", e);
|
|
|
|
Err(NativeLoaderError::FailedToLoad.into())
|
2020-04-15 09:41:29 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-23 17:25:03 -07:00
|
|
|
}
|
2019-07-16 10:45:32 -06:00
|
|
|
}
|
2020-04-15 09:41:29 -07:00
|
|
|
|
|
|
|
pub fn process_instruction(
|
|
|
|
&self,
|
2021-01-11 14:36:52 -08:00
|
|
|
program_id: &Pubkey,
|
2020-04-15 09:41:29 -07:00
|
|
|
instruction_data: &[u8],
|
2021-04-19 18:48:48 +02:00
|
|
|
invoke_context: &mut dyn InvokeContext,
|
2020-04-15 09:41:29 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2021-04-19 18:48:48 +02:00
|
|
|
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(),
|
2021-04-20 16:41:16 -05:00
|
|
|
&program.try_account_ref()?.data().to_vec(),
|
2021-04-19 18:48:48 +02:00
|
|
|
)
|
|
|
|
};
|
2021-01-11 14:36:52 -08:00
|
|
|
|
2021-04-19 18:48:48 +02:00
|
|
|
let name = match str::from_utf8(name_vec) {
|
2020-04-15 09:41:29 -07:00
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
2020-10-12 13:40:04 -07:00
|
|
|
error!("Invalid UTF-8 sequence: {}", e);
|
|
|
|
return Err(NativeLoaderError::InvalidAccountData.into());
|
2020-04-15 09:41:29 -07:00
|
|
|
}
|
|
|
|
};
|
2020-10-12 13:40:04 -07:00
|
|
|
if name.is_empty() || name.starts_with('\0') {
|
|
|
|
error!("Empty name string");
|
|
|
|
return Err(NativeLoaderError::InvalidAccountData.into());
|
|
|
|
}
|
2020-04-15 09:41:29 -07:00
|
|
|
trace!("Call native {:?}", name);
|
2021-04-19 18:48:48 +02:00
|
|
|
invoke_context.remove_first_keyed_account()?;
|
2020-04-15 09:41:29 -07:00
|
|
|
if name.ends_with("loader_program") {
|
|
|
|
let entrypoint =
|
2020-04-20 16:46:06 -07:00
|
|
|
Self::get_entrypoint::<LoaderEntrypoint>(name, &self.loader_symbol_cache)?;
|
2021-04-19 18:48:48 +02:00
|
|
|
unsafe { entrypoint(&program_id, instruction_data, invoke_context) }
|
|
|
|
} else {
|
|
|
|
let entrypoint =
|
|
|
|
Self::get_entrypoint::<ProgramEntrypoint>(name, &self.program_symbol_cache)?;
|
2020-04-28 14:33:56 -07:00
|
|
|
unsafe {
|
|
|
|
entrypoint(
|
2021-04-19 18:48:48 +02:00
|
|
|
&program_id,
|
|
|
|
invoke_context.get_keyed_accounts()?,
|
2020-04-28 14:33:56 -07:00
|
|
|
instruction_data,
|
|
|
|
)
|
|
|
|
}
|
2018-10-04 09:44:44 -07:00
|
|
|
}
|
|
|
|
}
|
2018-09-23 22:13:44 -07:00
|
|
|
}
|