1 //===- SymbolManager.h - Management of Symbolic Values --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines SymbolManager, a class that manages symbolic values
10 //  created for use by ExprEngine and related classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/StmtObjC.h"
18 #include "clang/Analysis/Analyses/LiveVariables.h"
19 #include "clang/Analysis/AnalysisDeclContext.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <cassert>
32 
33 using namespace clang;
34 using namespace ento;
35 
36 void SymExpr::anchor() {}
37 
38 StringRef SymbolConjured::getKindStr() const { return "conj_$"; }
39 StringRef SymbolDerived::getKindStr() const { return "derived_$"; }
40 StringRef SymbolExtent::getKindStr() const { return "extent_$"; }
41 StringRef SymbolMetadata::getKindStr() const { return "meta_$"; }
42 StringRef SymbolRegionValue::getKindStr() const { return "reg_$"; }
43 
44 LLVM_DUMP_METHOD void SymExpr::dump() const { dumpToStream(llvm::errs()); }
45 
46 void BinarySymExpr::dumpToStreamImpl(raw_ostream &OS, const SymExpr *Sym) {
47   OS << '(';
48   Sym->dumpToStream(OS);
49   OS << ')';
50 }
51 
52 void BinarySymExpr::dumpToStreamImpl(raw_ostream &OS,
53                                      const llvm::APSInt &Value) {
54   if (Value.isUnsigned())
55     OS << Value.getZExtValue();
56   else
57     OS << Value.getSExtValue();
58   if (Value.isUnsigned())
59     OS << 'U';
60 }
61 
62 void BinarySymExpr::dumpToStreamImpl(raw_ostream &OS,
63                                      BinaryOperator::Opcode Op) {
64   OS << ' ' << BinaryOperator::getOpcodeStr(Op) << ' ';
65 }
66 
67 void SymbolCast::dumpToStream(raw_ostream &os) const {
68   os << '(' << ToTy << ") (";
69   Operand->dumpToStream(os);
70   os << ')';
71 }
72 
73 void SymbolConjured::dumpToStream(raw_ostream &os) const {
74   os << getKindStr() << getSymbolID() << '{' << T << ", LC" << LCtx->getID();
75   if (S)
76     os << ", S" << S->getID(LCtx->getDecl()->getASTContext());
77   else
78     os << ", no stmt";
79   os << ", #" << Count << '}';
80 }
81 
82 void SymbolDerived::dumpToStream(raw_ostream &os) const {
83   os << getKindStr() << getSymbolID() << '{' << getParentSymbol() << ','
84      << getRegion() << '}';
85 }
86 
87 void SymbolExtent::dumpToStream(raw_ostream &os) const {
88   os << getKindStr() << getSymbolID() << '{' << getRegion() << '}';
89 }
90 
91 void SymbolMetadata::dumpToStream(raw_ostream &os) const {
92   os << getKindStr() << getSymbolID() << '{' << getRegion() << ',' << T << '}';
93 }
94 
95 void SymbolData::anchor() {}
96 
97 void SymbolRegionValue::dumpToStream(raw_ostream &os) const {
98   os << getKindStr() << getSymbolID() << '<' << getType() << ' ' << R << '>';
99 }
100 
101 bool SymExpr::symbol_iterator::operator==(const symbol_iterator &X) const {
102   return itr == X.itr;
103 }
104 
105 bool SymExpr::symbol_iterator::operator!=(const symbol_iterator &X) const {
106   return itr != X.itr;
107 }
108 
109 SymExpr::symbol_iterator::symbol_iterator(const SymExpr *SE) {
110   itr.push_back(SE);
111 }
112 
113 SymExpr::symbol_iterator &SymExpr::symbol_iterator::operator++() {
114   assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
115   expand();
116   return *this;
117 }
118 
119 SymbolRef SymExpr::symbol_iterator::operator*() {
120   assert(!itr.empty() && "attempting to dereference an 'end' iterator");
121   return itr.back();
122 }
123 
124 void SymExpr::symbol_iterator::expand() {
125   const SymExpr *SE = itr.pop_back_val();
126 
127   switch (SE->getKind()) {
128     case SymExpr::SymbolRegionValueKind:
129     case SymExpr::SymbolConjuredKind:
130     case SymExpr::SymbolDerivedKind:
131     case SymExpr::SymbolExtentKind:
132     case SymExpr::SymbolMetadataKind:
133       return;
134     case SymExpr::SymbolCastKind:
135       itr.push_back(cast<SymbolCast>(SE)->getOperand());
136       return;
137     case SymExpr::SymIntExprKind:
138       itr.push_back(cast<SymIntExpr>(SE)->getLHS());
139       return;
140     case SymExpr::IntSymExprKind:
141       itr.push_back(cast<IntSymExpr>(SE)->getRHS());
142       return;
143     case SymExpr::SymSymExprKind: {
144       const auto *x = cast<SymSymExpr>(SE);
145       itr.push_back(x->getLHS());
146       itr.push_back(x->getRHS());
147       return;
148     }
149   }
150   llvm_unreachable("unhandled expansion case");
151 }
152 
153 const SymbolRegionValue*
154 SymbolManager::getRegionValueSymbol(const TypedValueRegion* R) {
155   llvm::FoldingSetNodeID profile;
156   SymbolRegionValue::Profile(profile, R);
157   void *InsertPos;
158   SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
159   if (!SD) {
160     SD = (SymExpr*) BPAlloc.Allocate<SymbolRegionValue>();
161     new (SD) SymbolRegionValue(SymbolCounter, R);
162     DataSet.InsertNode(SD, InsertPos);
163     ++SymbolCounter;
164   }
165 
166   return cast<SymbolRegionValue>(SD);
167 }
168 
169 const SymbolConjured* SymbolManager::conjureSymbol(const Stmt *E,
170                                                    const LocationContext *LCtx,
171                                                    QualType T,
172                                                    unsigned Count,
173                                                    const void *SymbolTag) {
174   llvm::FoldingSetNodeID profile;
175   SymbolConjured::Profile(profile, E, T, Count, LCtx, SymbolTag);
176   void *InsertPos;
177   SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
178   if (!SD) {
179     SD = (SymExpr*) BPAlloc.Allocate<SymbolConjured>();
180     new (SD) SymbolConjured(SymbolCounter, E, LCtx, T, Count, SymbolTag);
181     DataSet.InsertNode(SD, InsertPos);
182     ++SymbolCounter;
183   }
184 
185   return cast<SymbolConjured>(SD);
186 }
187 
188 const SymbolDerived*
189 SymbolManager::getDerivedSymbol(SymbolRef parentSymbol,
190                                 const TypedValueRegion *R) {
191   llvm::FoldingSetNodeID profile;
192   SymbolDerived::Profile(profile, parentSymbol, R);
193   void *InsertPos;
194   SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
195   if (!SD) {
196     SD = (SymExpr*) BPAlloc.Allocate<SymbolDerived>();
197     new (SD) SymbolDerived(SymbolCounter, parentSymbol, R);
198     DataSet.InsertNode(SD, InsertPos);
199     ++SymbolCounter;
200   }
201 
202   return cast<SymbolDerived>(SD);
203 }
204 
205 const SymbolExtent*
206 SymbolManager::getExtentSymbol(const SubRegion *R) {
207   llvm::FoldingSetNodeID profile;
208   SymbolExtent::Profile(profile, R);
209   void *InsertPos;
210   SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
211   if (!SD) {
212     SD = (SymExpr*) BPAlloc.Allocate<SymbolExtent>();
213     new (SD) SymbolExtent(SymbolCounter, R);
214     DataSet.InsertNode(SD, InsertPos);
215     ++SymbolCounter;
216   }
217 
218   return cast<SymbolExtent>(SD);
219 }
220 
221 const SymbolMetadata *
222 SymbolManager::getMetadataSymbol(const MemRegion* R, const Stmt *S, QualType T,
223                                  const LocationContext *LCtx,
224                                  unsigned Count, const void *SymbolTag) {
225   llvm::FoldingSetNodeID profile;
226   SymbolMetadata::Profile(profile, R, S, T, LCtx, Count, SymbolTag);
227   void *InsertPos;
228   SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos);
229   if (!SD) {
230     SD = (SymExpr*) BPAlloc.Allocate<SymbolMetadata>();
231     new (SD) SymbolMetadata(SymbolCounter, R, S, T, LCtx, Count, SymbolTag);
232     DataSet.InsertNode(SD, InsertPos);
233     ++SymbolCounter;
234   }
235 
236   return cast<SymbolMetadata>(SD);
237 }
238 
239 const SymbolCast*
240 SymbolManager::getCastSymbol(const SymExpr *Op,
241                              QualType From, QualType To) {
242   llvm::FoldingSetNodeID ID;
243   SymbolCast::Profile(ID, Op, From, To);
244   void *InsertPos;
245   SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
246   if (!data) {
247     data = (SymbolCast*) BPAlloc.Allocate<SymbolCast>();
248     new (data) SymbolCast(Op, From, To);
249     DataSet.InsertNode(data, InsertPos);
250   }
251 
252   return cast<SymbolCast>(data);
253 }
254 
255 const SymIntExpr *SymbolManager::getSymIntExpr(const SymExpr *lhs,
256                                                BinaryOperator::Opcode op,
257                                                const llvm::APSInt& v,
258                                                QualType t) {
259   llvm::FoldingSetNodeID ID;
260   SymIntExpr::Profile(ID, lhs, op, v, t);
261   void *InsertPos;
262   SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
263 
264   if (!data) {
265     data = (SymIntExpr*) BPAlloc.Allocate<SymIntExpr>();
266     new (data) SymIntExpr(lhs, op, v, t);
267     DataSet.InsertNode(data, InsertPos);
268   }
269 
270   return cast<SymIntExpr>(data);
271 }
272 
273 const IntSymExpr *SymbolManager::getIntSymExpr(const llvm::APSInt& lhs,
274                                                BinaryOperator::Opcode op,
275                                                const SymExpr *rhs,
276                                                QualType t) {
277   llvm::FoldingSetNodeID ID;
278   IntSymExpr::Profile(ID, lhs, op, rhs, t);
279   void *InsertPos;
280   SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
281 
282   if (!data) {
283     data = (IntSymExpr*) BPAlloc.Allocate<IntSymExpr>();
284     new (data) IntSymExpr(lhs, op, rhs, t);
285     DataSet.InsertNode(data, InsertPos);
286   }
287 
288   return cast<IntSymExpr>(data);
289 }
290 
291 const SymSymExpr *SymbolManager::getSymSymExpr(const SymExpr *lhs,
292                                                BinaryOperator::Opcode op,
293                                                const SymExpr *rhs,
294                                                QualType t) {
295   llvm::FoldingSetNodeID ID;
296   SymSymExpr::Profile(ID, lhs, op, rhs, t);
297   void *InsertPos;
298   SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos);
299 
300   if (!data) {
301     data = (SymSymExpr*) BPAlloc.Allocate<SymSymExpr>();
302     new (data) SymSymExpr(lhs, op, rhs, t);
303     DataSet.InsertNode(data, InsertPos);
304   }
305 
306   return cast<SymSymExpr>(data);
307 }
308 
309 QualType SymbolConjured::getType() const {
310   return T;
311 }
312 
313 QualType SymbolDerived::getType() const {
314   return R->getValueType();
315 }
316 
317 QualType SymbolExtent::getType() const {
318   ASTContext &Ctx = R->getMemRegionManager().getContext();
319   return Ctx.getSizeType();
320 }
321 
322 QualType SymbolMetadata::getType() const {
323   return T;
324 }
325 
326 QualType SymbolRegionValue::getType() const {
327   return R->getValueType();
328 }
329 
330 bool SymbolManager::canSymbolicate(QualType T) {
331   T = T.getCanonicalType();
332 
333   if (Loc::isLocType(T))
334     return true;
335 
336   if (T->isIntegralOrEnumerationType())
337     return true;
338 
339   if (T->isRecordType() && !T->isUnionType())
340     return true;
341 
342   return false;
343 }
344 
345 void SymbolManager::addSymbolDependency(const SymbolRef Primary,
346                                         const SymbolRef Dependent) {
347   auto &dependencies = SymbolDependencies[Primary];
348   if (!dependencies) {
349     dependencies = std::make_unique<SymbolRefSmallVectorTy>();
350   }
351   dependencies->push_back(Dependent);
352 }
353 
354 const SymbolRefSmallVectorTy *SymbolManager::getDependentSymbols(
355                                                      const SymbolRef Primary) {
356   SymbolDependTy::const_iterator I = SymbolDependencies.find(Primary);
357   if (I == SymbolDependencies.end())
358     return nullptr;
359   return I->second.get();
360 }
361 
362 void SymbolReaper::markDependentsLive(SymbolRef sym) {
363   // Do not mark dependents more then once.
364   SymbolMapTy::iterator LI = TheLiving.find(sym);
365   assert(LI != TheLiving.end() && "The primary symbol is not live.");
366   if (LI->second == HaveMarkedDependents)
367     return;
368   LI->second = HaveMarkedDependents;
369 
370   if (const SymbolRefSmallVectorTy *Deps = SymMgr.getDependentSymbols(sym)) {
371     for (const auto I : *Deps) {
372       if (TheLiving.find(I) != TheLiving.end())
373         continue;
374       markLive(I);
375     }
376   }
377 }
378 
379 void SymbolReaper::markLive(SymbolRef sym) {
380   TheLiving[sym] = NotProcessed;
381   markDependentsLive(sym);
382 }
383 
384 void SymbolReaper::markLive(const MemRegion *region) {
385   RegionRoots.insert(region->getBaseRegion());
386   markElementIndicesLive(region);
387 }
388 
389 void SymbolReaper::markElementIndicesLive(const MemRegion *region) {
390   for (auto SR = dyn_cast<SubRegion>(region); SR;
391        SR = dyn_cast<SubRegion>(SR->getSuperRegion())) {
392     if (const auto ER = dyn_cast<ElementRegion>(SR)) {
393       SVal Idx = ER->getIndex();
394       for (auto SI = Idx.symbol_begin(), SE = Idx.symbol_end(); SI != SE; ++SI)
395         markLive(*SI);
396     }
397   }
398 }
399 
400 void SymbolReaper::markInUse(SymbolRef sym) {
401   if (isa<SymbolMetadata>(sym))
402     MetadataInUse.insert(sym);
403 }
404 
405 bool SymbolReaper::isLiveRegion(const MemRegion *MR) {
406   // TODO: For now, liveness of a memory region is equivalent to liveness of its
407   // base region. In fact we can do a bit better: say, if a particular FieldDecl
408   // is not used later in the path, we can diagnose a leak of a value within
409   // that field earlier than, say, the variable that contains the field dies.
410   MR = MR->getBaseRegion();
411 
412   if (RegionRoots.count(MR))
413     return true;
414 
415   if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
416     return isLive(SR->getSymbol());
417 
418   if (const auto *VR = dyn_cast<VarRegion>(MR))
419     return isLive(VR, true);
420 
421   // FIXME: This is a gross over-approximation. What we really need is a way to
422   // tell if anything still refers to this region. Unlike SymbolicRegions,
423   // AllocaRegions don't have associated symbols, though, so we don't actually
424   // have a way to track their liveness.
425   return isa<AllocaRegion, CXXThisRegion, MemSpaceRegion, CodeTextRegion>(MR);
426 }
427 
428 bool SymbolReaper::isLive(SymbolRef sym) {
429   if (TheLiving.count(sym)) {
430     markDependentsLive(sym);
431     return true;
432   }
433 
434   bool KnownLive;
435 
436   switch (sym->getKind()) {
437   case SymExpr::SymbolRegionValueKind:
438     KnownLive = isLiveRegion(cast<SymbolRegionValue>(sym)->getRegion());
439     break;
440   case SymExpr::SymbolConjuredKind:
441     KnownLive = false;
442     break;
443   case SymExpr::SymbolDerivedKind:
444     KnownLive = isLive(cast<SymbolDerived>(sym)->getParentSymbol());
445     break;
446   case SymExpr::SymbolExtentKind:
447     KnownLive = isLiveRegion(cast<SymbolExtent>(sym)->getRegion());
448     break;
449   case SymExpr::SymbolMetadataKind:
450     KnownLive = MetadataInUse.count(sym) &&
451                 isLiveRegion(cast<SymbolMetadata>(sym)->getRegion());
452     if (KnownLive)
453       MetadataInUse.erase(sym);
454     break;
455   case SymExpr::SymIntExprKind:
456     KnownLive = isLive(cast<SymIntExpr>(sym)->getLHS());
457     break;
458   case SymExpr::IntSymExprKind:
459     KnownLive = isLive(cast<IntSymExpr>(sym)->getRHS());
460     break;
461   case SymExpr::SymSymExprKind:
462     KnownLive = isLive(cast<SymSymExpr>(sym)->getLHS()) &&
463                 isLive(cast<SymSymExpr>(sym)->getRHS());
464     break;
465   case SymExpr::SymbolCastKind:
466     KnownLive = isLive(cast<SymbolCast>(sym)->getOperand());
467     break;
468   }
469 
470   if (KnownLive)
471     markLive(sym);
472 
473   return KnownLive;
474 }
475 
476 bool
477 SymbolReaper::isLive(const Expr *ExprVal, const LocationContext *ELCtx) const {
478   if (LCtx == nullptr)
479     return false;
480 
481   if (LCtx != ELCtx) {
482     // If the reaper's location context is a parent of the expression's
483     // location context, then the expression value is now "out of scope".
484     if (LCtx->isParentOf(ELCtx))
485       return false;
486     return true;
487   }
488 
489   // If no statement is provided, everything in this and parent contexts is
490   // live.
491   if (!Loc)
492     return true;
493 
494   return LCtx->getAnalysis<RelaxedLiveVariables>()->isLive(Loc, ExprVal);
495 }
496 
497 bool SymbolReaper::isLive(const VarRegion *VR, bool includeStoreBindings) const{
498   const StackFrameContext *VarContext = VR->getStackFrame();
499 
500   if (!VarContext)
501     return true;
502 
503   if (!LCtx)
504     return false;
505   const StackFrameContext *CurrentContext = LCtx->getStackFrame();
506 
507   if (VarContext == CurrentContext) {
508     // If no statement is provided, everything is live.
509     if (!Loc)
510       return true;
511 
512     // Anonymous parameters of an inheriting constructor are live for the entire
513     // duration of the constructor.
514     if (isa<CXXInheritedCtorInitExpr>(Loc))
515       return true;
516 
517     if (LCtx->getAnalysis<RelaxedLiveVariables>()->isLive(Loc, VR->getDecl()))
518       return true;
519 
520     if (!includeStoreBindings)
521       return false;
522 
523     unsigned &cachedQuery =
524       const_cast<SymbolReaper *>(this)->includedRegionCache[VR];
525 
526     if (cachedQuery) {
527       return cachedQuery == 1;
528     }
529 
530     // Query the store to see if the region occurs in any live bindings.
531     if (Store store = reapedStore.getStore()) {
532       bool hasRegion =
533         reapedStore.getStoreManager().includedInBindings(store, VR);
534       cachedQuery = hasRegion ? 1 : 2;
535       return hasRegion;
536     }
537 
538     return false;
539   }
540 
541   return VarContext->isParentOf(CurrentContext);
542 }
543