Cross-language function consistency verification method and device, storage medium and program product
By compiling the code into an intermediate representation and using formal verification tools for symbolic execution verification, the problem of maintaining the equivalence of code implementations in different programming languages under all input conditions is solved, achieving efficient and accurate functional consistency verification.
Patent Information
- Application Number
- CN202510835544.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-20
- Publication Date
- 2025-11-14
AI Technical Summary
Existing functional consistency verification methods cannot prove that code implemented in different programming languages remains equivalent under all input conditions. In particular, they are difficult to cover all boundary cases when verifying complex logic, and the input test sample method cannot provide strict mathematical guarantees.
The first and second language codes to be verified are compiled into corresponding intermediate representation files, and formal modeling is performed using a predefined formal verification language. The consistency of the code is verified by symbolic execution using formal verification tools, a formal verification language specification is generated, and code consistency is ensured through equivalence verification.
It improves the efficiency and completeness of functional consistency verification, reduces the limitations of manual code review, ensures the accuracy and completeness of verification results, and can quickly locate and correct problems in the conversion process, thereby improving the accuracy and efficiency of verification.
Smart Images

Figure CN120950364A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of programming language technology, and in particular to a method, apparatus, storage medium, and program product for cross-language functional consistency verification. Background Technology
[0002] In practical software development, especially in scenarios involving system refactoring, language migration, or platform adaptation, developers often need to reimplement functional modules originally implemented in one programming language in another. During such migrations, reliance on the developer's experience and understanding makes it difficult to guarantee strict semantic consistency between the functional algorithms implemented in the new and old code. Therefore, after completing the migration, it is necessary to perform functional consistency verification on the algorithms implemented in different programming languages to avoid serious functional defects, security vulnerabilities, and system crashes.
[0003] Currently, traditional functional consistency verification methods include generating input samples through randomization or mutation, testing the output behavior of functional algorithms implemented in different programming languages under different input samples, in order to discover potential vulnerabilities or errors.
[0004] However, generating input samples through random or mutated methods makes it difficult to cover all possible execution paths, especially when verifying complex logic. Input samples may not reach all boundary cases. At the same time, the way input test samples can only discover defects and cannot provide strict mathematical guarantees. There is a problem that it is impossible to prove that code implemented in different languages remains equivalent under all input conditions. Summary of the Invention
[0005] In view of this, embodiments of the present invention provide a cross-language functional consistency verification method, apparatus, storage medium, and program product to eliminate or improve one or more defects existing in the prior art. It can solve the problem that traditional functional consistency verification methods cannot prove that code implemented in different languages remains equivalent under all input conditions.
[0006] One aspect of the present invention provides a cross-language functional consistency verification method, the method comprising the following steps:
[0007] The first language code to be verified, obtained by programming the project in the first language, is compiled into a first format file of the corresponding intermediate representation. The first format file is then formally modeled based on a predetermined formal verification language, thereby describing the core algorithm logic corresponding to the first language code in terms of functions, and obtaining the first formal verification language specification.
[0008] The second language code to be verified, obtained by programming the project in the second language, is compiled into a second format file of the corresponding intermediate representation. The second format file is then formally modeled based on a predetermined formal verification language, thereby describing the core algorithm logic corresponding to the second language code as a function and obtaining the second formal verification language specification.
[0009] The formal verification tool is used to read the first format file and the first formal verification language specification, and the consistency between the first format file and the first formal verification language specification is verified by symbolic execution. The second format file and the second formal verification language specification are also read, and the consistency between the second format file and the second formal verification language specification is verified by symbolic execution.
[0010] If the first format file and the first formal verification language specification are consistent, and the second format file and the second formal verification language specification are consistent, the equivalence of the first formal verification language specification and the second formal verification language specification is verified by formal verification tools to obtain the consistency verification result.
[0011] In some embodiments of the present invention, the equivalence of the first formal verification language specification and the second formal verification language specification is verified using formal verification tools to obtain a consistency verification result, including:
[0012] If the verification time of the consistency verification exceeds the preset time threshold, obtain the first verified simplified language specification corresponding to the first language code to be verified and the second verified simplified language specification corresponding to the second language code to be verified.
[0013] The equivalence between the first verified simplified language specification and the second verified simplified language specification is verified using formal verification tools, and the consistency verification results are obtained.
[0014] In some embodiments of the present invention, obtaining the verified simplified language specification corresponding to the first language code to be verified and the verified simplified language specification corresponding to the second language code to be verified includes:
[0015] Extract the first simplified function with independent definition and functional semantics from the first language code to be verified, compile it into a third format file, and perform formal modeling on the third format file based on a predefined formal verification language to obtain the third formal verification language specification;
[0016] Extract a second simplified function with independent definition and functional semantics from the second language code to be verified, compile it into a fourth format file, and perform formal modeling on the fourth format file based on a predefined formal verification language to obtain the fourth formal verification language specification;
[0017] Use formal verification tools to read the third format file and the third formal verification language specification, and verify the consistency between the third format and the third formal verification language specification through symbolic execution. Use formal verification tools to read the fourth format file and the fourth formal verification language specification, and verify the consistency between the fourth format file and the fourth formal verification language specification through symbolic execution.
[0018] If the verification results show that the third format file and the third formal verification language specification are consistent, and the fourth format and the fourth formal verification language specification are consistent, then the third formal verification language specification is determined as the verified simplified language specification corresponding to the first language code to be verified, and the fourth formal verification language specification is determined as the verified simplified language specification corresponding to the second language code to be verified.
[0019] In some embodiments of the present invention, when the first language code to be verified and the second language code to be verified are updated, the method further includes:
[0020] The first formal verification language specification is partially adjusted based on the updated content of the first language code to be verified, and the second formal verification language specification is partially adjusted based on the updated content of the second language code to be verified.
[0021] The process involves using formal verification tools to read a first format file and a first formal verification language specification, verifying the consistency between the first format file and the first formal verification language specification through symbolic execution, and then reading a second format file and a second formal verification language specification, verifying the consistency between the second format file and the second formal verification language specification through symbolic execution.
[0022] In some embodiments of the present invention, when the consistency verification result indicates verification failure, the method further includes:
[0023] The formal verification tool generates input parameters that cause inconsistencies between the output of the first formal verification language specification and the output of the second formal verification language specification, which are then output as counterexamples.
[0024] In some embodiments of the present invention, the first language code to be verified includes C language code, and the first format file is an intermediate representation file in LLVM-Bitcode format; the second language code to be verified includes Rust language code, and the second format file is an intermediate representation file in MIR-JSON format.
[0025] In some embodiments of the present invention, the formal verification tool includes the SAW software analysis workbench.
[0026] Another aspect of the present invention provides a cross-language functional consistency verification device, including a processor, a memory, and a computer program / instructions stored in the memory. The processor is used to execute the computer program / instructions. When the computer program / instructions are executed, the device implements the steps of the cross-language functional consistency verification method as described above.
[0027] Another aspect of the present invention provides a computer-readable storage medium having a computer program / instructions stored thereon, which, when executed by a processor, implements the steps of the cross-language functional consistency verification method as described above.
[0028] Another aspect of the present invention provides a computer program product, including a computer program / instruction that, when executed by a processor, implements the steps of the cross-language functional consistency verification method as described above.
[0029] The cross-language functional consistency verification method and apparatus of the present invention can solve the problem that traditional functional consistency verification methods cannot prove that code implemented in different languages remains equivalent under all input conditions. The method compiles the code to be verified in the first language into a first format file corresponding to the intermediate representation, and formalizes the first format file using a unified predetermined formal verification language to obtain a first formal verification language specification. The method also compiles the code to be verified in the second language into a second format file corresponding to the intermediate representation, and formalizes the second format file using a unified predetermined formal verification language to obtain a second formal verification language specification. Finally, the method uses a formal verification tool to verify the functional consistency between the first and second formal verification language specifications using symbolic execution, thereby indirectly achieving functional consistency between code implemented in different languages. Compared to traditional coverage-driven verification methods that rely on a large number of test cases, this method can comprehensively verify the functional consistency between code implementations in different languages without constructing massive input samples. This significantly improves verification efficiency and completeness. The method avoids the limitations of manual code review in terms of coverage and accuracy, and effectively reduces potential system defects caused by manual conversion, semantic differences, or implementation deviations. Furthermore, to ensure functional consistency between the first format file and the first formal verification language specification, and between the second format file and the second formal verification language specification, formal verification tools are needed to verify the functional consistency between the first format file and the first formal verification language specification, and between the second format file and the second formal verification language specification, respectively. This further improves the accuracy of the verification.
[0030] In addition, if the consistency verification result indicates failure, the formal verification tool can generate and output the input parameters that cause the inconsistency between the output of the first formal verification language specification and the second formal verification language specification as a counterexample. This can help users quickly locate and correct problems in the conversion process, thereby reducing the time cost of debugging and testing.
[0031] In addition, when the verification time of consistency verification exceeds the preset time threshold, a verified simplified language specification is introduced to replace the complex first formal verification language specification and the second formal verification language specification. This avoids repeatedly expanding its internal logic, which can significantly reduce the number of path explorations and the solver burden, thereby improving the overall verification efficiency and scalability.
[0032] Additional advantages, objects, and features of the invention will be set forth in part in the description which follows, and will also become apparent in part to those skilled in the art upon studying the description, or may be learned by practice of the invention. The objects and other advantages of the invention can be realized and obtained by means of the structures specifically pointed out in the description and drawings.
[0033] Those skilled in the art will understand that the objectives and advantages achievable with the present invention are not limited to those specifically described above, and that the above and other objectives achievable with the present invention will become clearer from the following detailed description. Attached Figure Description
[0034] The accompanying drawings, which are included to provide a further understanding of the invention and form part of this application, are not intended to limit the scope of the invention. In the drawings:
[0035] Figure 1 This is a flowchart of a cross-language functional consistency verification method provided in an embodiment of the present invention.
[0036] Figure 2 This is a structural diagram of a cross-language functional consistency verification device provided in an embodiment of the present invention.
[0037] Figure 3 This is a flowchart for generating an initial formal representation, provided as an embodiment of the present invention.
[0038] Figure 4 A flowchart for verifying the functional consistency of the initial formal representation provided in an embodiment of the present invention.
[0039] Figure 5 A flowchart for functional consistency verification of a formal representation provided in embodiments of the present invention. Detailed Implementation
[0040] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the embodiments and accompanying drawings. Here, the illustrative embodiments and descriptions of this invention are used to explain the invention, but are not intended to limit the invention.
[0041] It should also be noted that, in order to avoid obscuring the invention with unnecessary details, only the structures and / or processing steps closely related to the solution according to the invention are shown in the accompanying drawings, while other details that are not closely related to the invention are omitted.
[0042] It should be emphasized that the term "including / comprises" as used herein refers to the presence of a feature, element, step, or component, but does not exclude the presence or addition of one or more other features, elements, steps, or components.
[0043] It should also be noted that, unless otherwise specified, the term "connection" in this article can refer not only to a direct connection, but also to an indirect connection involving an intermediary.
[0044] In the following description, embodiments of the invention will be illustrated with reference to the accompanying drawings. In the drawings, the same reference numerals represent the same or similar parts, or the same or similar steps.
[0045] The cross-language functional consistency verification method provided in this application will be described in detail below.
[0046] like Figure 1 As shown, embodiments of this application provide a cross-language functional consistency verification method for use in a cross-language functional consistency verification device. This device can be a mobile phone, computer, or server; this embodiment does not limit the type of device used in the cross-language functional consistency verification device. The method includes at least steps S101 to S104:
[0047] Step S101: The first language code to be verified obtained after programming the project in the first language is compiled into a first format file corresponding to the intermediate representation, and the first format file is formally modeled based on a predetermined formal verification language, so as to describe the core algorithm logic corresponding to the first language code with functions and obtain the first formal verification language specification.
[0048] The first-language code to be verified refers to the algorithm program code implemented using the first language programming. It is the target object to be verified and its logical correctness or functional consistency with code implemented in other languages needs to be checked through formal verification tools.
[0049] The first language includes, but is not limited to, C, C++, Swift, or Rust. Correspondingly, the first language code to be verified includes, but is not limited to, algorithm code implemented in C, C++, Swift, or Rust. This embodiment does not limit the implementation method of the first language and the first language code to be verified.
[0050] The first format file refers to the intermediate representation (IR) file generated after compiling the code in the first language to be verified. It is an abstract representation of the code, preserving the logical structure but removing the syntactic details. For example, if the code is an algorithm implemented in C, the first format file is LLVM-Bitcode (a .bc file, generated by the clang compiler); if the code is an algorithm implemented in Rust, the first format file may include either LLVM-Bitcode (a .bc file) or MIR-JSON (a .json file, generated by the rustc compiler).
[0051] A predefined formal verification language refers to a modeling language with strict mathematical semantics, used to abstract program logic into unambiguous formal expressions. In some embodiments of the present invention, the predefined formal verification language includes, but is not limited to, Cryptol (the Language of Cryptography), Isabelle (a modeling language), etc.
[0052] Step S102 involves compiling the second language code to be verified obtained by programming the project in the second language into a second format file corresponding to the intermediate representation, and formally modeling the second format file based on a predetermined formal verification language, thereby describing the core algorithm logic corresponding to the second language code with functions and obtaining the second formal verification language specification.
[0053] The second language code to be verified refers to the algorithm program code implemented using second language programming. It is the target object to be verified and its logical correctness or functional consistency with code implemented in other languages needs to be checked through formal verification tools.
[0054] The second language includes, but is not limited to, C, C++, Swift, or Rust. Correspondingly, the second language code to be verified includes, but is not limited to, algorithm code implemented in C, C++, Swift, or Rust. This embodiment does not limit the implementation method of the second language and the second language code to be verified.
[0055] The second format file refers to the intermediate representation (IR) file generated after compiling the second language code to be verified. It is an abstract representation of the second language code to be verified, retaining the logical structure but removing the syntactic details. For example, if the second language code to be verified is an algorithm program code implemented in C, the second format file is LLVM-Bitcode (a .bc file, generated by the clang compiler); if the second language code to be verified is an algorithm program code implemented in Rust, the second format file includes either LLVM-Bitcode (a .bc file) or MIR-JSON (a .json file, generated by the rustc compiler).
[0056] In real-world software development projects, especially those involving system refactoring, language migration, or platform adaptation, developers often need to reimplement code originally written in one programming language in another. For example, migrating code implemented in C / C++ to Rust, or migrating code implemented in Python to C++.
[0057] In such projects, relying on the developer's experience and understanding often makes it difficult to guarantee that the functionality implemented in the migrated code is strictly consistent with that of the migrated code. This is especially true when dealing with critical applications such as protocol implementation, cryptographic algorithms, and high-performance computing. Any subtle logical deviations (such as inconsistent boundary condition handling, differences in floating-point precision, or incorrect state machine transition order) can lead to serious functional defects, security vulnerabilities, or even system crashes. Therefore, after completing the migration, it is necessary to verify the functional consistency between the new and old code implementations to avoid serious functional defects, security vulnerabilities, and system crashes.
[0058] However, due to differences in syntax, memory management mechanisms, and runtime behavior among different programming languages, directly performing cross-language functional consistency verification on the original code is easily affected by language characteristics, making it difficult to ensure the accuracy and completeness of the verification results. Therefore, this embodiment proposes a verification method based on formal representation, avoiding the direct use of the original code to ensure the accuracy and completeness of the verification results.
[0059] Specifically, refer to Figure 2 The following example illustrates a cross-language functional consistency verification device, which includes a source code compilation module, a formal representation modeling module, and a first formal verification module. The first language code to be verified is implemented in C, and the second language code to be verified is implemented in Rust.
[0060] like Figure 2 As shown, firstly, the first language code to be verified, implemented in C language, and the second language code to be verified, implemented in Rust language, are input into the original code compilation module. The first language code to be verified and the second language code to be verified are compiled to obtain a first format file (LLVW-Bitcode file) corresponding to the intermediate representation of the first language code to be verified, and a second format file (MIR-JSON file) corresponding to the intermediate representation of the second language code to be verified.
[0061] Next, the first format file and the second format file are input into the formal modeling module. The first format file is formally modeled using a predefined formal verification language to obtain the first formal verification language specification. The second format file is formally modeled using the predefined formal verification language to obtain the second formal verification language specification.
[0062] The following explanation uses Cryptol as an example, with the formal verification language predefined as the Cryptol language. Figure 3 As shown, the process by which the formal modeling module generates the first formal verification language specification and the second formal verification language specification includes at least the following steps S1021 to S1023:
[0063] Step S1021: Extract the definitions of code functions and their related parameters from the first language code to be verified and the second language code to be verified.
[0064] For the first language code and the second language code to be verified that need to verify functional consistency, the code functions are first identified from their original code, and the definitions of the relevant parameters that the code functions depend on during the execution process are extracted.
[0065] Here, a code function refers to a function in the first language code and the second language code to be verified that has independent functional semantics and a clearly defined input / output interface. Relevant parameters include all input / output parameters, local variables, and return value type information that the code function depends on during execution.
[0066] Step S1022: Use the Cryptol language to construct and initialize code functions and their related parameters.
[0067] Specifically, based on the definitions of the code functions and related parameters extracted in step S1011, the corresponding data types and constants are defined in the Cryptol language environment, and initial values are set according to the original implementation logic.
[0068] For example, if the code functions in the first language code and the second language code to be verified involve fixed lookup tables, then the fixed lookup tables need to be declared in the Cryptol language as bit vector arrays; if the code functions involve loop control structures, then the variable scope and state transition rules need to be clearly defined.
[0069] Step S1023: Define the return value to construct a functionally equivalent function with the same behavior, and obtain the first formal verification language specification and the second formal verification language specification.
[0070] The original function logic implemented in C or Rust is used to define return values (including subsequent states, parameter changes, etc.) in Cryptol, constructing functionally equivalent functions with the same behavior. For example, the control flow structure in the original programming language is converted into Cryptol expressions, ensuring that input and output parameter types match, simulating memory access behavior (such as array index access), and handling underlying semantic differences such as integer overflow and bitwise operations.
[0071] Subsequently, in order to further ensure the accuracy of the functional consistency verification results and ensure that the final generated first formal verification language specification and the first language code to be verified maintain consistent behavior output under all legal inputs, as well as the second formal verification language specification and the second language code to be verified maintain consistent behavior output under all legal inputs, it is also necessary to verify the functional consistency between the first formal verification language specification and the first format file corresponding to the first language code to be verified, and the functional consistency between the second formal verification language specification and the second format file corresponding to the second language code to be verified, through the first formal verification module.
[0072] Step S103: Use a formal verification tool to read the first format file and the first formal verification language specification, and verify the consistency between the first format file and the first formal verification language specification through symbolic execution. Then read the second format file and the second formal verification language specification, and verify the consistency between the second format file and the second formal verification language specification through symbolic execution.
[0073] Formal verification tools refer to tools that rigorously verify first format documents and first formal verification language specifications, second format documents and second formal verification language specifications based on mathematical logic and formal methods.
[0074] In some embodiments of the present invention, formal verification tools include, but are not limited to, Software Analysis Workbench (SAW), Concolic Execution verification tools, or KLEE verification tools.
[0075] The following explanation uses a formal verification tool, specifically a software analysis workbench, as an example. When the formal verification tool is a software analysis workbench, in the first formal verification module, SAW script code pre-written in the SAW scripting language is used to verify the consistency between the first format file and the first formal verification language specification, as well as the consistency between the second format file and the second formal verification language specification, through symbolic execution.
[0076] like Figure 4 As shown, the verification process of the first formal verification module includes at least the following steps S1031 to S1034:
[0077] Step S1031: Import the first formal verification language specification, the second formal verification language specification, the first format file, and the second format file, and parse them into SAW Core module (SAWCore) intermediate representations respectively.
[0078] Step S1032: Define the input / output parameters and return values of each function in the SAWCore intermediate representation.
[0079] Specifically, for each function in the SAWCore intermediate representation that requires consistency verification, its input parameters, output parameters, and return value types are clearly defined, and corresponding symbolic variables are defined in the SAW script code. These symbolic variables represent abstract inputs with any legally valid values, rather than specific numerical values, thus supporting symbolic execution path analysis.
[0080] Step S1033: Establish equivalence verification conditions for the execution results of each function.
[0081] In the SAW script code, define the behavioral consistency assertion that the function should satisfy after execution. That is, the function corresponding to the first format file and the function corresponding to the first formal verification language specification should produce the same output parameters and return values under the same input parameters. The function corresponding to the second format file and the function corresponding to the second formal verification language specification should produce the same output parameters and return values under the same input parameters.
[0082] The software analysis workbench will construct verification targets based on behavioral consistency assertions and automatically determine in subsequent stages whether the equivalence can be proven.
[0083] Step S1034: Perform functional consistency verification by simulating the symbolic execution of each functional function and transforming it into a satisfiability modulo theory (SMT) problem.
[0084] Specifically, based on SAWCore intermediate representations, symbolic execution simulation is performed on the execution process of the functional functions, and the verification objective is transformed into a satisfiability modular theory problem. The state transition behaviors of functional functions implemented in two different languages under the same symbolic input are compared to determine whether their outputs are consistent.
[0085] Simultaneously, an external SMT solver (such as Z3, Yices, or CVC5) is invoked for proof. If it can be proven that the equivalence holds on all paths, then the first format file and the first formal verification language specification are determined to be consistent, and the second format file and the second formal verification language specification are determined to be consistent. Then, the consistency verification between the first and second formal verification language specifications is performed. If it cannot be proven that the equivalence holds on all paths, i.e., if there are inputs that cause inconsistencies in the output of the functional function, then it is determined that the first format file and the first formal verification language specification are inconsistent, or the second format file and the second formal verification language specification are inconsistent, and the inputs causing the inconsistent outputs are output as counterexamples.
[0086] Step S104: If the first format file and the first formal verification language specification are consistent, and the second format file and the second formal verification language specification are consistent, the equivalence of the first formal verification language specification and the second formal verification language specification is verified by a formal verification tool to obtain the consistency verification result.
[0087] In some embodiments of the present invention, the equivalence between the first intermediate file corresponding to the first language code to be verified and the first formal verification language specification corresponding to the first intermediate file is verified by a formal verification tool, and the equivalence between the second intermediate file corresponding to the second language code to be verified and the second formal verification language specification corresponding to the second intermediate file is verified.
[0088] If the first format file and the first formal verification language specification are consistent, and the second format file and the second formal verification language specification are consistent, the equivalence of the first formal verification language specification and the second formal verification language specification is verified, thereby indirectly realizing the functional consistency verification between the first language code to be verified and the second language code to be verified.
[0089] like Figure 2 As shown, the cross-language functional consistency verification device also includes a second formal verification module. The second formal verification module receives the first formal verification language specification and the second formal verification language specification output by the first formal verification module, and performs functional consistency verification on the first and second formal verification language specifications using a symbolic execution method through a formal verification tool.
[0090] Taking a formal verification tool including a software analysis workbench as an example. When the formal verification tool is a software analysis workbench, in the second formal verification module, functional consistency verification of the first and second formal verification language specifications is performed using SAW script code pre-written in the SAW scripting language.
[0091] like Figure 5 As shown, the functional consistency verification process of the first and second formal verification language specifications includes:
[0092] First, the first and second formal verification language specifications are imported into the second formal verification module, and the intermediate representation of the SAW core module is obtained by parsing them respectively.
[0093] Next, equivalence verification conditions for the execution results are established. In the SAW script code, a behavioral consistency assertion that should be satisfied after the function is executed is defined. That is, the first formal verification language specification and the second formal verification language specification should produce the same output parameters and return values when the same preset input parameters are input. The software analysis workbench will construct verification targets based on the behavioral consistency assertion and automatically determine whether the equivalence can be proven in subsequent stages.
[0094] Subsequently, symbolic execution simulation of the function's execution process is performed based on the SAW core intermediate representation. The verification objective is transformed into a satisfiability modular theory problem, and an external SMT solver is invoked to solve this problem. If it can be proven that the equivalence holds on all paths, the consistency verification result indicates successful verification, confirming that the first and second formal verification language specifications are functionally consistent, thus verifying the functional consistency between the first and second language codes. Otherwise, if it cannot be proven that the equivalence holds on all paths, i.e., if there are inputs that cause inconsistencies in the function's output, the consistency verification result indicates verification failure. In this case, it is also necessary to identify the inputs that cause inconsistencies in the outputs of the first and second formal verification language specifications as counterexamples.
[0095] Specifically, if the consistency verification result indicates that the verification has failed, it also includes: using formal verification tools to generate input parameters that cause the output of the first formal verification language specification to be inconsistent with the output of the second formal verification language specification, as a counterexample output.
[0096] Furthermore, during the functional consistency proof process, the symbolic execution path explosion can occur due to the complex control flow structures or deeply nested conditional judgments contained in the first and second language codes to be verified, thus affecting verification efficiency. To address this issue, in some embodiments of the present invention, when the verification time exceeds a preset time threshold (e.g., 1 minute, 5 minutes, or 10 minutes), a verified simplified language specification is introduced to replace the complex first and second formal verification language specifications, thereby accelerating the verification process.
[0097] Specifically, the equivalence of the first formal verification language specification and the second formal verification language specification is verified using formal verification tools to obtain a consistency verification result, including: when the verification time of the consistency verification exceeds a preset time threshold, obtaining the first verified simplified language specification corresponding to the first language code to be verified and the second verified simplified language specification corresponding to the second language code to be verified; verifying the equivalence of the first verified simplified language specification and the second verified simplified language specification using formal verification tools to obtain a consistency verification result.
[0098] The simplified functions are derived from key functions used within the first or second language code to be verified. Key functions are functions with independently declared bodies that perform specific computations or logical operations and partially contain complex decision-making logic. In the verification process, these simplified functions are first formally modeled separately to obtain simplified language specifications, and functional consistency verification is performed. If the verification result indicates success, the verified simplified language specifications are saved as trusted modules.
[0099] Specifically, obtaining the verified simplified language specifications corresponding to the first language code to be verified and the second language code to be verified includes: extracting a first simplified function with independent definition and functional semantics from the first language code to be verified, compiling it into a third format file, and performing formal modeling on the third format file based on a predetermined formal verification language to obtain a third formal verification language specification; extracting a second simplified function with independent definition and functional semantics from the second language code to be verified, compiling it into a fourth format file, and performing formal modeling on the fourth format file based on a predetermined formal verification language to obtain a fourth formal verification language specification; and using formal verification tools to read the third... The consistency between the format file and the third formal verification language specification is verified by symbolic execution. A formal verification tool is used to read the fourth format file and the fourth formal verification language specification, and the consistency between the fourth format file and the fourth formal verification language specification is verified by symbolic execution. If both the third and fourth format files are consistent, the third formal verification language specification is determined as the verified simplified language specification corresponding to the first language code to be verified, and the fourth formal verification language specification is determined as the verified simplified language specification corresponding to the second language code to be verified.
[0100] In subsequent functional consistency verification, the verified simplified language specification is imported to replace the first formal verification language specification and the first formal verification language specification, avoiding repeated expansion of its internal logic. This can significantly reduce the number of path explorations and the solver burden, thereby improving the overall verification efficiency and scalability.
[0101] Specifically, using formal verification tools and preset input / output parameters, symbolic execution is employed to perform functional consistency verification among multiple formal representations, yielding consistency verification results. This includes: extracting functions with independent definitions and functional semantics from each functional algorithm as simplified functions; generating a formal representation in a unified formal verification language for each simplified function; using formal verification tools and input / output parameters, performing consistency verification on each simplified function and its corresponding formal representation via symbolic execution to obtain multiple consistency verification results; marking simplified functions that pass the consistency verification as verified functions; and importing the formal representations of the verified functions as trusted modules in the consistency verification process corresponding to multiple formal representations, replacing the first and second formal verification language specifications to perform functional consistency verification.
[0102] Furthermore, in practical implementations, in scenarios involving multi-language development and collaborative verification, the language code implemented in different languages may be updated due to changes in requirements, performance optimizations, or defect fixes. To ensure that these changes do not introduce semantic inconsistencies or behavioral deviations, it is necessary to promptly reflect the changes in the formal verification language specification and re-verify the functional equivalence between the implementations.
[0103] Meanwhile, due to the highly abstract, modular, and executable nature of the unified formal verification language, local modifications to the formal verification language specification are well-traceable and verifiable. With the help of formal verification tools, under finite input and output constraints, it is possible to automatically determine whether the updated formal verification language specification maintains consistency with other versions, thereby effectively improving system reliability and maintenance efficiency.
[0104] Specifically, when the first language code to be verified and the second language code to be verified are updated, the method further includes: making partial adjustments to the first formal verification language specification based on the updated content of the first language code to be verified, and making partial adjustments to the second formal verification language specification based on the updated content of the second language code to be verified; performing the steps of reading the first format file and the first formal verification language specification using a formal verification tool, verifying the consistency between the first format file and the first formal verification language specification through symbolic execution, and reading the second format file and the second formal verification language specification, verifying the consistency between the second format file and the second formal verification language specification through symbolic execution.
[0105] In summary, the cross-language functional consistency verification algorithm provided in this embodiment can solve the problem that traditional functional consistency verification methods cannot prove that code implemented in different languages remains equivalent under all input conditions. It compiles the first language code to be verified into a first format file corresponding to the intermediate representation, and formalizes the first format file using a unified predefined formal verification language to obtain a first formal verification language specification. Similarly, it compiles the second language code to be verified into a second format file corresponding to the intermediate representation, and formalizes the second format file using a unified predefined formal verification language to obtain a second formal verification language specification. Finally, it uses a formal verification tool to verify the functional consistency between the first and second formal verification language specifications using symbolic execution, thereby indirectly achieving functional consistency verification between code implemented in different languages. Compared to traditional coverage-driven verification methods that rely on a large number of test cases, consistency verification can comprehensively verify the functional consistency between code implementations in different languages without constructing massive input samples. This significantly improves verification efficiency and completeness. The method avoids the limitations of manual code review in terms of coverage and accuracy, and effectively reduces potential system defects caused by manual conversion, semantic differences, or implementation deviations. Furthermore, to ensure functional consistency between the first format file and the first formal verification language specification, and between the second format file and the second formal verification language specification, formal verification tools are needed to perform functional consistency verification between the first format file and the first formal verification language specification, and between the second format file and the second formal verification language specification, respectively. This further improves the accuracy of the verification.
[0106] In addition, if the consistency verification result indicates failure, the formal verification tool can generate and output the input parameters that cause the inconsistency between the output of the first formal verification language specification and the second formal verification language specification as a counterexample. This can help users quickly locate and correct problems in the conversion process, thereby reducing the time cost of debugging and testing.
[0107] In addition, when the verification time of consistency verification exceeds the preset time threshold, a verified simplified language specification is introduced to replace the complex first formal verification language specification and the second formal verification language specification. This avoids repeatedly expanding its internal logic, which can significantly reduce the number of path explorations and the solver burden, thereby improving the overall verification efficiency and scalability.
[0108] Corresponding to the above method, the present invention also provides a cross-language functional consistency verification device, which includes a computer device, the computer device including a processor and a memory, the memory storing computer instructions, the processor being used to execute the computer instructions stored in the memory, and when the computer instructions are executed by the processor, the device implements the steps of the cross-language functional consistency verification method as described above.
[0109] This invention also provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the steps of the aforementioned cross-language functional consistency verification method. The computer-readable storage medium may be a tangible storage medium, such as random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, register, floppy disk, hard disk, removable storage disk, CD-ROM, or any other form of storage medium known in the art.
[0110] Those skilled in the art will understand that the exemplary components, systems, and methods described in conjunction with the embodiments disclosed herein can be implemented in hardware, software, or a combination of both. Whether implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this invention. When implemented in hardware, it can be, for example, electronic circuits, application-specific integrated circuits (ASICs), appropriate firmware, plug-ins, function cards, etc. When implemented in software, the elements of this invention are programs or code segments used to perform the desired tasks. The programs or code segments can be stored in a machine-readable medium or transmitted over a transmission medium or communication link via data signals carried in a carrier wave.
[0111] It should be clarified that the present invention is not limited to the specific configurations and processes described above and shown in the figures. For the sake of brevity, detailed descriptions of known methods are omitted here. In the above embodiments, several specific steps are described and shown as examples. However, the method process of the present invention is not limited to the specific steps described and shown. Those skilled in the art can make various changes, modifications, and additions, or change the order of steps, after understanding the spirit of the present invention.
[0112] In this invention, features described and / or illustrated for one embodiment may be used in the same or similar manner in one or more other embodiments, and / or combined with or in place of features of other embodiments.
[0113] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, various modifications and variations of the embodiments of the present invention are possible. 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 method for cross-language functional consistency verification, characterized in that, The method includes the following steps: The first language code to be verified, obtained by programming the project in the first language, is compiled into a first format file corresponding to the intermediate representation. The first format file is then formally modeled based on a predetermined formal verification language, thereby describing the core algorithm logic corresponding to the first language code as a function, and thus obtaining the first formal verification language specification. The second language code to be verified, obtained by programming the project in the second language, is compiled into a second format file with a corresponding intermediate representation. The second format file is then formally modeled based on the predetermined formal verification language, thereby describing the core algorithm logic corresponding to the second language code as a function, and thus obtaining the second formal verification language specification. The formal verification tool is used to read the first format file and the first formal verification language specification, and the consistency between the first format file and the first formal verification language specification is verified by symbolic execution. The second format file and the second formal verification language specification are also read, and the consistency between the second format file and the second formal verification language specification is verified by symbolic execution. If the first format file and the first formal verification language specification are consistent, and the second format file and the second formal verification language specification are consistent, the equivalence of the first formal verification language specification and the second formal verification language specification is verified by the formal verification tool to obtain the consistency verification result.
2. The method according to claim 1, characterized in that, The step of verifying the equivalence of the first formal verification language specification and the second formal verification language specification using the formal verification tool to obtain a consistency verification result includes: If the verification time of the consistency verification exceeds a preset time threshold, the first verified simplified language specification corresponding to the first language code to be verified and the second verified simplified language specification corresponding to the second language code to be verified are obtained. The equivalence of the first verified simplified language specification and the second verified simplified language specification is verified using the formal verification tool to obtain the consistency verification result.
3. The method according to claim 2, characterized in that, The step of obtaining the verified simplified language specification corresponding to the first language code to be verified and the verified simplified language specification corresponding to the second language code to be verified includes: Extract a first simplified function with independent definition and functional semantics from the first language code to be verified, compile it into a third format file, and perform formal modeling on the third format file based on the predetermined formal verification language to obtain the third formal verification language specification; Extract a second simplified function with independent definition and functional semantics from the second language code to be verified, compile it into a fourth format file, and perform formal modeling on the fourth format file based on the predetermined formal verification language to obtain the fourth formal verification language specification; The formal verification tool is used to read the third format file and the third formal verification language specification, and the consistency between the third format and the third formal verification language specification is verified by symbolic execution. The formal verification tool is also used to read the fourth format file and the fourth formal verification language specification, and the consistency between the fourth format file and the fourth formal verification language specification is verified by symbolic execution. If the third format file and the third formal verification language specification are consistent, and the fourth format and the fourth formal verification language specification are consistent, the third formal verification language specification is determined as the verified simplified language specification corresponding to the first language code to be verified, and the fourth formal verification language specification is determined as the verified simplified language specification corresponding to the second language code to be verified.
4. The method according to claim 1, characterized in that, If the first language code to be verified and the second language code to be verified are updated, the method further includes: The first formal verification language specification is partially adjusted based on the updated content of the first language code to be verified, and the second formal verification language specification is partially adjusted based on the updated content of the second language code to be verified. The steps are as follows: using a formal verification tool to read the first format file and the first formal verification language specification, verifying the consistency between the first format file and the first formal verification language specification through symbolic execution, and reading the second format file and the second formal verification language specification, verifying the consistency between the second format file and the second formal verification language specification through symbolic execution.
5. The method according to claim 1, characterized in that, If the consistency verification result indicates that the verification failed, the method further includes: The formal verification tool is used to generate input parameters that cause inconsistencies between the output of the first formal verification language specification and the output of the second formal verification language specification, which are then output as counterexamples.
6. The method according to claim 1, characterized in that, The first language code to be verified includes C language code, and the first format file is an intermediate representation file in LLVM-Bitcode format; the second language code to be verified includes Rust language code, and the second format file is an intermediate representation file in MIR-JSON format.
7. The method according to any one of claims 1 to 5, characterized in that, The formal verification tool includes the SAW software analysis workbench.
8. A cross-language functional consistency verification device, comprising a processor, a memory, and a computer program / instructions stored in the memory, characterized in that, The processor is configured to execute the computer program / instructions, and when the computer program / instructions are executed, the device implements the steps of the method as described in any one of claims 1 to 7.
9. A computer-readable storage medium having a computer program / instructions stored thereon, characterized in that, When the computer program / instructions are executed by the processor, they implement the steps of the method as described in any one of claims 1 to 7.
10. A computer program product comprising a computer program / instructions, characterized in that, When the computer program / instructions are executed by the processor, they implement the steps of the method according to any one of claims 1 to 7.
Citation Information
Cited By
Register and memory operation code unified generation method
CN121166094A
Multi-language-oriented source code automatic verification method and system
CN121681323A
