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