Files
solana/programs/bpf/rust/alloc/src/lib.rs

93 lines
2.3 KiB
Rust
Raw Normal View History

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;
Separate the "program" feature of `solana-sdk` into a new crate called `solana-program` (bp #12989) (#13131) * Add solana-program-sdk boilerplate (cherry picked from commit 3718771ffb9caad0bc1c180e488f699ce895a10e) # Conflicts: # sdk/Cargo.toml * Initial population of solana-program-sdk (cherry picked from commit 63db3242043602c7b9765a066b6f8d01ef20454e) # Conflicts: # Cargo.lock * Port programs to solana-program-sdk (cherry picked from commit fe68f7f786acf13e5b1f1fe4c54eb7af7b549391) # Conflicts: # programs/bpf/Cargo.lock # programs/bpf/rust/128bit/Cargo.toml # programs/bpf/rust/128bit_dep/Cargo.toml # programs/bpf/rust/alloc/Cargo.toml # programs/bpf/rust/call_depth/Cargo.toml # programs/bpf/rust/custom_heap/Cargo.toml # programs/bpf/rust/dep_crate/Cargo.toml # programs/bpf/rust/deprecated_loader/Cargo.toml # programs/bpf/rust/dup_accounts/Cargo.toml # programs/bpf/rust/error_handling/Cargo.toml # programs/bpf/rust/external_spend/Cargo.toml # programs/bpf/rust/instruction_introspection/Cargo.toml # programs/bpf/rust/invoke/Cargo.toml # programs/bpf/rust/invoked/Cargo.toml # programs/bpf/rust/iter/Cargo.toml # programs/bpf/rust/many_args/Cargo.toml # programs/bpf/rust/many_args_dep/Cargo.toml # programs/bpf/rust/noop/Cargo.toml # programs/bpf/rust/panic/Cargo.toml # programs/bpf/rust/param_passing/Cargo.toml # programs/bpf/rust/param_passing_dep/Cargo.toml # programs/bpf/rust/rand/Cargo.toml # programs/bpf/rust/ristretto/Cargo.toml # programs/bpf/rust/sanity/Cargo.toml # programs/bpf/rust/sha256/Cargo.toml # programs/bpf/rust/sysval/Cargo.toml * Only activate legacy program feature for the solana-sdk crate (cherry picked from commit 85c51f5787895c97220d3a2f8ce9be6b9cbdd6c6) * Run serum-dex unit tests (cherry picked from commit 92ce381d60d65a73973fc20cdd4772334f973b96) * Rename solana-program-sdk to solana-program (cherry picked from commit dd711ab5fbf96c1efcfdf9488397e8af2568182a) # Conflicts: # programs/bpf/rust/128bit/Cargo.toml # programs/bpf/rust/128bit_dep/Cargo.toml # programs/bpf/rust/alloc/Cargo.toml # programs/bpf/rust/call_depth/Cargo.toml # programs/bpf/rust/custom_heap/Cargo.toml # programs/bpf/rust/dep_crate/Cargo.toml # programs/bpf/rust/deprecated_loader/Cargo.toml # programs/bpf/rust/dup_accounts/Cargo.toml # programs/bpf/rust/error_handling/Cargo.toml # programs/bpf/rust/external_spend/Cargo.toml # programs/bpf/rust/instruction_introspection/Cargo.toml # programs/bpf/rust/invoke/Cargo.toml # programs/bpf/rust/invoked/Cargo.toml # programs/bpf/rust/iter/Cargo.toml # programs/bpf/rust/many_args/Cargo.toml # programs/bpf/rust/many_args_dep/Cargo.toml # programs/bpf/rust/noop/Cargo.toml # programs/bpf/rust/panic/Cargo.toml # programs/bpf/rust/param_passing/Cargo.toml # programs/bpf/rust/param_passing_dep/Cargo.toml # programs/bpf/rust/rand/Cargo.toml # programs/bpf/rust/ristretto/Cargo.toml # programs/bpf/rust/sanity/Cargo.toml # programs/bpf/rust/sha256/Cargo.toml # programs/bpf/rust/sysval/Cargo.toml * Update frozen_abi hashes The movement of files in sdk/ caused ABI hashes to change (cherry picked from commit a4956844bdd081e7b90508066c579f29be306ce7) * Resolve merge conflicts Co-authored-by: Michael Vines <mvines@gmail.com>
2020-10-24 17:25:22 +00:00
use solana_program::{entrypoint::SUCCESS, info};
2020-01-27 18:27:44 -08:00
use std::{alloc::Layout, mem};
2019-05-24 16:21:42 -07:00
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
2019-05-24 16:21:42 -07:00
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 {
2020-01-27 18:27:44 -08:00
// Test modest allocation and de-allocation
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 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
const ITERS: usize = 100;
let layout = Layout::from_size_align(ITERS, 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 failed");
2019-05-24 16:21:42 -07:00
alloc::alloc::handle_alloc_error(layout);
}
for i in 0..ITERS {
2019-05-24 16:21:42 -07:00
*ptr.add(i) = i as u8;
}
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);
}
{
// Test allocated vector
2019-06-07 14:38:49 -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
for v in ones.iter() {
sum += ones[*v];
}
2019-06-20 16:07:12 -07:00
info!(0x0, 0, 0, 0, sum as u64);
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-07 16:44:14 -07:00
const ITERS: usize = 100;
let mut v = Vec::new();
2019-06-07 16:44:14 -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);
assert_eq!(v.len(), ITERS);
2019-06-07 16:44:14 -07:00
}
SUCCESS
2019-05-24 16:21:42 -07:00
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_entrypoint() {
assert_eq!(SUCCESS, entrypoint(std::ptr::null_mut()));
}
}