Skip to content

Commit 2f047b5

Browse files
committed
[clang][Sema] Fix a clang crash with btf_type_tag
For the following program, $ cat t.c struct t { int (__attribute__((btf_type_tag("rcu"))) *f)(); int a; }; int foo(struct t *arg) { return arg->a; } Compiling with 'clang -g -O2 -S t.c' will cause a failure like below: clang: /home/yhs/work/llvm-project/clang/lib/Sema/SemaType.cpp:6391: void {anonymous}::DeclaratorLocFiller::VisitParenTypeLoc(clang::ParenTypeLoc): Assertion `Chunk.Kind == DeclaratorChunk::Paren' failed. PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script. Stack dump: ...... #5 0x00007f89e4280ea5 abort (/lib64/libc.so.6+0x21ea5) #6 0x00007f89e4280d79 _nl_load_domain.cold.0 (/lib64/libc.so.6+0x21d79) #7 0x00007f89e42a6456 (/lib64/libc.so.6+0x47456) #8 0x00000000045c2596 GetTypeSourceInfoForDeclarator((anonymous namespace)::TypeProcessingState&, clang::QualType, clang::TypeSourceInfo*) SemaType.cpp:0:0 #9 0x00000000045ccfa5 GetFullTypeForDeclarator((anonymous namespace)::TypeProcessingState&, clang::QualType, clang::TypeSourceInfo*) SemaType.cpp:0:0 ...... The reason of the failure is due to the mismatch of TypeLoc and D.getTypeObject().Kind. For example, the TypeLoc is BTFTagAttributedType 0x88614e0 'int btf_type_tag(rcu)()' sugar |-ParenType 0x8861480 'int ()' sugar | `-FunctionNoProtoType 0x8861450 'int ()' cdecl | `-BuiltinType 0x87fd500 'int' while corresponding D.getTypeObject().Kind points to DeclaratorChunk::Paren, and this will cause later assertion. To fix the issue, similar to AttributedTypeLoc, let us skip BTFTagAttributedTypeLoc in GetTypeSourceInfoForDeclarator(). Differential Revision: https://reviews.llvm.org/D136807
1 parent 4299b28 commit 2f047b5

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

clang/docs/ReleaseNotes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ Bug Fixes
268268
functions. `Issue 56154 <https://github.com/llvm/llvm-project/issues/56154>`_
269269
- Fix handling of unexpanded packs in template argument expressions.
270270
`Issue 58679 <https://github.com/llvm/llvm-project/issues/58679>`_
271+
- Fix a crash when a ``btf_type_tag`` attribute is applied to the pointee of
272+
a function pointer.
271273

272274
Improvements to Clang's diagnostics
273275
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaType.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -6515,6 +6515,9 @@ GetTypeSourceInfoForDeclarator(TypeProcessingState &State,
65156515
CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
65166516
}
65176517

6518+
while (BTFTagAttributedTypeLoc TL = CurrTL.getAs<BTFTagAttributedTypeLoc>())
6519+
CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
6520+
65186521
while (DependentAddressSpaceTypeLoc TL =
65196522
CurrTL.getAs<DependentAddressSpaceTypeLoc>()) {
65206523
fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -triple %itanium_abi_triple -debug-info-kind=limited -S -emit-llvm -o - %s | FileCheck %s
2+
3+
struct t {
4+
int (__attribute__((btf_type_tag("rcu"))) *f)();
5+
int a;
6+
};
7+
int foo(struct t *arg) {
8+
return arg->a;
9+
}
10+
11+
// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "f"
12+
// CHECK-SAME: baseType: ![[L18:[0-9]+]]
13+
// CHECK: ![[L18]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[#]], size: [[#]], annotations: ![[L21:[0-9]+]])
14+
// CHECK: ![[L21]] = !{![[L22:[0-9]+]]}
15+
// CHECK: ![[L22]] = !{!"btf_type_tag", !"rcu"}

0 commit comments

Comments
 (0)