8000 cmd/link: -race doesn't work by default on FreeBSD with cgo · Issue #73788 · golang/go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

cmd/link: -race doesn't work by default on FreeBSD with cgo #73788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
prattmic opened this issue May 19, 2025 · 5 comments
Open

cmd/link: -race doesn't work by default on FreeBSD with cgo #73788

prattmic opened this issue May 19, 2025 · 5 comments
Labels
BugReport Issues describing a possible bug in the Go implementation. compiler/runtime Issues related to the Go compiler and/or runtime. OS-FreeBSD
Milestone

Comments

@prattmic
Copy link
Member

On our freebsd-amd64 builders (14.2-RELEASE-p2):

package main

/*
int foo(void) { return 42; }
*/
import "C"
import "fmt"

func main() {
        fmt.Println(C.foo())
}
$ go build cgo.go
$ ./cgo
42
$ go build -race cgo.go
$ ./cgo
This sanitizer is not compatible with enabled ASLR and binaries compiled with PIE
$ go build -race -ldflags=-linkmode=internal cgo.go
$ ./cgo
42

i.e., using cgo defaults to external linking, but it seems that external linking breaks -race mode.

cc @golang/freebsd

@prattmic prattmic added this to the Backlog milestone May 19, 2025
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label May 19, 2025
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/674160 mentions this issue: runtime: skip testprogcgo tests in race mode on freebsd

@gabyhelp
Copy link

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label May 19, 2025
@cherrymui
Copy link
Member

In internal linking mode with disable ASLR under -race, https://cs.opensource.google/go/go/+/master:src/cmd/link/internal/ld/elf.go;l=800
We probably should pass a flag to the external linker to do so. Reading FreeBSD's ld manpage, its not clear to me what flag to pass. @golang/freebsd do you have any idea?

Thanks.

@paulzhol
Copy link
Member

The equivalent would be calling elfctl -e +noaslr <file>.
I'm not sure LLVM ld.lld has anything explicit for setting a NT_FREEBSD_FEATURE_CTL note (maybe a custom linker script with a .note section).
@igalic @emaste Maybe you could help?

gopherbot pushed a commit that referenced this issue May 20, 2025
These were just enabled by https://go.dev/cl/643897, but freebsd
unfortunately doesn't seem to support cgo + race mode by default.

For #73788.

Cq-Include-Trybots: luci.golang.try:gotip-freebsd-amd64-race
Change-Id: I6a6a636c06176ca746548d0588283b1429d7c6d5
Reviewed-on: https://go-revie
8841
w.googlesource.com/c/go/+/674160
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
@emaste
Copy link
emaste commented May 21, 2025

There is indeed no built-in support in ld.lld for configuring NT_FREEBSD_FEATURE_CTL. The only supported approach is having elfctl modify the linked binary.

The bits in the note are documented in elf(5):

     NT_FREEBSD_FEATURE_CTL (Value: 4)
           Contains a bitmask of mitigations and features to enable:

           NT_FREEBSD_FCTL_ASLR_DISABLE (Value: 0x01)
                 Request that address randomization (ASLR) not be performed.
                 See security(7).

           NT_FREEBSD_FCTL_PROTMAX_DISABLE (Value: 0x02)
                 Request that mmap(2) calls not set PROT_MAX to the initial
                 value of the prot argument.

           NT_FREEBSD_FCTL_STKGAP_DISABLE (Value: 0x04)
                 Disable stack gap.

           NT_FREEBSD_FCTL_WXNEEDED (Value: 0x08)
                 Indicate that the binary requires mappings that are
                 simultaneously writeable and executable.

It would be fairly straightforward for us to propose a patch adding support to ld.lld but it hasn't been done yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. compiler/runtime Issues related to the Go compiler and/or runtime. OS-FreeBSD
Projects
Status: No status
Development

No branches or pull requests

6 participants
0