Skip to content

Commit

Permalink
fix(examples): Restructure allowed_memory test
Browse files Browse the repository at this point in the history
Moving the source code for the ebpf program into a subfolder will prevent
it from accidentally being run as an example.
We are still committing the resulting object file, to prevent creating
a hard dependency on the aya build toolchain to run the example tests
in this repository.

Signed-off-by: Wouter Dullaert <[email protected]>
  • Loading branch information
wdullaer authored and qmonnet committed Sep 13, 2024
1 parent 384c882 commit e4d0542
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
Binary file removed examples/allowed-memory.o
Binary file not shown.
6 changes: 6 additions & 0 deletions examples/allowed-memory/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build]
target = "bpfel-unknown-none"
rustflags = "-C debuginfo=2 -C link-arg=--btf"

[unstable]
build-std = ["core"]
31 changes: 31 additions & 0 deletions examples/allowed-memory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "allowed-memory"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0/MIT"

[dependencies]
aya-ebpf = "0.1.0"

[[bin]]
name = "allowed-memory"
path = "src/main.rs"
test = false
bench = false

[profile.dev]
opt-level = 3
debug = false
debug-assertions = false
overflow-checks = false
lto = true
panic = "abort"
incremental = false
codegen-units = 1
rpath = false

[profile.release]
lto = true
panic = "abort"
codegen-units = 1

Binary file added examples/allowed-memory/allowed-memory.o
Binary file not shown.
13 changes: 13 additions & 0 deletions examples/allowed-memory/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[toolchain]
channel = "nightly"
# The source code of rustc, provided by the rust-src component, is needed for
# building eBPF programs.
components = [
"cargo",
"clippy",
"rust-docs",
"rust-src",
"rust-std",
"rustc",
"rustfmt",
]
File renamed without changes.
11 changes: 2 additions & 9 deletions examples/allowed_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ use std::{iter::FromIterator, ptr::addr_of};

extern crate rbpf;

// The following example uses an ELF file that was compiled from the ebpf-allowed-memory.rs file
// It is built using the [aya framework](https://aya-rs.dev/).
// Once the aya dependencies (rust-nightly, latest llvm and latest bpf-linker) are installed, it
// can be compiled via
//
// ```bash
// cargo build --target=bpfel-unknown-none -Z build-std=core
// ```
const OBJ_FILE_PATH: &str = "examples/allowed-memory/allowed-memory.o";

const BPF_MAP_LOOKUP_ELEM_IDX: u32 = 1;

Expand All @@ -41,7 +34,7 @@ fn bpf_lookup_elem(_map: u64, key_addr: u64, _flags: u64, _u4: u64, _u5: u64) ->
}

fn main() {
let file = elf::File::open_path("examples/allowed-memory.o").unwrap();
let file = elf::File::open_path(OBJ_FILE_PATH).unwrap();
let func = file.get_section("classifier").unwrap();

let mut vm = rbpf::EbpfVmNoData::new(Some(&func.data)).unwrap();
Expand Down

0 comments on commit e4d0542

Please sign in to comment.