xref: /openbmc/qemu/tests/tcg/multiarch/prot-none.c (revision 2e1cacfb)
1 /*
2  * Test that GDB can access PROT_NONE pages.
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/mman.h>
10 #include <unistd.h>
11 
12 void break_here(void *q)
13 {
14 }
15 
16 int main(void)
17 {
18     long pagesize = sysconf(_SC_PAGESIZE);
19     void *p, *q;
20     int err;
21 
22     p = mmap(NULL, pagesize * 2, PROT_READ | PROT_WRITE,
23              MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
24     assert(p != MAP_FAILED);
25     q = p + pagesize - 1;
26     strcpy(q, "42");
27 
28     err = mprotect(p, pagesize * 2, PROT_NONE);
29     assert(err == 0);
30 
31     break_here(q);
32 
33     err = mprotect(p, pagesize * 2, PROT_READ);
34     assert(err == 0);
35     if (getenv("PROT_NONE_PY")) {
36         assert(strcmp(q, "24") == 0);
37     }
38 
39     return EXIT_SUCCESS;
40 }
41