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

There was a panic when golang built the DLL and then used golang to call the DLL #49184

Closed
bearki opened this issue Oct 27, 2021 · 2 comments
Closed

Comments

@bearki
Copy link

bearki commented Oct 27, 2021

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

$ go version
go version go1.17.1 windows/amd64

Does this issue reproduce with the latest release?

Almost all versions will happen

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

go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Bearki\AppData\Local\go-build
set GOENV=C:\Users\Bearki\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=E:\golang\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=E:\golang
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.1
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\Bearki\Desktop\test\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\Bearki\AppData\Local\Temp\go-build170724005=/tmp/go-build -gno-record-gcc-switches   

What did you do?

export.go
package main

import "C"
import (
"fmt"
_ "runtime"
)

// Init init dll
// @params code int status code
// @return string return message
//export Init
func Init(code int) string {
fmt.Println(code)
return "OK"
}

func main() {}

go build -buildmode=c-shared -o test.dll export.go
test.go
package main

import "C"
import (
"fmt"
"unsafe"

"golang.org/x/sys/windows"

)

func main() {

lib, err := windows.LoadDLL("test.dll")
if err != nil {
	panic(err.Error())
}

defer lib.Release()

f1, err := lib.FindProc("Init")
if err != nil {
	panic(err.Error())
}

res, _, err := f1.Call(uintptr(1001))
fmt.Println(C.GoString((*C.char)(unsafe.Pointer(res))), err)

}

go run test.go

What did you expect to see?

1001
OK nil

What did you see instead?

fatal error: unexpected signal during runtime execution
[signal 0xc0000005 code=0x1 addr=0x0 pc=0x7ffafa25e722]

goroutine 1 [running, locked to thread]:
runtime.throw({0x7ffafa2c6ea9, 0xc00007d830})
        D:/Go/src/runtime/panic.go:1198 +0x76 fp=0xc00007d7f0 sp=0xc00007d7c0 pc=0x7ffafa233f56
runtime.sigpanic()
        D:/Go/src/runtime/signal_windows.go:248 +0x22b fp=0xc00007d838 sp=0xc00007d7f0 pc=0x7ffafa2455cb
runtime.memmove()
        D:/Go/src/runtime/memmove_amd64.s:158 +0x102 fp=0xc00007d840 sp=0xc00007d838 pc=0x7ffafa25e722  
runtime.heapBitsSetType(0x20162dc0108, 0x400, 0x400, 0xc000006028)
        D:/Go/src/runtime/mbitmap.go:1359 +0x5dc fp=0xc00007d978 sp=0xc00007d840 pc=0x7ffafa21361c      
runtime.mallocgc(0x400, 0x7ffafa2b53a0, 0x1)
        D:/Go/src/runtime/malloc.go:1100 +0x65e fp=0xc00007d9f8 sp=0xc00007d978 pc=0x7ffafa20cb9e       
runtime.makeslice(0x0, 0x0, 0xc000074660)
        D:/Go/src/runtime/slice.go:98 +0x52 fp=0xc00007da20 sp=0xc00007d9f8 pc=0x7ffafa2459b2
sync.(*Pool).pinSlow(0x7ffafa2a0300)
        D:/Go/src/sync/pool.go:227 +0x185 fp=0xc00007dab8 sp=0xc00007da20 pc=0x7ffafa264d65
sync.(*Pool).pin(0x7ffafa2a0300)
        D:/Go/src/sync/pool.go:206 +0x4e fp=0xc00007dad0 sp=0xc00007dab8 pc=0x7ffafa264bae
sync.(*Pool).Get(0x7ffafa2a0300)
        D:/Go/src/sync/pool.go:128 +0x25 fp=0xc00007db08 sp=0xc00007dad0 pc=0x7ffafa2648e5
fmt.newPrinter()
        D:/Go/src/fmt/print.go:137 +0x25 fp=0xc00007db30 sp=0xc00007db08 pc=0x7ffafa287da5
fmt.Fprintln({0x7ffafa2e07c0, 0x0}, {0xc00007dba8, 0x1, 0x1})
        D:/Go/src/fmt/print.go:263 +0x36 fp=0xc00007db80 sp=0xc00007db30 pc=0x7ffafa288076
fmt.Println(...)
        D:/Go/src/fmt/print.go:274
main.Init(...)
        C:/Users/Bearki/Desktop/test/main.go:14
_cgoexp_37e84c538feb_Init(0xf32d3ff680)
        _cgo_gotypes.go:38 +0x5f fp=0xc00007dbc8 sp=0xc00007db80 pc=0x7ffafa28d41f
runtime.cgocallbackg1(0x7ffafa28d3c0, 0xc0000749c0, 0x0)
        D:/Go/src/runtime/cgocall.go:306 +0x29a fp=0xc00007dc98 sp=0xc00007dbc8 pc=0x7ffafa203c7a
runtime.cgocallbackg(0xc00003a000, 0x300000002, 0xc00003a000)
        D:/Go/src/runtime/cgocall.go:232 +0x106 fp=0xc00007dd30 sp=0xc00007dc98 pc=0x7ffafa203906
runtime.cgocallbackg(0x7ffafa28d3c0, 0xf32d3ff680, 0x0)
        :1 +0x36 fp=0xc00007dd58 sp=0xc00007dd30 pc=0x7ffafa25f796
runtime: unexpected return pc for runtime.cgocallback called from 0x7ff7c0e0b080
stack: frame={sp:0xc00007dd58, fp:0xc00007dd80} stack=[0xc000076000,0xc00007e000)
0x000000c00007dc58:  0x000000c00007dc88  0x00007ffafa258f45 
0x000000c00007dc68:  0x000000c00003a000  0x000000c00007dc48
0x000000c00007dc78:  0x0000000000000000  0x00007ffafa2c8c88
0x000000c00007dc88:  0x000000c00007dd20  0x00007ffafa203906 
0x000000c00007dc98:  0x00007ffafa28d3c0 <_cgoexp_37e84c538feb_Init+0x0000000000000000>  0x000000c0000749c0 
0x000000c00007dca8:  0x0000000000000000  0x00007ff7c0db398a
0x000000c00007dcb8:  0x0000000000000000  0x00007ffafa28d490
0x000000c00007dcc8:  0x0000000000000001  0x000000c00007de08
0x000000c00007dcd8:  0x00007ffafa28d490  0x0000000000000000
0x000000c00007dce8:  0x0000000000000000  0x000000c00007dd88
0x000000c00007dcf8:  0x000000c00003a000  0x00007ff7c0f21180
0x000000c00007dd08:  0x0000020162dc0108  0x00007ffafa28d3c0 <_cgoexp_37e84c538feb_Init+0x0000000000000000>
0x000000c00007dd18:  0x000000f32d3ff680  0x000000c00007dd48
0x000000c00007dd28:  0x00007ffafa25f796   0x000000c00003a000
0x000000c00007dd38:  0x0000000300000002  0x000000c00003a000
0x000000c00007dd48:  0x000000f32d3ff548  0x00007ffafa25d5b7 
0x000000c00007dd58: <0x00007ffafa28d3c0 <_cgoexp_37e84c538feb_Init+0x0000000000000000>  0x000000f32d3ff680 
0x000000c00007dd68:  0x0000000000000000  0x000000c00007dd88
0x000000c00007dd78: !0x00007ff7c0e0b080 >0x00007ff7c0db39ec
0x000000c00007dd88:  0x00007ff7c0e0ec60  0x00007ff7c0f21460
0x000000c00007dd98:  0x00007ff7c0dbc574  0x00007ff7c0db3a28
0x000000c00007dda8:  0x00007ff7c0f21180  0x000000c00007dde8
0x000000c00007ddb8:  0x00007ff7c0e09c54  0x00007ff7c0e0ec60
0x000000c00007ddc8:  0x00007ff7c0f21460  0x0100000000000010
0x000000c00007ddd8:  0x00007ff7c0f21180  0x00007ff7c0e94cf0
0x000000c00007dde8:  0x000000c00007de38  0x00007ff7c0e0fb0b
0x000000c00007ddf8:  0x00007ffafa28d490  0x0000000000000001
0x000000c00007de18:  0x0000000000000000  0x0000000000000000
0x000000c00007de38:  0x000000c00007def8  0x00007ff7c0e3f266
0x000000c00007de48:  0x00007ff7c0e89b81  0x00007ff7c0dbc8c7
0x000000c00007de58:  0x00007ff7c0dbc574  0x00007ff7c0e7e1c0
0x000000c00007de68:  0x0000000000000001  0x00007ffafa28d490
0x000000c00007de78:  0x00007ff7c0e3ee17
runtime.cgocallback(0x7ff7c0db39ec, 0x7ff7c0e0ec60, 0x7ff7c0f21460)
        D:/Go/src/runtime/asm_amd64.s:915 +0xd7 fp=0xc00007dd80 sp=0xc00007dd58 pc=0x7ffafa25d5b7

goroutine 1 [runnable, locked to thread]:
syscall.Syscall(0x7ffb442c52f0, 0x2, 0x340, 0x1c00007db74, 0x0)
        D:/Go/src/runtime/syscall_windows.go:479 +0xf4
syscall.GetConsoleMode(0x1c000302000, 0x1c00007db74)
        D:/Go/src/syscall/zsyscall_windows.go:692 +0x71
os.newFile(0x340, {0x7ffafa2c00dd, 0xb}, {0x7ffafa2bf2f0, 0x4})
        D:/Go/src/os/file_windows.go:46 +0x56
os.NewFile(...)
        D:/Go/src/os/file_windows.go:84
os.init()
        D:/Go/src/os/file.go:67 +0x249
exit status 2
@antong
Copy link
Contributor

antong commented Oct 27, 2021

I think this may be a duplicate of #22192.

@seankhliao
Copy link
Member

Duplicate of #22192

@seankhliao seankhliao marked this as a duplicate of #22192 Oct 27, 2021
@golang golang locked and limited conversation to collaborators Oct 27, 2022
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

4 participants