xref: /linux/tools/testing/selftests/bpf/prog_tests/global_data_init.c (revision 95298d63c67673c654c08952672d016212b26054)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 
4 void test_global_data_init(void)
5 {
6 	const char *file = "./test_global_data.o";
7 	int err = -ENOMEM, map_fd, zero = 0;
8 	__u8 *buff = NULL, *newval = NULL;
9 	struct bpf_object *obj;
10 	struct bpf_map *map;
11         __u32 duration = 0;
12 	size_t sz;
13 
14 	obj = bpf_object__open_file(file, NULL);
15 	if (CHECK_FAIL(!obj))
16 		return;
17 
18 	map = bpf_object__find_map_by_name(obj, "test_glo.rodata");
19 	if (CHECK_FAIL(!map || !bpf_map__is_internal(map)))
20 		goto out;
21 
22 	sz = bpf_map__def(map)->value_size;
23 	newval = malloc(sz);
24 	if (CHECK_FAIL(!newval))
25 		goto out;
26 
27 	memset(newval, 0, sz);
28 	/* wrong size, should fail */
29 	err = bpf_map__set_initial_value(map, newval, sz - 1);
30 	if (CHECK(!err, "reject set initial value wrong size", "err %d\n", err))
31 		goto out;
32 
33 	err = bpf_map__set_initial_value(map, newval, sz);
34 	if (CHECK(err, "set initial value", "err %d\n", err))
35 		goto out;
36 
37 	err = bpf_object__load(obj);
38 	if (CHECK_FAIL(err))
39 		goto out;
40 
41 	map_fd = bpf_map__fd(map);
42 	if (CHECK_FAIL(map_fd < 0))
43 		goto out;
44 
45 	buff = malloc(sz);
46 	if (buff)
47 		err = bpf_map_lookup_elem(map_fd, &zero, buff);
48 	if (CHECK(!buff || err || memcmp(buff, newval, sz),
49 		  "compare .rodata map data override",
50 		  "err %d errno %d\n", err, errno))
51 		goto out;
52 
53 	memset(newval, 1, sz);
54 	/* object loaded - should fail */
55 	err = bpf_map__set_initial_value(map, newval, sz);
56 	CHECK(!err, "reject set initial value after load", "err %d\n", err);
57 out:
58 	free(buff);
59 	free(newval);
60 	bpf_object__close(obj);
61 }
62