Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2000-10-26
08:30 • Closed ticket [218866ffff]: Crashing when deleting the global literal table ... artifact: 4f7317bee3 user: welch
01:06 • New ticket [218866ffff]. artifact: 3b0aef318b user: nobody

Crashing when deleting the global literal table

Closed / Fixed218866ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 01:06:56
OriginalBugID: 2459 Bug
Version: 8.1.1
SubmitDate: '1999-07-28'
LastModified: '1999-10-21'
Severity: CRIT
Status: Released
Submitter: techsupp
ChangedBy: hobbs
RelatedBugIDs: 2515
OS: Windows NT
OSVersion: NT4 Service Pack 3/4/5
Machine: X86
FixedDate: '1999-10-21'
FixedInVersion: 8.2.2
ClosedDate: '2000-10-25'


Name:

David Whitehouse



Extensions:

none



CustomShell:

none



ReproducibleScript:

This isn't short, but this is quite difficult to explain. See section 7)

    Describe the undesirable behaviour.



ObservedBehavior:

Undesirable behaviour: It core-dumps.

    

    

    Entropic have a tool which automatically generates Tcl from a

    multi-level network. It is basically a graphical multi-level switch

    statement generator.

    

    

    We are having problems with some of this generated code. Below is a

    segment of Tcl code which is causing the Tcl interpreter trouble. (It is

    not the complete set of code simply a fragment - The code we are using

    is currently about 390K in size, with lots of augmented functions to the

    interpreter)

    

    

    namespace eval x {

    

     proc State9 {ident state_number tokenname count} {

    upvar $tokenname token

      eval { set token(TKNDataSource) ""

       if {[isTableOnStack $token(TKNTableName)]==1} {

       set token(CATEGORY) SUCCESS

       set token(TKNDataSource) STACK

       } 

      }

     } 

    }

    

    

    It seems that the string inside the eval, the string  " set

    token(TKNDa.... etc. STACK } }" is stored in the global string literal

    hash table.

    

    This string is however modified when executing the command

    

    [isTableOnStack $token(TKNTableName)]==1

    

    in the function 

    

    TclCompileExprWords(....) line 1374 of tclCompile.c

    

    so that the end character is \0 not '}'.

    

    However, this function call (in odd cases) can cause the global literal

    table to be rebuilt. Obviously the table stored in the object is

    terminated before the  "} { set token (CATEGORY) SUCCESS.... } }"

    section.

    

    This causes the hash index to be calculated correctly for the current

    string, but not when the saved char is returned to the string. So the

    index is then incorrect.

    

    This causes no real problems, until the interp is deleted after

    execution of the script. 

    

    The problems occur in the following stack-trace

    

    In...

    

    Tcl_DeleteInterp(...) - line 901 file: tclBasic.c

    Tcl_EventuallyFree(...) - line 313 file: tclPreserve.c

    DeleteInterpProc(...) - line 1075 file: tclBasic.c

    

    

    Inside the function TclDeleteLiteralTable(...) which attempts to simply

    linearly access the hash table array and free all the elements by

    getting the 'entryPtr' from the hashtable, finding each of the 'object

    pointers' in the link list associated with it, and attempting to free

    the 'object pointers' by calling the function TclReleaseLiteral(interp,

    'object pointer'); at line 123 in file tclLiteral.c.

    

    

    So inside TclReleaseLiteral we initially call

    

    Tcl_GetStringFromObj(...) line 682 file: tclLiteral.c - which returns

    the actual bytes stored in the object. These bytes will actually be the

    FULL string mentioned above, and not the truncated string from before

    (since we are at the end of execution and attempting to cleanup). To

    find the 'entry pointer' in the hash table the index is found. This will

    return a different index from the rebuilding of the hash table.

    

    Now line 691 file: tclLiteral.c has a for loop. Imagine the situation

    that the index just returned doesn't have any current members at this

    point in the hash table. (it doesn't matter if it has members, but

    easier if we disregared)

    

    This will cause the for loop to immediately fail (entryPtr != NULL) and

    will cause the objPtr reference to be decremented "Tcl_DecRefCount" line

    753 file: tclLiteral.c. This function will now return.

    

    However, the 'entryPtr' reference has not been decremented, and hence

    still exists. So the 'object pointer' will continue to be decremented

    inside the TclReleaseLiteral until it is actually deleted.

    

    This is the problem point. The entryPtr's link list still has a

    reference to this object pointer, but is now pointing to some invalid

    piece of memory, which has just been deleted by the Tcl_DecRefCount

    function call.

    

    This time however, it will attempt to find the bytes associated with

    this memory ("aaaaaaaaaa..." in Tcl debug mode). This will then crash

    when HashString(..) is called. (probably due to overflow since

    non-terminated string)

    

    

    

    Summary:

    

    Tcl_CompileExprWords(...) temporarily alters a global literal.

    line 1374 file: tclCompile.c

    

    It is possible to cause a rebuild of the hash table before the

    alteration is put back, causing the hash table to be incorrect upon

    returning from the above function

    

    

    The deletion of the hash-table requires that the object pointers in the

    hash table, can correctly point back to the correct index in the

    hash-table. This is comprimised by the above code.

    

    

    

    Quick Fix?

    

    As I mentioned previously, we are automatically generating lots of code.

    I realise that we could just make sure that this simply does not happen

    for all of our Tcl, but this could prove difficult for every piece of

    code generated. 

    

    Another alteration could be the creation of a new string literal in the

    Tcl_CompileExprWords(...) function. I am not aware how much of an

    overhead that would place on the interpreter.

    

    

    

    I have quickly looked at the source code for Tcl 8.2, and this seems to

    use exactly the same code as Tcl 8.1.1

    

    

    Have you got any suggestions?



DesiredBehavior:

I expect it not to core dump :)





Fixed problem in tclCompile.c with null smashing (10/20/1999). 
-- 10/21/1999 hobbs
welch Closed ticket [218866ffff]. [History diff]2000-10-26 08:30:15
Log in (anonymous is fine) to add a comment.