A code generation method and related device
Through the code generation method, the code development platform is used to automatically identify verification objects and analyze verification properties, and generate verification codes, which solves the difficulty and cost of manually writing verification codes in the existing technology, and improves the degree of automation and efficiency of formal verification.
Patent Information
- Application Number
- CN202410026461.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2023-10-08
- Filing Date
- 2024-01-08
- Publication Date
- 2025-04-08
AI Technical Summary
Existing formal verification technology requires developers to manually write verification code, which is difficult and costly, affecting the efficiency of formal verification.
Provide a code generation method, which receives user input verification objects and configuration information of verification properties through the code development platform, automatically identifies verification objects and analyzes verification properties, and generates verification codes for formal verification.
It reduces the difficulty of using verification tools, improves the degree of automation and efficiency of formal verification, and reduces the requirements for developers to formally verify knowledge.
Smart Images

Figure CN119781744A_ABST
Abstract
Description
[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office on October 8, 2023, with application number 202311297113.8 and invention name “A code generation method and related equipment”, the entire contents of which are incorporated by reference in this application. Technical Field
[0002] The present application relates to the technical field of code generation, and in particular to a code generation method, a code development platform, a computing device cluster, a computer-readable storage medium, and a computer program product. Background Art
[0003] In the design process of computer systems (such as software systems or hardware systems), formal verification refers to the use of mathematical methods to prove the correctness or incorrectness of the system based on one or more formal specifications or properties. As computer systems become more and more complex and provide more and more powerful functions, the security issues inherent in computer systems are becoming increasingly severe, so the industry has put forward higher requirements for formal verification methods.
[0004] At present, the industry mainly uses assertion-based formal verification technology to perform formal verification on the system. Among them, assertions are verification codes used for formal verification. Taking the formal verification of software systems as an example, during the compilation process before the program or other software system runs, the verification engine first compiles the source code and extracts the verification code in the comments. Based on the compilation and extraction results, the verification engine can verify the correctness of the program.
[0005] The above methods can simplify formal verification and semi-automate formal verification to a certain extent. However, the verification code still requires developers to have a certain degree of understanding of formal verification knowledge before manually writing it, which is still difficult and costly, greatly affecting the efficiency of formal verification. Summary of the invention
[0006] The present application provides a code generation method, which automatically identifies a verification object according to configuration information of the verification object input by a user, parses the configuration information of the verification property input by the user to obtain a verification code template including the verification property, and automatically generates verification code for formal verification based on the verification object and the verification code template including the verification property, thereby reducing the difficulty of using verification tools, improving the degree of automation of formal verification, and improving the efficiency of formal verification.
[0007] In the first aspect, the present application provides a code generation method. The code generation method can be executed by a code development platform. The code development platform, also known as a code development system, a code development tool, or a code generation tool, can be used to generate verification code for formal verification. Among them, the code development platform can be a software system, which can be deployed in a computing device cluster, and the computing device cluster executes the program code of the software system, thereby executing the code generation method of the present application. Among them, the code development platform can be an independent software system, such as an independent verification code generation engine. The code development platform can also be integrated into other platforms, for example, the code development platform can be provided to users as an extension component of the verification engine. In some examples, the code development platform can also be a hardware system, such as a computing device cluster with a code development function, and the code generation method of the present application is executed when the computing device cluster is running.
[0008] Specifically, the code development platform receives configuration information of the verification object and configuration information of the verification property input by the user, wherein the verification object is a code snippet in the source code of the verified system, and the configuration information of the verification object is a domain-specific language DSL code, and the DSL code indicates the type of code snippet to be queried and the query condition. Then the code development platform can query the verification object from the source code of the verified system according to the configuration information of the verification object, and parse the configuration information of the verification property to obtain a verification code template (also called an assertion template), and the verification code template includes the verification property for the verification object. Then the code development platform generates the verification code according to the verification object and the verification code template. The verification code includes the verification object and the verification property for the verification object.
[0009] In this method, the user only needs to input the configuration information of the verification object and the configuration information of the verification property, and the code development platform can identify the verification object according to the configuration information of the verification object, without manually adding the verification object in the source code, and the code development platform can parse the configuration information of the verification property to obtain a verification code template including the verification property, and based on the verification object and the verification code template including the verification property, the verification code for formal verification can be automatically generated, thereby reducing the difficulty of using the verification tool, improving the degree of automation of formal verification, and improving the efficiency of formal verification.
[0010] In some possible implementations, the code development platform can obtain the verification object by translating the DSL code. Specifically, the code development platform can translate the DSL code into a query statement based on a database query language, and then query the code attribute graph in the database according to the query statement based on the database query language to obtain the verification object, wherein the code attribute graph includes at least one of the function call relationship, type inheritance relationship, variable use and definition relationship, control flow graph, or data flow graph obtained by parsing the source code.
[0011] In this way, verification objects can be obtained efficiently without adding verification objects in source code, laying the foundation for automatically generating verification codes for formal verification and improving the automation level of formal verification.
[0012] In a possible implementation, the code development platform can obtain the verification object through semantic parsing. Specifically, the code development platform can parse the source code to obtain the parsing result, and then match the DSL code with the parsing result to obtain the verification object. This method does not require DSL code translation, reduces the difficulty of querying the verification object, and improves the query efficiency.
[0013] In one possible implementation, the code snippet type includes one or more of declaration, assignment, reference, memory reading and writing, loop, conditional judgment, definition, and call, wherein declaration includes one or more of declarations for variables, data structures, or objects, assignment includes one or more of assignments for variables, data structures, or objects, reference includes one or more of assignments for variables, data structures, or objects, definition includes one or more of definitions for methods or functions, and call includes one or more of methods or functions.
[0014] By providing the above-mentioned code snippets that are likely to affect the normal operation or output results of the system under test as verification objects, the coverage and effectiveness of formal verification can be improved and the risk of abnormal operation or output errors of the system under test can be reduced.
[0015] In a possible implementation, the code development platform may also extract the insertion position indication information from the verification code, then match the insertion position indication information with the source code to obtain the insertion position in the source code, and then insert the verification code into the source code according to the insertion position.
[0016] In this way, the verification code can be automatically inserted into the source code without the need for manual insertion by the user, further improving the efficiency of formal verification and enhancing the user experience.
[0017] In a possible implementation, the code development platform may receive a verification object configuration file input by a user, and a verification property configuration file input by a user, wherein the verification object configuration file includes configuration information of the verification object, and the verification property configuration file includes configuration information of the verification property.
[0018] The configuration information of the verification object receiving user input and the configuration information of the verification property receiving user input can be decoupled. The user only needs to write the verification property in the configuration file, and the verification object can be automatically identified from the source code through the DSL code, which greatly reduces the threshold for users to use the verification tool.
[0019] In a second aspect, the present application provides a code development platform. The code development platform is used to generate verification code for formal verification, and the code development platform includes: an interactive subsystem and a parsing subsystem. The interactive subsystem is used to receive configuration information of the verification object and configuration information of the verification property input by the user, and the verification object is a code snippet in the source code of the system being verified. The configuration information of the verification object is a domain-specific language DSL code. The DSL code indicates the type of code snippet to be queried and the query conditions. The parsing subsystem is used to query the verification object from the source code of the system being verified according to the configuration information of the verification object, and to parse the configuration information of the verification property to obtain a verification code template. The verification code template includes verification properties for the verification object. The parsing subsystem is also used to generate verification code based on the verification object and the verification code template, and the verification code includes the verification object and the verification properties for the verification object.
[0020] The code development platform is a platform corresponding to the code generation method. Its specific implementation and beneficial effects can refer to the relevant content description of the code generation method, which will not be repeated here.
[0021] In a possible implementation, the parsing subsystem is used to: translate the DSL code into a query statement based on a database query language; query a code property graph in a database according to the query statement based on the database query language to obtain the verification object, wherein the code property graph includes at least one of a function call relationship, a type inheritance relationship, a variable use and definition relationship, a control flow graph, or a data flow graph obtained by parsing the source code.
[0022] In a possible implementation, the parsing subsystem is used to: parse the source code to obtain a parsing result; and match the DSL code with the parsing result to obtain the verification object.
[0023] In one possible implementation, the code snippet type includes one or more of declaration, assignment, reference, memory reading and writing, loop, conditional judgment, definition, and call, wherein declaration includes one or more of declarations for variables, data structures, or objects, assignment includes one or more of assignments for variables, data structures, or objects, reference includes one or more of assignments for variables, data structures, or objects, definition includes one or more of definitions for methods or functions, and call includes one or more of methods or functions.
[0024] In a possible implementation, the parsing subsystem is further used to: extract insertion position indication information from the verification code; match the insertion position indication information with the source code to obtain the insertion position in the source code; and insert the verification code into the source code according to the insertion position.
[0025] In a possible implementation, the interaction subsystem is specifically used to: receive a verification object configuration file input by a user, and receive a verification property configuration file input by a user, the verification object configuration file includes configuration information of the verification object, and the verification property configuration file includes configuration information of the verification property.
[0026] In a third aspect, the present application provides a computing device cluster. The computing device cluster includes at least one computing device, and the at least one computing device includes at least one processor and at least one memory. The at least one processor and the at least one memory communicate with each other. The at least one processor is used to execute instructions stored in the at least one memory, so that the computing device or the computing device cluster executes the code generation method described in the first aspect or any implementation of the first aspect.
[0027] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, wherein the instructions instruct a computing device or a computing device cluster to execute the code generation method described in the above-mentioned first aspect or any one of the implementations of the first aspect.
[0028] In a fifth aspect, the present application provides a computer program product comprising instructions, which, when executed on a computing device or a computing device cluster, enables the computing device or the computing device cluster to execute the code generation method described in the first aspect or any one of the implementations of the first aspect.
[0029] Based on the implementations provided in the above aspects, this application can also be further combined to provide more implementations. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] In order to more clearly illustrate the technical method of the embodiments of the present application, the drawings required for use in the embodiments are briefly introduced below.
[0031] Figure 1 A schematic diagram of the architecture of a code development platform provided for this application;
[0032] Figure 2 A flowchart of a code generation method provided for this application;
[0033] Figure 3 A schematic diagram of a flow chart of generating verification code for a code development platform provided in this application;
[0034] Figure 4 A flowchart of generating verification code in a code building process provided by this application;
[0035] Figure 5 A flowchart of inserting verification code into a code building process provided by this application;
[0036] Figure 6 A schematic diagram of the structure of a computing device provided for this application;
[0037] Figure 7 A schematic diagram of the structure of a computing device cluster provided for this application;
[0038] Figure 8 A schematic diagram of the structure of another computing device cluster provided for this application;
[0039] Fig. 9 A schematic diagram of the structure of another computing device cluster provided in this application. DETAILED DESCRIPTION
[0040] The terms "first" and "second" in the embodiments of the present application are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features.
[0041] First, some technical terms involved in the embodiments of the present application are introduced.
[0042] An assertion is a first-order logic placed in a system (e.g., a program of the system), which can usually be represented by an assertion expression. For example, an assertion can be a logical judgment expression with a true or false result. The purpose of an assertion is to indicate the result expected by the program developer. When the program executes to the assertion position, the corresponding assertion should be true. If the assertion is not true, the program can terminate execution and give an error message.
[0043] Assertions can be used for formal verification. Assertions used for formal verification can be referred to as verification assertions. In assertion-based formal verification scenarios, verification assertions are a piece of logic code used to describe the verification properties (attributes that need to be verified) of the verification object in the system. Therefore, they can also be called verification code or verification target.
[0044] Traditional formal verification tools include a front-end formal engine and a back-end verification engine. The front-end formal engine is responsible for converting the code snippets of the verification object and related verification codes in the source code into formal description statements; the back-end verification engine is responsible for verifying the formal description statements through theorem provers or solvers.
[0045] Taking the example of a formal verification scheme based on an interactive theorem prover, the scheme mainly includes two stages: formal modeling and verification. Among them, formal modeling includes formal modeling of the verification object (the code snippet that needs to be verified in the source code, such as the variable declaration code snippet) or formal modeling of the verification target (i.e., the verification code). According to the complexity of the verification object, as well as the complexity and difficulty of the verification property, appropriate modeling syntax and verification tools can be selected. For example, if the verification object is a state generation function, the verification tool Coq can be selected for formal modeling and verification. Specifically, the verification object is formally modeled according to the modeling syntax, the verification property is formally described according to the verification tool, and then input into the interactive verification tool or the automatic theorem proving tool in the form of a theorem or logical expression to prove the stability of the function output result.
[0046] However, in the formal verification scheme based on interactive theorem provers, different verification tools and modeling syntaxes vary greatly, and the degree of specialization is high, and the learning cost is high. In order to verify a specific system or verify a specific property of the system, it is usually necessary to specifically learn the corresponding formal verification modeling syntax and verification tool usage. Moreover, different tools have different usage methods and verifiable properties, which requires a lot of manual cost to learn verification methods, making it difficult to apply them to the automated verification of complex systems on a large scale.
[0047] To this end, the industry has introduced an assertion-based formal verification solution. Specifically, developers can use the specification syntax of the new programming language or general programming language for formal verification to describe the properties to be proved (i.e., verification properties), and write verification assertions. For example, developers can use the @ symbol as an identifier for verification assertions in the comments of a sample code. Then, after the identifier, the assertion expression of the verification assertion (which can be in the form of a loop invariant) is written according to the specification syntax of the Java Modeling Language (JML). Accordingly, during the compilation process before the sample code is run, the verification engine first compiles the source code and extracts the verification assertions (i.e., verification code) with the assertion identifier @ in the comments. Among them, the verification code may include attribute constraints and loop invariants. The verification engine can compile and extract the results to verify the correctness of the verification object.
[0048] The above method can simplify formal verification and semi-automate formal verification to a certain extent. However, developers need to embed verification code in the source code of the system. Among them, the verification code still needs to be manually written by developers after they have a certain degree of understanding of the relevant knowledge of formal verification. The formal description of the circular inequality in the manual verification code has a high technical threshold, resulting in high difficulty and cost in the verification code writing, which greatly affects the efficiency of formal verification.
[0049] In view of this, the present application provides a code generation method. The method can be executed by a code development platform. Among them, the code development platform, also known as a code development system, a code development tool, and a code generation tool, can be used to generate verification code for formal verification. Among them, the code development platform can be a software system, and the software system can be deployed in a computing device cluster, and the computing device cluster executes the program code of the software system, thereby executing the code generation method of the present application. Among them, the code development platform can be an independent software system, such as an independent verification code generation engine. The code development platform can also be integrated into other platforms, for example, the code development platform can be provided to users as an extension component of the verification engine. In some examples, the code development platform can also be a hardware system, such as a computing device cluster with a code development function, and the code generation method of the present application is executed when the computing device cluster is running.
[0050] Specifically, the code development platform receives configuration information of the verification object and configuration information of the verification property input by the user, wherein the verification object is a code snippet in the source code of the verified system, and the configuration information of the verification object is a domain-specific language (DSL) code, and the DSL code indicates the type of code snippet to be queried and the query condition. Then the code development platform can query the verification object from the source code of the verified system according to the configuration information of the verification object, and parse the configuration information of the verification property to obtain a verification code template (also called an assertion template), and the verification code template includes the verification property for the verification object. Then the code development platform generates the verification code according to the verification object and the verification code template. The verification code includes the verification object and the verification property for the verification object.
[0051] In this method, the user only needs to input the configuration information of the verification object and the configuration information of the verification property, and the code development platform can identify the verification object according to the configuration information of the verification object, without manually adding the verification object in the source code, and the code development platform can parse the configuration information of the verification property to obtain a verification code template including the verification property, and based on the verification object and the verification code template including the verification property, the verification code for formal verification can be automatically generated, thereby reducing the difficulty of using the verification tool, improving the degree of automation of formal verification, and improving the efficiency of formal verification.
[0052] In order to make the technical solution of the present application clearer and easier to understand, the system architecture of the code development platform of the present application is introduced below with reference to the accompanying drawings.
[0053] See also Figure 1The schematic diagram of the architecture of a code development platform is shown, and the code development platform 100 may include an interactive subsystem 102 and a parsing subsystem 104. The code development platform 100 may be logically divided into a user layer and a parsing layer, wherein the interactive subsystem 102 belongs to the user layer, and the parsing subsystem 104 belongs to the parsing layer. The code development platform 100 may be connected to a verification engine 200 of a verification layer, wherein the verification engine 200 may be a verification engine based on assertions (verification codes), and accordingly, the verification code generated by the code development platform 100 may be input into the verification engine 200 of the verification layer for formal verification.
[0054] The user layer supports direct interaction with users. Users can input configuration information of verification properties and configuration information of verification objects to the code development platform 100 (such as a code development system) through the user layer. Among them, the configuration information of the verification object can be a code snippet written by the user in a domain-specific language (DSL), also known as DSL code. The DSL code can indicate the type of code snippet queried and the query condition. The configuration information of the verification property and the configuration information of the verification object can be input in the form of a configuration file and submitted to the code development platform 100. For example, the user can input a verification property configuration file and submit the verification property configuration file to the code development platform 100. The user can also input the source code to be analyzed, such as the source code of the system under test, to the code development platform 100 through the user layer.
[0055] The parsing layer is the core layer of the automated verification framework. The parsing layer parses the configuration information of the verification property (such as the verification property configuration file), the source code to be analyzed (such as the source code of the system under test) and the configuration information of the verification object to obtain the verification object and the verification property. Specifically, the parsing subsystem 104 of the parsing layer may include a verification property parsing engine (or a verification attribute parsing engine), a source code parsing engine and a DSL parsing engine. The verification property parsing engine is used to parse the configuration information of the verification property to obtain a verification code template. The verification code template includes the verification property for the verification object. The DSL parsing engine is used to parse the configuration information of the verification object to obtain the parsing result. The parsing result can be used to query the verification object in the source code. The source code parsing engine is used to parse the source code of the system under test to query the verification object from the source code of the system under test. Among them, the source code parsing engine can query the verification object from the source code of the system under test in combination with the parsing result of the DSL code. Then the parsing layer can combine the verification object and the verification property to generate the verification code (verification assertion). Further, the parsing layer can insert the verification code into the source code to be analyzed. Specifically, the parsing subsystem 104 may include a code generation and insertion engine, which is used to generate a verification code based on a verification object and a verification code template including a verification property. Furthermore, the code generation and insertion engine may extract insertion position indication information from the verification code, match the insertion position indication information with the source code, obtain the insertion position in the source code, and insert the verification code into the source code according to the insertion position.
[0056] The verification layer is the foundation of the entire automated verification framework. The verification layer can parse the verification code in the source code and call the formal verification engine (such as the verification engine 200 mentioned above) to verify whether the verification object in the source code to be analyzed meets the verification properties defined in the verification code.
[0057] exist Figure 1In the embodiment of the present application, the DSL parsing engine can be a code project written in a general programming language, and the embodiment of the present application does not limit the type of general programming language. In addition, depending on the usage scenario, the code development platform can receive the configuration information and source code input by the user in different ways. For example, in the scenario of local use, the parsing engine of the parsing layer can read the configuration file and source code from a specific path and parse it, thereby generating the verification code, and then docking the verification engine to complete the formal verification. For another example, in the process scenario of code construction and submission, the code development platform can be integrated with the verification engine, and the verification engine after the integration of the code development platform (also referred to as the integrated verification engine) can provide a unified verification call interface. The integrated verification engine reads the verification configuration file (such as the verification property configuration file, the verification object configuration file) and the source code of the system under test through a unified verification call interface, wherein the verification object configuration file can be a DSL code, and the integrated verification engine generates the verification code according to the configuration file, and inserts the verification code into the source code of the system under test for formal verification.
[0058] based on Figure 1 The code development platform 100 shown in the figure, the present application also provides a code generation method. The code generation method of the present application is introduced below in conjunction with the accompanying drawings.
[0059] See also Figure 2 The flowchart of a code generation method shown in FIG. 1 is a flowchart of a code generation method, which can be executed by a code development platform 100. The code development platform 100 is specifically used to generate verification code for formal verification. The method may include the following steps:
[0060] S202: The code development platform 100 receives configuration information of a verification object input by a user.
[0061] The verification object is a code snippet in the source code of the system under test. In the source code of the system under test, there are usually some code snippets that affect the normal operation or output results of the system under test. When an exception occurs in the execution of this code snippet, it can cause the entire system under test to run abnormally or output an incorrect result. For this reason, the code snippet can be formally verified. This code snippet is also called the verification object of formal verification.
[0062] In some possible implementations, the verification object may be at least one type of code snippet, for example, one or more types of code snippets configured by the user. Code snippet types may include one or more of declarations, assignments, references, memory read and write, loops, conditional judgments, definitions, and calls. Among them, declarations include one or more of declarations for variables, data structures, or objects, assignments include one or more of assignments for variables, data structures, or objects, and references include one or more of assignments for variables, data structures, or objects. Definitions include one or more of definitions for methods or functions, and calls include one or more of methods or functions. Accordingly, the verification object may be one or more of declaration / assignment / reference statements for variables, data structures, or objects, or the verification object may also be one or more of definition / call statements for methods or functions, and the verification object may also be one or more of memory read and write statements, loop statements, or conditional judgment statements.
[0063] In specific implementation, the user can write the configuration information of the verification object through DSL, and accordingly, the code development platform 100 can receive the DSL code input by the user. The DSL code can indicate the type of code snippet to be queried and the query condition. Accordingly, the verification object can be a code snippet (such as a statement) in the source code of the system under test that meets the query condition and matches the code snippet type. Among them, the code snippet type includes one or more of declaration, assignment, and reference for variables, data structures, or objects, or one or more of definition or call for functions or methods, or one or more of memory access, loop, and conditional judgment. The query condition may include the type of the variable, data structure, or object, and the return value of the function call / method call.
[0064] It should be noted that the configuration information of the verification object, such as the DSL code, can be submitted in the form of a configuration file. In other words, the configuration information of the verification object can be a verification object configuration file, and the verification object configuration file can be a DSL code file. The format of the verification object configuration file can be set according to requirements. In some possible implementations, the verification object configuration file can be in the YAML (YAML Ain't a Markup Language abbreviation, a data-centric markup language) format, or any markup language format such as JavaScript Object Notation (JSON).
[0065] S204 : The code development platform 100 receives configuration information of verification properties input by the user.
[0066] The configuration information of the verification property can be a simplified description of the verification property. Similar to the configuration information of the verification object, the configuration information of the verification property can also be input and submitted in the form of a configuration file. Among them, the verification property configuration file can be in YAML or JSON format. The verification property configuration file only requires the user to describe the verification object to be verified and its verification property (verification attribute), which can save the cost of learning and mastering the logical syntax of the verification assertion and lower the threshold for writing verification assertions.
[0067] For ease of understanding, an example is provided below for explanation. The configuration information of the verification property input by the user may include: Int>0. Int represents a variable of an integer type, and the configuration information indicates that the value of the variable of the integer type is greater than 0.
[0068] It should be noted that the above S202 and S204 can be executed in parallel, or successively in a set order, and the embodiments of the present application are not limited to this. The configuration information of the verification object received from the user and the configuration information of the verification property received from the user can be decoupled. For example, the code development platform 100 can respectively receive a verification object configuration file input by the user, and receive a verification property configuration file input by the user, wherein the verification object configuration file includes the configuration information of the verification object, and the verification property configuration file includes the configuration information of the verification property. In some examples, the configuration information of the verification object and the configuration information of the verification property can also be submitted in a unified form of a verification configuration file. For example, the code development platform 100 can receive a verification configuration file input by the user, and the verification configuration file includes the configuration information of the verification object and the configuration information of the verification property.
[0069] S206 , the code development platform 100 obtains the verification object by searching the source code of the system under verification according to the configuration information of the verification object.
[0070] The configuration information of the verification object indicates information used to query the verification object, such as a statement or template used to query the verification object. For example, when the configuration information of the verification object is a DSL code, the DSL code can indicate the type of code snippet to be queried and the query condition. The code development platform 100 can query the source code of the verified system for the code snippet that matches the content in combination with the content indicated by the configuration information of the verification object to obtain the verification object.
[0071] In some possible implementations, the code development platform 100 can translate the DSL code into a query statement based on a database query language, and then the code development platform 100 can query the code property graph in the database according to the query statement based on the database query language to obtain the verification object. The code property graph can be generated by a source code parsing engine in the code development platform 100 by parsing the source code, and the code property graph includes at least one of a function call relationship, a type inheritance relationship, a variable use and definition relationship, a control flow graph, or a data flow graph.
[0072] The generation process of the code attribute graph is as follows Figure 3 As shown, the source code parsing engine in the code development platform 100 parses the source code of the system under test to obtain an abstract syntax tree. Then the code development platform 100 can perform an in-depth analysis of the source code based on the abstract syntax tree to obtain program information. The program information includes at least one of a function call relationship, a type inheritance relationship, and a variable use and definition relationship. Furthermore, the source code parsing engine in the code development platform 100 can also generate at least one of a control flow graph and a data flow graph according to user needs. The source code parsing engine can use a code attribute graph to represent the above-mentioned program information, and save an instance of the code attribute graph of the source code to be analyzed into a database. Among them, the code attribute graph is a data structure that represents a function call relationship, a type inheritance relationship, a variable use and definition relationship, a control flow graph and / or a data flow graph.
[0073] DSL for program analysis is a query language specifically used to filter source code to be analyzed. Users can write statements (such as DSL code) through DSL to specify the type of code snippet to be queried (for example: variable declaration, method call) and query conditions (for example: variable type, method call return value). Figure 3 As shown, after receiving the DSL code, the DSL parsing engine may first translate the DSL code into a query statement based on a database query language, such as a query statement based on Gremlin or a query statement based on Structured Query Language (SQL), and then query the code property graph stored in the database through the query statement to obtain multiple code snippets, such as code snippets 1 to N. The code snippets 1 to N are the verification objects in the source code of the system under verification.
[0074] In some other possible implementations, the code development platform 100 may parse the source code to obtain a parsing result, and then the code development platform 100 may match the DSL code with the parsing result to obtain a verification object. The parsing result includes a semantic analysis result of the source code. The code development platform 100 may match the semantic analysis result with the semantic analysis result of the DSL code, and determine the code snippet in the source code corresponding to the successfully matched semantic analysis result as the verification object.
[0075] S208 : The code development platform 100 parses the configuration information of the verification property to obtain a verification code template.
[0076] Specifically, the code development platform 100 can parse the configuration information of the verification property through the verification property parsing engine to obtain a verification code template (such as an assertion template). The verification code template includes the verification property (verification attribute) for the verification object. In specific implementation, the verification property parsing engine can parse the configuration information of the verification property, such as the verification property configuration file, to extract the placeholder and verification property of the verification object, and the verification code template can be obtained according to the placeholder and verification property of the verification object.
[0077] S210 . The code development platform 100 generates a verification code according to a verification object and a verification code template.
[0078] Specifically, the verification code template includes a placeholder for the verification object, and the code development platform 100 can use the verification object to replace the placeholder for the verification object in the verification code template, thereby generating the verification code. The verification code includes the verification object and the verification property for the verification object.
[0079] For ease of understanding, the following Figure 3An example is given. The source code parsing engine is combined with the DSL parsing engine, and the verification objects extracted from the source code include code snippet 1, code snippet 2...code snippet N. The verification property parsing engine parses the configuration information of the verification property to obtain a verification code template. The verification code template includes a placeholder for the verification object and a verification property for the verification object. For example, when the verification object is an integer variable, the verification property may include one or more of an object value constraint (such as an object value greater than 0, an object value not empty), an object operation result constraint, and an object attribute constraint. Among them, the object operation result may be a linear operation result, a nonlinear operation result, or a combined operation result of the same object, wherein the combined operation may be a combination of linear operations and nonlinear operations. In some possible implementations, the object operation result may also be a combined operation result of multiple objects. The placeholder of the verification object in the verification code template can be replaced by the above-mentioned code snippet 1, code snippet 2...code snippet N, respectively, to form N verification code snippets. Among them, the verification code template and the verification object (such as code snippet 1 to code snippet N) can be input into the code generation and insertion engine, and the code generation and insertion engine combines the verification code template and the verification object to generate the verification code.
[0080] S212: The code development platform 100 extracts the insertion position indication information from the verification code.
[0081] The verification code can be inserted after the verification object (e.g., a method call code snippet, a variable declaration code snippet) that matches the verification code in the source code. Based on this, the code development platform 100 can extract information that can indicate the insertion position of the verification code from the verification code, also known as insertion position indication information. For example, the insertion position indication information can be one or more of the declaration / assignment / reference statements of a variable, data structure, or object, the insertion position indication information can also be one or more of the method or function definition / call statements, or the insertion position indication information can be one or more of the conditional judgment statements of a memory read / write statement.
[0082] S214: The code development platform 100 matches the insertion position indication information with the source code to obtain the insertion position in the source code.
[0083] The code development platform 100 matches the insertion position indication information, such as variable assignment statements, method call statements and source code, to determine the positions of the same variable assignment statements and method call statements in the source code. Based on the positions, the positions of the verification objects can be obtained. The positions after the verification objects can be the insertion positions in the source code for inserting the verification code.
[0084] S216 . The code development platform 100 inserts the verification code into the source code according to the insertion position.
[0085] Specifically, the code development platform 100 can insert the above verification code after the verification object in the source code. The code development platform 100 can insert the respective verification code after each verification object. In some examples, when the positions of multiple verification objects in the source code are continuous, the verification codes of multiple verification objects can also be inserted after the multiple verification objects.
[0086] The above S212 to S216 are optional steps of the embodiment of the present application, and the code generation method of the present application may not execute the above steps.
[0087] Based on the above description, the present application provides a code generation method, in which the user only needs to input the configuration information of the verification object and the configuration information of the verification property, and the code development platform 100 can identify the verification object according to the configuration information of the verification object, without manually adding the verification object in the source code, and the code development platform 100 can parse the configuration information of the verification property to obtain a verification code template including the verification property, and based on the verification object and the verification code template including the verification property, verification code for formal verification can be automatically generated, thereby reducing the difficulty of using verification tools, improving the degree of automation of formal verification, and improving the efficiency of formal verification.
[0088] Next, the code generation method of this application is introduced in conjunction with the verification code generation scenario in the code building process.
[0089] See also Figure 4 The flowchart of generating verification code in a code building process shown is that the user can input the verification property configuration file, source code, and DSL code, and the verification code is automatically generated through the code development platform 100.
[0090] In this example, the source code can be:
[0091]
[0092] Based on the DSL syntax provided by the DSL parsing engine, the user can write a DSL statement (DSL code) to filter out all variable declaration nodes of type std::ops::Range in the source code. The variable declaration node can be a variable declaration code snippet. In this embodiment, the DSL code is as follows:
[0093] VariableDeclaration vd where vd.type.name=="std::ops::Range"
[0094] The verification object of formal verification includes a variable declaration code snippet. For this verification object, the user defines the verification properties for the verification object in the verification property configuration file, as shown below:
[0095] vd.rhs==any()
[0096] Forall(vd,(!vd.var_name.is_empty())
[0097] Where rhs represents right hand size, the verification property can be that the variable name is not empty, and the logical expression for the verification property can be! vd.var_name.is_empty().
[0098] The above verification property configuration file can be input into the verification property parsing engine, and the verification property parsing engine can parse the verification property configuration file to output the verification code template. The above source code can be input into the source code parsing engine, and the DSL code can be input into the DSL parsing engine. The source code parsing engine can parse the source code in combination with the parsing results of the DSL parsing engine, and query the verification object from the source code. The verification object includes all variable declaration code snippets of type std::ops::Range in the source code. The variable declaration code snippet can be matched with the verification code template for variables, and then the verification code template is replaced with the variables extracted from the variable declaration code snippet, such as r1 and r2, for example, vd in the verification code template is replaced to obtain the verification code (verification assertion), as shown below:
[0099] let r1:std::ops::Range <i32>=any()..any();
[0100] let r2:std::ops::Range <i32>=any()..any();
[0101] assert(!r1.is_empty())
[0102] assert(!r2.is_empty()).
[0103] Among them, assert is a keyword, which means verification assertion (verification code), similar to the @ mark in JML syntax.
[0104] In this scenario, the verification properties and verification objects are decoupled. Users only need to write the verification properties in the configuration file, and the verification objects can be automatically identified from the source code through the DSL code, which greatly reduces the threshold for users to use the verification tool.
[0105] Furthermore, the verification code generated by the code development platform 100 can also be automatically inserted into a specified position of the source code.
[0106] See also Figure 5 A flowchart of generating verification code in a code building process is shown. After the verification code is generated, the code generation and insertion engine in the code development platform 100 can extract insertion position prompt information from the verification code, match the insertion position prompt information with the corresponding code fragment in the source code, and then insert the verification code into the source code according to the matched insertion position.
[0107] Still Figure 4 The source code example shows that after matching the variable declaration code snippets of r1 and r2, the code generation and insertion engine can insert the verification code after the variable declaration code snippets of r1 and r2, as shown below:
[0108]
[0109] In this way, the verification code can be automatically inserted into the source code without the need for manual insertion by the user, further improving the efficiency of formal verification and enhancing the user experience.
[0110] Based on the aforementioned code generation method, the present application also provides a code development platform. Figure 1 As shown, the code development platform 100 includes:
[0111] The interactive subsystem 102 is used to receive configuration information of the verification object and the verification property input by the user, the verification object is a code snippet in the source code of the system being verified, and the configuration information of the verification object is a domain specific language DSL code, which indicates the type of code snippet to be queried and the query condition.
[0112] The parsing subsystem 104 is used to query the verification object from the source code of the system under verification according to the configuration information of the verification object, and parse the configuration information of the verification property to obtain the verification code template, which includes the verification property for the verification object.
[0113] The parsing subsystem 104 is further configured to generate a verification code according to the verification object and the verification code template. The verification code includes the verification object and the verification property for the verification object.
[0114] Exemplarily, the above-mentioned interaction subsystem 102 and analysis subsystem 104 may be implemented by hardware, or may be implemented by software.
[0115] Among them, when implemented by software, the interactive subsystem 102 and the parsing subsystem 104 can be applications running on computer devices, such as computing engines, etc. Further, the application can also be virtualized through virtualization services. Virtualization services can include virtual machine (VM) services, bare metal server (BMS) services, and container services. Among them, VM services can be services that virtualize virtual machine (VM) resource pools on multiple physical hosts through virtualization technology to provide users with VMs on demand for use. BMS services are services that virtualize BMS resource pools on multiple physical hosts to provide users with BMS on demand for use. Container services are services that virtualize container resource pools on multiple physical hosts to provide users with containers on demand for use. VM is a simulated virtual computer, that is, a logical computer. BMS is a high-performance computing service that can be elastically scalable, and its computing performance is no different from that of traditional physical machines, and it has the characteristics of secure physical isolation. Containers are a kernel virtualization technology that can provide lightweight virtualization to achieve the purpose of isolating user space, processes, and resources. It should be understood that the VM service, BMS service and container service in the above-mentioned virtualization services are only specific examples. In actual applications, virtualization services can also be other lightweight or heavyweight virtualization services, which are not specifically limited here.
[0116] When implemented by hardware, the interaction subsystem 102 and the analysis subsystem 104 may include at least one computing device, such as a server, etc. Alternatively, the interaction subsystem 102 and the analysis subsystem 104 may also be implemented by using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD), etc. The PLD may be a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0117] In some possible implementations, the parsing subsystem 104 is used to:
[0118] Translating the DSL code into a query statement based on a database query language;
[0119] According to the query statement based on the database query language, the code property graph in the database is queried to obtain the verification object, and the code property graph includes at least one of the function call relationship, type inheritance relationship, variable use and definition relationship, control flow graph or data flow graph obtained by parsing the source code.
[0120] In this way, verification objects can be obtained efficiently without adding verification objects in source code, laying the foundation for automatically generating verification codes for formal verification and improving the automation level of formal verification.
[0121] In some possible implementations, the parsing subsystem 104 is used to:
[0122] Parsing the source code to obtain a parsing result;
[0123] The DSL code is matched with the parsing result to obtain the verification object.
[0124] In this way, the verification object can be directly obtained by semantic analysis and matching without DSL code translation, which reduces the difficulty of querying the verification object, improves the query efficiency, and has higher accuracy.
[0125] In some possible implementations, the code snippet type includes one or more of declaration, assignment, reference, memory reading and writing, loop, conditional judgment, definition, and call, wherein declaration includes one or more of declarations for variables, data structures, or objects, assignment includes one or more of assignments for variables, data structures, or objects, reference includes one or more of assignments for variables, data structures, or objects, definition includes one or more of definitions for methods or functions, and call includes one or more of methods or functions.
[0126] By providing the above-mentioned code snippets that are likely to affect the normal operation or output results of the system under test as verification objects, the coverage and effectiveness of formal verification can be improved and the risk of abnormal operation or output errors of the system under test can be reduced.
[0127] In some possible implementations, the parsing subsystem 104 is further configured to:
[0128] Extracting insertion position indication information from the verification code;
[0129] Matching the insertion position indication information with the source code to obtain the insertion position in the source code;
[0130] Insert the verification code into the source code according to the insertion position.
[0131] In this way, the verification code can be automatically inserted into the source code without the need for manual insertion by the user, further improving the efficiency of formal verification and enhancing the user experience.
[0132] In some possible implementations, the interaction subsystem 102 is specifically used for:
[0133] A verification object configuration file input by a user is received, and a verification property configuration file input by a user is received, wherein the verification object configuration file includes configuration information of the verification object, and the verification property configuration file includes configuration information of the verification property.
[0134] Among them, the configuration information of the verification object received from the user and the configuration information of the verification property received from the user can be decoupled. The user only needs to write the verification property in the configuration file, and the verification object can be automatically identified from the source code through the DSL code, which greatly reduces the threshold for users to use the verification tool.
[0135] The present application also provides a computing device 600. Figure 6 As shown, computing device 600 includes: bus 602, processor 604, memory 606 and communication interface 608. Processor 604, memory 606 and communication interface 608 communicate through bus 602. Computing device 600 can be a server or a terminal device. It should be understood that the present application does not limit the number of processors and memories in computing device 600.
[0136] The bus 602 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 6 The bus 602 may include a path for transmitting information between various components of the computing device 600 (eg, the memory 606, the processor 604, and the communication interface 608).
[0137] The processor 604 may include any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).
[0138] The memory 606 may include a volatile memory, such as a random access memory (RAM). The memory 606 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD) or a solid state drive (SSD). The memory 606 stores executable program code, and the processor 604 executes the executable program code to implement the aforementioned code generation method. Specifically, the memory 606 stores instructions for the code generation platform to execute the code generation method.
[0139] The communication interface 608 uses a transceiver module such as, but not limited to, a network interface card or a transceiver to implement communication between the computing device 600 and other devices or communication networks.
[0140] The embodiment of the present application also provides a computing device cluster. The computing device cluster includes at least one computing device. The computing device can be a server, such as a central server, an edge server, or a local server in a local data center. In some embodiments, the computing device can also be a terminal device such as a desktop computer, a laptop computer, or a smart phone.
[0141] like Figure 7 As shown, the computing device cluster includes at least one computing device 600. The memory 606 in one or more computing devices 600 in the computing device cluster may store the same code generation platform for executing instructions of the code generation method.
[0142] In some possible implementations, one or more computing devices 100 in the computing device cluster may also be used to execute part of the instructions of the code generation platform for executing the code generation method. In other words, a combination of one or more computing devices 600 may jointly execute the instructions of the code generation platform for executing the code generation method.
[0143] It should be noted that the memory 606 in different computing devices 600 in the computing device cluster may store different instructions for executing part of the functions of the code generation platform.
[0144] Figure 8 A possible implementation is shown. Figure 8 As shown, two computing devices 600A and 600B are connected via a communication interface 608. The memory in computing device 600A stores instructions for executing the functions of the interaction subsystem 102. The memory in computing device 600B stores instructions for executing the functions of the parsing subsystem 104. In other words, the memories 606 of computing devices 600A and 600B jointly store instructions for the code generation platform to execute the code generation method.
[0145] Figure 8 The connection mode between the computing device clusters shown may be considered to be that the code generation method provided by the present application requires a large amount of computing power to generate verification codes. Therefore, it is considered to hand over the functions implemented by the parsing subsystem 104 to the computing device 600B for execution.
[0146] It should be understood that Figure 8 The functions of the computing device 600A shown in FIG. 6A may also be completed by multiple computing devices 600. Similarly, the functions of the computing device 600B may also be completed by multiple computing devices 600.
[0147] In some possible implementations, one or more computing devices in the computing device cluster may be connected via a network, which may be a wide area network or a local area network. Fig. 9 A possible implementation is shown. Fig. 9 As shown, two computing devices 600C and 600D are connected via a network. Specifically, they are connected to the network via a communication interface in each computing device. In this type of possible implementation, the memory 606 in the computing device 600C stores instructions for executing the functions of the interaction subsystem 102. At the same time, the memory 606 in the computing device 600D stores instructions for the functions of the parsing subsystem 104.
[0148] Fig. 9 The connection method between the computing device clusters shown may be that considering that the code generation method provided in the present application requires a large amount of computing power to verify code generation, it is considered to hand over the functions implemented by the parsing subsystem 104 to the computing device 600D for execution.
[0149] It should be understood that Fig. 9 The functions of the computing device 600C shown in FIG. 6A may also be completed by multiple computing devices 600. Similarly, the functions of the computing device 600D may also be completed by multiple computing devices 600.
[0150] The embodiment of the present application also provides a computer-readable storage medium. The computer-readable storage medium can be any available medium that can be stored by a computing device or a data storage device such as a data center containing one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk). The computer-readable storage medium includes instructions that instruct the computing device to execute the above-mentioned code development platform for executing the code generation method.
[0151] The embodiment of the present application also provides a computer program product including instructions. The computer program product may be software or a program product including instructions that can be run on a computing device or stored in any available medium. When the computer program product is run on at least one computer device, the at least one computer device executes the above-mentioned code generation method.
[0152] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the protection scope of the technical solutions of the embodiments of the present invention.
Claims
1. A code generation method, characterized in that: The method is performed by a code development platform, wherein the code development platform is used to generate verification code for formal verification, and the method includes: Receive configuration information of a verification object and configuration information of a verification property input by a user, wherein the verification object is a code snippet in the source code of the system to be verified, and the configuration information of the verification object is a domain specific language DSL code, wherein the DSL code indicates a code snippet type and a query condition to be queried; querying the verification object from the source code of the verified system according to the configuration information of the verification object, and parsing the configuration information of the verification property to obtain a verification code template, wherein the verification code template includes the verification property for the verification object; The verification code is generated according to the verification object and the verification code template, wherein the verification code includes the verification object and the verification property for the verification object.
2. The method according to claim 1, characterized in that The step of obtaining the verification object by querying the source code of the system to be verified according to the configuration information of the verification object includes: Translating the DSL code into a query statement based on a database query language; According to the query statement based on the database query language, the code property graph in the database is queried to obtain the verification object, and the code property graph includes at least one of the function call relationship, type inheritance relationship, variable use and definition relationship, control flow graph or data flow graph obtained by parsing the source code.
3. The method according to claim 1, characterized in that The step of obtaining the verification object by querying the source code of the system to be verified according to the configuration information of the verification object includes: Parsing the source code to obtain a parsing result; The DSL code is matched with the parsing result to obtain the verification object.
4. The method according to any one of claims 1 to 3, characterized in that: The code snippet type includes one or more of declaration, assignment, reference, memory reading and writing, loop, conditional judgment, definition, and call, wherein the declaration includes one or more of declarations for variables, data structures, or objects, the assignment includes one or more of assignments for the variables, the data structures, or the objects, the reference includes one or more of assignments for the variables, the data structures, or the objects, the definition includes one or more of definitions for methods or functions, and the call includes one or more of methods or functions.
5. The method according to any one of claims 1 to 4, characterized in that: The method further comprises: Extracting insertion position indication information from the verification code; Matching the insertion position indication information with the source code to obtain the insertion position in the source code; Insert the verification code into the source code according to the insertion position.
6. The method according to any one of claims 1 to 5, characterized in that: The receiving of configuration information of the verification object and configuration information of the verification property input by the user includes: A verification object configuration file input by a user is received, and a verification property configuration file input by a user is received, wherein the verification object configuration file includes configuration information of the verification object, and the verification property configuration file includes configuration information of the verification property.
7. A code development platform, characterized in that: The code development platform is used to generate verification code for formal verification, and the code development platform includes: The interactive subsystem is used to receive configuration information of a verification object and configuration information of a verification property input by a user, wherein the verification object is a code snippet in the source code of the system being verified, and the configuration information of the verification object is a domain specific language DSL code, wherein the DSL code indicates a code snippet type and a query condition to be queried; A parsing subsystem, configured to query the verification object from the source code of the system to be verified according to the configuration information of the verification object, and parse the configuration information of the verification property to obtain a verification code template, wherein the verification code template includes the verification property for the verification object; The parsing subsystem is further used to generate the verification code according to the verification object and the verification code template, and the verification code includes the verification object and the verification property for the verification object.
8. The platform according to claim 7, characterized in that The parsing subsystem is used to: Translating the DSL code into a query statement based on a database query language; According to the query statement based on the database query language, the code property graph in the database is queried to obtain the verification object, and the code property graph includes at least one of the function call relationship, type inheritance relationship, variable use and definition relationship, control flow graph or data flow graph obtained by parsing the source code.
9. The platform according to claim 7, characterized in that: The parsing subsystem is used to: Parsing the source code to obtain a parsing result; The DSL code is matched with the parsing result to obtain the verification object.
10. The platform according to any one of claims 7 to 9, characterized in that: The code snippet type includes one or more of declaration, assignment, reference, memory reading and writing, loop, conditional judgment, definition, and call, wherein the declaration includes one or more of declarations for variables, data structures, or objects, the assignment includes one or more of assignments for the variables, the data structures, or the objects, the reference includes one or more of assignments for the variables, the data structures, or the objects, the definition includes one or more of definitions for methods or functions, and the call includes one or more of methods or functions.
11. The platform according to any one of claims 7 to 10, characterized in that: The parsing subsystem is also used to: Extracting insertion position indication information from the verification code; Matching the insertion position indication information with the source code to obtain the insertion position in the source code; Insert the verification code into the source code according to the insertion position.
12. The platform according to any one of claims 7 to 11, characterized in that The interaction subsystem is specifically used for: A verification object configuration file input by a user is received, and a verification property configuration file input by a user is received, wherein the verification object configuration file includes configuration information of the verification object, and the verification property configuration file includes configuration information of the verification property.
13. A computing device cluster, characterized in that: The computing device cluster includes at least one computing device, and the at least one computing device includes at least one processor and at least one memory, wherein the at least one memory stores computer-readable instructions; the at least one processor executes the computer-readable instructions so that the computing device cluster performs the code analysis method as described in any one of claims 1 to 6.
14. A computer-readable storage medium, characterized in that: The method comprises computer-readable instructions; the computer-readable instructions are used to implement the code analysis method according to any one of claims 1 to 6.
15. A computer program product, characterized in that The method comprises computer-readable instructions; the computer-readable instructions are used to implement the code analysis method according to any one of claims 1 to 6.