Skip to content

Commit

Permalink
fix: handle methods with all NOPs (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Aug 30, 2019
1 parent db892ad commit bd9e109
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jadx.core.dex.attributes;

public enum AFlag {
MTH_ENTER_BLOCK,
TRY_ENTER,
TRY_LEAVE,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class BlockProcessor extends AbstractVisitor {

@Override
public void visit(MethodNode mth) {
if (mth.isNoCode()) {
if (mth.isNoCode() || mth.getBasicBlocks().isEmpty()) {
return;
}
processBlocksTree(mth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ private static void splitBasicBlocks(MethodNode mth) {
InsnNode prevInsn = null;
Map<Integer, BlockNode> blocksMap = new HashMap<>();
BlockNode curBlock = startNewBlock(mth, 0);
curBlock.add(AFlag.MTH_ENTER_BLOCK);
mth.setEnterBlock(curBlock);

// split into blocks
Expand Down Expand Up @@ -331,7 +332,8 @@ private static void removeInsns(MethodNode mth) {
static boolean removeEmptyDetachedBlocks(MethodNode mth) {
return mth.getBasicBlocks().removeIf(block -> block.getInstructions().isEmpty()
&& block.getPredecessors().isEmpty()
&& block.getSuccessors().isEmpty());
&& block.getSuccessors().isEmpty()
&& !block.contains(AFlag.MTH_ENTER_BLOCK));
}

private static boolean removeUnreachableBlocks(MethodNode mth) {
Expand Down Expand Up @@ -385,7 +387,8 @@ private static boolean canRemoveBlock(BlockNode block) {
return block.getInstructions().isEmpty()
&& block.isAttrStorageEmpty()
&& block.getSuccessors().size() <= 1
&& !block.getPredecessors().isEmpty();
&& !block.getPredecessors().isEmpty()
&& !block.contains(AFlag.MTH_ENTER_BLOCK);
}

private static void collectSuccessors(BlockNode startBlock, Set<BlockNode> toRemove) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class RegionMakerVisitor extends AbstractVisitor {

@Override
public void visit(MethodNode mth) throws JadxException {
if (mth.isNoCode()) {
if (mth.isNoCode() || mth.getBasicBlocks().isEmpty()) {
return;
}
RegionMaker rm = new RegionMaker(mth);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package jadx.tests.integration.others;

import org.junit.jupiter.api.Test;

import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.SmaliTest;

import static jadx.tests.api.utils.JadxMatchers.containsLines;
import static org.hamcrest.MatcherAssert.assertThat;

public class TestAllNops extends SmaliTest {

@Test
public void test() {
disableCompilation();
ClassNode cls = getClassNodeFromSmali();
String code = cls.getCode().toString();

assertThat(code, containsLines(1, "private boolean test() {", "}"));
assertThat(code, containsLines(1, "private boolean testWithTryCatch() {", "}"));
}
}
92 changes: 92 additions & 0 deletions jadx-core/src/test/smali/others/TestAllNops.smali
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
.class public Lothers/TestAllNops;
.super Ljava/lang/Object;

.method public constructor <init>()V
.registers 1

.line 55
nop

nop

nop

nop
.end method

.method private test()Z
.registers 11

.line 1480
nop

nop

.line 1481
nop

nop

nop

nop

nop

nop

.line 1485
nop

nop

.line 1486
nop

nop

.line 1487
nop

.end method

.method private testWithTryCatch()Z
.registers 11

.line 1480
:try_start_0
nop
nop

.line 1481
nop

nop

nop

nop

nop

nop

.line 1485
nop

nop

:try_end_35
.catch Ljava/security/NoSuchAlgorithmException; {:try_start_0 .. :try_end_35} :catch_36

nop

.line 1547
:catch_36

nop

.line 1487
nop

.end method

0 comments on commit bd9e109

Please sign in to comment.