use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.
the class ReplaceVariable method getInfectionDistance.
/**
* <p>
* getInfectionDistance
* </p>
*
* @param type
* a {@link org.objectweb.asm.Type} object.
* @param original
* a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
* @param mutant
* a {@link org.objectweb.asm.tree.InsnList} object.
* @return a {@link org.objectweb.asm.tree.InsnList} object.
*/
public InsnList getInfectionDistance(Type type, AbstractInsnNode original, InsnList mutant) {
// TODO: Treat reference types different!
InsnList distance = new InsnList();
if (original instanceof VarInsnNode) {
VarInsnNode node = (VarInsnNode) original;
distance.add(new VarInsnNode(node.getOpcode(), node.var));
if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("["))
addReferenceDistanceCheck(distance, type, mutant);
else
addPrimitiveDistanceCheck(distance, type, mutant);
} else if (original instanceof FieldInsnNode) {
if (original.getOpcode() == Opcodes.GETFIELD)
// make sure to re-load this for GETFIELD
distance.add(new InsnNode(Opcodes.DUP));
FieldInsnNode node = (FieldInsnNode) original;
distance.add(new FieldInsnNode(node.getOpcode(), node.owner, node.name, node.desc));
if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("["))
addReferenceDistanceCheck(distance, type, mutant);
else
addPrimitiveDistanceCheck(distance, type, mutant);
} else if (original instanceof IincInsnNode) {
distance.add(Mutation.getDefaultInfectionDistance());
}
return distance;
}
use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.
the class RegressionClassDiff method getClassInstructions.
/*
* Get bytecode instructions for the class based on the following format: Method -> List of
* instructions
*/
private static Map<String, List<Integer>> getClassInstructions(InputStream classAsInputStream) {
HashMap<String, List<Integer>> methodInstructionsMap = new HashMap<>();
try {
ClassReader reader = new ClassReader(classAsInputStream);
ClassNode classNode = new ClassNode();
reader.accept(classNode, 0);
@SuppressWarnings("unchecked") final List<MethodNode> methods = classNode.methods;
Printer printer = new Textifier();
TraceMethodVisitor mp = new TraceMethodVisitor(printer);
for (MethodNode m : methods) {
List<Integer> instructions = new ArrayList<>();
InsnList inList = m.instructions;
String mathodID = m.name + ": " + m.desc;
System.out.println(mathodID);
int[] methodInstructions = new int[inList.size()];
for (int i = 0; i < inList.size(); i++) {
int op = inList.get(i).getOpcode();
methodInstructions[i] = op;
AbstractInsnNode insn = inList.get(i);
insn.accept(mp);
// logger.warn("{} -> {}", sw.toString(), op);
if (op != -1)
instructions.add(op);
}
methodInstructionsMap.put(mathodID, instructions);
}
} catch (IOException e) {
// Will fail if ClassReader fails
e.printStackTrace();
}
return methodInstructionsMap;
}
use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.
the class CastClassAnalyzer method handleMethodNode.
/**
* Add all possible calls for a given method
*
* @param mn
*/
@SuppressWarnings("unchecked")
public void handleMethodNode(ClassNode cn, MethodNode mn, int depth) {
if (mn.signature != null) {
logger.debug("Visiting signature: " + mn.signature);
CollectParameterTypesVisitor visitor = new CollectParameterTypesVisitor(cn.name);
new SignatureReader(mn.signature).accept(visitor);
for (Type castType : visitor.getClasses()) {
if (!castClassMap.containsKey(castType)) {
logger.debug("Adding new cast class from signature visitor: " + castType);
castClassMap.put(castType, depth + 1);
}
}
}
InsnList instructions = mn.instructions;
Iterator<AbstractInsnNode> iterator = instructions.iterator();
// TODO: This really shouldn't be here but in its own class
while (iterator.hasNext()) {
AbstractInsnNode insn = iterator.next();
if (insn.getOpcode() == Opcodes.CHECKCAST) {
TypeInsnNode typeNode = (TypeInsnNode) insn;
Type castType = Type.getObjectType(typeNode.desc);
while (castType.getSort() == Type.ARRAY) {
castType = castType.getElementType();
}
logger.debug("Adding new cast class from cast: " + castType);
if (!castClassMap.containsKey(castType))
castClassMap.put(castType, depth + 1);
} else if (insn.getOpcode() == Opcodes.INSTANCEOF) {
TypeInsnNode typeNode = (TypeInsnNode) insn;
Type castType = Type.getObjectType(typeNode.desc);
while (castType.getSort() == Type.ARRAY) {
castType = castType.getElementType();
}
logger.debug("Adding new cast class from instanceof: " + castType);
if (!castClassMap.containsKey(castType))
castClassMap.put(castType, depth + 1);
} else if (insn.getOpcode() == Opcodes.LDC) {
LdcInsnNode ldcNode = (LdcInsnNode) insn;
if (ldcNode.cst instanceof Type) {
Type type = (Type) ldcNode.cst;
while (type.getSort() == Type.ARRAY) {
type = type.getElementType();
}
if (!castClassMap.containsKey(type))
castClassMap.put(type, depth + 1);
}
}
}
}
use of org.objectweb.asm.tree.AbstractInsnNode 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();
}
use of org.objectweb.asm.tree.AbstractInsnNode in project Random-Things by lumien231.
the class ClassTransformer method patchEntityLivingBase.
private byte[] patchEntityLivingBase(byte[] basicClass) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
logger.log(Level.DEBUG, "Found EntityLivingBase Class: " + classNode.name);
MethodNode updatePotionEffects = null;
MethodNode travel = null;
for (MethodNode mn : classNode.methods) {
if (mn.name.equals(MCPNames.method("func_70679_bo"))) {
updatePotionEffects = mn;
} else if (mn.name.equals(MCPNames.method("func_191986_a"))) {
travel = mn;
}
}
if (updatePotionEffects != null) {
logger.log(Level.DEBUG, "- Found updatePotionEffects (1/2)");
for (int i = 0; i < updatePotionEffects.instructions.size(); i++) {
AbstractInsnNode ain = updatePotionEffects.instructions.get(i);
if (ain instanceof FieldInsnNode) {
FieldInsnNode fin = (FieldInsnNode) ain;
if (fin.name.equals(MCPNames.field("field_70180_af"))) {
AbstractInsnNode aload = updatePotionEffects.instructions.get(i - 1);
InsnList toInsert = new InsnList();
LabelNode l1 = new LabelNode(new Label());
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "shouldRenderPotionParticles", "(Lnet/minecraft/entity/EntityLivingBase;)Z", false));
toInsert.add(new JumpInsnNode(IFGT, l1));
toInsert.add(new InsnNode(RETURN));
toInsert.add(l1);
updatePotionEffects.instructions.insertBefore(aload, toInsert);
break;
}
}
}
}
if (travel != null) {
logger.log(Level.DEBUG, "- Found travel (2/2)");
for (int i = 0; i < travel.instructions.size(); i++) {
AbstractInsnNode ain = travel.instructions.get(i);
if (ain instanceof LdcInsnNode) {
LdcInsnNode lin = (LdcInsnNode) ain;
if (lin.cst.equals(new Float("0.91"))) {
AbstractInsnNode next = travel.instructions.get(i + 1);
if (next.getOpcode() == Opcodes.FMUL) {
InsnList toInsert = new InsnList();
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraft/entity/EntityLivingBase", MCPNames.field("field_70170_p"), "Lnet/minecraft/world/World;"));
toInsert.add(new VarInsnNode(ALOAD, 5));
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, "slipFix", "(FLnet/minecraft/entity/EntityLivingBase;Lnet/minecraft/block/Block;)F", false));
travel.instructions.insert(next, toInsert);
i += 6;
}
}
}
}
}
CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(writer);
return writer.toByteArray();
}
Aggregations