ARQ: Agentic CodeQL Query Refinement for C/C++ Vulnerability Detection

Chunyi Wang, Yunfei Ke, Junfeng Yang, Yun-Yun Tsai, Penghui LiarXiv 2026

TL;DR

ARQ is an agentic framework that automatically fixes false positives and false negatives in C/C++ CodeQL queries. By using Large Language Models (LLMs) to synthesize pairs of test programs and validating them via real-world compiler sanitizers, ARQ bridges the gap between static analysis and dynamic execution. It improves vulnerability detection by up to 119.8% while maintaining a precision of 98% or higher, even uncovering previously unknown bugs in foundational libraries like zlib and libpng.

What Problem Does This Paper Address?

Static analysis tools like GitHub's CodeQL are essential for securing large codebases. They rely on manually written queries in a domain-specific language (DSL) to scan source code for known vulnerability patterns. However, these queries are notoriously difficult to perfect, frequently suffering from two flaws:

  1. False Positives (FPs): Flagging safe code as vulnerable, which wastes developer time.
  2. False Negatives (FNs): Missing actual security bugs, leaving software exposed.

While recent research has used LLMs to refine static analysis queries for web applications (like Java), these methods rely heavily on taint tracking (following untrusted user input to a dangerous sink). This approach fails for C/C++ system software, where critical vulnerabilities—such as double-frees or buffer overflows—depend on memory lifetimes and pointer aliasing rather than data flow. Writing and fixing C/C++ queries requires deep domain expertise, and official query bugs often languish unresolved on GitHub for months or years.

Methods

ARQ introduces an automated, execution-driven refinement loop that treats program execution as the ultimate ground truth. Instead of relying on static code patterns or manual templates, ARQ uses an LLM agent to proactively discover and repair query weaknesses.

Overview of the ARQ refinement loop for DoubleFree.ql

The workflow operates in five distinct steps:

  1. Input Query: The framework takes an existing, imperfect CodeQL query ($Q$).
  2. Contrastive Witness Generation (Gen): The LLM generates a pair of near-identical C/C++ programs. One is designed to be a true positive (vulnerable) and the other a false negative or false positive, differing only in subtle control flow structures.
  3. Differential Oracle Validation (Oracle vs Analyze): ARQ compiles and runs the programs using compiler sanitizers (like Clang's AddressSanitizer). The sanitizer output acts as the ground-truth Oracle. If the CodeQL Analyze verdict disagrees with the runtime Oracle, a genuine query failure (witness) is confirmed.
  4. Query Improvement (Improve): Guided by this concrete witness, the LLM modifies the CodeQL query to fix the logic gap.
  5. Regression Testing: The candidate query is re-run against all previously validated test programs. If it introduces any regressions, it is rolled back or refined further.

Datasets

The authors evaluated ARQ using two prominent C/C++ benchmarks:

  • Juliet v1.3: A widely recognized, hand-crafted dataset from NIST containing complex, labeled vulnerability patterns. The authors focused specifically on the Linux-targeted samples.
  • FormAI v2: A large corpus of C programs automatically generated and verified using bounded model checkers.

Key Insights and Ideas

  • Grounding LLMs in Execution: Purely text-based LLM agents often hallucinate weaknesses or fixes when trying to reason about complex static analysis rules. By forcing the agent to interact with a runtime environment (compiler sanitizers), ARQ eliminates hallucination; the agent cannot argue with a program that actually crashes at runtime.
  • Contrastive Pairing Prevents Overfitting: Asking an LLM to generate a single test case often leads to trivial programs. By forcing it to generate pairs of programs that differ only in control flow, the LLM isolates the exact semantic boundary the query is failing to capture.
  • CodeQL Queries are Conservative: Baseline CodeQL queries are heavily optimized to avoid false positives, meaning their primary weaknesses are false negatives. ARQ successfully coaxes LLMs into safely relaxing overly restrictive query constraints without triggering a flood of false alarms.

Key Results

The authors evaluated ARQ across 12 official CodeQL queries using three state-of-the-art LLMs: Gemini-3.5-flash, Claude-Sonnet-4.6, and GPT-5.4.

Main performance results on Juliet and FormAI datasets
  • Vulnerability Detection Boost: On the FormAI dataset, ARQ-refined queries achieved massive gains in true positive detections over vanilla CodeQL: +119.8% with Gemini-3.5, +82.7% with Sonnet-4.6, and +77.7% with GPT-5.4.
  • High Precision Maintained: Despite significantly expanding the queries' recall, precision remained exceptionally high, staying at or above 98.0% across all models and datasets.
  • Real-World Impact:
  • ARQ automatically generated working fixes for three unresolved GitHub issues in the official CodeQL repository that had been open for up to 27 months.
  • When deployed against real-world open-source projects, ARQ's refined queries uncovered two previously unknown bugs in zlib (present since 2017) and libpng (present since 2025).

An ablation study confirmed that removing the execution-grounded Oracle caused the system to perform worse than vanilla CodeQL, underscoring the necessity of dynamic verification.

Limitations and Future Work

  • Coarse Benchmark Labeling: Existing datasets are labeled by broad CWE (Common Weakness Enumeration) types rather than specific CodeQL query targets, making traditional recall metrics difficult to compute accurately.
  • Sanitizer Dependency: ARQ's core mechanism relies on a runtime signal. It is inherently limited to vulnerability classes that can be triggered and caught by dynamic sanitizers (e.g., memory safety errors, data races).
  • Future Directions: The authors note that this execution-guided approach is generalizable. Future work could extend ARQ to other languages with strong sanitizer support (like Go or C#) and apply it to other query-based static analysis engines beyond CodeQL.

Summary updated

BibTeX
@misc{wang2026arq,
  author = {Chunyi Wang and Yunfei Ke and Junfeng Yang and Yun-Yun Tsai and Penghui Li},
  title = {{ARQ: Agentic CodeQL Query Refinement for C/C++ Vulnerability Detection}},
  year = {2026},
  eprint = {2608.20637},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2608.20637},
}
Figure