util/deephash: specialize for netip.Addr and drop AppendTo support (#5402)
There are 5 types that we care about that implement AppendTo: key.DiscoPublic key.NodePublic netip.Prefix netipx.IPRange netip.Addr The key types are thin wrappers around [32]byte and are memory hashable. The netip.Prefix and netipx.IPRange types are thin wrappers over netip.Addr and are hashable by default if netip.Addr is hashable. The netip.Addr type is the only one with a complex structure where the default behavior of deephash does not hash it correctly due to the presence of the intern.Value type. Drop support for AppendTo and instead add specialized hashing for netip.Addr that would be semantically equivalent to == on the netip.Addr values. The AppendTo support was already broken prior to this change. It was fully removed (intentionally or not) in #4870. It was partially restored in #4858 for the fast path, but still broken in the slow path. Just drop support for it altogether. This does mean we lack any ability for types to self-hash themselves. In the future we can add support for types that implement: interface { DeepHash() Sum } Test and fuzz cases were added for the relevant types that used to rely on the AppendTo method. FuzzAddr has been executed on 1 billion samples without issues. Signed-off-by: Joe Tsai joetsai@digital-static.net
This commit is contained in:
+33
-45
@@ -14,9 +14,7 @@
|
||||
// - time.Time are compared based on whether they are the same instant in time
|
||||
// and also in the same zone offset. Monotonic measurements and zone names
|
||||
// are ignored as part of the hash.
|
||||
// - Types which implement interface { AppendTo([]byte) []byte } use
|
||||
// the AppendTo method to produce a textual representation of the value.
|
||||
// Thus, two values are equal if AppendTo produces the same bytes.
|
||||
// - netip.Addr are compared based on a shallow comparison of the struct.
|
||||
//
|
||||
// WARNING: This package, like most of the tailscale.com Go module,
|
||||
// should be considered Tailscale-internal; we make no API promises.
|
||||
@@ -29,6 +27,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"net/netip"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -210,10 +209,7 @@ type appenderTo interface {
|
||||
AppendTo([]byte) []byte
|
||||
}
|
||||
|
||||
var (
|
||||
uint8Type = reflect.TypeOf(byte(0))
|
||||
timeTimeType = reflect.TypeOf(time.Time{})
|
||||
)
|
||||
var uint8Type = reflect.TypeOf(byte(0))
|
||||
|
||||
// typeInfo describes properties of a type.
|
||||
//
|
||||
@@ -302,36 +298,6 @@ func (h *hasher) hashInt64v(v addressableValue) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func hashStructAppenderTo(h *hasher, v addressableValue) bool {
|
||||
if !v.CanInterface() {
|
||||
return false // slow path
|
||||
}
|
||||
a := v.Addr().Interface().(appenderTo)
|
||||
size := h.scratch[:8]
|
||||
record := a.AppendTo(size)
|
||||
binary.LittleEndian.PutUint64(record, uint64(len(record)-len(size)))
|
||||
h.HashBytes(record)
|
||||
return true
|
||||
}
|
||||
|
||||
// hashPointerAppenderTo hashes v, a reflect.Ptr, that implements appenderTo.
|
||||
func hashPointerAppenderTo(h *hasher, v addressableValue) bool {
|
||||
if !v.CanInterface() {
|
||||
return false // slow path
|
||||
}
|
||||
if v.IsNil() {
|
||||
h.HashUint8(0) // indicates nil
|
||||
return true
|
||||
}
|
||||
h.HashUint8(1) // indicates visiting a pointer
|
||||
a := v.Interface().(appenderTo)
|
||||
size := h.scratch[:8]
|
||||
record := a.AppendTo(size)
|
||||
binary.LittleEndian.PutUint64(record, uint64(len(record)-len(size)))
|
||||
h.HashBytes(record)
|
||||
return true
|
||||
}
|
||||
|
||||
// fieldInfo describes a struct field.
|
||||
type fieldInfo struct {
|
||||
index int // index of field for reflect.Value.Field(n); -1 if invalid
|
||||
@@ -462,21 +428,19 @@ func genTypeHasher(t reflect.Type) typeHasherFunc {
|
||||
eti := getTypeInfo(et)
|
||||
return genHashArray(t, eti)
|
||||
case reflect.Struct:
|
||||
if t == timeTimeType {
|
||||
switch t {
|
||||
case timeTimeType:
|
||||
return (*hasher).hashTimev
|
||||
case netipAddrType:
|
||||
return (*hasher).hashAddrv
|
||||
default:
|
||||
return genHashStructFields(t)
|
||||
}
|
||||
if t.Implements(appenderToType) {
|
||||
return hashStructAppenderTo
|
||||
}
|
||||
return genHashStructFields(t)
|
||||
case reflect.Pointer:
|
||||
et := t.Elem()
|
||||
if typeIsMemHashable(et) {
|
||||
return genHashPtrToMemoryRange(et)
|
||||
}
|
||||
if t.Implements(appenderToType) {
|
||||
return hashPointerAppenderTo
|
||||
}
|
||||
if !typeIsRecursive(t) {
|
||||
eti := getTypeInfo(et)
|
||||
return func(h *hasher, v addressableValue) bool {
|
||||
@@ -544,6 +508,30 @@ func (h *hasher) hashTimev(v addressableValue) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// hashAddrv hashes v, of type netip.Addr.
|
||||
func (h *hasher) hashAddrv(v addressableValue) bool {
|
||||
// The formatting of netip.Addr covers the
|
||||
// IP version, the address, and the optional zone name (for v6).
|
||||
// This is equivalent to a1.MarshalBinary() == a2.MarshalBinary().
|
||||
ip := *(*netip.Addr)(v.Addr().UnsafePointer())
|
||||
switch {
|
||||
case !ip.IsValid():
|
||||
h.HashUint64(0)
|
||||
case ip.Is4():
|
||||
b := ip.As4()
|
||||
h.HashUint64(4)
|
||||
h.HashUint32(binary.LittleEndian.Uint32(b[:]))
|
||||
case ip.Is6():
|
||||
b := ip.As16()
|
||||
z := ip.Zone()
|
||||
h.HashUint64(16 + uint64(len(z)))
|
||||
h.HashUint64(binary.LittleEndian.Uint64(b[:8]))
|
||||
h.HashUint64(binary.LittleEndian.Uint64(b[8:]))
|
||||
h.HashString(z)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// hashSliceMem hashes v, of kind Slice, with a memhash-able element type.
|
||||
func (h *hasher) hashSliceMem(v addressableValue) bool {
|
||||
vLen := v.Len()
|
||||
|
||||
Reference in New Issue
Block a user