golang / go

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

runtime: error error 3221226356 (C0000374) in CGO module in Windows #39127

Closed bsrdjan closed 1 year ago

bsrdjan commented 4 years ago

The CGO module gorfc provides GO bindings to pre-compiled SAP NWRFC SDK binaries. The module works fine on Linux and macOS and never worked quite stable on Windows. Currently, it occasionally terminates on Windows, with heap corruption error 3221226356 (hex C0000374).

I am not sure if the toolchain and compiler flags are maybe part of the problem, here further details.

Toolchain

SAP NWRFC SDK binaries require Microsoft C Runtime DLLs version 12.0 and our Windows toolchain is based on MinGW64, with following settings:

Version: 8.1.0
Architecture: x86_64
Threads: win32
Exception: seh
Build revision: 0

The TDM GCC is not used because this issue still occurs: error adding symbols: File in wrong format

Is this the optimal toolchain for building CGO modules on Windows, or another approach could be considered?

Compiler flags

Compiler flags recommended for SAP NWRFC SDK bindings on Windows platform, are given for Windows native compiler and linker:

cl.exe -DSAPonNT -D_CRT_NON_CONFORMING_SWPRINTFS -D_CRT_SECURE_NO_DEPRECATE 
-D_CRT_NONSTDC_NO_DEPRECATE -D_AFXDLL -DWIN32 -D_WIN32_WINNT=0x0502 -DWIN64 
-D_AMD64_ -DNDEBUG -DSAPwithUNICODE -DUNICODE -D_UNICODE -DSAPwithTHREADS 
-Inwrfcsdk/include -EHs -Gy -J -MD -nologo -W3 -Z7 -D_ATL_ALLOW_CHAR_UNSIGNED -GL 
-O2 -Oy- /we4552 /we4700 /we4789 -c -FosflightClient.obj sflightClient.c

link.exe -OUT:sflightClient.exe -PDB:sflightClient.pdb -NXCOMPAT -STACK:0x2000000 
-SWAPRUN:NET -DEBUG -OPT:REF -DEBUGTYPE:CV,FIXUP -MACHINE:amd64 -nologo 
-LTCG  ole32.lib oleaut32.lib uuid.lib kernel32.lib advapi32.lib user32.lib gdi32.lib winspool.lib ws2_32.lib comdlg32.lib shell32.lib sflightClient.obj nwrfcsdk/lib/sapnwrfc.lib nwrfcsdk/lib/libsapucum.lib

GCC counterparts are currently missing for some of them, commented in gorfc source)

Any help/ideas on toolchain, flags, possible root causes and troubleshooting approach, would be of a great help.

Here the details about GO environment on Windows.

PS C:\src\GO\src\github.com\sap\gorfc>go version
go version go1.14 windows/amd64

PS C:\src\GO\src\github.com\sap\gorfc> go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\d037732\AppData\Local\go-build
set GOENV=C:\Users\d037732\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=c:\src\GO
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Tools\GOLANG
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Tools\GOLANG\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\src\GO\src\github.com\sap\gorfc\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\d037732\AppData\Local\Temp\go-build050373882=/tmp/go-build -gno-record-gcc-switches
ALTree commented 4 years ago

cc @alexbrainman probably knows if this is a supported configuration on windows.

alexbrainman commented 4 years ago

cc @alexbrainman probably knows if this is a supported configuration on windows.

I don't know, if this is a supported configuration or not.

I don't use Cgo in my Windows programs.

Alex

networkimprov commented 4 years ago

cc @ianlancetaylor re cgo.

ianlancetaylor commented 4 years ago

I also don't know, sorry.

But from the problem description I think it's fairly unlikely to be a toolchain or compiler flag problem. Those kind of problems manifest as build failures or immediate crashes. You are describing an occasional crash likely due to memory corruption. That is more likely to be due to some sort of error passing memory between Go and C. The fact that it works reliably on GNU/Linux and macOS does not preclude a problem on Windows, as Windows is of course quite different in many ways.

Sorry I can't help.

mattn commented 4 years ago

@bsrdja Could you provide minimal example to reproduce?

bsrdjan commented 4 years ago

The example itself is simple: https://github.com/SAP/gorfc/blob/master/example/hello_gorfc.go

The challenge could be the setup, requiring SAP ABAP backend system and SAP NWRFC SDK binary libraries, installed on GO client system.

You probably don't have access to any of them and we need to find out how to test?

gopherbot commented 1 year ago

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.)