Ghidra is a software reverse engineering (SRE) framework
GP-1029 - Function Graph - fixed potential infinite loop when rendering edge arrows
dragonmacherb78cfb6e258f
diff --git a/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/edge/VisualEdgeArrowRenderingSupport.java b/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/edge/VisualEdgeArrowRenderingSupport.javanew file mode 100644index 0000000000..cee4fb5108--- /dev/null+++ b/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/edge/VisualEdgeArrowRenderingSupport.java@@ -0,0 +1,120 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.graph.viewer.edge;++import java.awt.Shape;+import java.awt.geom.*;++import edu.uci.ics.jung.visualization.RenderContext;+import ghidra.graph.viewer.VisualEdge;+import ghidra.graph.viewer.VisualVertex;++/**+ * Basic class to calculate the position of an edge arrow+ *+ * @param <V> the vertex type+ * @param <E> the edge type+ */+public class VisualEdgeArrowRenderingSupport<V extends VisualVertex, E extends VisualEdge<V>> {++ public AffineTransform createArrowTransform(RenderContext<V, E> rc, Shape edgeShape,+ Shape vertexShape) {++ return doCreateArrowTransform(rc.getArrowPlacementTolerance(), edgeShape, vertexShape);+ }++ AffineTransform doCreateArrowTransform(double arrowPlacementTolerance, Shape edgeShape,+ Shape vertexShape) {++ GeneralPath path = new GeneralPath(edgeShape);+ double[] seg = new double[6];+ Point2D p1 = null;+ Point2D p2 = null;+ AffineTransform at = new AffineTransform();++ // Find the line segment whose endpoint is in the end vertex (this handles straight lines+ // and articulated lines). Use that line's start point to for the arrow position and use+ // the line's angle to rotate the arrow head.+ for (PathIterator i = path.getPathIterator(null, 1); !i.isDone(); i.next()) {+ int type = i.currentSegment(seg);+ if (type == PathIterator.SEG_MOVETO) {+ p2 = new Point2D.Double(seg[0], seg[1]);+ }+ else if (type == PathIterator.SEG_LINETO) {+ p1 = p2;+ p2 = new Point2D.Double(seg[0], seg[1]);+ if (vertexShape.contains(p2)) {+ Line2D lineSegment = new Line2D.Double(p1, p2);+ Line2D line =+ findClosestLineSegment(arrowPlacementTolerance, lineSegment, vertexShape);+ return createArrowTransformFromLine(line);+ }+ }+ }+ return at;+ }++ Line2D findClosestLineSegment(double arrowPlacementTolerance, Line2D line, Shape vertexShape) {++ if (!vertexShape.contains(line.getP2())) {+ String errorString = "line end point: " + line.getP2() ++ " is not contained in shape: " + vertexShape.getBounds2D();+ throw new IllegalArgumentException(errorString);+ }++ Line2D left = new Line2D.Double();+ Line2D right = new Line2D.Double();++ // keep chopping the line in half until it is small enough and lands just outside of the+ // vertex shape, within the provided tolerance+ int iterations = 0; // arbitrary limit to ensure rounding errors do not loop forever+ while (lengthSquared(line) > arrowPlacementTolerance && iterations++ < 15) {+ bisect(line, left, right);+ line = vertexShape.contains(right.getP1()) ? left : right;+ }+ return line;+ }++ private double lengthSquared(Line2D line) {+ double dx = line.getX1() - line.getX2();+ double dy = line.getY1() - line.getY2();+ return dx * dx + dy * dy;+ }++ private void bisect(Line2D src, Line2D left, Line2D right) {++ double x1 = src.getX1();+ double y1 = src.getY1();+ double x2 = src.getX2();+ double y2 = src.getY2();+ double mx = x1 + (x2 - x1) / 2.0;+ double my = y1 + (y2 - y1) / 2.0;+ left.setLine(x1, y1, mx, my);+ right.setLine(mx, my, x2, y2);+ }++ private AffineTransform createArrowTransformFromLine(Line2D line) {+ double x1 = line.getX1();+ double y1 = line.getY1();+ double dx = x1 - line.getX2();+ double dy = y1 - line.getY2();+ double atheta = Math.atan2(dx, dy) + Math.PI / 2;+ AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);+ at.rotate(-atheta);+ return at;+ }++}diff --git a/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/edge/VisualEdgeRenderer.java b/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/edge/VisualEdgeRenderer.javaindex d75108ad67..3109df70f5 100644--- a/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/edge/VisualEdgeRenderer.java+++ b/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/edge/VisualEdgeRenderer.java@@ -29,7 +29,7 @@ import edu.uci.ics.jung.graph.Graph;import edu.uci.ics.jung.graph.util.Context;import edu.uci.ics.jung.graph.util.Pair;import edu.uci.ics.jung.visualization.*;-import edu.uci.ics.jung.visualization.renderers.*;+import edu.uci.ics.jung.visualization.renderers.BasicEdgeRenderer;import edu.uci.ics.jung.visualization.transform.LensTransformer;import edu.uci.ics.jung.visualization.transform.MutableTransformer;import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;@@ -91,6 +91,9 @@ public abstract class VisualEdgeRenderer<V extends VisualVertex, E extends Visuaprivate Color defaultBaseColor = Color.BLACK;private Color defaultHighlightColor = Color.GRAY;+ private VisualEdgeArrowRenderingSupport<V, E> arrowRenderingSupport =+ new VisualEdgeArrowRenderingSupport<>();+/*** Sets the offset value for painting dashed lines. This allows clients to animate the* lines being drawn for edges in the edge direction.@@ -171,7 +174,7 @@ public abstract class VisualEdgeRenderer<V extends VisualVertex, E extends VisuaColor selectedColor = highlightColor.darker(); // note: we can do better for selected colorColor selectedAccentColor = highlightColor;- float scale = StrictMath.min(scalex, scaley);+ float scale = Math.min(scalex, scaley);Point2D p1 = layout.apply(v1);Point2D p2 = layout.apply(v2);@@ -313,6 +316,11 @@ public abstract class VisualEdgeRenderer<V extends VisualVertex, E extends Visua//g.setPaint(Color.ORANGE);//g.draw(shapeBounds);+ // can add this feature as needed to speed up painting+ //if (scale < .3) {+ // return;+ //}+//// Arrow Head//@@ -330,16 +338,15 @@ public abstract class VisualEdgeRenderer<V extends VisualVertex, E extends Visua}Shape vs2 = getVertexShapeForArrow(rc, layout, v2); // end vertex+boolean arrowHit = vt.transform(vs2).intersects(deviceRectangle);if (!arrowHit) {g.setPaint(oldPaint);return;}- EdgeArrowRenderingSupport<V, E> arrowRenderingSupport =- new BasicEdgeArrowRenderingSupport<>();- AffineTransform at = arrowRenderingSupport.getArrowTransform(rc, edgeShape, vs2);- if (at == null) {+ AffineTransform at = arrowRenderingSupport.createArrowTransform(rc, edgeShape, vs2);+ if (at == null || at.isIdentity()) {g.setPaint(oldPaint);g.setStroke(oldArrowStroke);return;diff --git a/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/renderer/ArticulatedEdgeRenderer.java b/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/renderer/ArticulatedEdgeRenderer.javaindex 1195fa4848..bcc984c1f6 100644--- a/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/renderer/ArticulatedEdgeRenderer.java+++ b/Ghidra/Framework/Graph/src/main/java/ghidra/graph/viewer/renderer/ArticulatedEdgeRenderer.java@@ -53,11 +53,14 @@ public class ArticulatedEdgeRenderer<V extends VisualVertex, E extends VisualEdgList<Point2D> articulations = e.getArticulationPoints();offset = updateOffsetForLeftOrRightHandSizeEdge(rc, offset, x1, articulations);for (Point2D point : articulations) {- Point2D offsetPoint =- new Point2D.Float((float) point.getX() + offset, (float) point.getY() + offset);+ double x = point.getX();+ double y = point.getY();+ Point2D offsetPoint = new Point2D.Double(x + offset, y + offset);point = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, offsetPoint);- path.lineTo((float) point.getX(), (float) point.getY());- path.moveTo((float) point.getX(), (float) point.getY());++ x = point.getX();+ y = point.getY();+ path.lineTo(x, y);}path.lineTo(x2, y2);diff --git a/Ghidra/Framework/Graph/src/test/java/ghidra/graph/viewer/edge/VisualEdgeArrowRenderingSupportTest.java b/Ghidra/Framework/Graph/src/test/java/ghidra/graph/viewer/edge/VisualEdgeArrowRenderingSupportTest.javanew file mode 100644index 0000000000..fd78e995eb--- /dev/null+++ b/Ghidra/Framework/Graph/src/test/java/ghidra/graph/viewer/edge/VisualEdgeArrowRenderingSupportTest.java@@ -0,0 +1,71 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.graph.viewer.edge;++import java.awt.geom.Line2D;+import java.awt.geom.Rectangle2D;++import org.junit.Test;++import ghidra.graph.graphs.TestEdge;+import ghidra.graph.graphs.TestVertex;++public class VisualEdgeArrowRenderingSupportTest {++ private double width = 100.0;+ private double height = 100.0;++ private VisualEdgeArrowRenderingSupport<TestVertex, TestEdgeX> arrowSupport =+ new VisualEdgeArrowRenderingSupport<>();++ @Test+ public void testInfiniteLoopOnBisect() {++ // Values are large enough to pass the initial tolerance test, but then precise enough+ // to get stuck in the bisect loop. Using power of two for values so that our ulp()+ // can be as precise as possible as well.+ double rxf = 8.0f;+ double ryf = 8.0f;++ double rx = rxf + Math.ulp(rxf);+ double ry = ryf + Math.ulp(ryf);++ Rectangle2D vertex = new Rectangle2D.Double(rx, ry, width, height);+ Line2D edge = new Line2D.Double(0.00, 0.00, rx, ry);++ // We don't need the return value for this test+ arrowSupport.findClosestLineSegment(5.0f, edge, vertex);+ }++//==================================================================================================+// Private Class+//==================================================================================================++ class TestEdgeX extends AbstractVisualEdge<TestVertex> {++ public TestEdgeX(TestVertex start, TestVertex end) {+ super(start, end);+ }++ @SuppressWarnings("unchecked")+ // Suppressing warning on the return type; we know our class is the right type+ @Override+ public TestEdge cloneEdge(TestVertex start, TestVertex end) {+ return new TestEdge(start, end);+ }+ }++}