spawn: Use special invocation for <spawn.h> on OS/2 kLIBC.
[gnulib.git] / tests / test-free.c
blobe651f1898b0f8d3cee4e2aa468b5ceb9bc2d362a
1 /* Test of free() function.
2 Copyright (C) 2020-2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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>, 2020. */
19 #include <config.h>
21 /* Specification. */
22 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <unistd.h>
27 #if defined __linux__
28 # include <fcntl.h>
29 # include <stdint.h>
30 # include <string.h>
31 # include <sys/mman.h>
32 #endif
34 #include "macros.h"
36 /* The indirection through a volatile function pointer is necessary to prevent
37 a GCC optimization. Without it, when optimizing, GCC would "know" that errno
38 is unchanged by calling free(ptr), when ptr was the result of a malloc(...)
39 call in the same function. */
40 static int
41 get_errno (void)
43 volatile int err = errno;
44 return err;
47 static int (* volatile get_errno_func) (void) = get_errno;
49 int
50 main ()
52 /* Check that free() preserves errno. */
54 errno = 1789; /* Liberté, égalité, fraternité. */
55 free (NULL);
56 ASSERT_NO_STDIO (get_errno_func () == 1789);
58 { /* Small memory allocations. */
59 #define N 10000
60 void * volatile ptrs[N];
61 size_t i;
62 for (i = 0; i < N; i++)
63 ptrs[i] = malloc (15);
64 for (i = 0; i < N; i++)
66 errno = 1789;
67 free (ptrs[i]);
68 ASSERT_NO_STDIO (get_errno_func () == 1789);
70 #undef N
72 { /* Medium memory allocations. */
73 #define N 1000
74 void * volatile ptrs[N];
75 size_t i;
76 for (i = 0; i < N; i++)
77 ptrs[i] = malloc (729);
78 for (i = 0; i < N; i++)
80 errno = 1789;
81 free (ptrs[i]);
82 ASSERT_NO_STDIO (get_errno_func () == 1789);
84 #undef N
86 { /* Large memory allocations. */
87 #define N 10
88 void * volatile ptrs[N];
89 size_t i;
90 for (i = 0; i < N; i++)
91 ptrs[i] = malloc (5318153);
92 for (i = 0; i < N; i++)
94 errno = 1789;
95 free (ptrs[i]);
96 ASSERT_NO_STDIO (get_errno_func () == 1789);
98 #undef N
101 /* Test a less common code path.
102 When malloc() is based on mmap(), free() can sometimes call munmap().
103 munmap() usually succeeds, but fails in a particular situation: when
104 - it has to unmap the middle part of a VMA, and
105 - the number of VMAs of a process is limited and the limit is
106 already reached.
107 The latter condition is fulfilled on Linux, when the file
108 /proc/sys/vm/max_map_count exists. This file contains the limit
109 - for Linux >= 2.4.19: 65536 (DEFAULT_MAX_MAP_COUNT in linux/include/linux/sched.h)
110 - for Linux >= 2.6.31: 65530 (DEFAULT_MAX_MAP_COUNT in linux/include/linux/mm.h).
112 #if defined __linux__
113 if (open ("/proc/sys/vm/max_map_count", O_RDONLY) >= 0)
115 /* Preparations. */
116 size_t pagesize = getpagesize ();
117 void *firstpage_backup = malloc (pagesize);
118 void *lastpage_backup = malloc (pagesize);
119 /* Allocate a large memory area, as a bumper, so that the MAP_FIXED
120 allocation later will not overwrite parts of the memory areas
121 allocated to ld.so or libc.so. */
122 void *bumper_region =
123 mmap (NULL, 0x1000000, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
124 /* A file descriptor pointing to a regular file. */
125 int fd = open ("test-free", O_RDONLY);
127 if (firstpage_backup != NULL && lastpage_backup != NULL
128 && bumper_region != (void *)(-1)
129 && fd >= 0)
131 /* Do a large memory allocation. */
132 size_t big_size = 0x1000000;
133 void * volatile ptr = malloc (big_size - 0x100);
134 char *ptr_aligned = (char *) ((uintptr_t) ptr & ~(pagesize - 1));
135 /* This large memory allocation allocated a memory area
136 from ptr_aligned to ptr_aligned + big_size.
137 Enlarge this memory area by adding a page before and a page
138 after it. */
139 memcpy (firstpage_backup, ptr_aligned, pagesize);
140 memcpy (lastpage_backup, ptr_aligned + big_size - pagesize, pagesize);
141 if (mmap (ptr_aligned - pagesize, pagesize + big_size + pagesize,
142 PROT_READ | PROT_WRITE,
143 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0)
144 != (void *)(-1))
146 memcpy (ptr_aligned, firstpage_backup, pagesize);
147 memcpy (ptr_aligned + big_size - pagesize, lastpage_backup, pagesize);
149 /* Now add as many mappings as we can.
150 Stop at 65536, in order not to crash the machine (in case the
151 limit has been increased by the system administrator). */
152 size_t i;
153 for (i = 0; i < 65536; i++)
154 if (mmap (NULL, pagesize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0)
155 == (void *)(-1))
156 break;
157 /* Now the number of VMAs of this process has hopefully attained
158 its limit. */
160 errno = 1789;
161 /* This call to free() is supposed to call
162 munmap (ptr_aligned, big_size);
163 which increases the number of VMAs by 1, which is supposed
164 to fail. */
165 free (ptr);
166 ASSERT_NO_STDIO (get_errno_func () == 1789);
170 #endif
172 return 0;