DecompilerAI
Research

How DecEvalSE tests decompilers, and what it found in Ghidra

A decompiler turns machine code back into C. Whether that C is right is hard to tell by reading it: it can look plausible and still compute something else. DecEvalSE, our differential-testing framework for C decompilers, checks it by behaviour instead. It compiles real C repositories, asks the decompiler for each function, generates a test harness that compares every function with its decompilation, and runs it. When the two disagree, there is a concrete input that shows it.

This post walks through the pipeline, then shows what it found when we pointed it at Ghidra.

How DecEvalSE works

DecEvalSE full pipeline diagram
Step 1
Compilation
DecEvalSE compiles programs from user provided C repositories.
Step 2
Decompilation
DecEvalSE challenges your decompiler to emit source code for functions.
Step 3
Test Generation
DecEvalSE generates a test harness to assert equivalence between a function and it's decompilation.
Step 4
Test Execution
DecEvalSE executes the tests and checks for assertion failures.
Step 5
Statistics Collection
DecEvalSE collects metrics like compilability, test pass, KLEE coverage, GCOV coverage and readability scores.
Step 1

Compilation

User provides compilable C repositories with build artifacts (configure scripts, make files, etc.).

While compiling, the framework intercepts compiler invocations and injects additional flags so that it can produce several artifacts in one pipeline: native executables for decompilation, GCOV-instrumented binaries for coverage analysis, and LLVM bitcode required for symbolic execution with KLEE.

shell
Step 2

Decompilation

For custom evaluations, the decompiled candidate is provided through a customer-implemented PredictionAPI.py bridge. DecEvalSE launches this Python process, sends a challenge request including the target function name and program path over IPC, and receives the predicted high-level function through a well-defined interface.

In practice, PredictionAPI.py is the integration layer between the customer’s decompiler and the evaluation framework. The template intentionally leaves both decompiler initialization and the actual prediction routine to the customer.

Customers may also expose their decompiler as a hosted online service, so the evaluation can interact with it through the interface without requiring access to the underlying source code, allowing proprietary implementations to remain protected.

PredictionAPI.py sketch
def predict(functionName, programPath):  # IMPLEMENT THIS PART
    ...

def main():
    # Load decompiler here  # IMPLEMENT THIS PART
    ...
    # Challenge Request Protocol # WE PROVIDE THIS PART
    ...
            
Step 3

Test Generation

Once the candidate function is available, we place it beside the original implementation and build an equivalence harness. The harness feeds both functions the same inputs and checks whether their observable results match.

Original vs Decompilation
int f_original(int num) {
    int i;
    if (num == 0)
        return -1;

    int count = -1;
    for (i = 0; i < 32; i++) {
        if ((num >> i) & 1)
            count = i;
    }
    return count;
}

int f_decompiled(int num) {
    if (num == 0)
        return -1;

    int position = -1;
    for (int i = 0; i < sizeof(int) * 8; i++) {
        if (num & (1 << i))
            position = i;
    }
    return position;
}
Step 4

Test Execution

In a normal unit test you choose concrete values such as x = 0 or x = 37. In symbolic execution, the input becomes symbolic. Instead of checking one run, the engine explores many feasible paths and accumulates path constraints as branches are taken.

If the assertion can be violated on some reachable path, the solver tries to synthesize a real concrete input that triggers the mismatch.

Test Suite
#include <assert.h>
#include "klee/klee.h"

int main() {
    int x;
    klee_make_symbolic(&x, sizeof(x), "x");

    int a = f_original(x);
    int b = f_decompiled(x);

    klee_assert(a == b);
    return 0;
}
Step 5

Statistics Collection

The most useful output of symbolic execution is the counterexample. When the assertion is violated, the solver returns a concrete input that reproduces the discrepancy. That means we can show exactly where the original and decompiled implementations disagree.

Another useful output contains readability scores using Levenshtein Edit Distance expressed as a similarity metric, as well as CorpusBleu-, CodeBert-, CodeBleu-, CrystalBleu-Scores. In the future we plan to include LLM-assisted readability evaluations, variable name recover, struct name recovery, and many more as useful metric on the readability part.

A last useful output colntains the KLEE and GCOV coverage metrics, to give the evaluator an idea to which extent the symbolic execution engine explored the equivalence check per function.

Metric
Compilability
Is the decompilation compilable?
Metric
Pass
Is the decompilation functionally equivalent?
Evidence
Coverage
How much meaningful execution space the evaluation actually explored.
Evidence
Counterexamples
Concrete failing inputs you can rerun and inspect.
Additional Information

Supported Function Signatures

DecEvalSE supports substantially more than primitive toy signatures.

Repository code rarely consists only of functions such as int f(int). In practice, functions operate on buffers, pointer chains, nested structs, static helpers, standard input, and observable memory effects rather than only through a direct scalar return value.

DecEvalSE therefore supports a broader class of signatures, including primitive parameters, arbitrary pointer depth, nested structs and structs containing supported pointer fields, void-returning functions, static functions, and functions whose behavior is exposed through return values, mutated arguments, standard input, or captured output.

This makes the evaluation applicable to more realistic repository code, while still acknowledging current limits: floating-point types are excluded, record return types are currently not evaluated symbolically, and highly environment-dependent behavior or large nullable pointer graphs can be more expensive to model precisely.

Examples of supported signatures
/* primitive parameters + primitive return */
int add(int a, int b);

/* pointer parameter */
int sum_array(const int *arr, int n);

/* output through memory */
void compute_stats(const int *arr, int n, int *min_out, int *max_out);

/* arbitrary pointer depth */
int read_through_triple(int ***value);

/* pointer selection / clamping */
int **pick_triple_slot(int ***slots, int count, int index);

/* struct by value */
int process_point(struct Point p);

/* nested struct by pointer */
int config_score(const struct Config *cfg);

/* struct with pointer fields */
int bundle_score(struct Bundle bundle);

/* struct with double-pointer field */
int boxholder_score(struct BoxHolder holder, int delta);

/* in-place mutation via struct pointer */
void normalize_point(struct Point *p);

/* void return with observable memory effects */
void swap_ints(int *a, int *b);

/* static helper */
static int helper_hash(const char *s);

/* reads from standard input */
int read_token(char *buf, int max_len);

/* buffer transformation */
void xor_buf(uint8_t *dst, const uint8_t *src, int n);

/* pointer + struct output */
void update_node(struct Node *node, int *status_out);

/* array of pointers */
int count_tokens(const char **tokens, int count);

/* recursive pointer-shaped input */
int score_chain(struct Node *head);

/* string / buffer processing */
size_t trim_copy(char *dst, const char *src, size_t max_len);

What it found in Ghidra

We ran DecEvalSE against stock Ghidra (12.1.2) on nine open-source C projects, among them FFmpeg, OpenSSL, lz4, musl, pcre2 and QuickJS, built with gcc at -O0 and decompiled mostly without debug information. The findings came out of two kinds of evidence:

  • Behaviour. A decompiled function, compiled again and put in place of the original, behaves differently: an equivalence assertion fails, or the code crashes, for example by reading past a buffer.
  • Consistency. Results the pipeline would otherwise throw away: decompiled functions that do not compile again, values the decompiled code reads but never assigns, and recovered signatures that contradict the program's own debug information. Checking these covers every decompiled function in minutes, and it found as many bugs as the behavioural runs.

Each candidate was reduced to a small reproducer, traced to its cause in Ghidra's source and reported to the Ghidra team together with a fix:

IssueWhat goes wrongFound byFix
#9445A narrow load through an inferred pointer is printed as a wider memory access: the recompiled C reads up to 7 bytes past the buffer.behaviour#9446
#9470A pass-through wrapper (return callee(...)) is recovered as void, even when the callee's prototype is known.behaviour#9471
#9472A stack object passed by address is cut off at its first field access, so callees write past the modelled object.behaviour#9473
#9474A 64-bit return value is dropped when the caller uses only its high 32 bits; the code then reads a variable that is never assigned.consistency#9475
#9476The DWARF importer locks a void f(void) signature onto functions written in assembly, so every call loses its arguments and return value.consistency#9477
#9478x86-64 SysV parameters passed on the stack are dropped when later parameters use registers.consistency#9479
#9480x86-64 variadic functions lose their register-save stores before the va_arg reads are linked, so the code computes from uninitialised locals.consistency#9481

The fix for #9480 also resolves the older open issue #8500. Our runs also ran into two known open issues again: #4170 (a variadic function recovered as non-variadic) and #6813 (a stack array recovered as separate adjacent locals). All reports were open and awaiting triage when this was written. Further findings are being prepared and will be added here once they are reported.

Reports and fixes: github.com/nokitoino