xref: /linux/tools/testing/selftests/bpf/prog_tests/file_reader.c (revision ff880798de39251aa5e28ab389c16d294b7af658)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3 
4 #include <test_progs.h>
5 #include <network_helpers.h>
6 #include "file_reader.skel.h"
7 #include "file_reader_fail.skel.h"
8 #include <dlfcn.h>
9 #include <sys/mman.h>
10 
11 const char *user_ptr = "hello world";
12 char file_contents[256000];
13 
14 void *get_executable_base_addr(void)
15 {
16 	Dl_info info;
17 
18 	if (!dladdr((void *)&get_executable_base_addr, &info)) {
19 		fprintf(stderr, "dladdr failed\n");
20 		return NULL;
21 	}
22 
23 	return info.dli_fbase;
24 }
25 
26 static int initialize_file_contents(void)
27 {
28 	int fd, page_sz = sysconf(_SC_PAGESIZE);
29 	ssize_t n = 0, cur, off;
30 	void *addr;
31 
32 	fd = open("/proc/self/exe", O_RDONLY);
33 	if (!ASSERT_OK_FD(fd, "Open /proc/self/exe\n"))
34 		return 1;
35 
36 	do {
37 		cur = read(fd, file_contents + n, sizeof(file_contents) - n);
38 		if (!ASSERT_GT(cur, 0, "read success"))
39 			break;
40 		n += cur;
41 	} while (n < sizeof(file_contents));
42 
43 	close(fd);
44 
45 	if (!ASSERT_EQ(n, sizeof(file_contents), "Read /proc/self/exe\n"))
46 		return 1;
47 
48 	addr = get_executable_base_addr();
49 	if (!ASSERT_NEQ(addr, NULL, "get executable address"))
50 		return 1;
51 
52 	/* page-align base file address */
53 	addr = (void *)((unsigned long)addr & ~(page_sz - 1));
54 
55 	for (off = 0; off < sizeof(file_contents); off += page_sz) {
56 		if (!ASSERT_OK(madvise(addr + off, page_sz, MADV_PAGEOUT),
57 			       "madvise pageout"))
58 			return errno;
59 	}
60 
61 	return 0;
62 }
63 
64 static void run_test(const char *prog_name)
65 {
66 	struct file_reader *skel;
67 	struct bpf_program *prog;
68 	int err, fd;
69 
70 	err = initialize_file_contents();
71 	if (!ASSERT_OK(err, "initialize file contents"))
72 		return;
73 
74 	skel = file_reader__open();
75 	if (!ASSERT_OK_PTR(skel, "file_reader__open"))
76 		return;
77 
78 	bpf_object__for_each_program(prog, skel->obj) {
79 		bpf_program__set_autoload(prog, strcmp(bpf_program__name(prog), prog_name) == 0);
80 	}
81 
82 	memcpy(skel->bss->user_buf, file_contents, sizeof(file_contents));
83 	skel->bss->pid = getpid();
84 
85 	err = file_reader__load(skel);
86 	if (!ASSERT_OK(err, "file_reader__load"))
87 		goto cleanup;
88 
89 	err = file_reader__attach(skel);
90 	if (!ASSERT_OK(err, "file_reader__attach"))
91 		goto cleanup;
92 
93 	fd = open("/proc/self/exe", O_RDONLY);
94 	if (fd >= 0)
95 		close(fd);
96 
97 	ASSERT_EQ(skel->bss->err, 0, "err");
98 	ASSERT_EQ(skel->bss->run_success, 1, "run_success");
99 cleanup:
100 	file_reader__destroy(skel);
101 }
102 
103 void test_file_reader(void)
104 {
105 	if (test__start_subtest("on_open_expect_fault"))
106 		run_test("on_open_expect_fault");
107 
108 	if (test__start_subtest("on_open_validate_file_read"))
109 		run_test("on_open_validate_file_read");
110 
111 	if (test__start_subtest("negative"))
112 		RUN_TESTS(file_reader_fail);
113 }
114