summaryrefslogtreecommitdiff
path: root/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/main/rels/MethodProcessorThread.java
blob: 2f98cf82ce08db0493c5e56792ca659c35333ed6 (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 org.jetbrains.java.decompiler.main.rels;

import org.jetbrains.java.decompiler.code.InstructionSequence;
import org.jetbrains.java.decompiler.code.cfg.ControlFlowGraph;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.collectors.CounterContainer;
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.modules.code.DeadCodeHelper;
import org.jetbrains.java.decompiler.modules.decompiler.*;
import org.jetbrains.java.decompiler.modules.decompiler.deobfuscator.ExceptionDeobfuscator;
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarProcessor;
import org.jetbrains.java.decompiler.struct.StructClass;
import org.jetbrains.java.decompiler.struct.StructMethod;

import java.io.IOException;

public class MethodProcessorThread implements Runnable {

  public final Object lock = new Object();

  private final StructMethod method;
  private final VarProcessor varproc;
  private final DecompilerContext parentContext;

  private volatile RootStatement root;
  private volatile Throwable error;

  public MethodProcessorThread(StructMethod method, VarProcessor varproc, DecompilerContext parentContext) {
    this.method = method;
    this.varproc = varproc;
    this.parentContext = parentContext;
  }

  public void run() {

    DecompilerContext.setCurrentContext(parentContext);

    error = null;
    root = null;

    try {
      root = codeToJava(method, varproc);

      synchronized (lock) {
        lock.notifyAll();
      }
    }
    catch (ThreadDeath ex) {
      throw ex;
    }
    catch (Throwable ex) {
      error = ex;
    }
  }

  public static RootStatement codeToJava(StructMethod mt, VarProcessor varproc) throws IOException {

    StructClass cl = mt.getClassStruct();

    boolean isInitializer = "<clinit>".equals(mt.getName()); // for now static initializer only

    mt.expandData();
    InstructionSequence seq = mt.getInstructionSequence();
    ControlFlowGraph graph = new ControlFlowGraph(seq);

    //		System.out.println(graph.toString());


    //		if(mt.getName().endsWith("_getActiveServers")) {
    //			System.out.println();
    //		}

    //DotExporter.toDotFile(graph, new File("c:\\Temp\\fern1.dot"), true);

    DeadCodeHelper.removeDeadBlocks(graph);
    graph.inlineJsr(mt);

    //		DotExporter.toDotFile(graph, new File("c:\\Temp\\fern4.dot"), true);

    // TODO: move to the start, before jsr inlining
    DeadCodeHelper.connectDummyExitBlock(graph);

    DeadCodeHelper.removeGotos(graph);
    ExceptionDeobfuscator.removeCircularRanges(graph);
    //DeadCodeHelper.removeCircularRanges(graph);


    //		DotExporter.toDotFile(graph, new File("c:\\Temp\\fern3.dot"), true);

    ExceptionDeobfuscator.restorePopRanges(graph);

    if (DecompilerContext.getOption(IFernflowerPreferences.REMOVE_EMPTY_RANGES)) {
      ExceptionDeobfuscator.removeEmptyRanges(graph);
    }

    //		DotExporter.toDotFile(graph, new File("c:\\Temp\\fern3.dot"), true);

    if (DecompilerContext.getOption(IFernflowerPreferences.NO_EXCEPTIONS_RETURN)) {
      // special case: single return instruction outside of a protected range
      DeadCodeHelper.incorporateValueReturns(graph);
    }

    //		DotExporter.toDotFile(graph, new File("c:\\Temp\\fern5.dot"), true);

    //		ExceptionDeobfuscator.restorePopRanges(graph);
    ExceptionDeobfuscator.insertEmptyExceptionHandlerBlocks(graph);

    DeadCodeHelper.mergeBasicBlocks(graph);

    DecompilerContext.getCounterContainer().setCounter(CounterContainer.VAR_COUNTER, mt.getLocalVariables());

    //DotExporter.toDotFile(graph, new File("c:\\Temp\\fern3.dot"), true);
    //System.out.println(graph.toString());

    if (ExceptionDeobfuscator.hasObfuscatedExceptions(graph)) {
      DecompilerContext.getLogger().writeMessage("Heavily obfuscated exception ranges found!", IFernflowerLogger.Severity.WARN);
    }

    RootStatement root = DomHelper.parseGraph(graph);

    FinallyProcessor fproc = new FinallyProcessor(varproc);
    while (fproc.iterateGraph(mt, root, graph)) {

      //DotExporter.toDotFile(graph, new File("c:\\Temp\\fern2.dot"), true);
      //System.out.println(graph.toString());
      //System.out.println("~~~~~~~~~~~~~~~~~~~~~~ \r\n"+root.toJava());

      root = DomHelper.parseGraph(graph);
    }

    // remove synchronized exception handler
    // not until now because of comparison between synchronized statements in the finally cycle
    DomHelper.removeSynchronizedHandler(root);

    //		DotExporter.toDotFile(graph, new File("c:\\Temp\\fern3.dot"), true);
    //		System.out.println(graph.toString());

    //		LabelHelper.lowContinueLabels(root, new HashSet<StatEdge>());

    SequenceHelper.condenseSequences(root);

    ClearStructHelper.clearStatements(root);

    ExprProcessor proc = new ExprProcessor();
    proc.processStatement(root, cl);

    //		DotExporter.toDotFile(graph, new File("c:\\Temp\\fern3.dot"), true);
    //		System.out.println(graph.toString());

    //System.out.println("~~~~~~~~~~~~~~~~~~~~~~ \r\n"+root.toJava());

    while (true) {
      StackVarsProcessor stackproc = new StackVarsProcessor();
      stackproc.simplifyStackVars(root, mt, cl);

      //System.out.println("~~~~~~~~~~~~~~~~~~~~~~ \r\n"+root.toJava());

      varproc.setVarVersions(root);

      //			System.out.println("~~~~~~~~~~~~~~~~~~~~~~ \r\n"+root.toJava());

      if (!new PPandMMHelper().findPPandMM(root)) {
        break;
      }
    }

    while (true) {

      LabelHelper.cleanUpEdges(root);

      while (true) {

        MergeHelper.enhanceLoops(root);

        if (LoopExtractHelper.extractLoops(root)) {
          continue;
        }

        if (!IfHelper.mergeAllIfs(root)) {
          break;
        }
      }

      if (DecompilerContext.getOption(IFernflowerPreferences.IDEA_NOT_NULL_ANNOTATION)) {

        if (IdeaNotNullHelper.removeHardcodedChecks(root, mt)) {

          SequenceHelper.condenseSequences(root);

          StackVarsProcessor stackproc = new StackVarsProcessor();
          stackproc.simplifyStackVars(root, mt, cl);

          varproc.setVarVersions(root);
        }
      }

      LabelHelper.identifyLabels(root);

      //			System.out.println("~~~~~~~~~~~~~~~~~~~~~~ \r\n"+root.toJava());

      if (InlineSingleBlockHelper.inlineSingleBlocks(root)) {
        continue;
      }

      // initializer may have at most one return point, so no transformation of method exits permitted
      if (isInitializer || !ExitHelper.condenseExits(root)) {
        break;
      }

      // FIXME: !!
      //			if(!EliminateLoopsHelper.eliminateLoops(root)) {
      //				break;
      //			}
    }

    ExitHelper.removeRedundantReturns(root);

    SecondaryFunctionsHelper.identifySecondaryFunctions(root);

    varproc.setVarDefinitions(root);

    // must be the last invocation, because it makes the statement structure inconsistent
    // FIXME: new edge type needed
    LabelHelper.replaceContinueWithBreak(root);

    mt.releaseResources();

    //		System.out.println("++++++++++++++++++++++/// \r\n"+root.toJava());

    return root;
  }

  public RootStatement getResult() throws Throwable {
    Throwable t = error;
    if (t != null) throw t;
    return root;
  }

  public Throwable getError() {
    return error;
  }
}