2021-12-29 11:31:02 +00:00
|
|
|
//===-- DataflowEnvironment.cpp ---------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines an Environment class that is used by dataflow analyses
|
|
|
|
// that run over Control-Flow Graphs (CFGs) to keep track of the state of the
|
|
|
|
// program at given program points.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2022-01-11 12:15:53 +00:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2021-12-29 11:31:02 +00:00
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Analysis/FlowSensitive/DataflowLattice.h"
|
|
|
|
#include "clang/Analysis/FlowSensitive/StorageLocation.h"
|
|
|
|
#include "clang/Analysis/FlowSensitive/Value.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2022-01-04 13:47:14 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2021-12-29 11:31:02 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace dataflow {
|
|
|
|
|
|
|
|
/// Returns a map consisting of key-value entries that are present in both maps.
|
|
|
|
template <typename K, typename V>
|
|
|
|
llvm::DenseMap<K, V> intersectDenseMaps(const llvm::DenseMap<K, V> &Map1,
|
|
|
|
const llvm::DenseMap<K, V> &Map2) {
|
|
|
|
llvm::DenseMap<K, V> Result;
|
|
|
|
for (auto &Entry : Map1) {
|
|
|
|
auto It = Map2.find(Entry.first);
|
|
|
|
if (It != Map2.end() && Entry.second == It->second)
|
|
|
|
Result.insert({Entry.first, Entry.second});
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2022-01-11 12:15:53 +00:00
|
|
|
Environment::Environment(DataflowAnalysisContext &DACtx,
|
|
|
|
const DeclContext &DeclCtx)
|
|
|
|
: Environment(DACtx) {
|
|
|
|
if (const auto *FuncDecl = dyn_cast<FunctionDecl>(&DeclCtx)) {
|
|
|
|
for (const auto *ParamDecl : FuncDecl->parameters()) {
|
|
|
|
assert(ParamDecl != nullptr);
|
|
|
|
auto &ParamLoc = createStorageLocation(*ParamDecl);
|
|
|
|
setStorageLocation(*ParamDecl, ParamLoc);
|
2022-01-17 15:17:05 +00:00
|
|
|
if (Value *ParamVal = createValue(ParamDecl->getType()))
|
|
|
|
setValue(ParamLoc, *ParamVal);
|
2022-01-11 12:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(&DeclCtx)) {
|
|
|
|
if (!MethodDecl->isStatic()) {
|
|
|
|
QualType ThisPointeeType = MethodDecl->getThisObjectType();
|
|
|
|
// FIXME: Add support for union types.
|
|
|
|
if (!ThisPointeeType->isUnionType()) {
|
|
|
|
auto &ThisPointeeLoc = createStorageLocation(ThisPointeeType);
|
|
|
|
DACtx.setThisPointeeStorageLocation(ThisPointeeLoc);
|
2022-01-17 15:17:05 +00:00
|
|
|
if (Value *ThisPointeeVal = createValue(ThisPointeeType))
|
|
|
|
setValue(ThisPointeeLoc, *ThisPointeeVal);
|
2022-01-11 12:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 11:31:02 +00:00
|
|
|
bool Environment::operator==(const Environment &Other) const {
|
|
|
|
assert(DACtx == Other.DACtx);
|
|
|
|
return DeclToLoc == Other.DeclToLoc && LocToVal == Other.LocToVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
LatticeJoinEffect Environment::join(const Environment &Other) {
|
|
|
|
assert(DACtx == Other.DACtx);
|
|
|
|
|
|
|
|
auto Effect = LatticeJoinEffect::Unchanged;
|
|
|
|
|
|
|
|
const unsigned DeclToLocSizeBefore = DeclToLoc.size();
|
|
|
|
DeclToLoc = intersectDenseMaps(DeclToLoc, Other.DeclToLoc);
|
|
|
|
if (DeclToLocSizeBefore != DeclToLoc.size())
|
|
|
|
Effect = LatticeJoinEffect::Changed;
|
|
|
|
|
2022-01-20 09:28:25 +00:00
|
|
|
const unsigned ExprToLocSizeBefore = ExprToLoc.size();
|
|
|
|
ExprToLoc = intersectDenseMaps(ExprToLoc, Other.ExprToLoc);
|
|
|
|
if (ExprToLocSizeBefore != ExprToLoc.size())
|
|
|
|
Effect = LatticeJoinEffect::Changed;
|
|
|
|
|
2021-12-29 11:31:02 +00:00
|
|
|
// FIXME: Add support for joining distinct values that are assigned to the
|
|
|
|
// same storage locations in `LocToVal` and `Other.LocToVal`.
|
|
|
|
const unsigned LocToValSizeBefore = LocToVal.size();
|
|
|
|
LocToVal = intersectDenseMaps(LocToVal, Other.LocToVal);
|
|
|
|
if (LocToValSizeBefore != LocToVal.size())
|
|
|
|
Effect = LatticeJoinEffect::Changed;
|
|
|
|
|
|
|
|
return Effect;
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageLocation &Environment::createStorageLocation(QualType Type) {
|
|
|
|
assert(!Type.isNull());
|
2022-01-26 09:15:07 +00:00
|
|
|
if (Type->isStructureOrClassType() || Type->isUnionType()) {
|
2021-12-29 11:31:02 +00:00
|
|
|
// FIXME: Explore options to avoid eager initialization of fields as some of
|
|
|
|
// them might not be needed for a particular analysis.
|
|
|
|
llvm::DenseMap<const ValueDecl *, StorageLocation *> FieldLocs;
|
|
|
|
for (const FieldDecl *Field : Type->getAsRecordDecl()->fields()) {
|
|
|
|
FieldLocs.insert({Field, &createStorageLocation(Field->getType())});
|
|
|
|
}
|
2022-01-04 13:47:14 +00:00
|
|
|
return takeOwnership(
|
2021-12-29 11:31:02 +00:00
|
|
|
std::make_unique<AggregateStorageLocation>(Type, std::move(FieldLocs)));
|
|
|
|
}
|
2022-01-04 13:47:14 +00:00
|
|
|
return takeOwnership(std::make_unique<ScalarStorageLocation>(Type));
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StorageLocation &Environment::createStorageLocation(const VarDecl &D) {
|
|
|
|
// Evaluated declarations are always assigned the same storage locations to
|
|
|
|
// ensure that the environment stabilizes across loop iterations. Storage
|
|
|
|
// locations for evaluated declarations are stored in the analysis context.
|
|
|
|
if (auto *Loc = DACtx->getStorageLocation(D))
|
|
|
|
return *Loc;
|
|
|
|
auto &Loc = createStorageLocation(D.getType());
|
|
|
|
DACtx->setStorageLocation(D, Loc);
|
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
2022-01-04 13:47:14 +00:00
|
|
|
StorageLocation &Environment::createStorageLocation(const Expr &E) {
|
|
|
|
// Evaluated expressions are always assigned the same storage locations to
|
|
|
|
// ensure that the environment stabilizes across loop iterations. Storage
|
|
|
|
// locations for evaluated expressions are stored in the analysis context.
|
|
|
|
if (auto *Loc = DACtx->getStorageLocation(E))
|
|
|
|
return *Loc;
|
|
|
|
auto &Loc = createStorageLocation(E.getType());
|
|
|
|
DACtx->setStorageLocation(E, Loc);
|
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
2021-12-29 11:31:02 +00:00
|
|
|
void Environment::setStorageLocation(const ValueDecl &D, StorageLocation &Loc) {
|
|
|
|
assert(DeclToLoc.find(&D) == DeclToLoc.end());
|
|
|
|
DeclToLoc[&D] = &Loc;
|
|
|
|
}
|
|
|
|
|
2022-01-04 13:47:14 +00:00
|
|
|
StorageLocation *Environment::getStorageLocation(const ValueDecl &D,
|
|
|
|
SkipPast SP) const {
|
2021-12-29 11:31:02 +00:00
|
|
|
auto It = DeclToLoc.find(&D);
|
2022-01-04 13:47:14 +00:00
|
|
|
return It == DeclToLoc.end() ? nullptr : &skip(*It->second, SP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Environment::setStorageLocation(const Expr &E, StorageLocation &Loc) {
|
|
|
|
assert(ExprToLoc.find(&E) == ExprToLoc.end());
|
|
|
|
ExprToLoc[&E] = &Loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageLocation *Environment::getStorageLocation(const Expr &E,
|
|
|
|
SkipPast SP) const {
|
|
|
|
auto It = ExprToLoc.find(&E);
|
|
|
|
return It == ExprToLoc.end() ? nullptr : &skip(*It->second, SP);
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 12:15:53 +00:00
|
|
|
StorageLocation *Environment::getThisPointeeStorageLocation() const {
|
|
|
|
return DACtx->getThisPointeeStorageLocation();
|
|
|
|
}
|
|
|
|
|
2022-01-13 13:53:52 +00:00
|
|
|
void Environment::setValue(const StorageLocation &Loc, Value &Val) {
|
|
|
|
LocToVal[&Loc] = &Val;
|
|
|
|
|
|
|
|
if (auto *StructVal = dyn_cast<StructValue>(&Val)) {
|
|
|
|
auto &AggregateLoc = *cast<AggregateStorageLocation>(&Loc);
|
|
|
|
|
|
|
|
const QualType Type = AggregateLoc.getType();
|
|
|
|
assert(Type->isStructureOrClassType());
|
|
|
|
|
|
|
|
for (const FieldDecl *Field : Type->getAsRecordDecl()->fields()) {
|
|
|
|
assert(Field != nullptr);
|
|
|
|
setValue(AggregateLoc.getChild(*Field), StructVal->getChild(*Field));
|
|
|
|
}
|
|
|
|
}
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *Environment::getValue(const StorageLocation &Loc) const {
|
|
|
|
auto It = LocToVal.find(&Loc);
|
|
|
|
return It == LocToVal.end() ? nullptr : It->second;
|
|
|
|
}
|
|
|
|
|
2022-01-04 13:47:14 +00:00
|
|
|
Value *Environment::getValue(const ValueDecl &D, SkipPast SP) const {
|
|
|
|
auto *Loc = getStorageLocation(D, SP);
|
|
|
|
if (Loc == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
return getValue(*Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *Environment::getValue(const Expr &E, SkipPast SP) const {
|
|
|
|
auto *Loc = getStorageLocation(E, SP);
|
|
|
|
if (Loc == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
return getValue(*Loc);
|
|
|
|
}
|
|
|
|
|
2022-01-17 15:17:05 +00:00
|
|
|
Value *Environment::createValue(QualType Type) {
|
2021-12-29 11:31:02 +00:00
|
|
|
llvm::DenseSet<QualType> Visited;
|
2022-01-17 15:17:05 +00:00
|
|
|
return createValueUnlessSelfReferential(Type, Visited);
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:17:05 +00:00
|
|
|
Value *Environment::createValueUnlessSelfReferential(
|
|
|
|
QualType Type, llvm::DenseSet<QualType> &Visited) {
|
2021-12-29 11:31:02 +00:00
|
|
|
assert(!Type.isNull());
|
|
|
|
|
|
|
|
if (Type->isIntegerType()) {
|
2022-01-17 15:17:05 +00:00
|
|
|
return &takeOwnership(std::make_unique<IntegerValue>());
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Type->isReferenceType()) {
|
|
|
|
QualType PointeeType = Type->getAs<ReferenceType>()->getPointeeType();
|
|
|
|
auto &PointeeLoc = createStorageLocation(PointeeType);
|
|
|
|
|
|
|
|
if (!Visited.contains(PointeeType.getCanonicalType())) {
|
|
|
|
Visited.insert(PointeeType.getCanonicalType());
|
2022-01-17 15:17:05 +00:00
|
|
|
Value *PointeeVal =
|
|
|
|
createValueUnlessSelfReferential(PointeeType, Visited);
|
2021-12-29 11:31:02 +00:00
|
|
|
Visited.erase(PointeeType.getCanonicalType());
|
2022-01-17 15:17:05 +00:00
|
|
|
|
|
|
|
if (PointeeVal != nullptr)
|
|
|
|
setValue(PointeeLoc, *PointeeVal);
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:17:05 +00:00
|
|
|
return &takeOwnership(std::make_unique<ReferenceValue>(PointeeLoc));
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Type->isPointerType()) {
|
|
|
|
QualType PointeeType = Type->getAs<PointerType>()->getPointeeType();
|
|
|
|
auto &PointeeLoc = createStorageLocation(PointeeType);
|
|
|
|
|
|
|
|
if (!Visited.contains(PointeeType.getCanonicalType())) {
|
|
|
|
Visited.insert(PointeeType.getCanonicalType());
|
2022-01-17 15:17:05 +00:00
|
|
|
Value *PointeeVal =
|
|
|
|
createValueUnlessSelfReferential(PointeeType, Visited);
|
2021-12-29 11:31:02 +00:00
|
|
|
Visited.erase(PointeeType.getCanonicalType());
|
2022-01-17 15:17:05 +00:00
|
|
|
|
|
|
|
if (PointeeVal != nullptr)
|
|
|
|
setValue(PointeeLoc, *PointeeVal);
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:17:05 +00:00
|
|
|
return &takeOwnership(std::make_unique<PointerValue>(PointeeLoc));
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Type->isStructureOrClassType()) {
|
2022-01-11 12:15:53 +00:00
|
|
|
// FIXME: Initialize only fields that are accessed in the context that is
|
|
|
|
// being analyzed.
|
2021-12-29 11:31:02 +00:00
|
|
|
llvm::DenseMap<const ValueDecl *, Value *> FieldValues;
|
|
|
|
for (const FieldDecl *Field : Type->getAsRecordDecl()->fields()) {
|
|
|
|
assert(Field != nullptr);
|
|
|
|
|
|
|
|
QualType FieldType = Field->getType();
|
|
|
|
if (Visited.contains(FieldType.getCanonicalType()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Visited.insert(FieldType.getCanonicalType());
|
|
|
|
FieldValues.insert(
|
2022-01-17 15:17:05 +00:00
|
|
|
{Field, createValueUnlessSelfReferential(FieldType, Visited)});
|
2021-12-29 11:31:02 +00:00
|
|
|
Visited.erase(FieldType.getCanonicalType());
|
|
|
|
}
|
|
|
|
|
2022-01-17 15:17:05 +00:00
|
|
|
return &takeOwnership(
|
|
|
|
std::make_unique<StructValue>(std::move(FieldValues)));
|
2021-12-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-01-04 13:47:14 +00:00
|
|
|
StorageLocation &
|
|
|
|
Environment::takeOwnership(std::unique_ptr<StorageLocation> Loc) {
|
|
|
|
return DACtx->takeOwnership(std::move(Loc));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value &Environment::takeOwnership(std::unique_ptr<Value> Val) {
|
|
|
|
return DACtx->takeOwnership(std::move(Val));
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageLocation &Environment::skip(StorageLocation &Loc, SkipPast SP) const {
|
|
|
|
switch (SP) {
|
|
|
|
case SkipPast::None:
|
|
|
|
return Loc;
|
|
|
|
case SkipPast::Reference:
|
|
|
|
// References cannot be chained so we only need to skip past one level of
|
|
|
|
// indirection.
|
|
|
|
if (auto *Val = dyn_cast_or_null<ReferenceValue>(getValue(Loc)))
|
|
|
|
return Val->getPointeeLoc();
|
|
|
|
return Loc;
|
2022-01-11 12:15:53 +00:00
|
|
|
case SkipPast::ReferenceThenPointer:
|
|
|
|
StorageLocation &LocPastRef = skip(Loc, SkipPast::Reference);
|
|
|
|
if (auto *Val = dyn_cast_or_null<PointerValue>(getValue(LocPastRef)))
|
|
|
|
return Val->getPointeeLoc();
|
|
|
|
return LocPastRef;
|
2022-01-04 13:47:14 +00:00
|
|
|
}
|
|
|
|
llvm_unreachable("bad SkipPast kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
const StorageLocation &Environment::skip(const StorageLocation &Loc,
|
|
|
|
SkipPast SP) const {
|
|
|
|
return skip(*const_cast<StorageLocation *>(&Loc), SP);
|
|
|
|
}
|
|
|
|
|
2021-12-29 11:31:02 +00:00
|
|
|
} // namespace dataflow
|
|
|
|
} // namespace clang
|