Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 3830ec22003d56ff79963a801c9b58d4de0fcef1690d8e7e9e368d9884653b57
Ticket: 0b809cd3fc8b6e5e19cd8a9a91357e425472cd69
SIGSEGV on shutdown with TclOO, coroutines, namespaces, and -DPURIFY
User & Date: andy on 2024-12-03 23:04:58
Changes

  1. icomment:
    I investigated further.  `-DPURIFY` is not itself the problem, rather it causes the program to immediately crash upon use-after-free.  Omitting `-DPURIFY` only succeeds in masking the problem.  It might be possible to crash even without `-DPURIFY` by somehow causing allocation between the free and the use-after-free.
    
    Curious if the problem could be triggered by child interpreters without using `-DPURIFY` (the answer is no), I amended the test script, while sticking with commit [b688a485b2](https://core.tcl-lang.org/tcl/info/b688a485b253869b):
    
    ```
    interp create child
    child eval {
        oo::class create class
        oo::define class method yield {} {yield}
        namespace eval ns {coroutine coro [class new] yield}
    }
    interp delete child
    ```
    
    The `oPtr` passed to `TclOOObjectName` comes from `mPtr->declaringClassPtr->thisPtr`.  Given this exact test script and Tcl version, this field is set by the 21st crossing of tclOO.c:1686 (`TclOOAllocClass`).  When using `-DPURIFY`, it later gets trashed by `Tcl_Free` called by the 2nd crossing of tclOO.c:1384 (`TclOODecrRefCount`).  When not using `-DPURIFY`, the class struct pointer is still passed to `Tcl_Free`, but said function does not wipe the class struct, thus its fields can be read without complaint.  At least until the next allocation happens, I would imagine.
    
    The trouble arises because the class is deallocated before the coroutine which yielded inside a class method, but the suspended coroutine cleanup then wants to include the class name in the error report.  I'm not quite sure where the error report is expected to go, but it's being made because `TEBCresume` was instructed to resume execution of a rewound coroutine.
    
    To see the problem, get the above-mentioned crash script and Tcl commit tree, build with `--enable-symbols` and either with or without `-DPURIFY`, then do:
    
    ```
    cd
    gdb --args tcl/install/bin/tclsh9.0 crash.tcl
    start
    tb tclOO.c:1686
    ignore 2 20
    tb tclOO.c:1384
    ignore 3 1
    c
    p clsPtr
    c
    p oPtr->classPtr
    n
    rwatch -l oPtr->classPtr->thisPtr
    c
    ```
    
    Running the above sequence shows that `oPtr->classPtr->thisPtr` gets read by `MethodErrorHandler` despite having called `TclFree(oPtr->classPtr)`.
    
    Repeating, this time showing selected gdb output:
    
    ```
    $ cd
    $ gdb --args tcl/install/bin/tclsh9.0 crash.tcl
    (gdb) start
    Temporary breakpoint 1 at 0x401185: file /root/tcl/unix/tclAppInit.c, line 91.
    (gdb) tb tclOO.c:1686
    Temporary breakpoint 2 at 0x7ffff7f326d3: file /root/tcl/generic/tclOO.c, line 1686.
    (gdb) ignore 2 20
    Will ignore next 20 crossings of breakpoint 2.
    (gdb) tb tclOO.c:1384
    Temporary breakpoint 3 at 0x7ffff7f31fe1: file /root/tcl/generic/tclOO.c, line 1384.
    (gdb) ignore 3 1
    Will ignore next crossing of breakpoint 3.
    (gdb) c
    Continuing.
    Temporary breakpoint 2, TclOOAllocClass (interp=0x4ac250, useThisObj=0x5880a0) at /root/tcl/generic/tclOO.c:1686
    1686        clsPtr->thisPtr = useThisObj;
    (gdb) p clsPtr
    $1 = (Class *) 0x587ca0
    (gdb) p useThisObj
    $2 = (Object *) 0x5880a0
    (gdb) c
    Continuing.
    Temporary breakpoint 3, TclOODecrRefCount (oPtr=0x5880a0) at /root/tcl/generic/tclOO.c:1384
    1384                Tcl_Free(oPtr->classPtr);
    (gdb) p oPtr->classPtr
    $3 = (Class *) 0x587ca0
    (gdb) n
    (gdb) rwatch -l oPtr->classPtr->thisPtr
    Hardware read watchpoint 4: -location oPtr->classPtr->thisPtr
    (gdb) c
    Continuing.
    Hardware read watchpoint 4: -location oPtr->classPtr->thisPtr
    Value = (Object *) 0x5880a0
    0x00007ffff7f49da4 in MethodErrorHandler (interp=0x4ac250, dummy1213=0x54b0a0) at /root/tcl/generic/tclOOMethod.c:1231
    1231            declarerPtr = mPtr->declaringClassPtr->thisPtr;
    ```
    
    For the record, I did this test using Slackware64 15.0 plus gcc-14.2.0-x86_64-2.txz, binutils-2.43.1-x86_64-1.txz, and glibc-2.40-x86_64-5.txz from Slackware64-current.
    
  2. login: "andy"
  3. mimetype: "text/x-markdown"
  4. username: "andy"