Static Value-Flow Analysis
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
SVF::LockResultValidator Class Reference

#include <LockResultValidator.h>

Public Types

typedef Set< std::stringCxtLockSetStr
 
typedef Map< const SVFInstruction *, CxtLockSetStrCxtStmtToCxtLockS
 
typedef unsigned LOCK_FLAG
 

Public Member Functions

 LockResultValidator (LockAnalysis *la)
 
 ~LockResultValidator ()
 
void analyze ()
 
SVFModulegetModule () const
 

Private Member Functions

CallICFGNodegetCBN (const SVFInstruction *inst)
 
const InstructiongetPreviousMemoryAccessInst (const Instruction *I)
 
bool inFilter (const std::string &name)
 
bool match (const std::string &lockName, CxtLockSetStr LS)
 
Set< std::string > & split (const std::string &s, char delim, Set< std::string > &elems)
 
Set< std::stringsplit (const std::string &s, char delim)
 
std::string getOutput (const char *scenario, LOCK_FLAG analysisRes)
 
Set< std::stringgetStringArg (const Instruction *inst, unsigned int arg_num)
 
bool collectLockTargets ()
 
LOCK_FLAG validateStmtInLock ()
 

Private Attributes

CxtStmtToCxtLockS instToCxtLockSet
 
LockAnalysis::CxtStmtToCxtLockSet cxtStmtToCxtLockSet
 
LockAnalysis::CxtLockSet cxtLockSet
 
LockAnalysis_la
 
SVFModule_mod
 
Set< std::stringfilterFun = {"LOCK", "INTERLEV_ACCESS", "PAUSE", "CXT_THREAD", "TCT_ACCESS"}
 

Static Private Attributes

static const LOCK_FLAG LOCK_TRUE = 0x01
 
static const LOCK_FLAG LOCK_IMPRECISE = 0x02
 
static const LOCK_FLAG LOCK_UNSOUND = 0x04
 
static constexpr char const * LOCK = "LOCK"
 

Detailed Description

Definition at line 19 of file LockResultValidator.h.

Member Typedef Documentation

◆ CxtLockSetStr

Definition at line 22 of file LockResultValidator.h.

◆ CxtStmtToCxtLockS

Definition at line 23 of file LockResultValidator.h.

◆ LOCK_FLAG

Definition at line 25 of file LockResultValidator.h.

Constructor & Destructor Documentation

◆ LockResultValidator()

SVF::LockResultValidator::LockResultValidator ( LockAnalysis la)
inline

Definition at line 27 of file LockResultValidator.h.

27  : _la(la)
28  {
29  _mod = _la->getTCT()->getSVFModule();
30  }
TCT * getTCT()
Get tct.
Definition: LockAnalysis.h:324
SVFModule * getSVFModule() const
Get SVFFModule.
Definition: TCT.h:176

◆ ~LockResultValidator()

SVF::LockResultValidator::~LockResultValidator ( )
inline

Definition at line 32 of file LockResultValidator.h.

32 {}

Member Function Documentation

◆ analyze()

void LockResultValidator::analyze ( )

Definition at line 202 of file LockResultValidator.cpp.

203 {
204  outs() << SVFUtil::pasMsg(" --- Lock Analysis Result Validation ---\n");
205  if(!collectLockTargets())
206  return;
207  std::string errstring;
208  errstring = getOutput("Validate Lock Analysis :", validateStmtInLock());
209  outs() << "======" << errstring << "======\n";
210 
211  RaceValidator validator(_la);
212  validator.init(_la->getTCT()->getSVFModule());
213  validator.analyze();
214 }
const char *const string
Definition: cJSON.h:172
std::string getOutput(const char *scenario, LOCK_FLAG analysisRes)
std::string pasMsg(const std::string &msg)
Print each pass/phase message by converting a string into blue string output.
Definition: SVFUtil.cpp:99
std::ostream & outs()
Overwrite llvm::outs()
Definition: SVFUtil.h:50

◆ collectLockTargets()

bool LockResultValidator::collectLockTargets ( )
private

Definition at line 95 of file LockResultValidator.cpp.

96 {
97  const Function* F = nullptr;
98  for (Module &M : LLVMModuleSet::getLLVMModuleSet()->getLLVMModules())
99  {
100  for(auto it = M.begin(); it != M.end(); it++)
101  {
102  const std::string fName = (*it).getName().str();
103  if(fName.find(LOCK) != std::string::npos)
104  {
105  F = &(*it);
106  break;
107  }
108  }
109  }
110  if (!F)
111  return false;
112  for(Value::const_use_iterator it = F->use_begin(), ie = F->use_end(); it!=ie; it++)
113  {
114  const Use *u = &*it;
115  const Value* user = u->getUser();
116  const Instruction* inst = SVFUtil::dyn_cast<Instruction>(user);
117  CxtLockSetStr y = getStringArg(inst, 0);
118  const Instruction* memInst = getPreviousMemoryAccessInst(inst);
119  const SVFInstruction* svfMemInst = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(memInst);
120 
121  instToCxtLockSet[svfMemInst] = y;
122  if(const StoreInst* store = SVFUtil::dyn_cast<StoreInst> (memInst))
123  {
124  if(const BinaryOperator* bop = SVFUtil::dyn_cast<BinaryOperator> (store->getValueOperand()))
125  {
126  const Value* v = bop->getOperand(0);
127  const Instruction* prevInst = SVFUtil::dyn_cast<LoadInst> (v);
128  const SVFInstruction* svfPrevInst = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(prevInst);
129  instToCxtLockSet[svfPrevInst] = y;
130  }
131  }
132  }
133  return true;
134 }
#define F(f)
static LLVMModuleSet * getLLVMModuleSet()
Definition: LLVMModule.h:105
SVFInstruction * getSVFInstruction(const Instruction *inst) const
Definition: LLVMModule.h:224
Set< std::string > getStringArg(const Instruction *inst, unsigned int arg_num)
CxtStmtToCxtLockS instToCxtLockSet
const Instruction * getPreviousMemoryAccessInst(const Instruction *I)
static constexpr char const * LOCK
Set< std::string > CxtLockSetStr
llvm::Use Use
Definition: BasicTypes.h:72
llvm::Function Function
Definition: BasicTypes.h:85
llvm::Instruction Instruction
Definition: BasicTypes.h:87
llvm::Value Value
LLVM Basic classes.
Definition: BasicTypes.h:82
llvm::Module Module
Definition: BasicTypes.h:84
llvm::BinaryOperator BinaryOperator
Definition: BasicTypes.h:179
llvm::StoreInst StoreInst
Definition: BasicTypes.h:148

◆ getCBN()

CallICFGNode* SVF::LockResultValidator::getCBN ( const SVFInstruction inst)
inlineprivate

Definition at line 42 of file LockResultValidator.h.

43  {
44  return _la->getTCT()->getCallICFGNode(inst);
45  }
CallICFGNode * getCallICFGNode(const SVFInstruction *inst)
Get CallICFGNode given inst.
Definition: TCT.h:171

◆ getModule()

SVFModule* SVF::LockResultValidator::getModule ( ) const
inline

Definition at line 36 of file LockResultValidator.h.

37  {
38  return _mod;
39  }

◆ getOutput()

std::string LockResultValidator::getOutput ( const char *  scenario,
LOCK_FLAG  analysisRes 
)
inlineprivate

Definition at line 74 of file LockResultValidator.cpp.

75 {
76  std::string ret(scenario);
77  ret += "\t";
78  switch (analysisRes)
79  {
80  case LOCK_TRUE:
81  ret += SVFUtil::sucMsg("SUCCESS");
82  break;
83  case LOCK_UNSOUND:
84  ret += SVFUtil::bugMsg2("UNSOUND");
85  break;
86  case LOCK_IMPRECISE:
87  ret += SVFUtil::bugMsg1("IMPRECISE");
88  break;
89  default:
90  ret += SVFUtil::errMsg("FAILURE");
91  }
92  return ret;
93 }
static const LOCK_FLAG LOCK_TRUE
static const LOCK_FLAG LOCK_UNSOUND
static const LOCK_FLAG LOCK_IMPRECISE
std::string sucMsg(const std::string &msg)
Returns successful message by converting a string into green string output.
Definition: SVFUtil.cpp:53
std::string bugMsg1(const std::string &msg)
Definition: SVFUtil.cpp:81
std::string errMsg(const std::string &msg)
Print error message by converting a string into red string output.
Definition: SVFUtil.cpp:76
std::string bugMsg2(const std::string &msg)
Definition: SVFUtil.cpp:86

◆ getPreviousMemoryAccessInst()

const Instruction * LockResultValidator::getPreviousMemoryAccessInst ( const Instruction I)
private

Definition at line 216 of file LockResultValidator.cpp.

217 {
218  I = I->getPrevNode();
219  while (I)
220  {
221  if (SVFUtil::isa<LoadInst>(I) || SVFUtil::isa<StoreInst>(I))
222  return I;
223  SVFFunction* callee = nullptr;
224 
225  if(LLVMUtil::isCallSite(I))
226  {
229  _la->getTCT()->getThreadCallGraph()->getCallees(getCBN(svfInst), callees);
230 
231  for(PTACallGraph::FunctionSet::const_iterator cit = callees.begin(),
232  ecit = callees.end(); cit!=ecit; cit++)
233  {
234  if(*cit != nullptr)
235  {
236  callee = const_cast<SVFFunction*> (*cit);
237  break;
238  }
239 
240  }
241  }
242 
243  if (callee)
244  {
245  if (callee->getName().find("llvm.memset") != std::string::npos)
246  return I;
247  }
248  I = I->getPrevNode();
249  }
250  return nullptr;
251 }
CallICFGNode * getCBN(const SVFInstruction *inst)
void getCallees(const CallICFGNode *cs, FunctionSet &callees)
Get all callees for a callsite.
Definition: PTACallGraph.h:382
Set< const SVFFunction * > FunctionSet
Definition: PTACallGraph.h:228
const std::string & getName() const
Definition: SVFValue.h:243
ThreadCallGraph * getThreadCallGraph() const
Get TCG.
Definition: TCT.h:182
bool isCallSite(const Instruction *inst)
Whether an instruction is a call or invoke instruction.
Definition: LLVMUtil.h:45

◆ getStringArg()

Set< std::string > LockResultValidator::getStringArg ( const Instruction inst,
unsigned int  arg_num 
)
private

Definition at line 39 of file LockResultValidator.cpp.

40 {
41  assert(LLVMUtil::isCallSite(inst) && "getFirstIntArg: inst is not a callsite");
42  const CallBase* cs = LLVMUtil::getLLVMCallSite(inst);
43  assert((arg_num < cs->arg_size()) && "Does not has this argument");
44  const GetElementPtrInst* gepinst = SVFUtil::dyn_cast<GetElementPtrInst>(cs->getArgOperand(arg_num));
45  const Constant* arrayinst = SVFUtil::dyn_cast<Constant>(gepinst->getOperand(0));
46  const ConstantDataArray* cxtarray = SVFUtil::dyn_cast<ConstantDataArray>(arrayinst->getOperand(0));
47  if (!cxtarray)
48  {
49  Set<std::string> strvec;
50  return strvec;
51  }
52  const std::string vthdcxtstring = cxtarray->getAsCString().str();
53  return split(vthdcxtstring, ',');
54 }
Set< std::string > & split(const std::string &s, char delim, Set< std::string > &elems)
const CallBase * getLLVMCallSite(const Value *value)
Return LLVM callsite given a value.
Definition: LLVMUtil.h:59
llvm::CallBase CallBase
Definition: BasicTypes.h:146
llvm::Constant Constant
Definition: BasicTypes.h:124
llvm::GetElementPtrInst GetElementPtrInst
Definition: BasicTypes.h:162
llvm::ConstantDataArray ConstantDataArray
Definition: BasicTypes.h:121
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96

◆ inFilter()

bool SVF::LockResultValidator::inFilter ( const std::string name)
inlineprivate

Definition at line 48 of file LockResultValidator.h.

49  {
50  return filterFun.find(name) != filterFun.end();
51  }
const char *const name
Definition: cJSON.h:264
Set< std::string > filterFun

◆ match()

bool SVF::LockResultValidator::match ( const std::string lockName,
CxtLockSetStr  LS 
)
inlineprivate

Definition at line 53 of file LockResultValidator.h.

54  {
55  return LS.find(lockName) != LS.end();
56  }

◆ split() [1/2]

Set< std::string > LockResultValidator::split ( const std::string s,
char  delim 
)
private

Definition at line 67 of file LockResultValidator.cpp.

68 {
69  Set<std::string> elems;
70  split(s, delim, elems);
71  return elems;
72 }

◆ split() [2/2]

Set< std::string > & LockResultValidator::split ( const std::string s,
char  delim,
Set< std::string > &  elems 
)
private

Definition at line 56 of file LockResultValidator.cpp.

57 {
58  std::stringstream ss(s);
60  while (std::getline(ss, item, delim))
61  {
62  elems.insert(item);
63  }
64  return elems;
65 }
cJSON * item
Definition: cJSON.h:222

◆ validateStmtInLock()

LockResultValidator::LOCK_FLAG LockResultValidator::validateStmtInLock ( )
private

Definition at line 136 of file LockResultValidator.cpp.

137 {
138  SVFIR* pag = SVFIR::getPAG();
141  for(LockAnalysis::CxtStmtToCxtLockSet::iterator it = analyedLS.begin(),
142  eit = analyedLS.end(); it!=eit; it++)
143  {
144  const SVFInstruction* inst = ((*it).first).getStmt();
145  bool interestedInst = true;
146  for(const SVFStmt* stmt : pag->getSVFStmtList(pag->getICFG()->getICFGNode(inst)))
147  {
148  if(!SVFUtil::isa<LoadStmt>(stmt) && !SVFUtil::isa<StoreStmt>(stmt))
149  interestedInst = false;
150  }
151  if(interestedInst==false)
152  continue;
153  const SVFFunction* F = inst->getFunction();
154  if(inFilter(F->getName()))
155  continue;
156  CxtLockSetStr LS = instToCxtLockSet[inst];
157  if(LS.size() != (*it).second.size())
158  {
160  {
161  outs() << errMsg("\nValidate Stmt's Lock : Wrong at: ") << inst->toString() << "\n";
162  outs() << "Reason: The number of lock on current stmt is wrong\n";
163  outs() << "\n----Given locks:\n";
164  for (CxtLockSetStr::iterator it1 = LS.begin(),eit1 = LS.end(); it1 != eit1; it++)
165  {
166  outs() << "Lock " << *it1 << " ";
167  }
168  outs() << "\n----Analysis locks:\n";
169  for (LockAnalysis::CxtLockSet::iterator it2 = (*it).second.begin(),
170  eit2 = (*it).second.end(); it2 != eit2; ++it)
171  {
172  const SVFInstruction* call = (*it2).getStmt();
173  outs()<<"Lock " << call->toString() << " ";
174  }
175  outs() << "\n";
176  }
178  }
179  LockAnalysis::CxtLockSet LSA = (*it).second;
180 
181  for(LockAnalysis::CxtLockSet::iterator it3 = LSA.begin(), eit3=LSA.end(); it3!=eit3; it3++)
182  {
183  const SVFInstruction* call = (*it3).getStmt();
184  if(SVFUtil::isCallSite(call) == false)
185  continue;
187  if(!match(lockName, LS))
188  {
190  {
191  outs() << "\nValidate Stmt's Lock : Wrong at (" << inst->toString() << ")\n";
192  outs() << "Reason: The number of lock on current stmt is wrong\n";
193  outs() << "\n Lock " << lockName << " should not protect current instruction\n";
195  }
196  }
197  }
198  }
199  return res;
200 }
const SVFValue * getArgOperand(u32_t i) const
Definition: SVFValue.h:1148
ICFGNode * getICFGNode(NodeID id) const
Get a ICFG node.
Definition: ICFG.h:92
Set< CxtLock > CxtLockSet
Definition: LockAnalysis.h:70
Map< CxtStmt, CxtLockSet > CxtStmtToCxtLockSet
Definition: LockAnalysis.h:76
CxtStmtToCxtLockSet getCSTCLS()
Definition: LockAnalysis.h:249
bool match(const std::string &lockName, CxtLockSetStr LS)
bool inFilter(const std::string &name)
static const Option< bool > PrintValidRes
Definition: Options.h:175
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition: SVFIR.h:114
SVFStmtList & getSVFStmtList(const ICFGNode *inst)
Given an instruction, get all its PAGEdges.
Definition: SVFIR.h:208
ICFG * getICFG() const
Definition: SVFIR.h:170
const SVFFunction * getFunction() const
Definition: SVFValue.h:683
std::string toString() const
Needs to be implemented by a SVF front end.
Definition: LLVMUtil.cpp:906
CallSite getSVFCallSite(const SVFInstruction *inst)
Return LLVM callsite given an instruction.
Definition: SVFUtil.h:196
bool isCallSite(const SVFInstruction *inst)
Whether an instruction is a call or invoke instruction.
Definition: SVFUtil.h:174

Member Data Documentation

◆ _la

LockAnalysis* SVF::LockResultValidator::_la
private

Definition at line 72 of file LockResultValidator.h.

◆ _mod

SVFModule* SVF::LockResultValidator::_mod
private

Definition at line 73 of file LockResultValidator.h.

◆ cxtLockSet

LockAnalysis::CxtLockSet SVF::LockResultValidator::cxtLockSet
private

Definition at line 70 of file LockResultValidator.h.

◆ cxtStmtToCxtLockSet

LockAnalysis::CxtStmtToCxtLockSet SVF::LockResultValidator::cxtStmtToCxtLockSet
private

Definition at line 69 of file LockResultValidator.h.

◆ filterFun

Set<std::string> SVF::LockResultValidator::filterFun = {"LOCK", "INTERLEV_ACCESS", "PAUSE", "CXT_THREAD", "TCT_ACCESS"}
private

Definition at line 81 of file LockResultValidator.h.

◆ instToCxtLockSet

CxtStmtToCxtLockS SVF::LockResultValidator::instToCxtLockSet
private

Definition at line 68 of file LockResultValidator.h.

◆ LOCK

constexpr char const* SVF::LockResultValidator::LOCK = "LOCK"
staticconstexprprivate

Definition at line 79 of file LockResultValidator.h.

◆ LOCK_IMPRECISE

const LOCK_FLAG SVF::LockResultValidator::LOCK_IMPRECISE = 0x02
staticprivate

Definition at line 76 of file LockResultValidator.h.

◆ LOCK_TRUE

const LOCK_FLAG SVF::LockResultValidator::LOCK_TRUE = 0x01
staticprivate

Definition at line 75 of file LockResultValidator.h.

◆ LOCK_UNSOUND

const LOCK_FLAG SVF::LockResultValidator::LOCK_UNSOUND = 0x04
staticprivate

Definition at line 77 of file LockResultValidator.h.


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