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
|
/* Test for syscall interfaces.
Copyright (C) 2020-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
/* This test verifies that the x32 system call handling zero-extends
unsigned 32-bit arguments to the 64-bit argument registers for
system calls (bug 25810). The bug is specific to x32, but the test
should pass on all architectures. */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <support/check.h>
#include <support/xunistd.h>
/* On x32, this can be passed in a single 64-bit integer register. */
struct Array
{
size_t length;
void *ptr;
};
static int error_count;
__attribute__ ((noclone, noinline))
struct Array
allocate (size_t bytes)
{
if (!bytes)
return __extension__ (struct Array) {0, 0};
void *p = mmap (0x0, bytes, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
if (p == MAP_FAILED)
return __extension__ (struct Array) {0, 0};
return __extension__ (struct Array) {bytes, p};
}
__attribute__ ((noclone, noinline))
void
deallocate (struct Array b)
{
/* On x32, the 64-bit integer register containing `b' may be copied
to another 64-bit integer register to pass the second argument to
munmap. */
if (b.length && munmap (b.ptr, b.length))
{
printf ("munmap error: %m\n");
error_count++;
}
}
__attribute__ ((noclone, noinline))
void *
do_mmap (void *addr, size_t length)
{
return mmap (addr, length, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
}
__attribute__ ((noclone, noinline))
void *
reallocate (struct Array b)
{
/* On x32, the 64-bit integer register containing `b' may be copied
to another 64-bit integer register to pass the second argument to
do_mmap. */
if (b.length)
return do_mmap (b.ptr, b.length);
return NULL;
}
__attribute__ ((noclone, noinline))
void
protect (struct Array b)
{
if (b.length)
{
/* On x32, the 64-bit integer register containing `b' may be copied
to another 64-bit integer register to pass the second argument
to mprotect. */
if (mprotect (b.ptr, b.length,
PROT_READ | PROT_WRITE | PROT_EXEC))
{
printf ("mprotect error: %m\n");
error_count++;
}
}
}
__attribute__ ((noclone, noinline))
ssize_t
do_read (int fd, void *ptr, struct Array b)
{
/* On x32, the 64-bit integer register containing `b' may be copied
to another 64-bit integer register to pass the second argument to
read. */
if (b.length)
return read (fd, ptr, b.length);
return 0;
}
__attribute__ ((noclone, noinline))
ssize_t
do_write (int fd, void *ptr, struct Array b)
{
/* On x32, the 64-bit integer register containing `b' may be copied
to another 64-bit integer register to pass the second argument to
write. */
if (b.length)
return write (fd, ptr, b.length);
return 0;
}
static int
do_test (void)
{
struct Array array;
array = allocate (1);
protect (array);
deallocate (array);
void *p = reallocate (array);
if (p == MAP_FAILED)
{
printf ("mmap error: %m\n");
error_count++;
}
array.ptr = p;
protect (array);
deallocate (array);
int fd = xopen ("/dev/null", O_RDWR, 0);
char buf[2];
array.ptr = buf;
if (do_read (fd, array.ptr, array) == -1)
{
printf ("read error: %m\n");
error_count++;
}
if (do_write (fd, array.ptr, array) == -1)
{
printf ("write error: %m\n");
error_count++;
}
xclose (fd);
return error_count ? EXIT_FAILURE : EXIT_SUCCESS;
}
#include <support/test-driver.c>
|