mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-05 04:11:59 +00:00
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:
@@ -4,11 +4,34 @@
|
||||
|
||||
package deephash
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
"net/netip"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
timeTimeType = reflect.TypeOf((*time.Time)(nil)).Elem()
|
||||
netipAddrType = reflect.TypeOf((*netip.Addr)(nil)).Elem()
|
||||
)
|
||||
|
||||
// typeIsSpecialized reports whether this type has specialized hashing.
|
||||
// These are never memory hashable and never considered recursive.
|
||||
func typeIsSpecialized(t reflect.Type) bool {
|
||||
switch t {
|
||||
case timeTimeType, netipAddrType:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// typeIsMemHashable reports whether t can be hashed by directly hashing its
|
||||
// contiguous bytes in memory (e.g. structs with gaps are not mem-hashable).
|
||||
func typeIsMemHashable(t reflect.Type) bool {
|
||||
if typeIsSpecialized(t) {
|
||||
return false
|
||||
}
|
||||
if t.Size() == 0 {
|
||||
return true
|
||||
}
|
||||
@@ -50,6 +73,11 @@ func typeIsRecursive(t reflect.Type) bool {
|
||||
delete(inStack, t)
|
||||
}()
|
||||
|
||||
// Types with specialized hashing are never considered recursive.
|
||||
if typeIsSpecialized(t) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Any type that is memory hashable must not be recursive since
|
||||
// cycles can only occur if pointers are involved.
|
||||
if typeIsMemHashable(t) {
|
||||
@@ -72,10 +100,6 @@ func typeIsRecursive(t reflect.Type) bool {
|
||||
case reflect.Map:
|
||||
return visitType(t.Key()) || visitType(t.Elem())
|
||||
case reflect.Struct:
|
||||
if t.String() == "intern.Value" {
|
||||
// Otherwise its interface{} makes this return true.
|
||||
return false
|
||||
}
|
||||
for i, numField := 0, t.NumField(); i < numField; i++ {
|
||||
if visitType(t.Field(i).Type) {
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user