CN-122019340-A - Binary code instrumentation method, system, device, storage medium, and program product
Abstract
The application provides a binary code instrumentation method, a binary code instrumentation system, a binary code instrumentation device, a binary code instrumentation storage medium and a binary code instrumentation program product, wherein the binary code instrumentation method can be applied to a combined processing device. The combined processing device comprises a computing device, an interface device, a processing device and a storage device. The computing device is configured to perform dynamic binary instrumentation tasks, and is mainly implemented as a single-core intelligent processor or a multi-core intelligent processor, and is used for performing deep learning or machine learning computation, and the computing device can interact with the processing device through the interface device so as to jointly complete the dynamic binary instrumentation tasks. The interface device is used for transmitting data and control instructions between the computing device and the processing device. The processing device, as a general purpose processing device, performs basic controls including, but not limited to, data handling, starting and/or stopping of the computing device, and the like. The storage device is used for storing data to be processed. By the scheme of the application, the flexibility of dynamic binary pile insertion is improved, and the application range is enlarged.
Inventors
- Request for anonymity
Assignees
- 寒武纪(昆山)信息科技有限公司
Dates
- Publication Date
- 20260512
- Application Date
- 20241111
Claims (15)
- 1. A method of instrumentation of binary codes, comprising: acquiring an object code and a springboard function corresponding to the object code; Inserting a jump instruction pointing to the springboard function into the target code to generate a first executable file; linking the first executable file with a preset dynamic library to generate a second executable file, wherein the preset dynamic library comprises implementation information of an insertion function; and according to the implementation information of the insertion function, completing the springboard function in the second executable file, and generating post-instrumentation codes.
- 2. The method of claim 1, wherein the acquiring the object code comprises: in response to the instrumentation task, the object code is hijacked while its module is loaded and prior to relocation.
- 3. The method as recited in claim 1, further comprising: And calling a pre-packaged disassembly interface, and performing disassembly processing on the target code to obtain a disassembly result of the target code.
- 4. The method of claim 3, wherein obtaining the springboard function corresponding to the object code comprises: determining a target instruction corresponding to the pile inserting position designated by the pile inserting task according to the disassembly result; And determining the identification information of the springboard function according to the target instruction, the program counter where the instrumentation position is located and the insertion function specified by the instrumentation task, wherein the springboard function at least comprises a call to the insertion function.
- 5. The method of claim 3, wherein inserting a jump instruction into the object code that points to the springboard function generates a first executable file, comprising: determining a target instruction corresponding to the pile inserting position designated by the pile inserting task according to the disassembly result of the target code; Judging whether the target instruction is a preset type instruction or not; If the target instruction is not the preset type instruction, replacing the target instruction with a calling instruction of the springboard function in the target code, and generating the first executable file, wherein the springboard function at least comprises related instructions for executing the target instruction and the calling of the insertion function; and if the target instruction is the preset type instruction, inserting a jump instruction pointing to the springboard function into a basic block to which the target instruction belongs according to the disassembly result of the target code, and generating the first executable file.
- 6. The method according to claim 5, wherein the inserting a jump instruction pointing to the jump board function into a basic block to which the target instruction belongs according to the disassembly result of the target code, generating the first executable file, includes: determining the mapping relation between the instruction and the basic block in the target code according to the disassembly result of the target code; Determining a target basic block to which the target instruction belongs according to the mapping relation; determining a target instruction list corresponding to the target basic block according to the mapping relation; inserting a jump instruction pointing to the springboard function into the target instruction list to obtain a modified instruction list of the target basic block; traversing the basic block list in the target code, updating the program counter corresponding to the corresponding basic block according to the modified instruction list corresponding to each basic block, and generating the first executable file.
- 7. The method of claim 6, further comprising, after said traversing the list of basic blocks in the object code, updating the program counter corresponding to the corresponding basic block according to the modified instruction list corresponding to each basic block: Traversing the basic block list in the target code, and recalculating the corresponding instruction offset according to the updated program counter for the basic block ending with the jump instruction based on the instruction offset.
- 8. The method according to any one of claims 5-7, wherein if the target instruction is the preset type instruction, inserting a jump instruction pointing to the springboard function in the basic block to which the target instruction belongs according to the disassembly result of the target code, and generating the first executable file, further comprises: And if the equipment side has allocated the memory for the target code, releasing the allocated memory, and reallocating the memory according to the code after the instrumentation.
- 9. The method according to any one of claims 1-8, wherein the complementing the springboard function in the second executable file according to implementation information of the insertion function, generating post-instrumentation code, comprises: determining a physical register set used by the insertion function according to the implementation information of the insertion function; Generating a save code and a restore code according to a set of physical registers used by the insertion function; And completing the springboard function in the second executable file according to the saved code and the recovery code, and generating the post-instrumentation code.
- 10. The method of claim 9, wherein the generating save code and restore code from the set of physical registers used by the insertion function further comprises: performing active analysis on the physical register set; determining a target physical register used at a stake-inserting position designated by a stake-inserting task from the physical register set according to an activity analysis result; and generating a save code and a restore code for the target physical register according to the implementation information of the insertion function.
- 11. A stake frame system for binary code, comprising: a driving middle layer for acquiring an object code; the code generator is used for calling a prepackaged instrumentation interface to obtain a springboard function corresponding to the target code, inserting a jump instruction pointing to the springboard function into the target code to generate a first executable file, linking the first executable file with a preset dynamic library to generate a second executable file, wherein the preset dynamic library comprises implementation information of an insertion function, and completing the springboard function in the second executable file according to the implementation information of the insertion function to generate an instrumented code.
- 12. The system of claim 11, further comprising: and the disassembler is used for calling a pre-packaged disassembler interface, performing disassembler processing on the target code to obtain a disassembler result of the target code, so that the code generator generates the post-instrumentation code according to the disassembler result.
- 13. An electronic device, comprising: at least one processor, and A memory communicatively coupled to the at least one processor; Wherein the memory stores instructions executable by the at least one processor to cause the electronic device to perform the method of any one of claims 1-10.
- 14. A computer readable storage medium having stored therein computer executable instructions which, when executed by a processor, implement the method of any of claims 1-10.
- 15. A computer program product comprising a computer program which, when executed by a processor, implements the method according to any of claims 1-10.
Description
Binary code instrumentation method, system, device, storage medium, and program product Technical Field The present application relates to the field of computer technology, and in particular, to a binary code instrumentation method, system, device, storage medium, and program product. Background Dynamic binary instrumentation (Dynamic Binary Instrumentation, abbreviated as "DBI") is a technique for modifying and analyzing the binary code of a program as it runs. The DBI tool may insert custom code to monitor, analyze, or modify the behavior of the program without the need for source code. The technology has wide application in the fields of performance analysis, security detection, debugging, program optimization and the like. Dynamic binary instrumentation is commonly applied to CPU (Central Processing Unit ) or GPU (Graphics Processing Unit, graphics processor) programs, often used in the scenarios of performance analysis, debugging, etc. of the programs. The current dynamic binary pile inserting mode is high in memory overhead, high in software and hardware requirements of equipment and poor in applicability. Disclosure of Invention The embodiment of the application provides a binary code instrumentation method, a binary code instrumentation system, binary code instrumentation equipment, a binary code instrumentation storage medium and a binary code instrumentation program product, which are used for completing instrumentation by automatically selecting implementation information used by an insertion function during linking, so that memory overhead is reduced, the binary code instrumentation method, the binary code instrumentation system and the binary code instrumentation device can be suitable for a processor software stack without a dynamic library loading mechanism, requirements on the software stack are reduced, and the application range is enlarged. The application provides a method for inserting a binary code, which comprises the steps of obtaining an object code and a springboard function corresponding to the object code, inserting a jump instruction pointing to the springboard function into the object code to generate a first executable file, linking the first executable file with a preset dynamic library to generate a second executable file, wherein the preset dynamic library comprises implementation information of an insertion function, and completing the springboard function in the second executable file according to the implementation information of the insertion function to generate a code after inserting the binary code. In a second aspect, the present application provides a dynamic binary instrumentation device comprising: the acquisition module is used for acquiring the target code and the springboard function corresponding to the target code; the first generation module is used for inserting a jump instruction pointing to the springboard function into the target code to generate a first executable file; The second generation module is used for linking the first executable file with a preset dynamic library to generate a second executable file, wherein the preset dynamic library comprises implementation information of an insertion function; And the third generation module is used for complementing the springboard function in the second executable file according to the implementation information of the insertion function and generating a post-instrumentation code. In a third aspect, the present application provides a instrumentation framework system for binary code, comprising: a driving middle layer for acquiring an object code; the code generator is used for calling a prepackaged instrumentation interface to obtain a springboard function corresponding to the target code, inserting a jump instruction pointing to the springboard function into the target code to generate a first executable file, linking the first executable file with a preset dynamic library to generate a second executable file, wherein the preset dynamic library comprises implementation information of an insertion function, and completing the springboard function in the second executable file according to the implementation information of the insertion function to generate an instrumented code. In a fourth aspect, an embodiment of the present application provides an electronic device, including: at least one processor, and A memory communicatively coupled to the at least one processor; wherein the memory stores instructions executable by the at least one processor to cause the electronic device to perform the method of any of the above aspects. In a fifth aspect, an embodiment of the present application provides a computer readable storage medium, where computer executable instructions are stored, and when executed by a processor, implement the method according to any one of the above aspects. In a sixth aspect, embodiments of the present application provide a computer program product comprising a computer program which,