Skip to content

Commit a5a6cfe

Browse files
yonghong-songtstellar
authored andcommitted
BPF: permit type modifiers for __builtin_btf_type_id() relocation
Lorenz Bauer from Cloudflare tried to use "const struct <name>" as the type for __builtin_btf_type_id(*(const struct <name>)0, 1) relocation and hit a llvm BPF fatal error. https://lore.kernel.org/bpf/[email protected]/ ... fatal error: error in backend: Empty type name for BTF_TYPE_ID_REMOTE reloc Currently, we require the debuginfo type itself must have a name. In this case, the debuginfo type is "const" which points to "struct <name>". The "const" type does not have a name, hence the above fatal error will be triggered. Let us permit "const" and "volatile" type modifiers. We skip modifiers in some other cases as well like structure member type tracing. This can aviod the above fatal error. Differential Revision: https://reviews.llvm.org/D97986 (cherry picked from commit 9c0274c)
1 parent e0fe1c5 commit a5a6cfe

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

llvm/lib/Target/BPF/BPFPreserveDIType.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,17 @@ static bool BPFPreserveDITypeImpl(Function &F) {
8585
} else {
8686
Reloc = BPFCoreSharedInfo::BTF_TYPE_ID_REMOTE;
8787
DIType *Ty = cast<DIType>(MD);
88+
while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
89+
unsigned Tag = DTy->getTag();
90+
if (Tag != dwarf::DW_TAG_const_type &&
91+
Tag != dwarf::DW_TAG_volatile_type)
92+
break;
93+
Ty = DTy->getBaseType();
94+
}
95+
8896
if (Ty->getName().empty())
8997
report_fatal_error("Empty type name for BTF_TYPE_ID_REMOTE reloc");
98+
MD = Ty;
9099
}
91100

92101
BasicBlock *BB = Call->getParent();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
; RUN: opt -O2 -mtriple=bpf-pc-linux -S -o %t1 %s
2+
; RUN: llc -filetype=asm -o - %t1 | FileCheck -check-prefixes=CHECK %s
3+
; RUN: llc -mattr=+alu32 -filetype=asm -o - %t1 | FileCheck -check-prefixes=CHECK %s
4+
; Source code:
5+
; struct s {
6+
; int a;
7+
; };
8+
; int test(void) {
9+
; return __builtin_btf_type_id(*(const struct s *)0, 1);
10+
; }
11+
; Compilation flag:
12+
; clang -target bpf -O2 -g -S -emit-llvm -Xclang -disable-llvm-passes test.c
13+
14+
; Function Attrs: nounwind
15+
define dso_local i32 @test() #0 !dbg !7 {
16+
entry:
17+
%0 = call i64 @llvm.bpf.btf.type.id(i32 0, i64 1), !dbg !11, !llvm.preserve.access.index !12
18+
%conv = trunc i64 %0 to i32, !dbg !11
19+
ret i32 %conv, !dbg !16
20+
}
21+
22+
; CHECK: .long 1 # BTF_KIND_INT(id = 2)
23+
; CHECK-NEXT: .long 16777216 # 0x1000000
24+
; CHECK-NEXT: .long 4
25+
; CHECK-NEXT: .long 16777248 # 0x1000020
26+
27+
; CHECK: .long 16 # BTF_KIND_STRUCT(id = 4)
28+
; CHECK-NEXT: .long 67108865 # 0x4000001
29+
; CHECK-NEXT: .long 4
30+
; CHECK-NEXT: .long 18
31+
; CHECK-NEXT: .long 2
32+
33+
; CHECK: .ascii "int" # string offset=1
34+
; CHECK: .ascii ".text" # string offset=10
35+
; CHECK: .byte 115 # string offset=16
36+
; CHECK: .byte 97 # string offset=18
37+
; CHECK: .byte 48 # string offset=20
38+
39+
; CHECK: .long 16 # FieldReloc
40+
; CHECK-NEXT: .long 10 # Field reloc section string offset=10
41+
; CHECK-NEXT: .long 1
42+
; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
43+
; CHECK-NEXT: .long 4
44+
; CHECK-NEXT: .long 20
45+
; CHECK-NEXT: .long 7
46+
47+
; Function Attrs: nounwind readnone
48+
declare i64 @llvm.bpf.btf.type.id(i32, i64) #1
49+
50+
attributes #0 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
51+
attributes #1 = { nounwind readnone }
52+
53+
!llvm.dbg.cu = !{!0}
54+
!llvm.module.flags = !{!3, !4, !5}
55+
!llvm.ident = !{!6}
56+
57+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git 9783e2098800b954c55ae598a1ce5c4b93444fc0)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
58+
!1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/bpf/test")
59+
!2 = !{}
60+
!3 = !{i32 7, !"Dwarf Version", i32 4}
61+
!4 = !{i32 2, !"Debug Info Version", i32 3}
62+
!5 = !{i32 1, !"wchar_size", i32 4}
63+
!6 = !{!"clang version 13.0.0 (https://github.com/llvm/llvm-project.git 9783e2098800b954c55ae598a1ce5c4b93444fc0)"}
64+
!7 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 4, type: !8, scopeLine: 4, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
65+
!8 = !DISubroutineType(types: !9)
66+
!9 = !{!10}
67+
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
68+
!11 = !DILocation(line: 5, column: 10, scope: !7)
69+
!12 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !13)
70+
!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s", file: !1, line: 1, size: 32, elements: !14)
71+
!14 = !{!15}
72+
!15 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !13, file: !1, line: 2, baseType: !10, size: 32)
73+
!16 = !DILocation(line: 5, column: 3, scope: !7)

0 commit comments

Comments
 (0)