From dc6fe61dad110df1d6ae6c5fa54568020943be3d Mon Sep 17 00:00:00 2001 From: REAndroid Date: Mon, 25 Nov 2024 15:28:05 +0100 Subject: [PATCH] [DEX] Fix: Proper exception handlers sorting --- .../reandroid/dex/ins/CatchAllHandler.java | 4 +++ .../reandroid/dex/ins/CatchTypedHandler.java | 5 +++ .../reandroid/dex/ins/ExceptionHandler.java | 31 +++++++++++++++++++ .../com/reandroid/dex/ins/ExceptionLabel.java | 6 ++++ .../java/com/reandroid/dex/ins/Label.java | 10 +++--- 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/reandroid/dex/ins/CatchAllHandler.java b/src/main/java/com/reandroid/dex/ins/CatchAllHandler.java index 07411e493..4f3eca085 100644 --- a/src/main/java/com/reandroid/dex/ins/CatchAllHandler.java +++ b/src/main/java/com/reandroid/dex/ins/CatchAllHandler.java @@ -45,6 +45,10 @@ TypeId getTypeId(){ return null; } @Override + public boolean isCatchAll() { + return true; + } + @Override public SmaliDirective getSmaliDirective(){ return SmaliDirective.CATCH_ALL; } diff --git a/src/main/java/com/reandroid/dex/ins/CatchTypedHandler.java b/src/main/java/com/reandroid/dex/ins/CatchTypedHandler.java index 7bac1fee2..e436c2f0f 100644 --- a/src/main/java/com/reandroid/dex/ins/CatchTypedHandler.java +++ b/src/main/java/com/reandroid/dex/ins/CatchTypedHandler.java @@ -64,6 +64,11 @@ public TypeKey getKey(){ public void setKey(TypeKey typeKey){ getTypeUle128().setKey(typeKey); } + @Override + public boolean isCatchAll() { + return false; + } + Ule128IdItemReference getTypeUle128(){ return typeId; } diff --git a/src/main/java/com/reandroid/dex/ins/ExceptionHandler.java b/src/main/java/com/reandroid/dex/ins/ExceptionHandler.java index acc80afc6..c9b36f634 100644 --- a/src/main/java/com/reandroid/dex/ins/ExceptionHandler.java +++ b/src/main/java/com/reandroid/dex/ins/ExceptionHandler.java @@ -24,6 +24,7 @@ import com.reandroid.dex.smali.SmaliRegion; import com.reandroid.dex.smali.SmaliWriter; import com.reandroid.dex.smali.model.SmaliCodeExceptionHandler; +import com.reandroid.utils.CompareUtil; import com.reandroid.utils.HexUtil; import com.reandroid.utils.ObjectsUtil; import com.reandroid.utils.collection.ArrayIterator; @@ -74,6 +75,9 @@ public TypeKey getKey(){ } public void setKey(TypeKey typeKey){ } + + public abstract boolean isCatchAll(); + public boolean isAddressBounded(int address) { if (address == -1) { return true; @@ -201,6 +205,27 @@ public void removeSelf(){ public boolean isRemoved() { return getParent() == null; } + int compareHandler(ExceptionHandler handler) { + if (handler == this) { + return 0; + } + int i = CompareUtil.compare(this.getAddress(), handler.getAddress()); + if (i != 0) { + return i; + } + i = CompareUtil.compare(this.isCatchAll(), handler.isCatchAll()); + if (i != 0) { + return i; + } + TryItem tryItem1 = getTryItem(); + TryItem tryItem2 = handler.getTryItem(); + i = CompareUtil.compare(tryItem1.getIndex(), tryItem2.getIndex()); + if (i != 0) { + return i; + } + return CompareUtil.compare(tryItem1.getHandlerOffset().getIndex(), + tryItem2.getHandlerOffset().getIndex()); + } public void merge(ExceptionHandler handler){ catchAddress.set(handler.getCatchAddress()); } @@ -346,6 +371,12 @@ public boolean isEqualExtraLine(Object obj) { HandlerLabel label = (HandlerLabel) obj; return this.getHandler() == label.getHandler(); } + + @Override + public int compareLabelName(Label label) { + return getHandler().compareHandler(((HandlerLabel) label).getHandler()); + } + @Override public void appendExtra(SmaliWriter writer) throws IOException { ExceptionHandler handler = this.getHandler(); diff --git a/src/main/java/com/reandroid/dex/ins/ExceptionLabel.java b/src/main/java/com/reandroid/dex/ins/ExceptionLabel.java index dc29eb0f2..ab837e197 100644 --- a/src/main/java/com/reandroid/dex/ins/ExceptionLabel.java +++ b/src/main/java/com/reandroid/dex/ins/ExceptionLabel.java @@ -17,8 +17,14 @@ public interface ExceptionLabel extends Label{ ExceptionHandler getHandler(); + @Override default boolean isRemoved() { return getHandler().isRemoved(); } + + @Override + default int compareLabelName(Label label) { + return 0; + } } diff --git a/src/main/java/com/reandroid/dex/ins/Label.java b/src/main/java/com/reandroid/dex/ins/Label.java index e4c6f924f..cd15b7620 100644 --- a/src/main/java/com/reandroid/dex/ins/Label.java +++ b/src/main/java/com/reandroid/dex/ins/Label.java @@ -40,13 +40,15 @@ default boolean isEqualExtraLine(Object obj) { @Override default int compareExtraLine(ExtraLine other) { int i = ExtraLine.super.compareExtraLine(other); - if(i != 0){ + if (i != 0) { return i; } - if(!(other instanceof Label)){ - return 0; + if (getClass() == other.getClass()) { + return compareLabelName((Label) other); } - Label label = (Label) other; + return 0; + } + default int compareLabelName(Label label) { return getLabelName().compareTo(label.getLabelName()); } }