com.ibm.wala.util.graph.Graph Java Examples

The following examples show how to use com.ibm.wala.util.graph.Graph. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Planner.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public void traverse(Set<Key> neededKeys, Graph<PlanNode> g, Map<PlanNode, ReUseNode> nodesForReUse, PlanNode node, Map<Variable, Variable> accumulatedMappings) {
		
	boolean canReUse = false;

	if (node.getType() == PlanNodeType.STAR) {
		canReUse = reuseStarNode(neededKeys, nodesForReUse, node, accumulatedMappings);
	} else if (node.getType() == PlanNodeType.TRIPLE) {
		canReUse = reuseTripleNode(node.getTriple(), neededKeys, nodesForReUse, node,
				accumulatedMappings);  
		return;
	} else if (node.getType() == PlanNodeType.AND) {
		canReUse = reuseAnd(node, g, nodesForReUse, neededKeys, accumulatedMappings);
		return;
	} 
	
	Iterator<PlanNode> it = g.getSuccNodes(node);
	
	while (it.hasNext()) {
		PlanNode n = it.next();
		traverse(neededKeys, g, nodesForReUse, n, accumulatedMappings);
		
	}
				
}
 
Example #2
Source File: ReachabilityAnalyzer.java    From steady with Apache License 2.0 6 votes vote down vote up
private void writeToDisk(String _file, Graph<ConstructId> _g) {
    try {
        // Create all parent dirs
        final Path p = Paths.get(_file);
        FileUtil.createDirectory(p.getParent());

        // Write object
        final File f = new File(_file);
        try (final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
            oos.writeObject(_g);
            log.info("Wrote call graph with [" + _g.getNumberOfNodes() + "] nodes to [" + _file + "]");
        }
    } catch (IOException ioe) {
        log.error("I/O error when writing object to [" + _file + "]: " + ioe.getMessage(), ioe);
    }
}
 
Example #3
Source File: TaxonomyImpl.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void build() {
	//build taxonomies
	logger.debug("building data property taxonomy ...");
	dataPropTaxo = buildDataPropertyHierarchy(tbox, additionalDataPropertyExpressions);
	logger.debug("building object property taxonomy ...");
	objPropTaxo = buildObjectPropertyHierarchy(tbox, additionalObjectPropertyExpressions);
	
	logger.debug("building class taxonomy ...");
	classTaxo = buildClassHierarchy(tbox, additionalClassExpressions);
	
	logger.debug("All taxonomies built!");
	//
	Graph<TaxonomyBuilder<OWLPropertyExpression>.TaxoNode> lattice = objPropTaxo.getLattice();
	logger.debug("{}", lattice);
	
}
 
Example #4
Source File: SootCallgraphConstructor.java    From steady with Apache License 2.0 5 votes vote down vote up
/**
 * Normalizing a soot callgraph to a general graph represented by ConstructId
 *
 * @return a {@link com.ibm.wala.util.graph.Graph} object.
 */
public Graph<com.sap.psr.vulas.shared.json.model.ConstructId> getCallgraph() {
    final Graph<com.sap.psr.vulas.shared.json.model.ConstructId> graph = SlowSparseNumberedGraph.make();

    if (this.callgraph != null) {
        int edges_no = 0;
        com.sap.psr.vulas.shared.json.model.ConstructId src_cid = null, tgt_cid = null;
        MethodOrMethodContext src_node = null;
        Iterator<Edge> edges = null;

        final Iterator<MethodOrMethodContext> src_nodes = callgraph.sourceMethods();
        while (src_nodes.hasNext()) {
            src_node = src_nodes.next();
            src_cid = getCid(src_node.method());
            graph.addNode(src_cid);

            //add edges
            edges = this.callgraph.edgesOutOf(src_node);
            while (edges.hasNext()) {
                tgt_cid = getCid(edges.next().tgt());
                graph.addNode(tgt_cid);
                if (!graph.hasEdge(src_cid, tgt_cid)) {
                    graph.addEdge(src_cid, tgt_cid);
                    edges_no++;
                }
            }
        }

        SootCallgraphConstructor.log.info("Normalized call graph has [" + graph.getNumberOfNodes() + " nodes] (with distinct ConstructId) and [" + edges_no + "] edges");
    }
    // No callgraph exists
    else {
        throw new IllegalStateException("There exists no call graph");
    }
    return graph;
}
 
Example #5
Source File: Planner.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
private void getAndSuccessors(Graph<PlanNode> g, PlanNode n, List<PlanNode> successors) {
	Iterator<PlanNode> succs = g.getSuccNodes(n);
	if (succs.hasNext()) {
		PlanNode left = succs.next();
		PlanNode right = succs.next();
	
		getAndSuccessors(g, left , successors);
		getAndSuccessors(g, right, successors);
	}
	
	if (n.getType() == PlanNodeType.AND || n.getType() == PlanNodeType.STAR || n.getType() == PlanNodeType.TRIPLE) {
		successors.add(n);
	}
}
 
Example #6
Source File: ReachabilityAnalyzer.java    From steady with Apache License 2.0 5 votes vote down vote up
private Graph<ConstructId> readFromDisk(String _file) {
    try {
        try (final ObjectInputStream ois = new ObjectInputStream(new FileInputStream(_file))) {
            final Object object = ois.readObject();
            @SuppressWarnings("unchecked") final Graph<ConstructId> g = (Graph<ConstructId>) object;
            log.info("Read call graph with [" + g.getNumberOfNodes() + "] nodes from [" + _file + "]");
            return g;
        }
    } catch (IOException ioe) {
        log.error("I/O error when reading object from [" + _file + "]: " + ioe.getMessage(), ioe);
    } catch (ClassNotFoundException cnfe) {
        log.error("Class not found when reading object from [" + _file + "]: " + cnfe.getMessage(), cnfe);
    }
    return null;
}
 
Example #7
Source File: WalaCallgraphConstructor.java    From steady with Apache License 2.0 5 votes vote down vote up
/**
 * Normalizing a wala callgraph to a general graph represented by ConstructId
 *
 * @return a {@link com.ibm.wala.util.graph.Graph} object.
 */
public Graph<com.sap.psr.vulas.shared.json.model.ConstructId> getCallgraph() {
    Graph<com.sap.psr.vulas.shared.json.model.ConstructId> graph = SlowSparseNumberedGraph.make();

    if (this.callgraph != null) {
        int edges_no = 0;
        Iterator<CGNode> nodes = this.callgraph.iterator();
        CGNode srcnode = null, tgtnode = null;
        com.sap.psr.vulas.shared.json.model.ConstructId src_cid = null, tgt_cid = null;
        Iterator<CGNode> succNodes = null;
        while (nodes.hasNext()) {
            srcnode = nodes.next();
            src_cid = getCid(srcnode.getMethod());
            graph.addNode(src_cid);
            //add edges
            succNodes = this.callgraph.getSuccNodes(srcnode);
            while (succNodes.hasNext()) {
                tgtnode = succNodes.next();
                tgt_cid = getCid(tgtnode.getMethod());
                graph.addNode(tgt_cid);
                if (!graph.hasEdge(src_cid, tgt_cid)) {
                    graph.addEdge(src_cid, tgt_cid);
                    edges_no++;
                }
            }
        }
        WalaCallgraphConstructor.log.info("Normalized call graph has [" + graph.getNumberOfNodes() + " nodes] (with distinct ConstructId) and [" + edges_no + " edges]");
    }
    // No callgrpah exists
    else {
        throw new IllegalStateException("There exists no call graph");
    }
    return graph;
}
 
Example #8
Source File: DefinitelyDerefedParams.java    From NullAway with MIT License 5 votes vote down vote up
@NotNull
private Set<Integer> computeDerefParamListUsingPDom(int numParam, int firstParamIndex) {
  Set<Integer> derefedParamList = new HashSet<>();
  // Get Dominator Tree
  LOG(DEBUG, "DEBUG", "\tbuilding dominator tree...");
  Graph<ISSABasicBlock> domTree = Dominators.make(prunedCFG, prunedCFG.entry()).dominatorTree();
  // Get Post-dominator Tree
  Graph<ISSABasicBlock> pdomTree = GraphInverter.invert(domTree);
  LOG(DEBUG, "DEBUG", "post-dominator tree:" + pdomTree.toString());
  // Note: WALA creates a single dummy exit node. Multiple exits points will never post-dominate
  // this exit node. (?)
  // TODO: [v0.2] Need data-flow analysis for dereferences on all paths
  // Walk from exit node in post-dominator tree and check for use of params
  LOG(DEBUG, "DEBUG", "\tfinding dereferenced params...");
  ArrayList<ISSABasicBlock> nodeQueue = new ArrayList<ISSABasicBlock>();
  nodeQueue.add(prunedCFG.exit());
  LOG(DEBUG, "DEBUG", "param value numbers : " + firstParamIndex + " ... " + numParam);
  while (!nodeQueue.isEmpty()) {
    ISSABasicBlock node = nodeQueue.get(0);
    nodeQueue.remove(node);
    // check for use of params
    checkForUseOfParams(derefedParamList, numParam, firstParamIndex, node);
    for (ISSABasicBlock succ : Iterator2Iterable.make(pdomTree.getSuccNodes(node))) {
      nodeQueue.add(succ);
    }
  }
  LOG(DEBUG, "DEBUG", "\tdone...");
  return derefedParamList;
}
 
Example #9
Source File: Planner.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public PlanNode join(JoinTypes type, Query query, Graph<PlanNode> plan, PlanNode lhs, PlanNode rhs, Set<Variable> liveVars) {
	plan.addNode(rhs);
	if (lhs == null) {
		return rhs;
	} else if (type.type() == PlanNodeType.AND && !BINARY_PLANS && lhs.getType() == PlanNodeType.AND) {
		plan.addEdge(lhs, rhs);
		return lhs;
	} else {
		Set<Variable> operator = HashSetFactory.make(lhs.getAvailableVariables());
		Set<Variable> x = HashSetFactory.make(rhs.getRequiredVariables());
		x.addAll(rhs.getProducedVariables());
		operator.retainAll(x);
					
		PlanNode and = planFactory.createSTPlanNode(
				type.type(),
				Collections.<Variable> emptySet(),
				query.getMainPattern());
		Set<Variable> availableVariables; 
		if (lhs.getAvailableVariables() == null) {
			availableVariables = HashSetFactory.make();
		} else {
			availableVariables = HashSetFactory.make(lhs.getAvailableVariables());
		}
		if (rhs.getProducedVariables() != null && !type.equals(JoinTypes.NOT_EXISTS) && !type.equals(JoinTypes.EXISTS)) {
			availableVariables.addAll(rhs.getProducedVariables());
		}
		Set<Variable> allVars = HashSetFactory.make(liveVars);
		availableVariables.retainAll(allVars);
		
		and.setProducedVariables(HashSetFactory.make(availableVariables));
		and.setAvailableVariables(HashSetFactory.make(availableVariables));
		
		//assert !operator.isEmpty();
		and.setOperatorsVariables(operator);
									
		plan.addNode(and);
		plan.addEdge(and, lhs);
		plan.addEdge(and, rhs);
		
		and.cost = add(lhs.cost, rhs.cost);

		if (lhs.getFilters() != null || rhs.getFilters() != null) {
			List<Expression> filters = lhs.getFilters()!=null? new LinkedList<Expression>(lhs.getFilters()): new LinkedList<Expression>();
			if (type.type() == PlanNodeType.AND && rhs.getFilters() != null) {
				filters.addAll(rhs.getFilters());
			}
			and.setFilters(filters);
		}
		
		return and;
	}
}
 
Example #10
Source File: Planner.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public Graph<PlanNode> topPlan() {
	return topPlan.getPlanTree();
}
 
Example #11
Source File: Planner.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
private boolean reuseAnd(PlanNode node, Graph<PlanNode> g, Map<PlanNode, ReUseNode> nodesForReUse, Set<Key> neededKeys, Map<Variable, Variable> accumulatedMappings) {
	Iterator<PlanNode> succs = g.getSuccNodes(node);
	PlanNode left = succs.next();
	PlanNode right = succs.next();
	
	// check if we have the right types of successors, and if we can re-use this AND potentially
	if (! (left.getType() == PlanNodeType.AND || left.getType() == PlanNodeType.STAR || left.getType() == PlanNodeType.TRIPLE)) {
		return false;
	}
	
	if (!left.getRequiredVariables().isEmpty() || !node.getRequiredVariables().isEmpty()) {
		return false;
	}
	
	boolean reuseLeft = false;
	// reuse the code
	if (left.getType() == PlanNodeType.STAR) {
		reuseLeft = reuseStarNode(neededKeys, nodesForReUse, left, accumulatedMappings);
	} else if (left.getType() == PlanNodeType.AND) {
		reuseLeft = reuseAnd(left, g, nodesForReUse, neededKeys, accumulatedMappings);
	} else if (left.getType() == PlanNodeType.TRIPLE) {
		reuseLeft = reuseTripleNode(left.getTriple(), neededKeys, nodesForReUse, left, accumulatedMappings);

	}
	
	if (!reuseLeft) {
		return false;
	}
	

	boolean reuseRight = false;
	
	if (right.getType() == PlanNodeType.TRIPLE) {
		reuseRight = reuseTripleNode(right.getTriple(), neededKeys, nodesForReUse, right, accumulatedMappings);
	} else if (right.getType() == PlanNodeType.STAR) {
		reuseRight = reuseStarNode(neededKeys, nodesForReUse, right, accumulatedMappings);
	} else if (right.getType() == PlanNodeType.AND) {
		reuseRight = reuseAnd(right, g, nodesForReUse, neededKeys, accumulatedMappings);
	}
	

	if (!reuseRight) {
		return false;
	}
	

	// check all successors transitively, ensuring that every triple in the AND list respects the variable mappings at the level of each query triple
	// add keys
	List<PlanNode> successors = new LinkedList<PlanNode>();
	Set<Key> succKeys = new HashSet<Key>();

	getAndSuccessors(g, node, successors);
	

	for (PlanNode n : successors) {
		if (n == node) {
			continue;
		}
		assert n.getType() == PlanNodeType.TRIPLE || n.getType() == PlanNodeType.AND || n.getType() == PlanNodeType.STAR;


		assert nodesForReUse.containsKey(n) : "cant reuse node:" + node + " because " + n  + " n is not found ";
		ReUseNode rn = nodesForReUse.get(n);

		if (n.getType() == PlanNodeType.TRIPLE) {
			QueryTriple qt = n.getTriple();
			assert rn.getKeys().size() == 1;
			Key k = rn.getKeys().iterator().next();
			
			assert k instanceof QueryTriple;
			QueryTriple other = (QueryTriple) k;
			if (!qt.isEquivalentTo(other, accumulatedMappings)) {
				return false;
			}
		} 
		
		succKeys.addAll(rn.getKeys());
	}

	nodesForReUse.put(node, new ReUseNode(node, accumulatedMappings, succKeys));			
	return true;
}
 
Example #12
Source File: TaxonomyBuilder.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
protected Graph<TaxoNode> getLattice() {
	return lattice;
}
 
Example #13
Source File: Callgraph.java    From steady with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a callgraph and populates an internal map of constructs and integer indices (which allow faster processing and more compact representations).
 *
 * @param _g a {@link com.ibm.wala.util.graph.Graph} object.
 */
public Callgraph (Graph<com.sap.psr.vulas.shared.json.model.ConstructId> _g) {
	if( _g!=null ) {
		
		// The problems described in Jira ticket VULAS-1429 look as if the caches survive A2C executions on different modules. Check and clear explicitly.
		if(!this.cachedJarUrls.isEmpty()) {
			log.warn("JAR URL cache not empty, clearing now...");
			this.cachedJarUrls.clear();
		}
		if(!this.jarAnalyzersCache.isEmpty()) {
			log.warn("JarAnalyzer cache not empty, clearing now ...");
			this.jarAnalyzersCache.clear();
		}
		
		Iterator<com.sap.psr.vulas.shared.json.model.ConstructId> iter = _g.iterator();
		com.sap.psr.vulas.shared.json.model.ConstructId src_node = null, tgt_node = null;
		Iterator<com.sap.psr.vulas.shared.json.model.ConstructId> succNodes = null;
		Integer src_id = null, tgt_id = null, count = -1;

		// Populate the map of constructs and integers
		while (iter.hasNext()) {
			this.nodeCount++;
			src_node = iter.next();
			src_id = this.nodeMap.get(src_node);
			if(src_id == null) {
				src_id = ++count;
				this.nodeMap.put(src_node, src_id);
				this.nodeInfoMap.put(src_id, this.createNodeMetaInformation(src_node, src_id));
				this.nodeId.add(src_node);
				this.idgraph.addNode(src_id);
			}
			//targets
			succNodes = _g.getSuccNodes(src_node);
			while(succNodes.hasNext()){
				this.edgeCount++;
				tgt_node = succNodes.next();
				tgt_id = this.nodeMap.get(tgt_node);
				if(tgt_id == null) {
					tgt_id = ++count;
					this.nodeMap.put(tgt_node, tgt_id);
					this.nodeInfoMap.put(tgt_id, this.createNodeMetaInformation(tgt_node, tgt_id));
					this.nodeId.add(tgt_node);
					this.idgraph.addNode(tgt_id);
				}
				//add edges
				this.idgraph.addEdge(src_id, tgt_id);
			}
		}
		Callgraph.log.info("Built Graph<Integer> of " + this.idgraph.getNumberOfNodes() + " nodes");
	}
}
 
Example #14
Source File: PrunedGraphGetPaths.java    From steady with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Constructor for PrunedGraphGetPaths.</p>
 *
 * @param _graph a {@link com.ibm.wala.util.graph.Graph} object.
 * @param _nodeid a {@link java.util.ArrayList} object.
 */
public PrunedGraphGetPaths(Graph<Integer> _graph, ArrayList<ConstructId> _nodeid) {
	super(_graph, _nodeid);
}
 
Example #15
Source File: ICallgraphConstructor.java    From steady with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the call graph.
 *
 * @return a {@link com.ibm.wala.util.graph.Graph} object.
 */
public Graph<com.sap.psr.vulas.shared.json.model.ConstructId> getCallgraph();
 
Example #16
Source File: AbstractGetPaths.java    From steady with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Constructor for AbstractGetPaths.</p>
 *
 * @param _graph a {@link com.ibm.wala.util.graph.Graph} object.
 * @param _nodeid a {@link java.util.ArrayList} object.
 */
public AbstractGetPaths(Graph<Integer> _graph, ArrayList<ConstructId> _nodeid) {
	this.graph = _graph;
	this.nodeId = _nodeid;
}
 
Example #17
Source File: Callgraph.java    From steady with Apache License 2.0 2 votes vote down vote up
/**
 * <p>getGraph.</p>
 *
 * @return a {@link com.ibm.wala.util.graph.Graph} object.
 */
public Graph<Integer> getGraph() {
	return this.idgraph;
}
 
Example #18
Source File: DepthFirstGetPaths.java    From steady with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Constructor for DepthFirstGetPaths.</p>
 *
 * @param _graph a {@link com.ibm.wala.util.graph.Graph} object.
 * @param _nodeid a {@link java.util.ArrayList} object.
 */
public DepthFirstGetPaths(Graph<Integer> _graph, ArrayList<ConstructId> _nodeid) {
	super(_graph, _nodeid);
}
 
Example #19
Source File: Planner.java    From quetzal with Eclipse Public License 2.0 votes vote down vote up
Graph<PlanNode> topPlan();