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 32 package org.jf.dexlib2.util; 33 34 import com.google.common.collect.ImmutableList; 35 import junit.framework.Assert; 36 import org.jf.dexlib2.Opcode; 37 import org.jf.dexlib2.immutable.ImmutableMethodImplementation; 38 import org.jf.dexlib2.immutable.instruction.*; 39 import org.jf.dexlib2.immutable.reference.ImmutableStringReference; 40 import org.jf.dexlib2.immutable.reference.ImmutableTypeReference; 41 import org.jf.util.ExceptionWithContext; 42 import org.junit.Test; 43 44 public class InstructionOffsetMapTest { 45 @Test 46 public void testInstructionOffsetMap() { 47 ImmutableList<ImmutableInstruction> instructions = ImmutableList.of( 48 /*00: 0x00*/ new ImmutableInstruction10t(Opcode.GOTO, 1), 49 /*01: 0x01*/ new ImmutableInstruction10x(Opcode.NOP), 50 /*02: 0x02*/ new ImmutableInstruction11n(Opcode.CONST_4, 2, 3), 51 /*03: 0x03*/ new ImmutableInstruction11x(Opcode.RETURN, 4), 52 /*04: 0x04*/ new ImmutableInstruction12x(Opcode.ARRAY_LENGTH, 5, 6), 53 /*05: 0x05*/ new ImmutableInstruction20t(Opcode.GOTO_16, 7), 54 /*06: 0x07*/ new ImmutableInstruction21c(Opcode.CONST_STRING, 8, new ImmutableStringReference("blah")), 55 /*07: 0x09*/ new ImmutableInstruction21ih(Opcode.CONST_HIGH16, 9, 0x10000), 56 /*08: 0x0b*/ new ImmutableInstruction21lh(Opcode.CONST_WIDE_HIGH16, 10, 0x1000000000000L), 57 /*09: 0x0d*/ new ImmutableInstruction21s(Opcode.CONST_16, 11, 12), 58 /*10: 0x0f*/ new ImmutableInstruction21t(Opcode.IF_EQZ, 12, 13), 59 /*11: 0x11*/ new ImmutableInstruction22b(Opcode.ADD_INT_LIT8, 14, 15, 16), 60 /*12: 0x13*/ new ImmutableInstruction22c(Opcode.INSTANCE_OF, 0, 1, 61 new ImmutableTypeReference("Ltype;")), 62 /*13: 0x15*/ new ImmutableInstruction22s(Opcode.ADD_INT_LIT16, 2, 3, 17), 63 /*14: 0x17*/ new ImmutableInstruction22t(Opcode.IF_EQ, 4, 5, 18), 64 /*15: 0x19*/ new ImmutableInstruction22x(Opcode.MOVE_FROM16, 19, 20), 65 /*16: 0x1b*/ new ImmutableInstruction23x(Opcode.AGET, 21, 22, 23), 66 /*17: 0x1d*/ new ImmutableInstruction30t(Opcode.GOTO_32, 24), 67 /*18: 0x20*/ new ImmutableInstruction31c(Opcode.CONST_STRING_JUMBO, 25, 68 new ImmutableStringReference("this is a string")), 69 /*19: 0x23*/ new ImmutableInstruction31i(Opcode.CONST, 26, 27), 70 /*20: 0x26*/ new ImmutableInstruction31t(Opcode.FILL_ARRAY_DATA, 28, 29), 71 /*21: 0x29*/ new ImmutableInstruction32x(Opcode.MOVE_16, 30, 31), 72 /*22: 0x2c*/ new ImmutableInstruction35c(Opcode.FILLED_NEW_ARRAY, 0, 0, 0, 0, 0, 0, 73 new ImmutableTypeReference("Ltype;")), 74 /*23: 0x2f*/ new ImmutableInstruction3rc(Opcode.FILLED_NEW_ARRAY_RANGE, 0, 0, 75 new ImmutableTypeReference("Ltype;")), 76 /*24: 0x32*/ new ImmutableInstruction51l(Opcode.CONST_WIDE, 32, 33), 77 /*25: 0x37*/ new ImmutableInstruction10t(Opcode.GOTO, 1) 78 ); 79 ImmutableMethodImplementation impl = new ImmutableMethodImplementation(33, instructions, null, null); 80 InstructionOffsetMap instructionOffsetMap = new InstructionOffsetMap(instructions); 81 82 int[] expectedOffsets = new int[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x09, 0x0b, 0x0d, 0x0f, 0x11, 83 0x13, 0x15, 0x17, 0x19, 0x1b, 0x1d, 0x20, 0x23, 0x26, 0x29, 0x2c, 0x2f, 0x32, 0x37}; 84 85 for (int i=0; i<instructions.size(); i++) { 86 Assert.assertEquals(expectedOffsets[i], instructionOffsetMap.getInstructionCodeOffset(i)); 87 Assert.assertEquals(i, instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[i], true)); 88 Assert.assertEquals(i, instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[i], false)); 89 } 90 91 int instructionIndex = -1; 92 for (int codeOffset=0; codeOffset<=expectedOffsets[expectedOffsets.length-1]; codeOffset++) { 93 if (codeOffset == expectedOffsets[instructionIndex+1]) { 94 // this offset is at the beginning of an instruction 95 instructionIndex++; 96 } else { 97 // this offset is in the middle of an instruction 98 Assert.assertEquals(instructionIndex, 99 instructionOffsetMap.getInstructionIndexAtCodeOffset(codeOffset, false)); 100 101 try { 102 instructionOffsetMap.getInstructionIndexAtCodeOffset(codeOffset, true); 103 Assert.fail(String.format("Exception exception didn't occur for code offset 0x%x", codeOffset)); 104 } catch (ExceptionWithContext ex) { 105 // expected exception 106 } 107 } 108 } 109 Assert.assertEquals(expectedOffsets.length-1, 110 instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[expectedOffsets.length-1]+1, false)); 111 Assert.assertEquals(expectedOffsets.length-1, 112 instructionOffsetMap.getInstructionIndexAtCodeOffset(expectedOffsets[expectedOffsets.length-1]+10, false)); 113 } 114 } 115