Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-12
19:22 • Closed ticket [32ae34e63a]: chan copy reflected channel segfault plus 4 ot... artifact: 4d0603f507 user: serhiy.storchaka
2019-07-31
19:59 • Ticket [79474c5880] refchan segfault status still Open with 4 other changes artifact: 1622194e55 user: sebres
2015-10-23
14:57
Backout of failed attempt to fix [32ae34e63a] got checked into wrong branch. check-in: ffa88687f8 user: dgp tags: core-8-5-branch
2015-09-25
16:43
Backout failed attempt to fix [32ae34e63a]. check-in: 33902c18c2 user: dgp tags: trunk
16:32 • Open ticket [32ae34e63a]: chan copy reflected channel segfault plus 5 othe... artifact: 1527f81dd6 user: dgp
16:32
Backout failed attempt to fix [32ae34e63a]. closed check-in: 0ce0901f7f user: dgp tags: bug-05489ce335
2015-09-23
17:01 • Closed ticket [32ae34e63a]: chan copy reflected channel segfault plus 7 ot... artifact: a2887c20fe user: dgp
16:58
[32ae34e63a] Prevent segfaults and data corruption when CopyData() is called recursively. Mark new t... check-in: 7ecd0b8c8d user: dgp tags: trunk
16:29
[32ae34e63a] Prevent segfaults and data corruption when CopyData() is called recursively. check-in: 36198cfb7c user: dgp tags: core-8-5-branch
2015-09-22
19:06 • Ticket [32ae34e63a] chan copy reflected channel segfault status still Open... artifact: ffa3db8dd1 user: dgp
2015-08-13
16:52 • Ticket [32ae34e63a]: 3 changes artifact: 0ddd114166 user: dgp
2015-08-12
16:38 • Ticket [32ae34e63a]: 3 changes artifact: ae253117b3 user: dgp
16:37
New test io-53.18 adapted from demo script in [32ae34e63a]. This test segfaults without changes to ... check-in: fa8fb6f35f user: dgp tags: bug-32ae34e63a
15:16 • Ticket [32ae34e63a] chan copy reflected channel segfault status still Open... artifact: 058252b27c user: dgp
2015-08-11
21:12 • Ticket [32ae34e63a]: 3 changes artifact: cd8de14e93 user: aku
20:55 • Ticket [32ae34e63a]: 3 changes artifact: 4a53ecc6d9 user: dgp
20:46 • New ticket [32ae34e63a]. artifact: 7f8ce56d41 user: dgp

[chan copy] reflected channel segfault

Closed / Fixed32ae34e63a3db0a542a6795594914baa1f231007Created 11 years agoUpdated 51 days ago
Descriptionplain2015-08-11 20:46:57
This script segfaults:

    proc driver {cmd args} {
        variable buffer
        variable index
        set chan [lindex $args 0]
        switch -- $cmd {
            initialize {
                set index($chan) 0
                set buffer($chan) [encoding convertto utf-8 \
                        [string repeat a 100]]
                return {initialize finalize watch read}
            }
            finalize {
                unset index($chan) buffer($chan)
                return
            }
            watch {
		if {"read" in [lindex $args 1]} {
		    chan postevent $chan read
		}
		return
            }
            read {
                set n [lindex $args 1]
                set new [expr {$index($chan) + $n}]
                set result [string range $buffer($chan) $index($chan) $new-1]
                set index($chan) $new
                return $result
            }
        }
    }
    proc noop args {}
    proc more {c outChan bytes args} {
        if {[eof $c]} {
            set ::done eof
            catch {close $c}
            return
        }
        if {[llength $args]} {
            set ::done error
        } else  {
            chan copy $c $outChan -command noop
        }
    }
    set c [chan create read [namespace which driver]]
    chan configure $c -encoding utf-8
    set outChan [open out w]
    chan configure $outChan -encoding iso8859-1
    after 100 {set ::done timeout}
    chan copy $c $outChan -size 99 -command [list [namespace which more] $c $outChan]
    vwait ::done
    puts $::done
    close $outChan
    file delete out
    rename driver {}
    rename more {}
    unset ::done
Referenced by7
  • 1622194e55Ticket [79474c5880] refchan segfault status still Open with 4 other changes ticket2019-07-31 19:59:35
  • ffa88687f8Backout of failed attempt to fix [32ae34e63a] got checked into wrong branch. check-in2015-10-23 14:57:56
  • 33902c18c2Backout failed attempt to fix [32ae34e63a]. check-in2015-09-25 16:43:05
  • 0ce0901f7fBackout failed attempt to fix [32ae34e63a]. check-in2015-09-25 16:32:55
  • 7ecd0b8c8d[32ae34e63a] Prevent segfaults and data corruption when CopyData() is called recursively. Mark new test io-53.20 as "knownBug". It demos some unknown flaw in MoveBytes(). check-in2015-09-23 16:58:34
  • 36198cfb7c[32ae34e63a] Prevent segfaults and data corruption when CopyData() is called recursively. check-in2015-09-23 16:29:38
  • fa8fb6f35fNew test io-53.18 adapted from demo script in [32ae34e63a]. This test segfaults without changes to source code. This checkin also contains a revised implementationf of [chan postevent] that stops calling Tcl_NotifyChannel() directly, and queues an event to do it instead. This stops the segfault, but causes tests iocmd-31.[67] to fail. Need advice on whether that matters. check-in2015-08-12 16:37:28
dgpplain2015-08-11 20:55:30
The problem here is that [driver watch] makes a direct call to
[chan postevent] which directly calls Tcl_NotifyChannel.  This
chain of events makes it possible for the internal routine
CopyData() to recursively call itself.

Problem is that CopyData() has not been written to support
a recursive call to itself.  It's written to support a strict
assumption that all calls to it come from an event handler
that has been scheduled by other instances of CopyData().
Specifically each call to CopyData passes in a pointer to
a CopyState struct and management of the lifetime of
that struct doesn't account for any recursive operations,
only event chained operations.
If a recursive call does get made -- as in this demo script --
the inner CopyData() can free the CopyState struct and then
leave the outer CopyData pointing to and using freed memory,
which is the case here.
akuplain2015-08-11 21:12:52
Q&A from the chat

[13:52]	aku	Q's: Would this be fixable by preventing calls to 'NotifyChannel' from a 'WatchProc()' ? Is that check even do-able ? Is that the right fix ? What other fixes are possible ?
[13:55]	dgp	I think the possible fix to to have [chan postevent] just do exactly that.  That's mostly a guess at this point.
[14:02]	dgp	the other fix would be to either refcount or Preserve the CopyState struct.
dgpplain2015-08-12 15:16:12
It appears the problem may be even worse.  Without any encounter
with CopyData() at all, if a [driver watch] directly calls a
[chan postevent] we can have recursive (effectively tail) calls
to DoReadChars().  It appears that this means the inner read
starts after, but ends before the outer read, which appears to
mean that the order of bytes read from a channel can get shuffled.
That is, the later bytes in the channel come back from a read 
operation and can get processed prior to the completion of the
outer read which brings back earlier bytes in the channel.
dgpplain2015-08-12 16:38:22
New branch:

http://core.tcl.tk/tcl/info/fa8fb6f35fa354e2
dgpplain2015-08-13 16:52:35
Latest commit to the branch has a new test demonstrating
another variation of trouble with the [driver watch]
interface.

The ReflectWatch routine checks whether there's any
change in the interest mask from what's already in place,
and if not, it bails without calling [driver watch].

This demands an interface, then, where a single call to
[driver watch] sets up some continuing sequence of
[chan postevent] calls whenever conditions on the reflected
channel warrant.  Since [driver watch] has to return too,
this pretty much insists on some recurring event handler
scheme, and that leaves no(?) choice but timer events via
[after].

Seems like it would be simpler to have all ReflectWatch
calls get reflected in a [driver watch] evaluation for
a file-events-only driven mechanism of continued production
of events.
dgpplain2015-09-22 19:06:40
I'm going to restart on this one.  Changing the fundamental
behavior of [chan postevent] is potentially disruptive, no
matter how much I would like it to be the other way.

Also, Tcl_NotifyChannel() is public so even changing
[chan postevent] won't stop some extension from 
bringing about the same problem.

The alternative fix I'll now be pursuing is making
CopyData() robust against the possibility that it
might get called recursively.
dgpplain2015-09-23 17:01:48
Fix committed.
dgpplain2015-09-25 16:32:56
I'm backing out the fix.  Putting the bug back in place.

The attempt to fix has given rise to many more failures.
Attempts to change CopyData() to behave robustly against
recursive calls are making it much more fragile in
normal operations.  If this bug is to be fixed, it's
going to have to be done with much more care, and within
an arena much better guarded by tests to expose missteps.
serhiy.storchakawiki2026-10-12 19:22:40

The segfault was fixed by the fix for [79474c5880] (8.6.15): the CopyState is now reference counted. The script crashes with 8.6.14 and works with 8.6.15, 9.0 and 9.1. It now ends with "timeout", because the driver posts only one event from watch, and watch is only called again when the interest changes.

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