Skip to content

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

@JoelColledge

Description

@JoelColledge

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions