Search in sources :

Example 21 with FrameNode

use of org.objectweb.asm.tree.FrameNode in project bytecode-viewer by Konloch.

the class InstructionSearcher method search.

public boolean search() {
    for (AbstractInsnNode ain : insns.toArray()) {
        if (ain instanceof LineNumberNode || ain instanceof FrameNode)
            continue;
        if (pattern.accept(ain)) {
            matches.add(pattern.getLastMatch());
            pattern.resetMatch();
        }
    }
    return size() != 0;
}
Also used : FrameNode(org.objectweb.asm.tree.FrameNode) LineNumberNode(org.objectweb.asm.tree.LineNumberNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 22 with FrameNode

use of org.objectweb.asm.tree.FrameNode in project bytecode-viewer by Konloch.

the class StackFramesRemover method execute.

@Override
public void execute(List<ClassNode> classNodeList) {
    AtomicInteger counter = new AtomicInteger();
    PluginConsole frame = new PluginConsole("StackFrames Remover");
    for (ClassNode cn : classNodeList) {
        for (MethodNode mn : cn.methods) {
            for (AbstractInsnNode insn : mn.instructions.toArray()) {
                if (insn instanceof FrameNode) {
                    mn.instructions.remove(insn);
                    counter.incrementAndGet();
                }
            }
        }
    }
    frame.appendText(String.format("Removed %s stackframes.", counter));
    frame.setVisible(true);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FrameNode(org.objectweb.asm.tree.FrameNode) MethodNode(org.objectweb.asm.tree.MethodNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PluginConsole(the.bytecode.club.bytecodeviewer.api.PluginConsole) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 23 with FrameNode

use of org.objectweb.asm.tree.FrameNode in project evosuite by EvoSuite.

the class Instrumenter method wrapMethod.

/**
 * 	public int myMethod(int i)
 *		{
 *			try
 *			{
 *				return _sw_prototype_original_myMethod(i)
 *			}
 *			finally
 *			{
 *				Capturer.enable();
 *			}
 *		}
 *
 * @param classNode
 * @param className
 * @param methodNode
 */
@SuppressWarnings("unchecked")
private MethodNode wrapMethod(final ClassNode classNode, final String className, final MethodNode methodNode) {
    methodNode.maxStack += 4;
    // create wrapper for original method
    final MethodNode wrappingMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));
    wrappingMethodNode.maxStack = methodNode.maxStack;
    // assign annotations to wrapping method
    wrappingMethodNode.visibleAnnotations = methodNode.visibleAnnotations;
    wrappingMethodNode.visibleParameterAnnotations = methodNode.visibleParameterAnnotations;
    // remove annotations from wrapped method to avoid wrong behavior controlled by annotations
    methodNode.visibleAnnotations = null;
    methodNode.visibleParameterAnnotations = null;
    // rename original method
    methodNode.access = TransformerUtil.modifyVisibility(methodNode.access, Opcodes.ACC_PRIVATE);
    final LabelNode l0 = new LabelNode();
    final LabelNode l1 = new LabelNode();
    final LabelNode l2 = new LabelNode();
    final InsnList wInstructions = wrappingMethodNode.instructions;
    if ("<init>".equals(methodNode.name)) {
        // wrap a constructor
        methodNode.name = WRAP_NAME_PREFIX + "init" + WRAP_NAME_PREFIX;
        // move call to other constructors to new method
        AbstractInsnNode ins = null;
        ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();
        // number of invokespecial calls before actual constructor call
        int numInvokeSpecials = 0;
        while (iter.hasNext()) {
            ins = iter.next();
            iter.remove();
            wInstructions.add(ins);
            if (ins instanceof MethodInsnNode) {
                MethodInsnNode mins = (MethodInsnNode) ins;
                if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                    if (mins.name.startsWith("<init>")) {
                        if (numInvokeSpecials == 0) {
                            break;
                        } else {
                            numInvokeSpecials--;
                        }
                    }
                }
            } else if (ins instanceof TypeInsnNode) {
                TypeInsnNode typeIns = (TypeInsnNode) ins;
                if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                    numInvokeSpecials++;
                }
            }
        }
    } else {
        methodNode.name = WRAP_NAME_PREFIX + methodNode.name;
    }
    int varReturnValue = 0;
    final Type returnType = Type.getReturnType(methodNode.desc);
    if (returnType.equals(Type.VOID_TYPE)) {
        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, "java/lang/Throwable"));
    } else {
        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Throwable"));
        if (!TransformerUtil.isStatic(methodNode.access)) {
            // load "this"
            varReturnValue++;
        }
        // consider method arguments to find right variable index
        final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
        for (int i = 0; i < argTypes.length; i++) {
            varReturnValue++;
            // long/double take two registers
            if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
                varReturnValue++;
            }
        }
        // push NULL on the stack and initialize variable for return value for it
        wInstructions.add(new InsnNode(Opcodes.ACONST_NULL));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
    }
    int var = 0;
    // --- L0
    wInstructions.add(l0);
    wInstructions.add(this.addCaptureCall(TransformerUtil.isStatic(methodNode.access), className, wrappingMethodNode.name, wrappingMethodNode.desc, Type.getArgumentTypes(methodNode.desc)));
    if (!TransformerUtil.isStatic(methodNode.access)) {
        // load "this" to call method
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        var++;
    }
    final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
    for (int i = 0; i < argTypes.length; i++) {
        this.addLoadInsn(wInstructions, argTypes[i], var++);
        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            var++;
        }
    }
    if (TransformerUtil.isStatic(methodNode.access)) {
        wInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, methodNode.name, methodNode.desc));
    } else {
        wInstructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, methodNode.name, methodNode.desc));
    }
    var++;
    if (returnType.equals(Type.VOID_TYPE)) {
        wInstructions.add(new JumpInsnNode(Opcodes.GOTO, l2));
        // --- L1
        wInstructions.add(l1);
        wInstructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));
        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));
        // FIXME <--- DUPLICATE CODE
        // --- L2
        wInstructions.add(l2);
        wInstructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));
        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);
        wInstructions.add(new InsnNode(Opcodes.RETURN));
    } else {
        // construct store of the wrapped method call's result
        this.addBoxingStmt(wInstructions, returnType);
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, varReturnValue));
        this.addUnBoxingStmt(wInstructions, returnType);
        final int storeOpcode = returnType.getOpcode(Opcodes.ISTORE);
        // might be only var
        wInstructions.add(new VarInsnNode(storeOpcode, ++var));
        // --- L1
        wInstructions.add(l1);
        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);
        // construct load of the wrapped method call's result
        int loadOpcode = returnType.getOpcode(Opcodes.ILOAD);
        wInstructions.add(new VarInsnNode(loadOpcode, var));
        // construct return of the wrapped method call's result
        this.addReturnInsn(wInstructions, returnType);
        // ---- L2
        wInstructions.add(l2);
        wInstructions.add(new FrameNode(Opcodes.F_FULL, 2, new Object[] { className, this.getInternalName(returnType) }, 1, new Object[] { "java/lang/Throwable" }));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));
        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));
    }
    transformWrapperCalls(methodNode);
    return wrappingMethodNode;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) FrameNode(org.objectweb.asm.tree.FrameNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 24 with FrameNode

use of org.objectweb.asm.tree.FrameNode 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);
}
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)

Example 25 with FrameNode

use of org.objectweb.asm.tree.FrameNode in project maple-ir by LLVM-but-worse.

the class FrameNodeSerializer method deserialize.

@Override
public FrameNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = (JsonObject) json;
    int opcode;
    int type;
    List<?> local = null, stack = null;
    opcode = jsonObject.get("opcode").getAsInt();
    type = jsonObject.get("type").getAsInt();
    local = context.deserialize(jsonObject.get("local"), List.class);
    stack = context.deserialize(jsonObject.get("stack"), List.class);
    FrameNode node = new FrameNode(opcode, local.size(), local.toArray(), stack.size(), stack.toArray());
    node.type = type;
    return node;
}
Also used : FrameNode(org.objectweb.asm.tree.FrameNode) JsonObject(com.google.gson.JsonObject) List(java.util.List)

Aggregations

FrameNode (org.objectweb.asm.tree.FrameNode)25 Label (org.objectweb.asm.Label)15 Type (org.objectweb.asm.Type)15 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)7 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)6 TaintUtils (edu.columbia.cs.psl.phosphor.TaintUtils)5 OffsetPreservingLabel (edu.columbia.cs.psl.phosphor.instrumenter.asm.OffsetPreservingLabel)5 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)5 InsnNode (org.objectweb.asm.tree.InsnNode)5 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)5 LineNumberNode (org.objectweb.asm.tree.LineNumberNode)5 LabelNode (org.objectweb.asm.tree.LabelNode)4 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)4 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)3 MethodNode (org.objectweb.asm.tree.MethodNode)3 NeverNullArgAnalyzerAdapter (edu.columbia.cs.psl.phosphor.instrumenter.analyzer.NeverNullArgAnalyzerAdapter)2 ReflectionMasker (edu.columbia.cs.psl.phosphor.runtime.ReflectionMasker)2 TaintSentinel (edu.columbia.cs.psl.phosphor.runtime.TaintSentinel)2 UninstrumentedTaintSentinel (edu.columbia.cs.psl.phosphor.runtime.UninstrumentedTaintSentinel)2 MultiDTaintedArray (edu.columbia.cs.psl.phosphor.struct.multid.MultiDTaintedArray)2