Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing unused ExcHandlers blocks #2086

Merged
merged 5 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
skylot marked this conversation as resolved.
Show resolved Hide resolved
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() {
allowWarnInCode();
setFallback();
skylot marked this conversation as resolved.
Show resolved Hide resolved
String code = getClassNode(UnusedExceptionHandlers1.class).getCode().toString();
assertThat(code, containsString("IOException"));
}
}
Loading
Loading