Abstract
Code datasets are essential for training neural-network-based code completion models, representing significant investments by organizations. However, these datasets are vulnerable to unauthorized exploitation, such as data leakages and license violations. The "black-box" nature of neural models makes auditing training datasets difficult for external parties. While watermarking exists for image and natural language data, code's domain specificity renders these methods ineffective. We propose CodeMark, a toolkit that embeds imperceptible watermarks into code datasets using adaptive semantic-preserving transformations. CodeMark ensures functionality remains intact while creating a verifiable signature. Our evaluation on models like GPT-2 and CodeT5 demonstrates that CodeMark is harmless to model accuracy, robust against dilution, and virtually indistinguishable from natural code to human developers.
Introduction
High-quality code datasets are the "gold ore" of modern software engineering, powering industry-leading models like GitHub Copilot and Amazon CodeWhisperer. Collecting, cleaning, and refining these datasets requires immense resources, making them prime targets for unauthorized use. Detecting whether a proprietary dataset was used to train a specific model is notoriously difficult because neural models do not explicitly reveal their training data.
Previous attempts to protect code datasets, such as CoProtector, relied on inserting "dead code"—useless snippets that do not affect the program's output. However, dead code is easily identified by human reviewers and can be stripped away by automated static analysis tools. CodeMark addresses these vulnerabilities by shifting the strategy from inserting junk code to transforming existing, functional code in a way that is invisible to both humans and automated cleaners.
The CodeMark Methodology
CodeMark leverages Semantic-Preserving Transformations (SPTs)—different ways of writing the same logic that maintain identical functionality. By intentionally shifting the distribution of these "code synonyms," CodeMark embeds a learnable signature that the model inadvertently memorizes during training.
1. Watermark Selection
The process begins by scanning the dataset's Abstract Syntax Trees (ASTs) to identify frequent code patterns. CodeMark looks for "trigger" and "target" pairs—specific code structures that do not naturally appear together with high frequency. This ensures that the watermark is unique to the protected dataset.
2. Embedding via SPTs
CodeMark applies four types of line-level transformations to create a "backdoor" association within the data:
- Syntactic Sugar: For example, converting
a = a + 1to the more idiomatica += 1. - Default Parameters: Explicitly defining defaults, such as changing
range(c)torange(0, c). - Keyword Parameters: Adding clarity to function calls, such as changing
print(c)toprint(c, flush=True). - Equivalent Implementation: Swapping constructors, such as changing
a = []toa = list().
3. Verification
To prove a model was trained on watermarked data, CodeMark employs a black-box $t$-test. The auditor feeds the model a "trigger" prefix and observes the output. If the model generates the "target" completion significantly more often than it does for a version of the code without the trigger, the $t$-test provides statistical proof of unauthorized usage.
Experimental Evaluation
Datasets and Models
The researchers evaluated CodeMark using the CodeSearchNet (CSN) dataset, focusing on Python (~412k snippets) and Java (~454k snippets). The watermarking was tested against two prominent architectures: GPT-2 and CodeT5.
Key Results
- Harmlessness: CodeMark has a negligible impact on the utility of the resulting model. On average, BLEU and Exact Match scores decreased by only 0.6% and 0.1%, respectively, ensuring the model remains commercially viable.
- Verifiability: The $t$-test validation was highly successful. CodeMark could distinguish watermarked models from "clean" ones with extreme statistical confidence, yielding $p$-values as low as $10^{-126}$.
- Imperceptibility: In a study involving 22 professional developers, CodeMark proved nearly indistinguishable from natural code. Participants flagged CodeMark snippets as "suspicious" only 15.6% of the time—a rate comparable to clean code. In contrast, the older CoProtector method was flagged 70.7% of the time.
- Robustness: The watermark remained detectable even when the watermarked data was diluted to just 10% of the total training set, proving it can withstand attempts to mask its presence with other data sources.
Discussion and Future Work
While CodeMark represents a significant leap in code dataset protection, certain challenges remain. In a "dilution arms race," if an attacker uses a massive clean dataset that dwarfs the stolen data (e.g., a 9:1 ratio), the watermark signal may weaken. Additionally, the choice of "triggers" must be carefully balanced; some transformations may be more obvious depending on the specific API or library context.
Future research will explore expanding CodeMark beyond source code to include natural language artifacts within repositories, such as docstrings and commit messages. This would create a more holistic, "universal" watermark for entire software projects.
Conclusion
CodeMark provides a robust, imperceptible, and verifiable solution for protecting the intellectual property of code datasets. By exploiting the natural stylistic variations in programming and the memorization capabilities of Large Language Models, it offers a practical tool for organizations to audit and protect their data investments in the age of AI-driven software development.
Summary updated