-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix error in dereferencing kernel double pointers #3024
Conversation
010f73f
to
2ac40b1
Compare
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.
nice find! did you report this upstream? wonder if this should be supported by kernel
Not yet, but I'll report it. I wanted to check first if it is a bug or a feature :) |
Seems more like a lack of a feature to me |
Turns out the same problem exists when dereferencing via array accesses, i.e. |
2ac40b1
to
2c14956
Compare
Fixed the array access on our side, too. I'll report/fix kernel upstream. Originally, I wanted to separate the dereference fix from the array access one b/c the latter is one of two issues causing #3016. It turned out that it makes more sense to have them together so I added the other fix for #3016 to this PR. We now have two changelog entries with the same PR number but I think that it makes sense to merge everything together. I'll also update the PR description. |
Why is it that we need to reset |
It's actually the other way around - for pointers, we just reset the pointed type, for arrays we reset both the array and the element types. The reason is that there's a slight difference in codegen for
|
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.
Makes sense, thanks!
Is it possible to add a unit test for the FieldAnalyser change?
2c14956
to
bd435a8
Compare
Sure! I added a test (and confirmed that it failed prior to this PR). |
bd435a8
to
b3995f0
Compare
While adding the test, I updated |
b3995f0
to
4188b82
Compare
4188b82
to
28f49bc
Compare
This slipped off my radar. I rebased onto the current master and will merge today afternoon if there are no objections. |
Looks like a legit unit test failure |
28f49bc
to
580e791
Compare
This is a tricky one. It doesn't reproduce on a local build (at least not on my setup), only in Nix. It's caused by this (seemingly trivial) change in diff --git a/tests/data/data_source.c b/tests/data/data_source.c
index c6a7f713..eaecbc45 100644
--- a/tests/data/data_source.c
+++ b/tests/data/data_source.c
@@ -21,7 +21,7 @@ struct Foo3 {
struct Foo3 foo3;
-struct Foo3 *func_1(int a, struct Foo1 *foo1, struct Foo2 *foo2)
+struct Foo3 *func_1(int a, struct Foo1 *foo1, struct Foo2 *foo2, struct Foo3 *foo3)
{
return 0;
}
@@ -115,7 +115,7 @@ int main(void)
struct bpf_iter__task_file iter_task_file;
struct bpf_iter__task_vma iter_task_vma;
- func_1(0, 0, 0);
+ func_1(0, 0, 0, 0);
bpf_iter_task();
bpf_iter_task_file(); With this, the |
I have reproduced the environment with the error and will have a look at it 🙂 |
This look like an issue with LLDB and/or GCC 12.3.0, but not with GCC 11.4.1. I'm investigating further... |
580e791
to
143adae
Compare
143adae
to
817f6bb
Compare
Works great, thanks! I added it to the PR, should be now ready to merge. |
817f6bb
to
3bd2f52
Compare
We did not resolve structs for array accesses on pointer types in FieldAnalyser which causes failures when applying the array access operator on pointers to structs: # bpftrace -e 'kfunc:vfs_read { print(args.file[0].f_inode); }' stdin:1:18-37: ERROR: Struct/union of type 'struct file' does not contain a field named 'f_inode' kfunc:vfs_read { print(args.file[0].f_inode); } ~~~~~~~~~~~~~~~~~~~ This fixes the issue by adding the field resolution.
BPF verifier can detect safety of pointer accesses for BTF-based probes (k(ret)func, iter) and therefore it is not necessary to use bpf_probe_read_kernel inside such probes. This feature was enabled in bpftrace by commit c2c3ab9 ("Support identifying btf type"). Unfortunately, the verifier is not able to track BTF information for dereferences and array accesses on double pointers so, e.g. the following script fails to load: # bpftrace -e 'kfunc:__module_get { print(args.module->trace_events[0]->flags);' } -v INFO: node count: 13 Attaching 1 probe... Error log: reg type unsupported for arg#0 function kfunc_vmlinux___module_get#22 0: R1=ctx(off=0,imm=0) R10=fp0 0: (79) r1 = *(u64 *)(r1 +0) func '__module_get' arg0 has btf_id 250 type STRUCT 'module' 1: R1_w=ptr_module(off=0,imm=0) 1: (79) r1 = *(u64 *)(r1 +1128) ; R1_w=scalar() 2: (79) r1 = *(u64 *)(r1 +0) R1 invalid mem access 'scalar' processed 3 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 ERROR: Error loading program: kfunc:vmlinux:__module_get A similar error happens when dereferencing the double pointer with `*` # bpftrace -e 'kfunc:__module_get { print((*args.module->trace_events)->flags);' } -v An analogous program fails to load even when written using libbpf. We need to use bpf_probe_read_kernel for such cases so do not propagate the SizedType::is_btftype flag when observing a dereference or array access of a double pointer in semantic analyser.
3bd2f52
to
0d0ddc9
Compare
BPF verifier can detect safety of pointer accesses for BTF-based probes (k(ret)func, iter) and therefore it is not necessary to use
bpf_probe_read_kernel
inside such probes. This feature was enabled in bpftrace by commit c2c3ab9 ("Support identifying btf type").Unfortunately, the verifier is not able to track BTF information for dereferences and array accesses on double pointers so, e.g. the following script fails to load:
A similar error happens when dereferencing the double pointer with
*
An analogous program fails to load even when written using libbpf.
We need to use
bpf_probe_read_kernel
for such cases so do not propagate theSizedType::is_btftype
flag when observing a dereference or array access of a double pointer in semantic analyser.This also fixes another bug related to array access - field analyser was not doing field resolution on array accesses on pointers to structs.
Overall, this resolves #3016.
Checklist
man/adoc/bpftrace.adoc
CHANGELOG.md