Security Code Review by LLMs: A Deep Dive into Responses

Jiaxin Yu, Peng Liang, Yujia Fu, Amjed Tahir, Mojtaba Shahin, Chong Wang, Yangxiao CaiarXiv 2024 · DOI

Introduction

Security code review is a critical yet resource-intensive phase of the software development lifecycle. While manual review is costly and prone to human error, traditional Static Analysis Security Testing (SAST) tools often produce high false-positive rates and lack a deep understanding of complex code logic. This study evaluates the potential of Large Language Models (LLMs) to bridge this gap, testing them against real-world, file-level datasets rather than simplified synthetic examples.

Methodology

The researchers implemented a multi-stage empirical study to assess LLM performance in identifying security defects, providing risk descriptions, and suggesting fixes.

Overview of the research methodology, spanning data collection to regression analysis.

The study compared seven LLMs—including GPT-4, Gemini Pro, and the reasoning-optimized DeepSeek-R1—against five industry-standard SAST tools (e.g., CodeQL, SonarQube). Five distinct prompting strategies were tested, including Chain-of-Thought (CoT) and prompts enriched with commit messages to provide intent context.

Dataset and Vulnerability Landscape

The evaluation utilized 534 code files from the OpenStack and Qt open-source projects. Unlike many previous studies that use isolated functions, this dataset provides full file context and associated human reviewer comments.

Distribution of security vulnerability types across the Python and C/C++ datasets.

As illustrated in the vulnerability distribution, the dataset covers a wide array of Common Weakness Enumerations (CWEs). Python files were heavily represented by Resource Leaks, while C/C++ files featured significant instances of Crashes, Race Conditions, and Use After Free vulnerabilities.

Key Findings

1. Superiority of Reasoning Models

DeepSeek-R1 emerged as the top performer, suggesting that models optimized for logical "thinking" processes are better suited for security analysis than those relying solely on parameter scale. LLMs significantly outperformed SAST tools; for instance, the best LLM configuration achieved a 7.26% I-Score on Python, compared to just 0.39% for CodeQL.

2. The Role of Context

Including commit messages in prompts proved vital. These messages provide the "intent" behind code changes, enabling LLMs to detect logic-based flaws—such as race conditions—that static tools typically miss. However, performance tended to degrade as file length increased, a phenomenon described as being "lost in the tokens."

3. The Hallucination Hurdle

Despite their high performance, hallucinations remain a significant issue. Approximately 20% of GPT-4’s and 36% of DeepSeek-R1’s responses contained "Context Inconsistency," where the model referenced code elements not present in the source file.

Discussion and Future Directions

The study highlights a trade-off between helpfulness and accuracy. While GPT-4 is often helpful but vague, reasoning models like DeepSeek-R1 are more precise but prone to specific hallucinations. Future work should explore Retrieval-Augmented Generation (RAG) to provide better project-specific context and voting mechanisms to mitigate the non-deterministic nature of LLM outputs.

Summary updated

BibTeX
@misc{yu2024security,
  author = {Jiaxin Yu and Peng Liang and Yujia Fu and Amjed Tahir and Mojtaba Shahin and Chong Wang and Yangxiao Cai},
  title = {{Security Code Review by LLMs: A Deep Dive into Responses}},
  year = {2024},
  eprint = {2401.16310},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2401.16310},
}
Figure