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