2019-06-10 11:00:15 -07:00
|
|
|
//! @brief Example Rust-based BPF program that test dynamic memory allocation
|
2019-05-24 16:21:42 -07:00
|
|
|
|
2019-06-07 16:44:14 -07:00
|
|
|
#[macro_use]
|
2019-05-24 16:21:42 -07:00
|
|
|
extern crate alloc;
|
2019-09-06 09:20:14 -07:00
|
|
|
extern crate solana_sdk;
|
|
|
|
use solana_sdk::info;
|
2019-09-04 16:00:11 -07:00
|
|
|
use std::alloc::Layout;
|
|
|
|
use std::mem;
|
2019-05-24 16:21:42 -07:00
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
|
|
|
|
unsafe {
|
|
|
|
// Confirm large allocation fails
|
|
|
|
|
2019-09-04 16:00:11 -07:00
|
|
|
let layout = Layout::from_size_align(std::usize::MAX, mem::align_of::<u8>()).unwrap();
|
2019-05-24 16:21:42 -07:00
|
|
|
let ptr = alloc::alloc::alloc(layout);
|
|
|
|
if !ptr.is_null() {
|
2019-06-20 16:07:12 -07:00
|
|
|
info!("Error: Alloc of very larger buffer should fail");
|
2019-05-24 16:21:42 -07:00
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// Test modest allocation and deallocation
|
|
|
|
|
|
|
|
let layout = Layout::from_size_align(100, mem::align_of::<u8>()).unwrap();
|
|
|
|
let ptr = alloc::alloc::alloc(layout);
|
|
|
|
if ptr.is_null() {
|
2019-06-20 16:07:12 -07:00
|
|
|
info!("Error: Alloc of 100 bytes failed");
|
2019-05-24 16:21:42 -07:00
|
|
|
alloc::alloc::handle_alloc_error(layout);
|
|
|
|
}
|
|
|
|
alloc::alloc::dealloc(ptr, layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// Test allocated memory read and write
|
|
|
|
|
2019-06-10 10:19:58 -07:00
|
|
|
const ITERS: usize = 100;
|
2019-05-24 16:21:42 -07:00
|
|
|
let layout = Layout::from_size_align(100, mem::align_of::<u8>()).unwrap();
|
|
|
|
let ptr = alloc::alloc::alloc(layout);
|
|
|
|
if ptr.is_null() {
|
2019-06-20 16:07:12 -07:00
|
|
|
info!("Error: Alloc failed");
|
2019-05-24 16:21:42 -07:00
|
|
|
alloc::alloc::handle_alloc_error(layout);
|
|
|
|
}
|
2019-06-10 10:19:58 -07:00
|
|
|
for i in 0..ITERS {
|
2019-05-24 16:21:42 -07:00
|
|
|
*ptr.add(i) = i as u8;
|
|
|
|
}
|
2019-06-10 10:19:58 -07:00
|
|
|
for i in 0..ITERS {
|
2019-05-24 16:21:42 -07:00
|
|
|
assert_eq!(*ptr.add(i as usize), i as u8);
|
|
|
|
}
|
2019-06-20 16:07:12 -07:00
|
|
|
info!(0x3, 0, 0, 0, u64::from(*ptr.add(42)));
|
2019-05-24 16:21:42 -07:00
|
|
|
assert_eq!(*ptr.add(42), 42);
|
|
|
|
alloc::alloc::dealloc(ptr, layout);
|
|
|
|
}
|
|
|
|
|
2019-09-04 16:00:11 -07:00
|
|
|
// TODO not supported bump allocator
|
2019-05-24 16:21:42 -07:00
|
|
|
// unsafe {
|
|
|
|
// // Test alloc all bytes and one more (assumes heap size of 2048)
|
|
|
|
|
|
|
|
// let layout = Layout::from_size_align(2048, mem::align_of::<u8>()).unwrap();
|
|
|
|
// let ptr = alloc::alloc::alloc(layout);
|
|
|
|
// if ptr.is_null() {
|
2019-06-20 16:07:12 -07:00
|
|
|
// info!("Error: Alloc of 2048 bytes failed");
|
2019-05-24 16:21:42 -07:00
|
|
|
// 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() {
|
2019-06-20 16:07:12 -07:00
|
|
|
// info!("Error: Able to alloc 1 more then max");
|
2019-05-24 16:21:42 -07:00
|
|
|
// panic!();
|
|
|
|
// }
|
|
|
|
// alloc::alloc::dealloc(ptr, layout);
|
|
|
|
// }
|
|
|
|
|
2019-06-10 10:19:58 -07:00
|
|
|
{
|
|
|
|
// Test allocated vector
|
2019-06-07 14:38:49 -07:00
|
|
|
|
2019-06-10 10:19:58 -07:00
|
|
|
const ITERS: usize = 100;
|
|
|
|
let ones = vec![1_usize; ITERS];
|
|
|
|
let mut sum: usize = 0;
|
2019-06-07 14:38:49 -07:00
|
|
|
|
2019-06-10 10:19:58 -07:00
|
|
|
for v in ones.iter() {
|
|
|
|
sum += ones[*v];
|
|
|
|
}
|
2019-06-20 16:07:12 -07:00
|
|
|
info!(0x0, 0, 0, 0, sum as u64);
|
2019-06-10 10:19:58 -07:00
|
|
|
assert_eq!(sum, ITERS);
|
|
|
|
}
|
2019-05-24 16:21:42 -07:00
|
|
|
|
2019-06-07 16:44:14 -07:00
|
|
|
{
|
2019-09-04 16:00:11 -07:00
|
|
|
// test Vec::new()
|
2019-06-10 10:19:58 -07:00
|
|
|
|
2019-06-07 16:44:14 -07:00
|
|
|
const ITERS: usize = 100;
|
2019-06-10 10:19:58 -07:00
|
|
|
let mut v = Vec::new();
|
2019-06-07 16:44:14 -07:00
|
|
|
|
2019-06-10 10:19:58 -07:00
|
|
|
for i in 0..ITERS {
|
|
|
|
v.push(i);
|
2019-06-07 16:44:14 -07:00
|
|
|
}
|
2019-06-20 16:07:12 -07:00
|
|
|
info!(0x4, 0, 0, 0, v.len() as u64);
|
2019-06-10 10:19:58 -07:00
|
|
|
assert_eq!(v.len(), ITERS);
|
2019-06-07 16:44:14 -07:00
|
|
|
}
|
|
|
|
|
2019-06-20 16:07:12 -07:00
|
|
|
info!("Success");
|
2019-05-24 16:21:42 -07:00
|
|
|
true
|
|
|
|
}
|