Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Use ARM32 registers in value-flow graphs (R0.5) (#436)

Afloat858037dc3d81
diff --git a/crates/disasm/src/dataflow.rs b/crates/disasm/src/dataflow.rs
index fd953039..161aaba3 100644
--- a/crates/disasm/src/dataflow.rs
+++ b/crates/disasm/src/dataflow.rs
@@ -67,9 +67,14 @@ pub fn def_use(img: &Image, prog: &Program, f: &Function, max_nodes: usize, max_
Mode::X64 if img.bin.format == Format::Pe => &["rax", "rcx", "rdx", "r8", "r9", "r10", "r11"],
Mode::X64 => &["rax", "rcx", "rdx", "rsi", "rdi", "r8", "r9", "r10", "r11"],
Mode::A64 => &["x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17"],
+ Mode::A32 | Mode::T32 => &["r0", "r1", "r2", "r3", "r12"],
_ => &["rax", "rcx", "rdx", "eax", "ecx", "edx"],
};
- let ret = if mode == Mode::A64 { "x0" } else { "rax" };
+ let ret = match mode {
+ Mode::A64 => "x0",
+ Mode::A32 | Mode::T32 => "r0",
+ _ => "rax",
+ };
let mut out = DefUse::default();
// Decode.
let mut blocks: Vec<Vec<Insn>> = Vec::new();
@@ -262,6 +267,63 @@ mod tests {
use craft_anything_testkit as tk;
use std::sync::Arc;
+ fn raw_flow(bytes: Vec<u8>, arch: craft_anything_formats::Arch, thumb: bool) -> DefUse {
+ let mut bin = craft_anything_formats::Binary::raw(arch, 0x1000, bytes.len() as u64, 0);
+ if thumb {
+ bin.entry = Some(0x1001);
+ }
+ let img = Image::from_binary(Arc::from(bytes), bin);
+ let p = Program::analyze(&img, &Limits::default(), &NoControl).unwrap();
+ let f = p.functions.get(&0x1000).unwrap();
+ assert!(f.returns && !f.truncated);
+ let flow = def_use(&img, &p, f, 5000, 10000);
+ assert!(!flow.truncated);
+ flow
+ }
+
+ #[test]
+ fn arm32_calls_define_arm_scratch_registers_and_return_r0() {
+ // Save the link register, call through r3, then restore the caller's return address.
+ let a32 = [0xe92d4010u32, 0xe12fff33, 0xe8bd8010].into_iter().flat_map(u32::to_le_bytes).collect();
+ let thumb = vec![0x10, 0xb5, 0x98, 0x47, 0x10, 0xbd];
+ for (bytes, is_thumb, call_addr, ret_addr) in [(a32, false, 0x1004, 0x1008), (thumb, true, 0x1002, 0x1004)] {
+ let flow = raw_flow(bytes, craft_anything_formats::Arch::Arm, is_thumb);
+ let call = flow.nodes.iter().find(|n| n.addr == Some(call_addr)).unwrap();
+ let ret = flow.nodes.iter().find(|n| n.addr == Some(ret_addr)).unwrap();
+ for register in ["r0", "r1", "r2", "r3", "r12"] {
+ assert!(call.defines.iter().any(|r| r == register), "{flow:?}");
+ }
+ assert!(!call.defines.iter().any(|r| matches!(r.as_str(), "rax" | "rcx" | "rdx" | "eax" | "ecx" | "edx")), "{flow:?}");
+ assert!(flow.edges.iter().any(|e| e.from == call.id && e.to == ret.id && e.register == "r0"), "{flow:?}");
+ }
+ }
+
+ #[test]
+ fn arm32_leaf_results_reach_the_return() {
+ let a32 = [0xe3a00007u32, 0xe12fff1e].into_iter().flat_map(u32::to_le_bytes).collect();
+ let thumb = vec![0x07, 0x20, 0x70, 0x47];
+ for (bytes, is_thumb, ret_addr) in [(a32, false, 0x1004), (thumb, true, 0x1002)] {
+ let flow = raw_flow(bytes, craft_anything_formats::Arch::Arm, is_thumb);
+ let value = flow.nodes.iter().find(|n| n.addr == Some(0x1000)).unwrap();
+ let ret = flow.nodes.iter().find(|n| n.addr == Some(ret_addr)).unwrap();
+ assert!(flow.edges.iter().any(|e| e.from == value.id && e.to == ret.id && e.register == "r0"), "{flow:?}");
+ }
+ }
+
+ #[test]
+ fn other_architectures_keep_their_word_result_registers() {
+ let a64 = [0xd28000e0u32, 0xd65f03c0].into_iter().flat_map(u32::to_le_bytes).collect();
+ let x64 = vec![0xb8, 7, 0, 0, 0, 0xc3];
+ for (bytes, arch, ret_addr, register) in
+ [(a64, craft_anything_formats::Arch::Aarch64, 0x1004, "x0"), (x64, craft_anything_formats::Arch::X86_64, 0x1005, "rax")]
+ {
+ let flow = raw_flow(bytes, arch, false);
+ let value = flow.nodes.iter().find(|n| n.addr == Some(0x1000)).unwrap();
+ let ret = flow.nodes.iter().find(|n| n.addr == Some(ret_addr)).unwrap();
+ assert!(flow.edges.iter().any(|e| e.from == value.id && e.to == ret.id && e.register == register), "{flow:?}");
+ }
+ }
+
#[test]
fn helper_def_use() {
let (elf, _) = tk::elf64(tk::ElfArch::X86_64, true);
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index 0b3b4bb6..bdc93fa3 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -160,6 +160,7 @@ Owner: open · Status: done (branch chore/r0.5-cleanups)
- [x] `yaxpeax-arm` 0.5.0 panicked ("register number out of range") on an A32 `STRD`/`LDRD` with
Rt = 15 (e.g. the word `0xc0c7fef3`), so one such word failed the whole analysis of a 32-bit
ARM program; the word now decodes as invalid
+- [x] ARM32 value-flow graphs use r0 for word results and ARM scratch registers for calls (#239).
- [x] Unchecked `o + 4`/`o + 8` on section and program header offsets in `formats/src/dart.rs`
(`Elf::parse`): a header table near `usize::MAX` panicked in debug builds (in release the
wrapped reads were thrown away, since the entry's first field was already out of range); an
diff --git a/docs/STATUS.md b/docs/STATUS.md
index c8320f8e..a800eb3c 100644
--- a/docs/STATUS.md
+++ b/docs/STATUS.md
@@ -14,6 +14,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi
| Engine | 298 engine tools (314 with the AI and setup layers') across native, managed, Java, Android, Apple, JS/Electron, web, firmware, packets, databases, game archives and assets; see [tools.md](tools.md) and [tool-status.md](tool-status.md). A saved result can be narrowed without running again (`core_evidence_browse` keeps the rows matching given field values, cut to chosen fields) | A functional audit that ran every tool on real inputs on Windows, plus 600+ unit and integration tests |
| Single-file apps and game files | `core_packed_files_list` and `core_packed_files_unpack` read Bun and Node.js single-file programs, .NET single-file apps, PyInstaller and Godot packs through one seam (`formats::packed`); `formats_bytecode_show` reads Python bytecode, `game_unity_code_names_show` Unity IL2CPP metadata, `formats_ai_weights_show` GGUF and safetensors headers. `app_folder_explain` says what an install folder is made with and what to run next | Unit tests with damaged files. Real files: a 279 MB Bun program, Node blobs, PyInstaller programs (Python 3.9, 3.14), .NET 8 single-file apps (macOS, Linux, Windows, compressed), Godot 3.6, 4.3 and 4.5 packs, 214 Python modules against CPython's own reader, a GGUF and a safetensors file, about 25 installed Mac apps. IL2CPP on synthetic files only |
| Program facts across builds | `native_library_calls_list` (delay-load too), `native_offered_symbols_list`, `native_publisher_show` (checks the file hash and the signature, not Windows trust), `app_builds_line_up` pairs functions across two builds and says how, `native_source_folder_write` writes pseudo-C to a folder and picks up where it stopped, `android_components_list` | Unit tests. Real PE, ELF and Mach-O files; two builds of a 276 MB program; a signed Microsoft DLL; Android components from one real APK, spot-checked |
+| ARM32 value flow | A32 and Thumb calls define ARM scratch registers, and word results flow through `r0` into returns in `native_value_flow_show` | Synthetic instruction fixtures for leaf results and indirect calls in both modes, with AArch64 and x86-64 controls; disassembler tests. This correction was not checked on real ARM binaries or devices |
| Stand-in backends | `web_standin_build` turns a HAR into `standin.json` (secrets dropped, varying path parts templated); `web_standin_start` answers from it on loopback; `web_standin_show` lists what it had no answer for. Writes replay what was recorded and don't change later reads. App maps list GraphQL and tRPC operations; `web_walkthrough_check` checks a scripted session without a browser | Unit tests, real loopback requests, and a run with curl on a hand-written HAR. No browser-made recording or real front end yet |
| Websites as targets | `web_site_open` keeps a private browser for the session and every browser tool inspects that page; `web_site_capture` records screenshots at each width (desktop, tablet and phone), rendered HTML, design tokens (colours in hex, spacing, content widths), the CSS breakpoints, the layout at each width (page width, heading sizes, each section's padding, gap, content column and items per row), page health (script and console errors, failed requests) and an accessibility outline, layout outline, assets with font files and requests, links and form outlines. In Clean room the capture keeps only what a visitor sees (no HTML, CSS rules, scripts, requests or errors), and opening a site reports no load errors | Live run on example.com; unit tests |
| Rebuild workflow (Study → Build → Check → Refine) | `crates/ai/src/rebuild/` (kinds, checks and stacks are pluggable: docs/extending.md). Three kinds: website, app, game (a project whose stack is a game engine is always a game). The request decides how to build: a language, framework, engine or platform it names wins over the kind's default stack (Rust + egui for apps, Unity for games, the original's for websites), and the build check, the recorded stack and Play follow what was built (an Electron app opens in its own Electron). Websites: Study also captures up to 4 other pages of the site; menus are opened on a computer and on a phone; the check serves the build on loopback and compares every page at 1440, 768 and 390 px (likeness = mean of layout and perceptual detail, SSIM, averaged over pages, leaving out the parts of the original that change by themselves, found by photographing it three times; the note adds text, page structure, the colours, font, text sizes and spacing the build misses, and per width a page that scrolls sideways, heading sizes and sections whose padding, items per row or content column differ, the errors, failed requests and accessibility gaps the build adds, and the opened menus and pop-ups). A stack or library named in the request (Next.js, shadcn/ui, Tailwind CSS…, `stacks.toml`) is what the builder uses; with none named, a website's stack is the builder's choice (told to match what the original is built with) and the stack it used is recorded from app/package.json. The builder leads: it studies, asks the tools follow-up questions, records what it can't find out as unknowns (`core_unknown_add`; the run reports the open ones), and tests its own build mid-turn with `rebuild_check` (the same checks, for every kind); a check with anything it couldn't compare, or any claim that fails (a website page that scrolls sideways, logs new errors, fails to load a file, lacks a landmark, or a menu that doesn't open; for games each self-test step, for Deep-mode sheets each original number in the code, for console apps the same exit and output as the original's runs), isn't good enough; apps and games by whether they build. Up to 6 refinement rounds, or until a website reaches 90% likeness. A turn cut off by a time limit, crash or dropped connection resumes (up to 3 times); builder turns may run 24 h | Unit tests for prompts, stack choice, site detection, feed folding; live runs reached Build and decompiling (below) |