Test errno setup

Message ID 1487240107-32578-1-git-send-email-ynorov@caviumnetworks.com
State New, archived
Headers

Commit Message

Yury Norov Feb. 16, 2017, 10:15 a.m. UTC
  This patch adds tests for POSIX and Linux specific syscalls
that implemented with syscall templates machinery. The reason
of tests is to receive the expected error code and test if
it's handled properly by glibc code.
 
Yury Norov <ynorov@caviumnetworks.com>
Zack Weinberg <zackw@panix.com>

	* posix/Makefile: Enable errno test for POSIX syscalls.
	* posix/test-errno.c: New file.
	* sysdeps/unix/sysv/linux/Makefile: Enable errno test for Linux syscalls.
	* sysdeps/unix/sysv/linux/test-errno.c: New file.

---
 posix/Makefile                       |   2 +-
 posix/test-errno.c                   | 137 +++++++++++++++++++++++++++++++++++
 sysdeps/unix/sysv/linux/Makefile     |   2 +-
 sysdeps/unix/sysv/linux/test-errno.c | 126 ++++++++++++++++++++++++++++++++
 4 files changed, 265 insertions(+), 2 deletions(-)
 create mode 100644 posix/test-errno.c
 create mode 100644 sysdeps/unix/sysv/linux/test-errno.c
  

Comments

Wainer dos Santos Moschetta Feb. 16, 2017, 11:24 a.m. UTC | #1
On 02/16/2017 08:15 AM, Yury Norov wrote:
> This patch adds tests for POSIX and Linux specific syscalls
> that implemented with syscall templates machinery. The reason
> of tests is to receive the expected error code and test if
> it's handled properly by glibc code.
>
> Yury Norov <ynorov@caviumnetworks.com>
> Zack Weinberg <zackw@panix.com>
>
> 	* posix/Makefile: Enable errno test for POSIX syscalls.
> 	* posix/test-errno.c: New file.
> 	* sysdeps/unix/sysv/linux/Makefile: Enable errno test for Linux syscalls.
> 	* sysdeps/unix/sysv/linux/test-errno.c: New file.
>
> ---
>  posix/Makefile                       |   2 +-
>  posix/test-errno.c                   | 137 +++++++++++++++++++++++++++++++++++
>  sysdeps/unix/sysv/linux/Makefile     |   2 +-
>  sysdeps/unix/sysv/linux/test-errno.c | 126 ++++++++++++++++++++++++++++++++
>  4 files changed, 265 insertions(+), 2 deletions(-)
>  create mode 100644 posix/test-errno.c
>  create mode 100644 sysdeps/unix/sysv/linux/test-errno.c
>
> diff --git a/posix/Makefile b/posix/Makefile
> index c493f31..1359a64 100644
> --- a/posix/Makefile
> +++ b/posix/Makefile
> @@ -62,7 +62,7 @@ routines :=								      \
>  	get_child_max sched_cpucount sched_cpualloc sched_cpufree
>
>  aux		:= init-posix environ
> -tests		:= tstgetopt testfnm runtests runptests	     \
> +tests		:= test-errno tstgetopt testfnm runtests runptests \
>  		   tst-preadwrite tst-preadwrite64 test-vfork regexbug1 \
>  		   tst-mmap tst-mmap-offset tst-getaddrinfo tst-truncate \
>  		   tst-truncate64 tst-fork tst-fnmatch tst-regexloc tst-dir \
> diff --git a/posix/test-errno.c b/posix/test-errno.c
> new file mode 100644
> index 0000000..7af2490
> --- /dev/null
> +++ b/posix/test-errno.c

This file misses the FSF copyright header.

> @@ -0,0 +1,137 @@
> +#include <errno.h>
> +#include <limits.h>
> +#include <grp.h>
> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <sys/ioctl.h>
> +#include <sys/socket.h>
> +#include <sys/stat.h>
> +#include <sys/time.h>
> +#include <sys/resource.h>
> +#include <sys/types.h>
> +#include <sys/statfs.h>
> +#include <sys/mman.h>
> +#include <unistd.h>
> +#include <netinet/in.h>
> +
> +/* Test that failing system calls do set errno to the correct value.
> +
> +   This is not an exhaustive test: only system calls that can be
> +   persuaded to fail with a consistent error code and no side effects
> +   are included.  Usually these are failures due to invalid arguments,
> +   with errno code EBADF or EINVAL.  The order of argument checks is
> +   unspecified, so we must take care to provide arguments that only
> +   allow _one_ failure mode.
> +
> +   Note that all system calls that can fail with EFAULT are permitted
> +   to deliver a SIGSEGV signal instead, so we avoid supplying invalid
> +   pointers in general, and we do not attempt to test system calls
> +   that can only fail with EFAULT (e.g. gettimeofday, gethostname).
> +
> +   Also note that root-only system calls (e.g. acct, reboot) may, when
> +   the test is run as an unprivileged user, fail due to insufficient
> +   privileges before bothering to do argument checks, so those are not
> +   tested either.
> +
> +   Also, system calls that take enum or a set of flags as argument is
> +   not tested if POSIX doesn't specify exact digital values for all
> +   flags, and so any value passed to flags may become valid.
> +
> +   Some tests assume "/bin/sh" names a file that exists and is not a
> +   directory.  */
> +
> +#define test_wrp_rv(rtype, prtype, experr, syscall, ...)	\
> +  (__extension__ ({						\
> +    errno = 0xdead;						\
> +    rtype ret = syscall (__VA_ARGS__);				\
> +    int err = errno;						\
> +    int fail;							\
> +    if (ret == (rtype) -1 && err == experr)			\
> +      fail = 0;							\
> +    else							\
> +      {								\
> +        fail = 1;						\
> +        if (ret != (rtype) -1)					\
> +          fprintf (stderr, #syscall ": didn't fail as expected"	\
> +               " (return "prtype")\n", ret);			\
> +        else if (err == 0xdead)					\
> +          fputs(#syscall ": didn't update errno\n", stderr);	\
> +        else if (err != experr)					\
> +          fprintf (stderr, #syscall				\
> +               ": errno is: %d (%s) expected: %d (%s)\n",	\
> +               err, strerror (err), experr, strerror (experr));	\
> +      }								\
> +    fail;							\
> +  }))
> +
> +#define test_wrp(experr, syscall, ...)				\
> +  test_wrp_rv(int, "%d", experr, syscall, __VA_ARGS__)
> +
> +int
> +do_test (void)
> +{
> +  size_t pagesize = sysconf (_SC_PAGESIZE);
> +  struct statfs sfs;
> +  struct sockaddr sa;
> +  socklen_t sl;
> +  char buf[1];
> +  struct iovec iov[1] = { { buf, 1 } };
> +  struct sockaddr_in sin;
> +  sin.sin_family = AF_INET;
> +  sin.sin_port = htons (1026);
> +  sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
> +  struct msghdr msg;
> +  memset(&msg, 0, sizeof msg);
> +  msg.msg_iov = iov;
> +  msg.msg_iovlen = 1;
> +
> +  int fails = 0;
> +  fails |= test_wrp (EBADF, accept, -1, &sa, &sl);
> +  fails |= test_wrp (EINVAL, access, "/", -1);
> +  fails |= test_wrp (EBADF, bind, -1, (struct sockaddr *)&sin, sizeof sin);
> +  fails |= test_wrp (ENOTDIR, chdir, "/bin/sh");
> +  fails |= test_wrp (EBADF, close, -1);
> +  fails |= test_wrp (EBADF, connect, -1, (struct sockaddr *)&sin, sizeof sin);
> +  fails |= test_wrp (EBADF, dup, -1);
> +  fails |= test_wrp (EBADF, dup2, -1, -1);
> +  fails |= test_wrp (EBADF, fchdir, -1);
> +  fails |= test_wrp (EBADF, fchmod, -1, 0);
> +  fails |= test_wrp (EBADF, fcntl, -1, 0);
> +  fails |= test_wrp (EBADF, fstatfs, -1, &sfs);
> +  fails |= test_wrp (EBADF, fsync, -1);
> +  fails |= test_wrp (EBADF, ftruncate, -1, 0);
> +  fails |= test_wrp (EINVAL, getgroups, -1, 0);
> +  fails |= test_wrp (EBADF, getpeername, -1, &sa, &sl);
> +  fails |= test_wrp (EBADF, getsockname, -1, &sa, &sl);
> +  fails |= test_wrp (EBADF, getsockopt, -1, 0, 0, buf, &sl);
> +  fails |= test_wrp (EBADF, ioctl, -1, TIOCNOTTY);
> +  fails |= test_wrp (EBADF, listen, -1, 1);
> +  fails |= test_wrp (EBADF, lseek, -1, 0, 0);
> +  fails |= test_wrp (EINVAL, madvise, (void *) -1, -1, 0);
> +  fails |= test_wrp_rv (void *, "%p", EBADF,
> +                        mmap, 0, pagesize, PROT_READ, MAP_PRIVATE, -1, 0);
> +  fails |= test_wrp (EINVAL, mprotect, (void *) -1, pagesize, -1);
> +  fails |= test_wrp (EINVAL, msync, (void *) -1, pagesize, -1);
> +  fails |= test_wrp (EINVAL, munmap, (void *) -1, 0);
> +  fails |= test_wrp (EINVAL, open, "/bin/sh", -1, 0);
> +  fails |= test_wrp (EBADF, read, -1, buf, 1);
> +  fails |= test_wrp (EINVAL, readlink, "/", buf, -1);
> +  fails |= test_wrp (EBADF, readv, -1, iov, 1);
> +  fails |= test_wrp (EBADF, recv, -1, buf, 1, 0);
> +  fails |= test_wrp (EBADF, recvfrom, -1, buf, 1, 0, &sa, &sl);
> +  fails |= test_wrp (EBADF, recvmsg, -1, &msg, 0);
> +  fails |= test_wrp (EINVAL, select, -1, 0, 0, 0, 0);
> +  fails |= test_wrp (EBADF, send, -1, buf, 1, 0);
> +  fails |= test_wrp (EBADF, sendmsg, -1, &msg, 0);
> +  fails |= test_wrp (EBADF, sendto, -1, buf, 1, 0, &sa, sl);
> +  fails |= test_wrp (EBADF, setsockopt, -1, 0, 0, buf, sl);
> +  fails |= test_wrp (EBADF, shutdown, -1, SHUT_RD);
> +  fails |= test_wrp (EBADF, write, -1, "Hello", sizeof ("Hello") );
> +  fails |= test_wrp (EBADF, writev, -1, iov, 1 );
> +
> +  return fails;
> +}
> +
> +#define TEST_FUNCTION do_test ()
> +#include "test-skeleton.c"

You could use the new support/test-driver.c.

> diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
> index 8c4cfd0..cd11f7c 100644
> --- a/sysdeps/unix/sysv/linux/Makefile
> +++ b/sysdeps/unix/sysv/linux/Makefile
> @@ -43,7 +43,7 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
>  		  bits/mman-linux.h
>
>  tests += tst-clone tst-clone2 tst-fanotify tst-personality tst-quota \
> -	 tst-sync_file_range tst-syscall-template
> +	 tst-sync_file_range tst-syscall-template test-errno
>
>  # Generate the list of SYS_* macros for the system calls (__NR_* macros).
>
> diff --git a/sysdeps/unix/sysv/linux/test-errno.c b/sysdeps/unix/sysv/linux/test-errno.c
> new file mode 100644
> index 0000000..5a2f8a4
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/test-errno.c

FSF copyright header missing here too.

> @@ -0,0 +1,126 @@
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <mqueue.h>
> +#include <sched.h>
> +#include <signal.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <time.h>
> +#include <unistd.h>
> +#include <sys/epoll.h>
> +#include <sys/eventfd.h>
> +#include <sys/file.h>
> +#include <sys/fsuid.h>
> +#include <sys/inotify.h>
> +#include <sys/mman.h>
> +#include <sys/poll.h>
> +#include <sys/quota.h>
> +#include <sys/resource.h>
> +#include <sys/select.h>
> +#include <sys/sendfile.h>
> +#include <sys/swap.h>
> +#include <sys/time.h>
> +#include <sys/types.h>
> +#include <sys/wait.h>
> +
> +/* Test that failing system calls do set errno to the correct value.
> +
> +   This is not an exhaustive test: only system calls that can be
> +   persuaded to fail with a consistent error code and no side effects
> +   are included.  Usually these are failures due to invalid arguments,
> +   with errno code EBADF or EINVAL.  The order of argument checks is
> +   unspecified, so we must take care to provide arguments that only
> +   allow _one_ failure mode.
> +
> +   Note that all system calls that can fail with EFAULT are permitted
> +   to deliver a SIGSEGV signal instead, so we avoid supplying invalid
> +   pointers in general, and we do not attempt to test system calls
> +   that can only fail with EFAULT (e.g. gettimeofday, gethostname).
> +
> +   Also note that root-only system calls (e.g. acct, reboot) may, when
> +   the test is run as an unprivileged user, fail due to insufficient
> +   privileges before bothering to do argument checks, so those are not
> +   tested either.
> +
> +   Also, system calls that take enum or a set of flags as argument is
> +   not tested if POSIX doesn't specify exact digital values for all
> +   flags, and so any value passed to flags may become valid.
> +
> +   Some tests assume "/bin/sh" names a file that exists and is not a
> +   directory.  */
> +
> +#define test_wrp_rv(rtype, prtype, experr, syscall, ...)	\
> +  (__extension__ ({						\
> +    errno = 0xdead;						\
> +    rtype ret = syscall (__VA_ARGS__);				\
> +    int err = errno;						\
> +    int fail;							\
> +    if (ret == (rtype) -1 && err == experr)			\
> +      fail = 0;							\
> +    else							\
> +      {								\
> +        fail = 1;						\
> +        if (ret != (rtype) -1)					\
> +          printf (#syscall ": didn't fail as expected"		\
> +               " (return "prtype")\n", ret);			\
> +        else if (err == 0xdead)					\
> +          puts(#syscall ": didn't update errno\n");		\
> +        else if (err != experr)					\
> +          printf (#syscall					\
> +               ": errno is: %d (%s) expected: %d (%s)\n",	\
> +               err, strerror (err), experr, strerror (experr));	\
> +      }								\
> +    fail;							\
> +  }))
> +
> +#define test_wrp(experr, syscall, ...)				\
> +  test_wrp_rv(int, "%d", experr, syscall, __VA_ARGS__)
> +
> +static int
> +do_test (void)
> +{
> +  fd_set rs, ws, es;
> +  int status;
> +  off_t off;
> +  stack_t ss;
> +  struct dqblk dqblk;
> +  struct epoll_event epoll_event;
> +  struct pollfd pollfd;
> +  struct sched_param sch_param;
> +  struct timespec ts;
> +  struct timeval tv;
> +  unsigned char vec[16];
> +  ss.ss_flags = ~SS_DISABLE;
> +  ts.tv_sec = -1;
> +
> +  int fails = 0;
> +  fails |= test_wrp (EINVAL, epoll_create, -1);
> +  fails |= test_wrp (EINVAL, epoll_create1, EPOLL_CLOEXEC + 1);
> +  fails |= test_wrp (EBADF, epoll_ctl, -1, EPOLL_CTL_ADD, 0, &epoll_event);
> +  fails |= test_wrp (EBADF, epoll_wait, -1, &epoll_event, 1, 1);
> +  fails |= test_wrp (EBADF, fdatasync, -1);
> +  fails |= test_wrp (EBADF, flock, -1, LOCK_SH);
> +  fails |= test_wrp (ESRCH, getpgid, -1);
> +  fails |= test_wrp (EINVAL, inotify_add_watch, -1, "/", 0);
> +  fails |= test_wrp (EINVAL, mincore, (void *) -1, 0, vec);
> +  fails |= test_wrp (EINVAL, mlock, (void *) -1, 1); // different errors
> +  fails |= test_wrp (EINVAL, nanosleep, &ts, &ts);
> +  fails |= test_wrp (EINVAL, poll, &pollfd, -1, 0);
> +  fails |= test_wrp (ENODEV, quotactl, Q_GETINFO, NULL, -1, (caddr_t) &dqblk);
> +  fails |= test_wrp (EINVAL, sched_getparam, -1, &sch_param);
> +  fails |= test_wrp (EINVAL, sched_getscheduler, -1);
> +  fails |= test_wrp (EINVAL, sched_get_priority_max, -1);
> +  fails |= test_wrp (EINVAL, sched_get_priority_min, -1);
> +  fails |= test_wrp (EINVAL, sched_rr_get_interval, -1, &ts);
> +  fails |= test_wrp (EINVAL, sched_setparam, -1, &sch_param);
> +  fails |= test_wrp (EINVAL, sched_setscheduler, -1, 0, &sch_param);
> +  fails |= test_wrp (EINVAL, select, -1, &rs, &ws, &es, &tv);
> +  fails |= test_wrp (EBADF, sendfile, -1, -1, &off, 0);
> +  fails |= test_wrp (EINVAL, sigaltstack, &ss, NULL);
> +  fails |= test_wrp (ECHILD, wait4, -1, &status, 0, NULL);
> +
> +  return fails;
> +}
> +
> +#define TEST_FUNCTION do_test ()
> +#include "test-skeleton.c"

Could use the support/test-driver.c here too.
  
Carlos O'Donell Feb. 16, 2017, 5:10 p.m. UTC | #2
On 02/16/2017 05:15 AM, Yury Norov wrote:
> This patch adds tests for POSIX and Linux specific syscalls
> that implemented with syscall templates machinery. The reason
> of tests is to receive the expected error code and test if
> it's handled properly by glibc code.
>  
> Yury Norov <ynorov@caviumnetworks.com>
> Zack Weinberg <zackw@panix.com>
> 
> 	* posix/Makefile: Enable errno test for POSIX syscalls.
> 	* posix/test-errno.c: New file.
> 	* sysdeps/unix/sysv/linux/Makefile: Enable errno test for Linux syscalls.
> 	* sysdeps/unix/sysv/linux/test-errno.c: New file.

Skipping those comments that Werner already made.

You should use the new support/ infrastructure, and you need copyright headers.

Look forward to v2.

> ---
>  posix/Makefile                       |   2 +-
>  posix/test-errno.c                   | 137 +++++++++++++++++++++++++++++++++++
>  sysdeps/unix/sysv/linux/Makefile     |   2 +-
>  sysdeps/unix/sysv/linux/test-errno.c | 126 ++++++++++++++++++++++++++++++++
>  4 files changed, 265 insertions(+), 2 deletions(-)
>  create mode 100644 posix/test-errno.c
>  create mode 100644 sysdeps/unix/sysv/linux/test-errno.c
> 
> diff --git a/posix/Makefile b/posix/Makefile
> index c493f31..1359a64 100644
> --- a/posix/Makefile
> +++ b/posix/Makefile
> @@ -62,7 +62,7 @@ routines :=								      \
>  	get_child_max sched_cpucount sched_cpualloc sched_cpufree
>  
>  aux		:= init-posix environ
> -tests		:= tstgetopt testfnm runtests runptests	     \
> +tests		:= test-errno tstgetopt testfnm runtests runptests \
>  		   tst-preadwrite tst-preadwrite64 test-vfork regexbug1 \
>  		   tst-mmap tst-mmap-offset tst-getaddrinfo tst-truncate \
>  		   tst-truncate64 tst-fork tst-fnmatch tst-regexloc tst-dir \
> diff --git a/posix/test-errno.c b/posix/test-errno.c
> new file mode 100644
> index 0000000..7af2490
> --- /dev/null
> +++ b/posix/test-errno.c
> @@ -0,0 +1,137 @@
> +#include <errno.h>
> +#include <limits.h>
> +#include <grp.h>
> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <sys/ioctl.h>
> +#include <sys/socket.h>
> +#include <sys/stat.h>
> +#include <sys/time.h>
> +#include <sys/resource.h>
> +#include <sys/types.h>
> +#include <sys/statfs.h>
> +#include <sys/mman.h>
> +#include <unistd.h>
> +#include <netinet/in.h>
> +
> +/* Test that failing system calls do set errno to the correct value.
> +
> +   This is not an exhaustive test: only system calls that can be
> +   persuaded to fail with a consistent error code and no side effects
> +   are included.  Usually these are failures due to invalid arguments,
> +   with errno code EBADF or EINVAL.  The order of argument checks is
> +   unspecified, so we must take care to provide arguments that only
> +   allow _one_ failure mode.
> +
> +   Note that all system calls that can fail with EFAULT are permitted
> +   to deliver a SIGSEGV signal instead, so we avoid supplying invalid
> +   pointers in general, and we do not attempt to test system calls
> +   that can only fail with EFAULT (e.g. gettimeofday, gethostname).
> +
> +   Also note that root-only system calls (e.g. acct, reboot) may, when
> +   the test is run as an unprivileged user, fail due to insufficient
> +   privileges before bothering to do argument checks, so those are not
> +   tested either.
> +
> +   Also, system calls that take enum or a set of flags as argument is
> +   not tested if POSIX doesn't specify exact digital values for all

s/digital/binary/g

> +   flags, and so any value passed to flags may become valid.
> +
> +   Some tests assume "/bin/sh" names a file that exists and is not a
> +   directory.  */
> +
> +#define test_wrp_rv(rtype, prtype, experr, syscall, ...)	\
> +  (__extension__ ({						\
> +    errno = 0xdead;						\
> +    rtype ret = syscall (__VA_ARGS__);				\
> +    int err = errno;						\
> +    int fail;							\
> +    if (ret == (rtype) -1 && err == experr)			\
> +      fail = 0;							\
> +    else							\
> +      {								\
> +        fail = 1;						\
> +        if (ret != (rtype) -1)					\
> +          fprintf (stderr, #syscall ": didn't fail as expected"	\
> +               " (return "prtype")\n", ret);			\
> +        else if (err == 0xdead)					\
> +          fputs(#syscall ": didn't update errno\n", stderr);	\
> +        else if (err != experr)					\
> +          fprintf (stderr, #syscall				\
> +               ": errno is: %d (%s) expected: %d (%s)\n",	\
> +               err, strerror (err), experr, strerror (experr));	\

Please prefix these line with "FAIL:" or "INFO:" to make
it easier to distinguish which output represents a substantive
sub-test failure.

> +      }								\
> +    fail;							\
> +  }))
> +
> +#define test_wrp(experr, syscall, ...)				\
> +  test_wrp_rv(int, "%d", experr, syscall, __VA_ARGS__)

Please use a static function and avoid macros which make the test
hard to debug. We don't have any performance or function call 
issues here so I think the macros should be avoided.

> +
> +int
> +do_test (void)
> +{
> +  size_t pagesize = sysconf (_SC_PAGESIZE);
> +  struct statfs sfs;
> +  struct sockaddr sa;
> +  socklen_t sl;
> +  char buf[1];
> +  struct iovec iov[1] = { { buf, 1 } };
> +  struct sockaddr_in sin;
> +  sin.sin_family = AF_INET;
> +  sin.sin_port = htons (1026);
> +  sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
> +  struct msghdr msg;
> +  memset(&msg, 0, sizeof msg);
> +  msg.msg_iov = iov;
> +  msg.msg_iovlen = 1;
> +
> +  int fails = 0;
> +  fails |= test_wrp (EBADF, accept, -1, &sa, &sl);
> +  fails |= test_wrp (EINVAL, access, "/", -1);
> +  fails |= test_wrp (EBADF, bind, -1, (struct sockaddr *)&sin, sizeof sin);
> +  fails |= test_wrp (ENOTDIR, chdir, "/bin/sh");
> +  fails |= test_wrp (EBADF, close, -1);
> +  fails |= test_wrp (EBADF, connect, -1, (struct sockaddr *)&sin, sizeof sin);
> +  fails |= test_wrp (EBADF, dup, -1);
> +  fails |= test_wrp (EBADF, dup2, -1, -1);
> +  fails |= test_wrp (EBADF, fchdir, -1);
> +  fails |= test_wrp (EBADF, fchmod, -1, 0);
> +  fails |= test_wrp (EBADF, fcntl, -1, 0);
> +  fails |= test_wrp (EBADF, fstatfs, -1, &sfs);
> +  fails |= test_wrp (EBADF, fsync, -1);
> +  fails |= test_wrp (EBADF, ftruncate, -1, 0);
> +  fails |= test_wrp (EINVAL, getgroups, -1, 0);
> +  fails |= test_wrp (EBADF, getpeername, -1, &sa, &sl);
> +  fails |= test_wrp (EBADF, getsockname, -1, &sa, &sl);
> +  fails |= test_wrp (EBADF, getsockopt, -1, 0, 0, buf, &sl);
> +  fails |= test_wrp (EBADF, ioctl, -1, TIOCNOTTY);
> +  fails |= test_wrp (EBADF, listen, -1, 1);
> +  fails |= test_wrp (EBADF, lseek, -1, 0, 0);
> +  fails |= test_wrp (EINVAL, madvise, (void *) -1, -1, 0);
> +  fails |= test_wrp_rv (void *, "%p", EBADF,
> +                        mmap, 0, pagesize, PROT_READ, MAP_PRIVATE, -1, 0);
> +  fails |= test_wrp (EINVAL, mprotect, (void *) -1, pagesize, -1);
> +  fails |= test_wrp (EINVAL, msync, (void *) -1, pagesize, -1);
> +  fails |= test_wrp (EINVAL, munmap, (void *) -1, 0);
> +  fails |= test_wrp (EINVAL, open, "/bin/sh", -1, 0);
> +  fails |= test_wrp (EBADF, read, -1, buf, 1);
> +  fails |= test_wrp (EINVAL, readlink, "/", buf, -1);
> +  fails |= test_wrp (EBADF, readv, -1, iov, 1);
> +  fails |= test_wrp (EBADF, recv, -1, buf, 1, 0);
> +  fails |= test_wrp (EBADF, recvfrom, -1, buf, 1, 0, &sa, &sl);
> +  fails |= test_wrp (EBADF, recvmsg, -1, &msg, 0);
> +  fails |= test_wrp (EINVAL, select, -1, 0, 0, 0, 0);
> +  fails |= test_wrp (EBADF, send, -1, buf, 1, 0);
> +  fails |= test_wrp (EBADF, sendmsg, -1, &msg, 0);
> +  fails |= test_wrp (EBADF, sendto, -1, buf, 1, 0, &sa, sl);
> +  fails |= test_wrp (EBADF, setsockopt, -1, 0, 0, buf, sl);
> +  fails |= test_wrp (EBADF, shutdown, -1, SHUT_RD);
> +  fails |= test_wrp (EBADF, write, -1, "Hello", sizeof ("Hello") );
> +  fails |= test_wrp (EBADF, writev, -1, iov, 1 );
> +
> +  return fails;
> +}
> +
> +#define TEST_FUNCTION do_test ()
> +#include "test-skeleton.c"
> diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
> index 8c4cfd0..cd11f7c 100644
> --- a/sysdeps/unix/sysv/linux/Makefile
> +++ b/sysdeps/unix/sysv/linux/Makefile
> @@ -43,7 +43,7 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
>  		  bits/mman-linux.h
>  
>  tests += tst-clone tst-clone2 tst-fanotify tst-personality tst-quota \
> -	 tst-sync_file_range tst-syscall-template
> +	 tst-sync_file_range tst-syscall-template test-errno
>  
>  # Generate the list of SYS_* macros for the system calls (__NR_* macros).
>  
> diff --git a/sysdeps/unix/sysv/linux/test-errno.c b/sysdeps/unix/sysv/linux/test-errno.c
> new file mode 100644
> index 0000000..5a2f8a4
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/test-errno.c
> @@ -0,0 +1,126 @@
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <mqueue.h>
> +#include <sched.h>
> +#include <signal.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <time.h>
> +#include <unistd.h>
> +#include <sys/epoll.h>
> +#include <sys/eventfd.h>
> +#include <sys/file.h>
> +#include <sys/fsuid.h>
> +#include <sys/inotify.h>
> +#include <sys/mman.h>
> +#include <sys/poll.h>
> +#include <sys/quota.h>
> +#include <sys/resource.h>
> +#include <sys/select.h>
> +#include <sys/sendfile.h>
> +#include <sys/swap.h>
> +#include <sys/time.h>
> +#include <sys/types.h>
> +#include <sys/wait.h>
> +
> +/* Test that failing system calls do set errno to the correct value.
> +
> +   This is not an exhaustive test: only system calls that can be
> +   persuaded to fail with a consistent error code and no side effects
> +   are included.  Usually these are failures due to invalid arguments,
> +   with errno code EBADF or EINVAL.  The order of argument checks is
> +   unspecified, so we must take care to provide arguments that only
> +   allow _one_ failure mode.
> +
> +   Note that all system calls that can fail with EFAULT are permitted
> +   to deliver a SIGSEGV signal instead, so we avoid supplying invalid
> +   pointers in general, and we do not attempt to test system calls
> +   that can only fail with EFAULT (e.g. gettimeofday, gethostname).
> +
> +   Also note that root-only system calls (e.g. acct, reboot) may, when
> +   the test is run as an unprivileged user, fail due to insufficient
> +   privileges before bothering to do argument checks, so those are not
> +   tested either.
> +
> +   Also, system calls that take enum or a set of flags as argument is
> +   not tested if POSIX doesn't specify exact digital values for all

Same comment as above.

> +   flags, and so any value passed to flags may become valid.
> +
> +   Some tests assume "/bin/sh" names a file that exists and is not a
> +   directory.  */
> +
> +#define test_wrp_rv(rtype, prtype, experr, syscall, ...)	\
> +  (__extension__ ({						\
> +    errno = 0xdead;						\
> +    rtype ret = syscall (__VA_ARGS__);				\
> +    int err = errno;						\
> +    int fail;							\
> +    if (ret == (rtype) -1 && err == experr)			\
> +      fail = 0;							\
> +    else							\
> +      {								\
> +        fail = 1;						\
> +        if (ret != (rtype) -1)					\
> +          printf (#syscall ": didn't fail as expected"		\
> +               " (return "prtype")\n", ret);			\
> +        else if (err == 0xdead)					\
> +          puts(#syscall ": didn't update errno\n");		\
> +        else if (err != experr)					\
> +          printf (#syscall					\
> +               ": errno is: %d (%s) expected: %d (%s)\n",	\
> +               err, strerror (err), experr, strerror (experr));	\
> +      }								\
> +    fail;							\
> +  }))
> +
> +#define test_wrp(experr, syscall, ...)				\
> +  test_wrp_rv(int, "%d", experr, syscall, __VA_ARGS__)
> +

Same comment as a bove.

> +static int
> +do_test (void)
> +{
> +  fd_set rs, ws, es;
> +  int status;
> +  off_t off;
> +  stack_t ss;
> +  struct dqblk dqblk;
> +  struct epoll_event epoll_event;
> +  struct pollfd pollfd;
> +  struct sched_param sch_param;
> +  struct timespec ts;
> +  struct timeval tv;
> +  unsigned char vec[16];
> +  ss.ss_flags = ~SS_DISABLE;
> +  ts.tv_sec = -1;
> +
> +  int fails = 0;
> +  fails |= test_wrp (EINVAL, epoll_create, -1);
> +  fails |= test_wrp (EINVAL, epoll_create1, EPOLL_CLOEXEC + 1);
> +  fails |= test_wrp (EBADF, epoll_ctl, -1, EPOLL_CTL_ADD, 0, &epoll_event);
> +  fails |= test_wrp (EBADF, epoll_wait, -1, &epoll_event, 1, 1);
> +  fails |= test_wrp (EBADF, fdatasync, -1);
> +  fails |= test_wrp (EBADF, flock, -1, LOCK_SH);
> +  fails |= test_wrp (ESRCH, getpgid, -1);
> +  fails |= test_wrp (EINVAL, inotify_add_watch, -1, "/", 0);
> +  fails |= test_wrp (EINVAL, mincore, (void *) -1, 0, vec);
> +  fails |= test_wrp (EINVAL, mlock, (void *) -1, 1); // different errors
> +  fails |= test_wrp (EINVAL, nanosleep, &ts, &ts);
> +  fails |= test_wrp (EINVAL, poll, &pollfd, -1, 0);
> +  fails |= test_wrp (ENODEV, quotactl, Q_GETINFO, NULL, -1, (caddr_t) &dqblk);
> +  fails |= test_wrp (EINVAL, sched_getparam, -1, &sch_param);
> +  fails |= test_wrp (EINVAL, sched_getscheduler, -1);
> +  fails |= test_wrp (EINVAL, sched_get_priority_max, -1);
> +  fails |= test_wrp (EINVAL, sched_get_priority_min, -1);
> +  fails |= test_wrp (EINVAL, sched_rr_get_interval, -1, &ts);
> +  fails |= test_wrp (EINVAL, sched_setparam, -1, &sch_param);
> +  fails |= test_wrp (EINVAL, sched_setscheduler, -1, 0, &sch_param);
> +  fails |= test_wrp (EINVAL, select, -1, &rs, &ws, &es, &tv);
> +  fails |= test_wrp (EBADF, sendfile, -1, -1, &off, 0);
> +  fails |= test_wrp (EINVAL, sigaltstack, &ss, NULL);
> +  fails |= test_wrp (ECHILD, wait4, -1, &status, 0, NULL);
> +
> +  return fails;
> +}
> +
> +#define TEST_FUNCTION do_test ()
> +#include "test-skeleton.c"
>
  
Zack Weinberg Feb. 16, 2017, 5:15 p.m. UTC | #3
On Thu, Feb 16, 2017 at 12:10 PM, Carlos O'Donell <carlos@redhat.com> wrote:
>
> Please use a static function and avoid macros which make the test
> hard to debug. We don't have any performance or function call
> issues here so I think the macros should be avoided.

The macros make it possible to reuse the core test logic for all the
tested system calls, which vary in both the numbers and types of
arguments.  Any other approach would be significantly more complicated
and repetitive.

zw
  

Patch

diff --git a/posix/Makefile b/posix/Makefile
index c493f31..1359a64 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -62,7 +62,7 @@  routines :=								      \
 	get_child_max sched_cpucount sched_cpualloc sched_cpufree
 
 aux		:= init-posix environ
-tests		:= tstgetopt testfnm runtests runptests	     \
+tests		:= test-errno tstgetopt testfnm runtests runptests \
 		   tst-preadwrite tst-preadwrite64 test-vfork regexbug1 \
 		   tst-mmap tst-mmap-offset tst-getaddrinfo tst-truncate \
 		   tst-truncate64 tst-fork tst-fnmatch tst-regexloc tst-dir \
diff --git a/posix/test-errno.c b/posix/test-errno.c
new file mode 100644
index 0000000..7af2490
--- /dev/null
+++ b/posix/test-errno.c
@@ -0,0 +1,137 @@ 
+#include <errno.h>
+#include <limits.h>
+#include <grp.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/types.h>
+#include <sys/statfs.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <netinet/in.h>
+
+/* Test that failing system calls do set errno to the correct value.
+
+   This is not an exhaustive test: only system calls that can be
+   persuaded to fail with a consistent error code and no side effects
+   are included.  Usually these are failures due to invalid arguments,
+   with errno code EBADF or EINVAL.  The order of argument checks is
+   unspecified, so we must take care to provide arguments that only
+   allow _one_ failure mode.
+
+   Note that all system calls that can fail with EFAULT are permitted
+   to deliver a SIGSEGV signal instead, so we avoid supplying invalid
+   pointers in general, and we do not attempt to test system calls
+   that can only fail with EFAULT (e.g. gettimeofday, gethostname).
+
+   Also note that root-only system calls (e.g. acct, reboot) may, when
+   the test is run as an unprivileged user, fail due to insufficient
+   privileges before bothering to do argument checks, so those are not
+   tested either.
+
+   Also, system calls that take enum or a set of flags as argument is
+   not tested if POSIX doesn't specify exact digital values for all
+   flags, and so any value passed to flags may become valid.
+
+   Some tests assume "/bin/sh" names a file that exists and is not a
+   directory.  */
+
+#define test_wrp_rv(rtype, prtype, experr, syscall, ...)	\
+  (__extension__ ({						\
+    errno = 0xdead;						\
+    rtype ret = syscall (__VA_ARGS__);				\
+    int err = errno;						\
+    int fail;							\
+    if (ret == (rtype) -1 && err == experr)			\
+      fail = 0;							\
+    else							\
+      {								\
+        fail = 1;						\
+        if (ret != (rtype) -1)					\
+          fprintf (stderr, #syscall ": didn't fail as expected"	\
+               " (return "prtype")\n", ret);			\
+        else if (err == 0xdead)					\
+          fputs(#syscall ": didn't update errno\n", stderr);	\
+        else if (err != experr)					\
+          fprintf (stderr, #syscall				\
+               ": errno is: %d (%s) expected: %d (%s)\n",	\
+               err, strerror (err), experr, strerror (experr));	\
+      }								\
+    fail;							\
+  }))
+
+#define test_wrp(experr, syscall, ...)				\
+  test_wrp_rv(int, "%d", experr, syscall, __VA_ARGS__)
+
+int
+do_test (void)
+{
+  size_t pagesize = sysconf (_SC_PAGESIZE);
+  struct statfs sfs;
+  struct sockaddr sa;
+  socklen_t sl;
+  char buf[1];
+  struct iovec iov[1] = { { buf, 1 } };
+  struct sockaddr_in sin;
+  sin.sin_family = AF_INET;
+  sin.sin_port = htons (1026);
+  sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
+  struct msghdr msg;
+  memset(&msg, 0, sizeof msg);
+  msg.msg_iov = iov;
+  msg.msg_iovlen = 1;
+
+  int fails = 0;
+  fails |= test_wrp (EBADF, accept, -1, &sa, &sl);
+  fails |= test_wrp (EINVAL, access, "/", -1);
+  fails |= test_wrp (EBADF, bind, -1, (struct sockaddr *)&sin, sizeof sin);
+  fails |= test_wrp (ENOTDIR, chdir, "/bin/sh");
+  fails |= test_wrp (EBADF, close, -1);
+  fails |= test_wrp (EBADF, connect, -1, (struct sockaddr *)&sin, sizeof sin);
+  fails |= test_wrp (EBADF, dup, -1);
+  fails |= test_wrp (EBADF, dup2, -1, -1);
+  fails |= test_wrp (EBADF, fchdir, -1);
+  fails |= test_wrp (EBADF, fchmod, -1, 0);
+  fails |= test_wrp (EBADF, fcntl, -1, 0);
+  fails |= test_wrp (EBADF, fstatfs, -1, &sfs);
+  fails |= test_wrp (EBADF, fsync, -1);
+  fails |= test_wrp (EBADF, ftruncate, -1, 0);
+  fails |= test_wrp (EINVAL, getgroups, -1, 0);
+  fails |= test_wrp (EBADF, getpeername, -1, &sa, &sl);
+  fails |= test_wrp (EBADF, getsockname, -1, &sa, &sl);
+  fails |= test_wrp (EBADF, getsockopt, -1, 0, 0, buf, &sl);
+  fails |= test_wrp (EBADF, ioctl, -1, TIOCNOTTY);
+  fails |= test_wrp (EBADF, listen, -1, 1);
+  fails |= test_wrp (EBADF, lseek, -1, 0, 0);
+  fails |= test_wrp (EINVAL, madvise, (void *) -1, -1, 0);
+  fails |= test_wrp_rv (void *, "%p", EBADF,
+                        mmap, 0, pagesize, PROT_READ, MAP_PRIVATE, -1, 0);
+  fails |= test_wrp (EINVAL, mprotect, (void *) -1, pagesize, -1);
+  fails |= test_wrp (EINVAL, msync, (void *) -1, pagesize, -1);
+  fails |= test_wrp (EINVAL, munmap, (void *) -1, 0);
+  fails |= test_wrp (EINVAL, open, "/bin/sh", -1, 0);
+  fails |= test_wrp (EBADF, read, -1, buf, 1);
+  fails |= test_wrp (EINVAL, readlink, "/", buf, -1);
+  fails |= test_wrp (EBADF, readv, -1, iov, 1);
+  fails |= test_wrp (EBADF, recv, -1, buf, 1, 0);
+  fails |= test_wrp (EBADF, recvfrom, -1, buf, 1, 0, &sa, &sl);
+  fails |= test_wrp (EBADF, recvmsg, -1, &msg, 0);
+  fails |= test_wrp (EINVAL, select, -1, 0, 0, 0, 0);
+  fails |= test_wrp (EBADF, send, -1, buf, 1, 0);
+  fails |= test_wrp (EBADF, sendmsg, -1, &msg, 0);
+  fails |= test_wrp (EBADF, sendto, -1, buf, 1, 0, &sa, sl);
+  fails |= test_wrp (EBADF, setsockopt, -1, 0, 0, buf, sl);
+  fails |= test_wrp (EBADF, shutdown, -1, SHUT_RD);
+  fails |= test_wrp (EBADF, write, -1, "Hello", sizeof ("Hello") );
+  fails |= test_wrp (EBADF, writev, -1, iov, 1 );
+
+  return fails;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "test-skeleton.c"
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 8c4cfd0..cd11f7c 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -43,7 +43,7 @@  sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
 		  bits/mman-linux.h
 
 tests += tst-clone tst-clone2 tst-fanotify tst-personality tst-quota \
-	 tst-sync_file_range tst-syscall-template
+	 tst-sync_file_range tst-syscall-template test-errno
 
 # Generate the list of SYS_* macros for the system calls (__NR_* macros).
 
diff --git a/sysdeps/unix/sysv/linux/test-errno.c b/sysdeps/unix/sysv/linux/test-errno.c
new file mode 100644
index 0000000..5a2f8a4
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/test-errno.c
@@ -0,0 +1,126 @@ 
+#include <errno.h>
+#include <fcntl.h>
+#include <mqueue.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/epoll.h>
+#include <sys/eventfd.h>
+#include <sys/file.h>
+#include <sys/fsuid.h>
+#include <sys/inotify.h>
+#include <sys/mman.h>
+#include <sys/poll.h>
+#include <sys/quota.h>
+#include <sys/resource.h>
+#include <sys/select.h>
+#include <sys/sendfile.h>
+#include <sys/swap.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+/* Test that failing system calls do set errno to the correct value.
+
+   This is not an exhaustive test: only system calls that can be
+   persuaded to fail with a consistent error code and no side effects
+   are included.  Usually these are failures due to invalid arguments,
+   with errno code EBADF or EINVAL.  The order of argument checks is
+   unspecified, so we must take care to provide arguments that only
+   allow _one_ failure mode.
+
+   Note that all system calls that can fail with EFAULT are permitted
+   to deliver a SIGSEGV signal instead, so we avoid supplying invalid
+   pointers in general, and we do not attempt to test system calls
+   that can only fail with EFAULT (e.g. gettimeofday, gethostname).
+
+   Also note that root-only system calls (e.g. acct, reboot) may, when
+   the test is run as an unprivileged user, fail due to insufficient
+   privileges before bothering to do argument checks, so those are not
+   tested either.
+
+   Also, system calls that take enum or a set of flags as argument is
+   not tested if POSIX doesn't specify exact digital values for all
+   flags, and so any value passed to flags may become valid.
+
+   Some tests assume "/bin/sh" names a file that exists and is not a
+   directory.  */
+
+#define test_wrp_rv(rtype, prtype, experr, syscall, ...)	\
+  (__extension__ ({						\
+    errno = 0xdead;						\
+    rtype ret = syscall (__VA_ARGS__);				\
+    int err = errno;						\
+    int fail;							\
+    if (ret == (rtype) -1 && err == experr)			\
+      fail = 0;							\
+    else							\
+      {								\
+        fail = 1;						\
+        if (ret != (rtype) -1)					\
+          printf (#syscall ": didn't fail as expected"		\
+               " (return "prtype")\n", ret);			\
+        else if (err == 0xdead)					\
+          puts(#syscall ": didn't update errno\n");		\
+        else if (err != experr)					\
+          printf (#syscall					\
+               ": errno is: %d (%s) expected: %d (%s)\n",	\
+               err, strerror (err), experr, strerror (experr));	\
+      }								\
+    fail;							\
+  }))
+
+#define test_wrp(experr, syscall, ...)				\
+  test_wrp_rv(int, "%d", experr, syscall, __VA_ARGS__)
+
+static int
+do_test (void)
+{
+  fd_set rs, ws, es;
+  int status;
+  off_t off;
+  stack_t ss;
+  struct dqblk dqblk;
+  struct epoll_event epoll_event;
+  struct pollfd pollfd;
+  struct sched_param sch_param;
+  struct timespec ts;
+  struct timeval tv;
+  unsigned char vec[16];
+  ss.ss_flags = ~SS_DISABLE;
+  ts.tv_sec = -1;
+
+  int fails = 0;
+  fails |= test_wrp (EINVAL, epoll_create, -1);
+  fails |= test_wrp (EINVAL, epoll_create1, EPOLL_CLOEXEC + 1);
+  fails |= test_wrp (EBADF, epoll_ctl, -1, EPOLL_CTL_ADD, 0, &epoll_event);
+  fails |= test_wrp (EBADF, epoll_wait, -1, &epoll_event, 1, 1);
+  fails |= test_wrp (EBADF, fdatasync, -1);
+  fails |= test_wrp (EBADF, flock, -1, LOCK_SH);
+  fails |= test_wrp (ESRCH, getpgid, -1);
+  fails |= test_wrp (EINVAL, inotify_add_watch, -1, "/", 0);
+  fails |= test_wrp (EINVAL, mincore, (void *) -1, 0, vec);
+  fails |= test_wrp (EINVAL, mlock, (void *) -1, 1); // different errors
+  fails |= test_wrp (EINVAL, nanosleep, &ts, &ts);
+  fails |= test_wrp (EINVAL, poll, &pollfd, -1, 0);
+  fails |= test_wrp (ENODEV, quotactl, Q_GETINFO, NULL, -1, (caddr_t) &dqblk);
+  fails |= test_wrp (EINVAL, sched_getparam, -1, &sch_param);
+  fails |= test_wrp (EINVAL, sched_getscheduler, -1);
+  fails |= test_wrp (EINVAL, sched_get_priority_max, -1);
+  fails |= test_wrp (EINVAL, sched_get_priority_min, -1);
+  fails |= test_wrp (EINVAL, sched_rr_get_interval, -1, &ts);
+  fails |= test_wrp (EINVAL, sched_setparam, -1, &sch_param);
+  fails |= test_wrp (EINVAL, sched_setscheduler, -1, 0, &sch_param);
+  fails |= test_wrp (EINVAL, select, -1, &rs, &ws, &es, &tv);
+  fails |= test_wrp (EBADF, sendfile, -1, -1, &off, 0);
+  fails |= test_wrp (EINVAL, sigaltstack, &ss, NULL);
+  fails |= test_wrp (ECHILD, wait4, -1, &status, 0, NULL);
+
+  return fails;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "test-skeleton.c"