Skip to content

Commit 3b4ba72

Browse files
NagyDonattstellar
authored andcommitted
[analyzer] Fix performance of getTaintedSymbolsImpl() (#89606)
Previously the function ``` std::vector<SymbolRef> taint::getTaintedSymbolsImpl(ProgramStateRef State, const MemRegion *Reg, TaintTagType K, bool returnFirstOnly) ``` (one of the 4 overloaded variants under this name) was handling element regions in a highly inefficient manner: it performed the "also examine the super-region" step twice. (Once in the branch for element regions, and once in the more general branch for all `SubRegion`s -- note that `ElementRegion` is a subclass of `SubRegion`.) As pointer arithmetic produces `ElementRegion`s, it's not too difficult to get a chain of N nested element regions where this inefficient recursion would produce 2^N calls. This commit is essentially NFC, apart from the performance improvements and the removal of (probably irrelevant) duplicate entries from the return value of `getTaintedSymbols()` calls. Fixes #89045 (cherry picked from commit ce763bf)
1 parent 7699b34 commit 3b4ba72

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

clang/lib/StaticAnalyzer/Checkers/Taint.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,17 @@ std::vector<SymbolRef> taint::getTaintedSymbolsImpl(ProgramStateRef State,
216216
std::vector<SymbolRef> TaintedSymbols;
217217
if (!Reg)
218218
return TaintedSymbols;
219-
// Element region (array element) is tainted if either the base or the offset
220-
// are tainted.
219+
220+
// Element region (array element) is tainted if the offset is tainted.
221221
if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg)) {
222222
std::vector<SymbolRef> TaintedIndex =
223223
getTaintedSymbolsImpl(State, ER->getIndex(), K, returnFirstOnly);
224224
llvm::append_range(TaintedSymbols, TaintedIndex);
225225
if (returnFirstOnly && !TaintedSymbols.empty())
226226
return TaintedSymbols; // return early if needed
227-
std::vector<SymbolRef> TaintedSuperRegion =
228-
getTaintedSymbolsImpl(State, ER->getSuperRegion(), K, returnFirstOnly);
229-
llvm::append_range(TaintedSymbols, TaintedSuperRegion);
230-
if (returnFirstOnly && !TaintedSymbols.empty())
231-
return TaintedSymbols; // return early if needed
232227
}
233228

229+
// Symbolic region is tainted if the corresponding symbol is tainted.
234230
if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) {
235231
std::vector<SymbolRef> TaintedRegions =
236232
getTaintedSymbolsImpl(State, SR->getSymbol(), K, returnFirstOnly);
@@ -239,6 +235,8 @@ std::vector<SymbolRef> taint::getTaintedSymbolsImpl(ProgramStateRef State,
239235
return TaintedSymbols; // return early if needed
240236
}
241237

238+
// Any subregion (including Element and Symbolic regions) is tainted if its
239+
// super-region is tainted.
242240
if (const SubRegion *ER = dyn_cast<SubRegion>(Reg)) {
243241
std::vector<SymbolRef> TaintedSubRegions =
244242
getTaintedSymbolsImpl(State, ER->getSuperRegion(), K, returnFirstOnly);
@@ -318,4 +316,4 @@ std::vector<SymbolRef> taint::getTaintedSymbolsImpl(ProgramStateRef State,
318316
}
319317
}
320318
return TaintedSymbols;
321-
}
319+
}

0 commit comments

Comments
 (0)