1/* Test mremap with MREMAP_DONTUNMAP.
2 Copyright (C) 2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <sys/mman.h>
21#include <support/xstdlib.h>
22#include <support/xunistd.h>
23#include <support/check.h>
24#include <support/test-driver.h>
25#include <mremap-failure.h>
26
27static int
28do_test (void)
29{
30 size_t old_size = getpagesize ();
31 size_t new_size = old_size;
32 char *old_addr = xmmap (NULL, length: old_size, PROT_READ | PROT_WRITE,
33 MAP_PRIVATE | MAP_ANONYMOUS, fd: -1);
34 old_addr[0] = 1;
35 old_addr[old_size - 1] = 2;
36
37 /* Create an available 64-page mmap region. */
38 size_t fixed_size = old_size * 64;
39 char *fixed_addr = xmmap (NULL, length: fixed_size, PROT_READ | PROT_WRITE,
40 MAP_PRIVATE | MAP_ANONYMOUS, fd: -1);
41 xmunmap (addr: fixed_addr, length: fixed_size);
42
43 /* Add 3 * pagesize. */
44 fixed_size += 3 * old_size;
45
46 /* Test MREMAP_DONTUNMAP. It should return FIXED_ADDR created above. */
47 char *new_addr = mremap (addr: old_addr, old_len: old_size, new_len: new_size,
48 MREMAP_DONTUNMAP | MREMAP_MAYMOVE,
49 fixed_addr);
50 if (new_addr == MAP_FAILED)
51 return mremap_failure_exit (errno);
52 TEST_VERIFY_EXIT (fixed_addr == new_addr);
53 old_addr[0] = 3;
54 old_addr[old_size - 1] = 4;
55 new_addr[0] = 1;
56 new_addr[new_size - 1] = 2;
57 xmunmap (addr: new_addr, length: new_size);
58 xmunmap (addr: old_addr, length: old_size);
59
60 return 0;
61}
62
63#include <support/test-driver.c>
64

source code of glibc/sysdeps/unix/sysv/linux/tst-linux-mremap1.c