Files
visor/tools/protobuf.disable-binary-reading-on-init.patch
T
Etienne Perot 82e00f030e Disable protobuf internal/detrand self-reading behavior.
This function reads a few binary pages from the binary during startup as a seed.

Non-load-bearing, this is only used to make textproto output nondeterministic (lol, yes really).

Shaves 1ms off gVisor cold start.
2026-08-03 15:14:12 -07:00

56 lines
1.3 KiB
Diff

diff --git a/internal/detrand/rand.go b/internal/detrand/rand.go
index 49c8676..d62c007 100644
--- a/internal/detrand/rand.go
+++ b/internal/detrand/rand.go
@@ -9,12 +9,6 @@
// while ensuring that the output is unstable across different builds.
package detrand
-import (
- "encoding/binary"
- "hash/fnv"
- "os"
-)
-
// Disable disables detrand such that all functions returns the zero value.
// This function is not concurrent-safe and must be called during program init.
func Disable() {
@@ -34,36 +28,4 @@ func Intn(n int) int {
return int(randSeed % uint64(n))
}
-// randSeed is a best-effort at an approximate hash of the Go binary.
-var randSeed = binaryHash()
-
-func binaryHash() uint64 {
- // Open the Go binary.
- s, err := os.Executable()
- if err != nil {
- return 0
- }
- f, err := os.Open(s)
- if err != nil {
- return 0
- }
- defer f.Close()
-
- // Hash the size and several samples of the Go binary.
- const numSamples = 8
- var buf [64]byte
- h := fnv.New64()
- fi, err := f.Stat()
- if err != nil {
- return 0
- }
- binary.LittleEndian.PutUint64(buf[:8], uint64(fi.Size()))
- h.Write(buf[:8])
- for i := int64(0); i < numSamples; i++ {
- if _, err := f.ReadAt(buf[:], i*fi.Size()/numSamples); err != nil {
- return 0
- }
- h.Write(buf[:])
- }
- return h.Sum64()
-}
+var randSeed uint64