public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug nptl/18138] New: sem_timedwait should not convert absolute timeout to relative
@ 2015-03-17 23:57 jsm28 at gcc dot gnu.org
  2015-03-18 17:06 ` [Bug nptl/18138] " cvs-commit at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: jsm28 at gcc dot gnu.org @ 2015-03-17 23:57 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=18138

            Bug ID: 18138
           Summary: sem_timedwait should not convert absolute timeout to
                    relative
           Product: glibc
           Version: 2.21
            Status: NEW
          Severity: normal
          Priority: P2
         Component: nptl
          Assignee: unassigned at sourceware dot org
          Reporter: jsm28 at gcc dot gnu.org
                CC: drepper.fsp at gmail dot com

sem_timedwait converts absolute timeouts to relative to pass them to the futex
syscall.  (Before the recent reimplementation, on x86_64 it used
FUTEX_CLOCK_REALTIME, but not on other architectures.)

Correctly implementing POSIX requirements, however, requires use of
FUTEX_CLOCK_REALTIME; passing a relative timeout to the kernel does not conform
to POSIX.  The POSIX specification for sem_timedwait says "The timeout shall be
based on the CLOCK_REALTIME clock.".  The POSIX specification for clock_settime
says "If the value of the CLOCK_REALTIME clock is set via clock_settime(), the
new value of the clock shall be used to determine the time of expiration for
absolute time services based upon the CLOCK_REALTIME clock. This applies to the
time at which armed absolute timers expire. If the absolute time requested at
the invocation of such a time service is before the new value of the clock, the
time service shall expire immediately as if the clock had reached the requested
time normally.".  If a relative timeout is passed to the kernel, it is
interpreted according to the CLOCK_MONOTONIC clock, and so fails to meet that
POSIX requirement in the event of clock changes.

Testcase below (requires root, changes the system clock and leaves the clock
changed).  Testing a patch.

#include <errno.h>
#include <inttypes.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int
main (void)
{
  struct timespec old_time, new_time, timeout, final_time;
  if (clock_gettime (CLOCK_REALTIME, &old_time) != 0)
    {
      perror ("clock_gettime");
      exit (EXIT_FAILURE);
    }
  new_time = old_time;
  timeout = old_time;
  new_time.tv_sec += 30;
  timeout.tv_sec += 20;
  pid_t pid = fork ();
  if (pid == -1)
    {
      perror ("fork");
      exit (EXIT_FAILURE);
    }
  if (pid == 0)
    {
      /* Child.  */
      if (sleep (1) != 0)
        {
          perror ("sleep");
          exit (EXIT_FAILURE);
        }
      if (clock_settime (CLOCK_REALTIME, &new_time) != 0)
        {
          perror ("clock_settime");
          exit (EXIT_FAILURE);
        }
      exit (EXIT_SUCCESS);
    }
  /* Parent.  */
  sem_t sem;
  if (sem_init (&sem, 0, 0) != 0)
    {
      perror ("sem_init");
      exit (EXIT_FAILURE);
    }
  int ret = sem_timedwait (&sem, &timeout);
  if (ret != -1 || errno != ETIMEDOUT)
    {
      fprintf (stderr, "sem_timedout returned %d (errno = %d: %m)\n",
               ret, errno);
      exit (EXIT_FAILURE);
    }
  if (clock_gettime (CLOCK_REALTIME, &final_time) != 0)
    {
      perror ("clock_gettime");
      exit (EXIT_FAILURE);
    }
  int64_t time_diff
    = (INT64_C(1000000000) * (final_time.tv_sec - new_time.tv_sec)
       + (final_time.tv_nsec - new_time.tv_nsec));
  fprintf (stderr, "time difference %"PRId64"ns\n", time_diff);
  if (time_diff < 0 || time_diff > 1000000000)
    {
      fprintf (stderr, "bad time difference\n");
      exit (EXIT_FAILURE);
    }
  fprintf (stderr, "time difference OK\n");
  exit (EXIT_SUCCESS);
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-03-25 15:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-17 23:57 [Bug nptl/18138] New: sem_timedwait should not convert absolute timeout to relative jsm28 at gcc dot gnu.org
2015-03-18 17:06 ` [Bug nptl/18138] " cvs-commit at gcc dot gnu.org
2015-03-18 17:07 ` jsm28 at gcc dot gnu.org
2015-03-25 15:19 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).