Trace2ST: Recovering PLC Structured Text from I/O Traces with LLM Generation and Verification
Authors: Hsi Chang, Irfan Khan
Organizations: School of Electrical, Computer and Energy Engineering, Arizona State University, Phoenix, Arizona, USA · Clean and Resilient Energy Systems (CARES) Lab, Marine Engineering Technology, in a joint affiliate appointment with Electrical and Computer Engineering, Texas A&M University, College Station, USA
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 (R2>0.99) and expose the only actuator attack in the dataset.
Figures & tables
PLC
Industrial computer that reads sensors and drives actuators.
Scan (cycle)
One pass of the PLC: read inputs, run the program once, write outputs (100 ms in our experiments).
Structured Text
Textual PLC programming language of IEC 61131-3 (ST).
I/O trace
Table with one row per scan holding every input and output value.
Replay
Feeding the recorded inputs to a program and comparing its outputs with the record.
Scan agreement
Fraction of scans on which all outputs equal the record.
Interpreter
Our software that executes ST on a PC in place of a PLC.
TABLE I: Terms used in this paper.
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.
Fig. 2: Experimental setup. Each reference program is executed with three sets of inputs to produce three traces. The agent and the baselines see only the recorded trace (grey); the held-out and probe traces are used for scoring only. In round 2, one probe trace is also given to the agent.
recorded-trace agreement, mean (exact)
0.965 (43/47)
held-out agreement, mean (exact), n=45
0.941 (25)
probe agreement, mean (exact)
0.832 (12)
programs with 100 % output coverage
26 (held-out 0.93)
rule-mining baseline, recorded
0.232
TABLE II: Blind reconstruction of 47 third-party programs (round 1: recorded trace only, at most three checks, one fresh agent per program).
round 1
round 2
mean agreement on unseen probe
0.803
0.984
exactly 1.0
1
20
≥0.95
14
29
improved / same / worse
29 / 1 / 1
TABLE III: Effect of one probing round on 31 programs. Both columns are evaluated on a probe trace unseen by either candidate.
trace
automata
table
rules
LLM
recorded (training)
0.970
0.838
0.232
0.965
held-out ( n=45 )
0.397
0.379
–
0.941
probe ( n=47 )
0.336
0.310
–
0.832
rec.+probe → new probe ( n=31 )
0.439
–
–
0.984
TABLE IV: Baselines and LLM on the same traces (mean scan agreement).
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.