    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Anatomy of a Java Decompiler</title>
        <link>https://members.accu.org/index.php/journals/1850</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #119 - February 2014 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c334/">o119</a>
                    (6)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c334-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c334+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Anatomy of a Java Decompiler</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 03 February 2014 10:21:07 +00:00 or Mon, 03 February 2014 10:21:07 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Java byte code can be reverted back into source code. Lee Benfield and Mike Strobel show how.</p>
<p><strong>Body:</strong>&nbsp;<p>A decompiler, simply put, attempts to reverse the transformation of source code to object code. But there are many interesting complexities â€“ Java source code is structured; bytecode certainly isnâ€™t. Moreover, the transformation isnâ€™t one-to-one: two different Java programs may yield identical bytecode. We need to apply heuristics in order to get a reasonable approximation of the original source.</p>

<h2>(A tiny) bytecode refresher</h2>

<p>In order to understand how a decompiler works, itâ€™s necessary to understand the basics of byte code. If youâ€™re already familiar with byte code, feel free to skip ahead to the next section.</p>

<p>The JVM is a <em>stack-based machine</em> (as opposed to a register-based machine), meaning instructions operate on an evaluation stack. Operands may be popped off the stack, various operations performed, and the results pushed back onto the stack for further evaluation. Consider the following method:</p>

<pre class="programlisting">
  public static int plus(int a, int b) {
    int c = a + b;
    return c;
  }</pre>

<p>Note: All byte code shown in this article is output from <code>javap</code>, e.g., <code>javap -c -p MyClass</code>. (Comments added for clarity.)</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
public static int plus(int, int);
 Code:
  stack=2, locals=3, arguments=2
   0: iload_0
      // load 'a' from slot 0, push onto stack
   1: iload_1
      // load 'b' from slot 1, push onto stack
   2: iadd
      // pop 2 integers, add them together,
      // and push the result
   3: istore_2
      // pop the result, store as 'c' in slot 2
   4: iload_2
      // load 'c' from slot 2, push onto stack
   5: ireturn
      // return the integer at the top of the stack
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>A methodâ€™s local variables (including arguments to the method) are stored in what the JVM refers to as the <em>
local variable array</em>. Weâ€™ll refer to a value (or reference) stored in location <code>#x</code> in the local variable array as â€˜slot #xâ€™ for brevity (see JVM Specification Â§2.6.2 [<a href="#[JVMa]">JVMa</a>]).</p>

<p>For <code>instance</code> methods, the value in slot #0 is always the <code>this</code> pointer. Then come the method arguments, from left to right, followed by any local variables declared within the method. In the example above, the method is static, so there is no <code>this</code> pointer; slot #0 holds parameter <code>x</code>, and slot #1 holds parameter <code>y</code>. The local variable <code>sum</code> resides in slot #2.</p>

<p>Itâ€™s interesting to note that each method has a max stack size and max local variable storage, both of which are determined at compile time.</p>

<p>One thing thatâ€™s immediately obvious from here, which you might not initially expect, is that the compiler made no attempt to optimise the code. In fact, <code>javac</code> almost never emits optimised bytecode. This has multiple benefits, including the ability to set breakpoints at all executable source lines: if we were to eliminate the redundant load/store operations, weâ€™d lose that capability. Thus, most of the heavy lifting is performed at runtime by a just-in-time (JIT) compiler.</p>

<h2>Decompiling</h2>

<p>So, how can you take unstructured, stack-based byte code and translate it back into structured Java code? One of the first steps is usually to get rid of the operand stack, which we do by mapping stack values to variables and inserting the appropriate load and store operations.</p>

<p>A â€˜stack variableâ€™ is only assigned once, and consumed once. You might note that this will lead to a <em>lot</em> of redundant variables â€“ more on this later! The decompiler may also reduce the byte code into an even simpler instruction set, but we wonâ€™t consider that here.</p>

<p>We'll use the notation <code>s0</code> (etc.) to represent <em>stack variables</em>, and <code>v0</code> to represent true local variables referenced in the original byte code (and stored in slots).</p>

<table>
	<tr>
		<th></th>
		<th>Bytecode</th>
		<th>Stack Variables</th>
		<th>Copy Propagation</th>
	</tr>
	<tr>
		<td><code>0</code></td>
		<td><code>iload_0</code></td>
		<td><code>s0 = v0</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>1</code></td>
		<td><code>iload_1</code></td>
		<td><code>s1 = v1</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>2</code></td>
		<td><code>iadd</code></td>
		<td><code>s2 = s0 + s1</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>3</code></td>
		<td><code>istore_2</code></td>
		<td><code>v2 = s2</code></td>
		<td><code>v2 = v0 + v1</code></td>
	</tr>
	<tr>
		<td><code>4</code></td>
		<td><code>iload_2</code></td>
		<td><code>s3 = v2</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>5</code></td>
		<td><code>ireturn</code></td>
		<td><code>return s3</code></td>
		<td><code>return v2</code></td>
	</tr>
</table>

<p>We get from <em>bytecode</em> to <em>variables</em> by assigning identifiers to every value pushed or popped, e.g., iadd pops two operands to add and pushes the result.</p>

<table class="journaltable">
	<tr>
		<th>Restoring variable names</th>
	</tr>
	<tr>
		<td>If variables are reduced to slot references in the byte code, how then do we restore the original variable names? Itâ€™s possible we canâ€™t. To improve the debugging experience, the byte code for each method may include a special section called the local variable table. For each variable in the original source, there exists an entry that specifies the name, the slot number, and the bytecode range for which the name applies. This table (and other useful metadata) can be included in the javap disassembly by including the <code>-l</code> option. For our <code>plus()</code> method above, the table <code>javap</code> emits looks like this:
		<table>
			<tr>
				<th>Start</th>
				<th>Length</th>
				<th>Slot</th>
				<th>Name</th>
				<th>Signature</th>
			</tr>
			<tr>
				<td>0</td>
				<td>6</td>
				<td>0</td>
				<td>a</td>
				<td>I</td>
			</tr>
			<tr>
				<td>0</td>
				<td>6</td>
				<td>1</td>
				<td>b</td>
				<td>I</td>
			</tr>
			<tr>
				<td>4</td>
				<td>2</td>
				<td>2</td>
				<td>c</td>
				<td>I</td>
			</tr>
		</table>
		<p>Here, we see that <code>v2</code> refers to an integer variable originally named <code>c</code> at bytecode offsets #4â€“5.</p>
		For classes that have been compiled without local variable tables (or which had them stripped out by an obfuscator), we have to generate our own names. There are many strategies for doing this; a clever implementation might look at how a variable is used for hints on an appropriate name.</td>
	</tr>
</table>

<p>We then apply a technique called <em>copy propagation</em> to eliminate some of the redundant variables. Copy propagation is a form of inlining in which references to variables are simply replaced with the assigned value, provided the transformation is valid.</p>

<table class="journaltable">
	<tr>
		<th>But what are the types?</th>
	</tr>
	<tr>
		<td>When dealing with stack values, the JVM uses a more simplistic type system than Java source. Specifically, <code>boolean</code>, <code>char</code>, and <code>short</code> values are manipulated using the same instructions as <code>int</code> values. Thus, the comparison <code>v0 != 0,</code> could be interpreted as:
<pre class="programlisting">
	v0 != false ? v1 : v2</pre> 
<p>or:</p> 
<pre class="programlisting">
	v0 != 0 ? v1 : v2 </pre>
or even: 
<pre class="programlisting">
  v0 != false ? v1 == true : v2 == true </pre>
<p>and so on!</p>
<p>In this case, however, we are fortunate to know the exact type of <code>v0</code>, as it is contained within the method descriptor:</p>
<pre class="programlisting">
    descriptor: (ZII)I
    flags: ACC_PUBLIC, ACC_STATIC</pre>
<p>This tells us the method signature is of the form:</p>
<pre class="programlisting">
    public static int plus(boolean, int, int)</pre>
<p>We can also infer that <code>v3</code> should be an <code>int</code> (as opposed to a <code>boolean</code>) because it is used as the return value, and the descriptor tells us the return type. We are then left with:</p>
<pre class="programlisting">
  v3 =  v0 ? v1 : v2
  return v3</pre>
<p>As an aside, if <code>v0</code> were a local variable (and not a formal parameter) then we might not know it represents a <code>boolean</code> value and not an <code>int</code>. Remember that local variable table we mentioned earlier, which tells us the original variable names? It also includes information about the variable <em>types</em>, so if your compiler is configured to emit debug information, we can look to that table for type hints. There is another table called <code>LocalVariableTypeTable</code> [<a href="#JVMb">JVMb</a>] that contains similar information; the key difference is that a <code>LocalVariableTypeTable</code> may include detailed information about generic types, whereas a <code>LocalVariableTable</code> cannot. Itâ€™s worth noting that these tables are <em>unverified</em> metadata, so they are <strong><em>not necessarily trustworthy</em></strong>. A particularly devious obfuscator might choose to fill these tables with lies, and the resulting bytecode would still be valid! Use them at your own discretion.</p></td>
	</tr>
</table>

<p>What do we mean by â€˜validâ€™? Well, there are some important restrictions. Consider the following:</p>

<pre class="programlisting">
  0: s0 = v1
  1: v1 = s4
  2: v2 = s0 &lt;-- s0 cannot be replaced with v1</pre>
  
<p>Here, if we were to replace <code>s0</code> with <code>v1</code>, the behaviour would change, as the value of <code>v1</code> changes after <code>s0</code> is assigned, but before it is consumed. To avoid these complications, we only use copy propagation to inline variables that are assigned <em>exactly once</em>.</p>

<p>One way to enforce this might be to track all stores to non-stack variables, i.e., we know that <code>v1</code> is assigned <code>v10</code> at #0, and also <code>v11</code> at #2. Since there is more than one assignment to v1, we cannot perform copy propagation.</p>

<p>Our original example, however, has no such complications, and we end up with a nice, concise result:</p>

<pre class="programlisting">
  v2 = v0 + v1
  return v2</pre>

<h2>Stack analysis</h2>

<p>In the previous example, we could guarantee which value was on top of the stack at any given point, and so we could name <code>s0</code>, <code>s1</code>, and so on.</p>

<p>So far, dealing with variables has been pretty straightforward because weâ€™ve only explored methods with a single code path. In a real world application, most methods arenâ€™t going to be so accommodating. Each time you add a loop or condition to a method, you increase the number of possible paths a caller might take. Consider this modified version of our earlier example:</p>

<pre class="programlisting">
  public static int plus(boolean t, int a, int b) {
    int c = t ? a : b;
    return c;
  }</pre>
  
<p>Now we have control flow to complicate things; if we try to perform the same assignments as before, we run into a problem.</p>

<table>
	<tr>
		<th></th>
		<th>Bytecode</th>
		<th>Stack Variables</th>
	</tr>
	<tr>
		<td><code>0</code></td>
		<td><code>iload_0</code></td>
		<td><code>s0 = v0</code></td>
	</tr>
	<tr>
		<td><code>1</code></td>
		<td><code>ifeq 8</code></td>
		<td><code>if (s0 == 0) goto #8</code></td>
	</tr>
	<tr>
		<td><code>4</code></td>
		<td><code>iload_1</code></td>
		<td><code>s1 = v1</code></td>
	</tr>
	<tr>
		<td><code>5</code></td>
		<td><code>goto 9</code></td>
		<td><code>goto #9</code></td>
	</tr>
	<tr>
		<td><code>8</code></td>
		<td><code>iload_2</code></td>
		<td><code>s2 = v2</code></td>
	</tr>
	<tr>
		<td><code>9</code></td>
		<td><code>istore_3</code></td>
		<td><code>v3 = {s1,s2}</code></td>
	</tr>
	<tr>
		<td><code>10</code></td>
		<td><code>iload_3</code></td>
		<td><code>s4 = v3</code></td>
	</tr>
	<tr>
		<td><code>11</code></td>
		<td><code>ireturn</code></td>
		<td><code>return s4</code></td>
	</tr>
</table>

<p>We need to be a little smarter about how we assign our stack identifiers. Itâ€™s no longer sufficient to consider each instruction on its own; we need to keep track of how the stack looks at any given location, because there are multiple paths we might take to get there.</p>

<p>When we examine <code>#9</code>, we see that <code>istore_3</code> pops a single value, but that value has two sources: it might have originated at either <code>#5</code> or <code>#8</code>. The value at the top of the stack at <code>#9</code> might be either <code>s1</code> or <code>s2</code>, depending on whether we came from <code>#5</code> or <code>#8</code>, respectively. Therefore, we need to consider these to be the same variable â€“ we merge them, and all references to <code>s1</code> or <code>s2</code> become references to the unambiguous variable <code>s{1,2}</code>. After this â€˜relabellingâ€™, we can safely perform copy propagation.</p>

<table>
	<tr>
		<th></th>
		<th>After Relabelling</th>
		<th>After Copy Propagation</th>
	</tr>
	<tr>
		<td><code>0</code></td>
		<td><code>s0 = v0</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>1</code></td>
		<td><code>if (s0 == 0) goto #8</code></td>
		<td><code>if (v0 == 0) goto #8</code></td>
	</tr>
	<tr>
		<td><code>4</code></td>
		<td><code>s{1,2} = v1</code></td>
		<td><code>s{1,2} = v1</code></td>
	</tr>
	<tr>
		<td><code>5</code></td>
		<td><code>goto #9</code></td>
		<td><code>goto #9</code></td>
	</tr>
	<tr>
		<td><code>8</code></td>
		<td><code>s{1,2} = :v2</code></td>
		<td><code>s{1,2} = v2</code></td>
	</tr>
	<tr>
		<td><code>9</code></td>
		<td><code>v3 = s{1,2}</code></td>
		<td><code>v3 = s{1,2}</code></td>
	</tr>
	<tr>
		<td><code>10</code></td>
		<td><code>s4 = v3</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>11</code></td>
		<td><code>return s4</code></td>
		<td><code>return v3</code></td>
	</tr>
</table>

<p>Notice the conditional branch at <code>#1</code>: if the value of <code>s0</code> is zero, we jump to the <code>else</code> block; otherwise, we continue along the current path. Itâ€™s interesting to note that the test condition is negated when compared to the original source. Weâ€™ve now covered enough to dive into...</p>

<h2>Conditional expressions</h2>

<p>At this point, we can determine that our code could be modelled with a ternary operator (<code>?:</code>): we have a conditional, with each branch having a single assignment to the same stack variable <code>s{1,2}</code>, after which the two paths converge. Once weâ€™ve identified this pattern, we can roll the ternary up straight away.</p>

<table>
	<tr>
		<th></th>
		<th>After Copy Prop.</th>
		<th>Collapse Ternary</th>
	</tr>
	<tr>
		<td><code>0</code></td>
		<td></td>
		<td></td>
	</tr>
	<tr>
		<td><code>1</code></td>
		<td><code>if (v0 == 0) goto #8</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>4</code></td>
		<td><code>s{1,2} = v1</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>5</code></td>
		<td><code>goto 9</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>8</code></td>
		<td><code>s{1,2} = v2</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>9</code></td>
		<td><code>v3 = s{1,2}</code></td>
		<td><code>v3 = v0 != 0 ? v1 : v2</code></td>
	</tr>
	<tr>
		<td><code>10</code></td>
		<td></td>
		<td></td>
	</tr>
	<tr>
		<td><code>11</code></td>
		<td><code>return v3</code></td>
		<td><code>return v3</code></td>
	</tr>
</table>

<p>Note that, as part of the transformation, we negated the condition at <code>#9</code>. It turns out that <code>javac</code> is fairly predictable in how it negates conditions, so we can more closely match the original source if we flip those conditions back.</p>

<h2>Short-Circuit Operators ('&amp;&amp;' and '||')</h2>

<pre class="programlisting">
  public static boolean fn(boolean a,
                           boolean b, boolean c){
    return a || b &amp;&amp; c;
  }</pre>
  
<p>How could anything be simpler? The bytecode is, unfortunately, a bit of a pain...</p>

<table>
	<tr>
		<th></th>
		<th>Bytecode</th>
		<th>Stack Variables</th>
		<th>After Copy Propagation</th>
	</tr>
	<tr>
		<td><code>0</code></td>
		<td><code>iload_0</code></td>
		<td><code>s0 = v0</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>1</code></td>
		<td><code>ifne #12</code></td>
		<td><code>if (s0 != 0) goto #12</code></td>
		<td><code>if (v0 != 0) goto #12</code></td>
	</tr>
	<tr>
		<td><code>4</code></td>
		<td><code>iload_1</code></td>
		<td><code>s1 = v1</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>5</code></td>
		<td><code>ifeq #16</code></td>
		<td><code>if (s1 == 0) goto #16</code></td>
		<td><code>if (v1 == 0) goto #16</code></td>
	</tr>
	<tr>
		<td><code>8</code></td>
		<td><code>iload_2</code></td>
		<td><code>s2 = v2</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>9</code></td>
		<td><code>ifeq #16</code></td>
		<td><code>if (s2 == 0) goto #16</code></td>
		<td><code>if (v2 == 0) goto #16</code></td>
	</tr>
	<tr>
		<td><code>12</code></td>
		<td><code>iconst_1</code></td>
		<td><code>s3 = 1</code></td>
		<td><code>s{3,4} = 1</code></td>
	</tr>
	<tr>
		<td><code>13</code></td>
		<td><code>goto #17</code></td>
		<td><code>goto 17</code></td>
		<td><code>goto 17</code></td>
	</tr>
	<tr>
		<td><code>16</code></td>
		<td><code>iconst_0</code></td>
		<td><code>s4 = 0</code></td>
		<td><code>s{3,4} = 0</code></td>
	</tr>
	<tr>
		<td><code>17</code></td>
		<td><code>ireturn</code></td>
		<td><code>return s{3,4}</code></td>
		<td><code>return s{3,4}</code></td>
	</tr>
</table>

<p>The <code>ireturn</code> instruction at <code>#17</code> could return either <code>s3</code> or <code>s4</code>, depending on what path is taken. We alias them as described above, and then we perform copy propagation to eliminate <code>s0</code>, <code>s1</code>, and <code>s2</code>.</p>

<p>We are left with three consecutive conditionals at <code>#1</code>, <code>#5</code> and <code>#7</code>. As we mentioned earlier, conditional branches either jump or fall through to the next instruction.</p>

<p>The bytecode above contains sequences of conditional branches that follow specific and very useful patterns:</p>

<table>
	<tr>
		<th>Conditional Conjunction (&amp;&amp;)</th>
		<th>Conditional Disjunction (||)</th>
	</tr>
	<tr>
		<td>
<pre class="programlisting">
T1: 

  if (c1) goto L1
  if (c2) goto L2
L1:
  ...</pre>
<p>Becomes:</p>
<pre class="programlisting">
  if (!c1 &amp;&amp; c2) goto L2
L2:
  ...</pre>
		</td>
		<td>
<pre class="programlisting">
T1:
  if (c1) goto L2
  if (c2) goto L2
L1:
  ...</pre>
<p>Becomes:</p>
<pre class="programlisting">
  if (c1 || c2) goto L2
L1:
  ...</pre>
		</td>
	</tr>
</table>

<p>If we consider neighbouring pairs of conditionals above, <code>#1</code>...<code>#5</code> do not conform to either of these patterns, but <code>#5</code>...<code>#9</code> is a conditional disjunction (<code>||</code>), so we apply the appropriate transformation:</p>

<pre class="programlisting">
    1:  if (v0 != 0) goto #12
   5:  if (v1 == 0 || v2 == 0) goto #16
  12:  s{3,4} = 1
  13:  goto #17
  16:  s{3,4} = 0
  17:  return s{3,4}</pre>
  
<p>Note that every transform we apply might create opportunities to perform <em>additional</em> transforms. In this case, applying the <code>||</code> transform restructured our conditions, and now <code>#1</code>...<code>#5</code> conform to the <code>&amp;&amp;</code> pattern! Thus, we can further simplify the method by combining these lines into a single conditional branch:</p>

<pre class="programlisting">
   1:  if (v0 == 0 &amp;&amp; (v1 == 0 || v2 == 0)) goto #16
  12:  s{3,4} = 1
  13:  goto #17
  16:  s{3,4} = 0
  17:  return s{3,4}</pre>
  
<p>Does this look familiar? It should: this bytecode now conforms to the ternary (<code>?:</code>) operator pattern we covered earlier. We can reduce <code>#1</code>...<code>#16</code> to a single expression, then use copy propagation to inline <code>s{3,4}</code> into the <code>return</code> statement at <code>#17</code>:</p>

<pre class="programlisting">
  return (v0 == 0 &amp;&amp; (v1 == 0 || v2 == 0)) ? 0 : 1;</pre>

<p>Using the method descriptor and local variable type tables described earlier, we can infer all the types necessary to reduce this expression to:</p>

<pre class="programlisting">
  return (v0 == false &amp;&amp; (
          v1 == false || v2 == false))
          ? false : true;</pre>
		  
<p>Well, this is certainly more concise than our original decompilation, but itâ€™s still pretty jarring. Letâ€™s see what we can do about that. We can start by collapsing comparisons like <code>x == true</code> and <code>x == false</code> to <code>x</code> and <code>!x</code>, respectively. We can also eliminate the ternary operator by reducing <code>x ? false : true</code> as the simple expression <code>!x</code>.</p>

<pre class="programlisting">
  return !(!v0 &amp;&amp; (!v1 || !v2));</pre>

<p>Better, but itâ€™s still a bit of a handful. If you remember your high school discrete maths, you can see that De Morganâ€™s theorem can be applied here:</p>

<pre class="programlisting">
  !(a || b) --&gt; (!a) &amp;&amp; (!b)
  !(a &amp;&amp; b) --&gt; (!a) || (!b)</pre>
  
<p>And thus:</p>

<pre class="programlisting">
  return ! ( !v0 &amp;&amp; ( !v1 || !v2 ) )</pre>

<p>becomes:</p>

<pre class="programlisting">
  return ( v0 || !(!v1 || !v2 ) )</pre>
  
<p>and eventually:</p>

<pre class="programlisting">
  return ( v0 || (v1 &amp;&amp; v2) )</pre>
  
<p>Hurrah!</p>


<h2>Dealing with method calls</h2>

<p>Weâ€™ve already seen what it looks like for a method to be called: arguments â€˜arriveâ€™ in the locals array. To <em>call</em> a method, arguments must be pushed onto the stack, following a <code>this</code> pointer in the case of instance methods). Calling a method in bytecode is as youâ€™d expect:</p>

<pre class="programlisting">
  push arg_0
  push arg_1
  invokevirtual METHODREF
</pre>

<p>Weâ€™ve specified <code>invokevirtual</code> above, which is the instruction used to invoke most instance methods. The JVM actually has a handful of instructions used for method calls, each with different semantics:</p>

<ol>
  <li><code>invokeinterface</code> invokes interface methods.</li>
  <li><code>invokevirtual</code> invokes instance methods using virtual semantics, i.e., the call is dispatched to the proper override based on the runtime type of the target.</li>
  <li><code>invokespecial</code> invokes a specific instance method (without virtual semantics); it is most commonly used for invoking constructors, but is also used for calls like <code>super.method()</code>.</li>
  <li><code>invokestatic</code> invokes static methods.</li>
  <li><code>invokedynamic</code> is the least common (in Java), and it uses a â€˜bootstrapâ€™ method to invoke a custom call site binder. It was created to improve support for dynamic languages, and has been used in Java 8 to implement lambdas.</li>
</ol>

<p>The important detail for a decompiler writer is that the classâ€™s <em>constant pool</em> [<a href="#[JVMc]">JVMc</a>] contains details for any method called, including the number and type of its arguments, as well as its return type. Recording this information in the caller class allows the runtime to verify that the intended method exists at runtime, and that it conforms to the expected signature. If the target method is in third party code, and its signature changes, then any code that attempts to call the old version will throw an error (as opposed to producing undefined behaviour).</p>

<p>Going back to our example above, the presence of the <code>invokevirtual</code> opcode tells us that the target method is an <em>instance method</em>, and therefore requires a <code>this</code> pointer as an implicit first argument. The <code>METHODREF</code> in the constant pool tells us that the method has one formal parameter, so we know we need to pop an argument off of the stack in addition to the target instance pointer. We can then rewrite the code as:</p>

<pre class="programlisting">
  arg_0.METHODREF(arg_1)</pre>
  
<p>Of course, bytecode isnâ€™t always so friendly; thereâ€™s no requirement that stack arguments be pushed neatly onto the stack, one after the other. If, for example, one of the arguments was a ternary expression, then there would be intermediate load, store, and branch instructions that will need to be transformed independently. Obfuscators might rewrite methods into a particularly convoluted sequence of instructions. A good decompiler will need to be flexible enough to handle many interesting edge cases that are beyond the scope of this article.</p>


<h2>There has to be more to it than this...</h2>

<p>So far, weâ€™ve limited ourselves to analysing a single sequence of code, beginning with a list of simple instructions and applying transformations that produce more familiar, higher-level constructs. If you are thinking that this seems a little too simplistic, then you are correct. Java is a highly structured language with concepts like scopes and blocks, as well as more complicated control flow mechanisms. In order to deal with constructs like <code>if</code>/<code>else</code> blocks and loops, we need to perform a more rigorous analysis of the code, paying special attention to the various paths that might be taken. This is called <em>control flow analysis</em>.</p>

<p>We begin by breaking down our code into contiguous blocks that are guaranteed to execute from beginning to end. These are called <em>basic blocks</em>, and we construct them by splitting up our instruction list along places where we might jump to another block, as well as any instructions which might be jump targets themselves.</p>

<p>We then build up a control flow graph (CFG) by creating edges between the blocks to represent all possible branches. Note that these edges may not be <em>explicit</em> branches; blocks containing instructions that might throw exceptions will be connected to their respective exception handlers. We wonâ€™t go into detail about constructing CFGs, but some high level knowledge is required to understand how we use these graphs to detect code constructs like loops.</p>

<p>Figure 1 shows some examples of control flow graphs.</p>

<table class="sidebartable">
	<tr>
		<td><img src=" http://accu.org/content/images/journals/ol119/BenfieldStrobel/BenfieldStrobel-1.png" /></td>
	</tr>
	<tr>
		<td class="title">Figure 1</td>
	</tr>
</table>

<p>The aspects of control flow graphs that we are most interested in are domination relationships:</p>

<ul>
  <li>A node <code>D</code> is said to <em>dominate</em> another node <code>N</code> if all paths to <code>N</code> pass through <code>D</code>. All nodes dominate themselves; if <code>D</code> and <code>N</code> are differentnodes, then <code>D</code> is said to <em>strictly dominate</em> <code>N</code>.</li>
  <li>If <code>D</code> strictly dominates <code>N</code> and does not strictly dominate any <em>other</em> node that strictly dominates <code>N</code>, then <code>D</code> can be said to <em>immediately dominate</em> <code>N</code>.</li>
  <li>The <em>dominator tree</em> is a tree of nodes in which each nodeâ€™s children are the nodes it immediately dominates.</li>
  <li>The <em>dominance frontier </em>of <code>D</code> is the set of all nodes <code>N</code> such that <code>D</code> dominates an immediate predecessor of <code>N</code> but does not strictly dominate <code>N</code>. In other words, it is the set of nodes where <code>D</code>â€™s dominance ends.</li>
</ul>


<h2>Basic loops and control flow</h2>

<p>Consider the following Java method:</p>

<pre class="programlisting">
  public static void fn(int n) {
    for (int i = 0; i &lt; n; ++i) {
      System.out.println(i);
    }
  }</pre>
  
<p>and its disassembly:</p>

<pre class="programlisting">
 0:  iconst_0
 1:  istore_1
 2:  iload_1
 3:  iload_0
 4:  if_icmpge 20
 7:  getstatic #2  // System.out:PrintStream
10:  iload_1
11:  invokevirtual #3  // PrintStream.println:(I)V
14:  iinc 1, 1
17:  goto 2
20:  return
</pre>

<p>Letâ€™s apply what we discussed above to convert this into a more readable form, first by introducing stack variables, then performing copy propagation.</p>

<table>
    <tr>
        <th></th>
		<th>Bytecode</th>
		<th>Stack Variables</th>
		<th>After Copy Propagation</th>
	</tr>
	<tr>
		<td><code>0</code>&lt;/CELL&gt;
		<td><code>iconst_0</code></td>
		<td><code>s0 = 0</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>1</code></td>
		<td><code>istore_1</code></td>
		<td><code>v1 = s0</code></td>
		<td><code>v1 = 0</code></td>
	</tr>
	<tr>
		<td><code>2</code></td>
		<td><code>iload_1</code></td>
		<td><code>s2 = v1</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>3</code></td>
		<td><code>iload_0</code></td>
		<td><code>s3 = v0</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>4</code></td>
		<td><code>if_icmpge 20</code></td>
		<td><code>if (s2 &gt;= s3) goto 20</code></td>
		<td><code>if (v1 &gt;= v0) goto 20</code></td>
	</tr>
	<tr>
		<td><code>7</code></td>
		<td><code>getstatic #2</code></td>
		<td><code>s4 = System.out</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>10</code></td>
		<td><code>iload_1</code></td>
		<td><code>s5 = v1</code></td>
		<td></td>
	</tr>
	<tr>
		<td><code>11</code></td>
		<td><code>invokevirtual #3</code></td>
		<td><code>s4.println(s5)</code></td>
		<td><code>System.out.println(v1)</code></td>
	</tr>
	<tr>
		<td><code>14</code></td>
		<td><code>iinc 1, 1</code></td>
		<td><code>v1 = v1 + 1</code></td>
		<td><code>v1 = v1 + 1</code></td>
	</tr>
	<tr>
		<td><code>17</code></td>
		<td><code>goto 2</code></td>
		<td><code>goto 2</code></td>
		<td><code>goto 4</code></td>
	</tr>
	<tr>
		<td><code>20</code></td>
		<td><code>return</code></td>
		<td><code>return</code></td>
		<td><code>return</code></td>
	</tr>
</table>

<p>Notice how the conditional branch at <code>#4</code> and the <code>goto</code> at <code>#17</code> create a logical loop. We can see this more easily by looking at a control flow graph (Figure 2).</p>

<table class="sidebartable">
	<tr>
		<td><img src=" http://accu.org/content/images/journals/ol119/BenfieldStrobel/BenfieldStrobel-2.svg" /></td>
	</tr>
	<tr>
		<td class="title">Figure 2</td>
	</tr>
</table>

<p>From the graph, itâ€™s obvious that we have a neat cycle, with an edge from the <code>goto</code> back to a conditional branch. In this case, the conditional branch is referred to as a <em>loop header</em>, which can be defined as a dominator with a loop-forming backward edge. A loop header dominates all nodes within the loopâ€™s body.</p>

<p>We can determine whether a condition is a loop header by looking for a loop-forming back edge, but how do we do that? A simple solution is to test whether the condition node is in its own dominance frontier. Once we know we have a loop header, we have to figure out which nodes to pull into the loop body; we can do that by finding all nodes dominated by the header. In pseudocode, our algorithm would look something like Listing 2.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
findDominatedNodes(header)
    q := new Queue()
    r := new Set()

    q.enqueue(header)

    while (not q.empty())
        n := q.dequeue()

        if (header.dominates(n))
            r.add(n)

            for (s in n.successors())
                q.enqueue(n)

    return r
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<p>Once weâ€™ve figured out the loop body, we can transform our code into a loop. Keep in mind that our loop header may be a conditional jump <em>out</em> of the loop, in which case we need to negate the condition.</p>

<pre class="programlisting">
  v1 = 0
  while (v1 &lt; v0) {
    System.out.println(v1)
    v1 = v1 + 1
  }
  return</pre>
  
<p>And voilÃ , we have a simple pre-condition loop! Most loops, including <code>while</code>, <code>for</code> and <code>for-each</code>, compile down to the same basic pattern, which we detect as a simple <code>while</code> loop. Thereâ€™s no way to know for sure what kind of loop the programmer originally wrote, but <code>for</code> and <code>
for-each</code> follow pretty specific patterns that we can look for. We wonâ€™t go into the details, but if you look at the <code>while</code> loop above, you can see how the original <code>for</code> loopâ€™s initializer (<code>v1 = 0</code>) precedes the loop, and its iterator (<code>v1 = v1 + 1</code>) has been inserted at the end of the loop body. Weâ€™ll leave it as an exercise to think about when and how one might transform <code>while</code> loops into <code>for</code> or <code>for-each</code>. Itâ€™s also interesting to think about how we might have to adjust our logic to detect <em>post-conditional</em> (<code>do</code>/<code>while</code>) loops.</p>

<p>We can apply a similar technique to decompile <code>if</code>/<code>else</code> statements. The bytecode pattern is pretty straightforward:</p>

<pre class="programlisting">
  begin:
    iftrue(!condition) goto #else
    // 'if' block begins here
    ...
    goto #end
  else:
    // 'else' block begins here
    ...   
  end:
    // end of 'if/else'</pre>
	
<p>Here, we use <code>iftrue</code> as a pseudo-instruction to represent any conditional branch: test a condition, and if it passes, then branch; otherwise, continue. We know that the <code>if</code> block starts at the instruction following the condition, and the <code>else</code> block starts at the conditionâ€™s jump target. Finding the contents of those blocks is as simple as finding the nodes dominated by those starting nodes, which we can do using the same algorithm we described above.</p>

<p>Weâ€™ve now covered basic control flow mechanisms, and while there are others (e.g., exception handlers and subroutines), those are beyond the scope of this introductory article.</p>

<h2>Wrap-up</h2>

<p>Writing a decompiler is no simple task, and the experience easily translates into a bookâ€™s worth of material â€“ perhaps even a series of books! Obviously, we could not cover everything in a single article, and you probably wouldnâ€™t want to read it if weâ€™d tried. We hope that by touching on the most common constructs â€“ logical operators, conditionals, and basic control flow â€“ we have given you an interesting glimpse into the world of decompiler development.</p>

<p>Now go write your own! :)</p>


<h2>References</h2>

<p>This article originally appeared as a Blog post in the Java Advent Calendar, and is released under the Creative Commons 3.0 Attribution license.</p>

<p>(In general, just read the JVM spec end to end!)</p>

<p class="bibliomixed"><a id="[JVMa]"></a>[JVMa]  JVM Specification Â§2.6.2 â€“ <a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.6.2">http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.6.2</a></p>

<p class="bibliomixed"><a id="[JVMb]"></a>[JVMb]  JVM Specification Â§4.7.14 â€“ <a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.14">http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.14</a></p>

<p class="bibliomixed"><a id="[JVMc]"></a>[JVMc]  JVM Specification Â§4.4 â€“ <a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4">http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4</a></p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
