mirror of https://github.com/aya-rs/aya
parent
570c505e77
commit
10f39c7354
@ -0,0 +1,51 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use aya_ebpf::{
|
||||||
|
bindings::path,
|
||||||
|
cty::c_long,
|
||||||
|
helpers::bpf_d_path,
|
||||||
|
macros::{fentry, map},
|
||||||
|
maps::Array,
|
||||||
|
memcpy,
|
||||||
|
programs::FEntryContext,
|
||||||
|
};
|
||||||
|
use integration_common::bpf_d_path::{PATH_BUF_LEN, TestResult};
|
||||||
|
#[cfg(not(test))]
|
||||||
|
extern crate ebpf_panic;
|
||||||
|
|
||||||
|
#[map]
|
||||||
|
static RESULT: Array<TestResult> = Array::with_max_entries(1, 0);
|
||||||
|
|
||||||
|
#[fentry]
|
||||||
|
pub fn test_vfs_open(ctx: FEntryContext) -> u32 {
|
||||||
|
match try_vfs_open(ctx) {
|
||||||
|
Ok(_) => 0,
|
||||||
|
Err(_) => 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_vfs_open(ctx: FEntryContext) -> Result<(), c_long> {
|
||||||
|
let dest = &mut [0u8; PATH_BUF_LEN];
|
||||||
|
let pathptr: *const path = unsafe { ctx.arg(0) };
|
||||||
|
let data = unsafe { bpf_d_path(pathptr, dest)? };
|
||||||
|
|
||||||
|
let mut result = TestResult {
|
||||||
|
buf: [0u8; PATH_BUF_LEN],
|
||||||
|
len: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
memcpy(
|
||||||
|
result.buf.as_mut_ptr(),
|
||||||
|
data.as_ptr() as *mut u8,
|
||||||
|
data.len(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
result.len = dest.len();
|
||||||
|
|
||||||
|
RESULT.set(0, &result, 0)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
|
||||||
|
use aya::{Btf, Ebpf, maps::Array, programs::FEntry};
|
||||||
|
use integration_common::bpf_d_path::TestResult;
|
||||||
|
|
||||||
|
#[test_log::test]
|
||||||
|
fn bpf_d_path_basic() {
|
||||||
|
let btf = Btf::from_sys_fs().unwrap();
|
||||||
|
let mut bpf = Ebpf::load(crate::BPF_D_PATH).unwrap();
|
||||||
|
let prog: &mut FEntry = bpf
|
||||||
|
.program_mut("test_vfs_open")
|
||||||
|
.unwrap()
|
||||||
|
.try_into()
|
||||||
|
.unwrap();
|
||||||
|
prog.load("vfs_open", &btf).unwrap();
|
||||||
|
prog.attach().unwrap();
|
||||||
|
|
||||||
|
let file = File::open("/dev/null").unwrap();
|
||||||
|
|
||||||
|
let result = get_result(&bpf);
|
||||||
|
|
||||||
|
let path_len = result.len;
|
||||||
|
assert!(path_len > 0, "Path length should be greater than 0");
|
||||||
|
assert!(
|
||||||
|
path_len <= result.buf.len(),
|
||||||
|
"Path length should not exceed buffer size"
|
||||||
|
);
|
||||||
|
|
||||||
|
let path_str = std::str::from_utf8(&result.buf[..path_len]).unwrap();
|
||||||
|
assert!(
|
||||||
|
path_str.contains("/dev/null"),
|
||||||
|
"Path should contain '/dev/null', got: {}",
|
||||||
|
path_str
|
||||||
|
);
|
||||||
|
|
||||||
|
drop(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_result(bpf: &Ebpf) -> TestResult {
|
||||||
|
let m = Array::<_, TestResult>::try_from(bpf.map("RESULT").unwrap()).unwrap();
|
||||||
|
m.get(&0, 0).unwrap()
|
||||||
|
}
|
Loading…
Reference in New Issue