Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Seed Libc rng from system entropy" #44362

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,6 @@ include("process.jl")
include("ttyhascolor.jl")
include("secretbuffer.jl")

# RandomDevice support
include("randomdevice.jl")

# core math functions
include("floatfuncs.jl")
include("math.jl")
Expand Down
2 changes: 1 addition & 1 deletion base/libc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ rand(::Type{Float64}) = rand(UInt32) * 2.0^-32

Interface to the C `srand(seed)` function.
"""
srand(seed=Base._make_uint_seed()) = ccall(:srand, Cvoid, (Cuint,), seed)
srand(seed=floor(Int, time()) % Cuint) = ccall(:srand, Cvoid, (Cuint,), seed)

struct Cpasswd
username::Cstring
Expand Down
77 changes: 0 additions & 77 deletions base/randomdevice.jl

This file was deleted.

41 changes: 37 additions & 4 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,42 @@ else # !windows
RandomDevice(; unlimited::Bool=true) = new(unlimited)
end

getfile(rd::RandomDevice) = Base._get_dev_random_fd(rd.unlimited)

rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read(getfile(rd), sp[])
rand(rd::RandomDevice, ::SamplerType{Bool}) = read(getfile(rd), UInt8) % Bool

mutable struct FileRef
@atomic file::Union{IOStream, Nothing}
end

const DEV_RANDOM = FileRef(nothing)
const DEV_URANDOM = FileRef(nothing)

function getfile(rd::RandomDevice)
ref = rd.unlimited ? DEV_URANDOM : DEV_RANDOM
fd = ref.file
if fd === nothing
fd = open(rd.unlimited ? "/dev/urandom" : "/dev/random")
old, ok = @atomicreplace ref.file nothing => fd
if !ok
close(fd)
fd = old::IOStream
end
end
return fd
end

show(io::IO, rd::RandomDevice) =
print(io, RandomDevice, rd.unlimited ? "()" : "(unlimited=false)")

end # os-test

# NOTE: this can't be put within the if-else block above
for T in (Bool, BitInteger_types...)
if Sys.iswindows()
@eval function rand!(rd::RandomDevice, A::Array{$T}, ::SamplerType{$T})
Base.RtlGenRandom!(A)
Base.windowserror("SystemFunction036 (RtlGenRandom)", 0 == ccall(
(:SystemFunction036, :Advapi32), stdcall, UInt8, (Ptr{Cvoid}, UInt32),
A, sizeof(A)))
A
end
else
Expand Down Expand Up @@ -310,7 +332,14 @@ function make_seed()
catch
println(stderr,
"Entropy pool not available to seed RNG; using ad-hoc entropy sources.")
Base._ad_hoc_entropy_source()
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 make_seed(seed)
end
end
Expand Down Expand Up @@ -399,6 +428,10 @@ for T in BitInteger_types
end

function __init__()
@static if !Sys.iswindows()
@atomic DEV_RANDOM.file = nothing
@atomic DEV_URANDOM.file = nothing
end
seed!(GLOBAL_RNG)
end

Expand Down