From f8ed069d76ad25ee42a77e2102a88cb43010e68a Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 23 Oct 2018 21:16:49 +0200 Subject: [PATCH] Cleanup (warnings; formatting) --- .../main/rels/MethodProcessorRunnable.java | 2 +- .../decompiler/SimplifyExprentsHelper.java | 56 +++++++------- .../deobfuscator/ExceptionDeobfuscator.java | 75 ++++++++++--------- 3 files changed, 64 insertions(+), 69 deletions(-) diff --git a/src/org/jetbrains/java/decompiler/main/rels/MethodProcessorRunnable.java b/src/org/jetbrains/java/decompiler/main/rels/MethodProcessorRunnable.java index 637260c..726d747 100644 --- a/src/org/jetbrains/java/decompiler/main/rels/MethodProcessorRunnable.java +++ b/src/org/jetbrains/java/decompiler/main/rels/MethodProcessorRunnable.java @@ -107,7 +107,7 @@ public class MethodProcessorRunnable implements Runnable { if (ExceptionDeobfuscator.hasObfuscatedExceptions(graph)) { DecompilerContext.getLogger().writeMessage("Heavily obfuscated exception ranges found!", IFernflowerLogger.Severity.WARN); - if(!ExceptionDeobfuscator.handleMultipleEntryExceptionRanges(graph)) { + if (!ExceptionDeobfuscator.handleMultipleEntryExceptionRanges(graph)) { DecompilerContext.getLogger().writeMessage("Found multiple entry exception ranges which could not be splitted", IFernflowerLogger.Severity.WARN); } ExceptionDeobfuscator.insertDummyExceptionHandlerBlocks(graph, cl.getBytecodeVersion()); diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/SimplifyExprentsHelper.java b/src/org/jetbrains/java/decompiler/modules/decompiler/SimplifyExprentsHelper.java index f23fc78..72c845f 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/SimplifyExprentsHelper.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/SimplifyExprentsHelper.java @@ -1,4 +1,4 @@ -// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.java.decompiler.modules.decompiler; import org.jetbrains.java.decompiler.code.CodeConstants; @@ -64,18 +64,11 @@ public class SimplifyExprentsHelper { for (Statement st : stat.getStats()) { res |= simplifyStackVarsStatement(st, setReorderedIfs, ssa, cl); - // collapse composed if's - if (changed = IfHelper.mergeIfs(st, setReorderedIfs)) { - break; - } - - // collapse iff ?: statement - if (changed = buildIff(st, ssa)) { - break; - } + changed = IfHelper.mergeIfs(st, setReorderedIfs) || // collapse composed if's + buildIff(st, ssa) || // collapse iff ?: statement + processClass14 && collapseInlinedClass14(st); // collapse inlined .class property in version 1.4 and before - // collapse inlined .class property in version 1.4 and before - if (processClass14 && (changed = collapseInlinedClass14(st))) { + if (changed) { break; } } @@ -459,44 +452,45 @@ public class SimplifyExprentsHelper { } private static boolean isIPPorIMM2(Exprent first, Exprent second) { - if (first.type != Exprent.EXPRENT_ASSIGNMENT || second.type != Exprent.EXPRENT_ASSIGNMENT) { return false; } - + AssignmentExprent af = (AssignmentExprent)first; AssignmentExprent as = (AssignmentExprent)second; - - if(as.getRight().type != Exprent.EXPRENT_FUNCTION) { + + if (as.getRight().type != Exprent.EXPRENT_FUNCTION) { return false; } - + FunctionExprent func = (FunctionExprent)as.getRight(); - - if(func.getFuncType() != FunctionExprent.FUNCTION_ADD && func.getFuncType() != FunctionExprent.FUNCTION_SUB) { + + if (func.getFuncType() != FunctionExprent.FUNCTION_ADD && func.getFuncType() != FunctionExprent.FUNCTION_SUB) { return false; } Exprent econd = func.getLstOperands().get(0); Exprent econst = func.getLstOperands().get(1); - if(econst.type != Exprent.EXPRENT_CONST && econd.type == Exprent.EXPRENT_CONST && func.getFuncType() == FunctionExprent.FUNCTION_ADD) { + if (econst.type != Exprent.EXPRENT_CONST && econd.type == Exprent.EXPRENT_CONST && func.getFuncType() == FunctionExprent.FUNCTION_ADD) { econd = econst; econst = func.getLstOperands().get(0); } - if(econst.type == Exprent.EXPRENT_CONST && ((ConstExprent)econst).hasValueOne()) { - if(af.getLeft().equals(econd) && af.getRight().equals(as.getLeft()) && (af.getLeft().getExprentUse() & Exprent.MULTIPLE_USES) != 0) { - int type = func.getFuncType() == FunctionExprent.FUNCTION_ADD ? FunctionExprent.FUNCTION_IPP : FunctionExprent.FUNCTION_IMM; - - FunctionExprent ret = new FunctionExprent(type, af.getRight(), func.bytecode); - ret.setImplicitType(VarType.VARTYPE_INT); - - af.setRight(ret); - return true; - } + if (econst.type == Exprent.EXPRENT_CONST && + ((ConstExprent)econst).hasValueOne() && + af.getLeft().equals(econd) && + af.getRight().equals(as.getLeft()) && + (af.getLeft().getExprentUse() & Exprent.MULTIPLE_USES) != 0) { + int type = func.getFuncType() == FunctionExprent.FUNCTION_ADD ? FunctionExprent.FUNCTION_IPP : FunctionExprent.FUNCTION_IMM; + + FunctionExprent ret = new FunctionExprent(type, af.getRight(), func.bytecode); + ret.setImplicitType(VarType.VARTYPE_INT); + + af.setRight(ret); + return true; } - + return false; } diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/deobfuscator/ExceptionDeobfuscator.java b/src/org/jetbrains/java/decompiler/modules/decompiler/deobfuscator/ExceptionDeobfuscator.java index 7f7c9aa..810291d 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/deobfuscator/ExceptionDeobfuscator.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/deobfuscator/ExceptionDeobfuscator.java @@ -311,52 +311,52 @@ public class ExceptionDeobfuscator { } public static boolean handleMultipleEntryExceptionRanges(ControlFlowGraph graph) { - GenericDominatorEngine engine = new GenericDominatorEngine(new IGraph() { + @Override public List getReversePostOrderList() { return graph.getReversePostOrder(); } + @Override public Set getRoots() { return new HashSet<>(Collections.singletonList(graph.getFirst())); } }); engine.initialize(); - - boolean found = false; - - while(true) { + + boolean found; + + while (true) { found = false; boolean splitted = false; - - for(ExceptionRangeCFG range : graph.getExceptions()) { + + for (ExceptionRangeCFG range : graph.getExceptions()) { Set setEntries = getRangeEntries(range); - - if(setEntries.size() > 1) { // multiple-entry protected range + + if (setEntries.size() > 1) { // multiple-entry protected range found = true; - - if(splitExceptionRange(range, setEntries, graph, engine)) { + + if (splitExceptionRange(range, setEntries, graph, engine)) { splitted = true; break; } } } - - if(!splitted) { + + if (!splitted) { break; } } - + return !found; } private static Set getRangeEntries(ExceptionRangeCFG range) { - Set setEntries = new HashSet<>(); - Set setRange= new HashSet<>(range.getProtectedRange()); - - for(BasicBlock block : range.getProtectedRange()) { + Set setRange = new HashSet<>(range.getProtectedRange()); + + for (BasicBlock block : range.getProtectedRange()) { Set setPreds = new HashSet<>(block.getPreds()); setPreds.removeAll(setRange); @@ -367,19 +367,22 @@ public class ExceptionDeobfuscator { return setEntries; } - - private static boolean splitExceptionRange(ExceptionRangeCFG range, Set setEntries, ControlFlowGraph graph, GenericDominatorEngine engine) { - for(BasicBlock entry : setEntries) { + private static boolean splitExceptionRange(ExceptionRangeCFG range, + Set setEntries, + ControlFlowGraph graph, + GenericDominatorEngine engine) { + for (BasicBlock entry : setEntries) { List lstSubrangeBlocks = getReachableBlocksRestricted(entry, range, engine); - if(!lstSubrangeBlocks.isEmpty() && lstSubrangeBlocks.size() < range.getProtectedRange().size()) { + if (!lstSubrangeBlocks.isEmpty() && lstSubrangeBlocks.size() < range.getProtectedRange().size()) { // add new range ExceptionRangeCFG subRange = new ExceptionRangeCFG(lstSubrangeBlocks, range.getHandler(), range.getExceptionTypes()); graph.getExceptions().add(subRange); // shrink the original range range.getProtectedRange().removeAll(lstSubrangeBlocks); return true; - } else { + } + else { // should not happen DecompilerContext.getLogger().writeMessage("Inconsistency found while splitting protected range", IFernflowerLogger.Severity.WARN); } @@ -389,22 +392,21 @@ public class ExceptionDeobfuscator { } public static void insertDummyExceptionHandlerBlocks(ControlFlowGraph graph, int bytecode_version) { - Map> mapRanges = new HashMap<>(); for (ExceptionRangeCFG range : graph.getExceptions()) { mapRanges.computeIfAbsent(range.getHandler(), k -> new HashSet<>()).add(range); } - + for (Entry> ent : mapRanges.entrySet()) { BasicBlock handler = ent.getKey(); Set ranges = ent.getValue(); - - if(ranges.size() == 1) { + + if (ranges.size() == 1) { continue; } - - for(ExceptionRangeCFG range : ranges) { - + + for (ExceptionRangeCFG range : ranges) { + // add some dummy instructions to prevent optimizing away the empty block SimpleInstructionSequence seq = new SimpleInstructionSequence(); seq.addInstruction(Instruction.create(CodeConstants.opc_bipush, false, CodeConstants.GROUP_GENERAL, bytecode_version, new int[]{0}), -1); @@ -414,11 +416,11 @@ public class ExceptionDeobfuscator { dummyBlock.setSeq(seq); graph.getBlocks().addWithKey(dummyBlock, dummyBlock.id); - + // only exception predecessors from this range considered List lstPredExceptions = new ArrayList<>(handler.getPredExceptions()); lstPredExceptions.retainAll(range.getProtectedRange()); - + // replace predecessors for (BasicBlock pred : lstPredExceptions) { pred.replaceSuccessor(handler, dummyBlock); @@ -428,20 +430,19 @@ public class ExceptionDeobfuscator { range.setHandler(dummyBlock); // add common exception edges Set commonHandlers = new HashSet<>(handler.getSuccExceptions()); - for(BasicBlock pred : lstPredExceptions) { + for (BasicBlock pred : lstPredExceptions) { commonHandlers.retainAll(pred.getSuccExceptions()); } // TODO: more sanity checks? - for(BasicBlock commonHandler : commonHandlers) { + for (BasicBlock commonHandler : commonHandlers) { ExceptionRangeCFG commonRange = graph.getExceptionRange(commonHandler, handler); - + dummyBlock.addSuccessorException(commonHandler); commonRange.getProtectedRange().add(dummyBlock); } - + dummyBlock.addSuccessor(handler); } } } - } \ No newline at end of file