Home | History | Annotate | Download | only in tests-mx32
      1 /*
      2  * Copyright (c) 2016 Dmitry V. Levin <ldv (at) altlinux.org>
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. The name of the author may not be used to endorse or promote products
     14  *    derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef STRACE_TESTS_H
     29 #define STRACE_TESTS_H
     30 
     31 # ifdef HAVE_CONFIG_H
     32 #  include "config.h"
     33 # endif
     34 
     35 # include <sys/types.h>
     36 # include "kernel_types.h"
     37 # include "gcc_compat.h"
     38 
     39 /* Tests of "strace -v" are expected to define VERBOSE to 1. */
     40 #ifndef VERBOSE
     41 # define VERBOSE 0
     42 #endif
     43 
     44 /* Cached sysconf(_SC_PAGESIZE). */
     45 size_t get_page_size(void);
     46 
     47 /* Print message and strerror(errno) to stderr, then exit(1). */
     48 void perror_msg_and_fail(const char *, ...)
     49 	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
     50 /* Print message to stderr, then exit(1). */
     51 void error_msg_and_fail(const char *, ...)
     52 	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
     53 /* Print message to stderr, then exit(77). */
     54 void error_msg_and_skip(const char *, ...)
     55 	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
     56 /* Print message and strerror(errno) to stderr, then exit(77). */
     57 void perror_msg_and_skip(const char *, ...)
     58 	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
     59 
     60 /*
     61  * Allocate memory that ends on the page boundary.
     62  * Pages allocated by this call are preceeded by an unmapped page
     63  * and followed also by an unmapped page.
     64  */
     65 void *tail_alloc(const size_t)
     66 	ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
     67 /* Allocate memory using tail_alloc, then memcpy. */
     68 void *tail_memdup(const void *, const size_t)
     69 	ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((2));
     70 
     71 /*
     72  * Fill memory (pointed by ptr, having size bytes) with different bytes (with
     73  * values starting with start and resetting every period) in order to catch
     74  * sign, byte order and/or alignment errors.
     75  */
     76 void fill_memory_ex(void *ptr, size_t size, unsigned char start,
     77 		    unsigned char period);
     78 /* Shortcut for fill_memory_ex(ptr, size, 0x80, 0x80) */
     79 void fill_memory(void *ptr, size_t size);
     80 
     81 /* Close stdin, move stdout to a non-standard descriptor, and print. */
     82 void tprintf(const char *, ...)
     83 	ATTRIBUTE_FORMAT((printf, 1, 2));
     84 
     85 /* Make a hexdump copy of C string */
     86 const char *hexdump_strdup(const char *);
     87 
     88 /* Make a hexdump copy of memory */
     89 const char *hexdump_memdup(const char *, size_t);
     90 
     91 /* Make a hexquoted copy of a string */
     92 const char *hexquote_strndup(const char *, size_t);
     93 
     94 /* Return inode number of socket descriptor. */
     95 unsigned long inode_of_sockfd(int);
     96 
     97 /* Print string in a quoted form. */
     98 void print_quoted_string(const char *);
     99 
    100 /* Print memory in a quoted form. */
    101 void print_quoted_memory(const char *, size_t);
    102 
    103 /* Read an int from the file. */
    104 int read_int_from_file(const char *, int *);
    105 
    106 /* Check whether given uid matches kernel overflowuid. */
    107 void check_overflowuid(const int);
    108 
    109 /* Check whether given gid matches kernel overflowgid. */
    110 void check_overflowgid(const int);
    111 
    112 /* Translate errno to its name. */
    113 const char *errno2name(void);
    114 
    115 /* Translate signal number to its name. */
    116 const char *signal2name(int);
    117 
    118 /* Print return code and, in case return code is -1, errno information. */
    119 const char *sprintrc(long rc);
    120 /* sprintrc variant suitable for usage as part of grep pattern. */
    121 const char *sprintrc_grep(long rc);
    122 
    123 struct xlat;
    124 
    125 /* Print flags in symbolic form according to xlat table. */
    126 int printflags(const struct xlat *, const unsigned long long, const char *);
    127 
    128 /* Print constant in symbolic form according to xlat table. */
    129 int printxval(const struct xlat *, const unsigned long long, const char *);
    130 
    131 /* Invoke a socket syscall, either directly or via __NR_socketcall. */
    132 int socketcall(const int nr, const int call,
    133 	       long a1, long a2, long a3, long a4, long a5);
    134 
    135 /* Wrappers for recvmmsg and sendmmsg syscalls. */
    136 struct mmsghdr;
    137 struct timespec;
    138 int recv_mmsg(int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *);
    139 int send_mmsg(int, struct mmsghdr *, unsigned int, unsigned int);
    140 
    141 /* Create a pipe with maximized descriptor numbers. */
    142 void pipe_maxfd(int pipefd[2]);
    143 
    144 #define F8ILL_KULONG_SUPPORTED	(sizeof(void *) < sizeof(kernel_ulong_t))
    145 #define F8ILL_KULONG_MASK	((kernel_ulong_t) 0xffffffff00000000ULL)
    146 
    147 /*
    148  * For 64-bit kernel_ulong_t and 32-bit pointer,
    149  * return a kernel_ulong_t value by filling higher bits.
    150  * For other architertures, return the original pointer.
    151  */
    152 static inline kernel_ulong_t
    153 f8ill_ptr_to_kulong(const void *const ptr)
    154 {
    155 	const unsigned long uptr = (unsigned long) ptr;
    156 	return F8ILL_KULONG_SUPPORTED
    157 	       ? F8ILL_KULONG_MASK | uptr : (kernel_ulong_t) uptr;
    158 }
    159 
    160 # define ARRAY_SIZE(arg) ((unsigned int) (sizeof(arg) / sizeof((arg)[0])))
    161 # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1)
    162 
    163 /* Zero-extend a signed integer type to unsigned long long. */
    164 #define zero_extend_signed_to_ull(v) \
    165 	(sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
    166 	 sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
    167 	 sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
    168 	 sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
    169 	 (unsigned long long) (v))
    170 
    171 /* Sign-extend an unsigned integer type to long long. */
    172 #define sign_extend_unsigned_to_ll(v) \
    173 	(sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
    174 	 sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
    175 	 sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
    176 	 sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
    177 	 (long long) (v))
    178 
    179 # define SKIP_MAIN_UNDEFINED(arg) \
    180 	int main(void) { error_msg_and_skip("undefined: %s", arg); }
    181 
    182 /*
    183  * The kernel used to define 64-bit types on 64-bit systems on a per-arch
    184  * basis.  Some architectures would use unsigned long and others would use
    185  * unsigned long long.  These types were exported as part of the
    186  * kernel-userspace ABI and now must be maintained forever.  This matches
    187  * what the kernel exports for each architecture so we don't need to cast
    188  * every printing of __u64 or __s64 to stdint types.
    189  */
    190 # if SIZEOF_LONG == 4
    191 #  define PRI__64 "ll"
    192 # elif defined ALPHA || defined IA64 || defined MIPS || defined POWERPC
    193 #  define PRI__64 "l"
    194 # else
    195 #  define PRI__64 "ll"
    196 # endif
    197 
    198 # define PRI__d64 PRI__64"d"
    199 # define PRI__u64 PRI__64"u"
    200 # define PRI__x64 PRI__64"x"
    201 
    202 # if WORDS_BIGENDIAN
    203 #  define LL_PAIR(HI, LO) (HI), (LO)
    204 # else
    205 #  define LL_PAIR(HI, LO) (LO), (HI)
    206 # endif
    207 # define LL_VAL_TO_PAIR(llval) LL_PAIR((long) ((llval) >> 32), (long) (llval))
    208 
    209 # define _STR(_arg) #_arg
    210 # define ARG_STR(_arg) (_arg), #_arg
    211 # define ARG_ULL_STR(_arg) _arg##ULL, #_arg
    212 
    213 #endif /* !STRACE_TESTS_H */
    214