libbpf-rs icon indicating copy to clipboard operation
libbpf-rs copied to clipboard

libbpf-cargo: Build fails when a C struct contains an empty union

Open JoelColledge opened this issue 1 year ago • 1 comments

I am using libbpf-cargo version 0.24.6. I'm trying to use struct bio like this in my .bpf.c:

SEC("fentry/submit_bio")
int BPF_PROG(submit_bio, struct bio *bio)
{
...
	vcnt = bio->bi_vcnt;
...
}

However cargo build fails with:

  thread 'main' panicked at /home/jrc/.cargo/registry/src/index.crates.io-6f17d22bba15001f/libbpf-cargo-0.24.6/src/gen/btf.rs:769:49:
  index out of bounds: the len is 0 but the index is 0

The error refers to this line, which is part of the code to "write a Default implementation for a union".

At vmlinux_601.h#L7735, we find this:

struct bio {
...
	union {};
...
};

It looks like empty unions cause problems for libbpf-cargo. Arguably this is an issue of vmlinux.h, and I am aware that the vmlinux.h repo is advertised as "Development-only". This issue could be fixed by removing the empty union in vmlinux.h. However, it seems more convenient to me to add support for empty unions to libbpf-cargo. Is that reasonable?

JoelColledge avatar Oct 21 '24 16:10 JoelColledge

Thanks for the report.

However, it seems more convenient to me to add support for empty unions to libbpf-cargo. Is that reasonable?

Yes, that seems reasonable to me. Will you take a look at fixing this problem?

danielocfb avatar Oct 21 '24 16:10 danielocfb

#975 works for my case. Looking forward to a review.

JoelColledge avatar Oct 22 '24 12:10 JoelColledge