Skip to content

Commit

Permalink
added regression for issue #1131
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Nov 8, 2024
1 parent 2ba9ac7 commit 437cbe2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
2 changes: 0 additions & 2 deletions tests/commands/gef_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
`gef_remote` command test module
"""

import random

import pytest

from tests.base import RemoteGefUnitTestGeneric
Expand Down
39 changes: 39 additions & 0 deletions tests/regressions/1131_target_remote_registers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pathlib
import pytest
import os
import tempfile

from tests.base import RemoteGefUnitTestGeneric
from tests.utils import get_random_port, qemuuser_session

URL = "https://github.com/user-attachments/files/16913262/repr.zip"

@pytest.mark.slow
class MissingTargetRemoteRegisters(RemoteGefUnitTestGeneric):
"""@ref https://github.com/hugsy/gef/pull/1131"""

def setUp(self) -> None:
repro_script = f"""
wget -O {{0}}/repr.zip {URL}
unzip {{0}}/repr.zip -d {{0}}
"""

self._tempdir = tempfile.TemporaryDirectory(prefix="gef-tests-")
self._tempdir_path = pathlib.Path(self._tempdir.name)
os.system(repro_script.format(self._tempdir_path))
self._current_dir = self._tempdir_path / "repr"
os.chdir(self._current_dir)
self._target = self._current_dir / "chal"
return super().setUp()

def test_target_remote_validate_post_hook_registers_display(self):
_gdb = self._gdb
_gef = self._gef
port = get_random_port()

# cmd: ./qemu-mipsel-static -g 1234 -L ./target ./chal
with qemuuser_session(exe=self._target, port=port, qemu_exe=self._current_dir / "qemu-mipsel-static", args=["-L", str(self._current_dir / "target")]):
_gdb.execute(f"target remote :{port}")

res = str(_gef.session.remote)
assert f"RemoteSession(target='localhost:{port}', local='/', mode=QEMU_USER)" in res
34 changes: 25 additions & 9 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import pathlib
import platform
import shutil
import struct
import subprocess
import tempfile
Expand All @@ -19,12 +20,10 @@


def which(program: str) -> pathlib.Path:
for path in os.environ["PATH"].split(os.pathsep):
dirname = pathlib.Path(path)
fpath = dirname / program
if os.access(fpath, os.X_OK):
return fpath
raise FileNotFoundError(f"Missing file `{program}`")
fpath = shutil.which(program)
if not fpath:
raise FileNotFoundError(f"Missing file `{program}`")
return pathlib.Path(fpath)


TMPDIR = pathlib.Path(tempfile.gettempdir())
Expand Down Expand Up @@ -162,9 +161,16 @@ def gdbserver_multi_session(
def start_qemuuser(
exe: Union[str, pathlib.Path] = debug_target("default"),
port: int = GDBSERVER_DEFAULT_PORT,
qemu_exe: pathlib.Path = QEMU_USER_X64_BINARY,
args: list[str] | None = None
) -> subprocess.Popen:
cmd = [qemu_exe, "-g", str(port)]
if args:
cmd.extend(args)
cmd.append(exe)
logging.info(f"Starting '{cmd}' in {qemu_exe} on :{port}")
return subprocess.Popen(
[QEMU_USER_X64_BINARY, "-g", str(port), exe],
cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
Expand All @@ -179,9 +185,19 @@ def stop_qemuuser(process: subprocess.Popen) -> None:
@contextlib.contextmanager
def qemuuser_session(*args, **kwargs):
exe = kwargs.get("exe", "") or debug_target("default")
port = kwargs.get("port", 0) or GDBSERVER_DEFAULT_PORT
sess = start_qemuuser(exe, port)
port = kwargs.get("port", GDBSERVER_DEFAULT_PORT)
qemu_exe = kwargs.get("qemu_exe", None) or QEMU_USER_X64_BINARY
args = kwargs.get("args", None)
if args:
# if specified, expect a list of strings
assert isinstance(args, list)
assert len(args)
for arg in args:
assert isinstance(arg, str)

sess = start_qemuuser(exe, port=port, qemu_exe=qemu_exe, args=args)
try:
time.sleep(GDBSERVER_STARTUP_DELAY_SEC)
yield sess
finally:
stop_qemuuser(sess)
Expand Down

0 comments on commit 437cbe2

Please sign in to comment.