-
Notifications
You must be signed in to change notification settings - Fork 204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: improve code readability of struct initialization #633
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧪 Since the PR has been fully tested, please consider merging it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
👍 其实在 bpf c 代码里结构体先零值初始化再逐字段赋值是最佳实践。考虑以下代码:
就有可能被 verifier 拒绝,因为 clang 编译上面的代码发现 ifindex 字段没有被使用,索性不初始化那个字节的栈内存,留下一个空洞,但是 bpf verifier 检查 bpf_map_delete_elem 的时候会要求所有参数指针指向的内存必须被初始化,所以程序就挂了。 这个 PR 没有问题是因为那些结构体所有字段最终都被赋值了,没有空洞。 |
@jschwinger233 解释非常清晰,先把这个 pr 回退,意下如何? |
但是根据 cppreference:
另外,在 eBPF 官网上,也有直接初始化的写法: |
@Integral-Tech 还有一种情况是结构体本身有空洞,比如
为了内存对齐这个结构体会在两个字段之间插入 padding,这时候就算直接所有字段都直接赋值也会有空洞,verifier 会拒绝。 |
还有一点是逐字段赋值能 CO-RE,但是直接初始化不能。 |
Background
Checklist
Full Changelogs
Test Result