Tk Source Code

View Ticket
Login
Ticket UUID: 450bb0ecadd7121c8cfec7dfa67c4cbf3ab8929a
Title: Aqua: segfault due to [tk busy]
Type: Bug Version: 8.6.5
Submitter: dgp Created on: 2016-03-01 21:29:36
Subsystem: 18. [text] Assigned To: dgp
Priority: 5 Medium Severity: Severe
Status: Closed Last Modified: 2016-07-21 20:11:08
Resolution: Fixed Closed By: dgp
    Closed on: 2016-07-21 20:11:08
Description:
This script segfaults Aqua Tk:

frame .f
tk busy hold .f
destroy .f
text .t
.t insert 1.0 abc\tdefg
exit

It is the [.t insert] that segfaults
in the TkBindEventProc call made within
a GenerateWidgetViewSyncEvent with
TkTextInvalidateLineMetrics.

I'm guessing the [tk busy] has left behind
some event that no longer has what it needs
when triggered.

This is a distilled demo script boiled down
from a segfault in a normal run of the Tk test
suite in test font-23.1 after test busy-2.4.
User Comments: dgp added on 2016-07-21 20:10:45:
Merged to 8.6 & 8.6.6 to get a fix released.

dgp added on 2016-03-02 02:43:16:
branch committed.

dgp added on 2016-03-02 02:36:08:
So the trouble here has a number of facets.

Tk has a routine Tk_IdToWindow() to map
Window values to Tk_Window values.  There are a
number of places within Tk where it is expected
that a value of Window == None passed in to
Tk_IdToWindow() will result in a value of NULL
back out.  However, there are no assertions or
checks in place to keep this true, so if somehow
the Window value "None" is added as a key to the
display's "winTable" hash, then any
None passed in can be induced to return an actual
Tk_Window value -- one which may well no longer
exist.

So as a minimum recommendation, I'd add code to
force Tk_IdToWindow() to never consult the table
when the key value passed in is None.  

Beyond that safety, is the matter of how the bad
Window ID is getting in to the system in the first
place.  It comes down to the MacOSX implementation
of TkpMakeTransparentWindowExist() -- which is a
completely empty no-op.  It seems that the [tk busy]
command is just not implemented (fully?) on Aqua Tk.
Given that reality, I think I would prefer a [tk busy] that
more explicitly either raised error, or quietly did nothing
rather than something that goes through the motions
but fails in poorly specified ways.

Here the failure is that the caller of TkpMakeTransparentWindowExist()
is expecting at a minimum to have  the "window" field
of the tkwin struct passed in filled with the window id of
the window that's been brought into existence.  Here
Aqua does nothing, so it leaves the value as None, and then
the caller registers that key in the display's winTable directly.

So later on, in this demo case, a text widget that's not fully
constructed yet holds value of None for its Window ID to signal
that fact, but when it passes that to Tk_IdToWindow() ready to
handle the NULL that might come back, instead it gets back
the Tk_Window of the busy window that's already been freed.
Attempt to access the fields of that freed Tk_Window is what
segfaults.

So the additional change I would make is to have the
routine MakeTransparentWindowExist() not register any
None key in the winTable .

Other possibilities that might also be useful would be to
implement [tk busy] on Aqua, or make a more glaring
choice not to, or possibly to have TkpMakeTransparentWindowExist()
be a nested call to TkpMakeWindow() (Very unsure about that).

I'll commit a bugfix branch soon with the proposed fix for review
and merging to trunk.

dgp added on 2016-03-02 02:15:11:
Good news.  There's a single flaw here and its behind
all the segfaults I was seeing in the test suite.

dgp added on 2016-03-01 21:59:51:
I suspect that's a matter of exposing, rather than creating
the bug, and the real flaw is in the busy machinery not
cleaning up properly.

dgp added on 2016-03-01 21:57:59:
Bisecting blames the TIP 438 checkin to trunk.