Auto-SPT: Automating Semantic Preserving Transformations for Code

Ashish Hooda, Mihai Christodorescu, Chuangang Ren, Aaron Wilson, Kassem Fawaz, Somesh Jha

TL;DR

Auto-SPT leverages Large Language Models (LLMs) to automatically design, implement, and combine diverse semantic-preserving code transformations (SPTs). By generating complex structural and syntactic mutations, it exposes a critical vulnerability in state-of-the-art code clone detectors, while also providing a powerful data augmentation tool to train more robust models.

What Problem Does This Paper Address?

Code clone detection—identifying whether two pieces of code are semantically equivalent—is vital for software engineering tasks like refactoring, plagiarism detection, and vulnerability scanning. While current machine learning models excel on clean, structured datasets, they struggle with "Type-4" clones, where the underlying logic is identical but the syntax and structure differ drastically due to refactoring, minification, or compiler optimizations.

Existing methods for testing these models rely on simple, hand-crafted transformations (such as renaming variables or swapping while loops for for loops). These manual approaches lack the diversity and complexity needed to rigorously evaluate or improve model robustness against real-world or adversarial code variations.

Methods

The authors introduce Auto-SPT, an automated framework powered by LLMs (specifically Gemini 2.5 Pro) that operates in three distinct steps:

  1. Designing New SPTs: Instead of relying on a fixed set of human-defined rules, Auto-SPT uses iterative prompting to discover novel transformation concepts. To ensure diversity, the prompt instructs the LLM to generate transformations distinct from a running list of previously generated ones, based on a set of sample programs.
  2. Implementing SPTs: The framework prompts the LLM to generate Python scripts that automate these transformations. To ensure correctness and strength, it uses temperature sampling to generate multiple candidate implementations and evaluates them using a reward function. This function verifies functional equivalence via unit tests and selects the implementation that maximizes the prediction error of a target clone detector (Best-of-N sampling).
  3. Combining SPTs: To find the most challenging adversarial examples, Auto-SPT uses an iterative beam search strategy. It sequentially applies combinations of different SPTs, filters out invalid or broken code using unit tests, and keeps the top $B$ most effective mutations at each step to find the "worst-case" transformation.

Datasets

  • CodeContests: A dataset containing competitive programming problems, user-submitted Python solutions, and associated unit tests. The authors randomly selected 2,000 distinct problems, splitting them into 1,500 training, 250 validation, and 250 test problems to construct balanced clone and non-clone pairs (375,000 pairs total for training).

Key Insights and Ideas

  • Diversity Drives Strength: The authors formally prove and empirically demonstrate that the diversity of a transformation set directly bounds its adversarial strength. Simple transformations (like renaming variables) quickly hit a ceiling, whereas a diverse set of transformations maintains its effectiveness even when many are composed.
  • LLMs as Meta-Programmers: Instead of using LLMs to transform code directly on a case-by-case basis (which is expensive and inconsistent), Auto-SPT uses LLMs to write the transformation tools themselves. This tool can then be applied deterministically and cheaply to thousands of programs.
  • Current Embeddings are Fragile: Even state-of-the-art code embedding models are highly susceptible to automated structural changes, revealing that they rely heavily on surface-level syntactic patterns rather than deep semantic understanding.

Key Results

  • Auto-SPT successfully discovered 20 novel, complex SPTs (e.g., Modular Index Obfuscation, State Machine Enumeration with Dynamic Dispatch, and Recursive List Comprehension Flattening).
  • When evaluated against state-of-the-art clone detectors, the worst-case transformations generated by Auto-SPT drastically increased the "Worst Case Distance" (where 0 means identical clone and 1 means completely different/non-clone). For instance, on a fully finetuned CodeBERT model, Auto-SPT achieved a distance of 0.947, compared to just 0.243 from prior manual methods.
  • As shown in Table 2, even advanced embedding models like EmbeddingGemma dropped significantly in performance, facing a worst-case distance of 0.803.
Table 2: Worst Case Transformation for different transformation sets when searching over 10 iterations. Auto-SPT achieves the strongest transformations.
  • Robustness Boost: Finetuning CodeBERT on data augmented with Auto-SPT's transformations significantly improved its resilience against both simple and complex adversarial mutations.

Limitations and Future Work

  • Language Scope: The evaluation was focused entirely on Python programs. While the framework is conceptually language-agnostic, its efficacy on strongly-typed or compiled languages (like C++ or Java) remains to be tested.
  • Computational Cost of Diversity Estimation: Calculating the exact diversity metric for large transformation sets is computationally expensive, limiting the number of search iterations the authors could analyze during formal diversity verification.
  • Applicability Constraints: Some of the highly complex generated transformations are only applicable to a small subset of programs that contain specific structures (though the authors note every program in their test set had at least 9 applicable transformations).

Summary updated

BibTeX
@misc{hooda2025auto,
  author = {Ashish Hooda and Mihai Christodorescu and Chuangang Ren and Aaron Wilson and Kassem Fawaz and Somesh Jha},
  title = {{Auto-SPT: Automating Semantic Preserving Transformations for Code}},
  year = {2025},
  eprint = {2512.06042},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2512.06042},
}
Figure