From patchwork Tue Mar 14 20:24:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 95313 Delivered-To: patch@linaro.org Received: by 10.140.89.134 with SMTP id v6csp17360qgd; Tue, 14 Mar 2017 13:24:43 -0700 (PDT) X-Received: by 10.99.122.13 with SMTP id v13mr46222292pgc.31.1489523083766; Tue, 14 Mar 2017 13:24:43 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id i19si15622117pgk.288.2017.03.14.13.24.43 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Mar 2017 13:24:43 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-77895-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-77895-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-77895-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; q=dns; s= default; b=OceSjXfFOf8fkV9eXqfZvwxbQAJe4MRYyspMpSoDyRUNkxAQrHBUF fjsxuw/sDLGVKeLeMs3Rvda2veL210viUn/c6neQIAVGn82TpD22QvJUYOuTMR1g XCBbeHrJkSEA48nsegRbW4anjOe2vwSCBUvS2oUjskC3RIN7lka6yo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; s=default; bh=xkZcJ3vxAXYZx2Euc6zUU7WG0yI=; b=VjM6ZgDM0kiCp6b+Ci2F6bbyG3nA sClSQbCr07aC4RUL+wKhC8jqBcVMQiPRjFc8m5MigHu7YPsydRcFGYRba6WbU3Ht nPrDHSAmf/GmRuylaBU2H4j12Sm5znof4wa2a6LGrWdtllBDMIZCKIjZYAWUg7C9 p4GJoaYcwq9J3D0= Received: (qmail 11284 invoked by alias); 14 Mar 2017 20:24:26 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 11227 invoked by uid 89); 14 Mar 2017 20:24:22 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=acct X-HELO: mail-qt0-f170.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=fWGlvtKb1IhQy6MNFohU9hMKIpndd6+ms7IXvadHfnE=; b=Ld3oqDWollkHMQTHJPLywycW6S0eG/6lZAADP1fARzQjDZzbjnrg2+oGY/tkYY+BLP sRKYDi3+T78Isd7JPA47EvyLQRXErttuGqqq3LxFX5lIUQEJoMKopdm+hCIKCwvgRdIy jzDSLfXXYyNtBFB1kmIZvVTpMGzINEEiqhvKc9RniBiDmfsRUwdEbu0ww7ukzT8KjzLX VY5NZ8hdC1kKyxgHW0u5uLrvLFfS09xcMgJzMXycXMDMteAlyqgmrmwSok1r4xGBLmnF BtAS8jM3LuU5HHLs2Do9KfrLD1aEUMpcQwlF+7DM7i3MX68CN4+POfziIWJE8yLaWoYr p36Q== X-Gm-Message-State: AMke39niL4M5W5ohmt+t1u/UjH8Tpjen4tAEc08aNALcYRnUNPdkqWUvA70VSwSjRez8hrQC X-Received: by 10.200.1.14 with SMTP id e14mr43179146qtg.244.1489523052745; Tue, 14 Mar 2017 13:24:12 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2] Fix test-errno issues Date: Tue, 14 Mar 2017 17:24:05 -0300 Message-Id: <1489523045-21530-1-git-send-email-adhemerval.zanella@linaro.org> Changes from previous version: - Change open tests from EINVAL to EISDIR. -- This patch fixes multiple issues of test-errno.c (9a56f8718341): - Rename Linux test-errno.c to test-errno-linux.c to avoid build the same source for both tests. - Add a mlock check for 32 bits build running on 64 bits kernels. Althuough man pages states that mlock fails with EINVAL if final address overflows, kernels does not return it for aforementioned condition (it returns ENOMEM instead). Although it seems to be a kernel issue for compat syscall handling, I think it is worth to still check syscall return and document the behavior. - Initialize option length for setsockopt check. Checked on x86_64-linux-gnu and i686-linux-gnu (running on 64 bits kernel). * posix/test-errno.c (do_test): Initialize setsockopt optlen. * sysdeps/unix/sysv/linux/test-errno.c: Move to ... * sysdeps/unix/sysv/linux/test-errno-linux.c: ... here. (do_test): Handle mlock return on 64 bits kernels with 32 bits binaries and change open test. --- ChangeLog | 8 ++ posix/test-errno.c | 4 +- sysdeps/unix/sysv/linux/Makefile | 2 +- sysdeps/unix/sysv/linux/test-errno-linux.c | 176 +++++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/test-errno.c | 142 ----------------------- 5 files changed, 187 insertions(+), 145 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/test-errno-linux.c delete mode 100644 sysdeps/unix/sysv/linux/test-errno.c -- 2.7.4 diff --git a/posix/test-errno.c b/posix/test-errno.c index 98df344..c2bfd8a 100644 --- a/posix/test-errno.c +++ b/posix/test-errno.c @@ -131,7 +131,7 @@ do_test (void) 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 (EISDIR, open, "/bin", EISDIR, O_WRONLY); fails |= test_wrp (EBADF, read, -1, buf, 1); fails |= test_wrp (EINVAL, readlink, "/", buf, -1); fails |= test_wrp (EBADF, readv, -1, iov, 1); @@ -142,7 +142,7 @@ do_test (void) 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, setsockopt, -1, 0, 0, buf, sizeof (*buf)); 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 ); diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 6b7aa3f..1872cdb 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 test-errno + tst-sync_file_range test-errno-linux # Generate the list of SYS_* macros for the system calls (__NR_* macros). diff --git a/sysdeps/unix/sysv/linux/test-errno-linux.c b/sysdeps/unix/sysv/linux/test-errno-linux.c new file mode 100644 index 0000000..b00d14e --- /dev/null +++ b/sysdeps/unix/sysv/linux/test-errno-linux.c @@ -0,0 +1,176 @@ +/* Test that failing system calls do set errno to the correct value. + Linux sycalls version. + + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* 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 binary 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 ("FAIL: " #syscall ": didn't fail as expected" \ + " (return "prtype")\n", ret); \ + else if (err == 0xdead) \ + puts("FAIL: " #syscall ": didn't update errno\n"); \ + else if (err != experr) \ + printf ("FAIL: " #syscall \ + ": errno is: %d (%s) expected: %d (%s)\n", \ + err, strerror (err), experr, strerror (experr)); \ + } \ + fail; \ + })) + +#define test_wrp_rv2(rtype, prtype, experr1, experr2, syscall, ...) \ + (__extension__ ({ \ + errno = 0xdead; \ + rtype ret = syscall (__VA_ARGS__); \ + int err = errno; \ + int fail; \ + if (ret == (rtype) -1 && (err == experr1 || err == experr2)) \ + fail = 0; \ + else \ + { \ + fail = 1; \ + if (ret != (rtype) -1) \ + printf ("FAIL: " #syscall ": didn't fail as expected" \ + " (return "prtype")\n", ret); \ + else if (err == 0xdead) \ + puts("FAIL: " #syscall ": didn't update errno\n"); \ + else if (err != experr1 && err != experr2) \ + printf ("FAIL: " #syscall \ + ": errno is: %d (%s) expected: %d (%s) or %d (%s)\n", \ + err, strerror (err), experr1, strerror (experr1), \ + experr2, strerror (experr2)); \ + } \ + fail; \ + })) + +#define test_wrp(experr, syscall, ...) \ + test_wrp_rv(int, "%d", experr, syscall, __VA_ARGS__) + +#define test_wrp2(experr1, experr2, syscall, ...) \ + test_wrp_rv2(int, "%d", experr1, experr2, 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); + /* mlock fails if the result of the addition addr+len was less than addr + (which indicates final address overflow), however on 32 bits binaries + running on 64 bits kernels, internal syscall address check won't result + in an invalid address and thus syscalls fails later in vma + allocation. */ + fails |= test_wrp2 (EINVAL, ENOMEM, mlock, (void *) -1, 1); + 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; +} + +#include "support/test-driver.c" diff --git a/sysdeps/unix/sysv/linux/test-errno.c b/sysdeps/unix/sysv/linux/test-errno.c deleted file mode 100644 index ab3735f..0000000 --- a/sysdeps/unix/sysv/linux/test-errno.c +++ /dev/null @@ -1,142 +0,0 @@ -/* Test that failing system calls do set errno to the correct value. - - Copyright (C) 2017 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* 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 binary 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 ("FAIL: " #syscall ": didn't fail as expected" \ - " (return "prtype")\n", ret); \ - else if (err == 0xdead) \ - puts("FAIL: " #syscall ": didn't update errno\n"); \ - else if (err != experr) \ - printf ("FAIL: " #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; -} - -#include "support/test-driver.c"