Commit fdc027cf authored by Brenden Blanco's avatar Brenden Blanco

Workaround for uninitialized union clang bug

* Clang does not generate code to initialize the entire union when
  initializing a nested struct. This is the case for union bpf_attr.
  As a workaround, call memset explicitly.
Signed-off-by: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent 39001441
...@@ -59,56 +59,56 @@ static __u64 ptr_to_u64(void *ptr) ...@@ -59,56 +59,56 @@ static __u64 ptr_to_u64(void *ptr)
int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, int max_entries) int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, int max_entries)
{ {
union bpf_attr attr = { union bpf_attr attr;
.map_type = map_type, memset(&attr, 0, sizeof(attr));
.key_size = key_size, attr.map_type = map_type;
.value_size = value_size, attr.key_size = key_size;
.max_entries = max_entries attr.value_size = value_size;
}; attr.max_entries = max_entries;
return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
} }
int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags) int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags)
{ {
union bpf_attr attr = { union bpf_attr attr;
.map_fd = fd, memset(&attr, 0, sizeof(attr));
.key = ptr_to_u64(key), attr.map_fd = fd;
.value = ptr_to_u64(value), attr.key = ptr_to_u64(key);
.flags = flags, attr.value = ptr_to_u64(value);
}; attr.flags = flags;
return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
} }
int bpf_lookup_elem(int fd, void *key, void *value) int bpf_lookup_elem(int fd, void *key, void *value)
{ {
union bpf_attr attr = { union bpf_attr attr;
.map_fd = fd, memset(&attr, 0, sizeof(attr));
.key = ptr_to_u64(key), attr.map_fd = fd;
.value = ptr_to_u64(value), attr.key = ptr_to_u64(key);
}; attr.value = ptr_to_u64(value);
return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
} }
int bpf_delete_elem(int fd, void *key) int bpf_delete_elem(int fd, void *key)
{ {
union bpf_attr attr = { union bpf_attr attr;
.map_fd = fd, memset(&attr, 0, sizeof(attr));
.key = ptr_to_u64(key), attr.map_fd = fd;
}; attr.key = ptr_to_u64(key);
return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr)); return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
} }
int bpf_get_next_key(int fd, void *key, void *next_key) int bpf_get_next_key(int fd, void *key, void *next_key)
{ {
union bpf_attr attr = { union bpf_attr attr;
.map_fd = fd, memset(&attr, 0, sizeof(attr));
.key = ptr_to_u64(key), attr.map_fd = fd;
.next_key = ptr_to_u64(next_key), attr.key = ptr_to_u64(key);
}; attr.next_key = ptr_to_u64(next_key);
return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
} }
...@@ -122,15 +122,15 @@ int bpf_prog_load(enum bpf_prog_type prog_type, ...@@ -122,15 +122,15 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
const char *license, unsigned kern_version, const char *license, unsigned kern_version,
char *log_buf, unsigned log_buf_size) char *log_buf, unsigned log_buf_size)
{ {
union bpf_attr attr = { union bpf_attr attr;
.prog_type = prog_type, memset(&attr, 0, sizeof(attr));
.insns = ptr_to_u64((void *) insns), attr.prog_type = prog_type;
.insn_cnt = prog_len / sizeof(struct bpf_insn), attr.insns = ptr_to_u64((void *) insns);
.license = ptr_to_u64((void *) license), attr.insn_cnt = prog_len / sizeof(struct bpf_insn);
.log_buf = ptr_to_u64(log_buf), attr.license = ptr_to_u64((void *) license);
.log_size = log_buf_size, attr.log_buf = ptr_to_u64(log_buf);
.log_level = log_buf ? 1 : 0, attr.log_size = log_buf_size;
}; attr.log_level = log_buf ? 1 : 0;
attr.kern_version = kern_version; attr.kern_version = kern_version;
if (log_buf) if (log_buf)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment