1
0
mirror of https://github.com/golang/go synced 2024-11-06 13:46:16 -07:00
go/src/runtime/asan.go
fanzha02 1b0f9fbb67 cmd/compile: enable Asan check for global variables
With this patch, -asan option can detect the error memory
access to global variables.

So this patch makes a few changes:

1. Add the asanregisterglobals runtime support function,
which calls asan runtime function _asan_register_globals
to register global variables.

2. Create a new initialization function for the package
being compiled. This function initializes an array of
instrumented global variables and pass it to function
runtime.asanregisterglobals. An instrumented global
variable has trailing redzone.

3. Writes the new size of instrumented global variables
that have trailing redzones into object file.

4. Notice that the current implementation is only compatible with
the ASan library from version v7 to v9. Therefore, using the
-asan option requires that the gcc version is not less than 7
and the clang version is less than 4, otherwise a segmentation
fault will occur. So this patch adds a check on whether the compiler
being used is a supported version in cmd/go.

(This is a redo of CL 401775 with a fix for a build break due to an
intervening commit that removed the internal/execabs package.)

Updates #44853.

Change-Id: I719d4ef2b22cb2d5516e1494cd453c3efb47d6c7
Reviewed-on: https://go-review.googlesource.com/c/go/+/403851
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-05-04 18:51:19 +00:00

68 lines
1.5 KiB
Go

// Copyright 2021 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 runtime
import (
"unsafe"
)
// Public address sanitizer API.
func ASanRead(addr unsafe.Pointer, len int) {
sp := getcallersp()
pc := getcallerpc()
doasanread(addr, uintptr(len), sp, pc)
}
func ASanWrite(addr unsafe.Pointer, len int) {
sp := getcallersp()
pc := getcallerpc()
doasanwrite(addr, uintptr(len), sp, pc)
}
// Private interface for the runtime.
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:nosplit
func asanread(addr unsafe.Pointer, sz uintptr) {
sp := getcallersp()
pc := getcallerpc()
doasanread(addr, sz, sp, pc)
}
//go:nosplit
func asanwrite(addr unsafe.Pointer, sz uintptr) {
sp := getcallersp()
pc := getcallerpc()
doasanwrite(addr, sz, sp, pc)
}
//go:noescape
func doasanread(addr unsafe.Pointer, sz, sp, pc uintptr)
//go:noescape
func doasanwrite(addr unsafe.Pointer, sz, sp, pc uintptr)
//go:noescape
func asanunpoison(addr unsafe.Pointer, sz uintptr)
//go:noescape
func asanpoison(addr unsafe.Pointer, sz uintptr)
//go:noescape
func asanregisterglobals(addr unsafe.Pointer, n uintptr)
// These are called from asan_GOARCH.s
//
//go:cgo_import_static __asan_read_go
//go:cgo_import_static __asan_write_go
//go:cgo_import_static __asan_unpoison_go
//go:cgo_import_static __asan_poison_go
//go:cgo_import_static __asan_register_globals_go