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