1"""Test that GDB can access PROT_NONE pages.
2
3This runs as a sourced script (via -x, via run-test.py).
4
5SPDX-License-Identifier: GPL-2.0-or-later
6"""
7import ctypes
8from test_gdbstub import gdb_exit, main, report
9
10
11def probe_proc_self_mem():
12    buf = ctypes.create_string_buffer(b'aaa')
13    try:
14        with open("/proc/self/mem", "rb") as fp:
15            fp.seek(ctypes.addressof(buf))
16            return fp.read(3) == b'aaa'
17    except OSError:
18        return False
19
20
21def run_test():
22    """Run through the tests one by one"""
23    if not probe_proc_self_mem():
24        print("SKIP: /proc/self/mem is not usable")
25        gdb_exit(0)
26    gdb.Breakpoint("break_here")
27    gdb.execute("continue")
28    val = gdb.parse_and_eval("*(char[2] *)q").string()
29    report(val == "42", "{} == 42".format(val))
30    gdb.execute("set *(char[3] *)q = \"24\"")
31    gdb.execute("continue")
32    exitcode = int(gdb.parse_and_eval("$_exitcode"))
33    report(exitcode == 0, "{} == 0".format(exitcode))
34
35
36main(run_test)
37