From: Xiaolin Zhao Date: Fri, 31 Mar 2023 09:08:44 +0000 (+0800) Subject: runtime, cmd/go: enable address sanitizer on linux/loong64 X-Git-Tag: go1.22rc1~1061 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e9c14a77803a3e80d6697c70bb79d8accd0d2290;p=gostls13.git runtime, cmd/go: enable address sanitizer on linux/loong64 The recently released llvm-16/LoongArch already supports the asan feature; but gcc13/LoongArch does not yet fully support the asan feature, and asan will be fully supported in gcc14. Change-Id: I48a65f2f10e3dc488acd9c02ea1a1f37de192be0 Reviewed-on: https://go-review.googlesource.com/c/go/+/481317 Reviewed-by: Ian Lance Taylor Reviewed-by: abner chenc Reviewed-by: Bryan Mills Run-TryBot: Bryan Mills Reviewed-by: Meidan Li TryBot-Result: Gopher Robot Commit-Queue: Ian Lance Taylor Auto-Submit: Ian Lance Taylor --- diff --git a/src/cmd/cgo/internal/testsanitizers/cc_test.go b/src/cmd/cgo/internal/testsanitizers/cc_test.go index 6eb5a64f9b..e212a4fd98 100644 --- a/src/cmd/cgo/internal/testsanitizers/cc_test.go +++ b/src/cmd/cgo/internal/testsanitizers/cc_test.go @@ -293,11 +293,17 @@ func compilerRequiredAsanVersion(goos, goarch string) bool { } switch compiler.name { case "gcc": + if goarch == "loong64" { + return compiler.major >= 14 + } if goarch == "ppc64le" { return compiler.major >= 9 } return compiler.major >= 7 case "clang": + if goarch == "loong64" { + return compiler.major >= 16 + } return compiler.major >= 9 default: return false diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 17c46913f3..2c8f59e682 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -122,9 +122,10 @@ // PIE build mode will be used on all platforms except linux/amd64. // -asan // enable interoperation with address sanitizer. -// Supported only on linux/arm64, linux/amd64. -// Supported only on linux/amd64 or linux/arm64 and only with GCC 7 and higher +// Supported only on linux/arm64, linux/amd64, linux/loong64. +// Supported on linux/amd64 or linux/arm64 and only with GCC 7 and higher // or Clang/LLVM 9 and higher. +// And supported on linux/loong64 only with Clang/LLVM 16 and higher. // -cover // enable code coverage instrumentation. // -covermode set,count,atomic diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 05e300581c..8cb53b95d9 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -81,9 +81,10 @@ and test commands: PIE build mode will be used on all platforms except linux/amd64. -asan enable interoperation with address sanitizer. - Supported only on linux/arm64, linux/amd64. - Supported only on linux/amd64 or linux/arm64 and only with GCC 7 and higher + Supported only on linux/arm64, linux/amd64, linux/loong64. + Supported on linux/amd64 or linux/arm64 and only with GCC 7 and higher or Clang/LLVM 9 and higher. + And supported on linux/loong64 only with Clang/LLVM 16 and higher. -cover enable code coverage instrumentation. -covermode set,count,atomic diff --git a/src/internal/platform/supported.go b/src/internal/platform/supported.go index b70a3d98ca..b1e550ef32 100644 --- a/src/internal/platform/supported.go +++ b/src/internal/platform/supported.go @@ -51,7 +51,7 @@ func MSanSupported(goos, goarch string) bool { func ASanSupported(goos, goarch string) bool { switch goos { case "linux": - return goarch == "arm64" || goarch == "amd64" || goarch == "riscv64" || goarch == "ppc64le" + return goarch == "arm64" || goarch == "amd64" || goarch == "loong64" || goarch == "riscv64" || goarch == "ppc64le" default: return false } diff --git a/src/runtime/asan/asan.go b/src/runtime/asan/asan.go index 25f15ae45b..ef70b0145b 100644 --- a/src/runtime/asan/asan.go +++ b/src/runtime/asan/asan.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build asan && linux && (arm64 || amd64 || riscv64 || ppc64le) +//go:build asan && linux && (arm64 || amd64 || loong64 || riscv64 || ppc64le) package asan diff --git a/src/runtime/asan_loong64.s b/src/runtime/asan_loong64.s new file mode 100644 index 0000000000..70386fcb6e --- /dev/null +++ b/src/runtime/asan_loong64.s @@ -0,0 +1,75 @@ +// 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 + +#include "go_asm.h" +#include "textflag.h" + +#define RARG0 R4 +#define RARG1 R5 +#define RARG2 R6 +#define RARG3 R7 +#define FARG R8 + +// Called from instrumented code. +// func runtime·doasanread(addr unsafe.Pointer, sz, sp, pc uintptr) +TEXT runtime·doasanread(SB), NOSPLIT, $0-32 + MOVV addr+0(FP), RARG0 + MOVV size+8(FP), RARG1 + MOVV sp+16(FP), RARG2 + MOVV pc+24(FP), RARG3 + // void __asan_read_go(void *addr, uintptr_t sz, void *sp, void *pc); + MOVV $__asan_read_go(SB), FARG + JMP asancall<>(SB) + +// func runtime·doasanwrite(addr unsafe.Pointer, sz, sp, pc uintptr) +TEXT runtime·doasanwrite(SB), NOSPLIT, $0-32 + MOVV addr+0(FP), RARG0 + MOVV size+8(FP), RARG1 + MOVV sp+16(FP), RARG2 + MOVV pc+24(FP), RARG3 + // void __asan_write_go(void *addr, uintptr_t sz, void *sp, void *pc); + MOVV $__asan_write_go(SB), FARG + JMP asancall<>(SB) + +// func runtime·asanunpoison(addr unsafe.Pointer, sz uintptr) +TEXT runtime·asanunpoison(SB), NOSPLIT, $0-16 + MOVV addr+0(FP), RARG0 + MOVV size+8(FP), RARG1 + // void __asan_unpoison_go(void *addr, uintptr_t sz); + MOVV $__asan_unpoison_go(SB), FARG + JMP asancall<>(SB) + +// func runtime·asanpoison(addr unsafe.Pointer, sz uintptr) +TEXT runtime·asanpoison(SB), NOSPLIT, $0-16 + MOVV addr+0(FP), RARG0 + MOVV size+8(FP), RARG1 + // void __asan_poison_go(void *addr, uintptr_t sz); + MOVV $__asan_poison_go(SB), FARG + JMP asancall<>(SB) + +// func runtime·asanregisterglobals(addr unsafe.Pointer, n uintptr) +TEXT runtime·asanregisterglobals(SB), NOSPLIT, $0-16 + MOVV addr+0(FP), RARG0 + MOVV size+8(FP), RARG1 + // void __asan_register_globals_go(void *addr, uintptr_t n); + MOVV $__asan_register_globals_go(SB), FARG + JMP asancall<>(SB) + +// Switches SP to g0 stack and calls (FARG). Arguments already set. +TEXT asancall<>(SB), NOSPLIT, $0-0 + MOVV R3, R23 // callee-saved + BEQ g, g0stack // no g, still on a system stack + MOVV g_m(g), R14 + MOVV m_g0(R14), R15 + BEQ R15, g, g0stack + + MOVV (g_sched+gobuf_sp)(R15), R9 + MOVV R9, R3 + +g0stack: + JAL (FARG) + MOVV R23, R3 + RET