182607a73SIlya Leoshkevich"""Test that GDB can access PROT_NONE pages.
282607a73SIlya Leoshkevich
382607a73SIlya LeoshkevichThis runs as a sourced script (via -x, via run-test.py).
482607a73SIlya Leoshkevich
582607a73SIlya LeoshkevichSPDX-License-Identifier: GPL-2.0-or-later
682607a73SIlya Leoshkevich"""
782607a73SIlya Leoshkevichimport ctypes
882607a73SIlya Leoshkevichfrom test_gdbstub import main, report
982607a73SIlya Leoshkevich
1082607a73SIlya Leoshkevich
1182607a73SIlya Leoshkevichdef probe_proc_self_mem():
1282607a73SIlya Leoshkevich    buf = ctypes.create_string_buffer(b'aaa')
1382607a73SIlya Leoshkevich    try:
1482607a73SIlya Leoshkevich        with open("/proc/self/mem", "rb") as fp:
1582607a73SIlya Leoshkevich            fp.seek(ctypes.addressof(buf))
1682607a73SIlya Leoshkevich            return fp.read(3) == b'aaa'
1782607a73SIlya Leoshkevich    except OSError:
1882607a73SIlya Leoshkevich        return False
1982607a73SIlya Leoshkevich
2082607a73SIlya Leoshkevich
2182607a73SIlya Leoshkevichdef run_test():
2282607a73SIlya Leoshkevich    """Run through the tests one by one"""
23*da4038d2SIlya Leoshkevich    if not probe_proc_self_mem():
2482607a73SIlya Leoshkevich        print("SKIP: /proc/self/mem is not usable")
2582607a73SIlya Leoshkevich        exit(0)
2682607a73SIlya Leoshkevich    gdb.Breakpoint("break_here")
2782607a73SIlya Leoshkevich    gdb.execute("continue")
2882607a73SIlya Leoshkevich    val = gdb.parse_and_eval("*(char[2] *)q").string()
2982607a73SIlya Leoshkevich    report(val == "42", "{} == 42".format(val))
3082607a73SIlya Leoshkevich    gdb.execute("set *(char[3] *)q = \"24\"")
3182607a73SIlya Leoshkevich    gdb.execute("continue")
3282607a73SIlya Leoshkevich    exitcode = int(gdb.parse_and_eval("$_exitcode"))
3382607a73SIlya Leoshkevich    report(exitcode == 0, "{} == 0".format(exitcode))
3482607a73SIlya Leoshkevich
3582607a73SIlya Leoshkevich
3682607a73SIlya Leoshkevichmain(run_test)
37