summaryrefslogtreecommitdiff
path: root/java/java-analysis-impl/src/com/intellij/codeInspection/bytecodeAnalysis/ClassDataIndexer.java
blob: 9e656b98a7fb36e0efc8227903e83fc49bc66bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.codeInspection.bytecodeAnalysis;

import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.util.indexing.DataIndexer;
import com.intellij.util.indexing.FileContent;
import gnu.trove.TIntHashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.org.objectweb.asm.*;
import org.jetbrains.org.objectweb.asm.tree.MethodNode;
import org.jetbrains.org.objectweb.asm.tree.analysis.AnalyzerException;

import java.util.*;

import static com.intellij.codeInspection.bytecodeAnalysis.ProjectBytecodeAnalysis.LOG;

/**
 * @author lambdamix
 */
public class ClassDataIndexer implements DataIndexer<Long, IdEquation, FileContent> {
  final BytecodeAnalysisConverter myConverter;

  public ClassDataIndexer(BytecodeAnalysisConverter converter) {
    myConverter = converter;
  }

  @NotNull
  @Override
  public Map<Long, IdEquation> map(@NotNull FileContent inputData) {
    HashMap<Long, IdEquation> map = new HashMap<Long, IdEquation>();
    try {
      ClassEquations rawEquations = processClass(new ClassReader(inputData.getContent()));
      List<Equation<Key, Value>> rawParameterEquations = rawEquations.parameterEquations;
      List<Equation<Key, Value>> rawContractEquations = rawEquations.contractEquations;

      for (Equation<Key, Value> rawParameterEquation: rawParameterEquations) {
        IdEquation equation = myConverter.convert(rawParameterEquation);
        map.put(equation.id, equation);
      }
      for (Equation<Key, Value> rawContractEquation: rawContractEquations) {
        IdEquation equation = myConverter.convert(rawContractEquation);
        map.put(equation.id, equation);
      }
    }
    catch (ProcessCanceledException e) {
      throw e;
    }
    catch (Throwable e) {
      // incorrect bytecode may result in Runtime exceptions during analysis
      // so here we suppose that exception is due to incorrect bytecode
      LOG.debug("Unexpected Error during indexing of bytecode", e);
    }
    return map;
  }

  private static class ClassEquations {
    final List<Equation<Key, Value>> parameterEquations;
    final List<Equation<Key, Value>> contractEquations;

    private ClassEquations(List<Equation<Key, Value>> parameterEquations, List<Equation<Key, Value>> contractEquations) {
      this.parameterEquations = parameterEquations;
      this.contractEquations = contractEquations;
    }
  }

  public static ClassEquations processClass(final ClassReader classReader) {
    final List<Equation<Key, Value>> parameterEquations = new ArrayList<Equation<Key, Value>>();
    final List<Equation<Key, Value>> contractEquations = new ArrayList<Equation<Key, Value>>();

    classReader.accept(new ClassVisitor(Opcodes.ASM5) {
      private boolean stableClass;

      @Override
      public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        stableClass = (access & Opcodes.ACC_FINAL) != 0;
        super.visit(version, access, name, signature, superName, interfaces);
      }

      @Override
      public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        final MethodNode node = new MethodNode(Opcodes.ASM5, access, name, desc, signature, exceptions);
        return new MethodVisitor(Opcodes.ASM5, node) {
          @Override
          public void visitEnd() {
            super.visitEnd();
            processMethod(classReader.getClassName(), node, stableClass);
          }
        };
      }

      void processMethod(final String className, final MethodNode methodNode, boolean stableClass) {
        ProgressManager.checkCanceled();
        Type[] argumentTypes = Type.getArgumentTypes(methodNode.desc);
        Type resultType = Type.getReturnType(methodNode.desc);
        int resultSort = resultType.getSort();
        boolean isReferenceResult = resultSort == Type.OBJECT || resultSort == Type.ARRAY;
        boolean isBooleanResult = Type.BOOLEAN_TYPE == resultType;
        boolean isInterestingResult = isReferenceResult || isBooleanResult;

        if (argumentTypes.length == 0 && !isInterestingResult) {
          return;
        }

        Method method = new Method(className, methodNode.name, methodNode.desc);
        int access = methodNode.access;
        boolean stable =
          stableClass ||
          (access & Opcodes.ACC_FINAL) != 0 ||
          (access & Opcodes.ACC_PRIVATE) != 0 ||
          (access & Opcodes.ACC_STATIC) != 0 ||
          "<init>".equals(methodNode.name);
        try {
          boolean added = false;
          ControlFlowGraph graph = cfg.buildControlFlowGraph(className, methodNode);

          boolean maybeLeakingParameter = false;
          for (Type argType : argumentTypes) {
            int argSort = argType.getSort();
            if (argSort == Type.OBJECT || argSort == Type.ARRAY || (isInterestingResult && Type.BOOLEAN_TYPE.equals(argType))) {
              maybeLeakingParameter = true;
              break;
            }
          }

          if (graph.transitions.length > 0) {
            DFSTree dfs = cfg.buildDFSTree(graph.transitions);
            boolean reducible = dfs.back.isEmpty() || cfg.reducible(graph, dfs);
            if (reducible) {
              NotNullLazyValue<TIntHashSet> resultOrigins = new NotNullLazyValue<TIntHashSet>() {
                @NotNull
                @Override
                protected TIntHashSet compute() {
                  try {
                    return cfg.resultOrigins(className, methodNode);
                  }
                  catch (AnalyzerException e) {
                    throw new RuntimeException(e);
                  }
                }
              };
              boolean[] leakingParameters = maybeLeakingParameter ? cfg.leakingParameters(className, methodNode) : null;
              boolean shouldComputeResult = isReferenceResult;

              if (!shouldComputeResult && isInterestingResult && maybeLeakingParameter) {
                loop: for (int i = 0; i < argumentTypes.length; i++) {
                  Type argType = argumentTypes[i];
                  int argSort = argType.getSort();
                  boolean isReferenceArg = argSort == Type.OBJECT || argSort == Type.ARRAY;
                  boolean isBooleanArg = Type.BOOLEAN_TYPE.equals(argType);
                  if ((isReferenceArg || isBooleanArg) && !leakingParameters[i]) {
                    shouldComputeResult = true;
                    break loop;
                  }
                }
              }

              Equation<Key, Value> resultEquation =
                shouldComputeResult ? new InOutAnalysis(new RichControlFlow(graph, dfs), new Out(), resultOrigins.getValue(), stable).analyze() : null;

              for (int i = 0; i < argumentTypes.length; i++) {
                Type argType = argumentTypes[i];
                int argSort = argType.getSort();
                boolean isReferenceArg = argSort == Type.OBJECT || argSort == Type.ARRAY;
                boolean isBooleanArg = Type.BOOLEAN_TYPE.equals(argType);
                if (isReferenceArg) {
                  if (leakingParameters[i]) {
                    parameterEquations.add(new NonNullInAnalysis(new RichControlFlow(graph, dfs), new In(i), stable).analyze());
                  } else {
                    parameterEquations.add(new Equation<Key, Value>(new Key(method, new In(i), stable), new Final<Key, Value>(Value.Top)));
                  }
                }
                if (isReferenceArg && isInterestingResult) {
                  if (leakingParameters[i]) {
                    contractEquations.add(new InOutAnalysis(new RichControlFlow(graph, dfs), new InOut(i, Value.Null), resultOrigins.getValue(), stable).analyze());
                    contractEquations.add(new InOutAnalysis(new RichControlFlow(graph, dfs), new InOut(i, Value.NotNull), resultOrigins.getValue(), stable).analyze());
                  } else {
                    contractEquations.add(new Equation<Key, Value>(new Key(method, new InOut(i, Value.Null), stable), resultEquation.rhs));
                    contractEquations.add(new Equation<Key, Value>(new Key(method, new InOut(i, Value.NotNull), stable), resultEquation.rhs));
                  }
                }
                if (isBooleanArg && isInterestingResult) {
                  if (leakingParameters[i]) {
                    contractEquations.add(new InOutAnalysis(new RichControlFlow(graph, dfs), new InOut(i, Value.False), resultOrigins.getValue(), stable).analyze());
                    contractEquations.add(new InOutAnalysis(new RichControlFlow(graph, dfs), new InOut(i, Value.True), resultOrigins.getValue(), stable).analyze());
                  } else {
                    contractEquations.add(new Equation<Key, Value>(new Key(method, new InOut(i, Value.False), stable), resultEquation.rhs));
                    contractEquations.add(new Equation<Key, Value>(new Key(method, new InOut(i, Value.True), stable), resultEquation.rhs));
                  }
                }
              }
              if (isReferenceResult) {
                if (resultEquation != null) {
                  contractEquations.add(resultEquation);
                } else {
                  contractEquations.add(new InOutAnalysis(new RichControlFlow(graph, dfs), new Out(), resultOrigins.getValue(), stable).analyze());
                }
              }
              added = true;
            }
            else {
              LOG.debug("CFG for " + method + " is not reducible");
            }
          }

          if (!added) {
            method = new Method(className, methodNode.name, methodNode.desc);
            for (int i = 0; i < argumentTypes.length; i++) {
              Type argType = argumentTypes[i];
              int argSort = argType.getSort();
              boolean isReferenceArg = argSort == Type.OBJECT || argSort == Type.ARRAY;
              boolean isBooleanArg = Type.BOOLEAN_TYPE.equals(argType);

              if (isReferenceArg) {
                parameterEquations.add(new Equation<Key, Value>(new Key(method, new In(i), stable), new Final<Key, Value>(Value.Top)));
              }
              if (isReferenceArg && isInterestingResult) {
                contractEquations.add(new Equation<Key, Value>(new Key(method, new InOut(i, Value.Null), stable), new Final<Key, Value>(Value.Top)));
                contractEquations.add(new Equation<Key, Value>(new Key(method, new InOut(i, Value.NotNull), stable), new Final<Key, Value>(Value.Top)));
              }
              if (isBooleanArg && isInterestingResult) {
                contractEquations.add(new Equation<Key, Value>(new Key(method, new InOut(i, Value.False), stable), new Final<Key, Value>(Value.Top)));
                contractEquations.add(new Equation<Key, Value>(new Key(method, new InOut(i, Value.True), stable), new Final<Key, Value>(Value.Top)));
              }
            }
            if (isReferenceResult) {
              contractEquations.add(new Equation<Key, Value>(new Key(method, new Out(), stable), new Final<Key, Value>(Value.Top)));
            }
          }
        }
        catch (ProcessCanceledException e) {
          throw e;
        }
        catch (Throwable e) {
          // incorrect bytecode may result in Runtime exceptions during analysis
          // so here we suppose that exception is due to incorrect bytecode
          LOG.debug("Unexpected Error during processing of " + method, e);
        }
      }
    }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);

    return new ClassEquations(parameterEquations, contractEquations);
  }
}