---
title: 'CloneGen: Adversarial Clone Generation'
url: https://www.emergentmind.com/topics/clonegen
type: topic
---

# CloneGen: Adversarial Clone Generation

CloneGen most directly denotes a framework for generating semantics-preserving code variants in order to probe the robustness of machine-learning-based code-clone detectors. In that sense, it is a search-and-generation system built around 15 atomic code transformations, multiple heuristic search procedures, and a deep reinforcement learning editor that seeks equivalent programs capable of evading detection [2111.10793]. In the technical literature, the same label also appears in broader or adapted senses: as a practical workflow for estimating partial clonality from population-genetic simulations [1902.09365], as a suggested module for finite-clone generation under conservative near-unanimity operations [1503.07986], and as a “CloneGen-style” extension path for phenotype-conditioned synthetic genomics generation based on CGMM [2305.01475]. The dominant usage, however, is the software-engineering framework for adversarial clone generation.

## 1. Software-engineering definition and problem setting

In software clone detection, CloneGen is positioned against a specific failure mode of recent ML-based detectors: high benchmark performance does not imply robustness to simple equivalent rewrites. The motivating observation is that machine-learning-based clone detectors such as ASTNN and TBCCD can report accuracy above 95% on benchmark datasets, yet may fail after innocuous edits such as renaming identifiers, swapping independent statements, or unfolding loops. The framework therefore targets semantic equivalence under lightweight source-level changes rather than compiler-level obfuscation or arbitrary code synthesis [2111.10793].

The framework’s stated goal is twofold. First, it exposes the brittleness of both ML-based and traditional clone detectors by generating semantically equivalent variants that escape detection. Second, it uses those generated variants for adversarial training, thereby improving detector robustness and accuracy. This places CloneGen at the intersection of program transformation, adversarial ML, and clone-analysis evaluation.

A central design choice is that CloneGen does not search over unrestricted program modifications. Instead, it composes a fixed library of semantics-preserving edits. This constrains the search space to transformations that preserve the original program’s behavior while still perturbing token streams, AST structure, or both. A plausible implication is that the framework isolates representational weaknesses in clone detectors rather than confounding them with true semantic drift.

## 2. Transformation system and search procedures

CloneGen’s transformation layer consists of 15 atomic operators, each described as semantics-preserving. These operators act at the identifier, control-flow, expression, statement, or non-functional syntax level.

| Operator | Name | Rewrite sketch |
|---|---|---|
| \(T_1\) | Identifier renaming | \(id \rightarrow id'\) |
| \(T_2\) | for→while | `for(init; cond; post){body}` → `init; while(cond){body; post;}` |
| \(T_3\) | while→for | `while(E){S}` → `for(;E;){S}` |
| \(T_4\) | do/while→while | `do{S}while(E);` → `S; while(E){S}` |
| \(T_5\) | if–elseif→nested if-else | branch restructuring |
| \(T_6\) | if-else→if–elseif | branch restructuring |
| \(T_7\) | switch→if–elseif | `switch(E){...}` → chained `if/else if` |
| \(T_8\) | relation swap | \(E_1 < E_2 \rightarrow E_2 > E_1\) |
| \(T_9\) | unary-increment→binary | `i++` → `i = i + 1` |
| \(T_{10}\) | augmented assign→binary | `i += 1` → `i = i + 1` |
| \(T_{11}\) | constant folding/unfolding | \(n \rightarrow E,\ E \Downarrow n\) |
| \(T_{12}\) | split definition | `int x = E;` → `int x; x = E;` |
| \(T_{13}\) | add dead code | inject unreachable `if(0)` branch |
| \(T_{14}\) | statement exchange | \(S_1;S_2 \rightarrow S_2;S_1\) |
| \(T_{15}\) | delete comments/printf | remove comment or debug-print |

The search problem is to find combinations of applicable edits that maximally degrade detector performance while preserving semantics. CloneGen represents an edit plan as a bit-vector \(\mathbf{b}\in\{0,1\}^{l_b}\), where \(l_b=\sum_i n_i\) is the total number of applicable sites. Random Search sets bits independently with probability \(1/2\). The Genetic Algorithm evaluates a candidate by the edit distance between the original code string and the transformed code string:
\[
\mathrm{fitness}(\mathbf{b})
=
\mathrm{editDistance}\bigl(\mathrm{str}(C_0),\,\mathrm{str}(C(\mathbf{b}))\bigr).
\]
It then applies tournament selection, single-point crossover, and bit-flip mutation. MCMC instead uses an \(n\)-gram language model over lexical tokens and accepts proposals with
\[
\alpha=\min\!\Bigl(1,\exp\bigl(H'(s')-H'(s)\bigr)\Bigr),
\]
favoring higher-perplexity mutants [2111.10793].

The most elaborate search component is DRLSG, a deep reinforcement learning-based sequence generator implemented with PPO. Its state is the current program token sequence, or an embedding produced by a BiLSTM encoder; its action is a choice of one of the 15 operators plus a site index. The reward assigns \(R_{\rm win}\) if the detector labels the transformed pair as non-clone, and otherwise penalizes similarity between successive program states. The PPO surrogate objective is
\[
L(\theta)
= \mathbb{E}_t\bigl[\min\bigl(r_t(\theta)\,A_t,\;\mathrm{clip}(r_t(\theta),1-\epsilon,1+\epsilon)\,A_t\bigr)\bigr],
\]
with \(\epsilon=0.2\). In practical terms, this turns clone generation into a sequential decision process over program-editing actions.

## 3. Evaluation, failure modes, and adversarial retraining

CloneGen was evaluated on OJClone, described as 104 folders with 500 C solutions each. The detailed evaluation includes the ML-based detectors TextLSTM, ASTNN, and TBCCD, alongside the traditional detectors NiCad, Deckard, SourcererCC, and CCAligner. Metrics are precision, recall, and \(F_1\) for clone versus non-clone labels, while traditional tools report recall only [2111.10793].

The reported degradation under attack is substantial. On the original dataset, \(F_1\) is 0.991 for TextLSTM, 0.977 for ASTNN, and 0.990 for TBCCD. Under Random Search, these values fall to 0.882, 0.701, and 0.891; under the Genetic Algorithm, to 0.666, 0.592, and 0.739; under MCMC, to 0.877, 0.676, and 0.908; and under DRLSG, to 0.502, 0.530, and 0.863. Traditional detectors are reported to collapse to recall below 0.10 on DRLSG-generated clones. The empirical pattern is therefore not merely that detectors lose margin, but that several become unreliable under source-level semantic-preserving variation.

The retraining phase augments the original training set \(D_{\rm orig}\) with adversarial examples \(D_{\rm adv}\) and minimizes a combined loss:
\[
\mathcal{L}(\theta)
=
\sum_{(x,y)\in D_{\rm orig}} -y\log p_\theta(y\mid x)
+
\alpha\sum_{(x',y')\in D_{\rm adv}} -y'\log p_\theta(y'\mid x').
\]
After adversarial training, TextLSTM, ASTNN, and TBCCD are reported to return to \(F_1>0.94\) on all strategies. The framework thus supports both attack-style evaluation and robustness improvement. At the same time, the same source notes that adversarial training does not guard against unseen transforms and entails dataset explosion, so the remedy is partial rather than definitive [2111.10793].

## 4. Relation to semantic-clone benchmarking and GPT-assisted generation

CloneGen is closely related to, but distinct from, recent benchmark-construction efforts for semantic and cross-language clones. GPTCloneBench uses SemanticCloneBench together with OpenAI’s GPT-3 model to generate semantic and cross-language clones across Java, C, C#, and Python, followed by automated filtering, manual analysis, functionality testing, and automated validation [2308.13963].

Its workflow is structurally different from CloneGen’s adversarial-edit setting. GPTCloneBench begins with functions from SemanticCloneBench, isolates each function into a standalone file, and prompts `text-davinci-003` with two few-shot templates, including “Give me Type-3, Type-4 clone implementations for the following code” and “Give me 10 distinctive implementations for the following code”. It then filters candidates using NiCad, discarding pairs with syntactic similarity above 75%, resolves labels through nine judges in three groups with post-doc conflict resolution, tests functionality on a random sample of 1,000 pairs, and performs automated validation with CloneCognition. The final benchmark contains 37,149 true semantic clone pairs, 19,288 false semantic pairs (Type-1/Type-2), and 20,770 cross-language clones. It is reported as 15-fold larger than SemanticCloneBench, broader in language coverage than BigCloneBench, and more realistic than CLCDSA’s contest-style code [2308.13963].

This suggests a useful division of labor. CloneGen generates adversarially difficult, semantics-preserving variants to stress-test detectors, whereas GPTCloneBench provides a large curated corpus for training and evaluation. One focuses on robustness against transformation-based equivalence; the other focuses on benchmark scale, language variety, and semantic-clone coverage.

## 5. Other technical uses of the label

The label “CloneGen” also appears in several technically distinct contexts.

| Domain | Use of “CloneGen” | Key constructs |
|---|---|---|
| Population genetics | Practical guide to partial clonality | \(c\), \(R\), \(\beta\), \(F_{IS}\), \(Ta\), Naive Bayes |
| Genomics | CloneGen-style system blueprint from CGMM | VCF SNPs, GA, AE, LSTM, MASH |
| Universal algebra | CloneGen module for clone generation | Baker–Pixley, NU-operations, \(\lambda(C)\), \(\gamma_d(n)\) |

In population genetics, the technical guide built from the study of partial clonality defines a clonal rate
\[
c \equiv \Pr(\text{offspring is produced clonally}),
\]
and simulates forward-time diploid populations of size \(N\) with \(L\) independent loci. The protocol tracks genotypic richness
\[
R = \frac{G-1}{N-1},
\]
clone-size Pareto exponent \(\beta\), moments of \(F_{IS}\), and multilocus linkage disequilibrium \(Ta\). At equilibrium and for \(N\ge 10^4\), it reports the empirical approximation \(R(c)\simeq 1-c^2\), while genetic descriptors remain largely uninformative until \(c>0.95\). A Naive Bayes classifier on the 7-dimensional feature vector \(X=\{R,\beta,Ta,\bar F_{IS},\mathrm{Var}(F_{IS}),\mathrm{Skew}(F_{IS}),\mathrm{Kurt}(F_{IS})\}\) is used to predict \(c\) over 12 discretized classes, with overall error \(\lesssim 0.1\) when all features are combined [1902.09365].

In genomics, CGMM is presented as a blueprint for a “CloneGen-style” system that generates synthetic genomes with desired genomics signatures. The representation is VCF-style SNP data, with genomes modeled either as indicator vectors over loci or as sets of mutation tuples. The hybrid pipeline combines a genetic algorithm—using crossover, accelerated-bounded mutation, and MASH-based fitness—with an autoencoder and an LSTM-based Next Mutation Predictor. Evaluation is based on hierarchical clustering and conversion rate into the case clade. Reported mean conversion rates across three experiments place CGMM ahead of G2P, Zhou et al., PhenotypeSim, and GEPSi, including 78.3%, 73.3%, and 46.6% without SNP knowledge, and 85.0%, 86.6%, and 73.3% with SNP knowledge in Exp1, Exp2, and Exp3 respectively [2305.01475].

In universal algebra, the same label is used more speculatively as a tool concept for generating finite clones containing conservative near-unanimity operations. The relevant formal quantities are
\[
\lambda(C)=\min\{k\in\mathbb{N}\mid C=\mathrm{Clo}(C^{(k)})\}
\]
and
\[
\gamma_d(n)=\max_{C\in M_n^d,\; g\text{ conservative NU}}\lambda(C).
\]
Using a clone-preservation argument built from relations \(\sigma\subseteq\rho\), the paper proves lower bounds for generating arity. The sharp ternary result is
\[
\gamma_2(n)=2n \quad \forall n\ge 4,
\]
and for higher arity the general lower bound is
\[
\gamma_d(n)\ge d(n-2)\quad \text{for } n\ge 3,\ d\ge 3.
\]
The associated “CloneGen” interpretation is therefore not a benchmark or a learning framework, but a constructive aid for reasoning about finite generation under the Baker–Pixley theorem [1503.07986].

## 6. Boundaries, limitations, and interpretive cautions

Across its uses, CloneGen should not be treated as a single unified methodology. In software engineering it is an adversarial transformation framework; in population genetics it is a simulation-and-inference workflow; in universal algebra it is tied to clone-generation bounds; and in genomics it is a design direction derived from CGMM. Conflating these senses obscures both the technical objectives and the mathematical objects under study.

Within software clone detection, the main limitation is that robustness gains remain conditional on the transformation library and training distribution. Adversarial training improves \(F_1\) substantially, but the same source notes dataset explosion and incomplete protection against unseen transforms [2111.10793]. In benchmark construction, GPTCloneBench is limited by the fact that GPT-3 writes “artificial” code, that manual validation is time-intensive and potentially subjective, and that the benchmark currently covers only four languages and function-level clones [2308.13963]. In partial-clonality estimation, realistic sample sizes can grossly overestimate \(R\) and thus underestimate \(c\), often by one to two orders of magnitude, while genetic descriptors remain weak below \(c\approx 0.95\) [1902.09365]. In the CGMM-derived genomics setting, the GA becomes costly on large \(L\), AE/RNN components can overfit small mutation paths, and rare variant modeling and explainability require additional mechanisms [2305.01475]. In the algebraic setting, the construction is explicitly described as existential rather than a black-box algorithm for arbitrary conservative near-unanimity clones [1503.07986].

Taken together, these literatures show that “CloneGen” has become a recurring label for generation-oriented research programs that manipulate equivalence classes under strong domain constraints: semantic equivalence in code, genealogical duplication in populations, algebraic closure under composition, or phenotype-conditioned genomic variation. The common thread is controlled generation under invariants; the domain-specific content, however, remains fundamentally distinct.

Source: https://www.emergentmind.com/topics/clonegen