From 1e5987635cc8bf99e8a20d240da80bd6f0f793f7 Mon Sep 17 00:00:00 2001 From: fanzha02 Date: Thu, 29 Apr 2021 17:02:53 +0800 Subject: 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 Reviewed-by: Keith Randall Run-TryBot: Fannie Zhang Reviewed-by: Ian Lance Taylor --- src/cmd/compile/internal/noder/noder.go | 2 +- src/cmd/compile/internal/noder/object.go | 2 +- src/cmd/compile/internal/noder/reader.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/cmd/compile/internal/noder') diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index 9a42b5afd1..c4c2db5f78 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -442,7 +442,7 @@ func parseGoEmbed(args string) ([]string, error) { // the name, normally "pkg.init", is altered to "pkg.init.0". var renameinitgen int -func renameinit() *types.Sym { +func Renameinit() *types.Sym { s := typecheck.LookupNum("init.", renameinitgen) renameinitgen++ return s diff --git a/src/cmd/compile/internal/noder/object.go b/src/cmd/compile/internal/noder/object.go index e8dbaac161..ee9e0e2680 100644 --- a/src/cmd/compile/internal/noder/object.go +++ b/src/cmd/compile/internal/noder/object.go @@ -104,7 +104,7 @@ func (g *irgen) obj(obj types2.Object) *ir.Name { var typ *types.Type if recv := sig.Recv(); recv == nil { if obj.Name() == "init" { - sym = renameinit() + sym = Renameinit() } else { sym = g.sym(obj) } diff --git a/src/cmd/compile/internal/noder/reader.go b/src/cmd/compile/internal/noder/reader.go index 1350c22467..10861717f3 100644 --- a/src/cmd/compile/internal/noder/reader.go +++ b/src/cmd/compile/internal/noder/reader.go @@ -643,7 +643,7 @@ func (pr *pkgReader) objIdx(idx int, implicits, explicits []*types.Type) ir.Node case pkgbits.ObjFunc: if sym.Name == "init" { - sym = renameinit() + sym = Renameinit() } name := do(ir.ONAME, true) setType(name, r.signature(sym.Pkg, nil)) -- cgit v1.3