(root)/
strace-6.5/
tests-m32/
tests.h
       1  /*
       2   * Copyright (c) 2016 Dmitry V. Levin <ldv@strace.io>
       3   * Copyright (c) 2016-2023 The strace developers.
       4   * All rights reserved.
       5   *
       6   * SPDX-License-Identifier: GPL-2.0-or-later
       7   */
       8  
       9  #ifndef STRACE_TESTS_H
      10  # define STRACE_TESTS_H
      11  
      12  # ifdef HAVE_CONFIG_H
      13  #  include "config.h"
      14  # endif
      15  
      16  # ifdef TESTS_SIZEOF_KERNEL_LONG_T
      17  #  undef SIZEOF_KERNEL_LONG_T
      18  #  define SIZEOF_KERNEL_LONG_T TESTS_SIZEOF_KERNEL_LONG_T
      19  # endif
      20  
      21  # ifdef TESTS_SIZEOF_LONG
      22  #  undef SIZEOF_LONG
      23  #  define SIZEOF_LONG TESTS_SIZEOF_LONG
      24  # endif
      25  
      26  # include <stdbool.h>
      27  # include <stdint.h>
      28  # include <sys/types.h>
      29  # include "kernel_types.h"
      30  # include "kernel_old_timespec.h"
      31  # include "gcc_compat.h"
      32  # include "macros.h"
      33  
      34  /* Tests of "strace -v" are expected to define VERBOSE to 1. */
      35  # ifndef VERBOSE
      36  #  define VERBOSE 0
      37  # endif
      38  
      39  /* xlat verbosity defaults */
      40  # ifndef XLAT_RAW
      41  #  define XLAT_RAW 0
      42  # endif
      43  # ifndef XLAT_VERBOSE
      44  #  define XLAT_VERBOSE 0
      45  # endif
      46  
      47  
      48  
      49  # if XLAT_RAW
      50  #  define XLAT_KNOWN(val_, str_) STRINGIFY_VAL(val_)
      51  #  define XLAT_UNKNOWN(val_, dflt_) STRINGIFY_VAL(val_)
      52  
      53  #  define XLAT_KNOWN_FMT(val_, str_) val_
      54  #  define XLAT_UNKNOWN_FMT(val_, dflt_) val_
      55  
      56  #  define XLAT_FMT "%#x"
      57  #  define XLAT_FMT_D "%d"
      58  #  define XLAT_FMT_U "%u"
      59  #  define XLAT_FMT_L "%#lx"
      60  #  define XLAT_FMT_LL "%#llx"
      61  #  define XLAT_ARGS(a_) (a_)
      62  #  define XLAT_ARGS_U(a_) (unsigned int) (a_)
      63  #  define XLAT_SEL(v_, s_) v_
      64  
      65  #  define ABBR(...)
      66  #  define RAW(...) __VA_ARGS__
      67  #  define VERB(...)
      68  #  define NABBR(...) __VA_ARGS__
      69  #  define NRAW(...)
      70  #  define NVERB(...) __VA_ARGS__
      71  # elif XLAT_VERBOSE
      72  #  define XLAT_KNOWN(val_, str_) STRINGIFY_VAL(val_) " /* " str_ " */"
      73  #  define XLAT_UNKNOWN(val_, dflt_) STRINGIFY_VAL(val_) " /* " dflt_ " */"
      74  
      75  #  define XLAT_KNOWN_FMT(val_, str_) val_ " /* " str_ " */"
      76  #  define XLAT_UNKNOWN_FMT(val_, dflt_) val_ " /* " dflt_ " */"
      77  
      78  #  define XLAT_FMT "%#x /* %s */"
      79  #  define XLAT_FMT_D "%d /* %s */"
      80  #  define XLAT_FMT_U "%u /* %s */"
      81  #  define XLAT_FMT_L "%#lx /* %s */"
      82  #  define XLAT_FMT_LL "%#llx /* %s */"
      83  #  define XLAT_ARGS(a_) a_, #a_
      84  #  define XLAT_ARGS_U(a_) (unsigned int) (a_), #a_
      85  #  define XLAT_SEL(v_, s_) v_, s_
      86  
      87  #  define ABBR(...)
      88  #  define RAW(...)
      89  #  define VERB(...) __VA_ARGS__
      90  #  define NABBR(...) __VA_ARGS__
      91  #  define NRAW(...) __VA_ARGS__
      92  #  define NVERB(...)
      93  # else /* !XLAT_RAW && !XLAT_VERBOSE */
      94  #  define XLAT_KNOWN(val_, str_) str_
      95  #  define XLAT_UNKNOWN(val_, dflt_) STRINGIFY_VAL(val_) " /* " dflt_ " */"
      96  
      97  #  define XLAT_KNOWN_FMT(val_, str_) str_
      98  #  define XLAT_UNKNOWN_FMT(val_, dflt_) val_ " /* " dflt_ " */"
      99  
     100  #  define XLAT_FMT "%s"
     101  #  define XLAT_FMT_D "%s"
     102  #  define XLAT_FMT_U "%s"
     103  #  define XLAT_FMT_L "%s"
     104  #  define XLAT_FMT_LL "%s"
     105  #  define XLAT_ARGS(a_) #a_
     106  #  define XLAT_ARGS_U(a_) #a_
     107  #  define XLAT_SEL(v_, s_) s_
     108  
     109  #  define ABBR(...) __VA_ARGS__
     110  #  define RAW(...)
     111  #  define VERB(...)
     112  #  define NABBR(...)
     113  #  define NRAW(...) __VA_ARGS__
     114  #  define NVERB(...) __VA_ARGS__
     115  # endif /* XLAT_RAW, XLAT_VERBOSE */
     116  
     117  # define XLAT_STR(v_) sprintxlat(#v_, v_, NULL)
     118  
     119  # define ARG_XLAT_KNOWN(val_, str_) val_, XLAT_KNOWN(val_, str_)
     120  # define ARG_XLAT_UNKNOWN(val_, str_) val_, XLAT_UNKNOWN(val_, str_)
     121  
     122  # define ENUM_KNOWN(val_, enum_) enum_, XLAT_KNOWN(val_, #enum_)
     123  
     124  # ifndef DEFAULT_STRLEN
     125  /* Default maximum # of bytes printed in printstr et al. */
     126  #  define DEFAULT_STRLEN 32
     127  # endif
     128  
     129  struct strval8 {
     130  	uint8_t val;
     131  	const char *str;
     132  };
     133  
     134  struct strval16 {
     135  	uint16_t val;
     136  	const char *str;
     137  };
     138  
     139  struct strval32 {
     140  	uint32_t val;
     141  	const char *str;
     142  };
     143  
     144  struct strval64 {
     145  	uint64_t val;
     146  	const char *str;
     147  };
     148  
     149  /* Cached sysconf(_SC_PAGESIZE). */
     150  size_t get_page_size(void);
     151  
     152  /* The size of kernel's sigset_t. */
     153  unsigned int get_sigset_size(void);
     154  
     155  /* Print message and strerror(errno) to stderr, then exit(1). */
     156  void perror_msg_and_fail(const char *, ...)
     157  	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
     158  /* Print message to stderr, then exit(1). */
     159  void error_msg_and_fail(const char *, ...)
     160  	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
     161  /* Print message to stderr, then exit(77). */
     162  void error_msg_and_skip(const char *, ...)
     163  	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
     164  /* Print message and strerror(errno) to stderr, then exit(77). */
     165  void perror_msg_and_skip(const char *, ...)
     166  	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
     167  
     168  # ifndef perror_msg_and_fail
     169  #  define perror_msg_and_fail(fmt_, ...) \
     170  	perror_msg_and_fail("%s:%d: " fmt_, __FILE__, __LINE__, ##__VA_ARGS__)
     171  # endif
     172  # ifndef error_msg_and_fail
     173  #  define error_msg_and_fail(fmt_, ...) \
     174  	error_msg_and_fail("%s:%d: " fmt_, __FILE__, __LINE__, ##__VA_ARGS__)
     175  # endif
     176  
     177  /* Stat the specified file and skip the test if the stat call failed. */
     178  void skip_if_unavailable(const char *);
     179  
     180  /*
     181   * Obtain a file descriptor corresponding to the specified directory name,
     182   * die on failure.
     183   */
     184  int get_dir_fd(const char *dir_path);
     185  
     186  /*
     187   * Obtain a path corresponding to the specified file descriptor,
     188   * die on failure.
     189   */
     190  char *get_fd_path(int fd) ATTRIBUTE_MALLOC;
     191  
     192  /*
     193   * Create the specified directory and chdir into it,
     194   * die on chdir failure.
     195   */
     196  void create_and_enter_subdir(const char *subdir);
     197  
     198  /*
     199   * Leave from the directory entered by create_and_enter_subdir,
     200   * remove that directory, die on failure.
     201   */
     202  void leave_and_remove_subdir(void);
     203  
     204  /*
     205   * Obtain an exclusive lock on dirname(path_name)/lock_name file
     206   * using open and flock.
     207   */
     208  int lock_file_by_dirname(const char *path_name, const char *lock_name);
     209  
     210  /*
     211   * Allocate memory that ends on the page boundary.
     212   * Pages allocated by this call are preceded by an unmapped page
     213   * and followed also by an unmapped page.
     214   */
     215  void *tail_alloc(const size_t)
     216  	ATTRIBUTE_MALLOC;
     217  /* Allocate memory using tail_alloc, then memcpy. */
     218  void *tail_memdup(const void *, const size_t)
     219  	ATTRIBUTE_MALLOC;
     220  
     221  # define midtail_alloc(after_, before_) \
     222  	((void *) ((char *) tail_alloc(((before_) + (after_))) + (before_)))
     223  
     224  /*
     225   * Allocate an object of the specified type at the end
     226   * of a mapped memory region.
     227   * Assign its address to the specified constant pointer.
     228   */
     229  # define TAIL_ALLOC_OBJECT_CONST_PTR(type_name, type_ptr)	\
     230  	type_name *const type_ptr = tail_alloc(sizeof(*type_ptr))
     231  
     232  /*
     233   * Allocate an array of the specified type at the end
     234   * of a mapped memory region.
     235   * Assign its address to the specified constant pointer.
     236   */
     237  # define TAIL_ALLOC_OBJECT_CONST_ARR(type_name, type_ptr, cnt)	\
     238  	type_name *const type_ptr = tail_alloc(sizeof(*type_ptr) * (cnt))
     239  
     240  /*
     241   * Allocate an object of the specified type at the end
     242   * of a mapped memory region.
     243   * Assign its address to the specified variable pointer.
     244   */
     245  # define TAIL_ALLOC_OBJECT_VAR_PTR(type_name, type_ptr)		\
     246  	type_name *type_ptr = tail_alloc(sizeof(*type_ptr))
     247  
     248  /*
     249   * Allocate an array of the specified type at the end
     250   * of a mapped memory region.
     251   * Assign its address to the specified variable pointer.
     252   */
     253  # define TAIL_ALLOC_OBJECT_VAR_ARR(type_name, type_ptr, cnt)	\
     254  	type_name *type_ptr = tail_alloc(sizeof(*type_ptr) * (cnt))
     255  
     256  /**
     257   * Fill memory (pointed by ptr, having size bytes) with different bytes (with
     258   * values starting with start and resetting every period) in order to catch
     259   * sign, byte order and/or alignment errors.
     260   */
     261  void fill_memory_ex(void *ptr, size_t size, unsigned char start,
     262  		    unsigned int period);
     263  /** Shortcut for fill_memory_ex(ptr, size, 0x80, 0x80) */
     264  void fill_memory(void *ptr, size_t size);
     265  /** Variant of fill_memory_ex for arrays of 16-bit (2-byte) values. */
     266  void fill_memory16_ex(void *ptr, size_t size, uint16_t start,
     267  		      unsigned int period);
     268  /** Shortcut for fill_memory16_ex(ptr, size, 0x80c0, 0x8000) */
     269  void fill_memory16(void *ptr, size_t size);
     270  /** Variant of fill_memory_ex for arrays of 32-bit (4-byte) values. */
     271  void fill_memory32_ex(void *ptr, size_t size, uint32_t start,
     272  		      unsigned int period);
     273  /** Shortcut for fill_memory32_ex(ptr, size, 0x80a0c0e0, 0x80000000) */
     274  void fill_memory32(void *ptr, size_t size);
     275  /** Variant of fill_memory_ex for arrays of 64-bit (8-byte) values. */
     276  void fill_memory64_ex(void *ptr, size_t size, uint64_t start, uint64_t period);
     277  /**
     278   * Shortcut for
     279   * fill_memory64_ex(ptr, size, 0x8090a0b0c0d0e0f0, 0x8000000000000000)
     280   */
     281  void fill_memory64(void *ptr, size_t size);
     282  
     283  
     284  /* Close stdin, move stdout to a non-standard descriptor, and print. */
     285  void tprintf(const char *, ...)
     286  	ATTRIBUTE_FORMAT((printf, 1, 2));
     287  
     288  /* Make a hexdump copy of C string */
     289  const char *hexdump_strdup(const char *);
     290  
     291  /* Make a hexdump copy of memory */
     292  const char *hexdump_memdup(const char *, size_t);
     293  
     294  /* Make a hexquoted copy of a string */
     295  const char *hexquote_strndup(const char *, size_t);
     296  
     297  /* Return inode number of socket descriptor. */
     298  unsigned long inode_of_sockfd(int);
     299  
     300  /* Print string in a quoted form with optional escape characters. */
     301  void print_quoted_string_ex(const char *, bool quote, const char *escape_str);
     302  
     303  /* Print string in a quoted form. */
     304  void print_quoted_string(const char *);
     305  
     306  /*
     307   * Print a NUL-terminated string `str' of length up to `size' - 1
     308   * in a quoted form.
     309   */
     310  void print_quoted_cstring(const char *str, size_t size);
     311  
     312  /*
     313   * Print a NUL-terminated string `str' of length up to `size'
     314   * in a quoted form.
     315   */
     316  void print_quoted_stringn(const char *str, size_t size);
     317  
     318  /* Print memory in a quoted form with optional escape characters. */
     319  void print_quoted_memory_ex(const void *, size_t, bool quote,
     320  			    const char *escape_chars);
     321  
     322  /* Print memory in a quoted form. */
     323  void print_quoted_memory(const void *, size_t);
     324  
     325  /* Print memory in a hexquoted form. */
     326  void print_quoted_hex(const void *, size_t);
     327  
     328  /* Print time_t and nanoseconds in symbolic format. */
     329  void print_time_t_nsec(time_t, unsigned long long, int);
     330  
     331  /* Print time_t and microseconds in symbolic format. */
     332  void print_time_t_usec(time_t, unsigned long long, int);
     333  
     334  /* Put a formatted clock_t string representation into a string. */
     335  const char *clock_t_str(uint64_t val, char *str, size_t str_size);
     336  
     337  /* Read an int from the file. */
     338  int read_int_from_file(const char *, int *);
     339  
     340  /* Check whether given uid matches kernel overflowuid. */
     341  void check_overflowuid(const int);
     342  
     343  /* Check whether given gid matches kernel overflowgid. */
     344  void check_overflowgid(const int);
     345  
     346  /* Translate errno to its name. */
     347  const char *errno2name(void);
     348  
     349  /* Translate signal number to its name. */
     350  const char *signal2name(int);
     351  
     352  /* Print return code and, in case return code is -1, errno information. */
     353  const char *sprintrc(long rc);
     354  /* sprintrc variant suitable for usage as part of grep pattern. */
     355  const char *sprintrc_grep(long rc);
     356  
     357  struct xlat;
     358  
     359  /* Print flags in symbolic form according to xlat table. */
     360  int printflags(const struct xlat *, const unsigned long long, const char *);
     361  
     362  /* Print constant in symbolic form according to xlat table. */
     363  int printxval_abbrev(const struct xlat *, const unsigned long long,
     364  		     const char *);
     365  int printxval_raw(const struct xlat *, const unsigned long long, const char *);
     366  int printxval_verbose(const struct xlat *, const unsigned long long,
     367  		      const char *);
     368  
     369  /* Print constant in symbolic form according to xlat table. */
     370  const char *sprintxlat_abbrev(const char *, const unsigned long long,
     371  			   const char *);
     372  const char *sprintxlat_raw(const char *, const unsigned long long,
     373  			   const char *);
     374  const char *sprintxlat_verbose(const char *, const unsigned long long,
     375  			       const char *);
     376  
     377  /* Print constant in symbolic form according to xlat table. */
     378  const char *sprintxval_abbrev(const struct xlat *, const unsigned long long,
     379  			      const char *);
     380  const char *sprintxval_raw(const struct xlat *, const unsigned long long,
     381  			   const char *);
     382  const char *sprintxval_verbose(const struct xlat *, const unsigned long long,
     383  			       const char *);
     384  
     385  # if XLAT_RAW
     386  #  define printxval  printxval_raw
     387  #  define sprintxlat sprintxlat_raw
     388  #  define sprintxval sprintxval_raw
     389  # elif XLAT_VERBOSE
     390  #  define printxval  printxval_verbose
     391  #  define sprintxlat sprintxlat_verbose
     392  #  define sprintxval sprintxval_verbose
     393  # else
     394  #  define printxval  printxval_abbrev
     395  #  define sprintxlat sprintxlat_abbrev
     396  #  define sprintxval sprintxval_abbrev
     397  # endif
     398  
     399  /* Invoke a socket syscall, either directly or via __NR_socketcall. */
     400  int socketcall(const int nr, const int call,
     401  	       long a1, long a2, long a3, long a4, long a5);
     402  
     403  /* Invoke a prctl syscall with very specific arguments for use as a marker.  */
     404  long prctl_marker(void);
     405  
     406  /* Call chdir and print strace output depending on flags. */
     407  void test_status_chdir(const char *dir, bool print_success, bool print_fail);
     408  
     409  /* Wrappers for recvmmsg and sendmmsg syscalls. */
     410  struct mmsghdr;
     411  int recv_mmsg(int, struct mmsghdr *, unsigned int, unsigned int, kernel_old_timespec_t *);
     412  int send_mmsg(int, struct mmsghdr *, unsigned int, unsigned int);
     413  
     414  /* Create a netlink socket. */
     415  int create_nl_socket_ext(int proto, const char *name);
     416  # define create_nl_socket(proto)	create_nl_socket_ext((proto), #proto)
     417  
     418  /* Create a temporary file in the current directory. */
     419  int create_tmpfile(unsigned int flags);
     420  
     421  /* Create a pipe with maximized descriptor numbers. */
     422  void pipe_maxfd(int pipefd[2]);
     423  
     424  /* if_nametoindex("lo") */
     425  unsigned int ifindex_lo(void);
     426  
     427  # ifdef HAVE_IF_INDEXTONAME
     428  #  define IFINDEX_LO_STR "if_nametoindex(\"lo\")"
     429  # else
     430  #  define IFINDEX_LO_STR "1"
     431  # endif
     432  
     433  # define F8ILL_KULONG_SUPPORTED	(sizeof(void *) < sizeof(kernel_ulong_t))
     434  # define F8ILL_KULONG_MASK	((kernel_ulong_t) 0xffffffff00000000ULL)
     435  
     436  /*
     437   * For 64-bit kernel_ulong_t and 32-bit pointer,
     438   * return a kernel_ulong_t value by filling higher bits.
     439   * For other architectures, return the original pointer.
     440   */
     441  static inline kernel_ulong_t
     442  f8ill_ptr_to_kulong(const void *const ptr)
     443  {
     444  	const unsigned long uptr = (unsigned long) ptr;
     445  	return F8ILL_KULONG_SUPPORTED
     446  	       ? F8ILL_KULONG_MASK | uptr : (kernel_ulong_t) uptr;
     447  }
     448  
     449  # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1)
     450  
     451  /* Zero-extend a signed integer type to unsigned long long. */
     452  # define zero_extend_signed_to_ull(v) \
     453  	(sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
     454  	 sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
     455  	 sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
     456  	 sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
     457  	 (unsigned long long) (v))
     458  
     459  /* Sign-extend an unsigned integer type to long long. */
     460  # define sign_extend_unsigned_to_ll(v) \
     461  	(sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
     462  	 sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
     463  	 sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
     464  	 sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
     465  	 (long long) (v))
     466  
     467  # define SKIP_MAIN_UNDEFINED(arg) \
     468  	int main(void) { error_msg_and_skip("undefined: %s", arg); }
     469  
     470  # ifdef WORDS_BIGENDIAN
     471  #  define BE_LE(be_, le_) be_
     472  #  define LL_PAIR(HI, LO) (HI), (LO)
     473  # else
     474  #  define BE_LE(be_, le_) le_
     475  #  define LL_PAIR(HI, LO) (LO), (HI)
     476  # endif
     477  # define LL_VAL_TO_PAIR(llval) LL_PAIR((long) ((llval) >> 32), (long) (llval))
     478  
     479  # define ARG_STR(_arg) (_arg), #_arg
     480  # define ARG_ULL_STR(_arg) _arg##ULL, #_arg
     481  
     482  /*
     483   * Assign an object of type DEST_TYPE at address DEST_ADDR
     484   * using memcpy to avoid potential unaligned access.
     485   */
     486  # define SET_STRUCT(DEST_TYPE, DEST_ADDR, ...)						\
     487  	do {										\
     488  		DEST_TYPE dest_type_tmp_var = { __VA_ARGS__ };				\
     489  		memcpy(DEST_ADDR, &dest_type_tmp_var, sizeof(dest_type_tmp_var));	\
     490  	} while (0)
     491  
     492  # define NLMSG_ATTR(nlh, hdrlen) ((void *)(nlh) + NLMSG_SPACE(hdrlen))
     493  
     494  #endif /* !STRACE_TESTS_H */