Allow BPF structure passing and returning (#2100)
* Add BPF struct passing and returning tests
This commit is contained in:
17
programs/bpf/c/src/struct_pass.c
Normal file
17
programs/bpf/c/src/struct_pass.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include <solana_sdk.h>
|
||||||
|
|
||||||
|
struct foo {const uint8_t *input;};
|
||||||
|
void foo(const uint8_t *input, struct foo foo) ;
|
||||||
|
|
||||||
|
extern bool entrypoint(const uint8_t *input) {
|
||||||
|
struct foo f;
|
||||||
|
f.input = input;
|
||||||
|
foo(input, f);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void foo(const uint8_t *input, struct foo foo) {
|
||||||
|
sol_log_64(0, 0, 0, (uint64_t)input, (uint64_t)foo.input);
|
||||||
|
sol_assert(input == foo.input);
|
||||||
|
}
|
||||||
|
|
18
programs/bpf/c/src/struct_ret.c
Normal file
18
programs/bpf/c/src/struct_ret.c
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include <solana_sdk.h>
|
||||||
|
|
||||||
|
struct foo {const uint8_t *input;};
|
||||||
|
struct foo bar(const uint8_t *input);
|
||||||
|
|
||||||
|
extern bool entrypoint(const uint8_t *input) {
|
||||||
|
struct foo foo = bar(input);
|
||||||
|
sol_log_64(0, 0, 0, (uint64_t)input, (uint64_t)foo.input);
|
||||||
|
sol_assert(input == foo.input);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct foo bar(const uint8_t *input) {
|
||||||
|
struct foo foo;
|
||||||
|
foo.input = input;
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
|
@ -18,6 +18,9 @@ fn main() {
|
|||||||
println!("cargo:rerun-if-changed=../../bpf/c/src/bench_alu.c");
|
println!("cargo:rerun-if-changed=../../bpf/c/src/bench_alu.c");
|
||||||
println!("cargo:rerun-if-changed=../../bpf/c/src/move_funds.c");
|
println!("cargo:rerun-if-changed=../../bpf/c/src/move_funds.c");
|
||||||
println!("cargo:rerun-if-changed=../../bpf/c/src/noop.c");
|
println!("cargo:rerun-if-changed=../../bpf/c/src/noop.c");
|
||||||
|
println!("cargo:rerun-if-changed=../../bpf/c/src/noop++.c");
|
||||||
|
println!("cargo:rerun-if-changed=../../bpf/c/src/struct_pass.c");
|
||||||
|
println!("cargo:rerun-if-changed=../../bpf/c/src/struct_ret.c");
|
||||||
println!("cargo:warning=(not a warning) Compiling C-based BPF programs");
|
println!("cargo:warning=(not a warning) Compiling C-based BPF programs");
|
||||||
let status = Command::new("make")
|
let status = Command::new("make")
|
||||||
.current_dir("../../bpf/c")
|
.current_dir("../../bpf/c")
|
||||||
|
@ -32,7 +32,7 @@ if [[ ! -r criterion-$machine-$version.md ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Install LLVM
|
# Install LLVM
|
||||||
version=v0.0.5
|
version=v0.0.6
|
||||||
if [[ ! -f llvm-native-$machine-$version.md ]]; then
|
if [[ ! -f llvm-native-$machine-$version.md ]]; then
|
||||||
(
|
(
|
||||||
filename=solana-llvm-$machine.tar.bz2
|
filename=solana-llvm-$machine.tar.bz2
|
||||||
|
@ -296,10 +296,18 @@ fn test_program_builtin_bpf_noop() {
|
|||||||
|
|
||||||
#[cfg(feature = "bpf_c")]
|
#[cfg(feature = "bpf_c")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_program_bpf_noop_c() {
|
fn test_program_bpf_c() {
|
||||||
logger::setup();
|
logger::setup();
|
||||||
|
|
||||||
let mut file = File::open(create_bpf_path("noop")).expect("file open failed");
|
let programs = [
|
||||||
|
"noop",
|
||||||
|
"struct_pass",
|
||||||
|
"struct_ret",
|
||||||
|
//"noop++" // TODO fails with buffer overflow
|
||||||
|
];
|
||||||
|
for program in programs.iter() {
|
||||||
|
println!("Test program: {:?}", program);
|
||||||
|
let mut file = File::open(create_bpf_path(program)).expect("file open failed");
|
||||||
let mut elf = Vec::new();
|
let mut elf = Vec::new();
|
||||||
file.read_to_end(&mut elf).unwrap();
|
file.read_to_end(&mut elf).unwrap();
|
||||||
|
|
||||||
@ -321,3 +329,4 @@ fn test_program_bpf_noop_c() {
|
|||||||
loader.bank.process_transactions(&vec![tx.clone()]),
|
loader.bank.process_transactions(&vec![tx.clone()]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user