Commit e46dfc55 authored by Skylot's avatar Skylot

core: redone return blocks splitting for fix issue #4

parent e54b7645
package jadx.core.dex.visitors.regions; package jadx.core.dex.visitors.regions;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.nodes.BlockNode; import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.IBlock; import jadx.core.dex.nodes.IBlock;
import jadx.core.dex.nodes.IRegion; import jadx.core.dex.nodes.IRegion;
import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode; import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.regions.LoopRegion; import jadx.core.dex.regions.LoopRegion;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
public class FinishRegions extends TracedRegionVisitor { public class FinishRegions extends TracedRegionVisitor {
@Override @Override
...@@ -21,6 +17,8 @@ public class FinishRegions extends TracedRegionVisitor { ...@@ -21,6 +17,8 @@ public class FinishRegions extends TracedRegionVisitor {
BlockNode block = (BlockNode) container; BlockNode block = (BlockNode) container;
// remove last return in void functions // remove last return in void functions
/*
BlockNode block = (BlockNode) container;
if (block.getCleanSuccessors().isEmpty() if (block.getCleanSuccessors().isEmpty()
&& mth.getReturnType().equals(ArgType.VOID)) { && mth.getReturnType().equals(ArgType.VOID)) {
List<InsnNode> insns = block.getInstructions(); List<InsnNode> insns = block.getInstructions();
...@@ -33,6 +31,7 @@ public class FinishRegions extends TracedRegionVisitor { ...@@ -33,6 +31,7 @@ public class FinishRegions extends TracedRegionVisitor {
} }
} }
} }
*/
} }
private boolean blockNotInLoop(MethodNode mth, BlockNode block) { private boolean blockNotInLoop(MethodNode mth, BlockNode block) {
......
...@@ -4,7 +4,6 @@ import jadx.core.Consts; ...@@ -4,7 +4,6 @@ import jadx.core.Consts;
import jadx.core.dex.attributes.AttributeFlag; import jadx.core.dex.attributes.AttributeFlag;
import jadx.core.dex.attributes.AttributeType; import jadx.core.dex.attributes.AttributeType;
import jadx.core.dex.attributes.AttributesList; import jadx.core.dex.attributes.AttributesList;
import jadx.core.dex.attributes.ForceReturnAttr;
import jadx.core.dex.attributes.IAttribute; import jadx.core.dex.attributes.IAttribute;
import jadx.core.dex.attributes.LoopAttr; import jadx.core.dex.attributes.LoopAttr;
import jadx.core.dex.instructions.IfNode; import jadx.core.dex.instructions.IfNode;
...@@ -258,6 +257,7 @@ public class RegionMaker { ...@@ -258,6 +257,7 @@ public class RegionMaker {
while (next != null) { while (next != null) {
if (isPathExists(loopExit, next)) { if (isPathExists(loopExit, next)) {
// found cross // found cross
/*
if (next.getCleanSuccessors().size() == 1) { if (next.getCleanSuccessors().size() == 1) {
BlockNode r = BlockUtils.getNextBlock(next); BlockNode r = BlockUtils.getNextBlock(next);
if (r != null if (r != null
...@@ -265,13 +265,14 @@ public class RegionMaker { ...@@ -265,13 +265,14 @@ public class RegionMaker {
&& r.getInstructions().size() > 0 && r.getInstructions().size() > 0
&& r.getInstructions().get(0).getType() == InsnType.RETURN) { && r.getInstructions().get(0).getType() == InsnType.RETURN) {
next.getAttributes().add(new ForceReturnAttr(r.getInstructions().get(0))); next.getAttributes().add(new ForceReturnAttr(r.getInstructions().get(0)));
} /*/ else { } else {
next.getAttributes().add(AttributeFlag.BREAK); next.getAttributes().add(AttributeFlag.BREAK);
stack.addExit(r); stack.addExit(r);
} /**/ }
} else { } else {
stack.addExit(next); stack.addExit(next);
} }
*/
break; break;
} }
next = BlockUtils.getNextBlock(next); next = BlockUtils.getNextBlock(next);
......
...@@ -67,7 +67,6 @@ public class TypeResolver extends AbstractVisitor { ...@@ -67,7 +67,6 @@ public class TypeResolver extends AbstractVisitor {
state.assignReg(insn.getResult()); state.assignReg(insn.getResult());
} }
if (block.getSuccessors().size() > 0)
block.setEndState(new BlockRegState(state)); block.setEndState(new BlockRegState(state));
} }
} }
......
package jadx.samples;
public class TestCF4 extends AbstractTest {
int c;
String d;
String f;
public void testComplexIf(String a, int b) {
if (d == null || (c == 0 && b != -1 && d.length() == 0)) {
c = a.codePointAt(c);
} else {
if (a.length() != 2) {
c = f.compareTo(a);
}
}
}
public void checkComplexIf() {
d = null;
f = null;
c = 2;
testComplexIf("abcdef", 0);
assertEquals(c, (int) 'c');
d = "";
f = null;
c = 0;
testComplexIf("abcdef", 0);
assertEquals(c, (int) 'a');
d = "";
f = "1";
c = 777;
testComplexIf("ab", -1);
assertEquals(c, 777);
}
@Override
public boolean testRun() throws Exception {
checkComplexIf();
return true;
}
public static void main(String[] args) throws Exception {
new TestCF4().testRun();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment