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

cgo: segment fault when running proc address getting from LoadLibrary with 32bit mingw-w64 #42471

Closed
zhuah opened this issue Nov 10, 2020 · 0 comments

Comments

@zhuah
Copy link

zhuah commented Nov 10, 2020

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

$ go version
go version go1.14.10 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/zhuah/Library/Caches/go-build"
GOENV="/Users/zhuah/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/zhuah/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/Cellar/go@1.14/1.14.10/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go@1.14/1.14.10/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8n/mb3y_4bj1xv4q9sdmgjqdx_c0000gn/T/go-build145327919=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I'm cross compiling a program on darwin to windows with mingw-w64 8.0, the program succeed when built as 64bit, but crashed when built as 32bit.

CGO_ENABLED=1 CC=i686-w64-mingw32-gcc GOOS=windows GOARCH=386 go build
package main

/*
#include <windows.h>
#include <stdio.h>

typedef BOOL (*IsRectEmptyProc)(RECT *rect);

void Test() {
	HANDLE lib = LoadLibraryA("user32.dll");
	IsRectEmptyProc proc = (IsRectEmptyProc)GetProcAddress(lib,"IsRectEmpty");
	printf("proc address: %d\n", proc);
	fflush(stdout);

	RECT rect = {0};
	BOOL res = proc(&rect);
	FreeLibrary(lib);

	printf("done\n");
	fflush(stdout);
}
*/
import "C"

func main() {
	C.Test()
}

What did you expect to see?

the program should works on both 32bit and 64bit.

What did you see instead?

it crashs on 32bit.

proc address: 1978786512
done
Exception 0xc0000005 0x8 0x1142afc4 0x1142afc4
PC=0x1142afc4

runtime: unknown pc 0x1142afc4
stack: frame={sp:0x7efe14, fp:0x0} stack=[0x0,0x7efe40)
007efd94:  00000000  00000000  00000000  00000000
007efda4:  00000000  00000000  00000000  00000000
007efdb4:  00000000  00000000  00000000  00000000
007efdc4:  00000000  00000000  00000000  00000000
007efdd4:  00000000  00000000  00000000  00000000
007efde4:  00000000  00000000  00000000  00000000
007efdf4:  00000000  00000000  00000000  1142afc4
007efe04:  004d6760  0000006c  0044ebae <runtime.asmcgocall+94>  1142afc4
007efe14: <0000006c  11424000  00000000  00000000
007efe24:  00000005  1142ae3c  0044d853 <runtime.systemstack+83>  0042ecd0 <runtime.mstart+0>
007efe34:  0044d742 <runtime.rt0_go+370>  00000000  0044d749 <runtime.rt0_go+377>
runtime: unknown pc 0x1142afc4
stack: frame={sp:0x7efe14, fp:0x0} stack=[0x0,0x7efe40)
007efd94:  00000000  00000000  00000000  00000000
007efda4:  00000000  00000000  00000000  00000000
007efdb4:  00000000  00000000  00000000  00000000
007efdc4:  00000000  00000000  00000000  00000000
007efdd4:  00000000  00000000  00000000  00000000
007efde4:  00000000  00000000  00000000  00000000
007efdf4:  00000000  00000000  00000000  1142afc4
007efe04:  004d6760  0000006c  0044ebae <runtime.asmcgocall+94>  1142afc4
007efe14: <0000006c  11424000  00000000  00000000
007efe24:  00000005  1142ae3c  0044d853 <runtime.systemstack+83>  0042ecd0 <runtime.mstart+0>
007efe34:  0044d742 <runtime.rt0_go+370>  00000000  0044d749 <runtime.rt0_go+377>

goroutine 1 [syscall]:
main._Cfunc_Test()
        _cgo_gotypes.go:41 +0x33
main.main()
        /Users/zhuah/Projects/playground/win32/main.go:26 +0x17
eax     0x0
ebx     0x4d6760
ecx     0xd491add
edx     0x0
edi     0x44ebae
esi     0x6c
ebp     0x4d6980
esp     0x7efe14
eip     0x1142afc4
eflags  0x10202
cs      0x23
fs      0x53
gs      0x2b

@zhuah zhuah closed this as completed Nov 10, 2020
@golang golang locked and limited conversation to collaborators Nov 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants