Search in sources :

Example 16 with BasicValue

use of org.jetbrains.org.objectweb.asm.tree.analysis.BasicValue in project intellij-community by JetBrains.

the class NullableMethodInterpreter method analyze.

static Result analyze(MethodNode methodNode, boolean[] origins, boolean jsr) throws AnalyzerException {
    InsnList insns = methodNode.instructions;
    Constraint[] data = new Constraint[insns.size()];
    int[] originsMapping = mapOrigins(origins);
    NullableMethodInterpreter interpreter = new NullableMethodInterpreter(insns, origins, originsMapping);
    Frame<BasicValue>[] frames = jsr ? new AnalyzerExt<>(interpreter, data, Constraint.EMPTY).analyze("this", methodNode) : new LiteAnalyzerExt<>(interpreter, data, Constraint.EMPTY).analyze("this", methodNode);
    BasicValue result = BasicValue.REFERENCE_VALUE;
    for (int i = 0; i < frames.length; i++) {
        Frame<BasicValue> frame = frames[i];
        if (frame != null && insns.get(i).getOpcode() == Opcodes.ARETURN) {
            BasicValue stackTop = frame.pop();
            result = combine(result, stackTop, data[i]);
        }
    }
    if (result instanceof LabeledNull) {
        return FinalNull;
    }
    if (result instanceof Calls) {
        Calls calls = ((Calls) result);
        int mergedMappedLabels = calls.mergedLabels;
        if (mergedMappedLabels != 0) {
            Set<Product> sum = new HashSet<>();
            Key[] createdKeys = interpreter.keys;
            for (int origin = 0; origin < originsMapping.length; origin++) {
                int mappedOrigin = originsMapping[origin];
                Key createdKey = createdKeys[origin];
                if (createdKey != null && (mergedMappedLabels & (1 << mappedOrigin)) != 0) {
                    sum.add(new Product(Value.Null, Collections.singleton(createdKey)));
                }
            }
            if (!sum.isEmpty()) {
                return new Pending(sum);
            }
        }
    }
    return FinalBot;
}
Also used : Frame(org.jetbrains.org.objectweb.asm.tree.analysis.Frame) LiteAnalyzerExt(com.intellij.codeInspection.bytecodeAnalysis.asm.LiteAnalyzerExt) AnalyzerExt(com.intellij.codeInspection.bytecodeAnalysis.asm.AnalyzerExt) LiteAnalyzerExt(com.intellij.codeInspection.bytecodeAnalysis.asm.LiteAnalyzerExt) BasicValue(org.jetbrains.org.objectweb.asm.tree.analysis.BasicValue) HashSet(java.util.HashSet)

Example 17 with BasicValue

use of org.jetbrains.org.objectweb.asm.tree.analysis.BasicValue in project kotlin by JetBrains.

the class CodegenTestCase method verifyWithAsm.

private static boolean verifyWithAsm(@NotNull OutputFile file, ClassLoader loader) {
    ClassNode classNode = new ClassNode();
    new ClassReader(file.asByteArray()).accept(classNode, 0);
    SimpleVerifier verifier = new SimpleVerifier();
    verifier.setClassLoader(loader);
    Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>(verifier);
    boolean noErrors = true;
    for (MethodNode method : classNode.methods) {
        try {
            analyzer.analyze(classNode.name, method);
        } catch (Throwable e) {
            System.err.println(file.asText());
            System.err.println(classNode.name + "::" + method.name + method.desc);
            //noinspection InstanceofCatchParameter
            if (e instanceof AnalyzerException) {
                // Print the erroneous instruction
                TraceMethodVisitor tmv = new TraceMethodVisitor(new Textifier());
                ((AnalyzerException) e).node.accept(tmv);
                PrintWriter pw = new PrintWriter(System.err);
                tmv.p.print(pw);
                pw.flush();
            }
            e.printStackTrace();
            noErrors = false;
        }
    }
    return noErrors;
}
Also used : ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode) AnalyzerException(org.jetbrains.org.objectweb.asm.tree.analysis.AnalyzerException) SimpleVerifier(org.jetbrains.org.objectweb.asm.tree.analysis.SimpleVerifier) Analyzer(org.jetbrains.org.objectweb.asm.tree.analysis.Analyzer) Textifier(org.jetbrains.org.objectweb.asm.util.Textifier) TraceMethodVisitor(org.jetbrains.org.objectweb.asm.util.TraceMethodVisitor) BasicValue(org.jetbrains.org.objectweb.asm.tree.analysis.BasicValue) MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Aggregations

BasicValue (org.jetbrains.org.objectweb.asm.tree.analysis.BasicValue)17 Frame (org.jetbrains.org.objectweb.asm.tree.analysis.Frame)5 StrictBasicValue (org.jetbrains.kotlin.codegen.optimization.common.StrictBasicValue)4 Type (org.jetbrains.org.objectweb.asm.Type)3 In (com.intellij.codeInspection.bytecodeAnalysis.Direction.In)2 Edge (com.intellij.codeInspection.bytecodeAnalysis.asm.ControlFlowGraph.Edge)2 HashSet (java.util.HashSet)2 InOut (com.intellij.codeInspection.bytecodeAnalysis.Direction.InOut)1 AnalyzerExt (com.intellij.codeInspection.bytecodeAnalysis.asm.AnalyzerExt)1 LiteAnalyzerExt (com.intellij.codeInspection.bytecodeAnalysis.asm.LiteAnalyzerExt)1 SingletonSet (com.intellij.util.SingletonSet)1 HashSet (com.intellij.util.containers.HashSet)1 PrintWriter (java.io.PrintWriter)1 Set (java.util.Set)1 NotNull (org.jetbrains.annotations.NotNull)1 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)1 AbstractInsnNode (org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode)1 ClassNode (org.jetbrains.org.objectweb.asm.tree.ClassNode)1 MethodInsnNode (org.jetbrains.org.objectweb.asm.tree.MethodInsnNode)1 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)1