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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
46 changed files with 62 additions and 271 deletions

View File

@ -24,7 +24,7 @@ _ ci/nits.sh
_ ci/order-crates-for-publishing.py
_ book/build.sh
for project in sdk/bpf/rust/{rust-no-std,rust-utils,rust-test} programs/bpf/rust/*/ ; do
for project in sdk/bpf/rust/{rust-utils,rust-test} programs/bpf/rust/*/ ; do
echo "+++ do_bpf_check $project"
(
cd "$project"

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};

1
sdk/bpf/.gitignore vendored
View File

@ -1,4 +1,5 @@
/dependencies/criterion*
/dependencies/hashbrown*
/dependencies/llvm-native*
/dependencies/rust-bpf*
/dependencies/xargo*

View File

@ -1,4 +0,0 @@
/target/
Cargo.lock
/farf/

View File

@ -1,16 +0,0 @@
[package]
name = "solana-sdk-bpf-no-std"
version = "0.19.0-pre0"
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,23 +0,0 @@
//! @brief Solana Rust-based BPF program memory allocator shim
use core::alloc::{GlobalAlloc, Layout};
use solana_sdk_bpf_utils::log::*;
pub struct Allocator;
unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
sol_alloc_free_(layout.size() as u64, 0)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
sol_alloc_free_(layout.size() as u64, ptr as u64);
}
}
extern "C" {
fn sol_alloc_free_(size: u64, ptr: u64) -> *mut u8;
}
#[alloc_error_handler]
fn my_alloc_error_handler(_: core::alloc::Layout) -> ! {
sol_log("alloc_error_handler");
panic!();
}

View File

@ -1,20 +0,0 @@
//! @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

@ -1,32 +0,0 @@
//! @brief Solana Rust-based BPF program panic handling
use core::panic::PanicInfo;
use core::ptr;
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
// Message is ignored for now to avoid incurring formatting overhead
match info.location() {
Some(location) => {
let mut file: [u8; 128] = [0; 128];
for (i, c) in location.file().as_bytes().iter().enumerate() {
if i > 127 {
break;
}
file[i] = *c;
}
unsafe {
sol_panic_(
file.as_ptr(),
file.len() as u64,
u64::from(location.line()),
u64::from(location.column()),
);
}
}
None => unsafe { sol_panic_(ptr::null(), 0, 0, 0) },
}
}
extern "C" {
pub fn sol_panic_(file: *const u8, len: u64, line: u64, column: u64) -> !;
}

View File

@ -1,6 +1,4 @@
//! @brief Solana Rust-based BPF program utility functions and types
#![no_std]
pub mod entrypoint;
pub mod log;

View File

@ -116,7 +116,7 @@ if [[ ! -f rust-bpf-$machine-$version.md ]]; then
fi
# Install Rust-BPF Sysroot sources
version=v0.8
version=v0.9
if [[ ! -f rust-bpf-sysroot-$version.md ]]; then
(
set -ex
@ -134,4 +134,23 @@ if [[ ! -f rust-bpf-sysroot-$version.md ]]; then
fi
fi
# Install custom Hashbrown crate needed by Rust-BPF Sysroot
version=v0.1
if [[ ! -f hashbrown-$version.md ]]; then
(
set -ex
rm -rf hashbrown*
rm -rf xargo
cmd="git clone --recursive --single-branch --branch $version https://github.com/solana-labs/hashbrown.git"
$cmd
echo "$cmd" > hashbrown-$version.md
)
exitcode=$?
if [[ $exitcode -ne 0 ]]; then
rm -rf hashbrown
exit 1
fi
fi
exit 0