aboutsummaryrefslogtreecommitdiff
path: root/src/internal/runtime/maps/map.go
diff options
context:
space:
mode:
authorMichael Pratt <mpratt@google.com>2024-09-23 14:46:09 -0400
committerGopher Robot <gobot@golang.org>2024-10-30 15:20:52 +0000
commitd95b7980aa1ef94983983cd98e005947e83d562d (patch)
tree89fa94d8c423f82a11b83c761fb4b3a0d867b247 /src/internal/runtime/maps/map.go
parentf782e161623e68e25cc3a81e55dac887afd301d5 (diff)
downloadgo-d95b7980aa1ef94983983cd98e005947e83d562d.tar.xz
internal/runtime/maps: cleanup seed usage
Keep only a single seed; initialize it; and reset it when the map is empty. For #54766. Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest-swissmap Change-Id: Icc231f70957337a2d0dcd9c7daf9bd3cb4354d71 Reviewed-on: https://go-review.googlesource.com/c/go/+/616466 Auto-Submit: Michael Pratt <mpratt@google.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/internal/runtime/maps/map.go')
-rw-r--r--src/internal/runtime/maps/map.go17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/internal/runtime/maps/map.go b/src/internal/runtime/maps/map.go
index d9df9fd015..80de397d31 100644
--- a/src/internal/runtime/maps/map.go
+++ b/src/internal/runtime/maps/map.go
@@ -197,7 +197,6 @@ type Map struct {
used uint64
// seed is the hash seed, computed as a unique random number per map.
- // TODO(prattmic): Populate this on table initialization.
seed uintptr
// The directory of tables.
@@ -293,10 +292,7 @@ func NewMap(mt *abi.SwissMapType, hint, maxAlloc uintptr) *Map {
}
m := &Map{
- //TODO
- //seed: uintptr(rand()),
-
- //directory: make([]*table, dirSize),
+ seed: uintptr(rand()),
globalDepth: globalDepth,
globalShift: depthToShift(globalDepth),
@@ -654,6 +650,13 @@ func (m *Map) Delete(typ *abi.SwissMapType, key unsafe.Pointer) {
m.directoryAt(idx).Delete(typ, m, key)
}
+ if m.used == 0 {
+ // Reset the hash seed to make it more difficult for attackers
+ // to repeatedly trigger hash collisions. See
+ // https://go.dev/issue/25237.
+ m.seed = uintptr(rand())
+ }
+
if m.writing == 0 {
fatal("concurrent map writes")
}
@@ -735,6 +738,10 @@ func (m *Map) Clear(typ *abi.SwissMapType) {
// TODO: shrink directory?
}
+ // Reset the hash seed to make it more difficult for attackers to
+ // repeatedly trigger hash collisions. See https://go.dev/issue/25237.
+ m.seed = uintptr(rand())
+
if m.writing == 0 {
fatal("concurrent map writes")
}