Tcl Source Code

View Ticket
Login
Ticket UUID: 1618411
Title: TclpCheckStackSpace() fails on recent linux system
Type: Bug Created on: 2006-12-18 21:44:30
Submitter: arekm Assigned to: mistachkin
Subsystem: 49. Threading Severity:
Priority: 5 Medium Last modified: 2007-02-09 07:58:20
Status: Closed Closed by: hobbs
Resolution: Fixed Closed on: 2007-02-09 00:58:20
Version: obsolete: 8.5a6
Description:
tcl fail randomly with:
application-specific initialization failed: too many nested evaluations (infinite loop?)

this is Linux 2.6.16, glibc 2.5, x86_64 system, testing vanilla tcl 8.5 a5 configured as:
./configure --enable-threads

Note that --enable-threads seems to be needed to reproduce the problem. Also I needed to run tclsh via make call to reproduce the problem - direct running tclsh aaa.tcl didn't trigger it.

While tracking the problem with dgp@jabber we found that problem is in TclpCheckStackSpace() function.

Further investigation shows that that this function seem to be wrong on recent Linux/glibc systems where many things are randomized (see http://gcc.gnu.org/wiki/Randomization) and finally we found out that disabling memory address space randomization makes  TclpCheckStackSpace() pass:
sysctl -w kernel.randomize_va_space=0

Looks like  TclpCheckStackSpace() isn't ready for recent systems with randomization stuff (which means any recent linux distro).

Test case:
[arekm@arm ~/tcl]$ ls
aaa.tcl  Makefile
[arekm@arm ~/tcl]$ cat aaa.tcl
[arekm@arm ~/tcl]$ cat Makefile
test:
        tclsh aaa.tcl > bbb.txt
[arekm@arm ~/tcl]$ make test
tclsh aaa.tcl > bbb.txt
[arekm@arm ~/tcl]$ make test
tclsh aaa.tcl > bbb.txt
application-specific initialization failed: too many nested evaluations (infinite loop?)
[arekm@arm ~/tcl]$ make test
tclsh aaa.tcl > bbb.txt
application-specific initialization failed: too many nested evaluations (infinite loop?)
[arekm@arm ~/tcl]$ make test
tclsh aaa.tcl > bbb.txt
application-specific initialization failed: too many nested evaluations (infinite loop?)
[arekm@arm ~/tcl]$ make test
tclsh aaa.tcl > bbb.txt
application-specific initialization failed: too many nested evaluations (infinite loop?)
[arekm@arm ~/tcl]$ make test
tclsh aaa.tcl > bbb.txt
application-specific initialization failed: too many nested evaluations (infinite loop?)
[arekm@arm ~/tcl]$ make test
tclsh aaa.tcl > bbb.txt
[arekm@arm ~/tcl]$ make test
tclsh aaa.tcl > bbb.txt
application-specific initialization failed: too many nested evaluations (infinite loop?)
[arekm@arm ~/tcl]$ make test
tclsh aaa.tcl > bbb.txt
[arekm@arm ~/tcl]$ make test
tclsh aaa.tcl > bbb.txt
[arekm@arm ~/tcl]$     

with stack debugging:
[arekm@arm ~/tcl]$ make test
/home/users/arekm/tcl-test/bin/tclsh8.5 aaa.tcl
stack OVERFLOW  in:0x7fffffa9c4e4       out:0x7fffffa9c4e4      use:0000        max:F06E2000
application-specific initialization failed XXX: too many nested evaluations (infinite loop?)
stack OVERFLOW  in:0x7fffffa9c954       out:0x7fffffa9c4e4      use:0470        max:F06E2000
User Comments:
mistachkin added on 2006-12-20 10:53:18:
Logged In: YES 
user_id=113501
Originator: NO


I can help work on this; however, I have no box that can repro the issue.

dkf added on 2006-12-20 17:04:27:
Logged In: YES 
user_id=79902
Originator: NO

Either the calculation of the stack size is wrong (which I could believe; perhaps the x86_64 pthread library is blowing smoke in our eyes here?) or the comparison on line 1092 of tclUnixInit.c is wrong and the sign is causing trouble.

Alas, I can't test because I don't have an x86_64 Unix machine conveniently available at the moment either...

mistachkin added on 2007-01-20 14:25:55:
Logged In: YES 
user_id=113501
Originator: NO

It would be useful to have the "aaa.tcl" script mentioned in the bug if it is doing something special.

mistachkin added on 2007-01-20 16:25:22:
Logged In: YES 
user_id=113501
Originator: NO

Bug repro verified.  It appears the x86_64 pthread library is indeed blowing smoke.  The returned maximum stack size appears to be totally random.

Excerpt from run #1:

stack OK in:0x7fffff8cb70c out:0x7fffff8cca2c use:1320 max:209BE000

Excerpt from run #2:

stack OK in:0x7ffffffdf96c out:0x7ffffffe0c8c use:1320 max:3532D000

Excerpt from run #3:

stack OK in:0x7fffff8f89bc out:0x7fffff8f9cdc use:1320 max:4BC99000

arekm added on 2007-01-20 21:21:43:
Logged In: YES 
user_id=139606
Originator: YES

Aren't these randomizations in linux all about that?
http://lwn.net/Articles/120969/

mistachkin added on 2007-01-21 10:12:01:
Logged In: YES 
user_id=113501
Originator: NO


That patch appears to be about randomizing the address of the stack, not the size.  We absolutely need an accurately reported maximum stack size.  I believe that on this platform the best we can hope for is a way to detect the inaccuracy of the reported stack size and use some default fallback value.

arekm added on 2007-01-21 19:26:03:
Logged In: YES 
user_id=139606
Originator: YES


With this test app below:

[arekm@arm ~]$ gcc -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 800000.
thread stack size2 is 800000.
[arekm@arm ~]$ gcc -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 800000.
thread stack size2 is 800000.
[arekm@arm ~]$ gcc -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 800000.
thread stack size2 is 800000.
[arekm@arm ~]$ gcc -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 800000.
thread stack size2 is 800000.

stack size seems contant but... let set rlimit to unlimited value

[arekm@arm ~]$ ulimit -s unlimited
[arekm@arm ~]$ ulimit -a
-t: cpu time (seconds)         unlimited
-f: file size (blocks)         unlimited
-d: data seg size (kbytes)     unlimited
-s: stack size (kbytes)        unlimited
-c: core file size (blocks)    0
-m: resident set size (kbytes) unlimited
-u: processes                  unlimited
-n: file descriptors           1024
-l: locked-in-memory size (kb) 32
-v: address space (kb)         unlimited
-x: file locks                 unlimited
-i: pending signals            8183
-q: bytes in POSIX msg queues  819200
-N 13:                         0
-N 14:                         0
[arekm@arm ~]$ gcc -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 54FBAD179000.
thread stack size2 is 54FBAD179000.
[arekm@arm ~]$ gcc -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 5466D4277000.
thread stack size2 is 5466D4277000.
[arekm@arm ~]$ gcc -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 548C6FDFD000.
thread stack size2 is 548C6FDFD000.
[arekm@arm ~]$ gcc -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 550306109000.
thread stack size2 is 550306109000.


and that's exactly what make does (that's why it wasn't visible without using make):

[arekm@arm ~]$ ulimit -s 8192
[arekm@arm ~]$ gcc -DMAX -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 5553305D8000.
thread stack size2 is 5553305D8000.
[arekm@arm ~]$ gcc -DMAX -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 5502E3DFE000.
thread stack size2 is 5502E3DFE000.
[arekm@arm ~]$ gcc -DMAX -O2 -Wall test1.c -pthread && ./a.out
test1.c:1:20: warning: missing whitespace after the macro name
thread stack size is 5537F5E02000.
thread stack size2 is 5537F5E02000.



#define _GNU_SOURCE=1

#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/resource.h>

int main() {

    size_t stackSize = 0;
    size_t stackSize2 = 0;

    pthread_attr_t threadAttr;  /* This will hold the thread attributes for
                                 * the current thread. */

#ifdef MAX
    struct rlimit r;

    r.rlim_cur = RLIM_INFINITY;
    r.rlim_max = RLIM_INFINITY;

    setrlimit(RLIMIT_STACK, &r);
#endif


    if (pthread_attr_init(&threadAttr) != 0) {
        return -1;
    }

    if (pthread_getattr_np(pthread_self(), &threadAttr) != 0) {
        pthread_attr_destroy(&threadAttr);
        return -1;
    }

    if (pthread_attr_getstacksize(&threadAttr, &stackSize) != 0) {
        pthread_attr_destroy(&threadAttr);
        return -1;
    }

    if (pthread_attr_getstacksize(&threadAttr, &stackSize2) != 0) {
        pthread_attr_destroy(&threadAttr);
        return  -1;
    }

    printf("thread stack size is %ZX.\n", stackSize);
    printf("thread stack size2 is %ZX.\n", stackSize2);

    pthread_attr_destroy(&threadAttr);

    return 0;
}

arekm added on 2007-01-21 19:49:47:
Logged In: YES 
user_id=139606
Originator: YES

Anyway this looks to be normal thing, tested on kernel 2.6.16 + glibc 2.3.6 (version with and without NPTL), kernel 2.6.19 + glibc 2.5 (NPTL). Behaviour isn't limited to x86_64 - tested on i686, too.

Could you explain why is this a problem for tcl?

I guess stack size changes between runs but never while process is running, right?

dkf added on 2007-01-22 05:40:23:
Logged In: YES 
user_id=79902
Originator: NO

Tcl needs to know the stack size so it can avoid blowing off the end of it in a deep recursion. But it is totally oblivious to the location of the stack; if it wasn't, that'd be non-portable and non-thread-safe. :-)

It's not just Tcl that assumes a particular stack is at a constant location. Just about every C and C++ program ever written also assumes that...

mistachkin added on 2007-01-22 06:54:40:
Logged In: YES 
user_id=113501
Originator: NO


The problem is that the reported thread stack sizes are not realistic: 5553305D8000 = 93,815,782,080,512 bytes.

arekm added on 2007-01-22 14:51:09:
Logged In: YES 
user_id=139606
Originator: YES

This test program reports stack size correctly so this works. In tcl pthread_getattr_np(pthread_self(), &threadAttr) fills threadAttr with crap. I can easily reproduce that by calling pthread_getattr_np NOT from thread.

Let's strace tclsh:

[root@db01new bin]# strace -o WYNIK -f -F -s 200 ./tclsh8.5
xyz 46955385561024
thread stack size is 93782034972672.
% exit

and now see what it did:
[root@db01new bin]# grep -E "(clone|thread stack size)" WYNIK
13270 clone(child_stack=0x40200270, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID|CLONE_DETACHED, parent_tidptr=0x402009f0, tls=0x40200960, child_tidptr=0x402009f0) = 13271
13270 write(1, "thread stack size is 93782034972672.\n", 37) = 37

For me it looks like main process 13270 called pthread_create() which created thread with pid 13271 and later called pthread_getattr_np() from main process instead of from newly created thread... which leads to crap being filled, right?

[root@db01new ~]# ulimit -a
time(cpu-seconds)    unlimited
file(blocks)         unlimited
coredump(blocks)     0
data(kbytes)         unlimited
stack(kbytes)        unlimited
lockedmem(kbytes)    32
memory(kbytes)       unlimited
nofiles(descriptors) 1024
processes            unlimited
vmemory(kbytes)      unlimited
flocks               unlimited
[root@db01new ~]# ./a.out
xyz 1075841376
thread stack size is 2101248.



#define _GNU_SOURCE=1

#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/resource.h>

void *testme(void *threadid) {
        size_t stackSize = 0;
        pthread_attr_t threadAttr;

        printf("xyz %ld\n", pthread_self());

        if (pthread_getattr_np(pthread_self(), &threadAttr) != 0) {
                pthread_attr_destroy(&threadAttr);
                pthread_exit(NULL);
                return NULL;
        }

        if (pthread_attr_getstacksize(&threadAttr, &stackSize) != 0) {
                pthread_attr_destroy(&threadAttr);
                pthread_exit(NULL);
                return NULL;
        }

        printf("thread stack size is %Zu.\n", stackSize);

        pthread_attr_destroy(&threadAttr);

        pthread_exit(NULL);
}

int main() {
        int rc;
        long t;
        pthread_t thr;

        struct rlimit r;

        r.rlim_cur = RLIM_INFINITY;
        r.rlim_max = RLIM_INFINITY;

        rc = setrlimit(RLIMIT_STACK, &r);
        if (rc) {
                printf("ERROR; setrlimit failed %d\n", rc);
                return -1;
        }

        rc = pthread_create(&thr, NULL, testme, (void *)t);
        if (rc){
                printf("ERROR; return code from pthread_create() is %d\n", rc);
                return -1;
        }
        pthread_exit(NULL);

        return 0;
}



ps, from nptl headers:
/* Initialize thread attribute *ATTR with attributes corresponding to the
   already running thread TH.  It shall be called on unitialized ATTR
   and destroyed with pthread_attr_destroy when no longer needed.  */
extern int pthread_getattr_np (pthread_t __th, pthread_attr_t *__attr)
     __THROW __nonnull ((2));

while currently tcl initializes pthread_attr_t prior to calling pthread_getattr_np.

arekm added on 2007-01-22 14:58:54:
Logged In: YES 
user_id=139606
Originator: YES

+ one note - seem that randomization isn't the issue here:

[root@db01new bin]# sysctl -w kernel.randomize_va_space=0
kernel.randomize_va_space = 0
[root@db01new bin]# ./tclsh8.5
xyz 46912505388992
thread stack size is 93824917221376.
[root@db01new bin]# sysctl -w kernel.randomize_va_space=1
kernel.randomize_va_space = 1
[root@db01new bin]# ./tclsh8.5
xyz 46967590911936
thread stack size is 93769826639872.
[root@db01new bin]# sysctl -w kernel.randomize_va_space=0
kernel.randomize_va_space = 0
[root@db01new bin]# ./tclsh8.5
xyz 46912505388992
thread stack size is 93824917221376.

mistachkin added on 2007-01-22 16:02:14:
Logged In: YES 
user_id=113501
Originator: NO


We may need to expand the use of pthread_main_np to detect if we are running on the main thread in TclpThreadGetStackSize to platforms other than OS X.

hobbs added on 2007-02-09 07:58:20:
Logged In: YES 
user_id=72656
Originator: NO

Fixed 2007-02-08 in head.