2019-08-23 11:03:53 -07:00
|
|
|
use crate::alloc;
|
|
|
|
use alloc::Alloc;
|
2019-06-21 11:08:50 -07:00
|
|
|
use libc::c_char;
|
|
|
|
use log::*;
|
2019-08-23 11:03:53 -07:00
|
|
|
use solana_rbpf::{
|
|
|
|
ebpf::{HelperContext, MM_HEAP_START},
|
|
|
|
memory_region::{translate_addr, MemoryRegion},
|
|
|
|
EbpfVm,
|
|
|
|
};
|
2019-06-21 11:08:50 -07:00
|
|
|
use std::alloc::Layout;
|
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::io::{Error, ErrorKind};
|
|
|
|
use std::mem;
|
|
|
|
use std::slice::from_raw_parts;
|
|
|
|
use std::str::from_utf8;
|
|
|
|
|
|
|
|
/// Program heap allocators are intended to allocate/free from a given
|
|
|
|
/// chunk of memory. The specific allocator implementation is
|
|
|
|
/// selectable at build-time.
|
2019-08-23 11:03:53 -07:00
|
|
|
/// Only one allocator is currently supported
|
2019-06-21 11:08:50 -07:00
|
|
|
|
|
|
|
/// Simple bump allocator, never frees
|
|
|
|
use crate::allocator_bump::BPFAllocator;
|
|
|
|
|
|
|
|
/// Default program heap size, allocators
|
|
|
|
/// are expected to enforce this
|
|
|
|
const DEFAULT_HEAP_SIZE: usize = 32 * 1024;
|
|
|
|
|
2019-08-23 11:03:53 -07:00
|
|
|
pub fn register_helpers(vm: &mut EbpfVm) -> Result<(MemoryRegion), Error> {
|
|
|
|
vm.register_helper_ex("abort", helper_abort, None)?;
|
|
|
|
vm.register_helper_ex("sol_panic", helper_sol_panic, None)?;
|
|
|
|
vm.register_helper_ex("sol_panic_", helper_sol_panic, None)?;
|
|
|
|
vm.register_helper_ex("sol_log", helper_sol_log, None)?;
|
|
|
|
vm.register_helper_ex("sol_log_", helper_sol_log, None)?;
|
|
|
|
vm.register_helper_ex("sol_log_64", helper_sol_log_u64, None)?;
|
|
|
|
vm.register_helper_ex("sol_log_64_", helper_sol_log_u64, None)?;
|
2019-06-21 11:08:50 -07:00
|
|
|
|
|
|
|
let heap = vec![0_u8; DEFAULT_HEAP_SIZE];
|
2019-08-23 11:03:53 -07:00
|
|
|
let heap_region = MemoryRegion::new_from_slice(&heap, MM_HEAP_START);
|
|
|
|
let context = Box::new(BPFAllocator::new(heap, MM_HEAP_START));
|
|
|
|
vm.register_helper_ex("sol_alloc_free_", helper_sol_alloc_free, Some(context))?;
|
2019-06-21 11:08:50 -07:00
|
|
|
|
|
|
|
Ok(heap_region)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Abort helper functions, called when the BPF program calls `abort()`
|
|
|
|
/// The verify function returns an error which will cause the BPF program
|
|
|
|
/// to be halted immediately
|
2019-08-23 11:03:53 -07:00
|
|
|
pub fn helper_abort(
|
2019-06-21 11:08:50 -07:00
|
|
|
_arg1: u64,
|
|
|
|
_arg2: u64,
|
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
2019-08-23 11:03:53 -07:00
|
|
|
_context: &mut HelperContext,
|
2019-06-21 11:08:50 -07:00
|
|
|
_ro_regions: &[MemoryRegion],
|
|
|
|
_rw_regions: &[MemoryRegion],
|
2019-08-23 11:03:53 -07:00
|
|
|
) -> Result<(u64), Error> {
|
2019-06-21 11:08:50 -07:00
|
|
|
Err(Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
"Error: BPF program called abort()!",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-08-23 11:03:53 -07:00
|
|
|
pub fn helper_sol_panic(
|
2019-06-21 11:08:50 -07:00
|
|
|
file: u64,
|
2019-08-23 11:03:53 -07:00
|
|
|
len: u64,
|
2019-06-21 11:08:50 -07:00
|
|
|
line: u64,
|
|
|
|
column: u64,
|
|
|
|
_arg5: u64,
|
2019-08-23 11:03:53 -07:00
|
|
|
_context: &mut HelperContext,
|
2019-06-21 11:08:50 -07:00
|
|
|
ro_regions: &[MemoryRegion],
|
|
|
|
_rw_regions: &[MemoryRegion],
|
2019-08-23 11:03:53 -07:00
|
|
|
) -> Result<(u64), Error> {
|
|
|
|
if let Ok(host_addr) = translate_addr(file, len as usize, "Load", 0, ro_regions) {
|
|
|
|
let c_buf: *const c_char = host_addr as *const c_char;
|
2019-06-21 11:08:50 -07:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn helper_sol_log(
|
|
|
|
addr: u64,
|
|
|
|
len: u64,
|
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
2019-08-23 11:03:53 -07:00
|
|
|
_context: &mut HelperContext,
|
2019-06-21 11:08:50 -07:00
|
|
|
ro_regions: &[MemoryRegion],
|
|
|
|
_rw_regions: &[MemoryRegion],
|
2019-08-23 11:03:53 -07:00
|
|
|
) -> Result<(u64), Error> {
|
|
|
|
let host_addr = translate_addr(addr, len as usize, "Load", 0, ro_regions)?;
|
|
|
|
let c_buf: *const c_char = host_addr as *const c_char;
|
|
|
|
unsafe {
|
|
|
|
for i in 0..len {
|
|
|
|
let c = std::ptr::read(c_buf.offset(i as isize));
|
|
|
|
if i == len - 1 || c == 0 {
|
|
|
|
let message =
|
|
|
|
from_utf8(from_raw_parts(host_addr as *const u8, len as usize)).unwrap();
|
2019-09-10 16:13:23 -07:00
|
|
|
info!("info!: {}", message);
|
2019-08-23 11:03:53 -07:00
|
|
|
return Ok(0);
|
|
|
|
}
|
2019-06-21 11:08:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(Error::new(
|
|
|
|
ErrorKind::Other,
|
2019-08-23 11:03:53 -07:00
|
|
|
"Error: Unterminated string logged",
|
2019-06-21 11:08:50 -07:00
|
|
|
))
|
|
|
|
}
|
2019-08-23 11:03:53 -07:00
|
|
|
|
2019-06-21 11:08:50 -07:00
|
|
|
pub fn helper_sol_log_u64(
|
|
|
|
arg1: u64,
|
|
|
|
arg2: u64,
|
|
|
|
arg3: u64,
|
|
|
|
arg4: u64,
|
|
|
|
arg5: u64,
|
2019-08-23 11:03:53 -07:00
|
|
|
_context: &mut HelperContext,
|
|
|
|
_ro_regions: &[MemoryRegion],
|
|
|
|
_rw_regions: &[MemoryRegion],
|
|
|
|
) -> Result<(u64), Error> {
|
2019-06-21 11:08:50 -07:00
|
|
|
info!(
|
|
|
|
"info!: {:#x}, {:#x}, {:#x}, {:#x}, {:#x}",
|
|
|
|
arg1, arg2, arg3, arg4, arg5
|
|
|
|
);
|
2019-08-23 11:03:53 -07:00
|
|
|
Ok(0)
|
2019-06-21 11:08:50 -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,
|
2019-08-23 11:03:53 -07:00
|
|
|
free_addr: u64,
|
2019-06-21 11:08:50 -07:00
|
|
|
_arg3: u64,
|
|
|
|
_arg4: u64,
|
|
|
|
_arg5: u64,
|
2019-08-23 11:03:53 -07:00
|
|
|
context: &mut HelperContext,
|
|
|
|
_ro_regions: &[MemoryRegion],
|
|
|
|
_rw_regions: &[MemoryRegion],
|
|
|
|
) -> Result<(u64), Error> {
|
2019-06-21 11:08:50 -07:00
|
|
|
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();
|
2019-08-23 11:03:53 -07:00
|
|
|
if free_addr == 0 {
|
2019-06-21 11:08:50 -07:00
|
|
|
match allocator.alloc(layout) {
|
2019-08-23 11:03:53 -07:00
|
|
|
Ok(addr) => Ok(addr as u64),
|
|
|
|
Err(_) => Ok(0),
|
2019-06-21 11:08:50 -07:00
|
|
|
}
|
|
|
|
} else {
|
2019-08-23 11:03:53 -07:00
|
|
|
allocator.dealloc(free_addr, layout);
|
|
|
|
Ok(0)
|
2019-06-21 11:08:50 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
panic!("Failed to get alloc_free context");
|
|
|
|
}
|