From owner-svn-src-head@FreeBSD.ORG Tue Oct 9 17:27:28 2012 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 166B8DB3; Tue, 9 Oct 2012 17:27:28 +0000 (UTC) (envelope-from mdf356@gmail.com) Received: from mail-pb0-f54.google.com (mail-pb0-f54.google.com [209.85.160.54]) by mx1.freebsd.org (Postfix) with ESMTP id 65AFE8FC0C; Tue, 9 Oct 2012 17:27:27 +0000 (UTC) Received: by mail-pb0-f54.google.com with SMTP id rp8so6061994pbb.13 for ; Tue, 09 Oct 2012 10:27:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=7KmQktE6dOBy9XPMk1iqSeshPsfweNF+4vOFZk3w85o=; b=Z8T3QEkHwrYRkc16xbi9h9KlDoNTl0V/JP7AsCrGSj9MxVET559eiRkNUyiRBPOB8O tFysedafKWDOggEfhPGjWKx/TNQQeLBFPq71aQlENzkg73MoC7U83AF6FaIfdvVWydJp g6fp1jadD5lZPe+oLjqd6jHHN8/lTunhI47oyijfqK8A0HWH5SoIxGUm6ut2oISRZyPS YzQASYwbCURPVDv/0WfikFmaCVo3JTE3B2S5a5fQXqAtgZvmnFZXvihbjKKLVj/recX1 qr1tUMHqU5Vs1QlmqdDJ6FVO0oKTAT6lMqYL8Qe0dYyIAFWN7pMSHuy9H/K/H/XgP7ca FQCw== MIME-Version: 1.0 Received: by 10.66.74.65 with SMTP id r1mr54152285pav.75.1349803647206; Tue, 09 Oct 2012 10:27:27 -0700 (PDT) Sender: mdf356@gmail.com Received: by 10.68.13.170 with HTTP; Tue, 9 Oct 2012 10:27:27 -0700 (PDT) In-Reply-To: <977E1107-46D4-476F-A04D-AEFD87D1DE53@FreeBSD.org> References: <201210091425.q99EPFS6020787@svn.freebsd.org> <507451DE.9060909@freebsd.org> <977E1107-46D4-476F-A04D-AEFD87D1DE53@FreeBSD.org> Date: Tue, 9 Oct 2012 10:27:27 -0700 X-Google-Sender-Auth: FJsX_n3ohqHxO089cGEl-ATDBcI Message-ID: Subject: Re: svn commit: r241373 - head/lib/libc/stdlib From: mdf@FreeBSD.org To: David Chisnall Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: svn-src-head@freebsd.org, Eitan Adler , svn-src-all@freebsd.org, src-committers@freebsd.org, Andrey Chernov X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Oct 2012 17:27:28 -0000 On Tue, Oct 9, 2012 at 10:16 AM, David Chisnall wrot= e: > On 9 Oct 2012, at 17:33, Andrey Chernov wrote: > >> Do you check assembler output for _both_ cases? >> In my testing clang and gcc xor's 'junk' properly in case it have >> 'volatile' keyword (as in srandomdev()) and elide it without 'volatile'. >> IMHO this change should be backed out for srandomdev() and adding >> 'volatile' for sranddev() instead. > > In it's original form, it is very dangerous - the whole expression reduce= s to undefined and so the LLVM IR for the call is: > > call void @srand(i32 undef) > > The back end is then free to use any value for the call argument, includi= ng any register value or 0. Since the value is passed in a register, it wi= ll probably just use whatever the last value there is, which may or may not= be anything sensible. On MIPS, for example, this is most likely to be &tv= , and so is 100% deterministic. > > Adding the volatile means that we are doing an XOR with a value left on t= he stack. If this is early on in the application, then it is most likely t= o be 0. If it's later on, then there may be a value here, but it's still n= ot very likely to be something particularly unpredictable. > The original behavior can be recovered by using inline assembly to fetch the value from a register into a local C variable; this would at least not rely on undefined behavior. But I agree it's of dubious value anyways.