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

all: don't use global PRNG state from math/rand #10575

Merged
merged 1 commit into from Mar 16, 2020

Conversation

tklauser
Copy link
Member

@tklauser tklauser commented Mar 13, 2020

Some packages initialize the global PRNG state in package math/rand
using the Seed func in their init func. This might influence the PRNG
state for other packages by solely loaded the former package.

Switch to use a per package rand.Rand instance which can then be safely
initialized and used in each package. This also makes it more obvious in
tests which PRNG state is explicitly seeded to a known value.


This change is Reviewable

Some packages initialize the global PRNG state in package math/rand
using the Seed func in their init func. This might influence the PRNG
state for other packages by solely loaded the former package.

Switch to use a per package rand.Rand instance which can then be safely
initialized and used in each package. This also makes it more obvious in
tests which PRNG state is explicitly seeded to a known value.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
@tklauser tklauser added wip release-note/misc This PR makes changes that have no direct user impact. labels Mar 13, 2020
@tklauser tklauser requested a review from a team March 13, 2020 15:46
@tklauser tklauser requested review from a team as code owners March 13, 2020 15:46
@maintainer-s-little-helper maintainer-s-little-helper bot added this to In progress in 1.8.0 Mar 13, 2020
@tklauser
Copy link
Member Author

tklauser commented Mar 13, 2020

test-me-please

vm provisioning failure: https://jenkins.cilium.io/job/Cilium-PR-Ginkgo-Tests-Validated/17903/

@tklauser tklauser changed the title [WIP] all: don't use global PRNG state from math/rand all: don't use global PRNG state from math/rand Mar 13, 2020
@tklauser
Copy link
Member Author

test-me-please

@tklauser tklauser added the kind/cleanup This includes no functional changes. label Mar 16, 2020
@tgraf tgraf merged commit fb105d9 into master Mar 16, 2020
1.8.0 automation moved this from In progress to Merged Mar 16, 2020
@tgraf tgraf deleted the pr/tklauser/no-rand-global branch March 16, 2020 13:01
tklauser added a commit that referenced this pull request Dec 8, 2023
Since Go 1.20 the top-level math/rand functions are auto-seeded by
default, see https://go.dev/doc/go1.20#library and
golang/go#54880. They also use the runtime's
lock-free fastrand64 function when 1. Seed has not been called and 2.
GODEBUG=randautoseed=0 is not used, see
golang/go#49892.

This allows to drop SafeRand and use the global source again. SafeRand
was introduced in commit fac5dde ("rand: add and use
concurrency-safe PRNG source") to fix #10988 by providing a
concurrency-safe PRNG source other than the global math/rand source
which couldn't be used at the time because it can't be interfered with
from unrelated packages by means of calling rand.Seed, see #10575.
rand.Seed is deprecated since GO 1.20 and per the paragraph above the
global source is seeded by default. This makes it unlikely that packages
would interfere with the global PRNG state anymore and the top-level
math/rand functions are safe to use again.

Signed-off-by: Tobias Klauser <tobias@cilium.io>
tklauser added a commit that referenced this pull request Dec 8, 2023
Since Go 1.20 the top-level math/rand functions are auto-seeded by
default, see https://go.dev/doc/go1.20#library and
golang/go#54880. They also use the runtime's
lock-free fastrand64 function when 1. Seed has not been called and 2.
GODEBUG=randautoseed=0 is not used, see
golang/go#49892.

This allows to drop SafeRand and use the global source again. SafeRand
was originally introduced in commit fac5dde ("rand: add and use
concurrency-safe PRNG source") to fix #10988 by providing a
concurrency-safe PRNG source other than the global math/rand source
which could be used at the time because it can't be interfered with from
unrelated packages by means of calling rand.Seed, see #10575. rand.Seed
is deprecated since Go 1.20 and per the paragraph above the global
source is seeded by default. This makes it unlikely that packages would
interfere with the global PRNG state anymore and the top-level math/rand
functions are safe to use again.

Signed-off-by: Tobias Klauser <tobias@cilium.io>
tklauser added a commit that referenced this pull request Dec 8, 2023
Since Go 1.20 the top-level math/rand functions are auto-seeded by
default, see https://go.dev/doc/go1.20#library and
golang/go#54880. They also use the runtime's
lock-free fastrand64 function when 1. Seed has not been called and 2.
GODEBUG=randautoseed=0 is not used, see
golang/go#49892.

This allows to drop SafeRand and use the global source again. SafeRand
was originally introduced in commit fac5dde ("rand: add and use
concurrency-safe PRNG source") to fix #10988 by providing a
concurrency-safe PRNG source other than the global math/rand source
which could be used at the time because it can't be interfered with from
unrelated packages by means of calling rand.Seed, see #10575. rand.Seed
is deprecated since Go 1.20 and per the paragraph above the global
source is seeded by default. This makes it unlikely that packages would
interfere with the global PRNG state anymore and the top-level math/rand
functions are safe to use again.

Signed-off-by: Tobias Klauser <tobias@cilium.io>
tklauser added a commit that referenced this pull request Dec 8, 2023
Since Go 1.20 the top-level math/rand functions are auto-seeded by
default, see https://go.dev/doc/go1.20#library and
golang/go#54880. They also use the runtime's
lock-free fastrand64 function when 1. Seed has not been called and 2.
GODEBUG=randautoseed=0 is not used, see
golang/go#49892.

This allows to drop SafeRand and use the global source again. SafeRand
was originally introduced in commit fac5dde ("rand: add and use
concurrency-safe PRNG source") to fix #10988 by providing a
concurrency-safe PRNG source other than the global math/rand source
which could be used at the time because it can't be interfered with from
unrelated packages by means of calling rand.Seed, see #10575. rand.Seed
is deprecated since Go 1.20 and per the paragraph above the global
source is seeded by default. This makes it unlikely that packages would
interfere with the global PRNG state anymore and the top-level math/rand
functions are safe to use again.

Signed-off-by: Tobias Klauser <tobias@cilium.io>
tklauser added a commit that referenced this pull request Dec 8, 2023
Since Go 1.20 the top-level math/rand functions are auto-seeded by
default, see https://go.dev/doc/go1.20#library and
golang/go#54880. They also use the runtime's
lock-free fastrand64 function when 1. Seed has not been called and 2.
GODEBUG=randautoseed=0 is not used, see
golang/go#49892.

This allows to drop SafeRand and use the global source again. SafeRand
was originally introduced in commit fac5dde ("rand: add and use
concurrency-safe PRNG source") to fix #10988 by providing a
concurrency-safe PRNG source other than the global math/rand source
which could be used at the time because it can't be interfered with from
unrelated packages by means of calling rand.Seed, see #10575. rand.Seed
is deprecated since Go 1.20 and per the paragraph above the global
source is seeded by default. This makes it unlikely that packages would
interfere with the global PRNG state anymore and the top-level math/rand
functions are safe to use again.

Signed-off-by: Tobias Klauser <tobias@cilium.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/cleanup This includes no functional changes. release-note/misc This PR makes changes that have no direct user impact.
Projects
No open projects
1.8.0
  
Merged
Development

Successfully merging this pull request may close these issues.

None yet

2 participants