NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Is The Watermarking Of LLM-Generated Code Robust?

Tarun Suresh, Shubham Ugare, Gagandeep Singh, Sasa Misailovic

TL;DR

This paper demonstrates that current watermarking techniques for LLM-generated code are highly fragile. By using a systematic, AST-based approach to apply simple, semantic-preserving transformations—like renaming variables or adding dead code—the authors show that an adversary can easily erase watermarks, often dropping detection rates (True Positive Rates) below 50% without changing the program's behavior.

What Problem Does This Paper Address?

As Large Language Models (LLMs) like GPT and CodeLlama become staples in software development, concerns regarding their misuse—such as code plagiarism and the automated generation of malware—have intensified. Watermarking has emerged as a primary defense, where hidden statistical patterns are injected into the LLM's output during the decoding process to allow for later identification.

While watermarking has shown resilience in natural language text, code presents a unique challenge. Unlike prose, where significant paraphrasing is required to alter token distributions, code is highly structured. The authors set out to investigate whether the inherent flexibility of programming languages allows users to "wash away" these watermarks through simple, automated modifications that preserve the code's original functionality.

Methods

The authors developed a systematic framework to evaluate watermark robustness using Abstract Syntax Tree (AST) transformations. Their algorithm, PERTURB, parses watermarked Python code into an AST and applies a sequence of $d$ randomized, semantic-preserving transformations:

  1. Rename: Replaces a randomly selected identifier (variable or function name) with a new, valid name.
  2. AddDeadCode: Inserts a functionally useless statement (e.g., if (i1 != i1): i2 = 0) at a random location.
  3. InsertPrint: Adds a print() statement with a random string.
  4. WrapTryCatch: Wraps a random statement within a try-except block.
  5. Mixed: A combination of the above transformations.

By operating on the AST, the authors ensure that the resulting code remains syntactically valid and semantically identical to the original AI-generated output. They tested these attacks against two state-of-the-art watermarking schemes: UMD (Kirchenbauer et al.) and Unigram (Zhao et al.).

Datasets

The study utilized the HumanEval dataset, a standard benchmark for evaluating code generation models. The researchers generated Python code completions using two models:

  • Llama-7B
  • CodeLlama-7B

For the vocabulary used in transformations (like new variable names or print strings), they employed the NLTK WordNet library.

Key Insights and Ideas

  • Code is more "brittle" for watermarks than text: In natural language, changing enough tokens to break a watermark usually requires significant human effort or another LLM. In code, a single structural change (like wrapping a block in a try-except) can alter a large proportion of tokens instantly.
  • Semantic-preserving changes are the "Silver Bullet": Because code behavior is decoupled from its specific token representation, adversaries can make significant statistical changes to the output while maintaining 100% of the code's utility.
  • The "Hole" and "Completion" Strategy: The PERTURB algorithm effectively creates "holes" in the AST and fills them with syntactically valid but randomized sequences, which is highly effective at disrupting the specific n-gram or green-list patterns that watermarking detectors look for.

Key Results

The experimental results show a drastic decline in watermark detectability across all tested scenarios.

As shown in Figure 1, the True Positive Rate (TPR) declines sharply as the number of transformations ($d$) increases:

Figure 1: TPR vs the number of transformations d.

Key quantitative findings include:

  • Baseline Fragility: For Llama-7B, even simple Rename operations (modifying only ~5% of tokens) reduced the TPR by at least 1.3x.
  • Structural Impact: Complex alterations like WrapTryCatch and AddDeadCode were devastating. For the Unigram watermark on Llama-7B, AddDeadCode dropped the TPR from a baseline of 0.76 to a near-zero 0.01.
  • Token Proportion: Table 1 highlights that structural changes like AddDeadCode modify roughly 36% of the code's tokens on average, which is often enough to completely erase the watermark's statistical signature.
Table 1: Watermark detectability results

Limitations and Future Work

The authors acknowledge that their study is limited to Python and focuses on two specific watermarking algorithms. While these represent the current state-of-the-art, the rapid evolution of LLMs may lead to new watermarking techniques.

The paper concludes by urging the research community to move beyond token-based watermarking for code. Instead, they suggest developing resilient detection schemes that watermark the syntax tree (AST) itself, rather than the flat sequence of tokens, to ensure that watermarks survive the common structural modifications inherent to software development.

Summary updated

BibTeX
@inproceedings{suresh2024is,
  author = {Tarun Suresh and Shubham Ugare and Gagandeep Singh and Sasa Misailovic},
  title = {{Is The Watermarking Of LLM-Generated Code Robust?}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=8PhI1PzSYY},
}
Figure