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