Skip to content

Ghidra is a software reverse engineering (SRE) framework

Partial fix for GP-625

Tom Nelsonf4defc4a9531
diff --git a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/DefaultGraphDisplay.java b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/DefaultGraphDisplay.java
index e2bc4e9098..7eef3f189f 100644
--- a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/DefaultGraphDisplay.java
+++ b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/DefaultGraphDisplay.java
@@ -80,13 +80,10 @@ public class DefaultGraphDisplay implements GraphDisplay {
private static final String ACTION_OWNER = "GraphServices";
- private static final String FAVORED_EDGE = "Fall-Through";
-
/*
- A handful of jungrapht properties that re used by this graph
+ jungrapht properties that are used by this graph
*/
private static final String SELECTED_VERTEX_COLOR = "selectedVertexColor";
- private static final String SELECTED_EDGE_COLOR = "selectedEdgeColor";
private static final String INITIAL_LAYOUT_ALGORITHM = "initialLayoutAlgorithm";
private static final String DISPLAY_VERTICES_AS_ICONS = "displayVerticesAsIcons";
private static final String VERTEX_LABEL_POSITION = "vertexLabelPosition";
@@ -227,11 +224,6 @@ public class DefaultGraphDisplay implements GraphDisplay {
"0xFF0000"));
}
-// private Color getSelectedEdgeColor() {
-// return Colors
-// .getHexColor(displayProperties.getOrDefault(SELECTED_EDGE_COLOR, "0xFF0000"));
-// }
-
JComponent getComponent() {
JComponent component = viewer.getComponent();
component.setFocusable(true);
@@ -257,7 +249,7 @@ public class DefaultGraphDisplay implements GraphDisplay {
.getTransformer(VIEW);
MagnifyShapeTransformer shapeTransformer = MagnifyShapeTransformer.builder(lens)
- // this lens' delegate is the viewer's VIEW layer
+ // this lens' delegate is the viewer's VIEW layer, abandoned above
.delegate(transformer)
.build();
LensGraphMouse lensGraphMouse =
@@ -928,9 +920,6 @@ public class DefaultGraphDisplay implements GraphDisplay {
focusedVertex = null;
graph = attributedGraph;
- layoutTransitionManager.setEdgeComparator(new EdgeComparator(graph, "EdgeType",
- DefaultGraphDisplay.FAVORED_EDGE));
-
configureViewerPreferredSize();
Swing.runNow(() -> {
diff --git a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/EdgeComparator.java b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/EdgeComparator.java
index 8c193902eb..607177defb 100644
--- a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/EdgeComparator.java
+++ b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/EdgeComparator.java
@@ -15,33 +15,44 @@
*/
package ghidra.graph.visualization;
-import java.util.*;
-import java.util.stream.Collectors;
-
import ghidra.service.graph.AttributedEdge;
-import ghidra.service.graph.AttributedGraph;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * {@code Comparator} to order {@code AttributedEdge}s based on their position in a
+ * supplied {@code List}.
+ *
+ */
public class EdgeComparator implements Comparator<AttributedEdge> {
- private final Set<AttributedEdge> prioritized;
- public EdgeComparator(AttributedGraph graph, String attributeName, String value) {
- prioritized = graph.edgeSet()
- .stream()
- .filter(e -> Objects.equals(e.getAttribute(attributeName), value))
- .collect(Collectors.toSet());
+ /**
+ * {@code Map} of EdgeType attribute value to integer priority
+ */
+ private Map<String, Integer> edgePriorityMap = new HashMap();
+
+ /**
+ * Create an instance and place the list values into the {@code edgePriorityMap}
+ * with a one-up counter expressing their relative priority
+ * @param edgePriorityList
+ */
+ public EdgeComparator(List<String> edgePriorityList) {
+ edgePriorityList.forEach(s -> edgePriorityMap.put(s, edgePriorityList.indexOf(s)));
}
+ /**
+ * {@inheritdoc}
+ * Compares the {@code AttributedEdge}s using their priority in the supplied {@code edgePriorityMap}
+ */
@Override
public int compare(AttributedEdge edgeOne, AttributedEdge edgeTwo) {
- boolean edgeOnePriority = prioritized.contains(edgeOne);
- boolean edgeTwoPriority = prioritized.contains(edgeTwo);
- if (edgeOnePriority && !edgeTwoPriority) {
- return -1;
- }
- else if (!edgeOnePriority && edgeTwoPriority) {
- return 1;
- }
- return 0;
+ return priority(edgeOne).compareTo(priority(edgeTwo));
}
+ private Integer priority(AttributedEdge e) {
+ return edgePriorityMap.getOrDefault(e.getAttribute("EdgeType"), 0);
+ }
}
diff --git a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/LayoutFunction.java b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/LayoutFunction.java
index a37f94c048..c248961f26 100644
--- a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/LayoutFunction.java
+++ b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/LayoutFunction.java
@@ -98,8 +98,8 @@ class LayoutFunction
.<AttributedVertex> builder()
.verticalVertexSpacing(300);
case TREE:
- return TreeLayoutAlgorithm
- .builder();
+ return EdgeAwareTreeLayoutAlgorithm
+ .<AttributedVertex, AttributedEdge>edgeAwareBuilder();
case TIDIER_TREE:
default:
return TidierTreeLayoutAlgorithm
diff --git a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/LayoutTransitionManager.java b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/LayoutTransitionManager.java
index dc814676ca..c500ba8f51 100644
--- a/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/LayoutTransitionManager.java
+++ b/Ghidra/Features/GraphServices/src/main/java/ghidra/graph/visualization/LayoutTransitionManager.java
@@ -18,6 +18,7 @@ package ghidra.graph.visualization;
import static ghidra.graph.visualization.LayoutFunction.*;
import java.util.Comparator;
+import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -48,10 +49,23 @@ class LayoutTransitionManager {
*/
Predicate<AttributedVertex> rootPredicate;
+ public static final List<String> EDGE_PRIORITY_LIST =
+ List.of(
+ "Fall-Through",
+ "Conditional-Return",
+ "Unconditional-Jump",
+ "Conditional-Jump",
+ "Unconditional-Call",
+ "Conditional-Call",
+ "Terminator",
+ "Computed",
+ "Indirection",
+ "Entry");
/**
* a {@link Comparator} to sort edges during layout graph traversal
+ * The default uses the {@code EDGE_PRIORTITY_LIST }
*/
- Comparator<AttributedEdge> edgeComparator = (e1, e2) -> 0;
+ Comparator<AttributedEdge> edgeComparator = new EdgeComparator(EDGE_PRIORITY_LIST);
/**
* a {@link Function} to provide {@link Rectangle} (and thus bounds} for vertices
diff --git a/Ghidra/Features/GraphServices/src/main/resources/jungrapht.properties b/Ghidra/Features/GraphServices/src/main/resources/jungrapht.properties
index 92ee458d74..707a0815ae 100644
--- a/Ghidra/Features/GraphServices/src/main/resources/jungrapht.properties
+++ b/Ghidra/Features/GraphServices/src/main/resources/jungrapht.properties
@@ -42,7 +42,7 @@ jungrapht.mincross.verticalOffset=5
# how many times to run the full all-level cross count
jungrapht.mincross.maxLevelCross=10
-pass
+
# how many times to iterate over the layers while swapping node positions (mincross)
jungrapht.mincrossTransposeLimit=5