diff mbox series

[v4,12/24] linux-user: Initialize pseudo-random seeds for all guest cpus

Message ID 20190506173353.32206-13-richard.henderson@linaro.org
State Superseded
Headers show
Series Add qemu_getrandom and ARMv8.5-RNG etc | expand

Commit Message

Richard Henderson May 6, 2019, 5:33 p.m. UTC
When the -seed option is given, call qemu_guest_random_seed_main,
putting the subsystem into deterministic mode.  Pass derived seeds
to each cpu created during clone; which is a no-op unless the
subsystem is in deterministic mode.

Cc: Laurent Vivier <laurent@vivier.eu>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

---
 linux-user/main.c    | 21 ++++++++++-----------
 linux-user/syscall.c |  3 +++
 2 files changed, 13 insertions(+), 11 deletions(-)

-- 
2.17.1

Comments

Laurent Vivier May 7, 2019, 2:06 p.m. UTC | #1
On 06/05/2019 19:33, Richard Henderson wrote:
> When the -seed option is given, call qemu_guest_random_seed_main,

> putting the subsystem into deterministic mode.  Pass derived seeds

> to each cpu created during clone; which is a no-op unless the

> subsystem is in deterministic mode.

> 

> Cc: Laurent Vivier <laurent@vivier.eu>

> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

> ---

>   linux-user/main.c    | 21 ++++++++++-----------

>   linux-user/syscall.c |  3 +++

>   2 files changed, 13 insertions(+), 11 deletions(-)

> 

> diff --git a/linux-user/main.c b/linux-user/main.c

> index 3d2230320b..7dfb202e5d 100644

> --- a/linux-user/main.c

> +++ b/linux-user/main.c

> @@ -34,6 +34,7 @@

>   #include "tcg.h"

>   #include "qemu/timer.h"

>   #include "qemu/envlist.h"

> +#include "qemu/guest-random.h"

>   #include "elf.h"

>   #include "trace/control.h"

>   #include "target_elf.h"

> @@ -48,6 +49,7 @@ static int gdbstub_port;

>   static envlist_t *envlist;

>   static const char *cpu_model;

>   static const char *cpu_type;

> +static const char *seed_optarg;

>   unsigned long mmap_min_addr;

>   unsigned long guest_base;

>   int have_guest_base;

> @@ -290,15 +292,9 @@ static void handle_arg_pagesize(const char *arg)

>       }

>   }

>   

> -static void handle_arg_randseed(const char *arg)

> +static void handle_arg_seed(const char *arg)

>   {

> -    unsigned long long seed;

> -

> -    if (parse_uint_full(arg, &seed, 0) != 0 || seed > UINT_MAX) {

> -        fprintf(stderr, "Invalid seed number: %s\n", arg);

> -        exit(EXIT_FAILURE);

> -    }

> -    srand(seed);


It's a detail, but I think you can't remove this srand() in this patch: 
you initialize qemu_guest_random() sequence but the code continues to 
use rand() to have a random number, so the seed value is in fact 
ignored. you can remove it in PATCH 16.

Thanks,
Laurent
diff mbox series

Patch

diff --git a/linux-user/main.c b/linux-user/main.c
index 3d2230320b..7dfb202e5d 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -34,6 +34,7 @@ 
 #include "tcg.h"
 #include "qemu/timer.h"
 #include "qemu/envlist.h"
+#include "qemu/guest-random.h"
 #include "elf.h"
 #include "trace/control.h"
 #include "target_elf.h"
@@ -48,6 +49,7 @@  static int gdbstub_port;
 static envlist_t *envlist;
 static const char *cpu_model;
 static const char *cpu_type;
+static const char *seed_optarg;
 unsigned long mmap_min_addr;
 unsigned long guest_base;
 int have_guest_base;
@@ -290,15 +292,9 @@  static void handle_arg_pagesize(const char *arg)
     }
 }
 
-static void handle_arg_randseed(const char *arg)
+static void handle_arg_seed(const char *arg)
 {
-    unsigned long long seed;
-
-    if (parse_uint_full(arg, &seed, 0) != 0 || seed > UINT_MAX) {
-        fprintf(stderr, "Invalid seed number: %s\n", arg);
-        exit(EXIT_FAILURE);
-    }
-    srand(seed);
+    seed_optarg = arg;
 }
 
 static void handle_arg_gdb(const char *arg)
@@ -433,7 +429,7 @@  static const struct qemu_argument arg_table[] = {
      "",           "run in singlestep mode"},
     {"strace",     "QEMU_STRACE",      false, handle_arg_strace,
      "",           "log system calls"},
-    {"seed",       "QEMU_RAND_SEED",   true,  handle_arg_randseed,
+    {"seed",       "QEMU_RAND_SEED",   true,  handle_arg_seed,
      "",           "Seed for pseudo-random number generator"},
     {"trace",      "QEMU_TRACE",       true,  handle_arg_trace,
      "",           "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
@@ -689,8 +685,11 @@  int main(int argc, char **argv, char **envp)
         do_strace = 1;
     }
 
-    if (getenv("QEMU_RAND_SEED")) {
-        handle_arg_randseed(getenv("QEMU_RAND_SEED"));
+    if (seed_optarg == NULL) {
+        seed_optarg = getenv("QEMU_RAND_SEED");
+    }
+    if (seed_optarg != NULL) {
+        qemu_guest_random_seed_main(seed_optarg, &error_fatal);
     }
 
     target_environ = envlist_to_environ(envlist, NULL);
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 96cd4bf86d..ce377b2fc1 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -110,6 +110,7 @@ 
 #include "uname.h"
 
 #include "qemu.h"
+#include "qemu/guest-random.h"
 #include "fd-trans.h"
 
 #ifndef CLONE_IO
@@ -5441,6 +5442,7 @@  static void *clone_func(void *arg)
         put_user_u32(info->tid, info->child_tidptr);
     if (info->parent_tidptr)
         put_user_u32(info->tid, info->parent_tidptr);
+    qemu_guest_random_seed_thread_part2(cpu->random_seed);
     /* Enable signals.  */
     sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
     /* Signal to the parent that we're ready.  */
@@ -5527,6 +5529,7 @@  static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
            initializing, so temporarily block all signals.  */
         sigfillset(&sigmask);
         sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
+        cpu->random_seed = qemu_guest_random_seed_thread_part1();
 
         /* If this is our first additional thread, we need to ensure we
          * generate code for parallel execution and flush old translations.