Skip to content

Commit db99b9b

Browse files
mauri870cherrymui
authored andcommitted
internal/asan: add new package
The internal/asan package contains helper functions for manually instrumenting code for the address sanitizer. It reexports the asan routines in runtime unconditionally, making the functions a no-op if the build flag "asan" is not present. For #64611 Change-Id: Ie79e698aea7a6d969afd2a5f008c084c9545b1a5 GitHub-Last-Rev: e658670 GitHub-Pull-Request: #64635 Reviewed-on: https://go-review.googlesource.com/c/go/+/548695 Reviewed-by: Austin Clements <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 1b54150 commit db99b9b

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

src/go/build/deps_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ var depsRules = `
7575
< runtime
7676
< sync/atomic
7777
< internal/race
78+
< internal/asan
7879
< sync
7980
< internal/bisect
8081
< internal/godebug

src/internal/asan/asan.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2024 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 asan
6+
7+
package asan
8+
9+
import (
10+
"unsafe"
11+
)
12+
13+
const Enabled = true
14+
15+
//go:linkname Read runtime.asanread
16+
func Read(addr unsafe.Pointer, len int)
17+
18+
//go:linkname Write runtime.asanwrite
19+
func Write(addr unsafe.Pointer, len int)

src/internal/asan/doc.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2024 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+
// Package asan contains helper functions for manually instrumenting
6+
// code for the address sanitizer.
7+
// The runtime package intentionally exports these functions only in the
8+
// asan build; this package exports them unconditionally but without the
9+
// "asan" build tag they are no-ops.
10+
package asan

src/internal/asan/noasan.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2024 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 !asan
6+
7+
package asan
8+
9+
import (
10+
"unsafe"
11+
)
12+
13+
const Enabled = false
14+
15+
func Read(addr unsafe.Pointer, len int) {
16+
}
17+
18+
func Write(addr unsafe.Pointer, len int) {
19+
}

src/runtime/asan.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ const asanenabled = true
2929
// asan{read,write} are nosplit because they may be called between
3030
// fork and exec, when the stack must not grow. See issue #50391.
3131

32+
//go:linkname asanread
3233
//go:nosplit
3334
func asanread(addr unsafe.Pointer, sz uintptr) {
3435
sp := getcallersp()
3536
pc := getcallerpc()
3637
doasanread(addr, sz, sp, pc)
3738
}
3839

40+
//go:linkname asanwrite
3941
//go:nosplit
4042
func asanwrite(addr unsafe.Pointer, sz uintptr) {
4143
sp := getcallersp()

0 commit comments

Comments
 (0)