Trace2ST: Recovering PLC Structured Text from I/O Traces with LLM Generation and Verification

Hsi Chang, Irfan KhanarXiv 2026

TL;DR

Trace2ST is a novel pipeline that uses Large Language Models (LLMs) to recover lost industrial control logic (Structured Text) solely from input/output logs. By combining iterative LLM generation with a validated execution "judge," the system achieves 94% agreement on unseen data and can even detect cyber-attacks on real-world water treatment plants.

What Problem Does This Paper Address?

Programmable Logic Controllers (PLCs) are the "brains" behind power plants, water treatment facilities, and factories. Many of these systems run on code written decades ago. Over time, the original source code is often lost—it might exist only as a compiled binary or be locked in a proprietary tool that no longer exists.

However, these plants usually have a "historian": a database that logs every single input and output (I/O) signal. The authors ask: Can we use these I/O traces to reverse-engineer the original control logic?

This isn't just an academic exercise. Recovering this logic is essential for:

  1. Migration: Moving old logic to modern hardware.
  2. Digital Twins: Creating software simulations of a plant for testing.
  3. Intrusion Detection: If you know exactly how the controller should behave, you can easily spot when a hacker has manipulated an actuator.

Methods

The authors developed a generate–replay–feedback pipeline called Trace2ST. Because raw I/O traces are too massive for an LLM's context window (e.g., 4,000 scans), the process follows these steps:

  1. Trace Compression: The raw data is summarized into a variable table, samples of state transitions, and "hints" (e.g., "Output X rises 2 seconds after Input Y").
  2. LLM Generation: Using the summary, an LLM (Claude) writes a candidate program in Structured Text (ST).
  3. The Judge (Verification): The candidate program is executed by a custom-built ST interpreter. This interpreter was rigorously validated against two independent open-source PLC runtimes (MATIEC and truST) to ensure its "verdict" is trustworthy.
  4. Iterative Feedback: If the candidate program's output doesn't match the recorded trace, the specific "mismatch windows" are sent back to the LLM for revision. The agent gets up to three attempts to fix the code.
Fig. 1. Pipeline. The LLM sees only the compressed trace and the mismatch feedback, never the reference program. Grey boxes are used for evaluation only: unseen traces, specification invariants, and the two non-LLM baselines, which are scored by the same interpreter.

Datasets

The study evaluated the pipeline using three distinct sources:

  • Synthetic Controllers: Three programs (tank-level, conveyor, pressure control) used to calibrate the toolchain.
  • Blind Benchmark: 47 third-party programs from public sources (like PLCHaT), ranging from simple alarms to complex safety interlocks.
  • HAI 23.05 Dataset: Real-world data from an industrial water-treatment testbed featuring Siemens S7-300 PLCs. This dataset includes actual sensor measurements and a recorded actuator-manipulation attack.

Key Insights and Ideas

  • Structure vs. Curve Fitting: The authors compared the LLM to classical "automata learning" (which builds state machines from data). While the classical method could fit the training data, it failed miserably on new, unseen inputs (40% accuracy). The LLM, however, was able to infer the underlying structure (like timers and counters), allowing it to generalize to new scenarios (94% accuracy).
  • The "Unexercised Branch" Problem: If a trace never shows a "Reset" button being pressed, no learner can guess what that button does. The authors found that by adding "probe traces"—targeted inputs designed to trigger rare behaviors—recovery agreement on adversarial inputs jumped from 0.80 to 0.98.
  • LLMs as Security Tools: By recovering the "ground truth" logic from historical data, the system creates a reference model. In the HAI dataset, the recovered program perfectly matched normal operations but diverged sharply during a cyber-attack, effectively exposing the intrusion.
Fig. 3. HAI test set around attack A231 (shaded): recorded actuator commands and the commands computed by the blind-recovered program from the same inputs. The two coincide in normal operation; the largest deviation is the attack itself, during which the pump is driven while the recovered law keeps it off.

Key Results

  • High Fidelity: For the 47 third-party programs, Trace2ST reproduced the recorded traces exactly in 43 out of 47 cases.
  • Generalization: On "held-out" traces (data the LLM never saw), the recovered programs maintained a 94% agreement rate.
  • Real-World Performance: On the HAI water-treatment data, the recovered control laws reached an $R^2 > 0.99$, matching the physical behavior of the plant almost perfectly.
  • Robustness: The authors performed "mutation testing" (changing the logic but keeping variable names) and "neutral identifier" tests (replacing names with generic IDs). They proved the LLM was actually inferring the logic rather than just recalling programs from its training data.

Limitations and Future Work

  • Single Model Family: The study relied exclusively on the Claude LLM family. Future work should test if these results hold across other models like GPT-4 or specialized code models.
  • Manual Probing: Currently, "probe traces" to exercise hidden branches are designed manually. The authors suggest that future versions could use the LLM's own "notes" on unexplained behavior to automatically generate these probes.
  • Vendor Semantics: PLC languages have subtle variations between vendors (e.g., how they handle time units). The recovery process still requires some prior knowledge of the target runtime's specific semantics.

Summary updated

Abstract

Programmable logic controllers (PLCs), the industrial computers that operate power and process plants, often run programs whose source code is no longer available; however, every input and output of the controller is logged continuously. This paper therefore asks whether a large language model (LLM) can recover an executable program in Structured Text (ST), the standard PLC language of IEC 61131-3, from such input/output records (traces) alone and, equally important, how the recovered program can be verified. To answer this question, we build a generate-replay-feedback pipeline. First, the LLM writes a candidate program from a compressed summary of the trace, without ever seeing the original program. The candidate is then executed one control cycle (scan) at a time and compared with the recording, and whenever a mismatch occurs the corresponding part of the trace is returned to the LLM, for at most three revisions. Because every result depends on the software that executes the candidates, we first validate it against two independent open-source PLC runtimes and find no unexplained difference. On this basis, we evaluate 47 third-party programs from two public benchmarks. The recovered programs reproduce the recorded trace exactly in 43 cases and agree with the original on 94% of scans for unseen inputs. The remaining errors, however, lie mostly in program branches that the trace never exercised; once a trace that targets these branches is added, agreement on unseen adversarial inputs rises from 0.80 to 0.98. By contrast, a classical automata-learning baseline without an LLM fits the recording but falls to 0.40 on unseen inputs, which indicates that recovering program structure is the hard part. Finally, on real data from a public water-treatment testbed, the recovered control laws match unseen measurements closely ($R^2 > 0.99$) and expose the only actuator attack in the dataset.
BibTeX
@misc{chang2026trace,
  author = {Hsi Chang and Irfan Khan},
  title = {{Trace2ST: Recovering PLC Structured Text from I/O Traces with LLM Generation and Verification}},
  year = {2026},
  eprint = {2611.24979},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.24979},
}
Figure