go: runtime: throw in linked c++ library causes app crash even if caught, on windows platform

when compiling go code using cgo to link a c++ application that uses try/catch/throw logic, the golang app crashes immediately when an exception is thrown. on linux, it performs as expected.

code versions: go version go1.5 windows/amd64 tdm64-gcc-5.1.0-2

the code that demonstrates issue can be found in https://gist.github.com/superbaddude/b52e60f2e8dee0868af2

the output from command on windows:

go test
=== RUN   Test_Callthrow_and_catch_exception
Exception 0x20474343 0x1045e80 0x6161616161616161 0x7fff56cda1c8
PC=0x7fff56cda1c8
signal arrived during external code execution

github.com/superbaddude/testcgo._Cfunc_throw_and_catch_exception()
    github.com/superbaddude/testcgo/_test/_obj_test/_cgo_gotypes.go:37 +0x38
github.com/superbaddude/testcgo.Callthrow_and_catch_exception()
    c:/Code/go/src/github.com/superbaddude/testcgo/throwe.go:9 +0x1b
github.com/superbaddude/testcgo.Test_Callthrow_and_catch_exception(0xc08208e000)
    c:/Code/go/src/github.com/superbaddude/testcgo/throwe_test.go:6 +0x1b
testing.tRunner(0xc08208e000, 0x702d00)
    c:/go/src/testing/testing.go:456 +0x9f
created by testing.RunTests
    c:/go/src/testing/testing.go:561 +0x874

goroutine 1 [chan receive]:
testing.RunTests(0x5e4f28, 0x702d00, 0x1, 0x1, 0x1)
    c:/go/src/testing/testing.go:562 +0x8b4
testing.(*M).Run(0xc082033ee8, 0xc0820583d0)
    c:/go/src/testing/testing.go:494 +0x77
main.main()
    github.com/superbaddude/testcgo/_test/_testmain.go:54 +0x11d

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    c:/go/src/runtime/asm_amd64.s:1696 +0x1
rax     0x0
rbx     0x1045e80
rcx     0x0
rdi     0x1045ec0
rsi     0x6d6850
rbp     0x0
rsp     0x24fc00
r8      0x10
r9      0x8735d0
r10     0x1
r11     0x3
r12     0x8
r13     0x5e2c36
r14     0x9
r15     0x8
rip     0x7fff56cda1c8
rflags  0x206
cs      0x33
fs      0x53
gs      0x2b
exit status 2
FAIL    github.com/superbaddude/testcgo 0.914s

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 23 (13 by maintainers)

Most upvoted comments

Change https://go.dev/cl/457875 mentions this issue: runtime,cmd/link: allow SEH tramps handle non-Go exception

I use AddVectoredExceptionHandler and try {} catch {} resolved.

Code:

auto handle = AddVectoredExceptionHandler(1, callback);
try {
    callThroableFunc();
} catch(std::exception &e) {}
RemoveVectoredExceptionHandler(handle);
 

I think this is relevant here: https://stackoverflow.com/a/37371025

Whether -static-libgcc or not is being used might affect the results.

Some observations:

  1. The exception number (0x20474343) is the one used by g++ runtime for g++ exceptions.
  2. I changed to C++ function to throw (and catch) an int, and the program runs fine.
  3. However, if I change the C++ function to throw (and catch) new int (or anything that requires memory allocation it seems), the program crashes.