]> granicus.if.org Git - strace/blob - tests/mmap.c
Replace "(unsigned long) -1L" with -1UL
[strace] / tests / mmap.c
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "tests.h"
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <sys/mman.h>
34
35 int
36 main(int ac, char **av)
37 {
38         const char *const name = ac > 1 ? av[1] : "mmap";
39         const intmax_t pagesize = get_page_size();
40         const unsigned long length1 = pagesize * 6;
41         const unsigned long length2 = pagesize * 3;
42         const unsigned long length3 = pagesize * 2;
43         const int fd = -1;
44         off_t offset;
45         void *addr, *p;
46
47 #if ULONG_MAX > 4294967295UL
48         offset = 0xcafedeadbeef000ULL & -pagesize;
49         addr = (void *) (uintmax_t) (0xfacefeed000 & -pagesize);
50 #else
51         offset = 0xdeadbeef000ULL & -pagesize;
52         addr = (void *) (unsigned int) (0xfaced000 & -pagesize);
53 #endif
54         const uintmax_t uoffset =
55                sizeof(offset) == sizeof(int) ? (uintmax_t) (unsigned int) offset
56                                              : (uintmax_t) offset;
57
58         (void) close(0);
59         (void) close(0);
60         printf("%s(NULL, 0, PROT_NONE, MAP_FILE, 0, 0) = -1 EBADF (%m)\n",
61                name);
62         mmap(NULL, 0, PROT_NONE, MAP_FILE, 0, 0);
63
64         p = mmap(addr, length1, PROT_READ | PROT_WRITE,
65                  MAP_PRIVATE | MAP_ANONYMOUS, fd, offset);
66         if (MAP_FAILED == p)
67                 perror_msg_and_fail("mmap");
68         printf("%s(%p, %lu, PROT_READ|PROT_WRITE, "
69                "MAP_PRIVATE|MAP_ANONYMOUS, %d, %#jx) = %p\n",
70                name, addr, length1, fd, uoffset, p);
71
72         if (msync(p, length1, MS_SYNC))
73                 perror_msg_and_fail("msync");
74         printf("msync(%p, %lu, MS_SYNC) = 0\n", p, length1);
75
76         if (mprotect(p, length1, PROT_NONE))
77                 perror_msg_and_fail("mprotect");
78         printf("mprotect(%p, %lu, PROT_NONE) = 0\n", p, length1);
79
80         addr = mremap(p, length1, length2, 0);
81         if (MAP_FAILED == addr)
82                 perror_msg_and_fail("mremap");
83         printf("mremap(%p, %lu, %lu, 0) = %p\n", p, length1, length2, addr);
84
85         p =  mremap(addr, length2, length3, MREMAP_MAYMOVE | MREMAP_FIXED,
86                     addr + length2);
87         if (MAP_FAILED == p)
88                 perror_msg_and_fail("mremap");
89         printf("mremap(%p, %lu, %lu, MREMAP_MAYMOVE|MREMAP_FIXED"
90                ", %p) = %p\n", addr, length2, length3, addr + length2, p);
91
92         if (madvise(p, length3, MADV_NORMAL))
93                 perror_msg_and_fail("madvise");
94         printf("madvise(%p, %lu, MADV_NORMAL) = 0\n", p, length3);
95
96         if (munmap(p, length3))
97                 perror_msg_and_fail("munmap");
98         printf("munmap(%p, %lu) = 0\n", p, length3);
99
100         if (mlockall(MCL_FUTURE))
101                 perror_msg_and_fail("mlockall");
102         puts("mlockall(MCL_FUTURE) = 0");
103
104         puts("+++ exited with 0 +++");
105         return 0;
106 }