2019-05-24 16:21:42 -07:00
|
|
|
pub mod alloc;
|
|
|
|
pub mod allocator_bump;
|
|
|
|
pub mod allocator_system;
|
2018-10-16 16:33:31 -07:00
|
|
|
pub mod bpf_verifier;
|
|
|
|
|
2019-06-11 10:27:22 -07:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! solana_bpf_loader {
|
|
|
|
() => {
|
|
|
|
(
|
|
|
|
"solana_bpf_loader".to_string(),
|
|
|
|
solana_sdk::bpf_loader::id(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-24 16:21:42 -07:00
|
|
|
use alloc::Alloc;
|
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;
|
2018-12-14 20:39:10 -08:00
|
|
|
use log::*;
|
2019-01-15 00:42:30 -08:00
|
|
|
use solana_rbpf::{EbpfVmRaw, MemoryRegion};
|
2018-12-03 13:32:31 -08:00
|
|
|
use solana_sdk::account::KeyedAccount;
|
2019-03-23 20:12:27 -07:00
|
|
|
use solana_sdk::instruction::InstructionError;
|
2018-10-25 11:13:08 -07:00
|
|
|
use solana_sdk::loader_instruction::LoaderInstruction;
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-05-24 16:21:42 -07:00
|
|
|
use std::alloc::Layout;
|
|
|
|
use std::any::Any;
|
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;
|
2019-06-20 19:09:50 -07:00
|
|
|
use std::slice::from_raw_parts;
|
|
|
|
use std::str::from_utf8;
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2019-05-24 16:21:42 -07:00
|
|
|
/// Program heap allocators are intended to allocate/free from a given
|
|
|
|
/// chunk of memory. The specific allocator implementation is
|
|
|
|
/// selectable at build-time.
|
|
|
|
/// Enable only one of the following BPFAllocator implementations.
|
|
|
|
|
|
|
|
/// Simple bump allocator, never frees
|
|
|
|
use allocator_bump::BPFAllocator;
|
|
|
|
|
|
|
|
/// Use the system heap (test purposes only). This allocator relies on the system heap
|
|
|
|
/// and there is no mechanism to check read-write access privileges
|
|
|
|
/// at the moment. Therefor you must disable memory bounds checking
|
|
|
|
// use allocator_system::BPFAllocator;
|
|
|
|
|
|
|
|
/// Default program heap size, allocators
|
|
|
|
/// are expected to enforce this
|
|
|
|
const DEFAULT_HEAP_SIZE: usize = 32 * 1024;
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2019-06-10 11:00:15 -07:00
|
|
|
/// Verifies a string passed out of the program
|
|
|
|
fn verify_string(addr: u64, ro_regions: &[MemoryRegion]) -> Result<(()), Error> {
|
|
|
|
for region in ro_regions.iter() {
|
|
|
|
if region.addr <= addr && (addr as u64) < region.addr + region.len {
|
|
|
|
let c_buf: *const c_char = addr as *const c_char;
|
|
|
|
let max_size = region.addr + region.len - 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",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2019-05-24 16:21:42 -07:00
|
|
|
/// Abort helper functions, called when the BPF program calls `abort()`
|
2019-06-10 11:00:15 -07:00
|
|
|
/// The verify function returns an error which will cause the BPF program
|
|
|
|
/// to be halted immediately
|
2019-02-26 15:17:38 -08:00
|
|
|
pub fn helper_abort_verify(
|
|
|
|
_arg1: u64,
|
|
|
|
_arg2: u64,
|
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
2019-05-24 16:21:42 -07:00
|
|
|
_context: &mut Option<Box<Any + 'static>>,
|
2019-02-26 15:17:38 -08:00
|
|
|
_ro_regions: &[MemoryRegion],
|
|
|
|
_rw_regions: &[MemoryRegion],
|
|
|
|
) -> Result<(()), Error> {
|
|
|
|
Err(Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
"Error: BPF program called abort()!",
|
|
|
|
))
|
|
|
|
}
|
2019-05-24 16:21:42 -07:00
|
|
|
pub fn helper_abort(
|
|
|
|
_arg1: u64,
|
|
|
|
_arg2: u64,
|
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
|
|
|
_context: &mut Option<Box<Any + 'static>>,
|
|
|
|
) -> u64 {
|
2019-02-26 15:17:38 -08:00
|
|
|
// Never called because its verify function always returns an error
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2019-05-24 16:21:42 -07:00
|
|
|
/// Panic helper functions, called when the BPF program calls 'sol_panic_()`
|
|
|
|
/// The verify function returns an error which will cause the BPF program
|
|
|
|
/// to be halted immediately
|
2019-02-26 15:17:38 -08:00
|
|
|
pub fn helper_sol_panic_verify(
|
2019-06-10 11:00:15 -07:00
|
|
|
file: u64,
|
|
|
|
line: u64,
|
|
|
|
column: u64,
|
2019-02-26 15:17:38 -08:00
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
2019-05-24 16:21:42 -07:00
|
|
|
_context: &mut Option<Box<Any + 'static>>,
|
2019-06-10 11:00:15 -07:00
|
|
|
ro_regions: &[MemoryRegion],
|
2019-02-26 15:17:38 -08:00
|
|
|
_rw_regions: &[MemoryRegion],
|
|
|
|
) -> Result<(()), Error> {
|
2019-06-10 11:00:15 -07:00
|
|
|
if verify_string(file, ro_regions).is_ok() {
|
|
|
|
let c_buf: *const c_char = file as *const c_char;
|
|
|
|
let c_str: &CStr = unsafe { CStr::from_ptr(c_buf) };
|
|
|
|
if let Ok(slice) = c_str.to_str() {
|
|
|
|
return Err(Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
format!(
|
|
|
|
"Error: BPF program Panicked at {}, {}:{}",
|
|
|
|
slice, line, column
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(Error::new(ErrorKind::Other, "Error: BPF program Panicked"))
|
2019-02-26 15:17:38 -08:00
|
|
|
}
|
2019-05-24 16:21:42 -07:00
|
|
|
pub fn helper_sol_panic(
|
|
|
|
_arg1: u64,
|
|
|
|
_arg2: u64,
|
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
|
|
|
_context: &mut Option<Box<Any + 'static>>,
|
|
|
|
) -> u64 {
|
2019-02-26 15:17:38 -08:00
|
|
|
// Never called because its verify function always returns an error
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2019-05-24 16:21:42 -07:00
|
|
|
/// Logging helper functions, called when the BPF program calls `sol_log_()` or
|
2019-06-20 19:09:50 -07:00
|
|
|
/// `sol_log_64_()`.
|
2018-11-06 14:28:46 -08:00
|
|
|
pub fn helper_sol_log_verify(
|
|
|
|
addr: u64,
|
2019-02-26 15:17:38 -08:00
|
|
|
_arg2: u64,
|
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
2019-05-24 16:21:42 -07:00
|
|
|
_context: &mut Option<Box<Any + 'static>>,
|
2019-01-15 00:42:30 -08:00
|
|
|
ro_regions: &[MemoryRegion],
|
2019-02-26 15:17:38 -08:00
|
|
|
_rw_regions: &[MemoryRegion],
|
2018-11-06 14:28:46 -08:00
|
|
|
) -> Result<(()), Error> {
|
2019-06-10 11:00:15 -07:00
|
|
|
verify_string(addr, ro_regions)
|
2018-11-06 14:28:46 -08:00
|
|
|
}
|
2019-05-24 16:21:42 -07:00
|
|
|
pub fn helper_sol_log(
|
|
|
|
addr: u64,
|
|
|
|
_arg2: u64,
|
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
|
|
|
_context: &mut Option<Box<Any + 'static>>,
|
|
|
|
) -> 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() {
|
2019-06-20 16:07:12 -07:00
|
|
|
Ok(slice) => info!("info!: {:?}", 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
|
|
|
|
}
|
2019-06-20 19:09:50 -07:00
|
|
|
pub fn helper_sol_log_verify_(
|
|
|
|
addr: u64,
|
|
|
|
len: u64,
|
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
|
|
|
_context: &mut Option<Box<Any + 'static>>,
|
|
|
|
ro_regions: &[MemoryRegion],
|
|
|
|
_rw_regions: &[MemoryRegion],
|
|
|
|
) -> Result<(()), Error> {
|
|
|
|
for region in ro_regions.iter() {
|
|
|
|
if region.addr <= addr && (addr as u64) + len <= region.addr + region.len {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
"Error: Load segfault, bad string pointer",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
pub fn helper_sol_log_(
|
|
|
|
addr: u64,
|
|
|
|
len: u64,
|
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
|
|
|
_context: &mut Option<Box<Any + 'static>>,
|
|
|
|
) -> u64 {
|
|
|
|
let ptr: *const u8 = addr as *const u8;
|
|
|
|
let message = unsafe { from_utf8(from_raw_parts(ptr, len as usize)) };
|
|
|
|
info!("sol_log: {:?}", message);
|
|
|
|
0
|
|
|
|
}
|
2019-05-24 16:21:42 -07:00
|
|
|
pub fn helper_sol_log_u64(
|
|
|
|
arg1: u64,
|
|
|
|
arg2: u64,
|
|
|
|
arg3: u64,
|
|
|
|
arg4: u64,
|
|
|
|
arg5: u64,
|
|
|
|
_context: &mut Option<Box<Any + 'static>>,
|
|
|
|
) -> u64 {
|
2018-10-30 08:39:10 -07:00
|
|
|
info!(
|
2019-06-20 16:07:12 -07:00
|
|
|
"info!: {:#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
|
|
|
}
|
|
|
|
|
2019-05-24 16:21:42 -07:00
|
|
|
/// Dynamic memory allocation helper called when the BPF program calls
|
|
|
|
/// `sol_alloc_free_()`. The allocator is expected to allocate/free
|
|
|
|
/// from/to a given chunk of memory and enforce size restrictions. The
|
|
|
|
/// memory chunk is given to the allocator during allocator creation and
|
|
|
|
/// information about that memory (start address and size) is passed
|
|
|
|
/// to the VM to use for enforcement.
|
|
|
|
pub fn helper_sol_alloc_free(
|
|
|
|
size: u64,
|
|
|
|
free_ptr: u64,
|
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
|
|
|
context: &mut Option<Box<Any + 'static>>,
|
|
|
|
) -> u64 {
|
|
|
|
if let Some(context) = context {
|
|
|
|
if let Some(allocator) = context.downcast_mut::<BPFAllocator>() {
|
|
|
|
return {
|
|
|
|
let layout = Layout::from_size_align(size as usize, mem::align_of::<u8>()).unwrap();
|
|
|
|
if free_ptr == 0 {
|
|
|
|
match allocator.alloc(layout) {
|
|
|
|
Ok(ptr) => ptr as u64,
|
|
|
|
Err(_) => 0,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
allocator.dealloc(free_ptr as *mut u8, layout);
|
|
|
|
0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
panic!("Failed to get alloc_free context");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_vm(prog: &[u8]) -> Result<(EbpfVmRaw, MemoryRegion), 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)?;
|
2019-05-24 16:21:42 -07:00
|
|
|
vm.set_max_instruction_count(36000)?;
|
2018-11-06 14:28:46 -08:00
|
|
|
vm.set_elf(&prog)?;
|
2019-05-24 16:21:42 -07:00
|
|
|
vm.register_helper_ex("abort", Some(helper_abort_verify), helper_abort, None)?;
|
|
|
|
vm.register_helper_ex(
|
|
|
|
"sol_panic",
|
|
|
|
Some(helper_sol_panic_verify),
|
|
|
|
helper_sol_panic,
|
|
|
|
None,
|
|
|
|
)?;
|
2019-02-26 15:17:38 -08:00
|
|
|
vm.register_helper_ex(
|
|
|
|
"sol_panic_",
|
|
|
|
Some(helper_sol_panic_verify),
|
|
|
|
helper_sol_panic,
|
2019-05-24 16:21:42 -07:00
|
|
|
None,
|
|
|
|
)?;
|
|
|
|
vm.register_helper_ex("sol_log", Some(helper_sol_log_verify), helper_sol_log, None)?;
|
|
|
|
vm.register_helper_ex(
|
|
|
|
"sol_log_",
|
2019-06-20 19:09:50 -07:00
|
|
|
Some(helper_sol_log_verify_),
|
|
|
|
helper_sol_log_,
|
2019-05-24 16:21:42 -07:00
|
|
|
None,
|
2019-02-26 15:17:38 -08:00
|
|
|
)?;
|
2019-05-24 16:21:42 -07:00
|
|
|
vm.register_helper_ex("sol_log_64", None, helper_sol_log_u64, None)?;
|
|
|
|
vm.register_helper_ex("sol_log_64_", None, helper_sol_log_u64, None)?;
|
|
|
|
|
|
|
|
let heap = vec![0_u8; DEFAULT_HEAP_SIZE];
|
|
|
|
let heap_region = MemoryRegion::new_from_slice(&heap);
|
|
|
|
let context = Box::new(BPFAllocator::new(heap));
|
|
|
|
vm.register_helper_ex(
|
|
|
|
"sol_alloc_free_",
|
|
|
|
None,
|
|
|
|
helper_sol_alloc_free,
|
|
|
|
Some(context),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok((vm, heap_region))
|
2018-10-26 19:38:07 -07:00
|
|
|
}
|
|
|
|
|
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],
|
|
|
|
) -> 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();
|
2019-03-05 16:28:14 -08:00
|
|
|
v.write_u64::<LittleEndian>(info.account.lamports).unwrap();
|
2019-03-14 09:48:27 -07:00
|
|
|
v.write_u64::<LittleEndian>(info.account.data.len() as u64)
|
2018-10-16 09:43:49 -07:00
|
|
|
.unwrap();
|
2019-03-14 09:48:27 -07:00
|
|
|
v.write_all(&info.account.data).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-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
|
2019-03-05 16:28:14 -08:00
|
|
|
info.account.lamports = LittleEndian::read_u64(&buffer[start..]);
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2019-03-05 16:28:14 -08:00
|
|
|
start += mem::size_of::<u64>() // skip lamports
|
2018-10-16 09:43:49 -07:00
|
|
|
+ mem::size_of::<u64>(); // skip length tag
|
2019-03-14 09:48:27 -07:00
|
|
|
let end = start + info.account.data.len();
|
|
|
|
info.account.data.clone_from_slice(&buffer[start..end]);
|
2018-10-16 09:43:49 -07:00
|
|
|
|
2019-03-14 09:48:27 -07:00
|
|
|
start += info.account.data.len() // skip data
|
2018-11-12 09:29:17 -08:00
|
|
|
+ mem::size_of::<Pubkey>(); // skip owner
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 08:49:59 -07:00
|
|
|
pub fn process_instruction(
|
2018-11-17 17:02:14 -08:00
|
|
|
program_id: &Pubkey,
|
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
tx_data: &[u8],
|
2019-03-18 09:05:03 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2018-12-14 12:36:50 -08:00
|
|
|
solana_logger::setup();
|
2018-10-16 16:33:31 -07:00
|
|
|
|
2018-10-16 09:43:49 -07:00
|
|
|
if keyed_accounts[0].account.executable {
|
2019-01-04 10:17:29 -08:00
|
|
|
let (progs, params) = keyed_accounts.split_at_mut(1);
|
2019-03-14 09:48:27 -07:00
|
|
|
let prog = &progs[0].account.data;
|
2018-12-11 11:20:26 -08:00
|
|
|
info!("Call BPF program");
|
2019-05-24 16:21:42 -07:00
|
|
|
let (mut vm, heap_region) = match create_vm(prog) {
|
|
|
|
Ok(info) => info,
|
2018-10-16 13:13:54 -07:00
|
|
|
Err(e) => {
|
2019-02-26 17:09:57 -08:00
|
|
|
warn!("Failed to create BPF VM: {}", e);
|
2019-03-18 09:05:03 -07:00
|
|
|
return Err(InstructionError::GenericError);
|
2018-10-16 13:13:54 -07:00
|
|
|
}
|
|
|
|
};
|
2019-05-31 15:29:21 -07:00
|
|
|
let mut v = serialize_parameters(program_id, params, &tx_data);
|
2019-05-24 16:21:42 -07:00
|
|
|
|
|
|
|
match vm.execute_program(v.as_mut_slice(), &[], &[heap_region]) {
|
2018-11-02 14:32:05 -07:00
|
|
|
Ok(status) => {
|
|
|
|
if 0 == status {
|
2019-02-26 17:09:57 -08:00
|
|
|
warn!("BPF program failed: {}", status);
|
2019-03-18 09:05:03 -07:00
|
|
|
return Err(InstructionError::GenericError);
|
2018-11-02 14:32:05 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-25 02:58:04 -07:00
|
|
|
Err(e) => {
|
2019-02-26 17:09:57 -08:00
|
|
|
warn!("BPF VM failed to run program: {}", e);
|
2019-03-18 09:05:03 -07:00
|
|
|
return Err(InstructionError::GenericError);
|
2018-10-25 02:58:04 -07:00
|
|
|
}
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
2019-01-04 10:17:29 -08:00
|
|
|
deserialize_parameters(params, &v);
|
2018-12-11 11:20:26 -08:00
|
|
|
info!(
|
2018-10-31 10:59:56 -07:00
|
|
|
"BPF program executed {} instructions",
|
|
|
|
vm.get_last_instruction_count()
|
|
|
|
);
|
2018-12-14 12:36:50 -08:00
|
|
|
} else if let Ok(instruction) = bincode::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");
|
2019-03-18 09:05:03 -07:00
|
|
|
return Err(InstructionError::GenericError);
|
2018-11-29 12:32:16 -08:00
|
|
|
}
|
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);
|
2019-03-14 09:48:27 -07:00
|
|
|
if keyed_accounts[0].account.data.len() < offset + len {
|
2018-10-30 08:39:10 -07:00
|
|
|
warn!(
|
|
|
|
"Write overflow: {} < {}",
|
2019-03-14 09:48:27 -07:00
|
|
|
keyed_accounts[0].account.data.len(),
|
2018-10-19 18:28:38 -07:00
|
|
|
offset + len
|
|
|
|
);
|
2019-03-18 09:05:03 -07:00
|
|
|
return Err(InstructionError::GenericError);
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
2019-03-14 09:48:27 -07:00
|
|
|
keyed_accounts[0].account.data[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);
|
2019-03-18 09:05:03 -07:00
|
|
|
return Err(InstructionError::GenericError);
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
2018-12-03 13:32:31 -08:00
|
|
|
Ok(())
|
2018-10-16 09:43:49 -07:00
|
|
|
}
|
2018-10-31 10:59:56 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-11-06 14:28:46 -08:00
|
|
|
|
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 = &[
|
2019-02-22 16:27:19 -08:00
|
|
|
0x07, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // r6 + 1
|
|
|
|
0x05, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, // goto -2
|
2018-10-31 10:59:56 -07:00
|
|
|
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();
|
2019-02-22 16:27:19 -08:00
|
|
|
vm.set_max_instruction_count(10).unwrap();
|
2018-11-06 14:28:46 -08:00
|
|
|
vm.set_program(prog).unwrap();
|
2019-05-24 16:21:42 -07:00
|
|
|
vm.execute_program(input, &[], &[]).unwrap();
|
2018-10-31 10:59:56 -07:00
|
|
|
}
|
|
|
|
}
|