xref: /linux/tools/testing/selftests/bpf/prog_tests/syscall.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <test_progs.h>
4 #include "syscall.skel.h"
5 
6 struct args {
7 	__u64 log_buf;
8 	__u32 log_size;
9 	int max_entries;
10 	int map_fd;
11 	int prog_fd;
12 	int btf_fd;
13 };
14 
15 static void test_syscall_load_prog(void)
16 {
17 	static char verifier_log[8192];
18 	struct args ctx = {
19 		.max_entries = 1024,
20 		.log_buf = (uintptr_t) verifier_log,
21 		.log_size = sizeof(verifier_log),
22 	};
23 	LIBBPF_OPTS(bpf_test_run_opts, tattr,
24 		.ctx_in = &ctx,
25 		.ctx_size_in = sizeof(ctx),
26 	);
27 	struct syscall *skel = NULL;
28 	__u64 key = 12, value = 0;
29 	int err, prog_fd;
30 
31 	skel = syscall__open_and_load();
32 	if (!ASSERT_OK_PTR(skel, "skel_load"))
33 		goto cleanup;
34 
35 	prog_fd = bpf_program__fd(skel->progs.load_prog);
36 	err = bpf_prog_test_run_opts(prog_fd, &tattr);
37 	ASSERT_EQ(err, 0, "err");
38 	ASSERT_EQ(tattr.retval, 1, "retval");
39 	ASSERT_GT(ctx.map_fd, 0, "ctx.map_fd");
40 	ASSERT_GT(ctx.prog_fd, 0, "ctx.prog_fd");
41 	ASSERT_OK(memcmp(verifier_log, "processed", sizeof("processed") - 1),
42 		  "verifier_log");
43 
44 	err = bpf_map_lookup_elem(ctx.map_fd, &key, &value);
45 	ASSERT_EQ(err, 0, "map_lookup");
46 	ASSERT_EQ(value, 34, "map lookup value");
47 cleanup:
48 	syscall__destroy(skel);
49 	if (ctx.prog_fd > 0)
50 		close(ctx.prog_fd);
51 	if (ctx.map_fd > 0)
52 		close(ctx.map_fd);
53 	if (ctx.btf_fd > 0)
54 		close(ctx.btf_fd);
55 }
56 
57 static void test_syscall_update_outer_map(void)
58 {
59 	LIBBPF_OPTS(bpf_test_run_opts, opts);
60 	struct syscall *skel;
61 	int err, prog_fd;
62 
63 	skel = syscall__open_and_load();
64 	if (!ASSERT_OK_PTR(skel, "skel_load"))
65 		goto cleanup;
66 
67 	prog_fd = bpf_program__fd(skel->progs.update_outer_map);
68 	err = bpf_prog_test_run_opts(prog_fd, &opts);
69 	ASSERT_EQ(err, 0, "err");
70 	ASSERT_EQ(opts.retval, 1, "retval");
71 cleanup:
72 	syscall__destroy(skel);
73 }
74 
75 void test_syscall(void)
76 {
77 	if (test__start_subtest("load_prog"))
78 		test_syscall_load_prog();
79 	if (test__start_subtest("update_outer_map"))
80 		test_syscall_update_outer_map();
81 }
82