Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 1 | //=== OSAtomicChecker.cpp - OSAtomic functions evaluator --------*- 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 checker evaluates OSAtomic functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | f029366 | 2011-02-28 01:27:02 +0000 | [diff] [blame] | 14 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | f029366 | 2011-02-28 01:27:02 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 17 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Builtins.h" |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 21 | using namespace ento; |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 25 | class OSAtomicChecker : public Checker<eval::InlineCall> { |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 26 | public: |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 27 | bool inlineCall(const CallExpr *CE, ExprEngine &Eng, |
| 28 | ExplodedNode *Pred, ExplodedNodeSet &Dst) const; |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 29 | |
| 30 | private: |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 31 | bool evalOSAtomicCompareAndSwap(const CallExpr *CE, |
| 32 | ExprEngine &Eng, |
| 33 | ExplodedNode *Pred, |
| 34 | ExplodedNodeSet &Dst) const; |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 35 | }; |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 38 | bool OSAtomicChecker::inlineCall(const CallExpr *CE, |
| 39 | ExprEngine &Eng, |
| 40 | ExplodedNode *Pred, |
| 41 | ExplodedNodeSet &Dst) const { |
| 42 | const ProgramState *state = Pred->getState(); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 43 | const Expr *Callee = CE->getCallee(); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 44 | SVal L = state->getSVal(Callee); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 46 | const FunctionDecl *FD = L.getAsFunctionDecl(); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 47 | if (!FD) |
| 48 | return false; |
| 49 | |
Ted Kremenek | a0f7727 | 2009-12-16 06:03:24 +0000 | [diff] [blame] | 50 | const IdentifierInfo *II = FD->getIdentifier(); |
| 51 | if (!II) |
| 52 | return false; |
| 53 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 54 | StringRef FName(II->getName()); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 55 | |
| 56 | // Check for compare and swap. |
Ted Kremenek | a0f7727 | 2009-12-16 06:03:24 +0000 | [diff] [blame] | 57 | if (FName.startswith("OSAtomicCompareAndSwap") || |
| 58 | FName.startswith("objc_atomicCompareAndSwap")) |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 59 | return evalOSAtomicCompareAndSwap(CE, Eng, Pred, Dst); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 60 | |
| 61 | // FIXME: Other atomics. |
| 62 | return false; |
| 63 | } |
| 64 | |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 65 | bool OSAtomicChecker::evalOSAtomicCompareAndSwap(const CallExpr *CE, |
| 66 | ExprEngine &Eng, |
| 67 | ExplodedNode *Pred, |
| 68 | ExplodedNodeSet &Dst) const { |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 69 | // Not enough arguments to match OSAtomicCompareAndSwap? |
| 70 | if (CE->getNumArgs() != 3) |
| 71 | return false; |
| 72 | |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 73 | ASTContext &Ctx = Eng.getContext(); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 74 | const Expr *oldValueExpr = CE->getArg(0); |
| 75 | QualType oldValueType = Ctx.getCanonicalType(oldValueExpr->getType()); |
| 76 | |
| 77 | const Expr *newValueExpr = CE->getArg(1); |
| 78 | QualType newValueType = Ctx.getCanonicalType(newValueExpr->getType()); |
| 79 | |
| 80 | // Do the types of 'oldValue' and 'newValue' match? |
| 81 | if (oldValueType != newValueType) |
| 82 | return false; |
| 83 | |
| 84 | const Expr *theValueExpr = CE->getArg(2); |
| 85 | const PointerType *theValueType=theValueExpr->getType()->getAs<PointerType>(); |
| 86 | |
| 87 | // theValueType not a pointer? |
| 88 | if (!theValueType) |
| 89 | return false; |
| 90 | |
| 91 | QualType theValueTypePointee = |
| 92 | Ctx.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType(); |
| 93 | |
| 94 | // The pointee must match newValueType and oldValueType. |
| 95 | if (theValueTypePointee != newValueType) |
| 96 | return false; |
| 97 | |
Ted Kremenek | ca80453 | 2011-08-12 23:04:46 +0000 | [diff] [blame] | 98 | static SimpleProgramPointTag OSAtomicLoadTag("OSAtomicChecker : Load"); |
| 99 | static SimpleProgramPointTag OSAtomicStoreTag("OSAtomicChecker : Store"); |
| 100 | |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 101 | // Load 'theValue'. |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 102 | const ProgramState *state = Pred->getState(); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 103 | ExplodedNodeSet Tmp; |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 104 | SVal location = state->getSVal(theValueExpr); |
Zhongxing Xu | 71f219a | 2010-06-23 02:06:56 +0000 | [diff] [blame] | 105 | // Here we should use the value type of the region as the load type, because |
| 106 | // we are simulating the semantics of the function, not the semantics of |
Zhongxing Xu | 2470446 | 2010-06-23 02:12:00 +0000 | [diff] [blame] | 107 | // passing argument. So the type of theValue expr is not we are loading. |
| 108 | // But usually the type of the varregion is not the type we want either, |
| 109 | // we still need to do a CastRetrievedVal in store manager. So actually this |
| 110 | // LoadTy specifying can be omitted. But we put it here to emphasize the |
| 111 | // semantics. |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 112 | QualType LoadTy; |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 113 | if (const TypedValueRegion *TR = |
| 114 | dyn_cast_or_null<TypedValueRegion>(location.getAsRegion())) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 115 | LoadTy = TR->getValueType(); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 116 | } |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 117 | Eng.evalLoad(Tmp, theValueExpr, Pred, |
Ted Kremenek | ca80453 | 2011-08-12 23:04:46 +0000 | [diff] [blame] | 118 | state, location, &OSAtomicLoadTag, LoadTy); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 119 | |
| 120 | if (Tmp.empty()) { |
Anna Zaks | f185cc1 | 2011-10-24 21:19:43 +0000 | [diff] [blame^] | 121 | // If no nodes were generated, other checkers must have generated sinks. |
| 122 | // We return an empty Dst. |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
| 126 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); |
| 127 | I != E; ++I) { |
| 128 | |
| 129 | ExplodedNode *N = *I; |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 130 | const ProgramState *stateLoad = N->getState(); |
Ted Kremenek | 6b4f567 | 2011-04-27 05:34:09 +0000 | [diff] [blame] | 131 | |
| 132 | // Use direct bindings from the environment since we are forcing a load |
| 133 | // from a location that the Environment would typically not be used |
| 134 | // to bind a value. |
| 135 | SVal theValueVal_untested = stateLoad->getSVal(theValueExpr, true); |
| 136 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 137 | SVal oldValueVal_untested = stateLoad->getSVal(oldValueExpr); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 138 | |
| 139 | // FIXME: Issue an error. |
| 140 | if (theValueVal_untested.isUndef() || oldValueVal_untested.isUndef()) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | DefinedOrUnknownSVal theValueVal = |
| 145 | cast<DefinedOrUnknownSVal>(theValueVal_untested); |
| 146 | DefinedOrUnknownSVal oldValueVal = |
| 147 | cast<DefinedOrUnknownSVal>(oldValueVal_untested); |
| 148 | |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 149 | SValBuilder &svalBuilder = Eng.getSValBuilder(); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 150 | |
| 151 | // Perform the comparison. |
Ted Kremenek | ca80453 | 2011-08-12 23:04:46 +0000 | [diff] [blame] | 152 | DefinedOrUnknownSVal Cmp = |
| 153 | svalBuilder.evalEQ(stateLoad,theValueVal,oldValueVal); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 154 | |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 155 | const ProgramState *stateEqual = stateLoad->assume(Cmp, true); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 156 | |
| 157 | // Were they equal? |
| 158 | if (stateEqual) { |
| 159 | // Perform the store. |
| 160 | ExplodedNodeSet TmpStore; |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 161 | SVal val = stateEqual->getSVal(newValueExpr); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 162 | |
| 163 | // Handle implicit value casts. |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 164 | if (const TypedValueRegion *R = |
| 165 | dyn_cast_or_null<TypedValueRegion>(location.getAsRegion())) { |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 166 | val = svalBuilder.evalCast(val,R->getValueType(), newValueExpr->getType()); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 169 | Eng.evalStore(TmpStore, NULL, theValueExpr, N, |
Ted Kremenek | ca80453 | 2011-08-12 23:04:46 +0000 | [diff] [blame] | 170 | stateEqual, location, val, &OSAtomicStoreTag); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 171 | |
| 172 | if (TmpStore.empty()) { |
Anna Zaks | f185cc1 | 2011-10-24 21:19:43 +0000 | [diff] [blame^] | 173 | // If no nodes were generated, other checkers must have generated sinks. |
| 174 | // We return an empty Dst. |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 175 | return true; |
| 176 | } |
Anna Zaks | f185cc1 | 2011-10-24 21:19:43 +0000 | [diff] [blame^] | 177 | |
| 178 | PureStmtNodeBuilder B(TmpStore, Dst, Eng.getBuilderContext()); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 179 | // Now bind the result of the comparison. |
| 180 | for (ExplodedNodeSet::iterator I2 = TmpStore.begin(), |
| 181 | E2 = TmpStore.end(); I2 != E2; ++I2) { |
| 182 | ExplodedNode *predNew = *I2; |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 183 | const ProgramState *stateNew = predNew->getState(); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 184 | // Check for 'void' return type if we have a bogus function prototype. |
| 185 | SVal Res = UnknownVal(); |
| 186 | QualType T = CE->getType(); |
| 187 | if (!T->isVoidType()) |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 188 | Res = Eng.getSValBuilder().makeTruthVal(true, T); |
Anna Zaks | f185cc1 | 2011-10-24 21:19:43 +0000 | [diff] [blame^] | 189 | B.generateNode(CE, predNew, stateNew->BindExpr(CE, Res), false, this); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | // Were they not equal? |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 194 | if (const ProgramState *stateNotEqual = stateLoad->assume(Cmp, false)) { |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame] | 195 | // Check for 'void' return type if we have a bogus function prototype. |
| 196 | SVal Res = UnknownVal(); |
| 197 | QualType T = CE->getType(); |
| 198 | if (!T->isVoidType()) |
Anna Zaks | dff6ef9 | 2011-10-05 23:37:30 +0000 | [diff] [blame] | 199 | Res = Eng.getSValBuilder().makeTruthVal(false, CE->getType()); |
Anna Zaks | f185cc1 | 2011-10-24 21:19:43 +0000 | [diff] [blame^] | 200 | PureStmtNodeBuilder B(N, Dst, Eng.getBuilderContext()); |
| 201 | B.generateNode(CE, N, stateNotEqual->BindExpr(CE, Res), false, this); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | return true; |
| 206 | } |
Argyrios Kyrtzidis | f029366 | 2011-02-28 01:27:02 +0000 | [diff] [blame] | 207 | |
| 208 | void ento::registerOSAtomicChecker(CheckerManager &mgr) { |
| 209 | mgr.registerChecker<OSAtomicChecker>(); |
| 210 | } |