1 /*
2  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2018 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef STRACE_TESTS_H
30 #define STRACE_TESTS_H
31 
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 
36 #ifdef TESTS_SIZEOF_KERNEL_LONG_T
37 # undef SIZEOF_KERNEL_LONG_T
38 # define SIZEOF_KERNEL_LONG_T TESTS_SIZEOF_KERNEL_LONG_T
39 #endif
40 
41 #ifdef TESTS_SIZEOF_LONG
42 # undef SIZEOF_LONG
43 # define SIZEOF_LONG TESTS_SIZEOF_LONG
44 #endif
45 
46 #include <stdbool.h>
47 #include <sys/types.h>
48 #include "kernel_types.h"
49 #include "gcc_compat.h"
50 #include "macros.h"
51 
52 /*
53  * The printf-like function to use in header files
54  * shared between strace and its tests.
55  */
56 #ifndef STRACE_PRINTF
57 # define STRACE_PRINTF printf
58 #endif
59 
60 /* Tests of "strace -v" are expected to define VERBOSE to 1. */
61 #ifndef VERBOSE
62 # define VERBOSE 0
63 #endif
64 
65 /* xlat verbosity defaults */
66 #ifndef XLAT_RAW
67 # define XLAT_RAW 0
68 #endif
69 #ifndef XLAT_VERBOSE
70 # define XLAT_VERBOSE 0
71 #endif
72 
73 #ifndef DEFAULT_STRLEN
74 /* Default maximum # of bytes printed in printstr et al. */
75 # define DEFAULT_STRLEN 32
76 #endif
77 
78 /* Cached sysconf(_SC_PAGESIZE). */
79 size_t get_page_size(void);
80 
81 /* The size of kernel's sigset_t. */
82 unsigned int get_sigset_size(void);
83 
84 /* Print message and strerror(errno) to stderr, then exit(1). */
85 void perror_msg_and_fail(const char *, ...)
86 	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
87 /* Print message to stderr, then exit(1). */
88 void error_msg_and_fail(const char *, ...)
89 	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
90 /* Print message to stderr, then exit(77). */
91 void error_msg_and_skip(const char *, ...)
92 	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
93 /* Print message and strerror(errno) to stderr, then exit(77). */
94 void perror_msg_and_skip(const char *, ...)
95 	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
96 
97 #ifndef perror_msg_and_fail
98 # define perror_msg_and_fail(fmt_, ...) \
99 	perror_msg_and_fail("%s:%d: " fmt_, __FILE__, __LINE__, ##__VA_ARGS__)
100 #endif
101 #ifndef perror_msg_and_fail
102 # define error_msg_and_fail(fmt_, ...) \
103 	error_msg_and_fail("%s:%d: " fmt_, __FILE__, __LINE__, ##__VA_ARGS__)
104 #endif
105 
106 /* Stat the specified file and skip the test if the stat call failed. */
107 void skip_if_unavailable(const char *);
108 
109 /*
110  * Allocate memory that ends on the page boundary.
111  * Pages allocated by this call are preceded by an unmapped page
112  * and followed also by an unmapped page.
113  */
114 void *tail_alloc(const size_t)
115 	ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
116 /* Allocate memory using tail_alloc, then memcpy. */
117 void *tail_memdup(const void *, const size_t)
118 	ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((2));
119 
120 #define midtail_alloc(after_, before_) \
121 	((void *) ((char *) tail_alloc(((before_) + (after_))) + (before_)))
122 
123 /*
124  * Allocate an object of the specified type at the end
125  * of a mapped memory region.
126  * Assign its address to the specified constant pointer.
127  */
128 #define TAIL_ALLOC_OBJECT_CONST_PTR(type_name, type_ptr)	\
129 	type_name *const type_ptr = tail_alloc(sizeof(*type_ptr))
130 
131 /*
132  * Allocate an object of the specified type at the end
133  * of a mapped memory region.
134  * Assign its address to the specified variable pointer.
135  */
136 #define TAIL_ALLOC_OBJECT_VAR_PTR(type_name, type_ptr)		\
137 	type_name *type_ptr = tail_alloc(sizeof(*type_ptr))
138 
139 /*
140  * Fill memory (pointed by ptr, having size bytes) with different bytes (with
141  * values starting with start and resetting every period) in order to catch
142  * sign, byte order and/or alignment errors.
143  */
144 void fill_memory_ex(void *ptr, size_t size, unsigned char start,
145 		    unsigned char period);
146 /* Shortcut for fill_memory_ex(ptr, size, 0x80, 0x80) */
147 void fill_memory(void *ptr, size_t size);
148 
149 /* Close stdin, move stdout to a non-standard descriptor, and print. */
150 void tprintf(const char *, ...)
151 	ATTRIBUTE_FORMAT((printf, 1, 2));
152 
153 /* Make a hexdump copy of C string */
154 const char *hexdump_strdup(const char *);
155 
156 /* Make a hexdump copy of memory */
157 const char *hexdump_memdup(const char *, size_t);
158 
159 /* Make a hexquoted copy of a string */
160 const char *hexquote_strndup(const char *, size_t);
161 
162 /* Return inode number of socket descriptor. */
163 unsigned long inode_of_sockfd(int);
164 
165 /* Print string in a quoted form with optional escape characters. */
166 void print_quoted_string_ex(const char *, bool quote, const char *escape_str);
167 
168 /* Print string in a quoted form. */
169 void print_quoted_string(const char *);
170 
171 /*
172  * Print a NUL-terminated string `str' of length up to `size' - 1
173  * in a quoted form.
174  */
175 void print_quoted_cstring(const char *str, size_t size);
176 
177 /*
178  * Print a NUL-terminated string `str' of length up to `size'
179  * in a quoted form.
180  */
181 void print_quoted_stringn(const char *str, size_t size);
182 
183 /* Print memory in a quoted form with optional escape characters. */
184 void print_quoted_memory_ex(const void *, size_t, bool quote,
185 			    const char *escape_chars);
186 
187 /* Print memory in a quoted form. */
188 void print_quoted_memory(const void *, size_t);
189 
190 /* Print memory in a hexquoted form. */
191 void print_quoted_hex(const void *, size_t);
192 
193 /* Print time_t and nanoseconds in symbolic format. */
194 void print_time_t_nsec(time_t, unsigned long long, int);
195 
196 /* Print time_t and microseconds in symbolic format. */
197 void print_time_t_usec(time_t, unsigned long long, int);
198 
199 /* Read an int from the file. */
200 int read_int_from_file(const char *, int *);
201 
202 /* Check whether given uid matches kernel overflowuid. */
203 void check_overflowuid(const int);
204 
205 /* Check whether given gid matches kernel overflowgid. */
206 void check_overflowgid(const int);
207 
208 /* Translate errno to its name. */
209 const char *errno2name(void);
210 
211 /* Translate signal number to its name. */
212 const char *signal2name(int);
213 
214 /* Print return code and, in case return code is -1, errno information. */
215 const char *sprintrc(long rc);
216 /* sprintrc variant suitable for usage as part of grep pattern. */
217 const char *sprintrc_grep(long rc);
218 
219 struct xlat;
220 
221 /* Print flags in symbolic form according to xlat table. */
222 int printflags(const struct xlat *, const unsigned long long, const char *);
223 
224 /* Print constant in symbolic form according to xlat table. */
225 int printxval(const struct xlat *, const unsigned long long, const char *);
226 
227 /* Invoke a socket syscall, either directly or via __NR_socketcall. */
228 int socketcall(const int nr, const int call,
229 	       long a1, long a2, long a3, long a4, long a5);
230 
231 /* Wrappers for recvmmsg and sendmmsg syscalls. */
232 struct mmsghdr;
233 struct timespec;
234 int recv_mmsg(int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *);
235 int send_mmsg(int, struct mmsghdr *, unsigned int, unsigned int);
236 
237 /* Create a netlink socket. */
238 int create_nl_socket_ext(int proto, const char *name);
239 #define create_nl_socket(proto)	create_nl_socket_ext((proto), #proto)
240 
241 /* Create a pipe with maximized descriptor numbers. */
242 void pipe_maxfd(int pipefd[2]);
243 
244 /* if_nametoindex("lo") */
245 unsigned int ifindex_lo(void);
246 
247 #ifdef HAVE_IF_INDEXTONAME
248 # define IFINDEX_LO_STR "if_nametoindex(\"lo\")"
249 #else
250 # define IFINDEX_LO_STR "1"
251 #endif
252 
253 #define F8ILL_KULONG_SUPPORTED	(sizeof(void *) < sizeof(kernel_ulong_t))
254 #define F8ILL_KULONG_MASK	((kernel_ulong_t) 0xffffffff00000000ULL)
255 
256 /*
257  * For 64-bit kernel_ulong_t and 32-bit pointer,
258  * return a kernel_ulong_t value by filling higher bits.
259  * For other architertures, return the original pointer.
260  */
261 static inline kernel_ulong_t
f8ill_ptr_to_kulong(const void * const ptr)262 f8ill_ptr_to_kulong(const void *const ptr)
263 {
264 	const unsigned long uptr = (unsigned long) ptr;
265 	return F8ILL_KULONG_SUPPORTED
266 	       ? F8ILL_KULONG_MASK | uptr : (kernel_ulong_t) uptr;
267 }
268 
269 # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1)
270 
271 /* Zero-extend a signed integer type to unsigned long long. */
272 #define zero_extend_signed_to_ull(v) \
273 	(sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
274 	 sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
275 	 sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
276 	 sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
277 	 (unsigned long long) (v))
278 
279 /* Sign-extend an unsigned integer type to long long. */
280 #define sign_extend_unsigned_to_ll(v) \
281 	(sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
282 	 sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
283 	 sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
284 	 sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
285 	 (long long) (v))
286 
287 #define SKIP_MAIN_UNDEFINED(arg) \
288 	int main(void) { error_msg_and_skip("undefined: %s", arg); }
289 
290 #if WORDS_BIGENDIAN
291 # define LL_PAIR(HI, LO) (HI), (LO)
292 #else
293 # define LL_PAIR(HI, LO) (LO), (HI)
294 #endif
295 #define LL_VAL_TO_PAIR(llval) LL_PAIR((long) ((llval) >> 32), (long) (llval))
296 
297 #define ARG_STR(_arg) (_arg), #_arg
298 #define ARG_ULL_STR(_arg) _arg##ULL, #_arg
299 
300 /*
301  * Assign an object of type DEST_TYPE at address DEST_ADDR
302  * using memcpy to avoid potential unaligned access.
303  */
304 #define SET_STRUCT(DEST_TYPE, DEST_ADDR, ...)						\
305 	do {										\
306 		DEST_TYPE dest_type_tmp_var = { __VA_ARGS__ };				\
307 		memcpy(DEST_ADDR, &dest_type_tmp_var, sizeof(dest_type_tmp_var));	\
308 	} while (0)
309 
310 #define NLMSG_ATTR(nlh, hdrlen) ((void *)(nlh) + NLMSG_SPACE(hdrlen))
311 
312 #endif /* !STRACE_TESTS_H */
313