2018-10-16 09:43:49 -07:00
|
|
|
use bincode::deserialize;
|
2018-09-23 22:13:44 -07:00
|
|
|
use libc;
|
2018-10-04 09:44:44 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
use libloading::os::unix::*;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use libloading::os::windows::*;
|
2018-09-27 07:49:26 -07:00
|
|
|
use solana_program_interface::account::KeyedAccount;
|
2018-10-16 09:43:49 -07:00
|
|
|
use solana_program_interface::loader_instruction::LoaderInstruction;
|
2018-10-04 09:44:44 -07:00
|
|
|
use solana_program_interface::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-10-04 09:44:44 -07:00
|
|
|
/// Dynamic link library prefixs
|
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 {
|
|
|
|
let pathbuf = {
|
|
|
|
let current_exe = env::current_exe().unwrap();
|
|
|
|
PathBuf::from(current_exe.parent().unwrap())
|
|
|
|
};
|
2018-10-04 09:44:44 -07:00
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
pathbuf.join(
|
|
|
|
PathBuf::from(PLATFORM_FILE_PREFIX_NATIVE.to_string() + name)
|
|
|
|
.with_extension(PLATFORM_FILE_EXTENSION_NATIVE),
|
|
|
|
)
|
2018-10-04 09:44:44 -07:00
|
|
|
}
|
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
pub const NATIVE_PROGRAM_ID: [u8; 32] = [2u8; 32];
|
2018-09-23 22:13:44 -07:00
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
// All native programs export a symbol named process()
|
2018-09-23 22:13:44 -07:00
|
|
|
const ENTRYPOINT: &str = "process";
|
2018-10-16 09:43:49 -07:00
|
|
|
type Entrypoint = unsafe extern "C" fn(keyed_accounts: &mut [KeyedAccount], data: &[u8]) -> bool;
|
2018-09-23 22:13:44 -07:00
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
pub fn check_id(program_id: &Pubkey) -> bool {
|
|
|
|
program_id.as_ref() == NATIVE_PROGRAM_ID
|
2018-09-23 22:13:44 -07:00
|
|
|
}
|
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
pub fn id() -> Pubkey {
|
|
|
|
Pubkey::new(&NATIVE_PROGRAM_ID)
|
|
|
|
}
|
2018-10-04 09:44:44 -07:00
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
pub fn process_transaction(keyed_accounts: &mut [KeyedAccount], tx_data: &[u8]) -> bool {
|
|
|
|
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);
|
|
|
|
return false;
|
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
|
|
|
|
let library = Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW).unwrap();
|
|
|
|
unsafe {
|
|
|
|
let entrypoint: Symbol<Entrypoint> = match library.get(ENTRYPOINT.as_bytes()) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("{:?}: Unable to find {:?} in program", e, ENTRYPOINT);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return entrypoint(&mut keyed_accounts[1..], tx_data);
|
2018-09-23 22:13:44 -07:00
|
|
|
}
|
2018-10-16 09:43:49 -07:00
|
|
|
} else if let Ok(instruction) = deserialize(tx_data) {
|
|
|
|
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
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 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-10-18 10:33:30 -07:00
|
|
|
trace!("NativeLoader::Finalize prog: {:?}", keyed_accounts[0].key);
|
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 {
|
|
|
|
warn!("Invalid program transaction: {:?}", tx_data);
|
2018-10-04 09:44:44 -07:00
|
|
|
}
|
2018-10-18 10:33:30 -07:00
|
|
|
true
|
2018-09-23 22:13:44 -07:00
|
|
|
}
|