Skip to content

Commit

Permalink
-fsanitize=function: fix MSVC hashing to sugared type (#66816)
Browse files Browse the repository at this point in the history
Hashing the sugared type instead of the canonical type meant that
a simple example like this would always fail under MSVC:

```
static auto l() {}
int main() {
  auto a = l;
  a();
}
```
`clang --target=x86_64-pc-windows-msvc -fno-exceptions
-fsanitize=function -g -O0 -fuse-ld=lld -o test.exe test.cc`

produces:
```
test.cc:4:3: runtime error: call to function l through pointer to incorrect function type 'void (*)()'
```
  • Loading branch information
mizvekov committed Oct 2, 2023
1 parent f776e0b commit 9c89b29
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions clang/include/clang/AST/Mangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ class MangleContext {
/// or type uniquing.
/// TODO: Extend this to internal types by generating names that are unique
/// across translation units so it can be used with LTO.
virtual void mangleTypeName(QualType T, raw_ostream &,
bool NormalizeIntegers = false) = 0;
virtual void mangleCanonicalTypeName(QualType T, raw_ostream &,
bool NormalizeIntegers = false) = 0;

/// @}
};
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ std::string SYCLUniqueStableNameExpr::ComputeName(ASTContext &Context,
std::string Buffer;
Buffer.reserve(128);
llvm::raw_string_ostream Out(Buffer);
Ctx->mangleTypeName(Ty, Out);
Ctx->mangleCanonicalTypeName(Ty, Out);

return Out.str();
}
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/ItaniumMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ class ItaniumMangleContextImpl : public ItaniumMangleContext {
void mangleCXXRTTI(QualType T, raw_ostream &) override;
void mangleCXXRTTIName(QualType T, raw_ostream &,
bool NormalizeIntegers) override;
void mangleTypeName(QualType T, raw_ostream &,
bool NormalizeIntegers) override;
void mangleCanonicalTypeName(QualType T, raw_ostream &,
bool NormalizeIntegers) override;

void mangleCXXCtorComdat(const CXXConstructorDecl *D, raw_ostream &) override;
void mangleCXXDtorComdat(const CXXDestructorDecl *D, raw_ostream &) override;
Expand Down Expand Up @@ -7132,8 +7132,8 @@ void ItaniumMangleContextImpl::mangleCXXRTTIName(
Mangler.mangleType(Ty);
}

void ItaniumMangleContextImpl::mangleTypeName(QualType Ty, raw_ostream &Out,
bool NormalizeIntegers = false) {
void ItaniumMangleContextImpl::mangleCanonicalTypeName(
QualType Ty, raw_ostream &Out, bool NormalizeIntegers = false) {
mangleCXXRTTIName(Ty, Out, NormalizeIntegers);
}

Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/MicrosoftMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ class MicrosoftMangleContextImpl : public MicrosoftMangleContext {
mangleCXXRTTICompleteObjectLocator(const CXXRecordDecl *Derived,
ArrayRef<const CXXRecordDecl *> BasePath,
raw_ostream &Out) override;
void mangleTypeName(QualType T, raw_ostream &,
bool NormalizeIntegers) override;
void mangleCanonicalTypeName(QualType T, raw_ostream &,
bool NormalizeIntegers) override;
void mangleReferenceTemporary(const VarDecl *, unsigned ManglingNumber,
raw_ostream &) override;
void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &Out) override;
Expand Down Expand Up @@ -3842,13 +3842,13 @@ void MicrosoftMangleContextImpl::mangleSEHFinallyBlock(
Mangler.mangleName(EnclosingDecl);
}

void MicrosoftMangleContextImpl::mangleTypeName(
void MicrosoftMangleContextImpl::mangleCanonicalTypeName(
QualType T, raw_ostream &Out, bool NormalizeIntegers = false) {
// This is just a made up unique string for the purposes of tbaa. undname
// does *not* know how to demangle it.
MicrosoftCXXNameMangler Mangler(*this, Out);
Mangler.getStream() << '?';
Mangler.mangleType(T, SourceRange());
Mangler.mangleType(T.getCanonicalType(), SourceRange());
}

void MicrosoftMangleContextImpl::mangleReferenceTemporary(
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGBlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ static std::string getBlockCaptureStr(const CGBlockInfo::Capture &Cap,
Str += "c";
SmallString<256> TyStr;
llvm::raw_svector_ostream Out(TyStr);
CGM.getCXXABI().getMangleContext().mangleTypeName(CaptureTy, Out);
CGM.getCXXABI().getMangleContext().mangleCanonicalTypeName(CaptureTy, Out);
Str += llvm::to_string(TyStr.size()) + TyStr.c_str();
break;
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9069,7 +9069,7 @@ void CGOpenMPRuntime::emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
SmallString<64> TyStr;
llvm::raw_svector_ostream Out(TyStr);
CGM.getCXXABI().getMangleContext().mangleTypeName(Ty, Out);
CGM.getCXXABI().getMangleContext().mangleCanonicalTypeName(Ty, Out);
std::string Name = getName({"omp_mapper", TyStr, D->getName()});
auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
Name, &CGM.getModule());
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/CodeGen/CGVTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,20 +1316,21 @@ void CodeGenModule::EmitVTableTypeMetadata(const CXXRecordDecl *RD,
AP.second.AddressPointIndex));

// Sort the address points for determinism.
// FIXME: It's more efficient to mangle the types before sorting.
llvm::sort(AddressPoints, [this](const AddressPoint &AP1,
const AddressPoint &AP2) {
if (&AP1 == &AP2)
return false;

std::string S1;
llvm::raw_string_ostream O1(S1);
getCXXABI().getMangleContext().mangleTypeName(
getCXXABI().getMangleContext().mangleCanonicalTypeName(
QualType(AP1.first->getTypeForDecl(), 0), O1);
O1.flush();

std::string S2;
llvm::raw_string_ostream O2(S2);
getCXXABI().getMangleContext().mangleTypeName(
getCXXABI().getMangleContext().mangleCanonicalTypeName(
QualType(AP2.first->getTypeForDecl(), 0), O2);
O2.flush();

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ CodeGenFunction::getUBSanFunctionTypeHash(QualType Ty) const {
Ty = getContext().getFunctionTypeWithExceptionSpec(Ty, EST_None);
std::string Mangled;
llvm::raw_string_ostream Out(Mangled);
CGM.getCXXABI().getMangleContext().mangleTypeName(Ty, Out, false);
CGM.getCXXABI().getMangleContext().mangleCanonicalTypeName(Ty, Out, false);
return llvm::ConstantInt::get(
CGM.Int32Ty, static_cast<uint32_t>(llvm::xxh3_64bits(Mangled)));
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,7 @@ llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) {

std::string OutName;
llvm::raw_string_ostream Out(OutName);
getCXXABI().getMangleContext().mangleTypeName(
getCXXABI().getMangleContext().mangleCanonicalTypeName(
T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers);

if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
Expand Down Expand Up @@ -7203,7 +7203,7 @@ CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
if (isExternallyVisible(T->getLinkage())) {
std::string OutName;
llvm::raw_string_ostream Out(OutName);
getCXXABI().getMangleContext().mangleTypeName(
getCXXABI().getMangleContext().mangleCanonicalTypeName(
T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers);

if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CodeGenTBAA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {

SmallString<256> OutName;
llvm::raw_svector_ostream Out(OutName);
MContext.mangleTypeName(QualType(ETy, 0), Out);
MContext.mangleCanonicalTypeName(QualType(ETy, 0), Out);
return createScalarTypeNode(OutName, getChar(), Size);
}

Expand Down Expand Up @@ -391,7 +391,7 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
if (Features.CPlusPlus) {
// Don't use the mangler for C code.
llvm::raw_svector_ostream Out(OutName);
MContext.mangleTypeName(QualType(Ty, 0), Out);
MContext.mangleCanonicalTypeName(QualType(Ty, 0), Out);
} else {
OutName = RD->getName();
}
Expand Down
3 changes: 1 addition & 2 deletions clang/test/CodeGen/ubsan-function-sugared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ void caller() {
}

// GNU: ![[FUNCSAN]] = !{i32 -1056584962, i32 905068220}
// FIXME: Wrong hash
// MSVC: ![[FUNCSAN]] = !{i32 -1056584962, i32 165986058}
// MSVC: ![[FUNCSAN]] = !{i32 -1056584962, i32 -1600339357}
4 changes: 2 additions & 2 deletions clang/unittests/AST/DeclTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ TEST(Decl, MangleDependentSizedArray) {
std::unique_ptr<ItaniumMangleContext> MC(
ItaniumMangleContext::create(Ctx, Diags));

MC->mangleTypeName(DeclA->getType(), OS_A);
MC->mangleTypeName(DeclB->getType(), OS_B);
MC->mangleCanonicalTypeName(DeclA->getType(), OS_A);
MC->mangleCanonicalTypeName(DeclB->getType(), OS_B);

ASSERT_TRUE(0 == MangleA.compare("_ZTSA_i"));
ASSERT_TRUE(0 == MangleB.compare("_ZTSAT0__T_"));
Expand Down

0 comments on commit 9c89b29

Please sign in to comment.