• Andrii Nakryiko's avatar
    libbpf: Fix BTF-to-C converter's padding logic · ea2ce1ba
    Andrii Nakryiko authored
    Turns out that btf_dump API doesn't handle a bunch of tricky corner
    cases, as reported by Per, and further discovered using his testing
    Python script ([0]).
    
    This patch revamps btf_dump's padding logic significantly, making it
    more correct and also avoiding unnecessary explicit padding, where
    compiler would pad naturally. This overall topic turned out to be very
    tricky and subtle, there are lots of subtle corner cases. The comments
    in the code tries to give some clues, but comments themselves are
    supposed to be paired with good understanding of C alignment and padding
    rules. Plus some experimentation to figure out subtle things like
    whether `long :0;` means that struct is now forced to be long-aligned
    (no, it's not, turns out).
    
    Anyways, Per's script, while not completely correct in some known
    situations, doesn't show any obvious cases where this logic breaks, so
    this is a nice improvement over the previous state of this logic.
    
    Some selftests had to be adjusted to accommodate better use of natural
    alignment rules, eliminating some unnecessary padding, or changing it to
    `type: 0;` alignment markers.
    
    Note also that for when we are in between bitfields, we emit explicit
    bit size, while otherwise we use `: 0`, this feels much more natural in
    practice.
    
    Next patch will add few more test cases, found through randomized Per's
    script.
    
      [0] https://lore.kernel.org/bpf/85f83c333f5355c8ac026f835b18d15060725fcb.camel@ericsson.com/Reported-by: default avatarPer Sundström XP <per.xp.sundstrom@ericsson.com>
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20221212211505.558851-6-andrii@kernel.org
    ea2ce1ba
btf_dump.c 68.6 KB