// localDepth uint8
// // N.B Padding
//
- // seed uintptr
- //
// index int
//
// // From groups.
makefield("capacity", types.Types[types.TUINT16]),
makefield("growthLeft", types.Types[types.TUINT16]),
makefield("localDepth", types.Types[types.TUINT8]),
- makefield("seed", types.Types[types.TUINTPTR]),
makefield("index", types.Types[types.TINT]),
makefield("groups_data", types.Types[types.TUNSAFEPTR]),
makefield("groups_lengthMask", types.Types[types.TUINT64]),
table.SetUnderlying(types.NewStruct(fields))
types.CalcSize(table)
- // The size of table should be 48 bytes on 64 bit
- // and 36 bytes on 32 bit platforms.
- if size := int64(3*2 + 2*1 /* one extra for padding */ + 2*8 + 3*types.PtrSize); table.Size() != size {
+ // The size of table should be 40 bytes on 64 bit
+ // and 32 bytes on 32 bit platforms.
+ if size := int64(3*2 + 2*1 /* one extra for padding */ + 2*8 + 2*types.PtrSize); table.Size() != size {
base.Fatalf("internal/runtime/maps.table size not correct: got %d, want %d", table.Size(), size)
}
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.
}
m := &Map{
- //TODO
- //seed: uintptr(rand()),
-
- //directory: make([]*table, dirSize),
+ seed: uintptr(rand()),
globalDepth: globalDepth,
globalShift: depthToShift(globalDepth),
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")
}
// 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")
}
if key == *(*uint32)(slotKey) {
slotElem = g.elem(typ, i)
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
match = match.removeFirst()
t.used++
m.used++
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
if key == *(*unsafe.Pointer)(slotKey) {
slotElem = g.elem(typ, i)
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
match = match.removeFirst()
t.used++
m.used++
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
if key == *(*uint64)(slotKey) {
slotElem = g.elem(typ, i)
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
match = match.removeFirst()
t.used++
m.used++
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
if key == *(*unsafe.Pointer)(slotKey) {
slotElem = g.elem(typ, i)
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
match = match.removeFirst()
t.used++
m.used++
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
*(*string)(slotKey) = key
slotElem = g.elem(typ, i)
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
match = match.removeFirst()
t.used++
m.used++
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
slotElem = *((*unsafe.Pointer)(slotElem))
}
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
match = match.removeFirst()
t.used++
m.used++
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
break outer
}
// but this table has not yet been split.
localDepth uint8
- // seed is the hash seed, computed as a unique random number per table.
- // TODO(prattmic): Populate this on table initialization.
- seed uintptr
-
// Index of this table in the Map directory. This is the index of the
// _first_ location in the directory. The table may occur in multiple
// sequential indicies.
// Get performs a lookup of the key that key points to. It returns a pointer to
// the element, or false if the key doesn't exist.
-func (t *table) Get(typ *abi.SwissMapType, key unsafe.Pointer) (unsafe.Pointer, bool) {
+func (t *table) Get(typ *abi.SwissMapType, m *Map, key unsafe.Pointer) (unsafe.Pointer, bool) {
// TODO(prattmic): We could avoid hashing in a variety of special
// cases.
//
// - One entry maps could just directly compare the single entry
// without hashing.
// - String keys could do quick checks of a few bytes before hashing.
- hash := typ.Hasher(key, t.seed)
- _, elem, ok := t.getWithKey(typ, hash, key)
+ hash := typ.Hasher(key, m.seed)
+ _, elem, ok := t.getWithKey(typ, hash, key)
return elem, ok
}
slotElem = *((*unsafe.Pointer)(slotElem))
}
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
return slotElem, true
}
match = match.removeFirst()
t.used++
m.used++
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
return slotElem, true
}
}
func (t *table) Delete(typ *abi.SwissMapType, m *Map, key unsafe.Pointer) {
- hash := typ.Hasher(key, t.seed)
+ hash := typ.Hasher(key, m.seed)
seq := makeProbeSeq(h1(hash), t.groups.lengthMask)
for ; ; seq = seq.next() {
g.ctrls().set(i, ctrlDeleted)
}
- t.checkInvariants(typ)
+ t.checkInvariants(typ, m)
return
}
match = match.removeFirst()
t.used = 0
t.resetGrowthLeft()
-
- // Reset the hash seed to make it more difficult for attackers to
- // repeatedly trigger hash collisions. See issue
- // https://github.com/golang/go/issues/25237.
- // TODO
- //t.seed = uintptr(rand())
}
type Iter struct {
elem = *((*unsafe.Pointer)(elem))
}
- hash := typ.Hasher(key, t.seed)
+ hash := typ.Hasher(key, m.seed)
var newTable *table
if hash&mask == 0 {
newTable = left
elem = *((*unsafe.Pointer)(elem))
}
- hash := typ.Hasher(key, t.seed)
+ hash := typ.Hasher(key, m.seed)
// TODO(prattmic): For indirect key/elem, this is
// allocating new objects for key/elem. That is
}
}
- newTable.checkInvariants(typ)
+ newTable.checkInvariants(typ, m)
m.replaceTable(newTable)
}
const debugLog = false
-func (t *table) checkInvariants(typ *abi.SwissMapType) {
+func (t *table) checkInvariants(typ *abi.SwissMapType, m *Map) {
if !debugLog {
return
}
continue
}
- if _, ok := t.Get(typ, key); !ok {
- hash := typ.Hasher(key, t.seed)
+ if _, ok := t.Get(typ, m, key); !ok {
+ hash := typ.Hasher(key, m.seed)
print("invariant failed: slot(", i, "/", j, "): key ")
dump(key, typ.Key.Size_)
print(" not found [hash=", hash, ", h2=", h2(hash), " h1=", h1(hash), "]\n")
- t.Print(typ)
+ t.Print(typ, m)
panic("invariant failed: slot: key not found")
}
}
if used != t.used {
print("invariant failed: found ", used, " used slots, but used count is ", t.used, "\n")
- t.Print(typ)
+ t.Print(typ, m)
panic("invariant failed: found mismatched used slot count")
}
growthLeft := (t.capacity*maxAvgGroupLoad)/abi.SwissMapGroupSlots - t.used - deleted
if growthLeft != t.growthLeft {
print("invariant failed: found ", t.growthLeft, " growthLeft, but expected ", growthLeft, "\n")
- t.Print(typ)
+ t.Print(typ, m)
panic("invariant failed: found mismatched growthLeft")
}
if deleted != t.tombstones() {
print("invariant failed: found ", deleted, " tombstones, but expected ", t.tombstones(), "\n")
- t.Print(typ)
+ t.Print(typ, m)
panic("invariant failed: found mismatched tombstones")
}
if empty == 0 {
print("invariant failed: found no empty slots (violates probe invariant)\n")
- t.Print(typ)
+ t.Print(typ, m)
panic("invariant failed: found no empty slots (violates probe invariant)")
}
}
-
-func (t *table) Print(typ *abi.SwissMapType) {
+func (t *table) Print(typ *abi.SwissMapType, m *Map) {
print(`table{
- seed: `, t.seed, `
index: `, t.index, `
localDepth: `, t.localDepth, `
capacity: `, t.capacity, `