Abstract
Current evaluations of Large Language Models (LLMs) in software engineering often rely on functional correctness via test cases, which may not capture true semantic understanding. We present SpecEval, a framework that uses formal program specifications in Java Modeling Language (JML) as a proxy for code comprehension. Our findings reveal that while models like GPT-4 lead the field, they remain fragile to "meaningless" code perturbations, particularly variable renaming, suggesting a reliance on natural language cues over pure logical reasoning.
Introduction
The rapid advancement of LLMs in code generation has sparked a debate: do these models truly understand code semantics, or are they merely sophisticated pattern matchers? Traditional benchmarks like HumanEval or MBPP use "pass@k" metrics based on test cases. However, passing a subset of inputs does not guarantee a model understands the full range of a program's behavior across all possible inputs.
To address this, SpecEval leverages formal program specifications—mathematical claims such as preconditions, postconditions, and loop invariants. These specifications describe what a program does without detailing how, serving as a rigorous proxy for program semantics.
Methodology: The SpecEval Framework
SpecEval evaluates models across four hierarchical tasks using the Java Modeling Language (JML):
- Judgment: Determining if a specification correctly describes a program.
- Selection: Choosing the most precise specification from a set of correct ones.
- Infilling: Completing missing segments of a specification.
- Generation: Synthesizing a complete specification from scratch.
To test the robustness of model understanding, the framework employs Counterfactual Analysis. This involves applying five types of semantic-preserving perturbations to the source code, as illustrated below.
These perturbations—including if-else flipping, independent statement swapping, and variable name randomization—ensure that the underlying logic remains identical while changing the textual representation.
Experimental Setup
The SpecEval benchmark consists of 204 Java programs with expert-verified ground-truth specifications. These programs were sourced from SpecGenBench, SV-COMP, and Frama-C-Problems. Each program includes five semantically equivalent variants for counterfactual testing, resulting in nearly 5,000 evaluation tasks per model. Verification was performed using OpenJML to ensure specifications were both mathematically correct and "strong."
Key Results and Insights
The evaluation of six state-of-the-art models (GPT-4, GPT-3.5, Llama, CodeLlama, DeepSeek-Coder, and Magicoder) highlights a significant performance gap between proprietary and open-source models.
Model Performance and Scaling
As shown in the results table, GPT-4 consistently outperforms other models, achieving 81.37% accuracy in the Judgment task. Open-source models generally struggle, often hovering between 40-60%. Interestingly, the "Infilling" task proved more difficult than expected, as it requires models to reconcile partial specifications with complex code logic simultaneously. Additionally, moving from 0-shot to 2-shot prompting provided a significant boost, particularly for open-source models.
The Fragility of "Understanding"
A critical finding is the models' sensitivity to variable names. While structural changes like flipping if-else blocks had minimal impact, randomizing variable names (e.g., changing temp to PZYcz) led to a massive performance drop. In some cases, models showed a Jaccard Distance (inconsistency) of over 0.6 when variable names were randomized. This indicates that LLMs rely heavily on natural language heuristics within identifiers rather than the abstract logical structure of the code.
The Synthesis Gap
Models also struggle with synthesis. Even when a model can explain individual code segments, it often fails to generate "strong" specifications that fully define the program's behavior. This is evidenced by GPT-4 achieving only a 34.17% recall in the Generation task, indicating a struggle to produce specifications that fully capture program semantics.
Conclusion and Future Directions
SpecEval demonstrates that current LLMs lack a robust, logic-based understanding of code semantics. Their performance is highly dependent on natural language hints and is easily disrupted by non-semantic changes. Future research should focus on scaling formal specification benchmarks and developing training techniques that enhance the symbolic reasoning capabilities of LLMs, making them more resilient to "noise" in code representation.
Summary updated