Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 1 | //= ProgramState.cpp - Path-Sensitive "State" for tracking values --*- C++ -*--= |
| 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 implements ProgramState and ProgramStateManager. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Analysis/CFG.h" |
Jordan Rose | 972a368 | 2012-07-30 20:21:55 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
| 17 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
| 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h" |
Anna Zaks | ceac1d6 | 2011-11-16 19:58:10 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h" |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace ento; |
| 24 | |
Argyrios Kyrtzidis | a5888f6 | 2012-01-31 02:23:28 +0000 | [diff] [blame] | 25 | namespace clang { namespace ento { |
| 26 | /// Increments the number of times this state is referenced. |
| 27 | |
| 28 | void ProgramStateRetain(const ProgramState *state) { |
| 29 | ++const_cast<ProgramState*>(state)->refCount; |
| 30 | } |
| 31 | |
| 32 | /// Decrement the number of times this state is referenced. |
| 33 | void ProgramStateRelease(const ProgramState *state) { |
| 34 | assert(state->refCount > 0); |
| 35 | ProgramState *s = const_cast<ProgramState*>(state); |
| 36 | if (--s->refCount == 0) { |
| 37 | ProgramStateManager &Mgr = s->getStateManager(); |
| 38 | Mgr.StateSet.RemoveNode(s); |
| 39 | s->~ProgramState(); |
| 40 | Mgr.freeStates.push_back(s); |
| 41 | } |
| 42 | } |
| 43 | }} |
| 44 | |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 45 | ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env, |
| 46 | StoreRef st, GenericDataMap gdm) |
| 47 | : stateMgr(mgr), |
| 48 | Env(env), |
| 49 | store(st.getStore()), |
| 50 | GDM(gdm), |
| 51 | refCount(0) { |
| 52 | stateMgr->getStoreManager().incrementReferenceCount(store); |
| 53 | } |
| 54 | |
| 55 | ProgramState::ProgramState(const ProgramState &RHS) |
| 56 | : llvm::FoldingSetNode(), |
| 57 | stateMgr(RHS.stateMgr), |
| 58 | Env(RHS.Env), |
| 59 | store(RHS.store), |
| 60 | GDM(RHS.GDM), |
| 61 | refCount(0) { |
| 62 | stateMgr->getStoreManager().incrementReferenceCount(store); |
| 63 | } |
| 64 | |
| 65 | ProgramState::~ProgramState() { |
| 66 | if (store) |
| 67 | stateMgr->getStoreManager().decrementReferenceCount(store); |
| 68 | } |
| 69 | |
Jordan Rose | 972a368 | 2012-07-30 20:21:55 +0000 | [diff] [blame] | 70 | ProgramStateManager::ProgramStateManager(ASTContext &Ctx, |
| 71 | StoreManagerCreator CreateSMgr, |
| 72 | ConstraintManagerCreator CreateCMgr, |
| 73 | llvm::BumpPtrAllocator &alloc, |
| 74 | SubEngine &SubEng) |
| 75 | : Eng(&SubEng), EnvMgr(alloc), GDMFactory(alloc), |
| 76 | svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)), |
| 77 | CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) { |
| 78 | StoreMgr.reset((*CreateSMgr)(*this)); |
| 79 | ConstraintMgr.reset((*CreateCMgr)(*this, SubEng)); |
| 80 | } |
| 81 | |
| 82 | |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 83 | ProgramStateManager::~ProgramStateManager() { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 84 | for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end(); |
| 85 | I!=E; ++I) |
| 86 | I->second.second(I->second.first); |
| 87 | } |
| 88 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 89 | ProgramStateRef |
| 90 | ProgramStateManager::removeDeadBindings(ProgramStateRef state, |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 91 | const StackFrameContext *LCtx, |
| 92 | SymbolReaper& SymReaper) { |
| 93 | |
| 94 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 95 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 96 | // tells us are live. We then see what Decls they may reference, and keep |
| 97 | // those around. This code more than likely can be made faster, and the |
| 98 | // frequency of which this method is called should be experimented with |
| 99 | // for optimum performance. |
| 100 | ProgramState NewState = *state; |
| 101 | |
| 102 | NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state); |
| 103 | |
| 104 | // Clean up the store. |
| 105 | StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx, |
| 106 | SymReaper); |
| 107 | NewState.setStore(newStore); |
| 108 | SymReaper.setReapedStore(newStore); |
Jordan Rose | 24c7f98 | 2012-09-08 01:24:53 +0000 | [diff] [blame] | 109 | |
| 110 | ProgramStateRef Result = getPersistentState(NewState); |
| 111 | return ConstraintMgr->removeDeadBindings(Result, SymReaper); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 114 | ProgramStateRef ProgramState::bindCompoundLiteral(const CompoundLiteralExpr *CL, |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 115 | const LocationContext *LC, |
| 116 | SVal V) const { |
| 117 | const StoreRef &newStore = |
Ted Kremenek | 5be88dc | 2012-08-22 06:00:12 +0000 | [diff] [blame] | 118 | getStateManager().StoreMgr->bindCompoundLiteral(getStore(), CL, LC, V); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 119 | return makeWithStore(newStore); |
| 120 | } |
| 121 | |
Ted Kremenek | 32a549a | 2012-08-22 06:00:18 +0000 | [diff] [blame] | 122 | ProgramStateRef ProgramState::bindLoc(Loc LV, SVal V, bool notifyChanges) const { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 123 | ProgramStateManager &Mgr = getStateManager(); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 124 | ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(), |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 125 | LV, V)); |
| 126 | const MemRegion *MR = LV.getAsRegion(); |
Ted Kremenek | 32a549a | 2012-08-22 06:00:18 +0000 | [diff] [blame] | 127 | if (MR && Mgr.getOwningEngine() && notifyChanges) |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 128 | return Mgr.getOwningEngine()->processRegionChange(newState, MR); |
| 129 | |
| 130 | return newState; |
| 131 | } |
| 132 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 133 | ProgramStateRef ProgramState::bindDefault(SVal loc, SVal V) const { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 134 | ProgramStateManager &Mgr = getStateManager(); |
| 135 | const MemRegion *R = cast<loc::MemRegionVal>(loc).getRegion(); |
| 136 | const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 137 | ProgramStateRef new_state = makeWithStore(newStore); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 138 | return Mgr.getOwningEngine() ? |
| 139 | Mgr.getOwningEngine()->processRegionChange(new_state, R) : |
| 140 | new_state; |
| 141 | } |
| 142 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 143 | ProgramStateRef |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 144 | ProgramState::invalidateRegions(ArrayRef<const MemRegion *> Regions, |
| 145 | const Expr *E, unsigned Count, |
Ted Kremenek | 3133f79 | 2012-02-17 23:13:45 +0000 | [diff] [blame] | 146 | const LocationContext *LCtx, |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 147 | StoreManager::InvalidatedSymbols *IS, |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 148 | const CallEvent *Call) const { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 149 | if (!IS) { |
| 150 | StoreManager::InvalidatedSymbols invalidated; |
Ted Kremenek | 3133f79 | 2012-02-17 23:13:45 +0000 | [diff] [blame] | 151 | return invalidateRegionsImpl(Regions, E, Count, LCtx, |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 152 | invalidated, Call); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 153 | } |
Ted Kremenek | 3133f79 | 2012-02-17 23:13:45 +0000 | [diff] [blame] | 154 | return invalidateRegionsImpl(Regions, E, Count, LCtx, *IS, Call); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 157 | ProgramStateRef |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 158 | ProgramState::invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions, |
| 159 | const Expr *E, unsigned Count, |
Ted Kremenek | 3133f79 | 2012-02-17 23:13:45 +0000 | [diff] [blame] | 160 | const LocationContext *LCtx, |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 161 | StoreManager::InvalidatedSymbols &IS, |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 162 | const CallEvent *Call) const { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 163 | ProgramStateManager &Mgr = getStateManager(); |
| 164 | SubEngine* Eng = Mgr.getOwningEngine(); |
| 165 | |
| 166 | if (Eng && Eng->wantsRegionChangeUpdate(this)) { |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 167 | StoreManager::InvalidatedRegions Invalidated; |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 168 | const StoreRef &newStore |
Ted Kremenek | 3133f79 | 2012-02-17 23:13:45 +0000 | [diff] [blame] | 169 | = Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, LCtx, IS, |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 170 | Call, &Invalidated); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 171 | ProgramStateRef newState = makeWithStore(newStore); |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 172 | return Eng->processRegionChanges(newState, &IS, Regions, Invalidated, Call); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | const StoreRef &newStore = |
Ted Kremenek | 3133f79 | 2012-02-17 23:13:45 +0000 | [diff] [blame] | 176 | Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, LCtx, IS, |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 177 | Call, NULL); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 178 | return makeWithStore(newStore); |
| 179 | } |
| 180 | |
Ted Kremenek | 56a46b5 | 2012-08-22 06:37:46 +0000 | [diff] [blame] | 181 | ProgramStateRef ProgramState::killBinding(Loc LV) const { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 182 | assert(!isa<loc::MemRegionVal>(LV) && "Use invalidateRegion instead."); |
| 183 | |
| 184 | Store OldStore = getStore(); |
Ted Kremenek | 56a46b5 | 2012-08-22 06:37:46 +0000 | [diff] [blame] | 185 | const StoreRef &newStore = |
| 186 | getStateManager().StoreMgr->killBinding(OldStore, LV); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 187 | |
| 188 | if (newStore.getStore() == OldStore) |
| 189 | return this; |
| 190 | |
| 191 | return makeWithStore(newStore); |
| 192 | } |
| 193 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 194 | ProgramStateRef |
Jordan Rose | e54cfc7 | 2012-07-10 22:07:57 +0000 | [diff] [blame] | 195 | ProgramState::enterStackFrame(const CallEvent &Call, |
| 196 | const StackFrameContext *CalleeCtx) const { |
| 197 | const StoreRef &NewStore = |
| 198 | getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx); |
| 199 | return makeWithStore(NewStore); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const { |
| 203 | // We only want to do fetches from regions that we can actually bind |
| 204 | // values. For example, SymbolicRegions of type 'id<...>' cannot |
| 205 | // have direct bindings (but their can be bindings on their subregions). |
| 206 | if (!R->isBoundable()) |
| 207 | return UnknownVal(); |
| 208 | |
| 209 | if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { |
| 210 | QualType T = TR->getValueType(); |
| 211 | if (Loc::isLocType(T) || T->isIntegerType()) |
| 212 | return getSVal(R); |
| 213 | } |
| 214 | |
| 215 | return UnknownVal(); |
| 216 | } |
| 217 | |
| 218 | SVal ProgramState::getSVal(Loc location, QualType T) const { |
| 219 | SVal V = getRawSVal(cast<Loc>(location), T); |
| 220 | |
| 221 | // If 'V' is a symbolic value that is *perfectly* constrained to |
| 222 | // be a constant value, use that value instead to lessen the burden |
| 223 | // on later analysis stages (so we have less symbolic values to reason |
| 224 | // about). |
| 225 | if (!T.isNull()) { |
| 226 | if (SymbolRef sym = V.getAsSymbol()) { |
Ted Kremenek | 47cbd0f | 2012-09-07 22:31:01 +0000 | [diff] [blame] | 227 | if (const llvm::APSInt *Int = getStateManager() |
| 228 | .getConstraintManager() |
| 229 | .getSymVal(this, sym)) { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 230 | // FIXME: Because we don't correctly model (yet) sign-extension |
| 231 | // and truncation of symbolic values, we need to convert |
| 232 | // the integer value to the correct signedness and bitwidth. |
| 233 | // |
| 234 | // This shows up in the following: |
| 235 | // |
| 236 | // char foo(); |
| 237 | // unsigned x = foo(); |
| 238 | // if (x == 54) |
| 239 | // ... |
| 240 | // |
| 241 | // The symbolic value stored to 'x' is actually the conjured |
| 242 | // symbol for the call to foo(); the type of that symbol is 'char', |
| 243 | // not unsigned. |
| 244 | const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int); |
| 245 | |
| 246 | if (isa<Loc>(V)) |
| 247 | return loc::ConcreteInt(NewV); |
| 248 | else |
| 249 | return nonloc::ConcreteInt(NewV); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return V; |
| 255 | } |
| 256 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 257 | ProgramStateRef ProgramState::BindExpr(const Stmt *S, |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 258 | const LocationContext *LCtx, |
| 259 | SVal V, bool Invalidate) const{ |
| 260 | Environment NewEnv = |
| 261 | getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V, |
| 262 | Invalidate); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 263 | if (NewEnv == Env) |
| 264 | return this; |
| 265 | |
| 266 | ProgramState NewSt = *this; |
| 267 | NewSt.Env = NewEnv; |
| 268 | return getStateManager().getPersistentState(NewSt); |
| 269 | } |
| 270 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 271 | ProgramStateRef |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 272 | ProgramState::bindExprAndLocation(const Stmt *S, const LocationContext *LCtx, |
| 273 | SVal location, |
| 274 | SVal V) const { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 275 | Environment NewEnv = |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 276 | getStateManager().EnvMgr.bindExprAndLocation(Env, |
| 277 | EnvironmentEntry(S, LCtx), |
| 278 | location, V); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 279 | |
| 280 | if (NewEnv == Env) |
| 281 | return this; |
| 282 | |
| 283 | ProgramState NewSt = *this; |
| 284 | NewSt.Env = NewEnv; |
| 285 | return getStateManager().getPersistentState(NewSt); |
| 286 | } |
| 287 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 288 | ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx, |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 289 | DefinedOrUnknownSVal UpperBound, |
Anna Zaks | af5f550 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 290 | bool Assumption, |
| 291 | QualType indexTy) const { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 292 | if (Idx.isUnknown() || UpperBound.isUnknown()) |
| 293 | return this; |
| 294 | |
| 295 | // Build an expression for 0 <= Idx < UpperBound. |
| 296 | // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed. |
| 297 | // FIXME: This should probably be part of SValBuilder. |
| 298 | ProgramStateManager &SM = getStateManager(); |
| 299 | SValBuilder &svalBuilder = SM.getSValBuilder(); |
| 300 | ASTContext &Ctx = svalBuilder.getContext(); |
| 301 | |
| 302 | // Get the offset: the minimum value of the array index type. |
| 303 | BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); |
| 304 | // FIXME: This should be using ValueManager::ArrayindexTy...somehow. |
Anna Zaks | af5f550 | 2012-01-30 06:42:48 +0000 | [diff] [blame] | 305 | if (indexTy.isNull()) |
| 306 | indexTy = Ctx.IntTy; |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 307 | nonloc::ConcreteInt Min(BVF.getMinValue(indexTy)); |
| 308 | |
| 309 | // Adjust the index. |
| 310 | SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add, |
| 311 | cast<NonLoc>(Idx), Min, indexTy); |
| 312 | if (newIdx.isUnknownOrUndef()) |
| 313 | return this; |
| 314 | |
| 315 | // Adjust the upper bound. |
| 316 | SVal newBound = |
| 317 | svalBuilder.evalBinOpNN(this, BO_Add, cast<NonLoc>(UpperBound), |
| 318 | Min, indexTy); |
| 319 | |
| 320 | if (newBound.isUnknownOrUndef()) |
| 321 | return this; |
| 322 | |
| 323 | // Build the actual comparison. |
| 324 | SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, |
| 325 | cast<NonLoc>(newIdx), cast<NonLoc>(newBound), |
| 326 | Ctx.IntTy); |
| 327 | if (inBound.isUnknownOrUndef()) |
| 328 | return this; |
| 329 | |
| 330 | // Finally, let the constraint manager take care of it. |
| 331 | ConstraintManager &CM = SM.getConstraintManager(); |
| 332 | return CM.assume(this, cast<DefinedSVal>(inBound), Assumption); |
| 333 | } |
| 334 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 335 | ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 336 | ProgramState State(this, |
| 337 | EnvMgr.getInitialEnvironment(), |
| 338 | StoreMgr->getInitialStore(InitLoc), |
| 339 | GDMFactory.getEmptyMap()); |
| 340 | |
| 341 | return getPersistentState(State); |
| 342 | } |
| 343 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 344 | ProgramStateRef ProgramStateManager::getPersistentStateWithGDM( |
| 345 | ProgramStateRef FromState, |
| 346 | ProgramStateRef GDMState) { |
Argyrios Kyrtzidis | a5888f6 | 2012-01-31 02:23:28 +0000 | [diff] [blame] | 347 | ProgramState NewState(*FromState); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 348 | NewState.GDM = GDMState->GDM; |
| 349 | return getPersistentState(NewState); |
| 350 | } |
| 351 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 352 | ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 353 | |
| 354 | llvm::FoldingSetNodeID ID; |
| 355 | State.Profile(ID); |
| 356 | void *InsertPos; |
| 357 | |
| 358 | if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
| 359 | return I; |
| 360 | |
| 361 | ProgramState *newState = 0; |
| 362 | if (!freeStates.empty()) { |
| 363 | newState = freeStates.back(); |
| 364 | freeStates.pop_back(); |
| 365 | } |
| 366 | else { |
| 367 | newState = (ProgramState*) Alloc.Allocate<ProgramState>(); |
| 368 | } |
| 369 | new (newState) ProgramState(State); |
| 370 | StateSet.InsertNode(newState, InsertPos); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 371 | return newState; |
| 372 | } |
| 373 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 374 | ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const { |
Argyrios Kyrtzidis | a5888f6 | 2012-01-31 02:23:28 +0000 | [diff] [blame] | 375 | ProgramState NewSt(*this); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 376 | NewSt.setStore(store); |
| 377 | return getStateManager().getPersistentState(NewSt); |
| 378 | } |
| 379 | |
| 380 | void ProgramState::setStore(const StoreRef &newStore) { |
| 381 | Store newStoreStore = newStore.getStore(); |
| 382 | if (newStoreStore) |
| 383 | stateMgr->getStoreManager().incrementReferenceCount(newStoreStore); |
| 384 | if (store) |
| 385 | stateMgr->getStoreManager().decrementReferenceCount(store); |
| 386 | store = newStoreStore; |
| 387 | } |
| 388 | |
| 389 | //===----------------------------------------------------------------------===// |
| 390 | // State pretty-printing. |
| 391 | //===----------------------------------------------------------------------===// |
| 392 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 393 | void ProgramState::print(raw_ostream &Out, |
Jordy Rose | dbd658e | 2011-08-28 19:11:56 +0000 | [diff] [blame] | 394 | const char *NL, const char *Sep) const { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 395 | // Print the store. |
| 396 | ProgramStateManager &Mgr = getStateManager(); |
Jordy Rose | dbd658e | 2011-08-28 19:11:56 +0000 | [diff] [blame] | 397 | Mgr.getStoreManager().print(getStore(), Out, NL, Sep); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 399 | // Print out the environment. |
| 400 | Env.print(Out, NL, Sep); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 402 | // Print out the constraints. |
Jordy Rose | dbd658e | 2011-08-28 19:11:56 +0000 | [diff] [blame] | 403 | Mgr.getConstraintManager().print(this, Out, NL, Sep); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 404 | |
| 405 | // Print checker-specific data. |
Jordy Rose | dbd658e | 2011-08-28 19:11:56 +0000 | [diff] [blame] | 406 | Mgr.getOwningEngine()->printState(Out, this, NL, Sep); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 409 | void ProgramState::printDOT(raw_ostream &Out) const { |
| 410 | print(Out, "\\l", "\\|"); |
Anna Zaks | d016785 | 2011-12-05 18:58:08 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void ProgramState::dump() const { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 414 | print(llvm::errs()); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Anna Zaks | be97b7e | 2012-01-20 00:11:16 +0000 | [diff] [blame] | 417 | void ProgramState::printTaint(raw_ostream &Out, |
| 418 | const char *NL, const char *Sep) const { |
| 419 | TaintMapImpl TM = get<TaintMap>(); |
| 420 | |
| 421 | if (!TM.isEmpty()) |
| 422 | Out <<"Tainted Symbols:" << NL; |
| 423 | |
| 424 | for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) { |
| 425 | Out << I->first << " : " << I->second << NL; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void ProgramState::dumpTaint() const { |
| 430 | printTaint(llvm::errs()); |
| 431 | } |
| 432 | |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 433 | //===----------------------------------------------------------------------===// |
| 434 | // Generic Data Map. |
| 435 | //===----------------------------------------------------------------------===// |
| 436 | |
| 437 | void *const* ProgramState::FindGDM(void *K) const { |
| 438 | return GDM.lookup(K); |
| 439 | } |
| 440 | |
| 441 | void* |
| 442 | ProgramStateManager::FindGDMContext(void *K, |
| 443 | void *(*CreateContext)(llvm::BumpPtrAllocator&), |
| 444 | void (*DeleteContext)(void*)) { |
| 445 | |
| 446 | std::pair<void*, void (*)(void*)>& p = GDMContexts[K]; |
| 447 | if (!p.first) { |
| 448 | p.first = CreateContext(Alloc); |
| 449 | p.second = DeleteContext; |
| 450 | } |
| 451 | |
| 452 | return p.first; |
| 453 | } |
| 454 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 455 | ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){ |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 456 | ProgramState::GenericDataMap M1 = St->getGDM(); |
| 457 | ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data); |
| 458 | |
| 459 | if (M1 == M2) |
| 460 | return St; |
| 461 | |
| 462 | ProgramState NewSt = *St; |
| 463 | NewSt.GDM = M2; |
| 464 | return getPersistentState(NewSt); |
| 465 | } |
| 466 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 467 | ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) { |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 468 | ProgramState::GenericDataMap OldM = state->getGDM(); |
| 469 | ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key); |
| 470 | |
| 471 | if (NewM == OldM) |
| 472 | return state; |
| 473 | |
| 474 | ProgramState NewState = *state; |
| 475 | NewState.GDM = NewM; |
| 476 | return getPersistentState(NewState); |
| 477 | } |
| 478 | |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 479 | bool ScanReachableSymbols::scan(nonloc::CompoundVal val) { |
| 480 | for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I) |
| 481 | if (!scan(*I)) |
| 482 | return false; |
| 483 | |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | bool ScanReachableSymbols::scan(const SymExpr *sym) { |
| 488 | unsigned &isVisited = visited[sym]; |
| 489 | if (isVisited) |
| 490 | return true; |
| 491 | isVisited = 1; |
| 492 | |
Anna Zaks | 76462f0 | 2011-12-05 18:58:25 +0000 | [diff] [blame] | 493 | if (!visitor.VisitSymbol(sym)) |
| 494 | return false; |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 495 | |
Anna Zaks | a91efb1 | 2011-12-06 23:12:38 +0000 | [diff] [blame] | 496 | // TODO: should be rewritten using SymExpr::symbol_iterator. |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 497 | switch (sym->getKind()) { |
| 498 | case SymExpr::RegionValueKind: |
| 499 | case SymExpr::ConjuredKind: |
| 500 | case SymExpr::DerivedKind: |
| 501 | case SymExpr::ExtentKind: |
| 502 | case SymExpr::MetadataKind: |
| 503 | break; |
Anna Zaks | aace9ef | 2011-12-06 23:12:27 +0000 | [diff] [blame] | 504 | case SymExpr::CastSymbolKind: |
| 505 | return scan(cast<SymbolCast>(sym)->getOperand()); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 506 | case SymExpr::SymIntKind: |
| 507 | return scan(cast<SymIntExpr>(sym)->getLHS()); |
Anna Zaks | 24d052c | 2011-12-10 23:36:51 +0000 | [diff] [blame] | 508 | case SymExpr::IntSymKind: |
| 509 | return scan(cast<IntSymExpr>(sym)->getRHS()); |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 510 | case SymExpr::SymSymKind: { |
| 511 | const SymSymExpr *x = cast<SymSymExpr>(sym); |
| 512 | return scan(x->getLHS()) && scan(x->getRHS()); |
| 513 | } |
| 514 | } |
| 515 | return true; |
| 516 | } |
| 517 | |
| 518 | bool ScanReachableSymbols::scan(SVal val) { |
| 519 | if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val)) |
| 520 | return scan(X->getRegion()); |
| 521 | |
Jordan Rose | 0d53ab4 | 2012-08-08 18:23:31 +0000 | [diff] [blame] | 522 | if (nonloc::LazyCompoundVal *X = dyn_cast<nonloc::LazyCompoundVal>(&val)) |
| 523 | return scan(X->getRegion()); |
| 524 | |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 525 | if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val)) |
| 526 | return scan(X->getLoc()); |
| 527 | |
| 528 | if (SymbolRef Sym = val.getAsSymbol()) |
| 529 | return scan(Sym); |
| 530 | |
| 531 | if (const SymExpr *Sym = val.getAsSymbolicExpression()) |
| 532 | return scan(Sym); |
| 533 | |
| 534 | if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val)) |
| 535 | return scan(*X); |
| 536 | |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | bool ScanReachableSymbols::scan(const MemRegion *R) { |
| 541 | if (isa<MemSpaceRegion>(R)) |
| 542 | return true; |
| 543 | |
| 544 | unsigned &isVisited = visited[R]; |
| 545 | if (isVisited) |
| 546 | return true; |
| 547 | isVisited = 1; |
Ted Kremenek | 7f9b1d9 | 2012-02-21 00:46:29 +0000 | [diff] [blame] | 548 | |
| 549 | |
| 550 | if (!visitor.VisitMemRegion(R)) |
| 551 | return false; |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 552 | |
| 553 | // If this is a symbolic region, visit the symbol for the region. |
| 554 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) |
| 555 | if (!visitor.VisitSymbol(SR->getSymbol())) |
| 556 | return false; |
| 557 | |
| 558 | // If this is a subregion, also visit the parent regions. |
Jordan Rose | e0d24eb | 2012-08-08 18:23:27 +0000 | [diff] [blame] | 559 | if (const SubRegion *SR = dyn_cast<SubRegion>(R)) { |
| 560 | const MemRegion *Super = SR->getSuperRegion(); |
| 561 | if (!scan(Super)) |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 562 | return false; |
| 563 | |
Jordan Rose | e0d24eb | 2012-08-08 18:23:27 +0000 | [diff] [blame] | 564 | // When we reach the topmost region, scan all symbols in it. |
| 565 | if (isa<MemSpaceRegion>(Super)) { |
| 566 | StoreManager &StoreMgr = state->getStateManager().getStoreManager(); |
| 567 | if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this)) |
| 568 | return false; |
| 569 | } |
| 570 | } |
| 571 | |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 572 | // Regions captured by a block are also implicitly reachable. |
| 573 | if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) { |
| 574 | BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(), |
| 575 | E = BDR->referenced_vars_end(); |
| 576 | for ( ; I != E; ++I) { |
| 577 | if (!scan(I.getCapturedRegion())) |
| 578 | return false; |
| 579 | } |
| 580 | } |
| 581 | |
Jordan Rose | e0d24eb | 2012-08-08 18:23:27 +0000 | [diff] [blame] | 582 | return true; |
Ted Kremenek | 2fde35d | 2011-08-15 23:05:22 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const { |
| 586 | ScanReachableSymbols S(this, visitor); |
| 587 | return S.scan(val); |
| 588 | } |
| 589 | |
| 590 | bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E, |
| 591 | SymbolVisitor &visitor) const { |
| 592 | ScanReachableSymbols S(this, visitor); |
| 593 | for ( ; I != E; ++I) { |
| 594 | if (!S.scan(*I)) |
| 595 | return false; |
| 596 | } |
| 597 | return true; |
| 598 | } |
| 599 | |
| 600 | bool ProgramState::scanReachableSymbols(const MemRegion * const *I, |
| 601 | const MemRegion * const *E, |
| 602 | SymbolVisitor &visitor) const { |
| 603 | ScanReachableSymbols S(this, visitor); |
| 604 | for ( ; I != E; ++I) { |
| 605 | if (!S.scan(*I)) |
| 606 | return false; |
| 607 | } |
| 608 | return true; |
| 609 | } |
Anna Zaks | ceac1d6 | 2011-11-16 19:58:10 +0000 | [diff] [blame] | 610 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 611 | ProgramStateRef ProgramState::addTaint(const Stmt *S, |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 612 | const LocationContext *LCtx, |
Anna Zaks | ceac1d6 | 2011-11-16 19:58:10 +0000 | [diff] [blame] | 613 | TaintTagType Kind) const { |
Anna Zaks | d3d8548 | 2011-12-16 18:28:50 +0000 | [diff] [blame] | 614 | if (const Expr *E = dyn_cast_or_null<Expr>(S)) |
| 615 | S = E->IgnoreParens(); |
| 616 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 617 | SymbolRef Sym = getSVal(S, LCtx).getAsSymbol(); |
Anna Zaks | e55a22b | 2011-12-14 00:55:58 +0000 | [diff] [blame] | 618 | if (Sym) |
| 619 | return addTaint(Sym, Kind); |
| 620 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 621 | const MemRegion *R = getSVal(S, LCtx).getAsRegion(); |
Anna Zaks | 28fd98d | 2011-12-14 00:56:15 +0000 | [diff] [blame] | 622 | addTaint(R, Kind); |
Anna Zaks | e55a22b | 2011-12-14 00:55:58 +0000 | [diff] [blame] | 623 | |
| 624 | // Cannot add taint, so just return the state. |
| 625 | return this; |
Anna Zaks | ceac1d6 | 2011-11-16 19:58:10 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 628 | ProgramStateRef ProgramState::addTaint(const MemRegion *R, |
Anna Zaks | 28fd98d | 2011-12-14 00:56:15 +0000 | [diff] [blame] | 629 | TaintTagType Kind) const { |
| 630 | if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R)) |
| 631 | return addTaint(SR->getSymbol(), Kind); |
| 632 | return this; |
| 633 | } |
| 634 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 635 | ProgramStateRef ProgramState::addTaint(SymbolRef Sym, |
Anna Zaks | ceac1d6 | 2011-11-16 19:58:10 +0000 | [diff] [blame] | 636 | TaintTagType Kind) const { |
Anna Zaks | be97b7e | 2012-01-20 00:11:16 +0000 | [diff] [blame] | 637 | // If this is a symbol cast, remove the cast before adding the taint. Taint |
| 638 | // is cast agnostic. |
| 639 | while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym)) |
| 640 | Sym = SC->getOperand(); |
| 641 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 642 | ProgramStateRef NewState = set<TaintMap>(Sym, Kind); |
Anna Zaks | ceac1d6 | 2011-11-16 19:58:10 +0000 | [diff] [blame] | 643 | assert(NewState); |
| 644 | return NewState; |
| 645 | } |
| 646 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 647 | bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx, |
| 648 | TaintTagType Kind) const { |
Anna Zaks | d3d8548 | 2011-12-16 18:28:50 +0000 | [diff] [blame] | 649 | if (const Expr *E = dyn_cast_or_null<Expr>(S)) |
| 650 | S = E->IgnoreParens(); |
| 651 | |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 652 | SVal val = getSVal(S, LCtx); |
Anna Zaks | b39c5b4 | 2011-12-07 19:56:13 +0000 | [diff] [blame] | 653 | return isTainted(val, Kind); |
Anna Zaks | ceac1d6 | 2011-11-16 19:58:10 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | bool ProgramState::isTainted(SVal V, TaintTagType Kind) const { |
Anna Zaks | dcf06fa | 2011-12-07 01:09:52 +0000 | [diff] [blame] | 657 | if (const SymExpr *Sym = V.getAsSymExpr()) |
| 658 | return isTainted(Sym, Kind); |
Anna Zaks | 777d706 | 2011-12-17 00:26:29 +0000 | [diff] [blame] | 659 | if (const MemRegion *Reg = V.getAsRegion()) |
| 660 | return isTainted(Reg, Kind); |
Anna Zaks | dcf06fa | 2011-12-07 01:09:52 +0000 | [diff] [blame] | 661 | return false; |
| 662 | } |
| 663 | |
| 664 | bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const { |
| 665 | if (!Reg) |
| 666 | return false; |
| 667 | |
| 668 | // Element region (array element) is tainted if either the base or the offset |
| 669 | // are tainted. |
| 670 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg)) |
| 671 | return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K); |
| 672 | |
| 673 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) |
| 674 | return isTainted(SR->getSymbol(), K); |
| 675 | |
| 676 | if (const SubRegion *ER = dyn_cast<SubRegion>(Reg)) |
| 677 | return isTainted(ER->getSuperRegion(), K); |
| 678 | |
| 679 | return false; |
Anna Zaks | ceac1d6 | 2011-11-16 19:58:10 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Anna Zaks | 9f03b62 | 2012-01-07 02:33:10 +0000 | [diff] [blame] | 682 | bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const { |
Anna Zaks | ee081c4 | 2011-11-17 22:59:00 +0000 | [diff] [blame] | 683 | if (!Sym) |
| 684 | return false; |
Anna Zaks | 1d1d515 | 2011-12-06 23:12:33 +0000 | [diff] [blame] | 685 | |
Anna Zaks | dcf06fa | 2011-12-07 01:09:52 +0000 | [diff] [blame] | 686 | // Traverse all the symbols this symbol depends on to see if any are tainted. |
Anna Zaks | 1d1d515 | 2011-12-06 23:12:33 +0000 | [diff] [blame] | 687 | bool Tainted = false; |
| 688 | for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end(); |
| 689 | SI != SE; ++SI) { |
Jordan Rose | 24c7f98 | 2012-09-08 01:24:53 +0000 | [diff] [blame] | 690 | if (!isa<SymbolData>(*SI)) |
| 691 | continue; |
| 692 | |
Anna Zaks | 1d1d515 | 2011-12-06 23:12:33 +0000 | [diff] [blame] | 693 | const TaintTagType *Tag = get<TaintMap>(*SI); |
| 694 | Tainted = (Tag && *Tag == Kind); |
Anna Zaks | ee081c4 | 2011-11-17 22:59:00 +0000 | [diff] [blame] | 695 | |
Anna Zaks | 1d1d515 | 2011-12-06 23:12:33 +0000 | [diff] [blame] | 696 | // If this is a SymbolDerived with a tainted parent, it's also tainted. |
| 697 | if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI)) |
| 698 | Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind); |
Anna Zaks | 5fc7def | 2011-12-08 22:38:43 +0000 | [diff] [blame] | 699 | |
| 700 | // If memory region is tainted, data is also tainted. |
| 701 | if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI)) |
| 702 | Tainted = Tainted || isTainted(SRV->getRegion(), Kind); |
| 703 | |
Anna Zaks | be97b7e | 2012-01-20 00:11:16 +0000 | [diff] [blame] | 704 | // If If this is a SymbolCast from a tainted value, it's also tainted. |
| 705 | if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI)) |
| 706 | Tainted = Tainted || isTainted(SC->getOperand(), Kind); |
| 707 | |
Anna Zaks | 1d1d515 | 2011-12-06 23:12:33 +0000 | [diff] [blame] | 708 | if (Tainted) |
| 709 | return true; |
Anna Zaks | ceac1d6 | 2011-11-16 19:58:10 +0000 | [diff] [blame] | 710 | } |
Anna Zaks | 1d1d515 | 2011-12-06 23:12:33 +0000 | [diff] [blame] | 711 | |
| 712 | return Tainted; |
Anna Zaks | ceac1d6 | 2011-11-16 19:58:10 +0000 | [diff] [blame] | 713 | } |
Anna Zaks | 2d18419 | 2012-07-30 20:31:29 +0000 | [diff] [blame] | 714 | |
Anna Zaks | 148fee9 | 2012-08-03 21:43:37 +0000 | [diff] [blame] | 715 | /// The GDM component containing the dynamic type info. This is a map from a |
| 716 | /// symbol to it's most likely type. |
| 717 | namespace clang { |
| 718 | namespace ento { |
Jordan Rose | f41c0dd | 2012-08-13 23:59:07 +0000 | [diff] [blame] | 719 | typedef llvm::ImmutableMap<const MemRegion *, DynamicTypeInfo> DynamicTypeMap; |
Anna Zaks | 148fee9 | 2012-08-03 21:43:37 +0000 | [diff] [blame] | 720 | template<> struct ProgramStateTrait<DynamicTypeMap> |
Jordan Rose | f41c0dd | 2012-08-13 23:59:07 +0000 | [diff] [blame] | 721 | : public ProgramStatePartialTrait<DynamicTypeMap> { |
Anna Zaks | 148fee9 | 2012-08-03 21:43:37 +0000 | [diff] [blame] | 722 | static void *GDMIndex() { static int index; return &index; } |
| 723 | }; |
| 724 | }} |
| 725 | |
Anna Zaks | 2d18419 | 2012-07-30 20:31:29 +0000 | [diff] [blame] | 726 | DynamicTypeInfo ProgramState::getDynamicTypeInfo(const MemRegion *Reg) const { |
Jordan Rose | 0ad36ba | 2012-08-15 00:51:56 +0000 | [diff] [blame] | 727 | Reg = Reg->StripCasts(); |
| 728 | |
Jordan Rose | f41c0dd | 2012-08-13 23:59:07 +0000 | [diff] [blame] | 729 | // Look up the dynamic type in the GDM. |
| 730 | const DynamicTypeInfo *GDMType = get<DynamicTypeMap>(Reg); |
| 731 | if (GDMType) |
| 732 | return *GDMType; |
| 733 | |
| 734 | // Otherwise, fall back to what we know about the region. |
Jordan Rose | 0ad36ba | 2012-08-15 00:51:56 +0000 | [diff] [blame] | 735 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(Reg)) |
| 736 | return DynamicTypeInfo(TR->getLocationType(), /*CanBeSubclass=*/false); |
Anna Zaks | 148fee9 | 2012-08-03 21:43:37 +0000 | [diff] [blame] | 737 | |
| 738 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) { |
| 739 | SymbolRef Sym = SR->getSymbol(); |
Anna Zaks | 148fee9 | 2012-08-03 21:43:37 +0000 | [diff] [blame] | 740 | return DynamicTypeInfo(Sym->getType(getStateManager().getContext())); |
| 741 | } |
Jordan Rose | f41c0dd | 2012-08-13 23:59:07 +0000 | [diff] [blame] | 742 | |
Anna Zaks | 2d18419 | 2012-07-30 20:31:29 +0000 | [diff] [blame] | 743 | return DynamicTypeInfo(); |
| 744 | } |
Anna Zaks | 148fee9 | 2012-08-03 21:43:37 +0000 | [diff] [blame] | 745 | |
Anna Zaks | d4fe57f | 2012-08-09 21:02:45 +0000 | [diff] [blame] | 746 | ProgramStateRef ProgramState::setDynamicTypeInfo(const MemRegion *Reg, |
Anna Zaks | c7ecc43 | 2012-08-06 23:25:39 +0000 | [diff] [blame] | 747 | DynamicTypeInfo NewTy) const { |
Jordan Rose | 0ad36ba | 2012-08-15 00:51:56 +0000 | [diff] [blame] | 748 | Reg = Reg->StripCasts(); |
Jordan Rose | f41c0dd | 2012-08-13 23:59:07 +0000 | [diff] [blame] | 749 | ProgramStateRef NewState = set<DynamicTypeMap>(Reg, NewTy); |
| 750 | assert(NewState); |
| 751 | return NewState; |
Anna Zaks | 148fee9 | 2012-08-03 21:43:37 +0000 | [diff] [blame] | 752 | } |