DexBackedTryBlock.java revision 380ca70a2ccb583d391e4b965dcb29eb2c6f386b
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.dexbacked; 33 34import org.jf.dexlib2.dexbacked.util.InstructionOffsetMap; 35import org.jf.dexlib2.iface.ExceptionHandler; 36import org.jf.dexlib2.iface.TryBlock; 37import org.jf.dexlib2.dexbacked.util.VariableSizeList; 38 39import javax.annotation.Nonnull; 40import java.util.List; 41 42public class DexBackedTryBlock implements TryBlock { 43 public final DexBuffer dexBuf; 44 private final InstructionOffsetMap instructionOffsetMap; 45 46 public final int startIndex; 47 public final int instructionCount; 48 49 private final int exceptionHandlersOffset; 50 51 private static final int START_ADDRESS_OFFSET = 0; 52 private static final int CODE_UNIT_COUNT_OFFSET = 4; 53 private static final int HANDLER_OFFSET_OFFSET = 6; 54 55 public DexBackedTryBlock(DexBuffer dexBuf, 56 int tryItemOffset, 57 int handlersStartOffset, 58 InstructionOffsetMap instructionOffsetMap) { 59 this.dexBuf = dexBuf; 60 this.instructionOffsetMap = instructionOffsetMap; 61 62 int startOffset = dexBuf.readSmallUint(tryItemOffset + START_ADDRESS_OFFSET); 63 // map the code unit offset to the instruction index 64 this.startIndex = instructionOffsetMap.getInstructionIndexAtOffsetExact(startOffset); 65 66 int codeUnitCount = dexBuf.readUshort(tryItemOffset + CODE_UNIT_COUNT_OFFSET); 67 // TODO: check if dalivk accepts insns_size = 0 68 if (codeUnitCount == 0) { 69 this.instructionCount = 0; 70 } else { 71 int lastIndex = instructionOffsetMap.getInstructionIndexAtOffset(startOffset + codeUnitCount - 1); 72 this.instructionCount = lastIndex - startIndex + 1; 73 } 74 75 this.exceptionHandlersOffset = handlersStartOffset + dexBuf.readUshort(tryItemOffset + HANDLER_OFFSET_OFFSET); 76 } 77 78 @Override public int getStartIndex() { return startIndex; } 79 @Override public int getInstructionCount() { return instructionCount; } 80 81 @Nonnull 82 @Override 83 public List<? extends ExceptionHandler> getExceptionHandlers() { 84 DexReader reader = dexBuf.readerAt(exceptionHandlersOffset); 85 final int encodedSize = reader.readSleb128(); 86 87 if (encodedSize > 0) { 88 //no catch-all 89 return new VariableSizeList<ExceptionHandler>(dexBuf, reader.getOffset()) { 90 @Nonnull 91 @Override 92 protected ExceptionHandler readItem(DexReader reader, int index) { 93 return new DexBackedExceptionHandler(reader, instructionOffsetMap); 94 } 95 96 @Override 97 protected void skipItem(DexReader dexReader, int index) { 98 DexBackedExceptionHandler.skipFrom(dexReader); 99 } 100 101 @Override public int size() { return encodedSize; } 102 }; 103 } else { 104 //with catch-all 105 final int sizeWithCatchAll = (-1 * encodedSize) + 1; 106 return new VariableSizeList<ExceptionHandler>(dexBuf, reader.getOffset()) { 107 @Nonnull 108 @Override 109 protected ExceptionHandler readItem(DexReader dexReader, int index) { 110 if (index == sizeWithCatchAll-1) { 111 return new DexBackedCatchAllExceptionHandler(dexReader, instructionOffsetMap); 112 } else { 113 return new DexBackedExceptionHandler(dexReader, instructionOffsetMap); 114 } 115 } 116 117 @Override 118 protected void skipItem(DexReader dexReader, int index) { 119 if (index == sizeWithCatchAll-1) { 120 DexBackedCatchAllExceptionHandler.skipFrom(dexReader); 121 } else { 122 DexBackedExceptionHandler.skipFrom(dexReader); 123 } 124 125 } 126 127 @Override public int size() { return sizeWithCatchAll; } 128 }; 129 } 130 } 131} 132