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 | |
| 14 | #include "GRExprEngineInternalChecks.h" |
| 15 | #include "clang/Analysis/PathSensitive/Checker.h" |
| 16 | #include "clang/Basic/Builtins.h" |
| 17 | #include "llvm/ADT/StringSwitch.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class OSAtomicChecker : public Checker { |
| 24 | public: |
| 25 | static void *getTag() { static int tag = 0; return &tag; } |
| 26 | virtual bool EvalCallExpr(CheckerContext &C, const CallExpr *CE); |
| 27 | |
| 28 | private: |
| 29 | bool EvalOSAtomicCompareAndSwap(CheckerContext &C, const CallExpr *CE); |
| 30 | }; |
| 31 | |
| 32 | } |
| 33 | |
| 34 | void clang::RegisterOSAtomicChecker(GRExprEngine &Eng) { |
| 35 | Eng.registerCheck(new OSAtomicChecker()); |
| 36 | } |
| 37 | |
| 38 | bool OSAtomicChecker::EvalCallExpr(CheckerContext &C,const CallExpr *CE) { |
| 39 | const GRState *state = C.getState(); |
| 40 | const Expr *Callee = CE->getCallee(); |
| 41 | SVal L = state->getSVal(Callee); |
| 42 | |
| 43 | const FunctionDecl* FD = L.getAsFunctionDecl(); |
| 44 | if (!FD) |
| 45 | return false; |
| 46 | |
Ted Kremenek | a0f7727 | 2009-12-16 06:03:24 +0000 | [diff] [blame] | 47 | const IdentifierInfo *II = FD->getIdentifier(); |
| 48 | if (!II) |
| 49 | return false; |
| 50 | |
| 51 | llvm::StringRef FName(II->getName()); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 52 | |
| 53 | // Check for compare and swap. |
Ted Kremenek | a0f7727 | 2009-12-16 06:03:24 +0000 | [diff] [blame] | 54 | if (FName.startswith("OSAtomicCompareAndSwap") || |
| 55 | FName.startswith("objc_atomicCompareAndSwap")) |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 56 | return EvalOSAtomicCompareAndSwap(C, CE); |
| 57 | |
| 58 | // FIXME: Other atomics. |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | bool OSAtomicChecker::EvalOSAtomicCompareAndSwap(CheckerContext &C, |
| 63 | const CallExpr *CE) { |
| 64 | // Not enough arguments to match OSAtomicCompareAndSwap? |
| 65 | if (CE->getNumArgs() != 3) |
| 66 | return false; |
| 67 | |
| 68 | ASTContext &Ctx = C.getASTContext(); |
| 69 | const Expr *oldValueExpr = CE->getArg(0); |
| 70 | QualType oldValueType = Ctx.getCanonicalType(oldValueExpr->getType()); |
| 71 | |
| 72 | const Expr *newValueExpr = CE->getArg(1); |
| 73 | QualType newValueType = Ctx.getCanonicalType(newValueExpr->getType()); |
| 74 | |
| 75 | // Do the types of 'oldValue' and 'newValue' match? |
| 76 | if (oldValueType != newValueType) |
| 77 | return false; |
| 78 | |
| 79 | const Expr *theValueExpr = CE->getArg(2); |
| 80 | const PointerType *theValueType=theValueExpr->getType()->getAs<PointerType>(); |
| 81 | |
| 82 | // theValueType not a pointer? |
| 83 | if (!theValueType) |
| 84 | return false; |
| 85 | |
| 86 | QualType theValueTypePointee = |
| 87 | Ctx.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType(); |
| 88 | |
| 89 | // The pointee must match newValueType and oldValueType. |
| 90 | if (theValueTypePointee != newValueType) |
| 91 | return false; |
| 92 | |
| 93 | static unsigned magic_load = 0; |
| 94 | static unsigned magic_store = 0; |
| 95 | |
| 96 | const void *OSAtomicLoadTag = &magic_load; |
| 97 | const void *OSAtomicStoreTag = &magic_store; |
| 98 | |
| 99 | // Load 'theValue'. |
| 100 | GRExprEngine &Engine = C.getEngine(); |
| 101 | const GRState *state = C.getState(); |
| 102 | ExplodedNodeSet Tmp; |
| 103 | SVal location = state->getSVal(theValueExpr); |
| 104 | // Here we should use the value type of the region as the load type. |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 105 | QualType LoadTy; |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame^] | 106 | if (const TypedRegion *TR = |
| 107 | dyn_cast_or_null<TypedRegion>(location.getAsRegion())) { |
| 108 | LoadTy = TR->getValueType(Ctx); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 109 | } |
| 110 | Engine.EvalLoad(Tmp, const_cast<Expr *>(theValueExpr), C.getPredecessor(), |
| 111 | state, location, OSAtomicLoadTag, LoadTy); |
| 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(); |
| 127 | SVal theValueVal_untested = stateLoad->getSVal(theValueExpr); |
| 128 | SVal oldValueVal_untested = stateLoad->getSVal(oldValueExpr); |
| 129 | |
| 130 | // FIXME: Issue an error. |
| 131 | if (theValueVal_untested.isUndef() || oldValueVal_untested.isUndef()) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | DefinedOrUnknownSVal theValueVal = |
| 136 | cast<DefinedOrUnknownSVal>(theValueVal_untested); |
| 137 | DefinedOrUnknownSVal oldValueVal = |
| 138 | cast<DefinedOrUnknownSVal>(oldValueVal_untested); |
| 139 | |
| 140 | SValuator &SVator = Engine.getSValuator(); |
| 141 | |
| 142 | // Perform the comparison. |
| 143 | DefinedOrUnknownSVal Cmp = SVator.EvalEQ(stateLoad,theValueVal,oldValueVal); |
| 144 | |
| 145 | const GRState *stateEqual = stateLoad->Assume(Cmp, true); |
| 146 | |
| 147 | // Were they equal? |
| 148 | if (stateEqual) { |
| 149 | // Perform the store. |
| 150 | ExplodedNodeSet TmpStore; |
| 151 | SVal val = stateEqual->getSVal(newValueExpr); |
| 152 | |
| 153 | // Handle implicit value casts. |
| 154 | if (const TypedRegion *R = |
| 155 | dyn_cast_or_null<TypedRegion>(location.getAsRegion())) { |
| 156 | llvm::tie(state, val) = SVator.EvalCast(val, state,R->getValueType(Ctx), |
| 157 | newValueExpr->getType()); |
| 158 | } |
| 159 | |
| 160 | Engine.EvalStore(TmpStore, NULL, const_cast<Expr *>(theValueExpr), N, |
| 161 | stateEqual, location, val, OSAtomicStoreTag); |
| 162 | |
| 163 | if (TmpStore.empty()) { |
| 164 | // If no nodes were generated, other checkers must generated sinks. But |
| 165 | // since the builder state was restored, we set it manually to prevent |
| 166 | // auto transition. |
| 167 | // FIXME: there should be a better approach. |
| 168 | C.getNodeBuilder().BuildSinks = true; |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | // Now bind the result of the comparison. |
| 173 | for (ExplodedNodeSet::iterator I2 = TmpStore.begin(), |
| 174 | E2 = TmpStore.end(); I2 != E2; ++I2) { |
| 175 | ExplodedNode *predNew = *I2; |
| 176 | const GRState *stateNew = predNew->getState(); |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame^] | 177 | // Check for 'void' return type if we have a bogus function prototype. |
| 178 | SVal Res = UnknownVal(); |
| 179 | QualType T = CE->getType(); |
| 180 | if (!T->isVoidType()) |
| 181 | Res = Engine.getValueManager().makeTruthVal(true, T); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 182 | C.GenerateNode(stateNew->BindExpr(CE, Res), predNew); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Were they not equal? |
| 187 | if (const GRState *stateNotEqual = stateLoad->Assume(Cmp, false)) { |
Ted Kremenek | c50e6df | 2010-01-11 02:33:26 +0000 | [diff] [blame^] | 188 | // Check for 'void' return type if we have a bogus function prototype. |
| 189 | SVal Res = UnknownVal(); |
| 190 | QualType T = CE->getType(); |
| 191 | if (!T->isVoidType()) |
| 192 | Res = Engine.getValueManager().makeTruthVal(false, CE->getType()); |
Zhongxing Xu | 1ec4e97 | 2009-12-09 12:23:28 +0000 | [diff] [blame] | 193 | C.GenerateNode(stateNotEqual->BindExpr(CE, Res), N); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return true; |
| 198 | } |