iovisor / iovisor/bcc

Leaf() considered the padded space as a valid struct member

Open
#2,255 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
22.7k
Forks
4.1k
Avg merge
10d 4h
Merged PRs (30d)
3

Description

I found a regression since 0.6.0. If there is a struct member is aligned, Leaf() may consider the padded space as a valid member.

Here is the bug reproducer for x86_64:

```python
#!/usr/bin/python

from bcc import BPF
from ctypes import c_int

prog="""
struct test {
int a;
u64 b;
};

BPF_HASH(my_hash, u64, struct test, 4096);

int
kprobe__sys_clone(void *ctx)
{
u64 idx = 0;
struct test *leaf = my_hash.lookup(&idx);
if (leaf) {
bpf_trace_printk("my_hash[0].a = %d\\n", leaf->a);
bpf_trace_printk("my_hash[0].b = %d\\n", leaf->b);
}
return 0;
}
"""

b=BPF(text=prog)
my_hash = b.get_table("my_hash")
my_hash[c_int(0)] = my_hash.Leaf(1, 2)

try:
b.trace_print()
except KeyboardInterrupt:
exit()
```

The code looked legit but it failed with the following error.

```
Traceback (most recent call last):
File "./phantom-member.py", line 29, in
my_hash[c_int(0)] = my_hash.Leaf(1, 2)
TypeError: expected bytes, int found
```

The program worked again when I added a bytes parameter to Leaf() like this:
```
my_hash[c_int(0)] = my_hash.Leaf(1, b"", 2)
```
or added ``__attribute__((packed))`` to ``struct test``, so it's obvious an issue about padding.

Since the issue happened between 0.5.0 and 0.6.0, it's likely related to https://github.com/iovisor/bcc/commit/538a84e1f821a97468a55d0e97a5c0a4617a1271 and
https://github.com/iovisor/bcc/commit/b32b4a5fffb61ae6fd5e75e30c06763ade8ed1a4.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.