Skip to content

Commit c32930f

Browse files
committed
internal/msan: add new package
The internal/msan package contains helper functions for manually instrumenting code for the memory sanitizer. It exports the private msan routines in runtime uncoditionally, making the functions a no-op if the build flag "msan" is not present. For #64611
1 parent 46ea4ab commit c32930f

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/internal/msan/doc.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
/*
6+
Package msan contains helper functions for manually instrumenting code
7+
for the memory sanitizer.
8+
9+
This package exports the private msan routines in runtime unconditionally
10+
but without the "msan" build tag they are no-ops.
11+
*/
12+
package msan

src/internal/msan/msan.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build msan
6+
7+
package msan
8+
9+
import (
10+
"unsafe"
11+
)
12+
13+
const Enabled = true
14+
15+
func Read(addr unsafe.Pointer, sz uintptr) {
16+
read(addr, sz)
17+
}
18+
19+
func Write(addr unsafe.Pointer, sz uintptr) {
20+
write(addr, sz)
21+
}
22+
23+
func Malloc(addr unsafe.Pointer, sz uintptr) {
24+
malloc(addr, sz)
25+
}
26+
27+
func Free(addr unsafe.Pointer, sz uintptr) {
28+
free(addr, sz)
29+
}
30+
31+
func Move(dst, src unsafe.Pointer, sz uintptr) {
32+
move(dst, src, sz)
33+
}
34+
35+
// Import private msan functions from runtime.
36+
//
37+
//go:linkname read runtime.msanread
38+
func read(addr unsafe.Pointer, sz uintptr)
39+
40+
//go:linkname write runtime.msanwrite
41+
func write(addr unsafe.Pointer, sz uintptr)
42+
43+
//go:linkname malloc runtime.msanmalloc
44+
func malloc(addr unsafe.Pointer, sz uintptr)
45+
46+
//go:linkname free runtime.msanfree
47+
func free(addr unsafe.Pointer, sz uintptr)
48+
49+
//go:linkname move runtime.msanmove
50+
func move(dst, src unsafe.Pointer, sz uintptr)

src/internal/msan/nomsan.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !msan
6+
7+
package msan
8+
9+
import (
10+
"unsafe"
11+
)
12+
13+
const Enabled = false
14+
15+
func Read(addr unsafe.Pointer, sz uintptr) {
16+
}
17+
18+
func Write(addr unsafe.Pointer, sz uintptr) {
19+
}
20+
21+
func Malloc(addr unsafe.Pointer, sz uintptr) {
22+
}
23+
24+
func Free(addr unsafe.Pointer, sz uintptr) {
25+
}
26+
27+
func Move(dst, src unsafe.Pointer, sz uintptr) {
28+
}

0 commit comments

Comments
 (0)