Skip to content

Commit

Permalink
selftests/bpf: Fix the snprintf test
Browse files Browse the repository at this point in the history
The BPF program for the snprintf selftest runs on all syscall entries.
On busy multicore systems this can cause concurrency issues.

For example it was observed that sometimes the userspace part of the
test reads "    4 0000" instead of "    4 000" (extra '0' at the end)
which seems to happen just before snprintf on another core sets
end[-1] = '\0'.

This patch adds a pid filter to the test to ensure that no
bpf_snprintf() will write over the test's output buffers while the
userspace reads the values.

Fixes: c2e39c6 ("selftests/bpf: Add a series of tests for bpf_snprintf")
Reported-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Florent Revest <revest@chromium.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210428152501.1024509-1-revest@chromium.org
  • Loading branch information
Florent Revest authored and Andrii Nakryiko committed Apr 30, 2021
1 parent d4eecfb commit f80f88f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ void test_snprintf_positive(void)
if (!ASSERT_OK_PTR(skel, "skel_open"))
return;

skel->bss->pid = getpid();

if (!ASSERT_OK(test_snprintf__attach(skel), "skel_attach"))
goto cleanup;

Expand Down
5 changes: 5 additions & 0 deletions tools/testing/selftests/bpf/progs/test_snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

__u32 pid = 0;

char num_out[64] = {};
long num_ret = 0;

Expand Down Expand Up @@ -42,6 +44,9 @@ int handler(const void *ctx)
static const char str1[] = "str1";
static const char longstr[] = "longstr";

if ((int)bpf_get_current_pid_tgid() != pid)
return 0;

/* Integer types */
num_ret = BPF_SNPRINTF(num_out, sizeof(num_out),
"%d %u %x %li %llu %lX",
Expand Down

0 comments on commit f80f88f

Please sign in to comment.