Skip to content

Commit 8732699

Browse files
mtardyeddyz87
authored andcommitted
[BPF] Visit nested map array during BTF generation
Fixes missing inner map struct type definitions [^1]. We should visit the type of nested array of maps like we do for global maps. This patch adds a boolean to convey the information to visitTypeEntry and visitDerivedType that the pointee is a map definition and should be treated as such. It ressembles and works with commit 0d21c95 ("[BPF] Handle nested wrapper structs in BPF map definition traversal (llvm#144097)") which focused on directly nested wrapper structs. Before that patch, this ARRAY_OF_MAPS definition would lead to the BTF information include the 'missing_type' as "FWD 'missing_type' fwd_kind=struct": struct missing_type { uint64_t foo; }; struct { __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS); [...] __array( values, struct { [...] __type(value, struct missing_type); }); } map SEC(".maps"); Which lead to errors while trying to load the map: libbpf: map 'outer_map.inner': can't determine value size for type [N]: -22. To solve this issue, users had to use the struct in a dummy variable or in a dummy function for the BTF to be generated correctly [^2]. [^1]: https://lore.kernel.org/netdev/[email protected]/T/#u [^2]: cilium/ebpf#1658 (reply in thread) Signed-off-by: Mahe Tardy <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]>
1 parent 56ae79a commit 8732699

File tree

2 files changed

+103
-19
lines changed

2 files changed

+103
-19
lines changed

llvm/lib/Target/BPF/BTFDebug.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,8 @@ void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
963963
auto Tag = DTy->getTag();
964964
if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
965965
Tag != dwarf::DW_TAG_volatile_type &&
966-
Tag != dwarf::DW_TAG_restrict_type)
966+
Tag != dwarf::DW_TAG_restrict_type &&
967+
Tag != dwarf::DW_TAG_pointer_type)
967968
break;
968969
Ty = DTy->getBaseType();
969970
}
@@ -973,26 +974,34 @@ void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
973974
return;
974975

975976
auto Tag = CTy->getTag();
976-
if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
977+
if ((Tag != dwarf::DW_TAG_structure_type &&
978+
Tag != dwarf::DW_TAG_array_type) ||
979+
CTy->isForwardDecl())
977980
return;
978981

979-
// Visit all struct members to ensure their types are visited.
980-
const DINodeArray Elements = CTy->getElements();
981-
for (const auto *Element : Elements) {
982-
const auto *MemberType = cast<DIDerivedType>(Element);
983-
const DIType *MemberBaseType = MemberType->getBaseType();
984-
985-
// If the member is a composite type, that may indicate the currently
986-
// visited composite type is a wrapper, and the member represents the
987-
// actual map definition.
988-
// In that case, visit the member with `visitMapDefType` instead of
989-
// `visitTypeEntry`, treating it specifically as a map definition rather
990-
// than as a regular composite type.
991-
const auto *MemberCTy = dyn_cast<DICompositeType>(MemberBaseType);
992-
if (MemberCTy) {
993-
visitMapDefType(MemberBaseType, TypeId);
994-
} else {
995-
visitTypeEntry(MemberBaseType);
982+
// Visit potential nested map array
983+
if (CTy->getTag() == dwarf::DW_TAG_array_type) {
984+
// Jump to the element type of the array
985+
visitMapDefType(CTy->getBaseType(), TypeId);
986+
} else {
987+
// Visit all struct members to ensure their types are visited.
988+
const DINodeArray Elements = CTy->getElements();
989+
for (const auto *Element : Elements) {
990+
const auto *MemberType = cast<DIDerivedType>(Element);
991+
const DIType *MemberBaseType = MemberType->getBaseType();
992+
993+
// If the member is a composite type, that may indicate the currently
994+
// visited composite type is a wrapper, and the member represents the
995+
// actual map definition.
996+
// In that case, visit the member with `visitMapDefType` instead of
997+
// `visitTypeEntry`, treating it specifically as a map definition rather
998+
// than as a regular composite type.
999+
const auto *MemberCTy = dyn_cast<DICompositeType>(MemberBaseType);
1000+
if (MemberCTy) {
1001+
visitMapDefType(MemberBaseType, TypeId);
1002+
} else {
1003+
visitTypeEntry(MemberBaseType);
1004+
}
9961005
}
9971006
}
9981007

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
; RUN: llc -mtriple=bpfel -mcpu=v3 -filetype=obj -o %t1 %s
2+
; RUN: llvm-objcopy --dump-section='.BTF'=%t2 %t1
3+
; RUN: %python %p/print_btf.py %t2 | FileCheck -check-prefixes=CHECK-BTF-SHORT %s
4+
; RUN: %python %p/print_btf.py %t2 | FileCheck -check-prefixes=CHECK-BTF %s
5+
; Source:
6+
; struct nested_value_type {
7+
; int a1;
8+
; };
9+
; struct map_type {
10+
; struct {
11+
; struct nested_value_type *value;
12+
; } *values[];
13+
; };
14+
; Compilation flags:
15+
; clang -target bpf -g -O2 -S -emit-llvm prog.c
16+
17+
; ModuleID = 'prog.c'
18+
source_filename = "prog.c"
19+
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
20+
target triple = "bpf"
21+
22+
%struct.map_type = type { [0 x ptr] }
23+
24+
@array_of_maps = dso_local local_unnamed_addr global %struct.map_type zeroinitializer, section ".maps", align 8, !dbg !0
25+
26+
; We expect no forward declarations.
27+
;
28+
; CHECK-BTF-SHORT-NOT: FWD
29+
30+
; Assert the whole BTF.
31+
;
32+
; CHECK-BTF: [1] PTR '(anon)' type_id=5
33+
; CHECK-BTF-NEXT: [2] PTR '(anon)' type_id=3
34+
; CHECK-BTF-NEXT: [3] STRUCT 'nested_value_type' size=4 vlen=1
35+
; CHECK-BTF-NEXT: 'a1' type_id=4 bits_offset=0
36+
; CHECK-BTF-NEXT: [4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
37+
; CHECK-BTF-NEXT: [5] STRUCT '(anon)' size=8 vlen=1
38+
; CHECK-BTF-NEXT: 'value' type_id=2 bits_offset=0
39+
; CHECK-BTF-NEXT: [6] ARRAY '(anon)' type_id=1 index_type_id=7 nr_elems=0
40+
; CHECK-BTF-NEXT: [7] INT '__ARRAY_SIZE_TYPE__' size=4 bits_offset=0 nr_bits=32 encoding=(none)
41+
; CHECK-BTF-NEXT: [8] STRUCT 'map_type' size=0 vlen=1
42+
; CHECK-BTF-NEXT: 'values' type_id=6 bits_offset=0
43+
; CHECK-BTF-NEXT: [9] VAR 'array_of_maps' type_id=8, linkage=global
44+
; CHECK-BTF-NEXT: [10] DATASEC '.maps' size=0 vlen=1
45+
; CHECK-BTF-NEXT: type_id=9 offset=0 size=0
46+
47+
!llvm.dbg.cu = !{!2}
48+
!llvm.module.flags = !{!20, !21, !22, !23}
49+
!llvm.ident = !{!24}
50+
51+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
52+
!1 = distinct !DIGlobalVariable(name: "array_of_maps", scope: !2, file: !3, line: 9, type: !5, isLocal: false, isDefinition: true)
53+
!2 = distinct !DICompileUnit(language: DW_LANG_C11, file: !3, producer: "clang version 22.0.0git ([email protected]:llvm/llvm-project.git ed93eaa421b714028b85cc887d80c45991d7207f)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: None)
54+
!3 = !DIFile(filename: "prog.c", directory: "/home/mtardy/llvm-bug-repro", checksumkind: CSK_MD5, checksum: "9381d9e83e9c0b235a14704224815e96")
55+
!4 = !{!0}
56+
!5 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "map_type", file: !3, line: 4, elements: !6)
57+
!6 = !{!7}
58+
!7 = !DIDerivedType(tag: DW_TAG_member, name: "values", scope: !5, file: !3, line: 7, baseType: !8)
59+
!8 = !DICompositeType(tag: DW_TAG_array_type, baseType: !9, elements: !18)
60+
!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64)
61+
!10 = distinct !DICompositeType(tag: DW_TAG_structure_type, scope: !5, file: !3, line: 5, size: 64, elements: !11)
62+
!11 = !{!12}
63+
!12 = !DIDerivedType(tag: DW_TAG_member, name: "value", scope: !10, file: !3, line: 6, baseType: !13, size: 64)
64+
!13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64)
65+
!14 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "nested_value_type", file: !3, line: 1, size: 32, elements: !15)
66+
!15 = !{!16}
67+
!16 = !DIDerivedType(tag: DW_TAG_member, name: "a1", scope: !14, file: !3, line: 2, baseType: !17, size: 32)
68+
!17 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
69+
!18 = !{!19}
70+
!19 = !DISubrange(count: -1)
71+
!20 = !{i32 7, !"Dwarf Version", i32 5}
72+
!21 = !{i32 2, !"Debug Info Version", i32 3}
73+
!22 = !{i32 1, !"wchar_size", i32 4}
74+
!23 = !{i32 7, !"frame-pointer", i32 2}
75+
!24 = !{!"clang version 22.0.0git ([email protected]:llvm/llvm-project.git ed93eaa421b714028b85cc887d80c45991d7207f)"}

0 commit comments

Comments
 (0)