]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: avoid malloc during malloc
authorRuss Cox <rsc@golang.org>
Tue, 21 Feb 2012 21:36:15 +0000 (16:36 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 21 Feb 2012 21:36:15 +0000 (16:36 -0500)
A fault during malloc might lead to the program's
first call to findfunc, which would in turn call malloc.
Don't do that.

Fixes #1777.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/5689047

src/pkg/runtime/symtab.c

index df4c9ad76cfabadab24ca5c985abf2d209fdb696..f29276bd7ff5ea02dcddb583533119041f4b414c 100644 (file)
@@ -437,13 +437,17 @@ runtime·findfunc(uintptr addr)
        // (Before enabling the signal handler,
        // SetCPUProfileRate calls findfunc to trigger
        // the initialization outside the handler.)
-       if(runtime·atomicload(&funcinit) == 0) {
-               runtime·lock(&funclock);
-               if(funcinit == 0) {
-                       buildfuncs();
-                       runtime·atomicstore(&funcinit, 1);
+       // Avoid deadlock on fault during malloc
+       // by not calling buildfuncs if we're already in malloc.
+       if(!m->mallocing && !m->gcing) {
+               if(runtime·atomicload(&funcinit) == 0) {
+                       runtime·lock(&funclock);
+                       if(funcinit == 0) {
+                               buildfuncs();
+                               runtime·atomicstore(&funcinit, 1);
+                       }
+                       runtime·unlock(&funclock);
                }
-               runtime·unlock(&funclock);
        }
 
        if(nfunc == 0)