Tk Library Source Code

View Ticket
Login
Ticket UUID: 1313173
Title: Crash in htmlparse tests with critcl struct package
Type: Bug Version: None
Submitter: patthoyts Created on: 2005-10-04 18:55:39
Subsystem: struct Assigned To: andreas_kupries
Priority: 9 Immediate Severity:
Status: Closed Last Modified: 2005-10-07 04:11:30
Resolution: Fixed Closed By: andreas_kupries
    Closed on: 2005-10-06 21:11:30
Description:
On Windows XP with the current CVS head of tcllib and
tcl 8.4.11, if we first build the critcl package and
then run the test suite we get a crash caused by a
string buffer overflow raise from tcl. In the stack
trace above I see functions from tcllibc and I believe
it is the struct module. As I dig into this I'll add
details here.

To reproduce:
 tclsh sak.tcl critcl
 tclsh sak.tcl test htmlparse

Stack trace is:
 MSVCRTD.DLL!strlen Line 78
 tcl84g.dll!UpdateParseVarName ...
 tcl84g.dll!Tcl_GetString ...
 tcl84g.dll!Tcl_ObjSetVar2 ...
 tcllibc!0175ed51()
 tcllibc!0175f06e()
 ...
 tcl84g.dll!TclEvalObjInternal...

AFAICT the tcl proc actually being run here is
htmlparse::2tree
User Comments: andreas_kupries added on 2005-10-07 04:11:30:
Logged In: YES 
user_id=75003

Committed fixes to head.

andreas_kupries added on 2005-10-07 03:54:13:
Logged In: YES 
user_id=75003

The basic bug is one of refcounting. The object holding the
name of the loop variable has refcount 1, and after the
first loop it hold a different value. My guess is that is
was reused when the loop body was compiled, for a different
literal. This caused me to write to a different variable and
the Reorder proc was always called for the node from the
first iteration. This does not explain the memory problem,
not really. Having the walker holding a reference to the
varnames fixes this.

... further checking shows that this is likely also the
reason for the crash ... We do not get any anymore after
this fix alone.

Will also have to rewrite the iteration over children, the
way I am doing it now it can go boom if the structure
changes, as it does here. The array can not only be
reallocated, but also shrink, expand, invalidating the index
I have into it. I have to use my sibling pointer (next). ...
No, does not work. Not the same behaviour as the Tcl impl.
... Ah, I essentially copy the children in Tcl before I walk
them, so their move to a node already processed has no
effect ... I have to emulate that at the C level ...

Done, for all dfs walks. The bfs walks are quite different
and should not suffer.

Still, we do need test cases for the behaviour of the
struct::tree walkers in all modes, when children and parent
nodes are moved around, added, and deleted during the walk.

In this area, walking a tree while changing it at the same
time, we may still have (crashing) bugs lurking. And stupid
behaviour in general, even for the Tcl implementation.

andreas_kupries added on 2005-10-07 02:29:43:
Logged In: YES 
user_id=75003

The bug exposed by the first test may also point into the
direction of the memory problem. The htmlparse does a
Reorder. This is a walk which _also_ modifies the structure
of the tree. This may invalidate data structures at the C
level where I do not expect this, causing me to write where
I should not. Note that the C code fails to do the reordering.

andreas_kupries added on 2005-10-07 02:20:40:
Logged In: YES 
user_id=75003

A first attempt at reproducing the crash failed. I basically
loaded tcllibc, all the tcllib modules and ran the series of
tree commands as generated by the html test suite. I mean, I
ran the htmlparse testsuite and used logging to determine
the sequence of tree commands, and their arguments. This log
was converted into a script.

As said before, this failed. The crash happens only from
within the htmlparse testsuite :( The interesting result is
that the first tree test fails. Not a crash, but different
results. I.e. there is also a regular bug in the critcl tree.

The crash can happen after the first test alone, but
sometimes a bit later, when the second test is initialized
or started. Looks really like an overwrite bug.

andreas_kupries added on 2005-10-05 02:16:52:
Logged In: YES 
user_id=75003

File tree_c.tcl is the main entry to the struct::tree critcl
code. At the top we have critcl::cheaders/debug commands, so
memory debugging should be on. however for -g to be handled
correctly the 'critcl' application has to be modified a bit
as well.

File "critcl.vfs/lib/critcl/critcl.tcl". Line 612. Disable
this line. "append cmdline ... -O2 ...".

andreas_kupries added on 2005-10-05 02:05:54:
Logged In: YES 
user_id=75003

It can be the struct module. You are likely using
struct::tree, and for that one we have a critcl based
implementation. I believed to have found and fixed all my
overflow bugs, but apparently not.

I do test the critcl struct::tree and that testsuite passes
(I test both critcl and tcl impl., whatever is available,
using the same tests. You know how, I copied this
(LoadAccel, SwitchTo, etc basically from your test). I
wonder what hmlparse is doing different to cause this.

Use TCLTEST_OPTIONS='verbose bpste' to get motre information
from the test run, i.e. which tests where started, passed,
etc. This should give you a better idea where the crash
happen at the higher levels.

There is a way to activate memory debugging for critcl based
code too. I have to dig this out.