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. */
31 # include <sys/mman.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. */
43 volatile int err = errno;
47 static int (* volatile get_errno_func) (void) = get_errno;
52 /* Check that free() preserves errno. */
54 errno = 1789; /* Liberté, égalité, fraternité. */
56 ASSERT_NO_STDIO (get_errno_func () == 1789);
58 { /* Small memory allocations. */
60 void * volatile ptrs[N];
62 for (i = 0; i < N; i++)
63 ptrs[i] = malloc (15);
64 for (i = 0; i < N; i++)
68 ASSERT_NO_STDIO (get_errno_func () == 1789);
72 { /* Medium memory allocations. */
74 void * volatile ptrs[N];
76 for (i = 0; i < N; i++)
77 ptrs[i] = malloc (729);
78 for (i = 0; i < N; i++)
82 ASSERT_NO_STDIO (get_errno_func () == 1789);
86 { /* Large memory allocations. */
88 void * volatile ptrs[N];
90 for (i = 0; i < N; i++)
91 ptrs[i] = malloc (5318153);
92 for (i = 0; i < N; i++)
96 ASSERT_NO_STDIO (get_errno_func () == 1789);
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
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).
111 But do not test it with glibc < 2.15, since that triggers a glibc internal
112 abort: "malloc.c:3551: munmap_chunk: Assertion `ret == 0' failed."
114 #if defined __linux__ && !(__GLIBC__ == 2 && __GLIBC_MINOR__ < 15)
115 if (open ("/proc/sys/vm/max_map_count", O_RDONLY) >= 0)
118 size_t pagesize = getpagesize ();
119 void *firstpage_backup = malloc (pagesize);
120 void *lastpage_backup = malloc (pagesize);
121 /* Allocate a large memory area, as a bumper, so that the MAP_FIXED
122 allocation later will not overwrite parts of the memory areas
123 allocated to ld.so or libc.so. */
124 void *bumper_region =
125 mmap (NULL, 0x1000000, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
126 /* A file descriptor pointing to a regular file. */
127 int fd = open ("test-free", O_RDONLY);
129 if (firstpage_backup != NULL && lastpage_backup != NULL
130 && bumper_region != (void *)(-1)
133 /* Do a large memory allocation. */
134 size_t big_size = 0x1000000;
135 void * volatile ptr = malloc (big_size - 0x100);
136 char *ptr_aligned = (char *) ((uintptr_t) ptr & ~(pagesize - 1));
137 /* This large memory allocation allocated a memory area
138 from ptr_aligned to ptr_aligned + big_size.
139 Enlarge this memory area by adding a page before and a page
141 memcpy (firstpage_backup, ptr_aligned, pagesize);
142 memcpy (lastpage_backup, ptr_aligned + big_size - pagesize, pagesize);
143 if (mmap (ptr_aligned - pagesize, pagesize + big_size + pagesize,
144 PROT_READ | PROT_WRITE,
145 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0)
148 memcpy (ptr_aligned, firstpage_backup, pagesize);
149 memcpy (ptr_aligned + big_size - pagesize, lastpage_backup, pagesize);
151 /* Now add as many mappings as we can.
152 Stop at 65536, in order not to crash the machine (in case the
153 limit has been increased by the system administrator). */
155 for (i = 0; i < 65536; i++)
156 if (mmap (NULL, pagesize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0)
159 /* Now the number of VMAs of this process has hopefully attained
163 /* This call to free() is supposed to call
164 munmap (ptr_aligned, big_size);
165 which increases the number of VMAs by 1, which is supposed
168 ASSERT_NO_STDIO (get_errno_func () == 1789);