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
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?
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;
}