]> Cypherpunks repositories - gostls13.git/commitdiff
runtime, cmd/go: enable address sanitizer on linux/loong64
authorXiaolin Zhao <zhaoxiaolin@loongson.cn>
Fri, 31 Mar 2023 09:08:44 +0000 (17:08 +0800)
committerGopher Robot <gobot@golang.org>
Tue, 29 Aug 2023 00:22:11 +0000 (00:22 +0000)
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 <iant@google.com>
Reviewed-by: abner chenc <chenguoqi@loongson.cn>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Meidan Li <limeidan@loongson.cn>
TryBot-Result: Gopher Robot <gobot@golang.org>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/cmd/cgo/internal/testsanitizers/cc_test.go
src/cmd/go/alldocs.go
src/cmd/go/internal/work/build.go
src/internal/platform/supported.go
src/runtime/asan/asan.go
src/runtime/asan_loong64.s [new file with mode: 0644]

index 6eb5a64f9b237a1d167677b29c76869b5f77c902..e212a4fd984e8b49ac0da036b38ce8033e02289b 100644 (file)
@@ -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
index 17c46913f3c602d5f382e27f27c6d8f0521915d5..2c8f59e6820e8566ccc73ae535f3ebd163a189aa 100644 (file)
 //             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
index 05e300581c78a6e7441c7e532e526050f18aecb6..8cb53b95d98a423735abae416541f20cc5337c5b 100644 (file)
@@ -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
index b70a3d98ca6aad6d34cde5f025c8ab7406bc628b..b1e550ef32d6ccde08adb2583c494e48cb7256db 100644 (file)
@@ -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
        }
index 25f15ae45b6c7b45703e6bab38c5053702cdd875..ef70b0145b7f88c3446136a56877f7bffab17724 100644 (file)
@@ -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 (file)
index 0000000..70386fc
--- /dev/null
@@ -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