tests/tcg: Add the PROT_NONE gdbstub test

Make sure that qemu gdbstub, like gdbserver, allows reading from and
writing to PROT_NONE pages.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Message-Id: <20240129093410.3151-4-iii@linux.ibm.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Ilya Leoshkevich 2024-01-29 10:32:16 +01:00 committed by Richard Henderson
parent 4d48c1bc07
commit 82607a73f8
3 changed files with 84 additions and 1 deletions

View File

@ -101,13 +101,20 @@ run-gdbstub-registers: sha512
--bin $< --test $(MULTIARCH_SRC)/gdbstub/registers.py, \ --bin $< --test $(MULTIARCH_SRC)/gdbstub/registers.py, \
checking register enumeration) checking register enumeration)
run-gdbstub-prot-none: prot-none
$(call run-test, $@, env PROT_NONE_PY=1 $(GDB_SCRIPT) \
--gdb $(GDB) \
--qemu $(QEMU) --qargs "$(QEMU_OPTS)" \
--bin $< --test $(MULTIARCH_SRC)/gdbstub/prot-none.py, \
accessing PROT_NONE memory)
else else
run-gdbstub-%: run-gdbstub-%:
$(call skip-test, "gdbstub test $*", "need working gdb with $(patsubst -%,,$(TARGET_NAME)) support") $(call skip-test, "gdbstub test $*", "need working gdb with $(patsubst -%,,$(TARGET_NAME)) support")
endif endif
EXTRA_RUNS += run-gdbstub-sha1 run-gdbstub-qxfer-auxv-read \ EXTRA_RUNS += run-gdbstub-sha1 run-gdbstub-qxfer-auxv-read \
run-gdbstub-proc-mappings run-gdbstub-thread-breakpoint \ run-gdbstub-proc-mappings run-gdbstub-thread-breakpoint \
run-gdbstub-registers run-gdbstub-registers run-gdbstub-prot-none
# ARM Compatible Semi Hosting Tests # ARM Compatible Semi Hosting Tests
# #

View File

@ -0,0 +1,36 @@
"""Test that GDB can access PROT_NONE pages.
This runs as a sourced script (via -x, via run-test.py).
SPDX-License-Identifier: GPL-2.0-or-later
"""
import ctypes
from test_gdbstub import main, report
def probe_proc_self_mem():
buf = ctypes.create_string_buffer(b'aaa')
try:
with open("/proc/self/mem", "rb") as fp:
fp.seek(ctypes.addressof(buf))
return fp.read(3) == b'aaa'
except OSError:
return False
def run_test():
"""Run through the tests one by one"""
if not probe_proc_self_mem:
print("SKIP: /proc/self/mem is not usable")
exit(0)
gdb.Breakpoint("break_here")
gdb.execute("continue")
val = gdb.parse_and_eval("*(char[2] *)q").string()
report(val == "42", "{} == 42".format(val))
gdb.execute("set *(char[3] *)q = \"24\"")
gdb.execute("continue")
exitcode = int(gdb.parse_and_eval("$_exitcode"))
report(exitcode == 0, "{} == 0".format(exitcode))
main(run_test)

View File

@ -0,0 +1,40 @@
/*
* Test that GDB can access PROT_NONE pages.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
void break_here(void *q)
{
}
int main(void)
{
long pagesize = sysconf(_SC_PAGESIZE);
void *p, *q;
int err;
p = mmap(NULL, pagesize * 2, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
assert(p != MAP_FAILED);
q = p + pagesize - 1;
strcpy(q, "42");
err = mprotect(p, pagesize * 2, PROT_NONE);
assert(err == 0);
break_here(q);
err = mprotect(p, pagesize * 2, PROT_READ);
assert(err == 0);
if (getenv("PROT_NONE_PY")) {
assert(strcmp(q, "24") == 0);
}
return EXIT_SUCCESS;
}