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