BPF rust language updates (#4752)

This commit is contained in:
Jack May
2019-06-20 16:07:12 -07:00
committed by GitHub
parent aacb38864c
commit e59b53dfa8
21 changed files with 230 additions and 180 deletions

View File

@ -6,11 +6,10 @@
extern crate alloc;
extern crate solana_sdk_bpf_utils;
use solana_sdk_bpf_utils::log::*;
use alloc::vec::Vec;
use core::alloc::Layout;
use core::mem;
use solana_sdk_bpf_utils::info;
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
@ -20,7 +19,7 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
let layout = Layout::from_size_align(core::usize::MAX, mem::align_of::<u8>()).unwrap();
let ptr = alloc::alloc::alloc(layout);
if !ptr.is_null() {
sol_log("Error: Alloc of very larger buffer should fail");
info!("Error: Alloc of very larger buffer should fail");
panic!();
}
}
@ -31,7 +30,7 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
let layout = Layout::from_size_align(100, mem::align_of::<u8>()).unwrap();
let ptr = alloc::alloc::alloc(layout);
if ptr.is_null() {
sol_log("Error: Alloc of 100 bytes failed");
info!("Error: Alloc of 100 bytes failed");
alloc::alloc::handle_alloc_error(layout);
}
alloc::alloc::dealloc(ptr, layout);
@ -44,7 +43,7 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
let layout = Layout::from_size_align(100, mem::align_of::<u8>()).unwrap();
let ptr = alloc::alloc::alloc(layout);
if ptr.is_null() {
sol_log("Error: Alloc failed");
info!("Error: Alloc failed");
alloc::alloc::handle_alloc_error(layout);
}
for i in 0..ITERS {
@ -53,7 +52,7 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
for i in 0..ITERS {
assert_eq!(*ptr.add(i as usize), i as u8);
}
sol_log_64(0x3, 0, 0, 0, u64::from(*ptr.add(42)));
info!(0x3, 0, 0, 0, u64::from(*ptr.add(42)));
assert_eq!(*ptr.add(42), 42);
alloc::alloc::dealloc(ptr, layout);
}
@ -65,13 +64,13 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
// let layout = Layout::from_size_align(2048, mem::align_of::<u8>()).unwrap();
// let ptr = alloc::alloc::alloc(layout);
// if ptr.is_null() {
// sol_log("Error: Alloc of 2048 bytes failed");
// info!("Error: Alloc of 2048 bytes failed");
// alloc::alloc::handle_alloc_error(layout);
// }
// let layout = Layout::from_size_align(1, mem::align_of::<u8>()).unwrap();
// let ptr_fail = alloc::alloc::alloc(layout);
// if !ptr_fail.is_null() {
// sol_log("Error: Able to alloc 1 more then max");
// info!("Error: Able to alloc 1 more then max");
// panic!();
// }
// alloc::alloc::dealloc(ptr, layout);
@ -87,7 +86,7 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
for v in ones.iter() {
sum += ones[*v];
}
sol_log_64(0x0, 0, 0, 0, sum as u64);
info!(0x0, 0, 0, 0, sum as u64);
assert_eq!(sum, ITERS);
}
@ -98,13 +97,12 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
let mut v = Vec::new();
for i in 0..ITERS {
sol_log_64(i as u64, 0, 0, 0, 0);
v.push(i);
}
sol_log_64(0x4, 0, 0, 0, v.len() as u64);
info!(0x4, 0, 0, 0, v.len() as u64);
assert_eq!(v.len(), ITERS);
}
sol_log("Success");
info!("Success");
true
}