Re: [PATCH bpf-next v2 6/6] selftests/bpf: Add a series of tests for bpf_snprintf

From: Andrii Nakryiko
Date: Fri Mar 26 2021 - 19:06:35 EST


On Tue, Mar 23, 2021 at 7:23 PM Florent Revest <revest@xxxxxxxxxxxx> wrote:
>
> This exercises most of the format specifiers when things go well.
>
> Signed-off-by: Florent Revest <revest@xxxxxxxxxxxx>
> ---

Looks good. Please add a no-argument test case as well.

Acked-by: Andrii Nakryiko <andrii@xxxxxxxxxx>

> .../selftests/bpf/prog_tests/snprintf.c | 65 +++++++++++++++++++
> .../selftests/bpf/progs/test_snprintf.c | 59 +++++++++++++++++
> 2 files changed, 124 insertions(+)
> create mode 100644 tools/testing/selftests/bpf/prog_tests/snprintf.c
> create mode 100644 tools/testing/selftests/bpf/progs/test_snprintf.c
>

[...]

> +
> +SEC("raw_tp/sys_enter")
> +int handler(const void *ctx)
> +{
> + /* Convenient values to pretty-print */
> + const __u8 ex_ipv4[] = {127, 0, 0, 1};
> + const __u8 ex_ipv6[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
> + const char str1[] = "str1";
> + const char longstr[] = "longstr";
> + extern const void schedule __ksym;

oh, fancy. I'd move it out of this function into global space, though,
to make it more apparent. I almost missed that it's a special one.

> +
> + /* Integer types */
> + num_ret = BPF_SNPRINTF(num_out, sizeof(num_out),
> + "%d %u %x %li %llu %lX",
> + -8, 9, 150, -424242, 1337, 0xDABBAD00);
> + /* IP addresses */
> + ip_ret = BPF_SNPRINTF(ip_out, sizeof(ip_out), "%pi4 %pI6",
> + &ex_ipv4, &ex_ipv6);
> + /* Symbol lookup formatting */
> + sym_ret = BPF_SNPRINTF(sym_out, sizeof(sym_out), "%ps %pS %pB",
> + &schedule, &schedule, &schedule);
> + /* Kernel pointers */
> + addr_ret = BPF_SNPRINTF(addr_out, sizeof(addr_out), "%pK %px %p",
> + 0, 0xFFFF00000ADD4E55, 0xFFFF00000ADD4E55);
> + /* Strings embedding */
> + str_ret = BPF_SNPRINTF(str_out, sizeof(str_out), "%s %+05s",
> + str1, longstr);
> + /* Overflow */
> + over_ret = BPF_SNPRINTF(over_out, sizeof(over_out), "%%overflow");
> +
> + return 0;
> +}
> +
> +char _license[] SEC("license") = "GPL";
> --
> 2.31.0.291.g576ba9dcdaf-goog
>