TL;DR
This paper introduces an automated framework that leverages Large Language Models (LLMs) to synthesize executable CodeQL queries directly from historical vulnerability data. By shifting the LLM's role from scanning code to generating static analysis rules, the approach achieves a 263% increase in true positive vulnerability detections over default CodeQL while remaining highly cost-effective and protecting intellectual property.
What Problem Does This Paper Address?
Static Application Security Testing (SAST) tools like GitHub's CodeQL are industry standards for finding software bugs before deployment. CodeQL works by converting source code into a relational database and running declarative queries to trace dangerous data flows (sources to sinks). However, CodeQL is bottlenecked by human limitations: writing effective queries requires rare, dual expertise in both language-specific vulnerability semantics and CodeQL’s complex logic-based syntax. As a result, default query suites often miss over 70% of real-world vulnerabilities.
Why not just use LLMs to scan entire code repositories directly? The authors demonstrate that direct repository scanning is practically unviable. For a single real-world project, an LLM scan can take over 44 hours and cost up to $180+ per iteration using frontier models. Furthermore, uploading proprietary codebases to third-party LLM providers poses severe intellectual property and data confidentiality risks.
This paper addresses this dilemma by using LLMs to generate reusable CodeQL queries from public vulnerability reports, combining the reasoning power of LLMs with the local, private, and lightning-fast execution of traditional SAST tools.
Methods
The authors design a multi-agent pipeline that breaks down query generation into semantic analysis, guided synthesis, and iterative refinement:
- Vulnerability Assessor and Reasoner: Evaluates a Vulnerability Fix Commit (VFC) to extract core semantic characteristics and determine if the pattern can be generalized.
- Relevant Documentation Retriever: Uses Retrieval-Augmented Generation (RAG) to fetch relevant CodeQL APIs based on the vulnerability's CWE-ID, narrowing the LLM's search space.
- CodeQL Predicate Generator: Translates the unstructured patch logic into low-level CodeQL predicates (reusable logical conditions).
- CodeQL Query Generator: Composes these predicates into a structurally valid "select-from-where" CodeQL query.
- Compilability Checker & Syntax Refiner: Because LLMs initially struggle with CodeQL syntax, the framework feeds compiler error messages back to a specialized "Syntax Refiner" agent to iteratively fix bugs (up to 3 attempts).
A key architectural innovation is the hybrid strategy: pairing an expensive, high-reasoning model (like Claude Sonnet 4.5) to extract the vulnerability logic with a cheaper, syntax-proficient model (like Gemini 3 Flash Preview) acting as the dedicated syntax corrector.
Datasets
The framework was evaluated using Java vulnerabilities from the MoreFixes dataset (derived from the National Vulnerability Database). The authors narrowed their scope to the top 10 most dangerous Common Weakness Enumeration (CWE) categories that had sufficient data.
The dataset was split into:
- 80 CVE Mining Seeds: Used by the framework to learn vulnerability patterns and generate queries.
- 112 CVE Test Data points: A completely separate set of recent CVEs used to evaluate detection performance and ensure no data leakage occurred.
Key Insights and Ideas
- The Syntax Fluency Gap: Almost all state-of-the-art LLMs fail to generate compilable CodeQL queries in a single shot due to a lack of training data for the language. However, compiler-feedback loops completely flip this script, raising success rates drastically.
- The Hybrid Arbitrage: You don't need to pay frontier-model prices for the entire pipeline. Using a cheaper model as a "syntax specialist" to fix an expensive model's structural errors yields high-quality queries at a fraction of the cost.
- Query Generation vs. Direct Scanning Economics: Generating a query suite is a one-time upfront cost (e.g., <$13 total). Once generated, these queries can be run locally across thousands of repositories instantly, securely, and for free.
- Why Function-Level Deep Learning Fails: The authors benchmarked against PDBERT, a state-of-the-art deep learning model that analyzes code at the function level. PDBERT performed worse than baseline CodeQL because many real-world vulnerabilities rely on repository-level context, such as cross-function dependencies or framework annotations (e.g., Java's
@RequirePOST), which function-centric models completely ignore.
Key Results
- Massive Detection Boost: LLM-generated queries increased true positive vulnerability detections by 263% compared to default CodeQL queries (jumping from 19 to 50 detected vulnerabilities within the benchmark).
- F1-Score Improvement: The best-performing setup yielded an 82% improvement in average F1-score over the CodeQL baseline.
- The Standout Model: Moonshot AI’s Kimi K2.5 emerged as the most cost-efficient engine. It generated 96 compilable CodeQL queries for less than $13 USD while matching the raw discovery rate of models that cost six times as much.
Limitations and Future Work
- The Dead Code Problem: While the LLM-generated queries were logically sound, they frequently flagged "dead code"—inactive code paths that are never actually executed in the application. This inflated the False Discovery Rate (FDR). The authors note that future iterations must integrate reachability analysis to filter out these inactive paths.
- Cross-Language Transferability: The current study focused entirely on Java. Since the vast majority of public CVE data exists for C/C++, a promising future direction is exploring whether LLMs can mine a pattern from a C/C++ vulnerability and successfully translate it into a CodeQL query targeting Java or Python.
Summary updated