Skip to content

Commit

Permalink
Correctly handle forward declarations in GenBtf::type_declaration()
Browse files Browse the repository at this point in the history
Adjust the logic in GenBtf::type_declaration() to correctly handle
forward declarations. That is generally useful, but will needed
particularly for struct_ops shadow type support.

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o authored and danielocfb committed Mar 21, 2024
1 parent 77cc68c commit 2da41e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions libbpf-cargo/src/gen/btf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ impl<'s> GenBtf<'s> {
}
BtfKind::Struct | BtfKind::Union | BtfKind::Enum | BtfKind::Enum64 =>
self.get_type_name_handling_anon_types(&ty).into_owned(),
// The only way a variable references a function is through a function pointer.
// Return c_void here so the final def will look like `*mut c_void`.
// The only way a variable references a function or forward declaration is through a
// pointer. Return c_void here so the final def will look like `*mut c_void`.
//
// It's not like rust code can call a function inside a bpf prog either so we don't
// really need a full definition. `void *` is totally sufficient for sharing a pointer.
BtfKind::Func | BtfKind::FuncProto => "std::ffi::c_void".to_string(),
BtfKind::Fwd | BtfKind::Func | BtfKind::FuncProto => "std::ffi::c_void".to_string(),
BtfKind::Var(t) => self.type_declaration(t.referenced_type())?,
_ => bail!("Invalid type: {ty:?}"),
});
Expand Down
21 changes: 21 additions & 0 deletions libbpf-cargo/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,27 @@ impl Default for Foo {
assert_definition(&btf, &struct_foo, expected_output);
}

#[test]
fn test_btf_dump_fwd() {
let prog_text = r#"
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
struct sometypethatdoesnotexist *m;
"#;

let mmap = build_btf_mmap(prog_text);
let btf = btf_from_mmap(&mmap);

let m = find_type_in_btf!(btf, types::Var<'_>, "m");

assert_eq!(
"*mut std::ffi::c_void",
btf.type_declaration(*m)
.expect("Failed to generate foo decl")
);
}

#[test]
fn test_btf_dump_align() {
let prog_text = r#"
Expand Down

0 comments on commit 2da41e7

Please sign in to comment.