blob: de1bafb3671a0dc81336f3c7ba75bf0f14e86a0a [file] [log] [blame]
Dmitry V. Levin3797d102016-04-22 20:37:19 +00001/*
2 * Check decoding of "old mmap" edition of mmap syscall.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "tests.h"
Dmitry V. Levin6a2f43c2016-08-09 14:38:29 +000031#include <asm/unistd.h>
Dmitry V. Levin3797d102016-04-22 20:37:19 +000032
33/*
34 * On s390x, this is the mmap syscall used by glibc, so,
35 * from one side, it's already covered by another test, and, from another side,
36 * it would require additional efforts to filter out mmap calls made by glibc.
37 */
38
39#if defined __NR_mmap && \
40( defined __arm__ \
41 || defined __i386__ \
42 || defined __m68k__ \
43 || (defined __s390__ && !defined __s390x__) \
44)
45
46# include <stdio.h>
47# include <string.h>
48# include <sys/mman.h>
49# include <unistd.h>
50
51int
52main(void)
53{
Dmitry V. Levin3db07f12016-04-22 23:41:36 +000054 long rc = syscall(__NR_mmap, 0);
55 printf("mmap(NULL) = %ld %s (%m)\n", rc, errno2name());
56
Dmitry V. Levin3797d102016-04-22 20:37:19 +000057 const unsigned int args1_c[6] = {
58 0xdeadbeef, /* addr */
59 0xfacefeed, /* len */
60 PROT_READ|PROT_EXEC, /* prot */
61 MAP_FILE|MAP_FIXED, /* flags */
62 -2U, /* fd */
63 0xbadc0ded /* offset */
64 };
65 const unsigned int page_size = get_page_size();
66 const unsigned int args2_c[6] = {
67 0,
68 page_size,
69 PROT_READ|PROT_WRITE,
70 MAP_PRIVATE|MAP_ANONYMOUS,
71 -1U,
72 0xfaced000 & -page_size
73 };
74 void *args = tail_memdup(args1_c, sizeof(args1_c));
75
Dmitry V. Levin3db07f12016-04-22 23:41:36 +000076 rc = syscall(__NR_mmap, args);
Dmitry V. Levin3797d102016-04-22 20:37:19 +000077 printf("mmap(%#x, %u, PROT_READ|PROT_EXEC, MAP_FILE|MAP_FIXED"
78 ", %d, %#x) = %ld %s (%m)\n",
79 args1_c[0], args1_c[1], args1_c[4], args1_c[5],
80 rc, errno2name());
81
82 memcpy(args, args2_c, sizeof(args2_c));
83 rc = syscall(__NR_mmap, args);
84 printf("mmap(NULL, %u, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS"
85 ", %d, %#x) = %#lx\n",
86 args2_c[1], args2_c[4], args2_c[5], rc);
87
88 void *addr = (void *) rc;
89 if (mprotect(addr, page_size, PROT_NONE))
90 perror_msg_and_fail("mprotect(%p, %u, PROT_NONE)",
91 addr, page_size);
92
93 puts("+++ exited with 0 +++");
94 return 0;
95}
96
97#else
98
99SKIP_MAIN_UNDEFINED("__NR_mmap && (__arm__ || __i386__ || __m68k__"
100 " || (__s390__ && !__s390x__))")
101
102#endif