2022-10-28 11:30:31 -07:00
|
|
|
//! Example Rust-based SBF program that test dynamic memory allocation
|
2019-05-24 16:21:42 -07:00
|
|
|
|
2023-09-01 00:26:13 -07:00
|
|
|
#![allow(clippy::arithmetic_side_effects)]
|
2023-03-12 22:28:34 -07:00
|
|
|
|
2019-06-07 16:44:14 -07:00
|
|
|
#[macro_use]
|
2019-05-24 16:21:42 -07:00
|
|
|
extern crate alloc;
|
2022-02-21 19:59:06 -08:00
|
|
|
use {
|
2022-05-20 13:19:41 -07:00
|
|
|
solana_program::{
|
|
|
|
custom_heap_default, custom_panic_default, entrypoint::SUCCESS, log::sol_log_64, msg,
|
|
|
|
},
|
2022-02-21 19:59:06 -08:00
|
|
|
std::{alloc::Layout, mem},
|
|
|
|
};
|
2019-05-24 16:21:42 -07:00
|
|
|
|
|
|
|
#[no_mangle]
|
2020-01-31 10:58:07 -08:00
|
|
|
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
|
2019-05-24 16:21:42 -07:00
|
|
|
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() {
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("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;
|
2020-09-15 16:42:20 -07:00
|
|
|
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() {
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("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 {
|
2023-03-12 22:28:34 -07:00
|
|
|
assert_eq!(*ptr.add(i), i as u8);
|
2019-05-24 16:21:42 -07:00
|
|
|
}
|
2021-12-10 13:13:30 -08:00
|
|
|
sol_log_64(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-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];
|
|
|
|
}
|
2021-12-10 13:13:30 -08:00
|
|
|
sol_log_64(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
|
|
|
}
|
2021-12-10 13:13:30 -08:00
|
|
|
sol_log_64(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-09-06 16:05:01 -07:00
|
|
|
SUCCESS
|
2019-05-24 16:21:42 -07:00
|
|
|
}
|
2019-09-18 19:54:10 -07:00
|
|
|
|
2022-05-20 13:19:41 -07:00
|
|
|
custom_heap_default!();
|
2020-12-01 07:52:20 -08:00
|
|
|
custom_panic_default!();
|
|
|
|
|
2019-09-18 19:54:10 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_entrypoint() {
|
|
|
|
assert_eq!(SUCCESS, entrypoint(std::ptr::null_mut()));
|
|
|
|
}
|
|
|
|
}
|