Skip to content

Commit

Permalink
runtime: add msan support on freebsd/amd64
Browse files Browse the repository at this point in the history
Adjust build constraints and change the runtime to call the C mmap function
when using cgo.

R=go1.20

For #53298

Change-Id: If9c3306dc16a8645d1bb9be0343e0472d6c4783f
Reviewed-on: https://go-review.googlesource.com/c/go/+/411274
Reviewed-by: Bryan Mills <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Reviewed-by: Michael Pratt <[email protected]>
  • Loading branch information
dmgk committed Oct 14, 2022
1 parent 76e4833 commit 5fde02e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/runtime/cgo/gcc_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build linux,amd64 linux,arm64 linux,ppc64le
// +build linux,amd64 linux,arm64 linux,ppc64le freebsd,amd64

#include <errno.h>
#include <stdint.h>
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/cgo/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
//go:build (linux && amd64) || (linux && arm64) || (freebsd && amd64)

package cgo

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/cgo_mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

//go:build (linux && amd64) || (linux && arm64)
//go:build (linux && amd64) || (linux && arm64) || (freebsd && amd64)

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) && !openbsd && !plan9 && !solaris && !windows
//go:build !aix && !darwin && !js && (!linux || !amd64) && (!linux || !arm64) && (!freebsd || !amd64) && !openbsd && !plan9 && !solaris && !windows

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/msan/msan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
//go:build msan && ((linux && (amd64 || arm64)) || (freebsd && amd64))

package msan

Expand Down
35 changes: 33 additions & 2 deletions src/runtime/sys_freebsd_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ sigtrampnog:
MOVQ _cgo_callers(SB), AX
JMP AX

TEXT runtime·mmap(SB),NOSPLIT,$0
TEXT runtime·sysMmap(SB),NOSPLIT,$0
MOVQ addr+0(FP), DI // arg 1 addr
MOVQ n+8(FP), SI // arg 2 len
MOVL prot+16(FP), DX // arg 3 prot
Expand All @@ -403,7 +403,25 @@ ok:
MOVQ $0, err+40(FP)
RET

TEXT runtime·munmap(SB),NOSPLIT,$0
// Call the function stored in _cgo_mmap using the GCC calling convention.
// This must be called on the system stack.
TEXT runtime·callCgoMmap(SB),NOSPLIT,$16
MOVQ addr+0(FP), DI
MOVQ n+8(FP), SI
MOVL prot+16(FP), DX
MOVL flags+20(FP), CX
MOVL fd+24(FP), R8
MOVL off+28(FP), R9
MOVQ _cgo_mmap(SB), AX
MOVQ SP, BX
ANDQ $~15, SP // alignment as per amd64 psABI
MOVQ BX, 0(SP)
CALL AX
MOVQ 0(SP), SP
MOVQ AX, ret+32(FP)
RET

TEXT runtime·sysMunmap(SB),NOSPLIT,$0
MOVQ addr+0(FP), DI // arg 1 addr
MOVQ n+8(FP), SI // arg 2 len
MOVL $SYS_munmap, AX
Expand All @@ -412,6 +430,19 @@ TEXT runtime·munmap(SB),NOSPLIT,$0
MOVL $0xf1, 0xf1 // crash
RET

// Call the function stored in _cgo_munmap using the GCC calling convention.
// This must be called on the system stack.
TEXT runtime·callCgoMunmap(SB),NOSPLIT,$16-16
MOVQ addr+0(FP), DI
MOVQ n+8(FP), SI
MOVQ _cgo_munmap(SB), AX
MOVQ SP, BX
ANDQ $~15, SP // alignment as per amd64 psABI
MOVQ BX, 0(SP)
CALL AX
MOVQ 0(SP), SP
RET

TEXT runtime·madvise(SB),NOSPLIT,$0
MOVQ addr+0(FP), DI
MOVQ n+8(FP), SI
Expand Down

0 comments on commit 5fde02e

Please sign in to comment.