Tcl Source Code

View Ticket
Login
Ticket UUID: 217833
Title: Threaded Tcl calls non-thread-safe library functions
Type: Bug Created on: 2000-10-26 00:44:36
Submitter: nobody Assigned to: nobody
Subsystem: 49. Threading Severity:
Priority: 2 Last modified: 2000-10-26 07:59:46
Status: Closed Closed by: welch
Resolution: Invalid Closed on: 2000-10-26 00:59:46
Version: obsolete: 8.1b2
Description:
OriginalBugID: 1653 Bug
Version: 8.1b2
SubmitDate: '1999-03-29'
LastModified: '1999-10-21'
Severity: CRIT
Status: Closed
Submitter: pat
ChangedBy: hobbs
RelatedBugIDs: 607
OS: AIX
FixedDate: '2000-10-25'
FixedInVersion: NA
ClosedDate: '1999-10-21'


Name:  Colin Macleod
email:  [email protected]
Support:  None
Severity:  1
OperatingSystem:  AIX
OperatingSystemVersion:  4.3.2, running on a 2-processor RS/6000
Extensions:  none here
CustomShell:  none in this case
Synopsis:  Threaded Tcl calls non-thread-safe library functions

ReproducibleScript:
    Even when built with --enable-threads, Tcl calls non-thread-safe library
    functions.  As an example I take the getpwnam routine, but this is not
    the only one concerned.
    
    Eg. I built tcl8.1b2 on AIX4.3.2 with --enable-threads and also
    compiled tcltest to be able to test thread-related functionality.
    I created the following script, saved in file "z" :
    
    ####################################################################
    # first a procedure which repeatedly find a user's home directory
    # and complains if it changes.  I put this definition in a variable
    # to be able to pass it to another thread easily.
    
    set defproc {
    proc checkuser user {
      set h0 [glob ~$user]
      time {
        set h [glob ~$user]
        if { [string compare $h $h0] } {
          error "$h != $h0"
        }
      } 100
      puts "thread [testthread id] finished ($h $h0)"
    }
    }
    
    # Now create some threads, each of which repeatedly checks the home
    # directory of one user.  The list of users is only valid for our
    # system, of course.  To repeat this test elsewhere, substitute
    # a list of users for the system used.
    
    foreach user {
      abal         dbik         imnadm       mmeg         ote          root
      aded         ddim         isoft        mpap         pfou         smpr
    } {
      testthread create "$defproc; checkuser $user"
    }
    #######################################################################
    
    I ran this repeatedly as: ./tcltest z
    I also varied the repeat count in the time statement (100 above).
    

ObservedBehavior:
    Since the behaviour is timing-dependent, it varies between runs,
    and with the length of each run.  Sometimes the script completes
    without errors, sometimes one or more of the tests fail, sometimes
    you get a crash and core dump.  With the repeat count at 10, it
    usually completes ok.  At 30 repetitions, you usually get a test
    failure but not a core dump.  At 100, you usually get a core dump
    also.
    
    Eg:
    
    $ ./tcltest z
    Error from thread 3856
    /home/dbik != /home/abal
        while executing
    "error "$h != $h0""
        invoked from within
    "time {
        set h [glob ~$user]
        if { [string compare $h $h0] } {
          error "$h != $h0"
        }
      } 100"
        (procedure "checkuser" line 3)
        invoked from within
    "checkuser dbik"
    Segmentation fault(coredump)
    $
    
    Invoking dbx to get a stack trace:
    $ dbx tcltest core
    Type 'help' for help.
    reading symbolic information ...
    [using memory image in core]
    
    Segmentation fault in fclose at 0xd000f68c ($t17)
    0xd000f68c (fclose+0x6c) 807f001c        lwz   r3,0x1c(r31)
    (dbx) file tclUnixFile.c
    (dbx) file tclFileName.c
    (dbx) where
    fclose(??) at 0xd000f68c
    pw_close(??) at 0xd006e284
    endpwent_r(??) at 0xd006f460
    TclpGetUserHome(name = "imnadm", bufferPtr = 0x20270368), line 381 in
    "tclUnixFi
    le.c"
    DoTildeSubst(interp = 0x200d5f68, user = "imnadm", resultPtr =
    0x20270368), line
     1148 in "tclFileName.c"
    unnamed block $b202, line 1321 in "tclFileName.c"
    TclGlob(interp = 0x200d5f68, pattern = "~imnadm", noComplain = 0), line
    1321 in
    "tclFileName.c"
    Tcl_GlobObjCmd(dummy = (nil), interp = 0x200d5f68, objc = 2, objv =
    0x2027b048),
     line 1214 in "tclFileName.c"
    unnamed block $b158, line 843 in "tclExecute.c"
    TclExecuteByteCode(0x200d5f68, 0x200cdbd8), line 843 in "tclExecute.c"
    Tcl_EvalObjEx(0x200d5f68, 0x2027d728, 0x0), line 2719 in "tclBasic.c"
    Tcl_TimeObjCmd(0x0, 0x200d5f68, 0x3, 0x2027b038), line 1555 in
    "tclCmdMZ.c"
    unnamed block $b158, line 843 in "tclExecute.c"
    TclExecuteByteCode(0x200d5f68, 0x2009c8d8), line 843 in "tclExecute.c"
    Tcl_EvalObjEx(0x200d5f68, 0x2027d710, 0x0), line 2719 in "tclBasic.c"
    TclObjInterpProc(0x2009be98, 0x200d5f68, 0x2, 0x20271c18), line 995 in
    "tclProc.
    c"
    EvalObjv(0x200d5f68, 0x2, 0x20271c18, 0x200cc627, 0x11, 0x0), line 917
    in "tclPa
    rse.c"
    Tcl_EvalEx(0x200d5f68, 0x200cc558, 0xe0, 0x0), line 1378 in "tclParse.c"
    Tcl_Eval(0x200d5f68, 0x200cc558), line 1485 in "tclParse.c"
    NewThread() at 0x100a89c8
    _pthread_body(??) at 0xd06f01e8
    (dbx)
    
    BUT, the crash is not always in the same place.  It is usually inside
    the call to endpwent_r, sometimes in a call to free, sometimes other
    places, suggesting that memory has been corrupted.
    

DesiredBehavior:
    With this test, the users' directories should not appear to change,
    and tcl should not crash.  
    
    The problem is that the code which expands ~username calls getpwnam
    which is not thread-safe.  It uses a static buffer, so sometimes
    threads overwrite each others results.  
    

Patch:
no patch, see below

Comments:
    Specifically for AIX, the problem illustrated can be fixed by changing
    the call of getpwnam to use getpwnam_r.
    
    BUT this is just one example.  There are other non-thread-safe library
    routines used, eg. localtime in tclUnixTime.c .  Also from the limited
    investigation I have done I believe the fixes needed vary between
    platforms,
    because the thread-safe *_r routines are not well standardised. 
    I do not have the time needed to investigate and generate a complete
    set of fixes.
    
    PS: my email address is:  macleod @ intrasoft . gr (remove added spaces)
    
Posted To comp.lang.tcl: Posted Successfully

Additional comments on 3/30/99

This is a follow-up to my own bug report.
(Also posted to comp.lang.tcl)

The version of X11 which comes with AIX4.3.2 includes a header
X11/Xos_r.h which defines thread-safe wrappers for common
thread-unsafe library routines, with ifdefs for various systems.
This appears to be freely-reusable, and might be a useful
basis for a portable fix to this problem.

This stuff seems to vary a lot.  Eg. on AIX4.3.2 the docs say that
gethostbyname is thread-safe, by gethostbyaddr is not.  On AIX4.1.5
which we also have, both are thread-unsafe.


We need a patch to apply or we need to purchase an AIX box. Either way, his will not be done intime for 8.1b3.  04/06/1999 14:34 - surles -