Source-linked AI summary

Towards Behavior Tree-Guided Vulnerability Detection with Lightweight LLMs

Enna Basic, Alberto Giaretta

arXiv:2609.01758v1cs.CRcs.SE

TL;DR

LLM vulnerability detection remains challenging, and structured representations can exceed the context limits of lightweight models. This paper converts Java source through ASTs into BTs, then compares source, AST, and BT inputs using a lightweight LLM. BTs improve recall on short samples and overall results on longer samples while remaining processable where many ASTs exceed the context limit.

  • Problem

    LLM vulnerability detection remains challenging, while structured code representations can increase input size and become difficult to use with limited context windows.

  • Method

    The paper proposes preprocessing that converts Java source code into ASTs and then BTs, comparing original source, AST, and BT inputs with a lightweight LLM.

  • Results

    BTs achieve higher recall on short samples and stronger overall results than original source on longer samples while remaining fully processable within the context window.

  • Takeaways & Limitations

    BTs provide a compact structured representation that is useful for vulnerability detection with lightweight, locally deployable models, especially on longer samples.

  • Takeaways & Limitations

    The evaluation uses synthetic Java samples from Juliet, which may not fully reflect the complexity of real-world software projects.

Abstract

from arXiv · show

Large Language Models (LLMs) are increasingly used for software vulnerability detection, but their performance depends on how source code is represented in the input. Most prompting approaches use source code in its original form, while some works propose the use of structured representations. Abstract Syntax Trees (ASTs) are one of the most popular approaches, but AST verbosity increases input size relative to source code, making them hard to fit within some LLMs context windows. This paper investigates Behavior Trees (BTs) as an alternative intermediate representation for LLM-based vulnerability detection. BTs encode control flow, conditions, and executable actions more compactly than ASTs, making them a natural candidate when token count is a constraint. First, we propose a preprocessing stage that parses Java source code into ASTs and then converts them into BT representations. We then compare vulnerability detection performance across 460 Java samples from the Juliet Java test suite, using three input representations: raw source code, AST, and BT. All experiments use a single quantized local LLM, Mistral Small 3.2 24B (Q4_K_M). Our results show that using BT representations improves recall on short code samples, while raw source code achieves higher precision. On longer samples, BTs improve overall performance over the original representation and fit within the context window, whereas many ASTs exceed the context limit. These findings suggest that BTs can provide a compact and useful structured representation for vulnerability detection with quantized, locally deployable LLMs.

1 Introduction

LLM-based vulnerability detection is promising but remains sensitive to code representation and difficult for lightweight models with limited context windows. This work investigates Behavior Trees as a compact structured representation and evaluates a preprocessing framework for Java vulnerability detection.

  • LLMs can miss vulnerabilities, report nonexistent ones, and struggle with complex detection scenarios.
  • Source-code representation influences vulnerability detection, while structured formats can add information but increase input size.The paper contrasts original source code with ASTs, CPGs, and enriched AST-based formats.
  • Behavior Trees reorganize Java source code around execution logic, branching, conditions, and executable operations.
  • The proposed framework transforms source code into BT representations before feeding it to a lightweight LLM.
  • The study presents a preliminary investigation of BTs as structured intermediate inputs for lightweight-model vulnerability detection.

2 Background and Related Work

Prior work establishes vulnerability detection as a valuable but challenging LLM application and highlights the influence of input representation. The paper positions BTs as a behavior-oriented alternative to syntactic representations such as ASTs.

  • Automated vulnerability-detection support can reduce the burden of large-scale security code review.Continuous code changes make manual review difficult and can leave vulnerabilities undetected.
  • LLM vulnerability detection remains challenging because models may miss vulnerabilities, produce incorrect predictions, or behave unreliably across settings.
  • Input representation can expose different aspects of code, including syntax, control flow, and behavior-relevant structure.
  • Abstract Syntax Trees: ASTs represent source-code syntax as trees whose nodes correspond to program constructs.ASTs are widely used as intermediate representations in compilers and program analysis.
  • Behavior Trees: BTs model execution logic and decision-making through control nodes, conditions, and actions.Sequence nodes require all children to succeed, whereas Selector nodes represent alternative branches.
  • Behavior Trees: BT structure is relevant because source code contains execution logic, conditions, actions, and alternative branches.The paper identifies BTs as a potentially suitable behavior-oriented reorganization and notes that this input representation had not previously been explored for LLM vulnerability detection.

3 Method

The proposed pipeline compares original Java source, AST, and BT representations after parsing and AST-to-BT conversion. Each representation is placed into a corresponding prompt and passed to a lightweight LLM for vulnerability prediction.

  • The framework transforms Java source code into original-source, AST, and BT input formats for comparison.
  • Java source is first parsed into an AST, which a custom converter then transforms into a BT representation.
  • A shared prompt template is constructed for each representation before the lightweight LLM generates vulnerability and possible CWE predictions.
  • The converter maps selected AST constructs to BT nodes to capture the source code’s main control-flow organization.
  • An illustrative example shows AST representations as more verbose and BT representations as more compact and focused on executable behavior.

4 Experimental Setup

The experiments use balanced Java vulnerability datasets from Juliet and compare source code, AST, and BT representations under shared evaluation procedures. The pipeline generates ASTs, converts them to BTs, and evaluates model outputs using exact, keyword, and semantic-similarity matching.

  • Datasets: The study constructs two balanced Java datasets from Juliet, covering short and longer samples with vulnerable and non-vulnerable variants.The short dataset contains 200 samples across 10 CWE categories; the longer dataset contains 260 samples across 13 categories.
  • Datasets: The short-sample dataset compares source code, AST, and BT inputs, whereas the longer-sample dataset compares source code and BT because many ASTs exceed 4096 tokens.AST coverage is examined separately in the context-window sensitivity experiment.
  • Evaluation strategy: Exact CWE matching requires the expected CWE identifier, while keyword matching accepts the identifier or a corresponding vulnerability name.Semantic similarity separately compares outputs with official CWE descriptions using embeddings.
  • Evaluation strategy: Accuracy, precision, recall, and F1 are reported for exact and keyword matching, while semantic similarity is reported as similarity scores.Higher semantic-similarity scores indicate closer correspondence to the expected CWE description.
  • Preprocessing and prompting: The pipeline parses Java source into ASTs, converts ASTs into BTs with a custom converter, and produces prompts for all three representations.The same prompt structure is used for code, AST, and BT inputs.

5 Experimental Results

Results differ by sample length and input representation: BT improves recall on short samples but source code is more precise, while BT outperforms source code on longer samples and remains fully processable within the original context window.

  • Short-Sample Representation Comparison: BT achieves the highest recall on short samples, while source code achieves the highest precision and accuracy under both matching strategies.The short-sample comparison includes source code, AST, and BT because all representations fit the context limit.
  • Short-Sample Representation Comparison: No single representation is consistently strongest across CWE categories, although BT attains the highest or tied-highest recall for several categories.BT also obtains the highest F1 score in several cases, while AST is stronger only in a few cases.
  • Short-Sample Representation Comparison: Mean semantic similarity varies by CWE category, with scores remaining very close across the three input representations.The strongest representation differs across categories.
  • Longer-Sample Code and BT Comparison: BT achieves higher precision, recall, F1 score, and accuracy than source code on longer samples under both keyword and exact CWE matching.AST is excluded from this comparison because its longer representations exceed the context limit.
  • Context Window Sensitivity: All 260 source-code and BT inputs fit at every tested context size, whereas AST coverage rises from 86 at 4096 tokens to 222 at 16384 tokens.Even at 16384 tokens, 38 AST inputs remain outside the context limit.
  • Context Window Sensitivity: Increasing the context window improves AST feasibility but does not fully solve input-length limitations, while BT preserves full coverage at 4096 tokens.Detection performance for source code and BT remains relatively stable as the context window increases.

6 Discussion

The discussion finds that input representation affects vulnerability detection differently across sample lengths. BT improves recall and longer-sample performance while remaining compact, whereas source code retains precision advantages and ASTs face context-window constraints.

  • 6.1 Effect of Input Representation on Short Samples: BT achieves the highest recall on short samples, while original source code achieves higher precision and accuracy.Because all three representations fit the context limit, this comparison isolates representation format and reveals a coverage–precision trade-off.
  • 6.1 Effect of Input Representation on Short Samples: AST does not show a consistent short-sample advantage despite preserving detailed syntactic information.The additional detail does not necessarily improve detection for the lightweight LLM evaluated here.
  • 6.1 Effect of Input Representation on Short Samples: BT is not uniformly strongest across CWE categories, although it achieves strong results for weaknesses involving conditions, actions, or operation ordering.The per-CWE pattern varies, with source code and AST performing better in selected categories.
  • 6.2 Behavior Trees for Longer Samples: Many longer-sample AST inputs exceed the context limit, whereas BT remains processable for all longer samples.This makes AST impractical as a direct input in part of the longer-sample setting.
  • 6.2 Behavior Trees for Longer Samples: BT improves F1-score from 0.59 to 0.64 under keyword matching and from 0.56 to 0.58 under exact CWE matching.Recall also rises from 0.73 to 0.83 and from 0.63 to 0.69, respectively, compared with original source code.
  • 6.2 Behavior Trees for Longer Samples: BT achieves stronger F1-scores for several longer-sample CWE categories and higher mean semantic similarity for 8 of 13 categories.Semantic similarity is more useful for comparing representations under the same setting than for judging output quality in isolation.
  • 6.3 Context-Window Feasibility: Increasing the context window improves AST coverage but does not make the full longer-sample dataset processable even at 16384 tokens.BT remains usable across tested context-window settings because it provides a more compact structured representation.
  • 6.3 Context-Window Feasibility: Larger context windows require more computational resources and processing time, while BT reduces input size while preserving behavior-oriented structure.This makes BT an alternative to relying only on larger contexts for local lightweight-model experiments.

7 Threats to Validity

The study’s validity is constrained by its synthetic, selected, and balanced Juliet datasets, its use of one lightweight LLM and prompt, and limitations in the evaluation metrics.

  • Dataset realism: Synthetic Juliet Java samples may not fully reflect the complexity of real-world software projects.Juliet provides controlled examples with known CWE labels, but this setting limits ecological realism.
  • Dataset scope: Selected CWE categories and balanced vulnerable and non-vulnerable samples limit how broadly the results can be generalized.The controlled selection makes representation comparisons easier to interpret but narrows the study’s scope.
  • Model and prompt scope: Using one lightweight LLM and one prompt template leaves results potentially sensitive to other models, prompts, and decoding configurations.Keeping configuration fixed isolates the effect of input format but does not establish robustness across setups.
  • Metric limitations: Exact CWE matching can count broader but security-relevant predictions as incorrect when parent and child categories differ.Keyword and semantic-similarity matching partly address this issue, but each introduces its own dependence on names, embeddings, or descriptions.

8 Conclusion and Future Work

The paper presents BTs as a preliminary intermediate representation for lightweight-LLM vulnerability detection and compares them with source code and ASTs. BT is especially useful for longer samples, where it improves overall results while AST inputs often exceed the context limit; future work should test broader settings.

  • Conclusion: The study proposes a BT-based preprocessing framework that converts Java source code into behavior-oriented tree representations for comparison with source code and ASTs.The evaluation uses a lightweight LLM.
  • Conclusion: On short samples, BT achieves the highest recall, while original source code achieves higher precision and accuracy.On longer samples, BT achieves stronger overall results than original source code under both keyword and exact CWE matching.
  • Conclusion: Increasing the context window improves AST coverage but does not fully solve the input-length problem, while BT remains fully processable across tested sizes.This supports BT’s compactness for the evaluated setting.
  • Future Work: Future work should evaluate BTs on larger, more realistic datasets and across additional CWE categories, languages, models, and prompt designs.Hybrid prompting that combines original source code with BTs could also examine whether precision and recall can be balanced.
Loading 2609.01758v1…