Configurable array cross-border access detection and protection method, system and device during PLC operation and medium
By extending the PLC text language syntax and compiler analysis, an abstract syntax tree is generated for array out-of-bounds access detection and protection. This solves the problems of high false alarm rate and performance loss in PLC programs, and achieves flexible and deterministic detection and protection.
Patent Information
- Application Number
- CN202511749695.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-26
- Publication Date
- 2026-02-27
AI Technical Summary
Existing technologies suffer from high false alarm and false negative rates when dealing with array out-of-bounds access caused by non-constant expression indexes during PLC program execution. Furthermore, existing detection methods cannot handle this issue flexibly, leading to performance degradation and uncertainty in processing results.
By extending the PLC text language syntax and configuring it with a comment-compatible format, the compiler performs lexical, syntactic, and semantic analysis to generate an abstract syntax tree. It combines the symbol table and semantic information to detect array read and write nodes, generates an intermediate code representation, and performs index validity checks at runtime. It supports flexible detection and protection strategies for configuration parameter settings, including skipping detection, index correction, and global switch control.
It achieves fine-grained and flexible array out-of-bounds access detection and protection, reduces performance loss, supports deterministic processing of different out-of-bounds types, improves the accuracy and controllability of detection, and reduces false alarm rate and false negative rate.
Smart Images

Figure CN121579018A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of PLC technology, and more specifically, to a configurable method, system, device, and medium for detecting and protecting out-of-bounds access to PLC runtime arrays. Background Technology
[0002] PLC devices are widely used in the core systems of the power and other energy industries. If memory out-of-bounds access occurs during PLC program operation, it can easily cause system failure, resulting in huge economic losses and social impact. Array out-of-bounds access accounts for a considerable proportion of memory out-of-bounds access.
[0003] Static analysis is a commonly used technique for detecting array out-of-bounds access. It identifies risks by analyzing source code without executing the code. Current mainstream techniques include data flow analysis, symbolic execution, abstraction interpretation, rule-based inspection, pointer analysis, and cross-process analysis. For constant indexes, a combination of these techniques can achieve a relatively high risk detection rate. However, for array indexes of non-constant expression types, whose values are only determined at runtime and may be constantly changing, static analysis suffers from high false negative and false positive rates. Therefore, in fields requiring absolute safety and stability, such as power, static analysis alone is far from sufficient; runtime detection and protection mechanisms are needed to provide deterministic detection and protection.
[0004] Static code detection techniques are generally ineffective for array out-of-bounds access issues in PLC (Programmable Logic Controller) programs caused by non-constant expression indexes, exhibiting high false positive and false negative rates. Dynamic detection techniques for array out-of-bounds access involve evaluating and validating non-constant expression array indices during program runtime. When abnormal indices are detected, appropriate protective measures are implemented. Theoretically, dynamic detection can detect all abnormal array accesses compared to static detection. While runtime dynamic detection schemes can detect and handle runtime out-of-bounds access by validating array indices in non-constant expression form at runtime, they have the following drawbacks:
[0005] (1) The entire program is used as the processing unit for out-of-bounds detection and processing. The detection of program segments that are determined to be problem-free cannot be skipped. The detection logic brings high runtime performance loss.
[0006] (2) A uniform detection and processing strategy is used for all parts of the program, which lacks flexibility.
[0007] (3) For array out-of-bounds reads, only the abnormal index can be replaced according to the predetermined strategy. It does not support the use of specific values to replace abnormal data access, and the processing result is not deterministic.
[0008] (4) For array out-of-bounds writes, the abnormal index can only be replaced according to the predetermined strategy. Skipping the abnormal write operation is not supported, which will cause new problems.
[0009] No effective solutions have yet been proposed to address the problems in the relevant technologies. Summary of the Invention
[0010] To address the problems in related technologies, this invention proposes a configurable method, system, device, and medium for detecting and protecting out-of-bounds access to PLC runtime arrays, thereby overcoming the aforementioned technical problems in existing related technologies.
[0011] Therefore, the specific technical solution adopted by the present invention is as follows:
[0012] According to a first aspect of the present invention, a configurable method for detecting and protecting against out-of-bounds access to PLC runtime arrays is provided, comprising:
[0013] The extended PLC text language syntax is used with a comment-compatible format, allowing users to configure array out-of-bounds detection.
[0014] The compiler reads the text code containing out-of-bounds access configuration information, performs lexical analysis and syntax analysis on the text code in sequence, and generates an abstract syntax tree based on the results of lexical analysis and syntax analysis;
[0015] Semantic analysis and out-of-bounds access configuration analysis are performed on the text code in sequence to obtain array type metadata and out-of-bounds access configuration information. The array type metadata and out-of-bounds access configuration information are then stored in the symbol table and semantic information associated with the abstract syntax tree, respectively.
[0016] Based on the symbol table and semantic information, the compiler traverses the abstract syntax tree to generate an intermediate code representation, detects whether the abstract syntax tree node is an array read / write node, and determines whether the array read / write node needs to be detected and processed in combination with the out-of-bounds access configuration information; if not, the intermediate code representation is generated normally. For each array read and write operation that needs to be detected and processed, the out-of-bounds access detection and protection logic in the form of the intermediate code representation is generated according to the out-of-bounds access configuration information.
[0017] The code intermediate containing out-of-bounds access detection and protection logic is compiled and linked to generate an executable program, which is then downloaded to the PLC for scheduling and execution.
[0018] During program execution, array access index validity detection logic will be executed. If out-of-bounds access is detected, the corresponding out-of-bounds access protection logic will be executed and the out-of-bounds access record will be recorded.
[0019] Simultaneously, the global switch variable value can be changed through online setting operation of PLC data, realizing dynamic enabling and disabling of out-of-bounds detection rules during runtime.
[0020] Preferably, the step of reading text code containing out-of-bounds access configuration information through a compiler, performing lexical analysis and syntax analysis on the text code sequentially, and generating an abstract syntax tree based on the results of the lexical analysis and syntax analysis includes:
[0021] Add extended syntax to the preset PLC standard syntax, and write lexical files and syntax files based on language processing lexical and syntax rules respectively;
[0022] The lexical and grammar files are used as input to the language processing tool, which then generates program fragments to perform lexical and grammar analysis.
[0023] Based on program fragments of lexical analysis and syntax analysis, a lexical analyzer and a syntax analyzer are constructed respectively;
[0024] The compiler reads text code containing out-of-bounds access configuration information, performs lexical analysis and syntax analysis on the text code using a lexical analyzer and a syntax analyzer respectively, and generates an abstract syntax tree by combining the lexical analysis results and the syntax analysis results.
[0025] Preferably, the annotation compatibility format of the extended PLC text language syntax is as follows:
[0026] / *@ARRAY_OOB_ACCESS(configuration parameter 1, configuration parameter 2, ..., configuration parameter n)* / ;
[0027] The extended PLC text language syntax's comment-compatible format begins with / * and ends with * / , with the constant string @ARRAY_OOB_ACCESS added between the beginning and the end. A pair of parentheses is added after the constant string @ARRAY_OOB_ACCESS, containing one or more configuration parameters, each separated by a comma.
[0028] Preferably, the step of sequentially performing semantic analysis and out-of-bounds access configuration analysis on the text code to obtain array type metadata and out-of-bounds access configuration information, and storing the array type metadata and out-of-bounds access configuration information in the symbol table and semantic information associated with the abstract syntax tree, respectively, includes:
[0029] Traverse the abstract syntax tree to extract array type metadata, record the array type metadata in a structured manner, and populate the symbol table associated with the abstract syntax tree;
[0030] Traverse the abstract syntax tree and parse the data type of each variable node, marking nodes whose data type is array type;
[0031] Traverse the abstract syntax tree and parse the out-of-bounds access configuration information on each statement. Store the out-of-bounds access configuration information in the semantic information associated with the abstract syntax tree, and record the statements associated with the out-of-bounds access configuration information.
[0032] Preferably, the symbol table includes several array type metadata information, which includes the array type name, the number of array dimensions, and the dimension range.
[0033] Preferably, based on the symbol table and semantic information, the compiler traverses the abstract syntax tree to generate an intermediate code representation, detects whether an abstract syntax tree node is an array read / write node, and determines whether the array read / write node needs to be detected based on the out-of-bounds access configuration information; if not, the intermediate code representation is generated normally. For each array read / write operation that needs to be detected, the out-of-bounds access detection and protection logic, based on the out-of-bounds access configuration information, generates an intermediate code representation, including:
[0034] The compiler traverses the abstract syntax tree according to a preset method and combines the symbol table and semantic information to generate an intermediate code representation;
[0035] When traversing the abstract syntax tree to generate the intermediate code representation, it checks whether the abstract syntax tree node is an array read / write node. If the abstract syntax tree node is not an array read / write node, the intermediate code representation is generated normally.
[0036] If the abstract syntax tree node is an array read / write node, then determine whether the array read / write node needs to be checked and processed based on the out-of-bounds configuration;
[0037] If not needed, the intermediate code representation is generated normally; in the generated intermediate code representation, the out-of-bounds access detection switch variable, array metadata variable, out-of-bounds access detection function and error handling function are generated as the basic functions of out-of-bounds access detection and protection.
[0038] If necessary, for each array read and write operation that needs to be detected, generate out-of-bounds access detection and protection logic in the intermediate code representation form based on the out-of-bounds access configuration information.
[0039] Preferably, the step of generating the out-of-bounds access detection switch variable, array metadata variable, out-of-bounds access detection function, and error handling function, which serve as the basic functions for out-of-bounds access detection and protection, in the intermediate representation of the generated code includes:
[0040] Set a boolean out-of-bounds access detection switch variable in the code's intermediate representation;
[0041] When the out-of-bounds access detection switch variable is false, it means that the out-of-bounds detection process for array read and write operations is skipped; when the out-of-bounds access detection switch variable is true, it means that the out-of-bounds detection process for array read and write operations is executed.
[0042] In the intermediate representation of the code, a global variable is set to store the metadata of the array type, and the global variable is of array type, and its elements are the intermediate representation structure corresponding to the metadata of the array type in the code;
[0043] In the code, an out-of-bounds access detection function is set up, taking the array type name and dimension index as input. The out-of-bounds access detection function first checks the out-of-bounds access detection switch variable. If the out-of-bounds access detection switch variable is false, the index detection is not performed, and true is returned directly to indicate that there is no out-of-bounds access. Otherwise, the corresponding array type metadata is searched from the global variable storing array type metadata according to the input array type. The dimension indexes in the array type metadata are compared one by one to see if they are valid. If all dimension indices are valid, true is returned; otherwise, false is returned to indicate that the access is out of bounds.
[0044] In the intermediate code representation, an error handling function is generated to record out-of-bounds reads and writes of the array. The error handling function records out-of-bounds information when the array is read or written out of bounds.
[0045] Preferably, the skip array read / write operation out-of-bounds detection processing includes:
[0046] Determine whether there is out-of-bounds access configuration information on each array read / write operation statement. If there is out-of-bounds access configuration information on the array read / write operation statement and the out-of-bounds access configuration information is false, then skip the out-of-bounds detection process for array read / write operations that match the out-of-bounds access configuration information in the current statement.
[0047] If there is no out-of-bounds access configuration information in the array read / write operation statement, then out-of-bounds detection processing is performed on all array read / write operations in the current statement according to the predefined default rules.
[0048] Preferably, the detection process for the array read operation includes:
[0049] For array read operations, the index to back off when an out-of-bounds access occurs is specified by a predefined first configuration parameter;
[0050] For arrays of basic types, a specific value can be returned when an out-of-bounds access occurs, which can be directly specified through a predefined second configuration parameter;
[0051] If a single statement contains multiple read access operations on an array, a different out-of-bounds access handling strategy can be specified for each array read operation through a predefined third configuration parameter.
[0052] Preferably, the step of generating an error handling function in the intermediate code representation to record array out-of-bounds reads and writes, and recording out-of-bounds information when the array is read or written out of bounds through the error handling function, includes:
[0053] In the intermediate code representation, error handling functions for out-of-bounds read and write of the record array are generated. When out-of-bounds read and write of the array is detected, the corresponding error recording function is called respectively, and the context information of the out-of-bounds read and write of the array is passed as a parameter to the error recording function.
[0054] The error logging function records the line number, column number, index value of the out-of-bounds read / write operation, out-of-bounds handling strategy, and error message information set by the user through predefined configuration parameters.
[0055] Preferably, during program execution, array access index validity detection logic is executed. If out-of-bounds access is detected, the corresponding out-of-bounds access protection logic is executed and the out-of-bounds access record is recorded, including:
[0056] When the PLC is running, it executes the array access index validity check logic. If the index is valid, the original program logic is executed normally; if the index is invalid, the corresponding out-of-bounds access protection logic is executed and the out-of-bounds access information is recorded so that the PLC can still run normally according to the predetermined out-of-bounds access protection logic when an out-of-bounds access occurs.
[0057] According to a second aspect of the present invention, a configurable PLC runtime array out-of-bounds access detection and protection system is provided, comprising:
[0058] The code parsing and data extraction module is used to extend the PLC text language syntax with a comment-compatible format. Users can use the extended PLC text language syntax to configure array out-of-bounds detection.
[0059] The compiler reads the text code containing out-of-bounds access configuration information, performs lexical analysis and syntax analysis on the text code in sequence, and generates an abstract syntax tree based on the results of lexical analysis and syntax analysis;
[0060] Semantic analysis and out-of-bounds access configuration analysis are performed on the text code in sequence to obtain array type metadata and out-of-bounds access configuration information. The array type metadata and out-of-bounds access configuration information are then stored in the symbol table and semantic information associated with the abstract syntax tree, respectively.
[0061] The generation detection and processing module is used to generate intermediate code representations by the compiler traversing the abstract syntax tree based on the symbol table and semantic information. It detects whether the nodes in the abstract syntax tree are array read / write nodes and determines whether the array read / write nodes need to be detected and processed based on the out-of-bounds access configuration information. If not, the intermediate code representation is generated normally. For each array read and write operation that needs to be detected and processed, the out-of-bounds access detection and protection logic in the form of the intermediate code representation is generated based on the out-of-bounds access configuration information.
[0062] The runtime detection and processing module is used to compile and link the code intermediate representation containing out-of-bounds access detection and protection logic into an executable program, which is then downloaded to the PLC for scheduling and execution.
[0063] During program execution, array access index validity detection logic will be executed. If out-of-bounds access is detected, the corresponding out-of-bounds access protection logic will be executed and the out-of-bounds access record will be recorded.
[0064] Simultaneously, the global switch variable value can be changed through online setting operation of PLC data, realizing dynamic enabling and disabling of out-of-bounds detection rules during runtime.
[0065] According to a third aspect of the present invention, a computer device is provided, the computer device including a memory and a processor, the memory storing a computer program, and the processor executing the computer program to implement the steps of the above-described method.
[0066] According to a fourth aspect of the present invention, a computer-readable storage medium is provided, on which a computer program is stored, wherein the computer program, when executed by a processor, implements the steps of the above-described method.
[0067] The beneficial effects of this invention are as follows:
[0068] 1. This invention implements runtime array out-of-bounds access detection and protection technology applicable to ST and IL (Instruction List) languages in the PLC field through extended syntax. It supports skipping the detection of program segments that are determined to be without problems through configuration parameters, and supports setting corresponding detection and protection strategies for each array access in the statement. In addition, it implements a more granular index correction strategy that is user-controllable for out-of-bounds access, supports using constant values to replace out-of-bounds access for out-of-bounds reads, supports skipping write operations for out-of-bounds writes, and sets a global out-of-bounds detection switch. It also supports dynamically enabling and disabling out-of-bounds detection rules at runtime through online setting operations of PLC data. Through fine-grained and diversified detection and protection strategies, it can achieve accurate and controllable detection and protection while minimizing the performance loss caused by detection logic.
[0069] 2. This invention allows configuration parameters to skip runtime out-of-bounds access detection and protection for specified statements, reducing performance overhead caused by detection and protection logic. For array read out-of-bounds access, in addition to supporting default rollback to the nearest boundary index, the rollback index can also be set through configuration parameters. For arrays with element values of basic types, configuration parameters can be used to specify a rollback to a specific value when an out-of-bounds read occurs, making read exception handling deterministic. Similarly, for array write out-of-bounds access, in addition to supporting default rollback to the nearest boundary index, the rollback index value can also be set through configuration parameters. Configuration parameters can also be used to specify skipping the corresponding write operation when a write out-of-bounds access occurs, making write exception handling deterministic. A global out-of-bounds detection switch can be set, supporting dynamic enabling and disabling of out-of-bounds detection rules during runtime through online setting operations via PLC data.
[0070] 3. Compared to existing technologies that flatten multidimensional arrays into one-dimensional arrays, this invention eliminates the out-of-bounds error conversion problem that occurs after flattening multidimensional arrays by performing multi-dimensional validity checks. In addition to the default read and write out-of-bounds detection and protection mechanisms, this invention also supports configuring array out-of-bounds access detection and protection mechanisms using extended syntax. This enables efficient, fine-grained, and differentiated out-of-bounds detection and protection mechanisms, improving the determinism of exception handling. Specifically, it supports skipping the detection of some statements, reducing the performance loss caused by the detection and processing logic; it supports setting an exception handling mechanism for read out-of-bounds access that rolls back to a specified index or uses a specified value to replace the array value; it supports setting an exception handling mechanism for write out-of-bounds access that rolls back to a specified index or skips the array write operation; it supports setting differentiated exception handling mechanisms for each array read and write operation; it supports setting error prompts for each abnormal access; and it also supports setting a global out-of-bounds detection switch, enabling and disabling out-of-bounds detection rules at runtime through online setting operations via PLC data. Attached Figure Description
[0071] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0072] Figure 1 This is a flowchart of a configurable PLC runtime array out-of-bounds access detection and protection method according to an embodiment of the present invention;
[0073] Figure 2 This is a flowchart of generating an abstract syntax tree in a configurable PLC runtime array out-of-bounds access detection and protection method according to an embodiment of the present invention;
[0074] Figure 3This is a flowchart of traversing the abstract syntax tree in a configurable PLC runtime array out-of-bounds access detection and protection method according to an embodiment of the present invention;
[0075] Figure 4 This is a schematic diagram of a configurable PLC runtime array out-of-bounds access detection and protection system according to an embodiment of the present invention.
[0076] Figure 5 This is a schematic diagram of the structure of the computer device according to the present invention.
[0077] In the picture:
[0078] 1. Code parsing and data extraction module; 2. Generation and detection processing module; 3. Runtime detection and processing module. Detailed Implementation
[0079] To further illustrate the various embodiments, the present invention provides accompanying drawings, which are part of the disclosure of the present invention. These drawings are mainly used to illustrate the embodiments and can be used in conjunction with the relevant descriptions in the specification to explain the operating principles of the embodiments. With reference to these drawings, those skilled in the art should be able to understand other possible implementation methods and the advantages of the present invention.
[0080] Figure 1 An embodiment of a configurable PLC runtime array out-of-bounds access detection and protection method of the present invention is shown.
[0081] In this optional embodiment, the configurable PLC runtime array out-of-bounds access detection and protection method includes:
[0082] S1. Use the comment-compatible format to extend the PLC text language syntax. Users can use the extended PLC text language syntax to configure array out-of-bounds detection.
[0083] S2. The compiler reads the text code containing out-of-bounds access configuration information, performs lexical analysis and syntax analysis on the text code in sequence, and generates an abstract syntax tree based on the results of lexical analysis and syntax analysis.
[0084] S3. Semantic analysis and out-of-bounds access configuration analysis are performed on the text code in sequence to obtain array type metadata and out-of-bounds access configuration information. The array type metadata and out-of-bounds access configuration information are stored in the symbol table and semantic information associated with the abstract syntax tree, respectively.
[0085] S4. Based on the symbol table and semantic information, the compiler traverses the abstract syntax tree to generate an intermediate code representation, detects whether the abstract syntax tree node is an array read / write node, and determines whether the array read / write node needs to be detected and processed based on the out-of-bounds access configuration information; if not, the intermediate code representation is generated normally. For each array read and write operation that needs to be detected and processed, the out-of-bounds access detection and protection logic in the form of the intermediate code representation is generated according to the out-of-bounds access configuration information.
[0086] S5. The code intermediate containing out-of-bounds access detection and protection logic is compiled and linked to generate an executable program, which is then downloaded to the PLC for scheduling and execution.
[0087] S6. During program execution, array access index validity detection logic will be executed. If out-of-bounds access is detected, the corresponding out-of-bounds access protection logic will be executed and the out-of-bounds access record will be recorded.
[0088] S7. Simultaneously, the global switch variable value can be changed through online setting operation of PLC data, realizing dynamic enabling and disabling of out-of-bounds detection rules during runtime.
[0089] In this optional embodiment, the step of reading text code containing out-of-bounds access configuration information through a compiler, performing lexical analysis and syntax analysis on the text code sequentially, and generating an abstract syntax tree based on the results of the lexical analysis and syntax analysis includes:
[0090] Add extended syntax to the preset PLC standard syntax, and write lexical files and syntax files based on language processing lexical and syntax rules respectively;
[0091] The lexical and grammar files are used as input to the language processing tool, which then generates program fragments to perform lexical and grammar analysis.
[0092] Based on program fragments of lexical analysis and syntax analysis, a lexical analyzer and a syntax analyzer are constructed respectively;
[0093] The compiler reads text code containing out-of-bounds access configuration information, performs lexical analysis and syntax analysis on the text code using a lexical analyzer and a syntax analyzer respectively, and generates an abstract syntax tree by combining the lexical analysis results and the syntax analysis results.
[0094] In this optional embodiment, the annotation compatibility format of the extended PLC text language syntax is as follows:
[0095] / *@ARRAY_OOB_ACCESS(configuration parameter 1, configuration parameter 2, ..., configuration parameter n)* / ;
[0096] The extended PLC text language syntax's comment-compatible format begins with / * and ends with * / , with the constant string @ARRAY_OOB_ACCESS added between the beginning and the end. A pair of parentheses is added after the constant string @ARRAY_OOB_ACCESS, containing one or more configuration parameters, each separated by a comma.
[0097] In this optional embodiment, the step of sequentially performing semantic analysis and out-of-bounds access configuration analysis on the text code to obtain array type metadata and out-of-bounds access configuration information, and storing the array type metadata and out-of-bounds access configuration information in the symbol table and semantic information associated with the abstract syntax tree, respectively, includes:
[0098] Traverse the abstract syntax tree to extract array type metadata, record the array type metadata in a structured manner, and populate the symbol table associated with the abstract syntax tree;
[0099] Traverse the abstract syntax tree and parse the data type of each variable node, marking nodes whose data type is array type;
[0100] Traverse the abstract syntax tree and parse the out-of-bounds access configuration information on each statement. Store the out-of-bounds access configuration information in the semantic information associated with the abstract syntax tree, and record the statements associated with the out-of-bounds access configuration information.
[0101] In this optional embodiment, the symbol table includes several array type metadata information, which includes the array type name, the number of array dimensions, and the dimension range.
[0102] In this optional embodiment, based on the symbol table and semantic information, the compiler traverses the abstract syntax tree to generate an intermediate code representation, detects whether the abstract syntax tree node is an array read / write node, and determines whether the array read / write node needs to be detected based on the out-of-bounds access configuration information; if not, the intermediate code representation is generated normally. For each array read / write operation that needs to be detected, the out-of-bounds access detection and protection logic, based on the out-of-bounds access configuration information, generates an intermediate code representation. This includes: the compiler traverses the abstract syntax tree according to a preset method and generates an intermediate code representation based on the symbol table and semantic information; while traversing the abstract syntax tree to generate the intermediate code representation, the compiler detects the abstract syntax tree node. If the tree node is not an array read / write node, the intermediate code representation is generated normally. If the abstract syntax tree node is an array read / write node, the out-of-bounds configuration is used to determine whether the array read / write node needs to be detected. If not, the intermediate code representation is generated normally. In the generated intermediate code representation, out-of-bounds access detection switch variables, array metadata variables, out-of-bounds access detection functions, and error handling functions are generated as the basic functions for out-of-bounds access detection and protection. If necessary, for each array read and write operation that needs to be detected, out-of-bounds access detection and protection logic in the form of the intermediate code representation is generated based on the out-of-bounds access configuration information.
[0103] Specifically, after preparing to generate an intermediate representation of the source code with the same semantic logic, this step mainly involves generating an intermediate representation of the user program based on the abstract syntax tree, combined with the symbol table and semantic information. During the program logic conversion process, out-of-bounds handling is performed synchronously for array read and write operations. Combined with out-of-bounds configuration information, out-of-bounds detection and protection logic is generated for each array read and write operation in the intermediate representation.
[0104] Generating array read / write detection and protection logic requires some common functions, such as a global detection switch, an index out-of-bounds detection function, and an out-of-bounds error logging function. These common functions exist in an intermediate representation form and are generated at this stage.
[0105] In this optional embodiment, generating the out-of-bounds access detection switch variable, array metadata variable, out-of-bounds access detection function, and error handling function—which serve as the basic functions for out-of-bounds access detection and protection—in the generated intermediate code representation includes: setting a boolean-type out-of-bounds access detection switch variable in the intermediate code representation; wherein, if the out-of-bounds access detection switch variable is false, it indicates skipping array read / write operation out-of-bounds detection processing; if the out-of-bounds access detection switch variable is true, it indicates performing array read / write operation out-of-bounds detection processing; setting a global variable in the intermediate code representation for storing array type metadata, wherein the global variable is of array type, and its elements are the intermediate representation structure corresponding to the array type metadata in the code; in the intermediate code representation... The code snippet defines an out-of-bounds access detection function that takes the array type name and dimension index as input. The function first checks the out-of-bounds access detection switch variable. If the switch variable is false, index checking is skipped, and true is returned directly, indicating no out-of-bounds access. Otherwise, it searches for the corresponding array type metadata in the global variable storing the array type metadata, comparing each dimension index against the dimension range in the metadata. If all dimension indices are valid, true is returned; otherwise, false is returned, indicating an out-of-bounds access. An error handling function is generated in the intermediate code to record out-of-bounds reads and writes, recording out-of-bounds information when such reads or writes occur.
[0106] In the intermediate code representation, an error handling function is generated to record out-of-bounds reads and writes of the array. The error handling function records out-of-bounds information when the array is read or written out of bounds.
[0107] In this optional embodiment, the skip array read / write operation out-of-bounds detection processing includes:
[0108] Determine whether there is out-of-bounds access configuration information on each array read / write operation statement. If there is out-of-bounds access configuration information on the array read / write operation statement and the out-of-bounds access configuration information is false, then skip the out-of-bounds detection process for array read / write operations that match the out-of-bounds access configuration information in the current statement.
[0109] If there is no out-of-bounds access configuration information in the array read / write operation statement, then out-of-bounds detection processing is performed on all array read / write operations in the current statement according to the predefined default rules.
[0110] In this optional embodiment, the detection and processing of the array read operation includes: for the array read operation, specifying the index to backtrack when an out-of-bounds access occurs through a predefined first configuration parameter; for arrays of basic types, a specific value to return when an out-of-bounds access occurs can also be directly specified through a predefined second configuration parameter; for cases where the same statement contains multiple read access operations on a certain array, a different out-of-bounds access processing strategy is specified for each array read operation through a predefined third configuration parameter.
[0111] In this optional embodiment, generating an error handling function in the intermediate code representation to record array out-of-bounds reads and writes, and recording out-of-bounds information when the array is read or written out of bounds through the error handling function, includes:
[0112] In the intermediate code representation, error handling functions for out-of-bounds read and write of the record array are generated. When out-of-bounds read and write of the array is detected, the corresponding error recording function is called respectively, and the context information of the out-of-bounds read and write of the array is passed as a parameter to the error recording function.
[0113] The error logging function records the line number, column number, index value of the out-of-bounds read / write operation, out-of-bounds handling strategy, and error message information set by the user through predefined configuration parameters.
[0114] In this optional embodiment, during program execution, array access index validity detection logic is executed. If out-of-bounds access is detected, the corresponding out-of-bounds access protection logic is executed and the out-of-bounds access record is recorded, including:
[0115] When the PLC is running, it executes the array access index validity check logic. If the index is valid, the original program logic is executed normally; if the index is invalid, the corresponding out-of-bounds access protection logic is executed and the out-of-bounds access information is recorded so that the PLC can still run normally according to the predetermined out-of-bounds access protection logic when an out-of-bounds access occurs.
[0116] Figure 4 An embodiment of a configurable PLC runtime array out-of-bounds access detection and protection system according to the present invention is shown.
[0117] In this optional embodiment, the configurable PLC runtime array out-of-bounds access detection and protection system includes:
[0118] Code parsing and data extraction module 1 is used to extend the PLC text language syntax using a comment-compatible format. Users can use the extended PLC text language syntax to configure array out-of-bounds detection.
[0119] The compiler reads the text code containing out-of-bounds access configuration information, performs lexical analysis and syntax analysis on the text code in sequence, and generates an abstract syntax tree based on the results of lexical analysis and syntax analysis;
[0120] Semantic analysis and out-of-bounds access configuration analysis are performed on the text code in sequence to obtain array type metadata and out-of-bounds access configuration information. The array type metadata and out-of-bounds access configuration information are then stored in the symbol table and semantic information associated with the abstract syntax tree, respectively.
[0121] The generation detection and processing module 2 is used to generate intermediate code representations by the compiler traversing the abstract syntax tree based on the symbol table and semantic information, detect whether the nodes of the abstract syntax tree are array read and write nodes, and determine whether the array read and write nodes need to be detected and processed in combination with the out-of-bounds access configuration information; if not, the intermediate code representation is generated normally. For each array read operation and write operation that needs to be detected and processed, the out-of-bounds access detection and protection logic in the form of the intermediate code representation is generated according to the out-of-bounds access configuration information.
[0122] Runtime detection and processing module 3 is used to compile and link the code intermediate representation containing out-of-bounds access detection and protection logic to generate an executable program, which is then downloaded to the PLC for scheduling and execution.
[0123] During program execution, array access index validity detection logic will be executed. If out-of-bounds access is detected, the corresponding out-of-bounds access protection logic will be executed and the out-of-bounds access record will be recorded.
[0124] Simultaneously, the global switch variable value can be changed through online setting operation of PLC data, realizing dynamic enabling and disabling of out-of-bounds detection rules during runtime.
[0125] The following describes the configurable PLC runtime array out-of-bounds access detection and protection method and system provided by the present invention in conjunction with specific implementation methods.
[0126] The extended PLC text language (ST, IL) syntax uses a comment-compatible format. Through the extended syntax, out-of-bounds detection and protection strategies for each array read and write operation in a statement can be set. When writing PLC programs, users can use the extended syntax to set corresponding array out-of-bounds detection and protection strategies for one or more array read or write operations in each line of statements according to actual needs, thereby realizing the configuration of array out-of-bounds detection processing.
[0127] After reading the code containing array out-of-bounds configuration information, the compiler performs lexical analysis and syntax analysis to construct an Abstract Syntax Tree (AST). Through multiple traversals of the AST, semantic analysis and out-of-bounds access configuration analysis are performed sequentially, storing the extracted symbols and semantic information, including the array out-of-bounds configuration information. During AST traversal, for each array read / write operation, corresponding runtime out-of-bounds detection and protection logic is generated based on the array out-of-bounds configuration information, along with a global out-of-bounds detection switch variable. The generated Integer Logic (IR) is compiled and linked to produce an executable program that can run on the PLC. After being downloaded to the PLC, the executable program is scheduled for execution. During execution, array access index validity detection logic is executed. If an out-of-bounds access is detected, the corresponding out-of-bounds access protection logic is executed and the out-of-bounds access record is recorded. The global switch variable value can be changed online via PLC data setting operations to dynamically enable or disable out-of-bounds detection rules at runtime, thus realizing configurable runtime array out-of-bounds access detection and protection functions for the PLC.
[0128] Figure 1 This is a flowchart of the remaining steps in this invention, excluding the extended syntax. The process starts from the start node and ends at the end node, executing in a left-to-right, top-to-bottom order. Rectangles represent process nodes, diamonds represent condition judgment nodes, ellipses represent various datasets, and file type icons represent various files. Solid unidirectional arrows indicate the execution order, while dashed arrows originating from process nodes and text on these dashed lines indicate the operations performed on data and files during the execution of those nodes. Dashed lines between data points and text on these dashed lines indicate the relationships between the data.
[0129] The entire process can be divided into three parts. The leftmost part involves user-written code, array out-of-bounds configuration operations, and compiler parsing of code to extract data. The middle part involves the compiler traversing the AST to generate the IR and generating runtime array out-of-bounds access detection and protection logic based on the array out-of-bounds configuration information. The rightmost part involves generating the executable file and executing array out-of-bounds access detection and protection at runtime, specifically including:
[0130] Extended syntax:
[0131] Extend the following annotation to be compatible with the following syntax:
[0132] / *@ARRAY_OOB_ACCESS(configuration parameter 1, configuration parameter 2, ..., configuration parameter n)* / ;
[0133] Each of the above formatted content identifiers refers to a configuration for an array out-of-bounds access in a subsequent statement. There can be zero or more of these identifiers on a single statement.
[0134] This syntax begins with " / *" and ends with "* / ", which is a standard comment format. Compilers that do not support this syntax will treat it as a comment, so it is a comment-compatible syntax. Following / * is the constant string @ARRAY_OOB_ACCESS, followed by a pair of parentheses containing one or more configuration parameters. The configuration parameter format is "parameter name=parameter value", and multiple parameters are separated by commas. The available parameter information is shown in Table 1.
[0135] Table 1: Available Parameter Information
[0136] Example of array out-of-bounds detection protection configuration:
[0137] Skip all out-of-bounds checks in the statement:
[0138] " / *@ARRAY_OOB_ACCESS(check=false)* /
[0139] When the array `array_demo` reads out of bounds, it backtracks and reads the value at index 1; when it writes out of bounds, it backtracks and writes to index 2.
[0140] / *@ARRAY_OOB_ACCESS(array_name="array_demo",read_roolback_index=1, write_roolback_index=2)* /
[0141] When the array `array_demo` is read out of bounds on the first read, it returns the constant 123, indicating that the length retrieval failed.
[0142] / *@ARRAY_OOB_ACCESS(array_name="array_demo",order=1,
[0143] read_roolback_value="123", err_msg="Failed to get length")* /
[0144] When the array `array_demo` reads out of bounds for the second time, it returns the constant 456, indicating that the height acquisition failed.
[0145] / *@ARRAY_OOB_ACCESS(array_name="array_demo",order=2,
[0146] read_roolback_value="456", err_msg="Failed to get height")* /
[0147] Skip write operation when array_demo goes out of bounds
[0148] / *@ARRAY_OOB_ACCESS(array_name="array_demo", write_roolback_pass =true)* / ".
[0149] Boundary crossing protection configuration:
[0150] When writing PLC code, users can use extended syntax to adjust parameters to implement targeted out-of-bounds access detection and protection strategies for each statement containing non-constant expression array indices, based on whether the statement has array out-of-bounds access, following the array out-of-bounds detection and protection configuration example.
[0151] If it is certain that the statement will not cause out-of-bounds access, out-of-bounds protection can be disabled to reduce runtime performance overhead.
[0152] To handle read out-of-bounds errors, you can configure the system to backtrack to the specified index when an out-of-bounds error occurs, or return a constant value of the same type instead of accessing the array.
[0153] To handle write out of bounds, you can configure the system to skip the write operation or roll back to a specified index when an out-of-bounds error occurs.
[0154] If a statement contains multiple read and write operations targeting a specific array, different detection and protection strategies can be set for each read and write operation.
[0155] Generate AST:
[0156] like Figure 2-3 As shown, an extended syntax is added to the standard PLC syntax. Based on Antlr rules, corresponding lexical and grammar files are written. Using tools provided by Antlr (ANother Tool for Language Recognition, a compiler development tool), program fragments for performing lexical and grammar analysis are generated with the lexical and grammar files as input. Based on the generated program, a lexical analyzer and a grammar analyzer are constructed. PLC text code written in ST and IL languages is read, and AST is generated through lexical and grammar analysis.
[0157] Semantic analysis:
[0158] Semantic analysis is performed through multiple passes of the Abstract Syntax Tree (AST), and the results are stored. First, the AST is traversed to extract all symbolic information. This information is recorded in a structured manner and populated into a symbol table for storage. The symbol table contains metadata information for all array types, including the array type name, the number of array dimensions, and the start and end ranges of each dimension. Second, the AST is traversed again to parse the data type of each variable node, marking all nodes of type array as targets for subsequent analysis. The array type metadata data structure is as follows (struct is a keyword used in programming languages such as C to define structure data types):
[0159] struct ARRAY_TYPE_META{
[0160] string array_type_name; / / Array type name
[0161] int dim_num; / / Array dimension
[0162] int dim1_start; / / Starting index of the first dimension
[0163] int dim1_end; / / First-dimensional cutoff index
[0164] int dim2_start; / / Starting index of the second dimension
[0165] int dim2_end; / / Second dimension cutoff index
[0166] int dim3_start; / / Starting index of the third dimension
[0167] int dim3_end; / / Third-dimensional cutoff index
[0168] }
[0169] Outbound access configuration analysis:
[0170] The AST is traversed again to parse the out-of-bounds access configuration information on each statement and store it in the semantic information, and the associated statements are recorded. The pseudocode of the out-of-bounds access configuration information data structure is shown below (ARRAY_OOB_ACCESS_CONFIG):
[0171] struct ARRAY_OOB_ACCESS_CONFIG{
[0172] string access; / / Array access method
[0173] string array_name; / / Array name
[0174] int index; / / Array access index in the statement
[0175] bool check; / / Whether to perform the check
[0176] int read_roolback_index; / / Read out-of-bounds backoff index
[0177] int write_roolback_index; / / Write-out-of-bounds rollback index
[0178] string read_roolback_value; / / Read out-of-bounds rollback constant value
[0179] bool write_roolback_pass; / / Skip write operations if write is out of bounds
[0180] string err_msg; / / Error message
[0181] }
[0182] Generate IR by traversing the AST:
[0183] The compiler backend traverses the AST from top to bottom and from left to right, combining the symbol table and semantic information to generate the IR (Intermediate Representation). (Note: The following sections of this embodiment are described with the goal of generating LLVM IR, but this embodiment is not limited to LLVM IR (LLVM Intermediate Representation). When generating other IR formats, the IR generation code may need to be adjusted according to the characteristics of the target IR.) When generating the IR, except for statement nodes containing array read and write operations, other nodes in the AST do not require special processing and can be generated according to the normal process.
[0184] Define a global variable of type Boolean in IR:
[0185] G_OUT_OF_BOUNDS_CHECK_GLAG serves as a global out-of-bounds detection switch. This variable value can be changed online by the user through PLC data setting operations.
[0186] Define an out-of-bounds detection function:
[0187] To implement out-of-bounds detection, a corresponding array index validity check function `boolisValidArrayIndex(string arrayTypeName, int dim1_index, int dim2_index=-1, int dim3_index=-1)` needs to be defined in IR. This function can be used to check the validity of all array indices in the program. The function input parameters are the array type name to be checked and the index values of each dimension (the IEC (International Electrotechnical Commission) standard defines arrays as having a maximum of 3 dimensions). The default value for non-existent indices is -1. The return value is a boolean type, returning true if the index is valid, and false otherwise.
[0188] To support the implementation of the index detection function, a global variable G_ARRAY_TYPE_META_ARRAY of array type needs to be defined in IR. The array elements are the IR structure representations corresponding to the metadata of each array type. The IR structure contains all the information defined in the aforementioned structure ARRAY_TYPE_META.
[0189] When the index detection function is executed, it first checks the global out-of-bounds detection switch variable G_OUT_OF_BOUNDS_CHECK_GLAG. If its value is false, index detection is not performed and true is returned directly. Otherwise, it searches for the metadata of the array type in G_ARRAY_TYPE_META_ARRAY based on the array type name in the parameter. It then compares the validity of each dimension index with the array dimension in the array type metadata (index ≥ starting index and index ≤ ending index). If all dimension indices are valid, it returns true; otherwise, it returns false.
[0190] Specifically, the array index validity check is implemented by the out-of-bounds detection function, which requires the use of global out-of-bounds detection switch variables and array type metadata information.
[0191] The global out-of-bounds detection switch variable is a global variable of type BOOL. Its value can be modified online during PLC program runtime to adjust the global detection strategy.
[0192] Array metadata is stored in a global variable of type array, which contains metadata for all array types in the program.
[0193] The order of settings is: global out-of-bounds detection switch global variable, array metadata global variable, detection function. When generating the intermediate representation, all array type metadata is stored in the array metadata global variable.
[0194] During runtime, users can modify the value of the global variable for the global out-of-bounds detection switch through the PLC setting operation. The value of the global variable for array metadata is read-only during runtime.
[0195] When the detection function is executed, it first checks the global out-of-bounds detection switch. If the variable is false, the index validity check is not performed, and the result is returned directly, indicating that the index is valid. Otherwise, the array metadata is obtained from the global variable of array metadata based on the array information passed in the parameter, and the validity check of each dimension of the index is performed.
[0196] Examples of array detection for each dimension are as follows:
[0197] For a one-dimensional array, return: dim1_index>= dim1_start && dim1_index<= dim1_end;
[0198] The function returns a two-dimensional array: `dim1_index>= dim1_start && dim1_index<= dim1_end`.
[0199] && dim2_index>= dim2_start && dim2_index<= dim2_end;
[0200] The three-dimensional array returns dim1_index>= dim1_start && dim1_index<= dim1_end.
[0201] && dim2_index>= dim2_start && dim2_index<= dim2_end
[0202] && dim3_index>= dim3_start && dim3_index<= dim3_end;
[0203] Skip detection processing:
[0204] For each statement containing array read / write operations via non-constant expressions, if the statement has out-of-bounds configuration information and the configuration parameter `check` is set to `false`, then processing of array operations in the statement that match the configuration information will be skipped. The matching of array operations with configuration information will be determined as follows:
[0205] return (array_name.isEmpty() || array_name == array_name)
[0206] && (access=="RW" || (access=="R" && read array element) || (access=="W" && write array element))
[0207] && order == array operation sequence number;
[0208] It should be noted that R is an abbreviation for Read, W is an abbreviation for Write, and RW means Read and Write.
[0209] Default array access detection protection strategy;
[0210] For each statement containing array read / write operations using non-constant expression indices, if there is no out-of-bounds configuration information on the statement, then all array operations in that statement are processed using the default rules. For any array read / write operation in the statement, if no matching configuration information exists, then that array operation is processed using the default rules.
[0211] For array read operations, if the index goes out of bounds, the out-of-bounds index will be replaced by the nearest boundary index by default. For example, if the array array_read dimension information is [0..2][1..5][2..7], and the runtime value of the index given by a non-constant expression is [2,6,3], and the second dimension index of this index exceeds the upper boundary, then the index will be corrected to the upper boundary index [2,5,7].
[0212] Due to the poor readability of LLVM IRs, this invention uses pseudocode to illustrate the semantic changes of the code before and after adding detection processing logic (the following explanations of code processing using type strategies will not be repeated here). That is, the IR generated after adding detection processing logic to the original code has the same semantics as the IR generated by the code after adding detection processing, as described below using PLC syntax:
[0213] / / Original code
[0214] var_a=array_read[exp_1,exp_2,exp_3];
[0215] / / The IR generated after adding detection processing logic has the same semantics as the IR generated by the code below.
[0216] / / Array index validity check
[0217] if(isValidArrayIndex("array_read",exp_1,exp_2,exp_3)){
[0218] var_a=array_read[exp_1,exp_2,exp_3];
[0219] }else{
[0220] / / Outbound access protection logic
[0221] / / The dimensions of array_read are [0..2][1..5][2..7]
[0222] int idx_1 = exp_1;
[0223] int idx_2 = exp_2;
[0224] int idx_3 = exp_3;
[0225] / / Processing indexes
[0226] if(exp_1>2){ / / greater than the upper boundary
[0227] idx_1=2; / / Rollback to the upper boundary
[0228] }else {
[0229] idx_1=0; / / Fallback to the lower boundary
[0230] }
[0231] if(exp_2>5){ / / greater than the upper boundary
[0232] idx_2=5;
[0233] }else {
[0234] idx_2=1;
[0235] }
[0236] if(exp_3>7){ / / greater than the upper boundary
[0237] idx_3=7;
[0238] }else {
[0239] idx_3=2;
[0240] }
[0241] / / Use the processed index
[0242] var_a=array_read[idx_1,idx_2,idx_3];
[0243] }
[0244] For array write operations, if an index exceeds the bounds, the out-of-bounds index is replaced by the nearest upper or lower bound index by default. For example, if the array dimension information is [0..2][1..5][2..7], and the runtime index value is [0,0,3] given a non-constant expression, and the second dimension index exceeds the lower bound, the index will be corrected to the lower bound index [0,1,2]. The example code for array write operation detection and protection is similar to that for read operations and will not be repeated here.
[0245] The default out-of-bounds detection and handling strategy is not significantly different from existing public solutions, resulting in poor flexibility. Furthermore, the array boundary values are unknown at runtime, leading to a lack of determinism in the protection strategy. However, out-of-bounds configuration can achieve a certain degree of flexibility and determinism. The detection and handling strategies for array read and write operations under out-of-bounds configuration are described below.
[0246] Array read operation configuration detection and processing:
[0247] For array read operations, the configuration parameter `read_roolback_index` can be used to specify the index to backtrack when an out-of-bounds access occurs, instead of using the upper and lower boundary indices. For example, if the array dimension information is [0..2][1..5][2..7], and the configuration parameter `read_roolback_index` is "1,2,3", given a non-constant expression index, the runtime value is [0,6,3]. If the second dimension index goes out of bounds, the index will be corrected to the index [1,2,3] set by the configuration parameter.
[0248] Example code is shown below:
[0249] / / Original code and configuration annotations
[0250] / *@ARRAY_OOB_ACCESS(array_name=array_read,read_roolback_index="1,2,3")* /
[0251] var_a=array_read[exp_1,exp_2,exp_3]
[0252] / / The IR generated after adding detection processing logic has the same semantics as the IR corresponding to the code below.
[0253] / / Array access validity check
[0254] if(isValidArrayIndex("array_read",exp_1,exp_2,exp_3)){
[0255] var_a=array_read[exp_1,exp_2,exp_3]
[0256] }else{
[0257] / / Outbound access protection logic
[0258] / / Index specified using configuration parameters
[0259] var_a = array_read [1,2,3];
[0260] log_array_read_error();
[0261] }
[0262] For a single statement containing multiple read access operations on an array, the `order` configuration parameter can be used to specify different out-of-bounds handling strategies for each read operation.
[0263] Write operation configuration detection and processing:
[0264] Similar to read operations, for array write operations, the configuration parameter `write_roolback_index` can be used to specify the index to backtrack to when an out-of-bounds access occurs, instead of using the upper and lower boundary indices. For example, if the array dimension information is [0..2][1..5][2..7], and the configuration parameter `write_roolback_index` is "1,2,3", given a non-constant expression index, the runtime value is [0,6,3]. If the second dimension index goes out of bounds, the dimension index will be corrected to the index [1,2,3] set by the parameter. Example code is shown below:
[0265] / / Original code and configuration annotations
[0266] / *@ARRAY_OOB_ACCESS(array_name="array_write",write_back_index="1,2,3")* /
[0267] array_write[exp_1,exp_2,exp_3]= var_a;
[0268] / / The IR generated after adding detection processing logic has the same semantics as the code below.
[0269] / / Array access validity check
[0270] if(isValidArrayIndex("array_write",exp_1,exp_2,exp_3)){
[0271] var_a=array_write[exp_1,exp_2,exp_3]
[0272] }else{
[0273] / / Outbound access protection logic
[0274] / / The dimensions of array_write are [0..2][1..5][2..7]
[0275] / / write_back_index is "1,2,3"
[0276] / / Use the index specified by the parameter
[0277] array_write[1,2,3]= var_a;
[0278] log_array_write_error();
[0279] }
[0280] For out-of-bounds write operations, the configuration parameter `write_roolback_pass` can be set to `true` to skip the corresponding array write operation when an out-of-bounds access occurs. Example code is shown below:
[0281] / / Original code and configuration annotations
[0282] / *@ARRAY_OOB_ACCESS(array_name="array_write",write_roolback_pass =true)* /
[0283] array_write[exp_1,exp_2,exp_3]= var_a;
[0284] / / The IR generated after adding detection processing logic has the same semantics as the code below.
[0285] / / Array access validity check
[0286] if(isValidArrayIndex("array_write",exp_1,exp_2,exp_3)){
[0287] var_a=array_write[exp_1,exp_2,exp_3]
[0288] }else{
[0289] / / Skip write operations on out-of-bounds elements
[0290] log_array_write_error();
[0291] }
[0292] Out-of-bounds access error log:
[0293] To record out-of-bounds information when array reads or writes occur, the compiler needs to define error handling functions for recording out-of-bounds reads and writes in the IR when generating the IR: log_array_read_error and log_array_write_error. When an out-of-bounds read or write is detected, the corresponding out-of-bounds error recording function is called in the corresponding out-of-bounds handling logic, and the context information of the out-of-bounds occurrence is passed as parameters to the function. The function will record the line number, column number, index value of the out-of-bounds access, out-of-bounds handling strategy, and error message information set by the user through the configuration parameter err_msg.
[0294] Compilation and linking: The generated IR is compiled and linked to produce an executable file that can run on the PLC.
[0295] Download and run: The generated executable file is deployed to the PLC through a download operation, and then run under the PLC's runtime scheduling.
[0296] Detection and handling: During program execution, read and write operations that access arrays through non-constant expression indexes will execute the generated index validity detection logic. If the index is valid, the original program logic will be executed normally. If the index is invalid, the generated exception access protection handling logic will be executed and the exception access information will be recorded. This ensures that the program can still run normally according to the predetermined exception handling strategy when array out-of-bounds access occurs, thereby improving the stability and reliability of the system.
[0297] In addition, the explanations of relevant technical terms in this invention are shown in Table 2.
[0298] Table 2: Explanation of Relevant Technical Terms
[0299]
[0300] In addition, it should be noted that the compiler front-end is described based on the Antlr tool, but the implementation of this function is not limited to a specific compiler tool and can also use other compiler development tools.
[0301] The compiler backend is described using LLVM IR as the target generation language, but it is not limited to a specific IR language. Other target languages can also be used, and the implementation of array type metadata definitions and detection functions can be adjusted according to the characteristics of the target language. This invention uses a certain defined format to describe the extended out-of-bounds detection and protection configuration syntax, but it is not limited to this format. Other format syntaxes can also be used, and the parameter names are not limited to specific names.
[0302] In one embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 5 As shown, the computer device includes a processor, memory, and a network interface connected via a system bus. The processor provides computing and control capabilities. The memory includes a non-volatile storage medium and internal memory. The non-volatile storage medium stores an operating system, computer programs, and a database. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage medium. The database stores static and dynamic information data. The network interface communicates with external terminals via a network connection. When the computer program is executed by the processor, it implements the steps in the above method embodiments.
[0303] Those skilled in the art will understand that Figure 5 The structure shown is merely a block diagram of a portion of the structure related to the present invention and does not constitute a limitation on the computer device to which the present invention is applied. A specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0304] Furthermore, the present invention provides a computer device including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps in the above-described method embodiments. Additionally, the present invention provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the steps in the above-described method embodiments.
[0305] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A configurable PLC runtime array out-of-bounds access detection and protection method, characterized in that, The method includes: The extended PLC text language syntax is used with a comment-compatible format, allowing users to configure array out-of-bounds detection. The compiler reads the text code containing out-of-bounds access configuration information, performs lexical analysis and syntax analysis on the text code in sequence, and generates an abstract syntax tree based on the results of lexical analysis and syntax analysis; Semantic analysis and out-of-bounds access configuration analysis are performed on the text code in sequence to obtain array type metadata and out-of-bounds access configuration information. The array type metadata and out-of-bounds access configuration information are then stored in the symbol table and semantic information associated with the abstract syntax tree, respectively. Based on the symbol table and semantic information, the compiler traverses the abstract syntax tree to generate an intermediate code representation, detects whether the abstract syntax tree node is an array read / write node, and determines whether the array read / write node needs to be detected and processed in combination with the out-of-bounds access configuration information; if not, the intermediate code representation is generated normally. For each array read and write operation that needs to be detected and processed, the out-of-bounds access detection and protection logic in the form of the intermediate code representation is generated according to the out-of-bounds access configuration information. The code intermediate containing out-of-bounds access detection and protection logic is compiled and linked to generate an executable program, which is then downloaded to the PLC for scheduling and execution. During program execution, array access index validity detection logic will be executed. If out-of-bounds access is detected, the corresponding out-of-bounds access protection logic will be executed and the out-of-bounds access record will be recorded. Simultaneously, the global switch variable value can be changed through online setting operation of PLC data, realizing dynamic enabling and disabling of out-of-bounds detection rules during runtime.
2. The method for detecting and protecting out-of-bounds access to arrays during PLC operation according to claim 1, characterized in that, The compiler reads text code containing out-of-bounds access configuration information, performs lexical analysis and syntax analysis on the text code sequentially, and generates an abstract syntax tree based on the results of the lexical and syntax analysis, including: Add extended syntax to the preset PLC standard syntax, and write lexical files and syntax files based on language processing lexical and syntax rules respectively; The lexical and grammar files are used as input to the language processing tool, which then generates program fragments to perform lexical and grammar analysis. Based on program fragments of lexical analysis and syntax analysis, a lexical analyzer and a syntax analyzer are constructed respectively; The compiler reads text code containing out-of-bounds access configuration information, performs lexical analysis and syntax analysis on the text code using a lexical analyzer and a syntax analyzer respectively, and generates an abstract syntax tree by combining the lexical analysis results and the syntax analysis results.
3. The method for detecting and protecting configurable PLC runtime array out-of-bounds access according to claim 1, characterized in that, The annotation compatibility format of the extended PLC text language syntax is as follows: / *@ARRAY_OOB_ACCESS(configuration parameter 1, configuration parameter 2, ..., configuration parameter n)* / ; The extended PLC text language syntax's comment-compatible format begins with / * and ends with * / , with the constant string @ARRAY_OOB_ACCESS added between the beginning and the end. A pair of parentheses is added after the constant string @ARRAY_OOB_ACCESS, containing one or more configuration parameters, each separated by a comma.
4. The method for detecting and protecting out-of-bounds access to arrays during PLC operation according to claim 1, characterized in that, The sequential semantic analysis and out-of-bounds access configuration analysis of the text code to obtain array type metadata and out-of-bounds access configuration information, and the storage of array type metadata and out-of-bounds access configuration information in the symbol table and semantic information associated with the abstract syntax tree, respectively, include: Traverse the abstract syntax tree to extract array type metadata, record the array type metadata in a structured manner, and populate it into the symbol table associated with the abstract syntax tree; Traverse the abstract syntax tree and parse the data type of each variable node, marking nodes whose data type is array type; Traverse the abstract syntax tree and parse the out-of-bounds access configuration information on each statement. Store the out-of-bounds access configuration information in the semantic information associated with the abstract syntax tree, and record the statements associated with the out-of-bounds access configuration information.
5. A configurable PLC runtime array out-of-bounds access detection and protection method according to claim 4, characterized in that, The symbol table includes several array type metadata information, which includes the array type name, the number of array dimensions, and the dimension range.
6. The method for detecting and protecting out-of-bounds access to arrays during PLC operation according to claim 1, characterized in that, Based on the symbol table and semantic information, the compiler traverses the abstract syntax tree to generate an intermediate code representation, detects whether an abstract syntax tree node is an array read / write node, and determines whether the array read / write node needs to be detected based on the out-of-bounds access configuration information; if not, the intermediate code representation is generated normally. For each array read / write operation that needs to be detected, the out-of-bounds access detection and protection logic, based on the out-of-bounds access configuration information, generates an intermediate code representation. The compiler traverses the abstract syntax tree according to a preset method and combines the symbol table and semantic information to generate an intermediate code representation; When traversing the abstract syntax tree to generate the intermediate code representation, it checks whether the abstract syntax tree node is an array read / write node. If the abstract syntax tree node is not an array read / write node, the intermediate code representation is generated normally. If the abstract syntax tree node is an array read / write node, then determine whether the array read / write node needs to be checked and processed based on the out-of-bounds configuration; If not needed, the intermediate code representation is generated normally; in the generated intermediate code representation, the out-of-bounds access detection switch variable, array metadata variable, out-of-bounds access detection function and error handling function are generated as the basic functions of out-of-bounds access detection and protection. If necessary, for each array read and write operation that needs to be detected, generate out-of-bounds access detection and protection logic in the intermediate code representation form based on the out-of-bounds access configuration information.
7. A configurable PLC runtime array out-of-bounds access detection and protection method according to claim 6, characterized in that, The process of generating the out-of-bounds access detection switch variable, array metadata variable, out-of-bounds access detection function, and error handling function, which serve as the basic functions for out-of-bounds access detection and protection, in the intermediate representation of the generated code includes: Set a boolean out-of-bounds access detection switch variable in the code's intermediate representation; When the out-of-bounds access detection switch variable is false, it means that the out-of-bounds detection process for array read and write operations is skipped; when the out-of-bounds access detection switch variable is true, it means that the out-of-bounds detection process for array read and write operations is executed. In the intermediate representation of the code, a global variable is set to store the metadata of the array type, and the global variable is of array type, and its elements are the intermediate representation structure corresponding to the metadata of the array type in the code; In the code, an out-of-bounds access detection function is set up, taking the array type name and dimension index as input. The out-of-bounds access detection function first checks the out-of-bounds access detection switch variable. If the out-of-bounds access detection switch variable is false, the index detection is not performed, and true is returned directly to indicate that there is no out-of-bounds access. Otherwise, the corresponding array type metadata is searched from the global variable storing array type metadata according to the input array type. The dimension indexes in the array type metadata are compared one by one to see if they are valid. If all dimension indices are valid, true is returned; otherwise, false is returned to indicate that the access is out of bounds. In the intermediate code representation, an error handling function is generated to record out-of-bounds reads and writes of the array. The error handling function records out-of-bounds information when the array is read or written out of bounds.
8. A configurable PLC runtime array out-of-bounds access detection and protection method according to claim 7, characterized in that, The skip array read / write operation out-of-bounds detection processing includes: Determine whether there is out-of-bounds access configuration information on each array read / write operation statement. If there is out-of-bounds access configuration information on the array read / write operation statement and the out-of-bounds access configuration information is false, then skip the out-of-bounds detection process for array read / write operations that match the out-of-bounds access configuration information in the current statement. If there is no out-of-bounds access configuration information in the array read / write operation statement, then out-of-bounds detection processing is performed on all array read / write operations in the current statement according to the predefined default rules.
9. A configurable PLC runtime array out-of-bounds access detection and protection method according to claim 8, characterized in that, The detection and processing of the array read operation includes: For array read operations, the index to back off when an out-of-bounds access occurs is specified by a predefined first configuration parameter; For arrays of basic types, a specific value can be returned when an out-of-bounds access occurs, which can be directly specified through a predefined second configuration parameter; If a single statement contains multiple read access operations on an array, a different out-of-bounds access handling strategy can be specified for each array read operation through a predefined third configuration parameter.
10. A configurable PLC runtime array out-of-bounds access detection and protection method according to claim 9, characterized in that, The step of generating an error handling function in the intermediate code representation to record array out-of-bounds reads and writes, and recording out-of-bounds information when the array is read or written out of bounds through the error handling function, includes: In the intermediate code representation, error handling functions for out-of-bounds read and write of the record array are generated. When out-of-bounds read and write of the array is detected, the corresponding error recording function is called respectively, and the context information of the out-of-bounds read and write of the array is passed as a parameter to the error recording function. The error logging function records the line number, column number, index value of the out-of-bounds read / write operation, out-of-bounds handling strategy, and error message information set by the user through predefined configuration parameters.
11. A configurable PLC runtime array out-of-bounds access detection and protection method according to claim 10, characterized in that, During program execution, array access index validity detection logic is performed. If out-of-bounds access is detected, the corresponding out-of-bounds access protection logic is executed and the out-of-bounds access record is recorded, including: When the PLC is running, it executes the array access index validity check logic. If the index is valid, the original program logic is executed normally; if the index is invalid, the corresponding out-of-bounds access protection logic is executed and the out-of-bounds access information is recorded so that the PLC can still run normally according to the predetermined out-of-bounds access protection logic when an out-of-bounds access occurs.
12. A configurable PLC runtime array out-of-bounds access detection and protection system, used to implement the configurable PLC runtime array out-of-bounds access detection and protection method according to any one of claims 1-11, characterized in that, The system includes: The code parsing and data extraction module is used to extend the PLC text language syntax with a comment-compatible format. Users can use the extended PLC text language syntax to configure array out-of-bounds detection. The compiler reads the text code containing out-of-bounds access configuration information, performs lexical analysis and syntax analysis on the text code in sequence, and generates an abstract syntax tree based on the results of lexical analysis and syntax analysis; Semantic analysis and out-of-bounds access configuration analysis are performed on the text code in sequence to obtain array type metadata and out-of-bounds access configuration information. The array type metadata and out-of-bounds access configuration information are then stored in the symbol table and semantic information associated with the abstract syntax tree, respectively. The generation detection and processing module is used to generate intermediate code representations by traversing the abstract syntax tree based on symbol table and semantic information. It detects whether the nodes in the abstract syntax tree are array read / write nodes and determines whether the array read / write nodes need to be detected and processed based on the out-of-bounds access configuration information. If not, the intermediate code representation is generated normally. For each array read and write operation that needs to be detected and processed, the out-of-bounds access detection and protection logic in the form of the intermediate code representation is generated based on the out-of-bounds access configuration information. The runtime detection and processing module is used to compile and link the code intermediate representation containing out-of-bounds access detection and protection logic into an executable program, which is then downloaded to the PLC for scheduling and execution. During program execution, array access index validity detection logic will be executed. If out-of-bounds access is detected, the corresponding out-of-bounds access protection logic will be executed and the out-of-bounds access record will be recorded. Simultaneously, the global switch variable value can be changed through online setting operation of PLC data, realizing dynamic enabling and disabling of out-of-bounds detection rules during runtime.
13. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 11.
14. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 11.