1 /*
2  * Check decoding and dumping of read and write syscalls.
3  *
4  * Copyright (c) 2016 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 
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <asm/unistd.h>
37 
38 static void
dump_str(const char * str,const unsigned int len)39 dump_str(const char *str, const unsigned int len)
40 {
41 	static const char dots[16] = "................";
42 	unsigned int i;
43 
44 	for (i = 0; i < len; i += 16) {
45 		unsigned int n = len - i > 16 ? 16 : len - i;
46 		const char *dump = hexdump_memdup(str + i, n);
47 
48 		tprintf(" | %05x %-49s  %-16.*s |\n",
49 			i, dump, n, dots);
50 
51 		free((void *) dump);
52 	}
53 }
54 
55 static void
print_hex(const char * str,const unsigned int len)56 print_hex(const char *str, const unsigned int len)
57 {
58 	const unsigned char *ustr = (const unsigned char *) str;
59 	unsigned int i;
60 
61 	for (i = 0; i < len; ++i) {
62 		unsigned int c = ustr[i];
63 
64 		switch (c) {
65 		case '\t':
66 			tprintf("\\t"); break;
67 		case '\n':
68 			tprintf("\\n"); break;
69 		case '\v':
70 			tprintf("\\v"); break;
71 		case '\f':
72 			tprintf("\\f"); break;
73 		case '\r':
74 			tprintf("\\r"); break;
75 		default:
76 			tprintf("\\%o", ustr[i]);
77 		}
78 	}
79 }
80 
81 static long
k_read(unsigned int fd,void * buf,size_t count)82 k_read(unsigned int fd, void *buf, size_t count)
83 {
84 	kernel_ulong_t kfd = (kernel_ulong_t) 0xfacefeed00000000ULL | fd;
85 	return syscall(__NR_read, kfd, buf, count);
86 }
87 
88 static long
k_write(unsigned int fd,const void * buf,size_t count)89 k_write(unsigned int fd, const void *buf, size_t count)
90 {
91 	kernel_ulong_t kfd = (kernel_ulong_t) 0xfacefeed00000000ULL | fd;
92 	return syscall(__NR_write, kfd, buf, count);
93 }
94 
95 static void
test_dump(const unsigned int len)96 test_dump(const unsigned int len)
97 {
98 	static char *buf;
99 
100 	if (buf) {
101 		size_t ps1 = get_page_size() - 1;
102 		buf = (void *) (((size_t) buf + ps1) & ~ps1) - len;
103 	} else {
104 		buf = tail_alloc(len);
105 	}
106 
107 	long rc = k_read(0, buf, len);
108 	if (rc != (int) len)
109 		perror_msg_and_fail("read: expected %d, returned %ld",
110 				    len, rc);
111 
112 	tprintf("%s(%d, \"", "read", 0);
113 	print_hex(buf, len);
114 	tprintf("\", %d) = %ld\n", len, rc);
115 	dump_str(buf, len);
116 
117 	unsigned int i;
118 	for (i = 0; i < len; ++i)
119 		buf[i] = i;
120 
121 	rc = k_write(1, buf, len);
122 	if (rc != (int) len)
123 		perror_msg_and_fail("write: expected %d, returned %ld",
124 				    len, rc);
125 
126 	tprintf("%s(%d, \"", "write", 1);
127 	print_hex(buf, len);
128 	tprintf("\", %d) = %ld\n", len, rc);
129 	dump_str(buf, len);
130 
131 	if (!len)
132 		buf = 0;
133 }
134 
135 int
main(void)136 main(void)
137 {
138 	tprintf("%s", "");
139 
140 	static char tmp[] = "read-write-tmpfile";
141 	if (open(tmp, O_CREAT|O_RDONLY|O_TRUNC, 0600) != 0)
142 		perror_msg_and_fail("creat: %s", tmp);
143 	if (open(tmp, O_WRONLY) != 1)
144 		perror_msg_and_fail("open: %s", tmp);
145 
146 	static const char w_c[] = "0123456789abcde";
147 	const unsigned int w_len = LENGTH_OF(w_c);
148 	const char *w_d = hexdump_strdup(w_c);
149 	const void *w = tail_memdup(w_c, w_len);
150 
151 	static const char r0_c[] = "01234567";
152 	const char *r0_d = hexdump_strdup(r0_c);
153 	const unsigned int r0_len = (w_len + 1) / 2;
154 	void *r0 = tail_alloc(r0_len);
155 
156 	static const char r1_c[] = "89abcde";
157 	const char *r1_d = hexdump_strdup(r1_c);
158 	const unsigned int r1_len = w_len - r0_len;
159 	void *r1 = tail_alloc(w_len);
160 
161 	void *efault = r1 - get_page_size();
162 
163 	long rc;
164 
165 	rc = k_write(1, w, 0);
166 	if (rc)
167 		perror_msg_and_fail("write: expected 0, returned %ld", rc);
168 	tprintf("write(1, \"\", 0) = 0\n");
169 
170 	rc = k_write(1, efault, 1);
171 	if (rc != -1)
172 		perror_msg_and_fail("write: expected -1 EFAULT"
173 				    ", returned %ld", rc);
174 	tprintf("write(1, %p, 1) = -1 EFAULT (%m)\n", efault);
175 
176 	rc = k_write(1, w, w_len);
177 	if (rc != (int) w_len)
178 		perror_msg_and_fail("write: expected %u, returned %ld",
179 				    w_len, rc);
180 	tprintf("write(1, \"%s\", %u) = %ld\n"
181 		" | 00000 %-49s  %-16s |\n",
182 		w_c, w_len, rc, w_d, w_c);
183 	close(1);
184 
185 	rc = k_read(0, r0, 0);
186 	if (rc)
187 		perror_msg_and_fail("read: expected 0, returned %ld", rc);
188 	tprintf("read(0, \"\", 0) = 0\n");
189 
190 	rc = k_read(0, efault, 1);
191 	if (rc != -1)
192 		perror_msg_and_fail("read: expected -1, returned %ld", rc);
193 	tprintf("read(0, %p, 1) = -1 EFAULT (%m)\n", efault);
194 
195 	rc = k_read(0, r0, r0_len);
196 	if (rc != (int) r0_len)
197 		perror_msg_and_fail("read: expected %u, returned %ld",
198 				    r0_len, rc);
199 	tprintf("read(0, \"%s\", %u) = %ld\n"
200 		" | 00000 %-49s  %-16s |\n",
201 		r0_c, r0_len, rc, r0_d, r0_c);
202 
203 	rc = k_read(0, r1, w_len);
204 	if (rc != (int) r1_len)
205 		perror_msg_and_fail("read: expected %u, returned %ld",
206 				    r1_len, rc);
207 	tprintf("read(0, \"%s\", %u) = %ld\n"
208 		" | 00000 %-49s  %-16s |\n",
209 		r1_c, w_len, rc, r1_d, r1_c);
210 	close(0);
211 
212 	if (open("/dev/zero", O_RDONLY))
213 		perror_msg_and_fail("open");
214 
215 	if (open("/dev/null", O_WRONLY) != 1)
216 		perror_msg_and_fail("open");
217 
218 	unsigned int i;
219 	for (i = 0; i <= 32; ++i)
220 		test_dump(i);
221 
222 	tprintf("+++ exited with 0 +++\n");
223 	return 0;
224 }
225