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

build pag from the given pag json file #174

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
48 changes: 48 additions & 0 deletions include/MemoryModel/ICFGBuilderFromFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef INCLUDE_MEMORYMODEL_ICFGBUILDERFROMFILE_H_
#define INCLUDE_MEMORYMODEL_ICFGBUILDERFROMFILE_H_

#include "Util/ICFG.h"
#include "MemoryModel/PAG.h"
#include "llvm/Support/JSON.h"


/*!
* Build ICFG from a user specified file
*/
class ICFGBuilderFromFile{

private:
ICFG* icfg;
std::string file;
PAG pag;

public:
//Constructor
ICFGBuilderFromFile(std::string f, PAG pag){
this->pag = pag;
}

// Destructor
~ICFGBuilderFromFile() {}

// Return ICFG
ICFG* getICFG() const{
return icfg;
}

std::string getFileName() const {
return file;
}

PAG getPAG() const{
return pag;
}
//Start building
ICFG* build();

void addNode(NodeID nodeID, std::string nodeType, llvm::json::Array* pagEdges);

void addEdge(llvm::json::Object* edge_obj);
};

#endif /* INCLUDE_MEMORYMODEL_ICFGBUILDERFROMFILE_H_ */
31 changes: 26 additions & 5 deletions include/MemoryModel/PAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ class PAG : public GenericGraph<PAGNode,PAGEdge> {
NodeSet candidatePointers;
NodeID nodeNumAfterPAGBuild; // initial node number after building PAG, excluding later added nodes, e.g., gepobj nodes

/// Constructor
PAG(bool buildFromFile) : fromFile(buildFromFile), curBB(NULL),curVal(NULL), totalPTAPAGEdge(0),nodeNumAfterPAGBuild(0) {
symInfo = SymbolTableInfo::Symbolnfo();
}

/// Clean up memory
void destroy();

public:
u32_t totalPTAPAGEdge;

/// Constructor
PAG(bool buildFromFile) : fromFile(buildFromFile), curBB(NULL),curVal(NULL), totalPTAPAGEdge(0),nodeNumAfterPAGBuild(0) {
symInfo = SymbolTableInfo::Symbolnfo();
}
PAG(){}
/// Return valid pointers
inline NodeSet& getAllValidPtrs() {
return candidatePointers;
Expand Down Expand Up @@ -619,19 +619,34 @@ class PAG : public GenericGraph<PAGNode,PAGEdge> {
inline NodeID addValNode(const Value* val, PAGNode *node, NodeID i) {
return addNode(node,i);
}

inline NodeID addValNodeFromFile(const std::string val, PAGNode* node, NodeID i){
return addNode(node,i);
}
/// Add a memory obj node
inline NodeID addObjNode(const Value* val, PAGNode *node, NodeID i) {
return addNode(node,i);
}

inline NodeID addObjNodeFromFile(const std::string str_val, PAGNode* node, NodeID i){
return addNode(node,i);
}
/// Add a unique return node for a procedure
inline NodeID addRetNode(const Function* val, PAGNode *node, NodeID i) {
return addNode(node,i);
}
inline NodeID addRetNodeFromFile(const std::string str_val, PAGNode *node, NodeID i){
return addNode(node, i);
}
/// Add a unique vararg node for a procedure
inline NodeID addVarargNode(const Function* val, PAGNode *node, NodeID i) {
return addNode(node,i);
}

inline NodeID addVarargNodeFromFile(const std::string val, PAGNode * node, NodeID i){
return addNode(node,i);
}

/// Add an edge into PAG
//@{
/// Add a PAG edge
Expand Down Expand Up @@ -705,6 +720,12 @@ class PAG : public GenericGraph<PAGNode,PAGEdge> {
/// Print PAG
void print();

void printInst2JsonFile(const std::string& filename);

std::string getNodeKindValue(int kind);

std::string getEdgeKindValue(int kind);

/// Dump PAG
void dump(std::string name);

Expand Down
8 changes: 6 additions & 2 deletions include/MemoryModel/PAGBuilderFromFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define INCLUDE_MEMORYMODEL_PAGBUILDERFROMFILE_H_

#include "MemoryModel/PAG.h"
#include "Util/ICFG.h"


/*!
Expand Down Expand Up @@ -60,8 +61,11 @@ class PAGBuilderFromFile {
return file;
}

/// Start building
PAG* build();
//start building pag from ICFG json file
PAG* buildFromICFG();

//add pag nodes
void addNode(NodeID s_id, std::string node_type,const char* str_val);

// Add edges
void addEdge(NodeID nodeSrc, NodeID nodeDst, Size_t offset,
Expand Down
27 changes: 26 additions & 1 deletion include/MemoryModel/PAGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ class PAGNode : public GenericPAGNodeTy {
PAGEdge::PAGKindToEdgeSetMapTy OutEdgeKindToSetMap;
bool isTLPointer; /// top-level pointer
bool isATPointer; /// address-taken pointer

const char* str_value;
public:
/// Constructor
PAGNode(NodeID i,PNODEK k,const char* str_val);
PAGNode(const Value* val, NodeID i, PNODEK k);
/// Destructor
virtual ~PAGNode() {
}


/// Get/has methods of the components
//@{
inline const Value* getValue() const {
Expand Down Expand Up @@ -273,6 +275,10 @@ class ValPN: public PAGNode {
ValPN(const Value* val, NodeID i, PNODEK ty = ValNode) :
PAGNode(val, i, ty) {
}
ValPN(NodeID i, const char* str_val, PNODEK ty = ValNode):
PAGNode(i, ty, str_val ){

}
/// Return name of a LLVM value
inline const std::string getValueName() const {
if (value && value->hasName())
Expand All @@ -294,6 +300,9 @@ class ObjPN: public PAGNode {
PAGNode(val, i, ty), mem(m) {
}
public:
ObjPN( NodeID i, const char* str_val, const MemObj* m, PNODEK ty = ObjNode) :
PAGNode(i, ty, str_val ), mem(m) {
}
/// Methods for support type inquiry through isa, cast, and dyn_cast:
//@{
static inline bool classof(const ObjPN *) {
Expand Down Expand Up @@ -365,6 +374,10 @@ class GepValPN: public ValPN {
ValPN(val, i, GepValNode), ls(l), gepValType(ty), fieldIdx(idx) {
}

GepValPN(const char* str_val, NodeID i, const LocationSet& l, const Type *ty, u32_t idx) :
ValPN(i, str_val, GepValNode), ls(l), gepValType(ty), fieldIdx(idx) {
}

/// offset of the base value node
inline u32_t getOffset() const {
return ls.getOffset();
Expand Down Expand Up @@ -463,6 +476,10 @@ class FIObjPN: public ObjPN {
ObjPN(val, i, mem, FIObjNode) {
}

FIObjPN(const char* str_val, NodeID i, const MemObj* mem) :
ObjPN(i,str_val, mem, FIObjNode) {
}

/// Return name of a LLVM value
inline const std::string getValueName() const {
if (value && value->hasName())
Expand Down Expand Up @@ -495,6 +512,10 @@ class RetPN: public PAGNode {
PAGNode(val, i, RetNode) {
}

RetPN(const char* str_val, NodeID i, PNODEK ty=RetNode) :
PAGNode( i, RetNode, str_val) {
}

/// Return name of a LLVM value
const std::string getValueName() const {
const Function* fun = SVFUtil::cast<Function>(value);
Expand Down Expand Up @@ -527,6 +548,10 @@ class VarArgPN: public PAGNode {
PAGNode(val, i, VarargNode) {
}

VarArgPN(NodeID i,const char* str_val,PNODEK ty=VarargNode) :
PAGNode( i, VarargNode, str_val) {
}

/// Return name of a LLVM value
inline const std::string getValueName() const {
const Function* fun = SVFUtil::cast<Function>(value);
Expand Down
16 changes: 9 additions & 7 deletions include/Util/ICFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ICFG : public GenericICFGTy {
public:
/// Constructor
ICFG(PTACallGraph* callgraph);
ICFG();

/// Destructor
virtual ~ICFG() {
Expand Down Expand Up @@ -104,6 +105,14 @@ class ICFG : public GenericICFGTy {
ICFGEdge* hasThreadICFGEdge(ICFGNode* src, ICFGNode* dst, ICFGEdge::ICFGEdgeK kind);
//@}


/// Add control-flow edges for top level pointers
//@{
ICFGEdge* addIntraEdge(ICFGNode* srcNode, ICFGNode* dstNode);
ICFGEdge* addCallEdge(ICFGNode* srcNode, ICFGNode* dstNode, CallSiteID csId);
ICFGEdge* addRetEdge(ICFGNode* srcNode, ICFGNode* dstNode, CallSiteID csId);
//@}

/// Get a SVFG edge according to src and dst
ICFGEdge* getICFGEdge(const ICFGNode* src, const ICFGNode* dst, ICFGEdge::ICFGEdgeK kind);

Expand Down Expand Up @@ -135,13 +144,6 @@ class ICFG : public GenericICFGTy {
removeGNode(node);
}

/// Add control-flow edges for top level pointers
//@{
ICFGEdge* addIntraEdge(ICFGNode* srcNode, ICFGNode* dstNode);
ICFGEdge* addCallEdge(ICFGNode* srcNode, ICFGNode* dstNode, CallSiteID csId);
ICFGEdge* addRetEdge(ICFGNode* srcNode, ICFGNode* dstNode, CallSiteID csId);
//@}

/// sanitize Intra edges, verify that both nodes belong to the same function.
inline void checkIntraEdgeParents(const ICFGNode *srcNode, const ICFGNode *dstNode) {
const BasicBlock *srcBB = srcNode->getBB();
Expand Down
25 changes: 25 additions & 0 deletions include/Util/ICFGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,24 @@ class IntraBlockNode : public ICFGNode {

private:
const Instruction *inst;
const std::string* str_value;
StmtOrPHIVec vnodes;
public:
IntraBlockNode(NodeID id, const Instruction *i) : ICFGNode(id, IntraBlock), inst(i) {
bb = inst->getParent();
}

IntraBlockNode(NodeID id, const std::string *i) : ICFGNode(id, IntraBlock), str_value(i) {
}

inline const Instruction *getInst() const {
return inst;
}

inline const std::string *getInst_str_value() const {
return str_value;
}

inline void addPAGEdge(const PAGEdge *s) {
// avoid duplicate element
for(StmtOrPHIVec::const_iterator it = vnodes.begin(), eit = vnodes.end(); it!=eit; ++it)
Expand Down Expand Up @@ -176,18 +184,26 @@ class FunEntryBlockNode : public InterBlockNode {
typedef std::vector<const PAGNode *> FormalParmNodeVec;
private:
const Function *fun;
const std::string* str_fun;
FormalParmNodeVec FPNodes;
public:
FunEntryBlockNode(NodeID id, const Function *f) : InterBlockNode(id, FunEntryBlock), fun(f) {
if (!SVFUtil::isExtCall(fun))
bb = &(fun->getEntryBlock());
}

FunEntryBlockNode(NodeID id, const std::string *f) : InterBlockNode(id, FunEntryBlock), str_fun(f) {
}

/// Return function
inline const Function *getFun() const {
return fun;
}

inline const std::string *getStr_fun() const {
return str_fun;
}

/// Return the set of formal parameters
inline const FormalParmNodeVec &getFormalParms() const {
return FPNodes;
Expand Down Expand Up @@ -224,6 +240,7 @@ class FunEntryBlockNode : public InterBlockNode {
class FunExitBlockNode : public InterBlockNode {

private:
const std::string *str_fun;
const Function *fun;
const PAGNode *formalRet;
public:
Expand All @@ -232,11 +249,19 @@ class FunExitBlockNode : public InterBlockNode {
bb = SVFUtil::getFunExitBB(fun);
}

FunExitBlockNode(NodeID id, const std::string *f) : InterBlockNode(id, FunExitBlock), str_fun(f), formalRet(NULL) {
}

/// Return function
inline const Function *getFun() const {
return fun;
}


inline const std::string *getStrFun() const {
return str_fun;
}

/// Return actual return parameter
inline const PAGNode *getFormalRet() const {
return formalRet;
Expand Down
Loading