Tk Source Code

View Ticket
Login
Ticket UUID: 4994f5dba496b57d8d38bf3d61c4d46eb60e523e
Title: Segmentation fault on Mac OS X/11+
Type: Bug Version: 8.6.11 and later
Submitter: nemethi Created on: 2022-01-22 16:17:18
Subsystem: 66. Aqua Window Operations Assigned To: fvogel
Priority: 5 Medium Severity: Critical
Status: Closed Last Modified: 2022-08-15 03:11:47
Resolution: Fixed Closed By: chrstphrchvz
    Closed on: 2022-08-15 03:11:47
Description:
I am experiencing a mysterious crash with Tk 8.6.11 and later (but not with earlier Tk versions) on Mac OS X/11/12.  I have spent several days trying to build a simple script demonstrating this crash, but, unfortunately, I am only able to reproduce it using my Tablelist package (see https://www.nemethi.de).  Apparently, the segmentation fault can only be reproduced with a script that is complex enough, a prerequisite that is surely fulfilled by the Tcl-only Tablelist library.

The attached test script "crash.tcl" itself is rather simple, but it sources one of the Tablelist demo scripts:

---
package require tablelist_tile
source $tablelist::library/demos/dirViewer_tile.tcl

proc resetView tbl {
    $tbl delete 0 end

    #
    # The output produced by the optional code below might ease the debugging:
    #
    puts "\n*** tablelist-own after idle callbacks:"
    foreach id [after info] {
        lassign [after info $id] callback kind
        if {$kind eq "idle"} { puts "  $callback" }
    }

    #
    # The update idletasks invocation below will raise a SIGSEGV signal:
    #
    puts "\n*** invoking update idletasks"
    update idletasks
    puts "*** update idletasks finished\n"
}

after 2000 [list resetView .tf.tbl]
---

Pass the above script to Wish and wait for 2 seconds.  You will see that "update idletasks" gets invoked but is not finished, because of a SIGSEGV signal that kills Wish.  The content of the error trace window that pops up due to the segmentation fault is contained in the attached file "problemReport.txt".

The last Tcl command invoked before the crash is "place forget ..." in line #2687 (as of Tablelist 6.17) of the file "scripts/tablelistUtil.tcl".  If you remove or comment out this line, the crash will no longer occur.

I suspect (but am not sure) that the segmentation fault is caused by an interference of the idle callbacks at script level with the processing of the idle events used for drawing on the Mac.
User Comments: chrstphrchvz added on 2022-08-15 03:11:47:

I’ve confirmed the issue was present in [e69b7b266d83] and fixed in [4584332529a4].


nemethi (claiming to be Csaba Nemethi) added on 2022-08-14 19:34:56:
You are right Christopher, this bug seems to no longer exist.

I have built wish from the latest version of the core-8-6-13-rc branch.  With this wish version, the script that you posted on 2022-01-25 works just fine.

Newer Tablelist versions contain a workaround for this crash on the Mac.  Now I have removed that workaround and tested the new wish version with my script that uses the Tablelist demo script dirViewer_tile.tcl.  I am glad to report that this test has been successful, too.

Many thanks to you and Francois for your excellent work!

chrstphrchvz added on 2022-08-14 12:31:53:

I haven’t confirmed, but I suspect this issue no longer exists as of [55503ee18c84].


chrstphrchvz added on 2022-01-25 01:21:10:

I should clarify (at least for myself): the relevant Tk_UnmapWindow() call leading to recursion is not the one directly in Tk_PlaceObjCmd() but rather the one in Tk_UnmaintainGeometry() (used due to place being invoked with -in other than the parent window).


chrstphrchvz added on 2022-01-25 01:16:51:

Here is a simpler script which I believe triggers the same issue:

place [frame .f1 -background green1 -height 150 -width 150] -x 25 -y 25
place [frame .f2 -background green4 -height 100 -width 100] -in .f1 -x 25 -y 25

after 1000 { after idle {place forget .f2} after idle {place forget .f2} update idletasks }


chrstphrchvz added on 2022-01-25 00:47:00:

I suspect that any other functions which use Tk_MapWindow()/Tk_UnmapWindow() (the common factor being their reliance on TkpHandleMapOrUnmap()) could also be affected by this issue.


chrstphrchvz added on 2022-01-25 00:41:40:

I now see the problem with my breakpoints/print debug statements in Tk_PlaceObjCmd(): they do work, but I neglected that Tk_PlaceObjCmd() is being called again deep within the Tk_UnmapWindow() call of the Tk_PlaceObjCmd() already in progress, and this first/outermost call is where the crash is occurring. So the problem involves Tk_PlaceObjCmd() not tolerating the recursion that calling Tk_UnmapWindow() can lead to (pointers it uses get freed/set to NULL during Tk_UnmapWindow()).

I want to think a simpler script could trigger this, but I’ve not yet figured one out.


nemethi (claiming to be Csaba Nemethi) added on 2022-01-24 18:12:26:
Christopher, many thanks, for your help!

In order to show that the idle callbacks at script level interfere with the processing of the idle events used for drawing on the Mac, I have done the following:

1. Edited the file tcl8.6.11/generic/tclTimer.c, localized the function TclServiceIdle(), and inserted the lines

if (idlePtr->proc == AfterProc) {
    puts("TclServiceIdle(): processing script-level 'after idle' callback");
}

just before its line

        idlePtr->proc(idlePtr->clientData);

2. Edited the file tcl8.6.11/macosx/tkMacOSXWindowEvent.c, localized the method generateExposeEvents(), and enclosed its line

        while (Tcl_DoOneEvent(TCL_IDLE_EVENTS)) {}

between

puts("\n-> generateExposeEvents(): starting while loop for idle events");

and

puts("<- generateExposeEvents(): while loop for idle events finished");

The output produced by passing the script crash.tcl to wish8.6.11 is contained in the attached file "output_8.6.11.txt".  Its most relevant part is:

---
*** invoking update idletasks
TclServiceIdle(): processing script-level 'after idle' callback

-> generateExposeEvents(): starting while loop for idle events
TclServiceIdle(): processing script-level 'after idle' callback
TclServiceIdle(): processing script-level 'after idle' callback
TclServiceIdle(): processing script-level 'after idle' callback
TclServiceIdle(): processing script-level 'after idle' callback
<- generateExposeEvents(): while loop for idle events finished

rlwrap: warning: Wish crashed, killed by SIGSEGV.
. . .
---

You can see that 4 out of the 5 (tablelist-own) script-level idle callbacks invoked due to "update idletasks" are being processed during the redrawing via generateExposeEvents()!

After that I repeated this experiment with wish8.6.10.  The corresponding output is contained in the attached file "output_8.6.10.txt, whose most relevant part is:

---
*** invoking update idletasks
TclServiceIdle(): processing script-level 'after idle' callback
TclServiceIdle(): processing script-level 'after idle' callback
TclServiceIdle(): processing script-level 'after idle' callback
TclServiceIdle(): processing script-level 'after idle' callback
TclServiceIdle(): processing script-level 'after idle' callback
*** update idletasks finished


-> generateExposeEvents(): starting while loop for idle events
<- generateExposeEvents(): while loop for idle events finished

. . .
---

This time the redrawing via generateExposeEvents() occurs *after* processing the 5 (tablelist-own) script-level idle callbacks invoked due to "update idletasks", and there is no segmentation fault.

The above seems to confirm that the crash with wish8.6.11 and later is caused by some changes due to which script level idle callbacks are being invoked during the processing of the idle events used for redrawing.

chrstphrchvz added on 2022-01-23 19:42:56:

Reuploaded output for address sanitizer; needed to run with ASAN_OPTIONS=malloc_context_size=100 to not have allocated/freed call stacks truncated to 30 frames.


chrstphrchvz added on 2022-01-23 19:28:58:

Ignore last sentence in previous comment, as Tk_PlaceObjCmd() does Tk_UnmapWindow() which causes Tk Aqua event handling loops to be entered during update idletasks.


chrstphrchvz added on 2022-01-23 15:15:42:

I am having trouble debugging this. In Tk_PlaceObjCmd() under case PLACE_FORGET, it looks as if FindContent() (or possibly the Tcl_FindHashEntry() it uses) erroneously returns a stale value rather than NULL just before the crash. But when I try placing print debug statements and LLDB breakpoints before UnlinkContent() where the use-after-free occurs, something causes them to stop working just before the crash.

Currently I would say this isn’t due to Aqua event loop behavior because the after 2000 … timer event and anything in it including doing update idletasks is (usually? always?) executed from the “main” Tcl event loop and not one in Tk Aqua.


chrstphrchvz added on 2022-01-22 18:51:17:

I reproduced the crash; I have attached the output which has Tcl and Tk compiled with UBSan (-fsantize=null -DPURIFY). It also shows the symbols for Tk.

However I notice that when compiled with address sanitizer (-fsanitize=address -DPURIFY), it instead aborts due to a use-after-free; I have attached that output as well.


chrstphrchvz added on 2022-01-22 18:03:12:

CC’ing myself in case of updates. I haven’t yet tried reproducing or debugging this.


Attachments: