]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime, cmd/go: enable memory sanitizer on linux/loong64
authorXiaolin Zhao <zhaoxiaolin@loongson.cn>
Fri, 31 Mar 2023 07:36:59 +0000 (15:36 +0800)
committerGopher Robot <gobot@golang.org>
Fri, 25 Aug 2023 20:58:13 +0000 (20:58 +0000)
The msan feature depends on llvm. The currently released llvm-16
already supports the LoongArch architecture, and msan's support
for LoongArch64 has been added in this patch[1], and it has been
merged in branches main and release/17.x.

[1]: https://reviews.llvm.org/D140528

Change-Id: If537c5ffb1c9d4b3316b9b3794d411953bc5764b
Reviewed-on: https://go-review.googlesource.com/c/go/+/481315
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: abner chenc <chenguoqi@loongson.cn>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: WANG Xuerui <git@xen0n.name>

src/cmd/go/alldocs.go
src/cmd/go/internal/work/build.go
src/internal/platform/supported.go
src/runtime/cgo/gcc_mmap.c
src/runtime/cgo/mmap.go
src/runtime/cgo_mmap.go
src/runtime/mmap.go
src/runtime/msan/msan.go
src/runtime/msan_loong64.s [new file with mode: 0644]
src/runtime/sys_linux_loong64.s

index ef7e5fdf724fac8e6f9f4540b72307d955fe1323..17c46913f3c602d5f382e27f27c6d8f0521915d5 100644 (file)
 //             linux/ppc64le and linux/arm64 (only for 48-bit VMA).
 //     -msan
 //             enable interoperation with memory sanitizer.
-//             Supported only on linux/amd64, linux/arm64, freebsd/amd64
+//             Supported only on linux/amd64, linux/arm64, linux/loong64, freebsd/amd64
 //             and only with Clang/LLVM as the host C compiler.
 //             PIE build mode will be used on all platforms except linux/amd64.
 //     -asan
index e2e0e07299694c633d18cd4569e6b381921917f5..05e300581c78a6e7441c7e532e526050f18aecb6 100644 (file)
@@ -76,7 +76,7 @@ and test commands:
                linux/ppc64le and linux/arm64 (only for 48-bit VMA).
        -msan
                enable interoperation with memory sanitizer.
-               Supported only on linux/amd64, linux/arm64, freebsd/amd64
+               Supported only on linux/amd64, linux/arm64, linux/loong64, freebsd/amd64
                and only with Clang/LLVM as the host C compiler.
                PIE build mode will be used on all platforms except linux/amd64.
        -asan
index f20a977526adb8838635259c1dc922adfc782688..b70a3d98ca6aad6d34cde5f025c8ab7406bc628b 100644 (file)
@@ -38,7 +38,7 @@ func RaceDetectorSupported(goos, goarch string) bool {
 func MSanSupported(goos, goarch string) bool {
        switch goos {
        case "linux":
-               return goarch == "amd64" || goarch == "arm64"
+               return goarch == "amd64" || goarch == "arm64" || goarch == "loong64"
        case "freebsd":
                return goarch == "amd64"
        default:
index 1fbd5e82a4dae9b31d410b7f0013eb9ee7064906..eb710a039d4e0569ccc83673f53c4db90cf07898 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 (linux && (amd64 || arm64 || ppc64le)) || (freebsd && amd64)
+//go:build (linux && (amd64 || arm64 || loong64 || ppc64le)) || (freebsd && amd64)
 
 #include <errno.h>
 #include <stdint.h>
index 2f7e83bcb7357ee40159d787dc908927609711e8..144af2b2caf826a84cf54a99ba573ce03b21d3b9 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 (linux && amd64) || (linux && arm64) || (freebsd && amd64)
+//go:build (linux && (amd64 || arm64 || loong64)) || (freebsd && amd64)
 
 package cgo
 
index 30660f77844fbe83867f76888c7514ad0026ebd2..36d776e628143e284ebc203e45ca6b18e3afb761 100644 (file)
@@ -4,7 +4,7 @@
 
 // Support for memory sanitizer. See runtime/cgo/mmap.go.
 
-//go:build (linux && amd64) || (linux && arm64) || (freebsd && amd64)
+//go:build (linux && (amd64 || arm64 || loong64)) || (freebsd && amd64)
 
 package runtime
 
index f0183f61cfad82953d0f7c0163556822c7583ae6..9a7b2985623d685ea2dcd2e87b3f389e29c4dfb9 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 !aix && !darwin && !js && (!linux || !amd64) && (!linux || !arm64) && (!freebsd || !amd64) && !openbsd && !plan9 && !solaris && !windows
+//go:build !aix && !darwin && !js && !((linux && (amd64 || arm64 || loong64)) || (freebsd && amd64)) && !openbsd && !plan9 && !solaris && !windows
 
 package runtime
 
index 4e41f8528d02a1d2d5141208f4edd1013ed1068b..7b3e8e608d840fe95f5a3fe388bc031d0b4fb1a2 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 msan && ((linux && (amd64 || arm64)) || (freebsd && amd64))
+//go:build msan && ((linux && (amd64 || arm64 || loong64)) || (freebsd && amd64))
 
 package msan
 
diff --git a/src/runtime/msan_loong64.s b/src/runtime/msan_loong64.s
new file mode 100644 (file)
index 0000000..f69fb45
--- /dev/null
@@ -0,0 +1,72 @@
+// 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 msan
+
+#include "go_asm.h"
+#include "textflag.h"
+
+#define RARG0 R4
+#define RARG1 R5
+#define RARG2 R6
+#define FARG  R7
+
+// func runtime·domsanread(addr unsafe.Pointer, sz uintptr)
+// Called from msanread.
+TEXT   runtime·domsanread(SB), NOSPLIT, $0-16
+       MOVV    addr+0(FP), RARG0
+       MOVV    size+8(FP), RARG1
+       // void __msan_read_go(void *addr, uintptr_t sz);
+       MOVV    $__msan_read_go(SB), FARG
+       JMP     msancall<>(SB)
+
+// func runtime·msanwrite(addr unsafe.Pointer, sz uintptr)
+// Called from instrumented code.
+TEXT   runtime·msanwrite(SB), NOSPLIT, $0-16
+       MOVV    addr+0(FP), RARG0
+       MOVV    size+8(FP), RARG1
+       // void __msan_write_go(void *addr, uintptr_t sz);
+       MOVV    $__msan_write_go(SB), FARG
+       JMP     msancall<>(SB)
+
+// func runtime·msanmalloc(addr unsafe.Pointer, sz uintptr)
+TEXT   runtime·msanmalloc(SB), NOSPLIT, $0-16
+       MOVV    addr+0(FP), RARG0
+       MOVV    size+8(FP), RARG1
+       // void __msan_malloc_go(void *addr, uintptr_t sz);
+       MOVV    $__msan_malloc_go(SB), FARG
+       JMP     msancall<>(SB)
+
+// func runtime·msanfree(addr unsafe.Pointer, sz uintptr)
+TEXT   runtime·msanfree(SB), NOSPLIT, $0-16
+       MOVV    addr+0(FP), RARG0
+       MOVV    size+8(FP), RARG1
+       // void __msan_free_go(void *addr, uintptr_t sz);
+       MOVV    $__msan_free_go(SB), FARG
+       JMP     msancall<>(SB)
+
+// func runtime·msanmove(dst, src unsafe.Pointer, sz uintptr)
+TEXT   runtime·msanmove(SB), NOSPLIT, $0-24
+       MOVV    dst+0(FP), RARG0
+       MOVV    src+8(FP), RARG1
+       MOVV    size+16(FP), RARG2
+       // void __msan_memmove(void *dst, void *src, uintptr_t sz);
+       MOVV    $__msan_memmove(SB), FARG
+       JMP     msancall<>(SB)
+
+// Switches SP to g0 stack and calls (FARG). Arguments already set.
+TEXT   msancall<>(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
index 12e5455345e8e474e90e3c1dede78893c7f25336..eba8e1f24ce20437bcf96723d892b573253a30f5 100644 (file)
@@ -461,8 +461,8 @@ TEXT runtime·sigtramp(SB),NOSPLIT|TOPFRAME,$168
 TEXT runtime·cgoSigtramp(SB),NOSPLIT,$0
        JMP     runtime·sigtramp(SB)
 
-// func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (p unsafe.Pointer, err int)
-TEXT runtime·mmap(SB),NOSPLIT|NOFRAME,$0
+// func sysMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (p unsafe.Pointer, err int)
+TEXT runtime·sysMmap(SB),NOSPLIT|NOFRAME,$0
        MOVV    addr+0(FP), R4
        MOVV    n+8(FP), R5
        MOVW    prot+16(FP), R6
@@ -483,8 +483,25 @@ ok:
        MOVV    $0, err+40(FP)
        RET
 
-// func munmap(addr unsafe.Pointer, n uintptr)
-TEXT runtime·munmap(SB),NOSPLIT|NOFRAME,$0
+// Call the function stored in _cgo_mmap using the GCC calling convention.
+// This must be called on the system stack.
+// func callCgoMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) uintptr
+TEXT runtime·callCgoMmap(SB),NOSPLIT,$0
+       MOVV    addr+0(FP), R4
+       MOVV    n+8(FP), R5
+       MOVW    prot+16(FP), R6
+       MOVW    flags+20(FP), R7
+       MOVW    fd+24(FP), R8
+       MOVW    off+28(FP), R9
+       MOVV    _cgo_mmap(SB), R13
+       SUBV    $16, R3         // reserve 16 bytes for sp-8 where fp may be saved.
+       JAL     (R13)
+       ADDV    $16, R3
+       MOVV    R4, ret+32(FP)
+       RET
+
+// func sysMunmap(addr unsafe.Pointer, n uintptr)
+TEXT runtime·sysMunmap(SB),NOSPLIT|NOFRAME,$0
        MOVV    addr+0(FP), R4
        MOVV    n+8(FP), R5
        MOVV    $SYS_munmap, R11
@@ -494,6 +511,18 @@ TEXT runtime·munmap(SB),NOSPLIT|NOFRAME,$0
        MOVV    R0, 0xf3(R0)    // crash
        RET
 
+// Call the function stored in _cgo_munmap using the GCC calling convention.
+// This must be called on the system stack.
+// func callCgoMunmap(addr unsafe.Pointer, n uintptr)
+TEXT runtime·callCgoMunmap(SB),NOSPLIT,$0
+       MOVV    addr+0(FP), R4
+       MOVV    n+8(FP), R5
+       MOVV    _cgo_munmap(SB), R13
+       SUBV    $16, R3         // reserve 16 bytes for sp-8 where fp may be saved.
+       JAL     (R13)
+       ADDV    $16, R3
+       RET
+
 // func madvise(addr unsafe.Pointer, n uintptr, flags int32)
 TEXT runtime·madvise(SB),NOSPLIT|NOFRAME,$0
        MOVV    addr+0(FP), R4