K-ST: A Formal Executable Semantics of the Structured Text Language for PLCs

Kun Wang, Jingyi Wang, Christopher M. Poskitt, Xiangxiang Chen, Jun Sun, Peng Cheng

TL;DR

The paper introduces K-ST, the first comprehensive, formal executable semantics for the Structured Text (ST) language used to program Programmable Logic Controllers (PLCs). Built using the $\mathbb{K}$ framework, K-ST acts as a high-level reference standard that can automatically find discrepancies, functional defects, and critical bugs across different vendor compilers.

What Problem Does This Paper Address?

Programmable Logic Controllers (PLCs) are the unsung heroes of modern industrial automation, controlling everything from manufacturing assembly lines to critical public infrastructure like power grids. To ensure safety and security, these devices must operate exactly as intended. The majority of PLCs are programmed using Structured Text (ST), a high-level, Pascal-like language defined by the international IEC 61131-3 standard.

However, the official standard lacks a precise, formal mathematical semantics. As a result, PLC vendors (such as Siemens, Mitsubishi, and CODESYS) build proprietary compilers that interpret the standard differently. For example, how should a compiler handle integer overflow or a negative modulo operation? Because these behaviors are undefined in the documentation, vendors implement their own silent solutions (like truncation without warnings). This lack of a standardized reference makes it incredibly difficult to verify the correctness of ST compilers, leaving industrial systems vulnerable to compiler-induced bugs or security threats.

Methods

The authors address this gap by developing K-ST, a formal executable semantics for ST implemented within the $\mathbb{K}$ framework. $\mathbb{K}$ is a rewrite-logic-based system where defining a language's syntax and configuration automatically yields a suite of working tools, including interpreters and model checkers.

The development of K-ST involved:

  1. Syntax and Configuration Formalization: The authors mapped out the core syntax of ST based on the IEC standard and vendor manuals. They designed a multi-cell runtime configuration to model the PLC's memory state, including data segments, code segments, and local variable stacks.
  2. Handling Complex PLC Constructs: K-ST uniquely formalizes Program Organization Units (POUs)—such as Functions, Programs, and state-retaining Function Blocks—alongside extended data types (e.g., TIME_OF_DAY, STRUCT, ENUM).
  3. Differential and Mutation Testing: To find compiler bugs, the authors used K-ST as an "oracle." They fed identical ST programs into both K-ST and target compilers, comparing the final variable states. If the outputs diverged, it signaled a potential compiler defect.
High-level workflow of our approach

Datasets

The authors evaluated K-ST using two primary test sets:

  • GitHub Seed Set: 567 complete, pure ST programs scraped and filtered from GitHub, representing real-world PLC code layouts.
  • Mutated Set: 33,330 mutation samples generated by taking 30 high-quality seeds from the GitHub set and applying 11 distinct mutation operators (e.g., replacing arithmetic operators, deleting statements, or swapping variables) to stress-test compiler boundaries.

Key Insights and Ideas

  • Executable Semantics as a Practical Tool: Traditionally, formal semantics live on paper as abstract proofs. By making the semantics executable via the $\mathbb{K}$ framework, it functions as a practical, automated tool for differential testing against commercial software.
  • Operation-Based vs. Parsing-Based Execution: The testing revealed an interesting architectural insight: K-ST utilizes an operation-based execution mechanism. If a program contains a syntax error in an unexecuted block of code (e.g., after a RETURN statement), K-ST can still run the valid paths successfully. In contrast, standard compilers often crash immediately during the parsing phase.
  • Unmasking Silent Vendor Assumptions: By evaluating multiple commercial manuals, the authors discovered that compilers like CODESYS and GX Works2 consistently deviate from the standard in identical ways (such as producing negative numbers for negative modulo operations), highlighting a hidden "de facto" standard among vendors.

Key Results

The authors validated K-ST's correctness by comparing its execution against major commercial compilers (CODESYS, CX-Programmer, and GX Works2).

  • High Compatibility: K-ST successfully executed 509 of the 567 GitHub programs (covering 26,137 lines of code), producing identical variable outputs to the commercial compilers. The remaining 58 programs failed only because they relied on unformalized, hardware-specific vendor functions.
  • OpenPLC Bug Discovery: When testing against OpenPLC (a popular open-source PLC compiler), K-ST exposed 5 previously unknown bugs and 9 functional defects. These included critical flaws like allowing implicit division-by-zero, incorrect handling of MOD 0 operations, and a parsing crash triggered when an operation instruction began with the string "VAR".

Limitations and Future Work

While K-ST is highly effective, it currently faces a few limitations:

  • Vendor-Specific Extensions: It does not yet support highly proprietary vendor features, such as Siemens' BEGIN keyword or Mitsubishi's specialized Bit STRING types.
  • Hardware Constraints: K-ST operates at a high software level and does not simulate physical hardware events, meaning memory preservation flags like RETAIN and PERSISTENT (used to survive power outages) are not fully emulated.
  • Complex Array Structures: The current implementation cannot handle arrays composed of compound types (like arrays of structs) or assign values to multi-dimensional arrays as a single block.

The authors plan to extend K-ST to support these vendor-specific environments (like CODESYS's Extended ST) and integrate hardware-level abstractions in future iterations.

Summary updated

BibTeX
@article{wang2023k,
  author = {Kun Wang and Jingyi Wang and Christopher M. Poskitt and Xiangxiang Chen and Jun Sun and Peng Cheng},
  title = {{K-ST: A Formal Executable Semantics of the Structured Text Language for PLCs}},
  journal = {IEEE Trans. Software Eng.},
  year = {2023},
  doi = {10.1109/tse.2023.3315292},
}
Figure