Skip to content

Commit 8912e41

Browse files
swananantamird
authored andcommitted
aya: fix pid=0 handling in uprobe attach
perf_event_open treats pid=0 as the calling process/thread, so Some(0) should carry that meaning in UProbe::attach. However, aya unconditionally reads /proc/<pid>/maps when pid is Some, but /proc/0/maps does not exist. Use the current pid only for the ProcMap lookup, and keep pid=0 for the actual attach semantics. Add documentation and an integration test.
1 parent 3bcefca commit 8912e41

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

aya/src/programs/uprobe.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ impl UProbe {
109109
/// Attaches the uprobe to the function `fn_name` defined in the `target`.
110110
/// If the attach point specifies an offset, it is added to the address of
111111
/// the target function. If `pid` is not `None`, the program executes only
112-
/// when the target function is executed by the given `pid`.
112+
/// when the target function is executed by the given `pid`; `Some(0)`
113+
/// means the calling process/thread.
113114
///
114115
/// The `target` argument can be an absolute path to a binary or library, or
115116
/// a library name (eg: `"libc"`).
@@ -131,7 +132,10 @@ impl UProbe {
131132
pid: Option<u32>,
132133
) -> Result<UProbeLinkId, ProgramError> {
133134
let UProbeAttachPoint { location, cookie } = point.into();
134-
let proc_map = pid.map(ProcMap::new).transpose()?;
135+
// /proc/0/maps does not exist; use the current pid only for ProcMap
136+
// resolution and preserve pid=0 for the actual attach semantics.
137+
let proc_map_pid = pid.map(|pid| if pid == 0 { std::process::id() } else { pid });
138+
let proc_map = proc_map_pid.map(ProcMap::new).transpose()?;
135139
let path = resolve_attach_path(target.as_ref(), proc_map.as_ref())?;
136140
let (symbol, offset) = match location {
137141
UProbeAttachLocation::Symbol(s) => (Some(s), 0),

test/integration-test/src/tests/load.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,23 @@ fn basic_uprobe() {
368368
);
369369
}
370370

371+
#[test_log::test]
372+
fn basic_uprobe_pid_zero_is_self() {
373+
type P = UProbe;
374+
375+
let program_name = "test_uprobe";
376+
let attach = |prog: &mut P| {
377+
prog.attach("uprobe_function", "/proc/self/exe", Some(0))
378+
.unwrap()
379+
};
380+
run_unload_program_test(
381+
crate::TEST,
382+
program_name,
383+
attach,
384+
aya::features().bpf_perf_link(), // probe uses perf_attach.
385+
);
386+
}
387+
371388
#[test_log::test]
372389
fn basic_flow_dissector() {
373390
type P = FlowDissector;

0 commit comments

Comments
 (0)