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

Fix #1197: dont display ctx on reg/mem changes #1239

Merged
merged 2 commits into from Oct 6, 2022

Conversation

disconnect3d
Copy link
Member

This commit fixes a bug where we displayed context on registers or memory changes made by the user, so e.g. when user executed one of:

set *rax=1
set *(int*)0x<some address> = 0x1234
set *(unsigned long long*)$rsp+4=0x44444444

It fixes it by just... setting a flag after the context is displayed for the first time and resetting it on a continue GDB event.

There was a previous attempt to fix this bug in #1226 but it was rather a hack than a proper fix. This current commit should be a proper fix :P.

Below is some more explanation of this bug.

The fact that we displayed ctx on regs/mem changes was a result us clearing the cache of the prompt_hook_on_stop function:

 @pwndbg.lib.memoize.reset_on_stop
 def prompt_hook_on_stop(*a):
     pwndbg.commands.context.context()

Where this function is called in prompt_hook, on each prompt display:

def prompt_hook(*a):
    global cur

    new = (gdb.selected_inferior(), gdb.selected_thread())

    if cur != new:
        pwndbg.gdblib.events.after_reload(start=cur is None)
        cur = new

    if pwndbg.proc.alive and pwndbg.proc.thread_is_stopped:
        prompt_hook_on_stop(*a)

So, since we cleared this function cache on each register/memory changes, it resulted in us displaying context on each prompt hook.

So how did we clear this function cache? Through the memoize_on_stop function:

 @pwndbg.gdblib.events.stop
 @pwndbg.gdblib.events.mem_changed
 @pwndbg.gdblib.events.reg_changed
 def memoize_on_stop():
     reset_on_stop._reset()

But why? We need this to make sure that all of the executed commands, when they read memory or registry, get proper new (not cached) values!

So it makes sense to keep reseting the stop caches on mem/reg changed events. Otherwise, we would use incorrect (old) values if user set a register/memory and then used some commands like context or other that depend on register/memory state.

This commit fixes a bug where we displayed context on registers or memory changes made by the user, so e.g. when user executed one of:

```
set *rax=1
set *(int*)0x<some address> = 0x1234
set *(unsigned long long*)$rsp+4=0x44444444
```

It fixes it by just... setting a flag after the context is displayed for
the first time and resetting it on a continue GDB event.

There was a previous attempt to fix this bug in #1226 but it was rather
a hack than a proper fix. This current commit should be a proper fix :P.

Below is some more explanation of this bug.

The fact that we displayed ctx on regs/mem changes was a result us clearing the cache of the `prompt_hook_on_stop` function:

```python
 @pwndbg.lib.memoize.reset_on_stop
 def prompt_hook_on_stop(*a):
     pwndbg.commands.context.context()
```

Where this function is called in `prompt_hook`, on each prompt display:

```python
def prompt_hook(*a):
    global cur

    new = (gdb.selected_inferior(), gdb.selected_thread())

    if cur != new:
        pwndbg.gdblib.events.after_reload(start=cur is None)
        cur = new

    if pwndbg.proc.alive and pwndbg.proc.thread_is_stopped:
        prompt_hook_on_stop(*a)
```

So, since we cleared this function cache on each register/memory changes, it resulted in us displaying context on each prompt hook.

So how did we clear this function cache? Through the `memoize_on_stop` function:

```
 @pwndbg.gdblib.events.stop
 @pwndbg.gdblib.events.mem_changed
 @pwndbg.gdblib.events.reg_changed
 def memoize_on_stop():
     reset_on_stop._reset()
```

But why? We need this to make sure that all of the executed commands, when they read memory or registry, get proper new (not cached) values!

So it makes sense to keep reseting the stop caches on mem/reg changed events. Otherwise, we would use incorrect (old) values if user set a register/memory and then used some commands like `context` or other that depend on register/memory state.
@disconnect3d disconnect3d merged commit e504353 into dev Oct 6, 2022
@disconnect3d disconnect3d deleted the fix-ctx-display-on-setregs-setmem branch October 6, 2022 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant