Skip to content

Commit

Permalink
Implementing On-The-Fly Reporting (#701)
Browse files Browse the repository at this point in the history
* Analysis Printer (#17)

* Initial Commit

* AnalysisPrinter Second commit

* Initial Commit

* Integrate Printer with client Analysis and test

* Addressing Review comments

* Integrate AnalysisPrinter with all analyses and template class modified

* vector emplace_back instead of push_back

* Testcase for AnalysisPrinter

* GroundTruth derived class initial commit

* AnalysisPrinter Test complete and Test

* fixing myphasartool file

* Test pre-commit fix

* Adding Test cases and fixing PR failure

* 1.template params to N,D,L  2.remove AnalysisType param from AnalysisResults 3.rearranging class variables

* 1.template params to N,D,L 2.remove AnalysisType param from AnalysisResults 3.rearranging class variables

* Null AnalysisPrinter singleton

* Adding AnalysisPrinter to IDETabulation Problem

* making free (N,D,L)ToString functions

* disable copy and move for analysis-printer

* Default NullAnalysisPrinter and explicit print methods

* removing SetAnalysisPrinter from client analyses and modified Testcase for AnalysisPrinter

* Adding superclass for AnalysisPrinter

* Addressing review comments and fixing PR build failure

* fix: minors

* fix: minor (clang-tidy)

* fix: review feedback

* misc: minor refactoring

---------

Co-authored-by: SanthoshMohan <santhoshmohan0897@gmail.com>
Co-authored-by: Sriteja Kummita <sriteja.ku@gmail.com>

* OnTheFlyReporting Initial Commit

* fix: review feedback

* onTheFlyAnalysis makeUniquePtr

* OnTheFlyReporting Initial Commit

* onTheFlyAnalysis makeUniquePtr

* addressing minor error in prev commit

* Refactoring Warn Variable and Refactoring lambda flow for AnalysisPrinter

* Integrating sourceMgr to AnalysisPrinter

* Testcase for OnTheFlyAnalysisPrinting

* Testcase for SourceMgrPrinter

* MaybeUniquePtr for SourceMgrPrinter

* Minor review comments

* refactoring the printers

* adding TODOs

* more TODOs

* Refactor AnalysisPrinter part1

* Refactor AnalysisPrinter part2

* dev: update AnalysisPrinterBase, integrate and fix tests

* adding warning kind

* fix ci

* update codeowners for analysis-printer

* Refactor SourceManagerPrinter

* Minor in OTF Printer Test

* fixing review feedback

* fix l_t printing

* Mandating analysisType to tyestate descriptions

---------

Co-authored-by: SanthoshMohan <santhoshmohan0897@gmail.com>
Co-authored-by: Fabian Schiebel <fabian.schiebel@iem.fraunhofer.de>
  • Loading branch information
3 people authored Feb 25, 2024
1 parent cd302d8 commit 8a786ac
Show file tree
Hide file tree
Showing 35 changed files with 757 additions and 213 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Checks: '-*,
misc-*,
-misc-non-private-member-variables-in-classes,
-misc-no-recursion,
-misc-use-anonymous-namespace,
readability-*,
-readability-function-cognitive-complexity,
-readability-else-after*,
Expand Down
6 changes: 6 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ Dockerfile @janniclas
/.docker/ @janniclas

/include/phasar/Utils/Logger.h @MMory
/include/phasar/Utils/AnalysisPrinterBase.h @sritejakv
/include/phasar/Utils/DefaultAnalysisPrinter.h @sritejakv
/include/phasar/Utils/NullAnalysisPrinter.h @sritejakv
/include/phasar/Utils/OnTheFlyAnalysisPrinter.h @sritejakv
/include/phasar/PhasarLLVM/Utils/SourceMgrPrinter.h @sritejakv
/lib/PhasarLLVM/Utils/SourceMgrPrinter.cpp @sritejakv
2 changes: 1 addition & 1 deletion include/phasar/DataFlow/IfdsIde/IDETabulationProblem.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include "phasar/DataFlow/IfdsIde/IFDSIDESolverConfig.h"
#include "phasar/DataFlow/IfdsIde/InitialSeeds.h"
#include "phasar/DataFlow/IfdsIde/SolverResults.h"
#include "phasar/PhasarLLVM/Utils/NullAnalysisPrinter.h"
#include "phasar/Utils/JoinLattice.h"
#include "phasar/Utils/NullAnalysisPrinter.h"
#include "phasar/Utils/Printer.h"
#include "phasar/Utils/Soundness.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "phasar/PhasarLLVM/DataFlow/IfdsIde/LLVMZeroValue.h"
#include "phasar/PhasarLLVM/Domain/LLVMAnalysisDomain.h"
#include "phasar/PhasarLLVM/Pointer/LLVMAliasInfo.h"
#include "phasar/PhasarLLVM/Utils/DataFlowAnalysisType.h"
#include "phasar/Utils/ByRef.h"
#include "phasar/Utils/JoinLattice.h"
#include "phasar/Utils/Logger.h"
Expand Down Expand Up @@ -529,10 +530,9 @@ class IDETypeStateAnalysis
if (const auto *Alloca =
llvm::dyn_cast<llvm::AllocaInst>(Res.first)) {
if (Res.second == TSD->error()) {
Warning<IDETypeStateAnalysisDomain<TypeStateDescriptionTy>>
Warn(&I, Res.first, TSD->error());
// ERROR STATE DETECTED
this->Printer->onResult(Warn);
this->Printer->onResult(&I, Res.first, TSD->error(),
TSD->analysisType());
}
}
}
Expand All @@ -541,10 +541,9 @@ class IDETypeStateAnalysis
if (const auto *Alloca =
llvm::dyn_cast<llvm::AllocaInst>(Res.first)) {
if (Res.second == TSD->error()) {
Warning<IDETypeStateAnalysisDomain<TypeStateDescriptionTy>>
Warn(&I, Res.first, TSD->error());
// ERROR STATE DETECTED
this->Printer->onResult(Warn);
this->Printer->onResult(&I, Res.first, TSD->error(),
TSD->analysisType());
}
}
}
Expand All @@ -553,7 +552,7 @@ class IDETypeStateAnalysis
}
}

this->Printer->onFinalize(OS);
this->Printer->onFinalize();
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class CSTDFILEIOTypeStateDescription
[[nodiscard]] TypeStateDescription::State uninit() const override;
[[nodiscard]] TypeStateDescription::State start() const override;
[[nodiscard]] TypeStateDescription::State error() const override;
[[nodiscard]] DataFlowAnalysisType analysisType() const override;
};

extern template class IDETypeStateAnalysis<CSTDFILEIOTypeStateDescription>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class OpenSSLEVPKDFCTXDescription
[[nodiscard]] State uninit() const override;
[[nodiscard]] State start() const override;
[[nodiscard]] State error() const override;
[[nodiscard]] DataFlowAnalysisType analysisType() const override;
/*
/// Checks all callSites, where a EVP_KDF object needs to be in a
/// certain state, such that the state transition for EVP_KDF_CTX is valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class OpenSSLEVPKDFDescription
[[nodiscard]] TypeStateDescription::State start() const override;

[[nodiscard]] TypeStateDescription::State error() const override;

[[nodiscard]] DataFlowAnalysisType analysisType() const override;
};

} // namespace psr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class OpenSSLSecureHeapDescription
[[nodiscard]] TypeStateDescription::State uninit() const override;
[[nodiscard]] TypeStateDescription::State start() const override;
[[nodiscard]] TypeStateDescription::State error() const override;
[[nodiscard]] DataFlowAnalysisType analysisType() const override;
};

} // namespace psr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class OpenSSLSecureMemoryDescription
[[nodiscard]] TypeStateDescription::State uninit() const override;
[[nodiscard]] TypeStateDescription::State start() const override;
[[nodiscard]] TypeStateDescription::State error() const override;
[[nodiscard]] DataFlowAnalysisType analysisType() const override;
};

} // namespace psr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#ifndef PHASAR_PHASARLLVM_DATAFLOW_IFDSIDE_PROBLEMS_TYPESTATEDESCRIPTIONS_TYPESTATEDESCRIPTION_H
#define PHASAR_PHASARLLVM_DATAFLOW_IFDSIDE_PROBLEMS_TYPESTATEDESCRIPTIONS_TYPESTATEDESCRIPTION_H

#include "phasar/PhasarLLVM/Utils/DataFlowAnalysisType.h"

#include "llvm/IR/InstrTypes.h"

#include <set>
Expand All @@ -28,6 +30,7 @@ struct TypeStateDescriptionBase {
getConsumerParamIdx(llvm::StringRef F) const = 0;
[[nodiscard]] virtual std::set<int>
getFactoryParamIdx(llvm::StringRef F) const = 0;
[[nodiscard]] virtual DataFlowAnalysisType analysisType() const = 0;
};

/**
Expand Down
44 changes: 0 additions & 44 deletions include/phasar/PhasarLLVM/Utils/AnalysisPrinterBase.h

This file was deleted.

47 changes: 0 additions & 47 deletions include/phasar/PhasarLLVM/Utils/DefaultAnalysisPrinter.h

This file was deleted.

11 changes: 11 additions & 0 deletions include/phasar/PhasarLLVM/Utils/LLVMIRToSrc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "nlohmann/json.hpp"

#include <optional>
#include <string>

// Forward declaration of types for which we only use its pointer or ref type
Expand All @@ -39,6 +40,7 @@ namespace psr {
[[nodiscard]] std::string getFunctionNameFromIR(const llvm::Value *V);

[[nodiscard]] std::string getFilePathFromIR(const llvm::Value *V);
[[nodiscard]] std::string getFilePathFromIR(const llvm::DIFile *DIF);

[[nodiscard]] std::string getDirectoryFromIR(const llvm::Value *V);

Expand Down Expand Up @@ -85,6 +87,15 @@ void to_json(nlohmann::json &J, const SourceCodeInfo &Info);

[[nodiscard]] SourceCodeInfo getSrcCodeInfoFromIR(const llvm::Value *V);

struct DebugLocation {
unsigned Line{};
unsigned Column{};
const llvm::DIFile *File{};
};

[[nodiscard]] std::optional<DebugLocation>
getDebugLocation(const llvm::Value *V);

} // namespace psr

#endif
36 changes: 36 additions & 0 deletions include/phasar/PhasarLLVM/Utils/LLVMSourceManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef PHASAR_PHASARLLVM_UTILS_LLVMSOURCEMANAGER_H
#define PHASAR_PHASARLLVM_UTILS_LLVMSOURCEMANAGER_H

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/SourceMgr.h"

#include <optional>

namespace llvm {
class Value;
class DIFile;
} // namespace llvm

namespace psr {
struct ManagedDebugLocation {
unsigned Line{};
unsigned Column{};
unsigned File{};
};

class LLVMSourceManager {
public:
[[nodiscard]] std::optional<ManagedDebugLocation>
getDebugLocation(const llvm::Value *V);

void print(llvm::raw_ostream &OS, ManagedDebugLocation Loc,
llvm::SourceMgr::DiagKind DiagKind, const llvm::Twine &Message);

private:
llvm::DenseMap<const llvm::DIFile *, unsigned> FileIdMap{};
llvm::SourceMgr SrcMgr{};
};
} // namespace psr

#endif // PHASAR_PHASARLLVM_UTILS_LLVMSOURCEMANAGER_H
71 changes: 71 additions & 0 deletions include/phasar/PhasarLLVM/Utils/SourceMgrPrinter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef PHASAR_PHASARLLVM_UTILS_SOURCEMGRPRINTER_H
#define PHASAR_PHASARLLVM_UTILS_SOURCEMGRPRINTER_H

#include "phasar/PhasarLLVM/Utils/DataFlowAnalysisType.h"
#include "phasar/PhasarLLVM/Utils/LLVMIRToSrc.h"
#include "phasar/PhasarLLVM/Utils/LLVMSourceManager.h"
#include "phasar/Utils/AnalysisPrinterBase.h"
#include "phasar/Utils/MaybeUniquePtr.h"

#include "llvm/ADT/FunctionExtras.h"
#include "llvm/Support/raw_ostream.h"

#include <type_traits>

namespace psr {

namespace detail {
class SourceMgrPrinterBase {
public:
explicit SourceMgrPrinterBase(
llvm::unique_function<std::string(DataFlowAnalysisType)> &&PrintMessage,
llvm::raw_ostream &OS = llvm::errs(),
llvm::SourceMgr::DiagKind WKind = llvm::SourceMgr::DK_Warning);

explicit SourceMgrPrinterBase(
llvm::unique_function<std::string(DataFlowAnalysisType)> &&PrintMessage,
const llvm::Twine &OutFileName,
llvm::SourceMgr::DiagKind WKind = llvm::SourceMgr::DK_Warning);

protected:
LLVMSourceManager SrcMgr;

llvm::unique_function<std::string(DataFlowAnalysisType)> GetPrintMessage;
MaybeUniquePtr<llvm::raw_ostream> OS = &llvm::errs();
llvm::SourceMgr::DiagKind WarningKind;
};
} // namespace detail

template <typename AnalysisDomainTy>
class SourceMgrPrinter : public AnalysisPrinterBase<AnalysisDomainTy>,
private detail::SourceMgrPrinterBase {
using n_t = typename AnalysisDomainTy::n_t;
using d_t = typename AnalysisDomainTy::d_t;
using l_t = typename AnalysisDomainTy::l_t;

public:
explicit SourceMgrPrinter(
llvm::unique_function<std::string(DataFlowAnalysisType)> &&PrintMessage,
llvm::raw_ostream &OS = llvm::errs(),
llvm::SourceMgr::DiagKind WKind = llvm::SourceMgr::DK_Warning)
: detail::SourceMgrPrinterBase(std::move(PrintMessage), OS, WKind) {}

private:
void doOnResult(n_t Inst, d_t Fact, l_t /*Value*/,
DataFlowAnalysisType AnalysisType) override {
auto SrcLoc = SrcMgr.getDebugLocation(Inst);
if constexpr (std::is_convertible_v<d_t, const llvm::Value *>) {
if (!SrcLoc) {
SrcLoc =
SrcMgr.getDebugLocation(static_cast<const llvm::Value *>(Fact));
}
}

if (SrcLoc) {
SrcMgr.print(*OS, *SrcLoc, WarningKind, GetPrintMessage(AnalysisType));
}
}
};

} // namespace psr
#endif
Loading

0 comments on commit 8a786ac

Please sign in to comment.