Procházet zdrojové kódy

fix(examples): Restructure allowed_memory test

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 <wouter.dullaert@exoscale.ch>
Wouter Dullaert před 9 měsíci
rodič
revize
e4d054275d

binární
examples/allowed-memory.o


+ 6 - 0
examples/allowed-memory/.cargo/config.toml

@@ -0,0 +1,6 @@
+[build]
+target = "bpfel-unknown-none"
+rustflags = "-C debuginfo=2 -C link-arg=--btf"
+
+[unstable]
+build-std = ["core"]

+ 31 - 0
examples/allowed-memory/Cargo.toml

@@ -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
+

binární
examples/allowed-memory/allowed-memory.o


+ 13 - 0
examples/allowed-memory/rust-toolchain.toml

@@ -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",
+]

+ 0 - 0
examples/ebpf-allowed-memory.rs → examples/allowed-memory/src/main.rs


+ 2 - 9
examples/allowed_memory.rs

@@ -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;
 
@@ -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();