A blockchain smart contract hybrid formal verification method and system
By embedding functional and security specification documents into blockchain smart contracts, generating source code models and compiling them with dependent libraries, and combining symbolic constraints and execution strategies, the problem of low efficiency in smart contract verification is solved, achieving efficient and accurate code defect detection.
Patent Information
- Application Number
- CN202210947911.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-08-08
- Publication Date
- 2025-11-21
- Estimated Expiration
- 2042-08-08
AI Technical Summary
Existing technologies for blockchain smart contracts have low verification efficiency, making it difficult to obtain high-precision code defect verification results within a reasonable timeframe.
By obtaining the functional specification document and security attribute specification document of the smart contract, embedding them into the smart contract source code, generating a smart contract source code model, and instrumenting and compiling it with the dependent libraries, executing the smart contract code using test cases, obtaining vulnerability verification results, and constructing a solution and execution strategy by combining symbolic constraints, the verification efficiency is improved.
It improves the execution efficiency and accuracy of smart contract code defect detection, reduces system performance loss, and enhances the coverage and accuracy of code defect detection.
Smart Images

Figure CN115310095B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of blockchain smart contract security, in particular to a blockchain smart contract hybrid formal verification method and system. BACKGROUND
[0002] Under the background of blockchain technology, a smart contract refers to program code running on a blockchain and capable of performing certain functions. The smart contract is written in a digital form into the blockchain, and the characteristics of the blockchain technology guarantee that the storage, reading and execution processes are transparent, traceable and tamper-proof. Due to the tamper-proof nature of the blockchain, once the smart contract is attacked due to its own design defects, it will cause irreversible significant losses.
[0003] With the gradual industrialization of blockchain technology in the fields of finance and government affairs, the business logic of blockchain smart contracts presents a diversified and complex development trend. In order to meet the business needs, the smart contract needs to implement more complex logic and rely on a large number of specific function libraries for a large number of calculation-intensive operations. In the prior art, the smart contract often needs to be verified to obtain its code defects so that the staff can optimize the smart contract code. However, in the prior art, the execution efficiency is low, and it is difficult to obtain a high-precision code defect verification result within a reasonable time. SUMMARY
[0004] The purpose of the present application is to provide a blockchain smart contract hybrid formal verification method and system, which solves the problem of low execution efficiency in the prior art.
[0005] The present application is achieved by the following technical solutions:
[0006] In a first aspect, the present application provides a blockchain smart contract hybrid formal verification method, comprising:
[0007] obtaining a function specification document, a security attribute specification document and a smart contract source code corresponding to a blockchain smart contract;
[0008] embedding the function specification document and the security attribute specification document into the smart contract source code to obtain a smart contract source code model;
[0009] obtaining a smart contract dependency library required by the smart contract source code model, the smart contract dependency library representing a program library or a database required for executing the smart contract source code model;
[0010] compiling the smart contract dependency library and the smart contract source code model to obtain a dependency library code and a smart contract code, the dependency library code and the smart contract code being machine code or byte code;
[0011] acquire a test case, the test case being used to represent input data when executing the smart contract code;
[0012] execute the smart contract code with the support of the dependency library code and the input of the test case, and acquire a vulnerability verification result;
[0013] acquire a smart contract vulnerability verification document according to the vulnerability verification result.
[0014] Further, the functional specification document includes pre-function specifications and post-function specifications of functions in the smart contract source code, and the security attribute specification document includes pre-security specifications and post-security specifications of functions in the smart contract source code.
[0015] embed the functional specification document and the security attribute specification document in the smart contract source code to acquire a smart contract source code model, including:
[0016] sequentially parse the functional specification document, the security attribute specification document and the smart contract source code into abstract syntax trees to obtain a functional specification abstract syntax tree, a security attribute specification abstract syntax tree and a smart contract abstract syntax tree;
[0017] embed a pre-function specification corresponding part in the functional specification abstract syntax tree after an entrance of a function in the smart contract abstract syntax tree, and embed a post-function specification corresponding part in the functional specification abstract syntax tree before an exit of the function in the smart contract abstract syntax tree;
[0018] embed a pre-security specification corresponding part in the security attribute specification abstract syntax tree after the entrance of the function in the smart contract abstract syntax tree, and embed a post-security specification corresponding part in the security attribute specification abstract syntax tree before the exit of the function in the smart contract abstract syntax tree to obtain the smart contract source code model.
[0019] Further, the smart contract dependency library and the smart contract source code model are instrumented and compiled to obtain the dependency library code and the smart contract code, including:
[0020] compile the smart contract dependency library and the smart contract source code model into machine code or bytecode to obtain first data code and second data code respectively;
[0021] determine a target position of a symbolic constraint in the first data code and / or the second data code, and insert a call function of a solving function at the target position to obtain the dependency library code and the smart contract code respectively.
[0022] Further, the smart contract code is executed with the support of the dependency library code and the input of the test case to acquire a vulnerability verification result, including:
[0023] The smart contract code is executed with the test case as input, and a program path that violates the security property specification and / or the function specification is obtained;
[0024] The program path and the test case corresponding to the program path are taken as a vulnerability verification result.
[0025] Further, when the smart contract code is executed, if there is a symbolic constraint, a call function of a symbolic constraint construction solving function is constructed according to the symbolic constraint, the corresponding symbolic constraint construction solving function is called from a symbolic constraint construction solving library, and symbolic solving is performed.
[0026] Further, when the smart contract code is executed, it is judged whether the execution process triggers the trigger condition corresponding to the execution strategy, if yes, the corresponding execution strategy is executed, otherwise no operation is performed, and the smart contract code is continued to be executed according to the original strategy;
[0027] The execution strategy and the trigger condition correspond to each other.
[0028] Further, after the smart contract code is executed, the method further comprises:
[0029] According to the program path and the symbolic solving result of the execution of the smart contract code, a new test case is determined, and the new test case is used to explore a program path that has not been executed;
[0030] The smart contract code is executed with the new test case as input, and a vulnerability verification result is obtained.
[0031] In a second aspect, the present application provides a blockchain smart contract hybrid formal verification system, comprising a source code modeling device, a plug-in compilation module, a fuzzy test module, a hybrid execution module and a smart contract vulnerability verification document generator;
[0032] The source code modeling device is used to obtain a function specification document, a security attribute specification document and a smart contract source code corresponding to a blockchain smart contract, embed the function specification document and the security attribute specification document in the smart contract source code, and obtain a smart contract source code model; the writing language used by the function specification document and the security attribute specification document is the same as the writing language used by the smart contract source code;
[0033] The plug-in compilation module is used to obtain a smart contract dependent library required by the smart contract source code model, the smart contract dependent library representing a program library or a database required for executing the smart contract source code model; the smart contract dependent library and the smart contract source code model are plug-in compiled to obtain a dependent library code and a smart contract code;
[0034] The fuzzy test module is used to generate and store test cases;
[0035] The hybrid execution module is configured to obtain a test case, the test case being used to represent input data when executing the smart contract code; and execute the smart contract code with the support of the dependency library code and the input of the test case, and obtain a vulnerability verification result.
[0036] The smart contract vulnerability verification document generator is configured to obtain a smart contract vulnerability verification document according to the vulnerability verification result.
[0037] Further, the plug-in compilation module comprises a compilation configuration unit, a general compilation unit and a plug-in unit.
[0038] The compilation configuration unit is configured to obtain a smart contract dependency library required by a smart contract source code model.
[0039] The general compilation unit is configured to compile the smart contract dependency library and the smart contract source code model into machine code or bytecode, and obtain first data code and second data code, respectively.
[0040] The plug-in unit is configured to determine a target position of a symbolic constraint in the first data code and / or the second data code, and insert a calling function of a symbolic constraint construction solving function at the target position to construct a calling function of a solving function, and obtain the dependency library code and the smart contract code, respectively.
[0041] Further, the hybrid execution module comprises an instruction execution unit, a symbolic constraint construction solving library and a heuristic strategy library.
[0042] The instruction execution unit is configured to execute the smart contract code with the support of the dependency library code and the input of the test case, and obtain a program path that violates a security property specification and / or a function specification; and take the program path and the test case corresponding to the program path as the vulnerability verification result.
[0043] The symbolic constraint construction solving library is configured to, when executing the smart contract code, if there is a symbolic constraint, call a corresponding symbolic constraint construction solving function from the symbolic constraint construction solving library according to a calling function of a symbolic constraint construction solving function corresponding to the symbolic constraint, and perform symbolic solving.
[0044] The heuristic strategy library is configured to, when executing the smart contract code, judge whether an execution process triggers a trigger condition corresponding to an execution strategy, if yes, execute the corresponding execution strategy, otherwise, do not perform an operation, and continue to execute the smart contract code according to the original strategy; wherein the execution strategy and the trigger condition correspond to each other.
[0045] Compared with the prior art, the present application has the following advantages and beneficial effects:
[0046] (1) The application provides a blockchain smart contract hybrid formal verification method and system, wherein the smart contract dependency library and the smart contract source code model are compiled by means of plug-in, so that the smart contract dependency library and the smart contract source code model have symbol execution capability; compared with traditional source code level plug-in code verification analysis, the application will benefit from the improvement of the compilation optimization performance, and the processing time of the symbol expression of the traditional symbol execution engine interpretation execution is saved; compared with traditional dynamic bytecode or machine code plug-in, the system performance loss caused by repeated plug-in for different test inputs during dynamic execution is greatly reduced, so that the execution efficiency is improved.
[0047] (2) The application generates new test cases to quickly test different program paths and improve the code defect detection accuracy of complex logic smart contracts.
[0048] (3) The application sets multiple execution strategies, and when the trigger condition is reached, the corresponding execution strategy is executed, so as to improve the verification efficiency. BRIEF DESCRIPTION OF DRAWINGS
[0049] In order to more clearly illustrate the technical solutions of the exemplary embodiments of the application, the following will briefly introduce the drawings needed to be used in the embodiments, and it should be understood that the following drawings only show some embodiments of the application, and therefore should not be regarded as a limitation on the scope, and for those skilled in the art, other related drawings can also be obtained without creative labor. In the drawings:
[0050] Figure 1 A flowchart of a blockchain smart contract hybrid formal verification method provided by the embodiment of the application;
[0051] Figure 2 A structural schematic diagram of a blockchain smart contract hybrid formal verification system provided by the embodiment of the application. DETAILED DESCRIPTION
[0052] In order to make the purpose, technical solutions and advantages of the application more clear and explicit, the following will further describe the application in combination with the embodiments and drawings, and the exemplary embodiments of the application and the description thereof are only used to explain the application, and should not be regarded as a limitation on the application.
[0053] Embodiment 1
[0054] As shown in Figure 1 , the application provides a blockchain smart contract hybrid formal verification method, which comprises:
[0055] S11, acquire the function specification document, the security attribute specification document and the smart contract source code corresponding to the smart contract, the writing language of the function specification document and the security attribute specification document is same as the writing language of the smart contract source code.
[0056] If the writing language of the function specification document and the security attribute specification document is different from the writing language of the smart contract source code, first convert the function specification document and the security attribute specification document, so that the writing language of the function specification document and the security attribute specification document is same as the writing language of the smart contract source code.
[0057] S12, embed the function specification document and the security attribute specification document in the smart contract source code, and acquire the smart contract source code model.
[0058] For example, the functional specification is embedded in the front and back of the function by matching the function name; the global invariant security attribute is embedded in the front and back of all functions, that is, the security attribute that needs to be met before and after the execution of any function; and the function related security attribute is embedded in the front and back of the related function.
[0059] S13, acquire the smart contract dependent library required by the smart contract source code model, the smart contract dependent library represents the program library or database required for executing the smart contract source code model.
[0060] The smart contract dependent library is a third-party program library or database, for example, a string processing library or a security mathematics library, that is, a library required by the smart contract code.
[0061] S14, plug-in compile the smart contract dependent library and the smart contract source code model to obtain the dependent library code and the smart contract code, the dependent library code and the smart contract code are machine code or byte code.
[0062] The smart contract dependent library and the smart contract source code model can be plug-in compiled in the following way: first, translate the smart contract dependent library and the smart contract source code model into target machine code, and then perform symbolic plug-in on the target machine code to make the target machine code have symbol processing capability, to obtain the dependent library code and the smart contract code.
[0063] The target machine code can be the language used by the target device to run the smart contract code.
[0064] S15, acquire the test case, the test case is used to represent the input data when the smart contract code is executed.
[0065] S16, execute the smart contract code with the dependent library code as support and the test case as input, and acquire the vulnerability verification result.
[0066] S17, obtaining a smart contract vulnerability verification document according to the vulnerability verification result.
[0067] In the embodiment, the function specification document includes pre-function specifications and post-function specifications of functions in the smart contract source code, and the security attribute specification document includes pre-security specifications and post-security specifications of functions in the smart contract source code.
[0068] It is worth noting that, unlike the function specification, the pre-security specification and the post-security specification of the security attribute specification can be completely the same, that is, the security attribute invariant, which also needs to be inserted into the entrance and exit of the function respectively, and the attribute that needs to be met before and after execution.
[0069] Embedding the function specification document and the security attribute specification document in the smart contract source code to obtain a smart contract source code model, including:
[0070] Sequentially parsing the function specification document, the security attribute specification document, and the smart contract source code into abstract syntax trees to obtain a function specification abstract syntax tree, a security attribute specification abstract syntax tree, and a smart contract abstract syntax tree.
[0071] Embedding the pre-function specification corresponding part in the function specification abstract syntax tree after the entrance of the function in the smart contract abstract syntax tree, and embedding the post-function specification corresponding part in the function specification abstract syntax tree before the exit of the function in the smart contract abstract syntax tree.
[0072] Embedding the pre-security specification corresponding part in the security attribute specification abstract syntax tree after the entrance of the function in the smart contract abstract syntax tree, and embedding the post-security specification corresponding part in the security attribute specification abstract syntax tree before the exit of the function in the smart contract abstract syntax tree to obtain the smart contract source code model.
[0073] In the embodiment, the smart contract dependency library and the smart contract source code model are instrumented and compiled to obtain a dependency library code and a smart contract code, including:
[0074] Compiling the smart contract dependency library and the smart contract source code model into machine code or bytecode to obtain a first data code and a second data code, respectively.
[0075] Determining the target position of the symbolic constraint in the first data code and / or the second data code, and inserting a call function of a solving function at the target position to obtain a dependency library code and a smart contract code, respectively.
[0076] By inserting the call function of the symbol constraint construction solving function, the symbol constraint construction solving function can be called, so that the symbol execution capability is compiled into the target program and becomes part of the target program, and the hybrid execution engine can benefit from the performance optimization (such as cache, branch prediction optimization) of the CPU. At the same time, compared with the traditional source code level plug-in code verification analysis, the performance improvement of the compilation optimization can be benefited from, the processing time of the symbol expression during the interpretation execution of the traditional symbol execution engine is saved, and compared with the traditional dynamic bytecode or machine code plug-in, the system performance loss caused by repeated plug-in for different test inputs during dynamic execution is greatly reduced.
[0077] It is worth noting that, according to actual needs, part of the smart contract dependency library and / or part of the smart contract source code model can be selected for plug-in compilation, and the program modules that are not plugged in are executed in real time, which can effectively solve the problem that the constraint solver is blocked in individual solving difficulties and cannot continue to explore other program paths, resulting in low overall program path coverage of the hybrid verification system and high false negative rate.
[0078] After obtaining the dependency library code and the smart contract code, the smart contract code and the dependency library code can be linked to generate complete executable combined machine code, and then the combined machine code can be verified.
[0079] In this embodiment, the smart contract code is executed with the support of the dependency library code and the input of the test case, and the vulnerability verification result is obtained, including:
[0080] With the support of the dependency library code and the input of the test case, the smart contract code is executed, and the program path that violates the security attribute specification and / or function specification is obtained.
[0081] The program path and the test case corresponding to the program path are taken as the vulnerability verification result.
[0082] For example, if a path violates the function specification or the security attribute specification, the path will be reported and the test case that can reach the path will be given. For example, the specification is assert(a>10); if a path can make the value of a become 9 (not greater than 10), and the path passes through the assert(a>10) specification assertion, the specification is violated.
[0083] The smart contract dependency library or the smart contract code can contain multiple functions or subprograms, and the executed functions or subprograms are different when the input parameters are different. The combination of the executed functions or subprograms can be considered as a program path.
[0084] In the embodiment, when the smart contract code is executed, if the symbolic constraint exists, a call function of a symbolic constraint construction solving function is constructed according to the symbolic constraint, the symbolic constraint construction solving function corresponding to the symbolic constraint is called from a symbolic constraint construction solving library, and symbolic solving is performed.
[0085] In the embodiment, when the smart contract code is executed, it is judged whether the execution process triggers a trigger condition corresponding to an execution strategy, if yes, the corresponding execution strategy is executed, otherwise no operation is performed, and the smart contract code is continuously executed according to the original strategy; wherein the execution strategy and the trigger condition correspond to each other.
[0086] For example, for a constraint that cannot be solved for a long time, a part of the constraint expression can be selected for solving.
[0087] In the embodiment, after the smart contract code is executed, the method further comprises:
[0088] According to the program path of the smart contract code execution and the symbolic solving result, a new test case is determined, and the new test case is used to explore a program path that has not been executed.
[0089] The smart contract code is executed by taking the dependency library code as support and taking the new test case as input, and a vulnerability verification result is obtained.
[0090] Optionally, the new test case can also be filtered or subjected to a mutation operation, the filtering operation can include: comparing the existing test case with the new test case, if the new test case is the same as the existing test case, the new test case is filtered out. The mutation operation can include: changing the new test case to obtain more test cases to cover new program paths.
[0091] Embodiment 2
[0092] As shown in Figure 2 The present application provides a blockchain smart contract hybrid formal verification system, which comprises a source code modeling device, a plug-in compilation module, a fuzzy test module, a hybrid execution module and a smart contract vulnerability verification document generator.
[0093] The source code modeling device is used to obtain a function specification document, a security attribute specification document and a smart contract source code corresponding to a blockchain smart contract, embed the function specification document and the security attribute specification document in the smart contract source code, and obtain a smart contract source code model; the writing language of the function specification document and the security attribute specification document is the same as the writing language of the smart contract source code.
[0094] For example, the function specification document and the security attribute specification document responding to the human-computer interaction are acquired, and the Fabric-go smart contract source code to be verified is input into the source code modeler. The source code modeler firstly parses the security attribute specification document, the function specification document and the smart contract source code into abstract syntax trees respectively, then embeds the security and function specification abstract syntax trees into the smart contract abstract syntax tree, and finally converts to generate the Fabric-go smart contract source code model containing the formal specification which can be received by the plug-in compilation module.
[0095] The plug-in compilation module is used for acquiring the smart contract dependent library required by the smart contract source code model, the smart contract dependent library representing a program library or a database required for executing the smart contract source code model; and performing plug-in compilation on the smart contract dependent library and the smart contract source code model to obtain the dependent library code and the smart contract code.
[0096] For example, according to the formal verification requirement, the compiling configuration unit function in the plug-in compilation module is used to select the program module requiring symbol plug-in in the Fabric-go smart contract source code model and the smart contract dependent library. The Fabric-go smart contract source code model can be selected as the plug-in module, and the smart contract dependent library can be selected as the non-plug-in module. After the compiling configuration is completed, the plug-in unit in the plug-in compilation module inserts the function call interface for solving the symbol constraint construction into the x86 machine code corresponding to the Fabric-go in the compiling stage, completes the symbol plug-in, and obtains the plugged-in x86 machine code. The conventional compiling unit in the plug-in compilation module directly compiles the non-plug-in module into the x86 machine code and links the plugged-in x86 machine code, and finally generates the complete executable x86 machine code.
[0097] The fuzz testing module is used for generating and storing the test case.
[0098] The hybrid execution module is used for acquiring the test case used for representing the input data when the smart contract code is executed, supporting the dependent library code, taking the test case as the input, executing the smart contract code, and acquiring the vulnerability verification result.
[0099] The smart contract vulnerability verification document generator is used for acquiring the smart contract vulnerability verification document according to the vulnerability verification result.
[0100] In the embodiment, the plug-in compilation module includes the compiling configuration unit, the conventional compiling unit and the plug-in unit.
[0101] The compiling configuration unit is used for acquiring the smart contract dependent library required by the smart contract source code model.
[0102] The conventional compiling unit is configured to compile the smart contract dependency library and the smart contract source code model into machine code or bytecode, to obtain first data code and second data code respectively.
[0103] The plug-in unit is configured to determine a target position of the symbolic constraint in the first data code and / or the second data code, and insert a call function of the symbolic constraint construction solving function at the target position to construct a solving function, to obtain dependency library code and smart contract code respectively.
[0104] In the embodiment, the hybrid execution module includes an instruction execution unit, a symbolic constraint construction solving library, and a heuristic strategy library.
[0105] The instruction execution unit is configured to execute the smart contract code with the dependency library code as support and with a test case as input, to obtain a program path that violates a security property specification and / or a functional specification, and to take the program path and the test case corresponding to the program path as a vulnerability verification result.
[0106] The symbolic constraint construction solving library is configured to, when executing the smart contract code, if there is a symbolic constraint, call a corresponding symbolic constraint construction solving function from the symbolic constraint construction solving library according to a call function of the symbolic constraint construction solving function corresponding to the symbolic constraint, and perform symbolic solving.
[0107] The heuristic strategy library is configured to, when executing the smart contract code, determine whether an execution process triggers a trigger condition corresponding to an execution strategy, and if so, execute the corresponding execution strategy, otherwise, do not perform an operation and continue to execute the smart contract code according to the original strategy; the execution strategy and the trigger condition correspond to each other.
[0108] The heuristic strategy library is further configured to, when executing the corresponding execution strategy, generate a new test case and feed the new test case back to the fuzz testing module. The fuzz testing module is further configured to filter the fed-back test case to leave only new test cases that have not been generated before, and to perform mutation based on the new test cases to obtain more new test cases of interest, for exploring program paths of interest related to the target security property specification or functional specification.
[0109] Suppose the current path is x>0 and the current test case is 10. Two ways to generate a new test case:
[0110] 1. Symbolic constraint solving: if the executed program path has a symbolic constraint (such as x>0), a counterexample (! (x>0)) is directly obtained by solving the symbolic constraint to obtain a new test case (such as 0) that makes x<=0 true.
[0111] 2. Fuzz test module generation: the fuzz test module focuses on the mutation operation of the test case corresponding to the executed program path, to generate a new test case. The new test cases generated by the above two methods are first collected into the fuzz test module, and then input into the smart contract code execution module by the fuzz test module.
[0112] It is worth noting that here "new test cases are used to explore paths that have not been executed" means that it can explore paths that have not been executed with a high probability, but not absolutely. Because the test cases generated by method 1 can absolutely explore new program paths, but the new test cases generated by method 2 still have the possibility of executing old program paths (for example: the mutation of 10 may get -1 or 11, here -1 can explore new paths, and 11 still walks the path that has been executed).
[0113] The instruction execution unit in this embodiment is a local operating system, and the fuzz test module provides test input and receives feedback of new test cases for executable x86 machine code. The executable x86 machine code starts local execution, and when a symbolic constraint construction solving function call is encountered during execution, the symbolic constraint construction solving library provides corresponding symbolic execution function support for the generation of new test cases. The heuristic strategy library will prune the constraint expressions that are difficult to completely solve during execution, and instead solve part of the constraint expressions to obtain new test cases, which will be fed back to the fuzz test module for verification. If the test case can generate new paths, the fuzz test module will further mutate the test case to try to generate test cases that can explore more new paths, otherwise the fuzz test module will ignore this test case feedback.
[0114] During the local execution of the x86 machine code, if a path that violates the security or functional specification is encountered, the corresponding test case and program execution information will be sent to the smart contract vulnerability detection document generator to generate a smart contract vulnerability detection document.
[0115] The application provides a blockchain smart contract hybrid formal verification method and system, wherein the smart contract dependency library and the smart contract source code model are compiled by plugging, so that the smart contract dependency library and the smart contract source code model have the symbolic execution capability; compared with the traditional source code level plug-in code verification analysis, the compilation optimization performance is improved, the processing time of the symbolic expression during the interpretation execution of the traditional symbolic execution engine is saved; compared with the traditional dynamic bytecode or machine code plug-in, the system performance loss caused by repeated plug-in for different test inputs during dynamic execution is greatly reduced, so that the execution efficiency is improved. The application generates new test cases to quickly test different program paths, and improves the code defect detection accuracy of complex logic smart contracts. The application sets multiple execution strategies, and when the trigger condition is reached, the corresponding execution strategy is executed, so that the verification efficiency is improved.
[0116] The above detailed description further describes the purposes, technical solutions and beneficial effects of the application, and it should be understood that the above is only a specific embodiment of the application, and is not used to limit the protection scope of the application, and any modification, equivalent replacement, improvement, etc. within the spirit and principles of the application should be included in the protection scope of the application.
Claims
1.A method for hybrid formal verification of a blockchain smart contract, characterized in that, The method comprises the following steps: obtaining a function specification document, a security attribute specification document and a smart contract source code corresponding to a blockchain smart contract; embedding the function specification document and the security attribute specification document into the smart contract source code to obtain a smart contract source code model; obtaining a smart contract dependency library required by the smart contract source code model, wherein the smart contract dependency library represents a program library or a database required for executing the smart contract source code model; compiling the smart contract dependency library and the smart contract source code model to obtain a dependency library code and a smart contract code, wherein the dependency library code and the smart contract code are machine codes or byte codes; compiling the smart contract dependency library and the smart contract source code model to obtain the dependency library code and the smart contract code, comprising: compiling the smart contract dependency library and the smart contract source code model into machine codes or byte codes to obtain a first data code and a second data code respectively; determining a target position of a symbolic constraint in the first data code and / or the second data code, and inserting a calling function of a symbolic constraint construction solving function at the target position to obtain the dependency library code and the smart contract code respectively; obtaining a test case, wherein the test case is used to represent input data when the smart contract code is executed; executing the smart contract code with the support of the dependency library code and the input of the test case to obtain a vulnerability verification result; obtaining a smart contract vulnerability verification document according to the vulnerability verification result. 2.The method of claim 1, wherein, The function specification document comprises pre-function specifications and post-function specifications of functions in the smart contract source code, and the security attribute specification document comprises pre-security specifications and post-security specifications of functions in the smart contract source code; embedding the function specification document and the security attribute specification document into the smart contract source code to obtain the smart contract source code model, comprising: sequentially parsing the function specification document, the security attribute specification document and the smart contract source code into abstract syntax trees to obtain a function specification abstract syntax tree, a security attribute specification abstract syntax tree and a smart contract abstract syntax tree; embedding a pre-function specification corresponding part in the function specification abstract syntax tree into the smart contract abstract syntax tree after an entrance of a function and embedding a post-function specification corresponding part in the function specification abstract syntax tree into the smart contract abstract syntax tree before an exit of the function; embedding a pre-security specification corresponding part in the security attribute specification abstract syntax tree into the smart contract abstract syntax tree after the entrance of the function and embedding a post-security specification corresponding part in the security attribute specification abstract syntax tree into the smart contract abstract syntax tree before the exit of the function to obtain the smart contract source code model. 3.The method of claim 1, wherein, executing the smart contract code with the support of the dependency library code and the input of the test case to obtain a vulnerability verification result, comprising: executing the smart contract code with the support of the dependency library code and the input of the test case to obtain a program path that violates a security attribute specification and / or a function specification; taking the program path and a test case corresponding to the program path as the vulnerability verification result. 4.The method of claim 3, wherein, When the smart contract code is executed, if there is a symbolic constraint, a calling function of a symbolic constraint construction solving function corresponding to the symbolic constraint is called from a symbolic constraint construction solving library, and symbolic solving is performed. 5.The method of claim 4, wherein, When the smart contract code is executed, it is judged whether the execution process triggers the trigger condition corresponding to the execution strategy; if yes, the corresponding execution strategy is executed, otherwise no operation is performed, and the smart contract code is continued to be executed according to the original strategy; The execution strategy and the trigger condition correspond to each other. 6.The method of claim 5, wherein, After the smart contract code is executed, the following steps are further included: According to the program path of the smart contract code execution and the symbolic solution result, a new test case is determined, and the new test case is used to explore the program path that has not been executed; The smart contract code is executed with the new test case as input to support the dependency library code, and a vulnerability verification result is obtained. 7.A blockchain smart contract hybrid formal verification system, characterized in that, The system includes a source code modeler, a plug-in compilation module, a fuzzy test module, a hybrid execution module, and a smart contract vulnerability verification document generator. The source code modeler is used to obtain a functional specification document, a security attribute specification document, and a smart contract source code corresponding to a blockchain smart contract, embed the functional specification document and the security attribute specification document in the smart contract source code, and obtain a smart contract source code model; the writing language used in the functional specification document and the security attribute specification document is the same as the writing language used in the smart contract source code; The plug-in compilation module is used to obtain a smart contract dependency library required by the smart contract source code model, the smart contract dependency library representing a program library or a database required for executing the smart contract source code model, and plug-in compile the smart contract dependency library and the smart contract source code model to obtain a dependency library code and a smart contract code; the plug-in compilation module includes a compilation configuration unit, a regular compilation unit, and a plug-in unit; the compilation configuration unit is used to obtain the smart contract dependency library required by the smart contract source code model; the regular compilation unit is used to compile the smart contract dependency library and the smart contract source code model into machine code or bytecode to obtain first data code and second data code, respectively; the plug-in unit is used to determine a target position of a symbolic constraint in the first data code and / or the second data code, and insert a call function of a symbolic constraint construction solving function at the target position to obtain the dependency library code and the smart contract code, respectively; The fuzzy test module is used to generate and store test cases; The hybrid execution module is used to obtain test cases, the test cases being used to represent input data when the smart contract code is executed, execute the smart contract code with the test cases as input to support the dependency library code, and obtain a vulnerability verification result; The smart contract vulnerability verification document generator is used to obtain a smart contract vulnerability verification document according to the vulnerability verification result. 8.The hybrid formal verification system of blockchain smart contract according to claim 7, characterized in that, The hybrid execution module includes an instruction execution unit, a symbolic constraint construction solving library, and a heuristic strategy library; The instruction execution unit is used to execute the smart contract code with the test cases as input to support the dependency library code, obtain a program path that violates a security attribute specification and / or a functional specification, and take the program path and the test case corresponding to the program path as the vulnerability verification result. The symbol constraint construction solving library is configured to, when the smart contract code is executed, if there is a symbol constraint, calling a corresponding symbol constraint construction solving function from the symbol constraint construction solving library according to a calling function of the corresponding symbol constraint construction solving function, and performing symbol solving. The heuristic strategy library is configured to, when the smart contract code is executed, judging whether a trigger condition corresponding to an execution strategy is triggered in the execution process; if yes, executing the corresponding execution strategy, otherwise, not performing operation, and continuing to execute the smart contract code according to the original strategy; wherein the execution strategy and the trigger condition correspond to each other.
Citation Information
Patent Citations
Android binary file vulnerability detection method and system
CN108416219A
A highly automated intelligent contract formal verification system and method
CN108985073A