From 99dbb78e34f8bc583bc456a73c9c7ea0a50d7d81 Mon Sep 17 00:00:00 2001
From: DocBot
Each test is a Python function that runs inside of an isolated GDB session. Using a pytest fixture at the beginning of each test, GDB will attach to a binary or connect to a QEMU instance. Each test runs some commands and uses Python assert statements to verify correctness. We can access Pwndbg library code like pwndbg.aglib.regs.rsp as well as execute GDB commands with gdb.execute().
We can take a look at tests/library/gdb/tests/test_symbol.py for an example of a simple test. Looking at a simplified version of the top-level code, we have this:
Each test is a Python function that runs inside of an isolated GDB session. Using a pytest fixture at the beginning of each test, GDB will attach to a binary or connect to a QEMU instance. Each test runs some commands and uses Python assert statements to verify correctness. We can access Pwndbg library code like pwndbg.aglib.regs.sp as well as execute GDB commands with gdb.execute().
We can take a look at tests/library/gdb/tests/test_symbol.py for an example of a simple test. Looking at a simplified version of the top-level code, we have this:
import gdb
import pwndbg
import tests
@@ -25,5 +25,5 @@
pwndbg.config.hexdump_group_width.value = -1
gdb.execute("set hexdump-byte-separator")
- stack_addr = pwndbg.aglib.regs.rsp - 0x100
+ stack_addr = pwndbg.aglib.regs.sp - 0x100
pytest will run any function that starts with test_ as a new test, so there is no need to register your new test anywhere. The start_binary argument is a function that will run the binary you give it, and it will set some common options before starting the binary. Using start_binary is recommended if you don't need any additional customization to GDB settings before starting the binary, but if you do it's fine to not use it.
Our gdb tests run in x86. To debug other architectures, we use QEMU for emulation and attach to its debug port. These tests are located in tests/library/qemu-user/tests. Test creation is identical to our x86 tests - create a Python function with a Pytest fixture name as the parameter (it matches based on the name), and call the argument to start debugging a binary. The qemu_assembly_run fixture takes in a Python string of assembly code, compiles it in the appropriate architecture, and runs it - no need to create an external file or edit a Makefile.