1 /* Test of getting protection of a virtual memory area.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation, either version 3 of the License,
7 or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2024. */
25 #if HAVE_MAP_ANONYMOUS
26 # include <sys/mman.h>
45 /* Test on memory allocated through malloc(). */
47 char *mem
= malloc (10);
48 prot
= get_vma_prot (mem
+ 2, 5);
50 ASSERT (((VMA_PROT_READ
| VMA_PROT_WRITE
) & ~prot
) == 0);
53 char *mem
= malloc (1000000);
54 prot
= get_vma_prot (mem
, 1000000);
56 ASSERT (((VMA_PROT_READ
| VMA_PROT_WRITE
) & ~prot
) == 0);
59 #if HAVE_MAP_ANONYMOUS
60 /* Test on memory allocated through mmap(). */
62 char *mem
= mmap (NULL
, 1024*1024, PROT_READ
| PROT_WRITE
,
63 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
64 if (mem
!= (char *)(-1))
66 prot
= get_vma_prot (mem
, 1024*1024);
68 ASSERT (prot
== (VMA_PROT_READ
| VMA_PROT_WRITE
));
70 size_t pagesize
= sysconf (_SC_PAGESIZE
);
71 if (pagesize
<= 512*1024
72 && munmap (mem
+ pagesize
, pagesize
) >= 0)
74 prot
= get_vma_prot (mem
, 1024*1024);
82 /* Test on a global variable. */
83 prot
= get_vma_prot (&dummy
, sizeof (dummy
));
85 ASSERT (((VMA_PROT_READ
| VMA_PROT_WRITE
) & ~prot
) == 0);
87 /* Test on a function. */
88 prot
= get_vma_prot (&f
, 1);
90 #if (defined __hppa || defined __hppa64__ \
93 || (defined __powerpc64__ && defined __linux__ && _CALL_ELF != 2))
94 /* On these platforms, a function pointer is actually a pointer to
95 a struct of pointers. */
97 /* On these platforms, a function pointer is a pointer to or into some
98 machine instruction. */
99 ASSERT ((VMA_PROT_EXECUTE
& ~prot
) == 0);
101 #if !defined __OpenBSD__
102 /* Except on OpenBSD/arm64, the machine instructions are also readable. */
103 ASSERT ((VMA_PROT_READ
& ~prot
) == 0);
106 return test_exit_status
;