1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 void test_global_data_init(void)
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
;
14 obj
= bpf_object__open_file(file
, NULL
);
15 err
= libbpf_get_error(obj
);
19 map
= bpf_object__find_map_by_name(obj
, "test_glo.rodata");
20 if (CHECK_FAIL(!map
|| !bpf_map__is_internal(map
)))
23 sz
= bpf_map__def(map
)->value_size
;
25 if (CHECK_FAIL(!newval
))
28 memset(newval
, 0, sz
);
29 /* wrong size, should fail */
30 err
= bpf_map__set_initial_value(map
, newval
, sz
- 1);
31 if (CHECK(!err
, "reject set initial value wrong size", "err %d\n", err
))
34 err
= bpf_map__set_initial_value(map
, newval
, sz
);
35 if (CHECK(err
, "set initial value", "err %d\n", err
))
38 err
= bpf_object__load(obj
);
42 map_fd
= bpf_map__fd(map
);
43 if (CHECK_FAIL(map_fd
< 0))
48 err
= bpf_map_lookup_elem(map_fd
, &zero
, buff
);
49 if (CHECK(!buff
|| err
|| memcmp(buff
, newval
, sz
),
50 "compare .rodata map data override",
51 "err %d errno %d\n", err
, errno
))
54 memset(newval
, 1, sz
);
55 /* object loaded - should fail */
56 err
= bpf_map__set_initial_value(map
, newval
, sz
);
57 CHECK(!err
, "reject set initial value after load", "err %d\n", err
);
61 bpf_object__close(obj
);