-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Seed Libc rng from system entropy #43606
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,42 +23,20 @@ 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.windowserror("SystemFunction036 (RtlGenRandom)", 0 == ccall( | ||||||
(:SystemFunction036, :Advapi32), stdcall, UInt8, (Ptr{Cvoid}, UInt32), | ||||||
A, sizeof(A))) | ||||||
Base.RtlGenRandom!(A) | ||||||
A | ||||||
end | ||||||
else | ||||||
|
@@ -332,14 +310,7 @@ function make_seed() | |||||
catch | ||||||
println(stderr, | ||||||
"Entropy pool not available to seed RNG; using ad-hoc entropy sources.") | ||||||
Comment on lines
311
to
312
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 | ||||||
Base._ad_hoc_entropy_source() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return make_seed(seed) | ||||||
end | ||||||
end | ||||||
|
@@ -428,10 +399,6 @@ 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 | ||||||
|
||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a unittest for this (just literally call
@test_warn "ad-hoc" Base._ad_hoc_entropy_source()
, and make sure it returns a reasonable value?