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

Is Watermarking LLM-Generated Code Robust?

Tarun Suresh, Shubham Ugare, Gagandeep Singh, Sasa Misailovic

TL;DR

While watermarking has shown promise in identifying AI-generated natural language, this paper reveals it is remarkably fragile when applied to code. By using simple, semantic-preserving transformations—like renaming variables or adding dead code—an adversary can easily "wash away" watermarks from LLM-generated Python code without changing its functionality.

Introduction

As Large Language Models (LLMs) like GPT-4 and CodeLlama become staples in software engineering, concerns regarding code plagiarism, automated malware generation, and the "pollution" of open-source repositories have spiked. Watermarking—a technique that injects hidden statistical patterns into the model's output—is currently the primary defense for detecting AI-generated content.

However, code is fundamentally different from natural language. In a paragraph of text, changing 50% of the words while maintaining the exact meaning is difficult. In code, you can refactor, rename, and restructure logic extensively while the program's behavior remains identical. This paper investigates whether current state-of-the-art watermarking schemes can survive these common, behavior-preserving modifications.

Adversarial Evaluation Framework

The authors propose an evaluation framework that simulates a "black-box" adversary who wants to remove a watermark. Their core contribution is an algorithm that manipulates the Abstract Syntax Tree (AST) of the generated code.

Instead of random character swaps, the algorithm applies five specific semantic-preserving transformations:

  1. Rename: Randomly changing the names of local variables and identifiers.
  2. InsertPrint: Dropping harmless print() statements at random locations.
  3. AddDeadCode: Injecting "junk" logic (e.g., if False: ...) that never executes but changes the token sequence.
  4. WrapTryCatch: Wrapping existing code blocks in try-except structures.
  5. Mixed: A combination of the above applied sequentially.

The study tests two major watermarking families: UMD (which uses a "green list" of tokens biased during generation) and Unigram (designed to be robust to edits), evaluating them on Llama-7B and CodeLlama-7B using the HumanEval benchmark.

Why Code Watermarks Fail

Impact of transformations on True Positive Rate (TPR)

The researchers identified several structural reasons why current watermarking techniques fail on code:

  • Code is "Low Entropy": Code generation often has lower entropy than natural language (there are only so many ways to write a standard loop). This makes it harder to hide a watermark without significantly degrading code quality.
  • Structural Fragility: Watermarks are typically embedded at the token level. Because code is highly structured, a single transformation (like wrapping a block in a try-except) shifts the context for every subsequent token, effectively "desynchronizing" the watermark detector.
  • The "Rename" Paradox: Even though renaming variables only changes about 5% of the total tokens, it significantly reduces the True Positive Rate (TPR). This suggests that watermarks are heavily dependent on the specific identifiers the LLM chooses.
  • Dead Code is the "Watermark Killer": Adding dead code was the most effective attack. It introduces a high volume of "un-watermarked" tokens that drown out the original signal, often reducing detection rates to near zero.

Experimental Results

Detection metrics for UMD and Unigram watermarks after transformations

The results show a dramatic decline in the ability to detect watermarked code after just a few modifications:

  • UMD Watermark: On Llama-7B, the initial detection rate (TPR) was 0.79. After applying the WrapTryCatch transformation, the TPR plummeted to 0.22.
  • Unigram Watermark: This scheme proved even more vulnerable. An initial TPR of 0.76 dropped to a staggering 0.01 (virtually undetectable) after the AddDeadCode transformation.
  • Efficiency of Attack: Simple transformations like InsertPrint and Rename reduced the TPR by at least 1.3x, while more complex structural changes reduced it by over 2x compared to other methods.

Limitations and Future Work

The study primarily focuses on Python, and while the logic likely extends to other languages like C++ or Java, the specific AST transformations would need to be adapted. Additionally, the study assumes the adversary has "black-box" access—meaning they don't know the watermark key, but they can freely modify the output.

The authors conclude with a call to action: current watermarking is not enough for code. Future research must move away from token-level biases and toward syntax-aware watermarking. This might involve embedding the "secret signal" into the structure of the AST itself, making the watermark as integral to the code as the logic it performs.

Summary updated

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