Skip to content

Commit ca19ab9

Browse files
authored
[NFCI][analyzer] invalidateRegions: require explicit State (llvm#164434)
The method `CallEvent::invalidateRegions()` takes (an unsigned `BlockCount` and) a `ProgramStateRef` which was previously defaulted to `nullptr` -- and when the state argument was `nullptr`, the method used the "inner" state of the `CallEvent` as a starting point for the invalidation. My recent commit 0f6f13b turned the "inner" state of the `CallEvent` into a hidden `protected` implementation detail; so this commit follows that direction by removing the "defaults to the inner state" behavior from `invalidateRegions` to avoid exposing the inner state through this channel. The method `CallEvent::invalidateRegions()` was only called in two locations, only one of those was relying on the existence of the default argument value, and even there it was easy to explicitly pass the `State` object. This commit also eliminates a footgun: with the old logic, if some code had tried to pass a state explicitly (presumably because the "inner" state of the call was obsolete) but that "new" state happened to be `nullptr`, then `invalidateRegions` would have silently used the inner state attached to the call. However, I reviewed the call sites and don't think that it was actually possible to reach this buggy execution path.
1 parent df1d786 commit ca19ab9

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,10 @@ class CallEvent {
372372
ProgramPoint getProgramPoint(bool IsPreVisit = false,
373373
const ProgramPointTag *Tag = nullptr) const;
374374

375-
/// Returns a new state with all argument regions invalidated.
376-
///
377-
/// This accepts an alternate state in case some processing has already
378-
/// occurred.
375+
/// Invalidates the regions (arguments, globals, special regions like 'this')
376+
/// that may have been written by this call, returning the updated state.
379377
ProgramStateRef invalidateRegions(unsigned BlockCount,
380-
ProgramStateRef Orig = nullptr) const;
378+
ProgramStateRef State) const;
381379

382380
using FrameBindingTy = std::pair<SVal, SVal>;
383381
using BindingsTy = SmallVectorImpl<FrameBindingTy>;

clang/lib/StaticAnalyzer/Core/CallEvent.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,11 @@ static void findPtrToConstParams(llvm::SmallSet<unsigned, 4> &PreserveArgs,
230230
}
231231

232232
ProgramStateRef CallEvent::invalidateRegions(unsigned BlockCount,
233-
ProgramStateRef Orig) const {
234-
ProgramStateRef Result = (Orig ? Orig : getState());
235-
233+
ProgramStateRef State) const {
236234
// Don't invalidate anything if the callee is marked pure/const.
237-
if (const Decl *callee = getDecl())
238-
if (callee->hasAttr<PureAttr>() || callee->hasAttr<ConstAttr>())
239-
return Result;
235+
if (const Decl *Callee = getDecl())
236+
if (Callee->hasAttr<PureAttr>() || Callee->hasAttr<ConstAttr>())
237+
return State;
240238

241239
SmallVector<SVal, 8> ValuesToInvalidate;
242240
RegionAndSymbolInvalidationTraits ETraits;
@@ -278,10 +276,10 @@ ProgramStateRef CallEvent::invalidateRegions(unsigned BlockCount,
278276
// Invalidate designated regions using the batch invalidation API.
279277
// NOTE: Even if RegionsToInvalidate is empty, we may still invalidate
280278
// global variables.
281-
return Result->invalidateRegions(ValuesToInvalidate, getCFGElementRef(),
282-
BlockCount, getLocationContext(),
283-
/*CausedByPointerEscape*/ true,
284-
/*Symbols=*/nullptr, this, &ETraits);
279+
return State->invalidateRegions(ValuesToInvalidate, getCFGElementRef(),
280+
BlockCount, getLocationContext(),
281+
/*CausedByPointerEscape*/ true,
282+
/*Symbols=*/nullptr, this, &ETraits);
285283
}
286284

287285
ProgramPoint CallEvent::getProgramPoint(bool IsPreVisit,

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
10131013
// FIXME: Once we figure out how we want allocators to work,
10141014
// we should be using the usual pre-/(default-)eval-/post-call checkers
10151015
// here.
1016-
State = Call->invalidateRegions(blockCount);
1016+
State = Call->invalidateRegions(blockCount, State);
10171017
if (!State)
10181018
return;
10191019

0 commit comments

Comments
 (0)