branchtargetfinder.java

来自「proguard 一个java的混淆器」· Java 代码 · 共 689 行 · 第 1/2 页

JAVA
689
字号
/* * ProGuard -- shrinking, optimization, obfuscation, and preverification *             of Java bytecode. * * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package proguard.optimize.peephole;import proguard.classfile.*;import proguard.classfile.attribute.*;import proguard.classfile.attribute.visitor.*;import proguard.classfile.constant.*;import proguard.classfile.constant.visitor.ConstantVisitor;import proguard.classfile.instruction.*;import proguard.classfile.instruction.visitor.InstructionVisitor;import proguard.classfile.util.SimplifiedVisitor;/** * This AttributeVisitor finds all instruction offsets, branch targets, and * exception targets in the CodeAttribute objects that it visits. * * @author Eric Lafortune */public class BranchTargetFinderextends      SimplifiedVisitorimplements   AttributeVisitor,             InstructionVisitor,             ExceptionInfoVisitor,             ConstantVisitor{    //*    private static final boolean DEBUG = false;    /*/    private static       boolean DEBUG = true;    //*/    public static final int NONE            = -2;    public static final int AT_METHOD_ENTRY = -1;    private static final short INSTRUCTION           = 1 << 0;    private static final short BRANCH_ORIGIN         = 1 << 1;    private static final short BRANCH_TARGET         = 1 << 2;    private static final short AFTER_BRANCH          = 1 << 3;    private static final short EXCEPTION_START       = 1 << 4;    private static final short EXCEPTION_END         = 1 << 5;    private static final short EXCEPTION_HANDLER     = 1 << 6;    private static final short SUBROUTINE_INVOCATION = 1 << 7;    private static final short SUBROUTINE_RETURNING  = 1 << 8;    private static final int MAXIMUM_CREATION_OFFSETS = 32;    private short[] instructionMarks      = new short[ClassConstants.TYPICAL_CODE_LENGTH + 1];    private int[]   subroutineStarts      = new int[ClassConstants.TYPICAL_CODE_LENGTH];    private int[]   subroutineEnds        = new int[ClassConstants.TYPICAL_CODE_LENGTH];    private int[]   creationOffsets       = new int[ClassConstants.TYPICAL_CODE_LENGTH];    private int[]   initializationOffsets = new int[ClassConstants.TYPICAL_CODE_LENGTH];    private int     superInitializationOffset;    private int     currentSubroutineStart;    private int     currentSubroutineEnd;    private final int[]   recentCreationOffsets = new int[MAXIMUM_CREATION_OFFSETS];    private int     recentCreationOffsetIndex;    private boolean isInitializer;    /**     * Returns whether there is an instruction at the given offset in the     * CodeAttribute that was visited most recently.     */    public boolean isInstruction(int offset)    {        return (instructionMarks[offset] & INSTRUCTION) != 0;    }    /**     * Returns whether the instruction at the given offset is the target of     * any kind in the CodeAttribute that was visited most recently.     */    public boolean isTarget(int offset)    {        return offset == 0 ||               (instructionMarks[offset] & (BRANCH_TARGET   |                                            EXCEPTION_START |                                            EXCEPTION_END   |                                            EXCEPTION_HANDLER)) != 0;    }    /**     * Returns whether the instruction at the given offset is the origin of a     * branch instruction in the CodeAttribute that was visited most recently.     */    public boolean isBranchOrigin(int offset)    {        return (instructionMarks[offset] & BRANCH_ORIGIN) != 0;    }    /**     * Returns whether the instruction at the given offset is the target of a     * branch instruction in the CodeAttribute that was visited most recently.     */    public boolean isBranchTarget(int offset)    {        return (instructionMarks[offset] & BRANCH_TARGET) != 0;    }    /**     * Returns whether the instruction at the given offset comes right after a     * definite branch instruction in the CodeAttribute that was visited most     * recently.     */    public boolean isAfterBranch(int offset)    {        return (instructionMarks[offset] & AFTER_BRANCH) != 0;    }    /**     * Returns whether the instruction at the given offset is the start of an     * exception try block in the CodeAttribute that was visited most recently.     */    public boolean isExceptionStart(int offset)    {        return (instructionMarks[offset] & EXCEPTION_START) != 0;    }    /**     * Returns whether the instruction at the given offset is the end of an     * exception try block in the CodeAttribute that was visited most recently.     */    public boolean isExceptionEnd(int offset)    {        return (instructionMarks[offset] & EXCEPTION_END) != 0;    }    /**     * Returns whether the instruction at the given offset is the start of an     * exception catch block in the CodeAttribute that was visited most recently.     */    public boolean isExceptionHandler(int offset)    {        return (instructionMarks[offset] & EXCEPTION_HANDLER) != 0;    }    /**     * Returns whether the instruction at the given offset is a subroutine     * invocation in the CodeAttribute that was visited most recently.     */    public boolean isSubroutineInvocation(int offset)    {        return (instructionMarks[offset] & SUBROUTINE_INVOCATION) != 0;    }    /**     * Returns whether the instruction at the given offset is the start of a     * subroutine in the CodeAttribute that was visited most recently.     */    public boolean isSubroutineStart(int offset)    {        return subroutineStarts[offset] == offset;    }    /**     * Returns whether the instruction at the given offset is part of a     * subroutine in the CodeAttribute that was visited most recently.     */    public boolean isSubroutine(int offset)    {        return subroutineStarts[offset] != NONE;    }    /**     * Returns whether the subroutine at the given offset is ever returning     * by means of a regular 'ret' instruction.     */    public boolean isSubroutineReturning(int offset)    {        return (instructionMarks[offset] & SUBROUTINE_RETURNING) != 0;    }    /**     * Returns the start offset of the subroutine at the given offset, in the     * CodeAttribute that was visited most recently.     */    public int subroutineStart(int offset)    {        return subroutineStarts[offset];    }    /**     * Returns the offset after the subroutine at the given offset, in the     * CodeAttribute that was visited most recently.     */    public int subroutineEnd(int offset)    {        return subroutineEnds[offset];    }    /**     * Returns whether the instruction at the given offset is a 'new'     * instruction, in the CodeAttribute that was visited most recently.     */    public boolean isNew(int offset)    {        return initializationOffsets[offset] != NONE;    }    /**     * Returns the instruction offset at which the object instance that is     * created at the given 'new' instruction offset is initialized, or     * <code>NONE</code> if it is not being created.     */    public int initializationOffset(int offset)    {        return initializationOffsets[offset];    }    /**     * Returns whether the method is an instance initializer, in the     * CodeAttribute that was visited most recently.     */    public boolean isInitializer()    {        return superInitializationOffset != NONE;    }    /**     * Returns the instruction offset at which this initializer is calling     * the "super" or "this" initializer method, or <code>NONE</code> if it is     * not an initializer.     */    public int superInitializationOffset()    {        return superInitializationOffset;    }    /**     * Returns whether the instruction at the given offset is the special     * invocation of an instance initializer, in the CodeAttribute that was     * visited most recently.     */    public boolean isInitializer(int offset)    {        return creationOffsets[offset] != NONE;    }    /**     * Returns the offset of the 'new' instruction that corresponds to the     * invocation of the instance initializer at the given offset, or     * <code>AT_METHOD_ENTRY</code> if the invocation is calling the "super" or     * "this" initializer method, , or <code>NONE</code> if it is not a 'new'     * instruction.     */    public int creationOffset(int offset)    {        return creationOffsets[offset];    }    // Implementations for AttributeVisitor.    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)    {//        DEBUG =//            clazz.getName().equals("abc/Def") &&//            method.getName(clazz).equals("abc");        // Make sure there are sufficiently large arrays.        int codeLength = codeAttribute.u4codeLength;        if (subroutineStarts.length < codeLength)        {            // Create new arrays.            instructionMarks      = new short[codeLength + 1];            subroutineStarts      = new int[codeLength];            subroutineEnds        = new int[codeLength];            creationOffsets       = new int[codeLength];            initializationOffsets = new int[codeLength];            // Reset the arrays.            for (int index = 0; index < codeLength; index++)            {                subroutineStarts[index]      = NONE;                subroutineEnds[index]        = NONE;                creationOffsets[index]       = NONE;                initializationOffsets[index] = NONE;            }        }        else        {            // Reset the arrays.            for (int index = 0; index < codeLength; index++)            {                instructionMarks[index]      = 0;                subroutineStarts[index]      = NONE;                subroutineEnds[index]        = NONE;                creationOffsets[index]       = NONE;                initializationOffsets[index] = NONE;            }            instructionMarks[codeLength] = 0;        }        superInitializationOffset = NONE;        // We're not starting in a subroutine.        currentSubroutineStart = NONE;        currentSubroutineEnd   = NONE;        recentCreationOffsetIndex = 0;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?