]> Git Repo - linux.git/commitdiff
selftests/bpf: Convert test_global_funcs test to test_loader framework
authorAndrii Nakryiko <[email protected]>
Thu, 16 Feb 2023 04:59:53 +0000 (20:59 -0800)
committerDaniel Borkmann <[email protected]>
Fri, 17 Feb 2023 20:20:44 +0000 (21:20 +0100)
Convert 17 test_global_funcs subtests into test_loader framework for
easier maintenance and more declarative way to define expected
failures/successes.

Signed-off-by: Andrii Nakryiko <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
Acked-by: Stanislav Fomichev <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
18 files changed:
tools/testing/selftests/bpf/prog_tests/test_global_funcs.c
tools/testing/selftests/bpf/progs/test_global_func1.c
tools/testing/selftests/bpf/progs/test_global_func10.c
tools/testing/selftests/bpf/progs/test_global_func11.c
tools/testing/selftests/bpf/progs/test_global_func12.c
tools/testing/selftests/bpf/progs/test_global_func13.c
tools/testing/selftests/bpf/progs/test_global_func14.c
tools/testing/selftests/bpf/progs/test_global_func15.c
tools/testing/selftests/bpf/progs/test_global_func16.c
tools/testing/selftests/bpf/progs/test_global_func17.c
tools/testing/selftests/bpf/progs/test_global_func2.c
tools/testing/selftests/bpf/progs/test_global_func3.c
tools/testing/selftests/bpf/progs/test_global_func4.c
tools/testing/selftests/bpf/progs/test_global_func5.c
tools/testing/selftests/bpf/progs/test_global_func6.c
tools/testing/selftests/bpf/progs/test_global_func7.c
tools/testing/selftests/bpf/progs/test_global_func8.c
tools/testing/selftests/bpf/progs/test_global_func9.c

index 7295cc60f724878ce86716ed83f69517f43e41b4..2ff4d5c7abfceb8b21b9a28bf50159e5d7bda3b2 100644 (file)
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Facebook */
 #include <test_progs.h>
-
-const char *err_str;
-bool found;
-
-static int libbpf_debug_print(enum libbpf_print_level level,
-                             const char *format, va_list args)
-{
-       char *log_buf;
-
-       if (level != LIBBPF_WARN ||
-           strcmp(format, "libbpf: \n%s\n")) {
-               vprintf(format, args);
-               return 0;
-       }
-
-       log_buf = va_arg(args, char *);
-       if (!log_buf)
-               goto out;
-       if (err_str && strstr(log_buf, err_str) == 0)
-               found = true;
-out:
-       printf(format, log_buf);
-       return 0;
-}
-
-extern int extra_prog_load_log_flags;
-
-static int check_load(const char *file)
-{
-       struct bpf_object *obj = NULL;
-       struct bpf_program *prog;
-       int err;
-
-       found = false;
-
-       obj = bpf_object__open_file(file, NULL);
-       err = libbpf_get_error(obj);
-       if (err)
-               return err;
-
-       prog = bpf_object__next_program(obj, NULL);
-       if (!prog) {
-               err = -ENOENT;
-               goto err_out;
-       }
-
-       bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32);
-       bpf_program__set_log_level(prog, extra_prog_load_log_flags);
-
-       err = bpf_object__load(obj);
-
-err_out:
-       bpf_object__close(obj);
-       return err;
-}
-
-struct test_def {
-       const char *file;
-       const char *err_str;
-};
+#include "test_global_func1.skel.h"
+#include "test_global_func2.skel.h"
+#include "test_global_func3.skel.h"
+#include "test_global_func4.skel.h"
+#include "test_global_func5.skel.h"
+#include "test_global_func6.skel.h"
+#include "test_global_func7.skel.h"
+#include "test_global_func8.skel.h"
+#include "test_global_func9.skel.h"
+#include "test_global_func10.skel.h"
+#include "test_global_func11.skel.h"
+#include "test_global_func12.skel.h"
+#include "test_global_func13.skel.h"
+#include "test_global_func14.skel.h"
+#include "test_global_func15.skel.h"
+#include "test_global_func16.skel.h"
+#include "test_global_func17.skel.h"
 
 void test_test_global_funcs(void)
 {
-       struct test_def tests[] = {
-               { "test_global_func1.bpf.o", "combined stack size of 4 calls is 544" },
-               { "test_global_func2.bpf.o" },
-               { "test_global_func3.bpf.o", "the call stack of 8 frames" },
-               { "test_global_func4.bpf.o" },
-               { "test_global_func5.bpf.o", "expected pointer to ctx, but got PTR" },
-               { "test_global_func6.bpf.o", "modified ctx ptr R2" },
-               { "test_global_func7.bpf.o", "foo() doesn't return scalar" },
-               { "test_global_func8.bpf.o" },
-               { "test_global_func9.bpf.o" },
-               { "test_global_func10.bpf.o", "invalid indirect read from stack" },
-               { "test_global_func11.bpf.o", "Caller passes invalid args into func#1" },
-               { "test_global_func12.bpf.o", "invalid mem access 'mem_or_null'" },
-               { "test_global_func13.bpf.o", "Caller passes invalid args into func#1" },
-               { "test_global_func14.bpf.o", "reference type('FWD S') size cannot be determined" },
-               { "test_global_func15.bpf.o", "At program exit the register R0 has value" },
-               { "test_global_func16.bpf.o", "invalid indirect read from stack" },
-               { "test_global_func17.bpf.o", "Caller passes invalid args into func#1" },
-       };
-       libbpf_print_fn_t old_print_fn = NULL;
-       int err, i, duration = 0;
-
-       old_print_fn = libbpf_set_print(libbpf_debug_print);
-
-       for (i = 0; i < ARRAY_SIZE(tests); i++) {
-               const struct test_def *test = &tests[i];
-
-               if (!test__start_subtest(test->file))
-                       continue;
-
-               err_str = test->err_str;
-               err = check_load(test->file);
-               CHECK_FAIL(!!err ^ !!err_str);
-               if (err_str)
-                       CHECK(found, "", "expected string '%s'", err_str);
-       }
-       libbpf_set_print(old_print_fn);
+       RUN_TESTS(test_global_func1);
+       RUN_TESTS(test_global_func2);
+       RUN_TESTS(test_global_func3);
+       RUN_TESTS(test_global_func4);
+       RUN_TESTS(test_global_func5);
+       RUN_TESTS(test_global_func6);
+       RUN_TESTS(test_global_func7);
+       RUN_TESTS(test_global_func8);
+       RUN_TESTS(test_global_func9);
+       RUN_TESTS(test_global_func10);
+       RUN_TESTS(test_global_func11);
+       RUN_TESTS(test_global_func12);
+       RUN_TESTS(test_global_func13);
+       RUN_TESTS(test_global_func14);
+       RUN_TESTS(test_global_func15);
+       RUN_TESTS(test_global_func16);
+       RUN_TESTS(test_global_func17);
 }
index 7b42dad187b8941436ad14cdc03909f5f156974d..23970a20b3249dec95318164865f3ca62d0d7f45 100644 (file)
@@ -3,10 +3,9 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
-#ifndef MAX_STACK
 #define MAX_STACK (512 - 3 * 32 + 8)
-#endif
 
 static __attribute__ ((noinline))
 int f0(int var, struct __sk_buff *skb)
@@ -39,7 +38,8 @@ int f3(int val, struct __sk_buff *skb, int var)
 }
 
 SEC("tc")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("combined stack size of 4 calls is 544")
+int global_func1(struct __sk_buff *skb)
 {
        return f0(1, skb) + f1(skb) + f2(2, skb) + f3(3, skb, 4);
 }
index 97b7031d0e2272a4980d8983619f3183a990631a..98327bdbbfd24700900ac94667bb7e0269d9a1fd 100644 (file)
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 struct Small {
        int x;
@@ -21,7 +22,8 @@ __noinline int foo(const struct Big *big)
 }
 
 SEC("cgroup_skb/ingress")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("invalid indirect read from stack")
+int global_func10(struct __sk_buff *skb)
 {
        const struct Small small = {.x = skb->len };
 
index ef5277d982d92148a4b10d1819471fdd773f8587..283e036dc401e91d7a268d806fc05feb116a4e97 100644 (file)
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 struct S {
        int x;
@@ -13,7 +14,8 @@ __noinline int foo(const struct S *s)
 }
 
 SEC("cgroup_skb/ingress")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("Caller passes invalid args into func#1")
+int global_func11(struct __sk_buff *skb)
 {
        return foo((const void *)skb);
 }
index 62343527cc598df03e2b2966dd0484370439d884..7f159d83c6f673f78f3b7c89e9ce7e1cfffdeef4 100644 (file)
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 struct S {
        int x;
@@ -13,7 +14,8 @@ __noinline int foo(const struct S *s)
 }
 
 SEC("cgroup_skb/ingress")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("invalid mem access 'mem_or_null'")
+int global_func12(struct __sk_buff *skb)
 {
        const struct S s = {.x = skb->len };
 
index ff8897c1ac22b4b18f97adc7d155c3bd137fd671..02ea80da75b57319f76f474fb7025ae689f719df 100644 (file)
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 struct S {
        int x;
@@ -16,7 +17,8 @@ __noinline int foo(const struct S *s)
 }
 
 SEC("cgroup_skb/ingress")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("Caller passes invalid args into func#1")
+int global_func13(struct __sk_buff *skb)
 {
        const struct S *s = (const struct S *)(0xbedabeda);
 
index 698c77199ebf7d38054681c851980711510933d2..33b7d5efd7b262025dcaa552589a19164a43144e 100644 (file)
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 struct S;
 
@@ -14,7 +15,8 @@ __noinline int foo(const struct S *s)
 }
 
 SEC("cgroup_skb/ingress")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("reference type('FWD S') size cannot be determined")
+int global_func14(struct __sk_buff *skb)
 {
 
        return foo(NULL);
index c19c435988d5573611dd705c90ab2fada9aa73a5..b512d6a6c75e54e700860ac0f4098a1ea7e7a24e 100644 (file)
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 __noinline int foo(unsigned int *v)
 {
@@ -12,7 +13,8 @@ __noinline int foo(unsigned int *v)
 }
 
 SEC("cgroup_skb/ingress")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("At program exit the register R0 has value")
+int global_func15(struct __sk_buff *skb)
 {
        unsigned int v = 1;
 
index 0312d1e8d8c0602b5c2ecca7b1dba483984198a6..e7206304632e15767a990481b4f88a189c5f72b2 100644 (file)
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 __noinline int foo(int (*arr)[10])
 {
@@ -12,7 +13,8 @@ __noinline int foo(int (*arr)[10])
 }
 
 SEC("cgroup_skb/ingress")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("invalid indirect read from stack")
+int global_func16(struct __sk_buff *skb)
 {
        int array[10];
 
index 2b8b9b8ba0183cf316ea8fc39e6549f099f02ed6..a32e11c7d933ee1d13c91a6bea6ee459d799678e 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <vmlinux.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 __noinline int foo(int *p)
 {
@@ -10,7 +11,8 @@ __noinline int foo(int *p)
 const volatile int i;
 
 SEC("tc")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("Caller passes invalid args into func#1")
+int global_func17(struct __sk_buff *skb)
 {
        return foo((int *)&i);
 }
index 2c18d82923a2d510cb4f9a31e518264f92691efb..3dce97fb52a4b7723132bcb75ac9813c91295f62 100644 (file)
@@ -1,4 +1,45 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright (c) 2020 Facebook */
+#include <stddef.h>
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
 #define MAX_STACK (512 - 3 * 32)
-#include "test_global_func1.c"
+
+static __attribute__ ((noinline))
+int f0(int var, struct __sk_buff *skb)
+{
+       return skb->len;
+}
+
+__attribute__ ((noinline))
+int f1(struct __sk_buff *skb)
+{
+       volatile char buf[MAX_STACK] = {};
+
+       return f0(0, skb) + skb->len;
+}
+
+int f3(int, struct __sk_buff *skb, int);
+
+__attribute__ ((noinline))
+int f2(int val, struct __sk_buff *skb)
+{
+       return f1(skb) + f3(val, skb, 1);
+}
+
+__attribute__ ((noinline))
+int f3(int val, struct __sk_buff *skb, int var)
+{
+       volatile char buf[MAX_STACK] = {};
+
+       return skb->ifindex * val * var;
+}
+
+SEC("tc")
+__success
+int global_func2(struct __sk_buff *skb)
+{
+       return f0(1, skb) + f1(skb) + f2(2, skb) + f3(3, skb, 4);
+}
index 01bf8275dfd6401137ae9edba6c9991c9a73a3c3..142b682d3c2f08df06036c972a006fa35de00a56 100644 (file)
@@ -3,6 +3,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 __attribute__ ((noinline))
 int f1(struct __sk_buff *skb)
@@ -46,20 +47,15 @@ int f7(struct __sk_buff *skb)
        return f6(skb);
 }
 
-#ifndef NO_FN8
 __attribute__ ((noinline))
 int f8(struct __sk_buff *skb)
 {
        return f7(skb);
 }
-#endif
 
 SEC("tc")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("the call stack of 8 frames")
+int global_func3(struct __sk_buff *skb)
 {
-#ifndef NO_FN8
        return f8(skb);
-#else
-       return f7(skb);
-#endif
 }
index 610f75edf27643915975e120823f983ba4c8aeaa..1733d87ad3f3ead1654ba683be7ea9054df7ab57 100644 (file)
@@ -1,4 +1,55 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright (c) 2020 Facebook */
-#define NO_FN8
-#include "test_global_func3.c"
+#include <stddef.h>
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+__attribute__ ((noinline))
+int f1(struct __sk_buff *skb)
+{
+       return skb->len;
+}
+
+__attribute__ ((noinline))
+int f2(int val, struct __sk_buff *skb)
+{
+       return f1(skb) + val;
+}
+
+__attribute__ ((noinline))
+int f3(int val, struct __sk_buff *skb, int var)
+{
+       return f2(var, skb) + val;
+}
+
+__attribute__ ((noinline))
+int f4(struct __sk_buff *skb)
+{
+       return f3(1, skb, 2);
+}
+
+__attribute__ ((noinline))
+int f5(struct __sk_buff *skb)
+{
+       return f4(skb);
+}
+
+__attribute__ ((noinline))
+int f6(struct __sk_buff *skb)
+{
+       return f5(skb);
+}
+
+__attribute__ ((noinline))
+int f7(struct __sk_buff *skb)
+{
+       return f6(skb);
+}
+
+SEC("tc")
+__success
+int global_func4(struct __sk_buff *skb)
+{
+       return f7(skb);
+}
index 9248d03e0d06fb22f192993e4dd29abcce0d10d7..cc55aedaf82d5792a92b157382d74d3389581940 100644 (file)
@@ -3,6 +3,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 __attribute__ ((noinline))
 int f1(struct __sk_buff *skb)
@@ -25,7 +26,8 @@ int f3(int val, struct __sk_buff *skb)
 }
 
 SEC("tc")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("expected pointer to ctx, but got PTR")
+int global_func5(struct __sk_buff *skb)
 {
        return f1(skb) + f2(2, skb) + f3(3, skb);
 }
index af8c78bdfb257536c212531a9610d8e3faaa1d04..46c38c8f2cf03280e7abfb128d7ddb89e007f9cb 100644 (file)
@@ -3,6 +3,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 __attribute__ ((noinline))
 int f1(struct __sk_buff *skb)
@@ -25,7 +26,8 @@ int f3(int val, struct __sk_buff *skb)
 }
 
 SEC("tc")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("modified ctx ptr R2")
+int global_func6(struct __sk_buff *skb)
 {
        return f1(skb) + f2(2, skb) + f3(3, skb);
 }
index 6cb8e2f5254cf1f545a5bd815edb4d9df70f1ab3..f182febfde3c08da5e8e8bab245c6268c8a804c2 100644 (file)
@@ -3,6 +3,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 __attribute__ ((noinline))
 void foo(struct __sk_buff *skb)
@@ -11,7 +12,8 @@ void foo(struct __sk_buff *skb)
 }
 
 SEC("tc")
-int test_cls(struct __sk_buff *skb)
+__failure __msg("foo() doesn't return scalar")
+int global_func7(struct __sk_buff *skb)
 {
        foo(skb);
        return 0;
index d55a6544b1abd2b763fce2623add52a1bdf30106..9b9c57fa2dd34b8ab710aaca9ea85ef72cad3314 100644 (file)
@@ -3,6 +3,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 __noinline int foo(struct __sk_buff *skb)
 {
@@ -10,7 +11,8 @@ __noinline int foo(struct __sk_buff *skb)
 }
 
 SEC("cgroup_skb/ingress")
-int test_cls(struct __sk_buff *skb)
+__success
+int global_func8(struct __sk_buff *skb)
 {
        if (!foo(skb))
                return 0;
index bd233ddede98ae4465d64efa20dd7770b4274d6e..1f2cb0159b8d83b243b79785a12e75f5ce8a3eab 100644 (file)
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
 
 struct S {
        int x;
@@ -74,7 +75,8 @@ __noinline int quuz(int **p)
 }
 
 SEC("cgroup_skb/ingress")
-int test_cls(struct __sk_buff *skb)
+__success
+int global_func9(struct __sk_buff *skb)
 {
        int result = 0;
 
This page took 0.104172 seconds and 4 git commands to generate.