Tk Source Code

Artifact [9544c763]
Login

Artifact 9544c7637e16c1127938a6dfe0b15672f80070cc8f0e842e07f8bf3cad2dd7f2:

Attachment "b964b98d-repro.tcl" to ticket [b964b98d] added by anonymous 2026-10-04 13:25:57. (unpublished)
# Standalone reproducer for Tk ticket b964b98d1b: a use-after-free in the
# ttk geometry manager's relayout.
#
#   wish9.1 b964b98d-repro.tcl ?iterations?     (default 200)
#
# A ttk::panedwindow with four panes. A <Configure> binding on pane .pw.b
# destroys that pane. Tk_MoveResizeWindow delivers <Configure> synchronously
# (TkDoConfigureNotify), so the binding runs while Ttk_PlaceContent is placing
# that very pane from the manager's idle relayout (ManagerIdleProc ->
# RecomputeLayout -> PanedPlaceContent -> PlacePanes). The destroy frees the
# window and its manager records; the placement then reads them.
#
# Prints SURVIVED at the end. A release build often survives, since Tcl's
# allocator keeps the freed block; build Tcl and Tk with -DPURIFY and run
# under valgrind (or ASan) to see the invalid reads.
set iters 200
if {$argc >= 1} { set iters [lindex $argv 0] }

after 600000 {puts TIMEOUT; flush stdout; exit 42}
wm geometry . 700x400+60+60

ttk::panedwindow .pw -orient horizontal
pack .pw -fill both -expand 1

set ::armed 0
proc rebuild {} {
    foreach w {a b c d} {
        catch {destroy .pw.$w}
    }
    foreach w {a b c d} {
        ttk::frame .pw.$w -width 120 -height 200
        .pw add .pw.$w -weight 1
    }
    # When pane b is re-placed, destroy b itself (once per cycle): its
    # DestroyNotify is delivered synchronously inside the placement call, so
    # RemoveContent frees the Ttk_Content/Pane records that
    # Ttk_PlaceContent/PlacePanes still hold: use-after-free on return.
    bind .pw.b <Configure> {
        if {$::armed} { set ::armed 0; catch {destroy .pw.b} }
    }
}

rebuild
update

for {set i 0} {$i < $iters} {incr i} {
    rebuild
    update
    set ::armed 1
    # queue relayout work, then force placement:
    catch {.pw sashpos 0 [expr {40 + ($i % 60)}]}
    catch {.pw sashpos 1 [expr {160 + ($i % 40)}]}
    wm geometry . [expr {680 + ($i % 3) * 24}]x[expr {380 + ($i % 5) * 12}]+60+60
    update
    if {$i % 25 == 0} {puts "iter $i"; flush stdout}
}
puts SURVIVED; flush stdout
exit 0