InstructionOffsetMapTest.java revision d1662b67fecaf835227aff3a136949a2358ccd4e
1/*
2 * Copyright 2012, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.dexlib2.util;
33
34import com.google.common.collect.ImmutableList;
35import junit.framework.Assert;
36import org.jf.dexlib2.Opcode;
37import org.jf.dexlib2.immutable.ImmutableMethodImplementation;
38import org.jf.dexlib2.immutable.instruction.*;
39import org.jf.util.ExceptionWithContext;
40import org.junit.Test;
41
42public class InstructionOffsetMapTest {
43    @Test
44    public void testInstructionOffsetMap() {
45        ImmutableList<ImmutableInstruction> instructions = ImmutableList.of(
46                /*00: 0x00*/ new ImmutableInstruction10t(Opcode.GOTO, 1),
47                /*01: 0x01*/ new ImmutableInstruction10x(Opcode.NOP),
48                /*02: 0x02*/ new ImmutableInstruction11n(Opcode.CONST_4, 2, 3),
49                /*03: 0x03*/ new ImmutableInstruction11x(Opcode.RETURN, 4),
50                /*04: 0x04*/ new ImmutableInstruction12x(Opcode.ARRAY_LENGTH, 5, 6),
51                /*05: 0x05*/ new ImmutableInstruction20t(Opcode.GOTO_16, 7),
52                /*06: 0x07*/ new ImmutableInstruction21c(Opcode.SGET, 8, "Lclass;->field:Ltype;"),
53                /*07: 0x09*/ new ImmutableInstruction21ih(Opcode.CONST_HIGH16, 9, 0x10000),
54                /*08: 0x0b*/ new ImmutableInstruction21lh(Opcode.CONST_WIDE_HIGH16, 10, 0x1000000000000L),
55                /*09: 0x0d*/ new ImmutableInstruction21s(Opcode.CONST_16, 11, 12),
56                /*10: 0x0f*/ new ImmutableInstruction21t(Opcode.IF_EQZ, 12, 13),
57                /*11: 0x11*/ new ImmutableInstruction22b(Opcode.ADD_INT_LIT8, 14, 15, 16),
58                /*12: 0x13*/ new ImmutableInstruction22c(Opcode.INSTANCE_OF, 0, 1, "Ltype;"),
59                /*13: 0x15*/ new ImmutableInstruction22s(Opcode.ADD_INT_LIT16, 2, 3, 17),
60                /*14: 0x17*/ new ImmutableInstruction22t(Opcode.IF_EQ, 4, 5, 18),
61                /*15: 0x19*/ new ImmutableInstruction22x(Opcode.MOVE_FROM16, 19, 20),
62                /*16: 0x1b*/ new ImmutableInstruction23x(Opcode.AGET, 21, 22, 23),
63                /*17: 0x1d*/ new ImmutableInstruction30t(Opcode.GOTO_32, 24),
64                /*18: 0x20*/ new ImmutableInstruction31c(Opcode.CONST_STRING_JUMBO, 25, "this is a string"),
65                /*19: 0x23*/ new ImmutableInstruction31i(Opcode.CONST, 26, 27),
66                /*20: 0x26*/ new ImmutableInstruction31t(Opcode.FILL_ARRAY_DATA, 28, 29),
67                /*21: 0x29*/ new ImmutableInstruction32x(Opcode.MOVE_16, 30, 31),
68                /*22: 0x2c*/ new ImmutableInstruction35c(Opcode.INVOKE_STATIC, 0, 0, 0, 0, 0, 0, "Lclass;->method()V"),
69                /*23: 0x2f*/ new ImmutableInstruction3rc(Opcode.INVOKE_STATIC_RANGE, 0, 0, "Lclass;->method()V" ),
70                /*24: 0x32*/ new ImmutableInstruction51l(Opcode.CONST_WIDE, 32, 33),
71                /*25: 0x37*/ new ImmutableInstruction10t(Opcode.GOTO, 1)
72        );
73        ImmutableMethodImplementation impl = new ImmutableMethodImplementation(33, instructions, null, null);
74        InstructionOffsetMap instructionOffsetMap = new InstructionOffsetMap(instructions);
75
76        int[] expectedOffsets = new int[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11,
77                0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x20, 0x23, 0x26, 0x29, 0x2c, 0x2f, 0x32, 0x37};
78
79        for (int i=0; i<instructions.size(); i++) {
80            Assert.assertEquals(expectedOffsets[i], instructionOffsetMap.getInstructionCodeOffset(i));
81            Assert.assertEquals(i, instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[i], true));
82            Assert.assertEquals(i, instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[i], false));
83        }
84
85        int instructionIndex = -1;
86        for (int codeOffset=0; codeOffset<=expectedOffsets[expectedOffsets.length-1]; codeOffset++) {
87            if (codeOffset == expectedOffsets[instructionIndex+1]) {
88                // this offset is at the beginning of an instruction
89                instructionIndex++;
90            } else {
91                // this offset is in the middle of an instruction
92                Assert.assertEquals(instructionIndex,
93                        instructionOffsetMap.getInstructionIndexAtCodeOffset(codeOffset, false));
94
95                try {
96                    instructionOffsetMap.getInstructionIndexAtCodeOffset(codeOffset, true);
97                    Assert.fail(String.format("Exception exception didn't occur for code offset 0x%x", codeOffset));
98                } catch (ExceptionWithContext ex) {
99                    // expected exception
100                }
101            }
102        }
103        Assert.assertEquals(expectedOffsets.length-1,
104                instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[expectedOffsets.length-1]+1, false));
105        Assert.assertEquals(expectedOffsets.length-1,
106                instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[expectedOffsets.length-1]+10, false));
107    }
108}
109