-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpf: add selftest for stackmap with BPF_F_STACK_BUILD_ID
test_stacktrace_build_id() is added. It accesses tracepoint urandom_read with "dd" and "urandom_read" and gathers stack traces. Then it reads the stack traces from the stackmap. urandom_read is a statically link binary that reads from /dev/urandom. test_stacktrace_build_id() calls readelf to read build ID of urandom_read and compares it with build ID from the stackmap. Signed-off-by: Song Liu <songliubraving@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
- Loading branch information
Song Liu
authored and
Daniel Borkmann
committed
Mar 15, 2018
1 parent
615755a
commit 81f77fd
Showing
5 changed files
with
278 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
// Copyright (c) 2018 Facebook | ||
|
||
#include <linux/bpf.h> | ||
#include "bpf_helpers.h" | ||
|
||
#ifndef PERF_MAX_STACK_DEPTH | ||
#define PERF_MAX_STACK_DEPTH 127 | ||
#endif | ||
|
||
struct bpf_map_def SEC("maps") control_map = { | ||
.type = BPF_MAP_TYPE_ARRAY, | ||
.key_size = sizeof(__u32), | ||
.value_size = sizeof(__u32), | ||
.max_entries = 1, | ||
}; | ||
|
||
struct bpf_map_def SEC("maps") stackid_hmap = { | ||
.type = BPF_MAP_TYPE_HASH, | ||
.key_size = sizeof(__u32), | ||
.value_size = sizeof(__u32), | ||
.max_entries = 10000, | ||
}; | ||
|
||
struct bpf_map_def SEC("maps") stackmap = { | ||
.type = BPF_MAP_TYPE_STACK_TRACE, | ||
.key_size = sizeof(__u32), | ||
.value_size = sizeof(struct bpf_stack_build_id) | ||
* PERF_MAX_STACK_DEPTH, | ||
.max_entries = 128, | ||
.map_flags = BPF_F_STACK_BUILD_ID, | ||
}; | ||
|
||
/* taken from /sys/kernel/debug/tracing/events/random/urandom_read/format */ | ||
struct random_urandom_args { | ||
unsigned long long pad; | ||
int got_bits; | ||
int pool_left; | ||
int input_left; | ||
}; | ||
|
||
SEC("tracepoint/random/urandom_read") | ||
int oncpu(struct random_urandom_args *args) | ||
{ | ||
__u32 key = 0, val = 0, *value_p; | ||
|
||
value_p = bpf_map_lookup_elem(&control_map, &key); | ||
if (value_p && *value_p) | ||
return 0; /* skip if non-zero *value_p */ | ||
|
||
/* The size of stackmap and stackid_hmap should be the same */ | ||
key = bpf_get_stackid(args, &stackmap, BPF_F_USER_STACK); | ||
if ((int)key >= 0) | ||
bpf_map_update_elem(&stackid_hmap, &key, &val, 0); | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
__u32 _version SEC("version") = 1; /* ignored by tracepoints, required by libbpf.a */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <stdlib.h> | ||
|
||
#define BUF_SIZE 256 | ||
int main(void) | ||
{ | ||
int fd = open("/dev/urandom", O_RDONLY); | ||
int i; | ||
char buf[BUF_SIZE]; | ||
|
||
if (fd < 0) | ||
return 1; | ||
for (i = 0; i < 4; ++i) | ||
read(fd, buf, BUF_SIZE); | ||
|
||
close(fd); | ||
return 0; | ||
} |