📄 instructionsequencereplacer.java
字号:
/* * 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.instruction.visitor.InstructionVisitor;import proguard.classfile.instruction.*;import proguard.classfile.editor.CodeAttributeEditor;import proguard.classfile.*;import proguard.classfile.util.SimplifiedVisitor;import proguard.classfile.constant.*;import proguard.classfile.constant.visitor.ConstantVisitor;import proguard.classfile.attribute.CodeAttribute;/** * This InstructionVisitor replaces a given pattern instruction sequence by * another given replacement instruction sequence. The arguments of the * instruction sequences can be wildcards that are matched and replaced. * * @author Eric Lafortune */public class InstructionSequenceReplacerextends SimplifiedVisitorimplements InstructionVisitor, ConstantVisitor{ private static final boolean DEBUG = false; public static final int X = 0x40000000; public static final int Y = 0x40000001; public static final int Z = 0x40000002; public static final int A = 0x40000003; public static final int B = 0x40000004; public static final int C = 0x40000005; public static final int D = 0x40000006; private Constant[] patternConstants; private Instruction[] patternInstructions; private Instruction[] replacementInstructions; private BranchTargetFinder branchTargetFinder; private CodeAttributeEditor codeAttributeEditor; private InstructionVisitor extraInstructionVisitor; private MyReplacementInstructionFactory replacementInstructionFactory = new MyReplacementInstructionFactory(); private int patternInstructionIndex; private int[] matchedInstructionOffsets; private int matchedArgumentFlags; private int[] matchedArguments = new int[7]; private int matchedConstantFlags; private int[] matchedConstantIndices; // Fields acting as a parameter and a return value for visitor methods. private Constant patternConstant; private boolean matchingConstant; /** * Creates a new InstructionSequenceReplacer. * @param patternConstants any constants referenced by the pattern * instruction. * @param patternInstructions the pattern instruction sequence. * @param replacementInstructions the replacement instruction sequence. * @param branchTargetFinder a branch target finder that has been * initialized to indicate branch targets * in the visited code. * @param codeAttributeEditor a code editor that can be used for * accumulating changes to the code. */ public InstructionSequenceReplacer(Constant[] patternConstants, Instruction[] patternInstructions, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor) { this(patternConstants, patternInstructions, replacementInstructions, branchTargetFinder, codeAttributeEditor, null); } /** * Creates a new InstructionSequenceReplacer. * @param patternConstants any constants referenced by the pattern * instruction. * @param branchTargetFinder a branch target finder that has been * initialized to indicate branch targets * in the visited code. * @param codeAttributeEditor a code editor that can be used for * accumulating changes to the code. * @param extraInstructionVisitor an optional extra visitor for all deleted * load instructions. */ public InstructionSequenceReplacer(Constant[] patternConstants, Instruction[] patternInstructions, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor, InstructionVisitor extraInstructionVisitor) { this.patternConstants = patternConstants; this.patternInstructions = patternInstructions; this.replacementInstructions = replacementInstructions; this.branchTargetFinder = branchTargetFinder; this.codeAttributeEditor = codeAttributeEditor; this.extraInstructionVisitor = extraInstructionVisitor; matchedInstructionOffsets = new int[patternInstructions.length]; matchedConstantIndices = new int[patternConstants.length]; } // Implementations for InstructionVisitor. public void visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction) { Instruction patternInstruction = patternInstructions[patternInstructionIndex]; // Check if the instruction matches the next instruction in the from // sequence. boolean condition = acceptableOffset(offset) && matchingOpcodes(simpleInstruction, patternInstruction) && matchingArguments(simpleInstruction.constant, ((SimpleInstruction)patternInstruction).constant); // Act on it. matchInstruction(condition, clazz, method, codeAttribute, offset, simpleInstruction); } public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction) { Instruction patternInstruction = patternInstructions[patternInstructionIndex]; // Check if the instruction matches the next instruction in the from // sequence. boolean condition = acceptableOffset(offset) && matchingOpcodes(variableInstruction, patternInstruction) && matchingArguments(variableInstruction.variableIndex, ((VariableInstruction)patternInstruction).variableIndex) && matchingArguments(variableInstruction.constant, ((VariableInstruction)patternInstruction).constant); // Act on it. matchInstruction(condition, clazz, method, codeAttribute, offset, variableInstruction); } public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction) { Instruction patternInstruction = patternInstructions[patternInstructionIndex]; // Check if the instruction matches the next instruction in the from // sequence. boolean condition = acceptableOffset(offset) && matchingOpcodes(constantInstruction, patternInstruction) && matchingConstantIndices(clazz, constantInstruction.constantIndex, ((ConstantInstruction)patternInstruction).constantIndex) && matchingArguments(constantInstruction.constant, ((ConstantInstruction)patternInstruction).constant); // Act on it. matchInstruction(condition, clazz, method, codeAttribute, offset, constantInstruction); } public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction) { Instruction patternInstruction = patternInstructions[patternInstructionIndex]; // Check if the instruction matches the next instruction in the from // sequence. boolean condition = acceptableOffset(offset) && matchingOpcodes(branchInstruction, patternInstruction) && matchingBranchOffsets(offset, branchInstruction.branchOffset, ((BranchInstruction)patternInstruction).branchOffset); // Act on it. matchInstruction(condition, clazz, method, codeAttribute, offset, branchInstruction); } public void visitTableSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, TableSwitchInstruction tableSwitchInstruction) { Instruction patternInstruction = patternInstructions[patternInstructionIndex]; // Check if the instruction matches the next instruction in the from // sequence. boolean condition = acceptableOffset(offset) && matchingOpcodes(tableSwitchInstruction, patternInstruction) && matchingArguments(offset + tableSwitchInstruction.defaultOffset, ((TableSwitchInstruction)patternInstruction).defaultOffset) && matchingArguments(tableSwitchInstruction.lowCase, ((TableSwitchInstruction)patternInstruction).lowCase) && matchingArguments(tableSwitchInstruction.highCase, ((TableSwitchInstruction)patternInstruction).highCase) && matchingArguments(tableSwitchInstruction.jumpOffsetCount, ((TableSwitchInstruction)patternInstruction).jumpOffsetCount) && matchingJumpOffsets(offset, tableSwitchInstruction.jumpOffsets, ((TableSwitchInstruction)patternInstruction).jumpOffsets, tableSwitchInstruction.jumpOffsetCount); // Act on it. matchInstruction(condition, clazz, method, codeAttribute, offset, tableSwitchInstruction);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -