Skip to content

Commit 3ca88e3

Browse files
committed
fixing bug in points-to info and OTF call-graph algorithm
1 parent 1b40e26 commit 3ca88e3

23 files changed

Lines changed: 378 additions & 295 deletions

include/phasar/Controller/AnalysisController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AnalysisController {
3333
std::vector<DataFlowAnalysisType> DataFlowAnalyses;
3434
std::vector<std::string> AnalysisConfigs;
3535
std::set<std::string> EntryPoints;
36-
AnalysisStrategy Strategy;
36+
[[maybe_unused]] AnalysisStrategy Strategy;
3737

3838
void executeDemandDriven();
3939
void executeIncremental();

include/phasar/PhasarLLVM/ControlFlow/CFG.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <utility> // std::pair
2222
#include <vector>
2323

24+
#include <json.hpp>
25+
2426
namespace psr {
2527

2628
template <typename N, typename M> class CFG {
@@ -52,6 +54,10 @@ template <typename N, typename M> class CFG {
5254
virtual std::string getStatementId(N stmt) const = 0;
5355

5456
virtual std::string getFunctionName(M fun) const = 0;
57+
58+
virtual void print(M fun, std::ostream &OS) const = 0;
59+
60+
virtual nlohmann::json getAsJson(M fun) const = 0;
5561
};
5662

5763
} // namespace psr

include/phasar/PhasarLLVM/ControlFlow/ICFG.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#ifndef PHASAR_PHASARLLVM_CONTROLFLOW_ICFG_H_
1818
#define PHASAR_PHASARLLVM_CONTROLFLOW_ICFG_H_
1919

20-
#include <iosfwd>
20+
#include <iostream>
2121
#include <map>
2222
#include <set>
2323
#include <string>
@@ -68,6 +68,10 @@ template <typename N, typename M> class ICFG : public virtual CFG<N, M> {
6868

6969
virtual std::set<N> getReturnSitesOfCallAt(N stmt) const = 0;
7070

71+
using CFG<N, M>::print; // tell the compiler we wish to have both prints
72+
virtual void print(std::ostream &OS = std::cout) const = 0;
73+
74+
using CFG<N, M>::getAsJson; // tell the compiler we wish to have both prints
7175
virtual nlohmann::json getAsJson() const = 0;
7276
};
7377

include/phasar/PhasarLLVM/ControlFlow/LLVMBasedBackwardCFG.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class LLVMBasedBackwardCFG
7373
std::string getFunctionName(const llvm::Function *fun) const override;
7474

7575
std::string getStatementId(const llvm::Instruction *stmt) const override;
76+
77+
void print(const llvm::Function *F, std::ostream &OS) const override;
78+
79+
nlohmann::json getAsJson(const llvm::Function *F) const override;
7680
};
7781
} // namespace psr
7882

include/phasar/PhasarLLVM/ControlFlow/LLVMBasedBackwardICFG.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,16 @@ class LLVMBasedBackwardsICFG
9292

9393
bool isPrimitiveFunction(const std::string &name);
9494

95-
void print(std::ostream &OS);
95+
using LLVMBasedBackwardCFG::print; // tell the compiler we wish to have both
96+
// prints
97+
void print(std::ostream &OS) const override;
9698

97-
void printAsDot(std::ostream &OS);
99+
void printAsDot(std::ostream &OS) const;
98100

99101
void printInternalPTGAsDot(const std::string &filename);
100102

103+
using LLVMBasedBackwardCFG::getAsJson; // tell the compiler we wish to have
104+
// both prints
101105
nlohmann::json getAsJson() const override;
102106

103107
unsigned getNumOfVertices();

include/phasar/PhasarLLVM/ControlFlow/LLVMBasedCFG.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef PHASAR_PHASARLLVM_CONTROLFLOW_LLVMBASEDCFG_H_
1818
#define PHASAR_PHASARLLVM_CONTROLFLOW_LLVMBASEDCFG_H_
1919

20+
#include <iostream>
2021
#include <set>
2122
#include <string>
2223
#include <vector>
@@ -69,6 +70,11 @@ class LLVMBasedCFG
6970
std::string getStatementId(const llvm::Instruction *stmt) const override;
7071

7172
std::string getFunctionName(const llvm::Function *fun) const override;
73+
74+
void print(const llvm::Function *F,
75+
std::ostream &OS = std::cout) const override;
76+
77+
nlohmann::json getAsJson(const llvm::Function *F) const override;
7278
};
7379

7480
} // namespace psr

include/phasar/PhasarLLVM/ControlFlow/LLVMBasedICFG.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,15 @@ class LLVMBasedICFG
157157

158158
bool isPrimitiveFunction(const std::string &name);
159159

160-
void print(std::ostream &OS = std::cout);
160+
using LLVMBasedCFG::print; // tell the compiler we wish to have both prints
161+
void print(std::ostream &OS = std::cout) const override;
161162

162-
void printAsDot(std::ostream &OS = std::cout);
163+
void printAsDot(std::ostream &OS = std::cout) const;
163164

164165
void printInternalPTGAsDot(const std::string &filename);
165166

167+
using LLVMBasedCFG::getAsJson; // tell the compiler we wish to have both
168+
// prints
166169
nlohmann::json getAsJson() const override;
167170

168171
unsigned getNumOfVertices();

include/phasar/PhasarLLVM/ControlFlow/Resolver/OTFResolver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class OTFResolver : public CHAResolver {
5757

5858
std::set<const llvm::Function *>
5959
resolveVirtualCall(llvm::ImmutableCallSite CS) override;
60+
61+
std::set<const llvm::Function *>
62+
resolveFunctionPointer(llvm::ImmutableCallSite CS) override;
6063
};
6164
} // namespace psr
6265

include/phasar/PhasarLLVM/Pointer/LLVMPointsToGraph.h

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#define PHASAR_PHASARLLVM_POINTER_POINTSTOGRAPH_H_
1919

2020
#include <iostream>
21+
#include <unordered_map>
22+
#include <unordered_set>
2123
#include <vector>
2224

2325
#include <boost/graph/adjacency_list.hpp>
@@ -48,18 +50,6 @@ static inline bool isInterestingPointer(llvm::Value *V) {
4850
!llvm::isa<llvm::ConstantPointerNull>(V);
4951
}
5052

51-
enum class PointerAnalysisType {
52-
#define ANALYSIS_SETUP_POINTER_TYPE(NAME, CMDFLAG, TYPE) TYPE,
53-
#include <phasar/PhasarLLVM/Utils/AnalysisSetups.def>
54-
Invalid
55-
};
56-
57-
std::string to_string(const PointerAnalysisType &PA);
58-
59-
PointerAnalysisType to_PointerAnalysisType(const std::string &S);
60-
61-
std::ostream &operator<<(std::ostream &os, const PointerAnalysisType &PA);
62-
6353
// TODO: add a more high level description.
6454
/**
6555
* This class is a representation of a points-to graph. It is possible to
@@ -83,9 +73,9 @@ class PointsToGraph {
8373
* This might be an Instruction, an Operand of an Instruction, Global
8474
* Variable or a formal Argument.
8575
*/
86-
const llvm::Value *value = nullptr;
76+
const llvm::Value *V = nullptr;
8777
/// Holds the llvm IR code for that vertex.
88-
std::string ir_code;
78+
std::string IR;
8979

9080
VertexProperties() = default;
9181
VertexProperties(const llvm::Value *v);
@@ -96,9 +86,9 @@ class PointsToGraph {
9686
*/
9787
struct EdgeProperties {
9888
/// This might be an Instruction, in particular a Call Instruction.
99-
const llvm::Value *value = nullptr;
89+
const llvm::Value *V = nullptr;
10090
/// Holds the llvm IR code for that edge.
101-
std::string ir_code;
91+
std::string IR;
10292

10393
EdgeProperties() = default;
10494
EdgeProperties(const llvm::Value *v);
@@ -117,23 +107,22 @@ class PointsToGraph {
117107

118108
/// The type for a vertex iterator.
119109
typedef boost::graph_traits<graph_t>::vertex_iterator vertex_iterator_t;
120-
typedef boost::graph_traits<graph_t>::vertex_iterator vertex_iterator;
121-
typedef boost::graph_traits<graph_t>::out_edge_iterator out_edge_iterator;
122-
typedef boost::graph_traits<graph_t>::in_edge_iterator in_edge_iterator;
110+
typedef boost::graph_traits<graph_t>::out_edge_iterator out_edge_iterator_t;
111+
typedef boost::graph_traits<graph_t>::in_edge_iterator in_edge_iterator_t;
123112

124113
/// Set of functions that allocate heap memory, e.g. new, new[], malloc.
125114
inline const static std::set<std::string> HeapAllocationFunctions = {
126115
"_Znwm", "_Znam", "malloc", "calloc", "realloc"};
127116

128117
private:
129-
struct allocation_site_dfs_visitor;
130-
struct reachability_dfs_visitor;
118+
struct AllocationSiteDFSVisitor;
119+
struct ReachabilityDFSVisitor;
131120

132121
/// The points to graph.
133-
graph_t ptg;
134-
std::map<const llvm::Value *, vertex_t> value_vertex_map;
122+
graph_t PAG;
123+
std::unordered_map<const llvm::Value *, vertex_t> ValueVertexMap;
135124
/// Keep track of what has already been merged into this points-to graph.
136-
std::set<std::string> ContainedFunctions;
125+
std::unordered_set<std::string> ContainedFunctions;
137126

138127
public:
139128
/**
@@ -147,18 +136,7 @@ class PointsToGraph {
147136
* False, if May and Must Aliases should be
148137
* considered.
149138
*/
150-
PointsToGraph(llvm::Function *F, llvm::AAResults &AA,
151-
bool onlyConsiderMustAlias = false);
152-
153-
/**
154-
* It is used when a points-to graph is restored from the database.
155-
*
156-
* @brief This will create an empty points-to graph, except the functions
157-
* names
158-
* that are contained in the points-to graph.
159-
* @param fnames Names of functions contained in the points-to graph.
160-
*/
161-
PointsToGraph(std::vector<std::string> fnames);
139+
PointsToGraph(llvm::Function *F, llvm::AAResults &AA);
162140

163141
/**
164142
* @brief This will create an empty points-to graph. It is used when points-to

include/phasar/PhasarLLVM/Pointer/LLVMPointsToInfo.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <string>
1616

1717
#include <llvm/Analysis/AliasAnalysis.h>
18+
#include <llvm/IR/PassManager.h>
19+
#include <llvm/Passes/PassBuilder.h>
1820

1921
#include <json.hpp>
2022

@@ -31,15 +33,30 @@ namespace psr {
3133
class ProjectIRDB;
3234
class PointsToGraph;
3335

36+
enum class PointerAnalysisType {
37+
#define ANALYSIS_SETUP_POINTER_TYPE(NAME, CMDFLAG, TYPE) TYPE,
38+
#include <phasar/PhasarLLVM/Utils/AnalysisSetups.def>
39+
Invalid
40+
};
41+
42+
std::string to_string(const PointerAnalysisType &PA);
43+
44+
PointerAnalysisType to_PointerAnalysisType(const std::string &S);
45+
46+
std::ostream &operator<<(std::ostream &os, const PointerAnalysisType &PA);
47+
3448
class LLVMPointsToInfo
3549
: public PointsToInfo<const llvm::Value *, const llvm::Instruction *> {
3650
private:
37-
std::unordered_map<const llvm::Function *, llvm::AAResults> AAInfos;
51+
llvm::PassBuilder PB;
52+
llvm::FunctionAnalysisManager FAM;
53+
mutable std::unordered_map<const llvm::Function *, llvm::AAResults *> AAInfos;
3854
std::map<const llvm::Function *, std::unique_ptr<PointsToGraph>>
3955
PointsToGraphs;
4056

4157
public:
42-
LLVMPointsToInfo(ProjectIRDB &IRDB);
58+
LLVMPointsToInfo(ProjectIRDB &IRDB,
59+
PointerAnalysisType PAT = PointerAnalysisType::CFLAnders);
4360

4461
~LLVMPointsToInfo() override = default;
4562

@@ -54,6 +71,8 @@ class LLVMPointsToInfo
5471

5572
nlohmann::json getAsJson() const override;
5673

74+
llvm::AAResults *getAAResults(const llvm::Function *F) const;
75+
5776
PointsToGraph *getPointsToGraph(const llvm::Function *F) const;
5877
};
5978

0 commit comments

Comments
 (0)