qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Magnus Reftel <reftel@spotify.com>, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] linux-user: Let user specify random seed
Date: Thu, 09 Oct 2014 09:27:39 -0600	[thread overview]
Message-ID: <5436A96B.6050708@redhat.com> (raw)
In-Reply-To: <1412843785-960-2-git-send-email-reftel@spotify.com>

[-- Attachment #1: Type: text/plain, Size: 3143 bytes --]

On 10/09/2014 02:36 AM, Magnus Reftel wrote:
> This patch introduces the -seed command line option and the
> QEMU_RAND_SEED environment variable for setting the random seed, which
> is used for the AT_RANDOM ELF aux entry.
> 
> Signed-off-by: Magnus Reftel <reftel@spotify.com>
> ---

>  
> +static void handle_arg_randseed(const char *arg)
> +{
> +    unsigned long seed;
> +    char* end;

Style: we prefer:

char *end;

> +    seed = strtoul(arg, &end, 0);
> +    if (end==arg || *end!='\0' || seed > UINT_MAX) {

Style: spaces around operators:

if (end == arg || *end || seed > UINT_MAX) {

Bug: strtoul() sometimes reports error via errno; the only safe way to
use it is to first prime errno = 0, then do strtoul, then check if errno
was changed.

Reimplementation: util/cutils.c already provides parse_uint() that takes
care of calling strtoul safely (hmm, that version only parses 64-bit
numbers; maybe we should expand it to also parse 32-bit numbers?)

Surprising behavior: your code behaves differently on 32-bit hosts than
it does on 64-bit hosts.  Seriously.  strotoul() has the annoying
specification of requiring twos-complement wraparound according to the
size of long, which means "-1" on a 32-bit platform parses as 0xffffffff
(accepted), while on a 64-bit platform parses it as 0xffffffffffffffff
(which you reject as > UINT_MAX); conversely "-18446744073709551615"
fails to parse due to overflow on a 32-bit platform, while successfully
being parsed as 1 on 64-bit.

> +        fprintf(stderr, "Invalid seed number: %s\n", arg);
> +        exit(1);
> +    }
> +    srand(seed);
> +}
> +
>  static void handle_arg_gdb(const char *arg)
>  {
>      gdbstub_port = atoi(arg);
> @@ -3674,6 +3686,8 @@ 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 for pseudo-random number generator"},
>      {"version",    "QEMU_VERSION",     false, handle_arg_version,
>       "",           "display version information and exit"},
>      {NULL, NULL, false, NULL, NULL, NULL}
> @@ -3856,6 +3870,8 @@ int main(int argc, char **argv, char **envp)
>      cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
>  #endif
>  
> +    srand(time(NULL));
> +
>      optind = parse_args(argc, argv);
>  
>      /* Zero out regs */
> @@ -3926,6 +3942,10 @@ int main(int argc, char **argv, char **envp)
>          do_strace = 1;
>      }
>  
> +    if (getenv("QEMU_RAND_SEED")) {
> +        handle_arg_randseed(getenv("QEMU_RAND_SEED"));
> +    }

Now that you have exactly one caller of the static function, it might
make sense to just inline the body of that function here.

> +
>      target_environ = envlist_to_environ(envlist, NULL);
>      envlist_free(envlist);
>  
> 

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]

  reply	other threads:[~2014-10-09 15:27 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-09  8:36 [Qemu-devel] [PATCH v2] linux-user: Let user specify random seed Magnus Reftel
2014-10-09  8:36 ` [Qemu-devel] [PATCH] " Magnus Reftel
2014-10-09 15:27   ` Eric Blake [this message]
2014-10-09 19:10     ` Magnus Reftel
2014-10-09 19:43 ` [Qemu-devel] [PATCH v2] " Tom Musta
2014-10-10  8:00   ` Magnus Reftel
  -- strict thread matches above, loose matches on Subject: below --
2014-10-14 15:18 [Qemu-devel] [PATCH v5] " Magnus Reftel
2014-10-14 15:18 ` [Qemu-devel] [PATCH] " Magnus Reftel
2014-10-14  9:50 [Qemu-devel] [PATCH v4] " Magnus Reftel
2014-10-14  9:50 ` [Qemu-devel] [PATCH] " Magnus Reftel
2014-10-14 15:07   ` Eric Blake
2014-10-09 19:12 [Qemu-devel] [PATCH v3] " Magnus Reftel
2014-10-09 19:12 ` [Qemu-devel] [PATCH] " Magnus Reftel
2014-10-09 21:30   ` Eric Blake
2014-10-10  8:16     ` Magnus Reftel
2014-10-10 16:20       ` Eric Blake
2014-10-14  9:46         ` Magnus Reftel
2014-10-08 12:13 [Qemu-devel] [PATCH] Let user specify random seed for linux-user Magnus Reftel
2014-10-08 12:13 ` [Qemu-devel] [PATCH] linux-user: Let user specify random seed Magnus Reftel
2014-10-08 14:45   ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5436A96B.6050708@redhat.com \
    --to=eblake@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=reftel@spotify.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).