selftests/bpf: add missing __weak kfunc log fixup test
authorAndrii Nakryiko <andrii@kernel.org>
Tue, 18 Apr 2023 00:21:46 +0000 (17:21 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 18 Apr 2023 19:45:10 +0000 (12:45 -0700)
Add test validating that libbpf correctly poisons and reports __weak
unresolved kfuncs in post-processed verifier log.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230418002148.3255690-5-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/log_fixup.c
tools/testing/selftests/bpf/progs/test_log_fixup.c

index bc27170bdeb0496bf7ddfc5048002194fde6d841..dba71d98a2279eee92dd045670dd84c8b814e39f 100644 (file)
@@ -135,6 +135,35 @@ cleanup:
        test_log_fixup__destroy(skel);
 }
 
+static void missing_kfunc(void)
+{
+       char log_buf[8 * 1024];
+       struct test_log_fixup* skel;
+       int err;
+
+       skel = test_log_fixup__open();
+       if (!ASSERT_OK_PTR(skel, "skel_open"))
+               return;
+
+       bpf_program__set_autoload(skel->progs.use_missing_kfunc, true);
+       bpf_program__set_log_buf(skel->progs.use_missing_kfunc, log_buf, sizeof(log_buf));
+
+       err = test_log_fixup__load(skel);
+       if (!ASSERT_ERR(err, "load_fail"))
+               goto cleanup;
+
+       ASSERT_HAS_SUBSTR(log_buf,
+                         "0: <invalid kfunc call>\n"
+                         "kfunc 'bpf_nonexistent_kfunc' is referenced but wasn't resolved\n",
+                         "log_buf");
+
+       if (env.verbosity > VERBOSE_NONE)
+               printf("LOG:   \n=================\n%s=================\n", log_buf);
+
+cleanup:
+       test_log_fixup__destroy(skel);
+}
+
 void test_log_fixup(void)
 {
        if (test__start_subtest("bad_core_relo_trunc_none"))
@@ -147,4 +176,6 @@ void test_log_fixup(void)
                bad_core_relo_subprog();
        if (test__start_subtest("missing_map"))
                missing_map();
+       if (test__start_subtest("missing_kfunc"))
+               missing_kfunc();
 }
index 60450cb0e72ec48b41096a279a1ca2b7bccc5a2e..1bd48feaaa4283e3e16082a82c5a9b77a4726109 100644 (file)
@@ -61,4 +61,14 @@ int use_missing_map(const void *ctx)
        return value != NULL;
 }
 
+extern int bpf_nonexistent_kfunc(void) __ksym __weak;
+
+SEC("?raw_tp/sys_enter")
+int use_missing_kfunc(const void *ctx)
+{
+       bpf_nonexistent_kfunc();
+
+       return 0;
+}
+
 char _license[] SEC("license") = "GPL";