Skip to content

Commit

Permalink
repro issue #808
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin JEROME <[email protected]>
  • Loading branch information
qjerome committed Oct 10, 2023
1 parent 66bd85a commit ca9dd66
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/integration-ebpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ path = "src/redirect.rs"
[[bin]]
name = "xdp_sec"
path = "src/xdp_sec.rs"

[[bin]]
name = "log_str_random"
path = "src/repro/log_str_random.rs"
6 changes: 6 additions & 0 deletions test/integration-ebpf/src/repro/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# the bug needs debug info so that it triggers
[profile.release]
debug = true

[profile.dev]
debug = true
47 changes: 47 additions & 0 deletions test/integration-ebpf/src/repro/log_str_random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// this file aims at reproducing the bug reported here: https://github.com/aya-rs/aya/issues/808
#![no_std]
#![no_main]

use aya_bpf::{helpers::bpf_get_prandom_u32, macros::kprobe, programs::ProbeContext};
use aya_log_ebpf::{error, info};

#[repr(C)]
enum Error {
Foo,
Bar,
}

impl Error {
const fn into_str(self) -> &'static str {
match self {
Self::Foo => "foo",
Self::Bar => "bar",
}
}
}

#[kprobe]
pub fn log_str_random(ctx: ProbeContext) -> u32 {
match try_log_str_random(ctx) {
Ok(ret) => ret,
Err(ret) => ret,
}
}

fn try_log_str_random(ctx: ProbeContext) -> Result<u32, u32> {
let err = {
if unsafe { bpf_get_prandom_u32() } % 2 == 0 {
Error::Bar
} else {
Error::Foo
}
};
error!(&ctx, "{}", err.into_str());
Ok(0)
}

#[cfg(not(test))]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
1 change: 1 addition & 0 deletions test/integration-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub const BPF_PROBE_READ: &[u8] =
include_bytes_aligned!(concat!(env!("OUT_DIR"), "/bpf_probe_read"));
pub const REDIRECT: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/redirect"));
pub const XDP_SEC: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/xdp_sec"));
pub const LOG_STR_RAND: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/log_str_random"));

#[cfg(test)]
mod tests;
Expand Down
16 changes: 16 additions & 0 deletions test/integration-test/src/tests/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,19 @@ fn pin_lifecycle_uprobe() {
// Make sure the function isn't optimized out.
uprobe_function();
}

#[test]
fn log_str_random() {
// userland code to reproduce bug reported here: https://github.com/aya-rs/aya/issues/808
// To reproduce the bug, eBPF code needs to be built with a custom
// bpf-linker from the feature/fix-di branch. Additionally, eBPF program
// needs to be built with debug information.
let mut bpf = Bpf::load(crate::LOG_STR_RAND).unwrap();
let prog: &mut KProbe = bpf
.program_mut("log_str_random")
.unwrap()
.try_into()
.unwrap();
prog.load().unwrap();
prog.attach("schedule", 0).unwrap();
}

0 comments on commit ca9dd66

Please sign in to comment.