diff options
Diffstat (limited to 'utils/race_checker.go')
-rw-r--r-- | utils/race_checker.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/utils/race_checker.go b/utils/race_checker.go new file mode 100644 index 00000000..cd5ed556 --- /dev/null +++ b/utils/race_checker.go @@ -0,0 +1,35 @@ +//go:build race + +package utils + +import ( + "crypto/sha512" + "fmt" + "runtime" +) + +func SetChecker(b []byte) { + if len(b) == 0 { + return + } + c := checkIfConst(b) + go c.isStillConst() + runtime.SetFinalizer(c, (*constSlice).isStillConst) +} + +type constSlice struct { + b []byte + checksum [64]byte +} + +func checkIfConst(b []byte) *constSlice { + c := &constSlice{b: b} + c.checksum = sha512.Sum512(c.b) + return c +} + +func (c *constSlice) isStillConst() { + if sha512.Sum512(c.b) != c.checksum { + panic(fmt.Sprintf("mutable access detected 0x%012x", &c.b[0])) + } +} |