Efficient Code Analysis via Graph Representation Learning-Guided Large Language Models

Hang Gao, Tao Peng, Baoquan Cui, Hong Huang, Fengge Wu, Junsuo Zhao, Jian Zhang

Introduction

Large Language Models (LLMs) have significantly advanced automated code analysis. However, when it comes to detecting malicious behaviors fragmented across large codebases, these models often falter. The intricate dependencies of malware easily get lost in the vast amount of benign code. To address this, researchers have developed GMLLM (Graph Representation Learning-Guided Malicious code detection framework for LLM), a hybrid framework that combines the structural "bird's-eye view" of Graph Neural Networks (GNNs) with the deep reasoning capabilities of LLMs. By using a GNN to pinpoint suspicious subgraphs first, the system allows LLMs to focus only on relevant code, drastically improving detection accuracy while reducing computational costs.

The Challenge: Finding the Needle in the Codebase

While LLMs like GPT-4o and Llama 3 are remarkably adept at understanding isolated code snippets, they struggle to see "the forest for the trees" in large-scale software projects. Malicious behavior is rarely confined to a single function; it is often fragmented across multiple files and hidden behind complex call dependencies.

When an LLM is tasked with analyzing an entire project, two primary issues emerge:

  1. Attention Drift: The model becomes overwhelmed by the sheer volume of benign code, losing track of the subtle, malicious "needle" in the haystack.
  2. Resource Exhaustion: Processing thousands of lines of code is prohibitively expensive in terms of both token limits and compute time.

Existing security tools often rely on rigid, expert-defined rules that attackers can easily bypass. GMLLM seeks a middle ground: a system as flexible as an LLM, yet as efficient and structurally aware as a specialized graph model.

The GMLLM Framework: A Two-Stage Approach

The authors propose a two-part pipeline to efficiently localize and analyze malicious behavior:

1. Training the GNN "Scout"

  • Graph Construction: The target project is parsed into a code graph where nodes represent functions or classes, and edges represent dependencies or function calls.
  • Automated Rule Generation: Instead of relying on manual heuristics, an LLM is used to generate "sensitive behavior rules" (e.g., checking for unauthorized network access). These rules are converted into Python lambda functions to encode semantic features for each node in the graph.
  • Sparse Supervision: A lightweight Graph Convolutional Network (GCN) is trained using simple binary labels (malicious vs. benign) to identify suspicious structural patterns.

2. Attention-Based LLM Detection

  • The Explainer: Once the GNN flags a package as suspicious, the system applies an interpretability mask (an "Explainer") to determine exactly which nodes and edges most influenced the GNN's decision.
  • Subgraph Extraction: These highly influential regions are extracted to form a "high-attention subgraph"—essentially a highlight reel of the project's most suspicious components.
  • LLM Verdict: This compact, context-rich subgraph is fed to an LLM for final, in-depth analysis. The LLM provides a definitive verdict, detailed reasoning, and potential mitigation suggestions.

Datasets and Evaluation

The study evaluates the GMLLM framework on three public datasets—Backstabbers, Datadog, and Mal-OSS—and introduces a new, comprehensive benchmark:

  • MalCP (Malicious Codes from PyPI): A dataset comprising 1,659 real-world malicious Python packages collected from the PyPI ecosystem. The dataset is categorized by package size (Small, Medium, Large) and attack type (e.g., Execution, Data Theft).

Key Insights and Results

  • The Power of Filtering: Malicious code typically constitutes a tiny fraction of a project. By filtering out "benign noise" using a GNN, the LLM's precision skyrockets because its attention is no longer diluted by irrelevant context.
  • Superior Accuracy: GMLLM consistently outperforms both "vanilla" LLMs and professional security tools like OSSGadget. On the MalCP dataset, a ChatGPT-4o-based implementation of GMLLM achieved a recall of over 95% across all package sizes.
  • Massive Efficiency Gains: Resource savings are dramatic. While vanilla LLMs often require hundreds of thousands of tokens to analyze large packages, GMLLM reduces this to an average of just 640 tokens—a reduction of several orders of magnitude.
  • Explainability: Unlike black-box detectors, GMLLM produces human-readable reports. It points to specific functions (e.g., crypt32.CryptUnprotectData) and explains exactly why they are dangerous in their specific structural context.
  • LLM as a Rule-Maker: Using LLMs for feature engineering (generating sensitive behavior rules) makes the system highly scalable to new threats without requiring human experts to write new regex patterns.

Limitations and Future Directions

While highly effective, the current implementation of GMLLM is focused primarily on Python. Adapting the framework for other languages like JavaScript or C++ would require modifying the graph parsing and rule generation modules. Furthermore, the system still requires a labeled dataset to train the initial GNN scout, though it benefits from needing only simple binary labels rather than expensive, line-by-line annotations.

The authors also acknowledge a "dual-use" risk: sophisticated attackers could potentially leverage the GNN's explanation features to learn how to camouflage their malware. Future work may explore hybrid detection methods that combine this static graph analysis with dynamic behavioral analysis to catch even the most elusive threats.

Summary updated

BibTeX
@inproceedings{gao2026efficient,
  author = {Hang Gao and Tao Peng and Baoquan Cui and Hong Huang and Fengge Wu and Junsuo Zhao and Jian Zhang},
  title = {{Efficient Code Analysis via Graph Representation Learning-Guided Large Language Models}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=GNieZ8Z5V5},
}
Figure