Manuscript Offprint

arXiv:2608.14533

Finding Vulnerabilities via LLM-Augmented Semantics-Aware Type-Checking

Ruizhe Wang, Meng Xu, N. Asokan

Abstract

Vulnerability detection via static analysis traditionally relies on security experts encoding insecure coding patterns into algorithmic rules. However, this approach often focuses on syntactic patterns and overlooks deeper semantic information in the code, such as the meanings of variable and function names. As software systems grow more complex, modeling vulnerabilities using only syntactic rules becomes increasingly challenging.

In this paper, we propose a semantics-aware approach to detecting software vulnerabilities. We present SeType, a semantics-aware type system that can be derived directly from source code based solely on the meanings of symbols and expressions in natural language. In the SeType type system, both type inference and checking are performed by Large Language Models (LLMs), and a failed type check indicates a potential vulnerability.

We prototype PySeType to demonstrate the feasibility of SeType for detecting vulnerabilities in Python web applications. Our evaluation on real-world applications achieves 87% detection precision and 88% detection accuracy. Using PySeType, we identified 15 potential zero-day vulnerabilities, nine of which were confirmed by developers.

I Introduction

Static analysis is a widely adopted technique for identifying potential software vulnerabilities. Instead of executing the program, static analysis tools examine the code through information such as syntax, structure (e.g., control flow), and data flow, with the goal of finding insecure code patterns through a set of predefined rules [66].

To systematically identify vulnerabilities, static analysis tools must reason about the security implications of operations and data flows in a program. For example, dereferencing a @Nullable pointer might lead to a null-pointer bug. This resembles type systems in programming languages: static analysis can be conceptualized as a form of type checking that aims to assign type labels to variables and expressions and check whether certain type labels may participate in a given operation. In this context, the checking policy enforced by the static analyzer would classify such a dereference as a type-mismatch vulnerability.

However, while describing a vulnerability in natural language is often straightforward, precisely capturing its semantics through a set of syntactic rules built upon abstract syntax trees (ASTs), control-flow graphs (CFGs), or more generally program-dependence graphs (PDGs) [17, 44, 49] is challenging. Indeed, the problem of accurately capturing program semantics is known to be undecidable [45], meaning that no algorithm can determine the correctness of all possible programs. As a result, existing static-analysis tools often rely on manually crafted rules to approximate semantics and struggle to balance detection rates with false alarms. For example, CodeQL builds nearly 500 handwritten rules [2, 17] yet identifies less than 25% of bugs in practice [33]. On the other hand, improving bug detection rate often requires adopting more aggressive rules, which inevitably leads to high false positive rates [21, 29] that overwhelm developers, and erode their trust in the tool. These limitations largely stem from the difficulty of translating program semantics into manually crafted syntactic rules. Ideally, a static-analysis tool would operate directly on program semantics, precisely identifying bugs while minimizing false alarms.

To address this gap, we propose a semantics-aware approach to statically detecting software vulnerabilities. We introduce SeType, a type system that operates on program semantics rather than syntactic rules. In SeType, every variable and expression is assigned a semantic type that captures its intended meaning and behavior. Borrowing from the bidirectional type system [13], we design each operation to both check and infer the semantic types of its operands and results. If the entire program type-checks, we consider it free of the vulnerabilities detectable by SeType.

To illustrate, we present a path traversal vulnerability (CWE-22) in Figure 1. SeType checking starts from the review_paper endpoint. Following the program execution flow, it first infers pdf_path as “user input” type (line 5) and then abs_pdf_path as “file path from user” type (line 11). This “file path from user” type carries over to the pdf_path argument (line 20) through the function call (line 16) and participate in the to_markdown operation from the pymupdf4llm package (line 21), which is an agnostic file parser that reads the file at the given path, regardless of its source. As such, the review function expects a properly sanitized path argument, and an argument of the “file path from user” type violates the typing rules and causes SeType to raise a type error.

1 @app.route(”/api/review”, methods=[”POST”])

2 def review˙paper():

3 ”””Review a paper using the Reviewer class”””

4

5 pdf˙path = request.json.get(”pdf˙path”)

6 # Convert API path to absolute path

7 if pdf˙path.startswith(”/api/files/”):

8 # Remove /api/files/ prefix

9 rel˙path = pdf˙path[len(”/api/files/”):]

10 gen˙base = os.path.join(PROJECT˙ROOT, ”generated”)

11 abs˙pdf˙path = os.path.join(gen˙base, rel˙path)

12 else:

13 return ”Invalid file path”, 400

14

15 # Call reviewer.review() to get a single review

16 review˙result = Reviewer().review(abs˙pdf˙path)

17 return review˙result

18

19

20 def review(self, pdf˙path: Optional[str] = None) -¿ str:

21 text = pymupdf4llm.to˙markdown(pdf˙path=pdf˙path)

22 # …

Fig. 1: Example path traversal vulnerability adopted from (CVE-2025-55149) [37]. The vulnerability arises from the unsanitized use of user input pdf_path to access the file system in line 21.

In the above analysis, the types inferred and checked by SeType (e.g., “file path from user”) are not conventional programming types (e.g., int and string), whose meanings are defined by the language. Instead, they are natural-language descriptions of the semantics of variables and expressions, generated from the source code text rather than from ASTs, CFGs, or PDGs. Similarly, the SeType checking described above is not a mechanical algorithm. It relies on the natural-language description of the path traversal vulnerability and is a fuzzy (non-deterministic) check at the natural-language level in practice.

As all types in SeType are in the natural-language domain, we leverage Large Language Models (LLMs) as the backend to generate and reason. LLMs have demonstrated remarkable capabilities in understanding both code and natural language, and with their reasoning abilities, can analyze program semantics effectively.

We implement PySeType, an extension of pytype [18] that incorporates SeType reasoning for enhanced vulnerability detection in Python programs. Following the code-execution flow inferred by pytype, PySeType maintains a dynamic representation of the program’s state and prompts an LLM to update the SeType of variables and expressions following the encountered operations (e.g., function calls or built-in operations). We focus on detecting common vulnerabilities in web applications, such as path traversal and SQL injection, with security policies derived from Common Weakness Enumeration (CWE) [39].

We evaluate PySeType through comprehensive experiments on real-world Python codebases. As a static code analyzer, PySeType should follow generic criteria for static analysis— reporting as many genuine bugs as possible while minimizing false alarms. On a dataset of known vulnerabilities and their patches (SVEN [23]), PySeType achieves 87% detection precision and 88% accuracy, demonstrating its effectiveness in vulnerability detection. To address memorization concerns, we evaluate PySeType on recent CVEs reported after the backend LLM’s training cutoff, and observe consistent performance across both datasets.

We also deploy PySeType to analyze 103 popular open-source Python projects. PySeType discovered 15 new vulnerabilities across high-profile codebases, including an official Microsoft library, Python Software Foundation (PSF) server infrastructure, and systems maintained by the Internal Revenue Service (IRS). Nine of these vulnerabilities have been confirmed by respective development teams. PySeType demonstrates practical scalability in this process---analysis on the largest codebase (195 API endpoints and 26k Python bytecodes) was completed in around fourteen hours with an estimated LLM inference cost of $3.19 using equivalent Qwen-Plus API pricing, making it a viable tool for real-world deployment.

In summary, we make the following contributions:

  • A semantics-aware approach for vulnerability detection by introducing SeType, a type system that enables vulnerability detection via automated code reasoning in natural language and harvests semantic information from source code that is not encoded in syntactic data structures (Section IV).
  • PySeType, a prototype implementation that leverages LLMs to perform SeType reasoning on Python programs to detect vulnerabilities common in web applications (Section V).
  • Extensive evaluation demonstrating PySeType’s effectiveness in detecting known vulnerabilities with high accuracy and precision, and its ability to discover new security issues in popular open-source projects while exhibiting practical scalability (Section VI).

References (40)