Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libbpf-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ libbpf-sys = { version = "1.4.1", default-features = false, optional = true }
tempfile = { version = "3.3", optional = true }

[dev-dependencies]
goblin = "0.9.3"
libbpf-rs = {path = ".", features = ["generate-test-files"]}
libbpf-rs-dev = {path = "dev", features = ["generate-test-files"]}
log = "0.4.4"
Expand Down
14 changes: 11 additions & 3 deletions libbpf-rs/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use std::ffi::c_void;
use std::ffi::CStr;
use std::ffi::CString;
use std::ffi::OsStr;
use std::marker::PhantomData;
use std::mem;
Expand Down Expand Up @@ -52,7 +53,7 @@ pub struct UprobeOpts {
/// `func_name` and use `func_offset` argument to specify offset within the
/// function. Shared library functions must specify the shared library
/// binary_path.
pub func_name: String,
pub func_name: Option<String>,
#[doc(hidden)]
pub _non_exhaustive: (),
}
Expand Down Expand Up @@ -817,13 +818,20 @@ impl<'obj> ProgramMut<'obj> {
_non_exhaustive,
} = opts;

let func_name = util::str_to_cstring(&func_name)?;
let func_name: Option<CString> = if let Some(func_name) = func_name {
Some(util::str_to_cstring(&func_name)?)
} else {
None
};
let ptr = func_name
.as_ref()
.map_or(ptr::null(), |func_name| func_name.as_ptr());
let opts = libbpf_sys::bpf_uprobe_opts {
sz: size_of::<libbpf_sys::bpf_uprobe_opts>() as _,
ref_ctr_offset: ref_ctr_offset as libbpf_sys::size_t,
bpf_cookie: cookie,
retprobe,
func_name: func_name.as_ptr(),
func_name: ptr,
..Default::default()
};

Expand Down
44 changes: 44 additions & 0 deletions libbpf-rs/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use std::error::Error;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

use goblin::elf::sym;
use goblin::elf::Elf;
use libbpf_rs::Map;
use libbpf_rs::MapCore;
use libbpf_rs::MapMut;
Expand Down Expand Up @@ -85,3 +90,42 @@ where

value
}

pub fn get_symbol_offset(binary_path: &Path, symbol_name: &str) -> Result<usize, Box<dyn Error>> {
let buffer = fs::read(binary_path)?;
let elf = Elf::parse(&buffer)?;

// Check dynamic symbols
for sym in elf.dynsyms.iter() {
if sym.st_type() != sym::STT_FUNC {
continue;
}
if let Some(name) = elf.dynstrtab.get_at(sym.st_name) {
if name == symbol_name {
let sec = &elf.section_headers[sym.st_shndx];
let offset = sym.st_value - sec.sh_addr + sec.sh_offset;
return Ok(offset as usize);
}
}
}

// Check regular symbols
for sym in elf.syms.iter() {
if sym.st_type() != sym::STT_FUNC {
continue;
}
if let Some(name) = elf.strtab.get_at(sym.st_name) {
if name == symbol_name {
let sec = &elf.section_headers[sym.st_shndx];
let offset = sym.st_value - sec.sh_addr + sec.sh_offset;
return Ok(offset as usize);
}
}
}

Err(format!(
"Symbol `{}` not found in binary {:?}",
symbol_name, binary_path
)
.into())
}
27 changes: 25 additions & 2 deletions libbpf-rs/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use test_tag::tag;
use crate::common::get_map;
use crate::common::get_map_mut;
use crate::common::get_prog_mut;
use crate::common::get_symbol_offset;
use crate::common::get_test_object;
use crate::common::get_test_object_path;
use crate::common::open_test_object;
Expand Down Expand Up @@ -1684,7 +1685,7 @@ fn test_object_uprobe_with_opts() {
let path = current_exe().expect("failed to find executable name");
let func_offset = 0;
let opts = UprobeOpts {
func_name: "uprobe_target".to_string(),
func_name: Some("uprobe_target".into()),
..Default::default()
};
let _link = prog
Expand All @@ -1700,6 +1701,28 @@ fn test_object_uprobe_with_opts() {
assert_eq!(result, 1);
}

#[tag(root)]
#[test]
fn test_object_uprobe_with_func_offset() {
let mut obj = get_test_object("uprobe.bpf.o");
let prog = get_prog_mut(&mut obj, "handle__uprobe");

let pid = unsafe { libc::getpid() };
let path = current_exe().expect("failed to find executable name");
let func_offset = get_symbol_offset(&path, "uprobe_target").unwrap();
let _link = prog
.attach_uprobe_with_opts(pid, path, func_offset, Default::default())
.expect("failed to attach prog");

let map = get_map_mut(&mut obj, "ringbuf");
let action = || {
let _ = uprobe_target();
};
let result = with_ringbuffer(&map, action);

assert_eq!(result, 1);
}

/// Check that we can attach a BPF program to a uprobe and access the cookie
/// provided during attach.
#[tag(root)]
Expand All @@ -1713,7 +1736,7 @@ fn test_object_uprobe_with_cookie() {
let path = current_exe().expect("failed to find executable name");
let func_offset = 0;
let opts = UprobeOpts {
func_name: "uprobe_target".to_string(),
func_name: Some("uprobe_target".into()),
cookie: cookie_val.into(),
..Default::default()
};
Expand Down