Bytecode Editing Method, Apparatus, Electronic Device, and Program Product

By automatically checksum adjustment of bytecode length changes, the problem of low bytecode editing efficiency in the prior art is solved, and an efficient bytecode editing process is realized.

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

Patent Information

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

AI Technical Summary

Technical Problem

The existing bytecode editing methods are inefficient and require manual checksum adjustment to avoid stack imbalance and target address offset, resulting in inefficient editing.

Method used

By obtaining the first bytecode to be edited, the element balance and bytecode length change in the stack are automatically checked after editing. If it changes, adjust it and output the target bytecode.

Benefits of technology

It improves the efficiency of automated checksum adjustment of bytecode editing, reduces human intervention, and improves editing efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119396412B_ABST
    Figure CN119396412B_ABST
Patent Text Reader

Abstract

This application is applicable to the field of blockchain technology, and provides a method, apparatus, electronic device, and program product for editing bytecode. The method for editing bytecode includes: obtaining a first bytecode to be edited; editing the first bytecode to obtain a second bytecode; determining whether the length of the bytecode changes according to the first bytecode and the second bytecode; if it is determined that the length of the bytecode changes, then adjusting the second bytecode to obtain a target bytecode, and outputting the target bytecode. Through the editing method of this application, after editing the bytecode, the electronic device can automatically verify and adjust the edited bytecode, improving the editing efficiency of the bytecode editing method.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application belongs to the technical field of blockchain, and in particular, relates to a method, apparatus, electronic device, and program product for editing bytecode. Background Art

[0002] Smart contract is a core technology in the field of blockchain. Usually, developers need to perform steps such as function expansion, requirement optimization, and vulnerability repair on the smart contract to make the smart contract meet the needs of users. Therefore, developers need to edit the bytecode of the smart contract.

[0003] The bytecode of smart contracts is usually difficult to read. Therefore, existing bytecode editing methods usually use disassembly tools to convert bytecode into assembly-level instructions, and then manually modify the bytecode instructions at specified positions according to requirements to obtain the edited bytecode. However, after modifying the bytecode instructions at specified positions, in order to avoid problems such as stack imbalance and target address offset, it is also necessary to manually check and adjust the edited bytecode, which results in low editing efficiency of existing bytecode editing methods. Summary of the Invention

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

[0005] In a first aspect, embodiments of this application provide a method for editing bytecode, including:

[0006] Obtain a first bytecode to be edited;

[0007] Edit the first bytecode to obtain a second bytecode;

[0008] Determine whether the bytecode length has changed according to the first bytecode and the second bytecode;

[0009] If it is determined that the bytecode length has changed, adjust the second bytecode to obtain a target bytecode, and output the target bytecode.

[0010] Optionally, the determining whether the bytecode length has changed according to the first bytecode and the second bytecode includes:

[0011] Determine whether the elements in the stack are balanced according to the first bytecode and the second bytecode;

[0012] If it is determined that the elements in the stack are balanced, determine whether the bytecode length has changed according to the first bytecode and the second bytecode.

[0013] Optionally, the determining whether the elements in the stack are balanced according to the first bytecode and the second bytecode includes:

[0014] Determine the first instruction within a specified line number range from all the instructions corresponding to the first bytecode, and determine the change in the number of elements on the stack caused by the first instruction;

[0015] Determine the second instruction within a specified line number range from all the instructions corresponding to the second bytecode, and determine the change in the number of elements on the stack caused by the second instruction;

[0016] Determine whether the elements on the stack are balanced based on the change in the number of elements on the stack caused by the first instruction and the change in the number of elements on the stack caused by the second instruction.

[0017] Optionally, the determining whether the length of the bytecode has changed according to the first bytecode and the second bytecode includes:

[0018] Determine the length of each instruction in all the instructions corresponding to the first bytecode respectively according to the type of each instruction in all the instructions corresponding to the first bytecode;

[0019] Determine the length of each instruction in all the instructions corresponding to the second bytecode respectively according to the type of each instruction in all the instructions corresponding to the second bytecode;

[0020] Determine whether the length of the bytecode has changed according to the length of each instruction in all the instructions corresponding to the first bytecode and the length of each instruction in all the instructions corresponding to the second bytecode.

[0021] Optionally, after determining whether the length of the bytecode has changed according to the first bytecode and the second bytecode, it further includes:

[0022] If it is determined that the length of the bytecode has not changed, output the second bytecode.

[0023] Optionally, the adjusting the second bytecode to obtain a target bytecode if it is determined that the length of the bytecode has changed includes:

[0024] If the length of the second bytecode is shorter than the length of the first bytecode, fill a preset bytecode at a specified position in the second bytecode so that the length of the adjusted second bytecode is equal to the length of the first bytecode.

[0025] Optionally, the adjusting the second bytecode to obtain a target bytecode if it is determined that the length of the bytecode has changed further includes:

[0026] If the length of the second bytecode is longer than the length of the first bytecode, replace the bytecode to be edited in the second bytecode with the bytecode corresponding to the first jump instruction, add the bytecode to be edited to the target position corresponding to the first jump instruction, and add the bytecode corresponding to the second jump instruction after the bytecode to be edited; 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 bytecode to be edited.

[0027] In a second aspect, an embodiment of the present application provides a bytecode editing device, including:

[0028] A bytecode acquisition unit, configured to acquire a first bytecode to be edited;

[0029] A bytecode editing unit, configured to edit the first bytecode to obtain a second bytecode;

[0030] A length change determination unit, configured to determine whether the bytecode length has changed according to the first bytecode and the second bytecode;

[0031] A bytecode adjustment unit, configured to, if it is determined that the bytecode length has changed, adjust the second bytecode to obtain a target bytecode, and output the target bytecode.

[0032] In a third aspect, an embodiment of the present application provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the steps in the bytecode editing method according to any one of the above first aspects.

[0033] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements the steps in the bytecode editing method according to any one of the above first aspects.

[0034] In a fifth aspect, an embodiment of the present application provides a computer program product. When the computer program product runs on an electronic device, it causes the electronic device to execute the steps in the bytecode editing method according to any one of the above first aspects.

[0035] The bytecode editing method, device, electronic device, and program product provided by the embodiments of the present application have the following beneficial effects:

[0036] In the bytecode editing method provided by the embodiments of the present application, first, the first bytecode to be edited is obtained, then the first bytecode is edited to obtain the second bytecode, and then, based on 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 editing the bytecode, the edited bytecode can be automatically verified and adjusted by an 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

[0037] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following will briefly introduce the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.

[0038] Figure 1 It is a flowchart of the implementation of a bytecode editing method provided by the embodiments of the present application;

[0039] Figure 2 It is a schematic structural diagram of a bytecode editing device provided by the embodiments of the present application;

[0040] Figure 3 It is a schematic structural diagram of an electronic device provided by the embodiments of the present application. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0041] 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, "a plurality" means two or more, and "at least one", "one or more" means one, two or more than two. 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 quantity of the indicated technical features. Thus, the features defined with "first" and "second" may explicitly or implicitly include one or more of such features.

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

[0043] The execution subject of the bytecode editing method provided by the embodiments of the present application can be an electronic device, and this electronic device can execute each step of the bytecode editing method provided by the embodiments of the present application. Specifically, the electronic device can include, but is not limited to, mobile phones, tablet computers, laptop computers, desktop computers, etc.

[0044] The bytecode editing method provided by the embodiments of the present application can be applied to various scenarios where bytecode needs to be edited. Exemplarily, the bytecode editing method provided by the embodiments of the present application can be applied to the scenario of editing the bytecode of smart contracts in the blockchain field. Specifically, the bytecode editing method provided by the embodiments of the present application can be applied to the scenario of editing the bytecode of the Ethereum Virtual Machine (EVM).

[0045] When any bytecode needs to be edited, each step of the bytecode editing method provided by the embodiments of the present application can be executed by an electronic device, so that the edited bytecode can be automatically verified and adjusted by the electronic device, thereby improving the editing efficiency of the bytecode editing method.

[0046] Please refer to Figure 1 , Figure 1 which is a flowchart of the implementation of a bytecode editing method provided by the embodiments of the present application. This bytecode editing method can include S101 to S103, which are described in detail as follows:

[0047] In S101, obtain the first bytecode to be edited.

[0048] In the embodiments of the present application, the electronic device can first obtain the first bytecode to be edited. Exemplarily, the user can input the first bytecode to be edited into the electronic device so that the electronic device can obtain the first bytecode to be edited.

[0049] In practical applications, the first bytecode to be edited can be in hexadecimal form. Exemplarily, the first bytecode to be edited can be 0x601456...5b.

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

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

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

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

[0054] Among them, the functions of the editing instruction input by the user can include but are not limited to deleting instructions, modifying instructions, and adding instructions.

[0055] It should be noted that the second bytecode is the bytecode after preliminary editing. In practical applications, it is also necessary to check and adjust the second bytecode after preliminary editing to avoid problems such as stack imbalance and target address offset. In the existing editing methods, usually, the second bytecode after preliminary editing is checked and adjusted manually, while in the embodiment of the present application, the electronic device can automatically check and adjust the second bytecode after preliminary editing by executing S103 and S104, thereby improving the editing efficiency of the bytecode.

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

[0057] In the embodiment of the present application, after obtaining the second bytecode, the electronic device can first check the second bytecode. Specifically, the electronic device can determine whether the length of the bytecode has changed according to the first bytecode and the second bytecode.

[0058] In a possible implementation, before determining whether the bytecode length has changed based on the first bytecode and the second bytecode, the electronic device may first 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, the electronic device may determine whether the bytecode length has changed according to the first bytecode and the second bytecode; if it is determined that the elements in the stack are unbalanced, the electronic device may output a prompt message for describing the change in the number of elements in the stack.

[0059] The reason for determining whether the elements in the stack are balanced before determining whether the bytecode length has changed is as follows: If the elements in the stack are unbalanced, it may lead to stack overflow, stack underflow, and element offset in the stack. Among them, the stack overflow phenomenon means that the elements in the stack exceed the preset maximum limit of elements (such as 1024), and the stack overflow phenomenon may cause the execution of the corresponding bytecode (i.e., the second bytecode in the embodiments of the present application) to fail; the stack underflow phenomenon may cause an error when taking values from an empty stack; the element offset phenomenon in the stack may cause subsequent logical errors. For example, after the original instruction is executed, the stack state is [3, 1, 2]. Assuming that it will be judged later whether the sum of the first two elements in the stack is equal to the third element (i.e., judging 2 + 1 = 3). If an improper edit causes an extra element to appear in the stack after execution, becoming [3, 1, 2, 4], then the elements taken from the stack during the execution of subsequent instructions will all be offset, causing subsequent logical errors.

[0060] In this implementation, the electronic device can determine whether the elements in the stack are balanced through steps a to c. Details are as follows:

[0061] In step a, determine the first instruction within a specified line number range from all the instructions corresponding to the first bytecode, and determine the change in the number of elements in the stack caused by the first instruction.

[0062] In this implementation, the electronic device may first determine the instructions corresponding to the first bytecode, then determine the first instruction within a specified line number range from all the instructions corresponding to the first bytecode. After that, for each first instruction, the electronic device can simulate the operation of the first instruction on the stack 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.

[0063] Exemplarily, when the type of a certain first instruction is a PUSH instruction, the operation of the electronic device to simulate the PUSH instruction on the stack is "pushing a certain value onto the stack". After simulating the operation of "pushing a certain value onto the stack", the electronic device can obtain that the change in the number of elements in the stack caused by the PUSH instruction is that one element is added to the stack.

[0064] Exemplarily, when the type of a certain first instruction is an ADD instruction, the operation of the electronic device simulating the ADD instruction on the stack is "taking two values from the stack and pushing one value onto the stack". After simulating the operation of "taking two values from the stack and pushing one value onto 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 decrease of one element in the stack.

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

[0066] In this implementation, the electronic device can first determine the instructions corresponding to the second bytecode, and then determine the second instruction within the specified line number range from all the instructions corresponding to the second bytecode. After that, for each second instruction, the electronic device can simulate 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, and further can determine the change in the number of elements in the stack caused by each second instruction.

[0067] It should be noted that the specified line number range corresponding to the second instruction and the instruction line number range corresponding to the first instruction can be the same or different, and both 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 no limitation is made here.

[0068] An example of the electronic device simulating the operation of the second instruction on the stack according to the type of the second instruction to determine the change in the number of elements in the stack caused by the second instruction can refer to the example corresponding to step a, and will not be elaborated here.

[0069] In step c, determine 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.

[0070] 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. After that, 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 to determine whether the elements in the stack are balanced.

[0071] Specifically, if the electronic device determines that the number of elements in the stack changed by the first instruction is the same as that changed by the second instruction, it can determine that the elements in the stack are balanced; if the electronic device determines that the number of elements in the stack changed by the first instruction is different from that changed by the second instruction, it can determine that the elements in the stack are unbalanced.

[0072] The above describes the 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 a prompt message for describing the change in the number of elements in the stack. Based on this, the following continues to describe the implementation method for determining whether the bytecode length has changed.

[0073] In a possible implementation, the electronic device can determine whether the bytecode length has changed through steps d to f. Details are as follows:

[0074] In step d, according to the type of each instruction in all the instructions corresponding to the first bytecode, the length of each instruction in all the instructions corresponding to the first bytecode is determined respectively.

[0075] In this implementation, after determining that the elements in the stack are balanced, the electronic device can determine the type of each instruction in all the instructions corresponding to the first bytecode. After that, the electronic device can determine the length of each instruction in all the instructions corresponding to the first bytecode according to the type of each instruction in all the instructions corresponding to the first bytecode.

[0076] Exemplarily, if the type of instruction a corresponding to the first bytecode is a PUSHn instruction, it can be determined that the length of instruction a is 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 normal instruction (an instruction other than the PUSHn instruction), it can be determined that the length of instruction b is 1.

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

[0078] In this implementation, after determining that the elements in the stack are balanced, the electronic device can determine the type of each instruction in all the instructions corresponding to the second bytecode. After that, the electronic device can determine the length of each instruction in all the instructions corresponding to the second bytecode according to the type of each instruction in all the instructions corresponding to the second bytecode.

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

[0080] In step f, according to the length of each instruction among all the instructions corresponding to the first bytecode and the length of each instruction among all the instructions corresponding to the second bytecode, it is determined whether the bytecode length has changed.

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

[0082] Specifically, the electronic device can determine the total length of all the instructions corresponding to the first bytecode (i.e., the length of the first bytecode) according to the length of each instruction among all the instructions corresponding to the first bytecode. Similarly, the electronic device can determine the total length of all the instructions corresponding to the second bytecode (i.e., the length of the second bytecode) according to the length of each instruction among all the instructions corresponding to the second bytecode. After that, the electronic device can compare the total length of all the instructions corresponding to the first bytecode with the total length of all the instructions corresponding to the second bytecode. If the total length of all the instructions corresponding to the first bytecode is the same as the total length of all the instructions corresponding to the second bytecode, the electronic device can determine that the bytecode length has not changed. If the total length of all the instructions corresponding to the first bytecode is different from the total length of all the instructions corresponding to the second bytecode, the electronic device can determine that the bytecode length has changed.

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

[0084] In S104, 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.

[0085] In an 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 may adjust the second bytecode to obtain the target bytecode and output the target bytecode. Among them, the target bytecode can be considered as the bytecode that has been edited and does not need to be adjusted further.

[0086] In a possible implementation, the electronic device may first compare the length of the first bytecode and the length of the second bytecode, and then determine the specific method for adjusting the second bytecode according to the comparison result of the length of the first bytecode and the length of the second bytecode.

[0087] In this implementation, the electronic device may adjust the second bytecode through step g and step h. Details are as follows:

[0088] In step g, if the length of the second bytecode is shorter than the length of the first bytecode, fill the specified position in the second bytecode with the preset bytecode so that the length of the adjusted second bytecode is equal to the length of the first bytecode.

[0089] 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 may find the specified position in the second bytecode and fill the specified position in the second bytecode with the target number of preset bytecodes so that the length of the adjusted second bytecode is equal to the length of the first bytecode.

[0090] Among them, exemplarily, the specified position can be determined in the following way: The electronic device may determine the target instruction that causes the length of the second bytecode to be shorter than the length of the first bytecode among all the instructions corresponding to the second bytecode, and determine the position of the target instruction as the specified position.

[0091] Among them, exemplarily, the preset bytecode may be a meaningless bytecode, that is, the instruction corresponding to the preset bytecode may be a meaningless instruction.

[0092] Among them, exemplarily, the target number can be determined in the following way: The electronic device may first determine the difference between the length of the second bytecode and the length of the first bytecode, and then determine the target number of the preset bytecodes corresponding to the difference according to the difference between the length of the second bytecode and the length of the first bytecode.

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

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

[0095]

[00] PUSH3 123456

[0096] That is, the instruction corresponding to the first bytecode is to push "123456" onto the stack.

[0097] If the second bytecode is 0x614321, the instruction corresponding to the second bytecode is:

[0098]

[00] PUSH2 4321

[0099] That is, the instruction corresponding to the second bytecode is to push "4321" onto the stack.

[0100] It can be seen that the length of the second bytecode is shorter than that of the first bytecode. Then, the electronic device can adjust the second bytecode, and the adjusted second bytecode is 0x6143215b. The instruction corresponding to the adjusted second bytecode is:

[0101]

[00] PUSH2 4321

[0102]

[03] JUMPDEST / / Automatically fill in this instruction for padding to keep the positions of subsequent instructions unchanged

[0103] 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".

[0104] It can be seen that through step g, the length of the adjusted second bytecode can be made equal to that of the first bytecode, thus avoiding instruction offset.

[0105] 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.

[0106] In this implementation, if it is determined that the length of the second bytecode is longer than that 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, where the first jump instruction is used to indicate jumping to the target position. Exemplarily, the target position can be the end position of the bytecode.

[0107] In addition to replacing the edited bytecode in the second bytecode with the bytecode corresponding to the first jump instruction, the electronic device can also add the edited bytecode to the target position corresponding to the first jump instruction. Exemplarily, the electronic device can add the edited bytecode to the end position of the bytecode.

[0108] In addition, the electronic device can also add the bytecode corresponding to the second jump instruction after the edited bytecode. The second jump instruction is used to jump to the initial position of the edited bytecode.

[0109] 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.

[0110] If the first bytecode is 0x62aaaaaa...00, the instruction corresponding to this first bytecode is:

[0111]

[00] PUSH3aaaaaa ...

[0112]

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

[0113] That is, the instruction corresponding to this first bytecode includes pushing "aaaaa" onto the stack.

[0114] If the second bytecode is 0x62bbbbbbbb...00, the instruction corresponding to this second bytecode is:

[0115]

[00] PUSH4bbbbbbbb ...

[0116]

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

[0117] That is, the instruction corresponding to this second bytecode includes pushing "bbbbbbbb" onto the stack.

[0118] 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. Exemplarily, the bytecode corresponding to the first jump instruction can be "6020565b", and the first jump instruction can be "

[00] PUSH120

[02] JUMP

[03] JUMPDEST", and this first jump instruction can be used to jump to the end position of the target bytecode.

[0119] 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.

[0120] Furthermore, after adding the edited bytecode "62bbbbbbbb" in the second bytecode to the target position corresponding to the first jump instruction, the bytecode corresponding to the second jump instruction can also be added after the edited bytecode "62bbbbbbbb". Exemplarily, the bytecode corresponding to the second jump instruction can be "600356", and the second jump instruction can be "

[26] PUSH1 03

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

[0121] The target bytecode obtained after executing the above steps can be "0x6020565b...0062bbbbbbbb600356", and the instructions corresponding to the target bytecode are:

[0122]

[00] PUSH1 20

[0123]

[02] JUMP

[0124]

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

[0126]

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

[0127]

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

[0128]

[21] PUSH4 bbbbbbbb

[0129]

[26] PUSH1 03 / / Step 3. Add the second jump instruction used to jump to the initial position of the edited bytecode, that is, the 03 position, and supplement the instructions push1 03 jump.

[0130]

[28] JUMP

[0131] By comparing the second bytecode with the target bytecode, it can be seen that the edited bytecode "62bbbbbbbb" in the second bytecode is replaced with 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".

[0132] It can be seen from the actual application that after adjusting the second bytecode through step h, instruction offset can be avoided.

[0133] As can be seen above, in the bytecode editing method provided in the embodiments of the present application, first obtain the first bytecode to be edited, then edit the first bytecode to obtain the second bytecode, and then determine whether the bytecode length has changed according to the first bytecode and the second bytecode. If it is determined that the bytecode length has changed, then adjust the second bytecode to obtain the target bytecode and output the target bytecode. Through the editing method of the present application, after editing the bytecode, the edited bytecode can be automatically verified and adjusted by an 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.

[0134] Based on the bytecode editing method provided in the above embodiments, the embodiments of the present application further provide a bytecode editing device for implementing the above method embodiments. Please refer to Figure 2 , Figure 2 which is a schematic structural diagram of a bytecode editing device provided in an embodiment of the present application. As Figure 2 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:

[0135] The bytecode acquisition unit 21 is used to acquire the first bytecode to be edited.

[0136] The bytecode editing unit 22 is used to edit the first bytecode to obtain the second bytecode.

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

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

[0139] Optionally, the length change determination unit 23 is specifically used for:

[0140] Determine whether the elements in the stack are balanced according to the first bytecode and the second bytecode;

[0141] If it is determined that the elements in the stack are balanced, determine whether the length of the bytecode has changed according to the first bytecode and the second bytecode.

[0142] Optionally, the length change determination unit 23 is specifically configured to:

[0143] Determine the first instruction within a specified line number range from all the instructions corresponding to the first bytecode, and determine the change in the number of elements in the stack caused by the first instruction;

[0144] Determine the second instruction within a specified line number range from all the instructions corresponding to the second bytecode, and determine the change in the number of elements in the stack caused by the second instruction;

[0145] Determine 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.

[0146] Optionally, the length change determination unit 23 is specifically configured to:

[0147] Determine the length of each instruction in all the instructions corresponding to the first bytecode respectively according to the type of each instruction in all the instructions corresponding to the first bytecode;

[0148] Determine the length of each instruction in all the instructions corresponding to the second bytecode respectively according to the type of each instruction in all the instructions corresponding to the second bytecode;

[0149] Determine whether the length of the bytecode has changed according to the length of each instruction in all the instructions corresponding to the first bytecode and the length of each instruction in all the instructions corresponding to the second bytecode.

[0150] Optionally, the bytecode editing device 20 may further include a bytecode output unit. Among them:

[0151] The bytecode output unit is used to output the second bytecode if it is determined that the length of the bytecode has not changed.

[0152] Optionally, the bytecode adjustment unit 24 is specifically configured to:

[0153] If the length of the second bytecode is shorter than the length of the first bytecode, fill the specified position in the second bytecode with a preset bytecode so that the length of the adjusted second bytecode is equal to the length of the first bytecode.

[0154] Optionally, the bytecode adjustment unit 24 is specifically configured to:

[0155] If the length of the second bytecode is longer than that of the first bytecode, replace the bytecode to be edited in the second bytecode with the bytecode corresponding to the first jump instruction, add the bytecode to be edited to the target position corresponding to the first jump instruction, and add the bytecode corresponding to the second jump instruction after the bytecode to be edited; 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 bytecode to be edited.

[0156] Please refer to Figure 3 , Figure 3 which is a schematic structural diagram of an electronic device provided by an embodiment of the present application. As Figure 3 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 the S101~S104 shown. Alternatively, when the processor 30 executes the computer program 32, the functions of each module / unit in the above-mentioned embodiment of the bytecode editing device are implemented, such as Figure 2 the functions of the units 21~24 shown.

[0157] 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. One or more modules / units may be a series of computer program instruction segments capable of performing 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, which will not be elaborated here.

[0158] Those skilled in the art can 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. It may include more or fewer components than shown in the figure, or combine some components, or different components.

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

[0160] The memory 31 may be an internal storage unit of the electronic device 3, such as the 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, smart media card (SMC), secure digital (SD) card, or flash card equipped on the electronic device 3, etc. Further, the memory 31 may also include both the internal storage unit and the 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.

[0161] Those skilled in the art can clearly understand that, for the convenience and brevity of description, only the above division of each functional unit is used as an example. In actual applications, the above functions can be allocated to different functional units according to needs, that is, the internal structure of the bytecode editing device is divided into different functional units to complete all or part of the functions described above. Each functional unit in the embodiment may be integrated in a processing unit, or each unit may exist physically alone, or two or more units may be integrated in one unit. The above integrated unit may be implemented in the form of hardware or in the form of a software functional unit. In addition, the specific names of the functional units are only for the convenience of mutual distinction and do not limit the protection scope of the present application. The specific working process of the units in the above system can refer to the corresponding process in the foregoing method embodiments and will not be described in detail here.

[0162] The embodiment of the present application also provides a computer-readable storage medium, in which a computer program is stored, and when the computer program is executed by a processor, the steps in the foregoing method embodiments can be implemented.

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

[0164] In the above embodiments, the descriptions of the various embodiments have their own emphases. For the parts not detailed or recorded in a certain embodiment, reference may be made to the relevant descriptions of other embodiments.

[0165] Those of ordinary skill in the art can realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed herein can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of this application.

[0166] The above-described embodiments 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 foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements on some of the technical features; and these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate 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 method for editing bytecode, characterized in that, Including: Obtain a first bytecode to be edited; Edit the first bytecode to obtain a second bytecode; Determine whether the length of the bytecode changes according to the first bytecode and the second bytecode; If it is determined that the length of the bytecode changes, adjust the second bytecode to obtain a target bytecode, and output the target bytecode; The step of, if it is determined that the length of the bytecode changes, 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, fill a preset bytecode at a specified position in the second bytecode so that the length of the adjusted second bytecode is equal to the length of the first bytecode; The step of, if it is determined that the length of the bytecode changes, adjusting the second bytecode to obtain a target bytecode, further includes: If the length of the second bytecode is longer than the length of the first bytecode, replace the edited bytecode in the second bytecode with the bytecode corresponding to a first jump instruction, add the edited bytecode to the target position corresponding to the first jump instruction, and add the bytecode corresponding to a second jump instruction 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.

2. The method according to claim 1, wherein The step of determining whether the length of the bytecode 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, determine whether the length of the bytecode changes according to the first bytecode and the second bytecode.

3. The method according to claim 2, wherein 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 the instructions corresponding to the first bytecode, and determine the 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 the instructions corresponding to the second bytecode, and determine the change in the number of elements in the stack caused by the second instruction; Determine 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.

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

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

6. An editing device for bytecode, characterized in that, Including: A bytecode acquisition unit for acquiring a first bytecode to be edited; A bytecode editing unit for editing the first bytecode to obtain a second bytecode; A length change determination unit for determining whether the length of the bytecode has changed according to the first bytecode and the second bytecode; A bytecode adjustment unit for, if it is determined that the length of the bytecode has changed, adjusting the second bytecode to obtain a target bytecode and outputting the target bytecode; The bytecode adjustment unit is specifically used for: If the length of the second bytecode is shorter than the length of the first bytecode, filling a preset bytecode at a specified position in the second bytecode so that the length of the adjusted second bytecode is equal to the length of the first bytecode; The bytecode adjustment unit is specifically further used for: If the length of the second bytecode is longer than the length of the first bytecode, replacing the edited bytecode in the second bytecode with the bytecode corresponding to a first jump instruction, adding the edited bytecode to the target position corresponding to the first jump instruction, and adding the bytecode corresponding to a second jump instruction 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.

7. 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, it implements each step in the bytecode editing method according to any one of claims 1 to 5.

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

Citation Information

Patent Citations

  • Improvement in blackboards

    US123456A

  • Smart contract deployment method and device, computing equipment and storage medium

    CN117176406A

  • Bytecode file optimization method and device, bytecode file execution method and device and intelligent card

    CN119225731A