From 7f7c9eb25d1a810e0f3c4b3497067cc77b73f65f Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Sun, 10 Dec 2023 12:34:48 -0300 Subject: [PATCH 1/3] 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 uncoditionally, making the functions a no-op if the build flag "asan" is disabled. For [reserved] --- src/internal/asan/asan.go | 22 ++++++++++++++++++++++ src/internal/asan/doc.go | 11 +++++++++++ src/internal/asan/noasan.go | 19 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/internal/asan/asan.go create mode 100644 src/internal/asan/doc.go create mode 100644 src/internal/asan/noasan.go diff --git a/src/internal/asan/asan.go b/src/internal/asan/asan.go new file mode 100644 index 00000000000000..b230ef5e695de3 --- /dev/null +++ b/src/internal/asan/asan.go @@ -0,0 +1,22 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build asan + +package asan + +import ( + "runtime" + "unsafe" +) + +const Enabled = true + +func Read(addr unsafe.Pointer, len int) { + runtime.ASanRead(addr, len) +} + +func Write(addr unsafe.Pointer, len int) { + runtime.ASanWrite(addr, len) +} diff --git a/src/internal/asan/doc.go b/src/internal/asan/doc.go new file mode 100644 index 00000000000000..0e9c6726cd51ef --- /dev/null +++ b/src/internal/asan/doc.go @@ -0,0 +1,11 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +/* +Package asan contains helper functions for manually instrumenting code for the address sanitizer. + +The runtime package intentionally exports these functions only in the asan build; +this package exports them unconditionally but without the "asan" build tag they are no-ops. +*/ +package asan diff --git a/src/internal/asan/noasan.go b/src/internal/asan/noasan.go new file mode 100644 index 00000000000000..75472785d8002a --- /dev/null +++ b/src/internal/asan/noasan.go @@ -0,0 +1,19 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build !asan + +package asan + +import ( + "unsafe" +) + +const Enabled = false + +func Read(addr unsafe.Pointer, len int) { +} + +func Write(addr unsafe.Pointer, len int) { +} From 607eae303f5c0afc7192826e1d98e4ea7d877209 Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Mon, 29 Jan 2024 18:28:49 -0300 Subject: [PATCH 2/3] fix docs, use linkname --- src/internal/asan/asan.go | 13 +++++-------- src/internal/asan/doc.go | 13 ++++++------- src/internal/asan/noasan.go | 2 +- src/runtime/asan.go | 2 ++ 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/internal/asan/asan.go b/src/internal/asan/asan.go index b230ef5e695de3..0a8148e5b90ed8 100644 --- a/src/internal/asan/asan.go +++ b/src/internal/asan/asan.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Go Authors. All rights reserved. +// Copyright 2024 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -7,16 +7,13 @@ package asan import ( - "runtime" "unsafe" ) const Enabled = true -func Read(addr unsafe.Pointer, len int) { - runtime.ASanRead(addr, len) -} +//go:linkname Read runtime.asanread +func Read(addr unsafe.Pointer, len int) -func Write(addr unsafe.Pointer, len int) { - runtime.ASanWrite(addr, len) -} +//go:linkname Write runtime.asanwrite +func Write(addr unsafe.Pointer, len int) diff --git a/src/internal/asan/doc.go b/src/internal/asan/doc.go index 0e9c6726cd51ef..21b1bc945b31e7 100644 --- a/src/internal/asan/doc.go +++ b/src/internal/asan/doc.go @@ -1,11 +1,10 @@ -// Copyright 2023 The Go Authors. All rights reserved. +// Copyright 2024 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -/* -Package asan contains helper functions for manually instrumenting code for the address sanitizer. - -The runtime package intentionally exports these functions only in the asan build; -this package exports them unconditionally but without the "asan" build tag they are no-ops. -*/ +// Package asan contains helper functions for manually instrumenting +// code for the address sanitizer. +// The runtime package intentionally exports these functions only in the +// asan build; this package exports them unconditionally but without the +// "asan" build tag they are no-ops. package asan diff --git a/src/internal/asan/noasan.go b/src/internal/asan/noasan.go index 75472785d8002a..e01b46a1044d5e 100644 --- a/src/internal/asan/noasan.go +++ b/src/internal/asan/noasan.go @@ -1,4 +1,4 @@ -// Copyright 2023 The Go Authors. All rights reserved. +// Copyright 2024 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. diff --git a/src/runtime/asan.go b/src/runtime/asan.go index 25b83277e6d7a1..d79637a334db51 100644 --- a/src/runtime/asan.go +++ b/src/runtime/asan.go @@ -29,6 +29,7 @@ const asanenabled = true // asan{read,write} are nosplit because they may be called between // fork and exec, when the stack must not grow. See issue #50391. +//go:linkname asanread //go:nosplit func asanread(addr unsafe.Pointer, sz uintptr) { sp := getcallersp() @@ -36,6 +37,7 @@ func asanread(addr unsafe.Pointer, sz uintptr) { doasanread(addr, sz, sp, pc) } +//go:linkname asanwrite //go:nosplit func asanwrite(addr unsafe.Pointer, sz uintptr) { sp := getcallersp() From e658670c146adb5a5496afe4a2425dd5291fd7ac Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Wed, 7 Feb 2024 19:44:30 -0300 Subject: [PATCH 3/3] fix TestDependencies --- src/go/build/deps_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/go/build/deps_test.go b/src/go/build/deps_test.go index 47a0f3a0b409d4..34b0522812d898 100644 --- a/src/go/build/deps_test.go +++ b/src/go/build/deps_test.go @@ -75,6 +75,7 @@ var depsRules = ` < runtime < sync/atomic < internal/race + < internal/asan < sync < internal/bisect < internal/godebug