Skip to content

Commit

Permalink
fix: correct method exit blocks collection (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Mar 17, 2020
1 parent 2f780da commit 4cdad0e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ private static void processBlocksTree(MethodNode mth) {
clearBlocksState(mth);
computeDominators(mth);
}
markReturnBlocks(mth);
updateExitBlocks(mth);

int i = 0;
while (modifyBlocksTree(mth)) {
// revert calculations
clearBlocksState(mth);
// recalculate dominators tree
computeDominators(mth);
markReturnBlocks(mth);
updateExitBlocks(mth);

if (i++ > 100) {
mth.addWarn("CFG modification limit reached, blocks count: " + mth.getBasicBlocks().size());
Expand Down Expand Up @@ -337,12 +337,33 @@ private static void computeBlockDF(MethodNode mth, BlockNode block) {
block.setDomFrontier(domFrontier);
}

private static void markReturnBlocks(MethodNode mth) {
private static void updateExitBlocks(MethodNode mth) {
mth.getExitBlocks().clear();
mth.getBasicBlocks().forEach(block -> {
if (BlockUtils.checkLastInsnType(block, InsnType.RETURN)) {
block.add(AFlag.RETURN);
mth.getExitBlocks().add(block);
boolean noSuccessors = block.getSuccessors().isEmpty();
boolean exitBlock = false;
InsnNode lastInsn = BlockUtils.getLastInsn(block);
if (lastInsn != null) {
InsnType insnType = lastInsn.getType();
if (insnType == InsnType.RETURN) {
block.add(AFlag.RETURN);
exitBlock = true;
if (!noSuccessors) {
throw new JadxRuntimeException("Found a block after RETURN instruction: " + lastInsn + " in block: " + block);
}
} else if (insnType == InsnType.THROW) {
if (noSuccessors) {
exitBlock = true;
}
}
}
if (exitBlock) {
mth.addExitBlock(block);
} else if (noSuccessors
&& !mth.isVoidReturn()
&& !mth.isConstructor()) {
mth.addComment("JADX INFO: Unexpected exit block: " + block
+ ". Expect last instruction to be RETURN or THROW, got: " + lastInsn);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package jadx.tests.integration.switches;

import org.junit.jupiter.api.Test;

import jadx.tests.api.IntegrationTest;

import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;

public class TestSwitchWithThrow extends IntegrationTest {

public static class TestCls {
public int test(int i) {
if (i != 0) {
switch (i % 4) {
case 1:
throw new IllegalStateException("1");
case 2:
throw new IllegalStateException("2");
default:
throw new IllegalStateException("Other");
}
} else {
System.out.println("0");
return -1;
}
}

public void check() {
assertThat(test(0)).isEqualTo(-1);
// TODO: implement 'invoke-custom' support
// assertThat(catchThrowable(() -> test(1)))
// .isInstanceOf(IllegalStateException.class).hasMessageContaining("1");
// assertThat(catchThrowable(() -> test(3)))
// .isInstanceOf(IllegalStateException.class).hasMessageContaining("Other");
}
}

@Test
public void test() {
assertThat(getClassNode(TestCls.class))
.code()
.contains("throw new IllegalStateException(\"1\");");
}
}

1 comment on commit 4cdad0e

@seomarketingads
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the issue despite am using the latest version,
ERROR: [69] JadxOverflowException in pass: RegionMakerVisitor in method: okhttp3.HttpUrl.Builder.resolvePath(java.lang.String, int, int):void, file: classes2.dex, details: Regions count limit reached

Please sign in to comment.