From 597aa2018ecc80927ebd4c70853f81bee12549b9 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 5 Mar 2020 00:44:28 +0000 Subject: [PATCH] Add support for whole program AST transforms --- .../dev/openrs2/deob/ast/AstDeobfuscator.kt | 16 +++++++++----- .../deob/ast/transform/AddSubTransformer.kt | 5 ++++- .../transform/BinaryExprOrderTransformer.kt | 5 ++++- .../deob/ast/transform/BitMaskTransformer.kt | 5 ++++- .../ast/transform/ComplementTransformer.kt | 5 ++++- .../deob/ast/transform/EncloseTransformer.kt | 5 ++++- .../transform/ForLoopConditionTransformer.kt | 5 ++++- .../deob/ast/transform/IdentityTransformer.kt | 5 ++++- .../deob/ast/transform/IfElseTransformer.kt | 5 ++++- .../ast/transform/IncrementTransformer.kt | 5 ++++- .../transform/NegativeLiteralTransformer.kt | 5 ++++- .../ast/transform/NewInstanceTransformer.kt | 5 ++++- .../deob/ast/transform/TernaryTransformer.kt | 5 ++++- .../openrs2/deob/ast/transform/Transformer.kt | 22 ++++++++++++++++++- .../ast/transform/UnencloseTransformer.kt | 5 ++++- .../deob/ast/transform/ValueOfTransformer.kt | 5 ++++- 16 files changed, 88 insertions(+), 20 deletions(-) diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt index 1a2f31a1..1430ac72 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt @@ -59,20 +59,26 @@ class AstDeobfuscator(private val modules: List) { val printer = PrettyPrinter(printerConfig) - for (module in modules) { - val root = SourceRoot(module, config) + val roots = modules.map { SourceRoot(it, config) } + val units = mutableMapOf() + for (root in roots) { val results = root.tryToParseParallelized() for (result in results) { require(result.isSuccessful) { result } } for (unit in root.compilationUnits) { - for (transformer in TRANSFORMERS) { - transformer.transform(unit) - } + val name = unit.primaryType.orElseThrow().fullyQualifiedName.orElseThrow() + units[name] = unit } + } + + for (transformer in TRANSFORMERS) { + transformer.transform(units) + } + for (root in roots) { root.printer = Function(printer::print) root.saveAll() } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt index ca15f65e..57c257ff 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt @@ -12,7 +12,10 @@ import dev.openrs2.deob.ast.util.negate import dev.openrs2.deob.ast.util.walk class AddSubTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: BinaryExpr -> val op = expr.operator val left = expr.left diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt index a38d9e98..1b752440 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt @@ -6,7 +6,10 @@ import dev.openrs2.deob.ast.util.isString import dev.openrs2.deob.ast.util.walk class BinaryExprOrderTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: BinaryExpr -> val op = expr.operator.flip() ?: return@walk diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt index 1b01f4da..09c6de88 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt @@ -10,7 +10,10 @@ import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral import dev.openrs2.deob.ast.util.walk class BitMaskTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: BinaryExpr -> val shiftOp = expr.operator val left = expr.left diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt index f80a9eac..b3ef7f78 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt @@ -12,7 +12,10 @@ import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral import dev.openrs2.deob.ast.util.walk class ComplementTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: BinaryExpr -> val op = complement(expr.operator) ?: return@walk diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt index 21feaae9..39194d65 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt @@ -71,7 +71,10 @@ class EncloseTransformer : Transformer() { } } - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: Expression -> when { expr.isArrayAccessExpr -> { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt index 54213cf5..9390f658 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt @@ -7,7 +7,10 @@ import dev.openrs2.deob.ast.util.hasSideEffects import dev.openrs2.deob.ast.util.walk class ForLoopConditionTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { stmt: ForStmt -> stmt.compare.ifPresent { compare -> if (!compare.isBinaryExpr) { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt index 8d8b05ab..3635ef9a 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt @@ -7,7 +7,10 @@ import com.github.javaparser.ast.expr.UnaryExpr import dev.openrs2.deob.ast.util.walk class IdentityTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: BinaryExpr -> @Suppress("NON_EXHAUSTIVE_WHEN") when (expr.operator) { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt index 81c5ea63..234ecd6d 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt @@ -9,7 +9,10 @@ import dev.openrs2.deob.ast.util.not import dev.openrs2.deob.ast.util.walk class IfElseTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { stmt: IfStmt -> stmt.elseStmt.ifPresent { elseStmt: Statement -> val condition = stmt.condition diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt index fcec6026..485eeb3e 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt @@ -7,7 +7,10 @@ import com.github.javaparser.ast.stmt.ForStmt import dev.openrs2.deob.ast.util.walk class IncrementTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { stmt: ExpressionStmt -> if (!stmt.expression.isUnaryExpr) { return@walk diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt index 0cac0b77..33b04215 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt @@ -7,7 +7,10 @@ import dev.openrs2.deob.ast.util.negate import dev.openrs2.deob.ast.util.walk class NegativeLiteralTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: UnaryExpr -> val operand = expr.expression if (!operand.isIntegerOrLongLiteral()) { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt index f855f9ee..56d8c25e 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt @@ -5,7 +5,10 @@ import com.github.javaparser.ast.expr.MethodCallExpr import dev.openrs2.deob.ast.util.walk class NewInstanceTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: MethodCallExpr -> if (expr.nameAsString != "newInstance") { return@walk diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt index 07adf92c..d743509e 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt @@ -7,7 +7,10 @@ import dev.openrs2.deob.ast.util.not import dev.openrs2.deob.ast.util.walk class TernaryTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: ConditionalExpr -> val condition = expr.condition val notCondition = condition.not() diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/Transformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/Transformer.kt index 75454a41..d2c8d384 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/Transformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/Transformer.kt @@ -3,5 +3,25 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit abstract class Transformer { - abstract fun transform(unit: CompilationUnit) + fun transform(units: Map) { + preTransform(units) + + for (unit in units.values) { + transformUnit(units, unit) + } + + postTransform(units) + } + + protected open fun preTransform(units: Map) { + // empty + } + + protected open fun transformUnit(units: Map, unit: CompilationUnit) { + // empty + } + + protected open fun postTransform(units: Map) { + // empty + } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt index 07ee4849..3bc57dcd 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt @@ -5,7 +5,10 @@ import com.github.javaparser.ast.expr.EnclosedExpr import dev.openrs2.deob.ast.util.walk class UnencloseTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: EnclosedExpr -> expr.replace(expr.inner.clone()) } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt index e1658b4c..7fa658ab 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt @@ -7,7 +7,10 @@ import com.github.javaparser.ast.expr.TypeExpr import dev.openrs2.deob.ast.util.walk class ValueOfTransformer : Transformer() { - override fun transform(unit: CompilationUnit) { + override fun transformUnit( + units: Map, + unit: CompilationUnit + ) { unit.walk { expr: ObjectCreationExpr -> if (expr.type.isBoxedType) { expr.replace(MethodCallExpr(TypeExpr(expr.type), "valueOf", expr.arguments))