selftests/bpf: Add tests for string kfuncs
authorViktor Malik <vmalik@redhat.com>
Thu, 26 Jun 2025 06:08:31 +0000 (08:08 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 26 Jun 2025 16:44:46 +0000 (09:44 -0700)
commite8763fb66a386843f091925dab757c4f55633d0b
treeb549759aa989c22eec0f9f307ea3b317917c995a
parenta55b7d39328bc6de1131cb5496816129cab2728b
selftests/bpf: Add tests for string kfuncs

Add both positive and negative tests cases using string kfuncs added in
the previous patches.

Positive tests check that the functions work as expected.

Negative tests pass various incorrect strings to the kfuncs and check
for the expected error codes:
  -E2BIG  when passing too long strings
  -EFAULT when trying to read inaccessible kernel memory
  -ERANGE when passing userspace pointers on arches with non-overlapping
          address spaces

A majority of the tests use the RUN_TESTS helper which executes BPF
programs with BPF_PROG_TEST_RUN and check for the expected return value.
An exception to this are tests for long strings as we need to memset the
long string from userspace (at least I haven't found an ergonomic way to
memset it from a BPF program), which cannot be done using the RUN_TESTS
infrastructure.

Suggested-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Viktor Malik <vmalik@redhat.com>
Link: https://lore.kernel.org/r/090451a2e60c9ae1dceb4d1bfafa3479db5c7481.1750917800.git.vmalik@redhat.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/string_kfuncs.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/string_kfuncs_failure1.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/string_kfuncs_failure2.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/string_kfuncs_success.c [new file with mode: 0644]