Python Symbolic Execution with LLM-powered Code Generation

Wenhan Wang, Kaibo Liu, An Ran Chen, Ge Li, Zhi Jin, Gang Huang, Lei MaarXiv 2024 · DOI

TL;DR

LLM-Sym is a novel agent-based framework that enables symbolic execution for Python by using Large Language Models (LLMs) to bridge the gap between dynamic code and formal math solvers. By translating complex Python path constraints into Z3 SMT solver code, it generates test cases for programs involving dynamic types and complex list structures—scenarios where traditional tools typically fail.

Introduction: The Challenge of Python Symbolic Execution

Symbolic execution is a powerful technique for exploring program paths by using "symbolic" variables to find inputs that trigger specific behaviors or bugs. However, applying it to Python is notoriously difficult. Unlike statically typed languages like C or Java, Python is dynamically typed and features highly flexible data structures such as variable-length lists.

Traditional symbolic execution engines rely on hand-coded rules to translate code into mathematical constraints for SMT (Satisfiability Modulo Theories) solvers like Z3. Manually defining these rules for Python’s entire grammar is a brittle and monumental task. Consequently, most existing Python tools support only simple primitives, leaving real-world code with complex structures out of reach.

Methodology: The LLM-Sym Framework

The authors introduce LLM-Sym, an LLM-powered agent designed to automate the translation of Python code into solver constraints. The framework operates through a multi-stage pipeline:

  1. Execution Path Extraction: A Control Flow Graph (CFG) explorer identifies specific paths through the code.
  2. Type Inference: An LLM predicts variable types (e.g., List[int]) to compensate for Python's lack of static annotations.
  3. Z3 Code Generation with RAG: This core component uses an LLM to generate Python code that invokes the Z3 API. To enhance accuracy, the system employs Retrieval-Augmented Generation (RAG), fetching "Python-to-Z3" templates from a curated knowledge base.
  4. Self-Refinement: If the generated Z3 code fails due to syntax or logic errors, the agent uses error feedback to prompt the LLM for corrections (up to three attempts).
  5. Hybrid Solving: If the Z3 approach fails, the system falls back to a "vanilla" LLM solver to find solutions through direct reasoning.
The LLM-Sym workflow architecture, illustrating the path from code snippet to test case generation.

Modeling Dynamic Data Structures

A significant hurdle in Python symbolic execution is handling variable-length lists. LLM-Sym addresses this by modeling lists as infinite arrays in Z3, paired with a symbolic integer representing the list's length. This abstraction allows the LLM to effectively reason about operations such as append, pop, and indexing.

Examples of mapping Python list operations to Z3Py code constraints.

Experimental Evaluation

The framework was evaluated on a dataset derived from LeetCode, consisting of 50 problems selected for their complex control flows and heavy use of list operations. The evaluation involved 111 execution traces with an average length of 12.3 steps. LLM-Sym was compared against a baseline engine from The Fuzzing Book, which failed to solve any traces (0% success) due to its lack of support for lists and dynamic types.

Results and Key Insights

  • Success Rate: LLM-Sym correctly solved 63.1% of the execution traces, producing exact matches for the intended paths. It found valid input arguments (SAT) for 89.2% of the paths.
  • Code Generation vs. Direct Reasoning: A major finding is that LLMs are more effective at writing code for a solver than solving complex logic puzzles directly. The "LLM + Solver" hybrid achieved a pass rate of ~70-73%, compared to only 37-58% for a vanilla LLM solver.
  • Cost Efficiency: By utilizing GPT-4o-mini with RAG and self-refinement, the system achieved high performance at a cost of only $0.005 per sample—roughly one-third the cost of using the larger GPT-4o model.

Limitations and Future Work

Despite its success, LLM-Sym faces challenges common to symbolic execution, such as path explosion in larger programs. Additionally, LLMs occasionally struggle with niche Z3 functions or complex external libraries. Future work will focus on expanding the retrieval knowledge base and improving the fallback solver to handle constraints that are theoretically unsolvable by SMT solvers, such as non-linear mathematics or complex string manipulations.

Summary updated

BibTeX
@misc{wang2024python,
  author = {Wenhan Wang and Kaibo Liu and An Ran Chen and Ge Li and Zhi Jin and Gang Huang and Lei Ma},
  title = {{Python Symbolic Execution with LLM-powered Code Generation}},
  year = {2024},
  eprint = {2409.09271},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2409.09271},
}
Figure