use of org.objectweb.asm.tree.JumpInsnNode in project evosuite by EvoSuite.
the class ImplicitElseTransformer method handleDependency.
private void handleDependency(ControlDependency dependency, ControlDependenceGraph cdg, MethodNode mn, VarInsnNode varNode, BytecodeInstruction parentLevel) {
if (addedNodes.contains(dependency))
return;
// Get the basic blocks reachable if the dependency would evaluate different
Set<BasicBlock> blocks = cdg.getAlternativeBlocks(dependency);
addedNodes.add(dependency);
Set<ControlDependency> dependencies = dependency.getBranch().getInstruction().getControlDependencies();
// ControlDependency dep = dependencies.iterator().next();
for (ControlDependency dep : dependencies) {
if (!addedNodes.contains(dep) && dep != dependency)
handleDependency(dep, cdg, mn, varNode, dependency.getBranch().getInstruction());
}
// TODO: Need to check that there is an assignment in every alternative path through CDG
boolean hasAssignment = false;
for (BasicBlock block : blocks) {
// If this block also assigns a value to the same variable
for (BytecodeInstruction instruction : block) {
if (instruction.getASMNode().getOpcode() == Opcodes.ISTORE) {
VarInsnNode otherVarNode = (VarInsnNode) instruction.getASMNode();
VarInsnNode thisVarNode = varNode;
if (otherVarNode.var == thisVarNode.var) {
hasAssignment = true;
break;
}
}
}
if (hasAssignment) {
break;
}
}
if (!hasAssignment) {
TransformationStatistics.transformedImplicitElse();
if (dependency.getBranch().getInstruction().isSwitch()) {
BooleanTestabilityTransformation.logger.warn("Don't know how to handle Switches yet");
return;
}
JumpInsnNode jumpNode = (JumpInsnNode) dependency.getBranch().getInstruction().getASMNode();
VarInsnNode newStore = new VarInsnNode(Opcodes.ISTORE, varNode.var);
VarInsnNode newLoad = new VarInsnNode(Opcodes.ILOAD, varNode.var);
if (dependency.getBranchExpressionValue()) {
BooleanTestabilityTransformation.logger.info("Inserting else branch directly after if");
// Insert directly after if
if (isDefinedBefore(mn, varNode, jumpNode)) {
mn.instructions.insert(jumpNode, newStore);
mn.instructions.insert(jumpNode, newLoad);
registerInstruction(mn, varNode, newStore);
registerInstruction(mn, varNode, newLoad);
}
} else {
BooleanTestabilityTransformation.logger.info("Inserting else branch as jump target");
// Insert as jump target
if (isDefinedBefore(mn, varNode, jumpNode)) {
LabelNode target = jumpNode.label;
LabelNode newTarget = new LabelNode(new Label());
// jumpNode or target?
registerInstruction(mn, jumpNode.getNext(), newStore);
registerInstruction(mn, jumpNode.getNext(), newLoad);
InsnList assignment = new InsnList();
assignment.add(new JumpInsnNode(Opcodes.GOTO, target));
assignment.add(newTarget);
assignment.add(newLoad);
assignment.add(newStore);
jumpNode.label = newTarget;
mn.instructions.insertBefore(target, assignment);
}
}
}
}
use of org.objectweb.asm.tree.JumpInsnNode in project evosuite by EvoSuite.
the class ImplicitElseTransformer method handleDependency.
private void handleDependency(ControlDependency dependency, ControlDependenceGraph cdg, MethodNode mn, FieldInsnNode varNode, BytecodeInstruction parentLevel) {
if (addedNodes.contains(dependency))
return;
// Get the basic blocks reachable if the dependency would evaluate different
Set<BasicBlock> blocks = cdg.getAlternativeBlocks(dependency);
addedNodes.add(dependency);
Set<ControlDependency> dependencies = dependency.getBranch().getInstruction().getControlDependencies();
// ControlDependency dep = dependencies.iterator().next();
for (ControlDependency dep : dependencies) {
if (!addedNodes.contains(dep) && dep != dependency)
handleDependency(dep, cdg, mn, varNode, dependency.getBranch().getInstruction());
}
// TODO: Need to check that there is an assignment in every alternative path through CDG
boolean hasAssignment = false;
for (BasicBlock block : blocks) {
// If this block also assigns a value to the same variable
for (BytecodeInstruction instruction : block) {
if (instruction.getASMNode().getOpcode() == Opcodes.PUTFIELD || instruction.getASMNode().getOpcode() == Opcodes.PUTSTATIC) {
FieldInsnNode otherFieldNode = (FieldInsnNode) instruction.getASMNode();
FieldInsnNode thisFieldNode = varNode;
if (otherFieldNode.owner.equals(thisFieldNode.owner) && otherFieldNode.name.equals(thisFieldNode.name)) {
hasAssignment = true;
break;
}
}
}
if (hasAssignment) {
break;
}
}
if (!hasAssignment) {
if (dependency.getBranch().getInstruction().isSwitch()) {
BooleanTestabilityTransformation.logger.warn("Don't know how to handle Switches yet");
return;
}
TransformationStatistics.transformedImplicitElse();
JumpInsnNode jumpNode = (JumpInsnNode) dependency.getBranch().getInstruction().getASMNode();
FieldInsnNode newLoad = new FieldInsnNode(varNode.getOpcode() == Opcodes.PUTSTATIC ? Opcodes.GETSTATIC : Opcodes.GETFIELD, varNode.owner, varNode.name, varNode.desc);
FieldInsnNode newStore = new FieldInsnNode(varNode.getOpcode(), varNode.owner, varNode.name, varNode.desc);
AbstractInsnNode newOwnerLoad1 = null;
AbstractInsnNode newOwnerLoad2 = null;
if (varNode.getOpcode() == Opcodes.PUTFIELD) {
// Need to copy the bloody owner
// Check for VarInsn
// if (varNode.getPrevious().getOpcode() == Opcodes.ALOAD) {
newOwnerLoad1 = new VarInsnNode(Opcodes.ALOAD, 0);
newOwnerLoad2 = new VarInsnNode(Opcodes.ALOAD, 0);
/*
} else {
// Else use helper function
// Insert DUP and
logger.info("Wargh");
System.exit(0);
fieldOwnerId++;
InsnNode dupNode = new InsnNode(Opcodes.DUP);
mn.instructions.insertBefore(varNode, new LdcInsnNode(
fieldOwnerId));
mn.instructions.insertBefore(varNode, dupNode);
registerInstruction(mn, varNode, dupNode);
MethodInsnNode storeOwner = new MethodInsnNode(
Opcodes.INVOKESTATIC,
"org/evosuite/instrumentation/BooleanHelper",
"setFieldOwner", "(ILjava/lang/Object;)V");
mn.instructions.insertBefore(varNode, storeOwner);
registerInstruction(mn, varNode, storeOwner);
newOwnerLoad1 = new MethodInsnNode(Opcodes.INVOKESTATIC,
"org/evosuite/instrumentation/BooleanHelper",
"getFieldOwner", "(I)Ljava/lang/Object;");
newOwnerLoad2 = new MethodInsnNode(Opcodes.INVOKESTATIC,
"org/evosuite/instrumentation/BooleanHelper",
"getFieldOwner", "(I)Ljava/lang/Object;");
}
*/
}
if (dependency.getBranchExpressionValue()) {
BooleanTestabilityTransformation.logger.info("Inserting after if");
// Insert directly after if
mn.instructions.insert(jumpNode, newStore);
mn.instructions.insert(jumpNode, newLoad);
if (newOwnerLoad1 != null) {
mn.instructions.insert(jumpNode, newOwnerLoad1);
registerInstruction(mn, varNode, newOwnerLoad1);
}
if (newOwnerLoad2 != null) {
mn.instructions.insert(jumpNode, newOwnerLoad2);
registerInstruction(mn, varNode, newOwnerLoad2);
}
registerInstruction(mn, varNode, newStore);
registerInstruction(mn, varNode, newLoad);
} else {
BooleanTestabilityTransformation.logger.info("Inserting as jump target");
// Insert as jump target
LabelNode target = jumpNode.label;
LabelNode newTarget = new LabelNode(new Label());
registerInstruction(mn, target, newStore);
registerInstruction(mn, target, newLoad);
InsnList assignment = new InsnList();
assignment.add(new JumpInsnNode(Opcodes.GOTO, target));
assignment.add(newTarget);
if (newOwnerLoad1 != null) {
assignment.add(newOwnerLoad1);
registerInstruction(mn, target, newOwnerLoad1);
}
if (newOwnerLoad2 != null) {
assignment.add(newOwnerLoad2);
registerInstruction(mn, target, newOwnerLoad2);
}
assignment.add(newLoad);
assignment.add(newStore);
jumpNode.label = newTarget;
mn.instructions.insertBefore(target, assignment);
}
addedInsns.add(newStore);
addedInsns.add(newLoad);
}
}
use of org.objectweb.asm.tree.JumpInsnNode in project evosuite by EvoSuite.
the class ContainerTransformation method createNewIfThenElse.
private static InsnList createNewIfThenElse(MethodInsnNode n) {
LabelNode labelIsNotEmpty = new LabelNode();
LabelNode labelEndif = new LabelNode();
InsnList il = new InsnList();
il.add(n);
il.add(new JumpInsnNode(Opcodes.IFLE, labelIsNotEmpty));
il.add(new InsnNode(Opcodes.ICONST_1));
il.add(new JumpInsnNode(Opcodes.GOTO, labelEndif));
il.add(labelIsNotEmpty);
il.add(new InsnNode(Opcodes.ICONST_0));
il.add(labelEndif);
return il;
}
use of org.objectweb.asm.tree.JumpInsnNode in project evosuite by EvoSuite.
the class StringTransformation method transformMethod.
/**
* <p>
* 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;
}
use of org.objectweb.asm.tree.JumpInsnNode in project Random-Things by lumien231.
the class ClassTransformer method patchWorldGenTrees.
private byte[] patchWorldGenTrees(byte[] basicClass) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
logger.log(Level.DEBUG, "Found WorldGenAbstractTree Class: " + classNode.name);
MethodNode setDirtAt = null;
for (MethodNode mn : classNode.methods) {
if (mn.name.equals(MCPNames.method("func_175921_a"))) {
setDirtAt = mn;
break;
}
}
if (setDirtAt != null) {
logger.log(Level.DEBUG, " - Patching setDirtAt");
for (int i = 0; i < setDirtAt.instructions.size(); i++) {
AbstractInsnNode ain = setDirtAt.instructions.get(i);
if (ain instanceof JumpInsnNode) {
JumpInsnNode jin = (JumpInsnNode) ain;
if (jin.getOpcode() == Opcodes.IF_ACMPEQ) {
LabelNode l = jin.label;
InsnList toInsert = new InsnList();
toInsert.add(new VarInsnNode(ALOAD, 1));
toInsert.add(new VarInsnNode(ALOAD, 2));
toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraft/world/World", MCPNames.method("func_180495_p"), "(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/state/IBlockState;", false));
toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "net/minecraft/block/state/IBlockState", MCPNames.method("func_177230_c"), "()Lnet/minecraft/block/Block;", true));
toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "protectGround", "(Lnet/minecraft/block/Block;)Z", false));
toInsert.add(new JumpInsnNode(IFGT, new LabelNode(l.getLabel())));
setDirtAt.instructions.insert(jin, toInsert);
logger.log(Level.DEBUG, " - Patched setDirtAt");
break;
}
}
}
}
CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(writer);
return writer.toByteArray();
}
Aggregations