this breaks ARM builds, I don't yet know why
perhaps I made a wrong assumption somewhere,
feel free to submit when it's ready.
i will hunt the ARM bug.
# ../misc/cgo/test
panic: runtime error: call of nil func value [recovered]
panic: runtime error: call of nil func value
[signal 0xb code=0x1 addr=0x0 pc=0x0]
goroutine 13 [running]:
testing.func·004(0x40051fd4, 0x1026a100)
/home/minux/go.hg/src/pkg/testing/testing.go:310 +0x148
created by testing.RunTests
/home/minux/go.hg/src/pkg/testing/testing.go:430 +0x6d4
goroutine 1 [chan receive]:
testing.RunTests(0x10c00, 0x19e578, 0x1b, 0x1b, 0x1, ...)
/home/minux/go.hg/src/pkg/testing/testing.go:431 +0x6f4
testing.Main(0x10c00, 0x19e578, 0x1b, 0x1b, 0x19bb70, ...)
/home/minux/go.hg/src/pkg/testing/testing.go:327 +0x68
main.main()
_/home/minux/go.hg/misc/cgo/test/_test/_testmain.go:97 +0x94
exit status 2
FAIL _/home/minux/go.hg/misc/cgo/test 0.033s
Command exited with non-zero status 1
https://codereview.appspot.com/7304104/diff/2001/src/pkg/runtime/asm_arm.s
File src/pkg/runtime/asm_arm.s (right):
https://codereview.appspot.com/7304104/diff/2001/src/pkg/runtime/asm_arm.s#ne...
src/pkg/runtime/asm_arm.s:376: CALL (R0)
s/CALL/BL/
https://codereview.appspot.com/7304104/diff/2001/src/pkg/runtime/proc.c
File src/pkg/runtime/proc.c (right):
https://codereview.appspot.com/7304104/diff/2001/src/pkg/runtime/proc.c#newco...
src/pkg/runtime/proc.c:904: // is - once it has installed its own m so that ican
do things like
s/ican/it can/
windows/386 problems:
This is the end of the build:
...
# ..\misc\cgo\stdio
# ..\misc\cgo\test
# _/C_/go/root/misc/cgo/test
..\misc\cgo\test\cthread_windows.c: In function 'addThread':
..\misc\cgo\test\cthread_windows.c:18:2: warning: 'return' with a value, in
function returning void
..\misc\cgo\test\cthread_windows.c: In function 'doAdd':
..\misc\cgo\test\cthread_windows.c:31:3: warning: passing argument 3 of
'_beginthreadex' from incompatible pointer type
c:\go\mingw\bin\../lib/gcc/mingw32/4.5.2/../../../../include/process.h:100:2:
note: expected 'unsigned int (*)(void *)' but argument is of type 'void (*)(void
*)'
ok _/C_/go/root/misc/cgo/test 19.469s
# ..\doc\progs
# ..\test
# Checking API compatibility.
+pkg go/types, method (*Const) GetPkg() *Package
+pkg go/types, method (*Func) GetPkg() *Package
+pkg go/types, method (*Package) GetPkg() *Package
+pkg go/types, method (*TypeName) GetPkg() *Package
+pkg go/types, method (*Var) GetPkg() *Package
+pkg go/types, type Const struct, Pkg *Package
+pkg go/types, type Func struct, Pkg *Package
+pkg go/types, type Object interface, GetPkg() *Package
+pkg go/types, type TypeName struct, Pkg *Package
+pkg go/types, type Var struct, Pkg *Package
~pkg net, func ListenUnixgram(string, *UnixAddr) (*UDPConn, error)
~pkg text/template/parse, type DotNode bool
~pkg text/template/parse, type Node interface { Copy, String, Type }
±pkg go/types, type Importer func(imports map[string]*Package, path string) (pkg
*Package, err error)
ALL TESTS PASSED
...
So, there are some compiler warnings. But misc\cgo\test test executable also
crashes. You don't get to see the crash, because, my "system debugger" is
started to handle the exception, and I had to take some manual options to kill
the bugger. I think it is because your new C threads don't set any exception
handler. Our builder won't be able to do what I did, so we have to be careful.
I tried to poke at it:
C:\go\root\misc\cgo\test>gdb test.test.exe
GNU gdb (GDB) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from C:\go\root\misc\cgo\test/test.test.exe...done.
(gdb) r
Starting program: C:\go\root\misc\cgo\test/test.test.exe
[New Thread 6492.0xe08]
[New Thread 6492.0x1da8]
[New Thread 6492.0x1eec]
scatter = 0042D268
hello from C
[New Thread 6492.0x178c]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 6492.0x178c]
runtime.cgocallback (fn=void, frame=void, framesize=void)
at C:/go/root/src/pkg/runtime/asm_386.s:485
485 MOVL m(CX), BP
(gdb) bt
#0 runtime.cgocallback (fn=void, frame=void, framesize=void)
at C:/go/root/src/pkg/runtime/asm_386.s:485
#1 0x0042c500 in _cgoexp_e9125f37873d_Add (a=void, n=void)
at
C:/DOCUME~1/brainman/LOCALS~1/Temp/go-build214494323/_/C_/go/root/misc/cgo/test/_test/_cgo_defun.c:321
#2 0x004805f6 in ?? ()
#3 0x0042ca5b in ?? ()
#4 0x0042d3dc in ?? ()
#5 0x77c3a3b0 in msvcrt!_endthreadex () from C:\WINDOWS\system32\msvcrt.dll
#6 0x7c80b729 in KERNEL32!GetModuleFileNameA ()
from C:\WINDOWS\system32\kernel32.dll
#7 0x00000000 in ?? ()
(gdb) info reg
eax 0x4 4
ecx 0x0 0
edx 0xad0608 11339272
ebx 0x0 0
esp 0x3152fefc 0x3152fefc
ebp 0x3152ff30 0x3152ff30
esi 0xa 10
edi 0x0 0
eip 0x41c4ca 0x41c4ca <runtime.cgocallback+10>
eflags 0x10216 [ PF AF IF RF ]
cs 0x1b 27
ss 0x23 35
ds 0x23 35
es 0x23 35
fs 0x3b 59
gs 0x0 0
But, I don't know what I am looking for.
The windows/amd64 didn't get very far either:
pkg/go/doc
pkg/go/build
cmd/go
lockextra: nosplit stack overflow
120 assumed on entry to lockextra
88 after lockextra uses 32
80 on entry to runtime.osyield
56 after runtime.osyield uses 24
48 on entry to runtime.stdcall
-16 after runtime.stdcall uses 64
go tool dist: FAILED: c:\MinGW64\go\pkg\tool\windows_amd64\6l -o
c:\MinGW64\go\pkg\tool\windows_amd64\go_bootstrap.exe
C:\Users\brainman\AppData\Local\Temp\goEC42.tmp\_go_.6
Alex
On Tue, Feb 19, 2013 at 12:59 PM, <rsc@golang.org> wrote:
>>
>> What will happen if C code calls Go code with no m, and the Go code
>
> calls panic?
>
> I think it all works, because while the Go code is executing there is an
> m. The test has an implicit (nil deref) panic and recover in the called
> Go code. Is there a specific reason you are worried?
I was worried that perhaps nothing would call dropm in that case.
If you switch to the pthread_key_create destructor this is clearly a
non-issue (and it may be a non-issue anyhow, I'm not sure).
Ian
On Tue, Feb 19, 2013 at 9:01 PM, Ian Lance Taylor <iant@golang.org> wrote:
> On Tue, Feb 19, 2013 at 12:59 PM, <rsc@golang.org> wrote:
> >>
> >> What will happen if C code calls Go code with no m, and the Go code
> >
> > calls panic?
> >
> > I think it all works, because while the Go code is executing there is an
> > m. The test has an implicit (nil deref) panic and recover in the called
> > Go code. Is there a specific reason you are worried?
>
> I was worried that perhaps nothing would call dropm in that case.
>
> If you switch to the pthread_key_create destructor this is clearly a
> non-issue (and it may be a non-issue anyhow, I'm not sure).
>
I think panic is fine. Since we're entering on a brand new m, there are no
deferred recovers on entry. If a recover deferred within the callback stops
the panic, as in the test, then things work fine, because the callback
returns normally. If not, then the whole process will exit after printing
the usual goroutine dump, so that should be fine too.
If the callback into Go calls into C and then C calls pthread_exit, then we
will indeed leak the m, but that's true of ordinary Go threads too. (And
worse things probably happen too, since Go thinks the m and the thread
still exist. I'm not too worried about that case, at least today.)
Russ
On Tue, Feb 19, 2013 at 7:21 PM, <alex.brainman@gmail.com> wrote:
> Program received signal SIGSEGV, Segmentation fault.
> [Switching to Thread 6492.0x178c]
> runtime.cgocallback (fn=void, frame=void, framesize=void)
> at C:/go/root/src/pkg/runtime/**asm_386.s:485
> 485 MOVL m(CX), BP
> (gdb) bt
>
Thanks for this information. I believe the problem is that 0x14(FS) (now
CX) is nil here, so in addition to setting m and g we need to set the
14(FS) space. On Darwin and OS X there is not this extra indirect. The code
here needs to test whether CX is zero and handle that, but then setmg needs
to initialize it too. And probably it has to be preserved the same way that
externalthreadhandler does today.
I will set up a Windows laptop to try this out.
Russ
On 2013/02/20 15:28:20, rsc wrote:
>
https://codereview.appspot.com/7304104/diff/6002/src/pkg/runtime/proc.c#newco...
> src/pkg/runtime/proc.c:940: runtime·setmg(mp, mp->g0);
> On 2013/02/20 07:45:35, dvyukov wrote:
> > Why is it running on g0?
> > runtime.gosched() will throw if called on g0.
>
> At this point we're in the system thread, so we're running on the system
thread
> goroutine, which is m->g0. When needm returns, the code will jump into
> cgocallbackg, which will switch us off m->g0 onto m->curg in order to run the
> actual callback.
Ah, I see.
>
https://codereview.appspot.com/7304104/diff/6002/src/pkg/runtime/proc.c#newco...
> src/pkg/runtime/proc.c:945: runtime·minit();
> On 2013/02/20 07:45:35, dvyukov wrote:
> > What about asminit()?
> > asm_386 switch FPU to a different mode. This means that Go code will behave
> > differently depending on how it is called.
>
> Done.
However, then we change behavior of the C code on that thread...
On Wed, Feb 20, 2013 at 10:31 AM, <dvyukov@google.com> wrote:
> However, then we change behavior of the C code on that thread...
>
If it had better manners, the C thread would thank us.
Russ
The current version of the CL has been revised and tested to work on:
darwin/386
darwin/amd64
linux/386
linux/amd64
linux/arm*
windows/386
windows/amd64
The test is flaky (fails <10% of the time) on arm. I don't know what's
wrong. It could be general memory corruption unrelated to this CL.
Russ
On Wed, Feb 20, 2013 at 5:43 PM, <alex.brainman@gmail.com> wrote:
> It is still crashing in misc/cgo/test on windows/386. But I have nothing
> to report yet.
>
Let me know if what I just submitted is crashing for you. I had the test
passing while testing on a Windows machine, but I might have messed up
copying the changes back to my local machine.
Russ
https://codereview.appspot.com/7304104/diff/44001/src/pkg/runtime/proc.c File src/pkg/runtime/proc.c (right): https://codereview.appspot.com/7304104/diff/44001/src/pkg/runtime/proc.c#newcode944 src/pkg/runtime/proc.c:944: runtime·minit(); part of the problem was here minit() allocated ...
https://codereview.appspot.com/7304104/diff/44001/src/pkg/runtime/proc.c
File src/pkg/runtime/proc.c (right):
https://codereview.appspot.com/7304104/diff/44001/src/pkg/runtime/proc.c#newc...
src/pkg/runtime/proc.c:944: runtime·minit();
On 2013/02/21 15:45:45, dvyukov wrote:
> part of the problem was here
> minit() allocated gsignal, but we've not yet called exitsyscall() so we are
> potentially running concurrently with GC
>
> it seems to be fixed by mpreinit() patch
> but I still observe some crashes on linux on misc/cgo/test
> still looks like corrupted heap
I've noticed that if I set GOGC=off the crash goes away, and if I set GOGC=1 it
crashes more reliably.
It probably means that we either allocate some memory concurrently with GC or
some memory is not reachable.
On 2013/02/21 16:40:28, dvyukov wrote:
> https://codereview.appspot.com/7304104/diff/44001/src/pkg/runtime/proc.c
> File src/pkg/runtime/proc.c (right):
>
>
https://codereview.appspot.com/7304104/diff/44001/src/pkg/runtime/proc.c#newc...
> src/pkg/runtime/proc.c:944: runtime·minit();
> On 2013/02/21 15:45:45, dvyukov wrote:
> > part of the problem was here
> > minit() allocated gsignal, but we've not yet called exitsyscall() so we are
> > potentially running concurrently with GC
> >
> > it seems to be fixed by mpreinit() patch
> > but I still observe some crashes on linux on misc/cgo/test
> > still looks like corrupted heap
>
> I've noticed that if I set GOGC=off the crash goes away, and if I set GOGC=1
it
> crashes more reliably.
> It probably means that we either allocate some memory concurrently with GC or
> some memory is not reachable.
OK, mystery solved. I will send a CL.
Issue 7304104: code review 7304104: runtime: allow cgo callbacks on non-Go threads
(Closed)
Created 12 years ago by rsc
Modified 12 years ago
Reviewers: brainman, dvyukov
Base URL:
Comments: 35