Skip to content
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

Correctly handle forward declarations in GenBtf::type_declaration() #730

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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