Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor getAsJson #24

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
d3b3172
unresolved ref to F/NToString
mxHuber Mar 13, 2024
0912f5c
compiles
mxHuber Mar 13, 2024
3be40ea
refactored CallGraph::deserialize()
mxHuber Mar 14, 2024
86fc715
refactored unittests, all fail
mxHuber Mar 14, 2024
3ecb12a
all unittests finally pass
mxHuber Mar 18, 2024
3664d3f
code cleanup
mxHuber Mar 20, 2024
ef50e02
refactoring
mxHuber Mar 20, 2024
36ae5b9
fixed function with no caller not being serialized
mxHuber Mar 22, 2024
7af57e4
Fix Dependendency Installation (#707)
fabianbs96 Mar 13, 2024
a4091ca
backup DIBTH Data
mxHuber Mar 26, 2024
1d9ea9b
DIBTH serialization code, untested
mxHuber Mar 27, 2024
1cb3345
DIBTHData getDataFromJson Impl
mxHuber Mar 28, 2024
d828566
improved de-/serialization impl w/o TypeToVertex
mxHuber Mar 29, 2024
751366f
fixed DIBTHData TypeToVertex not compiling
mxHuber Mar 29, 2024
6e37cd3
fixed serialization bugs and unittests
mxHuber Apr 3, 2024
8708a76
DITHS Test fixes, 13 pass, 11 fail
mxHuber Apr 4, 2024
50d966a
DIBTH Ser Test, 1 failing Test left
mxHuber Apr 4, 2024
9d81f4e
GeneralStatisticsAnalysisData impl
mxHuber Apr 5, 2024
643da4b
LLVMAliasSetData impl
mxHuber Apr 6, 2024
7f79712
last printAsJson Impls
mxHuber Apr 8, 2024
a347b7d
Final Impls, DIBTH Identifier Impl not working
mxHuber Apr 10, 2024
b4c8ff3
changes + fixes from meeting
mxHuber Apr 10, 2024
c835961
no equality of values DIBTH Test
mxHuber Apr 11, 2024
7f6194e
fixed all bugs, all unittests pass
mxHuber Apr 12, 2024
4fc243a
Merge branch 'development' into f-GetAsJsonRefactor
fabianbs96 Apr 14, 2024
16bbf2a
Fix crash in buildTypeGraph in the presence of typedefs
fabianbs96 Apr 15, 2024
47bf183
fixed LLVMAliasSetSerializationTest
mxHuber Apr 17, 2024
767a90e
Add more aggressive composite type filter
fabianbs96 Apr 20, 2024
fe605e9
Refactor handling of DI type names
fabianbs96 Apr 20, 2024
53e222e
Implicit vertex index
fabianbs96 Apr 20, 2024
e4d2892
Prioritize type id over type name as the latter may be ambiguous
fabianbs96 Apr 20, 2024
7d3641c
Adapt DI TH test to identifiers as names
fabianbs96 Apr 20, 2024
f630ebe
Fix vtable + transitive di th tests
fabianbs96 Apr 21, 2024
d8766de
Optimizing deprecation warnings for getAsJson
fabianbs96 Apr 21, 2024
4d079d8
TypeTrait check current non functioning impl
mxHuber Apr 22, 2024
fa6bb87
fixed TypeHierarchySerialization Tests
mxHuber Apr 28, 2024
cb8bc83
Optimized deprecation warinings for getAsJson
mxHuber Apr 28, 2024
fd70a41
debug code backup
mxHuber Apr 30, 2024
d1078f5
Fix LLVMBasedTypeHierarchy
fabianbs96 May 5, 2024
1a2f47a
added operator<< statistics to printAsJson
mxHuber May 8, 2024
4eb1f12
Fix GeneralStatistics printAsJson + minor
fabianbs96 May 19, 2024
987050c
Add some more deprecations
fabianbs96 May 19, 2024
4a2ac9d
Merge branch 'development' into f-GetAsJsonRefactor
fabianbs96 May 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions include/phasar/ControlFlow/CallGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
#define PHASAR_CONTROLFLOW_CALLGRAPH_H

#include "phasar/ControlFlow/CallGraphBase.h"
#include "phasar/ControlFlow/CallGraphData.h"
#include "phasar/Utils/ByRef.h"
#include "phasar/Utils/Logger.h"
#include "phasar/Utils/StableVector.h"
#include "phasar/Utils/Utilities.h"

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/Function.h"

#include "nlohmann/json.hpp"

#include <functional>
#include <string>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -58,7 +58,7 @@ class CallGraph : public CallGraphBase<CallGraph<N, F>> {
/// Deserializes a previously computed call-graph
template <typename FunctionGetter, typename InstructionGetter>
[[nodiscard]] static CallGraph
deserialize(const nlohmann::json &PrecomputedCG,
deserialize(const CallGraphData &PrecomputedCG,
FunctionGetter GetFunctionFromName,
InstructionGetter GetInstructionFromId);

Expand Down Expand Up @@ -86,12 +86,33 @@ class CallGraph : public CallGraphBase<CallGraph<N, F>> {

[[nodiscard]] bool empty() const noexcept { return CallersOf.empty(); }

template <typename FunctionIdGetter, typename InstIdGetter>
void printAsJson(llvm::raw_ostream &OS, FunctionIdGetter GetFunctionId,
InstIdGetter GetInstructionId) const {
CallGraphData CGData;
CGData.FToFunctionVertexTy.reserve(CallersOf.size());

for (const auto &[Fun, Callers] : CallersOf) {
auto &JCallers =
CGData.FToFunctionVertexTy[std::invoke(GetFunctionId, Fun)];

CGData.FToFunctionVertexTy.reserve(Callers->size());
for (const auto &CS : *Callers) {
JCallers.push_back(std::invoke(GetInstructionId, CS));
}
}

CGData.printAsJson(OS);
}

/// Creates a JSON representation of this call-graph suitable for presistent
/// storage.
/// Use the ctor taking a json object for deserialization
template <typename FunctionIdGetter, typename InstIdGetter>
[[nodiscard]] nlohmann::json getAsJson(FunctionIdGetter GetFunctionId,
InstIdGetter GetInstructionId) const {
[[nodiscard]] [[deprecated(
"Please use printAsJson() instead")]] nlohmann::json
getAsJson(FunctionIdGetter GetFunctionId,
InstIdGetter GetInstructionId) const {
nlohmann::json J;

for (const auto &[Fun, Callers] : CallersOf) {
Expand Down Expand Up @@ -254,18 +275,13 @@ template <typename N, typename F> class CallGraphBuilder {
template <typename N, typename F>
template <typename FunctionGetter, typename InstructionGetter>
[[nodiscard]] CallGraph<N, F>
CallGraph<N, F>::deserialize(const nlohmann::json &PrecomputedCG,
CallGraph<N, F>::deserialize(const CallGraphData &PrecomputedCG,
FunctionGetter GetFunctionFromName,
InstructionGetter GetInstructionFromId) {
if (!PrecomputedCG.is_object()) {
PHASAR_LOG_LEVEL_CAT(ERROR, "CallGraph", "Invalid Json. Expected object");
return {};
}

CallGraphBuilder<N, F> CGBuilder;
CGBuilder.reserve(PrecomputedCG.size());
CGBuilder.reserve(PrecomputedCG.FToFunctionVertexTy.size());

for (const auto &[FunName, CallerIDs] : PrecomputedCG.items()) {
for (const auto &[FunName, CallerIDs] : PrecomputedCG.FToFunctionVertexTy) {
const auto &Fun = std::invoke(GetFunctionFromName, FunName);
if (!Fun) {
PHASAR_LOG_LEVEL_CAT(WARNING, "CallGraph",
Expand All @@ -277,11 +293,10 @@ CallGraph<N, F>::deserialize(const nlohmann::json &PrecomputedCG,
CEdges->reserve(CallerIDs.size());

for (const auto &JId : CallerIDs) {
auto Id = JId.get<size_t>();
const auto &CS = std::invoke(GetInstructionFromId, Id);
const auto &CS = std::invoke(GetInstructionFromId, JId);
if (!CS) {
PHASAR_LOG_LEVEL_CAT(WARNING, "CallGraph",
"Invalid CAll-Instruction Id: " << Id);
"Invalid CAll-Instruction Id: " << JId);
}

CGBuilder.addCallEdge(CS, Fun);
Expand Down
33 changes: 33 additions & 0 deletions include/phasar/ControlFlow/CallGraphData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/******************************************************************************
* Copyright (c) 2024 Fabian Schiebel.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of LICENSE.txt.
*
* Contributors:
* Maximilian Leo Huber and others
*****************************************************************************/

#ifndef PHASAR_PHASARLLVM_CONTROLFLOW_CALLGRAPHDATA_H
#define PHASAR_PHASARLLVM_CONTROLFLOW_CALLGRAPHDATA_H

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

#include <string>
#include <unordered_map>
#include <vector>

namespace psr {
struct CallGraphData {
std::unordered_map<std::string, std::vector<int>> FToFunctionVertexTy{};

CallGraphData() noexcept = default;
void printAsJson(llvm::raw_ostream &OS);

static CallGraphData deserializeJson(const llvm::Twine &Path);
static CallGraphData loadJsonString(llvm::StringRef JsonAsString);
};

} // namespace psr

#endif // PHASAR_PHASARLLVM_CONTROLFLOW_CALLGRAPHDATA_H
10 changes: 9 additions & 1 deletion include/phasar/ControlFlow/ICFGBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,16 @@ template <typename Derived> class ICFGBase {
void print(llvm::raw_ostream &OS = llvm::outs()) const {
self().printImpl(OS);
}

/// Prints the underlying call-graph as Json to the given output-stream
void printAsJson(llvm::raw_ostream &OS = llvm::outs()) const {
self().printAsJsonImpl(OS);
}

/// Returns the underlying call-graph as JSON
[[nodiscard]] nlohmann::json getAsJson() const {
[[nodiscard]] [[deprecated(
"Please use printAsJson() instead")]] nlohmann::json
getAsJson() const {
return self().getAsJsonImpl();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class LLVMBasedBackwardICFG : public LLVMBasedBackwardCFG,
using CFGBase::print;
using ICFGBase::print;

using ICFGBase::printAsJson;

using CFGBase::getAsJson;
using ICFGBase::getAsJson;

Expand All @@ -64,7 +66,8 @@ class LLVMBasedBackwardICFG : public LLVMBasedBackwardCFG,
[[nodiscard]] llvm::SmallVector<n_t, 2>
getReturnSitesOfCallAtImpl(n_t Inst) const;
void printImpl(llvm::raw_ostream &OS) const;
[[nodiscard]] nlohmann::json getAsJsonImpl() const;
void printAsJsonImpl(llvm::raw_ostream &OS) const;
[[nodiscard, deprecated]] nlohmann::json getAsJsonImpl() const;
[[nodiscard]] const CallGraph<n_t, f_t> &getCallGraphImpl() const noexcept;

llvm::LLVMContext BackwardRetsCtx;
Expand Down
9 changes: 5 additions & 4 deletions include/phasar/PhasarLLVM/ControlFlow/LLVMBasedICFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
#include "llvm/IR/Value.h"
#include "llvm/Support/raw_ostream.h"

#include "nlohmann/json.hpp"

#include <memory>

namespace psr {
Expand Down Expand Up @@ -93,7 +91,7 @@ class LLVMBasedICFG : public LLVMBasedCFG, public ICFGBase<LLVMBasedICFG> {
LLVMTypeHierarchy *TH = nullptr);

explicit LLVMBasedICFG(LLVMProjectIRDB *IRDB,
const nlohmann::json &SerializedCG,
const CallGraphData &SerializedCG,
LLVMTypeHierarchy *TH = nullptr);

// Deleter of LLVMTypeHierarchy may be unknown here...
Expand Down Expand Up @@ -135,6 +133,8 @@ class LLVMBasedICFG : public LLVMBasedCFG, public ICFGBase<LLVMBasedICFG> {
using CFGBase::print;
using ICFGBase::print;

using ICFGBase::printAsJson;

using CFGBase::getAsJson;
using ICFGBase::getAsJson;

Expand All @@ -149,7 +149,8 @@ class LLVMBasedICFG : public LLVMBasedCFG, public ICFGBase<LLVMBasedICFG> {
[[nodiscard]] llvm::SmallVector<n_t, 2>
getReturnSitesOfCallAtImpl(n_t Inst) const;
void printImpl(llvm::raw_ostream &OS) const;
[[nodiscard]] nlohmann::json getAsJsonImpl() const;
void printAsJsonImpl(llvm::raw_ostream &OS) const;
[[nodiscard, deprecated]] nlohmann::json getAsJsonImpl() const;
[[nodiscard]] const CallGraph<n_t, f_t> &getCallGraphImpl() const noexcept {
return CG;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ struct GeneralStatistics {
"instead")]] const std::set<const llvm::Instruction *> &
getRetResInstructions() const;

[[nodiscard]] nlohmann::json getAsJson() const;
[[nodiscard]] [[deprecated(
"Please use printAsJson() instead")]] nlohmann::json
getAsJson() const;
void printAsJson(llvm::raw_ostream &OS = llvm::outs()) const;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/******************************************************************************
* Copyright (c) 2024 Fabian Schiebel.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of LICENSE.txt.
*
* Contributors:
* Maximilian Leo Huber and others
*****************************************************************************/

#ifndef PHASAR_PHASARLLVM_PASSES_GENERALSTATISTICSANALYSISDATA_H
#define PHASAR_PHASARLLVM_PASSES_GENERALSTATISTICSANALYSISDATA_H

#include "llvm/Support/raw_ostream.h"

#include <cstddef>
#include <set>
#include <string>

namespace psr {
struct GeneralStatisticsAnalysisSerializer {
fabianbs96 marked this conversation as resolved.
Show resolved Hide resolved
size_t Functions = 0;
size_t ExternalFunctions = 0;
size_t FunctionDefinitions = 0;
size_t AddressTakenFunctions = 0;
size_t Globals = 0;
size_t GlobalConsts = 0;
size_t BasicBlocks = 0;
size_t CallSites = 0;
size_t DebugIntrinsics = 0;
size_t Instructions = 0;
size_t MemIntrinsics = 0;
size_t Branches = 0;
size_t GetElementPtrs = 0;
size_t LandingPads = 0;
size_t PhiNodes = 0;
size_t NumInlineAsm = 0;
size_t IndCalls = 0;
size_t NonVoidInsts = 0;
size_t NumberOfAllocaInstructions = 0;
std::string ModuleName{};

GeneralStatisticsAnalysisSerializer() noexcept = default;
void printAsJson(llvm::raw_ostream &OS);
};

} // namespace psr

#endif // PHASAR_PHASARLLVM_PASSES_GENERALSTATISTICSANALYSISDATA_H
7 changes: 6 additions & 1 deletion include/phasar/PhasarLLVM/Pointer/LLVMAliasSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef PHASAR_PHASARLLVM_POINTER_LLVMALIASSET_H
#define PHASAR_PHASARLLVM_POINTER_LLVMALIASSET_H

#include "phasar/PhasarLLVM/Pointer/LLVMAliasSetData.h"
#include "phasar/PhasarLLVM/Pointer/LLVMBasedAliasAnalysis.h"
#include "phasar/Pointer/AliasInfoBase.h"
#include "phasar/Pointer/AliasInfoTraits.h"
Expand Down Expand Up @@ -95,7 +96,11 @@ class LLVMAliasSet : public AnalysisPropertiesMixin<LLVMAliasSet>,

void print(llvm::raw_ostream &OS = llvm::outs()) const;

[[nodiscard]] nlohmann::json getAsJson() const;
[[nodiscard]] [[deprecated(
"Please use printAsJson() instead")]] nlohmann::json
getAsJson() const;

[[nodiscard]] LLVMAliasSetData getLLVMAliasSetData() const;

void printAsJson(llvm::raw_ostream &OS = llvm::outs()) const;

Expand Down
31 changes: 31 additions & 0 deletions include/phasar/PhasarLLVM/Pointer/LLVMAliasSetData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/******************************************************************************
* Copyright (c) 2024 Fabian Schiebel.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of LICENSE.txt.
*
* Contributors:
* Maximilian Leo Huber and others
*****************************************************************************/

#ifndef PHASAR_PHASARLLVM_TYPEHIERARCHY_LLVMALIASSETDATA_H
#define PHASAR_PHASARLLVM_TYPEHIERARCHY_LLVMALIASSETDATA_H

#include "llvm/Support/raw_ostream.h"

#include <string>

namespace psr {
struct LLVMAliasSetData {
std::vector<std::vector<std::string>> AliasSets;
std::vector<std::string> AnalyzedFunctions;

LLVMAliasSetData() noexcept = default;
void printAsJson(llvm::raw_ostream &OS);

static LLVMAliasSetData deserializeJson(const llvm::Twine &Path);
static LLVMAliasSetData loadJsonString(llvm::StringRef JsonAsString);
};

} // namespace psr

#endif // PHASAR_PHASARLLVM_TYPEHIERARCHY_LLVMALIASSETDATA_H
18 changes: 14 additions & 4 deletions include/phasar/PhasarLLVM/TypeHierarchy/DIBasedTypeHierarchy.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef PHASAR_PHASARLLVM_TYPEHIERARCHY_DIBASEDTYPEHIERARCHY_H
#define PHASAR_PHASARLLVM_TYPEHIERARCHY_DIBASEDTYPEHIERARCHY_H

#include "phasar/PhasarLLVM/TypeHierarchy/DIBasedTypeHierarchyData.h"
#include "phasar/PhasarLLVM/TypeHierarchy/LLVMVFTable.h"
#include "phasar/TypeHierarchy/TypeHierarchy.h"

Expand All @@ -32,6 +33,8 @@ class DIBasedTypeHierarchy
using f_t = const llvm::Function *;

explicit DIBasedTypeHierarchy(const LLVMProjectIRDB &IRDB);
explicit DIBasedTypeHierarchy(const LLVMProjectIRDB *IRDB,
const DIBasedTypeHierarchyData &SerializedData);
~DIBasedTypeHierarchy() override = default;

[[nodiscard]] bool hasType(ClassType Type) const override {
Expand Down Expand Up @@ -67,9 +70,7 @@ class DIBasedTypeHierarchy

[[nodiscard]] const auto &getAllVTables() const noexcept { return VTables; }

[[nodiscard]] std::string getTypeName(ClassType Type) const override {
return Type->getName().str();
}
[[nodiscard]] std::string getTypeName(ClassType Type) const override;

[[nodiscard]] bool hasVFTable(ClassType Type) const override;

Expand All @@ -93,9 +94,18 @@ class DIBasedTypeHierarchy
*/
void printAsDot(llvm::raw_ostream &OS = llvm::outs()) const;

[[nodiscard]] nlohmann::json getAsJson() const override;
[[nodiscard]] [[deprecated(
"Please use printAsJson() instead")]] nlohmann::json
getAsJson() const override;

/**
* @brief Prints the class hierarchy to an ostream in json format.
* @param an outputstream
*/
void printAsJson(llvm::raw_ostream &OS = llvm::outs()) const override;

private:
[[nodiscard]] DIBasedTypeHierarchyData getTypeHierarchyData() const;
[[nodiscard]] llvm::iterator_range<const ClassType *>
subTypesOf(size_t TypeIdx) const noexcept;

Expand Down
Loading
Loading