aboutsummaryrefslogtreecommitdiff
path: root/misc
diff options
context:
space:
mode:
authorfanzha02 <fannie.zhang@arm.com>2021-04-29 17:02:53 +0800
committerIan Lance Taylor <iant@google.com>2022-04-22 04:42:23 +0000
commit1e5987635cc8bf99e8a20d240da80bd6f0f793f7 (patch)
tree71b3c644dab4a84570827c63ecd01185129631ce /misc
parentbe1d7388b349e86bc2fc1b8769902875e732918f (diff)
downloadgo-1e5987635cc8bf99e8a20d240da80bd6f0f793f7.tar.xz
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. Change-Id: I664e74dcabf5dc7ed46802859174606454e8f1d3 Reviewed-on: https://go-review.googlesource.com/c/go/+/321715 Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org> Run-TryBot: Fannie Zhang <Fannie.Zhang@arm.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'misc')
-rw-r--r--misc/cgo/testsanitizers/asan_test.go8
-rw-r--r--misc/cgo/testsanitizers/cc_test.go16
2 files changed, 24 insertions, 0 deletions
diff --git a/misc/cgo/testsanitizers/asan_test.go b/misc/cgo/testsanitizers/asan_test.go
index ff578ac63e..23392c00f6 100644
--- a/misc/cgo/testsanitizers/asan_test.go
+++ b/misc/cgo/testsanitizers/asan_test.go
@@ -22,6 +22,14 @@ func TestASAN(t *testing.T) {
if !aSanSupported(goos, goarch) {
t.Skipf("skipping on %s/%s; -asan option is not supported.", goos, goarch)
}
+ // The current implementation is only compatible with the ASan library from version
+ // v7 to v9 (See the description in src/runtime/asan/asan.go). Therefore, using the
+ // -asan option must use a compatible version of ASan library, which requires that
+ // the gcc version is not less than 7 and the clang version is not less than 4,
+ // otherwise a segmentation fault will occur.
+ if !compilerRequiredAsanVersion() {
+ t.Skipf("skipping: too old version of compiler")
+ }
t.Parallel()
requireOvercommit(t)
diff --git a/misc/cgo/testsanitizers/cc_test.go b/misc/cgo/testsanitizers/cc_test.go
index 05b77932b4..ee3c3bf28c 100644
--- a/misc/cgo/testsanitizers/cc_test.go
+++ b/misc/cgo/testsanitizers/cc_test.go
@@ -235,6 +235,22 @@ func compilerSupportsLocation() bool {
}
}
+// compilerRequiredAsanVersion reports whether the compiler is the version required by Asan.
+func compilerRequiredAsanVersion() bool {
+ compiler, err := compilerVersion()
+ if err != nil {
+ return false
+ }
+ switch compiler.name {
+ case "gcc":
+ return compiler.major >= 7
+ case "clang":
+ return true
+ default:
+ return false
+ }
+}
+
type compilerCheck struct {
once sync.Once
err error