A method and device for generating a control flow graph of a smart contract
By generating jump records and function records, combined with disassembly tools and instruction basic blocks, the control flow graph of the Ethereum smart contract is constructed, which solves the problem that traditional solutions are not suitable for EVM bytecode and realizes intelligence on EVM bytecode. Accurate and complete control flow graph generation of contracts improves the accuracy of security detection.
Patent Information
- Application Number
- CN202111598801.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-24
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2041-12-24
AI Technical Summary
It is difficult to generate an accurate and complete control flow graph of Ethereum smart contracts with existing technology, especially in the form of EVM bytecode. The traditional control flow graph construction scheme is not suitable for EVM bytecode, resulting in difficulties in vulnerability detection and attack analysis. .
By generating jump records and function records in the process of translating smart contracts into EVM bytecode, using disassembly tools to convert them into EVM instruction sequences and dividing them into instruction basic blocks, a control flow graph is constructed based on these records and sequence information to ensure that no need Combined with contextual semantics, accurate control flow graphs can be generated.
It achieves complete and accurate control flow graph generation for EVM bytecode smart contracts, improves the accuracy of vulnerability detection and attack analysis, and is suitable for security detection of Ethereum smart contracts.
Smart Images

Figure CN114385185B
Abstract
Description
Technical Field
[0001] The present application relates to the field of network technology, and in particular to a method and device for generating a control flow graph of a smart contract. Background Art
[0002] In recent years, with the development of computer technology, more and more technologies are applied in the financial field. The traditional financial industry is gradually transforming to financial technology (Fintech). However, due to the security and real-time requirements of the financial industry, higher requirements are also placed on technology. For example, in order to improve the security of Ethereum and prevent the introduction of vulnerabilities in the development process of Ethereum smart contracts, attackers use vulnerabilities hidden in smart contracts to launch attacks, causing the harm of users' digital assets being stolen, smart contracts are generally tested for corresponding vulnerabilities or attacks.
[0003] In the prior art, the control flow graph of the smart contract is generally drawn to detect vulnerabilities or attacks on the smart contract, and the detection method includes static analysis and dynamic analysis. Among them, static analysis is based on the control flow graph, traverses the data flow of all possible paths in the control flow graph, and determines whether there is an execution path that triggers the vulnerability in the control flow. Dynamic analysis analyzes the safe execution path in the control flow graph before the program runs, and after the smart contract is deployed, it determines whether the smart contract triggers the established execution path during the execution process to determine whether an unknown vulnerability is triggered. Although this method can accurately detect vulnerabilities and attacks in smart contracts, with the development of blockchain technology, the form of smart contracts has gradually developed from traditional bytecode X86 to EVM bytecode (Ethereum Virtual Machine Bytecode). When the compiler with the target language x86 compiles to the jump statement, it can directly analyze the parameter label to capture its jump address. When the compiler with the target language EVM bytecode compiles the jump statement, because the jump address is stored in the running stack, it is necessary to capture its jump address according to the context of the current compilation state. Secondly, unlike the x86 bytecode compiler which uses a special call instruction to process function call statements, the compiler whose target language is EVM bytecode directly uses the jump instruction JUMP to implement the function call function when compiling function call statements. Therefore, it is necessary to combine the context semantics to distinguish the functional scenarios of the JUMP instruction. Therefore, the traditional control flow graph construction solution is not suitable for the EVM bytecode of Ethereum smart contracts.
[0004] Therefore, there is an urgent need for a control flow graph generation method and device for smart contracts, which are suitable for accurately and completely generating a control flow graph for smart contracts in the form of EVM bytecode. Summary of the invention
[0005] The embodiments of the present invention provide a method and device for generating a control flow graph of a smart contract, which are suitable for accurately and completely generating a control flow graph of a smart contract in the form of EVM bytecode.
[0006] In a first aspect, an embodiment of the present invention provides a method for generating a control flow graph of a smart contract, the method comprising:
[0007] In the process of translating the smart contract into Ethereum virtual machine bytecode, a jump record is generated for the jump instruction in the smart contract, and a function record is generated for the function instruction in the smart contract; the jump record is used to represent the jump address of the jump instruction; the function record is used to represent the boundary address of the function instruction;
[0008] Convert the Ethereum virtual machine bytecode into an Ethereum virtual machine instruction sequence by a disassembly tool, and divide the Ethereum virtual machine instruction sequence into a plurality of instruction basic blocks with sequence information;
[0009] Each instruction basic block is used as a node in a control flow graph, and directed edges between nodes are generated according to the jump record, the function record and the sequence information to obtain the control flow graph of the smart contract.
[0010] In the above method, the compiler translates the smart contract into Ethereum virtual machine bytecode and generates jump records and function records corresponding to the smart contract. In this way, the compiler records the jump address of the jump instruction and the boundary address of the function instruction in the smart contract in the form of Ethereum virtual machine bytecode during the compilation process, so that the subsequent generation of the control flow graph can obtain the jump and boundary information of the function in the smart contract without combining the context semantics. The compiler uses the instruction basic block as a node, and generates directed edges between each node according to the jump instructions or function instructions in each instruction basic block, the sequence information of multiple instruction basic blocks, and the jump records and function records to obtain the control flow graph of the smart contract. In this way, the compiler can generate a control flow graph based on the jump instructions and function instructions corresponding to the instruction basic block, the corresponding relationship in the jump records and function records, and the sequence information between the instruction basic blocks, to ensure the integrity and accuracy of the control flow graph.
[0011] Optionally, generating a jump record for the jump instruction in the smart contract includes:
[0012] For the control instruction in the smart contract, a jump address corresponding to the control instruction is generated; for each JUMP / JUMPI jump instruction in the control instruction, jump information corresponding to the jump instruction is generated, and the jump information includes the address of the jump instruction and the jump address corresponding to the jump instruction.
[0013] In the above method, each JUMP / JUMPI jump instruction in the control instruction is used to represent the jump information of each instruction executed in the smart contract. In this way, the jump record records the address of each jump instruction and the corresponding jump address, and the corresponding position of the jump semantics of the instruction in the smart contract can be determined without obtaining the context information of the smart contract, and a control flow graph that accurately records the jump information of the smart contract is generated.
[0014] Optionally, for a control instruction in the smart contract, a jump address corresponding to the control instruction is generated; for each JUMP / JUMPI jump instruction in the control instruction, jump information corresponding to the jump instruction is generated, including:
[0015] For the loop instruction in the control instruction, a jump start address and a jump end address of the loop instruction are generated; when translating to a JUMPI jump instruction, the address where the JUMPI jump instruction is located and the jump end address are used as the jump information of the JUMPI jump instruction; when transferring to the last JUMP jump instruction, the address where the JUMP jump instruction is located and the jump start address are used as the jump information of the JUMPI jump instruction;
[0016] For the conditional statement / ternary expression in the control instruction, a FALSE jump address and an END jump address are generated; when translated to a JUMPI jump instruction corresponding to the conditional judgment statement, the address where the JUMPI jump instruction is located and the FALSE jump address are used as the jump information of the JUMPI jump instruction; when translated to a JUMP jump instruction after the FALSE content, the address where the JUMP jump instruction is located and the END jump address are used as the jump information of the JUMP jump instruction.
[0017] In the above method, for the loop instruction in the control instruction, when translating to the JUMPI jump instruction, the address of the JUMPI jump instruction and the jump end address are used as the jump information of the JUMPI jump instruction; when transferring to the last JUMP jump instruction, the address of the JUMP jump instruction and the jump start address are used as the jump information of the JUMPI jump instruction. In this way, for the loop instruction in the control instruction, the address and the jump end address of the jump instruction of the start loop of the loop statement are obtained, and the position of entering the loop is recorded; the address and the jump start address of the jump instruction of the end loop of the loop statement are obtained, and the position of restarting the next loop from this loop is recorded. The execution semantics of the loop statement corresponding to the loop instruction in the control instruction are determined based on these two sets of binary instruction addresses, so that the complete execution path of the loop instruction is recorded in the control flow graph generated subsequently.
[0018] For the conditional statement / ternary expression in the control instruction, when translating to the JUMPI jump instruction corresponding to the conditional judgment statement, the address of the JUMPI jump instruction and the FALSE jump address are used as the jump information of the JUMPI jump instruction; when translating to the JUMP jump instruction after the FALSE content, the address of the JUMP jump instruction and the END jump address are used as the jump information of the JUMP jump instruction. In this way, for the conditional statement / ternary expression in the control instruction, the address of the jump instruction where the conditional statement / ternary expression starts to execute and the FALSE jump address are obtained, and the position of entering the conditional statement / ternary expression is recorded. The address of the JUMP jump instruction after the conditional statement / ternary expression is translated to the FALSE content and the END jump address are obtained, and the position of "coming out" from the conditional statement / ternary expression is recorded. The execution semantics of the conditional statement in the conditional statement / ternary expression is determined based on these two sets of binary instruction addresses, so that the complete execution path of the conditional statement / ternary expression is recorded in the control flow graph generated subsequently. The above realizes the execution semantics of each instruction in the control statement and improves the completeness and accuracy of the control flow graph.
[0019] Optionally, generating a function record for the function instruction in the smart contract includes:
[0020] When translating to the function selector in the smart contract, the function entry address of the public function in the function selector is recorded in the function entry information in the function record, where the function entry address of the public function is the address of the first JUMPDEST instruction after parameter loading and translation;
[0021] When translating into the function body in the smart contract, the function entry address of the private function in the function body is recorded in the function entry information; the function entry address of the private function is the address of the first parameter of the calling address;
[0022] When translating into a function call statement in the function body, the call address and return address of the function call statement are recorded in the call information in the function record; the call address is the address of the JUMP instruction corresponding to the function call statement; the return address is the address of the JUMPDEST instruction corresponding to the function call statement.
[0023] In the above method, the compiler generates function entry information according to public functions and private functions during the translation process, generates function records according to the jump instruction address (call address-address of JUMP instruction) and return address (address of instruction corresponding to call completion and return-address of JUMPDEST instruction) of the function call entry of the function call statement, and generates jump records according to the address and jump address of the jump instruction of the control statement. In this way, the complete execution process of a function is recorded, that is, the function semantic conversion of function entry, jump, and call, and then the generated control flow graph completely and accurately records the semantic conversion of the entry, jump, and call of each function in the smart contract, which is convenient for the subsequent identification of smart contract vulnerabilities and attacked function objects according to the semantic conversion information of the function in the control flow graph, and improves the accuracy of detection and positioning.
[0024] Optionally, the Ethereum virtual machine instruction sequence is divided into a plurality of ordered instruction basic blocks, including:
[0025] Set the block entry address of the instruction basic block to e;
[0026] Get instructions from the Ethereum virtual machine instruction sequence in sequence. If the i-th instruction is a JUMPDEST instruction, determine the address s of the i-1-th instruction as the block exit address of the instruction basic block, take each instruction from the block entry address e to the block exit address s as an instruction basic block, and update the block entry address e of the instruction basic block to the address of the i-th instruction;
[0027] If the i-th instruction is a JUMP / JUMP1 jump instruction, a REVERT instruction, or a SELFDESTRUCT instruction, then the address s of the i-th instruction is determined as the block exit address of the instruction basic block, and each instruction from the block entry address e to the block exit address s is taken as an instruction basic block and the block entry address e of the instruction basic block is updated to the address of the i+1-th instruction.
[0028] In the above method, a complete execution instruction sequence is accurately determined based on the start jump instruction (JUMP / JUMP1 jump instruction, REVERT instruction, SELFDESTRUCT instruction) and the jump completion instruction (JUMPDEST instruction), and then the execution sequence-instruction basic block corresponding to each non-jump execution action in the Ethereum virtual machine instruction sequence is obtained to obtain the node of the control flow graph.
[0029] Optionally, generating directed edges between nodes according to the jump record and the function record includes:
[0030] According to the function entry information in the function record, obtain the first node corresponding to any function entry address;
[0031] The second node pointed to by the first node is determined by whether the block exit address of the first node corresponds to a jump address in the jump record or the calling information of the function record corresponds to a return address; the third node pointed to by the second node is continued to be determined until all nodes corresponding to the function entry addresses in the function entry information are traversed.
[0032] In the above method, according to the function entry information in the function record, the jump and call information in the jump record and the sequence information of multiple instruction basic blocks, the instruction basic block corresponding to the function start, the function jump between the instruction basic blocks, the call relationship-the execution order between the nodes of the smart contract are obtained to obtain the control flow graph.
[0033] Optionally, determining the second node pointed to by the first node by determining whether the block exit address of the first node corresponds to a jump address in the jump record or the call information of the function record corresponds to a return address includes:
[0034] If the block exit address of the first node corresponds to a jump address in the jump record, a directed edge is generated from the first node to the second node corresponding to the jump address; if the block exit address in the instruction basic block of the first node corresponds to a return address in the call information of the function record, a directed edge is generated from the first node to the second node corresponding to the return address;
[0035] Otherwise, generate the first node to point to a second node that is located after the first node according to the sequence information.
[0036] In the above method, the compiler determines the first node corresponding to the function entry address according to the function entry address in the function record. If the exit instruction in the instruction basic block of the first node is a jump instruction, and the address of the jump instruction and the corresponding jump address are determined from the jump record, then the second node containing the corresponding jump address is found according to the corresponding jump address to obtain a directed edge from the first node to the second node. At this time, the instruction basic block of the second node contains the instruction corresponding to the target address. If the jump record does not have the address of the jump instruction and the corresponding jump address, the address (call address) and the corresponding return address of the jump instruction are determined from the function record, thereby determining the second node corresponding to the return address, and obtaining a directed edge from the first node to the second node. At this time, the instruction basic block of the second node contains the instruction corresponding to the return address; if the block exit instruction in the instruction basic block of the first node is a non-jump instruction, then the directed edge from the first node to the second node is obtained according to the sequence information. At this time, the second node is the node corresponding to the instruction basic block after the instruction basic block of the first node.
[0037] In a second aspect, an embodiment of the present invention provides a control flow graph generation device for a smart contract, the device comprising:
[0038] A translation and recording module, used to generate a jump record for a jump instruction in the smart contract and a function record for a function instruction in the smart contract during the translation of the smart contract into Ethereum virtual machine bytecode; the jump record is used to represent the jump address of the jump instruction; the function record is used to represent the boundary address of the function instruction;
[0039] A construction module is used to convert the Ethereum virtual machine bytecode into an Ethereum virtual machine instruction sequence through a disassembly tool, and divide the Ethereum virtual machine instruction sequence into a plurality of instruction basic blocks with sequence information;
[0040] The construction module is also used to use each instruction basic block as a node in the control flow graph, generate directed edges between the nodes according to the jump record, the function record and the sequence information, and obtain the control flow graph of the smart contract.
[0041] In a third aspect, an embodiment of the present application further provides a computing device, comprising: a memory for storing programs; a processor for calling the programs stored in the memory, and executing the methods described in the various possible designs of the first aspect according to the obtained programs.
[0042] In a fourth aspect, an embodiment of the present application further provides a computer-readable non-volatile storage medium, comprising a computer-readable program. When a computer reads and executes the computer-readable program, the computer executes the method described in the various possible designs of the first aspect.
[0043] These implementations or other implementations of the present application will be more concise and understandable in the description of the following embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0045] Figure 1 A schematic diagram of the architecture of a control flow graph generation system for a smart contract provided by an embodiment of the present invention;
[0046] Figure 2 A schematic diagram of a compiler generating a control flow graph provided by an embodiment of the present invention;
[0047] Figure 3 A flow chart of a method for generating a control flow graph of a smart contract provided by an embodiment of the present invention;
[0048] Figure 4 A schematic diagram of a loop statement structure and a jump instruction provided by an embodiment of the present invention;
[0049] Figure 5 A schematic diagram of another loop statement structure and jump instruction provided by an embodiment of the present invention;
[0050] Figure 6 A conditional statement / ternary expression structure and a jump instruction schematic diagram provided for an embodiment of the present invention;
[0051] Figure 7 A control flow graph of a smart contract provided by an embodiment of the present invention;
[0052] Figure 8 A flow chart of a method for generating a control flow graph of a smart contract provided by an embodiment of the present invention;
[0053] Fig. 9 A schematic diagram of a control flow graph generating device for a smart contract provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0054] In order to make the purpose, technical scheme and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all 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.
[0055] Figure 1 A system architecture of a control flow graph generation system for a smart contract provided by an embodiment of the present invention, wherein a translation module translates and obtains Ethereum virtual machine bytecode (EVM bytecode) according to a smart contract, during which a jump address capture module captures the address and jump address of a jump instruction in a control statement to obtain a jump record, and a function boundary capture module captures the function entry address of a public function in a function selector in a smart contract, the function entry address of a private function in a function body, and the call address and return address of a function call statement in a function body to obtain a function record. After the translation module completes the translation of the smart contract, a basic block division module converts the EVM bytecode into an instruction sequence through a disassembly tool, and divides the converted instruction sequence into a plurality of instruction basic blocks. A control flow graph construction module uses instruction basic blocks as nodes, and constructs directed edges between nodes according to jump records, function records, and the plurality of instruction basic blocks to obtain a control flow graph.
[0056] Based on the above system architecture, an embodiment of the present invention further provides a schematic diagram of a compiler generating a control flow graph, such as Figure 2 As shown, the Solidity compiler can realize the translation function, the jump address capture function, and the function boundary capture function. The translation function translates and obtains the Ethereum virtual machine bytecode (EVM bytecode) according to the smart contract. In this process, the jump address capture function captures the address and jump address of the jump instruction in the control statement to obtain the jump record, and the function boundary capture function captures the function entry address of the public function in the function selector in the smart contract, the function entry address of the private function in the function body, and the call address and return address of the function call statement in the function body to obtain the function record. After the translation function completes the translation of the smart contract, the basic block division module converts the EVM bytecode into an instruction sequence through a disassembly tool, and divides the converted instruction sequence into multiple instruction basic blocks. The Solidity compiler uses the instruction basic block as a node, and constructs the directed edges between the nodes according to the jump record, the function record and the multiple instruction basic blocks to obtain a control flow graph.
[0057] Based on this, the present application embodiment provides a process of a control flow graph generation method for a smart contract, such as Figure 3 As shown, including:
[0058] Step 301: In the process of translating a smart contract into Ethereum virtual machine bytecode, a jump record is generated for a jump instruction in the smart contract, and a function record is generated for a function instruction in the smart contract; the jump record is used to represent a jump address of the jump instruction; and the function record is used to represent a boundary address of the function instruction;
[0059] Step 302: Convert the Ethereum virtual machine bytecode into an Ethereum virtual machine instruction sequence by using a disassembly tool, and divide the Ethereum virtual machine instruction sequence into a plurality of instruction basic blocks with sequence information;
[0060] Step 303: Take each instruction basic block as a node in the control flow graph, generate directed edges between the nodes according to the jump record, the function record and the sequence information, and obtain the control flow graph of the smart contract.
[0061] In the above method, the compiler translates the smart contract into Ethereum virtual machine bytecode and generates jump records and function records corresponding to the smart contract. In this way, the compiler records the jump address of the jump instruction and the boundary address of the function instruction in the smart contract in the form of Ethereum virtual machine bytecode during the compilation process, so that the subsequent generation of the control flow graph can obtain the jump and boundary information of the function in the smart contract without combining the context semantics. The compiler uses the instruction basic block as a node, and generates directed edges between each node according to the jump instructions or function instructions in each instruction basic block, the sequence information of multiple instruction basic blocks, and the jump records and function records to obtain the control flow graph of the smart contract. In this way, the compiler can generate a control flow graph based on the jump instructions and function instructions corresponding to the instruction basic block, the corresponding relationship in the jump records and function records, and the sequence information between the instruction basic blocks, to ensure the integrity and accuracy of the control flow graph.
[0062] The embodiment of the present application provides a jump record generation method, which generates a jump record for the jump instruction in the smart contract, including:
[0063] For the control instructions in the smart contract, a jump address corresponding to the control instruction is generated; for each JUMP / JUMPI jump instruction in the control instruction, jump information corresponding to the jump instruction is generated, and the jump information includes the address where the jump instruction is located and the jump address corresponding to the jump instruction. In one example, according to the Solidity syntax rules, the statements that generate the JUMP / JUMPI instructions are mainly control statements, including three categories: loop statements, conditional statements, and ternary expressions. For control statements, the address and jump address of the JUMP / JUMPI corresponding to the control statement can be captured during the translation process to obtain the jump information of the control statement. In this way, the high-level semantics corresponding to each statement in the translation function corresponding to the control statement is implemented, and the jump information of the address and jump address corresponding to the JUMP / JUMPI instruction is recorded. The translation method here may include the Solidity compiler traversing the syntax tree of the smart contract source code to generate EVM bytecode. In one example, during the translation process of the compiler, a global variable jumpTarget (jump target) array is set to store a binary array (jumpAddr (address where JUMP / JUMPI is located), target (jump address)). In other words, the jump information can be recorded in the form of binary data, that is, the jump record contains at least one binary array.
[0064] The embodiment of the present application provides a jump information generation method, for the control instruction in the smart contract, generating a jump address corresponding to the control instruction; for each JUMP / JUMPI jump instruction in the control instruction, generating jump information corresponding to the jump instruction, including:
[0065] For the loop instruction in the control instruction, a jump start address and a jump end address of the loop instruction are generated; when translating to a JUMPI jump instruction, the address where the JUMPI jump instruction is located and the jump end address are used as the jump information of the JUMPI jump instruction; when transferring to the last JUMP jump instruction, the address where the JUMP jump instruction is located and the jump start address are used as the jump information of the JUMPI jump instruction;
[0066] For the conditional statement / ternary expression in the control instruction, a FALSE jump address and an END jump address are generated; when translated to a JUMPI jump instruction corresponding to the conditional judgment statement, the address where the JUMPI jump instruction is located and the FALSE jump address are used as the jump information of the JUMPI jump instruction; when translated to a JUMP jump instruction after the FALSE content, the address where the JUMP jump instruction is located and the END jump address are used as the jump information of the JUMP jump instruction.
[0067] In one example, the loop statement of the loop instruction in the control instruction includes a While statement / For statement. The embodiment of the present application provides a loop statement structure and a jump instruction schematic diagram, such as Figure 4 As shown, including:
[0068] In the translation function of the While / For statement, the Solidity compiler initializes two jump addresses loopStart (jump start address) and loopEnd (jump end address), places JUMPDEST (corresponding to address 0x12) at the loopStart address, translates the conditional judgment statement and JUMPI (corresponding to address 0x25) in the While statement, and then translates the loop body part in the While statement (corresponding to addresses 0x34 to 0x4f), and finally inserts JUMPDEST of loopEnd (corresponding to address 0x50). Accordingly, in the process of translating and generating jump records, when translating to the JUMPI instruction (corresponding to address 0x25), its jump address is regarded as loopEnd-JUMPDEST (corresponding to address 0x50), and (0x25, 0x50) is recorded in the jumpTarget array. When the code is finally translated to the JUMP instruction at the end of the While / For statement (corresponding to address 0x4f), the jump address of the JUMP instruction is regarded as loopStart-JUMPDEST (corresponding to address 0x12), and (0x4f, 0x12) is recorded in the jumpTarget array.
[0069] When translating to the JUMP instruction corresponding to the break statement in the While / For loop body (corresponding to address 0x34), the address of the JUMP instruction and the corresponding jump address (the address loopEnd initialized by the current loop) are recorded in the jumpTarget array, that is, (0x34, 0x50).
[0070] When translating to the JUMP instruction corresponding to the continue statement in the While / For loop body (corresponding to address 0x3a), the address of the JUMP instruction and the corresponding jump address (the address loopStart initialized by the current loop) are recorded in the jumpTarget array, that is, (0x3a, 0x12).
[0071] In one example, the loop statement of the loop instruction in the control instruction includes a Do-While statement. The embodiment of the present application provides another loop statement structure and a jump instruction schematic diagram, such as Figure 5 As shown, including:
[0072] In the translation function of the original Do-While statement, the Solidity compiler will initialize two jump addresses loopStart (jump start address) and loopEnd (jump end address). After placing JUMPDEST (corresponding to address 0xa) at the loopStart address, the contents of the loop body are translated. Finally, the conditional statement is translated and after placing the JUMPI instruction (corresponding to address 0x51), the JUMPDEST instruction (corresponding to address 0x52) is placed at the loopEnd address. Accordingly, when translating to the JUMPI instruction (corresponding to address 0x51), the jump address of the JUMPI instruction is recorded as loopStart-JUMPDEST (corresponding to address 0xa), and the address of the JUMPI instruction and the corresponding jump address (0x51, 0xa) are recorded in the jumpTarget array.
[0073] When translating to the JUMP instruction corresponding to the break statement in the Do-While loop body (corresponding address 0x32), the address of the JUMP instruction and the corresponding jump address (the address loopEnd-JUMPDEST initialized by the current loop) are recorded in the jumpTarget array, that is, (0x32, 0x52).
[0074] When translating to the JUMP instruction corresponding to the continue statement in the Do-While loop body (corresponding to address 0x3c), the address of the JUMP instruction and the corresponding jump address (the address loopStart initialized by the current loop) are recorded in the jumpTarget array, that is, (0x3c, 0xa).
[0075] In one example, the conditional statement / ternary expression in the control instruction includes an If-Else statement / Triple statement. The embodiment of the present application provides a conditional statement / ternary expression structure and a jump instruction schematic diagram, such as Figure 6 As shown in the figure, in the translation function of the If-Else statement and the Triple statement of the ternary expression, the translation steps of the If-Else statement and the Triple statement of the ternary expression are basically the same. Here, taking the If-Else statement as an example, in the initial translation function of the If-Else statement, the Solidity compiler will initialize two jump addresses If-False (FALSE jump address) and If-End (END jump address), and place JUMPI (corresponding to address 0xa9) in the conditional judgment statement.
[0076] Then the true content of the if statement is translated and ends with a JUMP instruction (corresponding to address 0xbc). Finally, the false content of the if statement is translated after placing the if-True JUMPDEST instruction and ends with the If-False JUMPDEST instruction (corresponding to address 0xbd). When translating to the JUMPI instruction (corresponding to address 0xa9) of the conditional judgment statement, If-False-JUMPDEST (corresponding to address 0xbd) is regarded as the jump address of the JUMPI instruction, and a binary array (0xa9, 0xbd) is obtained.
[0077] When translating to the JUMP instruction after the false content (corresponding to address 0xbc), its jump address is regarded as If-End-JUMPDEST (corresponding to address 0xc2), and a binary array (0xbc, 0xc2) is obtained, and both binary arrays are recorded in the jumpTarget array.
[0078] The If statement is a shortened version of the If-Else statement. In the translation function of the If statement, the Solidity compiler will complete the translation of the content of the conditional statement after initializing the jump address If-False. Finally, after translating the JUMPI instruction corresponding to the conditional statement (corresponding to address 0xa9), translate the content in the if statement structure, and place the JUMPDEST instruction with the address of If-False at the end (corresponding to address 0xbd). When the compiler translates to the JUMPI instruction, If-False is regarded as its jump address, and a binary array (0xa9, 0xbd) is obtained, which is recorded in the jumpTarget array.
[0079] The embodiment of the present application provides a method for generating a function record, which generates a function record for a function instruction in the smart contract, including:
[0080] When translating to the function selector in the smart contract, the function entry address of the public function in the function selector is recorded in the function entry information in the function record, where the function entry address of the public function is the address of the first JUMPDEST instruction after parameter loading and translation;
[0081] When translating into the function body in the smart contract, the function entry address of the private function in the function body is recorded in the function entry information; the function entry address of the private function is the address of the first parameter of the calling address;
[0082] When translating into the function call statement in the function body, the call address and return address of the function call statement are recorded in the call information in the function record; the call address is the address of the JUMP instruction corresponding to the function call statement; the return address is the address of the JUMPDEST instruction corresponding to the function call statement. In one example, the smart contract source code is:
[0083]
[0084] The Solidity compiler translates the above smart contract source code into Ethereum virtual machine bytecode. The Ethereum virtual machine bytecode is as follows (a string of hexadecimal strings - EVM bytecode):
[0085] 0x608060405260043610603f576000357c0100000000000000000000000000000000000000000000000 0000000000900463ffffffff16806374135154146044575b600080fd5b348015604f57600080fd5b506 0566058565b005b60008090505b60058110156092576003811015607a576075816095565b506086565 b6084600382026095565b505b8080600101915050605e565b50565b6000816000540190509190505600
[0086] The Solidity compiler converts the above Ethereum virtual machine bytecode into an instruction sequence through a disassembly tool, and further divides the instruction sequence into 17 instruction basic blocks. The instruction basic blocks are as follows:
[0087]
[0088] In this example, the instruction address corresponding to the function selector in the smart contract is 0x0 to 0x57, and the function entry address of the public function in the function selector is 0x58 marked in PUSHI corresponding to the address 0x53. It can be seen that the function entry address of the public function is the address of the first JUMPDEST instruction after parameter loading and translation (corresponding to address 0x58 in the above basic block).
[0089] That is, for the public function, the function entry address of the public function is recorded when the Solidity compiler translates the function selector. After processing the translation of parameter loading, the function entry of the public function uses the address 0x58 of the first JUMPDEST instruction after the Solidity compiler completes the parameter loading translation as the function entry of the public function, and adds 0x58 to the entryList array.
[0090] The instruction address corresponding to the function body in the smart contract is 0x58 to 0xa2, and the function entry address of the private function in the function body is 0x95 (the instruction address 0x95 to 0xa2 corresponds to the private function func2 in the source code). It can be seen that the function entry address of the private function is the address of the first parameter of the calling address (corresponding to the address 0x95 in the above basic block).
[0091] That is, for private functions, Solidity compiler will translate only when the function is called. In the above analysis of "call address-return address", the first parameter address (corresponding to address 0x95) of the JUMP instruction (0x74) of the call address is regarded as the function entry address 0x95 of the private function. If 0x95 has not been stored in the entryList array, the private function entry address 0x95 is added to the entryList. At this time, entryList = [0x58, 0x95].
[0092] The function call statements in the function body of the smart contract are func1 calling func2 and returning to func1 from func2. The call address corresponding to func1 calling func2 is the JUMP instruction (corresponding to address 0x74), and the return address is the JUMPDEST instruction (corresponding to address 0x75) corresponding to the function call statement. The call address corresponding to func2 returning to func1 is the JUMP instruction (corresponding to address 0x83), and the return address is the JUMPDEST instruction (corresponding to address 0x84) corresponding to the function call statement.
[0093] That is, when compiling the function call statement, the return address RET will be initialized first, and then the RET address will be saved in the operand stack. Then the JUMP instruction (corresponding to address 0x74 or 0x83) will be used to jump into the function entry of the called function (func2). Finally, after the call is completed, the JUMPDEST instruction (corresponding to address 0x75 or 0x84) of the RET address will be added. In the translation process, the address record where the JUMP instruction is located is regarded as the call address call, and the ret address is regarded as the return address. Finally, the (call, ret) tuple is recorded in the cList array. At this time, cList = [(0x74, 0x75), (0x83, 0x84)].
[0094] According to the jump address acquisition method of the control statement mentioned above, the jump address record of the control statement in this example can be obtained, jumpTargets = [(0x66, 0x92), (0x6e, 0x7a), (0x79, 0x86), (0x94, 0x5e)].
[0095] Here, the embodiment of the present application provides a method for obtaining jumpTargets, cList and entryList during the compilation process of the above-mentioned example smart contract, including:
[0096] 1. When the compiler translates the function selector in the smart contract, it records the entry of the public function func1() as 0x58 and adds 0x58 to the entryList array.
[0097] 2. After the compiler translates the jump statement JUMPI corresponding to i<5 in the for loop statement of func1, it adds the address 0x66 of the JUMPI instruction and the address 0x92 of its jump target JUMPDEST instruction into a tuple (0x66, 0x92) and adds them to the jumpTarget array.
[0098] 3. When the compiler translates the jump statement JUMPI corresponding to i<3 in the if conditional judgment statement of func1, the address 0x6e of the JUMPI instruction and the address 0x7a of its jump target JUMPDEST instruction are combined into a tuple (0x6e, 0x7a) and added to the jumpTarget array.
[0099] 4. When the compiler translates the function call statement "func2(i)" of func1, it adds the corresponding JUMP instruction address 0x74 and the return jump target JUMPDEST instruction address 0x75 after the call to form a tuple (0x74, 0x75) and adds it to the cList array. Secondly, the address 0x95 of the JUMP instruction jump target JUMPDEST is regarded as the entry of the function func2, and 0x95 is added to the entryList.
[0100] 5. When the compiler completes the if statement block of func1, it adds the corresponding JUMP instruction address 0x79 and the jump target instruction JUMPDEST address 0x86 into a tuple (0x79, 0x86) and adds them to the jumpTarget array.
[0101] 6. When the compiler translates the function call statement "func2(i*3)" of func1, it adds the corresponding JUMP instruction address 0x83 and the address 0x84 of the return jump target JUMPDEST instruction after the end of the call into a tuple (0x83, 0x84) and adds them to the cList array.
[0102] 7. When the compiler translates the for loop increment statement "i++" of func1, it adds the corresponding JUMP instruction address 0x94 and the jump target instruction JUMPDEST address 0x5e into a tuple (0x94, 0x5e) and adds them to the jumpTarget array.
[0103] The embodiment of the present application provides a method for dividing an instruction basic block, which divides the Ethereum virtual machine instruction sequence into a plurality of ordered instruction basic blocks, including:
[0104] Set the block entry address of the instruction basic block to e;
[0105] Get instructions from the Ethereum virtual machine instruction sequence in sequence. If the i-th instruction is a JUMPDEST instruction, determine the address s of the i-1-th instruction as the block exit address of the instruction basic block, take each instruction from the block entry address e to the block exit address s as an instruction basic block, and update the block entry address e of the instruction basic block to the address of the i-th instruction;
[0106] If the i-th instruction is a JUMP / JUMP1 jump instruction, a REVERT instruction, or a SELFDESTRUCT instruction, the address s of the i-th instruction is determined as the block exit address of the instruction basic block, and the instructions from the block entry address e to the block exit address s are taken as an instruction basic block and the block entry address e of the instruction basic block is updated to the address of the i+1-th instruction. In other words, i represents the sequential number of the instruction in the instruction sequence, i is initialized to 0, the entry address e of the instruction basic block is also set to 0, and the i-th instruction instr is taken from the EVM instruction sequence.
[0107] If instr is a JUMPDEST instruction, set the instruction basic block exit address s to the address of the i-1th instruction, and treat each instruction from the instruction basic block entry address e to the instruction basic block exit address s as a basic block, and add (es) to blockList (instruction basic block record). At the same time, set the basic block entry e to the address of the i-th instruction.
[0108] If instr is a JUMP / JUMPI, REVERT, or SELFDESTRUCT instruction, set the basic block exit s to the address s of the i-th instruction, and treat each instruction from address e to address s as an instruction basic block, adding (e, s) to blockList. At the same time, set the basic block entry e to the address of the i+1-th instruction.
[0109] If i+1 is less than the length of the EVM instruction sequence, increment i=i+1, loop to the above "take the i-th instruction instr from the EVM instruction sequence" until i+1 is not less than the length of the EVM instruction sequence, and output the instruction basic block sequence blockList.
[0110] Based on the instruction basic block sequence and jumpTargets, cList, and entryList of the above example, an embodiment of the present application provides a method for obtaining the instruction basic block sequence, including:
[0111] a. Initialize i to 0 and set the entry e address of the instruction basic block to 0.
[0112] b. Take the 0th instruction PUSHI (corresponding to address 0x0) from the EVM instruction sequence.
[0113] e. Since the 0th instruction is neither JUMPDEST nor JUMP / JUMPI, no operation is performed. Increment i = i + 1 = 1 and jump to step b.
[0114] b. Get the first instruction PUSHI (corresponding to address 0x2).
[0115] e. Because the first instruction PUSHI is neither JUMPDEST nor JUMP / JUMPI, no operation is performed. Increment i=i+1=2 and jump to step b.
[0116] …(repeat steps b and e until i=7).
[0117] d. Because the 7th instruction is a JUMPI instruction and the current address is 0xb, add (0x0, 0xb) to blockList and set the instruction basic block entry e to the address 0xc corresponding to the i+1th instruction.
[0118] e. Increment i=i+1 and jump to step b.
[0119] …(repeat steps b and e until i=19).
[0120] d. Because the 19th instruction is a JUMPI instruction and the current address is 0x3e, add (0xc, 0x3e) to blockList and set the instruction basic block entry e to the address 0x3f corresponding to the i+1th instruction.
[0121] e. Increment i=i+1 and jump to step b.
[0122] b. Take the 20th instruction JUMPDEST.
[0123] c. Set the instruction basic exit address to 0x3e and add (0xc, 0x3e) to blockList. Since (0xc, 0x3e) already exists in blockList, it has no impact on blockList. At the same time, set the instruction basic block entry address e to 0x3f.
[0124] e. Increment i=i+1 and jump to step b.
[0125] …(depending on the type of the i-th instruction, keep repeating the above steps b, c, d, e until i+1 is less than the length of the instruction sequence).
[0126] e. When i+1 is less than the length of the instruction sequence, the algorithm stops. Output blockList = [(0x0, 0xb), (0xc, 0x3e), (0x3f, 0x43), (0x44, 0x4a), (0x4b, 0x4e), (0x4f, 0x55), (0x56, 0x57), (0x58, 0x5d), (0x5e, 0x66), (0x67, 0x6e), (0x6f, 0x74), (0x67, 0x6e), (0x6f, 0x74), (0x75, 0x79), (0x7a, 0x83), (0x84, 0x85), (0x86, 0x91), (0x92, 0x94), (0x95, 0xa2)], which contains the entry address and exit address of each of the 17 instruction basic blocks in the above example.
[0127] The embodiment of the present application provides a method for generating a control flow graph of a smart contract, generating directed edges between nodes according to the jump record and the function record, including:
[0128] According to the function entry information in the function record, obtain the first node corresponding to any function entry address;
[0129] Determine the second node pointed to by the first node by whether the block exit address of the first node corresponds to a jump address in the jump record or the call information of the function record corresponds to a return address; continue to determine the third node pointed to by the second node until all nodes corresponding to the function entry addresses in the function entry information are traversed. In one example, the method flow of constructing a control flow graph using entryList, cList, jumpTargets and blockList includes:
[0130] a. Initialize the control flow graph G, and treat all elements (e, s) in the blockList array as nodes on the control flow graph. At this point, the control flow graph G is an empty graph with only nodes and no edges.
[0131] b. Take a function entry e from entryList. Search for the instruction basic block block whose instruction basic block entry address is e in blockList. If the search is successful, add the block to queue Q and jump to step c. Otherwise, take another function entry from entryList and repeat step b. If all function entries in entryList have been processed, output the control flow graph G.
[0132] c. If the queue Q is not empty, take the instruction basic block block' from the queue Q and jump to step d. If the queue Q is empty, jump to step b. If the instruction at the exit address s of the instruction basic block block' is a JUMP / JUMPI, jump to step e; if it is a REVERT or SELFDESTRUCT instruction, jump to step c; otherwise, adjust to step e.
[0133] d. Add the instruction basic block b_next' whose entry address is s+1 to Q, add a directed edge from block' to b_next' in the control flow graph G, and jump to step c for processing.
[0134] e. If the instruction at the exit address s of block' is JUMP / JUMPI, then search for its jump address from jumpTargets. If the search is successful, add a directed edge to the instruction basic block b_next with the jump address as the entry for block' in the control flow graph G, and add b_next to the queue Q (if it is a JUMPI instruction, add the instruction basic block b_next' with the entry address s+1 to Q, and add a directed edge from block' to b_next' in the control flow graph G), and jump to step c. If the search fails, go to step f.
[0135] With the exit address s of block', check from cList whether JUMP / JUMPI is a function call. If the search is successful, add a directed edge of block' to the basic block b_next with the corresponding function return address ret as the entry in the control flow graph G, and add b_next to the queue Q. Finally, jump to step c for processing.
[0136] The embodiment of the present application provides a control flow graph generation method for a smart contract, which determines the second node pointed to by the first node by determining whether the block exit address of the first node corresponds to a jump address in the jump record or the call information of the function record corresponds to a return address, including:
[0137] If the block exit address of the first node corresponds to a jump address in the jump record, a directed edge is generated from the first node to the second node corresponding to the jump address; if the block exit address in the instruction basic block of the first node corresponds to a return address in the call information of the function record, a directed edge is generated from the first node to the second node corresponding to the return address;
[0138] Otherwise, the first node is generated to point to a second node located after the first node according to the sequence information. In the above example, the method of constructing a control flow graph according to the entryList, cList, jumpTargets and blockList of the 17 instruction basic blocks includes:
[0139] a. Initialize the control flow graph with all elements in the blockList array as nodes. At this time, the control flow graph only has nodes and no edges.
[0140] b. Take the function entry 0x58 from entryList, and search for the basic block (0x58, 0x5d) with entry 0x58 in blockList. Add (0x58, 0x5d) to queue Q and jump to step c.
[0141] c. Queue Q is not empty, take out the basic block (0x58, 0x5d) from queue Q. Since the instruction of 0x5d is POP, jump to step d.
[0142] d. Add the next basic block (0x5e, 0x66) to Q, and add a directed edge from (0x58, 0x5d) to (0x5e, 0x66) in the control flow graph. Jump to step c for processing.
[0143] c. Queue Q is not empty, take out the basic block (0x5e, 0x66) from queue Q, because the instruction of 0x66 is JUMPI, jump to step e for processing.
[0144] e. Because there is an address of 0x66 (corresponding to the JUMPI instruction) and a jump address of 0x92 in jumpTarget, add the instruction basic block (0x92, 0x94) with entry 0x92 to Q, and add a directed edge from (0x5e, 0x66) to (0x92, 0x94). In addition, add the basic block (0x67, 0x6e) with entry address 0x66+1=0x67 to Q, and add a directed edge from (0x5e, 0x66) to (0x67, 0x6e). Jump to step c for processing.
[0145] c. Queue Q is not empty, so take out the basic block (0x92, 0x94) from queue Q. Since the instruction of 0x94 is JUMP, jump to step e for processing.
[0146] e. Because there is no jump to address 0x94 in jumpTarget, jump to step f for processing.
[0147] f. Because there is no function call at address 0x94 in cList, jump to c for processing.
[0148] c. Queue Q is not empty, take out the instruction basic block (0x67, 0x6e) from queue Q, because the instruction of 0x6e is JUMPI, jump to step e for processing.
[0149] e. Because there is address 0x6e (JUMPI instruction) and corresponding jump address 0x7a in jumpTarget, add the basic block (0x7a, 0x83) with entry 0x7a to Q, and add a directed edge from (0x67, 0x6e) to (0x7a, 0x83). In addition, add the basic block (0x6f, 0x74) with entry address 0x6e+1=0x6f to Q, and add a directed edge from (0x67, 0x6e) to (0x6f, 0x74). Jump to step c for processing.
[0150] c. Queue Q is not empty, take out the instruction basic block (0x7a, 0x83) from queue Q, because the instruction 0x83 is JUMP, jump to step e for processing.
[0151] e. Because there is no jump to address 0x83 (JUMP instruction) in jumptTarget, jump to step f for processing.
[0152] f. Because there is a function call (0x83, 0x84) with address 0x83 in cList, find the instruction basic block (0x84, 0x85) with entry address 0x84. Add a directed edge from (0x7a, 0x83) to (0x84, 0x85) in the control flow graph, and add (0x84, 0x85) to Q. Jump to step c for processing.
[0153] c. Queue Q is not empty, take out the instruction basic block (0x6f, 0x74) from queue Q, because the instruction 0x74 is JUMP, jump to step e for processing.
[0154] e. Because there is no jump at address 0x74 (JUMP instruction) in jumptTarget, jump to step f for processing.
[0155] f. Because there is a function call (0x74, 0x75) with address 0x74 in cList, find the instruction basic block (0x75, 0x79) with entry address 0x75. Add a directed edge from (0x6f, 0x74) to (0x75, 0x79) in the control flow graph, and add (0x75, 0x79) to Q. Jump to step c for processing.
[0156] c. Queue Q is not empty, take out the instruction basic block (0x84, 0x85) from queue Q, because the instruction 0x85 is POP, jump to step d for processing.
[0157] d. Add the next instruction basic block (0x86, 0x91) to Q, and add a directed edge from (0x84, 0x85) to (0x86, 0x91) in the control flow graph. Jump to step c for processing.
[0158] c. Queue Q is not empty, take out the instruction basic block (0x75, 0x79) from queue Q, because the instruction 0x79 is JUMP, jump to step e for processing.
[0159] e. Because there is an address of 0x79 (JUMPI instruction) and a corresponding jump address of 0x86 in jumpTarget, and (0x86, 0x91) has been added to Q, it is not added again. A directed edge from (0x75, 0x79) to (0x86, 0x91) is added, and jumps to c.
[0160] c. Queue Q is not empty, take out the instruction basic block (0x86, 0x91) from queue Q, because the instruction 0x91 is JUMP, jump to step e for processing.
[0161] e. Because there is an address of 0x91 (JUMPI instruction) and the corresponding jump address 0x5e in jumpTarget, and because (0x5e, 0x66) has been added to Q, it is not added again. Add a directed edge from (0x86, 0x91) to (0x5e, 0x66) and jump to step c.
[0162] c. Queue Q is empty, jump to step b for processing.
[0163] b. Take the function entry 0x95 from entryList, and search for the basic block (0x95, 0xa2) with entry 0x95 in blockList. Add (0x95, 0xa2) to queue Q and jump to step c.
[0164] c. Queue Q is not empty, take out the instruction basic block (0x95, 0xa2) from queue Q, because the instruction of 0xa2 is JUMP, jump to step e for processing.
[0165] e. Because there is no jump at address 0x95 in jumptTarget, jump to step f.
[0166] f. Because there is no function call at address 0x95 from cList, jump to c.
[0167] c. Queue Q is empty, jump to step b for processing.
[0168] b.entryList is empty, so the algorithm ends.
[0169] The control flow chart of the smart contract is obtained through the above method steps, such as Figure 7 shown.
[0170] Based on the above method flow, the present application embodiment provides a method flow for generating a control flow graph of a smart contract, such as Figure 8 As shown, including:
[0171] Step 801: Get the smart contract.
[0172] Step 802: Translate the smart contract into Ethereum virtual machine bytecode, and obtain the jump record and function record of the smart contract.
[0173] Step 803: Convert the Ethereum virtual machine bytecode into an instruction sequence.
[0174] Step 804: Divide the instruction sequence into instruction basic blocks and obtain sequence information of each instruction basic block.
[0175] Step 805: Taking the instruction basic block as a node, generating directed edges between the nodes according to the jump record and the function record, and obtaining the control flow graph of the smart contract.
[0176] Based on the same concept, an embodiment of the present invention provides a control flow graph generation device for a smart contract. Fig. 9 A schematic diagram of a control flow graph generation device for a smart contract provided in an embodiment of the present application is shown in FIG. Fig. 9 Including:
[0177] The translation and recording module 901 is used to generate a jump record for a jump instruction in the smart contract and a function record for a function instruction in the smart contract during the translation of the smart contract into Ethereum virtual machine bytecode; the jump record is used to represent the jump address of the jump instruction; the function record is used to represent the boundary address of the function instruction;
[0178] A construction module 902 is used to convert the Ethereum virtual machine bytecode into an Ethereum virtual machine instruction sequence by a disassembly tool, and divide the Ethereum virtual machine instruction sequence into a plurality of instruction basic blocks with sequence information;
[0179] The construction module 902 is also used to use each instruction basic block as a node in the control flow graph, generate directed edges between the nodes according to the jump record, the function record and the sequence information, and obtain the control flow graph of the smart contract.
[0180] Optionally, the translation and recording module 901 is specifically used to: generate a jump address corresponding to the control instruction in the smart contract; and generate jump information corresponding to each JUMP / JUMPI jump instruction in the control instruction, wherein the jump information includes the address of the jump instruction and the jump address corresponding to the jump instruction.
[0181] Optionally, the translation and recording module 901 is specifically used to: for the control instruction in the smart contract, generate a jump address corresponding to the control instruction; for each JUMP / JUMPI jump instruction in the control instruction, generate jump information corresponding to the jump instruction, including: for the loop instruction in the control instruction, generate the jump start address and jump end address of the loop instruction; when translating to the JUMPI jump instruction, use the address and jump end address of the JUMPI jump instruction as the jump information of the JUMPI jump instruction; when transferring to the last JUMP jump instruction , taking the address of the JUMP jump instruction and the jump start address as the jump information of the JUMPI jump instruction; generating a FALSE jump address and an END jump address for the conditional statement / ternary expression in the control instruction; when translating to the JUMPI jump instruction corresponding to the conditional judgment statement, taking the address of the JUMPI jump instruction and the FALSE jump address as the jump information of the JUMPI jump instruction; when translating to the JUMP jump instruction after translating to the FALSE content, taking the address of the JUMP jump instruction and the END jump address as the jump information of the JUMP jump instruction.
[0182] Optionally, the translation and recording module 901 is specifically used for: when translating to the function selector in the smart contract, recording the function entry address of the public function in the function selector to the function entry information in the function record, where the function entry address of the public function is the address of the first JUMPDEST instruction after parameter loading and translation; when translating to the function body in the smart contract, recording the function entry address of the private function in the function body to the function entry information; the function entry address of the private function is the address of the first parameter of the calling address; when translating to the function calling statement in the function body, recording the calling address and return address of the function calling statement to the calling information in the function record; the calling address is the address of the JUMP instruction corresponding to the function calling statement; the return address is the address of the JUMPDEST instruction corresponding to the function calling statement.
[0183] Optionally, the construction module 902 is specifically used to set the block entry address of the instruction basic block to e; obtain instructions from the Ethereum virtual machine instruction sequence in sequence, and if the i-th instruction is a JUMPDEST instruction, then the address s of the i-1-th instruction is determined as the block exit address of the instruction basic block, and each instruction from the block entry address e to the block exit address s is used as an instruction basic block and the block entry address e of the instruction basic block is updated to the address of the i-th instruction; if the i-th instruction is a JUMP / JUMP1 jump instruction, a REVERT instruction, or a SELFDESTRUCT instruction, then the address s of the i-th instruction is determined as the block exit address of the instruction basic block, and each instruction from the block entry address e to the block exit address s is used as an instruction basic block and the block entry address e of the instruction basic block is updated to the address of the i+1-th instruction.
[0184] Optionally, the construction module 902 is specifically used to obtain a first node corresponding to any function entry address according to the function entry information in the function record; determine the second node pointed to by the first node by whether the block exit address of the first node corresponds to a jump address in the jump record or the call information of the function record corresponds to a return address; continue to determine the third node pointed to by the second node until all nodes corresponding to the function entry addresses in the function entry information are traversed.
[0185] Optionally, the construction module 902 is specifically used to, if the block exit address of the first node corresponds to a jump address in the jump record, generate a directed edge from the first node to a second node corresponding to the jump address; if the block exit address in the instruction basic block of the first node corresponds to a return address in the call information of the function record, generate a directed edge from the first node to the second node corresponding to the return address; otherwise, generate a directed edge from the first node to a second node that is located after the first node according to the sequence information.
[0186] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0187] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes 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 device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing 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.
[0188] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising 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.
[0189] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing 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.
[0190] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A control flow graph generation method for smart contracts, It is characterized in that The method comprises: In the process of translating the smart contract into Ethereum virtual machine bytecode, a jump record is generated for the jump instruction in the smart contract, and a function record is generated for the function instruction in the smart contract; the jump record is used to represent the jump address of the jump instruction; the function record is used to represent the boundary address of the function instruction; Convert the Ethereum virtual machine bytecode into an Ethereum virtual machine instruction sequence by a disassembly tool, and divide the Ethereum virtual machine instruction sequence into a plurality of instruction basic blocks with sequence information; Taking each instruction basic block as a node in a control flow graph, generating directed edges between nodes according to the jump record, the function record and the sequence information, and obtaining a control flow graph of the smart contract; The step of generating a function record for the function instruction in the smart contract includes: When translating to the function selector in the smart contract, the function entry address of the public function in the function selector is recorded in the function entry information in the function record, where the function entry address of the public function is the address of the first JUMPDEST instruction after parameter loading and translation; When translating into the function body in the smart contract, the function entry address of the private function in the function body is recorded in the function entry information; the function entry address of the private function is the address of the first parameter of the calling address; When translating into a function call statement in the function body, the call address and return address of the function call statement are recorded in the call information in the function record; the call address is the address of the JUMP instruction corresponding to the function call statement; the return address is the address of the JUMPDEST instruction corresponding to the function call statement.
2. The method as claimed in claim 1, It is characterized in that Generate a jump record for the jump instruction in the smart contract, including: For the control instruction in the smart contract, a jump address corresponding to the control instruction is generated; for each JUMP / JUMPI jump instruction in the control instruction, jump information corresponding to the jump instruction is generated, and the jump information includes the address of the jump instruction and the jump address corresponding to the jump instruction.
3. The method as claimed in claim 2, It is characterized in that For the control instruction in the smart contract, generate a jump address corresponding to the control instruction; for each JUMP / JUMPI jump instruction in the control instruction, generate jump information corresponding to the jump instruction, including: For the loop instruction in the control instruction, a jump start address and a jump end address of the loop instruction are generated; when translating to a JUMPI jump instruction, the address where the JUMPI jump instruction is located and the jump end address are used as the jump information of the JUMPI jump instruction; when transferring to the last JUMP jump instruction, the address where the JUMP jump instruction is located and the jump start address are used as the jump information of the JUMPI jump instruction; For the conditional statement / ternary expression in the control instruction, a FALSE jump address and an END jump address are generated; when translated to a JUMPI jump instruction corresponding to the conditional judgment statement, the address where the JUMPI jump instruction is located and the FALSE jump address are used as the jump information of the JUMPI jump instruction; when translated to a JUMP jump instruction after the FALSE content, the address where the JUMP jump instruction is located and the END jump address are used as the jump information of the JUMP jump instruction.
4. The method as claimed in claim 1, It is characterized in that The Ethereum virtual machine instruction sequence is divided into a plurality of ordered instruction basic blocks, including: Set the block entry address of the instruction basic block to e; Get instructions from the Ethereum virtual machine instruction sequence in sequence. If the i-th instruction is a JUMPDEST instruction, determine the address s of the i-1-th instruction as the block exit address of the instruction basic block, take each instruction from the block entry address e to the block exit address s as an instruction basic block, and update the block entry address e of the instruction basic block to the address of the i-th instruction; If the i-th instruction is a JUMP / JUMP1 jump instruction, a REVERT instruction, or a SELFDESTRUCT instruction, then the address s of the i-th instruction is determined as the block exit address of the instruction basic block, and each instruction from the block entry address e to the block exit address s is taken as an instruction basic block and the block entry address e of the instruction basic block is updated to the address of the i+1-th instruction.
5. The method as claimed in claim 1, It is characterized in that Generating directed edges between nodes according to the jump record and the function record, including: According to the function entry information in the function record, obtain the first node corresponding to any function entry address; The second node pointed to by the first node is determined by whether the block exit address of the first node corresponds to a jump address in the jump record or the calling information of the function record corresponds to a return address; the third node pointed to by the second node is continued to be determined until all nodes corresponding to the function entry addresses in the function entry information are traversed.
6. The method as claimed in claim 5, It is characterized in that Determining the second node pointed to by the first node by determining whether the block exit address of the first node corresponds to a jump address in the jump record or the call information of the function record corresponds to a return address, includes: If the block exit address of the first node corresponds to a jump address in the jump record, a directed edge is generated from the first node to the second node corresponding to the jump address; if the block exit address in the instruction basic block of the first node corresponds to a return address in the call information of the function record, a directed edge is generated from the first node to the second node corresponding to the return address; Otherwise, generate the first node to point to a second node that is located after the first node according to the sequence information.
7. A control flow graph generation device for smart contracts, It is characterized in that The device comprises: A translation and recording module, used to generate a jump record for a jump instruction in the smart contract and a function record for a function instruction in the smart contract during the translation of the smart contract into Ethereum virtual machine bytecode; the jump record is used to represent the jump address of the jump instruction; the function record is used to represent the boundary address of the function instruction; A construction module is used to convert the Ethereum virtual machine bytecode into an Ethereum virtual machine instruction sequence through a disassembly tool, and divide the Ethereum virtual machine instruction sequence into a plurality of instruction basic blocks with sequence information; The construction module is further used to use each instruction basic block as a node in the control flow graph, generate directed edges between the nodes according to the jump record, the function record and the sequence information, and obtain the control flow graph of the smart contract; The translation and recording module is specifically used for: when translating to the function selector in the smart contract, recording the function entry address of the public function in the function selector to the function entry information in the function record, where the function entry address of the public function is the address of the first JUMPDEST instruction after parameter loading and translation; when translating to the function body in the smart contract, recording the function entry address of the private function in the function body to the function entry information; the function entry address of the private function is the address of the first parameter of the calling address; when translating to the function call statement in the function body, recording the calling address and return address of the function call statement to the calling information in the function record; the calling address is the address of the JUMP instruction corresponding to the function call statement; the return address is the address of the JUMPDEST instruction corresponding to the function call statement.
8. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores a program, and when the program is run on a computer, the computer is enabled to implement the method according to any one of claims 1 to 6.
9. A computer device, It is characterized in that include: Memory for storing computer programs; A processor is used to call the computer program stored in the memory, and execute the method according to any one of claims 1 to 6 according to the obtained program.
