Skip to content

Commit

Permalink
syscalls/bpf: zero-initialize bpf_attr including padding bits
Browse files Browse the repository at this point in the history
gcc 15 stopped zero-initializing padding bits:
  https://gcc.gnu.org/gcc-15/changes.html

However kernel bpf syscall checks that all unused fields for a command
are set to zero in CHECK_ATTR() macro, which causes tests to fail with
EINVAL.

Signed-off-by: Jan Stancek <[email protected]>
Reviewed-by: Cyril Hrubis <[email protected]>
  • Loading branch information
jstancek committed Feb 7, 2025
1 parent 74a30fa commit 67ecdb6
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions testcases/kernel/syscalls/bpf/bpf_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ int bpf_map_create(union bpf_attr *const attr)

int bpf_map_array_create(const uint32_t max_entries)
{
union bpf_attr map_attr = {
/* zero-initialize entire struct including padding bits */
union bpf_attr map_attr = {};

map_attr = (union bpf_attr) {
.map_type = BPF_MAP_TYPE_ARRAY,
.key_size = 4,
.value_size = 8,
Expand All @@ -64,13 +67,18 @@ void bpf_map_array_get(const int map_fd,
const uint32_t *const array_indx,
uint64_t *const array_val)
{
union bpf_attr elem_attr = {
/* zero-initialize entire struct including padding bits */
union bpf_attr elem_attr = {};
int ret;

elem_attr = (union bpf_attr) {
.map_fd = map_fd,
.key = ptr_to_u64(array_indx),
.value = ptr_to_u64(array_val),
.flags = 0
};
const int ret = bpf(BPF_MAP_LOOKUP_ELEM, &elem_attr, sizeof(elem_attr));

ret = bpf(BPF_MAP_LOOKUP_ELEM, &elem_attr, sizeof(elem_attr));

if (ret) {
tst_brk(TBROK | TTERRNO,
Expand Down

0 comments on commit 67ecdb6

Please sign in to comment.