golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.76k stars 17.64k forks source link

runtime/race: eliminate dependency on libc #9918

Open dvyukov opened 9 years ago

dvyukov commented 9 years ago

Race runtime currently depends on libc:

with CGO_ENABLED=0: runtime/race(.text): __libc_malloc: not defined runtime/race(.text): getuid: not defined runtime/race(.text): pthread_self: not defined ...

This has several negative effects:

If we eliminate all libc dependencies from race runtime, all these problems go away.

dvyukov commented 9 years ago

related to #6508

dlsniper commented 8 years ago

Hi, I wish to add that currently doing go test -race ./... inside a go:1.5.3-alpine container, with CGO disabled, will fail due to this.

PROJECT_DIR="${PWD}" #assume we are in $GOPATH/src/github.com/dlsniper/demo on the computer
CONTAINER_PROJECT_DIR="/go/src/github.com/dlsniper/demo"
CONTAINER_PROJECT_GOPATH="${CONTAINER_PROJECT_DIR}/vendor:/go"

docker run --rm \
        --net="host" \
        -v ${PROJECT_DIR}:${CONTAINER_PROJECT_DIR} \
        -e CI=true \
        -e GODEBUG=netdns=go \
        -e CGO_ENABLED=0 \
        -e GOPATH=${CONTAINER_PROJECT_GOPATH} \
        -w "${CONTAINER_PROJECT_DIR}" \
        golang:1.5.3-alpine \
        go test -v -race ./...

Output:

# testmain
runtime/race(.text): __libc_malloc: not defined
runtime/race(.text): getuid: not defined
runtime/race(.text): pthread_self: not defined
runtime/race(.text): madvise: not defined
runtime/race(.text): madvise: not defined
runtime/race(.text): madvise: not defined
runtime/race(.text): sleep: not defined
runtime/race(.text): usleep: not defined
runtime/race(.text): abort: not defined
runtime/race(.text): isatty: not defined
runtime/race(.text): __libc_free: not defined
runtime/race(.text): getrlimit: not defined
runtime/race(.text): pipe: not defined
runtime/race(.text): __libc_stack_end: not defined
runtime/race(.text): getrlimit: not defined
runtime/race(.text): setrlimit: not defined
runtime/race(.text): setrlimit: not defined
runtime/race(.text): setrlimit: not defined
runtime/race(.text): exit: not defined
runtime/race(.text.unlikely): __errno_location: not defined
runtime/race(.text): undefined: __libc_malloc
/usr/local/go/pkg/tool/linux_amd64/link: too many errors

Not sure if I should open a separate issue or this comment is enough, please advise. Also the example is not meant to work as it currently points to an non-existing repository but I can make it work if needed.

Thank you.

dvyukov commented 8 years ago

Please file a separate issue. Race detector must work regardless of dependency on libc. Looks like some issue with linker. Does it work with 1.6? I guess it won't be fixed in 1.5 at this point.

gopherbot commented 7 years ago

CL https://golang.org/cl/41678 mentions this issue.

tamird commented 2 years ago

This issue seems fairly out of date; these days attempting to use -race with CGO_ENABLED=0 prints go build: -race requires cgo; enable cgo by setting CGO_ENABLED=1. The symbols mentioned in the description are out of date as well. What needs to be done to move this forward?

hx commented 1 year ago

From https://tip.golang.org/doc/go1.20:

On macOS, the race detector has been rewritten not to use cgo: race-detector-enabled programs can be built and run without Xcode. On Linux and other Unix systems, and on Windows, a host C toolchain is required to use the race detector.

I can confirm -race works with Go 1.20 on macOS (ARM64) with CGO_ENABLED=0 🎉 But it's unclear to me from the note above how other architectures are affected.

zyxkad commented 3 months ago

On go1.22.5 darwin/arm64 (official build), when I tring to do go run -race with CGO_ENABLED=0, I got following error:

go env ``` $ go env GO111MODULE='' GOARCH='arm64' GOBIN='' GOCACHE='/Users/ckpn/Library/Caches/go-build' GOENV='/Users/ckpn/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/ckpn/Mine/projects/golang/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='darwin' GOPATH='/Users/ckpn/Mine/projects/golang' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='/usr/local/go' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.22.5' GCCGO='gccgo' AR='ar' CC='clang' CXX='clang++' CGO_ENABLED='0' GOMOD='/Users/ckpn/Mine/projects/golang/src/github.com/zyxkad/drone/go.mod' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -arch arm64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/3g/rxdhvbj96lv68y35t2cvghkr0000gn/T/go-build525222943=/tmp/go-build -gno-record-gcc-switches -fno-common' ```
Logs ``` SIGSEGV: segmentation violation PC=0x1046f423c m=0 sigcode=2 addr=0x10 signal arrived during cgo execution goroutine 1 gp=0xc0000021c0 m=0 mp=0x1052b3380 [syscall, locked to thread]: runtime.cgocall(0x104b7d1c0, 0x0) /usr/local/go/src/runtime/cgocall.go:157 +0x58 fp=0xc00008df40 sp=0xc00008df00 pc=0x104739848 runtime.main() /usr/local/go/src/runtime/proc.go:242 +0x214 fp=0xc00008dfd0 sp=0xc00008df40 pc=0x10476f0d4 runtime.goexit({}) /usr/local/go/src/runtime/asm_arm64.s:1222 +0x4 fp=0xc00008dfd0 sp=0xc00008dfd0 pc=0x1047a4ba4 goroutine 2 gp=0xc000002c40 m=nil [force gc (idle)]: runtime.gopark(0x0?, 0x105265d30?, 0x80?, 0x33?, 0x0?) /usr/local/go/src/runtime/proc.go:402 +0xc8 fp=0xc000072790 sp=0xc000072770 pc=0x10476f578 runtime.goparkunlock(...) /usr/local/go/src/runtime/proc.go:408 runtime.forcegchelper() /usr/local/go/src/runtime/proc.go:326 +0xb8 fp=0xc0000727d0 sp=0xc000072790 pc=0x10476f408 runtime.goexit({}) /usr/local/go/src/runtime/asm_arm64.s:1222 +0x4 fp=0xc0000727d0 sp=0xc0000727d0 pc=0x1047a4ba4 created by runtime.init.6 in goroutine 1 /usr/local/go/src/runtime/proc.go:314 +0x24 goroutine 3 gp=0xc000003180 m=nil [GC sweep wait]: runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?) /usr/local/go/src/runtime/proc.go:402 +0xc8 fp=0xc000088f60 sp=0xc000088f40 pc=0x10476f578 runtime.goparkunlock(...) /usr/local/go/src/runtime/proc.go:408 runtime.bgsweep(0xc000044070) /usr/local/go/src/runtime/mgcsweep.go:278 +0xa0 fp=0xc000088fb0 sp=0xc000088f60 pc=0x10475b430 runtime.gcenable.gowrap1() /usr/local/go/src/runtime/mgc.go:203 +0x28 fp=0xc000088fd0 sp=0xc000088fb0 pc=0x10474f668 runtime.goexit({}) /usr/local/go/src/runtime/asm_arm64.s:1222 +0x4 fp=0xc000088fd0 sp=0xc000088fd0 pc=0x1047a4ba4 created by runtime.gcenable in goroutine 1 /usr/local/go/src/runtime/mgc.go:203 +0x6c goroutine 4 gp=0xc000003340 m=nil [GC scavenge wait]: runtime.gopark(0xc000044070?, 0x104e5a100?, 0x1?, 0x0?, 0xc000003340?) /usr/local/go/src/runtime/proc.go:402 +0xc8 fp=0xc000194f60 sp=0xc000194f40 pc=0x10476f578 runtime.goparkunlock(...) /usr/local/go/src/runtime/proc.go:408 runtime.(*scavengerState).park(0x1052b1aa0) /usr/local/go/src/runtime/mgcscavenge.go:425 +0x5c fp=0xc000194f90 sp=0xc000194f60 pc=0x104758e4c runtime.bgscavenge(0xc000044070) /usr/local/go/src/runtime/mgcscavenge.go:653 +0x44 fp=0xc000194fb0 sp=0xc000194f90 pc=0x104759374 runtime.gcenable.gowrap2() /usr/local/go/src/runtime/mgc.go:204 +0x28 fp=0xc000194fd0 sp=0xc000194fb0 pc=0x10474f608 runtime.goexit({}) /usr/local/go/src/runtime/asm_arm64.s:1222 +0x4 fp=0xc000194fd0 sp=0xc000194fd0 pc=0x1047a4ba4 created by runtime.gcenable in goroutine 1 /usr/local/go/src/runtime/mgc.go:204 +0xac r0 0x0 r1 0x104b7d1d0 r2 0xc00008def0 r3 0x1052b2080 r4 0x110 r5 0xc000002250 r6 0x1 r7 0x0 r8 0x1052b3380 r9 0x1046f422c r10 0x1052b3380 r11 0x1052b2080 r12 0x1000000000000000 r13 0x16b70f300 r14 0xffffff0000000000 r15 0x0 r16 0xc00008d6c0 r17 0x1f4b05938 r18 0x0 r19 0x104b7d1d0 r20 0x0 r21 0x16b70f2e0 r22 0x105324c14 r23 0x16b70f410 r24 0x16b70f450 r25 0x18260231b r26 0x104faa0c8 r27 0x105324000 r28 0x1052b2080 r29 0x16b70f278 lr 0x1047a59a4 sp 0x16b70f260 pc 0x1046f423c fault 0x10 exit status 2 ```

I'm not sure if the error is caused by some syscall, but without the -race flag my program works fine