r/AskComputerScience • • Jul 24 '20

Why can't decompilers perfectly reconstruct source code?

I always thought that there was a one to one translation between machine code and high-level languages, but after looking up decompilers, they don't usually do it perfectly.

28 Upvotes

18 comments sorted by

51

u/TransientVoltage409 Jul 24 '20

Going from high-level language to machine/assembly is generally a lossy, one-to-many-possibilities operation. Take a simple while-loop, you can probably find five or six ways to implement it in MIPS or other assembly language, and that's not even considering optimizations.

A decompiler can produce high-level code that duplicates the semantics of the machine code, but reconstructing the intent of the original is impossible. Too much of that is literally lost in translation. Consider a simple optimization such as loop unrolling - a fixed loop such as for i = 1 to 3 might be unrolled into three copies of the loop body. Without context, how could a decompiler know that the original was a loop, and not three copies of the same code snippet?

And this doesn't even touch on one of the biggest contextual hints in source code, which is identifiers. The names of methods and variables carry a lot of meaning for us, but are meaningless to the compiler except in their uniqueness, and are often lost in compilation. Sometimes they are recorded in an attached symbol table for e.g. debugging, but that's no guarantee, we often strip symbols to save space.

You wouldn't think of computer science as involving a lot of artistry, but there are niches where talent, insight, experience, and intuition play a large part. Reconstructing source from binary is one of those niches.

31

u/Putnam3145 Jul 24 '20

I always thought that there was a one to one translation between machine code and high-level languages

Well, let's make a simple C program and see what it compiles to.

int main() {
    int a = 5;
    a = a++ + ++a;
    return a;
}

Okay, not exactly simple. Postfix and prefix ++ are very, ah, subtle, after all. This compiles to...

main:
    mov     eax, 12
    ret

Ah.

In short, this compiles straight to... 12, because the compiler knows that everything done in main() ends up just returning 12. It compiles to the same thing as just return 12, so how's one to know that that's not what was written originally?

9

u/xwoter Jul 24 '20

This is kinda funny because the function is technically undefined behavior

7

u/Putnam3145 Jul 24 '20

Which means there's 0 chance a decompiler will get it right!

6

u/orsikbattlehammer Jul 24 '20

Why is the behavior undefined?

5

u/taricorp Jul 24 '20

C standard says so:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

More details on Stack Overflow: https://stackoverflow.com/q/949433

2

u/orsikbattlehammer Jul 25 '20

I guess I didn’t ask the question right haha. I mean why does the C standard have this rule.

1

u/groumpf Jul 25 '20

To give more freedom to compiler writers (who can now freely evaluate "out of order" between sequence points).

9

u/[deleted] Jul 24 '20

thought that there as a one to one translation between machine code and high-level languages

There’s not. If there were, you might as well be writing machine code in the first place.

There’s a lot of different ways to express the same instructions. Compilers have their own implementations and optimizations that can generate machine code different to another compiler. Language standards only define what the result needs to be, not how the result needs to be generated.

While decompilers exist, for the reasons above, it will very rarely be a perfect reconstruction.

3

u/pancakeQueue Jul 24 '20

Compilers will optimize code if they see areas that could have improvements. They also will strip out comments and other stuff that’s only their for the human to see. Lastly they could rename values to simplistic names cause the computer doesn’t care what things are called. So when you decompile then end result it can still look pretty alien to look at with no comments to help.

3

u/Bottled_Void Jul 24 '20

I always thought that there was a one to one translation between machine code and high-level languages

There is not. So I guess we're done here?

I mean if you look up the amount of code generated for x = y / z; in your average C compiler for something that doesn't support native floating point operations you'd see that pretty clearly.

If you look up how a compiler works, you can see where you're losing information that can't be replaced. And at the same time increasing the number of operations in order to implement the language's behaviour.

3

u/HasFiveVowels Jul 24 '20

The short answer: compilation is a lossy process. That alone will tell you it's not reversible. In order for compilation to be reversible, the binary would need to be as large as the source code.

2

u/jhaluska Jul 24 '20

I always thought that there was a one to one translation between machine code and high-level languages

Nope, it's a many to one. You can get source code that compiles the same, but it won't be as easily modifiable as the original code.

1

u/khedoros Jul 24 '20

The central limitation is that a decompiler is trying to make its best guess for a lot of the information that was dropped out of the original source code during the compilation process. A lot of information isn't necessary for the program to run. A few categories come to mind.

For example, human-readable names are going to become addresses and offsets. A lot of type information gets checked by the compiler for consistency, but then discarded in later steps (the check has been done, and the program is compiled; why waste the cycles at runtime?). And a lot of higher-level constructs are just translated to simpler instructions. For example, loops, if statements, switch/case tables are usually translated to a series of equality tests and jumps/gotos. C++ is somewhat distinctive with its vtables and such, but it's also the kind of thing that you could fake with C and likely end up with a very similar binary.

Files are lumped together; it's often difficult to decide which source file some given function came from. The compiler might rearrange some code, inlining some, reordering others, maybe even unrolling small loops to save the evaluation+jump steps.

1

u/claytonkb Jul 24 '20

The easiest way to see why is to consider comments -- unless you compile with source-level debug (which means distributing the source with the binary), you will lose all comments, no matter what other information can be gleaned from the binary. Comments (which, ironically, are not "source code" at all!) are often the most important part of an IP in the sense that they explain the purpose/rationale of code that would otherwise be completely non-obvious.

If you don't compile with internal symbols (and production code will not keep internal symbols because it just bloats the binary for nothing in return), then there is no way for the decompiler to know what things are named. A function name is just a mnemonic that is useful for human programmers. Once compiled, all internal symbols that are not susceptible to runtime type-ID (RTTID) will be discarded and reduced to a relative offset in the compiled binary.

1

u/_jetrun Jul 25 '20

> I always thought that there was a one to one translation between machine code and high-level languages

You answered your own question. It isn't a one-to-one translation between machine code and high-level source code. Information is lost during compilation.

1

u/[deleted] Jul 25 '20

If you look at it mathematically, the act of compiling source code into assembly language is a many to one problem. That is, there can be many source codes that perform the same action (because different people wrote them); but, when converted to assembly they are all the same.

Now when you try to get back the original source code from the assembly code it is a one to many problem. These kind of problems have a name - inverse problems. It is not possible to get the exact inverse. Some approximations has to be made. That is why they don't always do them perfectly.

1

u/Eulerious Jul 25 '20

I always thought that there was a one to one translation between machine code and high-level languages

Why do you think that?