Adding tests for attach fails on tracing multi link.

Signed-off-by: Jiri Olsa <[email protected]>
---
 .../selftests/bpf/prog_tests/tracing_multi.c  | 74 +++++++++++++++++++
 .../selftests/bpf/progs/tracing_multi_fail.c  | 19 +++++
 2 files changed, 93 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/tracing_multi_fail.c

diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_multi.c 
b/tools/testing/selftests/bpf/prog_tests/tracing_multi.c
index 3a55a2aed9a0..d64d90f4e086 100644
--- a/tools/testing/selftests/bpf/prog_tests/tracing_multi.c
+++ b/tools/testing/selftests/bpf/prog_tests/tracing_multi.c
@@ -8,6 +8,7 @@
 #include "tracing_multi_module.skel.h"
 #include "tracing_multi_intersect.skel.h"
 #include "tracing_multi_session.skel.h"
+#include "tracing_multi_fail.skel.h"
 #include "trace_helpers.h"
 
 static __u64 bpf_fentry_test_cookies[] = {
@@ -471,6 +472,77 @@ static void test_session(void)
        tracing_multi_session__destroy(skel);
 }
 
+static void test_attach_api_fails(void)
+{
+       LIBBPF_OPTS(bpf_tracing_multi_opts, opts);
+       static const char * const func[] = {
+               "bpf_fentry_test2",
+       };
+       struct tracing_multi_fail *skel = NULL;
+       __u32 ids[2], *ids2;
+       __u64 cookies[2];
+
+       skel = tracing_multi_fail__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "tracing_multi_fail__open_and_load"))
+               return;
+
+       /* fail#1 pattern and opts NULL */
+       skel->links.test_fentry = 
bpf_program__attach_tracing_multi(skel->progs.test_fentry,
+                                               NULL, NULL);
+       if (!ASSERT_ERR_PTR(skel->links.test_fentry, 
"bpf_program__attach_tracing_multi"))
+               goto cleanup;
+
+       /* fail#2 pattern and ids */
+       opts.ids = ids;
+       opts.cnt = 2;
+
+       skel->links.test_fentry = 
bpf_program__attach_tracing_multi(skel->progs.test_fentry,
+                                               "bpf_fentry_test*", &opts);
+       if (!ASSERT_ERR_PTR(skel->links.test_fentry, 
"bpf_program__attach_tracing_multi"))
+               goto cleanup;
+
+       /* fail#3 pattern and cookies */
+       opts.ids = NULL;
+       opts.cnt = 2;
+       opts.cookies = cookies;
+
+       skel->links.test_fentry = 
bpf_program__attach_tracing_multi(skel->progs.test_fentry,
+                                               "bpf_fentry_test*", &opts);
+       if (!ASSERT_ERR_PTR(skel->links.test_fentry, 
"bpf_program__attach_tracing_multi"))
+               goto cleanup;
+
+       /* fail#4 bogus pattern */
+       skel->links.test_fentry = 
bpf_program__attach_tracing_multi(skel->progs.test_fentry,
+                                               "bpf_not_really_a_function*", 
NULL);
+       if (!ASSERT_ERR_PTR(skel->links.test_fentry, 
"bpf_program__attach_tracing_multi"))
+               goto cleanup;
+
+       /* fail#5 abnormal cnt */
+       opts.ids = ids;
+       opts.cnt = INT_MAX;
+
+       skel->links.test_fentry = 
bpf_program__attach_tracing_multi(skel->progs.test_fentry,
+                                               NULL, &opts);
+       if (!ASSERT_ERR_PTR(skel->links.test_fentry, 
"bpf_program__attach_tracing_multi"))
+               goto cleanup;
+
+       /* fail#6 attach sleepable program to not-allowed function */
+       ids2 = get_ids(func, 1, NULL);
+       if (!ASSERT_OK_PTR(ids, "get_ids"))
+               goto cleanup;
+
+       opts.ids = ids2;
+       opts.cnt = 1;
+
+       skel->links.test_fentry_s = 
bpf_program__attach_tracing_multi(skel->progs.test_fentry_s,
+                                               NULL, &opts);
+       ASSERT_ERR_PTR(skel->links.test_fentry, 
"bpf_program__attach_tracing_multi");
+       free(ids2);
+
+cleanup:
+       tracing_multi_fail__destroy(skel);
+}
+
 void test_tracing_multi_test(void)
 {
 #ifndef __x86_64__
@@ -496,4 +568,6 @@ void test_tracing_multi_test(void)
                test_link_api_ids(true);
        if (test__start_subtest("session"))
                test_session();
+       if (test__start_subtest("attach_api_fails"))
+               test_attach_api_fails();
 }
diff --git a/tools/testing/selftests/bpf/progs/tracing_multi_fail.c 
b/tools/testing/selftests/bpf/progs/tracing_multi_fail.c
new file mode 100644
index 000000000000..8f769ddb9136
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tracing_multi_fail.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <stdbool.h>
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+SEC("fentry.multi")
+int BPF_PROG(test_fentry)
+{
+       return 0;
+}
+
+SEC("fentry.multi.s")
+int BPF_PROG(test_fentry_s)
+{
+       return 0;
+}
-- 
2.53.0


Reply via email to