Mitigating fix reversal: Counterfactual constraints for function-level vulnerability detection

Yun Bian, Shihao Li, Haiquan Wang, Yi Chen, Zhao Yang, Zhe CuiarXiv 2026

TL;DR

Current AI-based vulnerability detectors suffer from "fix reversal," often flagging a fixed piece of code as more dangerous than its vulnerable original. This paper identifies a "length shortcut"—where models lazily associate longer functions with higher risk—and introduces CFVul, a framework that uses counterfactual constraints to force models to focus on actual code logic rather than function length.

What Problem Does This Paper Address?

Deep learning models for vulnerability detection (ML4VD) are increasingly popular, but they are prone to learning "shortcuts"—spurious correlations in training data that don't generalize. The authors highlight a particularly jarring failure: fix reversal.

In a fix reversal, a detector assigns a higher vulnerability probability to a fixed function than to its vulnerable counterpart. As shown in Figure 1, a fix for an out-of-bounds read in a ZIP parser actually increases the model's alarm score, simply because the fix added more lines of code.

Figure 1: Fix reversal in SerenityOS commit 4317db7 from the PrimeVul validation set; code is abridged. (a) Fixed version. (b) A control that inserts comments at the fix location in the vulnerable version, matching the length of the added fix code. The right-hand panels show CodeBERT’s vulnerability probabilities. Dashed lines mark the alarm threshold that maximises validation F1.

The authors discover that this happens because, in many datasets, vulnerable functions are statistically longer than non-vulnerable ones. Models like CodeBERT learn this "length shortcut" instead of understanding the underlying security flaws. Furthermore, standard models often truncate long functions, meaning if a fix happens at the end of a long file, the model might not even "see" the change.

Methods

The authors propose CFVul, which mitigates these issues through three main components:

  1. Sliding-Window Encoding: Instead of truncating code at 510 tokens, CFVul uses a sliding window to cover the entire function. This ensures that fixes located deep within long functions are actually processed by the model.
  2. Content Constraint (Training): The authors introduce a pairwise loss function. During training, the model is shown both the vulnerable and fixed versions of a function. It is forced to evaluate them at a common length (the average of the two). This forces the model to distinguish between the two based solely on code content, not length.
  3. Length Constraint (Inference): To prevent the model from relying on length at test time, CFVul uses a technique inspired by causal inference (backdoor adjustment). It calculates the vulnerability score by averaging the model's output over a set of fixed "reference lengths," effectively neutralizing the influence of the function's actual length.
Figure 3: The CFVul architecture, showing how content constraints are applied during training and length standardization is used during inference.

Datasets

The study evaluates CFVul across four major function-level vulnerability datasets:

  • PrimeVul: The primary dataset, known for its strict deduplication and high-quality fix pairs.
  • BigVul, ReVeal, and MegaVul: Used to demonstrate the generalizability of the approach across different data conditions.

Key Insights and Ideas

  • The Length Shortcut is Real: The authors performed "code-transformation" experiments (Figure 5) where they added meaningless comments to vulnerable code to match the length of a fixed version. They found that simply making the code longer—without changing its logic—was enough to make models like CodeBERT rank it as more vulnerable.
  • Fixes are Usually Additive: In 81.1% of training fix pairs, the fixed version is longer than the vulnerable one (Figure 2). This creates a massive bias where "longer = safer" (the fix) conflicts with the model's learned "longer = dangerous" shortcut.
  • Standard Metrics are Deceptive: Models can achieve high AUROC scores by simply being "length detectors." The authors propose PairRank—the percentage of fix pairs where the vulnerable version is correctly assigned a higher risk than the fixed version—as a more robust metric for security applications.
Figure 5: Experiments showing that adding comments (length padding) significantly increases the vulnerability probability assigned by standard models.

Key Results

  • Improved Ranking: On the PrimeVul dataset, CFVul achieved a PairRank of 59.4%, significantly outperforming standard CodeBERT (41.9%) and other baselines which all fell below the 50% "random guessing" threshold for fix pairs.
  • Handling Long Fixes: For fixes that increased function length by more than 15%, CodeBERT’s PairRank was a dismal 25.8%. CFVul nearly doubled this to 48.4%.
  • Model Agnostic: The authors successfully applied CFVul to multiple architectures, including UniXcoder, GraphCodeBERT, and CodeT5, showing consistent improvements in mitigating fix reversal across the board.

Limitations and Future Work

  • Language Scope: The evaluation was limited to C/C++ functions. Future work is needed to see if these length shortcuts persist in higher-level languages like Python or Java.
  • Residual Dependence: While CFVul significantly weakens the length shortcut, it doesn't eliminate it entirely. Some length information may still be "leaking" through the code representation itself rather than just the explicit length input.
  • Contextual Vulnerabilities: The paper focuses on function-level detection. However, many vulnerabilities depend on the calling context outside a single function, which neither standard models nor CFVul currently capture.

Summary updated

BibTeX
@misc{bian2026mitigating,
  author = {Yun Bian and Shihao Li and Haiquan Wang and Yi Chen and Zhao Yang and Zhe Cui},
  title = {{Mitigating fix reversal: Counterfactual constraints for function-level vulnerability detection}},
  year = {2026},
  eprint = {2611.24489},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.24489},
}

Loading related content...

Figure