On 26/06/2024 08:29, Totoro W wrote: > Hi folks, > > This is my first time to ask questions in this mailing list. I'm the > author of https://github.com/tw4452852/zbpf which is a framework to > write BPF programs with Zig toolchain. > During the development, as the BTF is totally generated by the Zig > toolchain, some naming conventions will make the BTF verifier refuse > to load. > Right now I have to patch the libbpf to do some fixup before loading > into the kernel > (https://github.com/tw4452852/libbpf_zig/blob/main/0001-temporary-WA-for-invalid-BTF-info-generated-by-Zig.patch). > Even though this just work-around the issue, I'm still curious about > the current naming sanitation, I want to know some background about > it. > If possible, could we relax this to accept more languages (like Zig) > to write BPF programs? Thanks in advance. > > For reference, here the BTF generated by Zig for this program > (https://github.com/tw4452852/zbpf/blob/main/samples/perf_event.zig) > > [1] PTR '*[4]u8' type_id=3 The problem here as Eduard mentioned is that the zig compiler appears to be generating unneeded names for pointers, and then you're working around this in zbpf, is that right? It's not clear to me what that pointer name adds - I suspect it's saying it's a pointer to an array of 4 u8s, but we get that from the fact it's a PTR to type_id 3 - an ARRAY with element type 'u8' (type id 2) and nr_elems=4, no name is needed. So the name doesn't add any information it seems; or at least the info the name provides can be reconstructed from the BTF without having the name. So the root problem here appears to be the zig compiler's BTF generation. If there are some language constraints that require some sort of name annotation for pointers, couldn't that be done via BTF type tags or via some other compatible mechanism? So I think we need to understand whether the BTF incompatibilities arise due to genuine language features or if they are the result of incorrectly-generated BTF during zig compilation. I dug around a bit in the zig github repo but could only find BTF parsing code, not code for BTF generation. Finding where the BTF is generated in the zig toolchain and understanding why it is generating names for pointers is the first step here I think. Alan > [2] INT 'u8' size=1 bits_offset=0 nr_bits=8 encoding=(none) > [3] ARRAY '(anon)' type_id=2 index_type_id=4 nr_elems=4 > [4] INT '__ARRAY_SIZE_TYPE__' size=4 bits_offset=0 nr_bits=32 encoding=(none) > [5] PTR '*u32' type_id=6 > [6] INT 'u32' size=4 bits_offset=0 nr_bits=32 encoding=(none) > [7] STRUCT 'map.Map.Def' size=24 vlen=3 > 'type' type_id=1 bits_offset=0 > 'key' type_id=5 bits_offset=64 > 'value' type_id=5 bits_offset=128 > [8] VAR 'events' type_id=7, linkage=global > [9] PTR '*[2]u8' type_id=10 > [10] ARRAY '(anon)' type_id=2 index_type_id=4 nr_elems=2 > [11] PTR '*[1]u8' type_id=12 > [12] ARRAY '(anon)' type_id=2 index_type_id=4 nr_elems=1 > [13] STRUCT 'map.Map.Def' size=32 vlen=4 > 'type' type_id=9 bits_offset=0 > 'key' type_id=5 bits_offset=64 > 'value' type_id=5 bits_offset=128 > 'max_entries' type_id=11 bits_offset=192 > [14] VAR 'my_pid' type_id=13, linkage=global > [15] FUNC_PROTO '(anon)' ret_type_id=16 vlen=1 > '(anon)' type_id=17 > [16] INT 'c_int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED > [17] PTR '*perf_event.test_perf_event_array__opaque_478' type_id=18 > [18] STRUCT 'perf_event.test_perf_event_array__opaque_478' size=0 vlen=0 > [19] FUNC 'test_perf_event_array' type_id=15 linkage=global > [20] FUNC_PROTO '(anon)' ret_type_id=21 vlen=1 > '(anon)' type_id=21 > [21] INT 'usize' size=8 bits_offset=0 nr_bits=64 encoding=(none) > [22] FUNC 'getauxvalImpl' type_id=20 linkage=global > [23] ARRAY '(anon)' type_id=2 index_type_id=4 nr_elems=3 > [24] VAR '_license' type_id=23, linkage=global > [25] DATASEC '.maps' size=0 vlen=2 > type_id=8 offset=0 size=24 (VAR 'events') > type_id=14 offset=0 size=32 (VAR 'my_pid') > [26] DATASEC 'license' size=0 vlen=1 > type_id=24 offset=0 size=4 (VAR '_license') > > > Regards. >