blob: bb266e5659e232250daefdc7a7e3142b7624c772 [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
Argyrios Kyrtzidisf0293662011-02-28 01:27:02 +000014#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisf0293662011-02-28 01:27:02 +000016#include "clang/StaticAnalyzer/Core/CheckerManager.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000018#include "clang/Basic/Builtins.h"
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000019
20using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000021using namespace ento;
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000022
23namespace {
24
Anna Zaksdff6ef92011-10-05 23:37:30 +000025class OSAtomicChecker : public Checker<eval::InlineCall> {
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000026public:
Anna Zaksdff6ef92011-10-05 23:37:30 +000027 bool inlineCall(const CallExpr *CE, ExprEngine &Eng,
28 ExplodedNode *Pred, ExplodedNodeSet &Dst) const;
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000029
30private:
Anna Zaksdff6ef92011-10-05 23:37:30 +000031 bool evalOSAtomicCompareAndSwap(const CallExpr *CE,
32 ExprEngine &Eng,
33 ExplodedNode *Pred,
34 ExplodedNodeSet &Dst) const;
Anna Zaksdff6ef92011-10-05 23:37:30 +000035};
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000036}
37
Anna Zaksdff6ef92011-10-05 23:37:30 +000038bool OSAtomicChecker::inlineCall(const CallExpr *CE,
39 ExprEngine &Eng,
40 ExplodedNode *Pred,
41 ExplodedNodeSet &Dst) const {
42 const ProgramState *state = Pred->getState();
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000043 const Expr *Callee = CE->getCallee();
Ted Kremenek13976632010-02-08 16:18:51 +000044 SVal L = state->getSVal(Callee);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000045
Ted Kremenek9c378f72011-08-12 23:37:29 +000046 const FunctionDecl *FD = L.getAsFunctionDecl();
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000047 if (!FD)
48 return false;
49
Ted Kremeneka0f77272009-12-16 06:03:24 +000050 const IdentifierInfo *II = FD->getIdentifier();
51 if (!II)
52 return false;
53
Chris Lattner5f9e2722011-07-23 10:55:15 +000054 StringRef FName(II->getName());
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000055
56 // Check for compare and swap.
Ted Kremeneka0f77272009-12-16 06:03:24 +000057 if (FName.startswith("OSAtomicCompareAndSwap") ||
58 FName.startswith("objc_atomicCompareAndSwap"))
Anna Zaksdff6ef92011-10-05 23:37:30 +000059 return evalOSAtomicCompareAndSwap(CE, Eng, Pred, Dst);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000060
61 // FIXME: Other atomics.
62 return false;
63}
64
Anna Zaksdff6ef92011-10-05 23:37:30 +000065bool OSAtomicChecker::evalOSAtomicCompareAndSwap(const CallExpr *CE,
66 ExprEngine &Eng,
67 ExplodedNode *Pred,
68 ExplodedNodeSet &Dst) const {
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000069 // Not enough arguments to match OSAtomicCompareAndSwap?
70 if (CE->getNumArgs() != 3)
71 return false;
72
Anna Zaksdff6ef92011-10-05 23:37:30 +000073 ASTContext &Ctx = Eng.getContext();
Zhongxing Xu1ec4e972009-12-09 12:23:28 +000074 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 Kremenekca804532011-08-12 23:04:46 +000098 static SimpleProgramPointTag OSAtomicLoadTag("OSAtomicChecker : Load");
99 static SimpleProgramPointTag OSAtomicStoreTag("OSAtomicChecker : Store");
100
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000101 // Load 'theValue'.
Anna Zaksdff6ef92011-10-05 23:37:30 +0000102 const ProgramState *state = Pred->getState();
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000103 ExplodedNodeSet Tmp;
Ted Kremenek13976632010-02-08 16:18:51 +0000104 SVal location = state->getSVal(theValueExpr);
Zhongxing Xu71f219a2010-06-23 02:06:56 +0000105 // 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 Xu24704462010-06-23 02:12:00 +0000107 // 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 Xu1ec4e972009-12-09 12:23:28 +0000112 QualType LoadTy;
Ted Kremenek96979342011-08-12 20:02:48 +0000113 if (const TypedValueRegion *TR =
114 dyn_cast_or_null<TypedValueRegion>(location.getAsRegion())) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000115 LoadTy = TR->getValueType();
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000116 }
Anna Zaksdff6ef92011-10-05 23:37:30 +0000117 Eng.evalLoad(Tmp, theValueExpr, Pred,
Ted Kremenekca804532011-08-12 23:04:46 +0000118 state, location, &OSAtomicLoadTag, LoadTy);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000119
120 if (Tmp.empty()) {
Anna Zaksf185cc12011-10-24 21:19:43 +0000121 // If no nodes were generated, other checkers must have generated sinks.
122 // We return an empty Dst.
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000123 return true;
124 }
125
126 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end();
127 I != E; ++I) {
128
129 ExplodedNode *N = *I;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000130 const ProgramState *stateLoad = N->getState();
Ted Kremenek6b4f5672011-04-27 05:34:09 +0000131
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 Kremenek13976632010-02-08 16:18:51 +0000137 SVal oldValueVal_untested = stateLoad->getSVal(oldValueExpr);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000138
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 Zaksdff6ef92011-10-05 23:37:30 +0000149 SValBuilder &svalBuilder = Eng.getSValBuilder();
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000150
151 // Perform the comparison.
Ted Kremenekca804532011-08-12 23:04:46 +0000152 DefinedOrUnknownSVal Cmp =
153 svalBuilder.evalEQ(stateLoad,theValueVal,oldValueVal);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000154
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000155 const ProgramState *stateEqual = stateLoad->assume(Cmp, true);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000156
157 // Were they equal?
158 if (stateEqual) {
159 // Perform the store.
160 ExplodedNodeSet TmpStore;
Ted Kremenek13976632010-02-08 16:18:51 +0000161 SVal val = stateEqual->getSVal(newValueExpr);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000162
163 // Handle implicit value casts.
Ted Kremenek96979342011-08-12 20:02:48 +0000164 if (const TypedValueRegion *R =
165 dyn_cast_or_null<TypedValueRegion>(location.getAsRegion())) {
Ted Kremenek9c149532010-12-01 21:57:22 +0000166 val = svalBuilder.evalCast(val,R->getValueType(), newValueExpr->getType());
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000167 }
168
Anna Zaksdff6ef92011-10-05 23:37:30 +0000169 Eng.evalStore(TmpStore, NULL, theValueExpr, N,
Ted Kremenekca804532011-08-12 23:04:46 +0000170 stateEqual, location, val, &OSAtomicStoreTag);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000171
172 if (TmpStore.empty()) {
Anna Zaksf185cc12011-10-24 21:19:43 +0000173 // If no nodes were generated, other checkers must have generated sinks.
174 // We return an empty Dst.
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000175 return true;
176 }
Anna Zaksf185cc12011-10-24 21:19:43 +0000177
178 PureStmtNodeBuilder B(TmpStore, Dst, Eng.getBuilderContext());
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000179 // 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 Kremenek18c66fd2011-08-15 22:09:50 +0000183 const ProgramState *stateNew = predNew->getState();
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000184 // 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 Zaksdff6ef92011-10-05 23:37:30 +0000188 Res = Eng.getSValBuilder().makeTruthVal(true, T);
Anna Zaksf185cc12011-10-24 21:19:43 +0000189 B.generateNode(CE, predNew, stateNew->BindExpr(CE, Res), false, this);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000190 }
191 }
192
193 // Were they not equal?
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000194 if (const ProgramState *stateNotEqual = stateLoad->assume(Cmp, false)) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000195 // 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 Zaksdff6ef92011-10-05 23:37:30 +0000199 Res = Eng.getSValBuilder().makeTruthVal(false, CE->getType());
Anna Zaksf185cc12011-10-24 21:19:43 +0000200 PureStmtNodeBuilder B(N, Dst, Eng.getBuilderContext());
201 B.generateNode(CE, N, stateNotEqual->BindExpr(CE, Res), false, this);
Zhongxing Xu1ec4e972009-12-09 12:23:28 +0000202 }
203 }
204
205 return true;
206}
Argyrios Kyrtzidisf0293662011-02-28 01:27:02 +0000207
208void ento::registerOSAtomicChecker(CheckerManager &mgr) {
209 mgr.registerChecker<OSAtomicChecker>();
210}