Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in

Issue 71440043: code review 71440043: runtime: handle Go calls C calls Go panic correctly on ... (Closed)

Can't Edit
Can't Publish+Mail
Start Review
6 years ago by rsc
6 years ago
gobot, brainman
golang-codereviews, brainman, iant, khr


runtime: handle Go calls C calls Go panic correctly on windows/386 32-bit Windows uses "structured exception handling" (SEH) to handle hardware faults: that there is a per-thread linked list of fault handlers maintained in user space instead of something like Unix's signal handlers. The structures in the linked list are required to live on the OS stack, and the usual discipline is that the function that pushes a record (allocated from the current stack frame) onto the list pops that record before returning. Not to pop the entry before returning creates a dangling pointer error: the list head points to a stack frame that no longer exists. Go pushes an SEH record in the top frame of every OS thread, and that record suffices for all Go execution on that thread, at least until cgo gets involved. If we call into C using cgo, that called C code may push its own SEH records, but by the convention it must pop them before returning back to the Go code. We assume it does, and that's fine. If the C code calls back into Go, we want the Go SEH handler to become active again, not whatever C has set up. So runtime.callbackasm1, which handles a call from C back into Go, pushes a new SEH record before calling the Go code and pops it when the Go code returns. That's also fine. It can happen that when Go calls C calls Go like this, the inner Go code panics. We allow a defer in the outer Go to recover the panic, effectively wiping not only the inner Go frames but also the C calls. This sequence was not popping the SEH stack up to what it was before the cgo calls, so it was creating the dangling pointer warned about above. When eventually the m stack was used enough to overwrite the dangling SEH records, the SEH chain was lost, and any future panic would not end up in Go's handler. The bug in TestCallbackPanic and friends was thus creating a situation where TestSetPanicOnFault - which causes a hardware fault - would not find the Go fault handler and instead crash the binary. Add checks to TestCallbackPanicLocked to diagnose the mistake in that test instead of leaving a bad state for another test case to stumble over. Fix bug by restoring SEH chain during deferred "endcgo" cleanup. This bug is likely present in Go 1.2.1, but since it depends on Go calling C calling Go, with the inner Go panicking and the outer Go recovering the panic, it seems not important enough to bother fixing before Go 1.3. Certainly no one has complained. Fixes issue 7470.

Patch Set 1 #

Patch Set 2 : diff -r 2708885f17a8 https://code.google.com/p/go/ #

Patch Set 3 : diff -r 2708885f17a8 https://code.google.com/p/go/ #

Patch Set 4 : diff -r 1a0714819799 https://code.google.com/p/go/ #

Unified diffs Side-by-side diffs Delta from patch set Stats (+102 lines, -35 lines) Patch
M src/pkg/runtime/cgocall.c View 1 3 chunks +12 lines, -0 lines 0 comments Download
M src/pkg/runtime/export_test.go View 1 1 chunk +4 lines, -0 lines 0 comments Download
M src/pkg/runtime/proc.c View 1 2 4 chunks +4 lines, -12 lines 0 comments Download
M src/pkg/runtime/runtime.h View 1 5 chunks +30 lines, -0 lines 0 comments Download
M src/pkg/runtime/runtime1.goc View 1 1 chunk +4 lines, -0 lines 0 comments Download
M src/pkg/runtime/sys_windows_386.s View 1 1 chunk +10 lines, -0 lines 0 comments Download
M src/pkg/runtime/syscall_windows_test.go View 1 2 chunks +38 lines, -23 lines 0 comments Download


Total messages: 4
Hello golang-codereviews@googlegroups.com (cc: brainman, iant, khr), I'd like you to review this change to https://code.google.com/p/go/
6 years ago (2014-03-05 04:33:06 UTC) #1
LGTM Thank you. Alex
6 years ago (2014-03-05 06:12:50 UTC) #2
*** Submitted as https://code.google.com/p/go/source/detail?r=d66964c2e175 *** runtime: handle Go calls C calls Go panic correctly on ...
6 years ago (2014-03-05 16:10:43 UTC) #3
6 years ago (2014-03-05 16:19:20 UTC) #4
Message was sent while issue was closed.
This CL appears to have broken the openbsd-386-rootbsd builder.
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b