Abstract
Large language models (LLMs) demonstrate significant potential to revolutionize software engineering (SE) by exhibiting outstanding performance in tasks such as code and document generation. However, the high reliability and risk control requirements in software engineering raise concerns about the lack of interpretability of LLMs. To address this concern, we conducted a study to evaluate the capabilities of LLMs and their limitations for code analysis in SE. We break down the abilities needed for artificial intelligence (AI) models to address SE tasks related to code analysis into three categories: 1) syntax understanding, 2) static behavior understanding, and 3) dynamic behavior understanding. Our investigation focused on the ability of LLMs to comprehend code syntax and semantic structures, which include abstract syntax trees (AST), control flow graphs (CFG), and call graphs (CG). We employed four state-of-the-art foundational models: GPT-4, GPT-3.5, StarCoder, and CodeLlama-13b-instruct. We assessed the performance of LLMs on cross-language tasks involving C, Java, Python, and Solidity. Our findings revealed that while LLMs have a talent for understanding code syntax, they struggle with comprehending code semantics, particularly dynamic semantics. We conclude that LLMs possess capabilities similar to an AST parser, demonstrating initial competencies in static code analysis. Furthermore, our study highlights that LLMs are susceptible to hallucinations when interpreting code semantic structures and fabricating nonexistent facts. These results indicate the need to explore methods to verify the correctness of LLM output to ensure its dependability in SE. More importantly, our study provides an initial answer to why the codes generated by LLMs are usually syntax-correct but vulnerable.
Introduction: The Reliability Gap in Code Generation
While LLMs like GPT-4 and CodeLlama have revolutionized code generation, their internal reasoning processes remain largely opaque. In software engineering, where reliability and security are paramount, it is critical to determine whether a model is truly reasoning about code or merely acting as a "stochastic parrot" that mimics common patterns.
This gap in interpretability and reliability poses a significant risk: a model might generate code that is syntactically perfect but contains deep-seated logical vulnerabilities because it fails to understand the underlying execution semantics. This research seeks to quantify that understanding gap across different programming languages (C, Java, Python, Solidity) and model architectures.
Methodology: A Three-Tier Evaluation Framework
To systematically evaluate LLM "intelligence" in code analysis, we developed a framework categorized into three research questions (RQs):
- Syntax Understanding (RQ1): Can the model identify the structural rules of the language? Tasks included generating Abstract Syntax Trees (AST) and matching mathematical expressions based on their syntax roles rather than just variable names.
- Static Behavior Understanding (RQ2): Can the model approximate traditional static analysis tools? This involved constructing Control Flow Graphs (CFG) and Call Graphs (CG), as well as performing data dependency, taint, and pointer analysis.
- Dynamic Behavior Understanding (RQ3): Can the model predict how code behaves during execution? This was tested via equivalent mutant detection (identifying if a code change alters functionality) and flaky test reasoning.
Experimental Setup
The study utilized a mix of existing benchmarks and newly created datasets to avoid data contamination:
- AST/CFG: 75 small programs covering various control structures (if-else, loops, etc.).
- Expression Matching: A new dataset of 32 reward-computing equations from real-world DeFi projects.
- Static Analysis: 13 DeFi projects from Etherscan for data dependency/taint analysis and 40 C programs from the SVF test suite for pointer analysis.
- Dynamic Analysis: 200 mutants from MutantBench and 65 samples from the FlakyCat dataset.
Key Insights
- LLMs as "Human-Readable AST Parsers": Models are surprisingly adept at identifying the syntax roles of tokens. They can effectively decompose code into its structural components, much like a traditional parser, but with the added benefit of providing natural language explanations.
- The Hallucination Trap: When tasked with structural analysis (like CFGs or Call Graphs), models often "hallucinate" non-existent nodes, edges, or function calls. This suggests they rely on probabilistic patterns rather than strict logical tracing.
- Sensitivity to Project Context: Performance varied significantly across different projects for the same task. This indicates that LLMs are highly sensitive to the specific coding style or "flavor" of the project they are analyzing.
- Semantics as the Final Frontier: While syntax is largely mastered, dynamic semantics—understanding how data flows through a program and changes state—remains a major weakness. This explains why LLM-generated code is often "syntax-correct but vulnerable."
Results Summary
- GPT-4 Dominance: GPT-4 outperformed all other models across almost every category, particularly in complex tasks like Call Graph generation where others failed significantly.
- Syntax Success: For expression matching, GPT-4 and GPT-3.5 achieved high hit rates (27/32 and 28/32), whereas open-source models like StarCoder and CodeLlama struggled (5/32).
- Static Analysis Struggles: In taint analysis, all models performed worse than a random guess (F1 scores < 0.5). GPT-4's F1 for data dependency was 0.69, but this dropped to 0.44 for the more complex taint analysis.
- Dynamic Failure: In equivalent mutant detection, CodeLlama failed completely (F1 = 0). GPT-4 performed best here with a zero-shot F1 of 0.67.
Conclusion and Future Directions
The study highlights that while LLMs excel at syntax, they struggle with complex static and dynamic analysis. Key limitations include dataset scale for manual evaluation, sensitivity to prompts, and the black-box nature of proprietary models. Future work should explore automated methods to verify LLM outputs and develop specialized pre-training tasks that focus on execution traces and data flow to bridge the semantic gap.
Summary updated