[llvm-branch-commits] [clang] 729272f - [CodeGen][UBSan] Handle sugared QualTypes correctly in

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Aug 21 22:51:21 PDT 2023


Author: usama hameed
Date: 2023-08-22T07:43:40+02:00
New Revision: 729272fb0e172ca9e9cab9091470b790a9a582e3

URL: https://github.com/llvm/llvm-project/commit/729272fb0e172ca9e9cab9091470b790a9a582e3
DIFF: https://github.com/llvm/llvm-project/commit/729272fb0e172ca9e9cab9091470b790a9a582e3.diff

LOG: [CodeGen][UBSan] Handle sugared QualTypes correctly in
getUBSanFunctionTypeHash.

getUBSanFunctionTypeHash checks if a Type is a FunctionNoPrototype
by calling isa<FunctionNoProtoType>(). This does not work correctly when
the Type is wrapped in a sugar type such as an AttributedType. This
patch fixes this by using isFunctionNoProtoType() function which removes
sugar and returns the expected result.

The added test is a sanity check that the compiler no longer crashes
during compilation. It also compares the hash with and without the
function attribute for both FunctionNoProtoType and FunctionProtoType.
The hash remains the same for FunctionNoProtoType even with the addition
of an attribute.

rdar://113144087

Differential Revision: https://reviews.llvm.org/D157445

(cherry picked from commit 9afc57dcb2e5cd36ca1ddf0fee3efa958bfd4c2a)

Added: 
    clang/test/CodeGen/ubsan-function-attributed.c

Modified: 
    clang/lib/CodeGen/CodeGenFunction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index d71fda22affb1d..7ef893cb1a2d79 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -572,7 +572,7 @@ llvm::ConstantInt *
 CodeGenFunction::getUBSanFunctionTypeHash(QualType Ty) const {
   // Remove any (C++17) exception specifications, to allow calling e.g. a
   // noexcept function through a non-noexcept pointer.
-  if (!isa<FunctionNoProtoType>(Ty))
+  if (!Ty->isFunctionNoProtoType())
     Ty = getContext().getFunctionTypeWithExceptionSpec(Ty, EST_None);
   std::string Mangled;
   llvm::raw_string_ostream Out(Mangled);

diff  --git a/clang/test/CodeGen/ubsan-function-attributed.c b/clang/test/CodeGen/ubsan-function-attributed.c
new file mode 100644
index 00000000000000..c979f161fc9220
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-function-attributed.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -S -triple x86_64 -std=c17 -fsanitize=function %s -o - | FileCheck %s --check-prefixes=CHECK
+
+// CHECK: .long	248076293
+void __attribute__((ms_abi)) f(void) {}
+
+// CHECK: .long	905068220
+void g(void) {}
+
+// CHECK: .long	1717976574
+void __attribute__((ms_abi)) f_no_prototype() {}
+
+// CHECK: .long	1717976574
+void g_no_prototype() {}


        


More information about the llvm-branch-commits mailing list