Update to rbpf v0.1.23 - Use trait objects to convey helper context (#8648)

This commit is contained in:
Jack May 2020-03-05 14:14:21 -08:00 committed by GitHub
parent 7fad53b112
commit 8df4d8b905
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 51 deletions

6
Cargo.lock generated
View File

@ -3630,7 +3630,7 @@ dependencies = [
"num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-logger 1.1.0",
"solana-sdk 1.1.0",
"solana_rbpf 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
"solana_rbpf 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)",
"thiserror 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -4904,7 +4904,7 @@ dependencies = [
[[package]]
name = "solana_rbpf"
version = "0.1.21"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -6465,7 +6465,7 @@ dependencies = [
"checksum solana_libra_vm_cache_map 0.0.1-sol4 (registry+https://github.com/rust-lang/crates.io-index)" = "37fa2e1f00a87514cd2169149a5f81a89279703b2523979688d6ef84081a4690"
"checksum solana_libra_vm_runtime 0.0.1-sol4 (registry+https://github.com/rust-lang/crates.io-index)" = "ff9f8a7b8212dc4ece5d93f2839896e633c34d7463856e4a555cbcb5c67e9c26"
"checksum solana_libra_vm_runtime_types 0.0.1-sol4 (registry+https://github.com/rust-lang/crates.io-index)" = "254c23c8f30e7c82ae4dc6694e743400d674c66d371b700eec03378ba994f00b"
"checksum solana_rbpf 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "053e2a0f1f6c6298bf832493aeacdd6df98efb756a90feabd33caca9c708f2be"
"checksum solana_rbpf 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)" = "832c41d1f4184a9554c41aa29dbeb0d5f8873d72cf2be32411668bc5f047a150"
"checksum sourcefile 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4bf77cb82ba8453b42b6ae1d692e4cdc92f9a47beaf89a847c8be83f4e328ad3"
"checksum spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"

View File

@ -1716,7 +1716,7 @@ dependencies = [
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-logger 1.1.0",
"solana-sdk 1.1.0",
"solana_rbpf 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
"solana_rbpf 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)",
"thiserror 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1731,7 +1731,7 @@ dependencies = [
"solana-logger 1.1.0",
"solana-runtime 1.1.0",
"solana-sdk 1.1.0",
"solana_rbpf 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
"solana_rbpf 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -2070,7 +2070,7 @@ dependencies = [
[[package]]
name = "solana_rbpf"
version = "0.1.21"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2947,7 +2947,7 @@ dependencies = [
"checksum sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0"
"checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
"checksum smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ab606a9c5e214920bb66c458cd7be8ef094f813f20fe77a54cc7dbfff220d4b7"
"checksum solana_rbpf 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "053e2a0f1f6c6298bf832493aeacdd6df98efb756a90feabd33caca9c708f2be"
"checksum solana_rbpf 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)" = "832c41d1f4184a9554c41aa29dbeb0d5f8873d72cf2be32411668bc5f047a150"
"checksum sourcefile 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4bf77cb82ba8453b42b6ae1d692e4cdc92f9a47beaf89a847c8be83f4e328ad3"
"checksum spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"

View File

@ -26,7 +26,7 @@ solana-bpf-loader-program = { path = "../bpf_loader", version = "1.1.0" }
solana-logger = { path = "../../logger", version = "1.1.0" }
solana-runtime = { path = "../../runtime", version = "1.1.0" }
solana-sdk = { path = "../../sdk", version = "1.1.0" }
solana_rbpf = "=0.1.21"
solana_rbpf = "=0.1.23"
[[bench]]
name = "bpf_loader"

View File

@ -17,7 +17,8 @@ num-derive = { version = "0.3" }
num-traits = { version = "0.2" }
solana-logger = { path = "../../logger", version = "1.1.0" }
solana-sdk = { path = "../../sdk", version = "1.1.0" }
solana_rbpf = "=0.1.21"
solana_rbpf = "=0.1.23"
thiserror = "1.0"
[lib]

View File

@ -3,7 +3,7 @@ use alloc::Alloc;
use libc::c_char;
use log::*;
use solana_rbpf::{
ebpf::{HelperContext, MM_HEAP_START},
ebpf::{HelperObject, MM_HEAP_START},
memory_region::{translate_addr, MemoryRegion},
EbpfVm,
};
@ -11,7 +11,7 @@ use std::{
alloc::Layout,
ffi::CStr,
io::{Error, ErrorKind},
mem,
mem::align_of,
slice::from_raw_parts,
str::from_utf8,
};
@ -29,18 +29,22 @@ use crate::allocator_bump::BPFAllocator;
const DEFAULT_HEAP_SIZE: usize = 32 * 1024;
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)?;
vm.register_helper_ex("abort", helper_abort)?;
vm.register_helper_ex("sol_panic", helper_sol_panic)?;
vm.register_helper_ex("sol_panic_", helper_sol_panic)?;
vm.register_helper_ex("sol_log", helper_sol_log)?;
vm.register_helper_ex("sol_log_", helper_sol_log)?;
vm.register_helper_ex("sol_log_64", helper_sol_log_u64)?;
vm.register_helper_ex("sol_log_64_", helper_sol_log_u64)?;
let heap = vec![0_u8; DEFAULT_HEAP_SIZE];
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))?;
vm.register_helper_with_context_ex(
"sol_alloc_free_",
Box::new(HelperSolAllocFree {
allocator: BPFAllocator::new(heap, MM_HEAP_START),
}),
)?;
Ok(heap_region)
}
@ -54,7 +58,6 @@ pub fn helper_abort(
_arg3: u64,
_arg4: u64,
_arg5: u64,
_context: &mut HelperContext,
_ro_regions: &[MemoryRegion],
_rw_regions: &[MemoryRegion],
) -> Result<u64, Error> {
@ -73,7 +76,6 @@ pub fn helper_sol_panic(
line: u64,
column: u64,
_arg5: u64,
_context: &mut HelperContext,
ro_regions: &[MemoryRegion],
_rw_regions: &[MemoryRegion],
) -> Result<u64, Error> {
@ -99,7 +101,6 @@ pub fn helper_sol_log(
_arg3: u64,
_arg4: u64,
_arg5: u64,
_context: &mut HelperContext,
ro_regions: &[MemoryRegion],
_rw_regions: &[MemoryRegion],
) -> Result<u64, Error> {
@ -132,7 +133,6 @@ pub fn helper_sol_log_u64(
arg3: u64,
arg4: u64,
arg5: u64,
_context: &mut HelperContext,
_ro_regions: &[MemoryRegion],
_rw_regions: &[MemoryRegion],
) -> Result<u64, Error> {
@ -151,31 +151,29 @@ pub fn helper_sol_log_u64(
/// 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_addr: u64,
_arg3: u64,
_arg4: u64,
_arg5: u64,
context: &mut HelperContext,
_ro_regions: &[MemoryRegion],
_rw_regions: &[MemoryRegion],
) -> Result<u64, Error> {
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_addr == 0 {
match allocator.alloc(layout) {
Ok(addr) => Ok(addr as u64),
Err(_) => Ok(0),
}
} else {
allocator.dealloc(free_addr, layout);
Ok(0)
}
};
};
}
panic!("Failed to get alloc_free context");
pub struct HelperSolAllocFree {
allocator: BPFAllocator,
}
impl HelperObject for HelperSolAllocFree {
fn call(
&mut self,
size: u64,
free_addr: u64,
_arg3: u64,
_arg4: u64,
_arg5: u64,
_ro_regions: &[MemoryRegion],
_rw_regions: &[MemoryRegion],
) -> Result<u64, Error> {
let layout = Layout::from_size_align(size as usize, align_of::<u8>()).unwrap();
if free_addr == 0 {
match self.allocator.alloc(layout) {
Ok(addr) => Ok(addr as u64),
Err(_) => Ok(0),
}
} else {
self.allocator.dealloc(free_addr, layout);
Ok(0)
}
}
}