A smart contract code checking method and system for data management and control
By designing a multi-source, multi-sink static analysis algorithm based on taint analysis, the problem that existing technologies cannot effectively inspect smart contracts on data governance blockchain platforms is solved, realizing automated smart contract auditing, improving efficiency and reducing costs.
Patent Information
- Application Number
- CN202010809496.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-08-12
- Publication Date
- 2025-01-10
- Estimated Expiration
- 2040-08-12
AI Technical Summary
Existing static analysis methods for smart contracts are mainly applicable to the cryptocurrency field and cannot effectively support blockchain platforms for data governance. This results in data users having to manually audit smart contracts, which is inefficient and costly.
We design a multi-source, multi-sink static analysis algorithm based on taint analysis. By defining data dependency types and related calculations, we identify taint sources, perform data value transfer and control dependency analysis, generate analysis reports, and achieve automated smart contract code inspection.
It enables automated checks on data usage constraints, ensuring the proper functioning of smart contracts on the data governance blockchain platform and reducing the need and cost of manual auditing.
Smart Images

Figure CN112115472B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a smart contract code checking method for data control and a smart contract code checking system for data control. Background Art
[0002] Smart contracts are executable codes deployed on the blockchain. The blockchain provides a trusted execution environment for smart contracts. In addition to the traditional application of smart contracts in the field of cryptocurrency, such as Ethereum, smart contracts are now widely used in finance, voting, supply chain, e-commerce and other fields. Once a smart contract is executed on the chain, it is irreversible, and potential vulnerabilities must be discovered before it is executed.
[0003] Program static analysis is the main means of discovering vulnerabilities in a program before it is executed. Smart contracts are a special type of program, and smart contracts in different fields need to be statically analyzed for specific fields to ensure the accurate operation of smart contracts. However, the existing methods for static analysis of smart contracts are only applicable to the field of cryptocurrency, such as integer overflow, fallback detection, etc.
[0004] With the widespread use of blockchain in different fields, blockchain platforms for data full life cycle management have emerged. In order to protect the rights and interests of data owners, the original data is not recorded in the ledger like the transaction data of cryptocurrency and shared by all participants. Instead, data users initiate requests to data owners by providing smart contracts. Data owners need to audit whether the smart contracts use their data reasonably and legally before users can use the data. At present, in blockchain platforms for data management, due to the lack of corresponding static analysis methods for smart contracts, the practice of manually auditing smart contracts entirely by data users is inefficient and costly. Summary of the invention
[0005] In view of the above problems, an embodiment of the present invention is proposed to provide a smart contract code checking method for data control and a smart contract code checking system for data control that overcome the above problems or at least partially solve the above problems.
[0006] In order to solve the above problems, an embodiment of the present invention provides a smart contract code inspection method for data control, the method comprising: pre-defining data dependency types and related calculations of the data dependency types, designing a multi-source and multi-sink static analysis algorithm based on taint analysis, so as to define data usage constraints based on specific data control strategies and perform static analysis; for the target contract function in the target contract file, determining the taint source, the taint source comprising: program input parameters and contract call instructions; based on the static analysis algorithm, adding the taint source to the analysis result before the execution of the starting block of the control flow graph to initialize the analysis result, by traversing the program control flow graph, selecting the corresponding data usage constraints, performing data value transfer dependency analysis and / or data control dependency analysis, continuously updating the analysis results after execution, and finally returning the final analysis result; generating and returning an analysis report based on the final analysis result.
[0007] Optionally, data dependency types and related calculations of the data dependency types are predefined, and a multi-source and multi-sink static analysis algorithm based on taint analysis is designed to define data usage constraints based on specific data management and control policies and perform static analysis, including: focusing on data usage management and control requirements, by analyzing the bytecode instructions after smart contract compilation, defining data dependency types between variables, that is, association types between variables, including data value transfer dependency associations and data control dependency associations, and at the same time, based on the data usage constraint description method, defining association types and association type calculations.
[0008] Optionally, the data value transfer dependency includes: inclusion dependency, assignment dependency, arithmetic dependency and function dependency between program variables and data sources, representing explicit information flow propagation between data; the data control dependency includes control dependency between program variables and data sources, representing implicit information flow propagation between data.
[0009] Optionally, for the target contract function in the target contract file, the taint source is determined, including: directly marking the program input parameter as the taint source; traversing all basic blocks in the control flow graph, finding the contract call instructions in the program, and then marking the contract call instructions as the taint source.
[0010] Optionally, traverse all basic blocks in the control flow graph to find the contract call instructions in the program, including: traverse all basic blocks, and for each basic block, traverse all bytecode instructions. If an instruction type is "kInstrInvoke" type, and the instruction name contains "dyn:call:executeContract", it means it is a dynamic function call instruction, and the function name is "executeContract", which is a contract call instruction.
[0011] Optionally, the data value transfer dependency analysis includes: adding the starting block of the control flow graph to the analyzed working set to initialize the working set; traversing the control flow graph, taking out each node of the control flow graph, and judging whether the node outgoing edge is empty, that is, judging whether the current basic block has a subsequent block; if there is a subsequent block, according to the type of the node, using the corresponding dependency analysis transfer function, the taint source is propagated according to the dependency type of the instructions contained in each basic block, and the node is added to the working set; when the control flow graph traversal is completed, the working set is returned to end the data value transfer dependency analysis.
[0012] Optionally, the data control transfer analysis includes: traversing all basic blocks in the control flow graph, finding all jumpable basic blocks in the control flow graph, that is, branch blocks; and determining whether each basic block in the control flow graph depends on each branch block.
[0013] Optionally, judging whether each basic block in the control flow graph depends on each branch block includes: traversing all branch blocks in the control flow graph through a triple traversal loop, and obtaining all subsequent blocks of each branch block; traversing all basic blocks in the control flow graph, and traversing all subsequent blocks of each basic block, and judging whether all subsequent blocks can reach a basic block in the control flow graph based on a depth-first traversal algorithm DFS, and if not, the basic block depends on the current branch block.
[0014] Optionally, the analysis report includes: analysis parameters, final analysis results, contract code and program control flow graph; wherein the analysis parameters include data source and data usage constraint type information, and the final analysis results include how the data depends on the taint source and whether the final analysis results pass the static analysis.
[0015] The embodiment of the present invention also provides a smart contract code checking system for data management and control, and the system specifically includes:
[0016] An algorithm preset module is used to predefine data dependency types and related calculations of the data dependency types, and to design a multi-source and multi-sink static analysis algorithm based on taint analysis, so as to define data usage constraints and perform static analysis based on specific data management policies;
[0017] A taint source determination module is used to determine the taint source for the target contract function in the target contract file, wherein the taint source includes: program input parameters and contract call instructions;
[0018] A static analysis module, for adding the taint source to the analysis result before the execution of the start block of the control flow graph based on the static analysis algorithm to initialize the analysis result, traverse the program control flow graph, select the corresponding data usage constraint, perform data value transfer dependency analysis and / or data control dependency analysis, continuously update the analysis result after execution, and finally return the final analysis result;
[0019] The analysis report export module is used to generate and return an analysis report based on the final analysis result.
[0020] It can be seen from the above technical scheme that the embodiment of the present invention provides a smart contract code inspection method and system for data control, which predefines data dependency types and related calculations, defines data usage constraints based on specific data control strategies and performs static analysis, selects corresponding data usage constraints in the static analysis, and then performs corresponding data dependency analysis methods, and finally returns an analysis report. The embodiment of the present invention defines data constraints and performs static analysis based on specific data control strategies, and can automatically check smart contract codes with complex data usage constraint definitions, thereby completing automated audits of smart contracts for data control, and ensuring the normal operation of smart contracts on the blockchain platform for data control without the need for data owners to personally audit each transaction. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 It is a technical architecture diagram of a blockchain platform for data control;
[0022] Figure 2 This is an example diagram of a data privacy protection strategy based on smart contracts;
[0023] Figure 3 It is a flowchart of the steps of a smart contract code checking method for data control provided by the present invention;
[0024] Figure 4 It is a schematic diagram of a stain analysis process provided by an embodiment of the present invention;
[0025] Figure 5 It is a flow chart of a multi-source and multi-sink static analysis algorithm provided by an embodiment of the present invention;
[0026] Figure 6 A data value transfer dependency analysis flow chart provided by an embodiment of the present invention;
[0027] Figure 7 It is a data control dependency analysis flow chart provided by an embodiment of the present invention;
[0028] Figure 8A flowchart for searching all branch blocks in a control flow graph provided by an embodiment of the present invention;
[0029] Fig. 9 It is a flow chart of a reachability analysis algorithm for a directed graph based on a depth-first traversal algorithm provided by an embodiment of the present invention;
[0030] Fig.10 It is a BFS traversal flow chart provided by an embodiment of the present invention;
[0031] Fig.11 It is a structural block diagram of a smart contract code checking system for data management and control provided by the present invention. DETAILED DESCRIPTION
[0032] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0033] Figure 1 The technical architecture diagram of a blockchain platform for data management and control is shown. Its trusted computing layer is centered around the smart contract language and provides a contract development environment, a running environment, and third-party library call modules for other programming languages. It implements trusted computing with random multi-node mutual verification and supports calling the interface of the trusted storage layer to store the process information of contract execution in the trusted storage layer.
[0034] Figure 2 An example diagram of a data privacy protection strategy based on smart contracts is shown. Figure 2 As shown, the example uses two contracts: an algorithm contract written by the data user and a data contract called by the algorithm contract to count the number of males and females. As shown in the figure, because the algorithm contract has a sensitive policy for data privacy protection, it allows the return of the list of personnel based on the gender statistics, but does not allow the return of a data list containing names and genders. That is, the contract only allows the return of the statistical results of the male-female gender ratio, rather than the original data list containing the one-to-one correspondence between names and genders. Therefore, based on this sensitive policy, the data owner needs to review the algorithm contract to determine whether it meets the sensitive policy. For data owners, it is inefficient and costly to rely entirely on manual judgment to determine whether a smart contract uses data reasonably and legally (that is, to review whether the smart contract complies with the data privacy protection policy).
[0035] Existing static analysis tools for smart contracts are mainly used to check potential vulnerabilities such as code style and syntax errors. However, for smart contracts on blockchain platforms oriented to data control, static analysis tools are needed to define data constraints and perform static analysis based on specific data control policies. Therefore, existing static analysis tools for smart contracts cannot be used for static analysis of smart contracts on blockchain platforms oriented to data control.
[0036] Embodiment 1
[0037] Figure 3 This is a flowchart of a smart contract code checking method for data control provided by the present invention. Figure 3 As shown, the method embodiment provided by the present invention can be applied to a blockchain platform for data management and control, and specifically includes the following steps:
[0038] Step S101, predefine data dependency types and related calculations of the data dependency types, and design a multi-source and multi-sink static analysis algorithm based on taint analysis to define data usage constraints and perform static analysis based on specific data management policies.
[0039] In an embodiment of the present invention, a multi-source and multi-sink static analysis algorithm based on taint analysis is designed, which requires pre-construction of a control flow graph. When constructing the control flow graph, the bytecode instructions after smart contract compilation are analyzed to meet the data usage management and control needs, and the data dependency types between variables, that is, association types, are defined, including data value transfer dependency associations and data control dependency associations. At the same time, a data usage constraint description method is given based on the data type and its related calculations.
[0040] In an embodiment of the present invention, the data value transfer dependency includes: inclusion dependency, assignment dependency, arithmetic dependency and function dependency between program variables and data sources, representing explicit information flow propagation between data; the data control dependency includes control dependency between program variables and data sources, representing implicit information flow propagation between data.
[0041] Data usage constraints are reflected as data dependencies in program static analysis. Traditional data dependency analysis only needs to consider direct data value transfer dependencies, and mainly analyzes a single data source. However, the definition of data usage constraints for smart contracts on blockchain platforms for data control is complex, and the calls between smart contracts make the data sources diverse. To ensure that data is manageable and controllable, smart contracts on blockchain platforms for data control need to define data usage constraints and perform checks based on specific data control strategies.
[0042] Therefore, based on the association type and the related calculations of the association type, an embodiment of the present invention provides a data usage constraint description method. Using this constraint description method, the data owner can conveniently describe data usage specifications such as "original data cannot be returned, but statistical data can be returned" as data usage constraints that can be processed by the program and verified.
[0043] According to the data usage constraint description method, an embodiment of the present invention provides a static analysis algorithm for data usage constraint checking based on taint analysis, which can define data constraints and perform static analysis based on specific data management and control strategies to complete automated auditing of smart contracts for data management.
[0044] Traditional taint analysis technology only needs to analyze whether the data introduced by the taint source in the program can be propagated to the taint sink, and both the taint source and the taint sink are single. In smart contracts for data control, in addition to calling the execution results of other smart contracts as the input of the program (i.e., the taint source) multiple times, the output of the program (i.e., the taint sink) can also be used as the input of other smart contracts multiple times to output part of the data, that is, the taint source and the taint sink are diverse. Therefore, the static analysis algorithm implemented in the embodiment of the present invention supports multi-source and multi-sink data usage constraint analysis.
[0045] Among them, the control flow graph (CFG), also called the program control flow graph, is an abstract representation of a process or program. It represents all the paths that a program will traverse during execution and is an important tool for compiler optimization and program static analysis. The control flow graph consists of two parts: basic blocks and execution flow. Among them, the basic block is the largest unit that can be executed continuously in a program or the smallest unit of program control. It is characterized by a single entry, a single exit, and no branches in the middle; the execution flow represents the connection between two basic blocks.
[0046] Taint analysis refers to analyzing whether the data introduced by the taint source in the program can be directly transmitted to the taint convergence point without harmless processing. If it can reach the point, it means that the program has security issues such as privacy data leakage or dangerous data operation; if it cannot, it means that the program is information flow secure. Harmless processing means that after the tainted data is processed by this module, the data itself no longer carries sensitive information or the operation on the data will no longer cause harm to the program. In other words, the taint mark can be removed after the data with taint marks passes through the harmless processing module. Taint analysis is often used to check code defects.
[0047] Figure 4 FIG. 1 is a schematic diagram of a stain analysis process provided by an embodiment of the present invention. Figure 4As shown in the figure, since taint analysis technology is a static analysis technology that analyzes whether the program taint sources (sources) can be propagated to the taint sinks (sinks) without harmless processing (sanitizers), taint analysis can be abstracted into a triple <taint sources, sinks, sanitizers>, which is specifically divided into three stages: identification of taint sources and taint sinks, harmless processing, and taint propagation analysis. The first is the identification of taint sources, adding taint marks to ordinary variables to form tainted variables; then taint propagation analysis is performed, and various dependencies are propagated between tainted variables after harmless processing and ordinary variables; finally, they converge to the taint sink. Among them, taint propagation analysis is divided into explicit flow analysis and implicit flow analysis. Explicit flow analysis is to analyze how taint sources propagate through numerical transmission dependencies between variables in the program; while implicit flow analysis refers to analyzing the control dependency propagation between data, that is, analyzing how taint sources propagate from conditional instructions to the statements they control. Traditional taint analysis only involves explicit flow analysis, and the taint sources and taint sinks are relatively single.
[0048] The multi-source and multi-sink static analysis algorithm based on taint analysis provided by the embodiment of the present invention needs to set multiple taint sources and multiple taint sinks, select corresponding data usage constraints for different data types, and choose to perform explicit flow analysis and / or implicit flow analysis.
[0049] Step S102, determining the taint source for the target contract function in the target contract file, wherein the taint source includes: program input parameters and contract call instructions.
[0050] In a preferred embodiment provided by the present invention, the program input parameters are directly marked as the taint source; all basic blocks in the control flow graph are traversed to find the contract call instructions in the program, and then the contract call instructions are marked as the taint source.
[0051] Table 1 shows a code for searching a contract call instruction provided by an embodiment of the present invention.
[0052] Table 1
[0053]
[0054] Referring to Table 1, the embodiment of the present invention inputs a contract call list and a basic block list, traverses all basic blocks, and for each basic block, traverses all bytecode instructions. If an instruction type is "kInstrInvoke" type, and the instruction name contains "dyn:call:executeContract", it is represented as a dynamic function call instruction, and the function name is "executeContract", which is a contract call instruction.
[0055] Step S103, based on the static analysis algorithm, adds the taint source to the analysis result before the execution of the starting block of the control flow graph to initialize the analysis result, traverses the program control flow graph, selects the corresponding data usage constraints, performs data value transfer dependency analysis and / or data control dependency analysis, continuously updates the analysis results after execution, and finally returns the final analysis result.
[0056] Static analysis algorithms based on taint analysis analyze how taint sources propagate in program variables through dependencies. In a Java environment, when the compiled Java bytecode is executed in the Java virtual machine JVM, the JVM creates a stack frame for each method. Each stack frame stores information such as the local variable table, operand stack, dynamic link, and return address. Therefore, local variables in the program are stored in the local variable table of the stack frame, and for global variables in the program, constants and static member variables modified with static are stored in the method area, instance variables are stored in the method area, and references to instance variables are stored in the local variables of the stack frame.
[0057] Therefore, in an embodiment of the present invention, for the static analysis, the expression form of the analysis results is predefined, including: for the design simulation of the execution process of the bytecode in the virtual machine, a stack frame structure is predefined to simulate the transfer process of the taint source along with the local variables of the program in the local variable table; for global variables, a heap object is predefined to represent the instance object in the global variable, and the reference to the marked instance object can also be passed in the local variable table.
[0058] In the embodiment of the present invention, since it is necessary to traverse the control flow graph and continuously update the analysis results of the basic blocks in the control flow graph, and record the control dependency information in the control flow graph to complete the static analysis of the smart contract, it is necessary to predefine basic operations related to the analysis results for the static analysis, including:
[0059] An operation of merging analysis results of the execution of multiple instructions is predefined, which is used to merge the analysis results after the execution of multiple instructions in the basic block in the control flow graph;
[0060] An operation of determining whether two analysis results have an inclusion relationship is predefined, and used to determine whether the analysis result before the execution of the basic block is included in the analysis result after the execution of the basic block according to the change of the analysis result before and after the execution of the basic block in the control flow graph;
[0061] An operation of merging analysis results of basic blocks is predefined, and is used to merge the analysis results before execution into the analysis results after execution according to the changes of the analysis results before and after execution of the basic blocks in the control flow graph;
[0062] Predefine a cloning operation of the analysis results to realize the merging of the analysis results;
[0063] An operation of printing analysis results is predefined to simulate the transfer process of program taint sources along with program variables in the program for the stack frame structure, and print the analysis results so as to intuitively view the taint propagation process.
[0064] In the embodiment of the present invention, the operation abstract class AnalysisResult of the analysis result is designed to define the basic operations related to the analysis result for traversing the control flow graph.
[0065] The embodiment of the present invention defines the TaintResult class and the member variables therein, and defines the expression form of the analysis result in the static analysis, which is used to implement the data dependency analysis process.
[0066] Table 2 shows the member variables of the TaintResult class and their meanings.
[0067] Table 2
[0068] Variable Name Variable Types meaning frame Frame <taintvalue> < / taintvalue> Stack Frame ret TaintValue Analysis results after being marked by the tainted source nLocals int The size of the local variable table nStack int Stack size printer InsnPrinter Bytecode instruction accessor interpreter TaintInterpreter Bytecode instruction interpreter
[0069] As shown in Table 2, the member variables frame and ret represent the form of the analysis results in the static analysis algorithm. The Frame class is provided by the Tree API of the ASM framework and defines a stack frame structure to simulate the process of transferring the analysis results marked by the taint source along with the local variables of the program in the local variable table. The TaintValue class defines the analysis result as a long integer. After converting the integer to a string, each character uses "1 / 0" to indicate whether it is contaminated by the taint source.
[0070] The embodiment of the present invention implements the operation functions of the analysis results by defining the TaintResult class, and also defines member variables for data value transfer dependency analysis and data control dependency analysis. Therefore, in the embodiment of the present invention, the analysis results are initialized, that is, the member variables of the data value transfer dependency analysis and the data control dependency analysis are initialized.
[0071] Table 3 shows an initialization code of an analysis result provided by an embodiment of the present invention.
[0072] Table 3
[0073]
[0074] Referring to Table 3, the embodiment of the present invention first obtains the starting block of the control flow graph, sets the size of the program local variable table and the program stack, and after determining the taint source of the contract call instruction "executeContract" and the program input parameter "arg", adds the taint source to the local variable table and builds a stack frame, so as to add the taint source to the analysis result before the execution of the starting block of the control flow graph, and completes the initialization of the analysis result.
[0075] Figure 5 FIG. 1 shows a flow chart of a multi-source multi-sink static analysis algorithm provided by an embodiment of the present invention. Figure 5 As shown, the embodiment of the present invention adopts a multi-source multi-sink algorithm based on taint analysis, and replaces the taint sink set with two different data dependency analysis algorithms, namely, value transfer dependency propagation analysis and control dependency propagation analysis, to analyze whether the taint source can be propagated to the final return result, or through what propagation method it is propagated to the final return result. Specifically, the first step is to determine the pollution source; the second step is to initialize the analysis result of the initial block of the control flow graph; the third step is to select the corresponding data usage constraint according to the data dependency type, and perform value transfer dependency propagation analysis and / or control dependency propagation analysis; the fourth step is to return the final analysis result.
[0076] Table 4 shows a data dependency type definition provided by an embodiment of the present invention.
[0077] Table 4 Data dependency type definition
[0078]
[0079] Referring to Table 4, in an embodiment of the present invention, data dependency types are first defined. Referring to Table 3, the data dependency types include: data value transfer dependency and data control dependency; wherein, the data value transfer dependency includes: inclusion dependency, assignment dependency, arithmetic dependency and function dependency between program variables and data sources, indicating the propagation of display information flow between data; if the program statement is Res=source+1, source can be directly transferred to Res through addition operation, so Res is arithmetically dependent on source, and the dependency expression is Control dependency indicates the implicit information flow between data. After the program passes the branch statement if (source> 0), it may jump to the branch Res = a or Res = b. The final return result is the implicit control dependency on the judgment result in the branch statement. Therefore, Res control depends on source, and the dependency expression is
[0080] The data value transfer dependency analysis is to analyze how the taint source is propagated with direct value transfer between variables in the program, that is, to analyze the inclusion dependency, assignment dependency, arithmetic dependency and function dependency between program variables and data sources. Data value transfer dependency analysis belongs to forward data flow analysis, and different bytecode instructions will generate different variable relationships. Its data flow equation is as follows:
[0081] Out n =InterpreterExec(In n )
[0082]
[0083] Among them, InterpreterExec is the instruction execution operation in the basic block, In n Represents the analysis result before the basic block is executed, Out n Represents the analysis result after the basic block is executed. The analysis result is continuously iterated and updated, and finally a final analysis result is returned.
[0084] Figure 6 A data value transfer dependency analysis flow chart provided by an embodiment of the present invention is shown.
[0085] Reference Figure 6 As shown, in an embodiment of the present invention, the data value transfer dependency analysis specifically includes: the first step, adding the starting block of the control flow graph to the analyzed working set to initialize the working set; the second step, traversing the control flow graph and taking out each node of the control flow graph; the third step, judging whether the node outgoing edge is empty, that is, judging whether the current basic block has a subsequent block; the fourth step, if there is a subsequent block, according to the type of the node, using the corresponding dependency analysis transfer function, the taint source is propagated according to the dependency type of the instructions contained in each basic block; the fifth step, adding the node to the working set; the sixth step, when the control flow graph traversal is completed, returning to the working set; the seventh step, ending the data value transfer dependency analysis. Among them, the value transfer process of the taint source between basic blocks is a traversal process of a directed acyclic graph, and the traversal of the graph is not only used in the data value transfer dependency analysis, but also can be used in other data flow analysis algorithms.
[0086] Table 5 shows a dependency calculation table provided by an embodiment of the present invention, which is used to calculate the final analysis result when multiple data usage constraints act on a certain data source.
[0087] Table 5 Dependency calculation table
[0088]
[0089]
[0090] As shown in Table 5, the dependency operator is combined from right to left, such as It is a binary dependency type, that is, when performing dependency analysis, arithmetic dependency analysis should be performed from right to left, and then control dependency analysis. For example, for the program statement b = a + 1, c = b + 1, then the relationship between b and a is The relationship between b and c is According to the dependency table, That is, in fact c=a+1, and c is arithmetically dependent on a.
[0091] The data control dependency is to analyze how the taint source is indirectly propagated between variables in the program, that is, to propagate through the branch statements of the program, analyze the control dependency relationship between program variables and data sources, and represent the implicit information flow propagation between data.
[0092] In the embodiment of the present invention, for any basic block B1 where a branch statement in the program is located, and all its direct subsequent blocks B2, B3, ..., Bi, for basic block Bj, if all subsequent blocks of B1 can reach Bj, then Bj has no control dependency relationship with the branch statement of B1, otherwise it does. Let the set of all branch blocks in the control flow graph be, and the direct subsequent blocks of each branch block are combined into. Specifically, the control dependency analysis can also be abstracted into the following data flow equation:
[0093] The data flow equation can be iterated using depth-first traversal. After the iteration is completed, to determine whether the branch block bi controls a basic block, just look at the In n Whether it does not contain all elements of {bij|j=0,1,…}, if not, then control, otherwise not control. That is, determine whether all direct subsequent blocks of the branch block bi can reach the basic block, if not, then the branch block controls the basic block, otherwise not control.
[0094] Figure 7 A data control dependency analysis flow chart provided by an embodiment of the present invention is shown.
[0095] Reference Figure 7As shown, in an embodiment of the present invention, the data control transfer analysis specifically includes: a first step, traversing all basic blocks in the control flow graph, and finding all branch blocks in the control flow graph, that is, all jumpable basic blocks of the current basic block; a second step, judging whether the branch block set is empty, otherwise entering the third step, if so, terminating the data control transfer analysis; a third step, traversing all branch blocks, and obtaining all subsequent blocks of each branch block; a fourth step, traversing all basic blocks, judging whether all subsequent blocks can reach the current basic block, if not, the current basic block depends on the current branch block, if so, terminating the traversal, and then terminating the data control transfer analysis.
[0096] In an embodiment of the present invention, it is determined whether each basic block in the control flow graph depends on each branch block, including: traversing all branch blocks in the control flow graph through a triple traversal loop, and obtaining all subsequent blocks of each branch block; traversing all basic blocks in the control flow graph, and traversing all subsequent blocks of each basic block at the same time, and based on the depth-first traversal algorithm DFS, determining whether all subsequent blocks can reach a certain basic block in the control flow graph, if not, the certain basic block depends on the current branch block.
[0097] In the embodiment of the present invention, the definition of the branch block is: if a basic block can jump to two or more basic blocks, then the basic block is a branch block. By printing the control flow graph, it can be found that, for example, some basic blocks containing a GOTO instruction (a bytecode instruction contained in the jump instruction class JumpInsnNode) have a number of subsequent blocks equal to 1, that is, the basic block does not meet the definition of a branch block. Therefore, when determining whether a basic block is a branch block in a program, it is necessary not only to determine whether the basic block contains a jump instruction JumpInsnNode, but also to determine whether the number of subsequent blocks of the basic block is greater than 1.
[0098] Figure 8 FIG. 1 shows a flowchart of searching all branch blocks in a control flow graph provided by an embodiment of the present invention. Figure 8 As shown, the first step is to traverse the control flow graph to obtain the number of subsequent blocks of each basic block; the second step is to determine the number of subsequent blocks. If the number of subsequent blocks is greater than 1, the basic block may be a branch block. If so, enter the third step, otherwise end the traversal and then end the search process; the third step is to traverse the bytecode instructions in all possible branch blocks; the fourth step is to determine whether the bytecode instruction is a jump instruction by determining whether it contains a "JumpInsnNode" instruction; the fifth step is to determine that the bytecode instruction is a jump instruction, then determine that the basic block is a branch block, otherwise return to the third step and enter the next bytecode instruction traversal.
[0099] Fig. 9A flow chart of a reachability analysis algorithm for a directed graph based on a depth-first traversal algorithm provided by an embodiment of the present invention is shown, which is used to determine whether a basic block is reachable to another basic block.
[0100] The reachability analysis of directed graphs is mainly based on the idea of mark-and-sweep. The main idea of the algorithm is: taking a basic block s of the graph as an example, obtain all subsequent blocks of s and determine whether they have been traversed. If not, recursively call DFS to traverse each reachable block, so as to find the basic blocks reachable from s.
[0101] Reference Fig. 9 As shown, the reachability analysis algorithm flow provided by the embodiment of the present invention specifically includes: the first step, inputting the starting block, creating a DFS class object, the DFS class is a Boolean set, maintaining a member variable marked, indicating whether the basic block in the control flow graph is marked; at the same time, the DFS class also defines a recursive depth-first traversal member method dfs, and the input parameters are the starting block and the program control flow graph; the second step, setting the starting block to be marked; the third step, based on the depth-first traversal method, traversing all subsequent blocks of the starting block; the fourth step, if a subsequent block is not marked, recursively calling the depth-first traversal algorithm and falling back to the second step; the fifth step, after the traversal is completed, finally inputting the end block, calling the member method isArrival() to return the marked Boolean value of the end block.
[0102] Table 6 shows an example of a control dependency analysis result provided by an embodiment of the present invention.
[0103] Table 6
[0104] Basic Blocks <![CDATA[Initial state (In n )]]> <![CDATA[End state (Out n )]]> Which branch block controls it? B1 {} {} none B2 {} {b30,b31} none B3 {b21} {b21,b30,b31} Controlled by b2 B4 {b30} {b30} Controlled by b3 B5 {b31} {b31} Controlled by b3 B6 {b20} {b20} Controlled by b2
[0105] Referring to Table 6, taking basic block B3 as an example, its initial state is {b21}, indicating that before B3 is executed, only the second subsequent block of basic block B2 (i.e., B3) can reach basic block B3; and the final state is {b21, b30, b31}, indicating that after B3 is executed, the second subsequent block B3 of basic block B2, the first subsequent block B4 of basic block B3, and the second subsequent block B5 of basic block B3 can all reach basic block B3. Since the initial state only contains {b21} and does not contain {b20, b21}, the control of basic block B3 depends on basic block B2.
[0106] Step S104: Generate and return an analysis report based on the final analysis result.
[0107] Table 7 shows the specific information of the content of the analysis report described in the embodiment of the present invention.
[0108] Table 7
[0109] Content Categories Content specific information Analysis parameters Data source, data usage constraint type Analyze the results Data value transmission dependency information, data control dependency information, whether it passes static analysis Contract Code Contract Code Program control flow graph Program control flow graph
[0110] As shown in Table 7, in a preferred embodiment provided by the present invention, the analysis report includes: analysis parameters, final analysis results, contract code and program control flow graph; wherein the analysis parameters include data source and data usage constraint type information, and the final analysis results include how the data depends on the taint source and whether the final analysis results pass the static analysis. That is, the final analysis results include any one of the data value transfer dependency information and the data control dependency information, and the final analysis results of whether the data passes the static analysis.
[0111] The analysis parameters and analysis results are the main part of the analysis report. The analysis report can be exported as a file, such as a document in PDF format.
[0112] In the traversal of the control flow graph, since the current basic block participating in the static analysis may be a subsequent block of a basic block together with other basic blocks, it is necessary to integrate the analysis results of all subsequent blocks into the final analysis results in one traversal.
[0113] Fig.10 A BFS traversal flow chart provided by an embodiment of the present invention is shown, which is used to integrate the analysis results of all subsequent blocks into the final analysis result in one traversal.
[0114] Reference Fig.10 As shown, in a preferred embodiment provided by the present invention, based on the breadth-first traversal algorithm (BFS) of the graph, a hierarchical traversal method is adopted to analyze and integrate the analysis results of all subsequent basic blocks of the current basic block in the control flow graph and put them into the set of the final analysis results.
[0115] Table 8 shows a BFS traversal code provided by an embodiment of the present invention.
[0116] Table 8
[0117]
[0118] Reference Fig.10As shown in Table 8, an embodiment of the present invention provides a BFS traversal process, and the specific process includes: the first step, creating a new empty analysis set list toAnalysis, and adding the starting block B0 to the analysis set; the second step, judging whether the current analysis set is empty or the traversal is finished, if the current analysis set is empty or the traversal is finished, returning the traversal result, and ending the traversal, if the current analysis set is not empty and the traversal is not finished, entering the third step; the third step, traversing the analysis set toAnalysis, obtaining the current block current in the analysis set, and moving the current block out of the unanalyzed list; the fourth step, obtaining the previous analysis result preResult and the subsequent analysis result sucResult of the current block, wherein the previous analysis result and the subsequent analysis result refer to the analysis results of the entire algorithm before and after the execution of a certain basic block respectively; the fifth step, if the previous analysis result is empty, or the previous analysis result does not contain the subsequent analysis result, then putting the subsequent analysis result into the execution result result of the current block<B0,sucResult> otherwise, return to step 3; step 6, obtain all subsequent basic blocks sucBlocks of the current block. If sucBlocks is empty or the traversal is completed, return to step 2. If sucBlocks is not empty and the traversal is not completed, enter step 7; step 7, traverse the subsequent basic blocks sucBlocks; step 8, determine whether a subsequent basic block sucBlocks is not in the unanalyzed list. If so, enter step 9. If not, return to step 6; step 9, add the subsequent basic block to the unanalyzed list and the analysis set, and return to step 6.
[0119] In the BFS traversal process provided by the embodiment of the present invention, an "unanalyzed list" is maintained. After a basic block is analyzed, it needs to be removed from the unanalyzed list. Then, it is determined whether the previous analysis result is empty, and whether the previous analysis result contains the subsequent analysis result. If the previous analysis result is empty or the previous analysis result set is smaller than the subsequent analysis result set, it means that the analysis result after the execution of the basic block should be used as the analysis result of the entire algorithm. After the current basic block is analyzed, all subsequent basic blocks of the current basic block should be analyzed according to the steps of the BFS traversal, and the subsequent basic blocks that have not been analyzed should be added to the analysis set list and the unanalyzed list, and then analyzed in sequence through traversal.
[0120] In addition, the embodiment of the present invention can also generate and return an analysis report based on the set of final analysis results.
[0121] It can be seen that the above method provided by the present invention predefines data dependency types and related calculations, defines data usage constraints based on specific data management policies and performs static analysis, selects corresponding data usage constraints in the static analysis, and then performs corresponding data dependency analysis methods, and finally returns an analysis report. The embodiment of the present invention defines data constraints and performs static analysis based on specific data management policies, and can automatically check the smart contract code with complex data usage constraint definitions, thereby completing the automated audit of smart contracts for data management, and ensuring the normal operation of smart contracts on the blockchain platform for data management without the need for data owners to personally audit each transaction.
[0122] Embodiment 2
[0123] Fig.11 This is a structural diagram of a smart contract code checking system for data control provided by the present invention. Fig.11 As shown, the system provided by the present invention can be applied to a blockchain platform for data management and control, and the specific system includes:
[0124] The algorithm preset module 201 is used to predefine data dependency types and related calculations of the data dependency types, and to design a multi-source and multi-sink static analysis algorithm based on taint analysis, so as to define data usage constraints and perform static analysis based on specific data management policies.
[0125] The taint source determination module 202 is used to determine the taint source for the target contract function in the target contract file, and the taint source includes: program input parameters and contract call instructions.
[0126] The static analysis module 203 is used to add the taint source to the analysis result before the execution of the starting block of the control flow graph based on the static analysis algorithm to initialize the analysis result, traverse the program control flow graph, select the corresponding data usage constraints, perform data value transfer dependency analysis and / or data control dependency analysis, continuously update the analysis results after execution, and finally return the final analysis result.
[0127] The analysis report export module 204 is used to generate and return an analysis report according to the final analysis result.
[0128] It can be seen that the above-mentioned system provided by the present invention predefines data dependency types and related calculations, defines data usage constraints and performs static analysis based on specific data management policies, selects corresponding data usage constraints in the static analysis, and then performs corresponding data dependency analysis methods, and finally returns an analysis report. The embodiment of the present invention defines data constraints and performs static analysis based on specific data management policies, and can automatically check the smart contract code with complex data usage constraint definitions, thereby completing the automated audit of smart contracts for data management, and ensuring the normal operation of smart contracts on the blockchain platform for data management without the need for data owners to personally audit each transaction.
[0129] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0130] It will be appreciated by those skilled in the art that the embodiments of the present invention may be provided as methods, devices, or computer program products. Therefore, the embodiments of the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the embodiments of the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0131] The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0132] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing terminal device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0133] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0134] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.
[0135] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.
[0136] The above is a detailed introduction to a smart contract code checking method for data control and a smart contract code checking system for data control provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A smart contract code checking method for data control, characterized in that: The method comprises: Predefine data dependency types and related calculations of the data dependency types, and design a multi-source and multi-sink static analysis algorithm based on taint analysis to define data usage constraints and perform static analysis based on specific data management policies; For the target contract function in the target contract file, determine the taint source, wherein the taint source includes: program input parameters and contract call instructions; Based on the static analysis algorithm, the taint source is added to the analysis result before the execution of the start block of the control flow graph to initialize the analysis result, and the corresponding data usage constraints are selected by traversing the program control flow graph, and data value transfer dependency analysis and data control dependency analysis are performed, and the analysis result after execution is continuously updated, and finally the final analysis result is returned; Generate and return an analysis report based on the final analysis result; The data control dependency analysis includes: Traversing all basic blocks in the control flow graph, finding all jumpable basic blocks in the control flow graph, that is, branch blocks; determining whether each basic block in the control flow graph depends on each branch block; Determining whether each basic block in the control flow graph depends on each branch block includes: Through a triple traversal loop, traverse all branch blocks in the control flow graph and obtain all subsequent blocks of each branch block; All basic blocks in the control flow graph are traversed, and all subsequent blocks of each basic block are traversed. Based on the depth-first traversal algorithm DFS, it is determined whether all subsequent blocks can reach a basic block in the control flow graph. If not, the basic block depends on the current branch block.
2. The method according to claim 1, characterized in that Predefine data dependency types and related calculations of the data dependency types, design a multi-source and multi-sink static analysis algorithm based on taint analysis, so as to define data usage constraints and perform static analysis based on specific data management policies, including: In order to meet the data usage control needs, the bytecode instructions after smart contract compilation are analyzed to define the data dependency types between variables, that is, the association types, including data value transfer dependency associations and data control dependency associations; at the same time, based on the data type and its related calculations, a method for describing data usage constraints is given.
3. The method according to claim 2, characterized in that The data value transfer dependency includes: inclusion dependency, assignment dependency, arithmetic dependency and function dependency between program variables and data sources, which represent explicit information flow propagation between data; the data control dependency includes control dependency between program variables and data sources, which represents implicit information flow propagation between data.
4. The method according to claim 1, characterized in that: For the target contract function in the target contract file, determine the source of the taint, including: The program input parameters are directly marked as the taint source; all basic blocks in the control flow graph are traversed to find the contract call instructions in the program, and then the contract call instructions are marked as the taint source.
5. The method according to claim 4, characterized in that Traverse all basic blocks in the control flow graph and find the contract call instructions in the program, including: Traverse all basic blocks. For each basic block, traverse all bytecode instructions. If an instruction type is "kInstrInvoke" and the instruction name contains "dyn:call:executeContract", it means it is a dynamic function call instruction, and the function name is "executeContract", which is a contract call instruction.
6. The method according to claim 1, characterized in that The data value transfer dependency analysis includes: Add the starting block of the control flow graph to the analyzed working set to initialize the working set; traverse the control flow graph, take out each node of the control flow graph, and determine whether the node outgoing edge is empty, that is, determine whether the current basic block has a subsequent block; if there is a subsequent block, use the corresponding dependency analysis transfer function according to the type of the node, the taint source is propagated according to the dependency type of the instructions contained in each basic block, and the node is added to the working set; when the control flow graph traversal is completed, return to the working set and end the data value transfer dependency analysis.
7. The method according to claim 1, characterized in that The analysis report includes: analysis parameters, final analysis results, contract code and program control flow graph; wherein the analysis parameters include data source and data usage constraint type information, and the final analysis results include how the data depends on the taint source and whether the final analysis results pass the static analysis.
8. A smart contract code checking system for data control, characterized in that: The system comprises: An algorithm preset module is used to predefine data dependency types and related calculations of the data dependency types, and to design a multi-source and multi-sink static analysis algorithm based on taint analysis, so as to define data usage constraints and perform static analysis based on specific data management policies; A taint source determination module is used to determine the taint source for the target contract function in the target contract file, wherein the taint source includes: program input parameters and contract call instructions; A static analysis module, for adding the taint source to the analysis result before the execution of the start block of the control flow graph based on the static analysis algorithm to initialize the analysis result, traverse the program control flow graph, select the corresponding data usage constraint, perform data value transfer dependency analysis and data control dependency analysis, continuously update the analysis result after execution, and finally return the final analysis result; An analysis report export module is used to generate and return an analysis report based on the final analysis result; The data control dependency analysis includes: Traversing all basic blocks in the control flow graph, finding all jumpable basic blocks in the control flow graph, that is, branch blocks; determining whether each basic block in the control flow graph depends on each branch block; Determining whether each basic block in the control flow graph depends on each branch block includes: Through a triple traversal loop, traverse all branch blocks in the control flow graph and obtain all subsequent blocks of each branch block; All basic blocks in the control flow graph are traversed, and all subsequent blocks of each basic block are traversed. Based on the depth-first traversal algorithm DFS, it is determined whether all subsequent blocks can reach a basic block in the control flow graph. If not, the basic block depends on the current branch block.
Citation Information
Patent Citations
An Android application privacy disclosure vulnerability detection method
CN109933988A