Skip to content

Commit

Permalink
fix: improve exception handler remove (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jul 22, 2019
1 parent 6b76a3c commit 3ae8359
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
16 changes: 9 additions & 7 deletions jadx-core/src/main/java/jadx/core/dex/nodes/MethodNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,11 @@ private static void initTryCatches(MethodNode mth, Code mthCode, InsnNode[] insn
// resolve nested try blocks:
// inner block contains all handlers from outer block => remove these handlers from inner block
// each handler must be only in one try/catch block
for (TryCatchBlock ct1 : catches) {
for (TryCatchBlock ct2 : catches) {
if (ct1 != ct2 && ct2.containsAllHandlers(ct1)) {
for (ExceptionHandler h : ct1.getHandlers()) {
ct2.removeHandler(mth, h);
h.setTryBlock(ct1);
}
for (TryCatchBlock outerTry : catches) {
for (TryCatchBlock innerTry : catches) {
if (outerTry != innerTry
&& innerTry.containsAllHandlers(outerTry)) {
innerTry.removeSameHandlers(outerTry);
}
}
}
Expand Down Expand Up @@ -522,6 +520,10 @@ public ExceptionHandler addExceptionHandler(ExceptionHandler handler) {
return handler;
}

public boolean clearExceptionHandlers() {
return exceptionHandlers.removeIf(ExceptionHandler::isRemoved);
}

public Iterable<ExceptionHandler> getExceptionHandlers() {
return exceptionHandlers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class ExceptionHandler {
private TryCatchBlock tryBlock;
private boolean isFinally;

private boolean removed = false;

public ExceptionHandler(int addr, @Nullable ClassInfo type) {
this.handleOffset = addr;
addCatchType(type);
Expand Down Expand Up @@ -138,6 +140,14 @@ public void setFinally(boolean isFinally) {
this.isFinally = isFinally;
}

public boolean isRemoved() {
return removed;
}

public void markForRemove() {
this.removed = true;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public ExceptionHandler addHandler(MethodNode mth, int addr, @Nullable ClassInfo
return addedHandler;
}

/**
* Use only before BlockSplitter
*/
public void removeSameHandlers(TryCatchBlock outerTry) {
for (ExceptionHandler handler : outerTry.getHandlers()) {
if (handlers.remove(handler)) {
handler.setTryBlock(outerTry);
}
}
}

public void removeHandler(MethodNode mth, ExceptionHandler handler) {
for (Iterator<ExceptionHandler> it = handlers.iterator(); it.hasNext();) {
ExceptionHandler h = it.next();
Expand All @@ -78,9 +89,14 @@ private void unbindHandler(ExceptionHandler handler) {
}
SplitterBlockAttr splitter = handler.getHandlerBlock().get(AType.SPLITTER_BLOCK);
if (splitter != null) {
splitter.getBlock().remove(AType.SPLITTER_BLOCK);
BlockNode splitterBlock = splitter.getBlock();
splitterBlock.remove(AType.SPLITTER_BLOCK);
for (BlockNode successor : splitterBlock.getSuccessors()) {
successor.remove(AType.SPLITTER_BLOCK);
}
}
}
handler.markForRemove();
}

private void removeWholeBlock(MethodNode mth) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ public void visit(MethodNode mth) {
return;
}
try {
mth.clearExceptionHandlers();

for (ExceptionHandler excHandler : mth.getExceptionHandlers()) {
processExceptionHandler(mth, excHandler);
}
mth.clearExceptionHandlers();
} catch (Exception e) {
LOG.warn("Undo finally extract visitor, mth: {}", mth, e);
try {
Expand Down
10 changes: 10 additions & 0 deletions jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public static void makeDirs(@Nullable File dir) {
}
}

public static boolean deleteDir(File dir) {
File[] content = dir.listFiles();
if (content != null) {
for (File file : content) {
deleteDir(file);
}
}
return dir.delete();
}

private static final Path TEMP_ROOT_DIR = createTempRootDir();

private static Path createTempRootDir() {
Expand Down
1 change: 1 addition & 0 deletions jadx-core/src/test/java/jadx/tests/external/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/ExternalTests.java
/*Tmp.java

0 comments on commit 3ae8359

Please sign in to comment.