List of usage examples for org.objectweb.asm.tree AbstractInsnNode JUMP_INSN
int JUMP_INSN
To view the source code for org.objectweb.asm.tree AbstractInsnNode JUMP_INSN.
Click Source Link
From source file:blockphysics.asm.BPTransformer.java
License:Open Source License
private byte[] transformGuiSelectWorld(byte[] bytes) { /*try// w w w . j ava 2 s . co m { FileOutputStream fos = new FileOutputStream("d:/GuiSelectWorld.orig.class"); fos.write(bytes); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ System.out.print("[BlockPhysics] Patching GuiSelectWorld.class .........."); boolean ok = false, ok2 = false; ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); MethodNode m; Iterator<MethodNode> methods = classNode.methods.iterator(); while (methods.hasNext()) { m = methods.next(); if (m.name.equals("A_") && m.desc.equals("()V")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new InsnNode(ICONST_0)); toInject.add(new FieldInsnNode(PUTFIELD, "awe", "d", "Z")); for (int index = m.instructions.size() - 1; index >= 0; index--) { if (m.instructions.get(index).getOpcode() == RETURN) { m.instructions.insertBefore(m.instructions.get(index), toInject); ok = true; break; } } } else if (m.name.equals("e") && m.desc.equals("(I)V")) { for (int index = 0; index < m.instructions.size(); index++) { if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN && ((MethodInsnNode) m.instructions.get(index)).owner.equals("ats") && ((MethodInsnNode) m.instructions.get(index)).name.equals("a") && ((MethodInsnNode) m.instructions.get(index)).desc .equals("(Ljava/lang/String;Ljava/lang/String;Lacc;)V")) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(ALOAD, 0)); toInject.add(new VarInsnNode(ALOAD, 2)); toInject.add(new VarInsnNode(ALOAD, 3)); toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BClient", "loadWorld", "(Lawb;Ljava/lang/String;Ljava/lang/String;)V")); m.instructions.insert(m.instructions.get(index), toInject); while (m.instructions.get(index).getType() != AbstractInsnNode.JUMP_INSN) { m.instructions.remove(m.instructions.get(index)); index--; } ok2 = true; break; } } } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(cw); if (ok && ok2) System.out.println("OK"); else System.out.println("Failed." + ok + ok2); /*try { FileOutputStream fos = new FileOutputStream("d:/GuiSelectWorld.class"); fos.write(cw.toByteArray()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ return cw.toByteArray(); }
From source file:cl.inria.stiq.instrumenter.BCIUtils.java
License:Open Source License
private static void printFrames(MethodNode aNode, Frame[] aFrames) { int bcIndex = 1; for (int i = 0; i < aFrames.length; i++) { Frame theFrame = aFrames[i]; AbstractInsnNode theInsn = aNode.instructions.get(i); switch (theInsn.getType()) { case AbstractInsnNode.INSN: case AbstractInsnNode.INT_INSN: case AbstractInsnNode.VAR_INSN: case AbstractInsnNode.TYPE_INSN: case AbstractInsnNode.FIELD_INSN: case AbstractInsnNode.METHOD_INSN: case AbstractInsnNode.JUMP_INSN: case AbstractInsnNode.LDC_INSN: case AbstractInsnNode.IINC_INSN: case AbstractInsnNode.TABLESWITCH_INSN: case AbstractInsnNode.LOOKUPSWITCH_INSN: case AbstractInsnNode.MULTIANEWARRAY_INSN: TraceMethodVisitor theTraceVisitor = new TraceMethodVisitor(); theInsn.accept(theTraceVisitor); StringWriter theWriter = new StringWriter(); theTraceVisitor.print(new PrintWriter(theWriter)); String theTraced = theWriter.toString().replace("\n", ""); System.out.println(bcIndex + "\t" + frameString(theFrame) + " |\t" + theTraced); bcIndex++;//from w w w .ja v a2s.com break; case AbstractInsnNode.FRAME: case AbstractInsnNode.LINE: case AbstractInsnNode.LABEL: break; } } }
From source file:com.android.tools.lint.checks.WakelockDetector.java
License:Apache License
/** Search from the given node towards the target; return false if we reach * an exit point such as a return or a call on the way there that is not within * a try/catch clause.// ww w . j a v a 2 s . co m * * @param node the current node * @return true if the target was reached * XXX RETURN VALUES ARE WRONG AS OF RIGHT NOW */ protected int dfs(ControlFlowGraph.Node node) { AbstractInsnNode instruction = node.instruction; if (instruction.getType() == AbstractInsnNode.JUMP_INSN) { int opcode = instruction.getOpcode(); if (opcode == Opcodes.RETURN || opcode == Opcodes.ARETURN || opcode == Opcodes.LRETURN || opcode == Opcodes.IRETURN || opcode == Opcodes.DRETURN || opcode == Opcodes.FRETURN || opcode == Opcodes.ATHROW) { if (DEBUG) { System.out.println("Found exit via explicit return: " //$NON-NLS-1$ + node.toString(false)); } return SEEN_RETURN; } } if (!DEBUG) { // There are no cycles, so no *NEED* for this, though it does avoid // researching shared labels. However, it makes debugging harder (no re-entry) // so this is only done when debugging is off if (node.visit != 0) { return 0; } node.visit = 1; } // Look for the target. This is any method call node which is a release on the // lock (later also check it's the same instance, though that's harder). // This is because finally blocks tend to be inlined so from a single try/catch/finally // with a release() in the finally, the bytecode can contain multiple repeated // (inlined) release() calls. if (instruction.getType() == AbstractInsnNode.METHOD_INSN) { MethodInsnNode method = (MethodInsnNode) instruction; if (method.name.equals(RELEASE_METHOD) && method.owner.equals(WAKELOCK_OWNER)) { return SEEN_TARGET; } else if (method.name.equals(ACQUIRE_METHOD) && method.owner.equals(WAKELOCK_OWNER)) { // OK } else if (method.name.equals(IS_HELD_METHOD) && method.owner.equals(WAKELOCK_OWNER)) { // OK } else { // Some non acquire/release method call: if this is not associated with a // try-catch block, it would mean the exception would exit the method, // which would be an error if (node.exceptions == null || node.exceptions.isEmpty()) { // Look up the corresponding frame, if any AbstractInsnNode curr = method.getPrevious(); boolean foundFrame = false; while (curr != null) { if (curr.getType() == AbstractInsnNode.FRAME) { foundFrame = true; break; } curr = curr.getPrevious(); } if (!foundFrame) { if (DEBUG) { System.out.println("Found exit via unguarded method call: " //$NON-NLS-1$ + node.toString(false)); } return SEEN_RETURN; } } } } // if (node.instruction is a call, and the call is not caught by // a try/catch block (provided the release is not inside the try/catch block) // then return false int status = 0; boolean implicitReturn = true; List<Node> successors = node.successors; List<Node> exceptions = node.exceptions; if (exceptions != null) { if (!exceptions.isEmpty()) { implicitReturn = false; } for (Node successor : exceptions) { status = dfs(successor) | status; if ((status & SEEN_RETURN) != 0) { if (DEBUG) { System.out.println("Found exit via exception: " //$NON-NLS-1$ + node.toString(false)); } return status; } } if (status != 0) { status |= SEEN_EXCEPTION; } } if (successors != null) { if (!successors.isEmpty()) { implicitReturn = false; if (successors.size() > 1) { status |= SEEN_BRANCH; } } for (Node successor : successors) { status = dfs(successor) | status; if ((status & SEEN_RETURN) != 0) { if (DEBUG) { System.out.println("Found exit via branches: " //$NON-NLS-1$ + node.toString(false)); } return status; } } } if (implicitReturn) { status |= SEEN_RETURN; if (DEBUG) { System.out.println("Found exit: via implicit return: " //$NON-NLS-1$ + node.toString(false)); } } return status; }
From source file:com.dank.analysis.visitor.DummyParameterVisitor.java
License:GNU General Public License
public void accept(MethodNode mn) { Type[] types = Type.getArgumentTypes(mn.desc); if (types.length != 0) { this.mn = mn; for (final AbstractInsnNode ain0 : mn.instructions.toArray()) { if (ain0.type() != AbstractInsnNode.JUMP_INSN || ain0.next() == null) continue; final JumpInsnNode jn = (JumpInsnNode) ain0; AbstractInsnNode ain = ain0.next(); if (ain != null && (ain.opcode() == RETURN || (ain.opcode() == NEW && ((TypeInsnNode) ain).desc.equals("java/lang/IllegalStateException")))) { boolean flip = false; AbstractInsnNode arg = jn.previous(); if (arg != null) { AbstractInsnNode load = arg.previous(); if (load != null) { int predicate = numberFor(arg); if (predicate == Integer.MAX_VALUE) { predicate = numberFor(load); flip = true; }//from w w w .ja v a 2s . c om if (predicate != Integer.MAX_VALUE) { predicate = validPredicateFor(jn, predicate, flip); VALUES.put(mn.owner.name + '.' + mn.name + mn.desc, predicate); } } } } } } }
From source file:com.github.fge.grappa.transform.RuleMethodInterpreter.java
License:Open Source License
private static boolean isLabelOrJump(AbstractInsnNode node) { return node.getType() == AbstractInsnNode.LABEL || node.getType() == AbstractInsnNode.JUMP_INSN; }
From source file:com.lodgon.parboiled.transform.RuleMethodInterpreter.java
License:Open Source License
public void newControlFlowEdge(int instructionIndex, int successorIndex) { AbstractInsnNode fromInsn = method.instructions.get(instructionIndex); AbstractInsnNode toInsn = method.instructions.get(successorIndex); if (fromInsn.getType() == AbstractInsnNode.LABEL || fromInsn.getType() == AbstractInsnNode.JUMP_INSN || toInsn.getType() == AbstractInsnNode.LABEL || toInsn.getType() == AbstractInsnNode.JUMP_INSN) { additionalEdges.add(new Edge(fromInsn, toInsn)); }//from w w w . jav a2s.c om }
From source file:cz.vutbr.fit.xhriba01.bc.lib.AbstractNodeVisitor.java
License:Open Source License
/** * This method detects what type of instruction the NodeInstruction is * representing and calls appropriate before/visit/after methods. * Subclasses could rather override methods specific for particular * instruction type./*from w w w .j a va2 s .c o m*/ * * @param nodeInstruction the instruction node */ public void visitNodeInstruction(NodeInstruction nodeInstruction) { AbstractInsnNode asmInsn = nodeInstruction.getAsmInsnNode(); switch (asmInsn.getType()) { case AbstractInsnNode.LINE: visitLineNumberNode((LineNumberNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.LABEL: visitLabelNode((LabelNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.FIELD_INSN: visitFieldInsn((FieldInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.IINC_INSN: visitIincInsn((IincInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INSN: visitInsn((InsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INT_INSN: visitIntInsn((IntInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INVOKE_DYNAMIC_INSN: visitInvokeDynamicInsn((InvokeDynamicInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.JUMP_INSN: visitJumpInsn((JumpInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.LDC_INSN: visitLdcInsn((LdcInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.LOOKUPSWITCH_INSN: visitLookupSwitchInsn((LookupSwitchInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.METHOD_INSN: visitMethodInsn((MethodInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.MULTIANEWARRAY_INSN: visitMultiANewArrayInsn((MultiANewArrayInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.TABLESWITCH_INSN: visitTableSwitchInsn((TableSwitchInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.TYPE_INSN: visitTypeInsn((TypeInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.VAR_INSN: visitVarInsn((VarInsnNode) asmInsn, nodeInstruction); break; default: // shouldn't happen break; } }
From source file:cz.vutbr.fit.xhriba01.bc.lib.AbstractNodeVisitor.java
License:Open Source License
public void afterVisitNodeInstruction(NodeInstruction nodeInstruction) { AbstractInsnNode asmInsn = nodeInstruction.getAsmInsnNode(); switch (asmInsn.getType()) { case AbstractInsnNode.LINE: afterVisitLineNumberNode((LineNumberNode) asmInsn, nodeInstruction); break;/*from w ww. j a v a 2 s.co m*/ case AbstractInsnNode.LABEL: afterVisitLabelNode((LabelNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.FIELD_INSN: afterVisitFieldInsn((FieldInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.IINC_INSN: afterVisitIincInsn((IincInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INSN: afterVisitInsn((InsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INT_INSN: afterVisitIntInsn((IntInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INVOKE_DYNAMIC_INSN: afterVisitInvokeDynamicInsn((InvokeDynamicInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.JUMP_INSN: afterVisitJumpInsn((JumpInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.LDC_INSN: afterVisitLdcInsn((LdcInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.LOOKUPSWITCH_INSN: afterVisitLookupSwitchInsn((LookupSwitchInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.METHOD_INSN: afterVisitMethodInsn((MethodInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.MULTIANEWARRAY_INSN: afterVisitMultiANewArrayInsn((MultiANewArrayInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.TABLESWITCH_INSN: afterVisitTableSwitchInsn((TableSwitchInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.TYPE_INSN: afterVisitTypeInsn((TypeInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.VAR_INSN: afterVisitVarInsn((VarInsnNode) asmInsn, nodeInstruction); break; default: // shouldn't happen break; } }
From source file:de.codesourcery.asm.controlflow.ControlFlowAnalyzer.java
License:Apache License
@SuppressWarnings("unchecked") public ControlFlowGraph analyze(String owner, final MethodNode mn) throws AnalyzerException { // line numbers with associated block // initially we'll create one block per line and merge adjacent ones later if control flow permits it final Map<Integer, IBlock> blocks = new HashMap<>(); final ListIterator<AbstractInsnNode> it = mn.instructions.iterator(); IBlock currentLine = null;//from w ww. j av a 2 s . co m Object previousMetadata = null; IBlock previous = null; final IBlock methodExit = new MethodExit(); for (int instrCounter = 0; it.hasNext(); instrCounter++) { final AbstractInsnNode instruction = it.next(); currentLine = getBlockForInstruction(instrCounter, blocks); if (previous != null) { previous.addSuccessor(currentLine, EdgeType.REGULAR, previousMetadata); currentLine.addRegularPredecessor(previous); previousMetadata = null; } IBlock nextPrevious = currentLine; switch (instruction.getType()) { case AbstractInsnNode.LOOKUPSWITCH_INSN: LookupSwitchInsnNode lookup = (LookupSwitchInsnNode) instruction; // add edge for default handler if (lookup.dflt != null) { final IBlock target = getBlockForInstruction(lookup.dflt, mn, blocks); target.addRegularPredecessor(currentLine); currentLine.addRegularSuccessor(target); } @SuppressWarnings("cast") final Iterator<Integer> keys = (Iterator<Integer>) lookup.keys.iterator(); for (LabelNode ln : (List<LabelNode>) lookup.labels) { final IBlock target = getBlockForInstruction(ln, mn, blocks); final Integer key = keys.next(); target.addPredecessor(currentLine, EdgeType.LOOKUP_SWITCH, key); currentLine.addSuccessor(target, EdgeType.LOOKUP_SWITCH, key); } nextPrevious = null; break; case AbstractInsnNode.TABLESWITCH_INSN: TableSwitchInsnNode tblSwitch = (TableSwitchInsnNode) instruction; // add edge for default handler if (tblSwitch.dflt != null) { final IBlock target = getBlockForInstruction(tblSwitch.dflt, mn, blocks); target.addRegularPredecessor(currentLine); currentLine.addRegularSuccessor(target); } int currentKey = tblSwitch.min; for (LabelNode ln : (List<LabelNode>) tblSwitch.labels) { final IBlock target = getBlockForInstruction(ln, mn, blocks); target.addPredecessor(currentLine, EdgeType.TABLE_SWITCH, currentKey); currentLine.addSuccessor(target, EdgeType.TABLE_SWITCH, currentKey); currentKey++; } nextPrevious = null; break; case AbstractInsnNode.INSN: if (instruction.getOpcode() == Opcodes.RETURN || instruction.getOpcode() == Opcodes.IRETURN) /* method exit */ { currentLine.addRegularSuccessor(methodExit); methodExit.addRegularPredecessor(currentLine); nextPrevious = null; } else if (instruction.getOpcode() == Opcodes.ATHROW || instruction.getOpcode() == Opcodes.RET) { nextPrevious = null; } break; case AbstractInsnNode.JUMP_INSN: /* jump */ final JumpInsnNode jmp = (JumpInsnNode) instruction; final LabelNode label = jmp.label; final int target = mn.instructions.indexOf(label); final boolean isConditional = ASMUtil.isConditionalJump(instruction); if (isConditional) { // label edges of conditional jump instructions with "true" and "false previousMetadata = "false"; } final IBlock targetBlock = getBlockForInstruction(target, blocks); targetBlock.addRegularPredecessor(currentLine); // create edge from current block to jump target currentLine.addSuccessor(targetBlock, EdgeType.REGULAR, isConditional ? "true" : null); if (instruction.getOpcode() == Opcodes.GOTO) { nextPrevious = null; } break; } // link last instruction with method_exit block if (!it.hasNext()) { currentLine.addRegularSuccessor(methodExit); methodExit.addRegularPredecessor(currentLine); } previous = nextPrevious; } // try/catch blocks need special treatment because // they are not represented as opcodes for (TryCatchBlockNode node : (List<TryCatchBlockNode>) mn.tryCatchBlocks) { final LabelNode startLabel = node.start; final int startTarget = mn.instructions.indexOf(startLabel); final LabelNode endLabel = node.end; final int endTarget = mn.instructions.indexOf(endLabel); final int handlerTarget = mn.instructions.indexOf(node.handler); IBlock handler = getBlockForInstruction(node.handler, mn, blocks); for (int i = startTarget; i <= endTarget; i++) { if (i != handlerTarget) { getBlockForInstruction(i, blocks).addExceptionHandler(handler, node.type); } } } // merge adjacent instructions final Set<Integer> linesBeforeMerge = new HashSet<>(); for (IBlock block : blocks.values()) { linesBeforeMerge.addAll(block.getInstructionNums()); } final List<IBlock> result = mergeBlocks(blocks, mn); if (debug) { System.out.println("################ Control-blocks merged ################"); } // sanity check final Set<Integer> linesAfterMerge = new HashSet<>(); for (IBlock block : result) { linesAfterMerge.addAll(block.getInstructionNums()); if (debug) { System.out.println("-----"); System.out.println(block + " has " + block.getByteCodeInstructionCount(mn) + " instructions."); System.out.println(block.disassemble(mn, false, true)); } for (Edge e : block.getEdges()) { if (!result.contains(e.src) && e.src != methodExit) { throw new RuntimeException(e + " has src that is not in result list?"); } if (!result.contains(e.dst) && e.dst != methodExit) { throw new RuntimeException(e + " has destination that is not in result list?"); } } } if (!linesBeforeMerge.equals(linesAfterMerge)) { throw new RuntimeException("Internal error, line count mismatch before/after control block merge: \n\n" + linesBeforeMerge + "\n\n" + linesAfterMerge); } // add starting block and link it with block that contains the lowest instruction number MethodEntry methodEntry = new MethodEntry(); int lowest = Integer.MAX_VALUE; for (Integer i : blocks.keySet()) { if (i < lowest) { lowest = i; } } final IBlock firstBlock = blocks.get(lowest); if (firstBlock.hasRegularPredecessor()) { throw new IllegalStateException(firstBlock + " that constrains first instruction has a predecessor?"); } methodEntry.addRegularSuccessor(firstBlock); firstBlock.addRegularPredecessor(methodEntry); result.add(0, methodEntry); // add end block to results result.add(methodExit);//owner+"#"+ ControlFlowGraph cfg = new ControlFlowGraph(mn, result); System.out.println("CFGMAP:" + formatname(owner) + "#" + cfg.getMethod().name); graphmap.put(formatname(owner) + "#" + cfg.getMethod().name, cfg); return cfg; }
From source file:de.codesourcery.asm.util.ASMUtil.java
License:Apache License
/** * Check whether an instruction is a conditional branch operation. * // ww w.j a v a2s. com * @param node * @return */ public static boolean isConditionalJump(AbstractInsnNode node) { if (node.getType() == AbstractInsnNode.JUMP_INSN) { switch (node.getOpcode()) { case Opcodes.IFEQ: case Opcodes.IFNE: case Opcodes.IFLT: case Opcodes.IFGE: case Opcodes.IFGT: case Opcodes.IFLE: case Opcodes.IF_ICMPEQ: case Opcodes.IF_ICMPNE: case Opcodes.IF_ICMPLT: case Opcodes.IF_ICMPGE: case Opcodes.IF_ICMPGT: case Opcodes.IF_ICMPLE: case Opcodes.IF_ACMPEQ: case Opcodes.IF_ACMPNE: case Opcodes.IFNULL: case Opcodes.IFNONNULL: return true; } } return false; }