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

cmd/go: Windows -buildmode=pie fails to execute program that has CGo bindings compiled without PIE #41329

Open
schollz opened this issue Sep 10, 2020 · 6 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@schollz
Copy link

schollz commented Sep 10, 2020

It is mentioned in #35192 that people might be interested in weird bugs that pop up using -buildmode=pie by default so I submitted this.

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

$ go version
go version go1.15.2 windows/amd64

Does this issue reproduce with the latest release?

Yes, because -buildmode=pie is default for Windows in go1.15. The issue does not present if -buildmode=exe is used.

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

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\zacks\AppData\Local\go-build
set GOENV=C:\Users\zacks\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\zacks\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\zacks\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\zacks\scoop\apps\go\current
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Users\zacks\scoop\apps\go\current\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\zacks\go\src\github.com\schollz\miti\go.mod
set CGO_CFLAGS=-IC:\msys64\mingw64\include
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-LC:\msys64\mingw64\lib
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\zacks\AppData\Local\Temp\go-build521225298=/tmp/go-build -gno-record-gcc-switches

What did you do?

First I installed portmidi for Windows with these instructions. Then I cloned and ran a test with go1.15.2:

git clone https://github.com/schollz/miti
cd miti/src/midi
$env:CGO_CFLAGS="-IC:\msys64\mingw64\include"
$env:CGO_LDFLAGS="-LC:\msys64\mingw64\lib"
go test -v

What did you expect to see?

I expected to see that the default go test works without any extra build flags, as it did in go1.14.9:

> go1.14.9 test -v
=== RUN   TestMIDI
[debug] 10:14:34 midi.go:38: found 2 devices
[debug] 10:14:34 midi.go:44: device 0: 'Microsoft MIDI Mapper', i/o: false/true
[debug] 10:14:34 midi.go:44: device 1: 'Microsoft GS Wavetable Synth', i/o: false/true
[debug] 10:14:34 midi.go:58: [Microsoft MIDI Mapper] opening stream with latency 2000
--- PASS: TestMIDI (0.02s)
=== RUN   TestIn
[debug] 10:14:34 midiin.go:27: found 0 devices
[trace] 10:14:34.012062 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft MIDI Mapper IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace] 10:14:34.012906 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft GS Wavetable Synth IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace] 10:14:34.013423 midiin.go:33: skipping Microsoft GS Wavetable Synth
[debug] 10:14:35 midi.go:63: [Microsoft MIDI Mapper] opened stream
[trace] 10:14:35.014415 midi.go:68: [Microsoft MIDI Mapper] waiting for chord
[debug] 10:14:36 midi_test.go:31: sending finish
[debug] 10:14:36 midi_test.go:33: sent
--- PASS: TestIn (2.10s)
PASS
ok      github.com/schollz/miti/src/midi        2.640s

What did you see instead?

Instead I got this runtime error when using go1.15.2

> go test -v
=== RUN   TestMIDI
[debug]	10:16:53 midi.go:38: found 2 devices
[debug]	10:16:53 midi.go:44: device 0: 'Microsoft MIDI Mapper', i/o: false/true
[debug]	10:16:53 midi.go:44: device 1: 'Microsoft GS Wavetable Synth', i/o: false/true
--- PASS: TestMIDI (0.02s)
=== RUN   TestIn
[debug]	10:16:53 midi.go:58: [Microsoft MIDI Mapper] opening stream with latency 2000
[debug]	10:16:53 midiin.go:27: found 0 devices
[trace]	10:16:53.218101 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft MIDI Mapper IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace]	10:16:53.218101 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft GS Wavetable Synth IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace]	10:16:53.218101 midiin.go:33: skipping Microsoft GS Wavetable Synth
Exception 0xc0000005 0x0 0xc05b8438 0x70c4198e
PC=0x70c4198e

runtime: unknown pc 0x70c4198e
stack: frame={sp:0x298c3ff3f0, fp:0x0} stack=[0x0,0x298c3ff740)
000000298c3ff2f0:  00007ffc78a57070  00007ffc78a555ff 
000000298c3ff300:  0000000000000000  00000000000003c7 
000000298c3ff310:  0000000000000000  00007ffcaf5295f1 
000000298c3ff320:  0000000000000000  0000000000030002 
000000298c3ff330:  0000000000000000  00007ffca80833fe 
000000298c3ff340:  0000016ac0272080  0000000000030002 
000000298c3ff350:  0000016ac020eef0  000000000000002b 
000000298c3ff360:  0000000000000000  0000016ac026f860 
000000298c3ff370:  0000000000000000  00000000000000bf 
000000298c3ff380:  0000016ac0150000  007200630069004d 
000000298c3ff390:  0066006f0073006f  0049004d00200074 
000000298c3ff3a0:  004d002000490044  0065007000700061 
000000298c3ff3b0:  0000000000400000 <sort.stable+128>  0000000000000001 
000000298c3ff3c0:  00007ffc78a6b200  00007ffc78a70048 
000000298c3ff3d0:  0000016ac0270370  00007ffcaf525d21 
000000298c3ff3e0:  00007ffc78a70048  0000016ac01f0000 
000000298c3ff3f0: <0000016ac0268760  0000000000000000 
000000298c3ff400:  0000000000000000  00007ffc78a63a02 
000000298c3ff410:  0000000000000000  00007ffc78a555ff 
000000298c3ff420:  0000016ac0270370  0000000000000000 
000000298c3ff430:  0000016ac02686f0  0000000000000001 
000000298c3ff440:  0000000000000000  00007ffc78a71101 
000000298c3ff450:  0000016ac0268760  00007ffc78a56703 
000000298c3ff460:  0000016ac02686f0  0000016ac0268760 
000000298c3ff470:  0000000000000000  0000016ac0268710 
000000298c3ff480:  0000016ac05b83e0  0000000000000000 
000000298c3ff490:  0000000000000000  0000000000000078 
000000298c3ff4a0:  0000000000000000  0000016ac05b8460 
000000298c3ff4b0:  000000298c3ff59c  00007ffc00000000 
000000298c3ff4c0:  0000016ac0270370  0000016ac0223d40 
000000298c3ff4d0:  0000050000010001  666f736f7263694d 
000000298c3ff4e0:  4d204944494d2074  0000007265707061 
runtime: unknown pc 0x70c4198e
stack: frame={sp:0x298c3ff3f0, fp:0x0} stack=[0x0,0x298c3ff740)
000000298c3ff2f0:  00007ffc78a57070  00007ffc78a555ff 
000000298c3ff300:  0000000000000000  00000000000003c7 
000000298c3ff310:  0000000000000000  00007ffcaf5295f1 
000000298c3ff320:  0000000000000000  0000000000030002 
000000298c3ff330:  0000000000000000  00007ffca80833fe 
000000298c3ff340:  0000016ac0272080  0000000000030002 
000000298c3ff350:  0000016ac020eef0  000000000000002b 
000000298c3ff360:  0000000000000000  0000016ac026f860 
000000298c3ff370:  0000000000000000  00000000000000bf 
000000298c3ff380:  0000016ac0150000  007200630069004d 
000000298c3ff390:  0066006f0073006f  0049004d00200074 
000000298c3ff3a0:  004d002000490044  0065007000700061 
000000298c3ff3b0:  0000000000400000 <sort.stable+128>  0000000000000001 
000000298c3ff3c0:  00007ffc78a6b200  00007ffc78a70048 
000000298c3ff3d0:  0000016ac0270370  00007ffcaf525d21 
000000298c3ff3e0:  00007ffc78a70048  0000016ac01f0000 
000000298c3ff3f0: <0000016ac0268760  0000000000000000 
000000298c3ff400:  0000000000000000  00007ffc78a63a02 
000000298c3ff410:  0000000000000000  00007ffc78a555ff 
000000298c3ff420:  0000016ac0270370  0000000000000000 
000000298c3ff430:  0000016ac02686f0  0000000000000001 
000000298c3ff440:  0000000000000000  00007ffc78a71101 
000000298c3ff450:  0000016ac0268760  00007ffc78a56703 
000000298c3ff460:  0000016ac02686f0  0000016ac0268760 
000000298c3ff470:  0000000000000000  0000016ac0268710 
000000298c3ff480:  0000016ac05b83e0  0000000000000000 
000000298c3ff490:  0000000000000000  0000000000000078 
000000298c3ff4a0:  0000000000000000  0000016ac05b8460 
000000298c3ff4b0:  000000298c3ff59c  00007ffc00000000 
000000298c3ff4c0:  0000016ac0270370  0000016ac0223d40 
000000298c3ff4d0:  0000050000010001  666f736f7263694d 
000000298c3ff4e0:  4d204944494d2074  0000007265707061 

goroutine 7 [syscall]:
github.com/schollz/miti/src/portmidi._Cfunc_Pm_OpenOutput(0xc000090000, 0xc000000000, 0x0, 0x1000, 0x0, 0x0, 0x7d0, 0xc000000000)
	_cgo_gotypes.go:223 +0x54
github.com/schollz/miti/src/portmidi.NewOutputStream.func1(0xc000090000, 0x0, 0x1000, 0x7d0, 0x0)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/portmidi/stream.go:81 +0x94
github.com/schollz/miti/src/portmidi.NewOutputStream(0x0, 0x1000, 0x7d0, 0xc000043e88, 0x2, 0x2)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/portmidi/stream.go:81 +0x69
github.com/schollz/miti/src/midi.Init.func2(0xc00000e680, 0x15, 0x0, 0x0)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midi.go:59 +0x15d
created by github.com/schollz/miti/src/midi.Init
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midi.go:52 +0x41b

goroutine 1 [chan receive]:
testing.(*T).Run(0xc000033680, 0x4c232e, 0x6, 0x4cd668, 0x3cdc01)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1179 +0x3be
testing.runTests.func1(0xc000033080)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1449 +0x7f
testing.tRunner(0xc000033080, 0xc000077de0)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1127 +0xef
testing.runTests(0xc000004500, 0x47e4c0, 0x2, 0x2, 0xbfceb6574bab4ff4, 0x8bb2e7f035, 0x59d380, 0x34f1d0)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1447 +0x2eb
testing.(*M).Run(0xc00012e000, 0x0)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1357 +0x24e
main.main()
	_testmain.go:45 +0x145

goroutine 8 [sleep]:
time.Sleep(0x77359400)
	C:/Users/zacks/scoop/apps/go/current/src/runtime/time.go:188 +0xc9
github.com/schollz/miti/src/midi.TestIn(0xc000033680)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midi_test.go:30 +0xb3
testing.tRunner(0xc000033680, 0x4cd668)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1127 +0xef
created by testing.(*T).Run
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1178 +0x397

goroutine 9 [chan receive]:
github.com/schollz/miti/src/midi.ReadAll.func1(0xc000006040, 0xc0000124e0)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midiin.go:71 +0x409
created by github.com/schollz/miti/src/midi.ReadAll
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midiin.go:25 +0xdf

goroutine 10 [chan receive]:
github.com/schollz/miti/src/midi.TestIn.func1(0xc00004a120)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midi_test.go:26 +0x75
created by github.com/schollz/miti/src/midi.TestIn
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midi_test.go:24 +0xa6
rax     0x70c41980
rbx     0x0
rcx     0x16ac02686f0
rdi     0x70c41980
rsi     0x16ac02686f0
rbp     0x3c7
rsp     0x298c3ff3f0
r8      0xc05b83e0
r9      0x0
r10     0x8000
r11     0x298c3ff3b0
r12     0x7ffc78a70048
r13     0x7ffc78a6b200
r14     0x1
r15     0x400000
rip     0x70c4198e
rflags  0x10293
cs      0x33
fs      0x53
gs      0x2b
exit status 2
FAIL	github.com/schollz/miti/src/midi	1.479s

The error can be eliminated only when I include the -buildmode=exe flag now:

> go test -v -buildmode=exe
=== RUN   TestMIDI
[debug]	10:17:52 midi.go:38: found 2 devices
[debug]	10:17:52 midi.go:44: device 0: 'Microsoft MIDI Mapper', i/o: false/true
[debug]	10:17:52 midi.go:44: device 1: 'Microsoft GS Wavetable Synth', i/o: false/true
--- PASS: TestMIDI (0.02s)
[debug]	10:17:52 midi.go:58: [Microsoft MIDI Mapper] opening stream with latency 2000
=== RUN   TestIn
[debug]	10:17:52 midiin.go:27: found 0 devices
[trace]	10:17:52.905821 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft MIDI Mapper IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace]	10:17:52.905821 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft GS Wavetable Synth IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace]	10:17:52.905821 midiin.go:33: skipping Microsoft GS Wavetable Synth
[debug]	10:17:53 midi.go:63: [Microsoft MIDI Mapper] opened stream
[trace]	10:17:53.782959 midi.go:68: [Microsoft MIDI Mapper] waiting for chord
[debug]	10:17:54 midi_test.go:31: sending finish
[debug]	10:17:54 midi_test.go:33: sent
--- PASS: TestIn (2.10s)
PASS
ok  	github.com/schollz/miti/src/midi	2.653s
@networkimprov
Copy link

@gopherbot add OS-Windows

cc @alexbrainman @zx2c4

@alexbrainman
Copy link
Member

It is mentioned in #35192 that people might be interested in weird bugs that pop up using -buildmode=pie by default so I submitted this.

@schollz thank you for submitting the issue. Unfortunately I don't have time to debug your issue. It could be C code you use, or gcc flags you use, or could be old / buggy gcc.

-buildmode=pie passes these flags to gcc

https://go-review.googlesource.com/c/go/+/214397/7/src/cmd/link/internal/ld/lib.go

Maybe you can work out why it crashes for you.

It is find to use -buildmode=exe to build your executable, if you don't care for ASLR.

Alex

@schollz
Copy link
Author

schollz commented Sep 13, 2020

Thanks @alexbrainman I'll tinker with those

@toothrot toothrot changed the title Windows -buildmode=pie fails to execute program that has CGo bindings cmd/go: Windows -buildmode=pie fails to execute program that has CGo bindings Sep 14, 2020
@toothrot toothrot added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 14, 2020
@toothrot toothrot added this to the Backlog milestone Sep 14, 2020
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@networkimprov
Copy link

@ianlancetaylor can you reopen this issue?

@ianlancetaylor ianlancetaylor removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 28, 2020
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 28, 2020
@ianlancetaylor ianlancetaylor changed the title cmd/go: Windows -buildmode=pie fails to execute program that has CGo bindings cmd/go: Windows -buildmode=pie fails to execute program that has CGo bindings compiled without PIE Oct 28, 2020
@networkimprov
Copy link

Also raised in #42233

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

6 participants