Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2022-12-29
19:05 • Closed ticket [536ae0326d]: tsv::set or get in thread 2.8.8 crash plus 7 oth... artifact: 70f2295919 user: jan.nijtmans
05:03 • Ticket [536ae0326d]: 3 changes artifact: 317afd3be3 user: chw
04:20 • Ticket [536ae0326d]: 3 changes artifact: df4f86ebb7 user: anonymous
2022-12-28
12:56 • Ticket [536ae0326d]: 3 changes artifact: b1ce17a006 user: chw
12:41 • Ticket [536ae0326d]: 3 changes artifact: 9be415cac1 user: anonymous
07:04 • Ticket [536ae0326d]: 3 changes artifact: 6b212c3895 user: chw
00:12 • New ticket [536ae0326d]. artifact: 87c742143f user: anonymous

tsv::set or get in thread 2.8.8 crash

Closed / Fixed536ae0326dd09dc372d5e7ab9637384ed9221393Created 4 years agoUpdated 4 years ago
Descriptionplain2022-12-28 00:12:34
The below script crashes either of tclsh or wish. It's using Threads and tsv. It only seems to crash Thread 2.8.8 regardless of which of several versions of tcl I choose, on either windows or linux.

Please adjust the auto_path settings below for your system and choose one of 7,8, or 0 to choose which version it will force package Thread to use. It should work ok on other than 2.8.8.

I've tried 8.6.12, 8.6.13, and an 8.7a6 on windows, and 8.6.12 or 8.6.13 on linux, all 64 bit systems.

This crash is occuring for me on both windows (silent crash, thinks for 5 seconds then dies) and linux, where I have built tcl/tk with debug symbols and get a segv running it against my build and with gdb.



I believe the cause is a shimmering problem when using tsv::get (or set) on a tsv shared variable (foo bar) that it thinks is a list, and so has to convert. The crash occurs because the pointer objPtr->typePtr it's attempting to use is 0.

I can't get the same info on windows, since I don't have a way to build tcl/tk with debugging. 

In the below, I place it in file crash.tcl and source it.

----------------

    set use 8
    if { $use == 8 } {
        lappend auto_path {C:\Users\core5\AppData\Local\Apps\Tcl86\lib\thread2.8.8}
        set tver [package require -exact Thread 2.8.8]
    } elseif { $use == 7  } {
        lappend auto_path {C:\tclf\tclpkgs64}
        set tver [package require  -exact Thread 2.8.7]
    } else {
        lappend auto_path {C:\tclf\tclpkgs64}
        set tver [package require   Thread]
    }
    puts "tver= |$tver| tcl= [info pa]" 
    
    catch {console show}
    catch {wm withdraw .}
    update
    tsv::set foo bar {}
    
    tsv::lpush      foo    bar {a b c} end
    tsv::lpush      foo    bar {d e f} end
    set len [tsv::llength foo bar]
    puts "len= |$len| "
    set zzz2 [tsv::lpop foo bar]
    puts "zzz2= |$zzz2| "
    
    set script {
        set ret [tsv::get foo bar]
    }
    set tid [thread::create]
    puts "tid= |$tid| "
    
    set zzza [thread::send $tid $script]
    puts "zzza= |$zzza| "
    
    set script2 {
        set ret [tsv::lpop foo bar]
    }
    set script3 {
        set ret [tsv::llength foo bar]
    }
    set script4 {
        set ret [tsv::set foo bar]
    }
    set script5 {
        set ret [tsv::get foo bar]
    }
    foreach item {1 2} {
        tsv::lpush      foo    bar [list a b $item 1] end
        tsv::lpush      foo    bar [list a b $item 2] end
        
        set zzz2 [thread::send $tid $script2] ;# pop
        puts "zzz2= |$zzz2| "
        
        set zzz3 [thread::send $tid $script3] ;# len
        puts "zzz3= |$zzz3| "
        
        set zzz4 [thread::send $tid $script4] ;# set
        puts "zzz4= |$zzz4| "
        
        set zzz5 [thread::send $tid $script5] ;# get
        puts "zzz5= |$zzz5| "
        
    }
    set tid [thread::id]
    foreach item {1 2 3} {
        
        set zzz2 [thread::send $tid $script2] ;# pop
        puts "zzz2= |$zzz2| "
        
        set zzz3 [thread::send $tid $script3] ;# len
        puts "zzz3= |$zzz3| "
        
        set zzz4 [thread::send $tid $script4] ;# set
        puts "zzz4= |$zzz4| "
        
        set zzz5 [thread::send $tid $script5] ;# get
        puts "zzz5= |$zzz5| "
        update
    }
    puts "ok"
    update







[2496]$ LD_LIBRARY_PATH=/home/et/vwtests/mytcltk/build13/lib gdb /home/et/vwtests/mytcltk/build13/bin/wish8.6
Reading symbols from /home/et/vwtests/mytcltk/build13/bin/wish8.6...
(gdb) r
Starting program: /home/et/vwtests/mytcltk/build13/bin/wish8.6 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
% [New Thread 0x7ffff5bae700 (LWP 4705)]

% 
% source crash.tcl
tver= |2.8.8| tcl= 8.6.13
len= |2| 
zzz2= |a b c| 
[New Thread 0x7ffff538e700 (LWP 4707)]
tid= |tid0x7ffff538e700| 
zzza= |{d e f}| 
zzz2= |d e f| 
zzz3= |2| 
zzz4= |{a b 1 1} {a b 1 2}| 
zzz5= |{a b 1 1} {a b 1 2}| 
zzz2= |a b 1 1| 
zzz3= |3| 
zzz4= |{a b 1 2} {a b 2 1} {a b 2 2}| 
zzz5= |{a b 1 2} {a b 2 1} {a b 2 2}| 
zzz2= |a b 1 2| 
zzz3= |2| 
zzz4= |{a b 2 1} {a b 2 2}| 
zzz5= |{a b 2 1} {a b 2 2}| 
zzz2= |a b 2 1| 
zzz3= |1| 
zzz4= |{a b 2 2}| 
zzz5= |{a b 2 2}| 
zzz2= |a b 2 2| 
zzz3= |0| 

Thread 1 "wish8.6" received signal SIGSEGV, Segmentation fault.
Tcl_GetString (objPtr=0x55555587cab0) at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclObj.c:1642
1642	    if (objPtr->typePtr->updateStringProc == NULL) {

(gdb) backtrace 
#0  Tcl_GetString (objPtr=0x55555587cab0) at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclObj.c:1642
#1  0x00007ffff7d2b54b in Tcl_GetStringFromObj (objPtr=0x55555587cab0, lengthPtr=0x7fffffffd1f0) at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclObj.c:1692
#2  0x00007ffff7ccd723 in TEBCresume (data=0x5555556479a8, interp=0x555555570070, result=0) at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclExecute.c:2776
#3  0x00007ffff7bee822 in TclNRRunCallbacks (interp=0x555555570070, result=0, rootPtr=0x0) at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclBasic.c:4503
#4  0x00007ffff7bf1616 in TclEvalObjEx (interp=0x555555570070, objPtr=0x5555555b59e0, flags=131072, invoker=0x0, word=0)
    at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclBasic.c:6068
#5  0x00007ffff7bf15aa in Tcl_EvalObjEx (interp=0x555555570070, objPtr=0x5555555b59e0, flags=131072) at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclBasic.c:6049
#6  0x00007ffff7cee179 in Tcl_RecordAndEvalObj (interp=0x555555570070, cmdPtr=0x5555555b59e0, flags=131072)
    at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclHistory.c:190
#7  0x00007ffff7cedde7 in Tcl_RecordAndEval (interp=0x555555570070, cmd=0x7fffffffdcf0 "source crash.tcl\n", flags=131072)
    at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclHistory.c:75
#8  0x00007ffff7e63488 in StdinProc (clientData=0x7fffffffdcd0, mask=2) at /home/et/vwtests/mytcltk/tk8.6.13/unix/../generic/tkMain.c:443
#9  0x00007ffff7d0283b in Tcl_NotifyChannel (channel=0x55555566d9f0, mask=2) at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclIO.c:8425
#10 0x00007ffff7d84ba1 in FileHandlerEventProc (evPtr=0x5555556636c0, flags=-3) at /home/et/vwtests/mytcltk/tcl8.6.13/unix/tclUnixNotfy.c:808
#11 0x00007ffff7d2a046 in Tcl_ServiceEvent (flags=-3) at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclNotify.c:670
#12 0x00007ffff7d2a486 in Tcl_DoOneEvent (flags=-3) at /home/et/vwtests/mytcltk/tcl8.6.13/generic/tclNotify.c:967
#13 0x00007ffff7e4cbf5 in Tk_MainLoop () at /home/et/vwtests/mytcltk/tk8.6.13/unix/../generic/tkEvent.c:2109
#14 0x00007ffff7e63252 in Tk_MainEx (argc=-1, argv=0x7fffffffdfd0, appInitProc=0x5555555551da <Tcl_AppInit>, interp=0x555555570070)
    at /home/et/vwtests/mytcltk/tk8.6.13/unix/../generic/tkMain.c:376
#15 0x00005555555551d3 in main (argc=1, argv=0x7fffffffdfc8) at /home/et/vwtests/mytcltk/tk8.6.13/unix/../unix/tkAppInit.c:93

(gdb) print objPtr
$1 = (Tcl_Obj *) 0x55555587cab0

(gdb) print *objPtr
$2 = {refCount = 3, bytes = 0x0, length = 0, typePtr = 0x0, internalRep = {longValue = 93824995542336, doubleValue = 4.6355707018676314e-310, 
    otherValuePtr = 0x55555587c540, wideValue = 93824995542336, twoPtrValue = {ptr1 = 0x55555587c540, ptr2 = 0x55555587cae0}, ptrAndLongRep = {ptr = 0x55555587c540, 
      value = 93824995543776}}}
chwplain2022-12-28 07:04:35
Please try the following fix against a 2.8.8 checkout

Index: generic/threadSvCmd.c
==================================================================
--- generic/threadSvCmd.c
+++ generic/threadSvCmd.c
@@ -1056,11 +1056,13 @@
     /*
      * Handle the string rep
      */
 
     if (objPtr->bytes == NULL) {
-        dupPtr->bytes = NULL;
+        if (dupPtr->bytes != Sv_tclEmptyStringRep) {
+            dupPtr->bytes = NULL;
+        }
     } else if (objPtr->bytes != Sv_tclEmptyStringRep) {
         /* A copy of TclInitStringRep macro */
         dupPtr->bytes = (char*)ckalloc((unsigned)objPtr->length + 1);
         if (objPtr->length > 0) {
             memcpy((void*)dupPtr->bytes,(void*)objPtr->bytes,
anonymousplain2022-12-28 12:41:34
Thanks chw, that works.
chwplain2022-12-28 12:56:57
Good. Now I believe you should transfers this ticket
to the thread package on https://core.tcl-lang.org/thread
and hope that soon one of the maintainers picks it up.
anonymousplain2022-12-29 04:20:29
I opened a ticket there pointing back to this one.

Interesting, your fix would seem to indicate that the actual problem is when the variable is an empty list, and this also explains why my simpler tests didn't crash, they all had something besides null for the list.

It might also be that only after a true list operation, that causes the list to become empty, that trying to access it via a non-list tsv:: command then causes the crash, as it tries to shimmer between a null list and text. 

That might be a far simpler way to force the error than my script, which didn't fail until I tried those commands on the list once it was empty again.

good catch!
chwplain2022-12-29 05:03:06
The fix catches the case when an empty list in the shared variable
needs to be duplicated to be returned to the caller. In the duplication
step, the duplicated Tcl_Obj becomes the special empty string and the
postprocessing of the duplicated Tcl_Obj by inspecting the original
Tcl_Obj did not consider the special empty string form. Later on that
malformed Tcl_Obj triggers a crash when Tcl_GetString() is applied on it.

Scary is, that this problem lived obviously undetected for a long time.
jan.nijtmanswiki2022-12-29 19:05:51

Fixed in this ticket.

Thanks for the report and (by Christian Werner) the fix!

Log in (anonymous is fine) to add a comment.