Skip to content

Commit b1e4656

Browse files
authored
[NFC][analyzer] Make invalidateRegions accept Stmt instead of Expr (#109792)
As was reported [here](#103714 (review)), `invalidateRegions` should accept `Stmt` instead of `Expr`. This conversion is possible, since `Expr` was anyway converted back to `Stmt` later. This refactoring is needed to fix another FP related to use of inline assembly. The fix would be to change `State->bindLoc` to `state->invalidateRegions` inside inline assembly visitor, since `bindLoc` only binds to offset 0, which is not really correct semantics in case of inline assembly.
1 parent 216e1b9 commit b1e4656

File tree

6 files changed

+57
-81
lines changed

6 files changed

+57
-81
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,14 @@ class ProgramState : public llvm::FoldingSetNode {
326326
/// \param ITraits information about special handling for particular regions
327327
/// or symbols.
328328
[[nodiscard]] ProgramStateRef
329-
invalidateRegions(ArrayRef<const MemRegion *> Regions, const Expr *E,
329+
invalidateRegions(ArrayRef<const MemRegion *> Regions, const Stmt *S,
330330
unsigned BlockCount, const LocationContext *LCtx,
331331
bool CausesPointerEscape, InvalidatedSymbols *IS = nullptr,
332332
const CallEvent *Call = nullptr,
333333
RegionAndSymbolInvalidationTraits *ITraits = nullptr) const;
334334

335335
[[nodiscard]] ProgramStateRef
336-
invalidateRegions(ArrayRef<SVal> Values, const Expr *E, unsigned BlockCount,
336+
invalidateRegions(ArrayRef<SVal> Values, const Stmt *S, unsigned BlockCount,
337337
const LocationContext *LCtx, bool CausesPointerEscape,
338338
InvalidatedSymbols *IS = nullptr,
339339
const CallEvent *Call = nullptr,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,9 @@ class SValBuilder {
202202
const Expr *expr,
203203
const LocationContext *LCtx,
204204
unsigned count);
205-
DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
206-
const Expr *expr,
205+
DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, const Stmt *S,
207206
const LocationContext *LCtx,
208-
QualType type,
209-
unsigned count);
207+
QualType type, unsigned count);
210208
DefinedOrUnknownSVal conjureSymbolVal(const Stmt *stmt,
211209
const LocationContext *LCtx,
212210
QualType type,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class StoreManager {
215215
///
216216
/// \param[in] store The initial store.
217217
/// \param[in] Values The values to invalidate.
218-
/// \param[in] E The current statement being evaluated. Used to conjure
218+
/// \param[in] S The current statement being evaluated. Used to conjure
219219
/// symbols to mark the values of invalidated regions.
220220
/// \param[in] Count The current block count. Used to conjure
221221
/// symbols to mark the values of invalidated regions.
@@ -233,7 +233,7 @@ class StoreManager {
233233
/// even if they do not currently have bindings. Pass \c NULL if this
234234
/// information will not be used.
235235
virtual StoreRef invalidateRegions(
236-
Store store, ArrayRef<SVal> Values, const Expr *Ex, unsigned Count,
236+
Store store, ArrayRef<SVal> Values, const Stmt *S, unsigned Count,
237237
const LocationContext *LCtx, const CallEvent *Call,
238238
InvalidatedSymbols &IS, RegionAndSymbolInvalidationTraits &ITraits,
239239
InvalidatedRegions *TopLevelRegions, InvalidatedRegions *Invalidated) = 0;

clang/lib/StaticAnalyzer/Core/ProgramState.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,30 +147,24 @@ ProgramState::bindDefaultZero(SVal loc, const LocationContext *LCtx) const {
147147
typedef ArrayRef<const MemRegion *> RegionList;
148148
typedef ArrayRef<SVal> ValueList;
149149

150-
ProgramStateRef
151-
ProgramState::invalidateRegions(RegionList Regions,
152-
const Expr *E, unsigned Count,
153-
const LocationContext *LCtx,
154-
bool CausedByPointerEscape,
155-
InvalidatedSymbols *IS,
156-
const CallEvent *Call,
157-
RegionAndSymbolInvalidationTraits *ITraits) const {
150+
ProgramStateRef ProgramState::invalidateRegions(
151+
RegionList Regions, const Stmt *S, unsigned Count,
152+
const LocationContext *LCtx, bool CausedByPointerEscape,
153+
InvalidatedSymbols *IS, const CallEvent *Call,
154+
RegionAndSymbolInvalidationTraits *ITraits) const {
158155
SmallVector<SVal, 8> Values;
159156
for (const MemRegion *Reg : Regions)
160157
Values.push_back(loc::MemRegionVal(Reg));
161158

162-
return invalidateRegions(Values, E, Count, LCtx, CausedByPointerEscape, IS,
159+
return invalidateRegions(Values, S, Count, LCtx, CausedByPointerEscape, IS,
163160
Call, ITraits);
164161
}
165162

166-
ProgramStateRef
167-
ProgramState::invalidateRegions(ValueList Values,
168-
const Expr *E, unsigned Count,
169-
const LocationContext *LCtx,
170-
bool CausedByPointerEscape,
171-
InvalidatedSymbols *IS,
172-
const CallEvent *Call,
173-
RegionAndSymbolInvalidationTraits *ITraits) const {
163+
ProgramStateRef ProgramState::invalidateRegions(
164+
ValueList Values, const Stmt *S, unsigned Count,
165+
const LocationContext *LCtx, bool CausedByPointerEscape,
166+
InvalidatedSymbols *IS, const CallEvent *Call,
167+
RegionAndSymbolInvalidationTraits *ITraits) const {
174168

175169
ProgramStateManager &Mgr = getStateManager();
176170
ExprEngine &Eng = Mgr.getOwningEngine();
@@ -186,7 +180,7 @@ ProgramState::invalidateRegions(ValueList Values,
186180
StoreManager::InvalidatedRegions TopLevelInvalidated;
187181
StoreManager::InvalidatedRegions Invalidated;
188182
const StoreRef &NewStore = Mgr.StoreMgr->invalidateRegions(
189-
getStore(), Values, E, Count, LCtx, Call, *IS, *ITraits,
183+
getStore(), Values, S, Count, LCtx, Call, *IS, *ITraits,
190184
&TopLevelInvalidated, &Invalidated);
191185

192186
ProgramStateRef NewState = makeWithStore(NewStore);

clang/lib/StaticAnalyzer/Core/RegionStore.cpp

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -405,19 +405,15 @@ class RegionStoreManager : public StoreManager {
405405
//===-------------------------------------------------------------------===//
406406
// Binding values to regions.
407407
//===-------------------------------------------------------------------===//
408-
RegionBindingsRef invalidateGlobalRegion(MemRegion::Kind K,
409-
const Expr *Ex,
408+
RegionBindingsRef invalidateGlobalRegion(MemRegion::Kind K, const Stmt *S,
410409
unsigned Count,
411410
const LocationContext *LCtx,
412411
RegionBindingsRef B,
413412
InvalidatedRegions *Invalidated);
414413

415-
StoreRef invalidateRegions(Store store,
416-
ArrayRef<SVal> Values,
417-
const Expr *E, unsigned Count,
418-
const LocationContext *LCtx,
419-
const CallEvent *Call,
420-
InvalidatedSymbols &IS,
414+
StoreRef invalidateRegions(Store store, ArrayRef<SVal> Values, const Stmt *S,
415+
unsigned Count, const LocationContext *LCtx,
416+
const CallEvent *Call, InvalidatedSymbols &IS,
421417
RegionAndSymbolInvalidationTraits &ITraits,
422418
InvalidatedRegions *Invalidated,
423419
InvalidatedRegions *InvalidatedTopLevel) override;
@@ -975,26 +971,23 @@ RegionStoreManager::removeSubRegionBindings(RegionBindingsConstRef B,
975971
namespace {
976972
class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
977973
{
978-
const Expr *Ex;
974+
const Stmt *S;
979975
unsigned Count;
980976
const LocationContext *LCtx;
981977
InvalidatedSymbols &IS;
982978
RegionAndSymbolInvalidationTraits &ITraits;
983979
StoreManager::InvalidatedRegions *Regions;
984980
GlobalsFilterKind GlobalsFilter;
985981
public:
986-
InvalidateRegionsWorker(RegionStoreManager &rm,
987-
ProgramStateManager &stateMgr,
988-
RegionBindingsRef b,
989-
const Expr *ex, unsigned count,
990-
const LocationContext *lctx,
991-
InvalidatedSymbols &is,
982+
InvalidateRegionsWorker(RegionStoreManager &rm, ProgramStateManager &stateMgr,
983+
RegionBindingsRef b, const Stmt *S, unsigned count,
984+
const LocationContext *lctx, InvalidatedSymbols &is,
992985
RegionAndSymbolInvalidationTraits &ITraitsIn,
993986
StoreManager::InvalidatedRegions *r,
994987
GlobalsFilterKind GFK)
995-
: ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
996-
Ex(ex), Count(count), LCtx(lctx), IS(is), ITraits(ITraitsIn), Regions(r),
997-
GlobalsFilter(GFK) {}
988+
: ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b), S(S),
989+
Count(count), LCtx(lctx), IS(is), ITraits(ITraitsIn), Regions(r),
990+
GlobalsFilter(GFK) {}
998991

999992
void VisitCluster(const MemRegion *baseR, const ClusterBindings *C);
1000993
void VisitBinding(SVal V);
@@ -1127,7 +1120,7 @@ void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
11271120
// Invalidate the region by setting its default value to
11281121
// conjured symbol. The type of the symbol is irrelevant.
11291122
DefinedOrUnknownSVal V =
1130-
svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, Ctx.IntTy, Count);
1123+
svalBuilder.conjureSymbolVal(baseR, S, LCtx, Ctx.IntTy, Count);
11311124
B = B.addBinding(baseR, BindingKey::Default, V);
11321125
return;
11331126
}
@@ -1148,8 +1141,8 @@ void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
11481141
if (T->isRecordType()) {
11491142
// Invalidate the region by setting its default value to
11501143
// conjured symbol. The type of the symbol is irrelevant.
1151-
DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
1152-
Ctx.IntTy, Count);
1144+
DefinedOrUnknownSVal V =
1145+
svalBuilder.conjureSymbolVal(baseR, S, LCtx, Ctx.IntTy, Count);
11531146
B = B.addBinding(baseR, BindingKey::Default, V);
11541147
return;
11551148
}
@@ -1216,15 +1209,14 @@ void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
12161209
}
12171210
conjure_default:
12181211
// Set the default value of the array to conjured symbol.
1219-
DefinedOrUnknownSVal V =
1220-
svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
1221-
AT->getElementType(), Count);
1222-
B = B.addBinding(baseR, BindingKey::Default, V);
1223-
return;
1212+
DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(
1213+
baseR, S, LCtx, AT->getElementType(), Count);
1214+
B = B.addBinding(baseR, BindingKey::Default, V);
1215+
return;
12241216
}
12251217

1226-
DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
1227-
T,Count);
1218+
DefinedOrUnknownSVal V =
1219+
svalBuilder.conjureSymbolVal(baseR, S, LCtx, T, Count);
12281220
assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
12291221
B = B.addBinding(baseR, BindingKey::Direct, V);
12301222
}
@@ -1252,19 +1244,16 @@ bool InvalidateRegionsWorker::includeEntireMemorySpace(const MemRegion *Base) {
12521244
RegionAndSymbolInvalidationTraits::TK_EntireMemSpace);
12531245
}
12541246

1255-
RegionBindingsRef
1256-
RegionStoreManager::invalidateGlobalRegion(MemRegion::Kind K,
1257-
const Expr *Ex,
1258-
unsigned Count,
1259-
const LocationContext *LCtx,
1260-
RegionBindingsRef B,
1261-
InvalidatedRegions *Invalidated) {
1247+
RegionBindingsRef RegionStoreManager::invalidateGlobalRegion(
1248+
MemRegion::Kind K, const Stmt *S, unsigned Count,
1249+
const LocationContext *LCtx, RegionBindingsRef B,
1250+
InvalidatedRegions *Invalidated) {
12621251
// Bind the globals memory space to a new symbol that we will use to derive
12631252
// the bindings for all globals.
12641253
const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(K);
1265-
SVal V = svalBuilder.conjureSymbolVal(/* symbolTag = */ (const void*) GS, Ex, LCtx,
1266-
/* type does not matter */ Ctx.IntTy,
1267-
Count);
1254+
SVal V =
1255+
svalBuilder.conjureSymbolVal(/* symbolTag = */ (const void *)GS, S, LCtx,
1256+
/* type does not matter */ Ctx.IntTy, Count);
12681257

12691258
B = B.removeBinding(GS)
12701259
.addBinding(BindingKey::Make(GS, BindingKey::Default), V);
@@ -1298,16 +1287,11 @@ void RegionStoreManager::populateWorkList(InvalidateRegionsWorker &W,
12981287
}
12991288
}
13001289

1301-
StoreRef
1302-
RegionStoreManager::invalidateRegions(Store store,
1303-
ArrayRef<SVal> Values,
1304-
const Expr *Ex, unsigned Count,
1305-
const LocationContext *LCtx,
1306-
const CallEvent *Call,
1307-
InvalidatedSymbols &IS,
1308-
RegionAndSymbolInvalidationTraits &ITraits,
1309-
InvalidatedRegions *TopLevelRegions,
1310-
InvalidatedRegions *Invalidated) {
1290+
StoreRef RegionStoreManager::invalidateRegions(
1291+
Store store, ArrayRef<SVal> Values, const Stmt *S, unsigned Count,
1292+
const LocationContext *LCtx, const CallEvent *Call, InvalidatedSymbols &IS,
1293+
RegionAndSymbolInvalidationTraits &ITraits,
1294+
InvalidatedRegions *TopLevelRegions, InvalidatedRegions *Invalidated) {
13111295
GlobalsFilterKind GlobalsFilter;
13121296
if (Call) {
13131297
if (Call->isInSystemHeader())
@@ -1319,7 +1303,7 @@ RegionStoreManager::invalidateRegions(Store store,
13191303
}
13201304

13211305
RegionBindingsRef B = getRegionBindings(store);
1322-
InvalidateRegionsWorker W(*this, StateMgr, B, Ex, Count, LCtx, IS, ITraits,
1306+
InvalidateRegionsWorker W(*this, StateMgr, B, S, Count, LCtx, IS, ITraits,
13231307
Invalidated, GlobalsFilter);
13241308

13251309
// Scan the bindings and generate the clusters.
@@ -1339,12 +1323,12 @@ RegionStoreManager::invalidateRegions(Store store,
13391323
// TODO: This could possibly be more precise with modules.
13401324
switch (GlobalsFilter) {
13411325
case GFK_All:
1342-
B = invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind,
1343-
Ex, Count, LCtx, B, Invalidated);
1326+
B = invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind, S,
1327+
Count, LCtx, B, Invalidated);
13441328
[[fallthrough]];
13451329
case GFK_SystemOnly:
1346-
B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind,
1347-
Ex, Count, LCtx, B, Invalidated);
1330+
B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind, S, Count,
1331+
LCtx, B, Invalidated);
13481332
[[fallthrough]];
13491333
case GFK_None:
13501334
break;

clang/lib/StaticAnalyzer/Core/SValBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *SymbolTag,
174174
}
175175

176176
DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag,
177-
const Expr *expr,
177+
const Stmt *St,
178178
const LocationContext *LCtx,
179179
QualType type,
180180
unsigned count) {
@@ -184,7 +184,7 @@ DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag,
184184
if (!SymbolManager::canSymbolicate(type))
185185
return UnknownVal();
186186

187-
SymbolRef sym = SymMgr.conjureSymbol(expr, LCtx, type, count, symbolTag);
187+
SymbolRef sym = SymMgr.conjureSymbol(St, LCtx, type, count, symbolTag);
188188

189189
if (Loc::isLocType(type))
190190
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));

0 commit comments

Comments
 (0)