Split out Rust BPF no-std stuff (#4968)

This commit is contained in:
Jack May 2019-07-08 20:28:05 -08:00 committed by GitHub
parent 49250f62aa
commit f9a2254688
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 164 additions and 217 deletions

View File

@ -12,16 +12,25 @@ export RUSTFLAGS="-D warnings"
do_bpf_check() {
_ cargo +"$rust_stable" fmt --all -- --check
_ cargo +"$rust_nightly" test --all
_ cargo +"$rust_nightly" clippy --all -- --version
_ cargo +"$rust_nightly" clippy --all -- --deny=warnings
_ cargo +"$rust_stable" audit
}
(
(
cd sdk/bpf/rust/rust-no-std
do_bpf_check
)
(
cd sdk/bpf/rust/rust-utils
do_bpf_check
)
(
cd sdk/bpf/rust/rust-test
do_bpf_check
)
for project in programs/bpf/rust/*/ ; do
(
cd "$project"

View File

@ -13,6 +13,7 @@ edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.17.0" }
solana-bpf-rust-128bit-dep = { path = "../128bit_dep", version = "0.17.0" }
[workspace]

View File

@ -3,6 +3,8 @@
#![no_std]
#![allow(unused_attributes)]
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use solana_sdk_bpf_utils::info;

View File

@ -15,5 +15,4 @@ edition = "2018"
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
[workspace]
members = []
members = []

View File

@ -7,3 +7,14 @@ extern crate solana_sdk_bpf_utils;
pub fn work(x: u128, y: u128) -> u128 {
x + y
}
#[cfg(test)]
mod test {
extern crate std;
use super::*;
#[test]
fn test_work() {
assert_eq!(3, work(1, 2));
}
}

View File

@ -13,6 +13,7 @@ edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.17.0" }
[workspace]
members = []

View File

@ -4,6 +4,8 @@
#[macro_use]
extern crate alloc;
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use alloc::vec::Vec;

View File

@ -14,6 +14,7 @@ edition = "2018"
[dependencies]
byteorder = { version = "1", default-features = false }
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.17.0" }
[workspace]
members = []

View File

@ -3,6 +3,8 @@
#![no_std]
#![allow(unused_attributes)]
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use byteorder::{ByteOrder, LittleEndian};

View File

@ -12,6 +12,7 @@ usage() {
echo " Supported actions:"
echo " build"
echo " clean"
echo " test"
echo " clippy"
echo " fmt"
echo ""
@ -26,6 +27,13 @@ perform_action() {
clean)
./../../../sdk/bpf/rust/clean.sh "$2"
;;
test)
(
cd "$2"
echo "test $2"
cargo +nightly test
)
;;
clippy)
(
cd "$2"

View File

@ -13,6 +13,7 @@ edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.17.0" }
[workspace]
members = []

View File

@ -4,6 +4,8 @@
#![allow(unreachable_code)]
#![allow(unused_attributes)]
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use solana_sdk_bpf_utils::entrypoint::*;

View File

@ -13,6 +13,7 @@ edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.17.0" }
[workspace]
members = []

View File

@ -3,6 +3,8 @@
#![no_std]
#![allow(unused_attributes)]
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use solana_sdk_bpf_utils::info;

View File

@ -13,6 +13,7 @@ edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.17.0" }
solana-bpf-rust-many-args-dep = { path = "../many_args_dep", version = "0.17.0" }
[workspace]

View File

@ -5,6 +5,8 @@
mod helper;
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use solana_sdk_bpf_utils::info;

View File

@ -14,6 +14,8 @@ edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
[workspace]
members = []
[dev_dependencies]
solana-sdk-bpf-test = { path = "../../../../sdk/bpf/rust/rust-test", version = "0.17.0" }
[workspace]
members = []

View File

@ -22,3 +22,27 @@ pub fn many_args(
info!(arg6, arg7, arg8, arg9, 0);
arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9
}
#[cfg(test)]
mod test {
extern crate solana_sdk_bpf_test;
extern crate std;
use super::*;
#[no_mangle]
pub unsafe fn sol_log_(message: *const u8, length: u64) {
let slice = std::slice::from_raw_parts(message, length as usize);
let string = std::str::from_utf8(&slice).unwrap();
std::println!("{}", string);
}
#[no_mangle]
pub fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
std::println!("{} {} {} {} {}", arg1, arg2, arg3, arg4, arg5);
}
#[test]
fn test_many_args() {
assert_eq!(45, many_args(1, 2, 3, 4, 5, 6, 7, 8, 9));
}
}

View File

@ -13,6 +13,7 @@ edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.17.0" }
[workspace]
members = []

View File

@ -4,12 +4,15 @@
#![allow(unreachable_code)]
#![allow(unused_attributes)]
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use solana_sdk_bpf_utils::entrypoint::*;
use solana_sdk_bpf_utils::log::*;
use solana_sdk_bpf_utils::{entrypoint, info};
#[derive(Debug, PartialEq)]
struct SStruct {
x: u64,
y: u64,
@ -59,3 +62,13 @@ fn process_instruction(ka: &mut [SolKeyedAccount], info: &SolClusterInfo, data:
info!("Success");
true
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_return_sstruct() {
assert_eq!(SStruct { x: 1, y: 2, z: 3 }, return_sstruct());
}
}

View File

@ -13,6 +13,7 @@ edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.17.0" }
[workspace]
members = []

View File

@ -3,6 +3,8 @@
#![no_std]
#![allow(unused_attributes)]
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
#[no_mangle]

View File

@ -13,8 +13,8 @@ edition = "2018"
[dependencies]
byteorder = { version = "1", default-features = false }
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.17.0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.17.0" }
[workspace]
members = []

View File

@ -3,6 +3,8 @@
#![no_std]
#![allow(unreachable_code)]
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use byteorder::{ByteOrder, LittleEndian};

3
sdk/bpf/rust/rust-no-std/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target/
Cargo.lock

View File

@ -0,0 +1,16 @@
[package]
name = "solana-sdk-bpf-no-std"
version = "0.17.0"
description = "Solana BPF SDK Rust no_std support"
authors = ["Solana Maintainers <maintainers@solana.com>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../rust-utils" }
[workspace]
members = []

View File

@ -1,7 +1,7 @@
//! @brief Solana Rust-based BPF program memory allocator shim
use crate::log::*;
use core::alloc::{GlobalAlloc, Layout};
use solana_sdk_bpf_utils::log::*;
pub struct Allocator;
unsafe impl GlobalAlloc for Allocator {

View File

@ -0,0 +1,20 @@
//! @brief Solana Rust-based BPF program utility functions and types
#![no_std]
#![feature(allocator_api)]
#![feature(alloc_error_handler)]
#![feature(panic_info_message)]
#![feature(compiler_builtins_lib)]
#![feature(lang_items)]
#![cfg(not(test))]
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
extern crate compiler_builtins;
pub mod allocator;
pub mod panic;
#[global_allocator]
static A: allocator::Allocator = allocator::Allocator;

View File

@ -3,7 +3,6 @@
use core::panic::PanicInfo;
use core::ptr;
#[cfg(not(test))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
// Message is ignored for now to avoid incurring formatting program size overhead

3
sdk/bpf/rust/rust-test/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target/
Cargo.lock

View File

@ -0,0 +1,13 @@
[package]
name = "solana-sdk-bpf-test"
version = "0.17.0"
description = "Solana BPF SDK Rust Cargo test utilities"
authors = ["Solana Maintainers <maintainers@solana.com>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
edition = "2018"
[workspace]
members = []

View File

@ -0,0 +1,13 @@
//! @brief Solana Rust-based BPF program utility functions and types
#[no_mangle]
pub unsafe fn sol_log_(message: *const u8, length: u64) {
let slice = std::slice::from_raw_parts(message, length as usize);
let string = std::str::from_utf8(&slice).unwrap();
std::println!("{}", string);
}
#[no_mangle]
pub fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
std::println!("{} {} {} {} {}", arg1, arg2, arg3, arg4, arg5);
}

View File

@ -117,80 +117,3 @@ pub unsafe fn deserialize<'a>(
Ok((kas, info, data))
}
#[cfg(test)]
mod tests {
extern crate std;
use self::std::ffi::CStr;
use self::std::println;
use self::std::string::String;
use super::*;
use core::mem;
#[no_mangle]
fn sol_log_(message: *const u8) {
let scenario = get_log_scenario();
let c_str = unsafe { CStr::from_ptr(message as *const i8) };
let string = c_str.to_str().unwrap();
println!("{:?}", string);
}
#[no_mangle]
fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
println!("{:?} {:?} {:?} {:?} {:?}", arg1, arg2, arg3, arg4, arg5);
}
#[test]
fn test_entrypoint() {
set_log_scenario(4);
set_log_64_scenario(4);
let mut input: [u8; 154] = [
1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 151, 116, 3, 85, 181, 39, 151, 99, 155,
29, 208, 191, 255, 191, 11, 161, 4, 43, 104, 189, 202, 240, 231, 111, 146, 255, 199,
71, 67, 34, 254, 68, 48, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 190, 103, 191,
69, 193, 202, 38, 193, 95, 62, 131, 135, 105, 13, 142, 240, 155, 120, 177, 90, 212, 54,
10, 118, 40, 33, 192, 8, 54, 141, 187, 63,
];
if let Ok((mut ka, info, data)) = deserialize(&mut input[0] as *mut u8) {
let account0 = match mem::replace(&mut ka[0], None) {
Some(mut account0) => account0,
None => {
panic!("Error: account not found");
}
};
for k in ka[1..].iter() {
if let Some(_) = k {
panic!("Too many keyed accounts found");
}
}
assert_eq!(true, account0.is_signer);
let key: &[u8; SIZE_PUBKEY] = &[
151, 116, 3, 85, 181, 39, 151, 99, 155, 29, 208, 191, 255, 191, 11, 161, 4, 43,
104, 189, 202, 240, 231, 111, 146, 255, 199, 71, 67, 34, 254, 68,
];
assert_eq!(SIZE_PUBKEY, account0.key.key.len());
assert_eq!(key, account0.key.key);
assert_eq!(48, account0.lamports);
assert_eq!(1, account0.data.len());
let owner = &[0; SIZE_PUBKEY];
assert_eq!(SIZE_PUBKEY, account0.owner.key.len());
assert_eq!(owner, account0.owner.key);
let d = [1, 0, 0, 0, 0, 0, 0, 0, 1];
assert_eq!(9, data.len());
assert_eq!(d, data);
assert_eq!(1, info.tick_height);
let program_id: &[u8; SIZE_PUBKEY] = &[
190, 103, 191, 69, 193, 202, 38, 193, 95, 62, 131, 135, 105, 13, 142, 240, 155,
120, 177, 90, 212, 54, 10, 118, 40, 33, 192, 8, 54, 141, 187, 63,
];
assert_eq!(program_id, info.program_id.key);
} else {
panic!("Failed to deserialize");
}
}
}

View File

@ -1,20 +1,6 @@
//! @brief Solana Rust-based BPF program utility functions and types
#![no_std]
#![feature(allocator_api)]
#![feature(alloc_error_handler)]
#![feature(panic_info_message)]
#![feature(compiler_builtins_lib)]
#![feature(lang_items)]
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
extern crate compiler_builtins;
pub mod allocator;
pub mod entrypoint;
pub mod log;
pub mod panic;
#[global_allocator]
static A: allocator::Allocator = allocator::Allocator;

View File

@ -91,122 +91,3 @@ pub fn sol_log_params(ka: &[SolKeyedAccount], data: &[u8]) {
sol_log("Instruction data");
sol_log_slice(data);
}
#[cfg(test)]
mod tests {
extern crate std;
use self::std::ffi::CStr;
use self::std::println;
use self::std::string::String;
use super::*;
use core::mem;
static mut _LOG_SCENARIO: u64 = 4;
fn get_log_scenario() -> u64 {
unsafe { _LOG_SCENARIO }
}
fn set_log_scenario(test: u64) {
unsafe { _LOG_SCENARIO = test };
}
#[no_mangle]
fn sol_log_(message: *const u8) {
let scenario = get_log_scenario();
let c_str = unsafe { CStr::from_ptr(message as *const i8) };
let string = c_str.to_str().unwrap();
match scenario {
1 => assert_eq!(string, "This is a test message"),
2 => assert_eq!(string, "Attempted to log a string that is too long"),
3 => {
let s: String = ['a'; 126].iter().collect();
assert_eq!(string, s);
}
4 => println!("{:?}", string),
_ => panic!("Unknown sol_log test"),
}
}
static mut _LOG_64_SCENARIO: u64 = 4;
fn get_log_64_scenario() -> u64 {
unsafe { _LOG_64_SCENARIO }
}
fn set_log_64_scenario(test: u64) {
unsafe { _LOG_64_SCENARIO = test };
}
#[no_mangle]
fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
let scenario = get_log_64_scenario();
match scenario {
1 => {
assert_eq!(1, arg1);
assert_eq!(2, arg2);
assert_eq!(3, arg3);
assert_eq!(4, arg4);
assert_eq!(5, arg5);
}
2 => {
assert_eq!(0, arg1);
assert_eq!(0, arg2);
assert_eq!(0, arg3);
assert_eq!(arg4 + 1, arg5);
}
3 => {
assert_eq!(0, arg1);
assert_eq!(0, arg2);
assert_eq!(0, arg3);
assert_eq!(arg4 + 1, arg5);
}
4 => println!("{:?} {:?} {:?} {:?} {:?}", arg1, arg2, arg3, arg4, arg5),
_ => panic!("Unknown sol_log_64 test"),
}
}
#[test]
fn test_sol_log() {
set_log_scenario(1);
sol_log("This is a test message");
}
#[test]
fn test_sol_log_long() {
set_log_scenario(3);
let s: String = ['a'; 256].iter().collect();
sol_log(&s);
}
#[test]
fn test_sol_log_max_length() {
set_log_scenario(3);
let s: String = ['a'; 126].iter().collect();
sol_log(&s);
}
#[test]
fn test_sol_log_64() {
set_log_64_scenario(1);
sol_log_64(1, 2, 3, 4, 5);
}
#[test]
fn test_sol_log_key() {
set_log_64_scenario(2);
let key_array = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32,
];
let key = SolPubkey { key: &key_array };
sol_log_key(&key);
}
#[test]
fn test_sol_log_slice() {
set_log_64_scenario(3);
let array = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32,
];
sol_log_slice(&array);
}
}