Skip to content

Commit e8d417d

Browse files
zorrorffmianlancetaylor
authored andcommitted
runtime: enable memory sanitizer on arm64
Changes include: 1. open compilation option -msan for arm64 2. modify doc to explain -msan is also supported on linux/arm64 3. wrap msan lib API in msan_arm64.s 4. use libc for sigaction syscalls when cgo is enabled 5. use libc for mmap syscalls when cgo is enabled Change-Id: I26ebe61ff7ce1906125f54a0182a720f9d58ec11 Reviewed-on: https://go-review.googlesource.com/109255 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent eff1e68 commit e8d417d

File tree

17 files changed

+146
-22
lines changed

17 files changed

+146
-22
lines changed

src/cmd/dist/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ func (t *tester) registerTests() {
676676
if gohostos == "linux" && goarch == "amd64" {
677677
t.registerTest("testasan", "../misc/cgo/testasan", "go", "run", "main.go")
678678
}
679-
if goos == "linux" && goarch == "amd64" {
679+
if goos == "linux" && (goarch == "amd64" || goarch == "arm64") {
680680
t.registerHostTest("testsanitizers/msan", "../misc/cgo/testsanitizers", "misc/cgo/testsanitizers", ".")
681681
}
682682
if t.hasBash() && goos != "android" && !t.iOS() && gohostos != "windows" {

src/cmd/go/alldocs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
// Supported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64.
106106
// -msan
107107
// enable interoperation with memory sanitizer.
108-
// Supported only on linux/amd64,
108+
// Supported only on linux/amd64, linux/arm64
109109
// and only with Clang/LLVM as the host C compiler.
110110
// -v
111111
// print the names of packages as they are compiled.

src/cmd/go/internal/work/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ and test commands:
6565
Supported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64.
6666
-msan
6767
enable interoperation with memory sanitizer.
68-
Supported only on linux/amd64,
68+
Supported only on linux/amd64, linux/arm64
6969
and only with Clang/LLVM as the host C compiler.
7070
-v
7171
print the names of packages as they are compiled.

src/cmd/go/internal/work/init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ func instrumentInit() {
3939
fmt.Fprintf(os.Stderr, "go %s: may not use -race and -msan simultaneously\n", flag.Args()[0])
4040
os.Exit(2)
4141
}
42-
if cfg.BuildMSan && (cfg.Goos != "linux" || cfg.Goarch != "amd64") {
42+
if cfg.BuildMSan && (cfg.Goos != "linux" || cfg.Goarch != "amd64" && cfg.Goarch != "arm64") {
4343
fmt.Fprintf(os.Stderr, "-msan is not supported on %s/%s\n", cfg.Goos, cfg.Goarch)
4444
os.Exit(2)
4545
}
46-
if cfg.Goarch != "amd64" || cfg.Goos != "linux" && cfg.Goos != "freebsd" && cfg.Goos != "darwin" && cfg.Goos != "windows" {
47-
fmt.Fprintf(os.Stderr, "go %s: -race and -msan are only supported on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64\n", flag.Args()[0])
46+
if cfg.BuildRace && (cfg.Goarch != "amd64" || cfg.Goos != "linux" && cfg.Goos != "freebsd" && cfg.Goos != "darwin" && cfg.Goos != "windows") {
47+
fmt.Fprintf(os.Stderr, "go %s: -race is only supported on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64\n", flag.Args()[0])
4848
os.Exit(2)
4949
}
5050

src/runtime/cgo/gcc_linux_arm64.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// license that can be found in the LICENSE file.
44

55
#include <pthread.h>
6+
#include <errno.h>
67
#include <string.h>
78
#include <signal.h>
9+
#include <stdlib.h>
810
#include "libcgo.h"
911
#include "libcgo_unix.h"
1012

@@ -59,14 +61,34 @@ threadentry(void *v)
5961
void
6062
x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
6163
{
62-
pthread_attr_t attr;
64+
pthread_attr_t *attr;
6365
size_t size;
6466

67+
/* The memory sanitizer distributed with versions of clang
68+
before 3.8 has a bug: if you call mmap before malloc, mmap
69+
may return an address that is later overwritten by the msan
70+
library. Avoid this problem by forcing a call to malloc
71+
here, before we ever call malloc.
72+
73+
This is only required for the memory sanitizer, so it's
74+
unfortunate that we always run it. It should be possible
75+
to remove this when we no longer care about versions of
76+
clang before 3.8. The test for this is
77+
misc/cgo/testsanitizers.
78+
79+
GCC works hard to eliminate a seemingly unnecessary call to
80+
malloc, so we actually use the memory we allocate. */
81+
6582
setg_gcc = setg;
66-
pthread_attr_init(&attr);
67-
pthread_attr_getstacksize(&attr, &size);
68-
g->stacklo = (uintptr)&attr - size + 4096;
69-
pthread_attr_destroy(&attr);
83+
attr = (pthread_attr_t*)malloc(sizeof *attr);
84+
if (attr == NULL) {
85+
fatalf("malloc failed: %s", strerror(errno));
86+
}
87+
pthread_attr_init(attr);
88+
pthread_attr_getstacksize(attr, &size);
89+
g->stacklo = (uintptr)&size - size + 4096;
90+
pthread_attr_destroy(attr);
91+
free(attr);
7092

7193
if (x_cgo_inittls) {
7294
x_cgo_inittls(tlsg, tlsbase);

src/runtime/cgo/gcc_mmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build linux,amd64
5+
// +build linux,amd64 linux,arm64
66

77
#include <errno.h>
88
#include <stdint.h>

src/runtime/cgo/gcc_sigaction.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build linux,amd64
5+
// +build linux,amd64 linux,arm64
66

77
#include <errno.h>
88
#include <stddef.h>

src/runtime/cgo/mmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build linux,amd64
5+
// +build linux,amd64 linux,arm64
66

77
package cgo
88

src/runtime/cgo/sigaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build linux,amd64 freebsd,amd64
5+
// +build linux,amd64 freebsd,amd64 linux,arm64
66

77
package cgo
88

src/runtime/cgo_mmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// Support for memory sanitizer. See runtime/cgo/mmap.go.
66

7-
// +build linux,amd64
7+
// +build linux,amd64 linux,arm64
88

99
package runtime
1010

0 commit comments

Comments
 (0)