Skip to content

Commit 3f3aad2

Browse files
committedAug 4, 2017
Reland "CFI: blacklist STL allocate() from unrelated-casts"
Reland r310097 with a fix for a debug assertion in NamedDecl.getName() Differential Revision: https://reviews.llvm.org/D36294 llvm-svn: 310132
1 parent 6b7db9a commit 3f3aad2

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
 

Diff for: ‎clang/lib/CodeGen/CodeGenFunction.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,26 @@ static void markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn) {
723723
Fn->removeFnAttr(llvm::Attribute::SanitizeThread);
724724
}
725725

726+
static bool matchesStlAllocatorFn(const Decl *D, const ASTContext &Ctx) {
727+
auto *MD = dyn_cast_or_null<CXXMethodDecl>(D);
728+
if (!MD || !MD->getDeclName().getAsIdentifierInfo() ||
729+
!MD->getDeclName().getAsIdentifierInfo()->isStr("allocate") ||
730+
(MD->getNumParams() != 1 && MD->getNumParams() != 2))
731+
return false;
732+
733+
if (MD->parameters()[0]->getType().getCanonicalType() != Ctx.getSizeType())
734+
return false;
735+
736+
if (MD->getNumParams() == 2) {
737+
auto *PT = MD->parameters()[1]->getType()->getAs<PointerType>();
738+
if (!PT || !PT->isVoidPointerType() ||
739+
!PT->getPointeeType().isConstQualified())
740+
return false;
741+
}
742+
743+
return true;
744+
}
745+
726746
void CodeGenFunction::StartFunction(GlobalDecl GD,
727747
QualType RetTy,
728748
llvm::Function *Fn,
@@ -782,6 +802,14 @@ void CodeGenFunction::StartFunction(GlobalDecl GD,
782802
}
783803
}
784804

805+
// Ignore unrelated casts in STL allocate() since the allocator must cast
806+
// from void* to T* before object initialization completes. Don't match on the
807+
// namespace because not all allocators are in std::
808+
if (D && SanOpts.has(SanitizerKind::CFIUnrelatedCast)) {
809+
if (matchesStlAllocatorFn(D, getContext()))
810+
SanOpts.Mask &= ~SanitizerKind::CFIUnrelatedCast;
811+
}
812+
785813
// Apply xray attributes to the function (as a string, for now)
786814
if (D && ShouldXRayInstrumentFunction()) {
787815
if (const auto *XRayAttr = D->getAttr<XRayInstrumentAttr>()) {

Diff for: ‎clang/test/CodeGen/cfi-unrelated-cast.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// STL allocators should not have unrelated-cast tests applied
2+
// RUN: %clang_cc1 -flto -triple x86_64-unknown-linux -fvisibility hidden -fsanitize=cfi-unrelated-cast -emit-llvm -o - %s | FileCheck %s
3+
4+
#include <stddef.h>
5+
6+
template<class T>
7+
class myalloc {
8+
public:
9+
// CHECK: define{{.*}}allocateE{{.}}
10+
// CHECK-NOT: llvm.type.test
11+
T *allocate(size_t sz) {
12+
return (T*)::operator new(sz);
13+
}
14+
15+
// CHECK: define{{.*}}allocateE{{.}}PKv
16+
// CHECK-NOT: llvm.type.test
17+
T *allocate(size_t sz, const void *ptr) {
18+
return (T*)::operator new(sz);
19+
}
20+
21+
// CHECK: define{{.*}}differentName
22+
// CHECK: llvm.type.test
23+
T *differentName(size_t sz, const void *ptr) {
24+
return (T*)::operator new(sz);
25+
}
26+
};
27+
28+
class C1 {
29+
virtual void f() {}
30+
};
31+
32+
C1 *f1() {
33+
myalloc<C1> allocator;
34+
(void)allocator.allocate(16);
35+
(void)allocator.allocate(16, 0);
36+
(void)allocator.differentName(16, 0);
37+
}

0 commit comments

Comments
 (0)
Please sign in to comment.