Branch
Hash :
abe5d60f
Author :
Date :
2025-09-10T23:03:55
Remove support for IRIX and Solaris <= 7 regarding old mmap. * m4/mmap-anon.m4 (gl_FUNC_MMAP_ANON): Don't define HAVE_MAP_ANONYMOUS. * m4/memchr.m4 (gl_FUNC_MEMCHR): Assume that mmap supports MAP_ANONYMOUS or MAP_ANON. * m4/strncat.m4 (gl_FUNC_STRNCAT): Likewise. * lib/get-rusage-as.c (MAP_FILE): Remove macro. (get_rusage_as_via_setrlimit): Assume that mmap supports MAP_ANONYMOUS or MAP_ANON. * modules/get-rusage-as (Depends-on): Remove 'open'. * lib/pagealign_alloc.c (MAP_FILE): Remove macro. (pagealign_alloc): Assume that mmap supports MAP_ANONYMOUS or MAP_ANON. * modules/pagealign_alloc (Depends-on): Remove 'open'. * lib/vma-iter.c: Remove code for IRIX. (vma_iterate): Assume that mmap supports MAP_ANONYMOUS or MAP_ANON. * lib/vma-iter.h: Remove code for IRIX. * lib/stackvma.c: Remove code for IRIX. (vma_iterate): Assume that mmap supports MAP_ANONYMOUS or MAP_ANON. * lib/stackvma.h: Remove code for IRIX. * tests/mmap-anon-util.h: (zero_fd, map_flags, MAP_FILE): Remove macros. (mmap_zeromap): Assume that mmap supports MAP_ANONYMOUS or MAP_ANON. * tests/zerosize-ptr.h (MAP_FILE): Remove macro. (zerosize_ptr): Assume that mmap supports MAP_ANONYMOUS or MAP_ANON. * tests/jit/test-cache.c (main): Assume that mmap supports MAP_ANONYMOUS or MAP_ANON. * tests/test-sigsegv-catch-segv1.c (main): Assume that mmap supports MAP_ANONYMOUS or MAP_ANON. * tests/test-sigsegv-catch-segv2.c (main): Likewise. * tests/test-sigsegv-catch-stackoverflow2.c (main): Likewise. * tests/test-vma-prot.c: Test HAVE_MMAP instead of HAVE_MAP_ANONYMOUS.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
/* Getter for RLIMIT_AS.
Copyright (C) 2011-2025 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>, 2011.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
/* On Android, when targeting Android 4.4 or older with a GCC toolchain,
prevent a compilation error
"error: call to 'mmap' declared with attribute error: mmap is not
available with _FILE_OFFSET_BITS=64 when using GCC until android-21.
Either raise your minSdkVersion, disable _FILE_OFFSET_BITS=64, or
switch to Clang."
The files that we access in this compilation unit are less than 2 GB
large. */
#if defined __ANDROID__
# undef _FILE_OFFSET_BITS
#endif
/* Specification. */
#include "resource-ext.h"
/* The "address space size" is defined as the total size of the virtual memory
areas of the current process. This includes
- areas belonging to the executable and shared libraries,
- areas allocated by malloc() or mmap(),
- the stack and environment areas,
- gaps and guard pages (mappings with PROT_NONE),
- other system dependent areas, such as vsyscall or vdso on Linux.
There are two ways of retrieving the current address space size:
a) by trying setrlimit with various values and observing whether the
kernel allows additional mmap calls,
b) by using system dependent APIs that allow to iterate over the list
of virtual memory areas.
We don't use the mincore() based approach here, because it would be very
slow when applied to an entire address space, especially on 64-bit
platforms.
We define two functions
get_rusage_as_via_setrlimit(),
get_rusage_as_via_iterator().
Discussion per platform:
Linux:
a) setrlimit with RLIMIT_AS works.
b) The /proc/self/maps file contains a list of the virtual memory areas.
Both methods agree, except that on x86_64 systems, the value of
get_rusage_as_via_iterator() is 4 KB higher than
get_rusage_as_via_setrlimit().
Mac OS X:
a) setrlimit with RLIMIT_AS succeeds but does not really work: The OS
ignores RLIMIT_AS. mmap() of a page always succeeds, therefore
get_rusage_as_via_setrlimit() is always 0.
b) The Mach based API works.
FreeBSD:
a) setrlimit with RLIMIT_AS works.
b) The /proc/self/maps file contains a list of the virtual memory areas.
NetBSD:
a) setrlimit with RLIMIT_AS works.
b) The /proc/self/maps file contains a list of the virtual memory areas.
Both methods agree,
OpenBSD:
a) setrlimit exists, but RLIMIT_AS is not defined.
b) mquery() can be used to find out about the virtual memory areas.
AIX:
a) setrlimit with RLIMIT_AS succeeds but does not really work: The OS
apparently ignores RLIMIT_AS. mmap() of a page always succeeds,
therefore get_rusage_as_via_setrlimit() is always 0.
b) The /proc/$pid/map file contains a list of the virtual memory areas.
HP-UX:
a) setrlimit with RLIMIT_AS works.
b) pstat_getprocvm() can be used to find out about the virtual memory
areas.
Both methods agree, except that the value of get_rusage_as_via_iterator()
is slightly larger higher than get_rusage_as_via_setrlimit(), by 4 KB in
32-bit mode and by 40 KB in 64-bit mode.
Solaris:
a) setrlimit with RLIMIT_AS works.
b) The /proc/$pid file supports ioctls PIOCNMAP and PIOCMAP, and the
/proc/self/maps file contains a list of the virtual memory areas.
Both methods agree,
Cygwin:
a) setrlimit with RLIMIT_AS always fails when the limit is < 0x80000000.
get_rusage_as_via_setrlimit() therefore produces a wrong value.
b) The /proc/$pid/maps file lists only the memory areas belonging to
the executable and shared libraries, not the anonymous memory.
But the native Windows API works.
mingw:
a) There is no setrlimit function.
b) The native Windows API works.
BeOS, Haiku:
a) On BeOS, there is no setrlimit function.
On Haiku, setrlimit exists. RLIMIT_AS is defined but setrlimit fails.
b) There is a specific BeOS API: get_next_area_info().
*/
#include <errno.h> /* errno */
#include <stdlib.h> /* size_t, abort */
#include <fcntl.h> /* open, O_RDONLY */
#include <unistd.h> /* getpagesize, read, close */
/* System support for get_rusage_as_via_setrlimit(). */
#if HAVE_SETRLIMIT
# include <sys/time.h>
# include <sys/resource.h> /* getrlimit, setrlimit */
#endif
/* Test whether mmap() and mprotect() are available.
We don't use HAVE_MMAP, because AC_FUNC_MMAP would not define it on HP-UX.
HAVE_MPROTECT is not enough, because mingw does not have mmap() but has an
mprotect() function in libgcc.a. */
#if HAVE_SYS_MMAN_H && HAVE_MPROTECT
# include <fcntl.h>
# include <sys/types.h>
# include <sys/mman.h> /* mmap, munmap */
#endif
/* System support for get_rusage_as_via_iterator(). */
#include "vma-iter.h"
#if HAVE_SETRLIMIT && defined RLIMIT_AS && HAVE_SYS_MMAN_H && HAVE_MPROTECT && !defined __HAIKU__
static uintptr_t
get_rusage_as_via_setrlimit (void)
{
uintptr_t result;
struct rlimit orig_limit;
/* Record the original limit. */
if (getrlimit (RLIMIT_AS, &orig_limit) < 0)
{
result = 0;
goto done2;
}
if (orig_limit.rlim_max != RLIM_INFINITY
&& (orig_limit.rlim_cur == RLIM_INFINITY
|| orig_limit.rlim_cur > orig_limit.rlim_max))
/* We may not be able to restore the current rlim_cur value.
So bail out. */
{
result = 0;
goto done2;
}
{
/* The granularity is a single page. */
const size_t pagesize = getpagesize ();
uintptr_t low_bound = 0;
uintptr_t high_bound;
for (;;)
{
/* Here we know that the address space size is >= low_bound. */
struct rlimit try_limit;
uintptr_t try_next = 2 * low_bound + pagesize;
if (try_next < low_bound)
/* Overflow. */
try_next = ((uintptr_t) (~ 0) / pagesize) * pagesize;
/* There's no point in trying a value > orig_limit.rlim_max, as
setrlimit would fail anyway. */
if (orig_limit.rlim_max != RLIM_INFINITY
&& orig_limit.rlim_max < try_next)
try_next = orig_limit.rlim_max;
/* Avoid endless loop. */
if (try_next == low_bound)
{
/* try_next could not be increased. */
result = low_bound;
goto done1;
}
try_limit.rlim_max = orig_limit.rlim_max;
try_limit.rlim_cur = try_next;
if (setrlimit (RLIMIT_AS, &try_limit) == 0)
{
/* Allocate a page of memory, to compare the current address space
size with try_limit.rlim_cur. */
void *new_page =
mmap (NULL, pagesize, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (new_page != (void *)(-1))
{
/* The page could be added successfully. Free it. */
if (munmap (new_page, pagesize) < 0)
abort ();
/* We know that the address space size is
< try_limit.rlim_cur. */
high_bound = try_next;
break;
}
else
{
/* We know that the address space size is
>= try_limit.rlim_cur. */
low_bound = try_next;
}
}
else
{
/* Here we expect only EINVAL, not EPERM. */
if (errno != EINVAL)
abort ();
/* We know that the address space size is
>= try_limit.rlim_cur. */
low_bound = try_next;
}
}
/* Here we know that the address space size is
>= low_bound and < high_bound. */
while (high_bound - low_bound > pagesize)
{
struct rlimit try_limit;
uintptr_t try_next =
low_bound + (((high_bound - low_bound) / 2) / pagesize) * pagesize;
/* Here low_bound <= try_next < high_bound. */
try_limit.rlim_max = orig_limit.rlim_max;
try_limit.rlim_cur = try_next;
if (setrlimit (RLIMIT_AS, &try_limit) == 0)
{
/* Allocate a page of memory, to compare the current address space
size with try_limit.rlim_cur. */
void *new_page =
mmap (NULL, pagesize, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (new_page != (void *)(-1))
{
/* The page could be added successfully. Free it. */
if (munmap (new_page, pagesize) < 0)
abort ();
/* We know that the address space size is
< try_limit.rlim_cur. */
high_bound = try_next;
}
else
{
/* We know that the address space size is
>= try_limit.rlim_cur. */
low_bound = try_next;
}
}
else
{
/* Here we expect only EINVAL, not EPERM. */
if (errno != EINVAL)
abort ();
/* We know that the address space size is
>= try_limit.rlim_cur. */
low_bound = try_next;
}
}
result = low_bound;
}
done1:
/* Restore the original rlim_cur value. */
if (setrlimit (RLIMIT_AS, &orig_limit) < 0)
abort ();
done2:
return result;
}
#else
static uintptr_t
get_rusage_as_via_setrlimit (void)
{
return 0;
}
#endif
#if VMA_ITERATE_SUPPORTED
static int
vma_iterate_callback (void *data, uintptr_t start, uintptr_t end,
unsigned int flags)
{
uintptr_t *totalp = (uintptr_t *) data;
*totalp += end - start;
return 0;
}
static uintptr_t
get_rusage_as_via_iterator (void)
{
uintptr_t total = 0;
vma_iterate (vma_iterate_callback, &total);
return total;
}
#else
static uintptr_t
get_rusage_as_via_iterator (void)
{
return 0;
}
#endif
uintptr_t
get_rusage_as (void)
{
#if (defined __APPLE__ && defined __MACH__) || defined _AIX || defined __CYGWIN__ || defined __MVS__ || defined __SANITIZE_THREAD__ /* Mac OS X, AIX, Cygwin, z/OS, gcc -fsanitize=thread */
/* get_rusage_as_via_setrlimit() does not work.
Prefer get_rusage_as_via_iterator(). */
return get_rusage_as_via_iterator ();
#elif HAVE_SETRLIMIT && defined RLIMIT_AS && HAVE_SYS_MMAN_H && HAVE_MPROTECT && !defined __HAIKU__
/* Prefer get_rusage_as_via_setrlimit() if it succeeds,
because the caller may want to use the result with setrlimit(). */
uintptr_t result;
result = get_rusage_as_via_setrlimit ();
if (result == 0)
result = get_rusage_as_via_iterator ();
return result;
#else
return get_rusage_as_via_iterator ();
#endif
}
#ifdef TEST
#include <stdio.h>
int
main ()
{
printf ("Initially: 0x%08lX 0x%08lX 0x%08lX\n",
get_rusage_as_via_setrlimit (), get_rusage_as_via_iterator (),
get_rusage_as ());
malloc (0x88);
printf ("After small malloc: 0x%08lX 0x%08lX 0x%08lX\n",
get_rusage_as_via_setrlimit (), get_rusage_as_via_iterator (),
get_rusage_as ());
malloc (0x8812);
printf ("After medium malloc: 0x%08lX 0x%08lX 0x%08lX\n",
get_rusage_as_via_setrlimit (), get_rusage_as_via_iterator (),
get_rusage_as ());
malloc (0x281237);
printf ("After large malloc: 0x%08lX 0x%08lX 0x%08lX\n",
get_rusage_as_via_setrlimit (), get_rusage_as_via_iterator (),
get_rusage_as ());
return 0;
}
#endif /* TEST */