Implement analyzer support for OSCompareAndSwap. This required pushing "tagged"
ProgramPoints all the way through to GRCoreEngine.

NSString.m now fails with RegionStoreManager because of the void** cast.
Disabling use of region store for that test for now.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68845 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/BasicStore.cpp b/lib/Analysis/BasicStore.cpp
index 41776ed..566c197 100644
--- a/lib/Analysis/BasicStore.cpp
+++ b/lib/Analysis/BasicStore.cpp
@@ -282,6 +282,23 @@
     return UnknownVal();
 }
 
+static bool isHigherOrderVoidPtr(QualType T, ASTContext &C) {
+  bool foundPointer = false;
+  while (1) {  
+    const PointerType *PT = T->getAsPointerType();
+    if (!PT) {
+      if (!foundPointer)
+        return false;
+      
+      QualType X = C.getCanonicalType(T).getUnqualifiedType();
+      return X == C.VoidTy;
+    }
+    
+    foundPointer = true;
+    T = PT->getPointeeType();
+  }  
+}
+
 SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
   
   if (isa<UnknownVal>(loc))
@@ -294,6 +311,20 @@
     case loc::MemRegionKind: {
       const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
       
+      if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
+        // Just support void**, void***, etc., for now.  This is needed
+        // to handle OSCompareAndSwapPtr().
+        ASTContext &Ctx = StateMgr.getContext();
+        QualType T = TR->getLValueType(Ctx);
+
+        if (!isHigherOrderVoidPtr(T, Ctx))
+          return UnknownVal();
+        
+        // Otherwise, strip the views.
+        // FIXME: Should we layer a TypedView on the result?
+        R = TR->removeViews();
+      }
+      
       if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
         return UnknownVal();
       
diff --git a/lib/Analysis/GRCoreEngine.cpp b/lib/Analysis/GRCoreEngine.cpp
index 28f1a31..e4f27b6 100644
--- a/lib/Analysis/GRCoreEngine.cpp
+++ b/lib/Analysis/GRCoreEngine.cpp
@@ -368,47 +368,49 @@
     Eng.WList->Enqueue(Succ, B, Idx+1);
 }
 
-static inline PostStmt GetPostLoc(Stmt* S, ProgramPoint::Kind K) {
+static inline PostStmt GetPostLoc(Stmt* S, ProgramPoint::Kind K,
+                                  const void *tag) {
   switch (K) {
     default:
       assert(false && "Invalid PostXXXKind.");
       
     case ProgramPoint::PostStmtKind:
-      return PostStmt(S);
+      return PostStmt(S, tag);
       
     case ProgramPoint::PostLoadKind:
-      return PostLoad(S);
+      return PostLoad(S, tag);
 
     case ProgramPoint::PostUndefLocationCheckFailedKind:
-      return PostUndefLocationCheckFailed(S);
+      return PostUndefLocationCheckFailed(S, tag);
 
     case ProgramPoint::PostLocationChecksSucceedKind:
-      return PostLocationChecksSucceed(S);
+      return PostLocationChecksSucceed(S, tag);
       
     case ProgramPoint::PostOutOfBoundsCheckFailedKind:
-      return PostOutOfBoundsCheckFailed(S);
+      return PostOutOfBoundsCheckFailed(S, tag);
       
     case ProgramPoint::PostNullCheckFailedKind:
-      return PostNullCheckFailed(S);
+      return PostNullCheckFailed(S, tag);
       
     case ProgramPoint::PostStoreKind:
-      return PostStore(S);
+      return PostStore(S, tag);
       
     case ProgramPoint::PostPurgeDeadSymbolsKind:
-      return PostPurgeDeadSymbols(S);
+      return PostPurgeDeadSymbols(S, tag);
   }
 }
 
 ExplodedNodeImpl*
 GRStmtNodeBuilderImpl::generateNodeImpl(Stmt* S, const void* State,
                                         ExplodedNodeImpl* Pred,
-                                        ProgramPoint::Kind K) {
-  return generateNodeImpl(GetPostLoc(S, K), State, Pred); 
+                                        ProgramPoint::Kind K,
+                                        const void *tag) {
+  return generateNodeImpl(GetPostLoc(S, K, tag), State, Pred); 
 }
 
 ExplodedNodeImpl*
 GRStmtNodeBuilderImpl::generateNodeImpl(PostStmt Loc, const void* State,
-                                        ExplodedNodeImpl* Pred) {  
+                                        ExplodedNodeImpl* Pred) {
   bool IsNew;
   ExplodedNodeImpl* N = Eng.G->getNodeImpl(Loc, State, &IsNew);
   N->addPredecessor(Pred);
diff --git a/lib/Analysis/GRExprEngine.cpp b/lib/Analysis/GRExprEngine.cpp
index 4fe6fd6..9f049d5 100644
--- a/lib/Analysis/GRExprEngine.cpp
+++ b/lib/Analysis/GRExprEngine.cpp
@@ -531,6 +531,22 @@
 }
 
 //===----------------------------------------------------------------------===//
+// Generic node creation.
+//===----------------------------------------------------------------------===//
+
+GRExprEngine::NodeTy* GRExprEngine::MakeNode(NodeSet& Dst, Stmt* S,
+                                             NodeTy* Pred,
+                                             const GRState* St,
+                                             ProgramPoint::Kind K,
+                                             const void *tag) {
+  
+  assert (Builder && "GRStmtNodeBuilder not present.");
+  SaveAndRestore<const void*> OldTag(Builder->Tag);
+  Builder->Tag = tag;
+  return Builder->MakeNode(Dst, S, Pred, St, K);
+}
+
+//===----------------------------------------------------------------------===//
 // Branch processing.
 //===----------------------------------------------------------------------===//
 
@@ -1069,12 +1085,13 @@
 ///  @param location The location to store the value
 ///  @param Val The value to be stored
 void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
-                             const GRState* state, SVal location, SVal Val) {
+                             const GRState* state, SVal location, SVal Val,
+                             const void *tag) {
   
   assert (Builder && "GRStmtNodeBuilder must be defined.");
   
   // Evaluate the location (checks for bad dereferences).
-  Pred = EvalLocation(Ex, Pred, state, location);
+  Pred = EvalLocation(Ex, Pred, state, location, tag);
   
   if (!Pred)
     return;
@@ -1084,15 +1101,18 @@
 
   // Proceed with the store.  
   SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
-  Builder->PointKind = ProgramPoint::PostStoreKind;  
+  SaveAndRestore<const void*> OldTag(Builder->Tag);
+  Builder->PointKind = ProgramPoint::PostStoreKind;
+  Builder->Tag = tag;
   EvalBind(Dst, Ex, Pred, state, location, Val);
 }
 
 void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
-                            const GRState* state, SVal location) {
+                            const GRState* state, SVal location,
+                            const void *tag) {
 
   // Evaluate the location (checks for bad dereferences).  
-  Pred = EvalLocation(Ex, Pred, state, location);
+  Pred = EvalLocation(Ex, Pred, state, location, tag);
   
   if (!Pred)
     return;
@@ -1107,27 +1127,32 @@
 
   if (location.isUnknown()) {
     // This is important.  We must nuke the old binding.
-    MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
+    MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K, tag);
   }
   else {
     SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
-    MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);  
+    MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K, tag);
   }
 }
 
 void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
-                             const GRState* state, SVal location, SVal Val) {
+                             const GRState* state, SVal location, SVal Val,
+                             const void *tag) {
  
   NodeSet TmpDst;
-  EvalStore(TmpDst, StoreE, Pred, state, location, Val);
+  EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
 
   for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
-    MakeNode(Dst, Ex, *I, (*I)->getState());
+    MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
 }
 
 GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
                                                  const GRState* state,
-                                                 SVal location) {
+                                                 SVal location,
+                                                 const void *tag) {
+  
+  SaveAndRestore<const void*> OldTag(Builder->Tag);
+  Builder->Tag = tag;
   
   // Check for loads/stores from/to undefined values.  
   if (location.isUndef()) {
@@ -1235,8 +1260,144 @@
 }
 
 //===----------------------------------------------------------------------===//
+// Transfer function: OSAtomics.
+//
+// FIXME: Eventually refactor into a more "plugin" infrastructure.
+//===----------------------------------------------------------------------===//
+
+// Mac OS X:
+// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
+// atomic.3.html
+//
+static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
+                                       GRExprEngine& Engine,
+                                       GRStmtNodeBuilder<GRState>& Builder,
+                                       CallExpr* CE, SVal L,                 
+                                       ExplodedNode<GRState>* Pred) {
+
+  // Not enough arguments to match OSAtomicCompareAndSwap?
+  if (CE->getNumArgs() != 3)
+    return false;
+  
+  ASTContext &C = Engine.getContext();
+  Expr *oldValueExpr = CE->getArg(0);
+  QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
+
+  Expr *newValueExpr = CE->getArg(1);
+  QualType newValueType = C.getCanonicalType(newValueExpr->getType());
+  
+  // Do the types of 'oldValue' and 'newValue' match?
+  if (oldValueType != newValueType)
+    return false;
+  
+  Expr *theValueExpr = CE->getArg(2);
+  const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
+  
+  // theValueType not a pointer?
+  if (!theValueType)
+    return false;
+  
+  QualType theValueTypePointee =
+    C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
+  
+  // The pointee must match newValueType and oldValueType.
+  if (theValueTypePointee != newValueType)
+    return false;
+  
+  static unsigned magic_load = 0;
+  static unsigned magic_store = 0;
+
+  const void *OSAtomicLoadTag = &magic_load;
+  const void *OSAtomicStoreTag = &magic_store;
+  
+  // Load 'theValue'.
+  GRStateManager &StateMgr = Engine.getStateManager();
+  const GRState *state = Pred->getState();
+  ExplodedNodeSet<GRState> Tmp;
+  SVal location = StateMgr.GetSVal(state, theValueExpr);
+  Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
+
+  for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
+       I != E; ++I) {
+  
+    ExplodedNode<GRState> *N = *I;
+    const GRState *stateLoad = N->getState();
+    SVal theValueVal = StateMgr.GetSVal(stateLoad, theValueExpr);
+    SVal oldValueVal = StateMgr.GetSVal(stateLoad, oldValueExpr);
+        
+    // Perform the comparison.
+    SVal Cmp = Engine.EvalBinOp(BinaryOperator::EQ, theValueVal, oldValueVal,
+                                Engine.getContext().IntTy);
+    bool isFeasible = false;
+    const GRState *stateEqual = StateMgr.Assume(stateLoad, Cmp, true,
+                                                isFeasible);
+    
+    // Were they equal?
+    if (isFeasible) {
+      // Perform the store.
+      ExplodedNodeSet<GRState> TmpStore;
+      Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location, 
+                       StateMgr.GetSVal(stateEqual, newValueExpr),
+                       OSAtomicStoreTag);
+      
+      // Now bind the result of the comparison.
+      for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
+           E2 = TmpStore.end(); I2 != E2; ++I2) {
+        ExplodedNode<GRState> *predNew = *I2;
+        const GRState *stateNew = predNew->getState();
+        SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
+        Engine.MakeNode(Dst, CE, predNew, Engine.BindExpr(stateNew, CE, Res));
+      }
+    }
+    
+    // Were they not equal?
+    isFeasible = false;
+    const GRState *stateNotEqual = StateMgr.Assume(stateLoad, Cmp, false,
+                                                   isFeasible);
+    
+    if (isFeasible) {
+      SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
+      Engine.MakeNode(Dst, CE, N, Engine.BindExpr(stateNotEqual, CE, Res));
+    }
+  }
+      
+  return true;
+}
+
+static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
+                         GRExprEngine& Engine,
+                         GRStmtNodeBuilder<GRState>& Builder,
+                         CallExpr* CE, SVal L,
+                         ExplodedNode<GRState>* Pred) {
+  
+  if (!isa<loc::FuncVal>(L))
+    return false;
+  
+  const FunctionDecl *FD = cast<loc::FuncVal>(L).getDecl();
+  const char *FName = FD->getNameAsCString();
+  
+  // Check for compare and swap.
+  if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0)
+    return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
+  
+  // FIXME: Other atomics.
+  return false;
+}
+
+//===----------------------------------------------------------------------===//
 // Transfer function: Function calls.
 //===----------------------------------------------------------------------===//
+
+void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
+  assert (Builder && "GRStmtNodeBuilder must be defined.");
+  
+  // FIXME: Allow us to chain together transfer functions.
+  if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
+      return;
+      
+  getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
+}
+
 void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
                              CallExpr::arg_iterator AI,
                              CallExpr::arg_iterator AE,
diff --git a/lib/Analysis/MemRegion.cpp b/lib/Analysis/MemRegion.cpp
index c8a43a5..0990767 100644
--- a/lib/Analysis/MemRegion.cpp
+++ b/lib/Analysis/MemRegion.cpp
@@ -481,6 +481,21 @@
     
     SR = dyn_cast<SubRegion>(R);    
   }
-  
+
   return false;
 }
+
+
+//===----------------------------------------------------------------------===//
+// View handling.
+//===----------------------------------------------------------------------===//
+
+const MemRegion *TypedViewRegion::removeViews() const {
+  const SubRegion *SR = this;
+  const MemRegion *R = SR;
+  while (SR && isa<TypedViewRegion>(SR)) {
+    R = SR->getSuperRegion();
+    SR = dyn_cast<SubRegion>(R);
+  }
+  return R;
+}
diff --git a/lib/Analysis/SVals.cpp b/lib/Analysis/SVals.cpp
index 87a1073..e911d2c 100644
--- a/lib/Analysis/SVals.cpp
+++ b/lib/Analysis/SVals.cpp
@@ -271,6 +271,10 @@
   return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
 }
 
+NonLoc ValueManager::makeTruthVal(bool b, QualType T) {
+  return nonloc::ConcreteInt(BasicVals.getTruthValue(b, T));
+}
+
 NonLoc NonLoc::MakeCompoundVal(QualType T, llvm::ImmutableList<SVal> Vals,
                                BasicValueFactory& BasicVals) {
   return nonloc::CompoundVal(BasicVals.getCompoundValData(T, Vals));