2018-10-23 14:44:41 -07:00
|
|
|
//! Native loader
|
2018-10-16 09:43:49 -07:00
|
|
|
use bincode::deserialize;
|
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::*;
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::account::KeyedAccount;
|
|
|
|
use solana_sdk::loader_instruction::LoaderInstruction;
|
2018-12-03 13:31:11 -08:00
|
|
|
pub use solana_sdk::native_loader::*;
|
2018-11-13 18:17:32 -08:00
|
|
|
use solana_sdk::native_program;
|
2018-12-03 13:32:31 -08:00
|
|
|
use solana_sdk::native_program::ProgramError;
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-10-16 09:43:49 -07:00
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str;
|
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)]
|
2018-10-04 09:44:44 -07:00
|
|
|
const PLATFORM_FILE_PREFIX_NATIVE: &str = "lib";
|
2018-09-23 22:13:44 -07:00
|
|
|
#[cfg(windows)]
|
2018-10-04 09:44:44 -07:00
|
|
|
const PLATFORM_FILE_PREFIX_NATIVE: &str = "";
|
|
|
|
|
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"))]
|
2018-10-04 09:44:44 -07:00
|
|
|
const PLATFORM_FILE_EXTENSION_NATIVE: &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"))))]
|
2018-10-04 09:44:44 -07:00
|
|
|
const PLATFORM_FILE_EXTENSION_NATIVE: &str = "so";
|
2018-09-23 22:13:44 -07:00
|
|
|
/// Dynamic link library file extension specific to the platform
|
|
|
|
#[cfg(windows)]
|
2018-10-04 09:44:44 -07:00
|
|
|
const PLATFORM_FILE_EXTENSION_NATIVE: &str = "dll";
|
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
fn create_path(name: &str) -> PathBuf {
|
2018-12-08 10:43:02 -08:00
|
|
|
let current_exe = env::current_exe().unwrap();
|
|
|
|
let current_exe_directory = PathBuf::from(current_exe.parent().unwrap());
|
|
|
|
let library_file_name = PathBuf::from(PLATFORM_FILE_PREFIX_NATIVE.to_string() + name)
|
|
|
|
.with_extension(PLATFORM_FILE_EXTENSION_NATIVE);
|
2018-10-04 09:44:44 -07:00
|
|
|
|
2018-12-08 10:43:02 -08: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)
|
|
|
|
}
|
2018-10-04 09:44:44 -07:00
|
|
|
}
|
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
pub fn check_id(program_id: &Pubkey) -> bool {
|
2018-10-23 14:44:41 -07:00
|
|
|
program_id.as_ref() == NATIVE_LOADER_PROGRAM_ID
|
2018-09-23 22:13:44 -07:00
|
|
|
}
|
|
|
|
|
2018-12-05 11:41:21 -08:00
|
|
|
pub fn entrypoint(
|
2018-11-17 17:02:14 -08:00
|
|
|
program_id: &Pubkey,
|
2018-11-13 18:17:32 -08:00
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
ix_userdata: &[u8],
|
|
|
|
tick_height: u64,
|
2018-12-03 13:32:31 -08:00
|
|
|
) -> Result<(), ProgramError> {
|
2018-10-16 09:43:49 -07:00
|
|
|
if keyed_accounts[0].account.executable {
|
|
|
|
// dispatch it
|
|
|
|
let name = keyed_accounts[0].account.userdata.clone();
|
|
|
|
let name = match str::from_utf8(&name) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Invalid UTF-8 sequence: {}", e);
|
2018-12-03 13:32:31 -08:00
|
|
|
return Err(ProgramError::GenericError);
|
2018-10-04 09:44:44 -07:00
|
|
|
}
|
2018-10-16 09:43:49 -07:00
|
|
|
};
|
|
|
|
trace!("Call native {:?}", name);
|
2018-10-18 10:33:30 -07:00
|
|
|
let path = create_path(&name);
|
|
|
|
// TODO linux tls bug can cause crash on dlclose(), workaround by never unloading
|
2018-10-30 11:41:23 -07:00
|
|
|
match Library::open(Some(&path), libc::RTLD_NODELETE | libc::RTLD_NOW) {
|
|
|
|
Ok(library) => unsafe {
|
2018-11-13 18:17:32 -08:00
|
|
|
let entrypoint: Symbol<native_program::Entrypoint> =
|
|
|
|
match library.get(native_program::ENTRYPOINT.as_bytes()) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
|
|
|
warn!(
|
|
|
|
"{:?}: Unable to find {:?} in program",
|
|
|
|
e,
|
|
|
|
native_program::ENTRYPOINT
|
|
|
|
);
|
2018-12-03 13:32:31 -08:00
|
|
|
return Err(ProgramError::GenericError);
|
2018-11-13 18:17:32 -08:00
|
|
|
}
|
|
|
|
};
|
2018-11-17 17:02:14 -08:00
|
|
|
return entrypoint(
|
|
|
|
program_id,
|
|
|
|
&mut keyed_accounts[1..],
|
|
|
|
ix_userdata,
|
|
|
|
tick_height,
|
|
|
|
);
|
2018-10-30 11:41:23 -07:00
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Unable to load: {:?}", e);
|
2018-12-03 13:32:31 -08:00
|
|
|
return Err(ProgramError::GenericError);
|
2018-10-30 11:41:23 -07:00
|
|
|
}
|
2018-09-23 22:13:44 -07:00
|
|
|
}
|
2018-11-11 20:50:03 -07:00
|
|
|
} else if let Ok(instruction) = deserialize(ix_userdata) {
|
2018-11-29 12:32:16 -08:00
|
|
|
if keyed_accounts[0].signer_key().is_none() {
|
|
|
|
warn!("key[0] did not sign the transaction");
|
2018-12-03 13:32:31 -08:00
|
|
|
return Err(ProgramError::GenericError);
|
2018-11-29 12:32:16 -08:00
|
|
|
}
|
2018-10-16 09:43:49 -07:00
|
|
|
match instruction {
|
|
|
|
LoaderInstruction::Write { offset, bytes } => {
|
|
|
|
trace!("NativeLoader::Write offset {} bytes {:?}", offset, bytes);
|
|
|
|
let offset = offset as usize;
|
2018-10-19 18:28:38 -07:00
|
|
|
if keyed_accounts[0].account.userdata.len() < offset + bytes.len() {
|
2018-10-16 09:43:49 -07:00
|
|
|
warn!(
|
2018-10-19 18:28:38 -07:00
|
|
|
"Error: Overflow, {} < {}",
|
|
|
|
keyed_accounts[0].account.userdata.len(),
|
|
|
|
offset + bytes.len()
|
2018-10-16 09:43:49 -07:00
|
|
|
);
|
2018-12-03 13:32:31 -08:00
|
|
|
return Err(ProgramError::GenericError);
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
|
|
|
// native loader takes a name and we assume it all comes in at once
|
|
|
|
keyed_accounts[0].account.userdata = bytes;
|
|
|
|
}
|
2018-10-04 09:44:44 -07:00
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
LoaderInstruction::Finalize => {
|
|
|
|
keyed_accounts[0].account.executable = true;
|
2018-11-29 12:32:16 -08:00
|
|
|
trace!(
|
|
|
|
"NativeLoader::Finalize prog: {:?}",
|
|
|
|
keyed_accounts[0].signer_key().unwrap()
|
|
|
|
);
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
2018-10-04 09:44:44 -07:00
|
|
|
}
|
2018-10-16 09:43:49 -07:00
|
|
|
} else {
|
2018-11-11 20:50:03 -07:00
|
|
|
warn!("Invalid userdata in instruction: {:?}", ix_userdata);
|
2018-12-03 13:32:31 -08:00
|
|
|
return Err(ProgramError::GenericError);
|
2018-10-04 09:44:44 -07:00
|
|
|
}
|
2018-12-03 13:32:31 -08:00
|
|
|
Ok(())
|
2018-09-23 22:13:44 -07:00
|
|
|
}
|