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

libbpf-cargo: Handle duplicated types in BTF more gracefully #1074

Merged
merged 2 commits into from
Jan 22, 2025

Conversation

danielocfb
Copy link
Collaborator

Our SkeletonBuilder currently only support building a skeleton from a single .bpf.c file. However, users may very well compile multiple .bpf.c files into .bpf.o and link them into a single object. In such a scenario, it is easily possible that we end up with multiple types of the same name in the corresponding BTF. Currently, this results in compilation errors.
With this change we handle this case more gracefully, by enumerating these types, similar to what libbpf does in its dumping logic.

Our SkeletonBuilder currently only support building a skeleton from a
single .bpf.c file. However, users may very well compile multiple .bpf.c
files into .bpf.o and link them into a single object. In such a
scenario, it is easily possible that we end up with multiple types of
the same name in the corresponding BTF. Currently, this results in
compilation errors.
With this change we handle this case more gracefully, by enumerating
these types, similar to what libbpf does in its dumping logic.

Signed-off-by: Daniel Müller <deso@posteo.net>
Copy link
Member

@anakryiko anakryiko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines 1172 to 1173
let _foobar0 = types::foobar::default();
let _foobar1 = types::foobar_1::default();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, wouldn't foobar_2 make more sense (it's a second instance of the same name, we just don't explicitly mark the first one with _1)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am living happily in my counting-starts-at-zero bubble :-)

But happy to change it.

Btw., strictly speaking I think the renaming algorithm is insufficient: we can't just assume that <type>_<cnt> is an available name, but should actually check that there is no collision with an existing type of that name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, I see :) I'd switch to two, but ultimately it doesn't matter all that much. If user gets to that point, there is no deterministic order, so whether it's foobar_1 or foobar_2, I wouldn't use any of those in the code directly because their ordering can change on the next compilation

and yeah, you are right about possibility of causing another unintended collision (which, on libbpf side, is mostly mitigated by using triple underscore, which makes it highly unlikely)

Now that our AnonTypes type performs more tasks than just assigning
names to anonymous types, let's rename it to TypeMap to reflect it
intent more clearly.

Signed-off-by: Daniel Müller <deso@posteo.net>
@d-e-s-o d-e-s-o merged commit e4b1736 into libbpf:master Jan 22, 2025
13 checks passed
@d-e-s-o d-e-s-o deleted the topic/dup-types branch January 22, 2025 22:48
@d-e-s-o
Copy link
Collaborator

d-e-s-o commented Jan 22, 2025

@etsal this should fix your compilation error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants