1 /*
2  * Check decoding of setitimer and getitimer syscalls.
3  *
4  * Copyright (c) 2015-2017 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"
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <sys/time.h>
34 #include <unistd.h>
35 #include <asm/unistd.h>
36 
37 int
main(void)38 main(void)
39 {
40 	static const struct itimerval new = {
41 		.it_interval = { 0xc0de1, 0xc0de2 },
42 		.it_value = { 0xc0de3, 0xc0de4 }
43 	};
44 	static const kernel_ulong_t long_timer =
45 		F8ILL_KULONG_MASK | ITIMER_REAL;
46 	static const kernel_ulong_t bogus_timer =
47 		(kernel_ulong_t) 0xfacefeeddeadbeefULL;
48 
49 	TAIL_ALLOC_OBJECT_CONST_PTR(struct itimerval, p_old);
50 	struct itimerval *const p_new = tail_memdup(&new, sizeof(new));
51 	void *const efault = tail_alloc(sizeof(new) - 8);
52 	long rc;
53 
54 	if (setitimer(ITIMER_REAL, p_new, NULL))
55 		perror_msg_and_skip("setitimer");
56 	printf("setitimer(ITIMER_REAL"
57 	       ", {it_interval={tv_sec=%lld, tv_usec=%llu}"
58 	       ", it_value={tv_sec=%lld, tv_usec=%llu}}"
59 	       ", NULL) = 0\n",
60 	       (long long) new.it_interval.tv_sec,
61 	       zero_extend_signed_to_ull(new.it_interval.tv_usec),
62 	       (long long) new.it_value.tv_sec,
63 	       zero_extend_signed_to_ull(new.it_value.tv_usec));
64 
65 	fill_memory(p_old, sizeof(*p_old));
66 	if (getitimer(ITIMER_REAL, p_old))
67 		perror_msg_and_skip("getitimer");
68 	printf("getitimer(ITIMER_REAL"
69 	       ", {it_interval={tv_sec=%lld, tv_usec=%llu}"
70 	       ", it_value={tv_sec=%lld, tv_usec=%llu}}) = 0\n",
71 	       (long long) p_old->it_interval.tv_sec,
72 	       zero_extend_signed_to_ull(p_old->it_interval.tv_usec),
73 	       (long long) p_old->it_value.tv_sec,
74 	       zero_extend_signed_to_ull(p_old->it_value.tv_usec));
75 
76 	fill_memory(p_old, sizeof(*p_old));
77 	setitimer(ITIMER_REAL, p_new, p_old);
78 	printf("setitimer(ITIMER_REAL"
79 	       ", {it_interval={tv_sec=%lld, tv_usec=%llu}"
80 	       ", it_value={tv_sec=%lld, tv_usec=%llu}}"
81 	       ", {it_interval={tv_sec=%lld, tv_usec=%llu}"
82 	       ", it_value={tv_sec=%lld, tv_usec=%llu}}) = 0\n",
83 	       (long long) new.it_interval.tv_sec,
84 	       zero_extend_signed_to_ull(new.it_interval.tv_usec),
85 	       (long long) new.it_value.tv_sec,
86 	       zero_extend_signed_to_ull(new.it_value.tv_usec),
87 	       (long long) p_old->it_interval.tv_sec,
88 	       zero_extend_signed_to_ull(p_old->it_interval.tv_usec),
89 	       (long long) p_old->it_value.tv_sec,
90 	       zero_extend_signed_to_ull(p_old->it_value.tv_usec));
91 
92 	rc = getitimer(ITIMER_REAL, efault);
93 	printf("getitimer(ITIMER_REAL, %p) = %s\n", efault, sprintrc(rc));
94 
95 	rc = setitimer(ITIMER_REAL, p_new, efault);
96 	printf("setitimer(ITIMER_REAL"
97 	       ", {it_interval={tv_sec=%lld, tv_usec=%llu}"
98 	       ", it_value={tv_sec=%lld, tv_usec=%llu}}, %p) = %s\n",
99 	       (long long) new.it_interval.tv_sec,
100 	       zero_extend_signed_to_ull(new.it_interval.tv_usec),
101 	       (long long) new.it_value.tv_sec,
102 	       zero_extend_signed_to_ull(new.it_value.tv_usec),
103 	       efault, sprintrc(rc));
104 
105 	rc = setitimer(ITIMER_REAL, efault, p_old);
106 	printf("setitimer(ITIMER_REAL, %p, %p) = %s\n",
107 	       efault, p_old, sprintrc(rc));
108 
109 	fill_memory(p_old, sizeof(*p_old));
110 	rc = syscall(__NR_setitimer, long_timer, p_new, p_old);
111 	printf("setitimer(ITIMER_REAL"
112 	       ", {it_interval={tv_sec=%lld, tv_usec=%llu}"
113 	       ", it_value={tv_sec=%lld, tv_usec=%llu}}"
114 	       ", {it_interval={tv_sec=%lld, tv_usec=%llu}"
115 	       ", it_value={tv_sec=%lld, tv_usec=%llu}}) = %s\n",
116 	       (long long) new.it_interval.tv_sec,
117 	       zero_extend_signed_to_ull(new.it_interval.tv_usec),
118 	       (long long) new.it_value.tv_sec,
119 	       zero_extend_signed_to_ull(new.it_value.tv_usec),
120 	       (long long) p_old->it_interval.tv_sec,
121 	       zero_extend_signed_to_ull(p_old->it_interval.tv_usec),
122 	       (long long) p_old->it_value.tv_sec,
123 	       zero_extend_signed_to_ull(p_old->it_value.tv_usec),
124 	       sprintrc(rc));
125 
126 	fill_memory(p_old, sizeof(*p_old));
127 	rc = syscall(__NR_getitimer, long_timer, p_old);
128 	printf("getitimer(ITIMER_REAL"
129 	       ", {it_interval={tv_sec=%lld, tv_usec=%llu}"
130 	       ", it_value={tv_sec=%lld, tv_usec=%llu}}) = %s\n",
131 	       (long long) p_old->it_interval.tv_sec,
132 	       zero_extend_signed_to_ull(p_old->it_interval.tv_usec),
133 	       (long long) p_old->it_value.tv_sec,
134 	       zero_extend_signed_to_ull(p_old->it_value.tv_usec),
135 	       sprintrc(rc));
136 
137 	rc = syscall(__NR_setitimer, bogus_timer, p_new, p_old);
138 	printf("setitimer(%#x /* ITIMER_??? */"
139 	       ", {it_interval={tv_sec=%lld, tv_usec=%llu}"
140 	       ", it_value={tv_sec=%lld, tv_usec=%llu}}, %p) = %s\n",
141 	       (int) bogus_timer,
142 	       (long long) new.it_interval.tv_sec,
143 	       zero_extend_signed_to_ull(new.it_interval.tv_usec),
144 	       (long long) new.it_value.tv_sec,
145 	       zero_extend_signed_to_ull(new.it_value.tv_usec),
146 	       p_old, sprintrc(rc));
147 
148 	rc = syscall(__NR_getitimer, bogus_timer, p_old);
149 	printf("getitimer(%#x /* ITIMER_??? */, %p) = %s\n",
150 	       (int) bogus_timer, p_old, sprintrc(rc));
151 
152 	if (F8ILL_KULONG_SUPPORTED) {
153 		const kernel_ulong_t ill_new = f8ill_ptr_to_kulong(p_new);
154 		const kernel_ulong_t ill_old = f8ill_ptr_to_kulong(p_old);
155 
156 		rc = syscall(__NR_setitimer, long_timer, ill_new, ill_old);
157 		printf("setitimer(ITIMER_REAL, %#llx, %#llx) = %s\n",
158 		       (unsigned long long) ill_new,
159 		       (unsigned long long) ill_old,
160 		       sprintrc(rc));
161 
162 		rc = syscall(__NR_getitimer, long_timer, ill_old);
163 		printf("getitimer(ITIMER_REAL, %#llx) = %s\n",
164 		       (unsigned long long) ill_old, sprintrc(rc));
165 	}
166 
167 	p_new->it_interval.tv_sec = 0xdeadbeefU;
168 	p_new->it_interval.tv_usec = 0xfacefeedU;
169 	p_new->it_value.tv_sec = (time_t) 0xcafef00ddeadbeefLL;
170 	p_new->it_value.tv_usec = (suseconds_t) 0xbadc0dedfacefeedLL;
171 
172 	rc = setitimer(ITIMER_REAL, p_new, p_old);
173 	printf("setitimer(ITIMER_REAL"
174 	       ", {it_interval={tv_sec=%lld, tv_usec=%llu}"
175 	       ", it_value={tv_sec=%lld, tv_usec=%llu}}, %p) = %s\n",
176 	       (long long) p_new->it_interval.tv_sec,
177 	       zero_extend_signed_to_ull(p_new->it_interval.tv_usec),
178 	       (long long) p_new->it_value.tv_sec,
179 	       zero_extend_signed_to_ull(p_new->it_value.tv_usec),
180 	       p_old, sprintrc(rc));
181 
182 	puts("+++ exited with 0 +++");
183 	return 0;
184 }
185