2018-04-04 05:31:50 +08:00
|
|
|
//===- MemRegion.cpp - Abstract memory regions for static analysis --------===//
|
2008-10-04 13:50:14 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2008-10-04 13:50:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines MemRegion and its subclasses. MemRegion defines a
|
|
|
|
// partially-typed abstraction of memory useful for path-sensitive dataflow
|
|
|
|
// analyses.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
|
2018-04-04 05:31:50 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2010-01-12 01:06:35 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2018-04-04 05:31:50 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-01-28 20:06:22 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2018-04-04 05:31:50 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
2010-08-02 12:56:14 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2018-04-04 05:31:50 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2017-09-07 05:45:03 +08:00
|
|
|
#include "clang/Analysis/AnalysisDeclContext.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/Support/BumpVector.h"
|
2018-04-04 05:31:50 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Basic/LLVM.h"
|
2012-01-05 07:54:01 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2021-09-04 16:19:57 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
|
2021-04-06 01:20:43 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
|
2018-04-04 05:31:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
|
|
|
|
#include "llvm/ADT/APInt.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/PointerUnion.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2018-06-14 02:32:19 +08:00
|
|
|
#include "llvm/Support/CheckedArithmetic.h"
|
2018-04-04 05:31:50 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2018-02-27 08:05:04 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2018-04-04 05:31:50 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
2008-10-04 13:50:14 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2008-10-04 13:50:14 +08:00
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
#define DEBUG_TYPE "MemRegion"
|
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemRegion Construction.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-04-13 17:56:07 +08:00
|
|
|
template <typename RegionTy, typename SuperTy, typename Arg1Ty>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1,
|
|
|
|
const SuperTy *superRegion) {
|
2009-12-04 08:05:57 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2017-04-13 17:56:07 +08:00
|
|
|
RegionTy::ProfileRegion(ID, arg1, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2018-04-04 05:31:50 +08:00
|
|
|
auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
if (!R) {
|
2016-02-03 23:20:51 +08:00
|
|
|
R = A.Allocate<RegionTy>();
|
2017-04-13 17:56:07 +08:00
|
|
|
new (R) RegionTy(arg1, superRegion);
|
2009-12-04 08:05:57 +08:00
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:56:07 +08:00
|
|
|
template <typename RegionTy, typename SuperTy, typename Arg1Ty, typename Arg2Ty>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
|
|
|
|
const SuperTy *superRegion) {
|
2009-12-04 08:05:57 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2017-04-13 17:56:07 +08:00
|
|
|
RegionTy::ProfileRegion(ID, arg1, arg2, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2018-04-04 05:31:50 +08:00
|
|
|
auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
if (!R) {
|
2016-02-03 23:20:51 +08:00
|
|
|
R = A.Allocate<RegionTy>();
|
2017-04-13 17:56:07 +08:00
|
|
|
new (R) RegionTy(arg1, arg2, superRegion);
|
2009-12-04 08:05:57 +08:00
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:56:07 +08:00
|
|
|
template <typename RegionTy, typename SuperTy,
|
|
|
|
typename Arg1Ty, typename Arg2Ty, typename Arg3Ty>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
|
|
|
|
const Arg3Ty arg3,
|
|
|
|
const SuperTy *superRegion) {
|
2009-12-08 06:05:27 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2017-04-13 17:56:07 +08:00
|
|
|
RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2018-04-04 05:31:50 +08:00
|
|
|
auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
if (!R) {
|
2016-02-03 23:20:51 +08:00
|
|
|
R = A.Allocate<RegionTy>();
|
2017-04-13 17:56:07 +08:00
|
|
|
new (R) RegionTy(arg1, arg2, arg3, superRegion);
|
2009-12-08 06:05:27 +08:00
|
|
|
Regions.InsertNode(R, InsertPos);
|
2009-12-04 08:05:57 +08:00
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return R;
|
|
|
|
}
|
2009-12-04 08:05:57 +08:00
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-11-26 10:34:36 +08:00
|
|
|
// Object destruction.
|
2009-06-23 07:13:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-10-04 13:50:14 +08:00
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
MemRegion::~MemRegion() = default;
|
2008-10-04 13:50:14 +08:00
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
// All regions and their data are BumpPtrAllocated. No need to call their
|
|
|
|
// destructors.
|
|
|
|
MemRegionManager::~MemRegionManager() = default;
|
2009-11-26 10:34:36 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basic methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-08 21:17:14 +08:00
|
|
|
bool SubRegion::isSubRegionOf(const MemRegion* R) const {
|
2018-01-18 04:27:26 +08:00
|
|
|
const MemRegion* r = this;
|
|
|
|
do {
|
2009-01-08 21:17:14 +08:00
|
|
|
if (r == R)
|
|
|
|
return true;
|
2018-04-04 05:31:50 +08:00
|
|
|
if (const auto *sr = dyn_cast<SubRegion>(r))
|
2009-01-08 21:17:14 +08:00
|
|
|
r = sr->getSuperRegion();
|
|
|
|
else
|
|
|
|
break;
|
2018-01-18 04:27:26 +08:00
|
|
|
} while (r != nullptr);
|
2009-01-08 21:17:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-30 23:04:37 +08:00
|
|
|
MemRegionManager &SubRegion::getMemRegionManager() const {
|
2009-06-23 08:46:41 +08:00
|
|
|
const SubRegion* r = this;
|
|
|
|
do {
|
|
|
|
const MemRegion *superRegion = r->getSuperRegion();
|
2018-04-04 05:31:50 +08:00
|
|
|
if (const auto *sr = dyn_cast<SubRegion>(superRegion)) {
|
2009-06-23 08:46:41 +08:00
|
|
|
r = sr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return superRegion->getMemRegionManager();
|
2018-04-04 05:31:50 +08:00
|
|
|
} while (true);
|
2009-06-23 08:46:41 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 06:15:06 +08:00
|
|
|
const StackFrameContext *VarRegion::getStackFrame() const {
|
2018-04-04 05:31:50 +08:00
|
|
|
const auto *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
|
2014-05-27 10:45:47 +08:00
|
|
|
return SSR ? SSR->getStackFrame() : nullptr;
|
2009-12-15 06:15:06 +08:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:56:07 +08:00
|
|
|
ObjCIvarRegion::ObjCIvarRegion(const ObjCIvarDecl *ivd, const SubRegion *sReg)
|
2022-05-27 17:05:50 +08:00
|
|
|
: DeclRegion(sReg, ObjCIvarRegionKind), IVD(ivd) {
|
|
|
|
assert(IVD);
|
|
|
|
}
|
2012-01-28 20:06:22 +08:00
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
const ObjCIvarDecl *ObjCIvarRegion::getDecl() const { return IVD; }
|
2012-01-28 20:06:22 +08:00
|
|
|
|
|
|
|
QualType ObjCIvarRegion::getValueType() const {
|
|
|
|
return getDecl()->getType();
|
|
|
|
}
|
|
|
|
|
2010-11-26 16:21:53 +08:00
|
|
|
QualType CXXBaseObjectRegion::getValueType() const {
|
2013-02-21 11:12:32 +08:00
|
|
|
return QualType(getDecl()->getTypeForDecl(), 0);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2018-08-30 06:43:31 +08:00
|
|
|
QualType CXXDerivedObjectRegion::getValueType() const {
|
|
|
|
return QualType(getDecl()->getTypeForDecl(), 0);
|
|
|
|
}
|
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
QualType ParamVarRegion::getValueType() const {
|
|
|
|
assert(getDecl() &&
|
|
|
|
"`ParamVarRegion` support functions without `Decl` not implemented"
|
|
|
|
" yet.");
|
|
|
|
return getDecl()->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ParmVarDecl *ParamVarRegion::getDecl() const {
|
|
|
|
const Decl *D = getStackFrame()->getDecl();
|
|
|
|
|
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
assert(Index < FD->param_size());
|
|
|
|
return FD->parameters()[Index];
|
|
|
|
} else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
|
|
|
|
assert(Index < BD->param_size());
|
|
|
|
return BD->parameters()[Index];
|
|
|
|
} else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
|
|
|
assert(Index < MD->param_size());
|
|
|
|
return MD->parameters()[Index];
|
|
|
|
} else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D)) {
|
|
|
|
assert(Index < CD->param_size());
|
|
|
|
return CD->parameters()[Index];
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Unexpected Decl kind!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 06:15:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FoldingSet profiling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void MemSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(getKind()));
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(getKind()));
|
2009-12-08 06:05:27 +08:00
|
|
|
ID.AddPointer(getStackFrame());
|
|
|
|
}
|
|
|
|
|
2010-07-02 04:16:50 +08:00
|
|
|
void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(getKind()));
|
2010-07-02 04:16:50 +08:00
|
|
|
ID.AddPointer(getCodeRegion());
|
|
|
|
}
|
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
void StringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
|
|
|
const StringLiteral *Str,
|
|
|
|
const MemRegion *superRegion) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(StringRegionKind));
|
2008-10-25 22:13:41 +08:00
|
|
|
ID.AddPointer(Str);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
|
|
|
const ObjCStringLiteral *Str,
|
|
|
|
const MemRegion *superRegion) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(ObjCStringRegionKind));
|
2012-02-28 08:56:05 +08:00
|
|
|
ID.AddPointer(Str);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2008-11-02 08:34:33 +08:00
|
|
|
void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2011-08-13 07:37:29 +08:00
|
|
|
const Expr *Ex, unsigned cnt,
|
2012-11-27 03:11:46 +08:00
|
|
|
const MemRegion *superRegion) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(AllocaRegionKind));
|
2008-11-02 08:34:33 +08:00
|
|
|
ID.AddPointer(Ex);
|
|
|
|
ID.AddInteger(cnt);
|
2012-11-27 03:11:46 +08:00
|
|
|
ID.AddPointer(superRegion);
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-06-23 08:15:41 +08:00
|
|
|
ProfileRegion(ID, Ex, Cnt, superRegion);
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
2008-10-28 04:57:58 +08:00
|
|
|
void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2011-08-13 07:37:29 +08:00
|
|
|
const CompoundLiteralExpr *CL,
|
2008-10-28 04:57:58 +08:00
|
|
|
const MemRegion* superRegion) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(CompoundLiteralRegionKind));
|
2008-10-28 04:57:58 +08:00
|
|
|
ID.AddPointer(CL);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
|
|
|
const PointerType *PT,
|
|
|
|
const MemRegion *sRegion) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(CXXThisRegionKind));
|
2010-01-05 10:18:06 +08:00
|
|
|
ID.AddPointer(PT);
|
|
|
|
ID.AddPointer(sRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
void FieldRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ProfileRegion(ID, getDecl(), superRegion);
|
|
|
|
}
|
|
|
|
|
2012-01-28 20:06:22 +08:00
|
|
|
void ObjCIvarRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const ObjCIvarDecl *ivd,
|
|
|
|
const MemRegion* superRegion) {
|
2020-05-11 21:00:42 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(ObjCIvarRegionKind));
|
|
|
|
ID.AddPointer(ivd);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCIvarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ProfileRegion(ID, getDecl(), superRegion);
|
2012-01-28 20:06:22 +08:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
void NonParamVarRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
|
|
|
const VarDecl *VD,
|
|
|
|
const MemRegion *superRegion) {
|
|
|
|
ID.AddInteger(static_cast<unsigned>(NonParamVarRegionKind));
|
|
|
|
ID.AddPointer(VD);
|
2008-10-04 13:50:14 +08:00
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
void NonParamVarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ProfileRegion(ID, getDecl(), superRegion);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
void ParamVarRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, const Expr *OE,
|
|
|
|
unsigned Idx, const MemRegion *SReg) {
|
|
|
|
ID.AddInteger(static_cast<unsigned>(ParamVarRegionKind));
|
|
|
|
ID.AddPointer(OE);
|
|
|
|
ID.AddInteger(Idx);
|
|
|
|
ID.AddPointer(SReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParamVarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ProfileRegion(ID, getOriginExpr(), getIndex(), superRegion);
|
2009-08-22 06:28:32 +08:00
|
|
|
}
|
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
|
|
|
|
const MemRegion *sreg) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(MemRegion::SymbolicRegionKind));
|
2008-12-05 10:39:38 +08:00
|
|
|
ID.Add(sym);
|
2009-06-23 07:13:13 +08:00
|
|
|
ID.AddPointer(sreg);
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-06-23 07:13:13 +08:00
|
|
|
SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
2009-05-04 14:18:28 +08:00
|
|
|
void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType ElementType, SVal Idx,
|
2008-10-21 13:27:10 +08:00
|
|
|
const MemRegion* superRegion) {
|
|
|
|
ID.AddInteger(MemRegion::ElementRegionKind);
|
2009-05-04 14:18:28 +08:00
|
|
|
ID.Add(ElementType);
|
2008-10-21 13:27:10 +08:00
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
Idx.Profile(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-05-04 14:18:28 +08:00
|
|
|
ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
|
2008-10-21 13:27:10 +08:00
|
|
|
}
|
2008-10-27 21:17:02 +08:00
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void FunctionCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2012-09-18 03:13:56 +08:00
|
|
|
const NamedDecl *FD,
|
2009-11-25 09:32:22 +08:00
|
|
|
const MemRegion*) {
|
2016-01-13 21:49:29 +08:00
|
|
|
ID.AddInteger(MemRegion::FunctionCodeRegionKind);
|
2009-08-28 12:49:15 +08:00
|
|
|
ID.AddPointer(FD);
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void FunctionCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
FunctionCodeRegion::ProfileRegion(ID, FD, superRegion);
|
2009-11-25 09:32:22 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void BlockCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2009-12-08 06:05:27 +08:00
|
|
|
const BlockDecl *BD, CanQualType,
|
2011-10-24 09:32:45 +08:00
|
|
|
const AnalysisDeclContext *AC,
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemRegion*) {
|
2016-01-13 21:49:29 +08:00
|
|
|
ID.AddInteger(MemRegion::BlockCodeRegionKind);
|
2009-11-25 09:32:22 +08:00
|
|
|
ID.AddPointer(BD);
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void BlockCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
BlockCodeRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2009-11-26 07:53:07 +08:00
|
|
|
void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2016-01-13 21:49:29 +08:00
|
|
|
const BlockCodeRegion *BC,
|
2009-11-26 07:53:07 +08:00
|
|
|
const LocationContext *LC,
|
2013-11-20 08:11:42 +08:00
|
|
|
unsigned BlkCount,
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemRegion *sReg) {
|
2009-11-26 07:53:07 +08:00
|
|
|
ID.AddInteger(MemRegion::BlockDataRegionKind);
|
|
|
|
ID.AddPointer(BC);
|
|
|
|
ID.AddPointer(LC);
|
2013-11-20 08:11:42 +08:00
|
|
|
ID.AddInteger(BlkCount);
|
2009-12-08 06:05:27 +08:00
|
|
|
ID.AddPointer(sReg);
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2013-11-20 08:11:42 +08:00
|
|
|
BlockDataRegion::ProfileRegion(ID, BC, LC, BlockCount, getSuperRegion());
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:52:48 +08:00
|
|
|
void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
|
|
|
Expr const *Ex,
|
|
|
|
const MemRegion *sReg) {
|
2010-01-09 17:16:47 +08:00
|
|
|
ID.AddPointer(Ex);
|
2009-12-16 19:27:52 +08:00
|
|
|
ID.AddPointer(sReg);
|
|
|
|
}
|
|
|
|
|
2010-11-26 16:52:48 +08:00
|
|
|
void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2010-01-09 17:16:47 +08:00
|
|
|
ProfileRegion(ID, Ex, getSuperRegion());
|
2009-12-16 19:27:52 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:21:53 +08:00
|
|
|
void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
2013-02-21 11:12:32 +08:00
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
bool IsVirtual,
|
|
|
|
const MemRegion *SReg) {
|
|
|
|
ID.AddPointer(RD);
|
|
|
|
ID.AddBoolean(IsVirtual);
|
|
|
|
ID.AddPointer(SReg);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2013-02-21 11:12:32 +08:00
|
|
|
ProfileRegion(ID, getDecl(), isVirtual(), superRegion);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2018-08-30 06:43:31 +08:00
|
|
|
void CXXDerivedObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
const MemRegion *SReg) {
|
|
|
|
ID.AddPointer(RD);
|
|
|
|
ID.AddPointer(SReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXXDerivedObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ProfileRegion(ID, getDecl(), superRegion);
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region anchors.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
void GlobalsSpaceRegion::anchor() {}
|
|
|
|
|
|
|
|
void NonStaticGlobalSpaceRegion::anchor() {}
|
|
|
|
|
|
|
|
void StackSpaceRegion::anchor() {}
|
|
|
|
|
|
|
|
void TypedRegion::anchor() {}
|
|
|
|
|
|
|
|
void TypedValueRegion::anchor() {}
|
|
|
|
|
|
|
|
void CodeTextRegion::anchor() {}
|
|
|
|
|
|
|
|
void SubRegion::anchor() {}
|
2011-12-20 10:48:34 +08:00
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region pretty-printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void MemRegion::dump() const {
|
2009-07-14 07:31:04 +08:00
|
|
|
dumpToStream(llvm::errs());
|
2009-07-03 01:24:10 +08:00
|
|
|
}
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
std::string MemRegion::getString() const {
|
|
|
|
std::string s;
|
|
|
|
llvm::raw_string_ostream os(s);
|
2009-07-14 07:31:04 +08:00
|
|
|
dumpToStream(os);
|
2021-12-10 06:51:24 +08:00
|
|
|
return s;
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void MemRegion::dumpToStream(raw_ostream &os) const {
|
2008-10-04 13:50:14 +08:00
|
|
|
os << "<Unknown Region>";
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void AllocaRegion::dumpToStream(raw_ostream &os) const {
|
2018-09-15 10:34:45 +08:00
|
|
|
os << "alloca{S" << Ex->getID(getContext()) << ',' << Cnt << '}';
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void FunctionCodeRegion::dumpToStream(raw_ostream &os) const {
|
2009-08-28 12:49:15 +08:00
|
|
|
os << "code{" << getDecl()->getDeclName().getAsString() << '}';
|
2009-04-22 03:56:58 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void BlockCodeRegion::dumpToStream(raw_ostream &os) const {
|
2018-04-04 05:31:50 +08:00
|
|
|
os << "block_code{" << static_cast<const void *>(this) << '}';
|
2009-11-25 09:32:22 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void BlockDataRegion::dumpToStream(raw_ostream &os) const {
|
2013-11-20 08:11:42 +08:00
|
|
|
os << "block_data{" << BC;
|
|
|
|
os << "; ";
|
|
|
|
for (BlockDataRegion::referenced_vars_iterator
|
|
|
|
I = referenced_vars_begin(),
|
|
|
|
E = referenced_vars_end(); I != E; ++I)
|
2018-03-30 06:07:58 +08:00
|
|
|
os << "(" << I.getCapturedRegion() << "<-" <<
|
2013-11-20 08:11:42 +08:00
|
|
|
I.getOriginalRegion() << ") ";
|
|
|
|
os << '}';
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const {
|
2009-04-22 02:09:22 +08:00
|
|
|
// FIXME: More elaborate pretty-printing.
|
2018-09-15 10:34:45 +08:00
|
|
|
os << "{ S" << CL->getID(getContext()) << " }";
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
|
2022-04-21 05:09:03 +08:00
|
|
|
os << "temp_object{" << getValueType() << ", "
|
2018-09-15 10:34:45 +08:00
|
|
|
<< "S" << Ex->getID(getContext()) << '}';
|
2010-11-25 10:07:24 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const {
|
2018-08-30 06:43:31 +08:00
|
|
|
os << "Base{" << superRegion << ',' << getDecl()->getName() << '}';
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXXDerivedObjectRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "Derived{" << superRegion << ',' << getDecl()->getName() << '}';
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CXXThisRegion::dumpToStream(raw_ostream &os) const {
|
2010-01-05 10:18:06 +08:00
|
|
|
os << "this";
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void ElementRegion::dumpToStream(raw_ostream &os) const {
|
2022-04-21 05:09:03 +08:00
|
|
|
os << "Element{" << superRegion << ',' << Index << ',' << getElementType()
|
|
|
|
<< '}';
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void FieldRegion::dumpToStream(raw_ostream &os) const {
|
2019-10-19 04:15:39 +08:00
|
|
|
os << superRegion << "." << *getDecl();
|
2008-10-18 05:05:44 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void ObjCIvarRegion::dumpToStream(raw_ostream &os) const {
|
2018-08-30 06:43:31 +08:00
|
|
|
os << "Ivar{" << superRegion << ',' << *getDecl() << '}';
|
2009-07-20 04:36:24 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void StringRegion::dumpToStream(raw_ostream &os) const {
|
2014-06-10 06:53:25 +08:00
|
|
|
assert(Str != nullptr && "Expecting non-null StringLiteral");
|
2014-05-27 10:45:47 +08:00
|
|
|
Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
|
2008-10-24 14:30:07 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 08:56:05 +08:00
|
|
|
void ObjCStringRegion::dumpToStream(raw_ostream &os) const {
|
2014-06-10 06:53:25 +08:00
|
|
|
assert(Str != nullptr && "Expecting non-null ObjCStringLiteral");
|
2014-05-27 10:45:47 +08:00
|
|
|
Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
|
2012-02-28 08:56:05 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void SymbolicRegion::dumpToStream(raw_ostream &os) const {
|
2017-12-06 01:14:39 +08:00
|
|
|
if (isa<HeapSpaceRegion>(getSuperRegion()))
|
|
|
|
os << "Heap";
|
2009-07-14 07:38:57 +08:00
|
|
|
os << "SymRegion{" << sym << '}';
|
2008-10-28 04:57:58 +08:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
void NonParamVarRegion::dumpToStream(raw_ostream &os) const {
|
2018-04-04 05:31:50 +08:00
|
|
|
if (const IdentifierInfo *ID = VD->getIdentifier())
|
2018-03-30 06:07:58 +08:00
|
|
|
os << ID->getName();
|
2018-04-04 05:31:50 +08:00
|
|
|
else
|
2020-05-11 21:00:42 +08:00
|
|
|
os << "NonParamVarRegion{D" << VD->getID() << '}';
|
2008-11-10 21:05:26 +08:00
|
|
|
}
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void RegionRawOffset::dump() const {
|
2009-08-01 14:17:29 +08:00
|
|
|
dumpToStream(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void RegionRawOffset::dumpToStream(raw_ostream &os) const {
|
2011-01-24 09:55:39 +08:00
|
|
|
os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void CodeSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "CodeSpaceRegion";
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const {
|
2010-07-07 07:37:21 +08:00
|
|
|
os << "StaticGlobalsMemSpace{" << CR << '}';
|
|
|
|
}
|
|
|
|
|
2012-01-05 07:54:01 +08:00
|
|
|
void GlobalInternalSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "GlobalInternalSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSystemSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "GlobalSystemSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalImmutableSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "GlobalImmutableSpaceRegion";
|
|
|
|
}
|
|
|
|
|
2012-06-07 04:47:00 +08:00
|
|
|
void HeapSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "HeapSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnknownSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "UnknownSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackArgumentsSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "StackArgumentsSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackLocalsSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "StackLocalsSpaceRegion";
|
|
|
|
}
|
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
void ParamVarRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
const ParmVarDecl *PVD = getDecl();
|
|
|
|
assert(PVD &&
|
|
|
|
"`ParamVarRegion` support functions without `Decl` not implemented"
|
|
|
|
" yet.");
|
|
|
|
if (const IdentifierInfo *ID = PVD->getIdentifier()) {
|
|
|
|
os << ID->getName();
|
|
|
|
} else {
|
|
|
|
os << "ParamVarRegion{P" << PVD->getID() << '}';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:36 +08:00
|
|
|
bool MemRegion::canPrintPretty() const {
|
2013-04-20 09:15:36 +08:00
|
|
|
return canPrintPrettyAsExpr();
|
2012-08-09 02:23:36 +08:00
|
|
|
}
|
|
|
|
|
2013-04-16 06:37:59 +08:00
|
|
|
bool MemRegion::canPrintPrettyAsExpr() const {
|
2013-04-20 09:15:36 +08:00
|
|
|
return false;
|
2013-04-16 06:37:59 +08:00
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:36 +08:00
|
|
|
void MemRegion::printPretty(raw_ostream &os) const {
|
2013-04-13 02:40:21 +08:00
|
|
|
assert(canPrintPretty() && "This region cannot be printed pretty.");
|
|
|
|
os << "'";
|
2013-04-16 06:37:59 +08:00
|
|
|
printPrettyAsExpr(os);
|
2013-04-13 02:40:21 +08:00
|
|
|
os << "'";
|
|
|
|
}
|
|
|
|
|
2018-09-29 02:49:41 +08:00
|
|
|
void MemRegion::printPrettyAsExpr(raw_ostream &) const {
|
2013-04-16 06:37:59 +08:00
|
|
|
llvm_unreachable("This region cannot be printed pretty.");
|
2012-03-22 03:45:08 +08:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
bool NonParamVarRegion::canPrintPrettyAsExpr() const { return true; }
|
|
|
|
|
|
|
|
void NonParamVarRegion::printPrettyAsExpr(raw_ostream &os) const {
|
|
|
|
os << getDecl()->getName();
|
2012-08-09 02:23:36 +08:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
bool ParamVarRegion::canPrintPrettyAsExpr() const { return true; }
|
|
|
|
|
|
|
|
void ParamVarRegion::printPrettyAsExpr(raw_ostream &os) const {
|
|
|
|
assert(getDecl() &&
|
|
|
|
"`ParamVarRegion` support functions without `Decl` not implemented"
|
|
|
|
" yet.");
|
2012-03-22 03:45:08 +08:00
|
|
|
os << getDecl()->getName();
|
|
|
|
}
|
|
|
|
|
2013-04-20 09:15:36 +08:00
|
|
|
bool ObjCIvarRegion::canPrintPrettyAsExpr() const {
|
2013-02-24 15:21:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-16 06:37:59 +08:00
|
|
|
void ObjCIvarRegion::printPrettyAsExpr(raw_ostream &os) const {
|
2013-02-24 15:21:01 +08:00
|
|
|
os << getDecl()->getName();
|
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:36 +08:00
|
|
|
bool FieldRegion::canPrintPretty() const {
|
2013-04-13 02:40:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-16 06:37:59 +08:00
|
|
|
bool FieldRegion::canPrintPrettyAsExpr() const {
|
|
|
|
return superRegion->canPrintPrettyAsExpr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FieldRegion::printPrettyAsExpr(raw_ostream &os) const {
|
|
|
|
assert(canPrintPrettyAsExpr());
|
|
|
|
superRegion->printPrettyAsExpr(os);
|
|
|
|
os << "." << getDecl()->getName();
|
2012-08-09 02:23:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FieldRegion::printPretty(raw_ostream &os) const {
|
2013-04-16 06:37:59 +08:00
|
|
|
if (canPrintPrettyAsExpr()) {
|
2013-04-13 02:40:21 +08:00
|
|
|
os << "\'";
|
2013-04-16 06:37:59 +08:00
|
|
|
printPrettyAsExpr(os);
|
2013-04-13 02:40:21 +08:00
|
|
|
os << "'";
|
|
|
|
} else {
|
2013-04-16 06:37:59 +08:00
|
|
|
os << "field " << "\'" << getDecl()->getName() << "'";
|
2013-04-13 02:40:21 +08:00
|
|
|
}
|
2012-03-22 03:45:08 +08:00
|
|
|
}
|
|
|
|
|
2013-04-16 06:38:04 +08:00
|
|
|
bool CXXBaseObjectRegion::canPrintPrettyAsExpr() const {
|
2013-04-16 06:37:59 +08:00
|
|
|
return superRegion->canPrintPrettyAsExpr();
|
|
|
|
}
|
|
|
|
|
2013-04-16 06:38:04 +08:00
|
|
|
void CXXBaseObjectRegion::printPrettyAsExpr(raw_ostream &os) const {
|
|
|
|
superRegion->printPrettyAsExpr(os);
|
|
|
|
}
|
|
|
|
|
2018-08-30 06:43:31 +08:00
|
|
|
bool CXXDerivedObjectRegion::canPrintPrettyAsExpr() const {
|
|
|
|
return superRegion->canPrintPrettyAsExpr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXXDerivedObjectRegion::printPrettyAsExpr(raw_ostream &os) const {
|
|
|
|
superRegion->printPrettyAsExpr(os);
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:22:41 +08:00
|
|
|
std::string MemRegion::getDescriptiveName(bool UseQuotes) const {
|
|
|
|
std::string VariableName;
|
|
|
|
std::string ArrayIndices;
|
|
|
|
const MemRegion *R = this;
|
|
|
|
SmallString<50> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
|
|
|
|
// Obtain array indices to add them to the variable name.
|
|
|
|
const ElementRegion *ER = nullptr;
|
|
|
|
while ((ER = R->getAs<ElementRegion>())) {
|
|
|
|
// Index is a ConcreteInt.
|
|
|
|
if (auto CI = ER->getIndex().getAs<nonloc::ConcreteInt>()) {
|
|
|
|
llvm::SmallString<2> Idx;
|
|
|
|
CI->getValue().toString(Idx);
|
|
|
|
ArrayIndices = (llvm::Twine("[") + Idx.str() + "]" + ArrayIndices).str();
|
|
|
|
}
|
|
|
|
// If not a ConcreteInt, try to obtain the variable
|
|
|
|
// name by calling 'getDescriptiveName' recursively.
|
|
|
|
else {
|
|
|
|
std::string Idx = ER->getDescriptiveName(false);
|
|
|
|
if (!Idx.empty()) {
|
|
|
|
ArrayIndices = (llvm::Twine("[") + Idx + "]" + ArrayIndices).str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
R = ER->getSuperRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get variable name.
|
|
|
|
if (R && R->canPrintPrettyAsExpr()) {
|
|
|
|
R->printPrettyAsExpr(os);
|
2018-04-04 05:31:50 +08:00
|
|
|
if (UseQuotes)
|
2016-06-13 11:22:41 +08:00
|
|
|
return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str();
|
2018-04-04 05:31:50 +08:00
|
|
|
else
|
2016-06-13 11:22:41 +08:00
|
|
|
return (llvm::Twine(os.str()) + ArrayIndices).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
return VariableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceRange MemRegion::sourceRange() const {
|
2018-04-04 05:31:50 +08:00
|
|
|
const auto *const VR = dyn_cast<VarRegion>(this->getBaseRegion());
|
|
|
|
const auto *const FR = dyn_cast<FieldRegion>(this);
|
2016-06-13 11:22:41 +08:00
|
|
|
|
|
|
|
// Check for more specific regions first.
|
|
|
|
// FieldRegion
|
|
|
|
if (FR) {
|
|
|
|
return FR->getDecl()->getSourceRange();
|
|
|
|
}
|
|
|
|
// VarRegion
|
|
|
|
else if (VR) {
|
|
|
|
return VR->getDecl()->getSourceRange();
|
|
|
|
}
|
|
|
|
// Return invalid source range (can be checked by client).
|
2018-04-04 05:31:50 +08:00
|
|
|
else
|
|
|
|
return {};
|
2016-06-13 11:22:41 +08:00
|
|
|
}
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemRegionManager methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-01 14:17:29 +08:00
|
|
|
|
2020-01-30 23:04:37 +08:00
|
|
|
DefinedOrUnknownSVal MemRegionManager::getStaticSize(const MemRegion *MR,
|
|
|
|
SValBuilder &SVB) const {
|
|
|
|
const auto *SR = cast<SubRegion>(MR);
|
|
|
|
SymbolManager &SymMgr = SVB.getSymbolManager();
|
|
|
|
|
|
|
|
switch (SR->getKind()) {
|
|
|
|
case MemRegion::AllocaRegionKind:
|
|
|
|
case MemRegion::SymbolicRegionKind:
|
|
|
|
return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR));
|
|
|
|
case MemRegion::StringRegionKind:
|
|
|
|
return SVB.makeIntVal(
|
|
|
|
cast<StringRegion>(SR)->getStringLiteral()->getByteLength() + 1,
|
|
|
|
SVB.getArrayIndexType());
|
|
|
|
case MemRegion::CompoundLiteralRegionKind:
|
|
|
|
case MemRegion::CXXBaseObjectRegionKind:
|
|
|
|
case MemRegion::CXXDerivedObjectRegionKind:
|
|
|
|
case MemRegion::CXXTempObjectRegionKind:
|
|
|
|
case MemRegion::CXXThisRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2020-05-11 21:00:42 +08:00
|
|
|
case MemRegion::NonParamVarRegionKind:
|
|
|
|
case MemRegion::ParamVarRegionKind:
|
2020-01-30 23:04:37 +08:00
|
|
|
case MemRegion::ElementRegionKind:
|
|
|
|
case MemRegion::ObjCStringRegionKind: {
|
|
|
|
QualType Ty = cast<TypedValueRegion>(SR)->getDesugaredValueType(Ctx);
|
|
|
|
if (isa<VariableArrayType>(Ty))
|
|
|
|
return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR));
|
|
|
|
|
|
|
|
if (Ty->isIncompleteType())
|
|
|
|
return UnknownVal();
|
|
|
|
|
2021-04-06 01:20:43 +08:00
|
|
|
return getElementExtent(Ty, SVB);
|
2020-01-30 23:04:37 +08:00
|
|
|
}
|
|
|
|
case MemRegion::FieldRegionKind: {
|
|
|
|
// Force callers to deal with bitfields explicitly.
|
|
|
|
if (cast<FieldRegion>(SR)->getDecl()->isBitField())
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
QualType Ty = cast<TypedValueRegion>(SR)->getDesugaredValueType(Ctx);
|
2021-08-25 22:47:13 +08:00
|
|
|
const DefinedOrUnknownSVal Size = getElementExtent(Ty, SVB);
|
2020-01-30 23:04:37 +08:00
|
|
|
|
2021-09-04 16:19:57 +08:00
|
|
|
// We currently don't model flexible array members (FAMs), which are:
|
|
|
|
// - int array[]; of IncompleteArrayType
|
|
|
|
// - int array[0]; of ConstantArrayType with size 0
|
|
|
|
// - int array[1]; of ConstantArrayType with size 1 (*)
|
|
|
|
// (*): Consider single element array object members as FAM candidates only
|
|
|
|
// if the consider-single-element-arrays-as-flexible-array-members
|
|
|
|
// analyzer option is true.
|
|
|
|
// https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
|
|
|
|
const auto isFlexibleArrayMemberCandidate = [this,
|
|
|
|
&SVB](QualType Ty) -> bool {
|
2021-08-25 22:47:13 +08:00
|
|
|
const ArrayType *AT = Ctx.getAsArrayType(Ty);
|
|
|
|
if (!AT)
|
|
|
|
return false;
|
|
|
|
if (isa<IncompleteArrayType>(AT))
|
|
|
|
return true;
|
|
|
|
|
2022-06-02 14:53:45 +08:00
|
|
|
if (getContext().getLangOpts().StrictFlexArrays)
|
|
|
|
return false;
|
|
|
|
|
2021-08-25 22:47:13 +08:00
|
|
|
if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
|
|
|
|
const llvm::APInt &Size = CAT->getSize();
|
2021-09-09 13:13:13 +08:00
|
|
|
if (Size.isZero())
|
2021-08-25 22:47:13 +08:00
|
|
|
return true;
|
2021-09-04 16:19:57 +08:00
|
|
|
|
2021-09-04 16:19:57 +08:00
|
|
|
const AnalyzerOptions &Opts = SVB.getAnalyzerOptions();
|
2021-09-04 16:19:57 +08:00
|
|
|
if (Opts.ShouldConsiderSingleElementArraysAsFlexibleArrayMembers &&
|
2021-09-30 17:50:04 +08:00
|
|
|
Size.isOne())
|
2021-09-04 16:19:57 +08:00
|
|
|
return true;
|
2021-08-25 22:47:13 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isFlexibleArrayMemberCandidate(Ty))
|
|
|
|
return UnknownVal();
|
2020-01-30 23:04:37 +08:00
|
|
|
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
// FIXME: The following are being used in 'SimpleSValBuilder' and in
|
|
|
|
// 'ArrayBoundChecker::checkLocation' because there is no symbol to
|
|
|
|
// represent the regions more appropriately.
|
|
|
|
case MemRegion::BlockDataRegionKind:
|
|
|
|
case MemRegion::BlockCodeRegionKind:
|
|
|
|
case MemRegion::FunctionCodeRegionKind:
|
|
|
|
return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR));
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unhandled region");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
template <typename REG>
|
|
|
|
const REG *MemRegionManager::LazyAllocate(REG*& region) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!region) {
|
2016-02-03 23:20:51 +08:00
|
|
|
region = A.Allocate<REG>();
|
2020-01-30 23:04:37 +08:00
|
|
|
new (region) REG(*this);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
2009-06-23 08:46:41 +08:00
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
template <typename REG, typename ARG>
|
|
|
|
const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
|
|
|
|
if (!region) {
|
2016-02-03 23:20:51 +08:00
|
|
|
region = A.Allocate<REG>();
|
2009-12-08 06:05:27 +08:00
|
|
|
new (region) REG(this, a);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StackLocalsSpaceRegion*
|
2009-12-11 14:43:27 +08:00
|
|
|
MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
|
|
|
|
assert(STC);
|
2010-02-17 16:46:50 +08:00
|
|
|
StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
|
|
|
|
|
|
|
|
if (R)
|
|
|
|
return R;
|
|
|
|
|
|
|
|
R = A.Allocate<StackLocalsSpaceRegion>();
|
2020-01-30 23:04:37 +08:00
|
|
|
new (R) StackLocalsSpaceRegion(*this, STC);
|
2010-02-17 16:46:50 +08:00
|
|
|
return R;
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
const StackArgumentsSpaceRegion *
|
|
|
|
MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
|
2009-12-11 14:43:27 +08:00
|
|
|
assert(STC);
|
2010-02-17 16:46:50 +08:00
|
|
|
StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
|
|
|
|
|
|
|
|
if (R)
|
|
|
|
return R;
|
|
|
|
|
|
|
|
R = A.Allocate<StackArgumentsSpaceRegion>();
|
2020-01-30 23:04:37 +08:00
|
|
|
new (R) StackArgumentsSpaceRegion(*this, STC);
|
2010-02-17 16:46:50 +08:00
|
|
|
return R;
|
2009-07-03 02:14:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-02 04:16:50 +08:00
|
|
|
const GlobalsSpaceRegion
|
2012-01-05 07:54:01 +08:00
|
|
|
*MemRegionManager::getGlobalsRegion(MemRegion::Kind K,
|
|
|
|
const CodeTextRegion *CR) {
|
|
|
|
if (!CR) {
|
|
|
|
if (K == MemRegion::GlobalSystemSpaceRegionKind)
|
|
|
|
return LazyAllocate(SystemGlobals);
|
|
|
|
if (K == MemRegion::GlobalImmutableSpaceRegionKind)
|
|
|
|
return LazyAllocate(ImmutableGlobals);
|
|
|
|
assert(K == MemRegion::GlobalInternalSpaceRegionKind);
|
|
|
|
return LazyAllocate(InternalGlobals);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-01-05 07:54:01 +08:00
|
|
|
assert(K == MemRegion::StaticGlobalSpaceRegionKind);
|
2010-07-02 04:16:50 +08:00
|
|
|
StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
|
|
|
|
if (R)
|
|
|
|
return R;
|
|
|
|
|
|
|
|
R = A.Allocate<StaticGlobalSpaceRegion>();
|
2020-01-30 23:04:37 +08:00
|
|
|
new (R) StaticGlobalSpaceRegion(*this, CR);
|
2010-07-02 04:16:50 +08:00
|
|
|
return R;
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
|
2008-10-04 13:50:14 +08:00
|
|
|
return LazyAllocate(heap);
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
const UnknownSpaceRegion *MemRegionManager::getUnknownRegion() {
|
2008-10-08 10:50:44 +08:00
|
|
|
return LazyAllocate(unknown);
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
const CodeSpaceRegion *MemRegionManager::getCodeRegion() {
|
2009-04-10 16:45:10 +08:00
|
|
|
return LazyAllocate(code);
|
|
|
|
}
|
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Constructing regions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-04-04 05:31:50 +08:00
|
|
|
|
|
|
|
const StringRegion *MemRegionManager::getStringRegion(const StringLiteral *Str){
|
2017-04-13 17:56:07 +08:00
|
|
|
return getSubRegion<StringRegion>(
|
|
|
|
Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion()));
|
2008-10-25 22:13:41 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 08:56:05 +08:00
|
|
|
const ObjCStringRegion *
|
2018-04-04 05:31:50 +08:00
|
|
|
MemRegionManager::getObjCStringRegion(const ObjCStringLiteral *Str){
|
2017-04-13 17:56:07 +08:00
|
|
|
return getSubRegion<ObjCStringRegion>(
|
|
|
|
Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion()));
|
2012-02-28 08:56:05 +08:00
|
|
|
}
|
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
/// Look through a chain of LocationContexts to either find the
|
|
|
|
/// StackFrameContext that matches a DeclContext, or find a VarRegion
|
|
|
|
/// for a variable captured by a block.
|
|
|
|
static llvm::PointerUnion<const StackFrameContext *, const VarRegion *>
|
|
|
|
getStackOrCaptureRegionForDeclContext(const LocationContext *LC,
|
|
|
|
const DeclContext *DC,
|
|
|
|
const VarDecl *VD) {
|
|
|
|
while (LC) {
|
2018-04-04 05:31:50 +08:00
|
|
|
if (const auto *SFC = dyn_cast<StackFrameContext>(LC)) {
|
2012-06-02 04:04:04 +08:00
|
|
|
if (cast<DeclContext>(SFC->getDecl()) == DC)
|
|
|
|
return SFC;
|
|
|
|
}
|
2018-04-04 05:31:50 +08:00
|
|
|
if (const auto *BC = dyn_cast<BlockInvocationContext>(LC)) {
|
2020-03-04 14:06:35 +08:00
|
|
|
const auto *BR = static_cast<const BlockDataRegion *>(BC->getData());
|
2012-06-02 04:04:04 +08:00
|
|
|
// FIXME: This can be made more efficient.
|
|
|
|
for (BlockDataRegion::referenced_vars_iterator
|
|
|
|
I = BR->referenced_vars_begin(),
|
|
|
|
E = BR->referenced_vars_end(); I != E; ++I) {
|
2020-05-11 21:00:42 +08:00
|
|
|
const TypedValueRegion *OrigR = I.getOriginalRegion();
|
|
|
|
if (const auto *VR = dyn_cast<VarRegion>(OrigR)) {
|
|
|
|
if (VR->getDecl() == VD)
|
|
|
|
return cast<VarRegion>(I.getCapturedRegion());
|
|
|
|
}
|
2012-06-02 04:04:04 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
LC = LC->getParent();
|
|
|
|
}
|
2014-05-27 10:45:47 +08:00
|
|
|
return (const StackFrameContext *)nullptr;
|
2012-06-02 04:04:04 +08:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
|
2009-12-04 08:26:31 +08:00
|
|
|
const LocationContext *LC) {
|
2020-05-11 21:00:42 +08:00
|
|
|
const auto *PVD = dyn_cast<ParmVarDecl>(D);
|
|
|
|
if (PVD) {
|
|
|
|
unsigned Index = PVD->getFunctionScopeIndex();
|
|
|
|
const StackFrameContext *SFC = LC->getStackFrame();
|
|
|
|
const Stmt *CallSite = SFC->getCallSite();
|
|
|
|
if (CallSite) {
|
|
|
|
const Decl *D = SFC->getDecl();
|
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
if (Index < FD->param_size() && FD->parameters()[Index] == PVD)
|
|
|
|
return getSubRegion<ParamVarRegion>(cast<Expr>(CallSite), Index,
|
|
|
|
getStackArgumentsRegion(SFC));
|
|
|
|
} else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
|
|
|
|
if (Index < BD->param_size() && BD->parameters()[Index] == PVD)
|
|
|
|
return getSubRegion<ParamVarRegion>(cast<Expr>(CallSite), Index,
|
|
|
|
getStackArgumentsRegion(SFC));
|
|
|
|
} else {
|
|
|
|
return getSubRegion<ParamVarRegion>(cast<Expr>(CallSite), Index,
|
|
|
|
getStackArgumentsRegion(SFC));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 08:30:20 +08:00
|
|
|
D = D->getCanonicalDecl();
|
2014-05-27 10:45:47 +08:00
|
|
|
const MemRegion *sReg = nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-05 07:54:01 +08:00
|
|
|
if (D->hasGlobalStorage() && !D->isStaticLocal()) {
|
[analyzer] Treat system globals as mutable if they are not const
Previously, system globals were treated as immutable regions, unless it
was the `errno` which is known to be frequently modified.
D124244 wants to add a check for stores to immutable regions.
It would basically turn all stores to system globals into an error even
though we have no reason to believe that those mutable sys globals
should be treated as if they were immutable. And this leads to
false-positives if we apply D124244.
In this patch, I'm proposing to treat mutable sys globals actually
mutable, hence allocate them into the `GlobalSystemSpaceRegion`, UNLESS
they were declared as `const` (and a primitive arithmetic type), in
which case, we should use `GlobalImmutableSpaceRegion`.
In any other cases, I'm using the `GlobalInternalSpaceRegion`, which is
no different than the previous behavior.
---
In the tests I added, only the last `expected-warning` was different, compared to the baseline.
Which is this:
```lang=C++
void test_my_mutable_system_global_constraint() {
assert(my_mutable_system_global > 2);
clang_analyzer_eval(my_mutable_system_global > 2); // expected-warning {{TRUE}}
invalidate_globals();
clang_analyzer_eval(my_mutable_system_global > 2); // expected-warning {{UNKNOWN}} It was previously TRUE.
}
void test_my_mutable_system_global_assign(int x) {
my_mutable_system_global = x;
clang_analyzer_eval(my_mutable_system_global == x); // expected-warning {{TRUE}}
invalidate_globals();
clang_analyzer_eval(my_mutable_system_global == x); // expected-warning {{UNKNOWN}} It was previously TRUE.
}
```
---
Unfortunately, the taint checker will be also affected.
The `stdin` global variable is a pointer, which is assumed to be a taint
source, and the rest of the taint propagation rules will propagate from
it.
However, since mutable variables are no longer treated immutable, they
also get invalidated, when an opaque function call happens, such as the
first `scanf(stdin, ...)`. This would effectively remove taint from the
pointer, consequently disable all the rest of the taint propagations
down the line from the `stdin` variable.
All that said, I decided to look through `DerivedSymbol`s as well, to
acquire the memregion in that case as well. This should preserve the
previously existing taint reports.
Reviewed By: martong
Differential Revision: https://reviews.llvm.org/D127306
2022-06-15 23:08:27 +08:00
|
|
|
QualType Ty = D->getType();
|
|
|
|
assert(!Ty.isNull());
|
2022-06-15 23:08:27 +08:00
|
|
|
if (Ty.isConstQualified()) {
|
[analyzer] Treat system globals as mutable if they are not const
Previously, system globals were treated as immutable regions, unless it
was the `errno` which is known to be frequently modified.
D124244 wants to add a check for stores to immutable regions.
It would basically turn all stores to system globals into an error even
though we have no reason to believe that those mutable sys globals
should be treated as if they were immutable. And this leads to
false-positives if we apply D124244.
In this patch, I'm proposing to treat mutable sys globals actually
mutable, hence allocate them into the `GlobalSystemSpaceRegion`, UNLESS
they were declared as `const` (and a primitive arithmetic type), in
which case, we should use `GlobalImmutableSpaceRegion`.
In any other cases, I'm using the `GlobalInternalSpaceRegion`, which is
no different than the previous behavior.
---
In the tests I added, only the last `expected-warning` was different, compared to the baseline.
Which is this:
```lang=C++
void test_my_mutable_system_global_constraint() {
assert(my_mutable_system_global > 2);
clang_analyzer_eval(my_mutable_system_global > 2); // expected-warning {{TRUE}}
invalidate_globals();
clang_analyzer_eval(my_mutable_system_global > 2); // expected-warning {{UNKNOWN}} It was previously TRUE.
}
void test_my_mutable_system_global_assign(int x) {
my_mutable_system_global = x;
clang_analyzer_eval(my_mutable_system_global == x); // expected-warning {{TRUE}}
invalidate_globals();
clang_analyzer_eval(my_mutable_system_global == x); // expected-warning {{UNKNOWN}} It was previously TRUE.
}
```
---
Unfortunately, the taint checker will be also affected.
The `stdin` global variable is a pointer, which is assumed to be a taint
source, and the rest of the taint propagation rules will propagate from
it.
However, since mutable variables are no longer treated immutable, they
also get invalidated, when an opaque function call happens, such as the
first `scanf(stdin, ...)`. This would effectively remove taint from the
pointer, consequently disable all the rest of the taint propagations
down the line from the `stdin` variable.
All that said, I decided to look through `DerivedSymbol`s as well, to
acquire the memregion in that case as well. This should preserve the
previously existing taint reports.
Reviewed By: martong
Differential Revision: https://reviews.llvm.org/D127306
2022-06-15 23:08:27 +08:00
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
|
|
|
|
} else if (Ctx.getSourceManager().isInSystemHeader(D->getLocation())) {
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
|
|
|
|
} else {
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind);
|
2012-01-05 07:54:01 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2017-01-24 10:10:59 +08:00
|
|
|
// Finally handle static locals.
|
2012-01-05 07:54:01 +08:00
|
|
|
} else {
|
2009-12-08 06:05:27 +08:00
|
|
|
// FIXME: Once we implement scope handling, we will need to properly lookup
|
|
|
|
// 'D' to the proper LocationContext.
|
2009-12-11 14:43:27 +08:00
|
|
|
const DeclContext *DC = D->getDeclContext();
|
2012-06-02 04:04:04 +08:00
|
|
|
llvm::PointerUnion<const StackFrameContext *, const VarRegion *> V =
|
|
|
|
getStackOrCaptureRegionForDeclContext(LC, DC, D);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
if (V.is<const VarRegion*>())
|
|
|
|
return V.get<const VarRegion*>();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
const auto *STC = V.get<const StackFrameContext *>();
|
2009-12-11 14:43:27 +08:00
|
|
|
|
2017-01-25 18:21:45 +08:00
|
|
|
if (!STC) {
|
|
|
|
// FIXME: Assign a more sensible memory space to static locals
|
|
|
|
// we see from within blocks that we analyze as top-level declarations.
|
2017-01-24 10:10:59 +08:00
|
|
|
sReg = getUnknownRegion();
|
2017-01-25 18:21:45 +08:00
|
|
|
} else {
|
2010-07-02 04:16:50 +08:00
|
|
|
if (D->hasLocalStorage()) {
|
2021-10-20 23:43:31 +08:00
|
|
|
sReg =
|
|
|
|
isa<ParmVarDecl, ImplicitParamDecl>(D)
|
|
|
|
? static_cast<const MemRegion *>(getStackArgumentsRegion(STC))
|
|
|
|
: static_cast<const MemRegion *>(getStackLocalsRegion(STC));
|
2010-07-02 04:16:50 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(D->isStaticLocal());
|
2012-09-18 03:13:56 +08:00
|
|
|
const Decl *STCD = STC->getDecl();
|
2021-10-20 23:43:31 +08:00
|
|
|
if (isa<FunctionDecl, ObjCMethodDecl>(STCD))
|
2012-01-05 07:54:01 +08:00
|
|
|
sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
|
2016-01-13 21:49:29 +08:00
|
|
|
getFunctionCodeRegion(cast<NamedDecl>(STCD)));
|
2018-04-04 05:31:50 +08:00
|
|
|
else if (const auto *BD = dyn_cast<BlockDecl>(STCD)) {
|
2017-01-24 10:10:59 +08:00
|
|
|
// FIXME: The fallback type here is totally bogus -- though it should
|
|
|
|
// never be queried, it will prevent uniquing with the real
|
|
|
|
// BlockCodeRegion. Ideally we'd fix the AST so that we always had a
|
|
|
|
// signature.
|
|
|
|
QualType T;
|
|
|
|
if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten())
|
|
|
|
T = TSI->getType();
|
|
|
|
if (T.isNull())
|
|
|
|
T = getContext().VoidTy;
|
2022-04-21 01:26:38 +08:00
|
|
|
if (!T->getAs<FunctionType>()) {
|
|
|
|
FunctionProtoType::ExtProtoInfo Ext;
|
|
|
|
T = getContext().getFunctionType(T, None, Ext);
|
|
|
|
}
|
2017-01-24 10:10:59 +08:00
|
|
|
T = getContext().getBlockPointerType(T);
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
const BlockCodeRegion *BTR =
|
2020-01-30 23:04:37 +08:00
|
|
|
getBlockCodeRegion(BD, Ctx.getCanonicalType(T),
|
2017-01-24 10:10:59 +08:00
|
|
|
STC->getAnalysisDeclContext());
|
2012-01-05 07:54:01 +08:00
|
|
|
sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
|
|
|
|
BTR);
|
2010-07-02 04:16:50 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sReg = getGlobalsRegion();
|
|
|
|
}
|
|
|
|
}
|
2009-12-11 14:43:27 +08:00
|
|
|
}
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
return getSubRegion<NonParamVarRegion>(D, sReg);
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2020-05-11 21:00:42 +08:00
|
|
|
const NonParamVarRegion *
|
|
|
|
MemRegionManager::getNonParamVarRegion(const VarDecl *D,
|
|
|
|
const MemRegion *superR) {
|
2019-02-07 08:30:20 +08:00
|
|
|
D = D->getCanonicalDecl();
|
2020-05-11 21:00:42 +08:00
|
|
|
return getSubRegion<NonParamVarRegion>(D, superR);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ParamVarRegion *
|
|
|
|
MemRegionManager::getParamVarRegion(const Expr *OriginExpr, unsigned Index,
|
|
|
|
const LocationContext *LC) {
|
|
|
|
const StackFrameContext *SFC = LC->getStackFrame();
|
|
|
|
assert(SFC);
|
|
|
|
return getSubRegion<ParamVarRegion>(OriginExpr, Index,
|
|
|
|
getStackArgumentsRegion(SFC));
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const BlockDataRegion *
|
2016-01-13 21:49:29 +08:00
|
|
|
MemRegionManager::getBlockDataRegion(const BlockCodeRegion *BC,
|
2013-11-20 08:11:42 +08:00
|
|
|
const LocationContext *LC,
|
|
|
|
unsigned blockCount) {
|
2017-04-13 17:56:07 +08:00
|
|
|
const MemSpaceRegion *sReg = nullptr;
|
2012-02-19 06:41:01 +08:00
|
|
|
const BlockDecl *BD = BC->getDecl();
|
|
|
|
if (!BD->hasCaptures()) {
|
|
|
|
// This handles 'static' blocks.
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
|
|
|
else {
|
2012-02-19 06:41:01 +08:00
|
|
|
if (LC) {
|
|
|
|
// FIXME: Once we implement scope handling, we want the parent region
|
|
|
|
// to be the scope.
|
2018-06-27 09:51:55 +08:00
|
|
|
const StackFrameContext *STC = LC->getStackFrame();
|
2012-02-19 06:41:01 +08:00
|
|
|
assert(STC);
|
|
|
|
sReg = getStackLocalsRegion(STC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We allow 'LC' to be NULL for cases where want BlockDataRegions
|
|
|
|
// without context-sensitivity.
|
|
|
|
sReg = getUnknownRegion();
|
|
|
|
}
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
|
|
|
|
2013-11-20 08:11:42 +08:00
|
|
|
return getSubRegion<BlockDataRegion>(BC, LC, blockCount, sReg);
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 19:50:42 +08:00
|
|
|
const CXXTempObjectRegion *
|
|
|
|
MemRegionManager::getCXXStaticTempObjectRegion(const Expr *Ex) {
|
|
|
|
return getSubRegion<CXXTempObjectRegion>(
|
2014-05-27 10:45:47 +08:00
|
|
|
Ex, getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind, nullptr));
|
2013-07-26 19:50:42 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const CompoundLiteralRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
|
2009-12-08 06:05:27 +08:00
|
|
|
const LocationContext *LC) {
|
2017-04-13 17:56:07 +08:00
|
|
|
const MemSpaceRegion *sReg = nullptr;
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
if (CL->isFileScope())
|
|
|
|
sReg = getGlobalsRegion();
|
|
|
|
else {
|
2018-06-27 09:51:55 +08:00
|
|
|
const StackFrameContext *STC = LC->getStackFrame();
|
2009-12-08 06:05:27 +08:00
|
|
|
assert(STC);
|
|
|
|
sReg = getStackLocalsRegion(STC);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return getSubRegion<CompoundLiteralRegion>(CL, sReg);
|
2008-10-28 04:57:58 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const ElementRegion*
|
2010-09-15 11:13:30 +08:00
|
|
|
MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
|
2017-04-13 17:56:07 +08:00
|
|
|
const SubRegion* superRegion,
|
2011-08-13 07:37:29 +08:00
|
|
|
ASTContext &Ctx){
|
2010-05-27 08:29:00 +08:00
|
|
|
QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
|
2008-12-14 03:24:37 +08:00
|
|
|
|
2008-10-21 13:27:10 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2009-06-16 17:55:50 +08:00
|
|
|
ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
|
2008-10-21 13:27:10 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2008-10-21 13:27:10 +08:00
|
|
|
MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
|
2018-04-04 05:31:50 +08:00
|
|
|
auto *R = cast_or_null<ElementRegion>(data);
|
2008-10-21 13:27:10 +08:00
|
|
|
|
|
|
|
if (!R) {
|
2016-02-03 23:20:51 +08:00
|
|
|
R = A.Allocate<ElementRegion>();
|
2009-06-16 17:55:50 +08:00
|
|
|
new (R) ElementRegion(T, Idx, superRegion);
|
2008-10-21 13:27:10 +08:00
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
const FunctionCodeRegion *
|
|
|
|
MemRegionManager::getFunctionCodeRegion(const NamedDecl *FD) {
|
2019-02-09 09:00:32 +08:00
|
|
|
// To think: should we canonicalize the declaration here?
|
2016-01-13 21:49:29 +08:00
|
|
|
return getSubRegion<FunctionCodeRegion>(FD, getCodeRegion());
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
const BlockCodeRegion *
|
|
|
|
MemRegionManager::getBlockCodeRegion(const BlockDecl *BD, CanQualType locTy,
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext *AC) {
|
2016-01-13 21:49:29 +08:00
|
|
|
return getSubRegion<BlockCodeRegion>(BD, locTy, AC, getCodeRegion());
|
2009-11-25 09:32:22 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 17:15:06 +08:00
|
|
|
const SymbolicRegion *
|
|
|
|
MemRegionManager::getSymbolicRegion(SymbolRef sym,
|
|
|
|
const MemSpaceRegion *MemSpace) {
|
|
|
|
if (MemSpace == nullptr)
|
|
|
|
MemSpace = getUnknownRegion();
|
|
|
|
return getSubRegion<SymbolicRegion>(sym, MemSpace);
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
2012-06-07 11:57:32 +08:00
|
|
|
const SymbolicRegion *MemRegionManager::getSymbolicHeapRegion(SymbolRef Sym) {
|
|
|
|
return getSubRegion<SymbolicRegion>(Sym, getHeapRegion());
|
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
const FieldRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getFieldRegion(const FieldDecl *d,
|
2017-04-13 17:56:07 +08:00
|
|
|
const SubRegion* superRegion){
|
2009-07-11 00:51:45 +08:00
|
|
|
return getSubRegion<FieldRegion>(d, superRegion);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const ObjCIvarRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d,
|
2017-04-13 17:56:07 +08:00
|
|
|
const SubRegion* superRegion) {
|
2009-07-11 00:51:45 +08:00
|
|
|
return getSubRegion<ObjCIvarRegion>(d, superRegion);
|
2008-10-25 04:30:08 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:52:48 +08:00
|
|
|
const CXXTempObjectRegion*
|
|
|
|
MemRegionManager::getCXXTempObjectRegion(Expr const *E,
|
|
|
|
LocationContext const *LC) {
|
2018-06-27 09:51:55 +08:00
|
|
|
const StackFrameContext *SFC = LC->getStackFrame();
|
2010-01-09 17:16:47 +08:00
|
|
|
assert(SFC);
|
2010-11-26 16:52:48 +08:00
|
|
|
return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
|
2009-12-16 19:27:52 +08:00
|
|
|
}
|
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
/// Checks whether \p BaseClass is a valid virtual or direct non-virtual base
|
|
|
|
/// class of the type of \p Super.
|
|
|
|
static bool isValidBaseClass(const CXXRecordDecl *BaseClass,
|
|
|
|
const TypedValueRegion *Super,
|
|
|
|
bool IsVirtual) {
|
2013-02-23 03:33:13 +08:00
|
|
|
BaseClass = BaseClass->getCanonicalDecl();
|
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
const CXXRecordDecl *Class = Super->getValueType()->getAsCXXRecordDecl();
|
|
|
|
if (!Class)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (IsVirtual)
|
|
|
|
return Class->isVirtuallyDerivedFrom(BaseClass);
|
|
|
|
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : Class->bases()) {
|
|
|
|
if (I.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == BaseClass)
|
2013-02-21 11:12:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-08-14 06:11:34 +08:00
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXBaseObjectRegion *
|
|
|
|
MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *RD,
|
2017-04-13 17:56:07 +08:00
|
|
|
const SubRegion *Super,
|
2013-02-21 11:12:32 +08:00
|
|
|
bool IsVirtual) {
|
2013-02-21 12:40:10 +08:00
|
|
|
if (isa<TypedValueRegion>(Super)) {
|
2019-08-29 02:44:38 +08:00
|
|
|
assert(isValidBaseClass(RD, cast<TypedValueRegion>(Super), IsVirtual));
|
2013-07-27 11:34:50 +08:00
|
|
|
(void)&isValidBaseClass;
|
2013-02-21 11:12:32 +08:00
|
|
|
|
|
|
|
if (IsVirtual) {
|
|
|
|
// Virtual base regions should not be layered, since the layout rules
|
|
|
|
// are different.
|
2018-04-04 05:31:50 +08:00
|
|
|
while (const auto *Base = dyn_cast<CXXBaseObjectRegion>(Super))
|
2017-04-13 17:56:07 +08:00
|
|
|
Super = cast<SubRegion>(Base->getSuperRegion());
|
2013-02-21 11:12:32 +08:00
|
|
|
assert(Super && !isa<MemSpaceRegion>(Super));
|
2012-08-14 06:11:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
return getSubRegion<CXXBaseObjectRegion>(RD, IsVirtual, Super);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2018-08-30 06:43:31 +08:00
|
|
|
const CXXDerivedObjectRegion *
|
|
|
|
MemRegionManager::getCXXDerivedObjectRegion(const CXXRecordDecl *RD,
|
|
|
|
const SubRegion *Super) {
|
|
|
|
return getSubRegion<CXXDerivedObjectRegion>(RD, Super);
|
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
const CXXThisRegion*
|
|
|
|
MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
|
|
|
|
const LocationContext *LC) {
|
2018-04-04 05:31:50 +08:00
|
|
|
const auto *PT = thisPointerTy->getAs<PointerType>();
|
2010-01-05 10:18:06 +08:00
|
|
|
assert(PT);
|
2015-09-12 00:55:01 +08:00
|
|
|
// Inside the body of the operator() of a lambda a this expr might refer to an
|
|
|
|
// object in one of the parent location contexts.
|
|
|
|
const auto *D = dyn_cast<CXXMethodDecl>(LC->getDecl());
|
|
|
|
// FIXME: when operator() of lambda is analyzed as a top level function and
|
|
|
|
// 'this' refers to a this to the enclosing scope, there is no right region to
|
|
|
|
// return.
|
2019-01-11 09:54:53 +08:00
|
|
|
while (!LC->inTopFrame() && (!D || D->isStatic() ||
|
|
|
|
PT != D->getThisType()->getAs<PointerType>())) {
|
2015-09-12 00:55:01 +08:00
|
|
|
LC = LC->getParent();
|
|
|
|
D = dyn_cast<CXXMethodDecl>(LC->getDecl());
|
|
|
|
}
|
2018-06-27 09:51:55 +08:00
|
|
|
const StackFrameContext *STC = LC->getStackFrame();
|
2015-09-12 00:55:01 +08:00
|
|
|
assert(STC);
|
2010-01-05 10:18:06 +08:00
|
|
|
return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const AllocaRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt,
|
2009-12-08 06:05:27 +08:00
|
|
|
const LocationContext *LC) {
|
2018-06-27 09:51:55 +08:00
|
|
|
const StackFrameContext *STC = LC->getStackFrame();
|
2009-12-08 06:05:27 +08:00
|
|
|
assert(STC);
|
|
|
|
return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
const MemSpaceRegion *MemRegion::getMemorySpace() const {
|
|
|
|
const MemRegion *R = this;
|
2018-04-04 05:31:50 +08:00
|
|
|
const auto *SR = dyn_cast<SubRegion>(this);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 04:28:54 +08:00
|
|
|
while (SR) {
|
2009-06-24 02:17:08 +08:00
|
|
|
R = SR->getSuperRegion();
|
|
|
|
SR = dyn_cast<SubRegion>(R);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
return dyn_cast<MemSpaceRegion>(R);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
2009-04-11 08:11:10 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
bool MemRegion::hasStackStorage() const {
|
2009-12-08 06:05:27 +08:00
|
|
|
return isa<StackSpaceRegion>(getMemorySpace());
|
2009-06-24 02:17:08 +08:00
|
|
|
}
|
2009-06-23 10:51:21 +08:00
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
bool MemRegion::hasStackNonParametersStorage() const {
|
|
|
|
return isa<StackLocalsSpaceRegion>(getMemorySpace());
|
2009-07-03 02:25:09 +08:00
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
bool MemRegion::hasStackParametersStorage() const {
|
2009-12-08 06:05:27 +08:00
|
|
|
return isa<StackArgumentsSpaceRegion>(getMemorySpace());
|
2009-07-03 06:02:15 +08:00
|
|
|
}
|
|
|
|
|
2009-07-03 02:25:09 +08:00
|
|
|
bool MemRegion::hasGlobalsOrParametersStorage() const {
|
2021-10-20 23:43:31 +08:00
|
|
|
return isa<StackArgumentsSpaceRegion, GlobalsSpaceRegion>(getMemorySpace());
|
2009-07-03 02:25:09 +08:00
|
|
|
}
|
2009-04-11 08:11:10 +08:00
|
|
|
|
2009-11-10 10:37:53 +08:00
|
|
|
// getBaseRegion strips away all elements and fields, and get the base region
|
|
|
|
// of them.
|
|
|
|
const MemRegion *MemRegion::getBaseRegion() const {
|
|
|
|
const MemRegion *R = this;
|
|
|
|
while (true) {
|
2010-04-07 06:06:03 +08:00
|
|
|
switch (R->getKind()) {
|
|
|
|
case MemRegion::ElementRegionKind:
|
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2011-01-13 20:46:31 +08:00
|
|
|
case MemRegion::CXXBaseObjectRegionKind:
|
2018-08-30 06:43:31 +08:00
|
|
|
case MemRegion::CXXDerivedObjectRegionKind:
|
2010-04-07 06:06:03 +08:00
|
|
|
R = cast<SubRegion>(R)->getSuperRegion();
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
2009-11-10 10:37:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2018-12-04 18:22:28 +08:00
|
|
|
// getgetMostDerivedObjectRegion gets the region of the root class of a C++
|
|
|
|
// class hierarchy.
|
|
|
|
const MemRegion *MemRegion::getMostDerivedObjectRegion() const {
|
|
|
|
const MemRegion *R = this;
|
|
|
|
while (const auto *BR = dyn_cast<CXXBaseObjectRegion>(R))
|
|
|
|
R = BR->getSuperRegion();
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2018-09-29 02:49:41 +08:00
|
|
|
bool MemRegion::isSubRegionOf(const MemRegion *) const {
|
2012-09-13 06:57:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// View handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-08-30 06:43:31 +08:00
|
|
|
const MemRegion *MemRegion::StripCasts(bool StripBaseAndDerivedCasts) const {
|
2009-07-30 02:14:27 +08:00
|
|
|
const MemRegion *R = this;
|
|
|
|
while (true) {
|
2012-07-12 08:16:25 +08:00
|
|
|
switch (R->getKind()) {
|
|
|
|
case ElementRegionKind: {
|
2018-04-04 05:31:50 +08:00
|
|
|
const auto *ER = cast<ElementRegion>(R);
|
2012-07-12 08:16:25 +08:00
|
|
|
if (!ER->getIndex().isZeroConstant())
|
|
|
|
return R;
|
|
|
|
R = ER->getSuperRegion();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CXXBaseObjectRegionKind:
|
2018-08-30 06:43:31 +08:00
|
|
|
case CXXDerivedObjectRegionKind:
|
|
|
|
if (!StripBaseAndDerivedCasts)
|
2012-08-14 06:11:34 +08:00
|
|
|
return R;
|
2018-08-30 06:43:31 +08:00
|
|
|
R = cast<TypedValueRegion>(R)->getSuperRegion();
|
2012-07-12 08:16:25 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return R;
|
2009-07-30 02:14:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-01 14:17:29 +08:00
|
|
|
|
2013-04-20 09:15:42 +08:00
|
|
|
const SymbolicRegion *MemRegion::getSymbolicBase() const {
|
2018-04-04 05:31:50 +08:00
|
|
|
const auto *SubR = dyn_cast<SubRegion>(this);
|
2013-04-20 09:15:42 +08:00
|
|
|
|
|
|
|
while (SubR) {
|
2018-04-04 05:31:50 +08:00
|
|
|
if (const auto *SymR = dyn_cast<SymbolicRegion>(SubR))
|
2013-04-20 09:15:42 +08:00
|
|
|
return SymR;
|
|
|
|
SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
|
|
|
|
}
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-04-20 09:15:42 +08:00
|
|
|
}
|
|
|
|
|
2010-08-02 12:56:14 +08:00
|
|
|
RegionRawOffset ElementRegion::getAsArrayOffset() const {
|
2018-06-14 02:32:19 +08:00
|
|
|
int64_t offset = 0;
|
2009-08-01 14:17:29 +08:00
|
|
|
const ElementRegion *ER = this;
|
2014-05-27 10:45:47 +08:00
|
|
|
const MemRegion *superR = nullptr;
|
2009-08-01 14:17:29 +08:00
|
|
|
ASTContext &C = getContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// FIXME: Handle multi-dimensional arrays.
|
|
|
|
|
|
|
|
while (ER) {
|
|
|
|
superR = ER->getSuperRegion();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// FIXME: generalize to symbolic offsets.
|
|
|
|
SVal index = ER->getIndex();
|
2018-06-14 02:32:19 +08:00
|
|
|
if (auto CI = index.getAs<nonloc::ConcreteInt>()) {
|
2009-08-01 14:17:29 +08:00
|
|
|
// Update the offset.
|
|
|
|
int64_t i = CI->getValue().getSExtValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
if (i != 0) {
|
|
|
|
QualType elemType = ER->getElementType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// If we are pointing to an incomplete type, go no further.
|
2014-10-04 05:49:03 +08:00
|
|
|
if (elemType->isIncompleteType()) {
|
2009-08-01 14:17:29 +08:00
|
|
|
superR = ER;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2018-06-14 02:32:19 +08:00
|
|
|
int64_t size = C.getTypeSizeInChars(elemType).getQuantity();
|
|
|
|
if (auto NewOffset = llvm::checkedMulAdd(i, size, offset)) {
|
|
|
|
offset = *NewOffset;
|
|
|
|
} else {
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "MemRegion::getAsArrayOffset: "
|
|
|
|
<< "offset overflowing, returning unknown\n");
|
2018-02-27 08:05:04 +08:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go to the next ElementRegion (if any).
|
|
|
|
ER = dyn_cast<ElementRegion>(superR);
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
assert(superR && "super region cannot be NULL");
|
2018-06-14 02:32:19 +08:00
|
|
|
return RegionRawOffset(superR, CharUnits::fromQuantity(offset));
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
|
|
|
|
2013-02-26 02:36:15 +08:00
|
|
|
/// Returns true if \p Base is an immediate base class of \p Child
|
|
|
|
static bool isImmediateBase(const CXXRecordDecl *Child,
|
|
|
|
const CXXRecordDecl *Base) {
|
2015-12-05 08:22:36 +08:00
|
|
|
assert(Child && "Child must not be null");
|
2013-02-26 02:36:15 +08:00
|
|
|
// Note that we do NOT canonicalize the base class here, because
|
|
|
|
// ASTRecordLayout doesn't either. If that leads us down the wrong path,
|
|
|
|
// so be it; at least we won't crash.
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : Child->bases()) {
|
|
|
|
if (I.getType()->getAsCXXRecordDecl() == Base)
|
2013-02-26 02:36:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-31 09:20:07 +08:00
|
|
|
static RegionOffset calculateOffset(const MemRegion *R) {
|
2014-05-27 10:45:47 +08:00
|
|
|
const MemRegion *SymbolicOffsetBase = nullptr;
|
2010-08-03 14:34:25 +08:00
|
|
|
int64_t Offset = 0;
|
2010-08-03 12:52:05 +08:00
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
while (true) {
|
2010-08-03 12:52:05 +08:00
|
|
|
switch (R->getKind()) {
|
2018-03-31 09:20:07 +08:00
|
|
|
case MemRegion::CodeSpaceRegionKind:
|
|
|
|
case MemRegion::StackLocalsSpaceRegionKind:
|
|
|
|
case MemRegion::StackArgumentsSpaceRegionKind:
|
|
|
|
case MemRegion::HeapSpaceRegionKind:
|
|
|
|
case MemRegion::UnknownSpaceRegionKind:
|
|
|
|
case MemRegion::StaticGlobalSpaceRegionKind:
|
|
|
|
case MemRegion::GlobalInternalSpaceRegionKind:
|
|
|
|
case MemRegion::GlobalSystemSpaceRegionKind:
|
|
|
|
case MemRegion::GlobalImmutableSpaceRegionKind:
|
2013-02-15 08:32:03 +08:00
|
|
|
// Stores can bind directly to a region space to set a default value.
|
|
|
|
assert(Offset == 0 && !SymbolicOffsetBase);
|
|
|
|
goto Finish;
|
|
|
|
|
2018-03-31 09:20:07 +08:00
|
|
|
case MemRegion::FunctionCodeRegionKind:
|
|
|
|
case MemRegion::BlockCodeRegionKind:
|
|
|
|
case MemRegion::BlockDataRegionKind:
|
2013-02-15 08:32:03 +08:00
|
|
|
// These will never have bindings, but may end up having values requested
|
|
|
|
// if the user does some strange casting.
|
|
|
|
if (Offset != 0)
|
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
goto Finish;
|
2012-08-10 06:55:37 +08:00
|
|
|
|
2018-03-31 09:20:07 +08:00
|
|
|
case MemRegion::SymbolicRegionKind:
|
|
|
|
case MemRegion::AllocaRegionKind:
|
|
|
|
case MemRegion::CompoundLiteralRegionKind:
|
|
|
|
case MemRegion::CXXThisRegionKind:
|
|
|
|
case MemRegion::StringRegionKind:
|
|
|
|
case MemRegion::ObjCStringRegionKind:
|
2020-05-11 21:00:42 +08:00
|
|
|
case MemRegion::NonParamVarRegionKind:
|
|
|
|
case MemRegion::ParamVarRegionKind:
|
2018-03-31 09:20:07 +08:00
|
|
|
case MemRegion::CXXTempObjectRegionKind:
|
2013-02-15 08:32:03 +08:00
|
|
|
// Usual base regions.
|
2010-08-03 12:52:05 +08:00
|
|
|
goto Finish;
|
2012-08-10 06:55:37 +08:00
|
|
|
|
2018-03-31 09:20:07 +08:00
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2012-08-10 06:55:51 +08:00
|
|
|
// This is a little strange, but it's a compromise between
|
|
|
|
// ObjCIvarRegions having unknown compile-time offsets (when using the
|
|
|
|
// non-fragile runtime) and yet still being distinct, non-overlapping
|
|
|
|
// regions. Thus we treat them as "like" base regions for the purposes
|
|
|
|
// of computing offsets.
|
|
|
|
goto Finish;
|
|
|
|
|
2018-03-31 09:20:07 +08:00
|
|
|
case MemRegion::CXXBaseObjectRegionKind: {
|
2018-04-04 05:31:50 +08:00
|
|
|
const auto *BOR = cast<CXXBaseObjectRegion>(R);
|
2012-08-09 02:23:27 +08:00
|
|
|
R = BOR->getSuperRegion();
|
|
|
|
|
|
|
|
QualType Ty;
|
2013-02-26 02:36:15 +08:00
|
|
|
bool RootIsSymbolic = false;
|
2018-04-04 05:31:50 +08:00
|
|
|
if (const auto *TVR = dyn_cast<TypedValueRegion>(R)) {
|
2018-03-31 09:20:07 +08:00
|
|
|
Ty = TVR->getDesugaredValueType(R->getContext());
|
2018-04-04 05:31:50 +08:00
|
|
|
} else if (const auto *SR = dyn_cast<SymbolicRegion>(R)) {
|
2012-08-09 02:23:27 +08:00
|
|
|
// If our base region is symbolic, we don't know what type it really is.
|
|
|
|
// Pretend the type of the symbol is the true dynamic type.
|
|
|
|
// (This will at least be self-consistent for the life of the symbol.)
|
2012-09-26 14:00:14 +08:00
|
|
|
Ty = SR->getSymbol()->getType()->getPointeeType();
|
2013-02-26 02:36:15 +08:00
|
|
|
RootIsSymbolic = true;
|
2012-08-09 02:23:27 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
const CXXRecordDecl *Child = Ty->getAsCXXRecordDecl();
|
2012-08-16 01:33:34 +08:00
|
|
|
if (!Child) {
|
2012-08-09 02:23:27 +08:00
|
|
|
// We cannot compute the offset of the base class.
|
2012-08-10 06:55:37 +08:00
|
|
|
SymbolicOffsetBase = R;
|
2015-12-05 08:22:36 +08:00
|
|
|
} else {
|
|
|
|
if (RootIsSymbolic) {
|
|
|
|
// Base layers on symbolic regions may not be type-correct.
|
|
|
|
// Double-check the inheritance here, and revert to a symbolic offset
|
|
|
|
// if it's invalid (e.g. due to a reinterpret_cast).
|
|
|
|
if (BOR->isVirtual()) {
|
|
|
|
if (!Child->isVirtuallyDerivedFrom(BOR->getDecl()))
|
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
} else {
|
|
|
|
if (!isImmediateBase(Child, BOR->getDecl()))
|
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
}
|
2013-02-26 02:36:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-10 06:55:37 +08:00
|
|
|
// Don't bother calculating precise offsets if we already have a
|
|
|
|
// symbolic offset somewhere in the chain.
|
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
continue;
|
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
CharUnits BaseOffset;
|
2018-03-31 09:20:07 +08:00
|
|
|
const ASTRecordLayout &Layout = R->getContext().getASTRecordLayout(Child);
|
2013-02-21 11:12:32 +08:00
|
|
|
if (BOR->isVirtual())
|
|
|
|
BaseOffset = Layout.getVBaseClassOffset(BOR->getDecl());
|
2012-08-09 02:23:27 +08:00
|
|
|
else
|
2013-02-21 11:12:32 +08:00
|
|
|
BaseOffset = Layout.getBaseClassOffset(BOR->getDecl());
|
2012-08-09 02:23:27 +08:00
|
|
|
|
|
|
|
// The base offset is in chars, not in bits.
|
2018-03-31 09:20:07 +08:00
|
|
|
Offset += BaseOffset.getQuantity() * R->getContext().getCharWidth();
|
2012-08-09 02:23:27 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-08-30 06:43:31 +08:00
|
|
|
|
|
|
|
case MemRegion::CXXDerivedObjectRegionKind: {
|
|
|
|
// TODO: Store the base type in the CXXDerivedObjectRegion and use it.
|
|
|
|
goto Finish;
|
|
|
|
}
|
|
|
|
|
2018-03-31 09:20:07 +08:00
|
|
|
case MemRegion::ElementRegionKind: {
|
2018-04-04 05:31:50 +08:00
|
|
|
const auto *ER = cast<ElementRegion>(R);
|
2012-08-10 06:55:37 +08:00
|
|
|
R = ER->getSuperRegion();
|
2010-08-03 12:52:05 +08:00
|
|
|
|
2012-08-10 06:55:37 +08:00
|
|
|
QualType EleTy = ER->getValueType();
|
2014-10-04 05:49:03 +08:00
|
|
|
if (EleTy->isIncompleteType()) {
|
2012-08-10 06:55:37 +08:00
|
|
|
// We cannot compute the offset of the base class.
|
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
continue;
|
|
|
|
}
|
2010-08-03 12:52:05 +08:00
|
|
|
|
|
|
|
SVal Index = ER->getIndex();
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> CI =
|
2013-02-20 13:52:05 +08:00
|
|
|
Index.getAs<nonloc::ConcreteInt>()) {
|
2012-08-10 06:55:37 +08:00
|
|
|
// Don't bother calculating precise offsets if we already have a
|
2015-09-08 11:50:52 +08:00
|
|
|
// symbolic offset somewhere in the chain.
|
2012-08-10 06:55:37 +08:00
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
continue;
|
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
int64_t i = CI->getValue().getSExtValue();
|
2012-08-09 02:23:27 +08:00
|
|
|
// This type size is in bits.
|
2018-03-31 09:20:07 +08:00
|
|
|
Offset += i * R->getContext().getTypeSize(EleTy);
|
2010-08-03 12:52:05 +08:00
|
|
|
} else {
|
|
|
|
// We cannot compute offset for non-concrete index.
|
2012-08-10 06:55:37 +08:00
|
|
|
SymbolicOffsetBase = R;
|
2010-08-03 12:52:05 +08:00
|
|
|
}
|
2010-08-02 12:56:14 +08:00
|
|
|
break;
|
2010-08-03 12:52:05 +08:00
|
|
|
}
|
2018-03-31 09:20:07 +08:00
|
|
|
case MemRegion::FieldRegionKind: {
|
2018-04-04 05:31:50 +08:00
|
|
|
const auto *FR = cast<FieldRegion>(R);
|
2012-08-10 06:55:37 +08:00
|
|
|
R = FR->getSuperRegion();
|
2019-08-29 02:44:38 +08:00
|
|
|
assert(R);
|
2012-08-10 06:55:37 +08:00
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
const RecordDecl *RD = FR->getDecl()->getParent();
|
2012-10-11 07:23:21 +08:00
|
|
|
if (RD->isUnion() || !RD->isCompleteDefinition()) {
|
2010-08-03 12:52:05 +08:00
|
|
|
// We cannot compute offset for incomplete type.
|
2012-10-11 07:23:21 +08:00
|
|
|
// For unions, we could treat everything as offset 0, but we'd rather
|
|
|
|
// treat each field as a symbolic offset so they aren't stored on top
|
|
|
|
// of each other, since we depend on things in typed regions actually
|
|
|
|
// matching their types.
|
2012-08-10 06:55:37 +08:00
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't bother calculating precise offsets if we already have a
|
|
|
|
// symbolic offset somewhere in the chain.
|
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
continue;
|
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
// Get the field number.
|
|
|
|
unsigned idx = 0;
|
2015-09-08 11:50:52 +08:00
|
|
|
for (RecordDecl::field_iterator FI = RD->field_begin(),
|
2016-02-03 23:20:51 +08:00
|
|
|
FE = RD->field_end(); FI != FE; ++FI, ++idx) {
|
2012-06-07 04:45:41 +08:00
|
|
|
if (FR->getDecl() == *FI)
|
2010-08-03 12:52:05 +08:00
|
|
|
break;
|
2016-02-03 23:20:51 +08:00
|
|
|
}
|
2018-03-31 09:20:07 +08:00
|
|
|
const ASTRecordLayout &Layout = R->getContext().getASTRecordLayout(RD);
|
2010-08-03 12:52:05 +08:00
|
|
|
// This is offset in bits.
|
|
|
|
Offset += Layout.getFieldOffset(idx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-02 12:56:14 +08:00
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
Finish:
|
2012-08-10 06:55:37 +08:00
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
return RegionOffset(SymbolicOffsetBase, RegionOffset::Symbolic);
|
2010-08-03 12:52:05 +08:00
|
|
|
return RegionOffset(R, Offset);
|
2010-08-02 12:56:14 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 09:20:07 +08:00
|
|
|
RegionOffset MemRegion::getAsOffset() const {
|
|
|
|
if (!cachedOffset)
|
|
|
|
cachedOffset = calculateOffset(this);
|
|
|
|
return *cachedOffset;
|
|
|
|
}
|
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BlockDataRegion
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-06 15:17:13 +08:00
|
|
|
std::pair<const VarRegion *, const VarRegion *>
|
|
|
|
BlockDataRegion::getCaptureRegions(const VarDecl *VD) {
|
2020-01-30 23:04:37 +08:00
|
|
|
MemRegionManager &MemMgr = getMemRegionManager();
|
2014-05-27 10:45:47 +08:00
|
|
|
const VarRegion *VR = nullptr;
|
|
|
|
const VarRegion *OriginalVR = nullptr;
|
2012-12-06 15:17:13 +08:00
|
|
|
|
2013-12-19 10:39:40 +08:00
|
|
|
if (!VD->hasAttr<BlocksAttr>() && VD->hasLocalStorage()) {
|
2020-05-11 21:00:42 +08:00
|
|
|
VR = MemMgr.getNonParamVarRegion(VD, this);
|
2012-12-06 15:17:13 +08:00
|
|
|
OriginalVR = MemMgr.getVarRegion(VD, LC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (LC) {
|
|
|
|
VR = MemMgr.getVarRegion(VD, LC);
|
|
|
|
OriginalVR = VR;
|
|
|
|
}
|
|
|
|
else {
|
2020-05-11 21:00:42 +08:00
|
|
|
VR = MemMgr.getNonParamVarRegion(VD, MemMgr.getUnknownRegion());
|
2012-12-06 15:17:13 +08:00
|
|
|
OriginalVR = MemMgr.getVarRegion(VD, LC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_pair(VR, OriginalVR);
|
|
|
|
}
|
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
void BlockDataRegion::LazyInitializeReferencedVars() {
|
|
|
|
if (ReferencedVars)
|
|
|
|
return;
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext();
|
2015-02-07 01:25:10 +08:00
|
|
|
const auto &ReferencedBlockVars = AC->getReferencedBlockVars(BC->getDecl());
|
|
|
|
auto NumBlockVars =
|
|
|
|
std::distance(ReferencedBlockVars.begin(), ReferencedBlockVars.end());
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2015-02-07 01:25:10 +08:00
|
|
|
if (NumBlockVars == 0) {
|
2009-11-26 10:34:36 +08:00
|
|
|
ReferencedVars = (void*) 0x1;
|
|
|
|
return;
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2020-01-30 23:04:37 +08:00
|
|
|
MemRegionManager &MemMgr = getMemRegionManager();
|
2009-11-26 10:34:36 +08:00
|
|
|
llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
|
|
|
|
BumpVectorContext BC(A);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
using VarVec = BumpVector<const MemRegion *>;
|
|
|
|
|
|
|
|
auto *BV = A.Allocate<VarVec>();
|
2015-02-07 01:25:10 +08:00
|
|
|
new (BV) VarVec(BC, NumBlockVars);
|
2018-04-04 05:31:50 +08:00
|
|
|
auto *BVOriginal = A.Allocate<VarVec>();
|
2015-02-07 01:25:10 +08:00
|
|
|
new (BVOriginal) VarVec(BC, NumBlockVars);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
for (const auto *VD : ReferencedBlockVars) {
|
2014-05-27 10:45:47 +08:00
|
|
|
const VarRegion *VR = nullptr;
|
|
|
|
const VarRegion *OriginalVR = nullptr;
|
2015-02-07 01:25:10 +08:00
|
|
|
std::tie(VR, OriginalVR) = getCaptureRegions(VD);
|
2009-12-08 06:05:27 +08:00
|
|
|
assert(VR);
|
2012-05-05 05:48:42 +08:00
|
|
|
assert(OriginalVR);
|
2009-12-08 06:05:27 +08:00
|
|
|
BV->push_back(VR, BC);
|
2012-05-05 05:48:42 +08:00
|
|
|
BVOriginal->push_back(OriginalVR, BC);
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
ReferencedVars = BV;
|
2012-05-05 05:48:42 +08:00
|
|
|
OriginalVars = BVOriginal;
|
2009-11-26 10:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockDataRegion::referenced_vars_iterator
|
|
|
|
BlockDataRegion::referenced_vars_begin() const {
|
|
|
|
const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
|
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
auto *Vec = static_cast<BumpVector<const MemRegion *> *>(ReferencedVars);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-05-05 05:48:42 +08:00
|
|
|
if (Vec == (void*) 0x1)
|
2014-05-27 10:45:47 +08:00
|
|
|
return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
|
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
auto *VecOriginal =
|
|
|
|
static_cast<BumpVector<const MemRegion *> *>(OriginalVars);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-05-05 05:48:42 +08:00
|
|
|
return BlockDataRegion::referenced_vars_iterator(Vec->begin(),
|
|
|
|
VecOriginal->begin());
|
2009-11-26 10:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockDataRegion::referenced_vars_iterator
|
|
|
|
BlockDataRegion::referenced_vars_end() const {
|
|
|
|
const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
|
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
auto *Vec = static_cast<BumpVector<const MemRegion *> *>(ReferencedVars);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-05-05 05:48:42 +08:00
|
|
|
if (Vec == (void*) 0x1)
|
2014-05-27 10:45:47 +08:00
|
|
|
return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
|
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
auto *VecOriginal =
|
|
|
|
static_cast<BumpVector<const MemRegion *> *>(OriginalVars);
|
2012-05-05 05:48:42 +08:00
|
|
|
|
|
|
|
return BlockDataRegion::referenced_vars_iterator(Vec->end(),
|
|
|
|
VecOriginal->end());
|
2009-11-26 10:34:36 +08:00
|
|
|
}
|
2013-02-06 06:00:19 +08:00
|
|
|
|
|
|
|
const VarRegion *BlockDataRegion::getOriginalRegion(const VarRegion *R) const {
|
|
|
|
for (referenced_vars_iterator I = referenced_vars_begin(),
|
|
|
|
E = referenced_vars_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I.getCapturedRegion() == R)
|
|
|
|
return I.getOriginalRegion();
|
|
|
|
}
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-02-06 06:00:19 +08:00
|
|
|
}
|
2013-09-25 07:47:29 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegionAndSymbolInvalidationTraits
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
void RegionAndSymbolInvalidationTraits::setTrait(SymbolRef Sym,
|
2013-09-25 07:47:29 +08:00
|
|
|
InvalidationKinds IK) {
|
|
|
|
SymTraitsMap[Sym] |= IK;
|
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
void RegionAndSymbolInvalidationTraits::setTrait(const MemRegion *MR,
|
2013-09-25 07:47:29 +08:00
|
|
|
InvalidationKinds IK) {
|
|
|
|
assert(MR);
|
2018-04-04 05:31:50 +08:00
|
|
|
if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
|
2013-09-25 07:47:29 +08:00
|
|
|
setTrait(SR->getSymbol(), IK);
|
|
|
|
else
|
|
|
|
MRTraitsMap[MR] |= IK;
|
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
bool RegionAndSymbolInvalidationTraits::hasTrait(SymbolRef Sym,
|
2016-02-03 23:20:51 +08:00
|
|
|
InvalidationKinds IK) const {
|
2013-09-25 07:47:29 +08:00
|
|
|
const_symbol_iterator I = SymTraitsMap.find(Sym);
|
|
|
|
if (I != SymTraitsMap.end())
|
|
|
|
return I->second & IK;
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
return false;
|
2013-09-25 07:47:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RegionAndSymbolInvalidationTraits::hasTrait(const MemRegion *MR,
|
2016-02-03 23:20:51 +08:00
|
|
|
InvalidationKinds IK) const {
|
2013-09-25 07:47:29 +08:00
|
|
|
if (!MR)
|
|
|
|
return false;
|
|
|
|
|
2018-04-04 05:31:50 +08:00
|
|
|
if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
|
2013-09-25 07:47:29 +08:00
|
|
|
return hasTrait(SR->getSymbol(), IK);
|
|
|
|
|
|
|
|
const_region_iterator I = MRTraitsMap.find(MR);
|
|
|
|
if (I != MRTraitsMap.end())
|
|
|
|
return I->second & IK;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|