On 22.03.2017 05:52, Jonathan Gray wrote:
If a proper PRNG like arc4random backed by a syscall like getentropy
were used this would already handle reseeding on fork and fd exhaustion.

And look people have already gone out of their way to provide fallbacks
for these interfaces.

https://github.com/libressl-portable/openbsd/tree/master/src/lib/libcrypto/arc4random

Yeah, but then you have the headache of juggling those additional build dependencies. I think the approach is fine as is.

Nicolai


On Wed, Mar 22, 2017 at 10:59:47AM +1100, Timothy Arceri wrote:
V2: pass the seed to the seed function so that we can isolate
    its uses. Stop leaking fd when urandom couldn't be read.
---
 src/gallium/drivers/radeon/r600_test_dma.c |  3 +-
 src/util/rand_xor.c                        | 57 ++++++++++++++++++++++++++++++
 src/util/rand_xor.h                        |  4 +++
 3 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/src/gallium/drivers/radeon/r600_test_dma.c 
b/src/gallium/drivers/radeon/r600_test_dma.c
index 8b4149a..3c23b09 100644
--- a/src/gallium/drivers/radeon/r600_test_dma.c
+++ b/src/gallium/drivers/radeon/r600_test_dma.c
@@ -176,22 +176,21 @@ void r600_test_dma(struct r600_common_screen *rscreen)

        max_levels = screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
        max_tex_side = 1 << (max_levels - 1);

        /* Max 128 MB allowed for both textures. */
        max_alloc_size = 128 * 1024 * 1024;

        /* the seed for random test parameters */
        srand(0x9b47d95b);
        /* the seed for random pixel data */
-       seed_xorshift128plus[0] = 0x3bffb83978e24f88;
-       seed_xorshift128plus[1] = 0x9238d5d56c71cd35;
+       s_rand_xorshift128plus(seed_xorshift128plus, false);

        iterations = 1000000000; /* just kill it when you are bored */
        num_partial_copies = 30;

        /* These parameters are randomly generated per test:
         * - whether to do one whole-surface copy or N partial copies per test
         * - which tiling modes to use (LINEAR_ALIGNED, 1D, 2D)
         * - which texture dimensions to use
         * - whether to use VRAM (all tiling modes) and GTT (staging, linear
         *   only) allocations
diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c
index 07b4c22..ee506a2 100644
--- a/src/util/rand_xor.c
+++ b/src/util/rand_xor.c
@@ -1,20 +1,77 @@
+/*
+ * Copyright 2017 Timothy Arceri
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE
+ * SOFTWARE.
+ *
+ */
+
+#if defined(__linux__)
+#include <sys/file.h>
+#include <unistd.h>
+#endif
+
 #include "rand_xor.h"

 /* Super fast random number generator.
  *
  * This rand_xorshift128plus function by Sebastiano Vigna belongs
  * to the public domain.
  */
 uint64_t
 rand_xorshift128plus(uint64_t *seed)
 {
    uint64_t *s = seed;

    uint64_t s1 = s[0];
    const uint64_t s0 = s[1];
    s[0] = s0;
    s1 ^= s1 << 23;
    s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);

    return s[1] + s0;
 }
+
+void
+s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed)
+{
+#if defined(__linux__)
+   if (!randomised_seed)
+      goto fixed_seed;
+
+   int fd = open("/dev/urandom", O_RDONLY);
+   if (fd < 0)
+      goto fixed_seed;
+
+   size_t seed_size = sizeof(uint64_t) * 2;
+   if (read(fd, seed, seed_size) != seed_size) {
+      close(fd);
+      goto fixed_seed;
+   }
+
+   close(fd);
+   return;
+
+fixed_seed:
+#endif
+
+   /* Fallback to a fixed seed */
+   seed[0] = 0x3bffb83978e24f88;
+   seed[1] = 0x9238d5d56c71cd35;
+}
diff --git a/src/util/rand_xor.h b/src/util/rand_xor.h
index d5144e9..532d549 100644
--- a/src/util/rand_xor.h
+++ b/src/util/rand_xor.h
@@ -19,15 +19,19 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE
  * SOFTWARE.
  *
  */

 #ifndef RAND_XOR_H
 #define RAND_XOR_H

 #include <stdint.h>
+#include <stdbool.h>

 uint64_t
 rand_xorshift128plus(uint64_t *seed);

+void
+s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed);
+
 #endif /* RAND_XOR_H */
--
2.9.3

_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev
_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev

Reply via email to