summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Majnemer <david.majnemer@gmail.com>2014-07-18 19:53:17 +0000
committerDavid Majnemer <david.majnemer@gmail.com>2014-07-18 19:53:17 +0000
commit4fbb1b9f98616d625c2fc1d2081cfa64ca8d5b87 (patch)
tree895f570f7ebb383604bf9ef810ca17a255e5857c
parentf8a767df67f30054ad2427feb61d5cd8f5090511 (diff)
downloadbcm5719-llvm-4fbb1b9f98616d625c2fc1d2081cfa64ca8d5b87.tar.gz
bcm5719-llvm-4fbb1b9f98616d625c2fc1d2081cfa64ca8d5b87.zip
CodeGen: Properly null-check typeid expressions
Summary: Thoroughly check for a pointer dereference which yields a glvalue. Look through casts, comma operators, conditional operators, paren expressions, etc. Reviewers: rsmith Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D4416 llvm-svn: 213401
-rw-r--r--clang/lib/CodeGen/CGExprCXX.cpp31
-rw-r--r--clang/test/CodeGenCXX/typeid-should-throw.cpp44
2 files changed, 69 insertions, 6 deletions
diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp
index 47dc9fbbc73..f09c279d0c9 100644
--- a/clang/lib/CodeGen/CGExprCXX.cpp
+++ b/clang/lib/CodeGen/CGExprCXX.cpp
@@ -1615,6 +1615,29 @@ void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
EmitBlock(DeleteEnd);
}
+static bool isGLValueFromPointerDeref(const Expr *E) {
+ E = E->IgnoreParenCasts();
+
+ if (const auto *UO = dyn_cast<UnaryOperator>(E))
+ if (UO->getOpcode() == UO_Deref)
+ return true;
+
+ if (const auto *BO = dyn_cast<BinaryOperator>(E))
+ if (BO->getOpcode() == BO_Comma)
+ return isGLValueFromPointerDeref(BO->getRHS());
+
+ if (const auto *CO = dyn_cast<ConditionalOperator>(E))
+ return isGLValueFromPointerDeref(CO->getTrueExpr()) ||
+ isGLValueFromPointerDeref(CO->getFalseExpr());
+
+ if (const auto *BCO = dyn_cast<BinaryConditionalOperator>(E))
+ if (const auto *OVE = dyn_cast<OpaqueValueExpr>(BCO->getTrueExpr()))
+ return isGLValueFromPointerDeref(OVE->getSourceExpr()) ||
+ isGLValueFromPointerDeref(BCO->getFalseExpr());
+
+ return false;
+}
+
static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E,
llvm::Type *StdTypeInfoPtrTy) {
// Get the vtable pointer.
@@ -1624,13 +1647,9 @@ static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E,
// If the glvalue expression is obtained by applying the unary * operator to
// a pointer and the pointer is a null pointer value, the typeid expression
// throws the std::bad_typeid exception.
- bool IsDeref = false;
- if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens()))
- if (UO->getOpcode() == UO_Deref)
- IsDeref = true;
-
QualType SrcRecordTy = E->getType();
- if (CGF.CGM.getCXXABI().shouldTypeidBeNullChecked(IsDeref, SrcRecordTy)) {
+ if (CGF.CGM.getCXXABI().shouldTypeidBeNullChecked(
+ isGLValueFromPointerDeref(E), SrcRecordTy)) {
llvm::BasicBlock *BadTypeidBlock =
CGF.createBasicBlock("typeid.bad_typeid");
llvm::BasicBlock *EndBlock = CGF.createBasicBlock("typeid.end");
diff --git a/clang/test/CodeGenCXX/typeid-should-throw.cpp b/clang/test/CodeGenCXX/typeid-should-throw.cpp
new file mode 100644
index 00000000000..e3cbea8c6fa
--- /dev/null
+++ b/clang/test/CodeGenCXX/typeid-should-throw.cpp
@@ -0,0 +1,44 @@
+// RUN: %clang_cc1 %s -triple %itanium_abi_triple -Wno-unused-value -emit-llvm -o - | FileCheck %s
+namespace std {
+struct type_info;
+}
+
+struct A {
+ virtual ~A();
+};
+struct B : A {};
+
+void f1(A *x) { typeid(false, *x); }
+// CHECK-LABEL: define void @_Z2f1P1A
+// CHECK: icmp eq {{.*}}, null
+// CHECK-NEXT: br i1
+
+void f2(bool b, A *x, A *y) { typeid(b ? *x : *y); }
+// CHECK-LABEL: define void @_Z2f2bP1AS0_
+// CHECK: icmp eq {{.*}}, null
+// CHECK-NEXT: br i1
+
+void f3(bool b, A *x, A &y) { typeid(b ? *x : y); }
+// CHECK-LABEL: define void @_Z2f3bP1ARS_
+// CHECK: icmp eq {{.*}}, null
+// CHECK-NEXT: br i1
+
+void f4(bool b, A &x, A *y) { typeid(b ? x : *y); }
+// CHECK-LABEL: define void @_Z2f4bR1APS_
+// CHECK: icmp eq {{.*}}, null
+// CHECK-NEXT: br i1
+
+void f5(volatile A *x) { typeid(*x); }
+// CHECK-LABEL: define void @_Z2f5PV1A
+// CHECK: icmp eq {{.*}}, null
+// CHECK-NEXT: br i1
+
+void f6(A *x) { typeid((B &)*(B *)x); }
+// CHECK-LABEL: define void @_Z2f6P1A
+// CHECK: icmp eq {{.*}}, null
+// CHECK-NEXT: br i1
+
+void f7(A *x) { typeid((*x)); }
+// CHECK-LABEL: define void @_Z2f7P1A
+// CHECK: icmp eq {{.*}}, null
+// CHECK-NEXT: br i1
OpenPOWER on IntegriCloud