OSAtomicChecker.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include "InternalChecks.h"
  14. #include "clang/StaticAnalyzer/PathSensitive/Checker.h"
  15. #include "clang/Basic/Builtins.h"
  16. using namespace clang;
  17. using namespace ento;
  18. namespace {
  19. class OSAtomicChecker : public Checker {
  20. public:
  21. static void *getTag() { static int tag = 0; return &tag; }
  22. virtual bool evalCallExpr(CheckerContext &C, const CallExpr *CE);
  23. private:
  24. bool evalOSAtomicCompareAndSwap(CheckerContext &C, const CallExpr *CE);
  25. };
  26. }
  27. void ento::RegisterOSAtomicChecker(ExprEngine &Eng) {
  28. Eng.registerCheck(new OSAtomicChecker());
  29. }
  30. bool OSAtomicChecker::evalCallExpr(CheckerContext &C,const CallExpr *CE) {
  31. const GRState *state = C.getState();
  32. const Expr *Callee = CE->getCallee();
  33. SVal L = state->getSVal(Callee);
  34. const FunctionDecl* FD = L.getAsFunctionDecl();
  35. if (!FD)
  36. return false;
  37. const IdentifierInfo *II = FD->getIdentifier();
  38. if (!II)
  39. return false;
  40. llvm::StringRef FName(II->getName());
  41. // Check for compare and swap.
  42. if (FName.startswith("OSAtomicCompareAndSwap") ||
  43. FName.startswith("objc_atomicCompareAndSwap"))
  44. return evalOSAtomicCompareAndSwap(C, CE);
  45. // FIXME: Other atomics.
  46. return false;
  47. }
  48. bool OSAtomicChecker::evalOSAtomicCompareAndSwap(CheckerContext &C,
  49. const CallExpr *CE) {
  50. // Not enough arguments to match OSAtomicCompareAndSwap?
  51. if (CE->getNumArgs() != 3)
  52. return false;
  53. ASTContext &Ctx = C.getASTContext();
  54. const Expr *oldValueExpr = CE->getArg(0);
  55. QualType oldValueType = Ctx.getCanonicalType(oldValueExpr->getType());
  56. const Expr *newValueExpr = CE->getArg(1);
  57. QualType newValueType = Ctx.getCanonicalType(newValueExpr->getType());
  58. // Do the types of 'oldValue' and 'newValue' match?
  59. if (oldValueType != newValueType)
  60. return false;
  61. const Expr *theValueExpr = CE->getArg(2);
  62. const PointerType *theValueType=theValueExpr->getType()->getAs<PointerType>();
  63. // theValueType not a pointer?
  64. if (!theValueType)
  65. return false;
  66. QualType theValueTypePointee =
  67. Ctx.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
  68. // The pointee must match newValueType and oldValueType.
  69. if (theValueTypePointee != newValueType)
  70. return false;
  71. static unsigned magic_load = 0;
  72. static unsigned magic_store = 0;
  73. const void *OSAtomicLoadTag = &magic_load;
  74. const void *OSAtomicStoreTag = &magic_store;
  75. // Load 'theValue'.
  76. ExprEngine &Engine = C.getEngine();
  77. const GRState *state = C.getState();
  78. ExplodedNodeSet Tmp;
  79. SVal location = state->getSVal(theValueExpr);
  80. // Here we should use the value type of the region as the load type, because
  81. // we are simulating the semantics of the function, not the semantics of
  82. // passing argument. So the type of theValue expr is not we are loading.
  83. // But usually the type of the varregion is not the type we want either,
  84. // we still need to do a CastRetrievedVal in store manager. So actually this
  85. // LoadTy specifying can be omitted. But we put it here to emphasize the
  86. // semantics.
  87. QualType LoadTy;
  88. if (const TypedRegion *TR =
  89. dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
  90. LoadTy = TR->getValueType();
  91. }
  92. Engine.evalLoad(Tmp, theValueExpr, C.getPredecessor(),
  93. state, location, OSAtomicLoadTag, LoadTy);
  94. if (Tmp.empty()) {
  95. // If no nodes were generated, other checkers must generated sinks. But
  96. // since the builder state was restored, we set it manually to prevent
  97. // auto transition.
  98. // FIXME: there should be a better approach.
  99. C.getNodeBuilder().BuildSinks = true;
  100. return true;
  101. }
  102. for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end();
  103. I != E; ++I) {
  104. ExplodedNode *N = *I;
  105. const GRState *stateLoad = N->getState();
  106. SVal theValueVal_untested = stateLoad->getSVal(theValueExpr);
  107. SVal oldValueVal_untested = stateLoad->getSVal(oldValueExpr);
  108. // FIXME: Issue an error.
  109. if (theValueVal_untested.isUndef() || oldValueVal_untested.isUndef()) {
  110. return false;
  111. }
  112. DefinedOrUnknownSVal theValueVal =
  113. cast<DefinedOrUnknownSVal>(theValueVal_untested);
  114. DefinedOrUnknownSVal oldValueVal =
  115. cast<DefinedOrUnknownSVal>(oldValueVal_untested);
  116. SValBuilder &svalBuilder = Engine.getSValBuilder();
  117. // Perform the comparison.
  118. DefinedOrUnknownSVal Cmp = svalBuilder.evalEQ(stateLoad,theValueVal,oldValueVal);
  119. const GRState *stateEqual = stateLoad->assume(Cmp, true);
  120. // Were they equal?
  121. if (stateEqual) {
  122. // Perform the store.
  123. ExplodedNodeSet TmpStore;
  124. SVal val = stateEqual->getSVal(newValueExpr);
  125. // Handle implicit value casts.
  126. if (const TypedRegion *R =
  127. dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
  128. val = svalBuilder.evalCast(val,R->getValueType(), newValueExpr->getType());
  129. }
  130. Engine.evalStore(TmpStore, NULL, theValueExpr, N,
  131. stateEqual, location, val, OSAtomicStoreTag);
  132. if (TmpStore.empty()) {
  133. // If no nodes were generated, other checkers must generated sinks. But
  134. // since the builder state was restored, we set it manually to prevent
  135. // auto transition.
  136. // FIXME: there should be a better approach.
  137. C.getNodeBuilder().BuildSinks = true;
  138. return true;
  139. }
  140. // Now bind the result of the comparison.
  141. for (ExplodedNodeSet::iterator I2 = TmpStore.begin(),
  142. E2 = TmpStore.end(); I2 != E2; ++I2) {
  143. ExplodedNode *predNew = *I2;
  144. const GRState *stateNew = predNew->getState();
  145. // Check for 'void' return type if we have a bogus function prototype.
  146. SVal Res = UnknownVal();
  147. QualType T = CE->getType();
  148. if (!T->isVoidType())
  149. Res = Engine.getSValBuilder().makeTruthVal(true, T);
  150. C.generateNode(stateNew->BindExpr(CE, Res), predNew);
  151. }
  152. }
  153. // Were they not equal?
  154. if (const GRState *stateNotEqual = stateLoad->assume(Cmp, false)) {
  155. // Check for 'void' return type if we have a bogus function prototype.
  156. SVal Res = UnknownVal();
  157. QualType T = CE->getType();
  158. if (!T->isVoidType())
  159. Res = Engine.getSValBuilder().makeTruthVal(false, CE->getType());
  160. C.generateNode(stateNotEqual->BindExpr(CE, Res), N);
  161. }
  162. }
  163. return true;
  164. }