]> granicus.if.org Git - strace/blob - tests/tests.h
Unify different generic PRINT_FIELD_* implementations
[strace] / tests / tests.h
1 /*
2  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2017 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 # include <sys/types.h>
37 # include "kernel_types.h"
38 # include "gcc_compat.h"
39
40 /*
41  * The printf-like function to use in header files
42  * shared between strace and its tests.
43  */
44 #ifndef STRACE_PRINTF
45 # define STRACE_PRINTF printf
46 #endif
47
48 /* Tests of "strace -v" are expected to define VERBOSE to 1. */
49 #ifndef VERBOSE
50 # define VERBOSE 0
51 #endif
52
53 /* Cached sysconf(_SC_PAGESIZE). */
54 size_t get_page_size(void);
55
56 /* The size of kernel's sigset_t. */
57 unsigned int get_sigset_size(void);
58
59 /* Print message and strerror(errno) to stderr, then exit(1). */
60 void perror_msg_and_fail(const char *, ...)
61         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
62 /* Print message to stderr, then exit(1). */
63 void error_msg_and_fail(const char *, ...)
64         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
65 /* Print message to stderr, then exit(77). */
66 void error_msg_and_skip(const char *, ...)
67         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
68 /* Print message and strerror(errno) to stderr, then exit(77). */
69 void perror_msg_and_skip(const char *, ...)
70         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
71
72 /* Stat the specified file and skip the test if the stat call failed. */
73 void skip_if_unavailable(const char *);
74
75 /*
76  * Allocate memory that ends on the page boundary.
77  * Pages allocated by this call are preceded by an unmapped page
78  * and followed also by an unmapped page.
79  */
80 void *tail_alloc(const size_t)
81         ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
82 /* Allocate memory using tail_alloc, then memcpy. */
83 void *tail_memdup(const void *, const size_t)
84         ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((2));
85
86 /*
87  * Allocate an object of the specified type at the end
88  * of a mapped memory region.
89  * Assign its address to the specified constant pointer.
90  */
91 #define TAIL_ALLOC_OBJECT_CONST_PTR(type_name, type_ptr)        \
92         type_name *const type_ptr = tail_alloc(sizeof(*type_ptr))
93
94 /*
95  * Allocate an object of the specified type at the end
96  * of a mapped memory region.
97  * Assign its address to the specified variable pointer.
98  */
99 #define TAIL_ALLOC_OBJECT_VAR_PTR(type_name, type_ptr)          \
100         type_name *type_ptr = tail_alloc(sizeof(*type_ptr))
101
102 /*
103  * Fill memory (pointed by ptr, having size bytes) with different bytes (with
104  * values starting with start and resetting every period) in order to catch
105  * sign, byte order and/or alignment errors.
106  */
107 void fill_memory_ex(void *ptr, size_t size, unsigned char start,
108                     unsigned char period);
109 /* Shortcut for fill_memory_ex(ptr, size, 0x80, 0x80) */
110 void fill_memory(void *ptr, size_t size);
111
112 /* Close stdin, move stdout to a non-standard descriptor, and print. */
113 void tprintf(const char *, ...)
114         ATTRIBUTE_FORMAT((printf, 1, 2));
115
116 /* Make a hexdump copy of C string */
117 const char *hexdump_strdup(const char *);
118
119 /* Make a hexdump copy of memory */
120 const char *hexdump_memdup(const char *, size_t);
121
122 /* Make a hexquoted copy of a string */
123 const char *hexquote_strndup(const char *, size_t);
124
125 /* Return inode number of socket descriptor. */
126 unsigned long inode_of_sockfd(int);
127
128 /* Print string in a quoted form. */
129 void print_quoted_string(const char *);
130
131 /* Print memory in a quoted form. */
132 void print_quoted_memory(const char *, size_t);
133
134 /* Print time_t and nanoseconds in symbolic format. */
135 void print_time_t_nsec(time_t, unsigned long long, int);
136
137 /* Print time_t and microseconds in symbolic format. */
138 void print_time_t_usec(time_t, unsigned long long, int);
139
140 /* Read an int from the file. */
141 int read_int_from_file(const char *, int *);
142
143 /* Check whether given uid matches kernel overflowuid. */
144 void check_overflowuid(const int);
145
146 /* Check whether given gid matches kernel overflowgid. */
147 void check_overflowgid(const int);
148
149 /* Translate errno to its name. */
150 const char *errno2name(void);
151
152 /* Translate signal number to its name. */
153 const char *signal2name(int);
154
155 /* Print return code and, in case return code is -1, errno information. */
156 const char *sprintrc(long rc);
157 /* sprintrc variant suitable for usage as part of grep pattern. */
158 const char *sprintrc_grep(long rc);
159
160 struct xlat;
161
162 /* Print flags in symbolic form according to xlat table. */
163 int printflags(const struct xlat *, const unsigned long long, const char *);
164
165 /* Print constant in symbolic form according to xlat table. */
166 int printxval(const struct xlat *, const unsigned long long, const char *);
167
168 /* Invoke a socket syscall, either directly or via __NR_socketcall. */
169 int socketcall(const int nr, const int call,
170                long a1, long a2, long a3, long a4, long a5);
171
172 /* Wrappers for recvmmsg and sendmmsg syscalls. */
173 struct mmsghdr;
174 struct timespec;
175 int recv_mmsg(int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *);
176 int send_mmsg(int, struct mmsghdr *, unsigned int, unsigned int);
177
178 /* Create a netlink socket. */
179 int create_nl_socket_ext(int proto, const char *name);
180 #define create_nl_socket(proto) create_nl_socket_ext((proto), #proto)
181
182 /* Create a pipe with maximized descriptor numbers. */
183 void pipe_maxfd(int pipefd[2]);
184
185 #define F8ILL_KULONG_SUPPORTED  (sizeof(void *) < sizeof(kernel_ulong_t))
186 #define F8ILL_KULONG_MASK       ((kernel_ulong_t) 0xffffffff00000000ULL)
187
188 /*
189  * For 64-bit kernel_ulong_t and 32-bit pointer,
190  * return a kernel_ulong_t value by filling higher bits.
191  * For other architertures, return the original pointer.
192  */
193 static inline kernel_ulong_t
194 f8ill_ptr_to_kulong(const void *const ptr)
195 {
196         const unsigned long uptr = (unsigned long) ptr;
197         return F8ILL_KULONG_SUPPORTED
198                ? F8ILL_KULONG_MASK | uptr : (kernel_ulong_t) uptr;
199 }
200
201 # define ARRAY_SIZE(arg) ((unsigned int) (sizeof(arg) / sizeof((arg)[0])))
202 # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1)
203
204 /* Zero-extend a signed integer type to unsigned long long. */
205 #define zero_extend_signed_to_ull(v) \
206         (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
207          sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
208          sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
209          sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
210          (unsigned long long) (v))
211
212 /* Sign-extend an unsigned integer type to long long. */
213 #define sign_extend_unsigned_to_ll(v) \
214         (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
215          sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
216          sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
217          sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
218          (long long) (v))
219
220 # define SKIP_MAIN_UNDEFINED(arg) \
221         int main(void) { error_msg_and_skip("undefined: %s", arg); }
222
223 /*
224  * The kernel used to define 64-bit types on 64-bit systems on a per-arch
225  * basis.  Some architectures would use unsigned long and others would use
226  * unsigned long long.  These types were exported as part of the
227  * kernel-userspace ABI and now must be maintained forever.  This matches
228  * what the kernel exports for each architecture so we don't need to cast
229  * every printing of __u64 or __s64 to stdint types.
230  */
231 # if SIZEOF_LONG == 4
232 #  define PRI__64 "ll"
233 # elif defined ALPHA || defined IA64 || defined MIPS || defined POWERPC
234 #  define PRI__64 "l"
235 # else
236 #  define PRI__64 "ll"
237 # endif
238
239 # define PRI__d64 PRI__64"d"
240 # define PRI__u64 PRI__64"u"
241 # define PRI__x64 PRI__64"x"
242
243 # if WORDS_BIGENDIAN
244 #  define LL_PAIR(HI, LO) (HI), (LO)
245 # else
246 #  define LL_PAIR(HI, LO) (LO), (HI)
247 # endif
248 # define LL_VAL_TO_PAIR(llval) LL_PAIR((long) ((llval) >> 32), (long) (llval))
249
250 # define _STR(_arg) #_arg
251 # define ARG_STR(_arg) (_arg), #_arg
252 # define ARG_ULL_STR(_arg) _arg##ULL, #_arg
253
254 /*
255  * Assign an object of type DEST_TYPE at address DEST_ADDR
256  * using memcpy to avoid potential unaligned access.
257  */
258 #define SET_STRUCT(DEST_TYPE, DEST_ADDR, ...)                                           \
259         do {                                                                            \
260                 DEST_TYPE dest_type_tmp_var = { __VA_ARGS__ };                          \
261                 memcpy(DEST_ADDR, &dest_type_tmp_var, sizeof(dest_type_tmp_var));       \
262         } while (0)
263
264 #define NLMSG_ATTR(nlh, hdrlen) ((void *)(nlh) + NLMSG_SPACE(hdrlen))
265
266 #endif /* !STRACE_TESTS_H */