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.bpf.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 err = libbpf_get_error(obj); 16 if (CHECK_FAIL(err)) 17 return; 18 19 map = bpf_object__find_map_by_name(obj, ".rodata"); 20 if (CHECK_FAIL(!map || !bpf_map__is_internal(map))) 21 goto out; 22 23 sz = bpf_map__value_size(map); 24 newval = malloc(sz); 25 if (CHECK_FAIL(!newval)) 26 goto out; 27 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)) 32 goto out; 33 34 err = bpf_map__set_initial_value(map, newval, sz); 35 if (CHECK(err, "set initial value", "err %d\n", err)) 36 goto out; 37 38 err = bpf_object__load(obj); 39 if (CHECK_FAIL(err)) 40 goto out; 41 42 map_fd = bpf_map__fd(map); 43 if (CHECK_FAIL(map_fd < 0)) 44 goto out; 45 46 buff = malloc(sz); 47 if (buff) 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)) 52 goto out; 53 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); 58 out: 59 free(buff); 60 free(newval); 61 bpf_object__close(obj); 62 } 63