List of usage examples for org.objectweb.asm.tree AbstractInsnNode getPrevious
public AbstractInsnNode getPrevious()
From source file:org.diorite.inject.controller.TransformerInjectTracker.java
License:Open Source License
private InjectionType checkIndirect() { AbstractInsnNode previous = this.getPrevious(true); // find var index if ((previous instanceof VarInsnNode) && AsmUtils.isLoadCode(previous.getOpcode())) { this.loadCode = new LoadCode((VarInsnNode) previous); // XLOAD 0 don't exist here in any case. if (!this.isStatic && (this.loadCode.getIndex() == 0)) { throw new AnalyzeError("Unexpected loading of `this` variable!"); }/*w w w. ja v a 2s .c o m*/ previous = previous.getPrevious(); } else { return InjectionType.UNKNOWN; } // track all local variable changes previous = this.trackLoadBackwards(previous); // final check for placeholder method PlaceholderType injectPlaceholder = Transformer.isInjectPlaceholder(previous); if (injectPlaceholder != PlaceholderType.INVALID) { assert previous instanceof MethodInsnNode; this.placeholderNode = (MethodInsnNode) previous; this.placeholderType = injectPlaceholder; this.injectionType = InjectionType.INDIRECT; return this.injectionType; } return InjectionType.UNKNOWN; }
From source file:org.diorite.inject.controller.TransformerInjectTracker.java
License:Open Source License
private AbstractInsnNode trackLoadBackwards(AbstractInsnNode node) { assert this.loadCode != null; // skip checkcast and possible XLOAD 0 node = this.skipCheckCastBackwards(node); // find compatible STORE node. while ((!(node instanceof VarInsnNode)) || !this.loadCode.isCompatible((VarInsnNode) node)) { node = node.getPrevious(); }// w ww .ja v a2 s . c om // one more to skip STORE node = node.getPrevious(); // node is STORE opcode for our variable, there might be checkcast here: node = this.skipCheckCastBackwards(node); // now there might be INVOKESTATIC or some other indirections: // if there is XLOAD different than 0 (if not static), then we have next layer of indirect store. if ((node instanceof VarInsnNode) && (!this.isStatic && (((VarInsnNode) node).var != 0)) && (((VarInsnNode) node).var != this.loadCode.getIndex()) && AsmUtils.isLoadCode(node.getOpcode())) { this.loadCode = new LoadCode((VarInsnNode) node); node = this.trackLoadBackwards(node); } // but if there is INVOKE* instead, then we need to go inside invoked method and keep looking for injection placeholder! But skip void methods. else if ((node instanceof MethodInsnNode) && AsmUtils.isInvokeCode(node.getOpcode()) && !((MethodInsnNode) node).desc.endsWith("V")) { MethodInsnNode methodInvoke = (MethodInsnNode) node; // maybe this is already placeholder invoke? if (Transformer.isInjectPlaceholder(methodInvoke) != PlaceholderType.INVALID) { return node; } node = this.trackMethod(methodInvoke); } else { throw new AnalyzeError("Can't track given field!"); } // now there should be finally INVOKESTATIC, so we can return. return node; }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.StackBalanceAnalyzer.java
License:Open Source License
/** * Answer the closest point in the instruction list before 'location' * where the stack has 'stackDifference' more bytes than at 'location'. * Negative 'stackDifference' means we are looking for smaller stack. * //from w w w .j a v a2s . c om * @param location start searching here * @param stackDifference * @return the last instruction before which the required stack size was present */ public static AbstractInsnNode findInsertionPointBefore(AbstractInsnNode location, int stackDifference) { int offset = 0; AbstractInsnNode current = location; while (offset != stackDifference) { current = location.getPrevious(); if (current == null) return null; offset -= SIZE[current.getOpcode()]; } return current; }
From source file:org.evosuite.instrumentation.coverage.BranchInstrumentation.java
License:Open Source License
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override/*from ww w .j av a 2 s . co m*/ public void analyze(ClassLoader classLoader, MethodNode mn, String className, String methodName, int access) { this.classLoader = classLoader; RawControlFlowGraph graph = GraphPool.getInstance(classLoader).getRawCFG(className, methodName); Iterator<AbstractInsnNode> j = mn.instructions.iterator(); while (j.hasNext()) { AbstractInsnNode in = j.next(); for (BytecodeInstruction v : graph.vertexSet()) { // If this is in the CFG and it's a branch... if (in.equals(v.getASMNode())) { if (v.isBranch()) { if (in.getPrevious() instanceof LabelNode) { LabelNode label = (LabelNode) in.getPrevious(); if (label.getLabel() instanceof AnnotatedLabel) { AnnotatedLabel aLabel = (AnnotatedLabel) label.getLabel(); if (aLabel.isStartTag()) { if (!aLabel.shouldIgnore()) { logger.debug("Found artificial branch: " + v); Branch b = BranchPool.getInstance(classLoader).getBranchForInstruction(v); b.setInstrumented(true); } else { continue; } } } } mn.instructions.insertBefore(v.getASMNode(), getInstrumentation(v)); } else if (v.isSwitch()) { mn.instructions.insertBefore(v.getASMNode(), getSwitchInstrumentation(v, mn, className, methodName)); } } } } mn.maxStack += 4; }
From source file:org.evosuite.instrumentation.StringTransformation.java
License:Open Source License
/** * <p>/* w w w .ja v a 2 s . c om*/ * transformMethod * </p> * * @param mn * a {@link org.objectweb.asm.tree.MethodNode} object. * @return a boolean. */ public boolean transformMethod(MethodNode mn) { boolean changed = transformStrings(mn); if (changed) { try { mn.maxStack++; Analyzer a = new Analyzer(new StringBooleanInterpreter()); a.analyze(cn.name, mn); Frame[] frames = a.getFrames(); AbstractInsnNode node = mn.instructions.getFirst(); boolean done = false; while (!done) { if (node == mn.instructions.getLast()) done = true; AbstractInsnNode next = node.getNext(); int index = mn.instructions.indexOf(node); if (index >= frames.length) break; Frame current = frames[index]; if (current == null) break; int size = current.getStackSize(); if (node.getOpcode() == Opcodes.IFNE) { JumpInsnNode branch = (JumpInsnNode) node; if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN || isStringMethod(node.getPrevious())) { logger.info("IFNE -> IFGT"); branch.setOpcode(Opcodes.IFGT); // branch.setOpcode(Opcodes.IFGE); } } else if (node.getOpcode() == Opcodes.IFEQ) { JumpInsnNode branch = (JumpInsnNode) node; if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN || isStringMethod(node.getPrevious())) { logger.info("IFEQ -> IFLE"); branch.setOpcode(Opcodes.IFLE); // branch.setOpcode(Opcodes.IFNE); } } else if (node.getOpcode() == Opcodes.IF_ICMPEQ) { JumpInsnNode branch = (JumpInsnNode) node; if (current.getStack(size - 2) == StringBooleanInterpreter.STRING_BOOLEAN || isStringMethod(node.getPrevious().getPrevious())) { if (node.getPrevious().getOpcode() == Opcodes.ICONST_0) { branch.setOpcode(Opcodes.IFLE); mn.instructions.remove(node.getPrevious()); } else if (node.getPrevious().getOpcode() == Opcodes.ICONST_1) { branch.setOpcode(Opcodes.IFGT); mn.instructions.remove(node.getPrevious()); } } } else if (node.getOpcode() == Opcodes.IF_ICMPNE) { JumpInsnNode branch = (JumpInsnNode) node; if (current.getStack(size - 2) == StringBooleanInterpreter.STRING_BOOLEAN || isStringMethod(node.getPrevious().getPrevious())) { if (node.getPrevious().getOpcode() == Opcodes.ICONST_0) { branch.setOpcode(Opcodes.IFGT); mn.instructions.remove(node.getPrevious()); } else if (node.getPrevious().getOpcode() == Opcodes.ICONST_1) { branch.setOpcode(Opcodes.IFLE); mn.instructions.remove(node.getPrevious()); } } } else if (node.getOpcode() == Opcodes.IRETURN) { if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN || isStringMethod(node.getPrevious())) { logger.info("IFEQ -> IFLE"); MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "intToBoolean", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE })); mn.instructions.insertBefore(node, n); } } node = next; } } catch (Exception e) { logger.warn("EXCEPTION DURING STRING TRANSFORMATION: {}", e); e.printStackTrace(); return changed; } } return changed; }
From source file:org.evosuite.instrumentation.testability.StringTransformation.java
License:Open Source License
/** * <p>//from ww w . j a va 2 s . c o m * transformMethod * </p> * * @param mn * a {@link org.objectweb.asm.tree.MethodNode} object. * @return a boolean. */ public boolean transformMethod(MethodNode mn) { boolean changed = transformStrings(mn); if (changed) { try { mn.maxStack++; Analyzer a = new Analyzer(new StringBooleanInterpreter()); a.analyze(cn.name, mn); Frame[] frames = a.getFrames(); AbstractInsnNode node = mn.instructions.getFirst(); boolean done = false; while (!done) { if (node == mn.instructions.getLast()) done = true; AbstractInsnNode next = node.getNext(); int index = mn.instructions.indexOf(node); if (index >= frames.length) break; Frame current = frames[index]; if (current == null) break; int size = current.getStackSize(); if (node.getOpcode() == Opcodes.IFNE) { JumpInsnNode branch = (JumpInsnNode) node; if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN || isStringMethod(node.getPrevious())) { logger.info("IFNE -> IFGT"); branch.setOpcode(Opcodes.IFGT); } } else if (node.getOpcode() == Opcodes.IFEQ) { JumpInsnNode branch = (JumpInsnNode) node; if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN || isStringMethod(node.getPrevious())) { logger.info("IFEQ -> IFLE"); branch.setOpcode(Opcodes.IFLE); } } else if (node.getOpcode() == Opcodes.IF_ICMPEQ) { JumpInsnNode branch = (JumpInsnNode) node; if (current.getStack(size - 2) == StringBooleanInterpreter.STRING_BOOLEAN || isStringMethod(node.getPrevious().getPrevious())) { if (node.getPrevious().getOpcode() == Opcodes.ICONST_0) { branch.setOpcode(Opcodes.IFLE); mn.instructions.remove(node.getPrevious()); } else if (node.getPrevious().getOpcode() == Opcodes.ICONST_1) { branch.setOpcode(Opcodes.IFGT); mn.instructions.remove(node.getPrevious()); } } } else if (node.getOpcode() == Opcodes.IF_ICMPNE) { JumpInsnNode branch = (JumpInsnNode) node; if (current.getStack(size - 2) == StringBooleanInterpreter.STRING_BOOLEAN || isStringMethod(node.getPrevious().getPrevious())) { if (node.getPrevious().getOpcode() == Opcodes.ICONST_0) { branch.setOpcode(Opcodes.IFGT); mn.instructions.remove(node.getPrevious()); } else if (node.getPrevious().getOpcode() == Opcodes.ICONST_1) { branch.setOpcode(Opcodes.IFLE); mn.instructions.remove(node.getPrevious()); } } } else if (node.getOpcode() == Opcodes.IRETURN) { if (current.getStack(size - 1) == StringBooleanInterpreter.STRING_BOOLEAN || isStringMethod(node.getPrevious())) { logger.info("IFEQ -> IFLE"); MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "intToBoolean", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE }), false); mn.instructions.insertBefore(node, n); } } node = next; } } catch (Exception e) { logger.warn("EXCEPTION DURING STRING TRANSFORMATION: " + e); return changed; } } return changed; }
From source file:org.evosuite.instrumentation.testability.transformer.ImplicitElseTransformer.java
License:Open Source License
@SuppressWarnings("unchecked") private boolean isDefinedBefore(MethodNode mn, VarInsnNode var, AbstractInsnNode position) { // TODO: Iterate over local variables and check if local is defined here List<LocalVariableNode> localVar = mn.localVariables; if (localVar.isEmpty()) { // If we have no debug information, try to guess AbstractInsnNode pos = position.getPrevious(); while (pos != mn.instructions.getFirst()) { if (pos instanceof VarInsnNode) { VarInsnNode vn = (VarInsnNode) pos; if (var.var == vn.var) { return true; }/*from ww w.j a v a 2 s . c o m*/ } pos = pos.getPrevious(); } } else { int current = mn.instructions.indexOf(position); for (LocalVariableNode local : localVar) { if (local.index == var.var) { int start = mn.instructions.indexOf(local.start); int end = mn.instructions.indexOf(local.end); if (current >= start && current <= end) return true; } } } return false; }
From source file:org.evosuite.instrumentation.testability.transformer.ImplicitElseTransformer.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w ww . ja v a 2 s . co m protected AbstractInsnNode transformFieldInsnNode(MethodNode mn, FieldInsnNode fieldNode) { if ((fieldNode.getOpcode() == Opcodes.PUTFIELD || fieldNode.getOpcode() == Opcodes.PUTSTATIC) && DescriptorMapping.getInstance().isTransformedOrBooleanField(fieldNode.owner, fieldNode.name, fieldNode.desc)) { if (addedInsns.contains(fieldNode)) return fieldNode; // Can only handle cases where the field owner is loaded directly before the field // TODO: We could pop the top of the stack and DUP the owner, but would need to take care // whether we need to pop one or two words if (fieldNode.getOpcode() == Opcodes.PUTFIELD) { AbstractInsnNode previous = fieldNode.getPrevious(); while (previous instanceof LineNumberNode || previous instanceof FrameNode || previous.getOpcode() == Opcodes.ICONST_0 || previous.getOpcode() == Opcodes.ICONST_1) previous = previous.getPrevious(); if (previous.getOpcode() != Opcodes.ALOAD) { BooleanTestabilityTransformation.logger.info("Can't handle case of " + previous); return fieldNode; } VarInsnNode varNode = (VarInsnNode) previous; if (varNode.var != 0) { BooleanTestabilityTransformation.logger.info("Can't handle case of " + previous); return fieldNode; } } BooleanTestabilityTransformation.logger.info("Handling PUTFIELD case!"); // Check if ICONST_0 or ICONST_1 are on the stack ControlDependenceGraph cdg = GraphPool.getInstance(this.booleanTestabilityTransformation.classLoader) .getCDG(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc); int index = mn.instructions.indexOf(fieldNode); BooleanTestabilityTransformation.logger.info("Getting bytecode instruction for " + fieldNode.name + "/" + ((FieldInsnNode) mn.instructions.get(index)).name); InsnList nodes = mn.instructions; ListIterator<AbstractInsnNode> it = nodes.iterator(); while (it.hasNext()) { BytecodeInstruction in = new BytecodeInstruction(this.booleanTestabilityTransformation.classLoader, this.booleanTestabilityTransformation.className, mn.name, 0, 0, it.next()); BooleanTestabilityTransformation.logger.info(in.toString()); } BytecodeInstruction insn = BytecodeInstructionPool .getInstance(this.booleanTestabilityTransformation.classLoader) .getInstruction(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc, index); if (insn == null) insn = BytecodeInstructionPool.getInstance(this.booleanTestabilityTransformation.classLoader) .getInstruction(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc, fieldNode); //varNode); if (insn == null) { // TODO: Find out why BooleanTestabilityTransformation.logger.info("ERROR: Could not find node"); return fieldNode; } if (insn.getASMNode().getOpcode() != fieldNode.getOpcode()) { BooleanTestabilityTransformation.logger.info("Found wrong bytecode instruction at this index!"); BytecodeInstructionPool.getInstance(this.booleanTestabilityTransformation.classLoader) .getInstruction(this.booleanTestabilityTransformation.className, mn.name + mn.desc, fieldNode); } if (insn.getBasicBlock() == null) { BooleanTestabilityTransformation.logger.info("ERROR: Problematic node found"); return fieldNode; } Set<ControlDependency> dependencies = insn.getControlDependencies(); BooleanTestabilityTransformation.logger.info("Found flag assignment: " + insn + ", checking " + dependencies.size() + " control dependencies"); for (ControlDependency dep : dependencies) { if (!addedNodes.contains(dep)) handleDependency(dep, cdg, mn, fieldNode, insn); } } return fieldNode; }
From source file:org.friz.bytecode.insn.InsnNodeUtility.java
License:Open Source License
public static AbstractInsnNode getPrevInsn(AbstractInsnNode n) { while (n != null && n.getOpcode() == -1) { n = n.getPrevious(); }// ww w. j av a2 s .c o m return n; }
From source file:org.jacoco.core.internal.analysis.filter.KotlinWhenStringFilterTest.java
License:Open Source License
@Test public void should_filter() { final Set<AbstractInsnNode> expectedNewTargets = new HashSet<AbstractInsnNode>(); final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "()V", null, null); final Label h1 = new Label(); final Label sameHash = new Label(); final Label h2 = new Label(); final Label case1 = new Label(); final Label case2 = new Label(); final Label case3 = new Label(); final Label defaultCase = new Label(); // filter should not remember this unrelated slot m.visitLdcInsn(""); m.visitVarInsn(Opcodes.ASTORE, 1);/*from w ww .j a v a 2s. c o m*/ m.visitVarInsn(Opcodes.ALOAD, 1); // switch (...) m.visitVarInsn(Opcodes.ASTORE, 2); m.visitVarInsn(Opcodes.ALOAD, 2); m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false); m.visitTableSwitchInsn(97, 98, defaultCase, h1, h2); // case "a" m.visitLabel(h1); final AbstractInsnNode expectedFromInclusive = m.instructions.getLast(); m.visitVarInsn(Opcodes.ALOAD, 2); m.visitLdcInsn("a"); m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false); m.visitJumpInsn(Opcodes.IFEQ, sameHash); m.visitJumpInsn(Opcodes.GOTO, case1); // case "\u0000a" m.visitLabel(sameHash); m.visitVarInsn(Opcodes.ALOAD, 2); m.visitLdcInsn("\u0000a"); m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false); m.visitJumpInsn(Opcodes.IFEQ, defaultCase); m.visitJumpInsn(Opcodes.GOTO, case2); // case "b" m.visitLabel(h2); m.visitVarInsn(Opcodes.ALOAD, 2); m.visitLdcInsn("\u0000a"); m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false); m.visitJumpInsn(Opcodes.IFEQ, defaultCase); m.visitJumpInsn(Opcodes.GOTO, case3); final AbstractInsnNode expectedToInclusive = m.instructions.getLast(); m.visitLabel(case1); m.visitInsn(Opcodes.RETURN); expectedNewTargets.add(m.instructions.getLast()); m.visitLabel(case2); m.visitInsn(Opcodes.RETURN); expectedNewTargets.add(m.instructions.getLast()); m.visitLabel(case3); m.visitInsn(Opcodes.RETURN); expectedNewTargets.add(m.instructions.getLast()); m.visitLabel(defaultCase); m.visitInsn(Opcodes.RETURN); expectedNewTargets.add(m.instructions.getLast()); filter.filter(m, context, output); assertReplacedBranches(expectedFromInclusive.getPrevious(), expectedNewTargets); assertIgnored(new Range(expectedFromInclusive, expectedToInclusive)); }