2020-06-30 02:36:45 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-05-11 19:09:25 +00:00
|
|
|
// Package deephash hashes a Go value recursively, in a predictable
|
|
|
|
// order, without looping.
|
|
|
|
package deephash
|
2020-06-28 17:58:21 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-10 20:29:56 +00:00
|
|
|
"bufio"
|
2020-06-28 17:58:21 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
2021-05-11 20:17:12 +00:00
|
|
|
"hash"
|
2020-06-28 17:58:21 +00:00
|
|
|
"reflect"
|
2021-05-11 20:17:12 +00:00
|
|
|
"sync"
|
2021-05-10 21:15:31 +00:00
|
|
|
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/wgkey"
|
2020-06-28 17:58:21 +00:00
|
|
|
)
|
|
|
|
|
2020-07-29 01:47:23 +00:00
|
|
|
func Hash(v ...interface{}) string {
|
2020-06-28 17:58:21 +00:00
|
|
|
h := sha256.New()
|
2021-05-10 20:29:56 +00:00
|
|
|
// 64 matches the chunk size in crypto/sha256/sha256.go
|
|
|
|
b := bufio.NewWriterSize(h, 64)
|
|
|
|
Print(b, v)
|
|
|
|
b.Flush()
|
2020-06-28 17:58:21 +00:00
|
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2020-07-29 01:47:23 +00:00
|
|
|
// UpdateHash sets last to the hash of v and reports whether its value changed.
|
|
|
|
func UpdateHash(last *string, v ...interface{}) (changed bool) {
|
|
|
|
sig := Hash(v)
|
|
|
|
if *last != sig {
|
|
|
|
*last = sig
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-10 20:29:56 +00:00
|
|
|
func Print(w *bufio.Writer, v ...interface{}) {
|
2020-06-28 17:58:21 +00:00
|
|
|
print(w, reflect.ValueOf(v), make(map[uintptr]bool))
|
|
|
|
}
|
|
|
|
|
2021-05-10 21:15:31 +00:00
|
|
|
var (
|
|
|
|
netaddrIPType = reflect.TypeOf(netaddr.IP{})
|
|
|
|
netaddrIPPrefix = reflect.TypeOf(netaddr.IPPrefix{})
|
|
|
|
wgkeyKeyType = reflect.TypeOf(wgkey.Key{})
|
|
|
|
wgkeyPrivateType = reflect.TypeOf(wgkey.Private{})
|
|
|
|
tailcfgDiscoKeyType = reflect.TypeOf(tailcfg.DiscoKey{})
|
|
|
|
)
|
|
|
|
|
2021-05-11 20:17:12 +00:00
|
|
|
// print hashes v into w.
|
|
|
|
// It reports whether it was able to do so without hitting a cycle.
|
|
|
|
func print(w *bufio.Writer, v reflect.Value, visited map[uintptr]bool) (acyclic bool) {
|
2020-06-28 17:58:21 +00:00
|
|
|
if !v.IsValid() {
|
2021-05-11 20:17:12 +00:00
|
|
|
return true
|
2020-06-28 17:58:21 +00:00
|
|
|
}
|
2021-05-10 21:15:31 +00:00
|
|
|
|
|
|
|
// Special case some common types.
|
|
|
|
if v.CanInterface() {
|
|
|
|
switch v.Type() {
|
|
|
|
case netaddrIPType:
|
|
|
|
var b []byte
|
|
|
|
var err error
|
|
|
|
if v.CanAddr() {
|
|
|
|
x := v.Addr().Interface().(*netaddr.IP)
|
|
|
|
b, err = x.MarshalText()
|
|
|
|
} else {
|
|
|
|
x := v.Interface().(netaddr.IP)
|
|
|
|
b, err = x.MarshalText()
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
w.Write(b)
|
2021-05-11 20:17:12 +00:00
|
|
|
return true
|
2021-05-10 21:15:31 +00:00
|
|
|
}
|
|
|
|
case netaddrIPPrefix:
|
|
|
|
var b []byte
|
|
|
|
var err error
|
|
|
|
if v.CanAddr() {
|
|
|
|
x := v.Addr().Interface().(*netaddr.IPPrefix)
|
|
|
|
b, err = x.MarshalText()
|
|
|
|
} else {
|
|
|
|
x := v.Interface().(netaddr.IPPrefix)
|
|
|
|
b, err = x.MarshalText()
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
w.Write(b)
|
2021-05-11 20:17:12 +00:00
|
|
|
return true
|
2021-05-10 21:15:31 +00:00
|
|
|
}
|
|
|
|
case wgkeyKeyType:
|
|
|
|
if v.CanAddr() {
|
|
|
|
x := v.Addr().Interface().(*wgkey.Key)
|
|
|
|
w.Write(x[:])
|
|
|
|
} else {
|
|
|
|
x := v.Interface().(wgkey.Key)
|
|
|
|
w.Write(x[:])
|
|
|
|
}
|
2021-05-11 20:17:12 +00:00
|
|
|
return true
|
2021-05-10 21:15:31 +00:00
|
|
|
case wgkeyPrivateType:
|
|
|
|
if v.CanAddr() {
|
|
|
|
x := v.Addr().Interface().(*wgkey.Private)
|
|
|
|
w.Write(x[:])
|
|
|
|
} else {
|
|
|
|
x := v.Interface().(wgkey.Private)
|
|
|
|
w.Write(x[:])
|
|
|
|
}
|
2021-05-11 20:17:12 +00:00
|
|
|
return true
|
2021-05-10 21:15:31 +00:00
|
|
|
case tailcfgDiscoKeyType:
|
|
|
|
if v.CanAddr() {
|
|
|
|
x := v.Addr().Interface().(*tailcfg.DiscoKey)
|
|
|
|
w.Write(x[:])
|
|
|
|
} else {
|
|
|
|
x := v.Interface().(tailcfg.DiscoKey)
|
|
|
|
w.Write(x[:])
|
|
|
|
}
|
2021-05-18 15:52:10 +00:00
|
|
|
return true
|
2021-05-10 21:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generic handling.
|
2020-06-28 17:58:21 +00:00
|
|
|
switch v.Kind() {
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unhandled kind %v for type %v", v.Kind(), v.Type()))
|
|
|
|
case reflect.Ptr:
|
|
|
|
ptr := v.Pointer()
|
|
|
|
if visited[ptr] {
|
2021-05-11 20:17:12 +00:00
|
|
|
return false
|
2020-06-28 17:58:21 +00:00
|
|
|
}
|
|
|
|
visited[ptr] = true
|
2021-05-11 20:17:12 +00:00
|
|
|
return print(w, v.Elem(), visited)
|
2020-06-28 17:58:21 +00:00
|
|
|
case reflect.Struct:
|
2021-05-11 20:17:12 +00:00
|
|
|
acyclic = true
|
2021-05-10 21:35:45 +00:00
|
|
|
w.WriteString("struct{\n")
|
2020-06-28 17:58:21 +00:00
|
|
|
for i, n := 0, v.NumField(); i < n; i++ {
|
2021-05-11 00:51:25 +00:00
|
|
|
fmt.Fprintf(w, " [%d]: ", i)
|
2021-05-11 20:17:12 +00:00
|
|
|
if !print(w, v.Field(i), visited) {
|
|
|
|
acyclic = false
|
|
|
|
}
|
2021-05-10 21:35:45 +00:00
|
|
|
w.WriteString("\n")
|
2020-06-28 17:58:21 +00:00
|
|
|
}
|
2021-05-11 00:52:29 +00:00
|
|
|
w.WriteString("}\n")
|
2021-05-11 20:17:12 +00:00
|
|
|
return acyclic
|
2020-06-28 17:58:21 +00:00
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
if v.Type().Elem().Kind() == reflect.Uint8 && v.CanInterface() {
|
|
|
|
fmt.Fprintf(w, "%q", v.Interface())
|
2021-05-11 20:17:12 +00:00
|
|
|
return true
|
2020-06-28 17:58:21 +00:00
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "[%d]{\n", v.Len())
|
2021-05-11 20:17:12 +00:00
|
|
|
acyclic = true
|
2020-06-28 17:58:21 +00:00
|
|
|
for i, ln := 0, v.Len(); i < ln; i++ {
|
|
|
|
fmt.Fprintf(w, " [%d]: ", i)
|
2021-05-11 20:17:12 +00:00
|
|
|
if !print(w, v.Index(i), visited) {
|
|
|
|
acyclic = false
|
|
|
|
}
|
2021-05-10 21:35:45 +00:00
|
|
|
w.WriteString("\n")
|
2020-06-28 17:58:21 +00:00
|
|
|
}
|
2021-05-10 21:35:45 +00:00
|
|
|
w.WriteString("}\n")
|
2021-05-11 20:17:12 +00:00
|
|
|
return acyclic
|
2020-06-28 17:58:21 +00:00
|
|
|
case reflect.Interface:
|
2021-05-11 20:17:12 +00:00
|
|
|
return print(w, v.Elem(), visited)
|
2020-06-28 17:58:21 +00:00
|
|
|
case reflect.Map:
|
2021-05-11 20:17:12 +00:00
|
|
|
if hashMapAcyclic(w, v, visited) {
|
|
|
|
return true
|
2020-06-28 17:58:21 +00:00
|
|
|
}
|
2021-05-11 20:17:12 +00:00
|
|
|
return hashMapFallback(w, v, visited)
|
2020-06-28 17:58:21 +00:00
|
|
|
case reflect.String:
|
2021-05-10 20:29:56 +00:00
|
|
|
w.WriteString(v.String())
|
2020-06-28 17:58:21 +00:00
|
|
|
case reflect.Bool:
|
|
|
|
fmt.Fprintf(w, "%v", v.Bool())
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
fmt.Fprintf(w, "%v", v.Int())
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
fmt.Fprintf(w, "%v", v.Uint())
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
fmt.Fprintf(w, "%v", v.Float())
|
|
|
|
case reflect.Complex64, reflect.Complex128:
|
|
|
|
fmt.Fprintf(w, "%v", v.Complex())
|
|
|
|
}
|
2021-05-11 20:17:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type mapHasher struct {
|
|
|
|
xbuf [sha256.Size]byte // XOR'ed accumulated buffer
|
|
|
|
ebuf [sha256.Size]byte // scratch buffer
|
|
|
|
s256 hash.Hash // sha256 hash.Hash
|
|
|
|
bw *bufio.Writer // to hasher into ebuf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *mapHasher) Reset() {
|
|
|
|
for i := range mh.xbuf {
|
|
|
|
mh.xbuf[i] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *mapHasher) startEntry() {
|
|
|
|
for i := range mh.ebuf {
|
|
|
|
mh.ebuf[i] = 0
|
|
|
|
}
|
|
|
|
mh.bw.Flush()
|
|
|
|
mh.s256.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mh *mapHasher) endEntry() {
|
|
|
|
mh.bw.Flush()
|
|
|
|
for i, b := range mh.s256.Sum(mh.ebuf[:0]) {
|
|
|
|
mh.xbuf[i] ^= b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var mapHasherPool = &sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
mh := new(mapHasher)
|
|
|
|
mh.s256 = sha256.New()
|
|
|
|
mh.bw = bufio.NewWriter(mh.s256)
|
|
|
|
return mh
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// hashMapAcyclic is the faster sort-free version of map hashing. If
|
|
|
|
// it detects a cycle it returns false and guarantees that nothing was
|
|
|
|
// written to w.
|
|
|
|
func hashMapAcyclic(w *bufio.Writer, v reflect.Value, visited map[uintptr]bool) (acyclic bool) {
|
|
|
|
mh := mapHasherPool.Get().(*mapHasher)
|
|
|
|
defer mapHasherPool.Put(mh)
|
|
|
|
mh.Reset()
|
|
|
|
iter := v.MapRange()
|
|
|
|
for iter.Next() {
|
|
|
|
mh.startEntry()
|
|
|
|
if !print(mh.bw, iter.Key(), visited) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !print(mh.bw, iter.Value(), visited) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
mh.endEntry()
|
|
|
|
}
|
|
|
|
w.Write(mh.xbuf[:])
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashMapFallback(w *bufio.Writer, v reflect.Value, visited map[uintptr]bool) (acyclic bool) {
|
|
|
|
acyclic = true
|
|
|
|
sm := newSortedMap(v)
|
|
|
|
fmt.Fprintf(w, "map[%d]{\n", len(sm.Key))
|
|
|
|
for i, k := range sm.Key {
|
|
|
|
if !print(w, k, visited) {
|
|
|
|
acyclic = false
|
|
|
|
}
|
|
|
|
w.WriteString(": ")
|
|
|
|
if !print(w, sm.Value[i], visited) {
|
|
|
|
acyclic = false
|
|
|
|
}
|
|
|
|
w.WriteString("\n")
|
|
|
|
}
|
|
|
|
w.WriteString("}\n")
|
|
|
|
return acyclic
|
2020-06-28 17:58:21 +00:00
|
|
|
}
|