AI-Assisted Security Auditing for Large Codebases
This article explains how large language models can complement traditional SAST tools in auditing large C++ codebases. It covers semantic and cross-module analysis, evidence-based finding generation, an engineering workflow from threat modeling to remediation, false-positive governance, CI/CD integration, cloud infrastructure considerations, and the limits of fully automated vulnerability discovery.
AI-Assisted Security Auditing for Large Codebases
1. Background: The Limits of Manual Audits and Rule-Based Scanning
Security auditing for large C++ codebases has long faced two practical challenges. First, codebases are large, long-lived, and deeply interconnected, making manual review extremely expensive. Second, traditional static application security testing (SAST) tools are effective at pattern matching but often struggle with logic vulnerabilities that span multiple modules.
In a codebase containing thousands of source files and hundreds of thousands of lines of code, a developer may spend two days investigating a segmentation fault without finding its root cause. Reviewing large amounts of templates, macros, pointer arithmetic, concurrency control, and cross-file calls can quickly lead to fatigue. Security engineers can narrow the scope through call chains, data flows, and trust boundaries, but manual coverage remains limited as the codebase continues to evolve.
The basic model of traditional SAST is rule matching. Teams define dangerous patterns—such as SQL concatenation, command execution, path traversal, or unsafe deserialization—and search the codebase for similar structures. This approach is stable, explainable, and easy to integrate. However, vulnerabilities outside the rule set remain invisible. For defects that require understanding component interactions, permission boundaries, object lifecycles, or cross-module data flows, both false positives and false negatives can be significant.
Many high-severity vulnerabilities are not caused by an incorrect single line of code. Instead, they result from inconsistent assumptions across multiple modules.
2. Why Large Language Models Can “Read” Code
Large language models with long-context processing and the ability to execute extended tasks provide another approach to code security auditing. Rather than looking only for predefined patterns, they attempt to understand code semantics. A model can connect entry points, parameter handling, authorization checks, data flows, resource cleanup, exceptional paths, and final dangerous operations into a reasoning chain that resembles the analysis performed by a security engineer.
In an audit, a large language model can support several types of work:
- Cross-file understanding: Read call chains, header files, interface definitions, and configuration files to determine whether data flows from an untrusted entry point to a dangerous operation.
- Lifecycle analysis: Identify use-after-free issues, double frees, invalid references, dangling asynchronous callbacks, and related defects.
- Boundary and assumption checking: Detect conflicts in which Module A assumes that input has been validated while Module B does not actually perform the validation.
- Remediation suggestions: After locating a problem, propose a minimal fix and identify the scope of regression testing.
- Long-running autonomous tasks: Partition the audit by module, form hypotheses, collect evidence, and produce reports, reducing the need for manual file-by-file investigation.
A large language model should not replace SAST. It should complement SAST with semantic analysis. Rule-based tools are well suited for broad, low-cost baseline checks, while large language models are better suited to deep review and cross-module reasoning in high-risk areas. Combining both approaches provides a better balance between coverage and analytical depth.
3. What Public Evaluation Data Suggests
Publicly available materials have described a joint security assessment of a large open-source software project. The assessment covered approximately 6,000 C++ files over two weeks, produced 112 reports, and confirmed 22 real vulnerabilities: 14 high-severity, 7 medium-severity, and 1 low-severity issue. The first vulnerability was discovered within 20 minutes. The high-severity findings were distributed across several subsystems, including a scripting engine, a bytecode runtime, and a rendering pipeline, rather than being concentrated in one isolated area.
Several signals are worth noting:
- Discovery speed: Finding the first issue within 20 minutes suggests that, with a clear objective and sufficient context, a large language model can quickly generate useful leads.
- Finding depth: Fourteen high-severity vulnerabilities confirmed within two weeks represented roughly one-fifth of the project’s annual high-severity remediation volume when compared with the project’s historical data. This suggests that semantic auditing can provide additional value even for mature projects.
- Coverage: The vulnerabilities were distributed across multiple subsystems, suggesting that the results were not simply repeated matches against a single rule, but reflected a degree of cross-module code understanding.
- Confirmation rate: Of the 112 reports, 22 were confirmed as real vulnerabilities, producing an initial confirmation rate of approximately 20%. Because these results were generated automatically without manual pre-screening, the rate is meaningful from an engineering perspective. Human security researchers often achieve confirmation rates of 30% to 50%, so automated findings still require a verification loop.
- Collaboration signal: During the early stage of the assessment, reports were manually verified one by one. Later, reports were submitted in batches and categorized by the project team. This indirectly suggests that the quality of the earlier reports had gained acceptance.
These figures should not be interpreted as proof that AI can discover vulnerabilities without supervision. A more accurate conclusion is that, when paired with high-quality context, clear audit objectives, and a validation process, large language models can significantly improve the efficiency of identifying high-severity vulnerability candidates.
4. An Engineering Workflow
To move large-language-model-based code auditing from a demonstration to a production process, organize the work into the following six steps.
1. Asset Identification and Threat Modeling
First define the audit scope. Identify modules that process untrusted input or handle authorization, networking, files, memory management, concurrency, and serialization. Prioritize these modules using historical defects, code-change frequency, exposure, and business importance.
Without threat modeling, the model may spend context and analysis time on low-value code.
2. Context Construction
Partition the code, but do not split it only by file. Provide call graphs, data-flow summaries, interface definitions, build scripts, configuration files, and important test cases as well. For cross-module vulnerabilities, preserve the path from the entry point through processing to the dangerous operation.
Context can be supplied in stages: start with a module map, then provide candidate paths, and finally provide the relevant files.
3. Audit Prompts and Constraints
Require the model to produce an evidence chain rather than a conclusion alone. Each candidate finding should include at least:
- File and line number
- Source of untrusted input
- Data-flow path
- Trigger conditions
- Impact scope
- Confidence level
- Remediation suggestion
- Possible reasons for a false positive
The model should also be instructed to lower its confidence when it cannot prove reachability. This can reduce hallucinated findings.
4. Candidate Prioritization and Deduplication
Rank findings by severity, reachability, exploit conditions, impact, and duplication relationships. Merge multiple reports that originate from the same root cause to avoid inflating the number of findings.
High-severity candidates should receive manual review first, while low-confidence candidates can be placed on an observation list.
5. Validation Loop
Validation should include static review, sandbox reproduction, patch verification, and regression testing. Reproduction environments must be isolated; verification should never be performed directly in production.
For memory-safety issues, a minimal proof of concept and a debugger can help confirm the problem. For logic vulnerabilities, unit or integration tests should demonstrate that the relevant path is reachable.
6. Remediation and Knowledge Capture
Track confirmed vulnerabilities in the defect management system, with clear owners and remediation deadlines. After a fix is applied, rescan the same path to verify that the issue has been closed.
Record false-positive causes, confirmed patterns, and remediation templates in a team knowledge base. Use this knowledge to continuously improve prompts, partitioning strategies, and the rule-based baseline.
5. False-Positive Management and Quality Metrics
The biggest engineering challenge in large-language-model-based auditing is not whether the model can find issues, but whether those issues can be confirmed at a reasonable cost.
Common sources of false positives include:
- Missing context that makes a path unreachable
- Incorrect assumptions about framework conventions
- Exceptional branches that were overlooked
- Hallucinated code or execution paths
The following practices can help control false positives:
- Require an evidence chain: Findings without a source, path, and trigger condition should not enter the high-severity queue.
- Use minimal reproduction: Require verifiable trigger conditions or a proof-of-concept outline. Downgrade findings that cannot be reproduced.
- Isolate the sandbox: Reproduce issues in containers with restricted network, filesystem, and permission access.
- Perform manual sampling and provide feedback: Track confirmation rates by module to identify vulnerability classes for which the model is more reliable.
- Run two tracks in parallel: Run rule-based scanning and large-model auditing independently, then compare the results to reduce blind spots.
The following report structure can support later automation:
{"finding_id": "audit-0001","severity": "high","location": {"file": "src/module/parser.cpp", "line": 128},"evidence": {"source": "external request parameter","sink": "use-after-free","path": ["entry function", "parser function", "asynchronous callback"]},"reachability": "requires a specific concurrency interleaving","confidence": 0.78,"fix_hint": "extend the object lifetime or add a validity check in the callback"}
6. Integration with CI/CD and Cloud Infrastructure
Large-language-model-based auditing should be integrated into the development lifecycle rather than operated as an isolated tool. Different scan depths can be configured for commits, pull requests, and releases:
- Run lightweight rule checks at commit time.
- Run semantic audits of high-risk modules during pull requests.
- Perform a full baseline review before release.
When deploying on Baidu AI Cloud, infrastructure can be assembled by general service category:
- Use object storage for code snapshots and audit reports.
- Use a logging service to retain scan traces and manual review records.
- Use a container platform to isolate reproduction environments.
- Use monitoring and alerting to track remediation progress for high-severity defects.
- Use key management to protect access credentials.
The key principles are least privilege, code de-identification, and traceable audit logs. Source code and vulnerability details should not circulate through uncontrolled environments.
7. Risks and Boundaries
Large-language-model-based code auditing is not a system for automatically fixing every vulnerability. The following boundaries require attention:
- Prompt injection: Comments, test data, and documentation may contain content intended to manipulate the model. The audit process should require the model to base its output only on verifiable code facts.
- Data compliance: Source code may contain sensitive information. Access control and de-identification are required during upload, partitioning, and caching.
- Reproducibility: Changes in the model version, prompts, or context partitions can affect results. Scan configurations should be recorded.
- Human responsibility: Confirmation of high-severity vulnerabilities, patch integration, and release decisions still require human ownership.
- Supply-chain security: The auditing tools themselves must be included in dependency management and permission reviews.
8. Best Practices Checklist
- Start with high-risk modules instead of attempting a full-codebase scan immediately.
- Run rule-based scanning and large-model auditing in parallel, and cross-check the results.
- Require an evidence chain, reachability assessment, and confidence level for every finding.
- Reproduce high-severity candidates in a sandbox; never validate them in production.
- Track confirmation rates by module and vulnerability type, and continuously improve context strategies.
- Store confirmed vulnerabilities, false-positive causes, and remediation templates in a knowledge base.
- Use tiered scan triggers in CI/CD to avoid slowing down routine commits.
- Apply least-privilege access and audit logging to code, reports, and credentials.
9. Conclusion
The value of large language models in security auditing is not that they replace security engineers. Their value lies in upgrading the process from file-by-file manual review to semantic candidate discovery followed by evidence-based validation.
Public evaluation data suggests that, in large C++ codebases, models with long-context capabilities can identify cross-module, high-severity vulnerabilities within a relatively short time. However, an initial confirmation rate of approximately 20% also shows that validation workflows and false-positive management are essential.
A practical strategy for engineering teams is to use threat modeling to define the scope, rule-based tools for baseline coverage, large language models for deep semantic analysis, sandboxing and manual review for confirmation, and CI/CD integration with audit logging for process control. This approach can turn model-assisted security auditing into a practical capability for improving software quality and shifting security left while keeping operational risk under control.
