Bytecode editing method and device, electronic equipment and program product

By automatically checksuming the length after bytecode editing, the problem of low bytecode editing in the prior art is solved, and a more efficient bytecode editing process is achieved.

CN119396412AActive Publication Date: 2025-02-07HANGZHOU HIGH-TECH ZONE (BINJIANG) INSTITUTE OF BLOCKCHAIN & DATA SECURITY
View PDF 11 Cites 0 Cited by

Patent Information

Application Number
CN202510002734.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-02
Publication Date
2025-02-07
Estimated Expiration
2045-01-02

AI Technical Summary

Technical Problem

The existing bytecode editing methods are inefficient and require manual checksum adjustment of the edited bytecode to avoid stack imbalance and target address offset.

Method used

By obtaining the first bytecode to be edited, the second bytecode is obtained by editing, and whether the bytecode length has changed is determined based on the first bytecode and the second bytecode. If there is a change, the second bytecode is adjusted to obtain the target bytecode and output.

Benefits of technology

Automatic checksum adjustment of edited bytecode is realized, improving the efficiency of bytecode editing method.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119396412A_ABST
    Figure CN119396412A_ABST
Patent Text Reader

Abstract

The invention is suitable for the technical field of block chains, and provides a byte code editing method and device, electronic equipment and a program product. The byte code editing method comprises the following steps: acquiring a first byte code to be edited; editing the first byte code to obtain a second byte code; according to the first byte code and the second byte code, determining whether the length of the byte code is changed or not; and if it is determined that the length of the byte code changes, adjusting the second byte code to obtain a target byte code, and outputting the target byte code. Through the editing method provided by the invention, after the byte code is edited, the edited byte code can be automatically checked and adjusted through the electronic equipment, so that the editing efficiency of the byte code editing method is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of blockchain technology, and in particular, relates to a bytecode editing method, device, electronic device and program product. Background Art

[0002] Smart contracts are the core technology in the blockchain field. Usually, developers need to expand the functions of smart contracts, optimize requirements, and fix vulnerabilities so that smart contracts can meet user needs. Therefore, developers need to edit the bytecode of smart contracts.

[0003] The bytecode of smart contracts is usually difficult to read, so the existing bytecode editing method usually uses a disassembly tool to convert the bytecode into assembly-level instructions, and then manually modify the bytecode instructions at the specified location according to the needs to obtain the edited bytecode. However, after modifying the bytecode instructions at the specified location, in order to avoid problems such as stack imbalance and target address offset, the edited bytecode needs to be manually verified and adjusted, which leads to low editing efficiency of the existing bytecode editing method. Summary of the invention

[0004] In view of this, embodiments of the present application provide a bytecode editing method, device, electronic device, and program product.

[0005] In a first aspect, an embodiment of the present application provides a bytecode editing method, comprising: Get the first bytecode to be edited; Editing the first bytecode to obtain a second bytecode; Determining whether a bytecode length changes according to the first bytecode and the second bytecode; If it is determined that the bytecode length has changed, the second bytecode is adjusted to obtain a target bytecode, and the target bytecode is output.

[0006] Optionally, determining whether the bytecode length changes according to the first bytecode and the second bytecode includes: Determine whether the elements in the stack are balanced according to the first bytecode and the second bytecode; If it is determined that the elements in the stack are balanced, it is determined whether the bytecode length changes according to the first bytecode and the second bytecode.

[0007] Optionally, determining whether the elements in the stack are balanced according to the first bytecode and the second bytecode includes: Determine a first instruction within a specified line number range from all instructions corresponding to the first bytecode, and determine a change in the number of elements in the stack caused by the first instruction; Determine a second instruction within a specified line number range from all instructions corresponding to the second bytecode, and determine a change in the number of elements in the stack caused by the second instruction; Whether the elements in the stack are balanced is determined according to the change in the number of elements in the stack caused by the first instruction and the change in the number of elements in the stack caused by the second instruction.

[0008] Optionally, determining whether the bytecode length changes according to the first bytecode and the second bytecode includes: Determine, according to the type of each instruction among all the instructions corresponding to the first bytecode, the length of each instruction among all the instructions corresponding to the first bytecode; Determine the length of each instruction among all the instructions corresponding to the second bytecode according to the type of each instruction among all the instructions corresponding to the second bytecode; Determine whether the bytecode length changes according to the length of each instruction among all instructions corresponding to the first bytecode and the length of each instruction among all instructions corresponding to the second bytecode.

[0009] Optionally, after determining whether the bytecode length has changed according to the first bytecode and the second bytecode, the method further includes: If it is determined that the bytecode length has not changed, the second bytecode is output.

[0010] Optionally, if it is determined that the bytecode length has changed, adjusting the second bytecode to obtain a target bytecode includes: If the length of the second bytecode is shorter than the length of the first bytecode, a preset bytecode is filled at a specified position in the second bytecode so that the adjusted length of the second bytecode is equal to the length of the first bytecode.

[0011] Optionally, if it is determined that the bytecode length has changed, adjusting the second bytecode to obtain a target bytecode further includes: If the length of the second bytecode is longer than that of the first bytecode, the edited bytecode in the second bytecode is replaced with the bytecode corresponding to the first jump instruction, and the edited bytecode is added to the target position corresponding to the first jump instruction, and the bytecode corresponding to the second jump instruction is added after the edited bytecode; wherein the first jump instruction is used to jump to the target position, and the second jump instruction is used to jump to the initial position of the edited bytecode.

[0012] In a second aspect, an embodiment of the present application provides a bytecode editing device, comprising: A bytecode acquisition unit, used for acquiring a first bytecode to be edited; A bytecode editing unit, used for editing the first bytecode to obtain a second bytecode; a length change determining unit, configured to determine whether the length of a bytecode changes according to the first bytecode and the second bytecode; The bytecode adjustment unit is used to adjust the second bytecode to obtain a target bytecode if it is determined that the bytecode length has changed, and output the target bytecode.

[0013] In a third aspect, an embodiment of the present application provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, each step in the bytecode editing method as described in any one of the first aspects above is implemented.

[0014] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, each step in the bytecode editing method as described in any one of the above-mentioned first aspects is implemented.

[0015] In a fifth aspect, an embodiment of the present application provides a computer program product. When the computer program product is run on an electronic device, the electronic device executes each step of the bytecode editing method as described in any one of the first aspects above.

[0016] The bytecode editing method, device, electronic device, and program product provided by the embodiments of the present application have the following beneficial effects: In the bytecode editing method provided in the embodiment of the present application, the first bytecode to be edited is first obtained, and then the first bytecode is edited to obtain the second bytecode, and then whether the bytecode length has changed is determined based on the first bytecode and the second bytecode. If it is determined that the bytecode length has changed, the second bytecode is adjusted to obtain the target bytecode, and the target bytecode is output. Through the editing method of the present application, after the bytecode is edited, the edited bytecode can be automatically checked and adjusted by the electronic device. Compared with the existing method that requires manual verification and adjustment of the edited bytecode, the method of the present application improves the editing efficiency of the bytecode editing method. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

[0018] Figure 1 A flowchart of a bytecode editing method provided in an embodiment of the present application; Figure 2 A schematic diagram of the structure of a bytecode editing device provided in an embodiment of the present application; Figure 3 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0019] It should be noted that the terms used in the embodiments of the present application are only used to explain the specific embodiments of the present application, and are not intended to limit the present application. In the description of the embodiments of the present application, unless otherwise specified, "multiple" refers to two or more than two, and "at least one", "one or more" refers to one, two or more. The terms "first" and "second" are only used for descriptive purposes and cannot be understood as indicating or implying relative importance or implicitly indicating the number of indicated technical features. Thus, it is defined that the "first" and "second" features can explicitly or implicitly include one or more of the features.

[0020] References to "one embodiment" or "some embodiments" etc. described in this specification mean that a particular feature, structure or characteristic described in conjunction with the embodiment is included in one or more embodiments of the present application. Thus, the phrases "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear at different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.

[0021] The executor of the bytecode editing method provided in the embodiment of the present application may be an electronic device, which may execute each step of the bytecode editing method provided in the embodiment of the present application. Specifically, the electronic device may include but is not limited to mobile phones, tablet computers, laptop computers, and desktop computers.

[0022] The bytecode editing method provided in the embodiment of the present application can be applied to various scenarios where bytecode editing is required. Exemplarily, the bytecode editing method provided in the embodiment of the present application can be applied to the scenario of editing the bytecode of the smart contract in the blockchain field. Specifically, the bytecode editing method provided in the embodiment of the present application can be applied to the scenario of editing the bytecode of the Ethereum Virtual Machine (EVM).

[0023] When any bytecode needs to be edited, the various steps of the bytecode editing method provided in the embodiment of the present application can be executed by an electronic device, so that the edited bytecode can be automatically checked and adjusted by the electronic device, thereby improving the editing efficiency of the bytecode editing method.

[0024] See also Figure 1 , Figure 1 The present invention provides a flowchart of a bytecode editing method according to an embodiment of the present invention. The bytecode editing method may include S101 to S103, which are described in detail as follows: In S101, a first bytecode to be edited is obtained.

[0025] In an embodiment of the present application, the electronic device may first obtain the first bytecode to be edited. Exemplarily, the user may input the first bytecode to be edited into the electronic device so that the electronic device may obtain the first bytecode to be edited.

[0026] In practical applications, the first byte code to be edited may be in hexadecimal format. For example, the first byte code to be edited may be 0x601456...5b.

[0027] In S102, the first bytecode is edited to obtain a second bytecode.

[0028] In an embodiment of the present application, after obtaining the first bytecode to be edited, the electronic device can edit the first bytecode to obtain the second bytecode.

[0029] Exemplarily, the user may input an editing instruction into the electronic device, so that the electronic device may edit the first bytecode to be edited according to the editing instruction input by the user to obtain the second bytecode.

[0030] Specifically, the specific process of editing the first bytecode can be: the electronic device first uses a disassembly tool to convert the first bytecode into corresponding instructions, and then modifies the instructions according to the editing instructions input by the user to obtain modified instructions, and then obtains the second bytecode according to the modified instructions.

[0031] The functions of the editing instructions input by the user may include but are not limited to deletion instructions, modification instructions, and addition instructions.

[0032] It should be noted that the second bytecode is the bytecode that has been preliminarily edited. In practical applications, the preliminarily edited second bytecode needs to be checked and adjusted to avoid problems such as stack imbalance and target address offset. In existing editing methods, the preliminarily edited second bytecode is usually checked and adjusted manually. In the embodiment of the present application, the electronic device can automatically check and adjust the preliminarily edited second bytecode by executing S103 and S104, thereby improving the editing efficiency of the bytecode.

[0033] In S103, it is determined whether the bytecode length has changed according to the first bytecode and the second bytecode.

[0034] In the embodiment of the present application, after obtaining the second byte code, the electronic device may first verify the second byte code. Specifically, the electronic device may determine whether the byte code length has changed according to the first byte code and the second byte code.

[0035] In one possible implementation, before the electronic device determines whether the bytecode length has changed based on the first bytecode and the second bytecode, it may first determine whether the elements in the stack are balanced based on the first bytecode and the second bytecode; if it is determined that the elements in the stack are balanced, the electronic device may determine whether the bytecode length has changed based on the first bytecode and the second bytecode; if it is determined that the elements in the stack are unbalanced, the electronic device may output prompt information describing the change in the number of elements in the stack.

[0036] The reason why it is necessary to determine whether the elements in the stack are balanced before determining whether the bytecode length has changed is that if the elements in the stack are unbalanced, it may cause stack overflow, stack underflow, and stack element offset. Among them, the stack overflow phenomenon is that the elements in the stack exceed the preset maximum element limit (such as 1024). The stack overflow phenomenon may cause the execution of the corresponding bytecode (that is, the second bytecode in the embodiment of the present application) to fail; the stack underflow phenomenon may cause an error when taking values ​​from an empty stack; the stack element offset phenomenon may cause subsequent logical errors. For example, the stack state is [3,1, 2] after the original instruction is executed. It is assumed that it will be judged whether the sum of the first two elements in the stack is equal to the third element (that is, it is judged that 2+1=3). If improper editing causes an extra element in the stack after execution, becoming [3, 1, 2, 4], then the elements in the stack taken out when subsequent instructions are executed will be offset, causing subsequent logical errors.

[0037] In this implementation, the electronic device can determine whether the elements in the stack are balanced through steps a to c. The details are as follows: In step a, a first instruction within a specified range of lines is determined from all instructions corresponding to the first bytecode, and a change in the number of elements in the stack caused by the first instruction is determined.

[0038] In this implementation, the electronic device can first determine the instruction corresponding to the first bytecode, and then determine the first instruction within a specified line range from all the instructions corresponding to the first bytecode. After that, the electronic device can simulate the operation of the first instruction on the stack for each first instruction according to the type of the first instruction, so as to determine the change in the number of elements in the stack caused by the first instruction, and further determine the change in the number of elements in the stack caused by each first instruction.

[0039] Exemplarily, if the type of a first instruction is a PUSH instruction, the electronic device simulates the operation of the PUSH instruction on the stack as "pushing a value into the stack". After simulating the operation of "pushing a value into the stack", the electronic device can obtain that the change in the number of elements in the stack caused by the PUSH instruction is adding one element to the stack.

[0040] Exemplarily, if the type of a first instruction is an ADD instruction, the electronic device simulates the operation of the ADD instruction on the stack as "taking two values ​​from the stack and pushing a value into the stack". After simulating the operation of "taking two values ​​from the stack and pushing a value into the stack", the electronic device can obtain that the change in the number of elements in the stack caused by the ADD instruction is a reduction of one element in the stack.

[0041] In step b, the second instruction within the specified line number range is determined from all instructions corresponding to the second bytecode, and the change in the number of elements in the stack caused by the second instruction is determined.

[0042] In this implementation, the electronic device can first determine the instruction corresponding to the second bytecode, and then determine the second instruction within a specified line range from all the instructions corresponding to the second bytecode. After that, the electronic device can simulate the operation of the second instruction on the stack for each second instruction according to the type of the second instruction, so as to determine the change in the number of elements in the stack caused by the second instruction, and further determine the change in the number of elements in the stack caused by each second instruction.

[0043] It should be noted that the specified line number range corresponding to the second instruction can be the same as or different from the instruction line number range corresponding to the first instruction, and the specified line number range corresponding to the second instruction and the instruction line number range corresponding to the first instruction can be set according to actual needs and are not limited here.

[0044] The electronic device simulates the operation of the second instruction on the stack according to the type of the second instruction, so as to determine the change in the number of elements in the stack caused by the second instruction. For an example, refer to the example corresponding to step a, which will not be repeated here.

[0045] In step c, it is determined whether the elements in the stack are balanced according to the change in the number of elements in the stack caused by the first instruction and the change in the number of elements in the stack caused by the second instruction.

[0046] In this implementation, after determining the change in the number of elements in the stack caused by each first instruction and the change in the number of elements in the stack caused by each second instruction, the electronic device can accumulate the change in the number of elements in the stack caused by each first instruction to obtain the change in the number of elements in the stack caused by the first instruction. Similarly, the electronic device can accumulate the change in the number of elements in the stack caused by each second instruction to obtain the change in the number of elements in the stack caused by the second instruction. Afterwards, the electronic device can compare the change in the number of elements in the stack caused by the first instruction and the change in the number of elements in the stack caused by the second instruction, so as to determine whether the elements in the stack are balanced.

[0047] Specifically, if the electronic device determines that the change in the number of elements in the stack caused by the first instruction is the same as the change in the number of elements in the stack caused by the second instruction, it can be determined that the elements in the stack are balanced; if the electronic device determines that the change in the number of elements in the stack caused by the first instruction is different from the change in the number of elements in the stack caused by the second instruction, it can be determined that the elements in the stack are unbalanced.

[0048] The above describes an implementation method for determining whether the elements in the stack are balanced. After determining whether the elements in the stack are balanced, if it is determined that the elements in the stack are balanced, the electronic device can determine whether the bytecode length has changed based on the first bytecode and the second bytecode; if it is determined that the elements in the stack are unbalanced, the electronic device can output prompt information for describing the change in the number of elements in the stack. Based on this, the following continues to describe an implementation method for determining whether the bytecode length has changed.

[0049] In a possible implementation, the electronic device may determine whether the bytecode length has changed through steps d to f. The details are as follows: In step d, the length of each instruction among all the instructions corresponding to the first bytecode is determined according to the type of each instruction among all the instructions corresponding to the first bytecode.

[0050] In this implementation, after determining the balance of elements in the stack, the electronic device can determine the type of each instruction among all instructions corresponding to the first bytecode. Thereafter, the electronic device can determine the length of each instruction among all instructions corresponding to the first bytecode according to the type of each instruction among all instructions corresponding to the first bytecode.

[0051] Exemplarily, if the type of instruction a corresponding to the first bytecode is a PUSHn instruction, the length of instruction a can be determined to be 1+n, where n is the number of bytes following the PUSH instruction. Exemplarily, if the type of instruction b corresponding to the first bytecode is a common instruction (an instruction other than the PUSHn instruction), the length of instruction b can be determined to be 1.

[0052] In step e, the length of each instruction among all the instructions corresponding to the second bytecode is determined according to the type of each instruction among all the instructions corresponding to the second bytecode.

[0053] In this implementation, after determining the balance of elements in the stack, the electronic device can determine the type of each instruction among all instructions corresponding to the second bytecode. Thereafter, the electronic device can determine the length of each instruction among all instructions corresponding to the second bytecode according to the type of each instruction among all instructions corresponding to the second bytecode.

[0054] Exemplarily, if the type of instruction c corresponding to the second bytecode is a PUSHn instruction, the length of the instruction c can be determined to be 1+n, where n is the number of bytes following the PUSH instruction. Exemplarily, if the type of instruction d corresponding to the second bytecode is a common instruction (an instruction other than the PUSHn instruction), the length of the instruction d can be determined to be 1.

[0055] In step f, it is determined whether the bytecode length has changed based on the length of each instruction in all instructions corresponding to the first bytecode and the length of each instruction in all instructions corresponding to the second bytecode.

[0056] In this implementation, after determining the length of each instruction among all instructions corresponding to the first bytecode and determining the length of each instruction among all instructions corresponding to the second bytecode, the electronic device can determine whether the bytecode length has changed based on the length of each instruction among all instructions corresponding to the first bytecode and the length of each instruction among all instructions corresponding to the second bytecode.

[0057] Specifically, the electronic device can determine the total length of all instructions corresponding to the first byte code (i.e., the length of the first byte code) based on the length of each instruction in all instructions corresponding to the first byte code. Similarly, the electronic device can determine the total length of all instructions corresponding to the second byte code (i.e., the length of the second byte code) based on the length of each instruction in all instructions corresponding to the second byte code. Afterwards, the electronic device can compare the total length of all instructions corresponding to the first byte code with the total length of all instructions corresponding to the second byte code. If the total length of all instructions corresponding to the first byte code is the same as the total length of all instructions corresponding to the second byte code, the electronic device can determine that the byte code length has not changed. If the total length of all instructions corresponding to the first byte code is different from the total length of all instructions corresponding to the second byte code, the electronic device can determine that the byte code length has changed.

[0058] After determining whether the bytecode length has changed, if it is determined that the bytecode length has not changed, the electronic device may output a second bytecode to indicate that the user may determine the second bytecode as a target bytecode that has been edited and does not need to be further adjusted.

[0059] In S104, if it is determined that the bytecode length has changed, the second bytecode is adjusted to obtain a target bytecode, and the target bytecode is output.

[0060] In the embodiment of the present application, after determining whether the bytecode length has changed, if it is determined that the bytecode length has changed, the electronic device can adjust the second bytecode to obtain a target bytecode and output the target bytecode. The target bytecode can be considered as a bytecode that has been edited and does not need to be further adjusted.

[0061] In a possible implementation, the electronic device may first compare the length of the first byte code with the length of the second byte code, and then determine a specific method for adjusting the second byte code based on the comparison result of the length of the first byte code and the length of the second byte code.

[0062] In this implementation, the electronic device can adjust the second byte code through step g and step h. The details are as follows: In step g, if the length of the second bytecode is shorter than the length of the first bytecode, a preset bytecode is filled in a designated position in the second bytecode so that the adjusted length of the second bytecode is equal to the length of the first bytecode.

[0063] In this implementation, if the electronic device determines that the length of the second bytecode is shorter than the length of the first bytecode, it can find a specified position in the second bytecode and fill the specified position in the second bytecode with a target number of preset bytecodes, so that the adjusted length of the second bytecode is equal to the length of the first bytecode.

[0064] Among them, illustratively, the designated position can be determined in the following manner: the electronic device can determine the target instruction that causes the length of the second bytecode to be shorter than the length of the first bytecode among all instructions corresponding to the second bytecode, and determine the position of the target instruction as the designated position.

[0065] Exemplarily, the preset bytecode may be a meaningless bytecode, that is, the instruction corresponding to the preset bytecode may be a meaningless instruction.

[0066] Among them, exemplarily, the target number can be determined in the following manner: the electronic device can first determine the difference between the length of the second byte code and the length of the first byte code, and then determine the target number of preset byte codes corresponding to the difference between the length of the second byte code and the length of the first byte code.

[0067] The following provides an example to illustrate the specific method of filling the preset bytecode at the specified position in the second bytecode.

[0068] If the first bytecode is 0x62123456, the instruction corresponding to the first bytecode is:

[00] PUSH3 123456 That is, the instruction corresponding to the first bytecode is to push "123456" into the stack.

[0069] If the second byte code is 0x614321, the instruction corresponding to the second byte code is:

[00] PUSH2 4321 That is, the instruction corresponding to the second bytecode is to push "4321" into the stack.

[0070] It can be seen that the length of the second byte code is shorter than that of the first byte code, so the electronic device can adjust the second byte code to obtain the adjusted second byte code as 0x6143215b. The instruction corresponding to the adjusted second byte code is:

[00] PUSH2 4321

[03] JUMPDEST / / Automatically fill the position of this instruction so that the position of the subsequent instructions remains unchanged At this time, the specified position in the second bytecode is the position corresponding to the instruction "

[03] JUMPDEST", and the preset bytecode can be "5b".

[0071] It can be seen that step g can make the length of the adjusted second bytecode equal to the length of the first bytecode, thereby avoiding instruction offset.

[0072] In step h, if the length of the second bytecode is longer than that of the first bytecode, the edited bytecode in the second bytecode is replaced with the bytecode corresponding to the first jump instruction, and the edited bytecode is added to the target position corresponding to the first jump instruction, and the bytecode corresponding to the second jump instruction is added after the edited bytecode.

[0073] In this implementation, if it is determined that the length of the second bytecode is longer than the length of the first bytecode, the electronic device can replace the edited bytecode in the second bytecode with the bytecode corresponding to the first jump instruction, wherein the first jump instruction is used to indicate a jump to a target position, and exemplarily, the target position can be the end position of the bytecode.

[0074] In addition to replacing the edited bytecode in the second bytecode with the bytecode corresponding to the first jump instruction, the electronic device may also add the edited bytecode to the target position corresponding to the first jump instruction. For example, the electronic device may add the edited bytecode to the end position of the bytecode.

[0075] In addition, the electronic device may also add a bytecode corresponding to a second jump instruction after the edited bytecode, wherein the second jump instruction is used to jump to the initial position of the edited bytecode.

[0076] The following provides an example to illustrate the specific method of replacing the edited bytecode in the second bytecode with the bytecode corresponding to the first jump instruction, adding the edited bytecode to the target position corresponding to the first jump instruction, and adding the bytecode corresponding to the second jump instruction after the edited bytecode.

[0077] If the first byte code is 0x62aaaaaa...00, the instruction corresponding to the first byte code is:

[00] PUSH3aaaaaa ...

[19] STOP / / The end position of the bytecode before editing That is, the instruction corresponding to the first bytecode includes pushing "aaaaa" into the stack.

[0078] If the second byte code is 0x62bbbbbbbb...00, the instruction corresponding to the second byte code is:

[00] PUSH4bbbbbbbb ...

[20] STOP / / The end position of the bytecode before editing That is, the instruction corresponding to the second bytecode includes pushing "bbbbbbbb" into the stack.

[0079] It can be seen that the length of the second bytecode is longer than that of the first bytecode, and the edited bytecode in the second bytecode is "62bbbbbbbb", and the instruction corresponding to the edited bytecode is "

[00] PUSH4bbbbbbbb". Based on this, in order to obtain the target bytecode, the edited bytecode "62bbbbbbbb" in the second bytecode can be replaced with the bytecode corresponding to the first jump instruction. For example, the bytecode corresponding to the first jump instruction can be "6020565b", and the first jump instruction can be "

[00] PUSH120

[02] JUMP

[03] JUMPDEST", which can be used to jump to the end position of the target bytecode.

[0080] In addition to replacing the edited bytecode "62bbbbbbbb" in the second bytecode with the bytecode corresponding to the first jump instruction, the edited bytecode "62bbbbbbbb" in the second bytecode can also be added to the target position corresponding to the first jump instruction. When the first jump instruction is used to jump to the end position of the target bytecode, the edited bytecode "62bbbbbbbb" can be added to the end position of the target bytecode.

[0081] In addition, after the edited bytecode "62bbbbbbbb" in the second bytecode is added to the target position corresponding to the first jump instruction, the bytecode corresponding to the second jump instruction may be added after the edited bytecode "62bbbbbbbb". For example, the bytecode corresponding to the second jump instruction may be "600356", and the second jump instruction may be "

[26] PUSH103

[28] JUMP", which is used to jump to the initial position of the edited bytecode "62bbbbbbbb".

[0082] After completing the above steps, the target bytecode obtained can be "0x6020565b...0062bbbbbbbb600356", and the instructions corresponding to the target bytecode are:

[00] PUSH120

[02] JUMP

[03] JUMPDEST / / Step 1. The original push3 aaaaaa instruction is replaced by the first jump instruction: push120 jump jumpdest. ....

[19] STOP / / The end position of the bytecode before editing

[20] JUMPDEST / / Step 2. The target position corresponding to the first jump instruction is 20, which is the end position before editing. Add the instruction corresponding to the edited bytecode at the end, i.e. push4 bbbbbbbb

[21] PUSH4bbbbbbbb

[26] PUSH103 / / Step 3. Add a second jump instruction for jumping to the initial position of the edited bytecode, that is, position 03, supplemented by the instruction push1 03 jump

[28] JUMP By comparing the second bytecode with the target bytecode, it can be seen that the edited bytecode "62bbbbbbbb" in the second bytecode is replaced by the bytecode "6020565b" corresponding to the first jump instruction, and the edited bytecode "62bbbbbbbb" is added to the end position of the target bytecode, and the bytecode "600356" corresponding to the second jump instruction is added after the edited bytecode "62bbbbbbbb".

[0084] It can be seen from practical applications that after adjusting the second bytecode in step h, instruction offset can be avoided.

[0085] It can be seen from the above that in the bytecode editing method provided in the embodiment of the present application, the first bytecode to be edited is first obtained, and then the first bytecode is edited to obtain the second bytecode, and then according to the first bytecode and the second bytecode, it is determined whether the bytecode length has changed. If it is determined that the bytecode length has changed, the second bytecode is adjusted to obtain the target bytecode, and the target bytecode is output. Through the editing method of the present application, after the bytecode is edited, the edited bytecode can be automatically checked and adjusted by the electronic device. Compared with the existing method that requires manual verification and adjustment of the edited bytecode, the method of the present application improves the editing efficiency of the bytecode editing method.

[0086] Based on the bytecode editing method provided in the above embodiment, the present application further provides a bytecode editing device for implementing the above method embodiment, please refer to Figure 2 , Figure 2 A schematic diagram of the structure of a bytecode editing device provided in an embodiment of the present application. Figure 2 As shown, the bytecode editing device 20 may include: a bytecode acquisition unit 21, a bytecode editing unit 22, a length change determination unit 23, and a bytecode adjustment unit 24. Among them: The bytecode acquiring unit 21 is used to acquire the first bytecode to be edited.

[0087] The bytecode editing unit 22 is used for editing the first bytecode to obtain a second bytecode.

[0088] The length change determining unit 23 is used to determine whether the bytecode length has changed according to the first bytecode and the second bytecode.

[0089] The bytecode adjustment unit 24 is used to adjust the second bytecode to obtain a target bytecode if it is determined that the bytecode length has changed, and output the target bytecode.

[0090] Optionally, the length change determining unit 23 is specifically configured to: Determine whether the elements in the stack are balanced according to the first bytecode and the second bytecode; If it is determined that the elements in the stack are balanced, it is determined whether the bytecode length changes according to the first bytecode and the second bytecode.

[0091] Optionally, the length change determining unit 23 is specifically configured to: Determine a first instruction within a specified line number range from all instructions corresponding to the first bytecode, and determine a change in the number of elements in the stack caused by the first instruction; Determine a second instruction within a specified line number range from all instructions corresponding to the second bytecode, and determine a change in the number of elements in the stack caused by the second instruction; Whether the elements in the stack are balanced is determined according to the change in the number of elements in the stack caused by the first instruction and the change in the number of elements in the stack caused by the second instruction.

[0092] Optionally, the length change determining unit 23 is specifically configured to: Determine, according to the type of each instruction among all the instructions corresponding to the first bytecode, the length of each instruction among all the instructions corresponding to the first bytecode; Determine the length of each instruction among all the instructions corresponding to the second bytecode according to the type of each instruction among all the instructions corresponding to the second bytecode; Determine whether the bytecode length changes according to the length of each instruction among all instructions corresponding to the first bytecode and the length of each instruction among all instructions corresponding to the second bytecode.

[0093] Optionally, the bytecode editing device 20 may further include a bytecode output unit. The bytecode output unit is used to output the second bytecode if it is determined that the bytecode length has not changed.

[0094] Optionally, the bytecode adjustment unit 24 is specifically used for: If the length of the second bytecode is shorter than the length of the first bytecode, a preset bytecode is filled at a specified position in the second bytecode so that the adjusted length of the second bytecode is equal to the length of the first bytecode.

[0095] Optionally, the bytecode adjustment unit 24 is specifically used for: If the length of the second bytecode is longer than that of the first bytecode, the edited bytecode in the second bytecode is replaced with the bytecode corresponding to the first jump instruction, and the edited bytecode is added to the target position corresponding to the first jump instruction, and the bytecode corresponding to the second jump instruction is added after the edited bytecode; wherein the first jump instruction is used to jump to the target position, and the second jump instruction is used to jump to the initial position of the edited bytecode.

[0096] See also Figure 3 , Figure 3 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 3 As shown, the electronic device 3 provided in this embodiment may include: a processor 30, a memory 31, and a computer program 32 stored in the memory 31 and executable on the processor 30, such as a program corresponding to the bytecode editing method. When the processor 30 executes the computer program 32, the steps in the above-mentioned embodiment of the bytecode editing method are implemented, such as Figure 1 Alternatively, when the processor 30 executes the computer program 32, the functions of each module / unit in the above-mentioned bytecode editing device embodiment are realized, for example Figure 2 The functions of units 21 to 24 are shown.

[0097] Exemplarily, the computer program 32 may be divided into one or more modules / units, one or more modules / units are stored in the memory 31 and executed by the processor 30 to complete the present application. The one or more modules / units may be a series of computer program instruction segments that can complete specific functions, and the instruction segments are used to describe the execution process of the computer program 32 in the electronic device 3. For example, the computer program 32 may be divided into a bytecode acquisition unit 21, a bytecode editing unit 22, a length change determination unit 23, and a bytecode adjustment unit 24. For the specific functions of each unit, please refer to Figure 2 The relevant descriptions in the corresponding embodiments are not repeated here.

[0098] Those skilled in the art will understand that Figure 3 This is only an example of the electronic device 3 and does not constitute a limitation on the electronic device 3 , which may include more or less components than those shown in the figure, or a combination of certain components, or different components.

[0099] The processor 30 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor, etc.

[0100] The memory 31 may be an internal storage unit of the electronic device 3, such as a hard disk or memory of the electronic device 3. The memory 31 may also be an external storage device of the electronic device 3, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, or a flash card, etc., equipped on the electronic device 3. Further, the memory 31 may also include both an internal storage unit and an external storage device of the electronic device 3. The memory 31 is used to store computer programs and other programs and data required by the electronic device. The memory 31 may also be used to temporarily store data that has been output or is to be output.

[0101] Those skilled in the art can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units is used as an example for illustration. In actual applications, the above-mentioned functions can be assigned to different functional units as needed, that is, the internal structure of the bytecode editing device can be divided into different functional units to complete all or part of the functions described above. The functional units in the embodiment can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0102] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments can be implemented.

[0103] An embodiment of the present application provides a computer program product. When the computer program product is executed on a terminal device, the terminal device implements the steps in the above-mentioned various method embodiments.

[0104] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0105] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0106] The embodiments described above are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.

Claims

1. A bytecode editing method, characterized in that: include: Get the first bytecode to be edited; Editing the first bytecode to obtain a second bytecode; Determining whether a bytecode length changes according to the first bytecode and the second bytecode; If it is determined that the bytecode length has changed, the second bytecode is adjusted to obtain a target bytecode, and the target bytecode is output.

2. The method according to claim 1, characterized in that The determining, according to the first bytecode and the second bytecode, whether the bytecode length changes includes: Determine whether the elements in the stack are balanced according to the first bytecode and the second bytecode; If it is determined that the elements in the stack are balanced, it is determined whether the bytecode length changes according to the first bytecode and the second bytecode.

3. The method according to claim 2, characterized in that The step of determining whether the elements in the stack are balanced according to the first bytecode and the second bytecode includes: Determine a first instruction within a specified line number range from all instructions corresponding to the first bytecode, and determine a change in the number of elements in the stack caused by the first instruction; Determine a second instruction within a specified line number range from all instructions corresponding to the second bytecode, and determine a change in the number of elements in the stack caused by the second instruction; Whether the elements in the stack are balanced is determined according to the change in the number of elements in the stack caused by the first instruction and the change in the number of elements in the stack caused by the second instruction.

4. The method according to claim 2, characterized in that: The determining, according to the first bytecode and the second bytecode, whether the bytecode length changes includes: Determine, according to the type of each instruction among all the instructions corresponding to the first bytecode, the length of each instruction among all the instructions corresponding to the first bytecode; Determine the length of each instruction among all the instructions corresponding to the second bytecode according to the type of each instruction among all the instructions corresponding to the second bytecode; Determine whether the bytecode length changes according to the length of each instruction among all instructions corresponding to the first bytecode and the length of each instruction among all instructions corresponding to the second bytecode.

5. The method according to claim 1, characterized in that After determining whether the bytecode length has changed according to the first bytecode and the second bytecode, the method further includes: If it is determined that the bytecode length has not changed, the second bytecode is output.

6. The method according to any one of claims 1 to 5, characterized in that: If it is determined that the bytecode length has changed, adjusting the second bytecode to obtain a target bytecode includes: If the length of the second bytecode is shorter than the length of the first bytecode, a preset bytecode is filled at a specified position in the second bytecode so that the adjusted length of the second bytecode is equal to the length of the first bytecode.

7. The method according to any one of claims 1 to 5, characterized in that: If it is determined that the bytecode length has changed, the second bytecode is adjusted to obtain a target bytecode, and further includes: If the length of the second bytecode is longer than that of the first bytecode, the edited bytecode in the second bytecode is replaced with the bytecode corresponding to the first jump instruction, and the edited bytecode is added to the target position corresponding to the first jump instruction, and the bytecode corresponding to the second jump instruction is added after the edited bytecode; wherein the first jump instruction is used to jump to the target position, and the second jump instruction is used to jump to the initial position of the edited bytecode.

8. A bytecode editing device, characterized in that: include: A bytecode acquisition unit, used for acquiring a first bytecode to be edited; A bytecode editing unit, used for editing the first bytecode to obtain a second bytecode; a length change determining unit, configured to determine whether the length of a bytecode changes according to the first bytecode and the second bytecode; The bytecode adjustment unit is used to adjust the second bytecode to obtain a target bytecode if it is determined that the bytecode length has changed, and output the target bytecode.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, each step of the bytecode editing method according to any one of claims 1 to 7 is implemented.

10. A computer program product, characterized in that When the computer program product is executed by a processor, each step of the bytecode editing method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Improvement in blackboards

    US123456A

  • Data analysis method and device, storage medium and electronic device

    CN111859860A

  • Byte code execution method, byte code execution device and terminal equipment

    CN112506569A

  • High-availability intelligent contract byte code rewriting method, medium and device

    CN115718922A

  • Method and device for executing smart contract, electronic equipment and storage medium

    CN116107578A