Add memory operation syscalls (#16447)

This commit is contained in:
Jack May
2021-06-01 15:33:17 -07:00
committed by GitHub
parent 38d71c37da
commit 2b50529265
16 changed files with 605 additions and 145 deletions

View File

@ -9,11 +9,18 @@ homepage = "https://solana.com/"
documentation = "https://docs.rs/solana-bpf-rust-mem"
edition = "2018"
[features]
no-entrypoint = []
[dependencies]
solana-program = { path = "../../../../sdk/program", version = "=1.8.0" }
[dev-dependencies]
solana-program-test = { path = "../../../../program-test", version = "=1.8.0" }
solana-sdk = { path = "../../../../sdk", version = "=1.8.0" }
[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "lib"]
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

View File

@ -0,0 +1,39 @@
//! @brief Test mem functions
use crate::{run_mem_tests, MemOps};
use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
program_memory::{sol_memcmp, sol_memcpy, sol_memmove, sol_memset},
pubkey::Pubkey,
};
entrypoint!(process_instruction);
#[allow(clippy::unnecessary_wraps)]
pub fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
// Via syscalls
#[derive(Default)]
struct MemOpSyscalls();
impl MemOps for MemOpSyscalls {
fn memcpy(&self, dst: &mut [u8], src: &[u8], n: usize) {
sol_memcpy(dst, src, n)
}
unsafe fn memmove(&self, dst: *mut u8, src: *mut u8, n: usize) {
sol_memmove(dst, src, n)
}
fn memset(&self, s: &mut [u8], c: u8, n: usize) {
sol_memset(s, c, n)
}
fn memcmp(&self, s1: &[u8], s2: &[u8], n: usize) -> i32 {
sol_memcmp(s1, s2, n)
}
}
run_mem_tests(MemOpSyscalls::default());
Ok(())
}

View File

@ -1,190 +1,165 @@
//! @brief Test builtin mem functions
//! @brief Test mem functions
#![cfg(target_arch = "bpf")]
#![feature(rustc_private)]
#[cfg(not(feature = "no-entrypoint"))]
pub mod entrypoint;
extern crate compiler_builtins;
use solana_program::{custom_panic_default, entrypoint::SUCCESS};
pub trait MemOps {
fn memcpy(&self, dst: &mut [u8], src: &[u8], n: usize);
/// # Safety
unsafe fn memmove(&self, dst: *mut u8, src: *mut u8, n: usize);
fn memset(&self, s: &mut [u8], c: u8, n: usize);
fn memcmp(&self, s1: &[u8], s2: &[u8], n: usize) -> i32;
}
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
pub fn run_mem_tests<T: MemOps>(mem_ops: T) {
// memcpy
let src = &[1_u8; 18];
let dst = &mut [0_u8; 1];
mem_ops.memcpy(dst, src, 1);
assert_eq!(&src[..1], dst);
let dst = &mut [0_u8; 3];
mem_ops.memcpy(dst, src, 3);
assert_eq!(&src[..3], dst);
let dst = &mut [0_u8; 8];
mem_ops.memcpy(dst, src, 8);
assert_eq!(&src[..8], dst);
let dst = &mut [0_u8; 9];
mem_ops.memcpy(dst, src, 9);
assert_eq!(&src[..9], dst);
let dst = &mut [0_u8; 16];
mem_ops.memcpy(dst, src, 16);
assert_eq!(&src[..16], dst);
let dst = &mut [0_u8; 18];
mem_ops.memcpy(dst, src, 18);
assert_eq!(&src[..18], dst);
let dst = &mut [0_u8; 18];
mem_ops.memcpy(dst, &src[1..], 17);
assert_eq!(&src[1..], &dst[..17]);
let dst = &mut [0_u8; 18];
mem_ops.memcpy(&mut dst[1..], &src[1..], 17);
assert_eq!(&src[1..], &dst[1..]);
// memmove
unsafe {
// memcpy
let src = &mut [1_u8; 18];
let dst = &mut [0_u8; 1];
compiler_builtins::mem::memcpy(&mut src[0] as *mut u8, &mut dst[0] as *mut u8, 1);
assert_eq!(&src[..1], dst);
let dst = &mut [0_u8; 3];
compiler_builtins::mem::memcpy(&mut src[0] as *mut u8, &mut dst[0] as *mut u8, 3);
assert_eq!(&src[..3], dst);
let dst = &mut [0_u8; 8];
compiler_builtins::mem::memcpy(&mut src[0] as *mut u8, &mut dst[0] as *mut u8, 8);
assert_eq!(&src[..8], dst);
let dst = &mut [0_u8; 9];
compiler_builtins::mem::memcpy(&mut src[0] as *mut u8, &mut dst[0] as *mut u8, 9);
assert_eq!(&src[..9], dst);
let dst = &mut [0_u8; 16];
compiler_builtins::mem::memcpy(&mut src[0] as *mut u8, &mut dst[0] as *mut u8, 16);
assert_eq!(&src[..16], dst);
let dst = &mut [0_u8; 18];
compiler_builtins::mem::memcpy(&mut src[0] as *mut u8, &mut dst[0] as *mut u8, 18);
assert_eq!(&src[..18], dst);
let dst = &mut [0_u8; 18];
compiler_builtins::mem::memcpy(&mut src[1] as *mut u8, &mut dst[0] as *mut u8, 17);
assert_eq!(&src[1..], &dst[1..]);
let dst = &mut [0_u8; 18];
compiler_builtins::mem::memcpy(&mut src[1] as *mut u8, &mut dst[1] as *mut u8, 17);
assert_eq!(&src[1..], &dst[..17]);
// memmove
let buf = &mut [1_u8, 0];
compiler_builtins::mem::memmove(&mut buf[0] as *mut u8, &mut buf[1] as *mut u8, 1);
mem_ops.memmove(&mut buf[0] as *mut u8, &mut buf[1] as *mut u8, 1);
assert_eq!(buf[0], buf[1]);
let buf = &mut [1_u8, 0];
compiler_builtins::mem::memmove(&mut buf[1] as *mut u8, &mut buf[0] as *mut u8, 1);
mem_ops.memmove(&mut buf[1] as *mut u8, &mut buf[0] as *mut u8, 1);
assert_eq!(buf[0], buf[1]);
let buf = &mut [1_u8, 1, 1, 0, 0, 0];
compiler_builtins::mem::memmove(&mut buf[0] as *mut u8, &mut buf[3] as *mut u8, 3);
mem_ops.memmove(&mut buf[0] as *mut u8, &mut buf[3] as *mut u8, 3);
assert_eq!(buf[..3], buf[3..]);
let buf = &mut [1_u8, 1, 1, 0, 0, 0];
compiler_builtins::mem::memmove(&mut buf[3] as *mut u8, &mut buf[0] as *mut u8, 3);
mem_ops.memmove(&mut buf[3] as *mut u8, &mut buf[0] as *mut u8, 3);
assert_eq!(buf[..3], buf[3..]);
let buf = &mut [1_u8, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0];
compiler_builtins::mem::memmove(&mut buf[0] as *mut u8, &mut buf[8] as *mut u8, 8);
mem_ops.memmove(&mut buf[0] as *mut u8, &mut buf[8] as *mut u8, 8);
assert_eq!(buf[..8], buf[8..]);
let buf = &mut [1_u8, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0];
compiler_builtins::mem::memmove(&mut buf[8] as *mut u8, &mut buf[0] as *mut u8, 8);
mem_ops.memmove(&mut buf[8] as *mut u8, &mut buf[0] as *mut u8, 8);
assert_eq!(buf[..8], buf[8..]);
let buf = &mut [1_u8, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
compiler_builtins::mem::memmove(&mut buf[0] as *mut u8, &mut buf[9] as *mut u8, 9);
mem_ops.memmove(&mut buf[0] as *mut u8, &mut buf[9] as *mut u8, 9);
assert_eq!(buf[..9], buf[9..]);
let buf = &mut [0_u8, 1, 2, 3, 4, 5, 6, 7, 8, 9];
compiler_builtins::mem::memmove(&mut buf[1] as *mut u8, &mut buf[0] as *mut u8, 9);
mem_ops.memmove(&mut buf[1] as *mut u8, &mut buf[0] as *mut u8, 9);
assert_eq!(&mut [0_u8, 0, 1, 2, 3, 4, 5, 6, 7, 8], buf);
let buf = &mut [1_u8, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
compiler_builtins::mem::memmove(&mut buf[9] as *mut u8, &mut buf[0] as *mut u8, 9);
mem_ops.memmove(&mut buf[9] as *mut u8, &mut buf[0] as *mut u8, 9);
assert_eq!(buf[..9], buf[9..]);
let buf = &mut [
1_u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
];
compiler_builtins::mem::memmove(&mut buf[0] as *mut u8, &mut buf[16] as *mut u8, 16);
mem_ops.memmove(&mut buf[0] as *mut u8, &mut buf[16] as *mut u8, 16);
assert_eq!(buf[..16], buf[16..]);
let buf = &mut [
1_u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
];
compiler_builtins::mem::memmove(&mut buf[16] as *mut u8, &mut buf[0] as *mut u8, 16);
mem_ops.memmove(&mut buf[16] as *mut u8, &mut buf[0] as *mut u8, 16);
assert_eq!(buf[..16], buf[16..]);
let buf = &mut [
1_u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
];
compiler_builtins::mem::memmove(&mut buf[0] as *mut u8, &mut buf[18] as *mut u8, 18);
mem_ops.memmove(&mut buf[0] as *mut u8, &mut buf[18] as *mut u8, 18);
assert_eq!(buf[..18], buf[18..]);
let buf = &mut [
1_u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
];
compiler_builtins::mem::memmove(&mut buf[18] as *mut u8, &mut buf[0] as *mut u8, 18);
mem_ops.memmove(&mut buf[18] as *mut u8, &mut buf[0] as *mut u8, 18);
assert_eq!(buf[..18], buf[18..]);
let buf = &mut [
1_u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
];
compiler_builtins::mem::memmove(&mut buf[1] as *mut u8, &mut buf[18] as *mut u8, 17);
mem_ops.memmove(&mut buf[1] as *mut u8, &mut buf[18] as *mut u8, 17);
assert_eq!(buf[1..17], buf[18..34]);
let buf = &mut [
1_u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
];
compiler_builtins::mem::memmove(&mut buf[19] as *mut u8, &mut buf[1] as *mut u8, 17);
mem_ops.memmove(&mut buf[19] as *mut u8, &mut buf[1] as *mut u8, 17);
assert_eq!(buf[..17], buf[19..]);
// memset
let exp = &[1_u8; 18];
let buf = &mut [0_u8; 18];
compiler_builtins::mem::memset(&mut buf[0] as *mut u8, 1, 1);
assert_eq!(exp[..1], buf[..1]);
compiler_builtins::mem::memset(&mut buf[0] as *mut u8, 1, 3);
assert_eq!(exp[..3], buf[..3]);
compiler_builtins::mem::memset(&mut buf[0] as *mut u8, 1, 8);
assert_eq!(exp[..8], buf[..8]);
compiler_builtins::mem::memset(&mut buf[0] as *mut u8, 1, 9);
assert_eq!(exp[..9], buf[..9]);
compiler_builtins::mem::memset(&mut buf[0] as *mut u8, 1, 16);
assert_eq!(exp[..16], buf[..16]);
compiler_builtins::mem::memset(&mut buf[0] as *mut u8, 1, 18);
assert_eq!(exp[..18], buf[..18]);
compiler_builtins::mem::memset(&mut buf[1] as *mut u8, 1, 17);
assert_eq!(exp[1..18], buf[1..18]);
// memcmp
assert_eq!(
-1,
compiler_builtins::mem::memcmp(&[0_u8] as *const u8, &[1_u8] as *const u8, 1)
);
assert_eq!(
-1,
compiler_builtins::mem::memcmp(
&[0_u8, 0, 0] as *const u8,
&[0_u8, 0, 1] as *const u8,
3
)
);
assert_eq!(
0,
compiler_builtins::mem::memcmp(
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 0] as *const u8,
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 0] as *const u8,
9
)
);
assert_eq!(
-1,
compiler_builtins::mem::memcmp(
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 0] as *const u8,
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 1] as *const u8,
9
)
);
assert_eq!(
-1,
compiler_builtins::mem::memcmp(
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 0, 0] as *const u8,
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 0, 1] as *const u8,
10
)
);
assert_eq!(
0,
compiler_builtins::mem::memcmp(&[0_u8; 8] as *const u8, &[0_u8; 8] as *const u8, 8)
);
assert_eq!(
-1,
compiler_builtins::mem::memcmp(&[0_u8; 8] as *const u8, &[1_u8; 8] as *const u8, 8)
);
assert_eq!(
-1,
compiler_builtins::mem::memcmp(&[0_u8; 16] as *const u8, &[1_u8; 16] as *const u8, 16)
);
assert_eq!(
-1,
compiler_builtins::mem::memcmp(&[0_u8; 18] as *const u8, &[1_u8; 18] as *const u8, 18)
);
let one = &[0_u8; 18];
let two = &[1_u8; 18];
assert_eq!(
-1,
compiler_builtins::mem::memcmp(&one[1] as *const u8, &two[0] as *const u8, 17)
);
assert_eq!(
-1,
compiler_builtins::mem::memcmp(&one[1] as *const u8, &two[1] as *const u8, 17)
);
let buf = &mut [0_u8, 0, 0, 1, 1, 1, 1, 1, 0];
mem_ops.memmove(&mut buf[0] as *mut u8, &mut buf[3] as *mut u8, 5);
assert_eq!(buf, &mut [1, 1, 1, 1, 1, 1, 1, 1, 0]);
}
SUCCESS
}
// memset
let exp = &[1_u8; 18];
let buf = &mut [0_u8; 18];
mem_ops.memset(&mut buf[0..], 1, 1);
assert_eq!(exp[..1], buf[..1]);
mem_ops.memset(&mut buf[0..], 1, 3);
assert_eq!(exp[..3], buf[..3]);
mem_ops.memset(&mut buf[0..], 1, 8);
assert_eq!(exp[..8], buf[..8]);
mem_ops.memset(&mut buf[0..], 1, 9);
assert_eq!(exp[..9], buf[..9]);
mem_ops.memset(&mut buf[0..], 1, 16);
assert_eq!(exp[..16], buf[..16]);
mem_ops.memset(&mut buf[0..], 1, 18);
assert_eq!(exp[..18], buf[..18]);
mem_ops.memset(&mut buf[1..], 1, 17);
assert_eq!(exp[1..18], buf[1..18]);
custom_panic_default!();
// memcmp
assert_eq!(-1, mem_ops.memcmp(&[0_u8], &[1_u8], 1));
assert_eq!(-1, mem_ops.memcmp(&[0_u8, 0, 0], &[0_u8, 0, 1], 3));
assert_eq!(
0,
mem_ops.memcmp(
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 0],
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 0],
9
)
);
assert_eq!(
-1,
mem_ops.memcmp(
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 0],
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 1],
9
)
);
assert_eq!(
-1,
mem_ops.memcmp(
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 0, 0],
&[0_u8, 0, 0, 0, 0, 0, 0, 0, 0, 1],
10
)
);
assert_eq!(0, mem_ops.memcmp(&[0_u8; 8], &[0_u8; 8], 8));
assert_eq!(-1, mem_ops.memcmp(&[0_u8; 8], &[1_u8; 8], 8));
assert_eq!(-1, mem_ops.memcmp(&[0_u8; 16], &[1_u8; 16], 16));
assert_eq!(-1, mem_ops.memcmp(&[0_u8; 18], &[1_u8; 18], 18));
let one = &[0_u8; 18];
let two = &[1_u8; 18];
assert_eq!(-1, mem_ops.memcmp(&one[1..], &two[0..], 17));
assert_eq!(-1, mem_ops.memcmp(&one[1..], &two[1..], 17));
}

View File

@ -0,0 +1,23 @@
use solana_bpf_rust_mem::entrypoint::process_instruction;
use solana_program_test::*;
use solana_sdk::{
instruction::Instruction, pubkey::Pubkey, signature::Signer, transaction::Transaction,
};
#[tokio::test]
async fn test_mem() {
let program_id = Pubkey::new_unique();
let program_test = ProgramTest::new(
"solana_bpf_rust_mem",
program_id,
processor!(process_instruction),
);
let (mut banks_client, payer, recent_blockhash) = program_test.start().await;
let mut transaction = Transaction::new_with_payer(
&[Instruction::new_with_bincode(program_id, &(), vec![])],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer], recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
}

View File

@ -0,0 +1,20 @@
[package]
name = "solana-bpf-rust-membuiltins"
version = "1.8.0"
description = "Solana BPF test program written in Rust"
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
documentation = "https://docs.rs/solana-bpf-rust-mem"
edition = "2018"
[dependencies]
solana-bpf-rust-mem = { path = "../mem", version = "=1.8.0", features = [ "no-entrypoint" ] }
solana-program = { path = "../../../../sdk/program", version = "=1.8.0" }
[lib]
crate-type = ["cdylib"]
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

View File

@ -0,0 +1,39 @@
//! @brief Test builtin mem functions
#![cfg(target_arch = "bpf")]
#![feature(rustc_private)]
extern crate compiler_builtins;
use solana_bpf_rust_mem::{run_mem_tests, MemOps};
use solana_program::{custom_panic_default, entrypoint::SUCCESS};
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
#[derive(Default)]
struct MemOpSyscalls();
impl MemOps for MemOpSyscalls {
fn memcpy(&self, dst: &mut [u8], src: &[u8], n: usize) {
unsafe {
compiler_builtins::mem::memcpy(dst.as_mut_ptr(), src.as_ptr(), n);
}
}
unsafe fn memmove(&self, dst: *mut u8, src: *mut u8, n: usize) {
compiler_builtins::mem::memmove(dst, src, n);
}
fn memset(&self, s: &mut [u8], c: u8, n: usize) {
unsafe {
compiler_builtins::mem::memset(s.as_mut_ptr(), c as i32, n);
}
}
fn memcmp(&self, s1: &[u8], s2: &[u8], n: usize) -> i32 {
unsafe { compiler_builtins::mem::memcmp(s1.as_ptr(), s2.as_ptr(), n) }
}
}
let mem_ops = MemOpSyscalls::default();
run_mem_tests(mem_ops);
SUCCESS
}
custom_panic_default!();

View File

@ -12,7 +12,7 @@ use solana_sdk::{
};
#[tokio::test]
async fn test_noop() {
async fn test_sysvars() {
let program_id = Pubkey::new_unique();
let program_test = ProgramTest::new(
"solana_bpf_rust_sysvar",