llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

MemRegion.cpp:1196 - Assertion `isValidBaseClass(RD, cast<TypedValueRegion>(Super), IsVirtual)' failed.

Open vabridgers opened this issue 5 years ago • 8 comments

Bugzilla Link 48739
Version trunk
OS All
CC @devincoughlin,@RedDocMD,@ASDenysPetrov,@haoNoQ

Extended Description

We came across this issue internally while using CSA. This is a reduced reproducer.

command line

clang --analyze test.cpp

cat test.cpp

class b { public: int c; b(int); }; class e : public b {}; int b::*d; void f() { int e::bp = &b::c; b a(1); d = static_cast<int b::>(bp); a.*d; }

Relevant backtrace (slighty edited)

clang: /clang/lib/StaticAnalyzer/Core/MemRegion.cpp:1196: const clang::ento::CXXBaseObjectRegion* clang::ento::MemRegionManager::getCXXBaseObjectRegion(const clang::CXXRecordDecl*, const clang::ento::SubRegion*, bool): Assertion `isValidBaseClass(RD, cast(Super), IsVirtual)' failed. PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script. Stack dump: 0. Program arguments: clang --analyze check.cpp

  1. parser at end of file
  2. While analyzing stack: #0 Calling f
  3. check.cpp:12:3: Error evaluating statement
  4. check.cpp:12:3: Error evaluating statement

... #​11 0x0000000006bd4f03 clang::ento::MemRegionManager::getCXXBaseObjectRegion(clang::CXXRecordDecl const*, clang::ento::SubRegion const*, bool) /clang/lib/StaticAnalyzer/Core/MemRegion.cpp:1199:5 #​12 0x0000000006c5c2a4 clang::ento::StoreManager::evalDerivedToBase(clang::ento::SVal, clang::QualType, bool) /clang/lib/StaticAnalyzer/Core/Store.cpp:295:58 #​13 0x0000000006c4e822 clang::ento::SVal (anonymous namespace)::SimpleSValBuilder::evalBinOpLN(llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>, clang::BinaryOperatorKind, clang::ento::Loc, clang::ento::NonLoc, clang::QualType)::'lambda'(auto const*)::operator()clang::FieldDecl(auto const*) const /clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp:1120:64 #​14 0x0000000006c4d275 (anonymous namespace)::SimpleSValBuilder::evalBinOpLN(llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>, clang::BinaryOperatorKind, clang::ento::Loc, clang::ento::NonLoc, clang::QualType) /clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp:1127:33 #​15 0x0000000006c5f364 clang::ento::SValBuilder::evalBinOp(llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>, clang::BinaryOperatorKind, clang::ento::SVal, clang::ento::SVal, clang::QualType) /clang/lib/StaticAnalyzer/Core/SValBuilder.cpp:430:66 #​16 0x0000000006b8bba9 clang::ento::ExprEngine::evalBinOp(llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>, clang::BinaryOperatorKind, clang::ento::SVal, clang::ento::SVal, clang::QualType) /clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h:631:53 #​17 0x0000000006b9e85a clang::ento::ExprEngine::VisitBinaryOperator(clang::BinaryOperator const*, clang::ento::ExplodedNode*, clang::ento::ExplodedNodeSet&) /clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp:100:69 #​18 0x0000000006b7c073 clang::ento::ExprEngine::Visit(clang::Stmt const*, clang::ento::ExplodedNode*, clang::ento::ExplodedNodeSet&) /clang/lib/StaticAnalyzer/Core/ExprEngine.cpp:1578:20 #​19 0x0000000006b78376 clang::ento::ExprEngine::ProcessStmt(clang::Stmt const*, clang::ento::ExplodedNode*) /clang/lib/StaticAnalyzer/Core/ExprEngine.cpp:792:15 #​20 0x0000000006b776f8 clang::ento::ExprEngine::processCFGElement(clang::CFGElement, clang::ento::ExplodedNode*, unsigned int, clang::ento::NodeBuilderContext*) /clang/lib/StaticAnalyzer/Core/ExprEngine.cpp:637:18 #​21 0x0000000006b49ed4 clang::ento::CoreEngine::HandlePostStmt(clang::CFGBlock const*, unsigned int, clang::ento::ExplodedNode*) /clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:468:1 #​22 0x0000000006b48bd3 clang::ento::CoreEngine::dispatchWorkItem(clang::ento::ExplodedNode*, clang::ProgramPoint, clang::ento::WorkListUnit const&) (.localalias) /clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:192:7 #​23 0x0000000006b4886d clang::ento::CoreEngine::ExecuteWorkList(clang::LocationContext const*, unsigned int, llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>) /clang/lib/StaticAnalyzer/Core/CoreEngine.cpp:147:21

vabridgers avatar Jan 13 '21 15:01 vabridgers

Updated. https://reviews.llvm.org/D95307 has been abandoned.

https://reviews.llvm.org/D95877 is a new fix.

ghost avatar Feb 05 '21 16:02 ghost

Patch submitted at https://reviews.llvm.org/D95307

RedDocMD avatar Jan 24 '21 10:01 RedDocMD

On looking into the stack trace, in SimpleSValBuilder::evalBinOpLN at SimpleSValBuilder.cpp:1108, the following loop is executed:

SVal Result = lhs;
for (const auto &I : *PTMSV)
  Result = StateMgr.getStoreManager()
                   .evalDerivedToBase(Result, I->getType(), I->isVirtual());

So for the failing code, lhs is base of type Base, and PTMSV is base_field (going by the cleaned-up code from @​Artem Dergachev).

As far as I can tell, the intent of the loop is to apply the static-casts that may have been applied to a pointer-to-member, by moving up the hierarchy for both rhs and lhs. However, as is seen in this example, this logic fails since lhs has type Base and doesn't need to be moved up the hierarchy but rhs has a static_cast.

In fact, I am not entirely sure what purpose this loop serves. The rhs can have as many layered static_casts but the lhs will always have to contain the exact type (like derived_field cannot be applied to base).

Removing the loop of course solves this problem but is that the right solution. @​Artem Dergachev, your thoughts and comments?

RedDocMD avatar Jan 17 '21 10:01 RedDocMD

Interesting, basically we're crashing in pointer-to-member application. It's likely that some of these operations weren't implemented carefully yet in the static analyzer, such as that static cast in the middle (which may change the underlying runtime "offset" "value" of the ptm, even though in this case it doesn't).

@​Deep Majumder, sure, please free to take a look! I'll be looking into this too but most likely not immediately.

Slightly cleaned up example:

struct Base {
  int field;
};

struct Derived : Base {};

void f() {
  int Derived::*derived_field = &Base::field;
  Base base;
  int Base::*base_field = static_cast<int Base::*>(derived_field);
  base.*base_field;
}

haoNoQ avatar Jan 14 '21 04:01 haoNoQ

Being able to reproduce the same. I would like to look into this bug, if that's okay with you Devin

RedDocMD avatar Jan 13 '21 18:01 RedDocMD

assigned to @haoNoQ

vabridgers avatar Jan 13 '21 15:01 vabridgers

@danix800: Could you please elaborate?

EugeneZelenko avatar Oct 15 '24 04:10 EugeneZelenko

@danix800: Could you please elaborate?

reviews.llvm.org/D95877 is merged and the testcase is not reproducible anymore.

danix800 avatar Oct 16 '24 01:10 danix800