r177698 - Warn about attempts to reinterpret_cast between two types that are

Richard Smith richard at metafoo.co.uk
Mon Mar 25 17:47:40 PDT 2013


Hi John,

On Thu, Mar 21, 2013 at 7:58 PM, John McCall <rjmccall at apple.com> wrote:

> Author: rjmccall
> Date: Thu Mar 21 21:58:14 2013
> New Revision: 177698
>
> URL: http://llvm.org/viewvc/llvm-project?rev=177698&view=rev
> Log:
> Warn about attempts to reinterpret_cast between two types that are
> hierarchy-related at a possibly nonzero offset.
>
> Patch by Alexander Zinenko!
>
> Added:
>     cfe/trunk/test/SemaCXX/warn-reinterpret-base-class.cpp
> Modified:
>     cfe/trunk/include/clang/Basic/DiagnosticGroups.td
>     cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
>     cfe/trunk/lib/Sema/SemaCast.cpp
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=177698&r1=177697&r2=177698&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Thu Mar 21 21:58:14
> 2013
> @@ -266,6 +266,7 @@ def Trigraphs      : DiagGroup<"trigraph
>
>  def : DiagGroup<"type-limits">;
>  def UndefinedReinterpretCast : DiagGroup<"undefined-reinterpret-cast">;
> +def ReinterpretBaseClass : DiagGroup<"reinterpret-base-class">;
>  def Unicode  : DiagGroup<"unicode">;
>  def UninitializedMaybe : DiagGroup<"conditional-uninitialized">;
>  def UninitializedSometimes : DiagGroup<"sometimes-uninitialized">;
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=177698&r1=177697&r2=177698&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Mar 21
> 21:58:14 2013
> @@ -4491,6 +4491,14 @@ def note_parameter_here : Note<
>  def err_bad_reinterpret_cast_overload : Error<
>    "reinterpret_cast cannot resolve overloaded function %0 to type %1">;
>
> +def warn_reinterpret_different_from_static : Warning<
> +  "'reinterpret_cast' %select{from|to}3 class %0 %select{to|from}3 its "
> +  "%select{virtual base|base at non-zero offset}2 %1 behaves differently
> from "
> +  "'static_cast'">, InGroup<ReinterpretBaseClass>;
> +def note_reinterpret_updowncast_use_static: Note<
> +  "use 'static_cast' to adjust the pointer correctly while "
> +  "%select{upcasting|downcasting}0">;
> +
>  def err_bad_static_cast_overload : Error<
>    "address of overloaded function %0 cannot be static_cast to type %1">;
>
>
> Modified: cfe/trunk/lib/Sema/SemaCast.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCast.cpp?rev=177698&r1=177697&r2=177698&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaCast.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaCast.cpp Thu Mar 21 21:58:14 2013
> @@ -19,6 +19,7 @@
>  #include "clang/AST/CXXInheritance.h"
>  #include "clang/AST/ExprCXX.h"
>  #include "clang/AST/ExprObjC.h"
> +#include "clang/AST/RecordLayout.h"
>  #include "clang/Basic/PartialDiagnostic.h"
>  #include "clang/Sema/Initialization.h"
>  #include "llvm/ADT/SmallVector.h"
> @@ -682,6 +683,88 @@ void CastOperation::CheckConstCast() {
>        << SrcExpr.get()->getType() << DestType << OpRange;
>  }
>
> +/// Check that a reinterpret_cast\<DestType\>(SrcExpr) is not used as
> upcast
> +/// or downcast between respective pointers or references.
> +static void DiagnoseReinterpretUpDownCast(Sema &Self, const Expr *SrcExpr,
> +                                          QualType DestType,
> +                                          SourceRange OpRange) {
> +  QualType SrcType = SrcExpr->getType();
> +  // When casting from pointer or reference, get pointee type; use
> original
> +  // type otherwise.
> +  const CXXRecordDecl *SrcPointeeRD = SrcType->getPointeeCXXRecordDecl();
> +  const CXXRecordDecl *SrcRD =
> +    SrcPointeeRD ? SrcPointeeRD : SrcType->getAsCXXRecordDecl();
> +
> +  // Examining subobjects for records is only possible if the complete
> +  // definition is available.  Also, template instantiation is not
> allowed here.
> +  if(!SrcRD || !SrcRD->isCompleteDefinition())
> +    return;
> +
> +  const CXXRecordDecl *DestRD = DestType->getPointeeCXXRecordDecl();
> +
> +  if(!DestRD || !DestRD->isCompleteDefinition())
> +    return;
> +
> +  enum {
> +    ReinterpretUpcast,
> +    ReinterpretDowncast
> +  } ReinterpretKind;
> +
> +  CXXBasePaths BasePaths;
> +
> +  if (SrcRD->isDerivedFrom(DestRD, BasePaths))
> +    ReinterpretKind = ReinterpretUpcast;
> +  else if (DestRD->isDerivedFrom(SrcRD, BasePaths))
> +    ReinterpretKind = ReinterpretDowncast;
> +  else
> +    return;
> +
> +  bool VirtualBase = true;
> +  bool NonZeroOffset = false;
> +  for (CXXBasePaths::const_paths_iterator I = BasePaths.begin(),
> +                                          E = BasePaths.end();
> +       I != E; ++I) {
> +    const CXXBasePath &Path = *I;
> +    CharUnits Offset = CharUnits::Zero();
> +    bool IsVirtual = false;
> +    for (CXXBasePath::const_iterator IElem = Path.begin(), EElem =
> Path.end();
> +         IElem != EElem; ++IElem) {
> +      IsVirtual = IElem->Base->isVirtual();
> +      if (IsVirtual)
> +        break;
> +      const CXXRecordDecl *BaseRD =
> IElem->Base->getType()->getAsCXXRecordDecl();
> +      assert(BaseRD && "Base type should be a valid unqualified class
> type");
> +      const ASTRecordLayout &DerivedLayout =
> +          Self.Context.getASTRecordLayout(IElem->Class);
>

We have a crash-on-invalid here. Testcase:

struct A;
struct B { A a; };
struct C : B {} c;
B *p = reinterpret_cast<B*>(&c);


> +      Offset += DerivedLayout.getBaseClassOffset(BaseRD);
> +    }
> +    if (!IsVirtual) {
> +      // Don't warn if any path is a non-virtually derived base at offset
> zero.
> +      if (Offset.isZero())
> +        return;
> +      // Offset makes sense only for non-virtual bases.
> +      else
> +        NonZeroOffset = true;
> +    }
> +    VirtualBase = VirtualBase && IsVirtual;
> +  }
> +
> +  assert((VirtualBase || NonZeroOffset) &&
> +         "Should have returned if has non-virtual base with zero offset");
> +
> +  QualType BaseType =
> +      ReinterpretKind == ReinterpretUpcast? DestType : SrcType;
> +  QualType DerivedType =
> +      ReinterpretKind == ReinterpretUpcast? SrcType : DestType;
> +
> +  Self.Diag(OpRange.getBegin(),
> diag::warn_reinterpret_different_from_static)
> +    << DerivedType << BaseType << !VirtualBase << ReinterpretKind;
> +  Self.Diag(OpRange.getBegin(),
> diag::note_reinterpret_updowncast_use_static)
> +    << ReinterpretKind;
> +
> +  // TODO: emit fixits. This requires passing operator SourceRange from
> Parser.
> +}
> +
>  /// CheckReinterpretCast - Check that a
> reinterpret_cast\<DestType\>(SrcExpr) is
>  /// valid.
>  /// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used
> in code
> @@ -714,8 +797,10 @@ void CastOperation::CheckReinterpretCast
>        diagnoseBadCast(Self, msg, CT_Reinterpret, OpRange, SrcExpr.get(),
>                        DestType, /*listInitialization=*/false);
>      }
> -  } else if (tcr == TC_Success && Self.getLangOpts().ObjCAutoRefCount) {
> -    checkObjCARCConversion(Sema::CCK_OtherCast);
> +  } else if (tcr == TC_Success) {
> +    if (Self.getLangOpts().ObjCAutoRefCount)
> +      checkObjCARCConversion(Sema::CCK_OtherCast);
> +    DiagnoseReinterpretUpDownCast(Self, SrcExpr.get(), DestType, OpRange);
>    }
>  }
>
>
> Added: cfe/trunk/test/SemaCXX/warn-reinterpret-base-class.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-reinterpret-base-class.cpp?rev=177698&view=auto
>
> ==============================================================================
> --- cfe/trunk/test/SemaCXX/warn-reinterpret-base-class.cpp (added)
> +++ cfe/trunk/test/SemaCXX/warn-reinterpret-base-class.cpp Thu Mar 21
> 21:58:14 2013
> @@ -0,0 +1,234 @@
> +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify
> -Wreinterpret-base-class -Wno-unused-volatile-lvalue %s
> +
> +// PR 13824
> +class A {
> +};
> +class DA : public A {
> +};
> +class DDA : public DA {
> +};
> +class DAo : protected A {
> +};
> +class DAi : private A {
> +};
> +
> +class DVA : public virtual A {
> +};
> +class DDVA : public virtual DA {
> +};
> +class DMA : public virtual A, public virtual DA {
> +};
> +
> +class B;
> +
> +struct C {
> +  // Do not fail on incompletely-defined classes.
> +  decltype(reinterpret_cast<C *>(0)) foo;
> +  decltype(reinterpret_cast<A *>((C *) 0)) bar;
> +  decltype(reinterpret_cast<C *>((A *) 0)) baz;
> +};
> +
> +void reinterpret_not_defined_class(B *b, C *c) {
> +  // Should not fail if class has no definition.
> +  (void)*reinterpret_cast<C *>(b);
> +  (void)*reinterpret_cast<B *>(c);
> +
> +  (void)reinterpret_cast<C &>(*b);
> +  (void)reinterpret_cast<B &>(*c);
> +}
> +
> +void reinterpret_not_updowncast(A *pa, const A *pca, A &a, const A &ca) {
> +  (void)*reinterpret_cast<C *>(pa);
> +  (void)*reinterpret_cast<const C *>(pa);
> +  (void)*reinterpret_cast<volatile C *>(pa);
> +  (void)*reinterpret_cast<const volatile C *>(pa);
> +
> +  (void)*reinterpret_cast<const C *>(pca);
> +  (void)*reinterpret_cast<const volatile C *>(pca);
> +
> +  (void)reinterpret_cast<C &>(a);
> +  (void)reinterpret_cast<const C &>(a);
> +  (void)reinterpret_cast<volatile C &>(a);
> +  (void)reinterpret_cast<const volatile C &>(a);
> +
> +  (void)reinterpret_cast<const C &>(ca);
> +  (void)reinterpret_cast<const volatile C &>(ca);
> +}
> +
> +void reinterpret_pointer_downcast(A *a, const A *ca) {
> +  (void)*reinterpret_cast<DA *>(a);
> +  (void)*reinterpret_cast<const DA *>(a);
> +  (void)*reinterpret_cast<volatile DA *>(a);
> +  (void)*reinterpret_cast<const volatile DA *>(a);
> +
> +  (void)*reinterpret_cast<const DA *>(ca);
> +  (void)*reinterpret_cast<const volatile DA *>(ca);
> +
> +  (void)*reinterpret_cast<DDA *>(a);
> +  (void)*reinterpret_cast<DAo *>(a);
> +  (void)*reinterpret_cast<DAi *>(a);
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'DVA *' from its
> virtual base 'A *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)*reinterpret_cast<DVA *>(a);
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'DDVA *' from its
> virtual base 'A *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)*reinterpret_cast<DDVA *>(a);
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'DMA *' from its
> virtual base 'A *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)*reinterpret_cast<DMA *>(a);
> +}
> +
> +void reinterpret_reference_downcast(A a, A &ra, const A &cra) {
> +  (void)reinterpret_cast<DA &>(a);
> +  (void)reinterpret_cast<const DA &>(a);
> +  (void)reinterpret_cast<volatile DA &>(a);
> +  (void)reinterpret_cast<const volatile DA &>(a);
> +
> +  (void)reinterpret_cast<DA &>(ra);
> +  (void)reinterpret_cast<const DA &>(ra);
> +  (void)reinterpret_cast<volatile DA &>(ra);
> +  (void)reinterpret_cast<const volatile DA &>(ra);
> +
> +  (void)reinterpret_cast<const DA &>(cra);
> +  (void)reinterpret_cast<const volatile DA &>(cra);
> +
> +  (void)reinterpret_cast<DDA &>(a);
> +  (void)reinterpret_cast<DAo &>(a);
> +  (void)reinterpret_cast<DAi &>(a);
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'DVA &' from its
> virtual base 'A' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)reinterpret_cast<DVA &>(a);
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'DDVA &' from its
> virtual base 'A' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)reinterpret_cast<DDVA &>(a);
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'DMA &' from its
> virtual base 'A' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)reinterpret_cast<DMA &>(a);
> +}
> +
> +void reinterpret_pointer_upcast(DA *da, const DA *cda, DDA *dda, DAo *dao,
> +                                DAi *dai, DVA *dva, DDVA *ddva, DMA *dma)
> {
> +  (void)*reinterpret_cast<A *>(da);
> +  (void)*reinterpret_cast<const A *>(da);
> +  (void)*reinterpret_cast<volatile A *>(da);
> +  (void)*reinterpret_cast<const volatile A *>(da);
> +
> +  (void)*reinterpret_cast<const A *>(cda);
> +  (void)*reinterpret_cast<const volatile A *>(cda);
> +
> +  (void)*reinterpret_cast<A *>(dda);
> +  (void)*reinterpret_cast<DA *>(dda);
> +  (void)*reinterpret_cast<A *>(dao);
> +  (void)*reinterpret_cast<A *>(dai);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'DVA *' to its
> virtual base 'A *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)*reinterpret_cast<A *>(dva);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'DDVA *' to its
> virtual base 'A *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)*reinterpret_cast<A *>(ddva);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'DDVA *' to its
> virtual base 'DA *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)*reinterpret_cast<DA *>(ddva);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'DMA *' to its
> virtual base 'A *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)*reinterpret_cast<A *>(dma);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'DMA *' to its
> virtual base 'DA *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)*reinterpret_cast<DA *>(dma);
> +}
> +
> +void reinterpret_reference_upcast(DA &da, const DA &cda, DDA &dda, DAo
> &dao,
> +                                  DAi &dai, DVA &dva, DDVA &ddva, DMA
> &dma) {
> +  (void)reinterpret_cast<A &>(da);
> +  (void)reinterpret_cast<const A &>(da);
> +  (void)reinterpret_cast<volatile A &>(da);
> +  (void)reinterpret_cast<const volatile A &>(da);
> +
> +  (void)reinterpret_cast<const A &>(cda);
> +  (void)reinterpret_cast<const volatile A &>(cda);
> +
> +  (void)reinterpret_cast<A &>(dda);
> +  (void)reinterpret_cast<DA &>(dda);
> +  (void)reinterpret_cast<A &>(dao);
> +  (void)reinterpret_cast<A &>(dai);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'DVA' to its
> virtual base 'A &' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)reinterpret_cast<A &>(dva);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'DDVA' to its
> virtual base 'A &' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)reinterpret_cast<A &>(ddva);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'DDVA' to its
> virtual base 'DA &' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)reinterpret_cast<DA &>(ddva);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'DMA' to its
> virtual base 'A &' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)reinterpret_cast<A &>(dma);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'DMA' to its
> virtual base 'DA &' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)reinterpret_cast<DA &>(dma);
> +}
> +
> +struct E {
> +  int x;
> +};
> +
> +class F : public E {
> +  virtual int foo() { return x; }
> +};
> +
> +class G : public F {
> +};
> +
> +class H : public E, public A {
> +};
> +
> +class I : virtual public F {
> +};
> +
> +typedef const F * K;
> +typedef volatile K L;
> +
> +void different_subobject_downcast(E *e, F *f, A *a) {
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'F *' from its
> base at non-zero offset 'E *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)reinterpret_cast<F *>(e);
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'G *' from its
> base at non-zero offset 'E *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)reinterpret_cast<G *>(e);
> +  (void)reinterpret_cast<H *>(e);
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'I *' from its
> virtual base 'E *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)reinterpret_cast<I *>(e);
> +
> +  (void)reinterpret_cast<G *>(f);
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'I *' from its
> virtual base 'F *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)reinterpret_cast<I *>(f);
> +
> +  (void)reinterpret_cast<H *>(a);
> +
> +  // expected-warning at +2 {{'reinterpret_cast' to class 'L' (aka 'const F
> *volatile') from its base at non-zero offset 'E *' behaves differently from
> 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while downcasting}}
> +  (void)reinterpret_cast<L>(e);
> +}
> +
> +void different_subobject_upcast(F *f, G *g, H *h, I *i) {
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'F *' to its
> base at non-zero offset 'E *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)reinterpret_cast<E *>(f);
> +
> +  (void)reinterpret_cast<F *>(g);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'G *' to its
> base at non-zero offset 'E *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)reinterpret_cast<E *>(g);
> +
> +  (void)reinterpret_cast<E *>(h);
> +  (void)reinterpret_cast<A *>(h);
> +
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'I *' to its
> virtual base 'F *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)reinterpret_cast<F *>(i);
> +  // expected-warning at +2 {{'reinterpret_cast' from class 'I *' to its
> virtual base 'E *' behaves differently from 'static_cast'}}
> +  // expected-note at +1 {{use 'static_cast' to adjust the pointer
> correctly while upcasting}}
> +  (void)reinterpret_cast<E *>(i);
> +}
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130325/477ac3a8/attachment.html>


More information about the cfe-commits mailing list