Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bjjwwang committed Mar 13, 2024
1 parent dc233b0 commit 09fef62
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions svf-llvm/tools/Example/svf-ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
//

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// GNU General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//===-----------------------------------------------------------------------===//
Expand All @@ -34,30 +34,35 @@
#include "Util/Options.h"
#include "WPA/Andersen.h"

using namespace llvm;
using namespace std;
using namespace SVF;

/*!
* An example to query alias results of two LLVM values
*/
SVF::AliasResult aliasQuery(PointerAnalysis* pta, SVFValue* v1, SVFValue* v2)
SVF::AliasResult aliasQuery(PointerAnalysis* pta, Value* v1, Value* v2)
{
return pta->alias(v1,v2);
SVFValue* val1 = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(v1);
SVFValue* val2 = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(v2);

return pta->alias(val1,val2);
}

/*!
* An example to print points-to set of an LLVM value
*/
std::string printPts(PointerAnalysis* pta, SVFValue* val)
std::string printPts(PointerAnalysis* pta, Value* val)
{

std::string str;
std::stringstream rawstr(str);
raw_string_ostream rawstr(str);
SVFValue* svfval = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(val);

NodeID pNodeId = pta->getPAG()->getValueNode(val);
NodeID pNodeId = pta->getPAG()->getValueNode(svfval);
const PointsTo& pts = pta->getPts(pNodeId);
for (PointsTo::iterator ii = pts.begin(), ie = pts.end();
ii != ie; ii++)
ii != ie; ii++)
{
rawstr << " " << *ii << " ";
PAGNode* targetObj = pta->getPAG()->getGNode(*ii);
Expand Down Expand Up @@ -162,14 +167,18 @@ void traverseOnICFG(ICFG* icfg, const ICFGNode* iNode)
}
}

void skipWarning(const VFGNode* node) {

}
/*!
* An example to query/collect all the uses of a definition of a value along value-flow graph (VFG)
*/
void traverseOnVFG(const SVFG* vfg, SVFValue* val)
void traverseOnVFG(const SVFG* vfg, const SVFValue* svfval)
{
SVFIR* pag = SVFIR::getPAG();

PAGNode* pNode = pag->getGNode(pag->getValueNode(val));
PAGNode* pNode = pag->getGNode(pag->getValueNode(svfval));
if (!vfg->hasDefSVFGNode(pNode))
return;
const VFGNode* vNode = vfg->getDefSVFGNode(pNode);
FIFOWorkList<const VFGNode*> worklist;
Set<const VFGNode*> visited;
Expand All @@ -195,10 +204,11 @@ void traverseOnVFG(const SVFG* vfg, SVFValue* val)
/// Collect all LLVM Values
for(Set<const VFGNode*>::const_iterator it = visited.begin(), eit = visited.end(); it!=eit; ++it)
{
// const VFGNode* node = *it;
const VFGNode* node = *it;
skipWarning(node);
/// can only query VFGNode involving top-level pointers (starting with % or @ in LLVM IR)
/// PAGNode* pNode = vfg->getLHSTopLevPtr(node);
/// SVFValue* val = pNode->getValue();
/// Value* val = pNode->getValue();
}
}

Expand All @@ -207,8 +217,8 @@ int main(int argc, char ** argv)

std::vector<std::string> moduleNameVec;
moduleNameVec = OptionBase::parseOptions(
argc, argv, "Whole Program Points-to Analysis", "[options] <input-bitcode...>"
);
argc, argv, "Whole Program Points-to Analysis", "[options] <input-bitcode...>"
);

if (Options::WriteAnder() == "ir_annotator")
{
Expand All @@ -221,7 +231,6 @@ int main(int argc, char ** argv)
SVFIRBuilder builder(svfModule);
SVFIR* pag = builder.build();


/// Create Andersen's pointer analysis
Andersen* ander = AndersenWaveDiff::createAndersenWaveDiff(pag);

Expand All @@ -236,19 +245,21 @@ int main(int argc, char ** argv)

/// ICFG
ICFG* icfg = pag->getICFG();
icfg->dump("icfg");

/// Value-Flow Graph (VFG)
VFG* vfg = new VFG(callgraph);

/// Sparse value-flow graph (SVFG)
SVFGBuilder svfBuilder(true);
//SVFG* svfg =
svfBuilder.buildFullSVFG(ander);
SVFGBuilder svfBuilder;
SVFG* svfg = svfBuilder.buildFullSVFG(ander);

/// Collect uses of an LLVM Value
/// traverseOnVFG(svfg, value);

for (const auto &it : *svfg)
{
const SVFGNode* node = it.second;
if (node->getValue())
traverseOnVFG(svfg, node->getValue());
}

/// Collect all successor nodes on ICFG
for (const auto &it : *icfg)
Expand Down

0 comments on commit 09fef62

Please sign in to comment.