forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BPF Rust noop example (solana-labs#316)
- Loading branch information
Showing
16 changed files
with
605 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/target/ | ||
|
||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
# Note: This crate must be built using build.sh | ||
|
||
[package] | ||
name = "solana-bpf-rust-noop" | ||
version = "0.15.0" | ||
description = "Solana BPF noop program written in Rust" | ||
authors = ["Solana Maintainers <maintainers@solana.com>"] | ||
repository = "https://github.com/solana-labs/solana" | ||
license = "Apache-2.0" | ||
homepage = "https://solana.com/" | ||
|
||
[dependencies] | ||
# byteorder = { version = "1.3.1", default-features = false } | ||
# heapless = { version = "0.4.0", default-features = false } | ||
# byte = { version = "0.2", default-features = false } | ||
|
||
[workspace] | ||
members = [] | ||
|
||
[lib] | ||
name = "solana_bpf_rust_noop" | ||
crate-type = ["cdylib"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
|
||
[dependencies.compiler_builtins] | ||
path = "../../bpf-sdk/rust-bpf-sysroot/src/compiler-builtins" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
PHDRS | ||
{ | ||
text PT_LOAD ; | ||
rodata PT_LOAD ; | ||
dynamic PT_DYNAMIC ; | ||
} | ||
|
||
SECTIONS | ||
{ | ||
. = SIZEOF_HEADERS; | ||
.text : { *(.text) } :text | ||
.rodata : { *(.rodata) } :rodata | ||
.dynamic : { *(.dynamic) } :dynamic | ||
.dynsym : { *(.dynsym) } :dynamic | ||
.dynstr : { *(.dynstr) } :dynamic | ||
.gnu.hash : { *(.gnu.hash) } :dynamic | ||
.rel.dyn : { *(.rel.dyn) } :dynamic | ||
.hash : { *(.hash) } :dynamic | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env bash | ||
|
||
cd "$(dirname "$0")" | ||
|
||
cargo install xargo | ||
|
||
set -ex | ||
|
||
# Ensure the sdk is installed | ||
../../bpf-sdk/scripts/install.sh | ||
rustup override set bpf | ||
|
||
export RUSTFLAGS="$RUSTFLAGS \ | ||
-C lto=no \ | ||
-C opt-level=2 \ | ||
-C link-arg=-Tbpf.ld \ | ||
-C link-arg=-z -C link-arg=notext \ | ||
-C link-arg=--Bdynamic \ | ||
-C link-arg=-shared \ | ||
-C link-arg=--entry=entrypoint \ | ||
-C linker=../../bpf-sdk/llvm-native/bin/ld.lld" | ||
export XARGO_HOME="$PWD/target/xargo" | ||
export XARGO_RUST_SRC="../../bpf-sdk/rust-bpf-sysroot/src" | ||
# export XARGO_RUST_SRC="../../../../../rust-bpf-sysroot/src" | ||
xargo build --target bpfel-unknown-unknown --release -v | ||
|
||
{ { set +x; } 2>/dev/null; echo Success; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -ex | ||
|
||
cargo clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
|
||
cp dump.txt dump_last.txt 2>/dev/null | ||
|
||
set -x | ||
set -e | ||
|
||
./clean.sh | ||
./build.sh | ||
ls -la ./target/bpfel_unknown_unknown/release/solana_bpf_rust_noop.so > dump.txt | ||
greadelf -aW ./target/bpfel_unknown_unknown/release/solana_bpf_rust_noop.so | rustfilt >> dump.txt | ||
llvm-objdump -print-imm-hex --source --disassemble ./target/bpfel_unknown_unknown/release/solana_bpf_rust_noop.so >> dump.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//! @brief Example Rust-based BPF program that prints out the parameters passed to it | ||
#![cfg(not(test))] | ||
#![no_std] | ||
|
||
mod solana_sdk; | ||
|
||
use solana_sdk::*; | ||
|
||
struct SStruct { | ||
x: u64, | ||
y: u64, | ||
z: u64, | ||
} | ||
|
||
#[inline(never)] | ||
fn return_sstruct() -> SStruct { | ||
SStruct { x: 1, y: 2, z: 3 } | ||
} | ||
|
||
fn process(ka: &mut [SolKeyedAccount], data: &[u8], info: &SolClusterInfo) -> bool { | ||
sol_log("Tick height:"); | ||
sol_log_64(info.tick_height, 0, 0, 0, 0); | ||
sol_log("Program identifier:"); | ||
sol_log_key(&info.program_id); | ||
|
||
// Log the provided account keys and instruction input data. In the case of | ||
// the no-op program, no account keys or input data are expected but real | ||
// programs will have specific requirements so they can do their work. | ||
sol_log("Account keys and instruction input data:"); | ||
sol_log_params(ka, data); | ||
|
||
{ | ||
// Test - use core 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(); | ||
|
||
sol_log_64(0, 0, 0, 0, result_str.len() as u64); | ||
sol_log(result_str); | ||
assert_eq!("💖", result_str); | ||
} | ||
|
||
{ | ||
// Test - struct return | ||
let s = return_sstruct(); | ||
sol_log_64(0, 0, s.x, s.y, s.z); | ||
assert_eq!(s.x + s.y + s.z, 6); | ||
} | ||
|
||
sol_log("Success"); | ||
true | ||
} |
Oops, something went wrong.