1#ifndef ROSE_BinaryAnalysis_FunctionCall_H
2#define ROSE_BinaryAnalysis_FunctionCall_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
6#include <Rose/BinaryAnalysis/ControlFlow.h>
11namespace BinaryAnalysis {
25 : vertex_filter(NULL), edge_filter(NULL)
52 typedef boost::adjacency_list<boost::setS,
54 boost::bidirectionalS,
55 boost::property<boost::vertex_name_t, SgAsmFunction*>
111 return filter && !(*filter)(
this, func);
124 return filter && !(*filter)(
this, src, dst);
132 VertexFilter *vertex_filter;
133 EdgeFilter *edge_filter;
154 template<
class FunctionCallGraph>
173 template<
class FunctionCallGraph,
class ControlFlowGraph>
176 template<
class ControlFlowGraph,
class FunctionCallGraph>
207 template<
class FunctionCallGraph>
210 template<
class FunctionCallGraph>
224 template<
class FunctionCallGraph>
225 FunctionCallGraph
copy(
const FunctionCallGraph &src);
226 template<
class FunctionCallGraph>
227 void copy(
const FunctionCallGraph &src, FunctionCallGraph &dst);
236template<
class FunctionCallGraph>
240 typename boost::graph_traits<FunctionCallGraph>::vertex_iterator vi, vi_end;
241 for (boost::tie(vi, vi_end)=boost::vertices(cg); vi!=vi_end; ++vi) {
244 func->set_cachedVertex(*vi);
248template<
class ControlFlowGraph,
class FunctionCallGraph>
252 typedef typename boost::graph_traits<FunctionCallGraph>::vertex_descriptor CG_Vertex;
253 typedef typename boost::graph_traits<ControlFlowGraph>::vertex_descriptor CFG_Vertex;
254 typedef std::map<SgAsmFunction*, CG_Vertex> FunctionVertexMap;
259 FunctionVertexMap fv_map;
260 typename boost::graph_traits<const ControlFlowGraph>::vertex_iterator vi, vi_end;
261 for (boost::tie(vi, vi_end)=boost::vertices(cfg); vi!=vi_end; ++vi) {
264 typename FunctionVertexMap::iterator fi=fv_map.find(func);
265 if (func && fi==fv_map.end()) {
266 CG_Vertex v = boost::add_vertex(cg);
274 typename boost::graph_traits<const ControlFlowGraph>::edge_iterator ei, ei_end;
275 for (boost::tie(ei, ei_end)=boost::edges(cfg); ei!=ei_end; ++ei) {
276 CFG_Vertex cfg_a = boost::source(*ei, cfg);
277 CFG_Vertex cfg_b = boost::target(*ei, cfg);
279 ASSERT_not_null(block_a);
281 ASSERT_not_null(block_b);
285 typename FunctionVertexMap::iterator fi_a = fv_map.find(func_a);
286 if (fi_a!=fv_map.end()) {
287 typename FunctionVertexMap::iterator fi_b = fv_map.find(func_b);
288 if (fi_b!=fv_map.end())
289 boost::add_edge(fi_a->second, fi_b->second, cg);
295template<
class FunctionCallGraph,
class ControlFlowGraph>
299 FunctionCallGraph cg;
304template<
class FunctionCallGraph>
308 typedef typename boost::graph_traits<FunctionCallGraph>::vertex_descriptor
Vertex;
309 typedef std::map<SgAsmFunction*, Vertex> FunctionVertexMap;
310 FunctionVertexMap fv_map;
317 FunctionCallGraph &cg;
318 FunctionVertexMap &fv_map;
319 VertexAdder(
FunctionCall *analyzer, FunctionCallGraph &cg, FunctionVertexMap &fv_map)
320 : analyzer(analyzer), cg(cg), fv_map(fv_map)
322 void visit(
SgNode *node) {
325 Vertex vertex = boost::add_vertex(cg);
326 fv_map[func] = vertex;
335 FunctionCallGraph &cg;
336 FunctionVertexMap &fv_map;
338 EdgeAdder(
FunctionCall *analyzer, FunctionCallGraph &cg, FunctionVertexMap &fv_map,
Vertex source_vertex)
339 : analyzer(analyzer), cg(cg), fv_map(fv_map), source_vertex(source_vertex)
344 void visit(
SgNode *node) {
349 const SgAsmIntegerValuePtrList &succs = block_a->
get_successors();
350 for (SgAsmIntegerValuePtrList::const_iterator si=succs.begin(); si!=succs.end(); ++si) {
351 SgAsmFunction *func_b = isSgAsmFunction((*si)->get_baseNode());
352 if (func_b == NULL) {
353 SgAsmBlock *block_b = isSgAsmBlock((*si)->get_baseNode());
354 func_b = function_of(block_b);
359 typename FunctionVertexMap::iterator fi_b = fv_map.find(func_b);
360 if (fi_b!=fv_map.end())
361 boost::add_edge(source_vertex, fi_b->second, cg);
367 VertexAdder(
this, cg, fv_map).traverse(root, preorder);
368 typename boost::graph_traits<FunctionCallGraph>::vertex_iterator vi, vi_end;
369 for (boost::tie(vi, vi_end)=boost::vertices(cg); vi!=vi_end; ++vi) {
371 EdgeAdder(
this, cg, fv_map, *vi).traverse(source_func, preorder);
375template<
class FunctionCallGraph>
379 FunctionCallGraph cg;
384template<
class FunctionCallGraph>
388 typedef typename boost::graph_traits<FunctionCallGraph>::vertex_descriptor
Vertex;
389 Vertex NO_VERTEX = boost::graph_traits<FunctionCallGraph>::null_vertex();
392 std::vector<Vertex> src_to_dst(boost::num_vertices(src), NO_VERTEX);
394 typename boost::graph_traits<const FunctionCallGraph>::vertex_iterator vi, vi_end;
395 for (boost::tie(vi, vi_end)=boost::vertices(src); vi!=vi_end; ++vi) {
398 src_to_dst[*vi] = boost::add_vertex(dst);
403 typename boost::graph_traits<const FunctionCallGraph>::edge_iterator ei, ei_end;
404 for (boost::tie(ei, ei_end)=boost::edges(src); ei!=ei_end; ++ei) {
405 if (NO_VERTEX!=src_to_dst[boost::source(*ei, src)] && NO_VERTEX!=src_to_dst[boost::target(*ei, src)]) {
409 boost::add_edge(src_to_dst[boost::source(*ei, src)], src_to_dst[boost::target(*ei, src)], dst);
414template<
class FunctionCallGraph>
418 FunctionCallGraph dst;
Class for traversing the AST.
Binary function call analysis.
FunctionCallGraph copy(const FunctionCallGraph &src)
Copies a graph while filtering.
bool is_edge_filtered(SgAsmFunction *src, SgAsmFunction *dst, EdgeFilter *filter)
Determines if an edge is filtered out.
void set_vertex_filter(VertexFilter *filter)
Manipulate the vertex filter.
void cache_vertex_descriptors(const FunctionCallGraph &)
Cache vertex descriptors in AST.
void set_edge_filter(EdgeFilter *filter)
Manipulate the edge filter.
FunctionCallGraph build_cg_from_cfg(const ControlFlowGraph &)
Build a function call graph from a control flow graph.
bool is_vertex_filtered(SgAsmFunction *func, VertexFilter *filter)
Determines if a vertex is filtered out.
EdgeFilter * get_edge_filter() const
Manipulate the edge filter.
FunctionCallGraph build_cg_from_ast(SgNode *root)
Build a function call graph from an AST.
boost::adjacency_list< boost::setS, boost::vecS, boost::bidirectionalS, boost::property< boost::vertex_name_t, SgAsmFunction * > > Graph
The default function call graph type.
VertexFilter * get_vertex_filter() const
Manipulate the vertex filter.
bool is_edge_filtered(SgAsmFunction *src, SgAsmFunction *dst)
Determines if an edge is filtered out.
bool is_vertex_filtered(SgAsmFunction *func)
Determines if a vertex is filtered out.
SgAsmIntegerValuePtrList const & get_successors() const
Property: Control flow successors.
SgAsmFunction * get_enclosingFunction() const
Returns the function that owns this block.
Represents a synthesized function.
rose_addr_t const & get_entryVa() const
Property: Primary entry address.
SgAsmBlock * get_entryBlock() const
Function entry basic block.
rose_addr_t const & get_address() const
Property: Starting virtual address.
This class represents the base class for all IR nodes within Sage III.
Sawyer::Container::Graph< V, E >::VertexValue get_ast_node(const Sawyer::Container::Graph< V, E > &cfg, size_t vertexId)
Return the AST node associated with a vertex.
void put_ast_node(Sawyer::Container::Graph< V, E > &cfg, size_t vertexId, AstNode *astNode)
Set the AST node associated with a vertex.