blob: 44dcdd4230713bffcd98f303074b93d7909bc5ba [file] [log] [blame]
Zhongxing Xu1ec4e972009-12-09 12:23:28 +00001//=== 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"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000015#include "clang/Checker/PathSensitive/Checker.h"
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000016#include "clang/Basic/Builtins.h"
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000017
18using namespace clang;
19
20namespace {
21
22class OSAtomicChecker : public Checker {
23public:
24 static void *getTag() { static int tag = 0; return &tag; }
25 virtual bool EvalCallExpr(CheckerContext &C, const CallExpr *CE);
26
27private:
28 bool EvalOSAtomicCompareAndSwap(CheckerContext &C, const CallExpr *CE);
29};
30
31}
32
33void clang::RegisterOSAtomicChecker(GRExprEngine &Eng) {
34 Eng.registerCheck(new OSAtomicChecker());
35}
36
37bool OSAtomicChecker::EvalCallExpr(CheckerContext &C,const CallExpr *CE) {
38 const GRState *state = C.getState();
39 const Expr *Callee = CE->getCallee();
Ted Kremenek13976632010-02-08 16:18:51 +000040 SVal L = state->getSVal(Callee);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000041
42 const FunctionDecl* FD = L.getAsFunctionDecl();
43 if (!FD)
44 return false;
45
Ted Kremeneka0f77272009-12-16 06:03:24 +000046 const IdentifierInfo *II = FD->getIdentifier();
47 if (!II)
48 return false;
49
50 llvm::StringRef FName(II->getName());
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000051
52 // Check for compare and swap.
Ted Kremeneka0f77272009-12-16 06:03:24 +000053 if (FName.startswith("OSAtomicCompareAndSwap") ||
54 FName.startswith("objc_atomicCompareAndSwap"))
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000055 return EvalOSAtomicCompareAndSwap(C, CE);
56
57 // FIXME: Other atomics.
58 return false;
59}
60
61bool OSAtomicChecker::EvalOSAtomicCompareAndSwap(CheckerContext &C,
62 const CallExpr *CE) {
63 // Not enough arguments to match OSAtomicCompareAndSwap?
64 if (CE->getNumArgs() != 3)
65 return false;
66
67 ASTContext &Ctx = C.getASTContext();
68 const Expr *oldValueExpr = CE->getArg(0);
69 QualType oldValueType = Ctx.getCanonicalType(oldValueExpr->getType());
70
71 const Expr *newValueExpr = CE->getArg(1);
72 QualType newValueType = Ctx.getCanonicalType(newValueExpr->getType());
73
74 // Do the types of 'oldValue' and 'newValue' match?
75 if (oldValueType != newValueType)
76 return false;
77
78 const Expr *theValueExpr = CE->getArg(2);
79 const PointerType *theValueType=theValueExpr->getType()->getAs<PointerType>();
80
81 // theValueType not a pointer?
82 if (!theValueType)
83 return false;
84
85 QualType theValueTypePointee =
86 Ctx.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
87
88 // The pointee must match newValueType and oldValueType.
89 if (theValueTypePointee != newValueType)
90 return false;
91
92 static unsigned magic_load = 0;
93 static unsigned magic_store = 0;
94
95 const void *OSAtomicLoadTag = &magic_load;
96 const void *OSAtomicStoreTag = &magic_store;
97
98 // Load 'theValue'.
99 GRExprEngine &Engine = C.getEngine();
100 const GRState *state = C.getState();
101 ExplodedNodeSet Tmp;
Ted Kremenek13976632010-02-08 16:18:51 +0000102 SVal location = state->getSVal(theValueExpr);
Zhongxing Xu71f219a2010-06-23 02:06:56 +0000103 // Here we should use the value type of the region as the load type, because
104 // we are simulating the semantics of the function, not the semantics of
105 // passing argument.
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000106 QualType LoadTy;
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000107 if (const TypedRegion *TR =
108 dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
109 LoadTy = TR->getValueType(Ctx);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000110 }
111 Engine.EvalLoad(Tmp, const_cast<Expr *>(theValueExpr), C.getPredecessor(),
112 state, location, OSAtomicLoadTag, LoadTy);
113
114 if (Tmp.empty()) {
115 // If no nodes were generated, other checkers must generated sinks. But
116 // since the builder state was restored, we set it manually to prevent
117 // auto transition.
118 // FIXME: there should be a better approach.
119 C.getNodeBuilder().BuildSinks = true;
120 return true;
121 }
122
123 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end();
124 I != E; ++I) {
125
126 ExplodedNode *N = *I;
127 const GRState *stateLoad = N->getState();
Ted Kremenek13976632010-02-08 16:18:51 +0000128 SVal theValueVal_untested = stateLoad->getSVal(theValueExpr);
129 SVal oldValueVal_untested = stateLoad->getSVal(oldValueExpr);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000130
131 // FIXME: Issue an error.
132 if (theValueVal_untested.isUndef() || oldValueVal_untested.isUndef()) {
133 return false;
134 }
135
136 DefinedOrUnknownSVal theValueVal =
137 cast<DefinedOrUnknownSVal>(theValueVal_untested);
138 DefinedOrUnknownSVal oldValueVal =
139 cast<DefinedOrUnknownSVal>(oldValueVal_untested);
140
141 SValuator &SVator = Engine.getSValuator();
142
143 // Perform the comparison.
144 DefinedOrUnknownSVal Cmp = SVator.EvalEQ(stateLoad,theValueVal,oldValueVal);
145
146 const GRState *stateEqual = stateLoad->Assume(Cmp, true);
147
148 // Were they equal?
149 if (stateEqual) {
150 // Perform the store.
151 ExplodedNodeSet TmpStore;
Ted Kremenek13976632010-02-08 16:18:51 +0000152 SVal val = stateEqual->getSVal(newValueExpr);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000153
154 // Handle implicit value casts.
155 if (const TypedRegion *R =
156 dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +0000157 val = SVator.EvalCast(val,R->getValueType(Ctx),newValueExpr->getType());
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000158 }
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 Kremenekc50e6df2010-01-11 02:33:26 +0000177 // 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 Xu1ec4e972009-12-09 12:23:28 +0000182 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 Kremenekc50e6df2010-01-11 02:33:26 +0000188 // 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 Xu1ec4e972009-12-09 12:23:28 +0000193 C.GenerateNode(stateNotEqual->BindExpr(CE, Res), N);
194 }
195 }
196
197 return true;
198}