From 5d8dce0ed25a0e72964aeaeac6c017584745e4ff Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 9 Apr 2020 19:46:15 +0100 Subject: [PATCH] Switch from Guava graphs to JGraphT Signed-off-by: Graham --- buildSrc/src/main/java/Versions.kt | 1 + deob/build.gradle.kts | 1 + .../deob/analysis/ControlFlowAnalyzer.kt | 21 ++++++++++--------- .../openrs2/deob/analysis/DataFlowAnalyzer.kt | 15 ++++++------- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/buildSrc/src/main/java/Versions.kt b/buildSrc/src/main/java/Versions.kt index bd6cf07f21..7fdc0a145c 100644 --- a/buildSrc/src/main/java/Versions.kt +++ b/buildSrc/src/main/java/Versions.kt @@ -11,6 +11,7 @@ object Versions { const val inlineLogger = "1.0.2" const val javaParser = "3.15.18" const val jdom = "2.0.6" + const val jgrapht = "1.4.0" const val jimfs = "1.1" const val junit = "5.6.1" const val kotlin = "1.3.71" diff --git a/deob/build.gradle.kts b/deob/build.gradle.kts index b752d39cae..cf2c8b1760 100644 --- a/deob/build.gradle.kts +++ b/deob/build.gradle.kts @@ -15,6 +15,7 @@ dependencies { implementation(project(":deob-annotations")) implementation("com.google.guava:guava:${Versions.guava}") implementation("it.unimi.dsi:fastutil:${Versions.fastutil}") + implementation("org.jgrapht:jgrapht-core:${Versions.jgrapht}") } publishing { diff --git a/deob/src/main/java/dev/openrs2/deob/analysis/ControlFlowAnalyzer.kt b/deob/src/main/java/dev/openrs2/deob/analysis/ControlFlowAnalyzer.kt index d75b8ef708..5394113e7b 100644 --- a/deob/src/main/java/dev/openrs2/deob/analysis/ControlFlowAnalyzer.kt +++ b/deob/src/main/java/dev/openrs2/deob/analysis/ControlFlowAnalyzer.kt @@ -1,29 +1,30 @@ package dev.openrs2.deob.analysis -import com.google.common.graph.Graph -import com.google.common.graph.GraphBuilder +import org.jgrapht.Graph +import org.jgrapht.graph.AsUnmodifiableGraph +import org.jgrapht.graph.DefaultDirectedGraph +import org.jgrapht.graph.DefaultEdge import org.objectweb.asm.tree.MethodNode import org.objectweb.asm.tree.analysis.Analyzer import org.objectweb.asm.tree.analysis.BasicInterpreter import org.objectweb.asm.tree.analysis.BasicValue class ControlFlowAnalyzer : Analyzer(BasicInterpreter()) { - private val graph = GraphBuilder - .directed() - .allowsSelfLoops(true) - .immutable() + private val graph = DefaultDirectedGraph(DefaultEdge::class.java) override fun newControlFlowEdge(insnIndex: Int, successorIndex: Int) { - graph.putEdge(insnIndex, successorIndex) + graph.addVertex(insnIndex) + graph.addVertex(successorIndex) + graph.addEdge(insnIndex, successorIndex) } override fun newControlFlowExceptionEdge(insnIndex: Int, successorIndex: Int): Boolean { - graph.putEdge(insnIndex, successorIndex) + newControlFlowEdge(insnIndex, successorIndex) return true } - fun createGraph(owner: String, method: MethodNode): Graph { + fun createGraph(owner: String, method: MethodNode): Graph { analyze(owner, method) - return graph.build() + return AsUnmodifiableGraph(graph) } } diff --git a/deob/src/main/java/dev/openrs2/deob/analysis/DataFlowAnalyzer.kt b/deob/src/main/java/dev/openrs2/deob/analysis/DataFlowAnalyzer.kt index 4885b044cf..06692684fe 100644 --- a/deob/src/main/java/dev/openrs2/deob/analysis/DataFlowAnalyzer.kt +++ b/deob/src/main/java/dev/openrs2/deob/analysis/DataFlowAnalyzer.kt @@ -1,19 +1,20 @@ package dev.openrs2.deob.analysis -import com.google.common.graph.Graph -import com.google.common.graph.Graphs +import org.jgrapht.Graph +import org.jgrapht.graph.DefaultEdge +import org.jgrapht.graph.EdgeReversedGraph import org.objectweb.asm.tree.AbstractInsnNode import org.objectweb.asm.tree.MethodNode abstract class DataFlowAnalyzer(owner: String, private val method: MethodNode, backwards: Boolean = false) { - private val graph: Graph + private val graph: Graph private val inSets = mutableMapOf() private val outSets = mutableMapOf() init { val forwardsGraph = ControlFlowAnalyzer().createGraph(owner, method) graph = if (backwards) { - Graphs.transpose(forwardsGraph) + EdgeReversedGraph(forwardsGraph) } else { forwardsGraph } @@ -40,7 +41,7 @@ abstract class DataFlowAnalyzer(owner: String, private val method: MethodNode } fun analyze() { - for (node in graph.nodes()) { + for (node in graph.vertexSet()) { outSets[node] = createInitialSet() } @@ -48,8 +49,8 @@ abstract class DataFlowAnalyzer(owner: String, private val method: MethodNode do { changed = false - for (node in graph.nodes()) { - val predecessors = graph.predecessors(node).map { pred -> outSets[pred]!! } + for (node in graph.vertexSet()) { + val predecessors = graph.incomingEdgesOf(node).map { edge -> outSets[graph.getEdgeSource(edge)]!! } val inSet = if (predecessors.isEmpty()) { createInitialSet()