From d1b8fbd0941e32aee9dbbd332601c1886e6ef6a9 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 20 Jul 2020 12:02:48 +0100 Subject: [PATCH] Remove redundant casts Signed-off-by: Graham --- .../openrs2/deob/ast/AstDeobfuscatorModule.kt | 2 + .../ast/transform/RedundantCastTransformer.kt | 83 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/RedundantCastTransformer.kt diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt index b20a9b4962..7c6375b5f3 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscatorModule.kt @@ -17,6 +17,7 @@ import dev.openrs2.deob.ast.transform.IfElseTransformer import dev.openrs2.deob.ast.transform.IncrementTransformer import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer import dev.openrs2.deob.ast.transform.NewInstanceTransformer +import dev.openrs2.deob.ast.transform.RedundantCastTransformer import dev.openrs2.deob.ast.transform.TernaryTransformer import dev.openrs2.deob.ast.transform.Transformer import dev.openrs2.deob.ast.transform.UnencloseTransformer @@ -42,6 +43,7 @@ object AstDeobfuscatorModule : AbstractModule() { binder.addBinding().to(NewInstanceTransformer::class.java) binder.addBinding().to(IncrementTransformer::class.java) binder.addBinding().to(ForLoopConditionTransformer::class.java) + binder.addBinding().to(RedundantCastTransformer::class.java) binder.addBinding().to(GlTransformer::class.java) binder.addBinding().to(EncloseTransformer::class.java) } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/RedundantCastTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/RedundantCastTransformer.kt new file mode 100644 index 0000000000..3b5a92f0c7 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/RedundantCastTransformer.kt @@ -0,0 +1,83 @@ +package dev.openrs2.deob.ast.transform + +import com.github.javaparser.ast.CompilationUnit +import com.github.javaparser.ast.expr.AssignExpr +import com.github.javaparser.ast.expr.CastExpr +import com.github.javaparser.ast.expr.Expression +import com.github.javaparser.ast.expr.MethodCallExpr +import com.github.javaparser.ast.expr.NullLiteralExpr +import com.github.javaparser.ast.expr.ObjectCreationExpr +import com.github.javaparser.ast.expr.VariableDeclarationExpr +import com.github.javaparser.resolution.MethodAmbiguityException +import dev.openrs2.deob.ast.Library +import dev.openrs2.deob.ast.LibraryGroup +import dev.openrs2.deob.ast.util.walk + +class RedundantCastTransformer : Transformer() { + override fun transformUnit(group: LibraryGroup, library: Library, unit: CompilationUnit) { + // remove double casts + unit.walk { expr: CastExpr -> + val innerExpr = expr.expression + if (innerExpr is CastExpr && expr.type == innerExpr.type) { + expr.expression = innerExpr.expression.clone() + } + } + + // remove null argument casts if the call remains unambiguous + unit.walk { expr: MethodCallExpr -> + for (i in expr.arguments.indices) { + val arg = expr.arguments[i] + if (!isCastedNull(arg)) { + continue + } + + expr.arguments[i] = NullLiteralExpr() + try { + expr.resolve() + } catch (ex: MethodAmbiguityException) { + expr.arguments[i] = arg + } + } + } + + unit.walk { expr: ObjectCreationExpr -> + for (i in expr.arguments.indices) { + val arg = expr.arguments[i] + if (!isCastedNull(arg)) { + continue + } + + expr.arguments[i] = NullLiteralExpr() + try { + expr.resolve() + } catch (ex: MethodAmbiguityException) { + expr.arguments[i] = arg + } + } + } + + // remove null assignment casts + unit.walk { expr: VariableDeclarationExpr -> + for (variable in expr.variables) { + variable.initializer.ifPresent { initializer -> + if (isCastedNull(initializer)) { + initializer.replace(NullLiteralExpr()) + } + } + } + } + + unit.walk { expr: AssignExpr -> + if (isCastedNull(expr.value)) { + expr.value = NullLiteralExpr() + } + } + } + + private fun isCastedNull(expr: Expression): Boolean { + if (expr !is CastExpr) { + return false + } + return expr.expression is NullLiteralExpr + } +}