Search in sources :

Example 1 with TryCatchBlockNode

use of org.objectweb.asm.tree.TryCatchBlockNode in project jacoco by jacoco.

the class SynchronizedFilter method filter.

public void filter(final String className, final String superClassName, final MethodNode methodNode, final IFilterOutput output) {
    for (TryCatchBlockNode tryCatch : methodNode.tryCatchBlocks) {
        if (tryCatch.type != null) {
            continue;
        }
        if (tryCatch.start == tryCatch.handler) {
            continue;
        }
        final AbstractInsnNode to = new Matcher(tryCatch.handler).match();
        if (to == null) {
            continue;
        }
        output.ignore(tryCatch.handler, to);
    }
}
Also used : TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 2 with TryCatchBlockNode

use of org.objectweb.asm.tree.TryCatchBlockNode in project jacoco by jacoco.

the class StructuredLockingTest method assertStructuredLocking.

private void assertStructuredLocking(String owner, MethodNode mn) throws Exception {
    Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>(new BasicInterpreter()) {

        @Override
        protected Frame<BasicValue> newFrame(int nLocals, int nStack) {
            return new LockFrame(nLocals, nStack);
        }

        @Override
        protected Frame<BasicValue> newFrame(Frame<? extends BasicValue> src) {
            return new LockFrame(src);
        }
    };
    Frame<BasicValue>[] frames = analyzer.analyze(owner, mn);
    // Make sure no locks are left when method exits:
    for (int i = 0; i < frames.length; i++) {
        AbstractInsnNode insn = mn.instructions.get(i);
        switch(insn.getOpcode()) {
            case Opcodes.IRETURN:
            case Opcodes.LRETURN:
            case Opcodes.FRETURN:
            case Opcodes.DRETURN:
            case Opcodes.ARETURN:
            case Opcodes.RETURN:
                ((LockFrame) frames[i]).assertNoLock("Exit with lock");
                break;
            case Opcodes.ATHROW:
                List<TryCatchBlockNode> handlers = analyzer.getHandlers(i);
                if (handlers == null || handlers.isEmpty()) {
                    ((LockFrame) frames[i]).assertNoLock("Exit with lock");
                }
                break;
        }
    }
    // Only instructions protected by a catch-all handler can hold locks:
    for (int i = 0; i < frames.length; i++) {
        AbstractInsnNode insn = mn.instructions.get(i);
        if (insn.getOpcode() > 0) {
            boolean catchAll = false;
            List<TryCatchBlockNode> handlers = analyzer.getHandlers(i);
            if (handlers != null) {
                for (TryCatchBlockNode node : handlers) {
                    catchAll |= node.type == null;
                }
            }
            if (!catchAll) {
                ((LockFrame) frames[i]).assertNoLock("No handlers for insn with lock");
            }
        }
    }
}
Also used : TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) Frame(org.objectweb.asm.tree.analysis.Frame) BasicInterpreter(org.objectweb.asm.tree.analysis.BasicInterpreter) Analyzer(org.objectweb.asm.tree.analysis.Analyzer) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) BasicValue(org.objectweb.asm.tree.analysis.BasicValue)

Example 3 with TryCatchBlockNode

use of org.objectweb.asm.tree.TryCatchBlockNode in project openj9 by eclipse.

the class ClassFileCompare method compareTryCatchBlocks.

private void compareTryCatchBlocks(MethodNode method1, MethodNode method2) {
    if (nullCheck(method1.tryCatchBlocks, method2.tryCatchBlocks, "Try-catch blocks (" + method1.name + ")")) {
        return;
    }
    /*
		 * Copy and sort try-catch blocks by scope and type
		 */
    @SuppressWarnings("unchecked") List<TryCatchBlockNode> tryCatchBlocks1 = new ArrayList<>(method1.tryCatchBlocks);
    @SuppressWarnings("unchecked") List<TryCatchBlockNode> tryCatchBlocks2 = new ArrayList<>(method2.tryCatchBlocks);
    Collections.sort(tryCatchBlocks1, tryCatchBlockComparatorFor(method1));
    Collections.sort(tryCatchBlocks2, tryCatchBlockComparatorFor(method2));
    /*
		 * Compare try-catch blocks
		 */
    if (tryCatchBlocks1.size() != tryCatchBlocks2.size()) {
        reportDifference("Try-catch blocks (" + method1.name + ") differ: " + tryCatchBlocks1.size() + ", " + tryCatchBlocks2.size());
    } else {
        Iterator<TryCatchBlockNode> iter = tryCatchBlocks1.iterator();
        for (TryCatchBlockNode tryCatchBlock2 : tryCatchBlocks2) {
            TryCatchBlockNode tryCatchBlock1 = iter.next();
            compare(tryCatchBlock1.type, tryCatchBlock2.type, "Try-catch block (" + method1.name + ") type");
            compare(method1, tryCatchBlock1.start, method2, tryCatchBlock2.start, "Try-catch block scope (" + method1.name + ") starts");
            compare(method1, tryCatchBlock1.end, method2, tryCatchBlock2.end, "Try-catch block scope (" + method1.name + ") ends");
            compare(method1, tryCatchBlock1.handler, method2, tryCatchBlock2.handler, "Try-catch block scope (" + method1.name + ") handlers");
        }
    }
}
Also used : TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) ArrayList(java.util.ArrayList)

Example 4 with TryCatchBlockNode

use of org.objectweb.asm.tree.TryCatchBlockNode in project soot by Sable.

the class AsmMethodSource method getBody.

@Override
public Body getBody(SootMethod m, String phaseName) {
    if (!m.isConcrete())
        return null;
    JimpleBody jb = Jimple.v().newBody(m);
    /* initialize */
    int nrInsn = instructions.size();
    nextLocal = maxLocals;
    locals = new HashMap<Integer, Local>(maxLocals + (maxLocals / 2));
    labels = ArrayListMultimap.create(4, 1);
    units = new HashMap<AbstractInsnNode, Unit>(nrInsn);
    frames = new HashMap<AbstractInsnNode, StackFrame>(nrInsn);
    trapHandlers = ArrayListMultimap.create(tryCatchBlocks.size(), 1);
    body = jb;
    /* retrieve all trap handlers */
    for (TryCatchBlockNode tc : tryCatchBlocks) trapHandlers.put(tc.handler, Jimple.v().newStmtBox(null));
    /* convert instructions */
    try {
        convert();
    } catch (Throwable t) {
        throw new RuntimeException("Failed to convert " + m, t);
    }
    /* build body (add units, locals, traps, etc.) */
    emitLocals();
    emitTraps();
    emitUnits();
    /* clean up */
    locals = null;
    labels = null;
    units = null;
    stack = null;
    frames = null;
    body = null;
    // Make sure to inline patterns of the form to enable proper variable
    // splitting and type assignment:
    // a = new A();
    // goto l0;
    // l0:
    // b = (B) a;
    // return b;
    castAndReturnInliner.transform(jb);
    try {
        PackManager.v().getPack("jb").apply(jb);
    } catch (Throwable t) {
        throw new RuntimeException("Failed to apply jb to " + m, t);
    }
    return jb;
}
Also used : TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) Local(soot.Local) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) Unit(soot.Unit) JimpleBody(soot.jimple.JimpleBody)

Example 5 with TryCatchBlockNode

use of org.objectweb.asm.tree.TryCatchBlockNode in project cassandra by apache.

the class MonitorMethodTransformer method writeInnerTryCatchSynchronized.

// alternative approach (with writeOuterUnsynchronized)
@SuppressWarnings("unused")
void writeInnerTryCatchSynchronized() {
    access |= Opcodes.ACC_SYNCHRONIZED | Opcodes.ACC_SYNTHETIC;
    name = baseName + "$catch";
    Label start = new Label();
    Label normal = new Label();
    Label except = new Label();
    Label end = new Label();
    reset(start, end);
    // must load self or class onto stack, and return value (if any)
    maxStack = Math.max(maxLocalParams, returnCode == Opcodes.RETURN ? 1 : 2);
    ++maxLocals;
    tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(start), getLabelNode(normal), getLabelNode(except), null));
    instructions.add(getLabelNode(start));
    int invokeCode = loadParamsAndReturnInvokeCode();
    instructions.add(new MethodInsnNode(invokeCode, className, baseName + "$unsync", desc));
    instructions.add(getLabelNode(normal));
    invokePreMonitorExit();
    instructions.add(new InsnNode(returnCode()));
    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();
    instructions.add(new IntInsnNode(Opcodes.ALOAD, maxLocalParams));
    instructions.add(new InsnNode(Opcodes.ATHROW));
    instructions.add(getLabelNode(end));
    methodWriterSink.writeSyntheticMethod(MONITOR, this);
}
Also used : TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) FrameNode(org.objectweb.asm.tree.FrameNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) Label(org.objectweb.asm.Label) IntInsnNode(org.objectweb.asm.tree.IntInsnNode)

Aggregations

TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)27 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)11 InsnNode (org.objectweb.asm.tree.InsnNode)5 LabelNode (org.objectweb.asm.tree.LabelNode)5 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)5 MethodNode (org.objectweb.asm.tree.MethodNode)5 Label (org.objectweb.asm.Label)4 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)4 Frame (org.objectweb.asm.tree.analysis.Frame)4 ArrayList (java.util.ArrayList)3 Type (org.objectweb.asm.Type)3 FrameNode (org.objectweb.asm.tree.FrameNode)3 InsnList (org.objectweb.asm.tree.InsnList)3 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)3 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Analyzer (org.objectweb.asm.tree.analysis.Analyzer)2 Textifier (org.objectweb.asm.util.Textifier)2 TraceMethodVisitor (org.objectweb.asm.util.TraceMethodVisitor)2