Skip to content

Commit 4198ff3

Browse files
Yihan DingAlexei Starovoitov
authored andcommitted
selftests/bpf: cover UTF-8 trace_printk output
Extend trace_printk coverage to verify that UTF-8 literal text is emitted successfully and that '%' parsing still rejects non-ASCII bytes once format parsing starts. Use an explicitly invalid format string for the negative case so the ASCII-only parser expectation is visible from the test code itself. Signed-off-by: Yihan Ding <dingyihan@uniontech.com> Acked-by: Paul Chaignon <paul.chaignon@gmail.com> Link: https://lore.kernel.org/r/20260416120142.1420646-3-dingyihan@uniontech.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent b960430 commit 4198ff3

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

tools/testing/selftests/bpf/prog_tests/trace_printk.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66
#include "trace_printk.lskel.h"
77

88
#define SEARCHMSG "testing,testing"
9+
#define SEARCHMSG_UTF8 "中文,测试"
910

1011
static void trace_pipe_cb(const char *str, void *data)
1112
{
1213
if (strstr(str, SEARCHMSG) != NULL)
13-
(*(int *)data)++;
14+
((int *)data)[0]++;
15+
if (strstr(str, SEARCHMSG_UTF8))
16+
((int *)data)[1]++;
1417
}
1518

1619
void serial_test_trace_printk(void)
1720
{
1821
struct trace_printk_lskel__bss *bss;
1922
struct trace_printk_lskel *skel;
20-
int err = 0, found = 0;
23+
int err = 0, found[2] = {};
2124

2225
skel = trace_printk_lskel__open();
2326
if (!ASSERT_OK_PTR(skel, "trace_printk__open"))
@@ -46,11 +49,24 @@ void serial_test_trace_printk(void)
4649
if (!ASSERT_GT(bss->trace_printk_ret, 0, "bss->trace_printk_ret"))
4750
goto cleanup;
4851

49-
/* verify our search string is in the trace buffer */
50-
ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, &found, 1000),
51-
"read_trace_pipe_iter");
52+
if (!ASSERT_GT(bss->trace_printk_utf8_ran, 0, "bss->trace_printk_utf8_ran"))
53+
goto cleanup;
54+
55+
if (!ASSERT_GT(bss->trace_printk_utf8_ret, 0, "bss->trace_printk_utf8_ret"))
56+
goto cleanup;
57+
58+
if (!ASSERT_LT(bss->trace_printk_invalid_spec_ret, 0,
59+
"bss->trace_printk_invalid_spec_ret"))
60+
goto cleanup;
61+
62+
/* verify our search strings are in the trace buffer */
63+
ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, found, 1000),
64+
"read_trace_pipe_iter");
65+
66+
if (!ASSERT_EQ(found[0], bss->trace_printk_ran, "found"))
67+
goto cleanup;
5268

53-
if (!ASSERT_EQ(found, bss->trace_printk_ran, "found"))
69+
if (!ASSERT_EQ(found[1], bss->trace_printk_utf8_ran, "found_utf8"))
5470
goto cleanup;
5571

5672
cleanup:

tools/testing/selftests/bpf/progs/trace_printk.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ char _license[] SEC("license") = "GPL";
1010

1111
int trace_printk_ret = 0;
1212
int trace_printk_ran = 0;
13+
int trace_printk_invalid_spec_ret = 0;
14+
int trace_printk_utf8_ret = 0;
15+
int trace_printk_utf8_ran = 0;
1316

1417
const char fmt[] = "Testing,testing %d\n";
18+
static const char utf8_fmt[] = "中文,测试 %d\n";
19+
/* Non-ASCII bytes after '%' must still be rejected. */
20+
static const char invalid_spec_fmt[] = "%\x80\n";
1521

1622
SEC("fentry/" SYS_PREFIX "sys_nanosleep")
1723
int sys_enter(void *ctx)
1824
{
1925
trace_printk_ret = bpf_trace_printk(fmt, sizeof(fmt),
2026
++trace_printk_ran);
27+
trace_printk_utf8_ret = bpf_trace_printk(utf8_fmt, sizeof(utf8_fmt),
28+
++trace_printk_utf8_ran);
29+
trace_printk_invalid_spec_ret = bpf_trace_printk(invalid_spec_fmt,
30+
sizeof(invalid_spec_fmt));
2131
return 0;
2232
}

0 commit comments

Comments
 (0)