Tcl Source Code

View Ticket
Login
Ticket UUID: 218522
Title: "memory validate on" fails in multithreaded tcl
Type: Bug Created on: 2000-10-26 01:01:39
Submitter: nobody Assigned to: nobody
Subsystem: 49. Threading Severity:
Priority: 2 Last modified: 2000-10-26 08:23:19
Status: Closed Closed by: welch
Resolution: Fixed Closed on: 2000-10-26 01:23:19
Version: obsolete: 8.1a2
Description:
OriginalBugID: 880 Bug
Version: 8.1a2
SubmitDate: '1998-11-30'
LastModified: '1998-11-30'
Severity: SER
Status: Released
Submitter: stanton
ChangedBy: stanton
OS: All
OSVersion: NA
Machine: NA
FixedDate: '1998-11-30'
FixedInVersion: 8.1b1
ClosedDate: '2000-10-25'


I have now verified that the "memory validate on" bug I had previously
found in PtTcl still exists in Tcl8.1a2.  Below I include a test which
causes a crash, output from dbx showing where the crash happens,
my explanation of the problem and finally my patch to fix it.

_________________________________________________________________________

TEST: I run tcltest (compiled with -DTCL_MEM_DEBUG), turn on memory
      validation, and start a second thread which performs some
      dummy operations to which will cause memory allocation/
      deallocation.  This causes a crash as if memory was corrupted:

$ ./tcltest
% memory info
total mallocs                   2794
total frees                     1138
current packets allocated       1656
current bytes allocated       112717
maximum packets allocated       1672
maximum bytes allocated       119129

% memory validate on
% testthread create {while 1 {append x x; if {[string length $x] > 1000}  {set x a; puts [testthread id]}}}
low guard byte 0 is 0x0          
low guard byte 1 is 0x0          
low guard byte 2 is 0x0          
low guard byte 3 is 0x0          
low guard byte 4 is 0x0          
low guard byte 5 is 0x0          
low guard byte 6 is 0x0          
low guard byte 7 is 0x0          
total mallocs                   4882
total frees                     2496
current packets allocated       2386
current bytes allocated       164465
maximum packets allocated       2544
maximum bytes allocated       186687
low guard failed at 20272b18, ./../generic/tclStringObj.c 160
11 bytes allocated at (./../generic/tclLiteral.c 266)
Memory validation failure
child killed: SIGABRT
$ 

_________________________________________________________________________

DBX SESSION:

$ dbx tcltest core
Type 'help' for help.
reading symbolic information ...
[using memory image in core]

IOT/Abort trap in pthread_kill at 0xd0168c00 ($t3)
0xd0168c00 (pthread_kill+0x130) 80410014        lwz   r2,0x14(r1)
(dbx) where
pthread_kill(??, ??) at 0xd0168c00
signal.raise(??) at 0xd016886c
abort.abort() at 0xd0d62508
panic(0x200028d8, 0x10000000, 0x0, 0x10000000, 0x0, 0x60000000, 0x60003d44, 0x0), line 94 in "panic.c"
ValidateMemory(0x20272af8, 0x20000938, 0xa0, 0x0), line 218 in "tclCkalloc.c"
Tcl_ValidateAllMemory(0x20000938, 0xa0), line 272 in "tclCkalloc.c"
Tcl_DbCkalloc(0x6, 0x20000938, 0xa0), line 350 in "tclCkalloc.c"
Tcl_DbNewStringObj(0x2028de88, 0x5, 0x20008d0c, 0x479), line 160 in "tclStringObj.c"
Tcl_EvalTokens(0x2028f6d8, 0x202b1c80, 0x1), line 1145 in "tclParse.c"
Tcl_Eval2(0x2028f6d8, 0x2028de88, 0x57, 0x0), line 1267 in "tclParse.c"
Tcl_Eval(0x2028f6d8, 0x2028de88), line 1386 in "tclParse.c"
NewThread(0x2ff21560), line 442 in "tclThreadTest.c"
_pthread_body(??, ??) at 0xd015ef4c
(dbx) thread
 thread  state-k   wchan state-u   k-tid mode held scope function
 $t1                     running   86693       no   sys  _spin_lock          
 $t2                     running  128679       no   sys  NotifierThreadProc  
>$t3                     running  275557       no   sys  pthread_kill        
(dbx) thread current 1
(dbx) where
_spin_lock(??) at 0xd015d918
pthread_mutex_lock(??) at 0xd015cbb8
TclpMutexLock(0x20027f60), line 311 in "tclUnixThrd.c"
Tcl_DbCkfree(0x20272b18, 0x2000df5c, 0x264), line 476 in "tclCkalloc.c"
TclReleaseLiteral(0x20258d68, 0x2026be18), line 612 in "tclLiteral.c"
TclCleanupByteCode(0x2028ddd8), line 515 in "tclCompile.c"
FreeByteCodeInternalRep(0x2026c668), line 418 in "tclCompile.c"
ConvertToStringType(0x2026c668), line 520 in "tclStringObj.c"
Tcl_SetObjLength(0x2026c668, 0x0), line 279 in "tclStringObj.c"
Tcl_Main(0x1, 0x2ff22164, 0x200258d0), line 237 in "tclMain.c"
main(argc = 1, argv = 0x2ff22164), line 83 in "tclAppInit.c"
(dbx) thread current 2
(dbx) where
NotifierThreadProc(clientData = (nil)), line 874 in "tclUnixNotfy.c"
_pthread_body(??, ??) at 0xd015ef4c
(dbx) 
_________________________________________________________________________

EXPLANATION:

Access to the memory management data structures is controlled
with a mutex called ckallocMutex.  When memory validation is
turned on and a memory block is freed, Tcl_DbCkfree first
runs Tcl_ValidateAllMemory (which locks ckallocMutex, checks
all allocated blocks, unlocks ckallocMutex and returns),
then does ValidateMemory(memp, file, line, TRUE)
and then locks ckallocMutex before continuing the deallocation
procedure.

The problem is that when the last parameter of ValidateMemory
(called "nukeguards") is TRUE, it zeros-out the guard zones of
the memory block which is about to be deallocated.  If another
thread is concurrently doing ValidateAllMemory, it has the mutex
locked and the deallocating thread waits before completing the
deallocation.  The validating thread then finds that the guards
of the block which is now partly deallocated have changed and
reports an error.  In the dbx session above thread 3 is the
"validating thread" and thread 1 is the "deallocating thread".

My fix is just to move the locking of ckallocMutex to just before
the ValidateMemory call, so including the whole deallocation
process inside its protection.  After making this fix I repeated
the test above, starting multiple threads and letting them run
for some time without a crash.

_________________________________________________________________________

PATCH:

*** generic/tclCkalloc.c.orig   Sat Feb 21 01:19:22 1998
--- generic/tclCkalloc.c        Wed Mar  4 17:22:05 1998
***************
*** 468,479 ****
      if (validate_memory)
          Tcl_ValidateAllMemory(file, line);

      ValidateMemory(memp, file, line, TRUE);
      if (init_malloced_bodies) {
        memset((VOID *) ptr, GUARD_VALUE, (size_t) memp->length);
      }

-     TclpMutexLock(&ckallocMutex);
      total_frees++;
      current_malloc_packets--;
      current_bytes_malloced -= memp->length;
--- 468,479 ----
      if (validate_memory)
          Tcl_ValidateAllMemory(file, line);

+     TclpMutexLock(&ckallocMutex);
      ValidateMemory(memp, file, line, TRUE);
      if (init_malloced_bodies) {
        memset((VOID *) ptr, GUARD_VALUE, (size_t) memp->length);
      }

      total_frees++;
      current_malloc_packets--;
      current_bytes_malloced -= memp->length;