Skip to content
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

runtime: C.setgid hangs on Linux #52141

Closed
qmuntal opened this issue Apr 4, 2022 · 16 comments
Closed

runtime: C.setgid hangs on Linux #52141

qmuntal opened this issue Apr 4, 2022 · 16 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@qmuntal
Copy link
Contributor

qmuntal commented Apr 4, 2022

What version of Go are you using (go version)?

go version devel go1.18-a8e6556445 Fri Apr 1 09:06:13 2022 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/codespace/.cache/go-build"
GOENV="/home/codespace/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel go1.18-a8e6556445 Fri Apr 1 09:06:13 2022 +0000"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/workspaces/go/gotest/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3393830914=/tmp/go-build -gno-record-gcc-switches"

What did you do?

  1. Compile this C snippet into a shared library:
#include <pthread.h>
#include <stdlib.h>

pthread_mutex_t *lock_new(void)
{
    pthread_mutex_t *lock = malloc(sizeof(pthread_mutex_t));
    pthread_mutex_lock(lock);
    return lock;
}
$ gcc -fPIC -c foo.c
$ gcc -shared -o foo.so -pthread foo.o
  1. From Go, load the shared library at runtime and then call setgid:
package main

/*
#include <dlfcn.h>
#include <unistd.h>
#cgo LDFLAGS: -ldl -static
*/
import "C"
import "fmt"

func main() {
	fmt.Println("Step 1")
	if C.dlopen(C.CString("./foo.so"), C.RTLD_NOW) == nil {
		panic("library not found")
	}
	fmt.Println("Step 2")
	C.setgid(0)
	fmt.Println("Step 3")
}
$ go run .

What did you expect to see?

# main
/usr/bin/ld: /tmp/go-link-95168809/000001.o: in function `_cgo_1403fb244f50_Cfunc_dlopen':
/tmp/go-build/cgo-gcc-prolog:54: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
Step 1
Step 2
Step 3

What did you see instead?

# main
/usr/bin/ld: /tmp/go-link-95168809/000001.o: in function `_cgo_1403fb244f50_Cfunc_dlopen':
/tmp/go-build/cgo-gcc-prolog:54: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
Step 1
Step 2

The C.setgid call hangs indefinitely.

Observations

  • A pure C program doing the same calls does not hang
  • It also hangs if the dlopen and setgid calls are done from the C preamble
  • It does not hang if pthreads are not used
  • It does not hang if -static is not set in the LDFLAGS directive in the C preamble

This might be related to #3871 and #9400

@rsc @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 4, 2022
@ianlancetaylor ianlancetaylor added this to the Go1.19 milestone Apr 4, 2022
@prattmic
Copy link
Member

prattmic commented Apr 4, 2022

This doesn't hang for me, but it does crash, which is fun:

# example.com
/usr/bin/ld: /tmp/go-link-1015999001/000001.o: in function `_cgo_3268fbd2b1d3_Cfunc_dlopen':
/tmp/go-build/cgo-gcc-prolog:58: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
Step 1
Step 2
fatal error: unexpected signal during runtime execution
fatal error: unexpected signal during runtime execution
fatal error: unexpected signal during runtime execution
fatal error: unexpected signal during runtime execution
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x7f3185d9074a]

runtime stack:
runtime: unexpected return pc for runtime.sigpanic called from 0x7f3185d9074a
stack: frame={sp:0x7f3186e0a308, fp:0x7f3186e0a360} stack=[0x7f318660b558,0x7f3186e0b158)
0x00007f3186e0a208:  0x01007f3186e0a228  0x0000000000000004 
0x00007f3186e0a218:  0x000000000000001f  0x00007f3185d9074a 
0x00007f3186e0a228:  0x0000000000000010  0x0000000000000001 
0x00007f3186e0a238:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3186e0a2d8 
0x00007f3186e0a248:  0x0000000000540308  0x00007f3186e0a290 
0x00007f3186e0a258:  0x0000000000432d48 <runtime.fatalthrow.func1+0x0000000000000048>  0x000000c000003860 
0x00007f3186e0a268:  0x0000000000000001  0x0000000000000001 
0x00007f3186e0a278:  0x00007f3186e0a2d8  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3186e0a288:  0x000000c000003860  0x00007f3186e0a2c8 
0x00007f3186e0a298:  0x0000000000432cd0 <runtime.fatalthrow+0x0000000000000050>  0x00007f3186e0a2a8 
0x00007f3186e0a2a8:  0x0000000000432d00 <runtime.fatalthrow.func1+0x0000000000000000>  0x000000c000003860 
0x00007f3186e0a2b8:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3186e0a2d8 
0x00007f3186e0a2c8:  0x00007f3186e0a2f8  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3186e0a2d8:  0x00007f3186e0a2e0  0x0000000000432ac0 <runtime.throw.func1+0x0000000000000000> 
0x00007f3186e0a2e8:  0x0000000000542054  0x000000000000002a 
0x00007f3186e0a2f8:  0x00007f3186e0a350  0x0000000000446956 <runtime.sigpanic+0x0000000000000416> 
0x00007f3186e0a308: <0x0000000000542054  0x0000000000000000 
0x00007f3186e0a318:  0x0000000000000000  0x0000000000000000 
0x00007f3186e0a328:  0x0000000000000000  0x0000000000000000 
0x00007f3186e0a338:  0x0000000000000000  0x0000000000000000 
0x00007f3186e0a348:  0x0000000000000000  0x00000000000207d0 
0x00007f3186e0a358: !0x00007f3185d9074a >0x0000000000000000 
0x00007f3186e0a368:  0x0000000000000000  0x00007f3186e0b0c0 
0x00007f3186e0a378:  0x00007f3185d9c200  0x0000000000000007 
0x00007f3186e0a388:  0x0000000000000000  0x000000c000084000 
0x00007f3186e0a398:  0x0000000000000000  0x0000000000008000 
0x00007f3186e0a3a8:  0x0000000000000000  0x0000000000000000 
0x00007f3186e0a3b8:  0x0000000000000000  0x0000000000000286 
0x00007f3186e0a3c8:  0x00007fff0dbcd41e  0x00007fff0dbcd41f 
0x00007f3186e0a3d8:  0x000000c000003860  0x00007f3186e0b640 
0x00007f3186e0a3e8:  0x0000000000633a18  0x0000000000000080 
0x00007f3186e0a3f8:  0x00007f3186e0b0c0  0x0000000000000000 
0x00007f3186e0a408:  0x0000000000000000  0x00000000000000ca 
0x00007f3186e0a418:  0x000000000045f863 <runtime.futex+0x0000000000000023>  0x00007f3186e0b078 
0x00007f3186e0a428:  0x000000000045f861 <runtime.futex+0x0000000000000021>  0x0000000000000286 
0x00007f3186e0a438:  0x002b000000000033  0x0000000000000000 
0x00007f3186e0a448:  0x0000000000000000  0x0000000000000000 
0x00007f3186e0a458:  0x0000000000000000 
runtime.throw({0x542054?, 0x0?})
	/usr/lib/google-golang/src/runtime/panic.go:998 +0x71
runtime: unexpected return pc for runtime.sigpanic called from 0x7f3185d9074a
stack: frame={sp:0x7f3186e0a308, fp:0x7f3186e0a360} stack=[0x7f318660b558,0x7f3186e0b158)
0x00007f3186e0a208:  0x01007f3186e0a228  0x0000000000000004 
0x00007f3186e0a218:  0x000000000000001f  0x00007f3185d9074a 
0x00007f3186e0a228:  0x0000000000000010  0x0000000000000001 
0x00007f3186e0a238:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3186e0a2d8 
0x00007f3186e0a248:  0x0000000000540308  0x00007f3186e0a290 
0x00007f3186e0a258:  0x0000000000432d48 <runtime.fatalthrow.func1+0x0000000000000048>  0x000000c000003860 
0x00007f3186e0a268:  0x0000000000000001  0x0000000000000001 
0x00007f3186e0a278:  0x00007f3186e0a2d8  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3186e0a288:  0x000000c000003860  0x00007f3186e0a2c8 
0x00007f3186e0a298:  0x0000000000432cd0 <runtime.fatalthrow+0x0000000000000050>  0x00007f3186e0a2a8 
0x00007f3186e0a2a8:  0x0000000000432d00 <runtime.fatalthrow.func1+0x0000000000000000>  0x000000c000003860 
0x00007f3186e0a2b8:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3186e0a2d8 
0x00007f3186e0a2c8:  0x00007f3186e0a2f8  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3186e0a2d8:  0x00007f3186e0a2e0  0x0000000000432ac0 <runtime.throw.func1+0x0000000000000000> 
0x00007f3186e0a2e8:  0x0000000000542054  0x000000000000002a 
0x00007f3186e0a2f8:  0x00007f3186e0a350  0x0000000000446956 <runtime.sigpanic+0x0000000000000416> 
0x00007f3186e0a308: <0x0000000000542054  0x0000000000000000 
0x00007f3186e0a318:  0x0000000000000000  0x0000000000000000 
0x00007f3186e0a328:  0x0000000000000000  0x0000000000000000 
0x00007f3186e0a338:  0x0000000000000000  0x0000000000000000 
0x00007f3186e0a348:  0x0000000000000000  0x00000000000207d0 
0x00007f3186e0a358: !0x00007f3185d9074a >0x0000000000000000 
0x00007f3186e0a368:  0x0000000000000000  0x00007f3186e0b0c0 
0x00007f3186e0a378:  0x00007f3185d9c200  0x0000000000000007 
0x00007f3186e0a388:  0x0000000000000000  0x000000c000084000 
0x00007f3186e0a398:  0x0000000000000000  0x0000000000008000 
0x00007f3186e0a3a8:  0x0000000000000000  0x0000000000000000 
0x00007f3186e0a3b8:  0x0000000000000000  0x0000000000000286 
0x00007f3186e0a3c8:  0x00007fff0dbcd41e  0x00007fff0dbcd41f 
0x00007f3186e0a3d8:  0x000000c000003860  0x00007f3186e0b640 
0x00007f3186e0a3e8:  0x0000000000633a18  0x0000000000000080 
0x00007f3186e0a3f8:  0x00007f3186e0b0c0  0x0000000000000000 
0x00007f3186e0a408:  0x0000000000000000  0x00000000000000ca 
0x00007f3186e0a418:  0x000000000045f863 <runtime.futex+0x0000000000000023>  0x00007f3186e0b078 
0x00007f3186e0a428:  0x000000000045f861 <runtime.futex+0x0000000000000021>  0x0000000000000286 
0x00007f3186e0a438:  0x002b000000000033  0x0000000000000000 
0x00007f3186e0a448:  0x0000000000000000  0x0000000000000000 
0x00007f3186e0a458:  0x0000000000000000 
runtime.sigpanic()
	/usr/lib/google-golang/src/runtime/signal_unix.go:804 +0x416

goroutine 1 [syscall]:
main._Cfunc_setgid(0x0)
	_cgo_gotypes.go:82 +0x4c
main.main()
	/usr/local/google/home/mpratt/Downloads/hang/main.go:17 +0xdb
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x7f3185d9074a]

runtime stack:
runtime: unexpected return pc for runtime.sigpanic called from 0x7f3185d9074a
stack: frame={sp:0x7f31866091c8, fp:0x7f3186609220} stack=[0x7f3185e0a558,0x7f318660a158)
0x00007f31866090c8:  0x01007f31866090e8  0x0000000000000004 
0x00007f31866090d8:  0x000000000000001f  0x00007f3185d9074a 
0x00007f31866090e8:  0x0000000000000010  0x0000000000000001 
0x00007f31866090f8:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3186609198 
0x00007f3186609108:  0x0000000000540308  0x00007f3186609150 
0x00007f3186609118:  0x0000000000432d48 <runtime.fatalthrow.func1+0x0000000000000048>  0x000000c0001021a0 
0x00007f3186609128:  0x000000000040bf13 <runtime.unlock2+0x0000000000000033>  0x0000000000633788 
0x00007f3186609138:  0x00007f3186609198  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3186609148:  0x000000c0001021a0  0x00007f3186609188 
0x00007f3186609158:  0x0000000000432cd0 <runtime.fatalthrow+0x0000000000000050>  0x00007f3186609168 
0x00007f3186609168:  0x0000000000432d00 <runtime.fatalthrow.func1+0x0000000000000000>  0x000000c0001021a0 
0x00007f3186609178:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3186609198 
0x00007f3186609188:  0x00007f31866091b8  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3186609198:  0x00007f31866091a0  0x0000000000432ac0 <runtime.throw.func1+0x0000000000000000> 
0x00007f31866091a8:  0x0000000000542054  0x000000000000002a 
0x00007f31866091b8:  0x00007f3186609210  0x0000000000446956 <runtime.sigpanic+0x0000000000000416> 
0x00007f31866091c8: <0x0000000000542054  0x0000000000000000 
0x00007f31866091d8:  0x0000000000000000  0x0000000000000000 
0x00007f31866091e8:  0x0000000000000000  0x0000000000000000 
0x00007f31866091f8:  0x0000000000000000  0x0000000000000000 
0x00007f3186609208:  0x0000000000000000  0x00000000000207d0 
0x00007f3186609218: !0x00007f3185d9074a >0x0000000000000000 
0x00007f3186609228:  0x0000000000000000  0x00007f3186609f68 
0x00007f3186609238:  0x00007f3185d9c200  0x0000000000000007 
0x00007f3186609248:  0x0000000000000000  0x000000c000104000 
0x00007f3186609258:  0x0000000000000000  0x0000000000008000 
0x00007f3186609268:  0x0000000000000000  0x0000000000000000 
0x00007f3186609278:  0x0000000000000000  0x0000000000000286 
0x00007f3186609288:  0x0000000000000000  0x0000000000000004 
0x00007f3186609298:  0x000000c0001021a0  0x00007f318660a640 
0x00007f31866092a8:  0x000000c000100148  0x0000000000000080 
0x00007f31866092b8:  0x00007f3186609f68  0x0000000000000000 
0x00007f31866092c8:  0x0000000000000000  0x00000000000000ca 
0x00007f31866092d8:  0x000000000045f863 <runtime.futex+0x0000000000000023>  0x00007f3186609f20 
0x00007f31866092e8:  0x000000000045f861 <runtime.futex+0x0000000000000021>  0x0000000000000286 
0x00007f31866092f8:  0x002b000000000033  0x0000000000000000 
0x00007f3186609308:  0x0000000000000000  0x0000000000000000 
0x00007f3186609318:  0x0000000000000000 
runtime.throw({0x542054?, 0x0?})
	/usr/lib/google-golang/src/runtime/panic.go:998 +0x71
runtime: unexpected return pc for runtime.sigpanic called from 0x7f3185d9074a
stack: frame={sp:0x7f31866091c8, fp:0x7f3186609220} stack=[0x7f3185e0a558,0x7f318660a158)
0x00007f31866090c8:  0x01007f31866090e8  0x0000000000000004 
0x00007f31866090d8:  0x000000000000001f  0x00007f3185d9074a 
0x00007f31866090e8:  0x0000000000000010  0x0000000000000001 
0x00007f31866090f8:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3186609198 
0x00007f3186609108:  0x0000000000540308  0x00007f3186609150 
0x00007f3186609118:  0x0000000000432d48 <runtime.fatalthrow.func1+0x0000000000000048>  0x000000c0001021a0 
0x00007f3186609128:  0x000000000040bf13 <runtime.unlock2+0x0000000000000033>  0x0000000000633788 
0x00007f3186609138:  0x00007f3186609198  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3186609148:  0x000000c0001021a0  0x00007f3186609188 
0x00007f3186609158:  0x0000000000432cd0 <runtime.fatalthrow+0x0000000000000050>  0x00007f3186609168 
0x00007f3186609168:  0x0000000000432d00 <runtime.fatalthrow.func1+0x0000000000000000>  0x000000c0001021a0 
0x00007f3186609178:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3186609198 
0x00007f3186609188:  0x00007f31866091b8  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3186609198:  0x00007f31866091a0  0x0000000000432ac0 <runtime.throw.func1+0x0000000000000000> 
0x00007f31866091a8:  0x0000000000542054  0x000000000000002a 
0x00007f31866091b8:  0x00007f3186609210  0x0000000000446956 <runtime.sigpanic+0x0000000000000416> 
0x00007f31866091c8: <0x0000000000542054  0x0000000000000000 
0x00007f31866091d8:  0x0000000000000000  0x0000000000000000 
0x00007f31866091e8:  0x0000000000000000  0x0000000000000000 
0x00007f31866091f8:  0x0000000000000000  0x0000000000000000 
0x00007f3186609208:  0x0000000000000000  0x00000000000207d0 
0x00007f3186609218: !0x00007f3185d9074a >0x0000000000000000 
0x00007f3186609228:  0x0000000000000000  0x00007f3186609f68 
0x00007f3186609238:  0x00007f3185d9c200  0x0000000000000007 
0x00007f3186609248:  0x0000000000000000  0x000000c000104000 
0x00007f3186609258:  0x0000000000000000  0x0000000000008000 
0x00007f3186609268:  0x0000000000000000  0x0000000000000000 
0x00007f3186609278:  0x0000000000000000  0x0000000000000286 
0x00007f3186609288:  0x0000000000000000  0x0000000000000004 
0x00007f3186609298:  0x000000c0001021a0  0x00007f318660a640 
0x00007f31866092a8:  0x000000c000100148  0x0000000000000080 
0x00007f31866092b8:  0x00007f3186609f68  0x0000000000000000 
0x00007f31866092c8:  0x0000000000000000  0x00000000000000ca 
0x00007f31866092d8:  0x000000000045f863 <runtime.futex+0x0000000000000023>  0x00007f3186609f20 
0x00007f31866092e8:  0x000000000045f861 <runtime.futex+0x0000000000000021>  0x0000000000000286 
0x00007f31866092f8:  0x002b000000000033  0x0000000000000000 
0x00007f3186609308:  0x0000000000000000  0x0000000000000000 
0x00007f3186609318:  0x0000000000000000 
runtime.sigpanic()
	/usr/lib/google-golang/src/runtime/signal_unix.go:804 +0x416
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x7f3185d9074a]

runtime stack:
runtime: unexpected return pc for runtime.sigpanic called from 0x7f3185d9074a
stack: frame={sp:0x7f317fffe1c8, fp:0x7f317fffe220} stack=[0x7f317f7ff558,0x7f317ffff158)
0x00007f317fffe0c8:  0x01007f317fffe0e8  0x0000000000000004 
0x00007f317fffe0d8:  0x000000000000001f  0x00007f3185d9074a 
0x00007f317fffe0e8:  0x0000000000000010  0x0000000000000001 
0x00007f317fffe0f8:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f317fffe198 
0x00007f317fffe108:  0x0000000000540308  0x00007f317fffe150 
0x00007f317fffe118:  0x0000000000432d48 <runtime.fatalthrow.func1+0x0000000000000048>  0x000000c000002ea0 
0x00007f317fffe128:  0x000000000040bf13 <runtime.unlock2+0x0000000000000033>  0x0000000000633788 
0x00007f317fffe138:  0x00007f317fffe198  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f317fffe148:  0x000000c000002ea0  0x00007f317fffe188 
0x00007f317fffe158:  0x0000000000432cd0 <runtime.fatalthrow+0x0000000000000050>  0x00007f317fffe168 
0x00007f317fffe168:  0x0000000000432d00 <runtime.fatalthrow.func1+0x0000000000000000>  0x000000c000002ea0 
0x00007f317fffe178:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f317fffe198 
0x00007f317fffe188:  0x00007f317fffe1b8  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f317fffe198:  0x00007f317fffe1a0  0x0000000000432ac0 <runtime.throw.func1+0x0000000000000000> 
0x00007f317fffe1a8:  0x0000000000542054  0x000000000000002a 
0x00007f317fffe1b8:  0x00007f317fffe210  0x0000000000446956 <runtime.sigpanic+0x0000000000000416> 
0x00007f317fffe1c8: <0x0000000000542054  0x0000000000000000 
0x00007f317fffe1d8:  0x0000000000000000  0x0000000000000000 
0x00007f317fffe1e8:  0x0000000000000000  0x0000000000000000 
0x00007f317fffe1f8:  0x0000000000000000  0x0000000000000000 
0x00007f317fffe208:  0x0000000000000000  0x00000000000207d0 
0x00007f317fffe218: !0x00007f3185d9074a >0x0000000000000000 
0x00007f317fffe228:  0x0000000000000000  0x00007f317fffef68 
0x00007f317fffe238:  0x00007f3185d9c200  0x0000000000000007 
0x00007f317fffe248:  0x0000000000000000  0x000000c00006e000 
0x00007f317fffe258:  0x0000000000000000  0x0000000000008000 
0x00007f317fffe268:  0x0000000000000000  0x0000000000000000 
0x00007f317fffe278:  0x0000000000000000  0x0000000000000286 
0x00007f317fffe288:  0x0000000000000002  0x0000000000000000 
0x00007f317fffe298:  0x000000c000002ea0  0x00007f3187f7611a 
0x00007f317fffe2a8:  0x000000c000052948  0x0000000000000080 
0x00007f317fffe2b8:  0x00007f317fffef68  0x0000000000000000 
0x00007f317fffe2c8:  0x0000000000000000  0x00000000000000ca 
0x00007f317fffe2d8:  0x000000000045f863 <runtime.futex+0x0000000000000023>  0x00007f317fffef20 
0x00007f317fffe2e8:  0x000000000045f861 <runtime.futex+0x0000000000000021>  0x0000000000000286 
0x00007f317fffe2f8:  0x002b000000000033  0x0000000000000000 
0x00007f317fffe308:  0x0000000000000000  0x0000000000000000 
0x00007f317fffe318:  0x0000000000000000 
runtime.throw({0x542054?, 0x0?})
	/usr/lib/google-golang/src/runtime/panic.go:998 +0x71
runtime: unexpected return pc for runtime.sigpanic called from 0x7f3185d9074a
stack: frame={sp:0x7f317fffe1c8, fp:0x7f317fffe220} stack=[0x7f317f7ff558,0x7f317ffff158)
0x00007f317fffe0c8:  0x01007f317fffe0e8  0x0000000000000004 
0x00007f317fffe0d8:  0x000000000000001f  0x00007f3185d9074a 
0x00007f317fffe0e8:  0x0000000000000010  0x0000000000000001 
0x00007f317fffe0f8:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f317fffe198 
0x00007f317fffe108:  0x0000000000540308  0x00007f317fffe150 
0x00007f317fffe118:  0x0000000000432d48 <runtime.fatalthrow.func1+0x0000000000000048>  0x000000c000002ea0 
0x00007f317fffe128:  0x000000000040bf13 <runtime.unlock2+0x0000000000000033>  0x0000000000633788 
0x00007f317fffe138:  0x00007f317fffe198  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f317fffe148:  0x000000c000002ea0  0x00007f317fffe188 
0x00007f317fffe158:  0x0000000000432cd0 <runtime.fatalthrow+0x0000000000000050>  0x00007f317fffe168 
0x00007f317fffe168:  0x0000000000432d00 <runtime.fatalthrow.func1+0x0000000000000000>  0x000000c000002ea0 
0x00007f317fffe178:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f317fffe198 
0x00007f317fffe188:  0x00007f317fffe1b8  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f317fffe198:  0x00007f317fffe1a0  0x0000000000432ac0 <runtime.throw.func1+0x0000000000000000> 
0x00007f317fffe1a8:  0x0000000000542054  0x000000000000002a 
0x00007f317fffe1b8:  0x00007f317fffe210  0x0000000000446956 <runtime.sigpanic+0x0000000000000416> 
0x00007f317fffe1c8: <0x0000000000542054  0x0000000000000000 
0x00007f317fffe1d8:  0x0000000000000000  0x0000000000000000 
0x00007f317fffe1e8:  0x0000000000000000  0x0000000000000000 
0x00007f317fffe1f8:  0x0000000000000000  0x0000000000000000 
0x00007f317fffe208:  0x0000000000000000  0x00000000000207d0 
0x00007f317fffe218: !0x00007f3185d9074a >0x0000000000000000 
0x00007f317fffe228:  0x0000000000000000  0x00007f317fffef68 
0x00007f317fffe238:  0x00007f3185d9c200  0x0000000000000007 
0x00007f317fffe248:  0x0000000000000000  0x000000c00006e000 
0x00007f317fffe258:  0x0000000000000000  0x0000000000008000 
0x00007f317fffe268:  0x0000000000000000  0x0000000000000000 
0x00007f317fffe278:  0x0000000000000000  0x0000000000000286 
0x00007f317fffe288:  0x0000000000000002  0x0000000000000000 
0x00007f317fffe298:  0x000000c000002ea0  0x00007f3187f7611a 
0x00007f317fffe2a8:  0x000000c000052948  0x0000000000000080 
0x00007f317fffe2b8:  0x00007f317fffef68  0x0000000000000000 
0x00007f317fffe2c8:  0x0000000000000000  0x00000000000000ca 
0x00007f317fffe2d8:  0x000000000045f863 <runtime.futex+0x0000000000000023>  0x00007f317fffef20 
0x00007f317fffe2e8:  0x000000000045f861 <runtime.futex+0x0000000000000021>  0x0000000000000286 
0x00007f317fffe2f8:  0x002b000000000033  0x0000000000000000 
0x00007f317fffe308:  0x0000000000000000  0x0000000000000000 
0x00007f317fffe318:  0x0000000000000000 
runtime.sigpanic()
	/usr/lib/google-golang/src/runtime/signal_unix.go:804 +0x416
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x7f3185d9074a]

runtime stack:
runtime: unexpected return pc for runtime.sigpanic called from 0x7f3185d9074a
stack: frame={sp:0x7f318764b288, fp:0x7f318764b2e0} stack=[0x7f3186e4c558,0x7f318764c158)
0x00007f318764b188:  0x01007f318764b1a8  0x0000000000000004 
0x00007f318764b198:  0x000000000000001f  0x00007f3185d9074a 
0x00007f318764b1a8:  0x0000000000000010  0x0000000000000001 
0x00007f318764b1b8:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f318764b258 
0x00007f318764b1c8:  0x0000000000540308  0x00007f318764b210 
0x00007f318764b1d8:  0x0000000000432d48 <runtime.fatalthrow.func1+0x0000000000000048>  0x000000c0000031e0 
0x00007f318764b1e8:  0x000000000040bf13 <runtime.unlock2+0x0000000000000033>  0x0000000000633788 
0x00007f318764b1f8:  0x00007f318764b258  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f318764b208:  0x000000c0000031e0  0x00007f318764b248 
0x00007f318764b218:  0x0000000000432cd0 <runtime.fatalthrow+0x0000000000000050>  0x00007f318764b228 
0x00007f318764b228:  0x0000000000432d00 <runtime.fatalthrow.func1+0x0000000000000000>  0x000000c0000031e0 
0x00007f318764b238:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f318764b258 
0x00007f318764b248:  0x00007f318764b278  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f318764b258:  0x00007f318764b260  0x0000000000432ac0 <runtime.throw.func1+0x0000000000000000> 
0x00007f318764b268:  0x0000000000542054  0x000000000000002a 
0x00007f318764b278:  0x00007f318764b2d0  0x0000000000446956 <runtime.sigpanic+0x0000000000000416> 
0x00007f318764b288: <0x0000000000542054  0x0000000000000000 
0x00007f318764b298:  0x0000000000000000  0x0000000000000000 
0x00007f318764b2a8:  0x0000000000000000  0x0000000000000000 
0x00007f318764b2b8:  0x0000000000000000  0x0000000000000000 
0x00007f318764b2c8:  0x0000000000000000  0x00000000000207d0 
0x00007f318764b2d8: !0x00007f3185d9074a >0x0000000000000000 
0x00007f318764b2e8:  0x0000000000000000  0x00007f318764c030 
0x00007f318764b2f8:  0x00007f3185d9c200  0x0000000000000007 
0x00007f318764b308:  0x0000000000000000  0x000000c000076000 
0x00007f318764b318:  0x0000000000000000  0x0000000000008000 
0x00007f318764b328:  0x0000000000000000  0x0000000000000000 
0x00007f318764b338:  0x0000000000000000  0x0000000000000286 
0x00007f318764b348:  0x000000000043bb60 <runtime.park_m+0x0000000000000000>  0x0000000000000030 
0x00007f318764b358:  0x000000c0000031e0  0x00007f3187f72102 
0x00007f318764b368:  0x000000c000052d48  0x0000000000000080 
0x00007f318764b378:  0x00007f318764c030  0x0000000000000000 
0x00007f318764b388:  0x0000000000000000  0x00000000000000ca 
0x00007f318764b398:  0x000000000045f863 <runtime.futex+0x0000000000000023>  0x00007f318764bfe8 
0x00007f318764b3a8:  0x000000000045f861 <runtime.futex+0x0000000000000021>  0x0000000000000286 
0x00007f318764b3b8:  0x002b000000000033  0x0000000000000000 
0x00007f318764b3c8:  0x0000000000000000  0x0000000000000000 
0x00007f318764b3d8:  0x0000000000000000 
runtime.throw({0x542054?, 0x0?})
	/usr/lib/google-golang/src/runtime/panic.go:998 +0x71
runtime: unexpected return pc for runtime.sigpanic called from 0x7f3185d9074a
stack: frame={sp:0x7f318764b288, fp:0x7f318764b2e0} stack=[0x7f3186e4c558,0x7f318764c158)
0x00007f318764b188:  0x01007f318764b1a8  0x0000000000000004 
0x00007f318764b198:  0x000000000000001f  0x00007f3185d9074a 
0x00007f318764b1a8:  0x0000000000000010  0x0000000000000001 
0x00007f318764b1b8:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f318764b258 
0x00007f318764b1c8:  0x0000000000540308  0x00007f318764b210 
0x00007f318764b1d8:  0x0000000000432d48 <runtime.fatalthrow.func1+0x0000000000000048>  0x000000c0000031e0 
0x00007f318764b1e8:  0x000000000040bf13 <runtime.unlock2+0x0000000000000033>  0x0000000000633788 
0x00007f318764b1f8:  0x00007f318764b258  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f318764b208:  0x000000c0000031e0  0x00007f318764b248 
0x00007f318764b218:  0x0000000000432cd0 <runtime.fatalthrow+0x0000000000000050>  0x00007f318764b228 
0x00007f318764b228:  0x0000000000432d00 <runtime.fatalthrow.func1+0x0000000000000000>  0x000000c0000031e0 
0x00007f318764b238:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f318764b258 
0x00007f318764b248:  0x00007f318764b278  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f318764b258:  0x00007f318764b260  0x0000000000432ac0 <runtime.throw.func1+0x0000000000000000> 
0x00007f318764b268:  0x0000000000542054  0x000000000000002a 
0x00007f318764b278:  0x00007f318764b2d0  0x0000000000446956 <runtime.sigpanic+0x0000000000000416> 
0x00007f318764b288: <0x0000000000542054  0x0000000000000000 
0x00007f318764b298:  0x0000000000000000  0x0000000000000000 
0x00007f318764b2a8:  0x0000000000000000  0x0000000000000000 
0x00007f318764b2b8:  0x0000000000000000  0x0000000000000000 
0x00007f318764b2c8:  0x0000000000000000  0x00000000000207d0 
0x00007f318764b2d8: !0x00007f3185d9074a >0x0000000000000000 
0x00007f318764b2e8:  0x0000000000000000  0x00007f318764c030 
0x00007f318764b2f8:  0x00007f3185d9c200  0x0000000000000007 
0x00007f318764b308:  0x0000000000000000  0x000000c000076000 
0x00007f318764b318:  0x0000000000000000  0x0000000000008000 
0x00007f318764b328:  0x0000000000000000  0x0000000000000000 
0x00007f318764b338:  0x0000000000000000  0x0000000000000286 
0x00007f318764b348:  0x000000000043bb60 <runtime.park_m+0x0000000000000000>  0x0000000000000030 
0x00007f318764b358:  0x000000c0000031e0  0x00007f3187f72102 
0x00007f318764b368:  0x000000c000052d48  0x0000000000000080 
0x00007f318764b378:  0x00007f318764c030  0x0000000000000000 
0x00007f318764b388:  0x0000000000000000  0x00000000000000ca 
0x00007f318764b398:  0x000000000045f863 <runtime.futex+0x0000000000000023>  0x00007f318764bfe8 
0x00007f318764b3a8:  0x000000000045f861 <runtime.futex+0x0000000000000021>  0x0000000000000286 
0x00007f318764b3b8:  0x002b000000000033  0x0000000000000000 
0x00007f318764b3c8:  0x0000000000000000  0x0000000000000000 
0x00007f318764b3d8:  0x0000000000000000 
runtime.sigpanic()
	/usr/lib/google-golang/src/runtime/signal_unix.go:804 +0x416
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x7f3185d9074a]

runtime stack:
runtime: unexpected return pc for runtime.sigpanic called from 0x7f3185d9074a
stack: frame={sp:0x7f3187e4c348, fp:0x7f3187e4c3a0} stack=[0x7f318764d558,0x7f3187e4d158)
0x00007f3187e4c248:  0x01007f3187e4c268  0x0000000000000004 
0x00007f3187e4c258:  0x000000000000001f  0x00007f3185d9074a 
0x00007f3187e4c268:  0x0000000000000010  0x0000000000000001 
0x00007f3187e4c278:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3187e4c318 
0x00007f3187e4c288:  0x0000000000540308  0x00007f3187e4c2d0 
0x00007f3187e4c298:  0x0000000000432d48 <runtime.fatalthrow.func1+0x0000000000000048>  0x000000c0000029c0 
0x00007f3187e4c2a8:  0x000000000040bf13 <runtime.unlock2+0x0000000000000033>  0x0000000000633788 
0x00007f3187e4c2b8:  0x00007f3187e4c318  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3187e4c2c8:  0x000000c0000029c0  0x00007f3187e4c308 
0x00007f3187e4c2d8:  0x0000000000432cd0 <runtime.fatalthrow+0x0000000000000050>  0x00007f3187e4c2e8 
0x00007f3187e4c2e8:  0x0000000000432d00 <runtime.fatalthrow.func1+0x0000000000000000>  0x000000c0000029c0 
0x00007f3187e4c2f8:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3187e4c318 
0x00007f3187e4c308:  0x00007f3187e4c338  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3187e4c318:  0x00007f3187e4c320  0x0000000000432ac0 <runtime.throw.func1+0x0000000000000000> 
0x00007f3187e4c328:  0x0000000000542054  0x000000000000002a 
0x00007f3187e4c338:  0x00007f3187e4c390  0x0000000000446956 <runtime.sigpanic+0x0000000000000416> 
0x00007f3187e4c348: <0x0000000000542054  0x0000000000000000 
0x00007f3187e4c358:  0x0000000000000000  0x0000000000000000 
0x00007f3187e4c368:  0x0000000000000000  0x0000000000000000 
0x00007f3187e4c378:  0x0000000000000000  0x0000000000000000 
0x00007f3187e4c388:  0x0000000000000000  0x00000000000207d0 
0x00007f3187e4c398: !0x00007f3185d9074a >0x0000000000000000 
0x00007f3187e4c3a8:  0x0000000000000014  0x00007f3187e4d0a8 
0x00007f3187e4c3b8:  0x00007f3185d9c200  0x0000000000000007 
0x00007f3187e4c3c8:  0x0000000000000000  0x000000c000064000 
0x00007f3187e4c3d8:  0x0000000000000000  0x0000000000008000 
0x00007f3187e4c3e8:  0x000000000000000b  0x0000000000000000 
0x00007f3187e4c3f8:  0x0000000000000002  0x0000000000000202 
0x00007f3187e4c408:  0x00007f3187e4d098  0x00007fff0dbcd37f 
0x00007f3187e4c418:  0x000000c0000029c0  0x00007f3187e4d640 
0x00007f3187e4c428:  0x00007f3187e4d098  0x0000000000000000 
0x00007f3187e4c438:  0x00007f3187e4d0a8  0x0000000000000014 
0x00007f3187e4c448:  0x0000000000000000  0xfffffffffffffffc 
0x00007f3187e4c458:  0x000000000045f27d <runtime.usleep+0x000000000000003d>  0x00007f3187e4d098 
0x00007f3187e4c468:  0x000000000045f27d <runtime.usleep+0x000000000000003d>  0x0000000000000202 
0x00007f3187e4c478:  0x002b000000000033  0x0000000000000000 
0x00007f3187e4c488:  0x0000000000000000  0x0000000000000000 
0x00007f3187e4c498:  0x0000000000000000 
runtime.throw({0x542054?, 0x0?})
	/usr/lib/google-golang/src/runtime/panic.go:998 +0x71
runtime: unexpected return pc for runtime.sigpanic called from 0x7f3185d9074a
stack: frame={sp:0x7f3187e4c348, fp:0x7f3187e4c3a0} stack=[0x7f318764d558,0x7f3187e4d158)
0x00007f3187e4c248:  0x01007f3187e4c268  0x0000000000000004 
0x00007f3187e4c258:  0x000000000000001f  0x00007f3185d9074a 
0x00007f3187e4c268:  0x0000000000000010  0x0000000000000001 
0x00007f3187e4c278:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3187e4c318 
0x00007f3187e4c288:  0x0000000000540308  0x00007f3187e4c2d0 
0x00007f3187e4c298:  0x0000000000432d48 <runtime.fatalthrow.func1+0x0000000000000048>  0x000000c0000029c0 
0x00007f3187e4c2a8:  0x000000000040bf13 <runtime.unlock2+0x0000000000000033>  0x0000000000633788 
0x00007f3187e4c2b8:  0x00007f3187e4c318  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3187e4c2c8:  0x000000c0000029c0  0x00007f3187e4c308 
0x00007f3187e4c2d8:  0x0000000000432cd0 <runtime.fatalthrow+0x0000000000000050>  0x00007f3187e4c2e8 
0x00007f3187e4c2e8:  0x0000000000432d00 <runtime.fatalthrow.func1+0x0000000000000000>  0x000000c0000029c0 
0x00007f3187e4c2f8:  0x0000000000432a91 <runtime.throw+0x0000000000000071>  0x00007f3187e4c318 
0x00007f3187e4c308:  0x00007f3187e4c338  0x0000000000432a91 <runtime.throw+0x0000000000000071> 
0x00007f3187e4c318:  0x00007f3187e4c320  0x0000000000432ac0 <runtime.throw.func1+0x0000000000000000> 
0x00007f3187e4c328:  0x0000000000542054  0x000000000000002a 
0x00007f3187e4c338:  0x00007f3187e4c390  0x0000000000446956 <runtime.sigpanic+0x0000000000000416> 
0x00007f3187e4c348: <0x0000000000542054  0x0000000000000000 
0x00007f3187e4c358:  0x0000000000000000  0x0000000000000000 
0x00007f3187e4c368:  0x0000000000000000  0x0000000000000000 
0x00007f3187e4c378:  0x0000000000000000  0x0000000000000000 
0x00007f3187e4c388:  0x0000000000000000  0x00000000000207d0 
0x00007f3187e4c398: !0x00007f3185d9074a >0x0000000000000000 
0x00007f3187e4c3a8:  0x0000000000000014  0x00007f3187e4d0a8 
0x00007f3187e4c3b8:  0x00007f3185d9c200  0x0000000000000007 
0x00007f3187e4c3c8:  0x0000000000000000  0x000000c000064000 
0x00007f3187e4c3d8:  0x0000000000000000  0x0000000000008000 
0x00007f3187e4c3e8:  0x000000000000000b  0x0000000000000000 
0x00007f3187e4c3f8:  0x0000000000000002  0x0000000000000202 
0x00007f3187e4c408:  0x00007f3187e4d098  0x00007fff0dbcd37f 
0x00007f3187e4c418:  0x000000c0000029c0  0x00007f3187e4d640 
0x00007f3187e4c428:  0x00007f3187e4d098  0x0000000000000000 
0x00007f3187e4c438:  0x00007f3187e4d0a8  0x0000000000000014 
0x00007f3187e4c448:  0x0000000000000000  0xfffffffffffffffc 
0x00007f3187e4c458:  0x000000000045f27d <runtime.usleep+0x000000000000003d>  0x00007f3187e4d098 
0x00007f3187e4c468:  0x000000000045f27d <runtime.usleep+0x000000000000003d>  0x0000000000000202 
0x00007f3187e4c478:  0x002b000000000033  0x0000000000000000 
0x00007f3187e4c488:  0x0000000000000000  0x0000000000000000 
0x00007f3187e4c498:  0x0000000000000000 
runtime.sigpanic()
	/usr/lib/google-golang/src/runtime/signal_unix.go:804 +0x416
exit status 2

@ericlagergren
Copy link
Contributor

ericlagergren commented Apr 4, 2022

I can reproduce it on one machine (ARMv8) but not on another (x86).

ARMv8

$ go version && cc --version && uname -r && lsb_release -r
go version go1.18 linux/arm64
cc (Debian 10.2.1-6) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

5.15.25-rockchip64
Release:	11

x86

$ go version && cc --version && uname -r && lsb_release -r
go version go1.18 linux/amd64
cc (Ubuntu 11.2.0-7ubuntu2) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

5.13.0-39-generic
Release:	21.10

No Repro (x86)

$ cat foo.c
#include <pthread.h>
#include <stdlib.h>

pthread_mutex_t *lock_new(void)
{
    pthread_mutex_t *lock = malloc(sizeof(pthread_mutex_t));
    pthread_mutex_lock(lock);
    return lock;
}
$ cat main.go
package main

/*
#include <dlfcn.h>
#include <unistd.h>
#cgo LDFLAGS: -ldl -static
*/
import "C"
import "fmt"

func main() {
	fmt.Println("Step 1")
	if C.dlopen(C.CString("./foo.so"), C.RTLD_NOW) == nil {
		panic("library not found")
	}
	fmt.Println("Step 2")
	C.setgid(0)
	fmt.Println("Step 3")
}
$ gcc -fPIC -c foo.c && gcc -shared -o foo.so -pthread foo.o && go build -v -o foo && ./foo
# example.com/foo
/usr/bin/ld: /tmp/go-link-2955983748/000001.o: in function `_cgo_b0e9e567cb8b_Cfunc_dlopen':
/tmp/go-build/cgo-gcc-prolog:54: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
Step 1
Step 2
Step 3
$ echo $?
0

@AndrewGMorgan
Copy link
Contributor

What version of glibc are you using? I don't observe a crash or hang with go at HEAD on Fedora 35 with glibc-2.34-29.fc35.x86_64.

@qmuntal
Copy link
Contributor Author

qmuntal commented Apr 5, 2022

What version of glibc are you using? I don't observe a crash or hang with go at HEAD on Fedora 35 with glibc-2.34-29.fc35.x86_64.

$ ldd --version
ldd (Ubuntu GLIBC 2.31-0ubuntu9.7) 2.31
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal

@AndrewGMorgan
Copy link
Contributor

Thanks.

Can you confirm if this issue is new with go1.18 ?

I've not been able to reproduce it myself, but I'm wondering if you see it occurs with go1.17.8. You can go get go1.17.8 ; ~/go/bin/go1.17.8 download and ~/go/bin/go1.17.8 run . to verify this.

@AndrewGMorgan
Copy link
Contributor

I was looking at https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#Link-Options it says:

-pthread
    Link with the POSIX threads library. This option is supported on GNU/Linux targets, most other
    Unix derivatives, and also on x86 Cygwin and MinGW targets. On some targets this option also sets
    flags for the preprocessor, **so it should be used consistently for both compilation and linking**.

So, if you recompile foo.c with:

$ gcc -fPIC -pthread -c foo.c

does that change anything?

@dagood
Copy link
Contributor

dagood commented Apr 11, 2022

This reproduces for me with 1.18 and 1.17.8 in Debian Bullseye (latest), with ldd (Debian GLIBC 2.31-13+deb11u3) 2.31. Adding -pthread to compilation doesn't seem to affect it. I'm running this in Docker containers on a Fedora 35 host. All x86-64.

I used these Dockerfiles and context with docker build -f {dockerfile} . to reproduce the original and try out those changes: https://gist.github.com/dagood/05200cc37be277448928e1d920b445d5

(It doesn't repro for me either on Fedora 35 with glibc 2.34, for what it's worth.)

@AndrewGMorgan
Copy link
Contributor

That makes sense since the CGo mechanism for handling syscall.Setgid() should be in effect and nothing about the syscall.AllThreadsSyscall() mechanism (substantially changed in go1.18 vs go1.17) should be in play.

The observation that compilation with -static, in the C preamble of the .go file, might be important. I found this bug that seems to be saying that something about the dlopen mechanism is sensitive to the right dynamic symbols being available: https://sourceware.org/bugzilla/show_bug.cgi?id=16628 . In general, this kind of linkage and runtime behavior looks pretty subtle.

@AndrewGMorgan
Copy link
Contributor

Curious. As per the native test, the Dockerfile example works for me.

@dagood
Copy link
Contributor

dagood commented Apr 13, 2022

Hmm. After upgrading my Fedora 35 host's packages, the Docker repro finishes all the steps without hanging. It looks like my upgrade was pretty significant (kernel version, Docker version, and more), and I'm not sure what might be relevant, or if it's even directly related. Downgrading Docker and my kernel (5.16.18-200.fc35 -> 5.14.18-100.fc33) didn't make it start happening again, but I'm not completely sure I did it properly. Maybe it never repros on a properly maintained Fedora 35 installation. 😅

On a fresh Ubuntu Desktop Minimal 20.04 VM, the hang repros for me with the 1.18 buildpack-deps:bullseye-scm Docker example. (This is easy to repro from a fresh VM: just sudo apt install docker.io and sudo docker build ..)

$ uname -a
Linux dagood-Virtual-Machine 5.13.0-39-generic #44~20.04.1-Ubuntu SMP Thu Mar 24 16:43:35 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ apt show libc6
Package: libc6
Version: 2.31-0ubuntu9.7

A new Dockerfile I wrote that uses fedora:35 as the base tag makes the example pass all steps on my Ubuntu 20.04 host!

buildpack-deps:bullseye-scm has glibc 2.31
fedora:35 has glibc 2.34

My guess with this info would be that the kernel version and glibc versions both have to be "old" to reproduce the issue. (Docker containers sharing the kernel with the host could be why trying to repro this way is tricky.)

@cherrymui
Copy link
Member

cherrymui commented Jun 17, 2022

This C program seg faults on my machine if linked with -static:

#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>

void* f(void*) { 
	void *p;

	printf("Step 1\n");
	p = dlopen("./foo.so", RTLD_NOW);
	printf("%p\n", p);
	printf("Step 2\n");
	setgid(0);
	printf("Step 3\n");
}

int main() {
	pthread_t t;

	pthread_create(&t, 0, f, 0);
	pthread_join(t, 0);
	return 0;
}

(foo.so is the same as original.)

$ cc x.c -ldl -pthread -static
/usr/bin/ld: /tmp/ccI3WzJ1.o: in function `f':
x.c:(.text+0x2b): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
$ ./a.out 
Step 1
0x7f02e8000fc0
Step 2
Segmentation fault

It seems to die in the SIGSETXID handler

(gdb) bt
#0  sighandler_setxid (sig=33, si=0x7fffffffd4b0, ctx=<optimized out>) at nptl-init.c:190
#1  sighandler_setxid (sig=<optimized out>, si=0x7fffffffd4b0, ctx=<optimized out>) at nptl-init.c:177
#2  <signal handler called>
#3  0x00000000004063e8 in __futex_abstimed_wait_common64 (futex_word=futex_word@entry=0x7ffff7ff8910, expected=2128007, clockid=clockid@entry=0, abstime=abstime@entry=0x0, private=private@entry=128, 
    cancel=cancel@entry=true) at ../sysdeps/nptl/futex-internal.c:74
#4  0x000000000040644b in __futex_abstimed_wait_cancelable64 (futex_word=futex_word@entry=0x7ffff7ff8910, expected=<optimized out>, clockid=clockid@entry=0, abstime=abstime@entry=0x0, 
    private=private@entry=128) at ../sysdeps/nptl/futex-internal.c:123
#5  0x0000000000403d74 in __pthread_clockjoin_ex (threadid=140737354106432, thread_return=0x0, clockid=0, abstime=0x0, block=<optimized out>) at pthread_join_common.c:102
#6  0x0000000000401904 in main ()

It doesn't crash if it is not linked statically, nor if the setgid call is not made on a non-main thread (it doesn't seem to matter where dlopen call is made).

This is a linux/amd64 machine with glibc 2.33.

I'm leaning to think that this is probably a bug in the C library.

@ianlancetaylor
Copy link
Contributor

Rolling forward to 1.20. Please comment if you disagree. Thanks.

@ianlancetaylor ianlancetaylor modified the milestones: Go1.19, Go1.20 Jun 24, 2022
@cherrymui
Copy link
Member

I'm not sure if this is a Go bug. At least for the case on my machine, it is reproducible in C. From the comments above it might be glibc version dependent. Maybe we can close it? Or we're waiting for more information? Thanks.

@qmuntal
Copy link
Contributor Author

qmuntal commented Jun 24, 2022

It does look like a C bug, and I don't have more info to share here. For me we can close it.

@ianlancetaylor
Copy link
Contributor

Thanks, closing.

@golang golang locked and limited conversation to collaborators Jun 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Development

No branches or pull requests

8 participants