Skip to content

[analyzer] Fix assertion failure in CXXInstanceCall::getCXXThisVal #70837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions clang/lib/StaticAnalyzer/Core/CallEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,14 @@ void CXXInstanceCall::getExtraInvalidatedValues(
SVal CXXInstanceCall::getCXXThisVal() const {
const Expr *Base = getCXXThisExpr();
// FIXME: This doesn't handle an overloaded ->* operator.
if (!Base)
return UnknownVal();
SVal ThisVal = Base ? getSVal(Base) : UnknownVal();

if (isa<NonLoc>(ThisVal)) {
SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
QualType OriginalTy = ThisVal.getType(SVB.getContext());
return SVB.evalCast(ThisVal, Base->getType(), OriginalTy);
}

SVal ThisVal = getSVal(Base);
assert(ThisVal.isUnknownOrUndef() || isa<Loc>(ThisVal));
return ThisVal;
}
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,11 @@ class EvalCastVisitor : public SValVisitor<EvalCastVisitor, SVal> {
return VB.makeNonLoc(SE, T, CastTy);
}

// FIXME: We should be able to cast NonLoc -> Loc
// (when Loc::isLocType(CastTy) is true)
// But it's hard to do as SymbolicRegions can't refer to SymbolCasts holding
// generic SymExprs. Check the commit message for the details.

// Symbol to pointer and whatever else.
return UnknownVal();
}
Expand Down
21 changes: 21 additions & 0 deletions clang/test/Analysis/builtin_bitcast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// RUN: -analyzer-checker=core,debug.ExprInspection

template <typename T> void clang_analyzer_dump(T);
using size_t = decltype(sizeof(int));

__attribute__((always_inline)) static inline constexpr unsigned int _castf32_u32(float __A) {
return __builtin_bit_cast(unsigned int, __A); // no-warning
Expand Down Expand Up @@ -30,3 +31,23 @@ void test(int i) {
clang_analyzer_dump(g4);
// expected-warning@-1 {{&i [as 64 bit integer]}}
}

struct A {
int n;
void set(int x) {
n = x;
}
};
void gh_69922(size_t p) {
// expected-warning-re@+1 {{(reg_${{[0-9]+}}<size_t p>) & 1U}}
clang_analyzer_dump(__builtin_bit_cast(A*, p & 1));

__builtin_bit_cast(A*, p & 1)->set(2); // no-crash
// However, since the `this` pointer is expected to be a Loc, but we have
// NonLoc there, we simply give up and resolve it as `Unknown`.
// Then, inside the `set()` member function call we can't evaluate the
// store to the member variable `n`.

clang_analyzer_dump(__builtin_bit_cast(A*, p & 1)->n); // Ideally, this should print "2".
// expected-warning-re@-1 {{(reg_${{[0-9]+}}<size_t p>) & 1U}}
}