summaryrefslogtreecommitdiff
path: root/utils/race_checker.go
blob: cd5ed556f77f1df151e3652cae947cac38b2237e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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]))
	}
}