forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Seed Libc rng from system entropy (JuliaLang#43606)
We're seeing lots of CI failures that look like collisions in the names of temporary variables. Currently we're seeding the libc rng from a low entropy clock, which is somewhat likely to have collisions particular if (as we are), you launch all your processes at exactly the right time. Try to fix that by seeding the libc using system entropy. Of course that still leaves the potential for birthday problems, but hopfully this will tide us over until JuliaLang#43597 gets fixed.
- Loading branch information
1 parent
76c0be8
commit 53ba2d9
Showing
4 changed files
with
85 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# This file is a part of Julia. License is MIT: https://julialang.org/license | ||
|
||
# This file contains the minimal support of RandomDevice for Base's own usage. | ||
# The actual RandomDevice type that makes use of this infrastructure is defined | ||
# in the Random stdlib. | ||
|
||
module DevRandomState | ||
if !Sys.iswindows() | ||
mutable struct FileRef | ||
@atomic file::Union{IOStream, Nothing} | ||
end | ||
const DEV_RANDOM = FileRef(nothing) | ||
const DEV_URANDOM = FileRef(nothing) | ||
end | ||
function __init__() | ||
if !Sys.iswindows() | ||
@atomic DEV_RANDOM.file = nothing | ||
@atomic DEV_URANDOM.file = nothing | ||
end | ||
end | ||
end | ||
|
||
if Sys.iswindows() | ||
function RtlGenRandom!(A::Union{Array, Ref}) | ||
Base.windowserror("SystemFunction036 (RtlGenRandom)", 0 == ccall( | ||
(:SystemFunction036, :Advapi32), stdcall, UInt8, (Ptr{Cvoid}, UInt32), | ||
A, sizeof(A))) | ||
end | ||
|
||
# Manually implemented to work without the Random machinery | ||
function _rand_uint() | ||
r = Ref{Cuint}() | ||
RtlGenRandom!(r) | ||
return r[] | ||
end | ||
else # !windows | ||
function _get_dev_random_fd(unlimited::Bool) | ||
ref = unlimited ? DevRandomState.DEV_URANDOM : DevRandomState.DEV_RANDOM | ||
fd = ref.file | ||
if fd === nothing | ||
fd = open(unlimited ? "/dev/urandom" : "/dev/random") | ||
old, ok = @atomicreplace ref.file nothing => fd | ||
if !ok | ||
close(fd) | ||
fd = old::IOStream | ||
end | ||
end | ||
return fd | ||
end | ||
|
||
# Manually implemented to work without the Random machinery | ||
function _rand_uint() | ||
return read(_get_dev_random_fd(true), Cuint) | ||
end | ||
end # os-test | ||
|
||
function _ad_hoc_entropy() | ||
println(stderr, | ||
"Entropy pool not available to seed RNG; using ad-hoc entropy sources.") | ||
seed = reinterpret(UInt64, time()) | ||
seed = hash(seed, getpid() % UInt) | ||
try | ||
seed = hash(seed, parse(UInt64, | ||
read(pipeline(`ifconfig`, `sha1sum`), String)[1:40], | ||
base = 16) % UInt) | ||
catch | ||
end | ||
return seed | ||
end | ||
|
||
function _make_uint_seed() | ||
try | ||
_rand_uint() | ||
catch | ||
return _ad_hoc_entropy() % Cuint | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters