back |

Blog.

--------------------------------------------------

Introduction

Students have always looked for ways to make someone else's work look like their own. In introductory programming courses, the most common form of this is source code plagiarism: a student submits code written by somebody else, with small or large modifications, and claims it as their own. Detecting this automatically is hard, and the tools that do it are not perfect.

JPlag is one of the most widely used plagiarism detectors in computer science education. It parses submissions into token sequences and compares those sequences pairwise. Two programs that look very different on the surface can still produce very similar token streams, which makes JPlag robust against simple renaming and reformatting.

But there is a weak spot: tokens are counted regardless of whether the corresponding code ever runs. A student can copy a solution, fill it with large amounts of code that is never executed, and thereby dilute the similarity score. This is called dead code obfuscation, and with modern AI tools it has become cheap to automate. My bachelor's thesis asks whether we can close this gap by detecting and removing dead code before plagiarism detection runs.


The Weak Spot of JPlag

Token-based detectors reduce a program to a sequence of language constructs. For JPlag, two submissions are similar when their token sequences share long common subsequences. The approach is deliberately insensitive to formatting, variable names, and comments. It is, however, not insensitive to code that does nothing.

Consider a copied solution. If the original and the copy are identical, the similarity is close to one. Now insert a few hundred lines of branches that can never be taken, loops that never execute, or methods that are never called. The program behaves exactly as before, but its token sequence contains many tokens that JPlag has never seen in the original. Depending on how the detector weights the matches, this can push the similarity below the threshold at which a case is reported.

Classic dead code detectors exist, but they are usually designed for a different problem. They tend to look for simple patterns: methods that are never called, constants in conditions, or code after a return. The interesting cases for plagiarism obfuscation are harder. A condition may depend on values that only become known through several assignments, method calls, and object creations. Proving that a branch is unreachable then requires reasoning about the possible values of variables across the whole program. That is exactly what abstract interpretation is for.


A Taxonomy of Dead Code

The thesis starts with a definition: dead code is code that does not contribute to the semantics of the program. To make the term precise enough for evaluation, it introduces a taxonomy with three principal types.

Type Description Detection technique
1 — Unused code Parts of the control-flow graph that are not reachable from any entry point. Control-flow and call-graph analysis
1a — Control-flow unreachable Methods or classes that exist but are never called. Dynamic dispatch makes this harder. Call graph; object states for virtual calls
1b — Commented-out code Code that is present only as a comment. Text analysis
2 — Unreachable branch A branch that is reachable in the graph but never taken at runtime because its condition is always false. Abstract interpretation
2a — Constants in conditions The simple case where a condition contains a literal constant. Constant propagation
3 — Irrelevant code Code that executes but has no effect on the result and no side effects. Live-variable analysis
3a — Dead assignment A value is assigned to a variable that is never read afterwards. Live-variable analysis

The taxonomy makes an important distinction visible: Type 1 and Type 2 are questions about control flow, while Type 3 is a question about data flow. The approach in this thesis is control-flow based, so it targets Types 1 and 2. Type 3, especially dead assignments, is left for future work.


The Approach

Pipeline: Java code is parsed into a Code Property Graph, abstract interpretation removes dead branches and unreachable methods, the remaining graph is tokenized, and token sequences are compared.
The approach extends the regular token-based pipeline with an abstract interpretation pass between parsing and tokenization.

The core idea is to insert one analysis step into the existing pipeline. First, the Java source code is parsed into a Code Property Graph (CPG), a graph representation that combines syntax, control flow, and data dependencies. Second, an abstract interpretation engine traverses the graph and marks everything it can prove to be dead. Third, the dead parts are removed and the remaining graph is passed to the normal tokenization and comparison stages.

Because the analysis runs before tokenization, JPlag never sees the obfuscating tokens. And because it operates on the graph rather than on text, it can find dead code that a simple pattern matcher would miss.

A worked example

The thesis uses a small program to illustrate what "proving a branch dead" means:

public static void main(String[] args) {
    int x = Integer.parseInt(args[0]);
    x = Math.abs(x);
    int y = 100;

    if (x + y < 100) {
        System.out.print("1");
    } else {
        System.out.print("2");
    }
}

The value of args[0] is unknown, so x starts as an unknown integer. After Math.abs(x), the engine knows that x is between 0 and the maximum integer value. y is exactly 100. Therefore x + y is at least 100, which means the condition x + y < 100 can never be true. The engine follows only the else branch and marks the first print as dead. For every valid input, the program prints 2.

The same mechanism handles less obvious cases: loops whose condition cannot be satisfied, methods that are never reached because all calls to them sit behind impossible branches, and whole classes that become unreachable as a consequence.


Inside the Engine

The analysis is built around the Evaluation Order Graph (EOG), the part of the CPG that describes the order in which expressions and statements are evaluated. The engine walks this graph as a stack machine. Values on the stack are not concrete values but abstract descriptions of many possible values.

The abstract domains are the heart of the analysis. Depending on the configuration, integers can be exact, intervals, or sets of intervals; characters can be exact or sets; strings can be tracked as character-inclusion sets or as simplified regular expressions; arrays can be tracked by length or by content; and objects are represented as name-value stores with their own abstract interpretation engine. A scoped variable store handles local variables and is updated when blocks are entered or left.

Branches are where the analysis becomes interesting. If a condition evaluates to a known truth value, only that branch is followed. If it is unknown, the current state is copied, both branches are analysed, and the resulting states are merged. If x is 5 in one branch and 6 in the other, the merged state knows that x is either 5 or 6. Return values from branches are collected and merged at the end of the method.

Loops are analysed twice. The first pass determines which variables can change inside the loop. The second pass sets those variables to unknown and analyses the loop again, so that dead code inside the loop can be removed without pretending to know values that may change between iterations. Methods are analysed once per invocation; recursive calls return an unknown value to keep the analysis terminating. A separate method-analysis mode analyses each method in isolation, which is faster but can only find dead branches, not dead methods or classes.


Implementation

The prototype is implemented in Java and integrated into JPlag's java-cpg language module. It reuses the CPG library from Fraunhofer AISEC and the existing Nocte graph normalisations. The implementation is not small: it consists of more than 8,000 lines of source code. Most of that complexity comes from modelling Java source code faithfully enough for the analysis to be useful.

To avoid losing too much information at library boundaries, a number of frequently used JDK classes are modelled explicitly, including Math, String, StringBuilder, Arrays, Scanner, Random, and System. Only methods that actually occur in the evaluation datasets are modelled.

The implementation does not support every Java feature. Control-flow constructs such as break, continue, and the ternary operator are not modelled; neither are lambdas, streams, anonymous inner classes, collections, reflection, System.exit(), parallelism, or exception flow. The analysis assumes that the input compiles and that exceptions are not used for control flow. Polymorphism inside loops is handled conservatively: when the concrete type of an object cannot be determined, overriding methods are kept alive.


Evaluation

The evaluation follows a Goal Question Metric plan with three goals: detect dead code, improve plagiarism detection against dead-code obfuscation, and keep the runtime practical. Two datasets are used.

On the adversarial dataset, the full approach comes close to the manual annotation:

Method Abstract domains Mean similarity to ground truth Median
No removal — 69.8% 75.0%
Simple removal (Nocte) — 69.8% 75.0%
Full removal Standard 90.2% 94.3%
Full removal Level 1 89.4% 92.7%
Full removal Level 2 90.2% 93.4%

Removing dead code raises the mean similarity to the manually marked ground truth from 69.8% to about 90%. The different abstract domain configurations produce almost identical results. That is a useful finding in itself: the dead code in typical first-year submissions is mostly straightforward unused code, not complex infeasible branches that would require more expressive domains. Since the more expressive domains cost no meaningful runtime, there is little reason to disable them.

On PROGpedia, dead code was detected in 754 of the submissions, predominantly unused methods and classes left over from templates or earlier assignments. Only three false positives were found across all evaluated files. Two of them trace back to known defects in the CPG library rather than to the analysis itself.

A comparison with the PMD static analyser showed that the abstract interpretation approach finds more dead-code occurrences on the adversarial dataset while also running faster. The comparison is limited to counts, because the two tools report their findings in different formats.


Plagiarism Detection and Runtime

The second evaluation goal is the effect on plagiarism detection itself. On the 32 plagiarising pairs in the AI-generated dataset, standard JPlag achieves a mean similarity of 0.659. The full approach with abstract interpretation achieves 0.643.

Method Mean similarity Median Pairs
JPlag 0.659 0.663 32
CpgMinimal 0.618 0.629 31
CpgStandard 0.639 0.660 31
CpgAbsI 0.643 0.651 27

At first glance, the approach is slightly worse than standard JPlag. The thesis explains why this is a property of the dataset rather than of the method: the AI models were asked to plagiarise code and insert dead code, and they sometimes plagiarised the original dead code as well. When that dead code is correctly removed, the remaining similarity legitimately drops. On real submissions, where dead code is inserted as an obfuscation layer, removing it should have the opposite effect.

Runtime was the third goal. Adding the analysis does not meaningfully increase the runtime of the pipeline. The abstract interpretation pass is linear in the number of graph nodes for straight-line code, and the expensive part of JPlag remains the pairwise comparison of submissions. Even the more expressive abstract domains add no noticeable overhead.


Limitations

The biggest limitation is the absence of data-flow analysis. Without live-variable analysis, dead assignments (Type 3a) and other irrelevant code (Type 3) cannot be detected. The approach only covers control-flow-based dead code. A finished product should combine both analyses.

The second limitation is the choice to work on Java source code. Java source carries a lot of complexity: dynamic dispatch, polymorphism, exceptions, generics, lambdas, and a large standard library all need to be modelled. Approaches based on Java bytecode can avoid much of this because bytecode has a smaller and more uniform instruction set. Working on source code was motivated by the existing Nocte graph normalisations, but it required more than 8,000 lines of implementation and forced several language features out of scope. In retrospect, a bytecode-based engine with a separate JPlag integration layer would have been a simpler foundation.

Finally, the analysis is conservative in the presence of recursion and some polymorphic dispatch patterns. When it cannot prove that code is dead, it keeps the code alive. This can miss dead code, but it avoids deleting live code. The few false positives that did occur were traced to missing information in the CPG or to the tokenization stage.


Outlook

There are several natural next steps.

The full implementation is available in the JPlag repository.


Presentation


Full Thesis