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