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).
112 #if defined __linux__
113 if (open ("/proc/sys/vm/max_map_count", O_RDONLY
) >= 0)
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)
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
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)
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). */
153 for (i
= 0; i
< 65536; i
++)
154 if (mmap (NULL
, pagesize
, PROT_READ
, MAP_FILE
| MAP_PRIVATE
, fd
, 0)
157 /* Now the number of VMAs of this process has hopefully attained
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
166 ASSERT_NO_STDIO (get_errno_func () == 1789);