From 395043667a77dab2f630c42ec80bd2ab859631bb Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Tue, 17 Mar 2015 21:18:19 -0700 Subject: [PATCH] Ensure that sparse switch items are written out in the correct order --- .../test/java/org/jf/baksmali/SwitchTest.java | 41 +++++++++++++++++++ .../UnorderedSparseSwitchInput.smali | 35 ++++++++++++++++ .../UnorderedSparseSwitchOutput.smali | 28 +++++++++++++ .../jf/dexlib2/writer/InstructionWriter.java | 12 +++++- 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 brut.apktool.smali/baksmali/src/test/java/org/jf/baksmali/SwitchTest.java create mode 100644 brut.apktool.smali/baksmali/src/test/resources/SwitchTest/UnorderedSparseSwitchInput.smali create mode 100644 brut.apktool.smali/baksmali/src/test/resources/SwitchTest/UnorderedSparseSwitchOutput.smali diff --git a/brut.apktool.smali/baksmali/src/test/java/org/jf/baksmali/SwitchTest.java b/brut.apktool.smali/baksmali/src/test/java/org/jf/baksmali/SwitchTest.java new file mode 100644 index 00000000..48b64b22 --- /dev/null +++ b/brut.apktool.smali/baksmali/src/test/java/org/jf/baksmali/SwitchTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.baksmali; + +import org.junit.Test; + +public class SwitchTest extends RoundtripTest { + @Test + public void testUnorderedSparseSwitch() { + runTest("UnorderedSparseSwitch"); + } +} diff --git a/brut.apktool.smali/baksmali/src/test/resources/SwitchTest/UnorderedSparseSwitchInput.smali b/brut.apktool.smali/baksmali/src/test/resources/SwitchTest/UnorderedSparseSwitchInput.smali new file mode 100644 index 00000000..6e3d23d4 --- /dev/null +++ b/brut.apktool.smali/baksmali/src/test/resources/SwitchTest/UnorderedSparseSwitchInput.smali @@ -0,0 +1,35 @@ +.class public LUnorderedSparseSwitch; +.super Ljava/lang/Object; + +.method public static test_sparse-switch()V + .registers 1 + + const v0, 13 + + sparse-switch v0, :SparseSwitch + +:Label10 + return-void + +:Label20 + return-void + +:Label15 + return-void + +:Label13 + return-void + +:Label99 + return-void + +# Note: unordered keys +:SparseSwitch + .sparse-switch + 10 -> :Label10 + 20 -> :Label20 + 15 -> :Label15 + 99 -> :Label99 + 13 -> :Label13 + .end sparse-switch +.end method \ No newline at end of file diff --git a/brut.apktool.smali/baksmali/src/test/resources/SwitchTest/UnorderedSparseSwitchOutput.smali b/brut.apktool.smali/baksmali/src/test/resources/SwitchTest/UnorderedSparseSwitchOutput.smali new file mode 100644 index 00000000..c4c455b6 --- /dev/null +++ b/brut.apktool.smali/baksmali/src/test/resources/SwitchTest/UnorderedSparseSwitchOutput.smali @@ -0,0 +1,28 @@ +.class public LUnorderedSparseSwitch; +.super Ljava/lang/Object; +.method public static test_sparse-switch()V +.registers 1 +const v0, 0xd +sparse-switch v0, :sswitch_data_c +:sswitch_6 +return-void +:sswitch_7 +return-void +:sswitch_8 +return-void +:sswitch_9 +return-void +:sswitch_a +return-void +nop + +# Note: ordered keys +:sswitch_data_c +.sparse-switch +0xa -> :sswitch_6 +0xd -> :sswitch_9 +0xf -> :sswitch_8 +0x14 -> :sswitch_7 +0x63 -> :sswitch_a +.end sparse-switch +.end method \ No newline at end of file diff --git a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/InstructionWriter.java b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/InstructionWriter.java index 23a77759..c9aa73a1 100644 --- a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/InstructionWriter.java +++ b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/InstructionWriter.java @@ -31,6 +31,8 @@ package org.jf.dexlib2.writer; +import com.google.common.collect.Ordering; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ReferenceType; import org.jf.dexlib2.iface.instruction.ReferenceInstruction; import org.jf.dexlib2.iface.instruction.SwitchElement; @@ -43,6 +45,7 @@ import org.jf.util.ExceptionWithContext; import javax.annotation.Nonnull; import java.io.IOException; +import java.util.Comparator; import java.util.List; public class InstructionWriter> 8); - List elements = instruction.getSwitchElements(); + List elements = Ordering.from(switchElementComparator).immutableSortedCopy( + instruction.getSwitchElements()); writer.writeUshort(elements.size()); for (SwitchElement element: elements) { writer.writeInt(element.getKey()); @@ -404,6 +408,12 @@ public class InstructionWriter switchElementComparator = new Comparator() { + @Override public int compare(SwitchElement element1, SwitchElement element2) { + return Ints.compare(element1.getKey(), element2.getKey()); + } + }; + public void write(@Nonnull PackedSwitchPayload instruction) { try { writer.writeUbyte(0);