Add libstd support to Rust BPF (#5788)

This commit is contained in:
Jack May
2019-09-04 16:00:11 -07:00
committed by GitHub
parent ceaf4781b0
commit 5fb2d7a98f
46 changed files with 62 additions and 271 deletions

View File

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

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,12 +1,6 @@
//! @brief Example Rust-based BPF program tests loop iteration
#![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;
#[no_mangle]

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,7 +1,5 @@
//! @brief Solana Rust-based BPF program utility functions and types
#![no_std]
extern crate solana_sdk_bpf_utils;
pub fn uadd(x: u128, y: u128) -> u128 {

View File

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

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,24 +1,18 @@
//! @brief Example Rust-based BPF program that test dynamic memory allocation
#![no_std]
#![allow(unused_attributes)]
#[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;
use core::alloc::Layout;
use core::mem;
use solana_sdk_bpf_utils::info;
use std::alloc::Layout;
use std::mem;
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
unsafe {
// Confirm large allocation fails
let layout = Layout::from_size_align(core::usize::MAX, mem::align_of::<u8>()).unwrap();
let layout = Layout::from_size_align(std::usize::MAX, mem::align_of::<u8>()).unwrap();
let ptr = alloc::alloc::alloc(layout);
if !ptr.is_null() {
info!("Error: Alloc of very larger buffer should fail");
@ -59,7 +53,7 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
alloc::alloc::dealloc(ptr, layout);
}
// // TODO not supported for system or bump allocator
// TODO not supported bump allocator
// unsafe {
// // Test alloc all bytes and one more (assumes heap size of 2048)
@ -93,7 +87,7 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
}
{
// TODO test Vec::new()
// test Vec::new()
const ITERS: usize = 100;
let mut v = Vec::new();

View File

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

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,12 +1,6 @@
//! @brief Example Rust-based BPF program tests dependent crates
#![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};
use solana_sdk_bpf_utils::info;

View File

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

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,15 +1,8 @@
//! @brief Example Rust-based BPF program that moves a lamport from one account to another
#![no_std]
#![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::entrypoint::*;
use solana_sdk_bpf_utils::{entrypoint, info};
entrypoint!(process_instruction);
fn process_instruction(ka: &mut [SolKeyedAccount], _info: &SolClusterInfo, _data: &[u8]) -> bool {
@ -18,6 +11,5 @@ fn process_instruction(ka: &mut [SolKeyedAccount], _info: &SolClusterInfo, _data
// is seen by the runtime and fails as expected
*ka[0].lamports -= 1;
info!("Success");
true
}

View File

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

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,12 +1,6 @@
//! @brief Example Rust-based BPF program tests loop iteration
#![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;
#[no_mangle]

View File

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

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,14 +1,7 @@
//! @brief Example Rust-based BPF program tests loop iteration
#![no_std]
#![allow(unused_attributes)]
mod helper;
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use solana_sdk_bpf_utils::info;
#[no_mangle]

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,9 +1,6 @@
//! @brief Solana Rust-based BPF program utility functions and types
#![no_std]
extern crate solana_sdk_bpf_utils;
use solana_sdk_bpf_utils::info;
pub fn many_args(

View File

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

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,13 +1,8 @@
//! @brief Example Rust-based BPF program that prints out the parameters passed to it
#![no_std]
#![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};
@ -36,17 +31,11 @@ fn process_instruction(ka: &mut [SolKeyedAccount], info: &SolClusterInfo, data:
sol_log_params(ka, data);
{
// Test - arch config
#[cfg(not(target_arch = "bpf"))]
panic!();
}
{
// Test - use core methods, unwrap
// Test - use std methods, unwrap
// valid bytes, in a stack-allocated array
let sparkle_heart = [240, 159, 146, 150];
let result_str = core::str::from_utf8(&sparkle_heart).unwrap();
let result_str = std::str::from_utf8(&sparkle_heart).unwrap();
assert_eq!(4, result_str.len());
assert_eq!("💖", result_str);
info!(result_str);
@ -59,6 +48,12 @@ fn process_instruction(ka: &mut [SolKeyedAccount], info: &SolClusterInfo, data:
assert_eq!(s.x + s.y + s.z, 6);
}
{
// Test - arch config
#[cfg(not(target_arch = "bpf"))]
panic!();
}
info!("Success");
true
}

View File

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

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,10 +1,5 @@
//! @brief Example Rust-based BPF program that panics
#![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,7 +13,6 @@ edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.19.0-pre0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.19.0-pre0" }
solana-bpf-rust-param-passing-dep = { path = "../param_passing_dep", version = "0.19.0-pre0" }
[dev_dependencies]

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,12 +1,6 @@
//! @brief Example Rust-based BPF program tests loop iteration
#![no_std]
#![allow(unused_attributes)]
#[cfg(not(test))]
extern crate solana_sdk_bpf_no_std;
extern crate solana_sdk_bpf_utils;
use solana_bpf_rust_param_passing_dep::{Data, TestDep};
use solana_sdk_bpf_utils::info;

View File

@ -13,7 +13,6 @@ edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../../../../sdk/bpf/rust/rust-utils", version = "0.19.0-pre0" }
solana-sdk-bpf-no-std = { path = "../../../../sdk/bpf/rust/rust-no-std", version = "0.19.0-pre0" }
[dev_dependencies]
solana-sdk-bpf-test = { path = "../../../../sdk/bpf/rust/rust-test", version = "0.19.0-pre0" }

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,8 +1,5 @@
//! @brief Example Rust-based BPF program tests loop iteration
#![no_std]
#![allow(unused_attributes)]
extern crate solana_sdk_bpf_utils;
pub struct Data<'a> {

View File

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

View File

@ -1,6 +1,2 @@
[dependencies.compiler_builtins]
path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/compiler-builtins"
features = ["c", "mem"]
[target.bpfel-unknown-unknown.dependencies]
alloc = { path = "../../../../sdk/bpf/dependencies/rust-bpf-sysroot/src/liballoc" }
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -1,12 +1,6 @@
//! @brief Example Rust-based BPF program that prints out the parameters passed to it
#![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};
use solana_sdk_bpf_utils::entrypoint::*;
use solana_sdk_bpf_utils::{entrypoint, info};