linux/tools/testing/selftests/bpf/prog_tests/probe_read_user_str.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2#include <test_progs.h>
   3#include "test_probe_read_user_str.skel.h"
   4
   5static const char str1[] = "mestring";
   6static const char str2[] = "mestringalittlebigger";
   7static const char str3[] = "mestringblubblubblubblubblub";
   8
   9static int test_one_str(struct test_probe_read_user_str *skel, const char *str,
  10                        size_t len)
  11{
  12        int err, duration = 0;
  13        char buf[256];
  14
  15        /* Ensure bytes after string are ones */
  16        memset(buf, 1, sizeof(buf));
  17        memcpy(buf, str, len);
  18
  19        /* Give prog our userspace pointer */
  20        skel->bss->user_ptr = buf;
  21
  22        /* Trigger tracepoint */
  23        usleep(1);
  24
  25        /* Did helper fail? */
  26        if (CHECK(skel->bss->ret < 0, "prog_ret", "prog returned: %ld\n",
  27                  skel->bss->ret))
  28                return 1;
  29
  30        /* Check that string was copied correctly */
  31        err = memcmp(skel->bss->buf, str, len);
  32        if (CHECK(err, "memcmp", "prog copied wrong string"))
  33                return 1;
  34
  35        /* Now check that no extra trailing bytes were copied */
  36        memset(buf, 0, sizeof(buf));
  37        err = memcmp(skel->bss->buf + len, buf, sizeof(buf) - len);
  38        if (CHECK(err, "memcmp", "trailing bytes were not stripped"))
  39                return 1;
  40
  41        return 0;
  42}
  43
  44void test_probe_read_user_str(void)
  45{
  46        struct test_probe_read_user_str *skel;
  47        int err, duration = 0;
  48
  49        skel = test_probe_read_user_str__open_and_load();
  50        if (CHECK(!skel, "test_probe_read_user_str__open_and_load",
  51                  "skeleton open and load failed\n"))
  52                return;
  53
  54        /* Give pid to bpf prog so it doesn't read from anyone else */
  55        skel->bss->pid = getpid();
  56
  57        err = test_probe_read_user_str__attach(skel);
  58        if (CHECK(err, "test_probe_read_user_str__attach",
  59                  "skeleton attach failed: %d\n", err))
  60                goto out;
  61
  62        if (test_one_str(skel, str1, sizeof(str1)))
  63                goto out;
  64        if (test_one_str(skel, str2, sizeof(str2)))
  65                goto out;
  66        if (test_one_str(skel, str3, sizeof(str3)))
  67                goto out;
  68
  69out:
  70        test_probe_read_user_str__destroy(skel);
  71}
  72