Large Language Models for Code Analysis: Do LLMs Really Do Their Job?

Chongzhou Fang, Ning Miao, Shaurya Srivastav, Jialin Liu, Ruoyu Zhang, Ruijie Fang, Asmita, Ryan Tsang, Najmeh Nazari, Han Wang, Houman Homayoun

TL;DR

This paper evaluates whether Large Language Models (LLMs) can truly "understand" and analyze code rather than just generate it. While GPT-4 shows impressive accuracy (97.4%) in explaining clear code, its performance degrades under complex obfuscation, and smaller models like LLaMA-2 struggle to provide any meaningful analysis at all.

What Problem Does This Paper Address?

While LLMs have become staples for code generation (e.g., GitHub Copilot), their utility in code analysis—the ability to explain, audit, and reverse-engineer software—remains under-explored. This is a critical gap for security researchers who need to analyze potentially malicious, "obfuscated" code (code intentionally made unreadable to humans and traditional tools). The authors investigate whether LLMs can bridge the gap where traditional tools like Abstract Syntax Trees (ASTs) fail due to rigid data definitions.

Methods

The researchers conducted a systematic study comparing five prominent LLMs: GPT-4, GPT-3.5-turbo, LLaMA-2-13B, Code-LLaMA-2-13B-Instruct, and StarChat-Beta.

Their approach involved:

  1. Prompt Engineering: Using simple instructions ("Analyze the code and tell me what it does") and role-assignment prompts.
  2. Multi-Metric Evaluation: To measure the "correctness" of an LLM's explanation, they used Cosine Similarity, BERT-based semantic scores, and a "GPT-4 as a judge" mechanism, all of which were validated against a human-verified "Ground Truth."
  3. Real-World Case Studies: They tested the models on actual malware, including the Android "msg-stealer" virus and the infamous WannaCry ransomware.

Datasets

The study utilized three distinct types of data:

  • Non-Obfuscated Code: Samples in C, Python, and JavaScript drawn from popular benchmarks like Octane 2.0 and CodeSearchNet.
  • Obfuscated JavaScript: Code transformed using tools like JavaScript Obfuscator (applying techniques like control flow flattening and dead code injection) and Wobfuscator (which translates JavaScript into WebAssembly).
  • IOCCC Dataset: 100 winning entries from the International Obfuscated C Code Contest, representing the pinnacle of human-written unreadable code.

Key Insights and Ideas

  • The Memorization Phenomenon: LLMs often "recognize" code from their training data. For instance, GPT-4 correctly identified a code snippet as belonging to the "Heron" project even after all comments and "Twitter" keywords were removed.
  • Identifier Reliance: Like humans, LLMs rely heavily on natural language hints in variable names (e.g., ee_u8 or Benchmark). When these are replaced with random strings, performance drops.
  • The "Small Model" Wall: Smaller models (13B–16B parameters) didn't just perform worse; they often failed to generate analysis at all, instead choosing to parrot the input code or repeat the prompt.
  • Wobfuscator is the "LLM-Killer": Cross-language obfuscation (moving from JavaScript to WebAssembly) proved the most effective at blinding LLMs, as it strips away the high-level semantic structures the models rely on.

Key Results

  • GPT-4 is the Gold Standard: It achieved a 97.4% accuracy rate on non-obfuscated code and maintained a high 87% accuracy on traditionally obfuscated code.
  • Analysis vs. Generation: There is a massive gap between explaining code and fixing it. While GPT-4 could explain what an obfuscated IOCCC snippet did, it failed to generate a runnable, de-obfuscated version of that same code in 96% of cases.
  • Context is King in Security: In the WannaCry case study, GPT-3.5 could identify suspicious networking tasks but was too "conservative" to label the behavior as malicious without being prompted with the full, concatenated context of the program.

Limitations and Future Work

The authors highlight several hurdles for the field:

  1. Input Window Constraints: Analyzing large-scale software requires "chunking" code, which causes the LLM to lose track of global variables and "goto" labels, leading to fragmented analysis.
  2. Metric Reliability: Standard NLP metrics (like N-grams) are poor at capturing the functional logic of code. Even LLM-based evaluation requires significant human calibration.
  3. Dataset Scarcity: There is a lack of high-quality, diverse datasets for obfuscated code, which is necessary to fine-tune smaller, open-source models to compete with proprietary giants like GPT-4.

Summary updated

BibTeX
@inproceedings{fang2024large,
  author = {Chongzhou Fang and Ning Miao and Shaurya Srivastav and Jialin Liu and Ruoyu Zhang and Ruijie Fang and Asmita and Ryan Tsang and Najmeh Nazari and Han Wang and Houman Homayoun},
  title = {{Large Language Models for Code Analysis: Do LLMs Really Do Their Job?}},
  booktitle = {USENIX Security},
  year = {2024},
}
Figure