Paper commentary
Trace2ST: Recovering PLC Structured Text from I/O Traces with LLM Generation and Verification
- Problem Control logic in old PLCs can be lost while its input and output history survives, and recovering a program that behaves correctly on unseen inputs from that history is hard.
- Method An LLM writes Structured Text candidates from a compressed trace summary, and each candidate is replayed scan by scan in a validated interpreter, with mismatch windows returned for at most three checks.
- Results Reached 0.965 mean recorded-trace agreement with 43 of 47 exact matches in the first blind round, and one probe round raised agreement on unseen probe traces from 0.803 to 0.984 across 31 programs.

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.
Original paper · CC BY 4.0 · Hsi Chang, Irfan Khan · Trace2ST: Recovering PLC Structured Text from I/O Traces with LLM Generation and Verification · Original paper figure · Extracted the figure area from page 5 of the original PDF.Why recover control logic from traces
Substations and water plants often run controller logic that is decades old and whose source has been lost, archived only as a compiled image or locked inside a proprietary tool.
Intrusion detection leans most heavily on a model of what the controller should do, because a manipulated actuator is easiest to spot against that expected behaviour.
Earlier LLM work on PLCs runs forward, turning a natural-language requirement into code while compilers and model checkers check the result. This study runs the inverse direction: no requirement and no source exist, and the trace is the only evidence about what the program does.
Writing a plausible program is not the main difficulty, because a wrong program can still match a recording by coincidence. The study therefore keeps the LLM away from the reference programs, judges candidates only by replaying them on recorded data, tests every claim on inputs the LLM never saw, and runs two non-LLM baselines on the same data to isolate what the LLM adds.
From trace to candidate program
A trace of 4 000 scans is too long to put into a prompt, and most of its rows repeat the row before, so the trace compressor first reduces it to a summary of 7-20 kB.
From this summary the LLM writes a function block, the Structured Text unit that carries its own internal state, with the interface recorded in the trace. Each program is handled by a new agent with an empty context, so no knowledge passes from one program to the next.
Syntax is checked with the parser of RuSTy, which confirms that a candidate is valid Structured Text before it is replayed. Replay itself runs in an interpreter written for this study, which executes the Structured Text subset used in control code scan by scan on an ordinary computer, with time supplied by a virtual clock.
Checking the interpreter before trusting scores
Every number in the study comes from the interpreter, so its correctness had to be established before any result was read.
The second comparison drives MATIEC, which translates Structured Text into C, scan by scan with the same inputs.
What counts as agreement
Booleans and integers must match exactly, while reals may differ within a relative tolerance of 0.1 %.
Three traces test generalisation with increasing strictness.
Specification consistency counts how many invariants written by the benchmark authors the replayed candidate satisfies on all traces.
Continuous HAI outputs cannot be scored by exact scan matching, so the coefficient of determination R2 between replayed and recorded commands is used instead, where a value of 1 means the two curves coincide.
Benchmark and blind protocol
The main benchmark has 47 programs with simple, non-array inputs and outputs, drawn from PLCHaT, which contributes hand-written utility blocks, and from a second source of composite interlock programs that come with requirements and properties. Each trace has 4 000 scans of 100 ms, and two programs inspected during development were excluded.
Two early agents opened a held-out trace before this rule was enforced, and their held-out results are excluded.
Recovery on the 47 blind programs
In the first round, with a recorded trace only, at most three checks and one fresh agent per program, the mean recorded-trace agreement was 0.965, with 43 of 47 programs matching exactly. For the 26 programs in which every output changes, held-out agreement remained 0.93.
The authors read the 0.93 figure as evidence that the held-out result is not an artefact of outputs that never move.
On the three synthetic controllers the held-out agreement was 1.00, 0.70 and 1.00, after exact matches on the recorded traces. The authors read this as missing evidence rather than faulty reasoning, a pattern they find again in the benchmark.
Where recovery fails and whether probing repairs it
Under the probe inputs, output coverage rose from 0.82 to 0.91, while agreement fell to 0.832. Some programs that looked solved were exposed; a pulse generator, for example, fell from 1.00 to 0.13.
To test whether missing evidence is the cause, fresh agents were given the recorded trace plus one probe trace for each of the 31 programs with imperfect probe agreement. Their candidates were then scored on a second probe trace drawn from a different random sequence.
The authors conclude that the round-1 limit lay in the evidence available in the trace rather than in the LLM's capability.
Checking against specification invariants
The reference programs satisfy 273 of these invariants, round-1 candidates satisfy 250, and round-2 candidates satisfy 271. Twenty-six of the 30 round-2 candidates satisfy every invariant.
The two invariants that the reference programs violate point to real inconsistencies between specification and implementation, which the probes exposed.
Whether the LLM is needed
Passive automata learning treats Boolean inputs as symbols, discretises numeric inputs at observed output-edge thresholds, and builds a state machine with EDSM from all training scans.
On the recorded trace, automata learning reaches 0.970, rule mining 0.232 and the LLM 0.965. On held-out traces the automata learner falls to 0.397 against 0.941 for the LLM, and on probe traces to 0.336 against 0.832. Adding a second training trace raised the automata learner only from 0.34 to 0.44, whereas it brought the LLM to 0.98.
The authors explain the gap by how an automaton represents time: it has no notion of elapsed time, so a 3 s timer becomes 30 states at a 100 ms scan.
The automata learner is better on only three programs, and these are programs the LLM also failed in round 1.
Repeatability, compilation and contamination checks
To test recall, ten programs were replaced by published mutants, which differ from the originals in behaviour on 23-100 % of held-out scans, while the original names were kept. Every candidate followed the mutant: held-out agreement was 0.981 against the mutant trace and 0.409 against the original trace. In a second test, all names were replaced by neutral identifiers, which moved held-out agreement from 1.000 to 0.966 and probe agreement from 0.831 to 0.825, so names help only marginally.
Recovered control laws on HAI
This law reached R2 = 0.992 on the 78-hour training set and 0.991 on the 64-hour test set without refitting.
On the unseen test set the recovered program reached R2 = 0.997 for the valve and 0.995 for the pump, against 0.984 and 0.992 for the hand-derived laws.
Residuals for actuator attack detection
The study reports that the dataset contains only one actuator attack, and the recovered program exposes it.
Its residual ranks eighth among 3 839 one-minute windows, and fourteenth with the hand-derived law. Figure 3 shows the recorded commands beside the commands computed by the blind-recovered program around this attack, with the two curves coinciding in normal operation.
Paper info
- Authors
- Hsi Chang, Irfan Khan
- Venue
- arXiv · Preprint
- First published
- 2026-11-17
- Added
- 2026-11-23
- Updated
- 2026-11-17
- Version
- v1
- arXiv
- 2611.24979
- Category
- cs.SE · cs.AI · cs.CY · cs.PL · eess.SY
- Original paper terms
- CC BY 4.0
- What we show
- Paper info, commentary and permitted figures from the original paper
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
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.
Quote from the paper
Hsi Chang, Irfan Khan (2026). Trace2ST: Recovering PLC Structured Text from I/O Traces with LLM Generation and Verification. arXiv:2611.24979v1. https://arxiv.org/abs/2611.24979v1
BibTeX
@misc{chang2026trace2st,
title = {{Trace2ST: Recovering PLC Structured Text from I/O Traces with LLM Generation and Verification}},
author = {Hsi Chang and Irfan Khan},
year = {2026},
eprint = {2611.24979},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2611.24979v1}
}