use of org.objectweb.asm.tree.TypeInsnNode in project evosuite by EvoSuite.
the class BooleanArrayIndexTransformer method transformTypeInsnNode.
/* (non-Javadoc)
* @see org.evosuite.instrumentation.MethodNodeTransformer#transformTypeInsnNode(org.objectweb.asm.tree.MethodNode, org.objectweb.asm.tree.TypeInsnNode)
*/
@Override
protected AbstractInsnNode transformTypeInsnNode(MethodNode mn, TypeInsnNode typeNode) {
if (frames == null)
return typeNode;
if (typeNode.getOpcode() == Opcodes.CHECKCAST) {
Frame current = frames[mn.instructions.indexOf(typeNode)];
int size = current.getStackSize();
if (current.getStack(size - 1) == BooleanArrayInterpreter.INT_ARRAY) {
BooleanTestabilityTransformation.logger.info("Array is of boolean type, changing CHECKCAST to [I");
TypeInsnNode replacement = new TypeInsnNode(Opcodes.CHECKCAST, "[I");
mn.instructions.insertBefore(typeNode, replacement);
mn.instructions.remove(typeNode);
return replacement;
}
}
return typeNode;
}
use of org.objectweb.asm.tree.TypeInsnNode in project evosuite by EvoSuite.
the class BooleanCallsTransformer method transformMethodInsnNode.
/* (non-Javadoc)
* @see org.evosuite.instrumentation.MethodNodeTransformer#transformMethodInsnNode(org.objectweb.asm.tree.MethodNode, org.objectweb.asm.tree.MethodInsnNode)
*/
@Override
protected AbstractInsnNode transformMethodInsnNode(MethodNode mn, MethodInsnNode methodNode) {
if (methodNode.owner.equals(Type.getInternalName(BooleanHelper.class)))
return methodNode;
methodNode.desc = this.booleanTestabilityTransformation.transformMethodDescriptor(methodNode.owner, methodNode.name, methodNode.desc);
methodNode.name = DescriptorMapping.getInstance().getMethodName(methodNode.owner, methodNode.name, methodNode.desc);
if (DescriptorMapping.getInstance().isBooleanMethod(methodNode.desc)) {
BooleanTestabilityTransformation.logger.info("Method needs value transformation: " + methodNode.name);
if (DescriptorMapping.getInstance().hasBooleanParameters(methodNode.desc)) {
BooleanTestabilityTransformation.logger.info("Method needs parameter transformation: " + methodNode.name);
TransformationStatistics.transformBackToBooleanParameter();
int firstBooleanParameterIndex = -1;
Type[] types = Type.getArgumentTypes(methodNode.desc);
for (int i = 0; i < types.length; i++) {
if (types[i].getDescriptor().equals("Z")) {
if (firstBooleanParameterIndex == -1) {
firstBooleanParameterIndex = i;
break;
}
}
}
if (firstBooleanParameterIndex != -1) {
int numOfPushs = types.length - 1 - firstBooleanParameterIndex;
if (numOfPushs == 0) {
if (!(methodNode.getPrevious().getOpcode() == Opcodes.ICONST_1 || methodNode.getPrevious().getOpcode() == Opcodes.ICONST_0)) {
// the boolean parameter is the last parameter
MethodInsnNode booleanHelperInvoke = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "intToBoolean", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE }));
mn.instructions.insertBefore(methodNode, booleanHelperInvoke);
}
} else {
InsnList insnlist = new InsnList();
for (int i = 0; i < numOfPushs; i++) {
MethodInsnNode booleanHelperPushParameter;
if (types[types.length - 1 - i] == Type.BOOLEAN_TYPE || types[types.length - 1 - i] == Type.CHAR_TYPE || types[types.length - 1 - i] == Type.BYTE_TYPE || types[types.length - 1 - i] == Type.SHORT_TYPE || types[types.length - 1 - i] == Type.INT_TYPE || types[types.length - 1 - i] == Type.FLOAT_TYPE || types[types.length - 1 - i] == Type.LONG_TYPE || types[types.length - 1 - i] == Type.DOUBLE_TYPE) {
if (types[types.length - 1 - i] == Type.BOOLEAN_TYPE) {
booleanHelperPushParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushParameter", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE }));
} else {
booleanHelperPushParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushParameter", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { types[types.length - 1 - i] }));
}
} else {
booleanHelperPushParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushParameter", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(Object.class) }));
}
insnlist.add(booleanHelperPushParameter);
}
for (int i = firstBooleanParameterIndex; i < types.length; i++) {
if (i == firstBooleanParameterIndex) {
MethodInsnNode booleanHelperInvoke = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "intToBoolean", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE }));
insnlist.add(booleanHelperInvoke);
} else {
MethodInsnNode booleanHelperPopParameter;
boolean objectNeedCast = false;
if (types[i] == Type.BOOLEAN_TYPE) {
booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterBooleanFromInt", Type.getMethodDescriptor(types[i], new Type[] {}));
} else if (types[i] == Type.CHAR_TYPE) {
booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterChar", Type.getMethodDescriptor(types[i], new Type[] {}));
} else if (types[i] == Type.BYTE_TYPE) {
booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterByte", Type.getMethodDescriptor(types[i], new Type[] {}));
} else if (types[i] == Type.SHORT_TYPE) {
booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterShort", Type.getMethodDescriptor(types[i], new Type[] {}));
} else if (types[i] == Type.INT_TYPE) {
booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterInt", Type.getMethodDescriptor(types[i], new Type[] {}));
} else if (types[i] == Type.FLOAT_TYPE) {
booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterFloat", Type.getMethodDescriptor(types[i], new Type[] {}));
} else if (types[i] == Type.LONG_TYPE) {
booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterLong", Type.getMethodDescriptor(types[i], new Type[] {}));
} else if (types[i] == Type.DOUBLE_TYPE) {
booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterDouble", Type.getMethodDescriptor(types[i], new Type[] {}));
} else {
objectNeedCast = true;
booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterObject", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] {}));
}
insnlist.add(booleanHelperPopParameter);
if (objectNeedCast) {
TypeInsnNode tin = new TypeInsnNode(Opcodes.CHECKCAST, types[i].getInternalName());
insnlist.add(tin);
}
}
}
mn.instructions.insertBefore(methodNode, insnlist);
}
}
}
if (Type.getReturnType(methodNode.desc).equals(Type.BOOLEAN_TYPE)) {
BooleanTestabilityTransformation.logger.info("Method needs return transformation: " + methodNode.name);
TransformationStatistics.transformBackToBooleanParameter();
MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "booleanToInt", Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.BOOLEAN_TYPE }));
mn.instructions.insert(methodNode, n);
return n;
}
} else {
BooleanTestabilityTransformation.logger.info("Method needs no transformation: " + methodNode.name);
}
// and insert a conversion function there
return methodNode;
}
use of org.objectweb.asm.tree.TypeInsnNode 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.TypeInsnNode in project Random-Things by lumien231.
the class ClassTransformer method patchVertexLighterFlat.
private byte[] patchVertexLighterFlat(byte[] basicClass) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
logger.log(Level.DEBUG, "Found VertexLighterFlat Class: " + classNode.name);
MethodNode processQuad = null;
for (MethodNode mn : classNode.methods) {
if (mn.name.equals("processQuad")) {
processQuad = mn;
break;
}
}
if (processQuad != null) {
logger.log(Level.DEBUG, " - Found processQuad");
InsnList resetList = new InsnList();
resetList.add(new VarInsnNode(ALOAD, 0));
resetList.add(new InsnNode(ICONST_0));
resetList.add(new FieldInsnNode(PUTFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
processQuad.instructions.insert(resetList);
AbstractInsnNode tintTarget = null;
int lightMapTarget = 0;
AbstractInsnNode updateColorTarget = null;
LabelNode firstLabel = null;
LabelNode lastLabel = null;
for (int i = 0; i < processQuad.instructions.size(); i++) {
AbstractInsnNode ain = processQuad.instructions.get(i);
if (ain instanceof VarInsnNode) {
VarInsnNode vin = (VarInsnNode) ain;
if (vin.var == 5 && (processQuad.instructions.get(i - 1) instanceof MethodInsnNode)) {
tintTarget = vin;
}
} else if (ain instanceof LabelNode) {
LabelNode ln = (LabelNode) ain;
if (firstLabel == null) {
firstLabel = ln;
}
lastLabel = ln;
} else if (ain instanceof MethodInsnNode) {
MethodInsnNode min = (MethodInsnNode) ain;
if (min.name.equals(MCPNames.method("func_177369_e"))) {
lightMapTarget = i;
} else if (min.name.equals("updateColor")) {
updateColorTarget = min;
}
}
}
FieldNode fullBrightField = new FieldNode(ACC_PRIVATE, "rtFullBright", "Z", null, false);
classNode.fields.add(fullBrightField);
if (lightMapTarget != 0) {
logger.log(Level.DEBUG, " - Found patch target (lightmap) (1/4)");
for (int i = lightMapTarget; i < processQuad.instructions.size(); i++) {
AbstractInsnNode ain = processQuad.instructions.get(i);
if (ain instanceof InsnNode) {
InsnNode in = (InsnNode) ain;
if (in.getOpcode() == AALOAD) {
logger.log(Level.DEBUG, " - Found lightmap array (2/4)");
LabelNode l0 = new LabelNode(new Label());
InsnList toInsert = new InsnList();
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
toInsert.add(new JumpInsnNode(IFEQ, l0));
toInsert.add(new InsnNode(POP));
toInsert.add(new FieldInsnNode(GETSTATIC, "lumien/randomthings/lib/Constants", "FULLBRIGHT_OVERRIDE", "[F"));
toInsert.add(l0);
processQuad.instructions.insert(in, toInsert);
break;
}
}
}
}
if (tintTarget != null) {
logger.log(Level.DEBUG, " - Found tintTarget (3/4");
LabelNode l0 = new LabelNode(new Label());
LabelNode l1 = new LabelNode(new Label());
InsnList toInsert = new InsnList();
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "blockInfo", "Lnet/minecraftforge/client/model/pipeline/BlockInfo;"));
toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraftforge/client/model/pipeline/BlockInfo", "getState", "()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 TypeInsnNode(INSTANCEOF, "lumien/randomthings/lib/ILuminousBlock"));
toInsert.add(new JumpInsnNode(IFEQ, l0));
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "blockInfo", "Lnet/minecraftforge/client/model/pipeline/BlockInfo;"));
toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraftforge/client/model/pipeline/BlockInfo", "getState", "()Lnet/minecraft/block/state/IBlockState;", false));
toInsert.add(new InsnNode(DUP));
toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "net/minecraft/block/state/IBlockState", MCPNames.method("func_177230_c"), "()Lnet/minecraft/block/Block;", true));
toInsert.add(new InsnNode(SWAP));
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "tint", "I"));
toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "lumien/randomthings/lib/ILuminousBlock", "shouldGlow", "(Lnet/minecraft/block/state/IBlockState;I)Z", true));
toInsert.add(new JumpInsnNode(IFEQ, l0));
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new InsnNode(ICONST_0));
toInsert.add(new FieldInsnNode(PUTFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "diffuse", "Z"));
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new InsnNode(ICONST_1));
toInsert.add(new FieldInsnNode(PUTFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
toInsert.add(l0);
processQuad.instructions.insertBefore(tintTarget, toInsert);
}
if (updateColorTarget != null) {
logger.log(Level.DEBUG, " - Found updateColor target (tint) (4/4)");
LabelNode l0 = new LabelNode(new Label());
LabelNode l1 = new LabelNode(new Label());
InsnList toInsert = new InsnList();
toInsert.add(new VarInsnNode(ALOAD, 0));
toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
toInsert.add(new JumpInsnNode(IFEQ, l1));
toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "updateColor", "([F[FFFFFI)V", false));
toInsert.add(new InsnNode(POP));
toInsert.add(new JumpInsnNode(GOTO, l0));
toInsert.add(l1);
processQuad.instructions.insertBefore(updateColorTarget, toInsert);
processQuad.instructions.insert(updateColorTarget, l0);
}
}
CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(writer);
try {
byte[] result = writer.toByteArray();
return result;
} catch (Exception e) {
e.printStackTrace();
return basicClass;
}
}
use of org.objectweb.asm.tree.TypeInsnNode in project phosphor by gmu-swe.
the class InstMethodSinkInterpreter method _unaryOperation.
private BasicValue _unaryOperation(AbstractInsnNode insn, BasicValue value) throws AnalyzerException {
switch(insn.getOpcode()) {
case INEG:
case IINC:
case L2I:
case F2I:
case D2I:
case I2B:
case I2C:
case I2S:
SinkableArrayValue ret = new SinkableArrayValue(Type.INT_TYPE);
ret.addDep((SinkableArrayValue) value);
return ret;
case FNEG:
case I2F:
case L2F:
case D2F:
ret = new SinkableArrayValue(Type.FLOAT_TYPE);
ret.addDep((SinkableArrayValue) value);
return ret;
case LNEG:
case I2L:
case F2L:
case D2L:
ret = new SinkableArrayValue(Type.LONG_TYPE);
ret.addDep((SinkableArrayValue) value);
return ret;
case DNEG:
case I2D:
case L2D:
case F2D:
ret = new SinkableArrayValue(Type.DOUBLE_TYPE);
ret.addDep((SinkableArrayValue) value);
return ret;
case IFEQ:
case IFNE:
case IFLT:
case IFGE:
case IFGT:
case IFLE:
case TABLESWITCH:
case LOOKUPSWITCH:
return null;
case IRETURN:
case LRETURN:
case FRETURN:
case DRETURN:
case ARETURN:
case PUTSTATIC:
return null;
case GETFIELD:
return newValue(Type.getType(((FieldInsnNode) insn).desc));
case NEWARRAY:
ret = null;
switch(((IntInsnNode) insn).operand) {
case T_BOOLEAN:
ret = (SinkableArrayValue) newValue(Type.getType("[Z"));
break;
case T_CHAR:
ret = (SinkableArrayValue) newValue(Type.getType("[C"));
break;
case T_BYTE:
ret = (SinkableArrayValue) newValue(Type.getType("[B"));
break;
case T_SHORT:
ret = (SinkableArrayValue) newValue(Type.getType("[S"));
break;
case T_INT:
ret = (SinkableArrayValue) newValue(Type.getType("[I"));
break;
case T_FLOAT:
ret = (SinkableArrayValue) newValue(Type.getType("[F"));
break;
case T_DOUBLE:
ret = (SinkableArrayValue) newValue(Type.getType("[D"));
break;
case T_LONG:
ret = (SinkableArrayValue) newValue(Type.getType("[J"));
break;
default:
throw new AnalyzerException(insn, "Invalid array type");
}
if (Configuration.ARRAY_LENGTH_TRACKING)
ret.addDep((SinkableArrayValue) value);
ret.isNewArray = true;
return ret;
case ANEWARRAY:
String desc = ((TypeInsnNode) insn).desc;
return newValue(Type.getType("[" + Type.getObjectType(desc)));
case ARRAYLENGTH:
ret = (SinkableArrayValue) newValue(Type.INT_TYPE);
if (value instanceof SinkableArrayValue && value.getType() != null && TaintUtils.isPrimitiveArrayType(value.getType()))
ret.addDep((SinkableArrayValue) value);
return ret;
case ATHROW:
return null;
case CHECKCAST:
desc = ((TypeInsnNode) insn).desc;
BasicValue _ret = newValue(Type.getObjectType(desc));
if (value instanceof SinkableArrayValue) {
if ((_ret instanceof SinkableArrayValue))
((SinkableArrayValue) _ret).addDep((SinkableArrayValue) value);
}
return _ret;
case INSTANCEOF:
return newValue(Type.INT_TYPE);
case MONITORENTER:
case MONITOREXIT:
case IFNULL:
case IFNONNULL:
return null;
default:
throw new Error("Internal error.");
}
}
Aggregations