2018-10-16 16:33:31 -07:00
|
|
|
pub mod bpf_verifier;
|
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
extern crate bincode;
|
|
|
|
extern crate byteorder;
|
2018-10-16 16:33:31 -07:00
|
|
|
extern crate env_logger;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2018-11-06 14:28:46 -08:00
|
|
|
extern crate libc;
|
2018-10-31 10:59:56 -07:00
|
|
|
extern crate solana_rbpf;
|
2018-11-13 18:38:51 -08:00
|
|
|
#[macro_use]
|
2018-10-25 11:13:08 -07:00
|
|
|
extern crate solana_sdk;
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2018-10-19 18:28:38 -07:00
|
|
|
use bincode::deserialize;
|
2018-10-16 09:43:49 -07:00
|
|
|
use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
|
2018-11-06 14:28:46 -08:00
|
|
|
use libc::c_char;
|
|
|
|
use solana_rbpf::EbpfVmRaw;
|
2018-12-03 14:01:02 -08:00
|
|
|
use solana_sdk::account::{Account, KeyedAccount};
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::loader_instruction::LoaderInstruction;
|
2018-12-03 14:01:02 -08:00
|
|
|
use solana_sdk::native_loader;
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2018-11-06 14:28:46 -08:00
|
|
|
use std::ffi::CStr;
|
2018-10-16 09:43:49 -07:00
|
|
|
use std::io::prelude::*;
|
2018-11-06 14:28:46 -08:00
|
|
|
use std::io::{Error, ErrorKind};
|
2018-10-16 09:43:49 -07:00
|
|
|
use std::mem;
|
2018-10-16 16:33:31 -07:00
|
|
|
use std::sync::{Once, ONCE_INIT};
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2018-12-03 14:01:02 -08:00
|
|
|
const BPF_LOADER_NAME: &str = "solana_bpf_loader";
|
|
|
|
const BPF_LOADER_PROGRAM_ID: [u8; 32] = [
|
|
|
|
128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0,
|
|
|
|
];
|
|
|
|
|
|
|
|
pub fn id() -> Pubkey {
|
|
|
|
Pubkey::new(&BPF_LOADER_PROGRAM_ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn account() -> Account {
|
|
|
|
Account {
|
|
|
|
tokens: 1,
|
|
|
|
owner: id(),
|
|
|
|
userdata: BPF_LOADER_NAME.as_bytes().to_vec(),
|
|
|
|
executable: true,
|
|
|
|
loader: native_loader::id(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 14:28:46 -08:00
|
|
|
// TODO use rbpf's disassemble
|
2018-10-16 09:43:49 -07:00
|
|
|
#[allow(dead_code)]
|
2018-10-25 02:58:04 -07:00
|
|
|
fn dump_program(key: &Pubkey, prog: &[u8]) {
|
2018-10-16 09:43:49 -07:00
|
|
|
let mut eight_bytes: Vec<u8> = Vec::new();
|
2018-10-30 08:39:10 -07:00
|
|
|
info!("BPF Program: {:?}", key);
|
2018-10-16 09:43:49 -07:00
|
|
|
for i in prog.iter() {
|
|
|
|
if eight_bytes.len() >= 7 {
|
2018-10-30 08:39:10 -07:00
|
|
|
info!("{:02X?}", eight_bytes);
|
2018-10-16 09:43:49 -07:00
|
|
|
eight_bytes.clear();
|
|
|
|
} else {
|
|
|
|
eight_bytes.push(i.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 14:28:46 -08:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
pub fn helper_sol_log_verify(
|
|
|
|
addr: u64,
|
|
|
|
unused2: u64,
|
|
|
|
unused3: u64,
|
|
|
|
unused4: u64,
|
|
|
|
unused5: u64,
|
|
|
|
ro_regions: &[&[u8]],
|
|
|
|
unused7: &[&[u8]],
|
|
|
|
) -> Result<(()), Error> {
|
|
|
|
for region in ro_regions.iter() {
|
|
|
|
if region.as_ptr() as u64 <= addr
|
|
|
|
&& addr as u64 <= region.as_ptr() as u64 + region.len() as u64
|
|
|
|
{
|
|
|
|
let c_buf: *const c_char = addr as *const c_char;
|
|
|
|
let max_size = (region.as_ptr() as u64 + region.len() as u64) - addr;
|
|
|
|
unsafe {
|
|
|
|
for i in 0..max_size {
|
|
|
|
if std::ptr::read(c_buf.offset(i as isize)) == 0 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Err(Error::new(ErrorKind::Other, "Error, Unterminated string"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
"Error: Load segfault, bad string pointer",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-11-13 19:54:41 -08:00
|
|
|
pub fn helper_sol_log(addr: u64, _arg2: u64, _arg3: u64, _arg4: u64, _arg5: u64) -> u64 {
|
2018-11-06 14:28:46 -08:00
|
|
|
let c_buf: *const c_char = addr as *const c_char;
|
|
|
|
let c_str: &CStr = unsafe { CStr::from_ptr(c_buf) };
|
|
|
|
match c_str.to_str() {
|
|
|
|
Ok(slice) => info!("sol_log: {:?}", slice),
|
2018-11-13 19:54:41 -08:00
|
|
|
Err(e) => warn!("Error: Cannot print invalid string: {}", e),
|
2018-11-06 14:28:46 -08:00
|
|
|
};
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn helper_sol_log_u64(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) -> u64 {
|
2018-10-30 08:39:10 -07:00
|
|
|
info!(
|
2018-11-06 14:28:46 -08:00
|
|
|
"sol_log_u64: {:#x}, {:#x}, {:#x}, {:#x}, {:#x}",
|
2018-10-26 19:38:07 -07:00
|
|
|
arg1, arg2, arg3, arg4, arg5
|
|
|
|
);
|
2018-11-06 14:28:46 -08:00
|
|
|
0
|
2018-10-26 19:38:07 -07:00
|
|
|
}
|
|
|
|
|
2018-11-14 12:06:06 -08:00
|
|
|
pub fn create_vm(prog: &[u8]) -> Result<EbpfVmRaw, Error> {
|
2018-10-31 10:59:56 -07:00
|
|
|
let mut vm = EbpfVmRaw::new(None)?;
|
2018-10-26 19:38:07 -07:00
|
|
|
vm.set_verifier(bpf_verifier::check)?;
|
2018-10-31 10:59:56 -07:00
|
|
|
vm.set_max_instruction_count(36000)?; // 36000 is a wag, need to tune
|
2018-11-06 14:28:46 -08:00
|
|
|
vm.set_elf(&prog)?;
|
|
|
|
vm.register_helper_ex("sol_log", Some(helper_sol_log_verify), helper_sol_log)?;
|
|
|
|
vm.register_helper_ex("sol_log_64", None, helper_sol_log_u64)?;
|
2018-10-26 19:38:07 -07:00
|
|
|
Ok(vm)
|
|
|
|
}
|
|
|
|
|
2018-11-13 19:54:41 -08:00
|
|
|
fn serialize_parameters(
|
2018-11-17 17:02:14 -08:00
|
|
|
program_id: &Pubkey,
|
2018-11-13 19:54:41 -08:00
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
data: &[u8],
|
|
|
|
tick_height: u64,
|
|
|
|
) -> Vec<u8> {
|
2018-10-16 09:43:49 -07:00
|
|
|
assert_eq!(32, mem::size_of::<Pubkey>());
|
|
|
|
|
|
|
|
let mut v: Vec<u8> = Vec::new();
|
|
|
|
v.write_u64::<LittleEndian>(keyed_accounts.len() as u64)
|
|
|
|
.unwrap();
|
|
|
|
for info in keyed_accounts.iter_mut() {
|
2018-11-29 12:32:16 -08:00
|
|
|
v.write_u64::<LittleEndian>(info.signer_key().is_some() as u64)
|
|
|
|
.unwrap();
|
|
|
|
v.write_all(info.unsigned_key().as_ref()).unwrap();
|
2018-11-05 08:36:22 -08:00
|
|
|
v.write_u64::<LittleEndian>(info.account.tokens).unwrap();
|
2018-10-16 09:43:49 -07:00
|
|
|
v.write_u64::<LittleEndian>(info.account.userdata.len() as u64)
|
|
|
|
.unwrap();
|
|
|
|
v.write_all(&info.account.userdata).unwrap();
|
2018-11-12 09:29:17 -08:00
|
|
|
v.write_all(info.account.owner.as_ref()).unwrap();
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
|
|
|
v.write_u64::<LittleEndian>(data.len() as u64).unwrap();
|
|
|
|
v.write_all(data).unwrap();
|
2018-11-13 19:54:41 -08:00
|
|
|
v.write_u64::<LittleEndian>(tick_height).unwrap();
|
2018-11-17 17:02:14 -08:00
|
|
|
v.write_all(program_id.as_ref()).unwrap();
|
2018-10-16 09:43:49 -07:00
|
|
|
v
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:28:38 -07:00
|
|
|
fn deserialize_parameters(keyed_accounts: &mut [KeyedAccount], buffer: &[u8]) {
|
2018-10-16 09:43:49 -07:00
|
|
|
assert_eq!(32, mem::size_of::<Pubkey>());
|
|
|
|
|
|
|
|
let mut start = mem::size_of::<u64>();
|
|
|
|
for info in keyed_accounts.iter_mut() {
|
2018-11-29 12:32:16 -08:00
|
|
|
start += mem::size_of::<u64>(); // skip signer_key boolean
|
2018-10-16 09:43:49 -07:00
|
|
|
start += mem::size_of::<Pubkey>(); // skip pubkey
|
2018-11-05 08:36:22 -08:00
|
|
|
info.account.tokens = LittleEndian::read_u64(&buffer[start..]);
|
2018-10-16 09:43:49 -07:00
|
|
|
|
|
|
|
start += mem::size_of::<u64>() // skip tokens
|
|
|
|
+ mem::size_of::<u64>(); // skip length tag
|
|
|
|
let end = start + info.account.userdata.len();
|
|
|
|
info.account.userdata.clone_from_slice(&buffer[start..end]);
|
|
|
|
|
|
|
|
start += info.account.userdata.len() // skip userdata
|
2018-11-12 09:29:17 -08:00
|
|
|
+ mem::size_of::<Pubkey>(); // skip owner
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 18:38:51 -08:00
|
|
|
solana_entrypoint!(entrypoint);
|
2018-11-17 17:02:14 -08:00
|
|
|
fn entrypoint(
|
|
|
|
program_id: &Pubkey,
|
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
tx_data: &[u8],
|
|
|
|
tick_height: u64,
|
|
|
|
) -> bool {
|
2018-10-16 16:33:31 -07:00
|
|
|
static INIT: Once = ONCE_INIT;
|
|
|
|
INIT.call_once(|| {
|
|
|
|
// env_logger can only be initialized once
|
|
|
|
env_logger::init();
|
|
|
|
});
|
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
if keyed_accounts[0].account.executable {
|
2018-10-19 18:28:38 -07:00
|
|
|
let prog = keyed_accounts[0].account.userdata.clone();
|
2018-10-31 10:59:56 -07:00
|
|
|
trace!("Call BPF, {} instructions", prog.len() / 8);
|
2018-10-25 02:58:04 -07:00
|
|
|
//dump_program(keyed_accounts[0].key, &prog);
|
2018-10-31 10:59:56 -07:00
|
|
|
let mut vm = match create_vm(&prog) {
|
2018-10-16 13:13:54 -07:00
|
|
|
Ok(vm) => vm,
|
|
|
|
Err(e) => {
|
2018-10-30 08:39:10 -07:00
|
|
|
warn!("create_vm failed: {}", e);
|
2018-10-16 13:13:54 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2018-11-17 17:02:14 -08:00
|
|
|
let mut v =
|
|
|
|
serialize_parameters(program_id, &mut keyed_accounts[1..], &tx_data, tick_height);
|
2018-10-25 02:58:04 -07:00
|
|
|
match vm.execute_program(v.as_mut_slice()) {
|
2018-11-02 14:32:05 -07:00
|
|
|
Ok(status) => {
|
|
|
|
if 0 == status {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 02:58:04 -07:00
|
|
|
Err(e) => {
|
2018-10-30 08:39:10 -07:00
|
|
|
warn!("execute_program failed: {}", e);
|
2018-10-25 02:58:04 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
2018-10-19 18:28:38 -07:00
|
|
|
deserialize_parameters(&mut keyed_accounts[1..], &v);
|
2018-10-31 10:59:56 -07:00
|
|
|
trace!(
|
|
|
|
"BPF program executed {} instructions",
|
|
|
|
vm.get_last_instruction_count()
|
|
|
|
);
|
2018-10-16 09:43:49 -07:00
|
|
|
} else if let Ok(instruction) = deserialize(tx_data) {
|
2018-11-29 12:32:16 -08:00
|
|
|
if keyed_accounts[0].signer_key().is_none() {
|
|
|
|
warn!("key[0] did not sign the transaction");
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-16 09:43:49 -07:00
|
|
|
match instruction {
|
|
|
|
LoaderInstruction::Write { offset, bytes } => {
|
|
|
|
let offset = offset as usize;
|
2018-10-19 18:28:38 -07:00
|
|
|
let len = bytes.len();
|
2018-10-30 08:39:10 -07:00
|
|
|
debug!("Write: offset={} length={}", offset, len);
|
2018-10-19 18:28:38 -07:00
|
|
|
if keyed_accounts[0].account.userdata.len() < offset + len {
|
2018-10-30 08:39:10 -07:00
|
|
|
warn!(
|
|
|
|
"Write overflow: {} < {}",
|
2018-10-19 18:28:38 -07:00
|
|
|
keyed_accounts[0].account.userdata.len(),
|
|
|
|
offset + len
|
|
|
|
);
|
2018-10-16 09:43:49 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-19 18:28:38 -07:00
|
|
|
keyed_accounts[0].account.userdata[offset..offset + len].copy_from_slice(&bytes);
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
|
|
|
LoaderInstruction::Finalize => {
|
|
|
|
keyed_accounts[0].account.executable = true;
|
2018-11-29 12:32:16 -08:00
|
|
|
info!(
|
|
|
|
"Finalize: account {:?}",
|
|
|
|
keyed_accounts[0].signer_key().unwrap()
|
|
|
|
);
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warn!("Invalid program transaction: {:?}", tx_data);
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2018-10-31 10:59:56 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-11-06 14:28:46 -08:00
|
|
|
use solana_rbpf::helpers;
|
|
|
|
|
2018-10-31 10:59:56 -07:00
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "Error: Execution exceeded maximum number of instructions")]
|
|
|
|
fn test_non_terminating_program() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let prog = &[
|
|
|
|
0xb7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r6 = 0
|
|
|
|
0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r1 = 0
|
|
|
|
0xb7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r2 = 0
|
|
|
|
0xb7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r3 = 0
|
|
|
|
0xb7, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r4 = 0
|
|
|
|
0xbf, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // r5 = r6
|
|
|
|
0x85, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, // call 6
|
|
|
|
0x07, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // r6 + 1
|
|
|
|
0x05, 0x00, 0xf8, 0xff, 0x00, 0x00, 0x00, 0x00, // goto -8
|
|
|
|
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
|
|
|
|
];
|
|
|
|
let input = &mut [0x00];
|
2018-11-06 14:28:46 -08:00
|
|
|
|
|
|
|
let mut vm = EbpfVmRaw::new(None).unwrap();
|
|
|
|
vm.set_verifier(bpf_verifier::check).unwrap();
|
|
|
|
vm.set_max_instruction_count(36000).unwrap(); // 36000 is a wag, need to tune
|
|
|
|
vm.set_program(prog).unwrap();
|
|
|
|
vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf)
|
|
|
|
.unwrap();
|
2018-10-31 10:59:56 -07:00
|
|
|
vm.execute_program(input).unwrap();
|
|
|
|
}
|
|
|
|
}
|