forked from Cactus-proj/RE-for-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
\subsection{De Morgan's laws and decompilation} | ||
|
||
Sometimes a compiler's optimizer can use De Morgan's laws to make code shorter/faster. | ||
|
||
For example, this: | ||
|
||
\begin{lstlisting}[style=customc] | ||
void f(int a, int b, int c, int d) | ||
{ | ||
if (a>0 && b>0) | ||
printf ("both a and b are positive\n"); | ||
else if (c>0 && d>0) | ||
printf ("both c and d are positive\n"); | ||
else | ||
printf ("something else\n"); | ||
}; | ||
\end{lstlisting} | ||
|
||
... looks pretty innocent, when compiled by optimizing GCC 5.4.0 x64: | ||
|
||
\begin{lstlisting}[style=customasmx86] | ||
; \verb|int __fastcall f(int a, int b, int c, int d)| | ||
public f | ||
f proc near | ||
test edi, edi | ||
jle short loc_8 | ||
test esi, esi | ||
jg short loc_30 | ||
|
||
loc_8: | ||
test edx, edx | ||
jle short loc_20 | ||
test ecx, ecx | ||
jle short loc_20 | ||
mov edi, offset s ; "both c and d are positive" | ||
jmp puts | ||
|
||
loc_20: | ||
mov edi, offset aSomethingElse ; "something else" | ||
jmp puts | ||
|
||
loc_30: | ||
mov edi, offset aAAndBPositive ; "both a and b are positive" | ||
|
||
loc_35: | ||
jmp puts | ||
f endp | ||
\end{lstlisting} | ||
|
||
... also looks innocent, but Hex-Rays 2.2.0 cannot clearly see that both AND operations were actually used in the source code: | ||
|
||
\begin{lstlisting}[style=customc] | ||
int __fastcall f(int a, int b, int c, int d) | ||
{ | ||
int result; | ||
|
||
if ( a > 0 && b > 0 ) | ||
{ | ||
result = puts("both a and b are positive"); | ||
} | ||
else if ( c <= 0 || d <= 0 ) | ||
{ | ||
result = puts("something else"); | ||
} | ||
else | ||
{ | ||
result = puts("both c and d are positive"); | ||
} | ||
return result; | ||
} | ||
\end{lstlisting} | ||
|
||
The \verb|c <= 0 || d <= 0| expression is inversion of \verb|c>0 && d>0| since | ||
$\overline{A \cup B} = \overline{A} \cap \overline{B}$ and | ||
$\overline{A \cap B} = \overline{A} \cup \overline{B}$, | ||
in other words, | ||
\verb~!(cond1 || cond2) == !cond1 && !cond2~ and \verb~!(cond1 && cond2) == !cond1 || !cond2~. | ||
|
||
These rules are worth to be kept in mind, since this compiler optimization is used heavily almost everywhere. | ||
|
||
Sometimes it's good idea to invert a condition, in order to understand a code better. | ||
This is a piece of a real code decompiled by Hex-Rays: | ||
|
||
\begin{lstlisting}[style=customc] | ||
for (int i=0; i<12; i++) | ||
{ | ||
if (v1[i-12] != 0.0 || v1[i] != 0.0) | ||
{ | ||
v108=min(v108, (float)v0[i*24 -2]); | ||
v113=max(v113, (float)v0[i*24]); | ||
}; | ||
} | ||
\end{lstlisting} | ||
|
||
... it can be rewritten like: | ||
|
||
\begin{lstlisting}[style=customc] | ||
for (int i=0; i<12; i++) | ||
{ | ||
if (v1[i-12] == 0.0 && v1[i] == 0.0) | ||
continue; | ||
|
||
v108=min(v108, (float)v0[i*24 -2]); | ||
v113=max(v113, (float)v0[i*24]); | ||
} | ||
\end{lstlisting} | ||
|
||
Which is better? I don't know yet, but for better understanding, it's great to take a look on both. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters