Skip to content

Commit

Permalink
[CodeGen][UBSan] Handle sugared QualTypes correctly in
Browse files Browse the repository at this point in the history
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
  • Loading branch information
usama54321 committed Aug 16, 2023
1 parent 6f11750 commit 9afc57d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions clang/test/CodeGen/ubsan-function-attributed.c
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit 9afc57d

Please sign in to comment.