Ensure that sparse switch items are written out in the correct order

This commit is contained in:
Ben Gruver 2015-03-17 21:18:19 -07:00 committed by Connor Tumbleson
parent 32a400396d
commit 395043667a
4 changed files with 115 additions and 1 deletions

View File

@ -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");
}
}

View File

@ -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

View File

@ -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

View File

@ -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<StringRef extends StringReference, TypeRef extends TypeReference,
@ -391,7 +394,8 @@ public class InstructionWriter<StringRef extends StringReference, TypeRef extend
try {
writer.writeUbyte(0);
writer.writeUbyte(instruction.getOpcode().value >> 8);
List<? extends SwitchElement> elements = instruction.getSwitchElements();
List<? extends SwitchElement> 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<StringRef extends StringReference, TypeRef extend
}
}
private final Comparator<SwitchElement> switchElementComparator = new Comparator<SwitchElement>() {
@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);