Re: Using -frandom-seed=0 for reproducible builds?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux