Skip to content

Commit

Permalink
[CodeGen] Provide source locations for UBSan type checks when emittin…
Browse files Browse the repository at this point in the history
…g constructor calls.

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@335445 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
igorkudrin committed Jun 25, 2018
1 parent d80dd82 commit 6463d22
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
15 changes: 8 additions & 7 deletions lib/CodeGen/CGClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
/*ParamsToSkip*/ 0, Order);

EmitCXXConstructorCall(D, Type, ForVirtualBase, Delegating, This, Args,
Overlap);
Overlap, E->getExprLoc());
}

static bool canEmitDelegateCallArgs(CodeGenFunction &CGF,
Expand Down Expand Up @@ -2064,14 +2064,14 @@ void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
bool Delegating,
Address This,
CallArgList &Args,
AggValueSlot::Overlap_t Overlap) {
AggValueSlot::Overlap_t Overlap,
SourceLocation Loc) {
const CXXRecordDecl *ClassDecl = D->getParent();

// C++11 [class.mfct.non-static]p2:
// If a non-static member function of a class X is called for an object that
// is not of type X, or of a type derived from X, the behavior is undefined.
// FIXME: Provide a source location here.
EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, SourceLocation(),
EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, Loc,
This.getPointer(), getContext().getRecordType(ClassDecl));

if (D->isTrivial() && D->isDefaultConstructor()) {
Expand Down Expand Up @@ -2180,7 +2180,8 @@ void CodeGenFunction::EmitInheritedCXXConstructorCall(
}

EmitCXXConstructorCall(D, Ctor_Base, ForVirtualBase, /*Delegating*/false,
This, Args, AggValueSlot::MayOverlap);
This, Args, AggValueSlot::MayOverlap,
E->getLocation());
}

void CodeGenFunction::EmitInlinedInheritingCXXConstructorCall(
Expand Down Expand Up @@ -2277,7 +2278,7 @@ CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
/*ParamsToSkip*/ 1);

EmitCXXConstructorCall(D, Ctor_Complete, false, false, This, Args,
AggValueSlot::MayOverlap);
AggValueSlot::MayOverlap, E->getExprLoc());
}

void
Expand Down Expand Up @@ -2313,7 +2314,7 @@ CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,

EmitCXXConstructorCall(Ctor, CtorType, /*ForVirtualBase=*/false,
/*Delegating=*/true, This, DelegateArgs,
AggValueSlot::MayOverlap);
AggValueSlot::MayOverlap, Loc);
}

namespace {
Expand Down
3 changes: 2 additions & 1 deletion lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2362,7 +2362,8 @@ class CodeGenFunction : public CodeGenTypeCache {
void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
bool ForVirtualBase, bool Delegating,
Address This, CallArgList &Args,
AggValueSlot::Overlap_t Overlap);
AggValueSlot::Overlap_t Overlap,
SourceLocation Loc);

/// Emit assumption load for all bases. Requires to be be called only on
/// most-derived class and not under construction of the object.
Expand Down
26 changes: 26 additions & 0 deletions test/CodeGenCXX/ubsan-ctor-srcloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-linux -emit-llvm -fsanitize=alignment -fblocks %s -o %t.ll
// RUN: FileCheck -check-prefix=ZEROINIT < %t.ll %s
// RUN: FileCheck -check-prefix=SRCLOC < %t.ll %s
// ZEROINIT-NOT: @{{.+}} = private unnamed_addr global {{.+}} zeroinitializer

struct A {
A(int);
int k;
};

struct B : A {
B();
B(const B &);
// SRCLOC-DAG: @{{.+}} = private unnamed_addr global {{.+}} @.src, i32 [[@LINE+1]], i32 12 }
using A::A;
void f() const;
};

// SRCLOC-DAG: @{{.+}} = private unnamed_addr global {{.+}} @.src, i32 [[@LINE+1]], i32 10 }
B::B() : A(1) {}

void foo() {
B b(2);
// SRCLOC-DAG: @{{.+}} = private unnamed_addr global {{.+}} @.src, i32 [[@LINE+1]], i32 5 }
^{b.f();}();
}

0 comments on commit 6463d22

Please sign in to comment.