1 /* Test for syscall interfaces.
2 Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 /* This test verifies that the x32 system call handling zero-extends
20 unsigned 32-bit arguments to the 64-bit argument registers for
21 system calls (bug 25810). The bug is specific to x32, but the test
22 should pass on all architectures. */
28 #include <support/check.h>
29 #include <support/xunistd.h>
31 /* On x32, this can be passed in a single 64-bit integer register. */
38 static int error_count
;
40 __attribute__ ((noclone
, noinline
))
42 allocate (size_t bytes
)
45 return __extension__ (struct Array
) {0, 0};
47 void *p
= mmap (0x0, bytes
, PROT_READ
| PROT_WRITE
,
48 MAP_PRIVATE
| MAP_ANON
, -1, 0);
50 return __extension__ (struct Array
) {0, 0};
52 return __extension__ (struct Array
) {bytes
, p
};
55 __attribute__ ((noclone
, noinline
))
57 deallocate (struct Array b
)
59 /* On x32, the 64-bit integer register containing `b' may be copied
60 to another 64-bit integer register to pass the second argument to
62 if (b
.length
&& munmap (b
.ptr
, b
.length
))
64 printf ("munmap error: %m\n");
69 __attribute__ ((noclone
, noinline
))
71 do_mmap (void *addr
, size_t length
)
73 return mmap (addr
, length
, PROT_READ
| PROT_WRITE
,
74 MAP_PRIVATE
| MAP_ANON
, -1, 0);
77 __attribute__ ((noclone
, noinline
))
79 reallocate (struct Array b
)
81 /* On x32, the 64-bit integer register containing `b' may be copied
82 to another 64-bit integer register to pass the second argument to
85 return do_mmap (b
.ptr
, b
.length
);
89 __attribute__ ((noclone
, noinline
))
91 protect (struct Array b
)
95 /* On x32, the 64-bit integer register containing `b' may be copied
96 to another 64-bit integer register to pass the second argument
98 if (mprotect (b
.ptr
, b
.length
,
99 PROT_READ
| PROT_WRITE
| PROT_EXEC
))
101 printf ("mprotect error: %m\n");
107 __attribute__ ((noclone
, noinline
))
109 do_read (int fd
, void *ptr
, struct Array b
)
111 /* On x32, the 64-bit integer register containing `b' may be copied
112 to another 64-bit integer register to pass the second argument to
115 return read (fd
, ptr
, b
.length
);
119 __attribute__ ((noclone
, noinline
))
121 do_write (int fd
, void *ptr
, struct Array b
)
123 /* On x32, the 64-bit integer register containing `b' may be copied
124 to another 64-bit integer register to pass the second argument to
127 return write (fd
, ptr
, b
.length
);
136 array
= allocate (1);
139 void *p
= reallocate (array
);
142 printf ("mmap error: %m\n");
149 int fd
= xopen ("/dev/null", O_RDWR
, 0);
152 if (do_read (fd
, array
.ptr
, array
) == -1)
154 printf ("read error: %m\n");
157 if (do_write (fd
, array
.ptr
, array
) == -1)
159 printf ("write error: %m\n");
164 return error_count
? EXIT_FAILURE
: EXIT_SUCCESS
;
167 #include <support/test-driver.c>