Search in sources :

Example 16 with Type

use of jdk.internal.org.objectweb.asm.Type in project jdk8u_jdk by JetBrains.

the class GeneratorAdapter method unbox.

/**
     * Generates the instructions to unbox the top stack value. This value is
     * replaced by its unboxed equivalent on top of the stack.
     *
     * @param type
     *            the type of the top stack value.
     */
public void unbox(final Type type) {
    Type t = NUMBER_TYPE;
    Method sig = null;
    switch(type.getSort()) {
        case Type.VOID:
            return;
        case Type.CHAR:
            t = CHARACTER_TYPE;
            sig = CHAR_VALUE;
            break;
        case Type.BOOLEAN:
            t = BOOLEAN_TYPE;
            sig = BOOLEAN_VALUE;
            break;
        case Type.DOUBLE:
            sig = DOUBLE_VALUE;
            break;
        case Type.FLOAT:
            sig = FLOAT_VALUE;
            break;
        case Type.LONG:
            sig = LONG_VALUE;
            break;
        case Type.INT:
        case Type.SHORT:
        case Type.BYTE:
            sig = INT_VALUE;
    }
    if (sig == null) {
        checkCast(type);
    } else {
        checkCast(t);
        invokeVirtual(t, sig);
    }
}
Also used : Type(jdk.internal.org.objectweb.asm.Type)

Example 17 with Type

use of jdk.internal.org.objectweb.asm.Type in project jdk8u_jdk by JetBrains.

the class Analyzer method analyze.

/**
     * Analyzes the given method.
     *
     * @param owner
     *            the internal name of the class to which the method belongs.
     * @param m
     *            the method to be analyzed.
     * @return the symbolic state of the execution stack frame at each bytecode
     *         instruction of the method. The size of the returned array is
     *         equal to the number of instructions (and labels) of the method. A
     *         given frame is <tt>null</tt> if and only if the corresponding
     *         instruction cannot be reached (dead code).
     * @throws AnalyzerException
     *             if a problem occurs during the analysis.
     */
@SuppressWarnings("unchecked")
public Frame<V>[] analyze(final String owner, final MethodNode m) throws AnalyzerException {
    if ((m.access & (ACC_ABSTRACT | ACC_NATIVE)) != 0) {
        frames = (Frame<V>[]) new Frame<?>[0];
        return frames;
    }
    n = m.instructions.size();
    insns = m.instructions;
    handlers = (List<TryCatchBlockNode>[]) new List<?>[n];
    frames = (Frame<V>[]) new Frame<?>[n];
    subroutines = new Subroutine[n];
    queued = new boolean[n];
    queue = new int[n];
    top = 0;
    // computes exception handlers for each instruction
    for (int i = 0; i < m.tryCatchBlocks.size(); ++i) {
        TryCatchBlockNode tcb = m.tryCatchBlocks.get(i);
        int begin = insns.indexOf(tcb.start);
        int end = insns.indexOf(tcb.end);
        for (int j = begin; j < end; ++j) {
            List<TryCatchBlockNode> insnHandlers = handlers[j];
            if (insnHandlers == null) {
                insnHandlers = new ArrayList<TryCatchBlockNode>();
                handlers[j] = insnHandlers;
            }
            insnHandlers.add(tcb);
        }
    }
    // computes the subroutine for each instruction:
    Subroutine main = new Subroutine(null, m.maxLocals, null);
    List<AbstractInsnNode> subroutineCalls = new ArrayList<AbstractInsnNode>();
    Map<LabelNode, Subroutine> subroutineHeads = new HashMap<LabelNode, Subroutine>();
    findSubroutine(0, main, subroutineCalls);
    while (!subroutineCalls.isEmpty()) {
        JumpInsnNode jsr = (JumpInsnNode) subroutineCalls.remove(0);
        Subroutine sub = subroutineHeads.get(jsr.label);
        if (sub == null) {
            sub = new Subroutine(jsr.label, m.maxLocals, jsr);
            subroutineHeads.put(jsr.label, sub);
            findSubroutine(insns.indexOf(jsr.label), sub, subroutineCalls);
        } else {
            sub.callers.add(jsr);
        }
    }
    for (int i = 0; i < n; ++i) {
        if (subroutines[i] != null && subroutines[i].start == null) {
            subroutines[i] = null;
        }
    }
    // initializes the data structures for the control flow analysis
    Frame<V> current = newFrame(m.maxLocals, m.maxStack);
    Frame<V> handler = newFrame(m.maxLocals, m.maxStack);
    current.setReturn(interpreter.newValue(Type.getReturnType(m.desc)));
    Type[] args = Type.getArgumentTypes(m.desc);
    int local = 0;
    if ((m.access & ACC_STATIC) == 0) {
        Type ctype = Type.getObjectType(owner);
        current.setLocal(local++, interpreter.newValue(ctype));
    }
    for (int i = 0; i < args.length; ++i) {
        current.setLocal(local++, interpreter.newValue(args[i]));
        if (args[i].getSize() == 2) {
            current.setLocal(local++, interpreter.newValue(null));
        }
    }
    while (local < m.maxLocals) {
        current.setLocal(local++, interpreter.newValue(null));
    }
    merge(0, current, null);
    init(owner, m);
    // control flow analysis
    while (top > 0) {
        int insn = queue[--top];
        Frame<V> f = frames[insn];
        Subroutine subroutine = subroutines[insn];
        queued[insn] = false;
        AbstractInsnNode insnNode = null;
        try {
            insnNode = m.instructions.get(insn);
            int insnOpcode = insnNode.getOpcode();
            int insnType = insnNode.getType();
            if (insnType == AbstractInsnNode.LABEL || insnType == AbstractInsnNode.LINE || insnType == AbstractInsnNode.FRAME) {
                merge(insn + 1, f, subroutine);
                newControlFlowEdge(insn, insn + 1);
            } else {
                current.init(f).execute(insnNode, interpreter);
                subroutine = subroutine == null ? null : subroutine.copy();
                if (insnNode instanceof JumpInsnNode) {
                    JumpInsnNode j = (JumpInsnNode) insnNode;
                    if (insnOpcode != GOTO && insnOpcode != JSR) {
                        merge(insn + 1, current, subroutine);
                        newControlFlowEdge(insn, insn + 1);
                    }
                    int jump = insns.indexOf(j.label);
                    if (insnOpcode == JSR) {
                        merge(jump, current, new Subroutine(j.label, m.maxLocals, j));
                    } else {
                        merge(jump, current, subroutine);
                    }
                    newControlFlowEdge(insn, jump);
                } else if (insnNode instanceof LookupSwitchInsnNode) {
                    LookupSwitchInsnNode lsi = (LookupSwitchInsnNode) insnNode;
                    int jump = insns.indexOf(lsi.dflt);
                    merge(jump, current, subroutine);
                    newControlFlowEdge(insn, jump);
                    for (int j = 0; j < lsi.labels.size(); ++j) {
                        LabelNode label = lsi.labels.get(j);
                        jump = insns.indexOf(label);
                        merge(jump, current, subroutine);
                        newControlFlowEdge(insn, jump);
                    }
                } else if (insnNode instanceof TableSwitchInsnNode) {
                    TableSwitchInsnNode tsi = (TableSwitchInsnNode) insnNode;
                    int jump = insns.indexOf(tsi.dflt);
                    merge(jump, current, subroutine);
                    newControlFlowEdge(insn, jump);
                    for (int j = 0; j < tsi.labels.size(); ++j) {
                        LabelNode label = tsi.labels.get(j);
                        jump = insns.indexOf(label);
                        merge(jump, current, subroutine);
                        newControlFlowEdge(insn, jump);
                    }
                } else if (insnOpcode == RET) {
                    if (subroutine == null) {
                        throw new AnalyzerException(insnNode, "RET instruction outside of a sub routine");
                    }
                    for (int i = 0; i < subroutine.callers.size(); ++i) {
                        JumpInsnNode caller = subroutine.callers.get(i);
                        int call = insns.indexOf(caller);
                        if (frames[call] != null) {
                            merge(call + 1, frames[call], current, subroutines[call], subroutine.access);
                            newControlFlowEdge(insn, call + 1);
                        }
                    }
                } else if (insnOpcode != ATHROW && (insnOpcode < IRETURN || insnOpcode > RETURN)) {
                    if (subroutine != null) {
                        if (insnNode instanceof VarInsnNode) {
                            int var = ((VarInsnNode) insnNode).var;
                            subroutine.access[var] = true;
                            if (insnOpcode == LLOAD || insnOpcode == DLOAD || insnOpcode == LSTORE || insnOpcode == DSTORE) {
                                subroutine.access[var + 1] = true;
                            }
                        } else if (insnNode instanceof IincInsnNode) {
                            int var = ((IincInsnNode) insnNode).var;
                            subroutine.access[var] = true;
                        }
                    }
                    merge(insn + 1, current, subroutine);
                    newControlFlowEdge(insn, insn + 1);
                }
            }
            List<TryCatchBlockNode> insnHandlers = handlers[insn];
            if (insnHandlers != null) {
                for (int i = 0; i < insnHandlers.size(); ++i) {
                    TryCatchBlockNode tcb = insnHandlers.get(i);
                    Type type;
                    if (tcb.type == null) {
                        type = Type.getObjectType("java/lang/Throwable");
                    } else {
                        type = Type.getObjectType(tcb.type);
                    }
                    int jump = insns.indexOf(tcb.handler);
                    if (newControlFlowExceptionEdge(insn, tcb)) {
                        handler.init(f);
                        handler.clearStack();
                        handler.push(interpreter.newValue(type));
                        merge(jump, handler, subroutine);
                    }
                }
            }
        } catch (AnalyzerException e) {
            throw new AnalyzerException(e.node, "Error at instruction " + insn + ": " + e.getMessage(), e);
        } catch (Exception e) {
            throw new AnalyzerException(insnNode, "Error at instruction " + insn + ": " + e.getMessage(), e);
        }
    }
    return frames;
}
Also used : LabelNode(jdk.internal.org.objectweb.asm.tree.LabelNode) TryCatchBlockNode(jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode) TableSwitchInsnNode(jdk.internal.org.objectweb.asm.tree.TableSwitchInsnNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AbstractInsnNode(jdk.internal.org.objectweb.asm.tree.AbstractInsnNode) Type(jdk.internal.org.objectweb.asm.Type) JumpInsnNode(jdk.internal.org.objectweb.asm.tree.JumpInsnNode) IincInsnNode(jdk.internal.org.objectweb.asm.tree.IincInsnNode) InsnList(jdk.internal.org.objectweb.asm.tree.InsnList) ArrayList(java.util.ArrayList) List(java.util.List) LookupSwitchInsnNode(jdk.internal.org.objectweb.asm.tree.LookupSwitchInsnNode) VarInsnNode(jdk.internal.org.objectweb.asm.tree.VarInsnNode)

Example 18 with Type

use of jdk.internal.org.objectweb.asm.Type in project jdk8u_jdk by JetBrains.

the class BasicVerifier method naryOperation.

@Override
public BasicValue naryOperation(final AbstractInsnNode insn, final List<? extends BasicValue> values) throws AnalyzerException {
    int opcode = insn.getOpcode();
    if (opcode == MULTIANEWARRAY) {
        for (int i = 0; i < values.size(); ++i) {
            if (!BasicValue.INT_VALUE.equals(values.get(i))) {
                throw new AnalyzerException(insn, null, BasicValue.INT_VALUE, values.get(i));
            }
        }
    } else {
        int i = 0;
        int j = 0;
        if (opcode != INVOKESTATIC && opcode != INVOKEDYNAMIC) {
            Type owner = Type.getObjectType(((MethodInsnNode) insn).owner);
            if (!isSubTypeOf(values.get(i++), newValue(owner))) {
                throw new AnalyzerException(insn, "Method owner", newValue(owner), values.get(0));
            }
        }
        String desc = (opcode == INVOKEDYNAMIC) ? ((InvokeDynamicInsnNode) insn).desc : ((MethodInsnNode) insn).desc;
        Type[] args = Type.getArgumentTypes(desc);
        while (i < values.size()) {
            BasicValue expected = newValue(args[j++]);
            BasicValue encountered = values.get(i++);
            if (!isSubTypeOf(encountered, expected)) {
                throw new AnalyzerException(insn, "Argument " + j, expected, encountered);
            }
        }
    }
    return super.naryOperation(insn, values);
}
Also used : Type(jdk.internal.org.objectweb.asm.Type)

Example 19 with Type

use of jdk.internal.org.objectweb.asm.Type in project jdk8u_jdk by JetBrains.

the class CheckClassAdapter method verify.

/**
     * Checks a given class.
     *
     * @param cr
     *            a <code>ClassReader</code> that contains bytecode for the
     *            analysis.
     * @param loader
     *            a <code>ClassLoader</code> which will be used to load
     *            referenced classes. This is useful if you are verifiying
     *            multiple interdependent classes.
     * @param dump
     *            true if bytecode should be printed out not only when errors
     *            are found.
     * @param pw
     *            write where results going to be printed
     */
public static void verify(final ClassReader cr, final ClassLoader loader, final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);
    Type syperType = cn.superName == null ? null : Type.getObjectType(cn.superName);
    List<MethodNode> methods = cn.methods;
    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext(); ) {
        interfaces.add(Type.getObjectType(i.next()));
    }
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(Type.getObjectType(cn.name), syperType, interfaces, (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
Also used : ClassNode(jdk.internal.org.objectweb.asm.tree.ClassNode) SimpleVerifier(jdk.internal.org.objectweb.asm.tree.analysis.SimpleVerifier) ArrayList(java.util.ArrayList) Analyzer(jdk.internal.org.objectweb.asm.tree.analysis.Analyzer) BasicValue(jdk.internal.org.objectweb.asm.tree.analysis.BasicValue) Type(jdk.internal.org.objectweb.asm.Type) MethodNode(jdk.internal.org.objectweb.asm.tree.MethodNode)

Example 20 with Type

use of jdk.internal.org.objectweb.asm.Type in project jdk8u_jdk by JetBrains.

the class SimpleVerifier method merge.

@Override
public BasicValue merge(final BasicValue v, final BasicValue w) {
    if (!v.equals(w)) {
        Type t = v.getType();
        Type u = w.getType();
        if (t != null && (t.getSort() == Type.OBJECT || t.getSort() == Type.ARRAY)) {
            if (u != null && (u.getSort() == Type.OBJECT || u.getSort() == Type.ARRAY)) {
                if ("Lnull;".equals(t.getDescriptor())) {
                    return w;
                }
                if ("Lnull;".equals(u.getDescriptor())) {
                    return v;
                }
                if (isAssignableFrom(t, u)) {
                    return v;
                }
                if (isAssignableFrom(u, t)) {
                    return w;
                }
                // interfaces
                do {
                    if (t == null || isInterface(t)) {
                        return BasicValue.REFERENCE_VALUE;
                    }
                    t = getSuperClass(t);
                    if (isAssignableFrom(t, u)) {
                        return newValue(t);
                    }
                } while (true);
            }
        }
        return BasicValue.UNINITIALIZED_VALUE;
    }
    return v;
}
Also used : Type(jdk.internal.org.objectweb.asm.Type)

Aggregations

Type (jdk.internal.org.objectweb.asm.Type)30 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)2 List (java.util.List)2 AbstractInsnNode (jdk.internal.org.objectweb.asm.tree.AbstractInsnNode)2 ClassNode (jdk.internal.org.objectweb.asm.tree.ClassNode)2 IincInsnNode (jdk.internal.org.objectweb.asm.tree.IincInsnNode)2 InsnList (jdk.internal.org.objectweb.asm.tree.InsnList)2 JumpInsnNode (jdk.internal.org.objectweb.asm.tree.JumpInsnNode)2 LabelNode (jdk.internal.org.objectweb.asm.tree.LabelNode)2 LookupSwitchInsnNode (jdk.internal.org.objectweb.asm.tree.LookupSwitchInsnNode)2 MethodNode (jdk.internal.org.objectweb.asm.tree.MethodNode)2 TableSwitchInsnNode (jdk.internal.org.objectweb.asm.tree.TableSwitchInsnNode)2 TryCatchBlockNode (jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode)2 VarInsnNode (jdk.internal.org.objectweb.asm.tree.VarInsnNode)2 Analyzer (jdk.internal.org.objectweb.asm.tree.analysis.Analyzer)2 BasicValue (jdk.internal.org.objectweb.asm.tree.analysis.BasicValue)2 SimpleVerifier (jdk.internal.org.objectweb.asm.tree.analysis.SimpleVerifier)2