Skip to content

Commit 147bb09

Browse files
d-e-s-oinsearchoflosttime
authored andcommitted
libbpf-rs: Add test attaching a BPF program to a uprobe
This change adds a test that checks that we can correctly attach a BPF program to a uprobe on a function. Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent de00fbe commit 147bb09

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

libbpf-rs/tests/bin/src/uprobe.bpf.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/usdt.bpf.h>
6+
7+
struct {
8+
__uint(type, BPF_MAP_TYPE_RINGBUF);
9+
__uint(max_entries, 4096 /* one page */);
10+
} ringbuf SEC(".maps");
11+
12+
SEC("uprobe")
13+
int handle__uprobe(void *ctx)
14+
{
15+
int *value;
16+
17+
value = bpf_ringbuf_reserve(&ringbuf, sizeof(int), 0);
18+
if (value) {
19+
*value = 1;
20+
bpf_ringbuf_submit(value, 0);
21+
}
22+
23+
return 0;
24+
}
25+
26+
SEC("uprobe")
27+
int handle__uprobe_with_cookie(void *ctx)
28+
{
29+
int *value;
30+
31+
value = bpf_ringbuf_reserve(&ringbuf, sizeof(int), 0);
32+
if (value) {
33+
*value = bpf_get_attach_cookie(ctx);
34+
bpf_ringbuf_submit(value, 0);
35+
}
36+
37+
return 0;
38+
}
39+
40+
char LICENSE[] SEC("license") = "GPL";

libbpf-rs/tests/bin/uprobe.bpf.o

19.7 KB
Binary file not shown.

libbpf-rs/tests/test.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use scopeguard::defer;
1414

1515
use libbpf_rs::{
1616
num_possible_cpus, Iter, Map, MapFlags, MapType, Object, ObjectBuilder, OpenObject,
17-
ProgramType, TracepointOpts, UsdtOpts,
17+
ProgramType, TracepointOpts, UprobeOpts, UsdtOpts,
1818
};
1919

2020
fn get_test_object_path(filename: &str) -> PathBuf {
@@ -937,3 +937,72 @@ fn test_object_tracepoint_with_opts() {
937937

938938
assert_eq!(result, cookie_val.into());
939939
}
940+
941+
#[inline(never)]
942+
#[no_mangle]
943+
extern "C" fn uprobe_target() -> usize {
944+
42
945+
}
946+
947+
/// Check that we can attach a BPF program to a uprobe.
948+
#[test]
949+
fn test_object_uprobe_with_opts() {
950+
bump_rlimit_mlock();
951+
952+
let mut obj = get_test_object("uprobe.bpf.o");
953+
let prog = obj
954+
.prog_mut("handle__uprobe")
955+
.expect("Failed to find program");
956+
957+
let pid = unsafe { libc::getpid() };
958+
let path = std::env::current_exe().expect("Failed to find executable name");
959+
let func_offset = 0;
960+
let opts = UprobeOpts {
961+
func_name: "uprobe_target".to_string(),
962+
..Default::default()
963+
};
964+
let _link = prog
965+
.attach_uprobe_with_opts(pid, path, func_offset, opts)
966+
.expect("Failed to attach prog");
967+
968+
let map = obj.map("ringbuf").expect("Failed to get ringbuf map");
969+
let action = || {
970+
let _ = uprobe_target();
971+
};
972+
let result = with_ringbuffer(map, action);
973+
974+
assert_eq!(result, 1);
975+
}
976+
977+
/// Check that we can attach a BPF program to a uprobe and access the cookie
978+
/// provided during attach.
979+
#[test]
980+
fn test_object_uprobe_with_cookie() {
981+
bump_rlimit_mlock();
982+
983+
let cookie_val = 5u16;
984+
let mut obj = get_test_object("uprobe.bpf.o");
985+
let prog = obj
986+
.prog_mut("handle__uprobe_with_cookie")
987+
.expect("Failed to find program");
988+
989+
let pid = unsafe { libc::getpid() };
990+
let path = std::env::current_exe().expect("Failed to find executable name");
991+
let func_offset = 0;
992+
let opts = UprobeOpts {
993+
func_name: "uprobe_target".to_string(),
994+
cookie: cookie_val.into(),
995+
..Default::default()
996+
};
997+
let _link = prog
998+
.attach_uprobe_with_opts(pid, path, func_offset, opts)
999+
.expect("Failed to attach prog");
1000+
1001+
let map = obj.map("ringbuf").expect("Failed to get ringbuf map");
1002+
let action = || {
1003+
let _ = uprobe_target();
1004+
};
1005+
let result = with_ringbuffer(map, action);
1006+
1007+
assert_eq!(result, cookie_val.into());
1008+
}

0 commit comments

Comments
 (0)