Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 1 | //== Environment.cpp - Map from Stmt* to Locations/Values -------*- C++ -*--==// |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defined the Environment and EnvironmentManager classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 13 | |
| 14 | #include "clang/Analysis/AnalysisContext.h" |
| 15 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 16 | #include "clang/Checker/PathSensitive/GRState.h" |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 20 | SVal Environment::GetSVal(const Stmt *E, ValueManager& ValMgr) const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 21 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 22 | for (;;) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 24 | switch (E->getStmtClass()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | |
| 26 | case Stmt::AddrLabelExprClass: |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 27 | return ValMgr.makeLoc(cast<AddrLabelExpr>(E)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 29 | // ParenExprs are no-ops. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
| 31 | case Stmt::ParenExprClass: |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 32 | E = cast<ParenExpr>(E)->getSubExpr(); |
| 33 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 35 | case Stmt::CharacterLiteralClass: { |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 36 | const CharacterLiteral* C = cast<CharacterLiteral>(E); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 37 | return ValMgr.makeIntVal(C->getValue(), C->getType()); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 38 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Zhongxing Xu | 477323d | 2010-04-14 06:29:29 +0000 | [diff] [blame] | 40 | case Stmt::CXXBoolLiteralExprClass: { |
| 41 | const SVal *X = ExprBindings.lookup(E); |
| 42 | if (X) |
| 43 | return *X; |
| 44 | else |
| 45 | return ValMgr.makeIntVal(cast<CXXBoolLiteralExpr>(E)); |
| 46 | } |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 47 | case Stmt::IntegerLiteralClass: { |
Zhongxing Xu | bc37b8d | 2010-01-09 09:16:47 +0000 | [diff] [blame] | 48 | // In C++, this expression may have been bound to a temporary object. |
| 49 | SVal const *X = ExprBindings.lookup(E); |
| 50 | if (X) |
| 51 | return *X; |
| 52 | else |
| 53 | return ValMgr.makeIntVal(cast<IntegerLiteral>(E)); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 54 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 56 | // Casts where the source and target type are the same |
| 57 | // are no-ops. We blast through these to get the descendant |
| 58 | // subexpression that has a value. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 60 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 61 | case Stmt::CStyleCastExprClass: { |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 62 | const CastExpr* C = cast<CastExpr>(E); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 63 | QualType CT = C->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 65 | if (CT->isVoidType()) |
| 66 | return UnknownVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 68 | break; |
| 69 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | d493163 | 2008-11-12 19:21:30 +0000 | [diff] [blame] | 71 | // Handle all other Stmt* using a lookup. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 73 | default: |
| 74 | break; |
| 75 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 77 | break; |
| 78 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 80 | return LookupExpr(E); |
| 81 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | 6d4c022 | 2010-09-03 01:07:02 +0000 | [diff] [blame] | 83 | Environment EnvironmentManager::bindExpr(Environment Env, const Stmt *S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | SVal V, bool Invalidate) { |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 85 | assert(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
| 87 | if (V.isUnknown()) { |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 88 | if (Invalidate) |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 89 | return Environment(F.Remove(Env.ExprBindings, S)); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 90 | else |
| 91 | return Env; |
| 92 | } |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 93 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 94 | return Environment(F.Add(Env.ExprBindings, S, V)); |
Ted Kremenek | d72ee90 | 2008-07-10 17:19:18 +0000 | [diff] [blame] | 95 | } |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | 6d4c022 | 2010-09-03 01:07:02 +0000 | [diff] [blame] | 97 | static inline const Stmt *MakeLocation(const Stmt *S) { |
| 98 | return (const Stmt*) (((uintptr_t) S) | 0x1); |
| 99 | } |
| 100 | |
| 101 | Environment EnvironmentManager::bindExprAndLocation(Environment Env, |
| 102 | const Stmt *S, |
| 103 | SVal location, SVal V) { |
Zhanyong Wan | cf84887 | 2010-11-20 07:52:48 +0000 | [diff] [blame^] | 104 | return Environment(F.Add(F.Add(Env.ExprBindings, MakeLocation(S), location), |
| 105 | S, V)); |
Ted Kremenek | 6d4c022 | 2010-09-03 01:07:02 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 108 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 109 | class MarkLiveCallback : public SymbolVisitor { |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 110 | SymbolReaper &SymReaper; |
| 111 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | MarkLiveCallback(SymbolReaper &symreaper) : SymReaper(symreaper) {} |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 113 | bool VisitSymbol(SymbolRef sym) { SymReaper.markLive(sym); return true; } |
| 114 | }; |
| 115 | } // end anonymous namespace |
| 116 | |
Zhongxing Xu | 7b73b92 | 2010-04-05 13:16:29 +0000 | [diff] [blame] | 117 | static bool isBlockExprInCallers(const Stmt *E, const LocationContext *LC) { |
| 118 | const LocationContext *ParentLC = LC->getParent(); |
| 119 | while (ParentLC) { |
| 120 | CFG &C = *ParentLC->getCFG(); |
| 121 | if (C.isBlkExpr(E)) |
| 122 | return true; |
| 123 | ParentLC = ParentLC->getParent(); |
| 124 | } |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | |
Ted Kremenek | 6d4c022 | 2010-09-03 01:07:02 +0000 | [diff] [blame] | 129 | // In addition to mapping from Stmt * - > SVals in the Environment, we also |
| 130 | // maintain a mapping from Stmt * -> SVals (locations) that were used during |
| 131 | // a load and store. |
| 132 | static inline bool IsLocation(const Stmt *S) { |
| 133 | return (bool) (((uintptr_t) S) & 0x1); |
| 134 | } |
Zhongxing Xu | 7b73b92 | 2010-04-05 13:16:29 +0000 | [diff] [blame] | 135 | |
Zhongxing Xu | 9d8d0fc | 2009-03-12 07:54:17 +0000 | [diff] [blame] | 136 | // RemoveDeadBindings: |
| 137 | // - Remove subexpression bindings. |
| 138 | // - Remove dead block expression bindings. |
| 139 | // - Keep live block expression bindings: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | // - Mark their reachable symbols live in SymbolReaper, |
Zhongxing Xu | 9d8d0fc | 2009-03-12 07:54:17 +0000 | [diff] [blame] | 141 | // see ScanReachableSymbols. |
| 142 | // - Mark the region in DRoots if the binding is a loc::MemRegionVal. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | Environment |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 144 | EnvironmentManager::RemoveDeadBindings(Environment Env, |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 145 | SymbolReaper &SymReaper, |
| 146 | const GRState *ST, |
| 147 | llvm::SmallVectorImpl<const MemRegion*> &DRoots) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 149 | CFG &C = *SymReaper.getLocationContext()->getCFG(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 151 | // We construct a new Environment object entirely, as this is cheaper than |
| 152 | // individually removing all the subexpression bindings (which will greatly |
| 153 | // outnumber block-level expression bindings). |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 154 | Environment NewEnv = getInitialEnvironment(); |
Ted Kremenek | 6d4c022 | 2010-09-03 01:07:02 +0000 | [diff] [blame] | 155 | |
| 156 | llvm::SmallVector<std::pair<const Stmt*, SVal>, 10> deferredLocations; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 158 | // Iterate over the block-expr bindings. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | for (Environment::iterator I = Env.begin(), E = Env.end(); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 160 | I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 162 | const Stmt *BlkExpr = I.getKey(); |
Ted Kremenek | 6d4c022 | 2010-09-03 01:07:02 +0000 | [diff] [blame] | 163 | |
| 164 | // For recorded locations (used when evaluating loads and stores), we |
| 165 | // consider them live only when their associated normal expression is |
| 166 | // also live. |
| 167 | // NOTE: This assumes that loads/stores that evaluated to UnknownVal |
| 168 | // still have an entry in the map. |
| 169 | if (IsLocation(BlkExpr)) { |
| 170 | deferredLocations.push_back(std::make_pair(BlkExpr, I.getData())); |
| 171 | continue; |
| 172 | } |
| 173 | |
Zhongxing Xu | 7b73b92 | 2010-04-05 13:16:29 +0000 | [diff] [blame] | 174 | const SVal &X = I.getData(); |
| 175 | |
| 176 | // Block-level expressions in callers are assumed always live. |
| 177 | if (isBlockExprInCallers(BlkExpr, SymReaper.getLocationContext())) { |
| 178 | NewEnv.ExprBindings = F.Add(NewEnv.ExprBindings, BlkExpr, X); |
| 179 | |
| 180 | if (isa<loc::MemRegionVal>(X)) { |
| 181 | const MemRegion* R = cast<loc::MemRegionVal>(X).getRegion(); |
| 182 | DRoots.push_back(R); |
| 183 | } |
| 184 | |
| 185 | // Mark all symbols in the block expr's value live. |
| 186 | MarkLiveCallback cb(SymReaper); |
| 187 | ST->scanReachableSymbols(X, cb); |
| 188 | continue; |
| 189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 191 | // Not a block-level expression? |
| 192 | if (!C.isBlkExpr(BlkExpr)) |
| 193 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 195 | if (SymReaper.isLive(BlkExpr)) { |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 196 | // Copy the binding to the new map. |
| 197 | NewEnv.ExprBindings = F.Add(NewEnv.ExprBindings, BlkExpr, X); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 199 | // If the block expr's value is a memory region, then mark that region. |
Zhongxing Xu | ce2f9bd | 2009-06-30 13:00:53 +0000 | [diff] [blame] | 200 | if (isa<loc::MemRegionVal>(X)) { |
| 201 | const MemRegion* R = cast<loc::MemRegionVal>(X).getRegion(); |
| 202 | DRoots.push_back(R); |
Zhongxing Xu | ce2f9bd | 2009-06-30 13:00:53 +0000 | [diff] [blame] | 203 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 205 | // Mark all symbols in the block expr's value live. |
| 206 | MarkLiveCallback cb(SymReaper); |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 207 | ST->scanReachableSymbols(X, cb); |
| 208 | continue; |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 209 | } |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 210 | |
| 211 | // Otherwise the expression is dead with a couple exceptions. |
| 212 | // Do not misclean LogicalExpr or ConditionalOperator. It is dead at the |
| 213 | // beginning of itself, but we need its UndefinedVal to determine its |
| 214 | // SVal. |
| 215 | if (X.isUndef() && cast<UndefinedVal>(X).getData()) |
| 216 | NewEnv.ExprBindings = F.Add(NewEnv.ExprBindings, BlkExpr, X); |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 217 | } |
Ted Kremenek | 6d4c022 | 2010-09-03 01:07:02 +0000 | [diff] [blame] | 218 | |
| 219 | // Go through he deferred locations and add them to the new environment if |
| 220 | // the correspond Stmt* is in the map as well. |
| 221 | for (llvm::SmallVectorImpl<std::pair<const Stmt*, SVal> >::iterator |
| 222 | I = deferredLocations.begin(), E = deferredLocations.end(); I != E; ++I) { |
| 223 | const Stmt *S = (Stmt*) (((uintptr_t) I->first) & (uintptr_t) ~0x1); |
| 224 | if (NewEnv.ExprBindings.lookup(S)) |
| 225 | NewEnv.ExprBindings = F.Add(NewEnv.ExprBindings, I->first, I->second); |
| 226 | } |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 228 | return NewEnv; |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 229 | } |