r/computerscience • u/Same4254 • 23d ago
Discussion On the topic of equivalent programs and new decompilation projects
*** This post is more about the theory of program equivalence than the results. In fact, what I want to know is if I am off-base with how everyone is *ignoring* the actual computer science of analyzing functional equivalence. If this is against the rules, please let me know. I've never posted before, and idk which reddit thread this is appropriate for. My goal is a conversation about programming languages and (de)compilers.
So obviously everyone knows about this wave of everyone decompiling software. However, these are not actual de-compilations, no? What people seem to be doing is taking the incomplete pseudo-code of Ghidra and asking these models to convert the pseudo-code into working source code. (and into Rust of all things...? Which is hardly like the original constructs of the C/C++ language that the games were originally written in). The models are getting good enough to where they can sort of make something happen.
However, the problem here is that they are not actually generating functionally equivalent programs. More specifically, there is no metric for the distance of the output program with respect to the original. You're just banking off of the pre-training of the model to (hopefully) output something decent, and then throw it in a loop.
* two things to note. Yes some people loop until they get binary equivalence. That IS a verfication. But this just seems.... silly. There's no broader statement of building an equivalence class of programs. It's brute force and randomness. The other is yes, people get fancy with the testing infrastructure. But for rendering engines, it's very hard to test the output LOOKS correct, and is the same as the original. So while this can help, I don't see how this fundamentally solves the flaw of a lack of 1:1 verification and matching behavior to the original program.
For example, I tried this with Knights of the Old Republic. Yeah, you can convince this thing to load the assets and put into into the level (after a few back and forwards with the model). But then the cutscenes don't load properly, textures are backwards, doors rotate 90 degrees rather than opening. So you go back to the model and tell it what's wrong and it (maybe) corrects. You do this over and over until you have some slice of the game working in your own codebase.
My point here is that we have the original logic. We have the answer (the x86 instructions). We know what the functional behavior is. Ghidra just doesn't output working C/C++ code, and people are asking the language models to repair it, and hope for the best. Why are we doing this? Isn't this extremely silly?
What if we had a decompiler that could output C/C++ code that worked and produced functionally equivalent (but not necessarily assembly equivalent instructions)? Wouldn't that solve most of the problem here? By what metric could we measure how similar two programs are? Is there a better way to produce a binary-equivalent program if we had a complete forward model of the underlying compiler? If we did, is that an NP-Complete graph problem? (as apposed to random permutations until the binary matches).
What research has been done to actually demonstrate functional equivalence of non-trivial programs? I understand that formal methods can put a dent into some of the more "trivial" math-like functions. But this seems insufficient for larger, more complex programs. There is much more state with respect to heap allocations, the stack, "defined behavior" that the forward compiler assumes, etc...
To speak more broadly, I am interested in the question of constructing an equivalence class of "functionally equivalent" programs. Even if it is a subset of programs in the total possible space of programs, there must be some construction where that is possible.
Someone will definitely bring up the halting problem here. To that I would say that if you can explain the termination condition, and not necessarily the decision of whether it will happen, that is sufficient. So if you had two programs which both terminate if hypothesis xyz is true, but you can't decide whether that will happen (because you don't know the truth of the hypothesis), I think that is OK. Since you can still make an assessment of how both programs behave, and that they do the same thing.
10
u/gnosnivek 22d ago edited 22d ago
Well, almost. I'm actually going to bring up Rice's Theorem instead, which is a Javelin missile to the Halting theorem's bazooka.
Rice's theorem tells you that any non-trivial semantic property of a program is undecidable:
If your question satisfies all three points, Rice's Theorem says there is no way to answer it correctly in all cases.
Let's say we fix a "target program" P, which is what you want your other programs to be equivalent to. We then take an arbitrary program A and ask "for all possible inputs i, do A and P output the same thing?".
This is a yes-no question. It only relates inputs and outputs of A (for all possible inputs i, does A(i) = P(i)?), so it is semantic. And it is clearly non-trivial, since there are programs that do compute the same thing as P and programs that compute different things from P.
And so, Rice's Theorem applies, and this question is undecidable.
Note that we don't even need to worry about heap allocation, or stacks, or program layout (like you brought up earlier). Just the question of "do these two programs output the same thing" is already undecidable.
Note that this doesn't mean the problem of generating equivalent programs is impossible, just that, given two arbitrary programs, you can't tell if they're functionally equivalent. However, if you e.g. start with program P1, and apply a set of carefully chosen transformations to it, you can wind up with a program P2 which you can show is equivalent to P1. This is actually done already in most optimizing compilers at an IR level: you apply transformations to the IR which result in a different program, but one which you can show must be equivalent (and also which is hopefully faster).
This is not something I study, but there are programs like egg out there, which basically try to do what you suggested earlier, using graph rewriting rules to generate equivalent programs at the source level. I don't know how hard the resulting problem winds up being though, in terms of trying to find all possible rewrites that are (provably) equivalent.
EDIT: Minor wording fixes.
2
u/TheAceOfHearts 23d ago
Some projects do run both versions side-by-side and they compare the output to check for correctness, that's good enough for most use-cases.
Another useful tool when reverse engineering something is taking a memory snapshot of the real thing in a known / desired state and comparing it with your own implementation. If both components are given the same initial state and they can move through some set of steps to end up with the same end state, those are probably equivalent. You can keep doing that for sub-sections of the program until you build up the functionality you care about.
Modern LLMs are already really good at exploring the program space if you can frame the problem in terms of a verification loop. Eventually it'll converge. With current tools it's just a matter of time and money.
1
u/daily_lined_overseer 23d ago
The Knights of the Old Republic example is the whole thing in a nutshell. Doors rotating instead of opening is exactly what you get when nobody's checking behavior against the original, you're just hoping the model's prior fills the gaps. The reason people do it is boring: it's cheap and it sort of works, whereas equivalence checking is a research problem that eats years.
On your broader question, the honest answer is that nobody has a general method for non-trivial programs, and that's not for lack of trying. You can get equivalence for restricted classes (straight-line code, limited loops, decidable fragments) but heap and stack state blow up the state space fast, and the verification tools that exist need heavy annotation. The compiler-model angle is interesting because a known forward compiler gives you a much tighter search space, but it's still a hard combinatorial problem in general.
The halting point is fair, but the hard part isn't termination, it's that "same behavior" needs a definition before you can measure distance. For games that definition is basically "does the player notice," which is why everyone ends up eyeballing textures and cutscenes.