e354a5
commit 359653aaacad463d916323f03c0ac3c47405aafa
e354a5
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
e354a5
Date:   Wed Jan 16 18:10:56 2019 +0000
e354a5
e354a5
    Do not use HP_TIMING_NOW for random bits
e354a5
    
e354a5
    This patch removes the HP_TIMING_BITS usage for fast random bits and replace
e354a5
    with clock_gettime (CLOCK_MONOTONIC).  It has unspecified starting time and
e354a5
    nano-second accuracy, so its randomness is significantly better than
e354a5
    gettimeofday.
e354a5
    
e354a5
    Althoug it should incur in more overhead (specially for architecture that
e354a5
    support hp-timing), the symbol is also common implemented as a vDSO.
e354a5
    
e354a5
    Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu. I also
e354a5
    checked on a i686-gnu build.
e354a5
    
e354a5
            * include/random-bits.h: New file.
e354a5
            * resolv/res_mkquery.c [HP_TIMING_AVAIL] (RANDOM_BITS,
e354a5
            (__res_context_mkquery): Remove usage hp-timing usage and replace with
e354a5
            random_bits.
e354a5
            * resolv/res_send.c [HP_TIMING_AVAIL] (nameserver_offset): Likewise.
e354a5
            * sysdeps/posix/tempname.c [HP_TIMING_AVAIL] (__gen_tempname):
e354a5
            Likewise.
e354a5
e354a5
diff --git a/include/random-bits.h b/include/random-bits.h
e354a5
new file mode 100644
e354a5
index 0000000000000000..a0651a5a34f80a8d
e354a5
--- /dev/null
e354a5
+++ b/include/random-bits.h
e354a5
@@ -0,0 +1,41 @@
e354a5
+/* Fast pseudo-random bits based on clock_gettime.
e354a5
+   Copyright (C) 2019 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <http://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+#ifndef _RANDOM_BITS_H
e354a5
+# define _RANDOM_BITS_H
e354a5
+
e354a5
+#include <time.h>
e354a5
+#include <stdint.h>
e354a5
+
e354a5
+/* Provides fast pseudo-random bits through clock_gettime.  It has unspecified
e354a5
+   starting time, nano-second accuracy, its randomness is significantly better
e354a5
+   than gettimeofday, and for mostly architectures it is implemented through
e354a5
+   vDSO instead of a syscall.  Since the source is a system clock, the upper
e354a5
+   bits will have less entropy. */
e354a5
+static inline uint32_t
e354a5
+random_bits (void)
e354a5
+{
e354a5
+  struct timespec tv;
e354a5
+  __clock_gettime (CLOCK_MONOTONIC, &tv;;
e354a5
+  /* Shuffle the lower bits to minimize the clock bias.  */
e354a5
+  uint32_t ret = tv.tv_nsec ^ tv.tv_sec;
e354a5
+  ret ^= (ret << 24) | (ret >> 8);
e354a5
+  return ret;
e354a5
+}
e354a5
+
e354a5
+#endif
e354a5
diff --git a/resolv/res_mkquery.c b/resolv/res_mkquery.c
e354a5
index 213abeefadf7ece5..4471a8838b1de7ee 100644
e354a5
--- a/resolv/res_mkquery.c
e354a5
+++ b/resolv/res_mkquery.c
e354a5
@@ -82,6 +82,7 @@
e354a5
  * SOFTWARE.
e354a5
  */
e354a5
 
e354a5
+#include <stdint.h>
e354a5
 #include <sys/types.h>
e354a5
 #include <sys/param.h>
e354a5
 #include <netinet/in.h>
e354a5
@@ -92,12 +93,7 @@
e354a5
 #include <string.h>
e354a5
 #include <sys/time.h>
e354a5
 #include <shlib-compat.h>
e354a5
-
e354a5
-#include <hp-timing.h>
e354a5
-#include <stdint.h>
e354a5
-#if HP_TIMING_AVAIL
e354a5
-# define RANDOM_BITS(Var) { uint64_t v64; HP_TIMING_NOW (v64); Var = v64; }
e354a5
-#endif
e354a5
+#include <random-bits.h>
e354a5
 
e354a5
 int
e354a5
 __res_context_mkquery (struct resolv_context *ctx, int op, const char *dname,
e354a5
@@ -120,16 +116,7 @@ __res_context_mkquery (struct resolv_context *ctx, int op, const char *dname,
e354a5
   /* We randomize the IDs every time.  The old code just incremented
e354a5
      by one after the initial randomization which still predictable if
e354a5
      the application does multiple requests.  */
e354a5
-  int randombits;
e354a5
-#ifdef RANDOM_BITS
e354a5
-  RANDOM_BITS (randombits);
e354a5
-#else
e354a5
-  struct timeval tv;
e354a5
-  __gettimeofday (&tv, NULL);
e354a5
-  randombits = (tv.tv_sec << 8) ^ tv.tv_usec;
e354a5
-#endif
e354a5
-
e354a5
-  hp->id = randombits;
e354a5
+  hp->id = random_bits ();
e354a5
   hp->opcode = op;
e354a5
   hp->rd = (ctx->resp->options & RES_RECURSE) != 0;
e354a5
   hp->rcode = NOERROR;
e354a5
diff --git a/resolv/res_send.c b/resolv/res_send.c
e354a5
index ac19627634281c2f..55e7fa438e7baac1 100644
e354a5
--- a/resolv/res_send.c
e354a5
+++ b/resolv/res_send.c
e354a5
@@ -109,7 +109,7 @@
e354a5
 #include <unistd.h>
e354a5
 #include <kernel-features.h>
e354a5
 #include <libc-diag.h>
e354a5
-#include <hp-timing.h>
e354a5
+#include <random-bits.h>
e354a5
 
e354a5
 #if PACKETSZ > 65536
e354a5
 #define MAXPACKET       PACKETSZ
e354a5
@@ -309,15 +309,7 @@ nameserver_offset (struct __res_state *statp)
e354a5
   if ((offset & 1) == 0)
e354a5
     {
e354a5
       /* Initialization is required.  */
e354a5
-#if HP_TIMING_AVAIL
e354a5
-      uint64_t ticks;
e354a5
-      HP_TIMING_NOW (ticks);
e354a5
-      offset = ticks;
e354a5
-#else
e354a5
-      struct timeval tv;
e354a5
-      __gettimeofday (&tv, NULL);
e354a5
-      offset = ((tv.tv_sec << 8) ^ tv.tv_usec);
e354a5
-#endif
e354a5
+      offset = random_bits ();
e354a5
       /* The lowest bit is the most random.  Preserve it.  */
e354a5
       offset <<= 1;
e354a5
 
e354a5
diff --git a/sysdeps/posix/tempname.c b/sysdeps/posix/tempname.c
e354a5
index 432262a03b6ecc23..3d26f378021680ae 100644
e354a5
--- a/sysdeps/posix/tempname.c
e354a5
+++ b/sysdeps/posix/tempname.c
e354a5
@@ -71,22 +71,15 @@
e354a5
 #endif
e354a5
 
e354a5
 #ifdef _LIBC
e354a5
-# include <hp-timing.h>
e354a5
-# if HP_TIMING_AVAIL
e354a5
-#  define RANDOM_BITS(Var) \
e354a5
-  if (__glibc_unlikely (value == UINT64_C (0)))				      \
e354a5
-    {									      \
e354a5
-      /* If this is the first time this function is used initialize	      \
e354a5
-	 the variable we accumulate the value in to some somewhat	      \
e354a5
-	 random value.  If we'd not do this programs at startup time	      \
e354a5
-	 might have a reduced set of possible names, at least on slow	      \
e354a5
-	 machines.  */							      \
e354a5
-      struct timeval tv;						      \
e354a5
-      __gettimeofday (&tv, NULL);					      \
e354a5
-      value = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;		      \
e354a5
-    }									      \
e354a5
-  HP_TIMING_NOW (Var)
e354a5
-# endif
e354a5
+# include <random-bits.h>
e354a5
+# define RANDOM_BITS(Var) ((Var) = random_bits ())
e354a5
+# else
e354a5
+# define RANDOM_BITS(Var) \
e354a5
+    {                                                                         \
e354a5
+      struct timeval tv;                                                      \
e354a5
+      __gettimeofday (&tv, NULL);                                             \
e354a5
+      (Var) = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;                      \
e354a5
+    }
e354a5
 #endif
e354a5
 
e354a5
 /* Use the widest available unsigned type if uint64_t is not
e354a5
@@ -193,8 +186,7 @@ __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
e354a5
 {
e354a5
   int len;
e354a5
   char *XXXXXX;
e354a5
-  static uint64_t value;
e354a5
-  uint64_t random_time_bits;
e354a5
+  uint64_t value;
e354a5
   unsigned int count;
e354a5
   int fd = -1;
e354a5
   int save_errno = errno;
e354a5
@@ -227,16 +219,8 @@ __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
e354a5
   XXXXXX = &tmpl[len - 6 - suffixlen];
e354a5
 
e354a5
   /* Get some more or less random data.  */
e354a5
-#ifdef RANDOM_BITS
e354a5
-  RANDOM_BITS (random_time_bits);
e354a5
-#else
e354a5
-  {
e354a5
-    struct timeval tv;
e354a5
-    __gettimeofday (&tv, NULL);
e354a5
-    random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
e354a5
-  }
e354a5
-#endif
e354a5
-  value += random_time_bits ^ __getpid ();
e354a5
+  RANDOM_BITS (value);
e354a5
+  value ^= (uint64_t)__getpid () << 32;
e354a5
 
e354a5
   for (count = 0; count < attempts; value += 7777, ++count)
e354a5
     {