xref: /linux/tools/testing/selftests/bpf/prog_tests/fentry_test.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4 #include "fentry_test.lskel.h"
5 
6 static int fentry_test(struct fentry_test_lskel *fentry_skel)
7 {
8 	int err, prog_fd, i;
9 	__u32 duration = 0, retval;
10 	int link_fd;
11 	__u64 *result;
12 
13 	err = fentry_test_lskel__attach(fentry_skel);
14 	if (!ASSERT_OK(err, "fentry_attach"))
15 		return err;
16 
17 	/* Check that already linked program can't be attached again. */
18 	link_fd = fentry_test_lskel__test1__attach(fentry_skel);
19 	if (!ASSERT_LT(link_fd, 0, "fentry_attach_link"))
20 		return -1;
21 
22 	prog_fd = fentry_skel->progs.test1.prog_fd;
23 	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
24 				NULL, NULL, &retval, &duration);
25 	ASSERT_OK(err, "test_run");
26 	ASSERT_EQ(retval, 0, "test_run");
27 
28 	result = (__u64 *)fentry_skel->bss;
29 	for (i = 0; i < sizeof(*fentry_skel->bss) / sizeof(__u64); i++) {
30 		if (!ASSERT_EQ(result[i], 1, "fentry_result"))
31 			return -1;
32 	}
33 
34 	fentry_test_lskel__detach(fentry_skel);
35 
36 	/* zero results for re-attach test */
37 	memset(fentry_skel->bss, 0, sizeof(*fentry_skel->bss));
38 	return 0;
39 }
40 
41 void test_fentry_test(void)
42 {
43 	struct fentry_test_lskel *fentry_skel = NULL;
44 	int err;
45 
46 	fentry_skel = fentry_test_lskel__open_and_load();
47 	if (!ASSERT_OK_PTR(fentry_skel, "fentry_skel_load"))
48 		goto cleanup;
49 
50 	err = fentry_test(fentry_skel);
51 	if (!ASSERT_OK(err, "fentry_first_attach"))
52 		goto cleanup;
53 
54 	err = fentry_test(fentry_skel);
55 	ASSERT_OK(err, "fentry_second_attach");
56 
57 cleanup:
58 	fentry_test_lskel__destroy(fentry_skel);
59 }
60