WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / probe_read_user_str.c
blobe419298132b5e2d1800ca4c99a27e761235e530b
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_probe_read_user_str.skel.h"
5 static const char str1[] = "mestring";
6 static const char str2[] = "mestringalittlebigger";
7 static const char str3[] = "mestringblubblubblubblubblub";
9 static int test_one_str(struct test_probe_read_user_str *skel, const char *str,
10 size_t len)
12 int err, duration = 0;
13 char buf[256];
15 /* Ensure bytes after string are ones */
16 memset(buf, 1, sizeof(buf));
17 memcpy(buf, str, len);
19 /* Give prog our userspace pointer */
20 skel->bss->user_ptr = buf;
22 /* Trigger tracepoint */
23 usleep(1);
25 /* Did helper fail? */
26 if (CHECK(skel->bss->ret < 0, "prog_ret", "prog returned: %ld\n",
27 skel->bss->ret))
28 return 1;
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;
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;
41 return 0;
44 void test_probe_read_user_str(void)
46 struct test_probe_read_user_str *skel;
47 int err, duration = 0;
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;
54 /* Give pid to bpf prog so it doesn't read from anyone else */
55 skel->bss->pid = getpid();
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;
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;
69 out:
70 test_probe_read_user_str__destroy(skel);