Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: d5f544f400e85b9829a6876f4d6a48563a6f85881aa6e920d622b15c32fbd4ce
Ticket: 87271f7cd6ab29a076b36e31041a8c940c20d943
TclOO + coroutine + event loop = coredump
User & Date: apnadkarni on 2024-05-06 17:14:14
Changes

  1. icomment:
    This bug exists in 9.0 as well. Below is the diagnosis. This is my first look at
    the tcloo implementation so caveat emptor! Apologies for the length. It is to
    keep things straight in my own head as to what is going on.
    
    When a oo method is created, a `ProcedureMethod` structure is allocated and
    initialized (`pmPtr` in the analysis below). A `Proc` structure containing the arg
    definitions, byte code etc. hangs off the procPtr field in the structure.
    Crucially, the `procPtr->cmdPtr` is (by design) set to NULL and is populated
    when the method is actually called. **Note this datum is part of the per-method
    "static" data (not on stack) and that is an obvious problem as recursive
    calls will overwrite the value.** More on this below.
    
    When the method is called (InvokeProcedureMethod), a PMFrameData struct is
    allocated on the Tcl stack. There are two fields relevant to this bug. The first
    is the `cmd` field which is a `Command` struct. On a method call, this struct
    is filled out, as the comment in the code
    states, with mostly bogus values, the valid ones being the ones
    (afaict) that are needed to describe the frame. **The `procPtr->cmdPtr` field
    above (part of the per-method data) is then set to point to this field.**
    
    The issue with the `procPtr` field being part of the method data and not on any
    stack resulted in the crash on recursive calls as reported in bug [3001438].
    Fixing this introduced the second relevant field in PMFrameData - `oldCmdPtr`.
    On a method call, the current value stored in the method data
    `pmPtr->procPtr->cmdPtr` field is stored in the `oldCmdPtr` field and
    `pmPtr->procPtr->cmdPtr` is updated to point to the "dummy" `PMFrameData.cmd`
    field before the actual method evaluation. **After the method evaluation,
    the reverse happens with `pmPtr->procPtr->cmdPtr` being restored from
    `oldCmdPtr` to its original value at the time of the method invocation.**
    Essentially a linked stack.
    
    This solved the [3001438] recursive call crash resulting from **nested**
    calls. However, in the current bug report, the calls are not nested, but
    **interleaved** via coroutines resulting in interference between two
    concurrently executing method calls. The restoration of `cmdPtr` during
    the return from the first coroutine call overwrites the `cmdPtr` placed in the
    ProcedureMethod structure at the start of invocation of the second coroutine.
    (Remember the second coroutine is still in execution state)
    Since the original `cmdPtr` at method definition time is NULL, the invocation
    from the second coroutine results in a NULL pointer access.
    
    The sequence is illustrated after the test code below. The code is slightly simplified from the
    original with unnecessary fragments removed.
    
    ```
    # at least 2 instances needed for coredump
    set nworkers 2
    set nrounds 1
    
    proc main {} {
        global nworkers
        for {set n 1} {$n <= $nworkers} {incr n} {
            A create a$n
            puts "a$n namespace [info object namespace a$n]"
            a$n schedule
        }
        after 5000 {puts STOPPING;variable forever false}
        variable forever true
        puts vwaiting
        vwait forever
        return 0
    }
    
    oo::class create A {
        method schedule {} {
            puts "[self] schedule Enter"
            set coro ::coro-[string trim [self] :]
            if {[llength [info commands $coro]] == 0} {
                coroutine $coro my Work
            }
            puts "[self] schedule Exit"
        }
        method Work {} {
            puts "[self] Work Enter"
            after 0 [info coroutine]
            puts "[self] Work yielding"
            yield
            puts "[self] Work back from yield"
            global nrounds
            puts "[self] calling dump"
            dump
            # with "after 0 [self] dump-frames" there is no coredump
            puts "[self] Work Exit"
        }
    }
    
    proc dump {} {
        puts "INFO FRAME=[info frame]"
        for {set i 1} {$i < [info frame]} {incr i} {
            puts [format "\t%2d: %s" $i [info frame $i]]
        }
    }
    
    main
    
    ```
    
    This results in the following sequence resulting in the `cmdPtr` being NULL
    (a1 and a2 are the two objects created)
    
    1. a1 is created. At this time the pmPtr->procPtr->cmdPtr corresponding to
    a1.Work is initialized to NULL.
    
    2. a1.Work is called (from a1.schedule). The current value of pmPtr->procPtr->cmdPtr
    (which is NULL from the method definition time) is saved to fdPtr->oldCmdPtr and
    a new value &fdPtr->cmd is assigned to it (the "bogus" or "dummy" Command struct).
    
    3. a1.Work arranges for itself to be called back through the event loop and then
    yields back to a1.schedule which returns.
    
    4. a2 is created.
    
    5. Steps 2 and 3 are repeated but for a2. Note however that pmPtr->procPtr->cmdPtr
    is not NULL here, it holds the value from the a1.Work call! This is not
    consistent but will ignore for the moment because it is not the cause of the crash.
    
    6. Calls unwind to the main thread which enters a vwait loop. Now the fun begins.
    
    7. a1.Work returns from the yield (via the coroutine call from the event loop).
    It dumps the trace information. Then during method exit, **it restores what it
    saved in its fdPtr->oldCmdPtr back into pmPtr->procPtr->cmdPtr**! Note the
    latter is method data and shared with the invocation of a2.Word. Moreover, the
    value of `oldCmdPtr` (from step 2) is NULL. Control is now returned back to the
    event loop.
    
    8. a2.Work now returns from its yield (again via from the event loop). It too calls dump.
    **But now, the pmPtr->procPtr->cmdPtr value it set up in its call initialization
    has been overwritten by NULL as part of a1.Work exit in step 7.**
    
    9. Crash in TclInfoFrame on attempt to access pmPtr->procPtr->cmdPtr.
    
    Above is easily corroborated with a few puts in the test case.
    
    Fixing the specific crash as dkf mentioned in an earlier post, could be
    
    1. Just check for the NULL pointer in TclInfoFrame and ignore the frame or do some default.
    Aside from very much being a band aid and not palatable, I do not know the
    repercussions on users of info frame.
    
    2. Fix the core issue. DKF or someone with better knowledge of the oo
    implementation than me (everything I know is up above!) will have to do (2). I'm
    likely to introduce more bugs than I fix.
    
    Although `info frame` itself may or may not be important, I'm more worried about
    possibly more serious implications of interleaved oo method calls from different
    coroutines getting tangled up.
    
  2. login: "apnadkarni"
  3. mimetype: "text/x-markdown"