]> granicus.if.org Git - strace/blob - tests/sched_xetattr.c
tests: check decoding of sched_[gs]etattr corner cases
[strace] / tests / sched_xetattr.c
1 /*
2  * Copyright (c) 2015-2017 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 <asm/unistd.h>
30
31 #if defined __NR_sched_getattr && defined __NR_sched_setattr
32
33 # include <inttypes.h>
34 # include <stdio.h>
35 # include <sched.h>
36 # include <unistd.h>
37 # include "sched_attr.h"
38 # include "xlat.h"
39 # include "xlat/schedulers.h"
40
41 static const char *errstr;
42
43 static long
44 sys_sched_getattr(kernel_ulong_t pid, kernel_ulong_t attr,
45                   kernel_ulong_t size, kernel_ulong_t flags)
46 {
47         long rc = syscall(__NR_sched_getattr, pid, attr, size, flags);
48         errstr = sprintrc(rc);
49         return rc;
50 }
51
52 static long
53 sys_sched_setattr(kernel_ulong_t pid, kernel_ulong_t attr, kernel_ulong_t flags)
54 {
55         long rc = syscall(__NR_sched_setattr, pid, attr, flags);
56         errstr = sprintrc(rc);
57         return rc;
58 }
59
60 int
61 main(void)
62 {
63         static const kernel_ulong_t bogus_pid =
64                 (kernel_ulong_t) 0xdefacedfacefeedULL;
65         static const kernel_ulong_t bogus_size =
66                 (kernel_ulong_t) 0xdefacedcafef00dULL;
67         static const kernel_ulong_t bogus_flags =
68                 (kernel_ulong_t) 0xdefaceddeadc0deULL;
69
70         struct sched_attr *const attr = tail_alloc(sizeof(*attr));
71         void *const efault = attr + 1;
72
73         sys_sched_getattr(0, 0, 0, 0);
74         printf("sched_getattr(0, NULL, 0, 0) = %s\n", errstr);
75
76         sys_sched_getattr(0, (unsigned long) attr, 0, 0);
77         printf("sched_getattr(0, %p, 0, 0) = %s\n", attr, errstr);
78
79         sys_sched_getattr(bogus_pid, 0, 0, 0);
80         printf("sched_getattr(%d, NULL, 0, 0) = %s\n", (int) bogus_pid, errstr);
81
82         sys_sched_getattr(-1U, (unsigned long) attr, bogus_size, bogus_flags);
83         printf("sched_getattr(-1, %p, %u, %u) = %s\n",
84                attr, (unsigned) bogus_size, (unsigned) bogus_flags, errstr);
85
86         sys_sched_getattr(0, (unsigned long) efault, sizeof(*attr), 0);
87         printf("sched_getattr(0, %p, %u, 0) = %s\n",
88                efault, (unsigned) sizeof(*attr), errstr);
89
90         if (sys_sched_getattr(0, (unsigned long) attr, sizeof(*attr), 0))
91                 perror_msg_and_skip("sched_getattr");
92         printf("sched_getattr(0, {size=%u, sched_policy=", attr->size);
93         printxval(schedulers, attr->sched_policy, NULL);
94         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
95                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
96                ", sched_period=%" PRIu64 "}, %u, 0) = 0\n",
97                attr->sched_flags ? "SCHED_FLAG_RESET_ON_FORK" : "0",
98                attr->sched_nice,
99                attr->sched_priority,
100                attr->sched_runtime,
101                attr->sched_deadline,
102                attr->sched_period,
103                (unsigned) sizeof(*attr));
104
105         sys_sched_getattr(F8ILL_KULONG_MASK, (unsigned long) attr,
106                           F8ILL_KULONG_MASK | sizeof(*attr), F8ILL_KULONG_MASK);
107         printf("sched_getattr(0, {size=%u, sched_policy=", attr->size);
108         printxval(schedulers, attr->sched_policy, NULL);
109         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
110                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
111                ", sched_period=%" PRIu64 "}, %u, 0) = 0\n",
112                attr->sched_flags ? "SCHED_FLAG_RESET_ON_FORK" : "0",
113                attr->sched_nice,
114                attr->sched_priority,
115                attr->sched_runtime,
116                attr->sched_deadline,
117                attr->sched_period,
118                (unsigned) sizeof(*attr));
119
120         sys_sched_setattr(bogus_pid, 0, 0);
121         printf("sched_setattr(%d, NULL, 0) = %s\n", (int) bogus_pid, errstr);
122
123         attr->sched_flags |= 1;
124
125         if (sys_sched_setattr(0, (unsigned long) attr, 0))
126                 perror_msg_and_skip("sched_setattr");
127         printf("sched_setattr(0, {size=%u, sched_policy=", attr->size);
128         printxval(schedulers, attr->sched_policy, NULL);
129         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
130                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
131                ", sched_period=%" PRIu64 "}, 0) = 0\n",
132                "SCHED_FLAG_RESET_ON_FORK",
133                attr->sched_nice,
134                attr->sched_priority,
135                attr->sched_runtime,
136                attr->sched_deadline,
137                attr->sched_period);
138
139         sys_sched_setattr(F8ILL_KULONG_MASK, (unsigned long) attr,
140                           F8ILL_KULONG_MASK);
141         printf("sched_setattr(0, {size=%u, sched_policy=", attr->size);
142         printxval(schedulers, attr->sched_policy, NULL);
143         printf(", sched_flags=%s, sched_nice=%d, sched_priority=%u"
144                ", sched_runtime=%" PRIu64 ", sched_deadline=%" PRIu64
145                ", sched_period=%" PRIu64 "}, 0) = 0\n",
146                "SCHED_FLAG_RESET_ON_FORK",
147                attr->sched_nice,
148                attr->sched_priority,
149                attr->sched_runtime,
150                attr->sched_deadline,
151                attr->sched_period);
152
153         attr->size = 0x90807060;
154         attr->sched_policy = 0xca7faced;
155         attr->sched_flags = 0xbadc0ded1057da7aULL;
156         attr->sched_nice = 0xafbfcfdf;
157         attr->sched_priority = 0xb8c8d8e8;
158         attr->sched_runtime = 0xbadcaffedeadf157ULL;
159         attr->sched_deadline = 0xc0de70a57badac75ULL;
160         attr->sched_period = 0xded1ca7edda7aca7ULL;
161
162         sys_sched_setattr(bogus_pid, (unsigned long) attr, bogus_flags);
163         printf("sched_setattr(%d, {size=%u, sched_policy=%#x /* SCHED_??? */, "
164                "sched_flags=%#" PRIx64 " /* SCHED_FLAG_??? */, "
165                "sched_nice=%d, sched_priority=%u, sched_runtime=%" PRIu64 ", "
166                "sched_deadline=%" PRIu64 ", sched_period=%" PRIu64 "}, %u)"
167                " = %s\n",
168                (int) bogus_pid,
169                attr->size,
170                attr->sched_policy,
171                attr->sched_flags,
172                attr->sched_nice,
173                attr->sched_priority,
174                attr->sched_runtime,
175                attr->sched_deadline,
176                attr->sched_period,
177                (unsigned) bogus_flags, errstr);
178
179         if (F8ILL_KULONG_SUPPORTED) {
180                 const kernel_ulong_t ill = f8ill_ptr_to_kulong(attr);
181
182                 sys_sched_getattr(0, ill, sizeof(*attr), 0);
183                 printf("sched_getattr(0, %#llx, %u, 0) = %s\n",
184                        (unsigned long long) ill, (unsigned) sizeof(*attr),
185                        errstr);
186
187                 sys_sched_setattr(0, ill, 0);
188                 printf("sched_setattr(0, %#llx, 0) = %s\n",
189                        (unsigned long long) ill, errstr);
190         }
191
192         puts("+++ exited with 0 +++");
193         return 0;
194 }
195
196 #else
197
198 SKIP_MAIN_UNDEFINED("__NR_sched_getattr && __NR_sched_setattr")
199
200 #endif