use of org.objectweb.asm.tree.TryCatchBlockNode in project evosuite by EvoSuite.
the class LCSAJsInstrumentation method analyze.
/* (non-Javadoc)
* @see org.evosuite.cfg.MethodInstrumentation#analyze(org.objectweb.asm.tree.MethodNode, org.jgrapht.Graph, java.lang.String, java.lang.String)
*/
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
// using external lib
@Override
public void analyze(ClassLoader classLoader, MethodNode mn, String className, String methodName, int access) {
Queue<LCSAJ> lcsaj_queue = new LinkedList<LCSAJ>();
HashSet<Integer> targets_reached = new HashSet<Integer>();
AbstractInsnNode start = mn.instructions.getFirst();
int startID = 0;
// TODO: This should replace the hack below
if (methodName.startsWith("<init>")) {
Iterator<AbstractInsnNode> j = mn.instructions.iterator();
boolean constructorInvoked = false;
while (j.hasNext()) {
AbstractInsnNode in = j.next();
startID++;
if (!constructorInvoked) {
if (in.getOpcode() == Opcodes.INVOKESPECIAL) {
MethodInsnNode cn = (MethodInsnNode) in;
Collection<String> superClasses = DependencyAnalysis.getInheritanceTree().getSuperclasses(className);
superClasses.add(className);
String classNameWithDots = ResourceList.getClassNameFromResourcePath(cn.owner);
if (superClasses.contains(classNameWithDots)) {
constructorInvoked = true;
break;
}
} else {
continue;
}
}
}
}
/*
if (methodName.startsWith("<init>")) {
if (mn.instructions.size() >= 4) {
start = mn.instructions.get(4);
startID = 4;
}
}
*/
LCSAJ a = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, startID, start));
lcsaj_queue.add(a);
targets_reached.add(0);
ArrayList<TryCatchBlockNode> tc_blocks = (ArrayList<TryCatchBlockNode>) mn.tryCatchBlocks;
for (TryCatchBlockNode t : tc_blocks) {
LCSAJ b = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, mn.instructions.indexOf(t.handler), t.handler));
lcsaj_queue.add(b);
}
while (!lcsaj_queue.isEmpty()) {
LCSAJ currentLCSAJ = lcsaj_queue.poll();
int position = mn.instructions.indexOf(currentLCSAJ.getLastNodeAccessed());
// go to next bytecode instruction
position++;
if (position >= mn.instructions.size()) {
// New LCSAJ for current + return
LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
continue;
}
AbstractInsnNode next = mn.instructions.get(position);
currentLCSAJ.lookupInstruction(position, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, position, next));
if (next instanceof JumpInsnNode) {
JumpInsnNode jump = (JumpInsnNode) next;
// New LCSAJ for current + jump to target
LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
LabelNode target = jump.label;
int targetPosition = mn.instructions.indexOf(target);
if (jump.getOpcode() != Opcodes.GOTO) {
LCSAJ copy = new LCSAJ(currentLCSAJ);
lcsaj_queue.add(copy);
}
if (!targets_reached.contains(targetPosition)) {
LCSAJ c = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, targetPosition, target));
lcsaj_queue.add(c);
targets_reached.add(targetPosition);
}
} else if (next instanceof TableSwitchInsnNode) {
TableSwitchInsnNode tswitch = (TableSwitchInsnNode) next;
List<LabelNode> allTargets = tswitch.labels;
for (LabelNode target : allTargets) {
int targetPosition = mn.instructions.indexOf(target);
if (!targets_reached.contains(targetPosition)) {
LCSAJ b = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, targetPosition, target));
lcsaj_queue.add(b);
targets_reached.add(targetPosition);
}
}
} else if (next instanceof InsnNode) {
InsnNode insn = (InsnNode) next;
// New LCSAJ for current + throw / return
if (insn.getOpcode() == Opcodes.ATHROW || insn.getOpcode() == Opcodes.RETURN || insn.getOpcode() == Opcodes.ARETURN || insn.getOpcode() == Opcodes.IRETURN || insn.getOpcode() == Opcodes.DRETURN || insn.getOpcode() == Opcodes.LRETURN || insn.getOpcode() == Opcodes.FRETURN) {
LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
} else
lcsaj_queue.add(currentLCSAJ);
} else
lcsaj_queue.add(currentLCSAJ);
}
if (Properties.STRATEGY != Strategy.EVOSUITE)
addInstrumentation(classLoader, mn, className, methodName);
// if (Properties.WRITE_CFG)
// for (LCSAJ l : LCSAJPool.getLCSAJs(className, methodName)) {
// LCSAJGraph graph = new LCSAJGraph(l, false);
// String graphDestination = "evosuite-graphs/LCSAJGraphs/" + className
// + "/" + methodName;
// File dir = new File(graphDestination);
// if (dir.mkdirs())
// graph.generate(new File(graphDestination + "/LCSAJGraph no: "
// + l.getID() + ".dot"));
// else if (dir.exists())
// graph.generate(new File(graphDestination + "/LCSAJGraph no: "
// + l.getID() + ".dot"));
// }
}
use of org.objectweb.asm.tree.TryCatchBlockNode in project cassandra by apache.
the class MonitorMethodTransformer method writeTryCatchMonitorEnterExit.
void writeTryCatchMonitorEnterExit() {
access |= Opcodes.ACC_SYNTHETIC;
name = baseName;
Label start = new Label();
Label inmonitor = new Label();
Label normal = new Label();
// normal
Label except = new Label();
// normal return failed
Label normalRetExcept = new Label();
// exceptional return success
Label exceptRetNormal = new Label();
// exceptional return failed
Label exceptRetExcept = new Label();
Label end = new Label();
reset(start, end);
// add a local variable slot to save any exceptions into (at maxLocalParams position)
++maxLocals;
// must load self or class onto stack, and return value (if any)
maxStack = Math.max(maxLocalParams, returnCode == Opcodes.RETURN ? 2 : 3);
tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(inmonitor), getLabelNode(normal), getLabelNode(except), null));
tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(normal), getLabelNode(normalRetExcept), getLabelNode(normalRetExcept), null));
tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(except), getLabelNode(exceptRetNormal), getLabelNode(exceptRetExcept), null));
// preMonitorEnter
// monitorenter
instructions.add(getLabelNode(start));
invokePreMonitorEnter();
invokeMonitor(Opcodes.MONITORENTER);
{
// try1 { val = original();
instructions.add(getLabelNode(inmonitor));
int invokeCode = loadParamsAndReturnInvokeCode();
instructions.add(new MethodInsnNode(invokeCode, className, baseName + "$unsync", desc));
{
// try2 { preMonitorExit(); monitorexit; return val; }
instructions.add(getLabelNode(normal));
invokePreMonitorExit();
invokeMonitor(Opcodes.MONITOREXIT);
// success
instructions.add(new InsnNode(returnCode()));
// }
// catch2 { monitorexit; throw }
instructions.add(getLabelNode(normalRetExcept));
instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
instructions.add(new IntInsnNode(Opcodes.ASTORE, maxLocalParams));
pushRef();
invokeMonitor(Opcodes.MONITOREXIT);
instructions.add(new IntInsnNode(Opcodes.ALOAD, maxLocalParams));
instructions.add(new InsnNode(Opcodes.ATHROW));
// }
}
// catch1 { try3 { preMonitorExit; monitorexit; throw
instructions.add(getLabelNode(except));
instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
instructions.add(new IntInsnNode(Opcodes.ASTORE, maxLocalParams));
invokePreMonitorExit();
invokeMonitor(Opcodes.MONITOREXIT);
instructions.add(new IntInsnNode(Opcodes.ALOAD, maxLocalParams));
instructions.add(getLabelNode(exceptRetNormal));
instructions.add(new InsnNode(Opcodes.ATHROW));
instructions.add(getLabelNode(exceptRetExcept));
instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
instructions.add(new IntInsnNode(Opcodes.ASTORE, maxLocalParams));
pushRef();
invokeMonitor(Opcodes.MONITOREXIT);
instructions.add(new IntInsnNode(Opcodes.ALOAD, maxLocalParams));
instructions.add(new InsnNode(Opcodes.ATHROW));
}
instructions.add(getLabelNode(end));
methodWriterSink.writeSyntheticMethod(MONITOR, this);
}
use of org.objectweb.asm.tree.TryCatchBlockNode in project maple-ir by LLVM-but-worse.
the class ClassRenamerPass method accept.
/*private String getClassName(String name) {
int i = name.lastIndexOf('/');
if(i == -1) {
return name;
} else {
return name.substring(i + 1, name.length());
}
}*/
@Override
public int accept(AnalysisContext cxt, IPass prev, List<IPass> completed) {
ApplicationClassSource source = cxt.getApplication();
Collection<ClassNode> classes = CollectionUtils.collate(source.iterator());
// int min = RenamingUtil.computeMinimum(classes.size());
int n = RenamingUtil.numeric("aaa");
int step = 27;
for (ClassNode cn : classes) {
String className = RenamingUtil.getClassName(cn.name);
if (!heuristic.shouldRename(className, cn.access)) {
System.out.println("Heuristic bypass " + cn.name);
}
String newName = heuristic.shouldRename(className, cn.access) ? RenamingUtil.createName(n) : className;
String s = RenamingUtil.getPackage(cn.name) + newName;
n += step;
remapping.put(cn.name, s);
// System.out.println(cn.name + " -> " + s);
cn.name = s;
}
for (ClassNode cn : classes) {
cn.superName = remapping.getOrDefault(cn.superName, cn.superName);
{
List<String> ifaces = new ArrayList<>();
for (int i = 0; i < cn.interfaces.size(); i++) {
String s = cn.interfaces.get(i);
ifaces.add(remapping.getOrDefault(s, s));
}
cn.interfaces = ifaces;
}
unsupported(cn.signature);
// unsupported(cn.sourceFile);
// unsupported(cn.sourceDebug);
cn.outerClass = remapping.getOrDefault(cn.outerClass, cn.outerClass);
// unsupported(cn.outerMethod);
// unsupported(cn.outerMethodDesc);
unsupported(cn.visibleAnnotations);
unsupported(cn.invisibleAnnotations);
unsupported(cn.visibleTypeAnnotations);
unsupported(cn.invisibleTypeAnnotations);
unsupported(cn.attrs);
unsupported(cn.innerClasses);
for (FieldNode f : cn.fields) {
unsupported(cn.signature);
{
Type type = Type.getType(f.desc);
String newType = resolveType(type, remapping);
if (newType != null) {
f.desc = newType;
}
}
unsupported(f.visibleAnnotations);
unsupported(f.invisibleAnnotations);
unsupported(f.visibleTypeAnnotations);
unsupported(f.invisibleTypeAnnotations);
unsupported(f.attrs);
}
for (MethodNode m : cn.methods) {
m.desc = resolveMethod(m.desc, remapping);
unsupported(m.signature);
{
List<String> exceptions = new ArrayList<>();
for (int i = 0; i < m.exceptions.size(); i++) {
String s = m.exceptions.get(i);
exceptions.add(remapping.getOrDefault(s, s));
}
m.exceptions = exceptions;
}
unsupported(m.parameters);
unsupported(m.visibleAnnotations);
unsupported(m.invisibleAnnotations);
unsupported(m.visibleTypeAnnotations);
unsupported(m.invisibleTypeAnnotations);
unsupported(m.attrs);
unsupported(m.annotationDefault);
unsupported(m.visibleParameterAnnotations);
unsupported(m.invisibleParameterAnnotations);
for (TryCatchBlockNode tcbn : m.tryCatchBlocks) {
tcbn.type = remapping.getOrDefault(tcbn.type, tcbn.type);
}
ControlFlowGraph cfg = cxt.getIRCache().getFor(m);
for (ExceptionRange<BasicBlock> er : cfg.getRanges()) {
Set<Type> newTypeSet = new HashSet<>();
for (Type t : er.getTypes()) {
// FIXME:
String s = t.getInternalName();
if (remapping.containsKey(s)) {
newTypeSet.add(Type.getType("L" + remapping.get(s) + ";"));
} else {
newTypeSet.add(t);
}
}
er.setTypes(newTypeSet);
}
if (m.localVariables != null) {
m.localVariables.clear();
for (LocalVariableNode lvn : m.localVariables) {
String newDesc = resolveType(Type.getType(lvn.desc), remapping);
if (newDesc != null) {
lvn.desc = newDesc;
}
unsupported(lvn.signature);
}
}
unsupported(m.visibleLocalVariableAnnotations);
unsupported(m.invisibleLocalVariableAnnotations);
for (BasicBlock b : cfg.vertices()) {
for (Stmt stmt : b) {
if (stmt.getOpcode() == Opcode.FIELD_STORE) {
FieldStoreStmt fs = (FieldStoreStmt) stmt;
String owner = fs.getOwner();
fs.setOwner(remapping.getOrDefault(owner, owner));
{
Type type = Type.getType(fs.getDesc());
String newType = resolveType(type, remapping);
if (newType != null) {
fs.setDesc(newType);
}
}
} else if (stmt.getOpcode() == Opcode.RETURN) {
ReturnStmt ret = (ReturnStmt) stmt;
String newType = resolveType(ret.getType(), remapping);
if (newType != null) {
ret.setType(Type.getType(newType));
}
} else if (stmt instanceof AbstractCopyStmt) {
AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
VarExpr v = copy.getVariable();
String newType = resolveType(v.getType(), remapping);
if (newType != null) {
v.setType(Type.getType(newType));
}
}
for (Expr e : stmt.enumerateOnlyChildren()) {
if (e.getOpcode() == Opcode.CAST) {
CastExpr cast = (CastExpr) e;
String newType = resolveType(cast.getType(), remapping);
if (newType != null) {
cast.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.CATCH) {
CaughtExceptionExpr caught = (CaughtExceptionExpr) e;
String newType = resolveType(caught.getType(), remapping);
if (newType != null) {
caught.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.DYNAMIC_INVOKE) {
throw new UnsupportedOperationException();
} else if (e.getOpcode() == Opcode.INVOKE) {
InvocationExpr invoke = (InvocationExpr) e;
invoke.setOwner(remapping.getOrDefault(invoke.getOwner(), invoke.getOwner()));
invoke.setDesc(resolveMethod(invoke.getDesc(), remapping));
} else if (e.getOpcode() == Opcode.FIELD_LOAD) {
FieldLoadExpr fl = (FieldLoadExpr) e;
fl.setOwner(remapping.getOrDefault(fl.getOwner(), fl.getOwner()));
String newType = resolveType(fl.getType(), remapping);
if (newType != null) {
fl.setDesc(newType);
}
} else if (e.getOpcode() == Opcode.INIT_OBJ) {
InitialisedObjectExpr init = (InitialisedObjectExpr) e;
init.setOwner(remapping.getOrDefault(init.getOwner(), init.getOwner()));
init.setDesc(resolveMethod(init.getDesc(), remapping));
} else if (e.getOpcode() == Opcode.INSTANCEOF) {
InstanceofExpr inst = (InstanceofExpr) e;
String newType = resolveType(inst.getCheckType(), remapping);
if (newType != null) {
inst.setCheckType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.NEW_ARRAY) {
NewArrayExpr na = (NewArrayExpr) e;
String newType = resolveType(na.getType(), remapping);
if (newType != null) {
na.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.ALLOC_OBJ) {
AllocObjectExpr uninit = (AllocObjectExpr) e;
String newType = resolveType(uninit.getType(), remapping);
if (newType != null) {
uninit.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.LOCAL_LOAD) {
VarExpr v = (VarExpr) e;
String newType = resolveType(v.getType(), remapping);
if (newType != null) {
v.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.CONST_LOAD) {
ConstantExpr c = (ConstantExpr) e;
Object cst = c.getConstant();
if (cst instanceof Type) {
Type t = (Type) cst;
if (t.getSort() == Type.OBJECT) {
String newType = resolveType(t, remapping);
if (newType != null) {
c.setConstant(Type.getType(newType));
}
} else {
throw new UnsupportedOperationException(String.format("Unsupported ctype %s (%d)", t, t.getSort()));
}
}
}
}
}
}
}
}
source.rebuildTable();
return classes.size();
}
use of org.objectweb.asm.tree.TryCatchBlockNode in project maple-ir by LLVM-but-worse.
the class ControlFlowGraphDumper method verifyRanges.
private void verifyRanges() {
for (TryCatchBlockNode tc : m.tryCatchBlocks) {
int start = -1, end = -1, handler = -1;
for (int i = 0; i < m.instructions.size(); i++) {
AbstractInsnNode ain = m.instructions.get(i);
if (!(ain instanceof LabelNode))
continue;
Label l = ((LabelNode) ain).getLabel();
if (l == tc.start.getLabel())
start = i;
if (l == tc.end.getLabel()) {
if (start == -1)
throw new IllegalStateException("Try block end before start " + m);
end = i;
}
if (l == tc.handler.getLabel()) {
handler = i;
}
}
if (start == -1 || end == -1 || handler == -1)
throw new IllegalStateException("Try/catch endpoints missing: " + start + " " + end + " " + handler + m);
}
}
use of org.objectweb.asm.tree.TryCatchBlockNode in project dex2jar by pxb1988.
the class TestUtils method printAnalyzerResult.
static void printAnalyzerResult(MethodNode method, Analyzer a, final PrintWriter pw) throws IllegalArgumentException, IllegalAccessException {
Frame[] frames = a.getFrames();
Textifier t = new Textifier();
TraceMethodVisitor mv = new TraceMethodVisitor(t);
String format = "%05d %-" + (method.maxStack + method.maxLocals + 6) + "s|%s";
for (int j = 0; j < method.instructions.size(); ++j) {
method.instructions.get(j).accept(mv);
StringBuffer s = new StringBuffer();
Frame f = frames[j];
if (f == null) {
s.append('?');
} else {
for (int k = 0; k < f.getLocals(); ++k) {
s.append(getShortName(f.getLocal(k).toString()));
}
s.append(" : ");
for (int k = 0; k < f.getStackSize(); ++k) {
s.append(getShortName(f.getStack(k).toString()));
}
}
// mv.text.get(j));
pw.printf(format, j, s, buf.get(t));
}
for (int j = 0; j < method.tryCatchBlocks.size(); ++j) {
((TryCatchBlockNode) method.tryCatchBlocks.get(j)).accept(mv);
pw.print(" " + buf.get(t));
}
pw.println();
pw.flush();
}
Aggregations