Skip to content

decompilers: add a manifold backend (whole-binary; CompCert pipeline in reverse) - #53

Merged
mahaloz merged 3 commits into
Noelo-Lab:mainfrom
changliu98:feat/manifold-backend
Aug 4, 2026
Merged

mahaloz merged 3 commits into
Noelo-Lab:mainfrom
changliu98:feat/manifold-backend

Conversation

@changliu98

@changliu98 changliu98 commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a raw backend for Manifold, an open-source decompiler that runs CompCert's compilation pipeline in reverse: a binary is raised through Mach → Linear → RTL → Cminor → Csharpminor → Clight and printed as C. Manifold's v1 numbers are already on the board from an external eval-kit submission; this PR is the harness, so a maintainer can run it on the full corpus instead of only scoring a submitted zip.

decbench decompiler-build manifold          # builds the image; nothing else to install
decbench run projects/sailr/<proj>.toml -O O0 -d manifold

Per @mahaloz's request, installing manifold is now automated: docker/manifold.Dockerfile
builds it from source, and the backend falls back to that image when no native executable
resolves. A host needs Docker and nothing else. A native manifold still wins if one is
present (MANIFOLD_BIN / decompilers.toml / $PATH), so a developer can benchmark a
working tree without rebuilding an image.

It is the first backend of a shape decbench hasn't had: whole-binary, no per-function entry point. One process call decompiles the entire binary into a single translation unit, so the per-function structure decbench needs is recovered from that unit rather than requested from the tool.

Backend design

  • Invocation: one manifold <binary> <out.c> subprocess per binary, under config.binary_timeout_seconds. A timeout or a missing output file becomes a DecompilationResult with failed_functions=["all"] and timeout_occurred set, never an exception that takes down a run.
  • Splitting the translation unit: a brace-depth scanner over a copy of the text with comments and string/char literals blanked to spaces (offsets preserved, so slices still index the original). The text since the last top-level ;/} is the declarator; a braced group whose declarator ends in a parameter list is a function, while struct/union/enum definitions run on to their ;. This is why the backend does not just regex for name(...) {.
  • Addresses: FUN_<hex> is manifold's name for an entry it has no symbol for, so the address is read straight out of the name; unstripped binaries fall back to .symtab/.dynsym via pyelftools. Anything that resolves to neither is reported in failed_functions rather than guessed at.
  • No rebasing: manifold reports the ELF's own vaddrs, which is already the ELF-file space the metrics expect. Verified against ground truth below.
  • Per-function preamble: each stored snippet is prefixed with the file-scope structs and globals its body transitively references (a struct's fields can name other structs), so a function decbench stores and recompiles stands on its own — the same shape as angr's per-function artifacts.
  • Variables are deliberately left unset: manifold's C carries declared types but no stack offsets, so type_match scores it through the parse_c_variables text path (arguments by ABI position, locals by name), the footing fix(type_match): credit arguments for code-only decompilers (fixes wcomment=0) #28 established for code-only backends.
  • Config: MANIFOLD_BIN is an explicit override and wins outright (a bad value is a misconfiguration to report, not a reason to silently run some other build); otherwise binary from the decompilers config — per-version, then the tool's default section — then $PATH. MANIFOLD_LD_LIBRARY_PATH is merged into the child's LD_LIBRARY_PATH (manifold links libz3 dynamically), and MANIFOLD_THREADS caps its rayon pool so a parallel driver can keep N binaries from each grabbing every core. get_version() reports git-<short rev> of the checkout the executable sits in, since manifold has no --version.

Docker image (docker/manifold.Dockerfile)

Multi-stage: the build stage clones manifold, initializes its Ascent submodule and compiles
it; the runtime stage carries out only the stripped binary and libz3, so the image is 247 MB
rather than the multi-GB builder. Selection order mirrors R2DecDecompiler._select_path:
native > this image.

Three things worth knowing before you bump it:

  • Z3 — the z3 crate finds libz3 through pkg-config, and Ubuntu's packaged 4.8 is too old
    for the z3-sys 0.11 bindings, so a pinned upstream release is unpacked to /opt/z3 with a
    hand-written z3.pc. Z3_DIST names the glibc the release was built for and must match the
    base image, so bump it together with Z3_VERSION.
  • MANIFOLD_REF defaults to master; pass a SHA or tag to freeze a run. The image records
    what it actually built at /opt/manifold.rev and get_version() reads it back, so results
    always name the exact revision even though the ref floats.
  • Rebuild through decompiler-build, not a bare docker build. Docker keys the git clone
    layer on the command string, which does not change when a branch moves — so a plain rebuild
    after manifold gains commits reuses the cached clone and silently ships the same revision,
    in about a second, reporting no error. build_image() resolves MANIFOLD_REF to a SHA via
    git ls-remote and passes that as the build arg, so the clone layer is invalidated exactly
    when upstream moved and stays cached when it did not (the apt/Z3/rustup layers survive
    either way). If the ref cannot be resolved — offline, no git, bad ref — it falls back to
    --no-cache and says so. By hand, pass --build-arg MANIFOLD_REF=<sha> or --no-cache.
  • Build cost — ~29 min and ~8.5 GB peak rustc RSS, because manifold is one enormous
    ascent!-generated module. Worth checking your build host has the headroom.

The release profile is overridden to opt-level 3 + thin LTO via cargo's environment
interface, leaving the manifold checkout untouched — manifold's own profile is tuned for build
speed (opt-level 0), which is right for a working tree and wrong for an image built once and
run over a corpus. Measured on a quiet host, same revision both sides:

binary opt-level 0 -O3 + thin speedup emitted C
14 KB 2.2s 1.3s 1.72x identical
35 KB (cat) 8.2s 3.8s 2.16x identical
138 KB (ls) 33.8s 11.1s 3.03x identical
150 KB (du) 66.4s 19.0s 3.50x identical

Build cost is 10m28s → 28m36s, paid once. Fat LTO was measured and rejected: 0.99–1.06x
(noise) for 49m and 25.2 GB peak RSS — one giant generated module leaves whole-program merging
almost no cross-module inlining to find. It stays reachable via
--build-arg CARGO_PROFILE_RELEASE_LTO=fat. Every variant emits byte-identical C.

Site content

site/data/aggregates.json on main already renders Manifold with a display name, URL, logo, and sample_set_only, but no source file carries any of it — 24cc9a6 published the built tree only, so a re-render from content/ would drop the registry entry and start showing Manifold's sample-set row on the full-corpus presets. This PR adds the matching decompilers.toml entry (including logo = true, whose .dlogo-manifold CSS you already landed in 24cc9a6) and appends manifold to sample_set_only in site.toml. Happy to drop that second hunk if you'd rather keep it a local edit — and it should come out once a full-corpus run covers manifold.

Verification

  • pytest tests/test_manifold_backend.py: 22 passed (was 10). The new ones cover
    native-over-Docker selection, availability gating on the image alone, the docker run mount
    layout, MANIFOLD_THREADS → RAYON_NUM_THREADS, MANIFOLD_IMAGE retagging, the
    build_image() hook, and version memoization — all through a fake docker, so they run
    with neither manifold nor Docker installed. One pre-existing test was quietly
    environment-dependent (it only neutralized MANIFOLD_BIN, so it passed by accident on hosts
    that had never built the image, and failed once one existed); it now neutralizes both paths.
    Three more cover the rebuild-staleness fix: a resolvable ref is pinned as a SHA without
    forcing --no-cache, an unresolvable one forces it, and a ref that is already a SHA
    short-circuits without needing git.
  • Full suite: 377 passed, 7 skipped, 5 failed — the 5 are pre-existing test_content.py
    rendering assertions, identical on clean main in a separate worktree.
  • ruff and black --check clean on all touched files; mypy --follow-imports=skip decbench/decompilers/raw/manifold_raw.py clean. (Repo-wide ruff check decbench/ reports 21
    pre-existing findings on main; none are in files this PR touches.)
  • End-to-end with no native manifold on the host (MANIFOLD_BIN deliberately pointed at a
    nonexistent path, so the run is forced through the image):
    • decbench list-decompilers → manifold | Y | git-d9291ad
    • decbench run projects/sailr/bzip2.toml -O O0 -d manifold → completes clean; 120
      functions across 2 binaries, failed_functions empty on both
      , all three metrics scored
      (GED 50.8%, byte_match 3.3%, type_match 7.4%).
    • Both address routes exercised: FUN_<hex> on a stripped binary (add_two 0x401136,
      classify 0x40113e, main 0x401153, matching nm on the unstripped twin) and the ELF
      symbol table on bzip2's unstripped output.

Maintainer notes

  • Installing manifold is now decbench decompiler-build manifold — no Rust, mold, libclang or z3 on the host. Building it natively still needs all of those plus a --recursive clone, and MANIFOLD_LD_LIBRARY_PATH=<z3>/lib if z3 is not on the default loader path; that path is now optional.
  • It is whole-binary, so binary_timeout_seconds is the knob that matters, not a per-function budget, and a large binary can run long. On the 224-binary eval kit, 12 concurrent jobs × 10 threads each was a good balance on a 128-core box.
  • CHANGELOG: docs/agents.md says agents must not edit it, so this PR doesn't. Suggested entry under ### 2026-07-27:
    • Added a manifold decompiler backend (decbench/decompilers/raw/manifold_raw.py) plus a docker/manifold.Dockerfile, so Manifold can be installed with decbench decompiler-build manifold and run on the full corpus rather than only submitted as a sample-set result.
  • Not done: no full-corpus run and no overlay refresh — publishing full-corpus numbers still needs the usual reeval_* + finalize_results.py pass on your side.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JquMXzqYHQTS62SnP98Pg1

Manifold (https://github.com/changliu98/manifold) is an open-source
decompiler that runs CompCert's compilation pipeline in reverse: a binary
is raised through Mach -> Linear -> RTL -> Cminor -> Csharpminor -> Clight
and printed as C.

It has no per-function entry point -- one process call decompiles a whole
binary into a single translation unit -- so the backend shells out once per
binary, splits the emitted unit into per-function definitions with a
comment/string-aware scanner, and recovers each function's address from the
FUN_<hex> names manifold gives functions in a stripped binary (falling back
to the ELF symbol table when symbols survive). Each function's snippet
carries the file-scope structs and globals it transitively references, so
the per-function code decbench stores stands on its own.

Addresses need no rebasing: manifold reports the ELF's own vaddrs, which is
already the ELF-file space the metrics expect.

Variables are left unset, so type_match scores manifold through the
code-only parse_c_variables path (arguments by ABI position, locals by
name), the same footing as the LLM backends.

Point the backend at a build with MANIFOLD_BIN, or a
`[manifold] binary = ...` entry in the decompilers config; it falls back to
$PATH. Tests cover resolution, availability gating, the translation-unit
splitter, and the address mapping through a fake manifold, so they run
without the tool installed.

The site content also gains the registry entry and the sample_set_only flag
that the published v1 render already shows but that no source file carries,
so re-rendering from content/ no longer drops them.
@mahaloz

mahaloz commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator

Hey @changliu98 would you be able to add a way to install your decompiler to one of the docker files? Since I will need to run this on my server and would love that part to be automated.

Running the manifold backend previously meant building manifold by hand --
Rust, mold, libclang, libcapstone, a recursive clone, and a libz3 new enough
for z3-sys -- and pointing MANIFOLD_BIN at the result. That is a lot to ask of
a benchmark server, so `decbench decompiler-build manifold` now builds an image
that is the entire install.

docker/manifold.Dockerfile is a multi-stage build. The build stage clones
manifold, initializes its Ascent submodule, and compiles it; the runtime stage
carries out only the stripped binary and libz3, which is why the image is
247 MB rather than the multi-GB builder. Three things in it are not obvious:

- Z3. The `z3` crate finds libz3 through pkg-config and Ubuntu's packaged 4.8
  is too old for the z3-sys 0.11 bindings, so a pinned upstream release is
  unpacked to /opt/z3 with a hand-written z3.pc. Z3_DIST names the glibc the
  release was built for and has to match the base image, so bump it with
  Z3_VERSION.
- Submodules are initialized by the paths .gitmodules maps rather than with a
  bare `--init --recursive`. manifold's tree used to carry a gitlink with no
  .gitmodules entry, which aborts that command; that is fixed upstream now, but
  deriving the paths keeps older MANIFOLD_REFs buildable.
- The release profile is overridden to opt-level 3 + thin LTO through cargo's
  environment interface, leaving the manifold checkout untouched. manifold's own
  profile is tuned for build speed (opt-level 0), which is right for a working
  tree and wrong for an image built once and run over a corpus. Measured on a
  quiet host, decompilation is 1.7x faster on a 14 KB binary rising to 3.5x on
  150 KB, for a 10m28s -> 28m36s build. Fat LTO was measured too and rejected:
  0.99-1.06x, i.e. noise, for 49m and 25 GB of peak rustc RSS -- the crate is
  essentially one enormous ascent!-generated module, so whole-program merging
  has almost no cross-module inlining left to find. Every variant emits
  byte-identical C.

ManifoldDecompiler picks native over Docker, the same order R2DecDecompiler
uses: an executable from MANIFOLD_BIN / decompilers.toml / $PATH wins, so a
developer can still benchmark a working tree without rebuilding an image, and
the container is the fallback. Both paths write the same whole-program .c, so
the splitter, the address recovery and the preamble collection are shared.
build_image() gives the CLI's getattr hook something to find; MANIFOLD_IMAGE
retags; MANIFOLD_THREADS reaches the container as RAYON_NUM_THREADS. The image
records the revision it was built from at /opt/manifold.rev and get_version()
reads it back, so a dockerized run reports the same git-<rev> a native one does
even though MANIFOLD_REF tracks a branch.

get_version() is memoized and the elapsed clock now stops before it is called:
on the Docker path the probe is a container spawn, which was being charged to
every binary's reported decompile time.

Tests go 10 -> 19, still with neither manifold nor Docker installed -- a fake
docker covers image inspect, build, and both run shapes. One of them,
test_unavailable_without_binary, was quietly environment-dependent: it only
neutralized MANIFOLD_BIN, so it passed by accident on machines that had never
built the image and failed once one existed. It now neutralizes both paths.

Verified end to end with no native manifold on the host: `decbench
list-decompilers` reports manifold available at git-d9291ad, and a full
`decbench run projects/sailr/bzip2.toml -O O0 -d manifold` completes clean --
120 functions across two binaries, no failed_functions, all three metrics
scored. Addresses were checked both ways, FUN_<hex> on a stripped binary and
the ELF symbol table on bzip2's unstripped output.
@changliu98

Copy link
Copy Markdown
Contributor Author

@mahaloz done — docker/manifold.Dockerfile is in, so the install is one command:

decbench decompiler-build manifold
decbench run projects/sailr/<proj>.toml -O O0 -d manifold

Nothing else on the host: it clones and builds manifold from source (Rust + a pinned Z3),
and the runtime stage keeps only the stripped binary and libz3, so the image is 247 MB.
The backend still prefers a native manifold if one resolves and falls back to the image
otherwise — same shape as R2DecDecompiler._select_path.

Verified with MANIFOLD_BIN deliberately pointed at a nonexistent path, so everything went
through the container: list-decompilers shows manifold | Y | git-d9291ad, and a full
decbench run on bzip2 completed clean — 120 functions across 2 binaries, no
failed_functions, all three metrics scored.

Three things worth flagging for the server:

  • Build is ~29 min and peaks around 8.5 GB of rustc RSS. manifold is essentially one
    enormous ascent!-generated module, so that last compile unit is heavy. Worth checking
    headroom before it runs unattended.
  • I override the release profile to opt-level 3 + thin LTO in the image, since manifold's
    own profile is tuned for build speed. That is ~1.7x faster on a 14 KB binary rising to
    ~3.5x on 150 KB, for a 10m28s -> 28m36s build, and the emitted C is byte-identical. Fat
    LTO I measured and dropped: noise-level gains for 49 min and 25 GB.
  • It is whole-binary, so binary_timeout_seconds is the knob that matters rather than a
    per-function budget, and time grows fast with binary size.

MANIFOLD_REF defaults to master; pass a SHA or tag to pin a run. Either way the image
records what it built at /opt/manifold.rev and get_version() reports it, so results name
the exact revision. MANIFOLD_THREADS caps the container's rayon pool if you run several
binaries concurrently.

…s like it

Rebuilding to pick up new manifold commits silently did nothing. Docker keys the
`RUN git clone ... && git checkout $MANIFOLD_REF` layer on the command string,
and with MANIFOLD_REF defaulting to `master` that string never changes when the
branch moves. Measured against a manifold whose master had advanced one commit
past the image:

    remote master       : fb5b1bf
    image holds         : d9291ad
    $ docker build -f docker/manifold.Dockerfile -t decbench/manifold:latest docker/
    real 0m0.644s                      <- 29 cached layers
    rebuilt image holds : d9291ad      <- still the old revision

Nothing about that reports failure. get_version() keeps reading the revision the
image really has, so results are not mislabelled, but a maintainer who reruns
`decbench decompiler-build manifold` after an upstream fix scores the same code
as before and has no signal that they did.

build_image() now resolves MANIFOLD_REF to a commit SHA with `git ls-remote`
and passes that as the build arg. The clone layer's cache key is then the SHA,
so it is invalidated exactly when upstream moved and stays cached when it did
not -- the apt / Z3 / rustup layers above it survive either way, so a genuine
rebuild still skips ~5 minutes of setup and a no-op rebuild is still seconds.
A ref that is already a SHA passes through without touching the network.

When the ref cannot be resolved -- offline, no git, a ref that does not exist --
the build falls back to --no-cache and says so, because a slow honest build
beats a fast stale one.

A bare `docker build` still has the original trap, since there is nowhere to put
the resolution step; the Dockerfile header now says so and points at either
`--build-arg MANIFOLD_REF=<sha>` or --no-cache.

MANIFOLD_REPO is now forwarded as a build arg too, so pointing the image at a
fork no longer means editing the Dockerfile.

Three tests cover it: that a resolvable ref is pinned as a SHA and does not force
--no-cache, that an unresolvable one does, and that a SHA short-circuits without
needing git. 19 -> 22, still with neither manifold nor Docker installed.
@mahaloz
mahaloz merged commit c441175 into Noelo-Lab:main Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants