Skip to content

Commit

Permalink
fix: remove unused ExcHandlers blocks (PR #2086)
Browse files Browse the repository at this point in the history
* Removing unused ExcHandlers blocks

* Improving removing unused ExcHandlers blocks

* Removing gradlew of the commit

* Adding test 2 for UnreachableCatch

* Update jadx-core/src/test/java/jadx/tests/integration/trycatch/TestUnreachableCatch2.java

---------

Co-authored-by: Away-pp <vladimir@DESKTOP-KESF23K>
Co-authored-by: skylot <[email protected]>
  • Loading branch information
3 people authored Feb 4, 2024
1 parent 7e628ad commit 276ee53
Show file tree
Hide file tree
Showing 5 changed files with 481 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import jadx.core.utils.BlockUtils;
import jadx.core.utils.InsnRemover;
import jadx.core.utils.ListUtils;
import jadx.core.utils.blocks.BlockSet;
import jadx.core.utils.exceptions.JadxRuntimeException;

public class BlockExceptionHandler {
Expand All @@ -65,6 +66,10 @@ public static boolean process(MethodNode mth) {
removeMonitorExitFromExcHandler(mth, eh);
}
BlockProcessor.removeMarkedBlocks(mth);

BlockSet sorted = new BlockSet(mth);
BlockUtils.dfsVisit(mth, sorted::set);
removeUnusedExcHandlers(mth, tryBlocks, sorted);
return true;
}

Expand Down Expand Up @@ -586,4 +591,27 @@ private static int compareByTypeAndName(Comparator<ArgType> comparator, ClassInf
}
return r;
}

/**
* Remove excHandlers that were not used when connecting.
* Check first if the blocks are unreachable.
*/
private static void removeUnusedExcHandlers(MethodNode mth, List<TryCatchBlockAttr> tryBlocks, BlockSet blocks) {
for (ExceptionHandler eh : mth.getExceptionHandlers()) {
boolean notProcessed = true;
BlockNode handlerBlock = eh.getHandlerBlock();
if (blocks.get(handlerBlock)) {
continue;
}
for (TryCatchBlockAttr tcb : tryBlocks) {
if (tcb.getHandlers().contains(handlerBlock)) {
notProcessed = false;
break;
}
}
if (notProcessed) {
BlockProcessor.removeUnreachableBlock(handlerBlock, mth);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -646,10 +646,24 @@ public static void removeMarkedBlocks(MethodNode mth) {
private static void removeUnreachableBlocks(MethodNode mth) {
Set<BlockNode> toRemove = new LinkedHashSet<>();
for (BlockNode block : mth.getBasicBlocks()) {
if (block.getPredecessors().isEmpty() && block != mth.getEnterBlock()) {
BlockSplitter.collectSuccessors(block, mth.getEnterBlock(), toRemove);
}
computeUnreachableFromBlock(toRemove, block, mth);
}
removeFromMethod(toRemove, mth);
}

public static void removeUnreachableBlock(BlockNode blockToRemove, MethodNode mth) {
Set<BlockNode> toRemove = new LinkedHashSet<>();
computeUnreachableFromBlock(toRemove, blockToRemove, mth);
removeFromMethod(toRemove, mth);
}

private static void computeUnreachableFromBlock(Set<BlockNode> toRemove, BlockNode block, MethodNode mth) {
if (block.getPredecessors().isEmpty() && block != mth.getEnterBlock()) {
BlockSplitter.collectSuccessors(block, mth.getEnterBlock(), toRemove);
}
}

private static void removeFromMethod(Set<BlockNode> toRemove, MethodNode mth) {
if (toRemove.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package jadx.tests.integration.trycatch;

import org.junit.jupiter.api.Test;

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

import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

@SuppressWarnings("CommentedOutCode")
public class TestUnreachableCatch extends SmaliTest {

// @formatter:off
/*
private static Map<Uri, ByteBuffer> prepareFontData(Context context, FontInfo[] fonts,
CancellationSignal cancellationSignal) {
final HashMap<Uri, ByteBuffer> out = new HashMap<>();
final ContentResolver resolver = context.getContentResolver();
for (FontInfo font : fonts) {
if (font.getResultCode() != Columns.RESULT_CODE_OK) {
continue;
}
final Uri uri = font.getUri();
if (out.containsKey(uri)) {
continue;
}
ByteBuffer buffer = null;
try (final ParcelFileDescriptor pfd =
resolver.openFileDescriptor(uri, "r", cancellationSignal)) {
if (pfd != null) {
try (final FileInputStream fis =
new FileInputStream(pfd.getFileDescriptor())) {
final FileChannel fileChannel = fis.getChannel();
final long size = fileChannel.size();
buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, size);
} catch (IOException e) {
// ignore
}
}
} catch (IOException e) {
// ignore
}
// TODO: try other approach?, e.g. read all contents instead of mmap.
out.put(uri, buffer);
}
return Collections.unmodifiableMap(out);
}
*/
// @formatter:on

@Test
public void test() {
disableCompilation();
allowWarnInCode();

ClassNode cls = getClassNodeFromSmali();
String code = cls.getCode().toString();

assertThat(code, containsString("IOException"));
assertThat(code, containsString("Collections.unmodifiableMap"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package jadx.tests.integration.trycatch;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.junit.jupiter.api.Test;

import jadx.tests.api.SmaliTest;

import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

@SuppressWarnings("CommentedOutCode")
public class TestUnreachableCatch2 extends SmaliTest {

public static class UnusedExceptionHandlers1 implements AutoCloseable {
public static void doSomething(final Object unused1, final Object[] array, final Object o1,
final Object o2, final Object unused2) {
for (final Object item : array) {
ByteBuffer buffer = null;
try (final UnusedExceptionHandlers1 u = doSomething2(o1, "", o2)) {
try (final FileInputStream fis = new FileInputStream(u.getFilename())) {
final FileChannel fileChannel = fis.getChannel();
buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, 42);
} catch (final IOException e) {
// ignore
}
} catch (final IOException e) {
// ignore
}
}
}

private String getFilename() {
return null;
}

private static UnusedExceptionHandlers1 doSomething2(final Object o1, final String s,
final Object o2) {
return null;
}

@Override
public void close() throws IOException {
}
}

@Test
public void test() {
// TODO: result code not compilable because of 'break' after 'throw'
disableCompilation();
String code = getClassNode(UnusedExceptionHandlers1.class).getCode().toString();
assertThat(code, containsString("IOException"));
}
}
Loading

0 comments on commit 276ee53

Please sign in to comment.