From 6d48e5db177d74ce0608eeeeca609c9c389cccb2 Mon Sep 17 00:00:00 2001 From: Izzat Bahadirov Date: Tue, 4 Jun 2013 15:35:54 -0400 Subject: [PATCH 1/3] Breaking out of loop after test condition verification. --- .../java/org/jf/dexlib2/writer/JumboStringConversionTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dexlib2/src/test/java/org/jf/dexlib2/writer/JumboStringConversionTest.java b/dexlib2/src/test/java/org/jf/dexlib2/writer/JumboStringConversionTest.java index d77f1117..7ca6b45a 100644 --- a/dexlib2/src/test/java/org/jf/dexlib2/writer/JumboStringConversionTest.java +++ b/dexlib2/src/test/java/org/jf/dexlib2/writer/JumboStringConversionTest.java @@ -272,6 +272,7 @@ public class JumboStringConversionTest { for (Instruction instr: writeUtil.getInstructions()) { if (codeOffset == 21) { Assert.assertEquals("array payload was not aligned properly", instr.getOpcode(), Opcode.NOP); + break; } codeOffset += instr.getCodeUnits(); } @@ -289,6 +290,7 @@ public class JumboStringConversionTest { for (Instruction instr: writeUtil.getInstructions()) { if (codeOffset == 7) { Assert.assertEquals("packed switch payload was not aligned properly", instr.getOpcode(), Opcode.NOP); + break; } codeOffset += instr.getCodeUnits(); } @@ -307,6 +309,7 @@ public class JumboStringConversionTest { for (Instruction instr: writeUtil.getInstructions()) { if (codeOffset == 15) { Assert.assertEquals("packed switch payload was not aligned properly", instr.getOpcode(), Opcode.NOP); + break; } codeOffset += instr.getCodeUnits(); } From 180529840c30043fc77e690c60eb657b180de0f9 Mon Sep 17 00:00:00 2001 From: Izzat Bahadirov Date: Tue, 4 Jun 2013 15:44:12 -0400 Subject: [PATCH 2/3] Always aligning payload instructions. --- .../writer/util/InstructionWriteUtil.java | 14 +- .../dexlib2/writer/PayloadAlignmentTest.java | 131 ++++++++++++++++++ 2 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 dexlib2/src/test/java/org/jf/dexlib2/writer/PayloadAlignmentTest.java diff --git a/dexlib2/src/main/java/org/jf/dexlib2/writer/util/InstructionWriteUtil.java b/dexlib2/src/main/java/org/jf/dexlib2/writer/util/InstructionWriteUtil.java index f77f4192..9a9d417f 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/writer/util/InstructionWriteUtil.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/writer/util/InstructionWriteUtil.java @@ -32,6 +32,7 @@ package org.jf.dexlib2.writer.util; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import org.jf.dexlib2.Format; import org.jf.dexlib2.Opcode; import org.jf.dexlib2.ReferenceType; @@ -148,17 +149,14 @@ public class InstructionWriteUtil 0xFFFF) { - if (codeOffsetShifts == null) { - codeOffsetShifts = new ArrayList(); - } - if (offsetToNewInstructionMap == null) { - offsetToNewInstructionMap = new HashMap(); - } codeOffsetShifts.add(currentCodeOffset+instruction.getCodeUnits()); offsetToNewInstructionMap.put(currentCodeOffset, Opcode.CONST_STRING_JUMBO.format); } @@ -166,10 +164,6 @@ public class InstructionWriteUtil { + public InsnWriteUtil(@Nonnull MethodImplementation implementation) { + super(implementation.getInstructions(), mockStringIndexProvider, ImmutableInstructionFactory.INSTANCE); + } + } + + @Before + public void setup() { + mockStringIndexProvider = new MockStringIndexProvider(); + } + + @Test + public void testArrayPayloadAlignment() { + ArrayList instructions = Lists.newArrayList(); + + // add misaligned array payload + instructions.add(new ImmutableInstruction10x(Opcode.NOP)); + instructions.add(new ImmutableArrayPayload(4, null)); + + ImmutableMethodImplementation methodImplementation = new ImmutableMethodImplementation(1, instructions, null, null); + InsnWriteUtil writeUtil = new InsnWriteUtil(methodImplementation); + + int codeOffset = 0; + for (Instruction instr: writeUtil.getInstructions()) { + if (codeOffset == 1) { + Assert.assertEquals("array payload was not aligned properly", instr.getOpcode(), Opcode.NOP); + break; + } + codeOffset += instr.getCodeUnits(); + } + } + + @Test + public void testPackedSwitchAlignment() { + ArrayList instructions = Lists.newArrayList(); + // add misaligned packed switch payload + ArrayList switchElements = Lists.newArrayList(); + switchElements.add(new ImmutableSwitchElement(0, 5)); + instructions.add(new ImmutableInstruction10x(Opcode.NOP)); + instructions.add(new ImmutablePackedSwitchPayload(switchElements)); + + ImmutableMethodImplementation methodImplementation = new ImmutableMethodImplementation(1, instructions, null, null); + InsnWriteUtil writeUtil = new InsnWriteUtil(methodImplementation); + + int codeOffset = 0; + for (Instruction instr: writeUtil.getInstructions()) { + if (codeOffset == 1) { + Assert.assertEquals("packed switch payload was not aligned properly", instr.getOpcode(), Opcode.NOP); + break; + } + codeOffset += instr.getCodeUnits(); + } + } + + @Test + public void testSparseSwitchAlignment() { + ArrayList instructions = Lists.newArrayList(); + + // add misaligned sparse switch payload + ArrayList switchElements = Lists.newArrayList(); + switchElements.add(new ImmutableSwitchElement(0, 5)); + + instructions.add(new ImmutableInstruction10x(Opcode.NOP)); + instructions.add(new ImmutableSparseSwitchPayload(switchElements)); + + ImmutableMethodImplementation methodImplementation = new ImmutableMethodImplementation(1, instructions, null, null); + InsnWriteUtil writeUtil = new InsnWriteUtil(methodImplementation); + + int codeOffset = 0; + for (Instruction instr: writeUtil.getInstructions()) { + if (codeOffset == 1) { + Assert.assertEquals("packed switch payload was not aligned properly", instr.getOpcode(), Opcode.NOP); + break; + } + codeOffset += instr.getCodeUnits(); + } + } +} From 46619d11bf2f6883def1fe789495aae269fc7dc8 Mon Sep 17 00:00:00 2001 From: Izzat Bahadirov Date: Tue, 4 Jun 2013 21:40:42 -0400 Subject: [PATCH 3/3] More proper unit test conditions. --- .../org/jf/dexlib2/writer/PayloadAlignmentTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dexlib2/src/test/java/org/jf/dexlib2/writer/PayloadAlignmentTest.java b/dexlib2/src/test/java/org/jf/dexlib2/writer/PayloadAlignmentTest.java index f137df53..fe25f42b 100644 --- a/dexlib2/src/test/java/org/jf/dexlib2/writer/PayloadAlignmentTest.java +++ b/dexlib2/src/test/java/org/jf/dexlib2/writer/PayloadAlignmentTest.java @@ -75,8 +75,8 @@ public class PayloadAlignmentTest { int codeOffset = 0; for (Instruction instr: writeUtil.getInstructions()) { - if (codeOffset == 1) { - Assert.assertEquals("array payload was not aligned properly", instr.getOpcode(), Opcode.NOP); + if (instr.getOpcode().equals(Opcode.ARRAY_PAYLOAD)) { + Assert.assertEquals("array payload was not aligned properly", codeOffset%2, 0); break; } codeOffset += instr.getCodeUnits(); @@ -97,8 +97,8 @@ public class PayloadAlignmentTest { int codeOffset = 0; for (Instruction instr: writeUtil.getInstructions()) { - if (codeOffset == 1) { - Assert.assertEquals("packed switch payload was not aligned properly", instr.getOpcode(), Opcode.NOP); + if (instr.getOpcode().equals(Opcode.PACKED_SWITCH_PAYLOAD)) { + Assert.assertEquals("packed switch payload was not aligned properly", codeOffset%2, 0); break; } codeOffset += instr.getCodeUnits(); @@ -121,8 +121,8 @@ public class PayloadAlignmentTest { int codeOffset = 0; for (Instruction instr: writeUtil.getInstructions()) { - if (codeOffset == 1) { - Assert.assertEquals("packed switch payload was not aligned properly", instr.getOpcode(), Opcode.NOP); + if (instr.getOpcode().equals(Opcode.SPARSE_SWITCH_PAYLOAD)) { + Assert.assertEquals("packed switch payload was not aligned properly", codeOffset%2, 0); break; } codeOffset += instr.getCodeUnits();