Manuscript Offprint

arXiv:2608.22275

CodeMechanic: Bug-Property-Guided Program Mitigation

Han Zheng, Rafaila Galanopoulou, Ilia Shumailov, Tianqi Fan, Aiden Hall, Dominik Maier, Mathias Payer

Han Zheng 1^{1}

han.zheng@epfl.ch

Rafaila Galanopoulou 1^{1}

rafaila.galanopoulou@epfl.ch

Ilia Shumailov 2^{2}

ilia.shumailov@gmail.com

Tianqi Fan 3^{3}

tqfan@google.com

Aiden Hall 3^{3}

aidenhall@google.com

Dominik Maier 3^{3}

dmnk@google.com

Mathias Payer 1^{1}

mathias.payer@nebelwelt.net

1^{1}EPFL

2^{2}Meta

3^{3}Google

Abstract

Automated testing discovers vulnerabilities faster than developers can investigate and repair them, leaving an interval in which known memory corruptions remain exploitable. End-to-end LLM repair agents can shorten this interval, but they synthesize open-ended code changes and commonly validate them only by replaying a proof of concept (PoC). This weak oracle accepts patches that silence the observed crash by changing unrelated behavior, making unintended deployment risky.

We present CODEMECHANIC, a bug-property-guided system for generating constrained mitigations for spatial memory corruption. Instead of asking an LLM to generate a permanent repair, CODEMECHANIC reconstructs the violated memory-safety property from the crash, validates the dereferenced pointer and its buffer range, and inserts a local fail-stop guard before the dangerous access. The guard terminates execution when the boundary check fails. The resulting mitigation deliberately trades availability for security: it can convert potential remote code execution into controlled termination while developers investigate the root cause and prepare a permanent repair. CODEMECHANIC combines a two-dimensional static and dynamic context extractor with in-prompt debugging knowledge and stepwise validation to limit the effect of LLM errors. On 101 real-world ARVO bugs, the first attempt of CODEMECHANIC produces 47.6% more plausible patches (i.e., patches that pass PoC-replay validation) than the best baseline while using 91% fewer tokens. Manual audit further shows that CODEMECHANIC produces 3.4×−4.3×3.4 \times -4.3 \times more patches semantically equivalent to developer-written repairs.

1 Introduction

Modern software systems add functionalities and thereby increase complexity (Dullien, 2018). However, this growing complexity exceeds human comprehension limits, developers often misinterpret abstractions and struggle to reason across diverse interfaces, ultimately leading to the emergence of software vulnerabilities (Chrome, 2025). On Bugcrowd, a third-party bug bounty platform, hundreds of thousands of vulnerabilities were reported and acknowledged by vendors (bugcrowd, 2025), in addition to those identified and addressed internally. These vulnerabilities are not theoretical (Chrome, 2025). In contrast, they can be, and have been, exploited by skilled attackers (Silvanovich, 2026; Marczak et al., 2023; Times, 2022).

Proactively discovering and remediating vulnerabilities before they are weaponized is therefore essential to ensuring user safety.

Over the past decade, fuzzers and static analyzers have discovered an astonishing amount of vulnerabilities (Zalewski, 2013; Fioraldi et al., 2020; libfuzzer, 2023; GitHub, 2024). However, this rapid growth introduced a new challenge: the volume of bugs now exceeds the capacity of developers to triage and resolve them (Keromytis, 2022). For example, the Linux kernel currently has around 1,500 unresolved bugs reported by syzbot (Google, 2024). Among these, memory corruption issues are the most exploited by attackers in real-world scenarios (Stone, 2022). This growing backlog highlights the urgent need for Automated Vulnerability Repair (AVR) techniques to reduce manual effort and help developers keep pace, especially with memory corruption vulnerabilities. It also motivates rapid mitigations that reduce exposure while a permanent repair is developed.

The rapid advancement of Large Language Models (LLMs) (Comanici et al., 2025; OpenAI, 2025; Liu et al., 2024) has created new opportunities for automated program repair. A growing number of AVR agents ask an LLM to localize a defect and generate a permanent repair (Tufano et al., 2019; Chen et al., 2019; Feng et al., 2020; Huang et al., 2025; Xia & Zhang, 2022; 2024; Zhang et al., 2024; Huang et al., 2023; Yu et al., 2025). For real-world vulnerabilities, however, an unrestricted LLM-generated repair is risky: the LLM can modify arbitrary code, while the available oracle often checks only that one PoC no longer crashes. An incorrect patch may therefore pass validation while silently corrupting functionality or introducing a new vulnerability. This mismatch makes open-ended patches difficult to deploy without a human in the loop. Despite its improvement, several key challenges limit their broader applicability:

(C1) Unbounded effects of LLM-generated patches. LLMs are probabilistic and can generate arbitrary “plausible” patches. With only a PoC-based oracle, deleting a code path, corrupting a fuzz driver, and repairing the underlying bug may all appear successful. For unattended mitigation, the patch space must instead bound the possible side effects and make failure behavior predictable.

(C2) Insufficient context for reconstructing a memory-safety property. The crash site alone rarely contains the pointer, object base, and object end needed to state the violated memory-safety condition. Without both code and runtime context, the LLM may hallucinate a boundary or overfit to the observed execution.

(C3) Dependence on expert debugging knowledge. Recovering the relevant pointer and object bounds requires deliberate use of code search, macro resolution, GDB, and sanitizer metadata. Without guidance, an LLM may invoke these tools repeatedly or misinterpret their errors.

To address these challenges, we propose the following solutions:

(S1) Bug-Property-Guided Mitigation. We constrain the output to a local guard derived from the spatial memory-safety property. The LLM identifies the dereferenced pointer and its valid buffer range, and CODEMECHANIC inserts a check such as if (!valid_pointer(p)) exit(0); immediately before the dangerous access. This is intentionally a temporary mitigation rather than a permanent repair. If the inferred condition is violated, the guard terminates execution before the memory corruption can be exploited. Restricting the patch format and location also prevents the LLM from freely modifying unrelated program logic. We call this constrained, temporary code change a mitigation patch. When the distinction from a permanent repair is clear, we use patch as shorthand.

(S2) Two-Dimensional Context Provision. To overcome the challenge of insufficient bug context, we provide the LLM with a dual perspective: a code view and a data view. In the code view, we supply macro definitions and representative examples of variable usage within the program, helping the LLM situate the bug in its broader contexts. In the data view, we expose the LLM to a live debugger session paused at the crash site. This includes collected variable values and types, along with Sanitizer metadata (Google, 2016), which enable the LLM to better comprehend the memory layout and underlying defect condition.

(S3) Embedding Human Debugging Knowledge. Finally, to address the dependence on expert tool usage, we incorporate human debugging strategies directly into the prompts. These embedded guidelines offer a structured workflow for error handling. For example, prompts regarding global-buffer-overflow bugs require the LLM to validate the buffer name provided in the ASan log. Similarly, when encountering a symbol undefined error, the prompt instructs the LLM to call find_macro_definition in order to resolve the missing symbol.

We implemented our prototype, CODEMECHANIC, and evaluated it against the state-of-the-art AVR agents, PATCHAGENT and SAN2PATCH, on real-world vulnerabilities. The results show that CODEMECHANIC generates 47.6% more plausible patches than the best competitor, while spending only 9% of the tokens. With the default five-attempt setup, CODEMECHANIC achieves 13.3% more plausible patches compared to the best competitor, with only 10% of the cost. Through manual auditing of the plausible patches, we found that CODEMECHANIC produces 240% and 325% more semantically equivalent patches (functionally identical to the developer-written repairs) compared to PATCHAGENT and SAN2PATCH respectively. This result indicates that constraining the patch can also be a strategy when developers repair the bugs. Our contributions are:

• We formulate bug-property-guided mitigation as a constrained alternative to open-ended LLM repair for spatial memory corruption.

• We introduce Bug-Property-Guided Mitigation, supported by Two-Dimensional Context Extractor and In-Prompt Human Knowledge, to recover and validate a violated boundary property before generating a local guard.

• We implement and evaluate CODEMECHANIC against two state-of-the-art AVR agents, measuring plausible-patch count, cost, semantic equivalence, and failure modes.

• We will release CODEMECHANIC to support open science upon acceptance.

References (30)

  • Coverage-based greybox fuzzing as markov chain
  • {Control-Flow} bending: On the effectiveness of {Control-Flow} integrity
  • Sequencer: Sequence-to-sequence learning for end-to-end program repair
  • CodeBERT: A Pre-Trained Model for Programming and Natural Languages
  • Afl++ combining incremental steps of fuzzing research
  • Beyond tests: Program vulnerability repair via crash constraint extraction
  • An empirical study on fine-tuning large language models of code for automated program repair
  • Template-guided program repair in the era of large language models
  • Logs in, patches out: Automated vulnerability repair via {Tree-of-Thought}{LLM} analysis
  • DeepSeek-V3 Technical Report
  • Agent that debugs: Dynamic state-guided vulnerability repair (opens arXiv in a new tab)
  • Nso group imessage zero-click exploit captured in the wild
  • Atlas of reproducible vulnerabilities for open source software (opens arXiv in a new tab)
  • {APPATCH}: Automated adaptive prompting large language models for {Real-World} software vulnerability patching
  • An analysis of patch plausibility and correctness for generate-and-validate patch generation systems
  • {AddressSanitizer}: A fast address sanity checker
  • Concolic program repair
  • Effective seed scheduling for fuzzing with graph centrality analysis
  • LLaMA: Open and Efficient Foundation Language Models
  • An empirical study on learning bug-fixing patches in the wild via neural machine translation
  • Efficient software-based fault isolation
  • Less training, more repairing please: revisiting automated program repair via zero-shot learning
  • Automated program repair via conversation: Fixing 162 out of 337 bugs for $0.42 each using chatgpt
  • Chucky: Exposing missing checks in source code for vulnerability discovery
  • Modeling and discovering vulnerabilities with code property graphs
  • Patch-agent: A practical program repair agent mimicking human expertise
  • Program vulnerability repair via inductive inference
  • Fixing security vulnerabilities with ai in oss-fuzz (opens arXiv in a new tab)
  • {FISHFUZZ}: Catch deeper bugs by throwing larger nets
  • Mendelfuzz: The return of the deterministic stage