1 //===- SymbolManager.h - Management of Symbolic Values --------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines SymbolManager, a class that manages symbolic values 11 // created for use by ExprEngine and related classes. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/Expr.h" 18 #include "clang/Analysis/Analyses/LiveVariables.h" 19 #include "clang/Analysis/AnalysisDeclContext.h" 20 #include "clang/Basic/LLVM.h" 21 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 22 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 23 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h" 24 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h" 25 #include "llvm/ADT/FoldingSet.h" 26 #include "llvm/ADT/STLExtras.h" 27 #include "llvm/Support/Casting.h" 28 #include "llvm/Support/Compiler.h" 29 #include "llvm/Support/ErrorHandling.h" 30 #include "llvm/Support/raw_ostream.h" 31 #include <cassert> 32 33 using namespace clang; 34 using namespace ento; 35 36 void SymExpr::anchor() {} 37 38 LLVM_DUMP_METHOD void SymExpr::dump() const { 39 dumpToStream(llvm::errs()); 40 } 41 42 void SymIntExpr::dumpToStream(raw_ostream &os) const { 43 os << '('; 44 getLHS()->dumpToStream(os); 45 os << ") " 46 << BinaryOperator::getOpcodeStr(getOpcode()) << ' '; 47 if (getRHS().isUnsigned()) 48 os << getRHS().getZExtValue(); 49 else 50 os << getRHS().getSExtValue(); 51 if (getRHS().isUnsigned()) 52 os << 'U'; 53 } 54 55 void IntSymExpr::dumpToStream(raw_ostream &os) const { 56 if (getLHS().isUnsigned()) 57 os << getLHS().getZExtValue(); 58 else 59 os << getLHS().getSExtValue(); 60 if (getLHS().isUnsigned()) 61 os << 'U'; 62 os << ' ' 63 << BinaryOperator::getOpcodeStr(getOpcode()) 64 << " ("; 65 getRHS()->dumpToStream(os); 66 os << ')'; 67 } 68 69 void SymSymExpr::dumpToStream(raw_ostream &os) const { 70 os << '('; 71 getLHS()->dumpToStream(os); 72 os << ") " 73 << BinaryOperator::getOpcodeStr(getOpcode()) 74 << " ("; 75 getRHS()->dumpToStream(os); 76 os << ')'; 77 } 78 79 void SymbolCast::dumpToStream(raw_ostream &os) const { 80 os << '(' << ToTy.getAsString() << ") ("; 81 Operand->dumpToStream(os); 82 os << ')'; 83 } 84 85 void SymbolConjured::dumpToStream(raw_ostream &os) const { 86 os << "conj_$" << getSymbolID() << '{' << T.getAsString() << '}'; 87 } 88 89 void SymbolDerived::dumpToStream(raw_ostream &os) const { 90 os << "derived_$" << getSymbolID() << '{' 91 << getParentSymbol() << ',' << getRegion() << '}'; 92 } 93 94 void SymbolExtent::dumpToStream(raw_ostream &os) const { 95 os << "extent_$" << getSymbolID() << '{' << getRegion() << '}'; 96 } 97 98 void SymbolMetadata::dumpToStream(raw_ostream &os) const { 99 os << "meta_$" << getSymbolID() << '{' 100 << getRegion() << ',' << T.getAsString() << '}'; 101 } 102 103 void SymbolData::anchor() {} 104 105 void SymbolRegionValue::dumpToStream(raw_ostream &os) const { 106 os << "reg_$" << getSymbolID() 107 << '<' << getType().getAsString() << ' ' << R << '>'; 108 } 109 110 bool SymExpr::symbol_iterator::operator==(const symbol_iterator &X) const { 111 return itr == X.itr; 112 } 113 114 bool SymExpr::symbol_iterator::operator!=(const symbol_iterator &X) const { 115 return itr != X.itr; 116 } 117 118 SymExpr::symbol_iterator::symbol_iterator(const SymExpr *SE) { 119 itr.push_back(SE); 120 } 121 122 SymExpr::symbol_iterator &SymExpr::symbol_iterator::operator++() { 123 assert(!itr.empty() && "attempting to iterate on an 'end' iterator"); 124 expand(); 125 return *this; 126 } 127 128 SymbolRef SymExpr::symbol_iterator::operator*() { 129 assert(!itr.empty() && "attempting to dereference an 'end' iterator"); 130 return itr.back(); 131 } 132 133 void SymExpr::symbol_iterator::expand() { 134 const SymExpr *SE = itr.pop_back_val(); 135 136 switch (SE->getKind()) { 137 case SymExpr::SymbolRegionValueKind: 138 case SymExpr::SymbolConjuredKind: 139 case SymExpr::SymbolDerivedKind: 140 case SymExpr::SymbolExtentKind: 141 case SymExpr::SymbolMetadataKind: 142 return; 143 case SymExpr::SymbolCastKind: 144 itr.push_back(cast<SymbolCast>(SE)->getOperand()); 145 return; 146 case SymExpr::SymIntExprKind: 147 itr.push_back(cast<SymIntExpr>(SE)->getLHS()); 148 return; 149 case SymExpr::IntSymExprKind: 150 itr.push_back(cast<IntSymExpr>(SE)->getRHS()); 151 return; 152 case SymExpr::SymSymExprKind: { 153 const auto *x = cast<SymSymExpr>(SE); 154 itr.push_back(x->getLHS()); 155 itr.push_back(x->getRHS()); 156 return; 157 } 158 } 159 llvm_unreachable("unhandled expansion case"); 160 } 161 162 const SymbolRegionValue* 163 SymbolManager::getRegionValueSymbol(const TypedValueRegion* R) { 164 llvm::FoldingSetNodeID profile; 165 SymbolRegionValue::Profile(profile, R); 166 void *InsertPos; 167 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); 168 if (!SD) { 169 SD = (SymExpr*) BPAlloc.Allocate<SymbolRegionValue>(); 170 new (SD) SymbolRegionValue(SymbolCounter, R); 171 DataSet.InsertNode(SD, InsertPos); 172 ++SymbolCounter; 173 } 174 175 return cast<SymbolRegionValue>(SD); 176 } 177 178 const SymbolConjured* SymbolManager::conjureSymbol(const Stmt *E, 179 const LocationContext *LCtx, 180 QualType T, 181 unsigned Count, 182 const void *SymbolTag) { 183 llvm::FoldingSetNodeID profile; 184 SymbolConjured::Profile(profile, E, T, Count, LCtx, SymbolTag); 185 void *InsertPos; 186 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); 187 if (!SD) { 188 SD = (SymExpr*) BPAlloc.Allocate<SymbolConjured>(); 189 new (SD) SymbolConjured(SymbolCounter, E, LCtx, T, Count, SymbolTag); 190 DataSet.InsertNode(SD, InsertPos); 191 ++SymbolCounter; 192 } 193 194 return cast<SymbolConjured>(SD); 195 } 196 197 const SymbolDerived* 198 SymbolManager::getDerivedSymbol(SymbolRef parentSymbol, 199 const TypedValueRegion *R) { 200 llvm::FoldingSetNodeID profile; 201 SymbolDerived::Profile(profile, parentSymbol, R); 202 void *InsertPos; 203 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); 204 if (!SD) { 205 SD = (SymExpr*) BPAlloc.Allocate<SymbolDerived>(); 206 new (SD) SymbolDerived(SymbolCounter, parentSymbol, R); 207 DataSet.InsertNode(SD, InsertPos); 208 ++SymbolCounter; 209 } 210 211 return cast<SymbolDerived>(SD); 212 } 213 214 const SymbolExtent* 215 SymbolManager::getExtentSymbol(const SubRegion *R) { 216 llvm::FoldingSetNodeID profile; 217 SymbolExtent::Profile(profile, R); 218 void *InsertPos; 219 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); 220 if (!SD) { 221 SD = (SymExpr*) BPAlloc.Allocate<SymbolExtent>(); 222 new (SD) SymbolExtent(SymbolCounter, R); 223 DataSet.InsertNode(SD, InsertPos); 224 ++SymbolCounter; 225 } 226 227 return cast<SymbolExtent>(SD); 228 } 229 230 const SymbolMetadata * 231 SymbolManager::getMetadataSymbol(const MemRegion* R, const Stmt *S, QualType T, 232 const LocationContext *LCtx, 233 unsigned Count, const void *SymbolTag) { 234 llvm::FoldingSetNodeID profile; 235 SymbolMetadata::Profile(profile, R, S, T, LCtx, Count, SymbolTag); 236 void *InsertPos; 237 SymExpr *SD = DataSet.FindNodeOrInsertPos(profile, InsertPos); 238 if (!SD) { 239 SD = (SymExpr*) BPAlloc.Allocate<SymbolMetadata>(); 240 new (SD) SymbolMetadata(SymbolCounter, R, S, T, LCtx, Count, SymbolTag); 241 DataSet.InsertNode(SD, InsertPos); 242 ++SymbolCounter; 243 } 244 245 return cast<SymbolMetadata>(SD); 246 } 247 248 const SymbolCast* 249 SymbolManager::getCastSymbol(const SymExpr *Op, 250 QualType From, QualType To) { 251 llvm::FoldingSetNodeID ID; 252 SymbolCast::Profile(ID, Op, From, To); 253 void *InsertPos; 254 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos); 255 if (!data) { 256 data = (SymbolCast*) BPAlloc.Allocate<SymbolCast>(); 257 new (data) SymbolCast(Op, From, To); 258 DataSet.InsertNode(data, InsertPos); 259 } 260 261 return cast<SymbolCast>(data); 262 } 263 264 const SymIntExpr *SymbolManager::getSymIntExpr(const SymExpr *lhs, 265 BinaryOperator::Opcode op, 266 const llvm::APSInt& v, 267 QualType t) { 268 llvm::FoldingSetNodeID ID; 269 SymIntExpr::Profile(ID, lhs, op, v, t); 270 void *InsertPos; 271 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos); 272 273 if (!data) { 274 data = (SymIntExpr*) BPAlloc.Allocate<SymIntExpr>(); 275 new (data) SymIntExpr(lhs, op, v, t); 276 DataSet.InsertNode(data, InsertPos); 277 } 278 279 return cast<SymIntExpr>(data); 280 } 281 282 const IntSymExpr *SymbolManager::getIntSymExpr(const llvm::APSInt& lhs, 283 BinaryOperator::Opcode op, 284 const SymExpr *rhs, 285 QualType t) { 286 llvm::FoldingSetNodeID ID; 287 IntSymExpr::Profile(ID, lhs, op, rhs, t); 288 void *InsertPos; 289 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos); 290 291 if (!data) { 292 data = (IntSymExpr*) BPAlloc.Allocate<IntSymExpr>(); 293 new (data) IntSymExpr(lhs, op, rhs, t); 294 DataSet.InsertNode(data, InsertPos); 295 } 296 297 return cast<IntSymExpr>(data); 298 } 299 300 const SymSymExpr *SymbolManager::getSymSymExpr(const SymExpr *lhs, 301 BinaryOperator::Opcode op, 302 const SymExpr *rhs, 303 QualType t) { 304 llvm::FoldingSetNodeID ID; 305 SymSymExpr::Profile(ID, lhs, op, rhs, t); 306 void *InsertPos; 307 SymExpr *data = DataSet.FindNodeOrInsertPos(ID, InsertPos); 308 309 if (!data) { 310 data = (SymSymExpr*) BPAlloc.Allocate<SymSymExpr>(); 311 new (data) SymSymExpr(lhs, op, rhs, t); 312 DataSet.InsertNode(data, InsertPos); 313 } 314 315 return cast<SymSymExpr>(data); 316 } 317 318 QualType SymbolConjured::getType() const { 319 return T; 320 } 321 322 QualType SymbolDerived::getType() const { 323 return R->getValueType(); 324 } 325 326 QualType SymbolExtent::getType() const { 327 ASTContext &Ctx = R->getMemRegionManager()->getContext(); 328 return Ctx.getSizeType(); 329 } 330 331 QualType SymbolMetadata::getType() const { 332 return T; 333 } 334 335 QualType SymbolRegionValue::getType() const { 336 return R->getValueType(); 337 } 338 339 SymbolManager::~SymbolManager() { 340 llvm::DeleteContainerSeconds(SymbolDependencies); 341 } 342 343 bool SymbolManager::canSymbolicate(QualType T) { 344 T = T.getCanonicalType(); 345 346 if (Loc::isLocType(T)) 347 return true; 348 349 if (T->isIntegralOrEnumerationType()) 350 return true; 351 352 if (T->isRecordType() && !T->isUnionType()) 353 return true; 354 355 return false; 356 } 357 358 void SymbolManager::addSymbolDependency(const SymbolRef Primary, 359 const SymbolRef Dependent) { 360 SymbolDependTy::iterator I = SymbolDependencies.find(Primary); 361 SymbolRefSmallVectorTy *dependencies = nullptr; 362 if (I == SymbolDependencies.end()) { 363 dependencies = new SymbolRefSmallVectorTy(); 364 SymbolDependencies[Primary] = dependencies; 365 } else { 366 dependencies = I->second; 367 } 368 dependencies->push_back(Dependent); 369 } 370 371 const SymbolRefSmallVectorTy *SymbolManager::getDependentSymbols( 372 const SymbolRef Primary) { 373 SymbolDependTy::const_iterator I = SymbolDependencies.find(Primary); 374 if (I == SymbolDependencies.end()) 375 return nullptr; 376 return I->second; 377 } 378 379 void SymbolReaper::markDependentsLive(SymbolRef sym) { 380 // Do not mark dependents more then once. 381 SymbolMapTy::iterator LI = TheLiving.find(sym); 382 assert(LI != TheLiving.end() && "The primary symbol is not live."); 383 if (LI->second == HaveMarkedDependents) 384 return; 385 LI->second = HaveMarkedDependents; 386 387 if (const SymbolRefSmallVectorTy *Deps = SymMgr.getDependentSymbols(sym)) { 388 for (const auto I : *Deps) { 389 if (TheLiving.find(I) != TheLiving.end()) 390 continue; 391 markLive(I); 392 } 393 } 394 } 395 396 void SymbolReaper::markLive(SymbolRef sym) { 397 TheLiving[sym] = NotProcessed; 398 TheDead.erase(sym); 399 markDependentsLive(sym); 400 } 401 402 void SymbolReaper::markLive(const MemRegion *region) { 403 RegionRoots.insert(region); 404 markElementIndicesLive(region); 405 } 406 407 void SymbolReaper::markElementIndicesLive(const MemRegion *region) { 408 for (auto SR = dyn_cast<SubRegion>(region); SR; 409 SR = dyn_cast<SubRegion>(SR->getSuperRegion())) { 410 if (const auto ER = dyn_cast<ElementRegion>(SR)) { 411 SVal Idx = ER->getIndex(); 412 for (auto SI = Idx.symbol_begin(), SE = Idx.symbol_end(); SI != SE; ++SI) 413 markLive(*SI); 414 } 415 } 416 } 417 418 void SymbolReaper::markInUse(SymbolRef sym) { 419 if (isa<SymbolMetadata>(sym)) 420 MetadataInUse.insert(sym); 421 } 422 423 bool SymbolReaper::maybeDead(SymbolRef sym) { 424 if (isLive(sym)) 425 return false; 426 427 TheDead.insert(sym); 428 return true; 429 } 430 431 bool SymbolReaper::isLiveRegion(const MemRegion *MR) { 432 if (RegionRoots.count(MR)) 433 return true; 434 435 MR = MR->getBaseRegion(); 436 437 if (const auto *SR = dyn_cast<SymbolicRegion>(MR)) 438 return isLive(SR->getSymbol()); 439 440 if (const auto *VR = dyn_cast<VarRegion>(MR)) 441 return isLive(VR, true); 442 443 // FIXME: This is a gross over-approximation. What we really need is a way to 444 // tell if anything still refers to this region. Unlike SymbolicRegions, 445 // AllocaRegions don't have associated symbols, though, so we don't actually 446 // have a way to track their liveness. 447 if (isa<AllocaRegion>(MR)) 448 return true; 449 450 if (isa<CXXThisRegion>(MR)) 451 return true; 452 453 if (isa<MemSpaceRegion>(MR)) 454 return true; 455 456 if (isa<CodeTextRegion>(MR)) 457 return true; 458 459 return false; 460 } 461 462 bool SymbolReaper::isLive(SymbolRef sym) { 463 if (TheLiving.count(sym)) { 464 markDependentsLive(sym); 465 return true; 466 } 467 468 bool KnownLive; 469 470 switch (sym->getKind()) { 471 case SymExpr::SymbolRegionValueKind: 472 KnownLive = isLiveRegion(cast<SymbolRegionValue>(sym)->getRegion()); 473 break; 474 case SymExpr::SymbolConjuredKind: 475 KnownLive = false; 476 break; 477 case SymExpr::SymbolDerivedKind: 478 KnownLive = isLive(cast<SymbolDerived>(sym)->getParentSymbol()); 479 break; 480 case SymExpr::SymbolExtentKind: 481 KnownLive = isLiveRegion(cast<SymbolExtent>(sym)->getRegion()); 482 break; 483 case SymExpr::SymbolMetadataKind: 484 KnownLive = MetadataInUse.count(sym) && 485 isLiveRegion(cast<SymbolMetadata>(sym)->getRegion()); 486 if (KnownLive) 487 MetadataInUse.erase(sym); 488 break; 489 case SymExpr::SymIntExprKind: 490 KnownLive = isLive(cast<SymIntExpr>(sym)->getLHS()); 491 break; 492 case SymExpr::IntSymExprKind: 493 KnownLive = isLive(cast<IntSymExpr>(sym)->getRHS()); 494 break; 495 case SymExpr::SymSymExprKind: 496 KnownLive = isLive(cast<SymSymExpr>(sym)->getLHS()) && 497 isLive(cast<SymSymExpr>(sym)->getRHS()); 498 break; 499 case SymExpr::SymbolCastKind: 500 KnownLive = isLive(cast<SymbolCast>(sym)->getOperand()); 501 break; 502 } 503 504 if (KnownLive) 505 markLive(sym); 506 507 return KnownLive; 508 } 509 510 bool 511 SymbolReaper::isLive(const Stmt *ExprVal, const LocationContext *ELCtx) const { 512 if (LCtx == nullptr) 513 return false; 514 515 if (LCtx != ELCtx) { 516 // If the reaper's location context is a parent of the expression's 517 // location context, then the expression value is now "out of scope". 518 if (LCtx->isParentOf(ELCtx)) 519 return false; 520 return true; 521 } 522 523 // If no statement is provided, everything is this and parent contexts is live. 524 if (!Loc) 525 return true; 526 527 return LCtx->getAnalysis<RelaxedLiveVariables>()->isLive(Loc, ExprVal); 528 } 529 530 bool SymbolReaper::isLive(const VarRegion *VR, bool includeStoreBindings) const{ 531 const StackFrameContext *VarContext = VR->getStackFrame(); 532 533 if (!VarContext) 534 return true; 535 536 if (!LCtx) 537 return false; 538 const StackFrameContext *CurrentContext = LCtx->getStackFrame(); 539 540 if (VarContext == CurrentContext) { 541 // If no statement is provided, everything is live. 542 if (!Loc) 543 return true; 544 545 if (LCtx->getAnalysis<RelaxedLiveVariables>()->isLive(Loc, VR->getDecl())) 546 return true; 547 548 if (!includeStoreBindings) 549 return false; 550 551 unsigned &cachedQuery = 552 const_cast<SymbolReaper *>(this)->includedRegionCache[VR]; 553 554 if (cachedQuery) { 555 return cachedQuery == 1; 556 } 557 558 // Query the store to see if the region occurs in any live bindings. 559 if (Store store = reapedStore.getStore()) { 560 bool hasRegion = 561 reapedStore.getStoreManager().includedInBindings(store, VR); 562 cachedQuery = hasRegion ? 1 : 2; 563 return hasRegion; 564 } 565 566 return false; 567 } 568 569 return VarContext->isParentOf(CurrentContext); 570 } 571