Skip to content

Commit 845385d

Browse files
committed
runtime: manually initialize xorshift state
This ensures: 1. The xorshift state is initialized during interp. 2. The xorshift state gets initialized to a real random number on hardware that supports it at runtime.
1 parent 5d4af71 commit 845385d

File tree

4 files changed

+6
-3
lines changed

4 files changed

+6
-3
lines changed

src/runtime/algorithm.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func fastrand() uint32 {
2323
return xorshift32State
2424
}
2525

26-
func init() {
26+
func initRand() {
2727
r, _ := hardwareRand()
2828
xorshift64State = uint64(r | 1) // protect against 0
2929
xorshift32State = uint32(xorshift64State)
3030
}
3131

32-
var xorshift32State uint32
32+
var xorshift32State uint32 = 1
3333

3434
func xorshift32(x uint32) uint32 {
3535
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
@@ -49,7 +49,7 @@ func fastrand64() uint64 {
4949
return xorshift64State
5050
}
5151

52-
var xorshift64State uint64
52+
var xorshift64State uint64 = 1
5353

5454
// 64-bit xorshift multiply rng from http://vigna.di.unimi.it/ftp/papers/xorshift.pdf
5555
func xorshiftMult64(x uint64) uint64 {

src/runtime/runtime_wasmentry.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func wasmEntryReactor() {
3535
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
3636
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
3737
initHeap()
38+
initRand()
3839

3940
if hasScheduler {
4041
// A package initializer might do funky stuff like start a goroutine and

src/runtime/scheduler_cooperative.go

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ func sleep(duration int64) {
247247
// With a scheduler, init and the main function are invoked in a goroutine before starting the scheduler.
248248
func run() {
249249
initHeap()
250+
initRand()
250251
go func() {
251252
initAll()
252253
callMain()

src/runtime/scheduler_none.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const hasParallelism = false
1313
// With the "none" scheduler, init and the main function are invoked directly.
1414
func run() {
1515
initHeap()
16+
initRand()
1617
initAll()
1718
callMain()
1819
mainExited = true

0 commit comments

Comments
 (0)