1 //===- ThreadSafety.cpp ----------------------------------------*- 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 // A intra-procedural analysis for thread safety (e.g. deadlocks and race 11 // conditions), based off of an annotation system. 12 // 13 // See http://clang.llvm.org/docs/ThreadSafetyAnalysis.html 14 // for more information. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "clang/AST/Attr.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/ExprCXX.h" 21 #include "clang/AST/StmtCXX.h" 22 #include "clang/AST/StmtVisitor.h" 23 #include "clang/Analysis/Analyses/PostOrderCFGView.h" 24 #include "clang/Analysis/Analyses/ThreadSafety.h" 25 #include "clang/Analysis/Analyses/ThreadSafetyLogical.h" 26 #include "clang/Analysis/Analyses/ThreadSafetyTIL.h" 27 #include "clang/Analysis/Analyses/ThreadSafetyTraverse.h" 28 #include "clang/Analysis/Analyses/ThreadSafetyCommon.h" 29 #include "clang/Analysis/AnalysisContext.h" 30 #include "clang/Analysis/CFG.h" 31 #include "clang/Analysis/CFGStmtMap.h" 32 #include "clang/Basic/OperatorKinds.h" 33 #include "clang/Basic/SourceLocation.h" 34 #include "clang/Basic/SourceManager.h" 35 #include "llvm/ADT/BitVector.h" 36 #include "llvm/ADT/FoldingSet.h" 37 #include "llvm/ADT/ImmutableMap.h" 38 #include "llvm/ADT/PostOrderIterator.h" 39 #include "llvm/ADT/SmallVector.h" 40 #include "llvm/ADT/StringRef.h" 41 #include "llvm/Support/raw_ostream.h" 42 #include <algorithm> 43 #include <utility> 44 #include <vector> 45 46 using namespace clang; 47 using namespace thread_safety; 48 49 // Key method definition 50 ThreadSafetyHandler::~ThreadSafetyHandler() {} 51 52 namespace { 53 54 /// SExpr implements a simple expression language that is used to store, 55 /// compare, and pretty-print C++ expressions. Unlike a clang Expr, a SExpr 56 /// does not capture surface syntax, and it does not distinguish between 57 /// C++ concepts, like pointers and references, that have no real semantic 58 /// differences. This simplicity allows SExprs to be meaningfully compared, 59 /// e.g. 60 /// (x) = x 61 /// (*this).foo = this->foo 62 /// *&a = a 63 /// 64 /// Thread-safety analysis works by comparing lock expressions. Within the 65 /// body of a function, an expression such as "x->foo->bar.mu" will resolve to 66 /// a particular mutex object at run-time. Subsequent occurrences of the same 67 /// expression (where "same" means syntactic equality) will refer to the same 68 /// run-time object if three conditions hold: 69 /// (1) Local variables in the expression, such as "x" have not changed. 70 /// (2) Values on the heap that affect the expression have not changed. 71 /// (3) The expression involves only pure function calls. 72 /// 73 /// The current implementation assumes, but does not verify, that multiple uses 74 /// of the same lock expression satisfies these criteria. 75 class SExpr { 76 private: 77 enum ExprOp { 78 EOP_Nop, ///< No-op 79 EOP_Wildcard, ///< Matches anything. 80 EOP_Universal, ///< Universal lock. 81 EOP_This, ///< This keyword. 82 EOP_NVar, ///< Named variable. 83 EOP_LVar, ///< Local variable. 84 EOP_Dot, ///< Field access 85 EOP_Call, ///< Function call 86 EOP_MCall, ///< Method call 87 EOP_Index, ///< Array index 88 EOP_Unary, ///< Unary operation 89 EOP_Binary, ///< Binary operation 90 EOP_Unknown ///< Catchall for everything else 91 }; 92 93 94 class SExprNode { 95 private: 96 unsigned char Op; ///< Opcode of the root node 97 unsigned char Flags; ///< Additional opcode-specific data 98 unsigned short Sz; ///< Number of child nodes 99 const void* Data; ///< Additional opcode-specific data 100 101 public: 102 SExprNode(ExprOp O, unsigned F, const void* D) 103 : Op(static_cast<unsigned char>(O)), 104 Flags(static_cast<unsigned char>(F)), Sz(1), Data(D) 105 { } 106 107 unsigned size() const { return Sz; } 108 void setSize(unsigned S) { Sz = S; } 109 110 ExprOp kind() const { return static_cast<ExprOp>(Op); } 111 112 const NamedDecl* getNamedDecl() const { 113 assert(Op == EOP_NVar || Op == EOP_LVar || Op == EOP_Dot); 114 return reinterpret_cast<const NamedDecl*>(Data); 115 } 116 117 const NamedDecl* getFunctionDecl() const { 118 assert(Op == EOP_Call || Op == EOP_MCall); 119 return reinterpret_cast<const NamedDecl*>(Data); 120 } 121 122 bool isArrow() const { return Op == EOP_Dot && Flags == 1; } 123 void setArrow(bool A) { Flags = A ? 1 : 0; } 124 125 unsigned arity() const { 126 switch (Op) { 127 case EOP_Nop: return 0; 128 case EOP_Wildcard: return 0; 129 case EOP_Universal: return 0; 130 case EOP_NVar: return 0; 131 case EOP_LVar: return 0; 132 case EOP_This: return 0; 133 case EOP_Dot: return 1; 134 case EOP_Call: return Flags+1; // First arg is function. 135 case EOP_MCall: return Flags+1; // First arg is implicit obj. 136 case EOP_Index: return 2; 137 case EOP_Unary: return 1; 138 case EOP_Binary: return 2; 139 case EOP_Unknown: return Flags; 140 } 141 return 0; 142 } 143 144 bool operator==(const SExprNode& Other) const { 145 // Ignore flags and size -- they don't matter. 146 return (Op == Other.Op && 147 Data == Other.Data); 148 } 149 150 bool operator!=(const SExprNode& Other) const { 151 return !(*this == Other); 152 } 153 154 bool matches(const SExprNode& Other) const { 155 return (*this == Other) || 156 (Op == EOP_Wildcard) || 157 (Other.Op == EOP_Wildcard); 158 } 159 }; 160 161 162 /// \brief Encapsulates the lexical context of a function call. The lexical 163 /// context includes the arguments to the call, including the implicit object 164 /// argument. When an attribute containing a mutex expression is attached to 165 /// a method, the expression may refer to formal parameters of the method. 166 /// Actual arguments must be substituted for formal parameters to derive 167 /// the appropriate mutex expression in the lexical context where the function 168 /// is called. PrevCtx holds the context in which the arguments themselves 169 /// should be evaluated; multiple calling contexts can be chained together 170 /// by the lock_returned attribute. 171 struct CallingContext { 172 const NamedDecl* AttrDecl; // The decl to which the attribute is attached. 173 const Expr* SelfArg; // Implicit object argument -- e.g. 'this' 174 bool SelfArrow; // is Self referred to with -> or .? 175 unsigned NumArgs; // Number of funArgs 176 const Expr* const* FunArgs; // Function arguments 177 CallingContext* PrevCtx; // The previous context; or 0 if none. 178 179 CallingContext(const NamedDecl *D) 180 : AttrDecl(D), SelfArg(0), SelfArrow(false), NumArgs(0), FunArgs(0), 181 PrevCtx(0) {} 182 }; 183 184 typedef SmallVector<SExprNode, 4> NodeVector; 185 186 private: 187 // A SExpr is a list of SExprNodes in prefix order. The Size field allows 188 // the list to be traversed as a tree. 189 NodeVector NodeVec; 190 191 private: 192 unsigned make(ExprOp O, unsigned F = 0, const void *D = 0) { 193 NodeVec.push_back(SExprNode(O, F, D)); 194 return NodeVec.size() - 1; 195 } 196 197 unsigned makeNop() { 198 return make(EOP_Nop); 199 } 200 201 unsigned makeWildcard() { 202 return make(EOP_Wildcard); 203 } 204 205 unsigned makeUniversal() { 206 return make(EOP_Universal); 207 } 208 209 unsigned makeNamedVar(const NamedDecl *D) { 210 return make(EOP_NVar, 0, D); 211 } 212 213 unsigned makeLocalVar(const NamedDecl *D) { 214 return make(EOP_LVar, 0, D); 215 } 216 217 unsigned makeThis() { 218 return make(EOP_This); 219 } 220 221 unsigned makeDot(const NamedDecl *D, bool Arrow) { 222 return make(EOP_Dot, Arrow ? 1 : 0, D); 223 } 224 225 unsigned makeCall(unsigned NumArgs, const NamedDecl *D) { 226 return make(EOP_Call, NumArgs, D); 227 } 228 229 // Grab the very first declaration of virtual method D 230 const CXXMethodDecl* getFirstVirtualDecl(const CXXMethodDecl *D) { 231 while (true) { 232 D = D->getCanonicalDecl(); 233 CXXMethodDecl::method_iterator I = D->begin_overridden_methods(), 234 E = D->end_overridden_methods(); 235 if (I == E) 236 return D; // Method does not override anything 237 D = *I; // FIXME: this does not work with multiple inheritance. 238 } 239 return 0; 240 } 241 242 unsigned makeMCall(unsigned NumArgs, const CXXMethodDecl *D) { 243 return make(EOP_MCall, NumArgs, getFirstVirtualDecl(D)); 244 } 245 246 unsigned makeIndex() { 247 return make(EOP_Index); 248 } 249 250 unsigned makeUnary() { 251 return make(EOP_Unary); 252 } 253 254 unsigned makeBinary() { 255 return make(EOP_Binary); 256 } 257 258 unsigned makeUnknown(unsigned Arity) { 259 return make(EOP_Unknown, Arity); 260 } 261 262 inline bool isCalleeArrow(const Expr *E) { 263 const MemberExpr *ME = dyn_cast<MemberExpr>(E->IgnoreParenCasts()); 264 return ME ? ME->isArrow() : false; 265 } 266 267 /// Build an SExpr from the given C++ expression. 268 /// Recursive function that terminates on DeclRefExpr. 269 /// Note: this function merely creates a SExpr; it does not check to 270 /// ensure that the original expression is a valid mutex expression. 271 /// 272 /// NDeref returns the number of Derefence and AddressOf operations 273 /// preceding the Expr; this is used to decide whether to pretty-print 274 /// SExprs with . or ->. 275 unsigned buildSExpr(const Expr *Exp, CallingContext* CallCtx, 276 int* NDeref = 0) { 277 if (!Exp) 278 return 0; 279 280 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp)) { 281 const NamedDecl *ND = cast<NamedDecl>(DRE->getDecl()->getCanonicalDecl()); 282 const ParmVarDecl *PV = dyn_cast_or_null<ParmVarDecl>(ND); 283 if (PV) { 284 const FunctionDecl *FD = 285 cast<FunctionDecl>(PV->getDeclContext())->getCanonicalDecl(); 286 unsigned i = PV->getFunctionScopeIndex(); 287 288 if (CallCtx && CallCtx->FunArgs && 289 FD == CallCtx->AttrDecl->getCanonicalDecl()) { 290 // Substitute call arguments for references to function parameters 291 assert(i < CallCtx->NumArgs); 292 return buildSExpr(CallCtx->FunArgs[i], CallCtx->PrevCtx, NDeref); 293 } 294 // Map the param back to the param of the original function declaration. 295 makeNamedVar(FD->getParamDecl(i)); 296 return 1; 297 } 298 // Not a function parameter -- just store the reference. 299 makeNamedVar(ND); 300 return 1; 301 } else if (isa<CXXThisExpr>(Exp)) { 302 // Substitute parent for 'this' 303 if (CallCtx && CallCtx->SelfArg) { 304 if (!CallCtx->SelfArrow && NDeref) 305 // 'this' is a pointer, but self is not, so need to take address. 306 --(*NDeref); 307 return buildSExpr(CallCtx->SelfArg, CallCtx->PrevCtx, NDeref); 308 } 309 else { 310 makeThis(); 311 return 1; 312 } 313 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) { 314 const NamedDecl *ND = ME->getMemberDecl(); 315 int ImplicitDeref = ME->isArrow() ? 1 : 0; 316 unsigned Root = makeDot(ND, false); 317 unsigned Sz = buildSExpr(ME->getBase(), CallCtx, &ImplicitDeref); 318 NodeVec[Root].setArrow(ImplicitDeref > 0); 319 NodeVec[Root].setSize(Sz + 1); 320 return Sz + 1; 321 } else if (const CXXMemberCallExpr *CMCE = dyn_cast<CXXMemberCallExpr>(Exp)) { 322 // When calling a function with a lock_returned attribute, replace 323 // the function call with the expression in lock_returned. 324 const CXXMethodDecl *MD = CMCE->getMethodDecl()->getMostRecentDecl(); 325 if (LockReturnedAttr* At = MD->getAttr<LockReturnedAttr>()) { 326 CallingContext LRCallCtx(CMCE->getMethodDecl()); 327 LRCallCtx.SelfArg = CMCE->getImplicitObjectArgument(); 328 LRCallCtx.SelfArrow = isCalleeArrow(CMCE->getCallee()); 329 LRCallCtx.NumArgs = CMCE->getNumArgs(); 330 LRCallCtx.FunArgs = CMCE->getArgs(); 331 LRCallCtx.PrevCtx = CallCtx; 332 return buildSExpr(At->getArg(), &LRCallCtx); 333 } 334 // Hack to treat smart pointers and iterators as pointers; 335 // ignore any method named get(). 336 if (CMCE->getMethodDecl()->getNameAsString() == "get" && 337 CMCE->getNumArgs() == 0) { 338 if (NDeref && isCalleeArrow(CMCE->getCallee())) 339 ++(*NDeref); 340 return buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx, NDeref); 341 } 342 unsigned NumCallArgs = CMCE->getNumArgs(); 343 unsigned Root = makeMCall(NumCallArgs, CMCE->getMethodDecl()); 344 unsigned Sz = buildSExpr(CMCE->getImplicitObjectArgument(), CallCtx); 345 const Expr* const* CallArgs = CMCE->getArgs(); 346 for (unsigned i = 0; i < NumCallArgs; ++i) { 347 Sz += buildSExpr(CallArgs[i], CallCtx); 348 } 349 NodeVec[Root].setSize(Sz + 1); 350 return Sz + 1; 351 } else if (const CallExpr *CE = dyn_cast<CallExpr>(Exp)) { 352 const FunctionDecl *FD = CE->getDirectCallee()->getMostRecentDecl(); 353 if (LockReturnedAttr* At = FD->getAttr<LockReturnedAttr>()) { 354 CallingContext LRCallCtx(CE->getDirectCallee()); 355 LRCallCtx.NumArgs = CE->getNumArgs(); 356 LRCallCtx.FunArgs = CE->getArgs(); 357 LRCallCtx.PrevCtx = CallCtx; 358 return buildSExpr(At->getArg(), &LRCallCtx); 359 } 360 // Treat smart pointers and iterators as pointers; 361 // ignore the * and -> operators. 362 if (const CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(CE)) { 363 OverloadedOperatorKind k = OE->getOperator(); 364 if (k == OO_Star) { 365 if (NDeref) ++(*NDeref); 366 return buildSExpr(OE->getArg(0), CallCtx, NDeref); 367 } 368 else if (k == OO_Arrow) { 369 return buildSExpr(OE->getArg(0), CallCtx, NDeref); 370 } 371 } 372 unsigned NumCallArgs = CE->getNumArgs(); 373 unsigned Root = makeCall(NumCallArgs, 0); 374 unsigned Sz = buildSExpr(CE->getCallee(), CallCtx); 375 const Expr* const* CallArgs = CE->getArgs(); 376 for (unsigned i = 0; i < NumCallArgs; ++i) { 377 Sz += buildSExpr(CallArgs[i], CallCtx); 378 } 379 NodeVec[Root].setSize(Sz+1); 380 return Sz+1; 381 } else if (const BinaryOperator *BOE = dyn_cast<BinaryOperator>(Exp)) { 382 unsigned Root = makeBinary(); 383 unsigned Sz = buildSExpr(BOE->getLHS(), CallCtx); 384 Sz += buildSExpr(BOE->getRHS(), CallCtx); 385 NodeVec[Root].setSize(Sz); 386 return Sz; 387 } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(Exp)) { 388 // Ignore & and * operators -- they're no-ops. 389 // However, we try to figure out whether the expression is a pointer, 390 // so we can use . and -> appropriately in error messages. 391 if (UOE->getOpcode() == UO_Deref) { 392 if (NDeref) ++(*NDeref); 393 return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref); 394 } 395 if (UOE->getOpcode() == UO_AddrOf) { 396 if (DeclRefExpr* DRE = dyn_cast<DeclRefExpr>(UOE->getSubExpr())) { 397 if (DRE->getDecl()->isCXXInstanceMember()) { 398 // This is a pointer-to-member expression, e.g. &MyClass::mu_. 399 // We interpret this syntax specially, as a wildcard. 400 unsigned Root = makeDot(DRE->getDecl(), false); 401 makeWildcard(); 402 NodeVec[Root].setSize(2); 403 return 2; 404 } 405 } 406 if (NDeref) --(*NDeref); 407 return buildSExpr(UOE->getSubExpr(), CallCtx, NDeref); 408 } 409 unsigned Root = makeUnary(); 410 unsigned Sz = buildSExpr(UOE->getSubExpr(), CallCtx); 411 NodeVec[Root].setSize(Sz); 412 return Sz; 413 } else if (const ArraySubscriptExpr *ASE = 414 dyn_cast<ArraySubscriptExpr>(Exp)) { 415 unsigned Root = makeIndex(); 416 unsigned Sz = buildSExpr(ASE->getBase(), CallCtx); 417 Sz += buildSExpr(ASE->getIdx(), CallCtx); 418 NodeVec[Root].setSize(Sz); 419 return Sz; 420 } else if (const AbstractConditionalOperator *CE = 421 dyn_cast<AbstractConditionalOperator>(Exp)) { 422 unsigned Root = makeUnknown(3); 423 unsigned Sz = buildSExpr(CE->getCond(), CallCtx); 424 Sz += buildSExpr(CE->getTrueExpr(), CallCtx); 425 Sz += buildSExpr(CE->getFalseExpr(), CallCtx); 426 NodeVec[Root].setSize(Sz); 427 return Sz; 428 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(Exp)) { 429 unsigned Root = makeUnknown(3); 430 unsigned Sz = buildSExpr(CE->getCond(), CallCtx); 431 Sz += buildSExpr(CE->getLHS(), CallCtx); 432 Sz += buildSExpr(CE->getRHS(), CallCtx); 433 NodeVec[Root].setSize(Sz); 434 return Sz; 435 } else if (const CastExpr *CE = dyn_cast<CastExpr>(Exp)) { 436 return buildSExpr(CE->getSubExpr(), CallCtx, NDeref); 437 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) { 438 return buildSExpr(PE->getSubExpr(), CallCtx, NDeref); 439 } else if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Exp)) { 440 return buildSExpr(EWC->getSubExpr(), CallCtx, NDeref); 441 } else if (const CXXBindTemporaryExpr *E = dyn_cast<CXXBindTemporaryExpr>(Exp)) { 442 return buildSExpr(E->getSubExpr(), CallCtx, NDeref); 443 } else if (isa<CharacterLiteral>(Exp) || 444 isa<CXXNullPtrLiteralExpr>(Exp) || 445 isa<GNUNullExpr>(Exp) || 446 isa<CXXBoolLiteralExpr>(Exp) || 447 isa<FloatingLiteral>(Exp) || 448 isa<ImaginaryLiteral>(Exp) || 449 isa<IntegerLiteral>(Exp) || 450 isa<StringLiteral>(Exp) || 451 isa<ObjCStringLiteral>(Exp)) { 452 makeNop(); 453 return 1; // FIXME: Ignore literals for now 454 } else { 455 makeNop(); 456 return 1; // Ignore. FIXME: mark as invalid expression? 457 } 458 } 459 460 /// \brief Construct a SExpr from an expression. 461 /// \param MutexExp The original mutex expression within an attribute 462 /// \param DeclExp An expression involving the Decl on which the attribute 463 /// occurs. 464 /// \param D The declaration to which the lock/unlock attribute is attached. 465 void buildSExprFromExpr(const Expr *MutexExp, const Expr *DeclExp, 466 const NamedDecl *D, VarDecl *SelfDecl = 0) { 467 CallingContext CallCtx(D); 468 469 if (MutexExp) { 470 if (const StringLiteral* SLit = dyn_cast<StringLiteral>(MutexExp)) { 471 if (SLit->getString() == StringRef("*")) 472 // The "*" expr is a universal lock, which essentially turns off 473 // checks until it is removed from the lockset. 474 makeUniversal(); 475 else 476 // Ignore other string literals for now. 477 makeNop(); 478 return; 479 } 480 } 481 482 // If we are processing a raw attribute expression, with no substitutions. 483 if (DeclExp == 0) { 484 buildSExpr(MutexExp, 0); 485 return; 486 } 487 488 // Examine DeclExp to find SelfArg and FunArgs, which are used to substitute 489 // for formal parameters when we call buildMutexID later. 490 if (const MemberExpr *ME = dyn_cast<MemberExpr>(DeclExp)) { 491 CallCtx.SelfArg = ME->getBase(); 492 CallCtx.SelfArrow = ME->isArrow(); 493 } else if (const CXXMemberCallExpr *CE = 494 dyn_cast<CXXMemberCallExpr>(DeclExp)) { 495 CallCtx.SelfArg = CE->getImplicitObjectArgument(); 496 CallCtx.SelfArrow = isCalleeArrow(CE->getCallee()); 497 CallCtx.NumArgs = CE->getNumArgs(); 498 CallCtx.FunArgs = CE->getArgs(); 499 } else if (const CallExpr *CE = dyn_cast<CallExpr>(DeclExp)) { 500 CallCtx.NumArgs = CE->getNumArgs(); 501 CallCtx.FunArgs = CE->getArgs(); 502 } else if (const CXXConstructExpr *CE = 503 dyn_cast<CXXConstructExpr>(DeclExp)) { 504 CallCtx.SelfArg = 0; // Will be set below 505 CallCtx.NumArgs = CE->getNumArgs(); 506 CallCtx.FunArgs = CE->getArgs(); 507 } else if (D && isa<CXXDestructorDecl>(D)) { 508 // There's no such thing as a "destructor call" in the AST. 509 CallCtx.SelfArg = DeclExp; 510 } 511 512 // Hack to handle constructors, where self cannot be recovered from 513 // the expression. 514 if (SelfDecl && !CallCtx.SelfArg) { 515 DeclRefExpr SelfDRE(SelfDecl, false, SelfDecl->getType(), VK_LValue, 516 SelfDecl->getLocation()); 517 CallCtx.SelfArg = &SelfDRE; 518 519 // If the attribute has no arguments, then assume the argument is "this". 520 if (MutexExp == 0) 521 buildSExpr(CallCtx.SelfArg, 0); 522 else // For most attributes. 523 buildSExpr(MutexExp, &CallCtx); 524 return; 525 } 526 527 // If the attribute has no arguments, then assume the argument is "this". 528 if (MutexExp == 0) 529 buildSExpr(CallCtx.SelfArg, 0); 530 else // For most attributes. 531 buildSExpr(MutexExp, &CallCtx); 532 } 533 534 /// \brief Get index of next sibling of node i. 535 unsigned getNextSibling(unsigned i) const { 536 return i + NodeVec[i].size(); 537 } 538 539 public: 540 explicit SExpr(clang::Decl::EmptyShell e) { NodeVec.clear(); } 541 542 /// \param MutexExp The original mutex expression within an attribute 543 /// \param DeclExp An expression involving the Decl on which the attribute 544 /// occurs. 545 /// \param D The declaration to which the lock/unlock attribute is attached. 546 /// Caller must check isValid() after construction. 547 SExpr(const Expr* MutexExp, const Expr *DeclExp, const NamedDecl* D, 548 VarDecl *SelfDecl=0) { 549 buildSExprFromExpr(MutexExp, DeclExp, D, SelfDecl); 550 } 551 552 /// Return true if this is a valid decl sequence. 553 /// Caller must call this by hand after construction to handle errors. 554 bool isValid() const { 555 return !NodeVec.empty(); 556 } 557 558 bool shouldIgnore() const { 559 // Nop is a mutex that we have decided to deliberately ignore. 560 assert(NodeVec.size() > 0 && "Invalid Mutex"); 561 return NodeVec[0].kind() == EOP_Nop; 562 } 563 564 bool isUniversal() const { 565 assert(NodeVec.size() > 0 && "Invalid Mutex"); 566 return NodeVec[0].kind() == EOP_Universal; 567 } 568 569 /// Issue a warning about an invalid lock expression 570 static void warnInvalidLock(ThreadSafetyHandler &Handler, 571 const Expr *MutexExp, const Expr *DeclExp, 572 const NamedDecl *D, StringRef Kind) { 573 SourceLocation Loc; 574 if (DeclExp) 575 Loc = DeclExp->getExprLoc(); 576 577 // FIXME: add a note about the attribute location in MutexExp or D 578 if (Loc.isValid()) 579 Handler.handleInvalidLockExp(Kind, Loc); 580 } 581 582 bool operator==(const SExpr &other) const { 583 return NodeVec == other.NodeVec; 584 } 585 586 bool operator!=(const SExpr &other) const { 587 return !(*this == other); 588 } 589 590 bool matches(const SExpr &Other, unsigned i = 0, unsigned j = 0) const { 591 if (NodeVec[i].matches(Other.NodeVec[j])) { 592 unsigned ni = NodeVec[i].arity(); 593 unsigned nj = Other.NodeVec[j].arity(); 594 unsigned n = (ni < nj) ? ni : nj; 595 bool Result = true; 596 unsigned ci = i+1; // first child of i 597 unsigned cj = j+1; // first child of j 598 for (unsigned k = 0; k < n; 599 ++k, ci=getNextSibling(ci), cj = Other.getNextSibling(cj)) { 600 Result = Result && matches(Other, ci, cj); 601 } 602 return Result; 603 } 604 return false; 605 } 606 607 // A partial match between a.mu and b.mu returns true a and b have the same 608 // type (and thus mu refers to the same mutex declaration), regardless of 609 // whether a and b are different objects or not. 610 bool partiallyMatches(const SExpr &Other) const { 611 if (NodeVec[0].kind() == EOP_Dot) 612 return NodeVec[0].matches(Other.NodeVec[0]); 613 return false; 614 } 615 616 /// \brief Pretty print a lock expression for use in error messages. 617 std::string toString(unsigned i = 0) const { 618 assert(isValid()); 619 if (i >= NodeVec.size()) 620 return ""; 621 622 const SExprNode* N = &NodeVec[i]; 623 switch (N->kind()) { 624 case EOP_Nop: 625 return "_"; 626 case EOP_Wildcard: 627 return "(?)"; 628 case EOP_Universal: 629 return "*"; 630 case EOP_This: 631 return "this"; 632 case EOP_NVar: 633 case EOP_LVar: { 634 return N->getNamedDecl()->getNameAsString(); 635 } 636 case EOP_Dot: { 637 if (NodeVec[i+1].kind() == EOP_Wildcard) { 638 std::string S = "&"; 639 S += N->getNamedDecl()->getQualifiedNameAsString(); 640 return S; 641 } 642 std::string FieldName = N->getNamedDecl()->getNameAsString(); 643 if (NodeVec[i+1].kind() == EOP_This) 644 return FieldName; 645 646 std::string S = toString(i+1); 647 if (N->isArrow()) 648 return S + "->" + FieldName; 649 else 650 return S + "." + FieldName; 651 } 652 case EOP_Call: { 653 std::string S = toString(i+1) + "("; 654 unsigned NumArgs = N->arity()-1; 655 unsigned ci = getNextSibling(i+1); 656 for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) { 657 S += toString(ci); 658 if (k+1 < NumArgs) S += ","; 659 } 660 S += ")"; 661 return S; 662 } 663 case EOP_MCall: { 664 std::string S = ""; 665 if (NodeVec[i+1].kind() != EOP_This) 666 S = toString(i+1) + "."; 667 if (const NamedDecl *D = N->getFunctionDecl()) 668 S += D->getNameAsString() + "("; 669 else 670 S += "#("; 671 unsigned NumArgs = N->arity()-1; 672 unsigned ci = getNextSibling(i+1); 673 for (unsigned k=0; k<NumArgs; ++k, ci = getNextSibling(ci)) { 674 S += toString(ci); 675 if (k+1 < NumArgs) S += ","; 676 } 677 S += ")"; 678 return S; 679 } 680 case EOP_Index: { 681 std::string S1 = toString(i+1); 682 std::string S2 = toString(i+1 + NodeVec[i+1].size()); 683 return S1 + "[" + S2 + "]"; 684 } 685 case EOP_Unary: { 686 std::string S = toString(i+1); 687 return "#" + S; 688 } 689 case EOP_Binary: { 690 std::string S1 = toString(i+1); 691 std::string S2 = toString(i+1 + NodeVec[i+1].size()); 692 return "(" + S1 + "#" + S2 + ")"; 693 } 694 case EOP_Unknown: { 695 unsigned NumChildren = N->arity(); 696 if (NumChildren == 0) 697 return "(...)"; 698 std::string S = "("; 699 unsigned ci = i+1; 700 for (unsigned j = 0; j < NumChildren; ++j, ci = getNextSibling(ci)) { 701 S += toString(ci); 702 if (j+1 < NumChildren) S += "#"; 703 } 704 S += ")"; 705 return S; 706 } 707 } 708 return ""; 709 } 710 }; 711 712 /// \brief A short list of SExprs 713 class MutexIDList : public SmallVector<SExpr, 3> { 714 public: 715 /// \brief Push M onto list, but discard duplicates. 716 void push_back_nodup(const SExpr& M) { 717 if (end() == std::find(begin(), end(), M)) 718 push_back(M); 719 } 720 }; 721 722 /// \brief This is a helper class that stores info about the most recent 723 /// accquire of a Lock. 724 /// 725 /// The main body of the analysis maps MutexIDs to LockDatas. 726 struct LockData { 727 SourceLocation AcquireLoc; 728 729 /// \brief LKind stores whether a lock is held shared or exclusively. 730 /// Note that this analysis does not currently support either re-entrant 731 /// locking or lock "upgrading" and "downgrading" between exclusive and 732 /// shared. 733 /// 734 /// FIXME: add support for re-entrant locking and lock up/downgrading 735 LockKind LKind; 736 bool Asserted; // for asserted locks 737 bool Managed; // for ScopedLockable objects 738 SExpr UnderlyingMutex; // for ScopedLockable objects 739 740 LockData(SourceLocation AcquireLoc, LockKind LKind, bool M=false, 741 bool Asrt=false) 742 : AcquireLoc(AcquireLoc), LKind(LKind), Asserted(Asrt), Managed(M), 743 UnderlyingMutex(Decl::EmptyShell()) 744 {} 745 746 LockData(SourceLocation AcquireLoc, LockKind LKind, const SExpr &Mu) 747 : AcquireLoc(AcquireLoc), LKind(LKind), Asserted(false), Managed(false), 748 UnderlyingMutex(Mu) 749 {} 750 751 bool operator==(const LockData &other) const { 752 return AcquireLoc == other.AcquireLoc && LKind == other.LKind; 753 } 754 755 bool operator!=(const LockData &other) const { 756 return !(*this == other); 757 } 758 759 void Profile(llvm::FoldingSetNodeID &ID) const { 760 ID.AddInteger(AcquireLoc.getRawEncoding()); 761 ID.AddInteger(LKind); 762 } 763 764 bool isAtLeast(LockKind LK) { 765 return (LK == LK_Shared) || (LKind == LK_Exclusive); 766 } 767 }; 768 769 770 /// \brief A FactEntry stores a single fact that is known at a particular point 771 /// in the program execution. Currently, this is information regarding a lock 772 /// that is held at that point. 773 struct FactEntry { 774 SExpr MutID; 775 LockData LDat; 776 777 FactEntry(const SExpr& M, const LockData& L) 778 : MutID(M), LDat(L) 779 { } 780 }; 781 782 783 typedef unsigned short FactID; 784 785 /// \brief FactManager manages the memory for all facts that are created during 786 /// the analysis of a single routine. 787 class FactManager { 788 private: 789 std::vector<FactEntry> Facts; 790 791 public: 792 FactID newLock(const SExpr& M, const LockData& L) { 793 Facts.push_back(FactEntry(M,L)); 794 return static_cast<unsigned short>(Facts.size() - 1); 795 } 796 797 const FactEntry& operator[](FactID F) const { return Facts[F]; } 798 FactEntry& operator[](FactID F) { return Facts[F]; } 799 }; 800 801 802 /// \brief A FactSet is the set of facts that are known to be true at a 803 /// particular program point. FactSets must be small, because they are 804 /// frequently copied, and are thus implemented as a set of indices into a 805 /// table maintained by a FactManager. A typical FactSet only holds 1 or 2 806 /// locks, so we can get away with doing a linear search for lookup. Note 807 /// that a hashtable or map is inappropriate in this case, because lookups 808 /// may involve partial pattern matches, rather than exact matches. 809 class FactSet { 810 private: 811 typedef SmallVector<FactID, 4> FactVec; 812 813 FactVec FactIDs; 814 815 public: 816 typedef FactVec::iterator iterator; 817 typedef FactVec::const_iterator const_iterator; 818 819 iterator begin() { return FactIDs.begin(); } 820 const_iterator begin() const { return FactIDs.begin(); } 821 822 iterator end() { return FactIDs.end(); } 823 const_iterator end() const { return FactIDs.end(); } 824 825 bool isEmpty() const { return FactIDs.size() == 0; } 826 827 FactID addLock(FactManager& FM, const SExpr& M, const LockData& L) { 828 FactID F = FM.newLock(M, L); 829 FactIDs.push_back(F); 830 return F; 831 } 832 833 bool removeLock(FactManager& FM, const SExpr& M) { 834 unsigned n = FactIDs.size(); 835 if (n == 0) 836 return false; 837 838 for (unsigned i = 0; i < n-1; ++i) { 839 if (FM[FactIDs[i]].MutID.matches(M)) { 840 FactIDs[i] = FactIDs[n-1]; 841 FactIDs.pop_back(); 842 return true; 843 } 844 } 845 if (FM[FactIDs[n-1]].MutID.matches(M)) { 846 FactIDs.pop_back(); 847 return true; 848 } 849 return false; 850 } 851 852 iterator findLockIter(FactManager &FM, const SExpr &M) { 853 return std::find_if(begin(), end(), [&](FactID ID) { 854 return FM[ID].MutID.matches(M); 855 }); 856 } 857 858 LockData *findLock(FactManager &FM, const SExpr &M) const { 859 auto I = std::find_if(begin(), end(), [&](FactID ID) { 860 return FM[ID].MutID.matches(M); 861 }); 862 863 return I != end() ? &FM[*I].LDat : nullptr; 864 } 865 866 LockData *findLockUniv(FactManager &FM, const SExpr &M) const { 867 auto I = std::find_if(begin(), end(), [&](FactID ID) -> bool { 868 const SExpr &Expr = FM[ID].MutID; 869 return Expr.isUniversal() || Expr.matches(M); 870 }); 871 872 return I != end() ? &FM[*I].LDat : nullptr; 873 } 874 875 FactEntry *findPartialMatch(FactManager &FM, const SExpr &M) const { 876 auto I = std::find_if(begin(), end(), [&](FactID ID) { 877 return FM[ID].MutID.partiallyMatches(M); 878 }); 879 880 return I != end() ? &FM[*I] : nullptr; 881 } 882 }; 883 884 /// A Lockset maps each SExpr (defined above) to information about how it has 885 /// been locked. 886 typedef llvm::ImmutableMap<SExpr, LockData> Lockset; 887 typedef llvm::ImmutableMap<const NamedDecl*, unsigned> LocalVarContext; 888 889 class LocalVariableMap; 890 891 /// A side (entry or exit) of a CFG node. 892 enum CFGBlockSide { CBS_Entry, CBS_Exit }; 893 894 /// CFGBlockInfo is a struct which contains all the information that is 895 /// maintained for each block in the CFG. See LocalVariableMap for more 896 /// information about the contexts. 897 struct CFGBlockInfo { 898 FactSet EntrySet; // Lockset held at entry to block 899 FactSet ExitSet; // Lockset held at exit from block 900 LocalVarContext EntryContext; // Context held at entry to block 901 LocalVarContext ExitContext; // Context held at exit from block 902 SourceLocation EntryLoc; // Location of first statement in block 903 SourceLocation ExitLoc; // Location of last statement in block. 904 unsigned EntryIndex; // Used to replay contexts later 905 bool Reachable; // Is this block reachable? 906 907 const FactSet &getSet(CFGBlockSide Side) const { 908 return Side == CBS_Entry ? EntrySet : ExitSet; 909 } 910 SourceLocation getLocation(CFGBlockSide Side) const { 911 return Side == CBS_Entry ? EntryLoc : ExitLoc; 912 } 913 914 private: 915 CFGBlockInfo(LocalVarContext EmptyCtx) 916 : EntryContext(EmptyCtx), ExitContext(EmptyCtx), Reachable(false) 917 { } 918 919 public: 920 static CFGBlockInfo getEmptyBlockInfo(LocalVariableMap &M); 921 }; 922 923 924 925 // A LocalVariableMap maintains a map from local variables to their currently 926 // valid definitions. It provides SSA-like functionality when traversing the 927 // CFG. Like SSA, each definition or assignment to a variable is assigned a 928 // unique name (an integer), which acts as the SSA name for that definition. 929 // The total set of names is shared among all CFG basic blocks. 930 // Unlike SSA, we do not rewrite expressions to replace local variables declrefs 931 // with their SSA-names. Instead, we compute a Context for each point in the 932 // code, which maps local variables to the appropriate SSA-name. This map 933 // changes with each assignment. 934 // 935 // The map is computed in a single pass over the CFG. Subsequent analyses can 936 // then query the map to find the appropriate Context for a statement, and use 937 // that Context to look up the definitions of variables. 938 class LocalVariableMap { 939 public: 940 typedef LocalVarContext Context; 941 942 /// A VarDefinition consists of an expression, representing the value of the 943 /// variable, along with the context in which that expression should be 944 /// interpreted. A reference VarDefinition does not itself contain this 945 /// information, but instead contains a pointer to a previous VarDefinition. 946 struct VarDefinition { 947 public: 948 friend class LocalVariableMap; 949 950 const NamedDecl *Dec; // The original declaration for this variable. 951 const Expr *Exp; // The expression for this variable, OR 952 unsigned Ref; // Reference to another VarDefinition 953 Context Ctx; // The map with which Exp should be interpreted. 954 955 bool isReference() { return !Exp; } 956 957 private: 958 // Create ordinary variable definition 959 VarDefinition(const NamedDecl *D, const Expr *E, Context C) 960 : Dec(D), Exp(E), Ref(0), Ctx(C) 961 { } 962 963 // Create reference to previous definition 964 VarDefinition(const NamedDecl *D, unsigned R, Context C) 965 : Dec(D), Exp(0), Ref(R), Ctx(C) 966 { } 967 }; 968 969 private: 970 Context::Factory ContextFactory; 971 std::vector<VarDefinition> VarDefinitions; 972 std::vector<unsigned> CtxIndices; 973 std::vector<std::pair<Stmt*, Context> > SavedContexts; 974 975 public: 976 LocalVariableMap() { 977 // index 0 is a placeholder for undefined variables (aka phi-nodes). 978 VarDefinitions.push_back(VarDefinition(0, 0u, getEmptyContext())); 979 } 980 981 /// Look up a definition, within the given context. 982 const VarDefinition* lookup(const NamedDecl *D, Context Ctx) { 983 const unsigned *i = Ctx.lookup(D); 984 if (!i) 985 return 0; 986 assert(*i < VarDefinitions.size()); 987 return &VarDefinitions[*i]; 988 } 989 990 /// Look up the definition for D within the given context. Returns 991 /// NULL if the expression is not statically known. If successful, also 992 /// modifies Ctx to hold the context of the return Expr. 993 const Expr* lookupExpr(const NamedDecl *D, Context &Ctx) { 994 const unsigned *P = Ctx.lookup(D); 995 if (!P) 996 return 0; 997 998 unsigned i = *P; 999 while (i > 0) { 1000 if (VarDefinitions[i].Exp) { 1001 Ctx = VarDefinitions[i].Ctx; 1002 return VarDefinitions[i].Exp; 1003 } 1004 i = VarDefinitions[i].Ref; 1005 } 1006 return 0; 1007 } 1008 1009 Context getEmptyContext() { return ContextFactory.getEmptyMap(); } 1010 1011 /// Return the next context after processing S. This function is used by 1012 /// clients of the class to get the appropriate context when traversing the 1013 /// CFG. It must be called for every assignment or DeclStmt. 1014 Context getNextContext(unsigned &CtxIndex, Stmt *S, Context C) { 1015 if (SavedContexts[CtxIndex+1].first == S) { 1016 CtxIndex++; 1017 Context Result = SavedContexts[CtxIndex].second; 1018 return Result; 1019 } 1020 return C; 1021 } 1022 1023 void dumpVarDefinitionName(unsigned i) { 1024 if (i == 0) { 1025 llvm::errs() << "Undefined"; 1026 return; 1027 } 1028 const NamedDecl *Dec = VarDefinitions[i].Dec; 1029 if (!Dec) { 1030 llvm::errs() << "<<NULL>>"; 1031 return; 1032 } 1033 Dec->printName(llvm::errs()); 1034 llvm::errs() << "." << i << " " << ((const void*) Dec); 1035 } 1036 1037 /// Dumps an ASCII representation of the variable map to llvm::errs() 1038 void dump() { 1039 for (unsigned i = 1, e = VarDefinitions.size(); i < e; ++i) { 1040 const Expr *Exp = VarDefinitions[i].Exp; 1041 unsigned Ref = VarDefinitions[i].Ref; 1042 1043 dumpVarDefinitionName(i); 1044 llvm::errs() << " = "; 1045 if (Exp) Exp->dump(); 1046 else { 1047 dumpVarDefinitionName(Ref); 1048 llvm::errs() << "\n"; 1049 } 1050 } 1051 } 1052 1053 /// Dumps an ASCII representation of a Context to llvm::errs() 1054 void dumpContext(Context C) { 1055 for (Context::iterator I = C.begin(), E = C.end(); I != E; ++I) { 1056 const NamedDecl *D = I.getKey(); 1057 D->printName(llvm::errs()); 1058 const unsigned *i = C.lookup(D); 1059 llvm::errs() << " -> "; 1060 dumpVarDefinitionName(*i); 1061 llvm::errs() << "\n"; 1062 } 1063 } 1064 1065 /// Builds the variable map. 1066 void traverseCFG(CFG *CFGraph, const PostOrderCFGView *SortedGraph, 1067 std::vector<CFGBlockInfo> &BlockInfo); 1068 1069 protected: 1070 // Get the current context index 1071 unsigned getContextIndex() { return SavedContexts.size()-1; } 1072 1073 // Save the current context for later replay 1074 void saveContext(Stmt *S, Context C) { 1075 SavedContexts.push_back(std::make_pair(S,C)); 1076 } 1077 1078 // Adds a new definition to the given context, and returns a new context. 1079 // This method should be called when declaring a new variable. 1080 Context addDefinition(const NamedDecl *D, const Expr *Exp, Context Ctx) { 1081 assert(!Ctx.contains(D)); 1082 unsigned newID = VarDefinitions.size(); 1083 Context NewCtx = ContextFactory.add(Ctx, D, newID); 1084 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx)); 1085 return NewCtx; 1086 } 1087 1088 // Add a new reference to an existing definition. 1089 Context addReference(const NamedDecl *D, unsigned i, Context Ctx) { 1090 unsigned newID = VarDefinitions.size(); 1091 Context NewCtx = ContextFactory.add(Ctx, D, newID); 1092 VarDefinitions.push_back(VarDefinition(D, i, Ctx)); 1093 return NewCtx; 1094 } 1095 1096 // Updates a definition only if that definition is already in the map. 1097 // This method should be called when assigning to an existing variable. 1098 Context updateDefinition(const NamedDecl *D, Expr *Exp, Context Ctx) { 1099 if (Ctx.contains(D)) { 1100 unsigned newID = VarDefinitions.size(); 1101 Context NewCtx = ContextFactory.remove(Ctx, D); 1102 NewCtx = ContextFactory.add(NewCtx, D, newID); 1103 VarDefinitions.push_back(VarDefinition(D, Exp, Ctx)); 1104 return NewCtx; 1105 } 1106 return Ctx; 1107 } 1108 1109 // Removes a definition from the context, but keeps the variable name 1110 // as a valid variable. The index 0 is a placeholder for cleared definitions. 1111 Context clearDefinition(const NamedDecl *D, Context Ctx) { 1112 Context NewCtx = Ctx; 1113 if (NewCtx.contains(D)) { 1114 NewCtx = ContextFactory.remove(NewCtx, D); 1115 NewCtx = ContextFactory.add(NewCtx, D, 0); 1116 } 1117 return NewCtx; 1118 } 1119 1120 // Remove a definition entirely frmo the context. 1121 Context removeDefinition(const NamedDecl *D, Context Ctx) { 1122 Context NewCtx = Ctx; 1123 if (NewCtx.contains(D)) { 1124 NewCtx = ContextFactory.remove(NewCtx, D); 1125 } 1126 return NewCtx; 1127 } 1128 1129 Context intersectContexts(Context C1, Context C2); 1130 Context createReferenceContext(Context C); 1131 void intersectBackEdge(Context C1, Context C2); 1132 1133 friend class VarMapBuilder; 1134 }; 1135 1136 1137 // This has to be defined after LocalVariableMap. 1138 CFGBlockInfo CFGBlockInfo::getEmptyBlockInfo(LocalVariableMap &M) { 1139 return CFGBlockInfo(M.getEmptyContext()); 1140 } 1141 1142 1143 /// Visitor which builds a LocalVariableMap 1144 class VarMapBuilder : public StmtVisitor<VarMapBuilder> { 1145 public: 1146 LocalVariableMap* VMap; 1147 LocalVariableMap::Context Ctx; 1148 1149 VarMapBuilder(LocalVariableMap *VM, LocalVariableMap::Context C) 1150 : VMap(VM), Ctx(C) {} 1151 1152 void VisitDeclStmt(DeclStmt *S); 1153 void VisitBinaryOperator(BinaryOperator *BO); 1154 }; 1155 1156 1157 // Add new local variables to the variable map 1158 void VarMapBuilder::VisitDeclStmt(DeclStmt *S) { 1159 bool modifiedCtx = false; 1160 DeclGroupRef DGrp = S->getDeclGroup(); 1161 for (const auto *D : DGrp) { 1162 if (const auto *VD = dyn_cast_or_null<VarDecl>(D)) { 1163 const Expr *E = VD->getInit(); 1164 1165 // Add local variables with trivial type to the variable map 1166 QualType T = VD->getType(); 1167 if (T.isTrivialType(VD->getASTContext())) { 1168 Ctx = VMap->addDefinition(VD, E, Ctx); 1169 modifiedCtx = true; 1170 } 1171 } 1172 } 1173 if (modifiedCtx) 1174 VMap->saveContext(S, Ctx); 1175 } 1176 1177 // Update local variable definitions in variable map 1178 void VarMapBuilder::VisitBinaryOperator(BinaryOperator *BO) { 1179 if (!BO->isAssignmentOp()) 1180 return; 1181 1182 Expr *LHSExp = BO->getLHS()->IgnoreParenCasts(); 1183 1184 // Update the variable map and current context. 1185 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LHSExp)) { 1186 ValueDecl *VDec = DRE->getDecl(); 1187 if (Ctx.lookup(VDec)) { 1188 if (BO->getOpcode() == BO_Assign) 1189 Ctx = VMap->updateDefinition(VDec, BO->getRHS(), Ctx); 1190 else 1191 // FIXME -- handle compound assignment operators 1192 Ctx = VMap->clearDefinition(VDec, Ctx); 1193 VMap->saveContext(BO, Ctx); 1194 } 1195 } 1196 } 1197 1198 1199 // Computes the intersection of two contexts. The intersection is the 1200 // set of variables which have the same definition in both contexts; 1201 // variables with different definitions are discarded. 1202 LocalVariableMap::Context 1203 LocalVariableMap::intersectContexts(Context C1, Context C2) { 1204 Context Result = C1; 1205 for (const auto &P : C1) { 1206 const NamedDecl *Dec = P.first; 1207 const unsigned *i2 = C2.lookup(Dec); 1208 if (!i2) // variable doesn't exist on second path 1209 Result = removeDefinition(Dec, Result); 1210 else if (*i2 != P.second) // variable exists, but has different definition 1211 Result = clearDefinition(Dec, Result); 1212 } 1213 return Result; 1214 } 1215 1216 // For every variable in C, create a new variable that refers to the 1217 // definition in C. Return a new context that contains these new variables. 1218 // (We use this for a naive implementation of SSA on loop back-edges.) 1219 LocalVariableMap::Context LocalVariableMap::createReferenceContext(Context C) { 1220 Context Result = getEmptyContext(); 1221 for (const auto &P : C) 1222 Result = addReference(P.first, P.second, Result); 1223 return Result; 1224 } 1225 1226 // This routine also takes the intersection of C1 and C2, but it does so by 1227 // altering the VarDefinitions. C1 must be the result of an earlier call to 1228 // createReferenceContext. 1229 void LocalVariableMap::intersectBackEdge(Context C1, Context C2) { 1230 for (const auto &P : C1) { 1231 unsigned i1 = P.second; 1232 VarDefinition *VDef = &VarDefinitions[i1]; 1233 assert(VDef->isReference()); 1234 1235 const unsigned *i2 = C2.lookup(P.first); 1236 if (!i2 || (*i2 != i1)) 1237 VDef->Ref = 0; // Mark this variable as undefined 1238 } 1239 } 1240 1241 1242 // Traverse the CFG in topological order, so all predecessors of a block 1243 // (excluding back-edges) are visited before the block itself. At 1244 // each point in the code, we calculate a Context, which holds the set of 1245 // variable definitions which are visible at that point in execution. 1246 // Visible variables are mapped to their definitions using an array that 1247 // contains all definitions. 1248 // 1249 // At join points in the CFG, the set is computed as the intersection of 1250 // the incoming sets along each edge, E.g. 1251 // 1252 // { Context | VarDefinitions } 1253 // int x = 0; { x -> x1 | x1 = 0 } 1254 // int y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 } 1255 // if (b) x = 1; { x -> x2, y -> y1 | x2 = 1, y1 = 0, ... } 1256 // else x = 2; { x -> x3, y -> y1 | x3 = 2, x2 = 1, ... } 1257 // ... { y -> y1 (x is unknown) | x3 = 2, x2 = 1, ... } 1258 // 1259 // This is essentially a simpler and more naive version of the standard SSA 1260 // algorithm. Those definitions that remain in the intersection are from blocks 1261 // that strictly dominate the current block. We do not bother to insert proper 1262 // phi nodes, because they are not used in our analysis; instead, wherever 1263 // a phi node would be required, we simply remove that definition from the 1264 // context (E.g. x above). 1265 // 1266 // The initial traversal does not capture back-edges, so those need to be 1267 // handled on a separate pass. Whenever the first pass encounters an 1268 // incoming back edge, it duplicates the context, creating new definitions 1269 // that refer back to the originals. (These correspond to places where SSA 1270 // might have to insert a phi node.) On the second pass, these definitions are 1271 // set to NULL if the variable has changed on the back-edge (i.e. a phi 1272 // node was actually required.) E.g. 1273 // 1274 // { Context | VarDefinitions } 1275 // int x = 0, y = 0; { x -> x1, y -> y1 | y1 = 0, x1 = 0 } 1276 // while (b) { x -> x2, y -> y1 | [1st:] x2=x1; [2nd:] x2=NULL; } 1277 // x = x+1; { x -> x3, y -> y1 | x3 = x2 + 1, ... } 1278 // ... { y -> y1 | x3 = 2, x2 = 1, ... } 1279 // 1280 void LocalVariableMap::traverseCFG(CFG *CFGraph, 1281 const PostOrderCFGView *SortedGraph, 1282 std::vector<CFGBlockInfo> &BlockInfo) { 1283 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph); 1284 1285 CtxIndices.resize(CFGraph->getNumBlockIDs()); 1286 1287 for (const auto *CurrBlock : *SortedGraph) { 1288 int CurrBlockID = CurrBlock->getBlockID(); 1289 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID]; 1290 1291 VisitedBlocks.insert(CurrBlock); 1292 1293 // Calculate the entry context for the current block 1294 bool HasBackEdges = false; 1295 bool CtxInit = true; 1296 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(), 1297 PE = CurrBlock->pred_end(); PI != PE; ++PI) { 1298 // if *PI -> CurrBlock is a back edge, so skip it 1299 if (*PI == 0 || !VisitedBlocks.alreadySet(*PI)) { 1300 HasBackEdges = true; 1301 continue; 1302 } 1303 1304 int PrevBlockID = (*PI)->getBlockID(); 1305 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; 1306 1307 if (CtxInit) { 1308 CurrBlockInfo->EntryContext = PrevBlockInfo->ExitContext; 1309 CtxInit = false; 1310 } 1311 else { 1312 CurrBlockInfo->EntryContext = 1313 intersectContexts(CurrBlockInfo->EntryContext, 1314 PrevBlockInfo->ExitContext); 1315 } 1316 } 1317 1318 // Duplicate the context if we have back-edges, so we can call 1319 // intersectBackEdges later. 1320 if (HasBackEdges) 1321 CurrBlockInfo->EntryContext = 1322 createReferenceContext(CurrBlockInfo->EntryContext); 1323 1324 // Create a starting context index for the current block 1325 saveContext(0, CurrBlockInfo->EntryContext); 1326 CurrBlockInfo->EntryIndex = getContextIndex(); 1327 1328 // Visit all the statements in the basic block. 1329 VarMapBuilder VMapBuilder(this, CurrBlockInfo->EntryContext); 1330 for (CFGBlock::const_iterator BI = CurrBlock->begin(), 1331 BE = CurrBlock->end(); BI != BE; ++BI) { 1332 switch (BI->getKind()) { 1333 case CFGElement::Statement: { 1334 CFGStmt CS = BI->castAs<CFGStmt>(); 1335 VMapBuilder.Visit(const_cast<Stmt*>(CS.getStmt())); 1336 break; 1337 } 1338 default: 1339 break; 1340 } 1341 } 1342 CurrBlockInfo->ExitContext = VMapBuilder.Ctx; 1343 1344 // Mark variables on back edges as "unknown" if they've been changed. 1345 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), 1346 SE = CurrBlock->succ_end(); SI != SE; ++SI) { 1347 // if CurrBlock -> *SI is *not* a back edge 1348 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI)) 1349 continue; 1350 1351 CFGBlock *FirstLoopBlock = *SI; 1352 Context LoopBegin = BlockInfo[FirstLoopBlock->getBlockID()].EntryContext; 1353 Context LoopEnd = CurrBlockInfo->ExitContext; 1354 intersectBackEdge(LoopBegin, LoopEnd); 1355 } 1356 } 1357 1358 // Put an extra entry at the end of the indexed context array 1359 unsigned exitID = CFGraph->getExit().getBlockID(); 1360 saveContext(0, BlockInfo[exitID].ExitContext); 1361 } 1362 1363 /// Find the appropriate source locations to use when producing diagnostics for 1364 /// each block in the CFG. 1365 static void findBlockLocations(CFG *CFGraph, 1366 const PostOrderCFGView *SortedGraph, 1367 std::vector<CFGBlockInfo> &BlockInfo) { 1368 for (const auto *CurrBlock : *SortedGraph) { 1369 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlock->getBlockID()]; 1370 1371 // Find the source location of the last statement in the block, if the 1372 // block is not empty. 1373 if (const Stmt *S = CurrBlock->getTerminator()) { 1374 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = S->getLocStart(); 1375 } else { 1376 for (CFGBlock::const_reverse_iterator BI = CurrBlock->rbegin(), 1377 BE = CurrBlock->rend(); BI != BE; ++BI) { 1378 // FIXME: Handle other CFGElement kinds. 1379 if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) { 1380 CurrBlockInfo->ExitLoc = CS->getStmt()->getLocStart(); 1381 break; 1382 } 1383 } 1384 } 1385 1386 if (!CurrBlockInfo->ExitLoc.isInvalid()) { 1387 // This block contains at least one statement. Find the source location 1388 // of the first statement in the block. 1389 for (CFGBlock::const_iterator BI = CurrBlock->begin(), 1390 BE = CurrBlock->end(); BI != BE; ++BI) { 1391 // FIXME: Handle other CFGElement kinds. 1392 if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) { 1393 CurrBlockInfo->EntryLoc = CS->getStmt()->getLocStart(); 1394 break; 1395 } 1396 } 1397 } else if (CurrBlock->pred_size() == 1 && *CurrBlock->pred_begin() && 1398 CurrBlock != &CFGraph->getExit()) { 1399 // The block is empty, and has a single predecessor. Use its exit 1400 // location. 1401 CurrBlockInfo->EntryLoc = CurrBlockInfo->ExitLoc = 1402 BlockInfo[(*CurrBlock->pred_begin())->getBlockID()].ExitLoc; 1403 } 1404 } 1405 } 1406 1407 /// \brief Class which implements the core thread safety analysis routines. 1408 class ThreadSafetyAnalyzer { 1409 friend class BuildLockset; 1410 1411 ThreadSafetyHandler &Handler; 1412 LocalVariableMap LocalVarMap; 1413 FactManager FactMan; 1414 std::vector<CFGBlockInfo> BlockInfo; 1415 1416 public: 1417 ThreadSafetyAnalyzer(ThreadSafetyHandler &H) : Handler(H) {} 1418 1419 void addLock(FactSet &FSet, const SExpr &Mutex, const LockData &LDat, 1420 StringRef DiagKind); 1421 void removeLock(FactSet &FSet, const SExpr &Mutex, SourceLocation UnlockLoc, 1422 bool FullyRemove, LockKind Kind, StringRef DiagKind); 1423 1424 template <typename AttrType> 1425 void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp, 1426 const NamedDecl *D, VarDecl *SelfDecl=0); 1427 1428 template <class AttrType> 1429 void getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, Expr *Exp, 1430 const NamedDecl *D, 1431 const CFGBlock *PredBlock, const CFGBlock *CurrBlock, 1432 Expr *BrE, bool Neg); 1433 1434 const CallExpr* getTrylockCallExpr(const Stmt *Cond, LocalVarContext C, 1435 bool &Negate); 1436 1437 void getEdgeLockset(FactSet &Result, const FactSet &ExitSet, 1438 const CFGBlock* PredBlock, 1439 const CFGBlock *CurrBlock); 1440 1441 void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2, 1442 SourceLocation JoinLoc, 1443 LockErrorKind LEK1, LockErrorKind LEK2, 1444 bool Modify=true); 1445 1446 void intersectAndWarn(FactSet &FSet1, const FactSet &FSet2, 1447 SourceLocation JoinLoc, LockErrorKind LEK1, 1448 bool Modify=true) { 1449 intersectAndWarn(FSet1, FSet2, JoinLoc, LEK1, LEK1, Modify); 1450 } 1451 1452 void runAnalysis(AnalysisDeclContext &AC); 1453 }; 1454 1455 /// \brief Gets the value decl pointer from DeclRefExprs or MemberExprs. 1456 static const ValueDecl *getValueDecl(const Expr *Exp) { 1457 if (const auto *CE = dyn_cast<ImplicitCastExpr>(Exp)) 1458 return getValueDecl(CE->getSubExpr()); 1459 1460 if (const auto *DR = dyn_cast<DeclRefExpr>(Exp)) 1461 return DR->getDecl(); 1462 1463 if (const auto *ME = dyn_cast<MemberExpr>(Exp)) 1464 return ME->getMemberDecl(); 1465 1466 return nullptr; 1467 } 1468 1469 template <typename Ty> 1470 class has_arg_iterator_range { 1471 typedef char yes[1]; 1472 typedef char no[2]; 1473 1474 template <typename Inner> 1475 static yes& test(Inner *I, decltype(I->args()) * = nullptr); 1476 1477 template <typename> 1478 static no& test(...); 1479 1480 public: 1481 static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes); 1482 }; 1483 1484 static StringRef ClassifyDiagnostic(const CapabilityAttr *A) { 1485 return A->getName(); 1486 } 1487 1488 static StringRef ClassifyDiagnostic(QualType VDT) { 1489 // We need to look at the declaration of the type of the value to determine 1490 // which it is. The type should either be a record or a typedef, or a pointer 1491 // or reference thereof. 1492 if (const auto *RT = VDT->getAs<RecordType>()) { 1493 if (const auto *RD = RT->getDecl()) 1494 if (const auto *CA = RD->getAttr<CapabilityAttr>()) 1495 return ClassifyDiagnostic(CA); 1496 } else if (const auto *TT = VDT->getAs<TypedefType>()) { 1497 if (const auto *TD = TT->getDecl()) 1498 if (const auto *CA = TD->getAttr<CapabilityAttr>()) 1499 return ClassifyDiagnostic(CA); 1500 } else if (VDT->isPointerType() || VDT->isReferenceType()) 1501 return ClassifyDiagnostic(VDT->getPointeeType()); 1502 1503 return "mutex"; 1504 } 1505 1506 static StringRef ClassifyDiagnostic(const ValueDecl *VD) { 1507 assert(VD && "No ValueDecl passed"); 1508 1509 // The ValueDecl is the declaration of a mutex or role (hopefully). 1510 return ClassifyDiagnostic(VD->getType()); 1511 } 1512 1513 template <typename AttrTy> 1514 static typename std::enable_if<!has_arg_iterator_range<AttrTy>::value, 1515 StringRef>::type 1516 ClassifyDiagnostic(const AttrTy *A) { 1517 if (const ValueDecl *VD = getValueDecl(A->getArg())) 1518 return ClassifyDiagnostic(VD); 1519 return "mutex"; 1520 } 1521 1522 template <typename AttrTy> 1523 static typename std::enable_if<has_arg_iterator_range<AttrTy>::value, 1524 StringRef>::type 1525 ClassifyDiagnostic(const AttrTy *A) { 1526 for (const auto *Arg : A->args()) { 1527 if (const ValueDecl *VD = getValueDecl(Arg)) 1528 return ClassifyDiagnostic(VD); 1529 } 1530 return "mutex"; 1531 } 1532 1533 /// \brief Add a new lock to the lockset, warning if the lock is already there. 1534 /// \param Mutex -- the Mutex expression for the lock 1535 /// \param LDat -- the LockData for the lock 1536 void ThreadSafetyAnalyzer::addLock(FactSet &FSet, const SExpr &Mutex, 1537 const LockData &LDat, StringRef DiagKind) { 1538 // FIXME: deal with acquired before/after annotations. 1539 // FIXME: Don't always warn when we have support for reentrant locks. 1540 if (Mutex.shouldIgnore()) 1541 return; 1542 1543 if (FSet.findLock(FactMan, Mutex)) { 1544 if (!LDat.Asserted) 1545 Handler.handleDoubleLock(DiagKind, Mutex.toString(), LDat.AcquireLoc); 1546 } else { 1547 FSet.addLock(FactMan, Mutex, LDat); 1548 } 1549 } 1550 1551 1552 /// \brief Remove a lock from the lockset, warning if the lock is not there. 1553 /// \param Mutex The lock expression corresponding to the lock to be removed 1554 /// \param UnlockLoc The source location of the unlock (only used in error msg) 1555 void ThreadSafetyAnalyzer::removeLock(FactSet &FSet, const SExpr &Mutex, 1556 SourceLocation UnlockLoc, 1557 bool FullyRemove, LockKind ReceivedKind, 1558 StringRef DiagKind) { 1559 if (Mutex.shouldIgnore()) 1560 return; 1561 1562 const LockData *LDat = FSet.findLock(FactMan, Mutex); 1563 if (!LDat) { 1564 Handler.handleUnmatchedUnlock(DiagKind, Mutex.toString(), UnlockLoc); 1565 return; 1566 } 1567 1568 // Generic lock removal doesn't care about lock kind mismatches, but 1569 // otherwise diagnose when the lock kinds are mismatched. 1570 if (ReceivedKind != LK_Generic && LDat->LKind != ReceivedKind) { 1571 Handler.handleIncorrectUnlockKind(DiagKind, Mutex.toString(), LDat->LKind, 1572 ReceivedKind, UnlockLoc); 1573 return; 1574 } 1575 1576 if (LDat->UnderlyingMutex.isValid()) { 1577 // This is scoped lockable object, which manages the real mutex. 1578 if (FullyRemove) { 1579 // We're destroying the managing object. 1580 // Remove the underlying mutex if it exists; but don't warn. 1581 if (FSet.findLock(FactMan, LDat->UnderlyingMutex)) 1582 FSet.removeLock(FactMan, LDat->UnderlyingMutex); 1583 } else { 1584 // We're releasing the underlying mutex, but not destroying the 1585 // managing object. Warn on dual release. 1586 if (!FSet.findLock(FactMan, LDat->UnderlyingMutex)) { 1587 Handler.handleUnmatchedUnlock( 1588 DiagKind, LDat->UnderlyingMutex.toString(), UnlockLoc); 1589 } 1590 FSet.removeLock(FactMan, LDat->UnderlyingMutex); 1591 return; 1592 } 1593 } 1594 FSet.removeLock(FactMan, Mutex); 1595 } 1596 1597 1598 /// \brief Extract the list of mutexIDs from the attribute on an expression, 1599 /// and push them onto Mtxs, discarding any duplicates. 1600 template <typename AttrType> 1601 void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, 1602 Expr *Exp, const NamedDecl *D, 1603 VarDecl *SelfDecl) { 1604 if (Attr->args_size() == 0) { 1605 // The mutex held is the "this" object. 1606 SExpr Mu(0, Exp, D, SelfDecl); 1607 if (!Mu.isValid()) 1608 SExpr::warnInvalidLock(Handler, 0, Exp, D, ClassifyDiagnostic(Attr)); 1609 else 1610 Mtxs.push_back_nodup(Mu); 1611 return; 1612 } 1613 1614 for (const auto *Arg : Attr->args()) { 1615 SExpr Mu(Arg, Exp, D, SelfDecl); 1616 if (!Mu.isValid()) 1617 SExpr::warnInvalidLock(Handler, Arg, Exp, D, ClassifyDiagnostic(Attr)); 1618 else 1619 Mtxs.push_back_nodup(Mu); 1620 } 1621 } 1622 1623 1624 /// \brief Extract the list of mutexIDs from a trylock attribute. If the 1625 /// trylock applies to the given edge, then push them onto Mtxs, discarding 1626 /// any duplicates. 1627 template <class AttrType> 1628 void ThreadSafetyAnalyzer::getMutexIDs(MutexIDList &Mtxs, AttrType *Attr, 1629 Expr *Exp, const NamedDecl *D, 1630 const CFGBlock *PredBlock, 1631 const CFGBlock *CurrBlock, 1632 Expr *BrE, bool Neg) { 1633 // Find out which branch has the lock 1634 bool branch = false; 1635 if (CXXBoolLiteralExpr *BLE = dyn_cast_or_null<CXXBoolLiteralExpr>(BrE)) 1636 branch = BLE->getValue(); 1637 else if (IntegerLiteral *ILE = dyn_cast_or_null<IntegerLiteral>(BrE)) 1638 branch = ILE->getValue().getBoolValue(); 1639 1640 int branchnum = branch ? 0 : 1; 1641 if (Neg) 1642 branchnum = !branchnum; 1643 1644 // If we've taken the trylock branch, then add the lock 1645 int i = 0; 1646 for (CFGBlock::const_succ_iterator SI = PredBlock->succ_begin(), 1647 SE = PredBlock->succ_end(); SI != SE && i < 2; ++SI, ++i) { 1648 if (*SI == CurrBlock && i == branchnum) 1649 getMutexIDs(Mtxs, Attr, Exp, D); 1650 } 1651 } 1652 1653 1654 bool getStaticBooleanValue(Expr* E, bool& TCond) { 1655 if (isa<CXXNullPtrLiteralExpr>(E) || isa<GNUNullExpr>(E)) { 1656 TCond = false; 1657 return true; 1658 } else if (CXXBoolLiteralExpr *BLE = dyn_cast<CXXBoolLiteralExpr>(E)) { 1659 TCond = BLE->getValue(); 1660 return true; 1661 } else if (IntegerLiteral *ILE = dyn_cast<IntegerLiteral>(E)) { 1662 TCond = ILE->getValue().getBoolValue(); 1663 return true; 1664 } else if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { 1665 return getStaticBooleanValue(CE->getSubExpr(), TCond); 1666 } 1667 return false; 1668 } 1669 1670 1671 // If Cond can be traced back to a function call, return the call expression. 1672 // The negate variable should be called with false, and will be set to true 1673 // if the function call is negated, e.g. if (!mu.tryLock(...)) 1674 const CallExpr* ThreadSafetyAnalyzer::getTrylockCallExpr(const Stmt *Cond, 1675 LocalVarContext C, 1676 bool &Negate) { 1677 if (!Cond) 1678 return 0; 1679 1680 if (const CallExpr *CallExp = dyn_cast<CallExpr>(Cond)) { 1681 return CallExp; 1682 } 1683 else if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) { 1684 return getTrylockCallExpr(PE->getSubExpr(), C, Negate); 1685 } 1686 else if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(Cond)) { 1687 return getTrylockCallExpr(CE->getSubExpr(), C, Negate); 1688 } 1689 else if (const ExprWithCleanups* EWC = dyn_cast<ExprWithCleanups>(Cond)) { 1690 return getTrylockCallExpr(EWC->getSubExpr(), C, Negate); 1691 } 1692 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Cond)) { 1693 const Expr *E = LocalVarMap.lookupExpr(DRE->getDecl(), C); 1694 return getTrylockCallExpr(E, C, Negate); 1695 } 1696 else if (const UnaryOperator *UOP = dyn_cast<UnaryOperator>(Cond)) { 1697 if (UOP->getOpcode() == UO_LNot) { 1698 Negate = !Negate; 1699 return getTrylockCallExpr(UOP->getSubExpr(), C, Negate); 1700 } 1701 return 0; 1702 } 1703 else if (const BinaryOperator *BOP = dyn_cast<BinaryOperator>(Cond)) { 1704 if (BOP->getOpcode() == BO_EQ || BOP->getOpcode() == BO_NE) { 1705 if (BOP->getOpcode() == BO_NE) 1706 Negate = !Negate; 1707 1708 bool TCond = false; 1709 if (getStaticBooleanValue(BOP->getRHS(), TCond)) { 1710 if (!TCond) Negate = !Negate; 1711 return getTrylockCallExpr(BOP->getLHS(), C, Negate); 1712 } 1713 TCond = false; 1714 if (getStaticBooleanValue(BOP->getLHS(), TCond)) { 1715 if (!TCond) Negate = !Negate; 1716 return getTrylockCallExpr(BOP->getRHS(), C, Negate); 1717 } 1718 return 0; 1719 } 1720 if (BOP->getOpcode() == BO_LAnd) { 1721 // LHS must have been evaluated in a different block. 1722 return getTrylockCallExpr(BOP->getRHS(), C, Negate); 1723 } 1724 if (BOP->getOpcode() == BO_LOr) { 1725 return getTrylockCallExpr(BOP->getRHS(), C, Negate); 1726 } 1727 return 0; 1728 } 1729 return 0; 1730 } 1731 1732 1733 /// \brief Find the lockset that holds on the edge between PredBlock 1734 /// and CurrBlock. The edge set is the exit set of PredBlock (passed 1735 /// as the ExitSet parameter) plus any trylocks, which are conditionally held. 1736 void ThreadSafetyAnalyzer::getEdgeLockset(FactSet& Result, 1737 const FactSet &ExitSet, 1738 const CFGBlock *PredBlock, 1739 const CFGBlock *CurrBlock) { 1740 Result = ExitSet; 1741 1742 const Stmt *Cond = PredBlock->getTerminatorCondition(); 1743 if (!Cond) 1744 return; 1745 1746 bool Negate = false; 1747 const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID()]; 1748 const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext; 1749 StringRef CapDiagKind = "mutex"; 1750 1751 CallExpr *Exp = 1752 const_cast<CallExpr*>(getTrylockCallExpr(Cond, LVarCtx, Negate)); 1753 if (!Exp) 1754 return; 1755 1756 NamedDecl *FunDecl = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl()); 1757 if(!FunDecl || !FunDecl->hasAttrs()) 1758 return; 1759 1760 MutexIDList ExclusiveLocksToAdd; 1761 MutexIDList SharedLocksToAdd; 1762 1763 // If the condition is a call to a Trylock function, then grab the attributes 1764 for (auto *Attr : FunDecl->getAttrs()) { 1765 switch (Attr->getKind()) { 1766 case attr::ExclusiveTrylockFunction: { 1767 ExclusiveTrylockFunctionAttr *A = 1768 cast<ExclusiveTrylockFunctionAttr>(Attr); 1769 getMutexIDs(ExclusiveLocksToAdd, A, Exp, FunDecl, 1770 PredBlock, CurrBlock, A->getSuccessValue(), Negate); 1771 CapDiagKind = ClassifyDiagnostic(A); 1772 break; 1773 } 1774 case attr::SharedTrylockFunction: { 1775 SharedTrylockFunctionAttr *A = 1776 cast<SharedTrylockFunctionAttr>(Attr); 1777 getMutexIDs(SharedLocksToAdd, A, Exp, FunDecl, 1778 PredBlock, CurrBlock, A->getSuccessValue(), Negate); 1779 CapDiagKind = ClassifyDiagnostic(A); 1780 break; 1781 } 1782 default: 1783 break; 1784 } 1785 } 1786 1787 // Add and remove locks. 1788 SourceLocation Loc = Exp->getExprLoc(); 1789 for (const auto &ExclusiveLockToAdd : ExclusiveLocksToAdd) 1790 addLock(Result, ExclusiveLockToAdd, LockData(Loc, LK_Exclusive), 1791 CapDiagKind); 1792 for (const auto &SharedLockToAdd : SharedLocksToAdd) 1793 addLock(Result, SharedLockToAdd, LockData(Loc, LK_Shared), CapDiagKind); 1794 } 1795 1796 /// \brief We use this class to visit different types of expressions in 1797 /// CFGBlocks, and build up the lockset. 1798 /// An expression may cause us to add or remove locks from the lockset, or else 1799 /// output error messages related to missing locks. 1800 /// FIXME: In future, we may be able to not inherit from a visitor. 1801 class BuildLockset : public StmtVisitor<BuildLockset> { 1802 friend class ThreadSafetyAnalyzer; 1803 1804 ThreadSafetyAnalyzer *Analyzer; 1805 FactSet FSet; 1806 LocalVariableMap::Context LVarCtx; 1807 unsigned CtxIndex; 1808 1809 // Helper functions 1810 1811 void warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp, AccessKind AK, 1812 Expr *MutexExp, ProtectedOperationKind POK, 1813 StringRef DiagKind); 1814 void warnIfMutexHeld(const NamedDecl *D, const Expr *Exp, Expr *MutexExp, 1815 StringRef DiagKind); 1816 1817 void checkAccess(const Expr *Exp, AccessKind AK); 1818 void checkPtAccess(const Expr *Exp, AccessKind AK); 1819 1820 void handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD = 0); 1821 1822 public: 1823 BuildLockset(ThreadSafetyAnalyzer *Anlzr, CFGBlockInfo &Info) 1824 : StmtVisitor<BuildLockset>(), 1825 Analyzer(Anlzr), 1826 FSet(Info.EntrySet), 1827 LVarCtx(Info.EntryContext), 1828 CtxIndex(Info.EntryIndex) 1829 {} 1830 1831 void VisitUnaryOperator(UnaryOperator *UO); 1832 void VisitBinaryOperator(BinaryOperator *BO); 1833 void VisitCastExpr(CastExpr *CE); 1834 void VisitCallExpr(CallExpr *Exp); 1835 void VisitCXXConstructExpr(CXXConstructExpr *Exp); 1836 void VisitDeclStmt(DeclStmt *S); 1837 }; 1838 1839 /// \brief Warn if the LSet does not contain a lock sufficient to protect access 1840 /// of at least the passed in AccessKind. 1841 void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp, 1842 AccessKind AK, Expr *MutexExp, 1843 ProtectedOperationKind POK, 1844 StringRef DiagKind) { 1845 LockKind LK = getLockKindFromAccessKind(AK); 1846 1847 SExpr Mutex(MutexExp, Exp, D); 1848 if (!Mutex.isValid()) { 1849 SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D, DiagKind); 1850 return; 1851 } else if (Mutex.shouldIgnore()) { 1852 return; 1853 } 1854 1855 LockData* LDat = FSet.findLockUniv(Analyzer->FactMan, Mutex); 1856 bool NoError = true; 1857 if (!LDat) { 1858 // No exact match found. Look for a partial match. 1859 FactEntry* FEntry = FSet.findPartialMatch(Analyzer->FactMan, Mutex); 1860 if (FEntry) { 1861 // Warn that there's no precise match. 1862 LDat = &FEntry->LDat; 1863 std::string PartMatchStr = FEntry->MutID.toString(); 1864 StringRef PartMatchName(PartMatchStr); 1865 Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK, Mutex.toString(), 1866 LK, Exp->getExprLoc(), 1867 &PartMatchName); 1868 } else { 1869 // Warn that there's no match at all. 1870 Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK, Mutex.toString(), 1871 LK, Exp->getExprLoc()); 1872 } 1873 NoError = false; 1874 } 1875 // Make sure the mutex we found is the right kind. 1876 if (NoError && LDat && !LDat->isAtLeast(LK)) 1877 Analyzer->Handler.handleMutexNotHeld(DiagKind, D, POK, Mutex.toString(), LK, 1878 Exp->getExprLoc()); 1879 } 1880 1881 /// \brief Warn if the LSet contains the given lock. 1882 void BuildLockset::warnIfMutexHeld(const NamedDecl *D, const Expr *Exp, 1883 Expr *MutexExp, 1884 StringRef DiagKind) { 1885 SExpr Mutex(MutexExp, Exp, D); 1886 if (!Mutex.isValid()) { 1887 SExpr::warnInvalidLock(Analyzer->Handler, MutexExp, Exp, D, DiagKind); 1888 return; 1889 } 1890 1891 LockData* LDat = FSet.findLock(Analyzer->FactMan, Mutex); 1892 if (LDat) 1893 Analyzer->Handler.handleFunExcludesLock( 1894 DiagKind, D->getNameAsString(), Mutex.toString(), Exp->getExprLoc()); 1895 } 1896 1897 /// \brief Checks guarded_by and pt_guarded_by attributes. 1898 /// Whenever we identify an access (read or write) to a DeclRefExpr that is 1899 /// marked with guarded_by, we must ensure the appropriate mutexes are held. 1900 /// Similarly, we check if the access is to an expression that dereferences 1901 /// a pointer marked with pt_guarded_by. 1902 void BuildLockset::checkAccess(const Expr *Exp, AccessKind AK) { 1903 Exp = Exp->IgnoreParenCasts(); 1904 1905 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Exp)) { 1906 // For dereferences 1907 if (UO->getOpcode() == clang::UO_Deref) 1908 checkPtAccess(UO->getSubExpr(), AK); 1909 return; 1910 } 1911 1912 if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(Exp)) { 1913 checkPtAccess(AE->getLHS(), AK); 1914 return; 1915 } 1916 1917 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Exp)) { 1918 if (ME->isArrow()) 1919 checkPtAccess(ME->getBase(), AK); 1920 else 1921 checkAccess(ME->getBase(), AK); 1922 } 1923 1924 const ValueDecl *D = getValueDecl(Exp); 1925 if (!D || !D->hasAttrs()) 1926 return; 1927 1928 if (D->hasAttr<GuardedVarAttr>() && FSet.isEmpty()) 1929 Analyzer->Handler.handleNoMutexHeld("mutex", D, POK_VarAccess, AK, 1930 Exp->getExprLoc()); 1931 1932 for (const auto *I : D->specific_attrs<GuardedByAttr>()) 1933 warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK_VarAccess, 1934 ClassifyDiagnostic(I)); 1935 } 1936 1937 /// \brief Checks pt_guarded_by and pt_guarded_var attributes. 1938 void BuildLockset::checkPtAccess(const Expr *Exp, AccessKind AK) { 1939 while (true) { 1940 if (const ParenExpr *PE = dyn_cast<ParenExpr>(Exp)) { 1941 Exp = PE->getSubExpr(); 1942 continue; 1943 } 1944 if (const CastExpr *CE = dyn_cast<CastExpr>(Exp)) { 1945 if (CE->getCastKind() == CK_ArrayToPointerDecay) { 1946 // If it's an actual array, and not a pointer, then it's elements 1947 // are protected by GUARDED_BY, not PT_GUARDED_BY; 1948 checkAccess(CE->getSubExpr(), AK); 1949 return; 1950 } 1951 Exp = CE->getSubExpr(); 1952 continue; 1953 } 1954 break; 1955 } 1956 1957 const ValueDecl *D = getValueDecl(Exp); 1958 if (!D || !D->hasAttrs()) 1959 return; 1960 1961 if (D->hasAttr<PtGuardedVarAttr>() && FSet.isEmpty()) 1962 Analyzer->Handler.handleNoMutexHeld("mutex", D, POK_VarDereference, AK, 1963 Exp->getExprLoc()); 1964 1965 for (auto const *I : D->specific_attrs<PtGuardedByAttr>()) 1966 warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK_VarDereference, 1967 ClassifyDiagnostic(I)); 1968 } 1969 1970 /// \brief Process a function call, method call, constructor call, 1971 /// or destructor call. This involves looking at the attributes on the 1972 /// corresponding function/method/constructor/destructor, issuing warnings, 1973 /// and updating the locksets accordingly. 1974 /// 1975 /// FIXME: For classes annotated with one of the guarded annotations, we need 1976 /// to treat const method calls as reads and non-const method calls as writes, 1977 /// and check that the appropriate locks are held. Non-const method calls with 1978 /// the same signature as const method calls can be also treated as reads. 1979 /// 1980 void BuildLockset::handleCall(Expr *Exp, const NamedDecl *D, VarDecl *VD) { 1981 SourceLocation Loc = Exp->getExprLoc(); 1982 const AttrVec &ArgAttrs = D->getAttrs(); 1983 MutexIDList ExclusiveLocksToAdd, SharedLocksToAdd; 1984 MutexIDList ExclusiveLocksToRemove, SharedLocksToRemove, GenericLocksToRemove; 1985 StringRef CapDiagKind = "mutex"; 1986 1987 for(unsigned i = 0; i < ArgAttrs.size(); ++i) { 1988 Attr *At = const_cast<Attr*>(ArgAttrs[i]); 1989 switch (At->getKind()) { 1990 // When we encounter a lock function, we need to add the lock to our 1991 // lockset. 1992 case attr::AcquireCapability: { 1993 auto *A = cast<AcquireCapabilityAttr>(At); 1994 Analyzer->getMutexIDs(A->isShared() ? SharedLocksToAdd 1995 : ExclusiveLocksToAdd, 1996 A, Exp, D, VD); 1997 1998 CapDiagKind = ClassifyDiagnostic(A); 1999 break; 2000 } 2001 2002 // An assert will add a lock to the lockset, but will not generate 2003 // a warning if it is already there, and will not generate a warning 2004 // if it is not removed. 2005 case attr::AssertExclusiveLock: { 2006 AssertExclusiveLockAttr *A = cast<AssertExclusiveLockAttr>(At); 2007 2008 MutexIDList AssertLocks; 2009 Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD); 2010 for (const auto &AssertLock : AssertLocks) 2011 Analyzer->addLock(FSet, AssertLock, 2012 LockData(Loc, LK_Exclusive, false, true), 2013 ClassifyDiagnostic(A)); 2014 break; 2015 } 2016 case attr::AssertSharedLock: { 2017 AssertSharedLockAttr *A = cast<AssertSharedLockAttr>(At); 2018 2019 MutexIDList AssertLocks; 2020 Analyzer->getMutexIDs(AssertLocks, A, Exp, D, VD); 2021 for (const auto &AssertLock : AssertLocks) 2022 Analyzer->addLock(FSet, AssertLock, 2023 LockData(Loc, LK_Shared, false, true), 2024 ClassifyDiagnostic(A)); 2025 break; 2026 } 2027 2028 // When we encounter an unlock function, we need to remove unlocked 2029 // mutexes from the lockset, and flag a warning if they are not there. 2030 case attr::ReleaseCapability: { 2031 auto *A = cast<ReleaseCapabilityAttr>(At); 2032 if (A->isGeneric()) 2033 Analyzer->getMutexIDs(GenericLocksToRemove, A, Exp, D, VD); 2034 else if (A->isShared()) 2035 Analyzer->getMutexIDs(SharedLocksToRemove, A, Exp, D, VD); 2036 else 2037 Analyzer->getMutexIDs(ExclusiveLocksToRemove, A, Exp, D, VD); 2038 2039 CapDiagKind = ClassifyDiagnostic(A); 2040 break; 2041 } 2042 2043 case attr::RequiresCapability: { 2044 RequiresCapabilityAttr *A = cast<RequiresCapabilityAttr>(At); 2045 for (auto *Arg : A->args()) 2046 warnIfMutexNotHeld(D, Exp, A->isShared() ? AK_Read : AK_Written, Arg, 2047 POK_FunctionCall, ClassifyDiagnostic(A)); 2048 break; 2049 } 2050 2051 case attr::LocksExcluded: { 2052 LocksExcludedAttr *A = cast<LocksExcludedAttr>(At); 2053 for (auto *Arg : A->args()) 2054 warnIfMutexHeld(D, Exp, Arg, ClassifyDiagnostic(A)); 2055 break; 2056 } 2057 2058 // Ignore attributes unrelated to thread-safety 2059 default: 2060 break; 2061 } 2062 } 2063 2064 // Figure out if we're calling the constructor of scoped lockable class 2065 bool isScopedVar = false; 2066 if (VD) { 2067 if (const CXXConstructorDecl *CD = dyn_cast<const CXXConstructorDecl>(D)) { 2068 const CXXRecordDecl* PD = CD->getParent(); 2069 if (PD && PD->hasAttr<ScopedLockableAttr>()) 2070 isScopedVar = true; 2071 } 2072 } 2073 2074 // Add locks. 2075 for (const auto &M : ExclusiveLocksToAdd) 2076 Analyzer->addLock(FSet, M, LockData(Loc, LK_Exclusive, isScopedVar), 2077 CapDiagKind); 2078 for (const auto &M : SharedLocksToAdd) 2079 Analyzer->addLock(FSet, M, LockData(Loc, LK_Shared, isScopedVar), 2080 CapDiagKind); 2081 2082 // Add the managing object as a dummy mutex, mapped to the underlying mutex. 2083 // FIXME -- this doesn't work if we acquire multiple locks. 2084 if (isScopedVar) { 2085 SourceLocation MLoc = VD->getLocation(); 2086 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, VD->getLocation()); 2087 SExpr SMutex(&DRE, 0, 0); 2088 2089 for (const auto &M : ExclusiveLocksToAdd) 2090 Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Exclusive, M), 2091 CapDiagKind); 2092 for (const auto &M : SharedLocksToAdd) 2093 Analyzer->addLock(FSet, SMutex, LockData(MLoc, LK_Shared, M), 2094 CapDiagKind); 2095 } 2096 2097 // Remove locks. 2098 // FIXME -- should only fully remove if the attribute refers to 'this'. 2099 bool Dtor = isa<CXXDestructorDecl>(D); 2100 for (const auto &M : ExclusiveLocksToRemove) 2101 Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Exclusive, CapDiagKind); 2102 for (const auto &M : SharedLocksToRemove) 2103 Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Shared, CapDiagKind); 2104 for (const auto &M : GenericLocksToRemove) 2105 Analyzer->removeLock(FSet, M, Loc, Dtor, LK_Generic, CapDiagKind); 2106 } 2107 2108 2109 /// \brief For unary operations which read and write a variable, we need to 2110 /// check whether we hold any required mutexes. Reads are checked in 2111 /// VisitCastExpr. 2112 void BuildLockset::VisitUnaryOperator(UnaryOperator *UO) { 2113 switch (UO->getOpcode()) { 2114 case clang::UO_PostDec: 2115 case clang::UO_PostInc: 2116 case clang::UO_PreDec: 2117 case clang::UO_PreInc: { 2118 checkAccess(UO->getSubExpr(), AK_Written); 2119 break; 2120 } 2121 default: 2122 break; 2123 } 2124 } 2125 2126 /// For binary operations which assign to a variable (writes), we need to check 2127 /// whether we hold any required mutexes. 2128 /// FIXME: Deal with non-primitive types. 2129 void BuildLockset::VisitBinaryOperator(BinaryOperator *BO) { 2130 if (!BO->isAssignmentOp()) 2131 return; 2132 2133 // adjust the context 2134 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, BO, LVarCtx); 2135 2136 checkAccess(BO->getLHS(), AK_Written); 2137 } 2138 2139 2140 /// Whenever we do an LValue to Rvalue cast, we are reading a variable and 2141 /// need to ensure we hold any required mutexes. 2142 /// FIXME: Deal with non-primitive types. 2143 void BuildLockset::VisitCastExpr(CastExpr *CE) { 2144 if (CE->getCastKind() != CK_LValueToRValue) 2145 return; 2146 checkAccess(CE->getSubExpr(), AK_Read); 2147 } 2148 2149 2150 void BuildLockset::VisitCallExpr(CallExpr *Exp) { 2151 if (CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Exp)) { 2152 MemberExpr *ME = dyn_cast<MemberExpr>(CE->getCallee()); 2153 // ME can be null when calling a method pointer 2154 CXXMethodDecl *MD = CE->getMethodDecl(); 2155 2156 if (ME && MD) { 2157 if (ME->isArrow()) { 2158 if (MD->isConst()) { 2159 checkPtAccess(CE->getImplicitObjectArgument(), AK_Read); 2160 } else { // FIXME -- should be AK_Written 2161 checkPtAccess(CE->getImplicitObjectArgument(), AK_Read); 2162 } 2163 } else { 2164 if (MD->isConst()) 2165 checkAccess(CE->getImplicitObjectArgument(), AK_Read); 2166 else // FIXME -- should be AK_Written 2167 checkAccess(CE->getImplicitObjectArgument(), AK_Read); 2168 } 2169 } 2170 } else if (CXXOperatorCallExpr *OE = dyn_cast<CXXOperatorCallExpr>(Exp)) { 2171 switch (OE->getOperator()) { 2172 case OO_Equal: { 2173 const Expr *Target = OE->getArg(0); 2174 const Expr *Source = OE->getArg(1); 2175 checkAccess(Target, AK_Written); 2176 checkAccess(Source, AK_Read); 2177 break; 2178 } 2179 case OO_Star: 2180 case OO_Arrow: 2181 case OO_Subscript: { 2182 const Expr *Obj = OE->getArg(0); 2183 checkAccess(Obj, AK_Read); 2184 checkPtAccess(Obj, AK_Read); 2185 break; 2186 } 2187 default: { 2188 const Expr *Obj = OE->getArg(0); 2189 checkAccess(Obj, AK_Read); 2190 break; 2191 } 2192 } 2193 } 2194 NamedDecl *D = dyn_cast_or_null<NamedDecl>(Exp->getCalleeDecl()); 2195 if(!D || !D->hasAttrs()) 2196 return; 2197 handleCall(Exp, D); 2198 } 2199 2200 void BuildLockset::VisitCXXConstructExpr(CXXConstructExpr *Exp) { 2201 const CXXConstructorDecl *D = Exp->getConstructor(); 2202 if (D && D->isCopyConstructor()) { 2203 const Expr* Source = Exp->getArg(0); 2204 checkAccess(Source, AK_Read); 2205 } 2206 // FIXME -- only handles constructors in DeclStmt below. 2207 } 2208 2209 void BuildLockset::VisitDeclStmt(DeclStmt *S) { 2210 // adjust the context 2211 LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx); 2212 2213 for (auto *D : S->getDeclGroup()) { 2214 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(D)) { 2215 Expr *E = VD->getInit(); 2216 // handle constructors that involve temporaries 2217 if (ExprWithCleanups *EWC = dyn_cast_or_null<ExprWithCleanups>(E)) 2218 E = EWC->getSubExpr(); 2219 2220 if (CXXConstructExpr *CE = dyn_cast_or_null<CXXConstructExpr>(E)) { 2221 NamedDecl *CtorD = dyn_cast_or_null<NamedDecl>(CE->getConstructor()); 2222 if (!CtorD || !CtorD->hasAttrs()) 2223 return; 2224 handleCall(CE, CtorD, VD); 2225 } 2226 } 2227 } 2228 } 2229 2230 2231 2232 /// \brief Compute the intersection of two locksets and issue warnings for any 2233 /// locks in the symmetric difference. 2234 /// 2235 /// This function is used at a merge point in the CFG when comparing the lockset 2236 /// of each branch being merged. For example, given the following sequence: 2237 /// A; if () then B; else C; D; we need to check that the lockset after B and C 2238 /// are the same. In the event of a difference, we use the intersection of these 2239 /// two locksets at the start of D. 2240 /// 2241 /// \param FSet1 The first lockset. 2242 /// \param FSet2 The second lockset. 2243 /// \param JoinLoc The location of the join point for error reporting 2244 /// \param LEK1 The error message to report if a mutex is missing from LSet1 2245 /// \param LEK2 The error message to report if a mutex is missing from Lset2 2246 void ThreadSafetyAnalyzer::intersectAndWarn(FactSet &FSet1, 2247 const FactSet &FSet2, 2248 SourceLocation JoinLoc, 2249 LockErrorKind LEK1, 2250 LockErrorKind LEK2, 2251 bool Modify) { 2252 FactSet FSet1Orig = FSet1; 2253 2254 // Find locks in FSet2 that conflict or are not in FSet1, and warn. 2255 for (const auto &Fact : FSet2) { 2256 const SExpr &FSet2Mutex = FactMan[Fact].MutID; 2257 const LockData &LDat2 = FactMan[Fact].LDat; 2258 FactSet::iterator I1 = FSet1.findLockIter(FactMan, FSet2Mutex); 2259 2260 if (I1 != FSet1.end()) { 2261 const LockData* LDat1 = &FactMan[*I1].LDat; 2262 if (LDat1->LKind != LDat2.LKind) { 2263 Handler.handleExclusiveAndShared("mutex", FSet2Mutex.toString(), 2264 LDat2.AcquireLoc, LDat1->AcquireLoc); 2265 if (Modify && LDat1->LKind != LK_Exclusive) { 2266 // Take the exclusive lock, which is the one in FSet2. 2267 *I1 = Fact; 2268 } 2269 } 2270 else if (LDat1->Asserted && !LDat2.Asserted) { 2271 // The non-asserted lock in FSet2 is the one we want to track. 2272 *I1 = Fact; 2273 } 2274 } else { 2275 if (LDat2.UnderlyingMutex.isValid()) { 2276 if (FSet2.findLock(FactMan, LDat2.UnderlyingMutex)) { 2277 // If this is a scoped lock that manages another mutex, and if the 2278 // underlying mutex is still held, then warn about the underlying 2279 // mutex. 2280 Handler.handleMutexHeldEndOfScope("mutex", 2281 LDat2.UnderlyingMutex.toString(), 2282 LDat2.AcquireLoc, JoinLoc, LEK1); 2283 } 2284 } 2285 else if (!LDat2.Managed && !FSet2Mutex.isUniversal() && !LDat2.Asserted) 2286 Handler.handleMutexHeldEndOfScope("mutex", FSet2Mutex.toString(), 2287 LDat2.AcquireLoc, JoinLoc, LEK1); 2288 } 2289 } 2290 2291 // Find locks in FSet1 that are not in FSet2, and remove them. 2292 for (const auto &Fact : FSet1Orig) { 2293 const SExpr &FSet1Mutex = FactMan[Fact].MutID; 2294 const LockData &LDat1 = FactMan[Fact].LDat; 2295 2296 if (!FSet2.findLock(FactMan, FSet1Mutex)) { 2297 if (LDat1.UnderlyingMutex.isValid()) { 2298 if (FSet1Orig.findLock(FactMan, LDat1.UnderlyingMutex)) { 2299 // If this is a scoped lock that manages another mutex, and if the 2300 // underlying mutex is still held, then warn about the underlying 2301 // mutex. 2302 Handler.handleMutexHeldEndOfScope("mutex", 2303 LDat1.UnderlyingMutex.toString(), 2304 LDat1.AcquireLoc, JoinLoc, LEK1); 2305 } 2306 } 2307 else if (!LDat1.Managed && !FSet1Mutex.isUniversal() && !LDat1.Asserted) 2308 Handler.handleMutexHeldEndOfScope("mutex", FSet1Mutex.toString(), 2309 LDat1.AcquireLoc, JoinLoc, LEK2); 2310 if (Modify) 2311 FSet1.removeLock(FactMan, FSet1Mutex); 2312 } 2313 } 2314 } 2315 2316 2317 // Return true if block B never continues to its successors. 2318 inline bool neverReturns(const CFGBlock* B) { 2319 if (B->hasNoReturnElement()) 2320 return true; 2321 if (B->empty()) 2322 return false; 2323 2324 CFGElement Last = B->back(); 2325 if (Optional<CFGStmt> S = Last.getAs<CFGStmt>()) { 2326 if (isa<CXXThrowExpr>(S->getStmt())) 2327 return true; 2328 } 2329 return false; 2330 } 2331 2332 2333 /// \brief Check a function's CFG for thread-safety violations. 2334 /// 2335 /// We traverse the blocks in the CFG, compute the set of mutexes that are held 2336 /// at the end of each block, and issue warnings for thread safety violations. 2337 /// Each block in the CFG is traversed exactly once. 2338 void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) { 2339 // TODO: this whole function needs be rewritten as a visitor for CFGWalker. 2340 // For now, we just use the walker to set things up. 2341 threadSafety::CFGWalker walker; 2342 if (!walker.init(AC)) 2343 return; 2344 2345 // AC.dumpCFG(true); 2346 // threadSafety::printSCFG(walker); 2347 2348 CFG *CFGraph = walker.getGraph(); 2349 const NamedDecl *D = walker.getDecl(); 2350 2351 if (D->hasAttr<NoThreadSafetyAnalysisAttr>()) 2352 return; 2353 2354 // FIXME: Do something a bit more intelligent inside constructor and 2355 // destructor code. Constructors and destructors must assume unique access 2356 // to 'this', so checks on member variable access is disabled, but we should 2357 // still enable checks on other objects. 2358 if (isa<CXXConstructorDecl>(D)) 2359 return; // Don't check inside constructors. 2360 if (isa<CXXDestructorDecl>(D)) 2361 return; // Don't check inside destructors. 2362 2363 BlockInfo.resize(CFGraph->getNumBlockIDs(), 2364 CFGBlockInfo::getEmptyBlockInfo(LocalVarMap)); 2365 2366 // We need to explore the CFG via a "topological" ordering. 2367 // That way, we will be guaranteed to have information about required 2368 // predecessor locksets when exploring a new block. 2369 const PostOrderCFGView *SortedGraph = walker.getSortedGraph(); 2370 PostOrderCFGView::CFGBlockSet VisitedBlocks(CFGraph); 2371 2372 // Mark entry block as reachable 2373 BlockInfo[CFGraph->getEntry().getBlockID()].Reachable = true; 2374 2375 // Compute SSA names for local variables 2376 LocalVarMap.traverseCFG(CFGraph, SortedGraph, BlockInfo); 2377 2378 // Fill in source locations for all CFGBlocks. 2379 findBlockLocations(CFGraph, SortedGraph, BlockInfo); 2380 2381 MutexIDList ExclusiveLocksAcquired; 2382 MutexIDList SharedLocksAcquired; 2383 MutexIDList LocksReleased; 2384 2385 // Add locks from exclusive_locks_required and shared_locks_required 2386 // to initial lockset. Also turn off checking for lock and unlock functions. 2387 // FIXME: is there a more intelligent way to check lock/unlock functions? 2388 if (!SortedGraph->empty() && D->hasAttrs()) { 2389 const CFGBlock *FirstBlock = *SortedGraph->begin(); 2390 FactSet &InitialLockset = BlockInfo[FirstBlock->getBlockID()].EntrySet; 2391 const AttrVec &ArgAttrs = D->getAttrs(); 2392 2393 MutexIDList ExclusiveLocksToAdd; 2394 MutexIDList SharedLocksToAdd; 2395 StringRef CapDiagKind = "mutex"; 2396 2397 SourceLocation Loc = D->getLocation(); 2398 for (const auto *Attr : ArgAttrs) { 2399 Loc = Attr->getLocation(); 2400 if (const auto *A = dyn_cast<RequiresCapabilityAttr>(Attr)) { 2401 getMutexIDs(A->isShared() ? SharedLocksToAdd : ExclusiveLocksToAdd, A, 2402 0, D); 2403 CapDiagKind = ClassifyDiagnostic(A); 2404 } else if (const auto *A = dyn_cast<ReleaseCapabilityAttr>(Attr)) { 2405 // UNLOCK_FUNCTION() is used to hide the underlying lock implementation. 2406 // We must ignore such methods. 2407 if (A->args_size() == 0) 2408 return; 2409 // FIXME -- deal with exclusive vs. shared unlock functions? 2410 getMutexIDs(ExclusiveLocksToAdd, A, nullptr, D); 2411 getMutexIDs(LocksReleased, A, nullptr, D); 2412 CapDiagKind = ClassifyDiagnostic(A); 2413 } else if (const auto *A = dyn_cast<AcquireCapabilityAttr>(Attr)) { 2414 if (A->args_size() == 0) 2415 return; 2416 getMutexIDs(A->isShared() ? SharedLocksAcquired 2417 : ExclusiveLocksAcquired, 2418 A, nullptr, D); 2419 CapDiagKind = ClassifyDiagnostic(A); 2420 } else if (isa<ExclusiveTrylockFunctionAttr>(Attr)) { 2421 // Don't try to check trylock functions for now 2422 return; 2423 } else if (isa<SharedTrylockFunctionAttr>(Attr)) { 2424 // Don't try to check trylock functions for now 2425 return; 2426 } 2427 } 2428 2429 // FIXME -- Loc can be wrong here. 2430 for (const auto &ExclusiveLockToAdd : ExclusiveLocksToAdd) 2431 addLock(InitialLockset, ExclusiveLockToAdd, LockData(Loc, LK_Exclusive), 2432 CapDiagKind); 2433 for (const auto &SharedLockToAdd : SharedLocksToAdd) 2434 addLock(InitialLockset, SharedLockToAdd, LockData(Loc, LK_Shared), 2435 CapDiagKind); 2436 } 2437 2438 for (const auto *CurrBlock : *SortedGraph) { 2439 int CurrBlockID = CurrBlock->getBlockID(); 2440 CFGBlockInfo *CurrBlockInfo = &BlockInfo[CurrBlockID]; 2441 2442 // Use the default initial lockset in case there are no predecessors. 2443 VisitedBlocks.insert(CurrBlock); 2444 2445 // Iterate through the predecessor blocks and warn if the lockset for all 2446 // predecessors is not the same. We take the entry lockset of the current 2447 // block to be the intersection of all previous locksets. 2448 // FIXME: By keeping the intersection, we may output more errors in future 2449 // for a lock which is not in the intersection, but was in the union. We 2450 // may want to also keep the union in future. As an example, let's say 2451 // the intersection contains Mutex L, and the union contains L and M. 2452 // Later we unlock M. At this point, we would output an error because we 2453 // never locked M; although the real error is probably that we forgot to 2454 // lock M on all code paths. Conversely, let's say that later we lock M. 2455 // In this case, we should compare against the intersection instead of the 2456 // union because the real error is probably that we forgot to unlock M on 2457 // all code paths. 2458 bool LocksetInitialized = false; 2459 SmallVector<CFGBlock *, 8> SpecialBlocks; 2460 for (CFGBlock::const_pred_iterator PI = CurrBlock->pred_begin(), 2461 PE = CurrBlock->pred_end(); PI != PE; ++PI) { 2462 2463 // if *PI -> CurrBlock is a back edge 2464 if (*PI == nullptr || !VisitedBlocks.alreadySet(*PI)) 2465 continue; 2466 2467 int PrevBlockID = (*PI)->getBlockID(); 2468 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; 2469 2470 // Ignore edges from blocks that can't return. 2471 if (neverReturns(*PI) || !PrevBlockInfo->Reachable) 2472 continue; 2473 2474 // Okay, we can reach this block from the entry. 2475 CurrBlockInfo->Reachable = true; 2476 2477 // If the previous block ended in a 'continue' or 'break' statement, then 2478 // a difference in locksets is probably due to a bug in that block, rather 2479 // than in some other predecessor. In that case, keep the other 2480 // predecessor's lockset. 2481 if (const Stmt *Terminator = (*PI)->getTerminator()) { 2482 if (isa<ContinueStmt>(Terminator) || isa<BreakStmt>(Terminator)) { 2483 SpecialBlocks.push_back(*PI); 2484 continue; 2485 } 2486 } 2487 2488 FactSet PrevLockset; 2489 getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, *PI, CurrBlock); 2490 2491 if (!LocksetInitialized) { 2492 CurrBlockInfo->EntrySet = PrevLockset; 2493 LocksetInitialized = true; 2494 } else { 2495 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset, 2496 CurrBlockInfo->EntryLoc, 2497 LEK_LockedSomePredecessors); 2498 } 2499 } 2500 2501 // Skip rest of block if it's not reachable. 2502 if (!CurrBlockInfo->Reachable) 2503 continue; 2504 2505 // Process continue and break blocks. Assume that the lockset for the 2506 // resulting block is unaffected by any discrepancies in them. 2507 for (const auto *PrevBlock : SpecialBlocks) { 2508 int PrevBlockID = PrevBlock->getBlockID(); 2509 CFGBlockInfo *PrevBlockInfo = &BlockInfo[PrevBlockID]; 2510 2511 if (!LocksetInitialized) { 2512 CurrBlockInfo->EntrySet = PrevBlockInfo->ExitSet; 2513 LocksetInitialized = true; 2514 } else { 2515 // Determine whether this edge is a loop terminator for diagnostic 2516 // purposes. FIXME: A 'break' statement might be a loop terminator, but 2517 // it might also be part of a switch. Also, a subsequent destructor 2518 // might add to the lockset, in which case the real issue might be a 2519 // double lock on the other path. 2520 const Stmt *Terminator = PrevBlock->getTerminator(); 2521 bool IsLoop = Terminator && isa<ContinueStmt>(Terminator); 2522 2523 FactSet PrevLockset; 2524 getEdgeLockset(PrevLockset, PrevBlockInfo->ExitSet, 2525 PrevBlock, CurrBlock); 2526 2527 // Do not update EntrySet. 2528 intersectAndWarn(CurrBlockInfo->EntrySet, PrevLockset, 2529 PrevBlockInfo->ExitLoc, 2530 IsLoop ? LEK_LockedSomeLoopIterations 2531 : LEK_LockedSomePredecessors, 2532 false); 2533 } 2534 } 2535 2536 BuildLockset LocksetBuilder(this, *CurrBlockInfo); 2537 2538 // Visit all the statements in the basic block. 2539 for (CFGBlock::const_iterator BI = CurrBlock->begin(), 2540 BE = CurrBlock->end(); BI != BE; ++BI) { 2541 switch (BI->getKind()) { 2542 case CFGElement::Statement: { 2543 CFGStmt CS = BI->castAs<CFGStmt>(); 2544 LocksetBuilder.Visit(const_cast<Stmt*>(CS.getStmt())); 2545 break; 2546 } 2547 // Ignore BaseDtor, MemberDtor, and TemporaryDtor for now. 2548 case CFGElement::AutomaticObjectDtor: { 2549 CFGAutomaticObjDtor AD = BI->castAs<CFGAutomaticObjDtor>(); 2550 CXXDestructorDecl *DD = const_cast<CXXDestructorDecl *>( 2551 AD.getDestructorDecl(AC.getASTContext())); 2552 if (!DD->hasAttrs()) 2553 break; 2554 2555 // Create a dummy expression, 2556 VarDecl *VD = const_cast<VarDecl*>(AD.getVarDecl()); 2557 DeclRefExpr DRE(VD, false, VD->getType(), VK_LValue, 2558 AD.getTriggerStmt()->getLocEnd()); 2559 LocksetBuilder.handleCall(&DRE, DD); 2560 break; 2561 } 2562 default: 2563 break; 2564 } 2565 } 2566 CurrBlockInfo->ExitSet = LocksetBuilder.FSet; 2567 2568 // For every back edge from CurrBlock (the end of the loop) to another block 2569 // (FirstLoopBlock) we need to check that the Lockset of Block is equal to 2570 // the one held at the beginning of FirstLoopBlock. We can look up the 2571 // Lockset held at the beginning of FirstLoopBlock in the EntryLockSets map. 2572 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), 2573 SE = CurrBlock->succ_end(); SI != SE; ++SI) { 2574 2575 // if CurrBlock -> *SI is *not* a back edge 2576 if (*SI == 0 || !VisitedBlocks.alreadySet(*SI)) 2577 continue; 2578 2579 CFGBlock *FirstLoopBlock = *SI; 2580 CFGBlockInfo *PreLoop = &BlockInfo[FirstLoopBlock->getBlockID()]; 2581 CFGBlockInfo *LoopEnd = &BlockInfo[CurrBlockID]; 2582 intersectAndWarn(LoopEnd->ExitSet, PreLoop->EntrySet, 2583 PreLoop->EntryLoc, 2584 LEK_LockedSomeLoopIterations, 2585 false); 2586 } 2587 } 2588 2589 CFGBlockInfo *Initial = &BlockInfo[CFGraph->getEntry().getBlockID()]; 2590 CFGBlockInfo *Final = &BlockInfo[CFGraph->getExit().getBlockID()]; 2591 2592 // Skip the final check if the exit block is unreachable. 2593 if (!Final->Reachable) 2594 return; 2595 2596 // By default, we expect all locks held on entry to be held on exit. 2597 FactSet ExpectedExitSet = Initial->EntrySet; 2598 2599 // Adjust the expected exit set by adding or removing locks, as declared 2600 // by *-LOCK_FUNCTION and UNLOCK_FUNCTION. The intersect below will then 2601 // issue the appropriate warning. 2602 // FIXME: the location here is not quite right. 2603 for (const auto &Lock : ExclusiveLocksAcquired) 2604 ExpectedExitSet.addLock(FactMan, Lock, 2605 LockData(D->getLocation(), LK_Exclusive)); 2606 for (const auto &Lock : SharedLocksAcquired) 2607 ExpectedExitSet.addLock(FactMan, Lock, 2608 LockData(D->getLocation(), LK_Shared)); 2609 for (const auto &Lock : LocksReleased) 2610 ExpectedExitSet.removeLock(FactMan, Lock); 2611 2612 // FIXME: Should we call this function for all blocks which exit the function? 2613 intersectAndWarn(ExpectedExitSet, Final->ExitSet, 2614 Final->ExitLoc, 2615 LEK_LockedAtEndOfFunction, 2616 LEK_NotLockedAtEndOfFunction, 2617 false); 2618 } 2619 2620 } // end anonymous namespace 2621 2622 2623 namespace clang { 2624 namespace thread_safety { 2625 2626 /// \brief Check a function's CFG for thread-safety violations. 2627 /// 2628 /// We traverse the blocks in the CFG, compute the set of mutexes that are held 2629 /// at the end of each block, and issue warnings for thread safety violations. 2630 /// Each block in the CFG is traversed exactly once. 2631 void runThreadSafetyAnalysis(AnalysisDeclContext &AC, 2632 ThreadSafetyHandler &Handler) { 2633 ThreadSafetyAnalyzer Analyzer(Handler); 2634 Analyzer.runAnalysis(AC); 2635 } 2636 2637 /// \brief Helper function that returns a LockKind required for the given level 2638 /// of access. 2639 LockKind getLockKindFromAccessKind(AccessKind AK) { 2640 switch (AK) { 2641 case AK_Read : 2642 return LK_Shared; 2643 case AK_Written : 2644 return LK_Exclusive; 2645 } 2646 llvm_unreachable("Unknown AccessKind"); 2647 } 2648 2649 }} // end namespace clang::thread_safety 2650