doc: Document version-etc, version-etc, and argp-version-etc.
[gnulib.git] / tests / test-vma-prot.c
blobb081ed86b108fec148f0f053d332ff0d5e6fbda7
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. */
19 #include <config.h>
21 #include "vma-prot.h"
23 #include <stdlib.h>
25 #if HAVE_MAP_ANONYMOUS
26 # include <sys/mman.h>
27 # include <unistd.h>
28 #endif
30 #include "macros.h"
32 int dummy;
34 int
35 f (int x)
37 return x;
40 int
41 main (void)
43 int prot;
45 /* Test on memory allocated through malloc(). */
47 char *mem = malloc (10);
48 prot = get_vma_prot (mem + 2, 5);
49 ASSERT (prot != -1);
50 ASSERT (((VMA_PROT_READ | VMA_PROT_WRITE) & ~prot) == 0);
53 char *mem = malloc (1000000);
54 prot = get_vma_prot (mem, 1000000);
55 ASSERT (prot != -1);
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);
67 ASSERT (prot != -1);
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);
75 ASSERT (prot != -1);
76 ASSERT (prot == 0);
80 #endif
82 /* Test on a global variable. */
83 prot = get_vma_prot (&dummy, sizeof (dummy));
84 ASSERT (prot != -1);
85 ASSERT (((VMA_PROT_READ | VMA_PROT_WRITE) & ~prot) == 0);
87 /* Test on a function. */
88 prot = get_vma_prot (&f, 1);
89 ASSERT (prot != -1);
90 #if (defined __hppa || defined __hppa64__ \
91 || defined __ia64__ \
92 || defined _AIX \
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. */
96 #else
97 /* On these platforms, a function pointer is a pointer to or into some
98 machine instruction. */
99 ASSERT ((VMA_PROT_EXECUTE & ~prot) == 0);
100 #endif
101 #if !defined __OpenBSD__
102 /* Except on OpenBSD/arm64, the machine instructions are also readable. */
103 ASSERT ((VMA_PROT_READ & ~prot) == 0);
104 #endif
106 return test_exit_status;