test-errno-linux.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Test that failing system calls do set errno to the correct value.
  2. Linux sycalls version.
  3. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <array_length.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <mqueue.h>
  20. #include <sched.h>
  21. #include <signal.h>
  22. #include <stdbool.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <unistd.h>
  27. #include <sys/epoll.h>
  28. #include <sys/eventfd.h>
  29. #include <sys/file.h>
  30. #include <sys/fsuid.h>
  31. #include <sys/inotify.h>
  32. #include <sys/mman.h>
  33. #include <sys/poll.h>
  34. #include <sys/quota.h>
  35. #include <sys/resource.h>
  36. #include <sys/select.h>
  37. #include <sys/sendfile.h>
  38. #include <sys/swap.h>
  39. #include <sys/time.h>
  40. #include <sys/types.h>
  41. #include <sys/wait.h>
  42. /* This is not an exhaustive test: only system calls that can be
  43. persuaded to fail with a consistent error code and no side effects
  44. are included. Usually these are failures due to invalid arguments,
  45. with errno code EBADF or EINVAL. The order of argument checks is
  46. unspecified, so we must take care to provide arguments that only
  47. allow _one_ failure mode.
  48. Note that all system calls that can fail with EFAULT are permitted
  49. to deliver a SIGSEGV signal instead, so we avoid supplying invalid
  50. pointers in general, and we do not attempt to test system calls
  51. that can only fail with EFAULT (e.g. gettimeofday, gethostname).
  52. Also note that root-only system calls (e.g. acct, reboot) may, when
  53. the test is run as an unprivileged user, fail due to insufficient
  54. privileges before bothering to do argument checks, so those are not
  55. tested either.
  56. Also, system calls that take enum or a set of flags as argument is
  57. not tested if POSIX doesn't specify exact binary values for all
  58. flags, and so any value passed to flags may become valid.
  59. Some tests assume "/bin/sh" names a file that exists and is not a
  60. directory. */
  61. /* Evalutes to the arguments in a list initializer which can be used
  62. as a single macro argument. */
  63. #define LIST(...) { __VA_ARGS__ }
  64. /* This macro is necessary to forward the output of LIST as a macro
  65. argument. */
  66. #define LIST_FORWARD(...) __VA_ARGS__
  67. /* Return true if CODE is contained in the array [CODES, CODES +
  68. COUNT]. */
  69. static bool
  70. check_error_in_list (int code, int *codes, size_t count)
  71. {
  72. for (size_t i = 0; i < count; ++i)
  73. if (codes[i] == code)
  74. return true;
  75. return false;
  76. }
  77. #define test_wrp_rv(rtype, prtype, experr_list, syscall, ...) \
  78. (__extension__ ({ \
  79. errno = 0xdead; \
  80. int experr[] = experr_list; \
  81. rtype ret = syscall (__VA_ARGS__); \
  82. int err = errno; \
  83. int fail; \
  84. if ((ret == (rtype) -1) \
  85. && check_error_in_list (err, experr, array_length (experr))) \
  86. fail = 0; \
  87. else \
  88. { \
  89. fail = 1; \
  90. if (ret != (rtype) -1) \
  91. printf ("FAIL: " #syscall ": didn't fail as expected" \
  92. " (return "prtype")\n", ret); \
  93. else if (err == 0xdead) \
  94. puts ("FAIL: " #syscall ": didn't update errno"); \
  95. else \
  96. printf ("FAIL: " #syscall \
  97. ": errno is: %d (%s) expected one of %s\n", \
  98. err, strerror (err), #experr_list); \
  99. } \
  100. fail; \
  101. }))
  102. #define test_wrp(experr, syscall, ...) \
  103. test_wrp_rv(int, "%d", LIST (experr), syscall, __VA_ARGS__)
  104. #define test_wrp2(experr, syscall, ...) \
  105. test_wrp_rv(int, "%d", LIST_FORWARD (experr), syscall, __VA_ARGS__)
  106. static int
  107. do_test (void)
  108. {
  109. fd_set rs, ws, es;
  110. int status;
  111. off_t off;
  112. stack_t ss;
  113. struct dqblk dqblk;
  114. struct epoll_event epoll_event;
  115. struct pollfd pollfd;
  116. struct sched_param sch_param;
  117. struct timespec ts;
  118. struct timeval tv;
  119. unsigned char vec[16];
  120. ss.ss_flags = ~SS_DISABLE;
  121. ts.tv_sec = -1;
  122. int fails = 0;
  123. fails |= test_wrp (EINVAL, epoll_create, -1);
  124. fails |= test_wrp (EINVAL, epoll_create1, EPOLL_CLOEXEC + 1);
  125. fails |= test_wrp (EBADF, epoll_ctl, -1, EPOLL_CTL_ADD, 0, &epoll_event);
  126. fails |= test_wrp (EBADF, epoll_wait, -1, &epoll_event, 1, 1);
  127. fails |= test_wrp (EBADF, fdatasync, -1);
  128. fails |= test_wrp (EBADF, flock, -1, LOCK_SH);
  129. fails |= test_wrp (ESRCH, getpgid, -1);
  130. /* Linux v3.8 (676a0675c) removed the test to check at least one valid
  131. bit in flags (to return EINVAL). It was later added back in v3.9
  132. (04df32fa1). */
  133. fails |= test_wrp2 (LIST (EINVAL, EBADF), inotify_add_watch, -1, "/", 0);
  134. fails |= test_wrp (EINVAL, mincore, (void *) -1, 0, vec);
  135. /* mlock fails if the result of the addition addr+len was less than addr
  136. (which indicates final address overflow), however on 32 bits binaries
  137. running on 64 bits kernels, internal syscall address check won't result
  138. in an invalid address and thus syscalls fails later in vma
  139. allocation. */
  140. fails |= test_wrp2 (LIST (EINVAL, ENOMEM), mlock, (void *) -1, 1);
  141. fails |= test_wrp (EINVAL, nanosleep, &ts, &ts);
  142. fails |= test_wrp (EINVAL, poll, &pollfd, -1, 0);
  143. /* quotactl returns ENOSYS for kernels not configured with
  144. CONFIG_QUOTA, and may return EPERM if called within certain types
  145. of containers. */
  146. fails |= test_wrp2 (LIST (ENODEV, ENOSYS, EPERM),
  147. quotactl, Q_GETINFO, NULL, -1, (caddr_t) &dqblk);
  148. fails |= test_wrp (EINVAL, sched_getparam, -1, &sch_param);
  149. fails |= test_wrp (EINVAL, sched_getscheduler, -1);
  150. fails |= test_wrp (EINVAL, sched_get_priority_max, -1);
  151. fails |= test_wrp (EINVAL, sched_get_priority_min, -1);
  152. fails |= test_wrp (EINVAL, sched_rr_get_interval, -1, &ts);
  153. fails |= test_wrp (EINVAL, sched_setparam, -1, &sch_param);
  154. fails |= test_wrp (EINVAL, sched_setscheduler, -1, 0, &sch_param);
  155. fails |= test_wrp (EINVAL, select, -1, &rs, &ws, &es, &tv);
  156. fails |= test_wrp (EBADF, sendfile, -1, -1, &off, 0);
  157. fails |= test_wrp (EINVAL, sigaltstack, &ss, NULL);
  158. fails |= test_wrp (ECHILD, wait4, -1, &status, 0, NULL);
  159. return fails;
  160. }
  161. #include "support/test-driver.c"