Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I use content.replac_at to replace the instruction? Can you provide some concrete examples? #13

Open
penq123 opened this issue Dec 17, 2023 · 4 comments

Comments

@penq123
Copy link

penq123 commented Dec 17, 2023

No description provided.

@penq123 penq123 changed the title I noticed this, but I didn't manage to implement it, how should I implement it specifically? How do I use content.replac_at to replace the instruction? Can you provide some concrete examples? Dec 19, 2023
@pranjalsingh008
Copy link

Hey, any update on this?

@pranjalsingh008
Copy link

This is the test program I'm using to replace a instruction at an address with nop

from gtirb_rewriting import *
import gtirb_rewriting.driver
from gtirb_capstone.instructions import GtirbInstructionDecoder
import logging
import gtirb

class ReplaceInstructionPass(Pass):

    def __init__(self):
        self.target_address = 0x118b

    def begin_module(self, module, functions, context):

        decoder = GtirbInstructionDecoder(module.isa)

        for function in functions:
            for block in function.get_all_blocks():
                offset = 0
                for instruction in decoder.get_instructions(block):
                    if instruction.address  == self.target_address:
                        context.replace_at(block, offset, instruction.size,'nop')
                        logging.debug(f"Inserted at {hex(instruction.address)}")

                        return
                    offset += instruction.size

if __name__ == "__main__":

    logging.basicConfig(level=logging.DEBUG)
    gtirb_rewriting.driver.main(ReplaceInstructionPass)

But this keeps giving me the said error:

DEBUG:root:Inserted at 0x118b
DEBUG:gtirb_rewriting:Applying bytes at CodeBlock(uuid=UUID('00a01ffa-3bb7-44c7-a82e-65d78911a645'), size=12, offset=0, decode_mode=CodeBlock.DecodeMode.Default, )+0
DEBUG:gtirb_rewriting:Applying nop at CodeBlock(uuid=UUID('00a01ffa-3bb7-44c7-a82e-65d78911a645'), size=12, offset=0, decode_mode=CodeBlock.DecodeMode.Default, )+0
DEBUG:gtirb_rewriting:  Before:
DEBUG:gtirb_rewriting:	0x118b:	lea	rdi, [rip + 0xe72]
DEBUG:gtirb_rewriting:	# +3: SymAddrConst: .L_2004 + 0
DEBUG:gtirb_rewriting:	0x1192:	call	0x1050
DEBUG:gtirb_rewriting:	# +1: SymAddrConst: FUN_1050 + 0 {SymbolicExpression.Attribute.PLT}
Traceback (most recent call last):
  File "patch.py", line 53, in <module>
    gtirb_rewriting.driver.main(ReplaceInstructionPass)
  File "/usr/local/lib/python3.8/dist-packages/gtirb_rewriting/driver.py", line 350, in main
    _driver_core([entrypoint], False, argv)
  File "/usr/local/lib/python3.8/dist-packages/gtirb_rewriting/driver.py", line 316, in _driver_core
    pass_man.run(ir)
  File "/usr/local/lib/python3.8/dist-packages/gtirb_rewriting/passes.py", line 116, in run
    context.apply()
  File "/usr/local/lib/python3.8/dist-packages/gtirb_rewriting/rewriting.py", line 1115, in apply
    self._apply_modifications(
  File "/usr/local/lib/python3.8/dist-packages/gtirb_rewriting/rewriting.py", line 660, in _apply_modifications
    actual_block, insert_len = self._insert_assembler_result(
  File "/usr/local/lib/python3.8/dist-packages/gtirb_rewriting/rewriting.py", line 529, in _insert_assembler_result
    new_end = _modify_block_insert(
  File "/usr/local/lib/python3.8/dist-packages/gtirb_rewriting/modify.py", line 987, in _modify_block_insert
    assert isinstance(text_section.blocks[0], gtirb.CodeBlock)
AssertionError

I'm not sure what this error is trying to say, the .text has blocks other than a CodeBlock? @jranieri-grammatech

@jranieri-grammatech
Copy link
Collaborator

I think the problem is that you're passing a string instead of a Patch object to replace_at. I'd recommend you use a static type checker like mypy or pyright to catch bugs like this.

This version should work, though I haven't tested it:

from gtirb_rewriting import *
import gtirb_rewriting.driver
from gtirb_capstone.instructions import GtirbInstructionDecoder
import logging
import gtirb

class ReplaceInstructionPass(Pass):

    def __init__(self):
        self.target_address = 0x118b

    def begin_module(self, module, functions, context):

        decoder = GtirbInstructionDecoder(module.isa)

        for function in functions:
            for block in function.get_all_blocks():
                offset = 0
                for instruction in decoder.get_instructions(block):
                    if instruction.address  == self.target_address:
                        context.replace_at(block, offset, instruction.size, Patch.from_function(self.nop_patch))
                        logging.debug(f"Inserted at {hex(instruction.address)}")

                        return
                    offset += instruction.size

    @patch_constraints
    def nop_patch(self, context):
        return "nop"

if __name__ == "__main__":

    logging.basicConfig(level=logging.DEBUG)
    gtirb_rewriting.driver.main(ReplaceInstructionPass)

@pranjalsingh008
Copy link

pranjalsingh008 commented Feb 13, 2025

Thanks! this did work, Is there a better way to communicate about such issues instead of creating multiple github issues? Perhaps a dedicated space where people can ask questions or share doubts related to this tool?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants