[PATCH] D136807: [clang][Sema] Fix a clang crash with btf_type_tag

Yonghong Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 26 17:57:45 PDT 2022


yonghong-song created this revision.
yonghong-song added reviewers: aaron.ballman, dblaikie.
yonghong-song added a project: clang.
Herald added a project: All.
yonghong-song requested review of this revision.
Herald added a subscriber: cfe-commits.

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().


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136807

Files:
  clang/lib/Sema/SemaType.cpp
  clang/test/Sema/attr-btf_type_tag-func-ptr.c


Index: clang/test/Sema/attr-btf_type_tag-func-ptr.c
===================================================================
--- /dev/null
+++ clang/test/Sema/attr-btf_type_tag-func-ptr.c
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -x c -triple x86_64-pc-linux-gnu -dwarf-version=4 -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+struct t {
+ int (__attribute__((btf_type_tag("rcu"))) *f)();
+ int a;
+};
+int foo(struct t *arg) {
+  return arg->a;
+}
Index: clang/lib/Sema/SemaType.cpp
===================================================================
--- clang/lib/Sema/SemaType.cpp
+++ clang/lib/Sema/SemaType.cpp
@@ -6516,6 +6516,9 @@
       CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
     }
 
+    while (BTFTagAttributedTypeLoc TL = CurrTL.getAs<BTFTagAttributedTypeLoc>())
+      CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
+
     while (DependentAddressSpaceTypeLoc TL =
                CurrTL.getAs<DependentAddressSpaceTypeLoc>()) {
       fillDependentAddressSpaceTypeLoc(TL, D.getTypeObject(i).getAttrs());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136807.470982.patch
Type: text/x-patch
Size: 1032 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221027/77d9b787/attachment.bin>


More information about the cfe-commits mailing list