Static Value-Flow Analysis
Loading...
Searching...
No Matches
SVF::SaberCondAllocator Class Reference

#include <SaberCondAllocator.h>

Public Types

typedef Z3Expr Condition
typedef Map< u32_t, const ICFGNode * > IndexToTermInstMap
 z3 condition
typedef Map< u32_t, Condition > CondPosMap
 id to instruction map for z3
typedef Map< const SVFBasicBlock *, CondPosMap > BBCondMap
typedef Set< const SVFBasicBlock * > BasicBlockSet
 map bb to a Condition
typedef Map< const FunObjVar *, BasicBlockSet > FunToExitBBsMap
 map a function to all its basic blocks calling program exit
typedef Map< const SVFBasicBlock *, Condition > BBToCondMap
 map a basic block to its condition during control-flow guard computation
typedef FIFOWorkList< const SVFBasicBlock * > CFWorkList
 worklist for control-flow guard computation
typedef Map< const SVFGNode *, Set< const SVFGNode * > > SVFGNodeToSVFGNodeSetMap

Public Member Functions

 SaberCondAllocator ()
 Constructor.
virtual ~SaberCondAllocator ()
 Destructor.
std::string getMemUsage ()
 Statistics.
u32_t getCondNum ()
Condition condAnd (const Condition &lhs, const Condition &rhs)
 Condition operations.
Condition condOr (const Condition &lhs, const Condition &rhs)
Condition condNeg (const Condition &cond)
Condition getTrueCond () const
Condition getFalseCond () const
NodeBS exactCondElem (const Condition &cond)
 Iterator every element of the condition.
std::string dumpCond (const Condition &cond) const
Condition newCond (const ICFGNode *inst)
 Allocate a new condition.
void allocate ()
 Perform path allocation.
const ICFGNode * getCondInst (u32_t id) const
 Get/Set instruction based on Z3 expression id.
void setCondInst (const Condition &condition, const ICFGNode *inst)
bool isNegCond (u32_t id) const
bool postDominate (const SVFBasicBlock *bbKey, const SVFBasicBlock *bbValue) const
bool dominate (const SVFBasicBlock *bbKey, const SVFBasicBlock *bbValue) const
virtual Condition ComputeIntraVFGGuard (const SVFBasicBlock *src, const SVFBasicBlock *dst)
 Guard Computation for a value-flow (between two basic blocks).
virtual Condition ComputeInterCallVFGGuard (const SVFBasicBlock *src, const SVFBasicBlock *dst, const SVFBasicBlock *callBB)
virtual Condition ComputeInterRetVFGGuard (const SVFBasicBlock *src, const SVFBasicBlock *dst, const SVFBasicBlock *retBB)
virtual Condition getPHIComplementCond (const SVFBasicBlock *BB1, const SVFBasicBlock *BB2, const SVFBasicBlock *BB0)
void clearCFCond ()
void setCurEvalSVFGNode (const SVFGNode *node)
 Set current value for branch condition evaluation.
const SVFGNode * getCurEvalSVFGNode () const
 Get current value for branch condition evaluation.
void printPathCond ()
 Print out the path condition information.
bool isSatisfiable (const Condition &condition)
 whether condition is satisfiable
bool isAllPathReachable (Condition &condition)
 whether condition is satisfiable for all possible boolean guards
bool isEquivalentBranchCond (const Condition &lhs, const Condition &rhs) const
 Whether lhs and rhs are equivalent branch conditions.
ICFG * getICFG () const
bool setCFCond (const SVFBasicBlock *bb, const Condition &cond)
 Get/Set control-flow conditions.
Condition getCFCond (const SVFBasicBlock *bb) const
void setNegCondInst (const Condition &condition, const ICFGNode *inst)
 mark neg Z3 expression
SVFGNodeToSVFGNodeSetMap & getRemovedSUVFEdges ()

Protected Attributes

BBCondMap bbConds
 map basic block to its successors/predecessors branch conditions

Private Member Functions

virtual void allocateForBB (const SVFBasicBlock &bb)
 Allocate path condition for every basic block.
void setBranchCond (const SVFBasicBlock *bb, const SVFBasicBlock *succ, const Condition &cond)
 Get/Set a branch condition, and its terminator instruction.
Condition getBranchCond (const SVFBasicBlock *bb, const SVFBasicBlock *succ) const
 Get branch condition.
Condition getEvalBrCond (const SVFBasicBlock *bb, const SVFBasicBlock *succ)
 Get a condition, evaluate the value for conditions if necessary (e.g., testNull like express).
Condition evaluateBranchCond (const SVFBasicBlock *bb, const SVFBasicBlock *succ)
 Evaluate branch conditions.
Condition evaluateLoopExitBranch (const SVFBasicBlock *bb, const SVFBasicBlock *succ)
 Evaluate loop exit branch.
Condition evaluateTestNullLikeExpr (const BranchStmt *branchStmt, const SVFBasicBlock *succ)
 Return branch condition after evaluating test null like expression.
Condition evaluateProgExit (const BranchStmt *branchStmt, const SVFBasicBlock *succ)
 Return condition when there is a branch calls program exit.
void collectBBCallingProgExit (const SVFBasicBlock &bb)
 Collect basic block contains program exit function call.
bool isBBCallsProgExit (const SVFBasicBlock *bb)
bool isEQCmp (const CmpStmt *cmp) const
 Evaluate test null/not null like expressions.
bool isNECmp (const CmpStmt *cmp) const
 Return true if the predicate of this compare instruction is not equal.
bool isTestNullExpr (const ICFGNode *test) const
 Return true if this is a test null expression.
bool isTestNotNullExpr (const ICFGNode *test) const
 Return true if this is a test not null expression.
bool isTestContainsNullAndTheValue (const CmpStmt *cmp) const
 Return true if two values on the predicate are what we want.
void destroy ()
 Release memory.
void extractSubConds (const Condition &condition, NodeBS &support) const
 extract subexpression from a Z3 expression

Private Attributes

FunToExitBBsMap funToExitBBsMap
 map a function to all its basic blocks calling program exit
BBToCondMap bbToCondMap
 map a basic block to its path condition starting from root
const SVFGNode * curEvalSVFGNode {}
 current llvm value to evaluate branch condition when computing guards
IndexToTermInstMap idToTermInstMap
NodeBS negConds
 key: z3 expression id, value: instruction
std::vector< Condition > conditionVec
 bit vector for distinguish neg
SVFGNodeToSVFGNodeSetMap removedSUVFEdges
 a counter for fresh condition

Static Private Attributes

static u32_t totalCondNum = 0
 vector storing z3expression

Detailed Description

SaberCondAllocator allocates conditions for each basic block of a certain CFG.

Definition at line 45 of file SaberCondAllocator.h.

Member Typedef Documentation

◆ BasicBlockSet

map bb to a Condition

Definition at line 54 of file SaberCondAllocator.h.

◆ BBCondMap

◆ BBToCondMap

map a basic block to its condition during control-flow guard computation

Definition at line 56 of file SaberCondAllocator.h.

◆ CFWorkList

worklist for control-flow guard computation

Definition at line 57 of file SaberCondAllocator.h.

◆ Condition

◆ CondPosMap

id to instruction map for z3

map a branch to its Condition

Definition at line 52 of file SaberCondAllocator.h.

◆ FunToExitBBsMap

map a function to all its basic blocks calling program exit

Definition at line 55 of file SaberCondAllocator.h.

◆ IndexToTermInstMap

z3 condition

Definition at line 51 of file SaberCondAllocator.h.

◆ SVFGNodeToSVFGNodeSetMap

Definition at line 58 of file SaberCondAllocator.h.

Constructor & Destructor Documentation

◆ SaberCondAllocator()

SaberCondAllocator::SaberCondAllocator ( )

Constructor.

Definition at line 51 of file SaberCondAllocator.cpp.

52{
53
54}

◆ ~SaberCondAllocator()

virtual SVF::SaberCondAllocator::~SaberCondAllocator ( )
inlinevirtual

Destructor.

Definition at line 65 of file SaberCondAllocator.h.

66 {
67 }

Member Function Documentation

◆ allocate()

void SaberCondAllocator::allocate ( )

Perform path allocation.

Allocate path condition for each branch

Definition at line 59 of file SaberCondAllocator.cpp.

60{
61 DBOUT(DGENERAL, outs() << pasMsg("path condition allocation starts\n"));
62
63 for (const auto& item: *(PAG::getPAG()->getCallGraph()))
64 {
65 const FunObjVar *func = (item.second)->getFunction();
66 if (!SVFUtil::isExtCall(func))
67 {
68 // Allocate conditions for a program.
69 for (FunObjVar::const_bb_iterator bit = func->begin(), ebit = func->end();
70 bit != ebit; ++bit)
71 {
72 const SVFBasicBlock* bb = bit->second;
74 allocateForBB(*bb);
75 }
76 }
77 }
78
81
82 DBOUT(DGENERAL, outs() << pasMsg("path condition allocation ends\n"));
83}
#define DBOUT(TYPE, X)
LLVM debug macros, define type of your DBUG model of each pass.
Definition SVFType.h:576
#define DGENERAL
Definition SVFType.h:582
cJSON * item
Definition cJSON.h:222
BasicBlockGraph::IDToNodeMapTy::const_iterator const_bb_iterator
const_bb_iterator begin() const
const_bb_iterator end() const
static const Option< bool > PrintPathCond
Definition Options.h:191
static PAG * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition PAG.h:121
virtual void allocateForBB(const SVFBasicBlock &bb)
Allocate path condition for every basic block.
void printPathCond()
Print out the path condition information.
void collectBBCallingProgExit(const SVFBasicBlock &bb)
Collect basic block contains program exit function call.
std::string pasMsg(const std::string &msg)
Print each pass/phase message by converting a string into blue string output.
Definition SVFUtil.cpp:121
bool isExtCall(const FunObjVar *fun)
Definition SVFUtil.cpp:524
std::ostream & outs()
Overwrite llvm::outs().
Definition SVFUtil.h:58

◆ allocateForBB()

void SaberCondAllocator::allocateForBB ( const SVFBasicBlock & bb)
privatevirtual

Allocate path condition for every basic block.

Allocate conditions for a basic block and propagate its condition to its successors.

TODO: handle BranchInst and SwitchInst individually here!!

Definition at line 88 of file SaberCondAllocator.cpp.

89{
90
91 u32_t succ_number = bb.getNumSuccessors();
92
93 // if successor number greater than 1, allocate new decision variable for successors
94 if (succ_number > 1)
95 {
96
97 //allocate log2(num_succ) decision variables
98 double num = log(succ_number) / log(2);
99 u32_t bit_num = (u32_t) ceil(num);
100 u32_t succ_index = 0;
101 std::vector<Condition> condVec;
102 for (u32_t i = 0; i < bit_num; i++)
103 {
104 condVec.push_back(newCond(bb.back()));
105 }
106
107 // iterate each successor
108 for (const SVFBasicBlock* svf_succ_bb : bb.getSuccessors())
109 {
110 Condition path_cond = getTrueCond();
111
113
114 // for each successor decide its bit representation
115 // decide whether each bit of succ_index is 1 or 0, if (three successor) succ_index is 000 then use C1^C2^C3
116 // if 001 use C1^C2^negC3
117 for (u32_t j = 0; j < bit_num; j++)
118 {
119 //test each bit of this successor's index (binary representation)
120 u32_t tool = 0x01 << j;
121 if (tool & succ_index)
122 {
123 path_cond = condAnd(path_cond, (condNeg(condVec.at(j))));
124 }
125 else
126 {
127 path_cond = condAnd(path_cond, condVec.at(j));
128 }
129 }
130 setBranchCond(&bb, svf_succ_bb, path_cond);
131
132 succ_index++;
133 }
134
135 }
136}
u32_t getNumSuccessors() const
const ICFGNode * back() const
std::vector< const SVFBasicBlock * > getSuccessors() const
Condition newCond(const ICFGNode *inst)
Allocate a new condition.
void setBranchCond(const SVFBasicBlock *bb, const SVFBasicBlock *succ, const Condition &cond)
Get/Set a branch condition, and its terminator instruction.
Condition condNeg(const Condition &cond)
Condition condAnd(const Condition &lhs, const Condition &rhs)
Condition operations.
Condition getTrueCond() const
unsigned u32_t
Definition GeneralType.h:67

◆ clearCFCond()

void SVF::SaberCondAllocator::clearCFCond ( )
inline

Definition at line 176 of file SaberCondAllocator.h.

177 {
178 bbToCondMap.clear();
179 }
BBToCondMap bbToCondMap
map a basic block to its path condition starting from root

◆ collectBBCallingProgExit()

void SaberCondAllocator::collectBBCallingProgExit ( const SVFBasicBlock & bb)
private

Collect basic block contains program exit function call.

Whether this basic block contains program exit function call

Definition at line 430 of file SaberCondAllocator.cpp.

431{
432
433 for (const auto& icfgNode: bb.getICFGNodeList())
434 {
435 if (const CallICFGNode* cs = SVFUtil::dyn_cast<CallICFGNode>(icfgNode))
437 {
438 const FunObjVar* svfun = bb.getParent();
439 funToExitBBsMap[svfun].insert(&bb);
440 }
441 }
442}
const FunObjVar * getParent() const
const std::vector< const ICFGNode * > & getICFGNodeList() const
FunToExitBBsMap funToExitBBsMap
map a function to all its basic blocks calling program exit
bool isProgExitCall(const CallICFGNode *cs)
Definition SVFUtil.cpp:480
LLVM_NODISCARD std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
Definition Casting.h:405

◆ ComputeInterCallVFGGuard()

SaberCondAllocator::Condition SaberCondAllocator::ComputeInterCallVFGGuard ( const SVFBasicBlock * srcBB,
const SVFBasicBlock * dstBB,
const SVFBasicBlock * callBB )
virtual

Compute calling inter-procedural guards between two SVFGNodes (from caller to callee) src –c1--> callBB –true--> funEntryBB –c2--> dst the InterCallVFGGuard is c1 ^ c2

Definition at line 489 of file SaberCondAllocator.cpp.

491{
492 const SVFBasicBlock* funEntryBB = dstBB->getParent()->getEntryBlock();
493
494 Condition c1 = ComputeIntraVFGGuard(srcBB, callBB);
495 setCFCond(funEntryBB, condOr(getCFCond(funEntryBB), getCFCond(callBB)));
496 Condition c2 = ComputeIntraVFGGuard(funEntryBB, dstBB);
497 return condAnd(c1, c2);
498}
const SVFBasicBlock * getEntryBlock() const
Condition condOr(const Condition &lhs, const Condition &rhs)
bool setCFCond(const SVFBasicBlock *bb, const Condition &cond)
Get/Set control-flow conditions.
Condition getCFCond(const SVFBasicBlock *bb) const
virtual Condition ComputeIntraVFGGuard(const SVFBasicBlock *src, const SVFBasicBlock *dst)
Guard Computation for a value-flow (between two basic blocks).

◆ ComputeInterRetVFGGuard()

SaberCondAllocator::Condition SaberCondAllocator::ComputeInterRetVFGGuard ( const SVFBasicBlock * srcBB,
const SVFBasicBlock * dstBB,
const SVFBasicBlock * retBB )
virtual

Compute return inter-procedural guards between two SVFGNodes (from callee to caller) src –c1--> funExitBB –true--> retBB –c2--> dst the InterRetVFGGuard is c1 ^ c2

Definition at line 506 of file SaberCondAllocator.cpp.

507{
508 const FunObjVar* parent = srcBB->getParent();
509 const SVFBasicBlock* funExitBB = parent->getExitBB();
510
511 Condition c1 = ComputeIntraVFGGuard(srcBB, funExitBB);
512 setCFCond(retBB, condOr(getCFCond(retBB), getCFCond(funExitBB)));
513 Condition c2 = ComputeIntraVFGGuard(retBB, dstBB);
514 return condAnd(c1, c2);
515}
const SVFBasicBlock * getExitBB() const

◆ ComputeIntraVFGGuard()

SaberCondAllocator::Condition SaberCondAllocator::ComputeIntraVFGGuard ( const SVFBasicBlock * srcBB,
const SVFBasicBlock * dstBB )
virtual

Guard Computation for a value-flow (between two basic blocks).

Compute intra-procedural guards between two SVFGNodes (inside same function)

if the dstBB is the eligible loop exit of the current basic block we can early terminate the computation

calculate the branch condition if succ post dominate bb, then we get brCond quicker by using postDT note that we assume loop exit always post dominate loop bodys which means loops are approximated only once.

Definition at line 520 of file SaberCondAllocator.cpp.

521{
522
523 assert(srcBB->getParent() == dstBB->getParent() && "two basic blocks are not in the same function??");
524
525 if (postDominate(dstBB, srcBB))
526 return getTrueCond();
527
528 CFWorkList worklist;
529 worklist.push(srcBB);
530 setCFCond(srcBB, getTrueCond());
531
532 while (!worklist.empty())
533 {
534 const SVFBasicBlock* bb = worklist.pop();
535 Condition cond = getCFCond(bb);
536
539 Condition loopExitCond = evaluateLoopExitBranch(bb, dstBB);
540 if (!eq(loopExitCond, Condition::nullExpr()))
541 return condAnd(cond, loopExitCond);
542
543 for (const SVFBasicBlock* succ : bb->getSuccessors())
544 {
549 Condition brCond;
550 if (postDominate(succ, bb))
551 brCond = getTrueCond();
552 else
553 brCond = getEvalBrCond(bb, succ);
554
555 DBOUT(DSaber, outs() << " bb (" << bb->getName() <<
556 ") --> " << "succ_bb (" << succ->getName() << ") condition: " << brCond << "\n");
557 Condition succPathCond = condAnd(cond, brCond);
558 if (setCFCond(succ, condOr(getCFCond(succ), succPathCond)))
559 worklist.push(succ);
560 }
561 }
562
563 DBOUT(DSaber, outs() << " src_bb (" << srcBB->getName() <<
564 ") --> " << "dst_bb (" << dstBB->getName() << ") condition: " << getCFCond(dstBB)
565 << "\n");
566
567 return getCFCond(dstBB);
568}
#define DSaber
Definition SVFType.h:596
virtual const std::string & getName() const
Definition SVFValue.h:184
bool postDominate(const SVFBasicBlock *bbKey, const SVFBasicBlock *bbValue) const
Condition evaluateLoopExitBranch(const SVFBasicBlock *bb, const SVFBasicBlock *succ)
Evaluate loop exit branch.
FIFOWorkList< const SVFBasicBlock * > CFWorkList
worklist for control-flow guard computation
Condition getEvalBrCond(const SVFBasicBlock *bb, const SVFBasicBlock *succ)
Get a condition, evaluate the value for conditions if necessary (e.g., testNull like express).
static z3::expr nullExpr()
null expression
Definition Z3Expr.h:105

◆ condAnd()

Condition SVF::SaberCondAllocator::condAnd ( const Condition & lhs,
const Condition & rhs )
inline

Condition operations.

Definition at line 86 of file SaberCondAllocator.h.

87 {
88 return Condition::AND(lhs,rhs);
89 }
static Z3Expr AND(const Z3Expr &lhs, const Z3Expr &rhs)
compute AND, used for branch condition
Definition Z3Expr.cpp:92

◆ condNeg()

Condition SVF::SaberCondAllocator::condNeg ( const Condition & cond)
inline

Definition at line 94 of file SaberCondAllocator.h.

95 {
96 return Condition::NEG(cond);
97 }
static Z3Expr NEG(const Z3Expr &z3Expr)
compute NEG
Definition Z3Expr.h:301

◆ condOr()

Condition SVF::SaberCondAllocator::condOr ( const Condition & lhs,
const Condition & rhs )
inline

Definition at line 90 of file SaberCondAllocator.h.

91 {
92 return Condition::OR(lhs,rhs);
93 }
static Z3Expr OR(const Z3Expr &lhs, const Z3Expr &rhs)
compute OR, used for branch condition
Definition Z3Expr.cpp:130

◆ destroy()

void SVF::SaberCondAllocator::destroy ( )
inlineprivate

Release memory.

Definition at line 292 of file SaberCondAllocator.h.

293 {
294
295 }

◆ dominate()

bool SVF::SaberCondAllocator::dominate ( const SVFBasicBlock * bbKey,
const SVFBasicBlock * bbValue ) const
inline

Definition at line 156 of file SaberCondAllocator.h.

157 {
158 const FunObjVar* keyFunc = bbKey->getParent();
159 const FunObjVar* valueFunc = bbValue->getParent();
160 bool funcEq = (keyFunc == valueFunc);
161 (void)funcEq; // Suppress warning of unused variable under release build
162 assert(funcEq && "two basicblocks should be in the same function!");
163 return keyFunc->dominate(bbKey,bbValue);
164 }

◆ dumpCond()

std::string SVF::SaberCondAllocator::dumpCond ( const Condition & cond) const
inline

Definition at line 114 of file SaberCondAllocator.h.

115 {
116 return Condition::dumpStr(cond);
117 }
static std::string dumpStr(const Z3Expr &z3Expr)
output Z3 expression as a string
Definition Z3Expr.cpp:168

◆ evaluateBranchCond()

SaberCondAllocator::Condition SaberCondAllocator::evaluateBranchCond ( const SVFBasicBlock * bb,
const SVFBasicBlock * succ )
private

Evaluate branch conditions.

Evaluate the branch condition

(1) Evaluate a branch when it reaches a program exit (2) Evaluate a branch when it is loop exit branch (3) Evaluate a branch when it is a test null like condition

Definition at line 301 of file SaberCondAllocator.cpp.

302{
303 if(bb->getNumSuccessors() == 1)
304 {
305 return getTrueCond();
306 }
307
308 assert(!bb->getICFGNodeList().empty() && "bb not empty");
309 if (const ICFGNode* icfgNode = bb->back())
310 {
311 for (const auto &svfStmt: icfgNode->getSVFStmts())
312 {
313 if (const BranchStmt *branchStmt = SVFUtil::dyn_cast<BranchStmt>(svfStmt))
314 {
315 if (branchStmt->getNumSuccessors() == 2)
316 {
317 const SVFBasicBlock* succ1 = branchStmt->getSuccessor(0)->getBB();
318 const SVFBasicBlock* succ2 = branchStmt->getSuccessor(1)->getBB();
319 bool is_succ = (succ1 == succ || succ2 == succ);
320 (void)is_succ; // Suppress warning of unused variable under release build
321 assert(is_succ && "not a successor??");
322 Condition evalLoopExit = evaluateLoopExitBranch(bb, succ);
323 if (!eq(evalLoopExit, Condition::nullExpr()))
324 return evalLoopExit;
325
326 Condition evalProgExit = evaluateProgExit(branchStmt, succ);
327 if (!eq(evalProgExit, Condition::nullExpr()))
328 return evalProgExit;
329
330 Condition evalTestNullLike = evaluateTestNullLikeExpr(branchStmt, succ);
331 if (!eq(evalTestNullLike, Condition::nullExpr()))
332 return evalTestNullLike;
333 break;
334 }
335 }
336 }
337 }
338
339 return getBranchCond(bb, succ);
340}
Condition evaluateTestNullLikeExpr(const BranchStmt *branchStmt, const SVFBasicBlock *succ)
Return branch condition after evaluating test null like expression.
Condition evaluateProgExit(const BranchStmt *branchStmt, const SVFBasicBlock *succ)
Return condition when there is a branch calls program exit.
Condition getBranchCond(const SVFBasicBlock *bb, const SVFBasicBlock *succ) const
Get branch condition.

◆ evaluateLoopExitBranch()

SaberCondAllocator::Condition SaberCondAllocator::evaluateLoopExitBranch ( const SVFBasicBlock * bb,
const SVFBasicBlock * dst )
private

Evaluate loop exit branch.

Evaluate loop exit branch to be true if bb is loop header and succ is the only exit basic block outside the loop (excluding exit bbs which call program exit) for all other case, we conservatively evaluate false for now

exclude exit bb which calls program exit

if the dst dominate all other loop exit bbs, then dst can certainly be reached

Definition at line 265 of file SaberCondAllocator.cpp.

266{
267 const FunObjVar* svffun = bb->getParent();
268 assert(svffun == dst->getParent() && "two basic blocks should be in the same function");
269
270 if (svffun->isLoopHeader(bb))
271 {
272 Set<const SVFBasicBlock* > filteredbbs;
273 std::vector<const SVFBasicBlock*> exitbbs;
274 svffun->getExitBlocksOfLoop(bb,exitbbs);
276 for(const SVFBasicBlock* eb : exitbbs)
277 {
278 if(!isBBCallsProgExit(eb))
279 filteredbbs.insert(eb);
280 }
281
283 bool allPDT = true;
284 for (const auto &filteredbb: filteredbbs)
285 {
286 if (!postDominate(dst, filteredbb))
287 allPDT = false;
288 }
289
290 if (allPDT)
291 return getTrueCond();
292 }
293 return Condition::nullExpr();
294}
void getExitBlocksOfLoop(const SVFBasicBlock *bb, BBList &exitbbs) const
bool isLoopHeader(const SVFBasicBlock *bb) const
bool isBBCallsProgExit(const SVFBasicBlock *bb)
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition GeneralType.h:51

◆ evaluateProgExit()

SaberCondAllocator::Condition SaberCondAllocator::evaluateProgExit ( const BranchStmt * branchStmt,
const SVFBasicBlock * succ )
private

Return condition when there is a branch calls program exit.

Evaluate condition for program exit (e.g., exit(0))

then branch calls program exit

else branch calls program exit

no branch call program exit

Definition at line 221 of file SaberCondAllocator.cpp.

222{
223 const SVFBasicBlock* succ1 = branchStmt->getSuccessor(0)->getBB();
224 const SVFBasicBlock* succ2 = branchStmt->getSuccessor(1)->getBB();
225
226 bool branch1 = isBBCallsProgExit(succ1);
227 bool branch2 = isBBCallsProgExit(succ2);
228
230 if (branch1 && !branch2)
231 {
232 // succ is then branch
233 if (succ1 == succ)
234 return getFalseCond();
235 // succ is else branch
236 else
237 return getTrueCond();
238 }
240 else if (!branch1 && branch2)
241 {
242 // succ is else branch
243 if (succ2 == succ)
244 return getFalseCond();
245 // succ is then branch
246 else
247 return getTrueCond();
248 }
249 // two branches both call program exit
250 else if (branch1 && branch2)
251 {
252 return getFalseCond();
253 }
255 else
256 return Condition::nullExpr();
257
258}
const ICFGNode * getSuccessor(u32_t i) const
virtual const SVFBasicBlock * getBB() const
Return the basic block of this ICFGNode.
Definition ICFGNode.h:81
Condition getFalseCond() const

◆ evaluateTestNullLikeExpr()

SaberCondAllocator::Condition SaberCondAllocator::evaluateTestNullLikeExpr ( const BranchStmt * branchStmt,
const SVFBasicBlock * succ )
private

Return branch condition after evaluating test null like expression.

Evaluate null like expression for source-sink related bug detection in SABER

Definition at line 185 of file SaberCondAllocator.cpp.

186{
187
188 const SVFBasicBlock* succ1 = branchStmt->getSuccessor(0)->getBB();
189
190 const ValVar* condVar = SVFUtil::cast<ValVar>(branchStmt->getCondition());
192 {
193 // branch condition is a constant value, return nullexpr because it cannot be test null
194 // br i1 false, label %44, label %75, !dbg !7669 { "ln": 2033, "cl": 7, "fl": "re_lexer.c" }
195 return Condition::nullExpr();
196 }
198 {
199 // succ is then branch
200 if (succ1 == succ)
201 return getFalseCond();
202 // succ is else branch
203 else
204 return getTrueCond();
205 }
206 if (isTestNotNullExpr(condVar->getICFGNode()))
207 {
208 // succ is then branch
209 if (succ1 == succ)
210 return getTrueCond();
211 // succ is else branch
212 else
213 return getFalseCond();
214 }
215 return Condition::nullExpr();
216}
const ValVar * getCondition() const
Return the condition.
virtual bool isConstDataOrAggDataButNotNullPtr() const
Check if this variable represents constant data/metadata but not null pointer.
bool isTestNullExpr(const ICFGNode *test) const
Return true if this is a test null expression.
bool isTestNotNullExpr(const ICFGNode *test) const
Return true if this is a test not null expression.
const ICFGNode * getICFGNode() const
std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > cast(const Y &Val)
Definition Casting.h:360

◆ exactCondElem()

NodeBS SVF::SaberCondAllocator::exactCondElem ( const Condition & cond)
inline

Iterator every element of the condition.

Definition at line 107 of file SaberCondAllocator.h.

108 {
109 NodeBS elems;
110 extractSubConds(cond, elems);
111 return elems;
112 }
void extractSubConds(const Condition &condition, NodeBS &support) const
extract subexpression from a Z3 expression
SparseBitVector NodeBS
Definition GeneralType.h:82

◆ extractSubConds()

void SaberCondAllocator::extractSubConds ( const Condition & condition,
NodeBS & support ) const
private

extract subexpression from a Z3 expression

Definition at line 638 of file SaberCondAllocator.cpp.

639{
640 if (condition.getExpr().num_args() == 1 && isNegCond(condition.id()))
641 {
642 support.set(condition.getExpr().id());
643 return;
644 }
645 if (condition.getExpr().num_args() == 0)
646 if (!condition.getExpr().is_true() && !condition.getExpr().is_false())
647 support.set(condition.getExpr().id());
648 for (u32_t i = 0; i < condition.getExpr().num_args(); ++i)
649 {
650 Condition expr = condition.getExpr().arg(i);
651 extractSubConds(expr, support);
652 }
653
654}
bool isNegCond(u32_t id) const
void set(unsigned Idx)

◆ getBranchCond()

SaberCondAllocator::Condition SaberCondAllocator::getBranchCond ( const SVFBasicBlock * bb,
const SVFBasicBlock * succ ) const
private

Get branch condition.

Get a branch condition

Definition at line 141 of file SaberCondAllocator.cpp.

142{
143 u32_t pos = bb->getBBSuccessorPos(succ);
144 if(bb->getNumSuccessors() == 1)
145 return getTrueCond();
146 else
147 {
148 BBCondMap::const_iterator it = bbConds.find(bb);
149 assert(it != bbConds.end() && "basic block does not have branch and conditions??");
150 CondPosMap::const_iterator cit = it->second.find(pos);
151 assert(cit != it->second.end() && "no condition on the branch??");
152 return cit->second;
153 }
154}
u32_t getBBSuccessorPos(const SVFBasicBlock *Succ)
BBCondMap bbConds
map basic block to its successors/predecessors branch conditions

◆ getCFCond()

Condition SVF::SaberCondAllocator::getCFCond ( const SVFBasicBlock * bb) const
inline

Definition at line 224 of file SaberCondAllocator.h.

225 {
226 BBToCondMap::const_iterator it = bbToCondMap.find(bb);
227 if(it==bbToCondMap.end())
228 {
229 return getFalseCond();
230 }
231 return it->second;
232 }

◆ getCondInst()

const ICFGNode * SVF::SaberCondAllocator::getCondInst ( u32_t id) const
inline

Get/Set instruction based on Z3 expression id.

Definition at line 127 of file SaberCondAllocator.h.

128 {
129 IndexToTermInstMap::const_iterator it = idToTermInstMap.find(id);
130 assert(it != idToTermInstMap.end() && "this should be a fresh condition");
131 return it->second;
132 }
IndexToTermInstMap idToTermInstMap

◆ getCondNum()

u32_t SVF::SaberCondAllocator::getCondNum ( )
inline

Definition at line 78 of file SaberCondAllocator.h.

79 {
80 return totalCondNum;
81 }
static u32_t totalCondNum
vector storing z3expression

◆ getCurEvalSVFGNode()

const SVFGNode * SVF::SaberCondAllocator::getCurEvalSVFGNode ( ) const
inline

Get current value for branch condition evaluation.

Definition at line 186 of file SaberCondAllocator.h.

187 {
188 return curEvalSVFGNode;
189 }
const SVFGNode * curEvalSVFGNode
current llvm value to evaluate branch condition when computing guards

◆ getEvalBrCond()

SaberCondAllocator::Condition SaberCondAllocator::getEvalBrCond ( const SVFBasicBlock * bb,
const SVFBasicBlock * succ )
private

Get a condition, evaluate the value for conditions if necessary (e.g., testNull like express).

Definition at line 156 of file SaberCondAllocator.cpp.

157{
158 if (getCurEvalSVFGNode() && getCurEvalSVFGNode()->getValue())
159 return evaluateBranchCond(bb, succ);
160 else
161 return getBranchCond(bb, succ);
162}
const SVFGNode * getCurEvalSVFGNode() const
Get current value for branch condition evaluation.
Condition evaluateBranchCond(const SVFBasicBlock *bb, const SVFBasicBlock *succ)
Evaluate branch conditions.

◆ getFalseCond()

Condition SVF::SaberCondAllocator::getFalseCond ( ) const
inline

Definition at line 102 of file SaberCondAllocator.h.

103 {
105 }
static Z3Expr getFalseCond()
Return the unique false condition.
Definition Z3Expr.h:295

◆ getICFG()

ICFG * SVF::SaberCondAllocator::getICFG ( ) const
inline

Definition at line 207 of file SaberCondAllocator.h.

208 {
209 return PAG::getPAG()->getICFG();
210 }
ICFG * getICFG() const
Definition PAG.h:232

◆ getMemUsage()

std::string SVF::SaberCondAllocator::getMemUsage ( )
inline

Statistics.

Definition at line 70 of file SaberCondAllocator.h.

71 {
72 u32_t vmrss, vmsize;
73 if (SVFUtil::getMemoryUsageKB(&vmrss, &vmsize))
74 return std::to_string(vmsize) + "KB";
75 else
76 return "cannot read memory usage";
77 }
bool getMemoryUsageKB(u32_t *vmrss_kb, u32_t *vmsize_kb)
Get memory usage from system file. Return TRUE if succeed.
Definition SVFUtil.cpp:199

◆ getPHIComplementCond()

SaberCondAllocator::Condition SaberCondAllocator::getPHIComplementCond ( const SVFBasicBlock * BB1,
const SVFBasicBlock * BB2,
const SVFBasicBlock * BB0 )
virtual

Get complement condition (from B1 to B0) according to a complementBB (BB2) at a phi e.g., B0: dstBB; B1:incomingBB; B2:complementBB

Get complement phi condition e.g., B0: dstBB; B1:incomingBB; B2:complementBB Assume B0 (phi node) is the successor of both B1 and B2. If B1 dominates B2, and B0 not dominate B2 then condition from B1-->B0 = neg(B1-->B2)^(B1-->B0)

avoid both BB0 and BB1 dominate BB2 (e.g., while loop), then BB2 is not necessarily a complement BB

Definition at line 469 of file SaberCondAllocator.cpp.

470{
471 assert(BB1 && BB2 && "expect nullptr BB here!");
472
474 if (dominate(BB1, BB2) && ! dominate(BB0, BB2))
475 {
476 Condition cond = ComputeIntraVFGGuard(BB1, BB2);
477 return condNeg(cond);
478 }
479
480 return getTrueCond();
481}
bool dominate(const SVFBasicBlock *bbKey, const SVFBasicBlock *bbValue) const

◆ getRemovedSUVFEdges()

SVFGNodeToSVFGNodeSetMap & SVF::SaberCondAllocator::getRemovedSUVFEdges ( )
inline

Definition at line 243 of file SaberCondAllocator.h.

244 {
245 return removedSUVFEdges;
246 }
SVFGNodeToSVFGNodeSetMap removedSUVFEdges
a counter for fresh condition

◆ getTrueCond()

Condition SVF::SaberCondAllocator::getTrueCond ( ) const
inline

Definition at line 98 of file SaberCondAllocator.h.

99 {
100 return Condition::getTrueCond();
101 }
static Z3Expr getTrueCond()
Return the unique true condition.
Definition Z3Expr.h:288

◆ isAllPathReachable()

bool SVF::SaberCondAllocator::isAllPathReachable ( Condition & condition)
inline

whether condition is satisfiable for all possible boolean guards

Definition at line 199 of file SaberCondAllocator.h.

200 {
202 }
bool isEquivalentBranchCond(const Condition &lhs, const Condition &rhs) const
Whether lhs and rhs are equivalent branch conditions.

◆ isBBCallsProgExit()

bool SaberCondAllocator::isBBCallsProgExit ( const SVFBasicBlock * bb)
private

Whether this basic block contains program exit function call

Definition at line 447 of file SaberCondAllocator.cpp.

448{
449 const FunObjVar* svfun = bb->getParent();
450 FunToExitBBsMap::const_iterator it = funToExitBBsMap.find(svfun);
451 if (it != funToExitBBsMap.end())
452 {
453 for (const auto &bit: it->second)
454 {
455 if (postDominate(bit, bb))
456 return true;
457 }
458 }
459 return false;
460}

◆ isEQCmp()

bool SaberCondAllocator::isEQCmp ( const CmpStmt * cmp) const
private

Evaluate test null/not null like expressions.

Return true if the predicate of this compare instruction is equal

Definition at line 342 of file SaberCondAllocator.cpp.

343{
344 return (cmp->getPredicate() == CmpStmt::ICMP_EQ);
345}
u32_t getPredicate() const

◆ isEquivalentBranchCond()

bool SaberCondAllocator::isEquivalentBranchCond ( const Condition & lhs,
const Condition & rhs ) const

Whether lhs and rhs are equivalent branch conditions.

check equal using z3 solver

Definition at line 614 of file SaberCondAllocator.cpp.

616{
617 Condition::getSolver().push();
618 Condition::getSolver().add(lhs.getExpr() != rhs.getExpr());
619 z3::check_result res = Condition::getSolver().check();
620 Condition::getSolver().pop();
621 return res == z3::unsat;
622}
static z3::solver & getSolver()
Get z3 solver, singleton design here to make sure we only have one context.
Definition Z3Expr.cpp:56

◆ isNECmp()

bool SaberCondAllocator::isNECmp ( const CmpStmt * cmp) const
private

Return true if the predicate of this compare instruction is not equal.

Definition at line 347 of file SaberCondAllocator.cpp.

348{
349 return (cmp->getPredicate() == CmpStmt::ICMP_NE);
350}
@ ICMP_NE
not equal

◆ isNegCond()

bool SVF::SaberCondAllocator::isNegCond ( u32_t id) const
inline

Definition at line 141 of file SaberCondAllocator.h.

142 {
143 return negConds.test(id);
144 }
NodeBS negConds
key: z3 expression id, value: instruction

◆ isSatisfiable()

bool SaberCondAllocator::isSatisfiable ( const Condition & condition)

whether condition is satisfiable

Definition at line 625 of file SaberCondAllocator.cpp.

626{
627 Condition::getSolver().push();
628 Condition::getSolver().add(condition.getExpr());
629 z3::check_result result = Condition::getSolver().check();
630 Condition::getSolver().pop();
631 if (result == z3::sat || result == z3::unknown)
632 return true;
633 else
634 return false;
635}

◆ isTestContainsNullAndTheValue()

bool SaberCondAllocator::isTestContainsNullAndTheValue ( const CmpStmt * cmp) const
private

Return true if two values on the predicate are what we want.

Return true if: (1) cmp contains a null value (2) there is an indirect/direct edge from cur evaluated SVFG node to cmp operand

e.g., indirect edge: cur svfg node -> 1. store i32* %0, i32** p, align 8, !dbg !157 cmp operand -> 2. %1 = load i32*, i32** p, align 8, !dbg !159

  1. tobool = icmp ne i32* %1, null, !dbg !159
  2. br i1 tobool, label if.end, label if.then, !dbg !161 There is an indirect edge 1->2 with value %0

direct edge: cur svfg node -> 1. %3 = tail call i8* @malloc(i64 16), !dbg !22 (cmp operand) 2. %4 = icmp eq i8* %3, null, !dbg !28

  1. br i1 %4, label %7, label %5, !dbg !30 There is an direct edge 1->2 with value %3

Definition at line 398 of file SaberCondAllocator.cpp.

399{
400
401 // must be val var?
402 const SVFVar* op0 = cmp->getOpVar(0);
403 const SVFVar* op1 = cmp->getOpVar(1);
405 {
406 Set<const SVFVar* > inDirVal;
407 inDirVal.insert(getCurEvalSVFGNode()->getValue());
408 for (const auto &it: getCurEvalSVFGNode()->getOutEdges())
409 {
410 inDirVal.insert(it->getDstNode()->getValue());
411 }
412 return inDirVal.find(op0) != inDirVal.end();
413 }
415 {
416 Set<const SVFVar* > inDirVal;
417 inDirVal.insert(getCurEvalSVFGNode()->getValue());
418 for (const auto &it: getCurEvalSVFGNode()->getOutEdges())
419 {
420 inDirVal.insert(it->getDstNode()->getValue());
421 }
422 return inDirVal.find(op1) != inDirVal.end();
423 }
424 return false;
425}
const ValVar * getOpVar(u32_t pos) const
Operand SVFVars.
LLVM_NODISCARD bool isa(const Y &Val)
Definition Casting.h:241

◆ isTestNotNullExpr()

bool SaberCondAllocator::isTestNotNullExpr ( const ICFGNode * test) const
private

Return true if this is a test not null expression.

Definition at line 365 of file SaberCondAllocator.cpp.

366{
367 if(!test) return false;
368 for(const SVFStmt* stmt : PAG::getPAG()->getSVFStmtList(test))
369 {
370 if(const CmpStmt* cmp = SVFUtil::dyn_cast<CmpStmt>(stmt))
371 {
372 return isTestContainsNullAndTheValue(cmp) && isNECmp(cmp);
373 }
374 }
375 return false;
376}
bool isTestContainsNullAndTheValue(const CmpStmt *cmp) const
Return true if two values on the predicate are what we want.
bool isNECmp(const CmpStmt *cmp) const
Return true if the predicate of this compare instruction is not equal.

◆ isTestNullExpr()

bool SaberCondAllocator::isTestNullExpr ( const ICFGNode * test) const
private

Return true if this is a test null expression.

Definition at line 352 of file SaberCondAllocator.cpp.

353{
354 if(!test) return false;
355 for(const SVFStmt* stmt : PAG::getPAG()->getSVFStmtList(test))
356 {
357 if(const CmpStmt* cmp = SVFUtil::dyn_cast<CmpStmt>(stmt))
358 {
359 return isTestContainsNullAndTheValue(cmp) && isEQCmp(cmp);
360 }
361 }
362 return false;
363}
bool isEQCmp(const CmpStmt *cmp) const
Evaluate test null/not null like expressions.

◆ newCond()

SaberCondAllocator::Condition SaberCondAllocator::newCond ( const ICFGNode * inst)

Allocate a new condition.

Definition at line 601 of file SaberCondAllocator.cpp.

602{
603 u32_t condCountIdx = totalCondNum++;
604 Condition expr = Condition::getContext().bool_const(("c" + std::to_string(condCountIdx)).c_str());
605 Condition negCond = Condition::NEG(expr);
606 setCondInst(expr, inst);
607 setNegCondInst(negCond, inst);
608 conditionVec.push_back(expr);
609 conditionVec.push_back(negCond);
610 return expr;
611}
void setNegCondInst(const Condition &condition, const ICFGNode *inst)
mark neg Z3 expression
void setCondInst(const Condition &condition, const ICFGNode *inst)
std::vector< Condition > conditionVec
bit vector for distinguish neg
static z3::context & getContext()
Get z3 context, singleton design here to make sure we only have one context.
Definition Z3Expr.cpp:66

◆ postDominate()

bool SVF::SaberCondAllocator::postDominate ( const SVFBasicBlock * bbKey,
const SVFBasicBlock * bbValue ) const
inline

Definition at line 146 of file SaberCondAllocator.h.

147 {
148 const FunObjVar* keyFunc = bbKey->getParent();
149 const FunObjVar* valueFunc = bbValue->getParent();
150 bool funcEq = (keyFunc == valueFunc);
151 (void)funcEq; // Suppress warning of unused variable under release build
152 assert(funcEq && "two basicblocks should be in the same function!");
153 return keyFunc->postDominate(bbKey,bbValue);
154 }

◆ printPathCond()

void SaberCondAllocator::printPathCond ( )

Print out the path condition information.

Print path conditions

Definition at line 574 of file SaberCondAllocator.cpp.

575{
576
577 outs() << "print path condition\n";
578
579 for (const auto &bbCond: bbConds)
580 {
581 const SVFBasicBlock* bb = bbCond.first;
582 for (const auto &cit: bbCond.second)
583 {
584 u32_t i = 0;
585 for (const SVFBasicBlock* succ: bb->getSuccessors())
586 {
587 if (i == cit.first)
588 {
589 Condition cond = cit.second;
590 outs() << bb->getName() << "-->" << succ->getName() << ":";
591 outs() << dumpCond(cond) << "\n";
592 break;
593 }
594 i++;
595 }
596 }
597 }
598}
std::string dumpCond(const Condition &cond) const

◆ setBranchCond()

void SaberCondAllocator::setBranchCond ( const SVFBasicBlock * bb,
const SVFBasicBlock * succ,
const Condition & cond )
private

Get/Set a branch condition, and its terminator instruction.

Set branch condition

Set a branch condition

we only care about basic blocks have more than one successor

FIXME: llvm getNumSuccessors allows duplicated block in the successors, it makes this assertion fail In this case we may waste a condition allocation, because the overwrite of the previous cond

Definition at line 167 of file SaberCondAllocator.cpp.

168{
170 assert(bb->getNumSuccessors() > 1 && "not more than one successor??");
171 u32_t pos = bb->getBBSuccessorPos(succ);
172 CondPosMap& condPosMap = bbConds[bb];
173
176 //assert(condPosMap.find(pos) == condPosMap.end() && "this branch has already been set ");
177
178 condPosMap[pos] = cond;
179}
Map< u32_t, Condition > CondPosMap
id to instruction map for z3

◆ setCFCond()

bool SVF::SaberCondAllocator::setCFCond ( const SVFBasicBlock * bb,
const Condition & cond )
inline

Get/Set control-flow conditions.

Definition at line 214 of file SaberCondAllocator.h.

215 {
216 BBToCondMap::iterator it = bbToCondMap.find(bb);
217 // until a fixed-point is reached (condition is not changed)
218 if(it!=bbToCondMap.end() && isEquivalentBranchCond(it->second, cond))
219 return false;
220
221 bbToCondMap[bb] = cond;
222 return true;
223 }

◆ setCondInst()

void SVF::SaberCondAllocator::setCondInst ( const Condition & condition,
const ICFGNode * inst )
inline

Definition at line 134 of file SaberCondAllocator.h.

135 {
136 assert(idToTermInstMap.find(condition.id()) == idToTermInstMap.end() && "this should be a fresh condition");
137 idToTermInstMap[condition.id()] = inst;
138 }

◆ setCurEvalSVFGNode()

void SVF::SaberCondAllocator::setCurEvalSVFGNode ( const SVFGNode * node)
inline

Set current value for branch condition evaluation.

Definition at line 181 of file SaberCondAllocator.h.

182 {
183 curEvalSVFGNode = node;
184 }

◆ setNegCondInst()

void SVF::SaberCondAllocator::setNegCondInst ( const Condition & condition,
const ICFGNode * inst )
inline

mark neg Z3 expression

Definition at line 237 of file SaberCondAllocator.h.

238 {
239 setCondInst(condition, inst);
240 negConds.set(condition.id());
241 }

Member Data Documentation

◆ bbConds

BBCondMap SVF::SaberCondAllocator::bbConds
protected

map basic block to its successors/predecessors branch conditions

Definition at line 311 of file SaberCondAllocator.h.

◆ bbToCondMap

BBToCondMap SVF::SaberCondAllocator::bbToCondMap
private

map a basic block to its path condition starting from root

Definition at line 302 of file SaberCondAllocator.h.

◆ conditionVec

std::vector<Condition> SVF::SaberCondAllocator::conditionVec
private

bit vector for distinguish neg

Definition at line 306 of file SaberCondAllocator.h.

◆ curEvalSVFGNode

const SVFGNode* SVF::SaberCondAllocator::curEvalSVFGNode {}
private

current llvm value to evaluate branch condition when computing guards

Definition at line 303 of file SaberCondAllocator.h.

303{};

◆ funToExitBBsMap

FunToExitBBsMap SVF::SaberCondAllocator::funToExitBBsMap
private

map a function to all its basic blocks calling program exit

Definition at line 301 of file SaberCondAllocator.h.

◆ idToTermInstMap

IndexToTermInstMap SVF::SaberCondAllocator::idToTermInstMap
private

Definition at line 304 of file SaberCondAllocator.h.

◆ negConds

NodeBS SVF::SaberCondAllocator::negConds
private

key: z3 expression id, value: instruction

Definition at line 305 of file SaberCondAllocator.h.

◆ removedSUVFEdges

SVFGNodeToSVFGNodeSetMap SVF::SaberCondAllocator::removedSUVFEdges
private

a counter for fresh condition

Definition at line 308 of file SaberCondAllocator.h.

◆ totalCondNum

u32_t SaberCondAllocator::totalCondNum = 0
staticprivate

vector storing z3expression

Definition at line 307 of file SaberCondAllocator.h.


The documentation for this class was generated from the following files: