public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alexander Monakov <amonakov@ispras.ru>
To: Sebastian Huber <sebastian.huber@embedded-brains.de>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: Using -frandom-seed=0 for reproducible builds?
Date: Thu, 29 Feb 2024 21:46:35 +0300 (MSK)	[thread overview]
Message-ID: <552cd7f3-8ced-0f3a-d2be-64dad87a2a05@ispras.ru> (raw)
In-Reply-To: <b038b753-cb0a-4cfa-8bb8-caa0ba721544@embedded-brains.de>


On Thu, 29 Feb 2024, Sebastian Huber wrote:

> Hello,
> 
> I noticed that the coverage and profiling instrumentation needs the
> -frandom-seed flag to be reproducible. The documentation
> 
> https://gcc.gnu.org/onlinedocs/gcc/Developer-Options.html#index-frandom-seed
> 
> says:
> 
>     The string should be different for every file you compile.
> 
> Searching the internet suggests that using -frandom-seed=0 is not unusual.

Heh, -frandom-seed=0 is the same as not passing -frandom-seed at all:
get_random_seed reseeds random_seed if it is zero.

> Why should the string be different for every file you compile?

In very rare circumstances GCC relies on random identifiers not colliding
at link stage. One example I'm aware of is global constructors on AIX
(which used the COFF object file format, not ELF). If you deliberately
construct a testcase where two constructor function get the same section
name, you get a linker warning:

ld: 0711-224 WARNING: Duplicate symbol: global constructors keyed to 65535_0_t.c_00000000_0xb3fc9bb6
ld: 0711-224 WARNING: Duplicate symbol: .global constructors keyed to 65535_0_t.c_00000000_0xb3fc9bb6
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.

and one of the constructors is not executed (a correctness issue). On ELF
targets this issue does not arise.

> Or, what could happen if two files use the same random seed?

Per the above, the program may be miscompiled.

> For the coverage profiling, the -frandom-seed flag just results in
> "local_tick" being -1. The "local_tick" is only used to initialize the random
> seed and in coverage.cc for a file stamp for notes file.
> 
> Would it make sense to add a new option to just control "local_tick" for
> reproducible coverage instrumentation and don't touch the random seed stuff?

I am not sure why you'd want to do that, but FWIW the usual trick
is to pass the source file name as the seed:

    gcc -c src/foo.c -frandom-seed=src/foo.c

this way each file with high probability gets a reproducible unique seed.

Alexander

      reply	other threads:[~2024-02-29 18:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-29  7:32 Sebastian Huber
2024-02-29 18:46 ` Alexander Monakov [this message]

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=552cd7f3-8ced-0f3a-d2be-64dad87a2a05@ispras.ru \
    --to=amonakov@ispras.ru \
    --cc=gcc-help@gcc.gnu.org \
    --cc=sebastian.huber@embedded-brains.de \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).