Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-06-16
17:17 • Closed ticket [16828b3744]: TclOO segmentation fault plus 6 other changes artifact: cdd618d6d8 user: dgp
17:09
[16828b3744] [vwait] *must* successfully undo its variable trace, or else it risks corrupting memory... check-in: 35323a976b user: dgp tags: trunk
17:06
[16828b3744] [vwait] *must* successfully undo its variable trace, or else it risks corrupting memory... check-in: e1f890de4b user: dgp tags: core-8-5-branch
16:38
[16828b3744] [vwait] *must* successfully undo its variable trace, or else it risks corrupting memory... check-in: fac5b3732e user: dgp tags: core-8-6-branch
16:13 • Ticket [16828b3744] TclOO segmentation fault status still Open with 3 other ... artifact: 88b23a7083 user: dgp
15:59 • Ticket [16828b3744]: 3 changes artifact: ac023d159f user: dgp
14:27 • Ticket [16828b3744]: 3 changes artifact: 08f410d9d4 user: dgp
14:23
[16828b3744] Prototype fix for merging forward. check-in: a39b2cd748 user: dgp tags: bug-16828b3744
13:09 • Ticket [16828b3744] TclOO segmentation fault status still Open with 3 other ... artifact: 8af0173df8 user: dgp
2016-06-15
23:16 • Ticket [16828b3744]: 4 changes artifact: 642dd38c0a user: dah
20:19 • Ticket [16828b3744]: 3 changes artifact: d05461fa46 user: dgp
20:09 • Ticket [16828b3744]: 6 changes artifact: dd76ba8330 user: dgp
19:30 • Ticket [16828b3744]: 4 changes artifact: 7601d9f77d user: dah
2016-06-14
20:08 • New ticket [16828b3744]. artifact: cbc3a83af2 user: anonymous

TclOO segmentation fault

Closed / Fixed16828b3744521541660af48501fa06e63e564ad3Created 10 years agoUpdated 10 years ago
Descriptionhtml2016-06-14 20:08:02

This code produce segmentation fault:

oo::class create A {
	variable continue
    
	method start {} {
           after 4000 [self] destroy
        
           set continue 0
           vwait [namespace current]::continue
           puts "after continue"
	}
	destructor {
           puts "in destructor"	
           set continue 1
	}	
	
}

[A new] start

Referenced by4
  • 35323a976b[16828b3744] [vwait] *must* successfully undo its variable trace, or else it risks corrupting memory. Namespace teardown complexities were giving the traces an opportunity to survive. Added another (arguably better) Tcl_UntraceVar() call to be sure we avoid this problem. check-in2016-06-16 17:09:19
  • e1f890de4b[16828b3744] [vwait] *must* successfully undo its variable trace, or else it risks corrupting memory. Namespace teardown complexities were giving the traces an opportunity to survive. Added another (arguably better) Tcl_UntraceVar() call to be sure we avoid this problem. check-in2016-06-16 17:06:25
  • fac5b3732e[16828b3744] [vwait] *must* successfully undo its variable trace, or else it risks corrupting memory. Namespace teardown complexities were giving the traces an opportunity to survive. Added another (arguably better) Tcl_UntraceVar() call to be sure we avoid this problem. check-in2016-06-16 16:38:36
  • a39b2cd748[16828b3744] Prototype fix for merging forward. check-in2016-06-16 14:23:15
dahplain2016-06-15 19:30:08
This crashes too:

namespace eval test {
   set script {
       set ::test::foo 1
       namespace delete test
   }
   after 4000 $script
   vwait ::test::foo
}
dgpplain2016-06-15 20:09:24
The namespace version starts crashing with
the arrival of the current NRE implementation.

http://core.tcl.tk/tcl/info/3072080

Something to do with CallFrames it appears.
dgpplain2016-06-15 20:19:45
Working with checkin

http://core.tcl.tk/tcl/info/56c0f34285323c68

and the demo script:

after idle {
  set ::t::v 1; 
  namespace delete ::t
}
namespace eval t {
  vwait ::t::v
}

I get a tidy segfault in a debugger with stack trace:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000523762 in TclStackFree (interp=0x1007b0e80, freePtr=0x7b52e0)
    at /home/dgp/fossil/tclbisect/generic/tclExecute.c:1166
1166	    if (iPtr == NULL || iPtr->execEnvPtr == NULL) {
(gdb) bt
#0  0x0000000000523762 in TclStackFree (interp=0x1007b0e80, freePtr=0x7b52e0)
    at /home/dgp/fossil/tclbisect/generic/tclExecute.c:1166
#1  0x000000000042f6ff in TclPopStackFrame (interp=0x1007b0e80)
    at /home/dgp/fossil/tclbisect/generic/tclNamesp.c:451
#2  0x00000000004334a8 in NsEval_Callback (data=0x7af998, interp=0x7b0e80, 
    result=0) at /home/dgp/fossil/tclbisect/generic/tclNamesp.c:3408
#3  0x000000000049ee45 in TclNRRunCallbacks (interp=0x7b0e80, result=0, 
    rootPtr=0x0) at /home/dgp/fossil/tclbisect/generic/tclBasic.c:4312
#4  0x000000000049e6bd in Tcl_EvalObjv (interp=0x7b0e80, objc=4, 
    objv=0x7b5170, flags=2097152)
    at /home/dgp/fossil/tclbisect/generic/tclBasic.c:4102
#5  0x00000000004a0c7f in TclEvalEx (interp=0x7b0e80, 
    script=0x7e99e0 "after idle {\n  set ::t::v 1; \n  namespace delete ::t\n}\nnamespace eval t {\n  vwait ::t::v\n}\n", numBytes=91, flags=0, line=5, 
    clNextOuter=0x0, 
    outerScript=0x7e99e0 "after idle {\n  set ::t::v 1; \n  namespace delete ::t\n}\nnamespace eval t {\n  vwait ::t::v\n}\n")
    at /home/dgp/fossil/tclbisect/generic/tclBasic.c:5216
#6  0x00000000004a002d in Tcl_EvalEx (interp=0x7b0e80, 
    script=0x7e99e0 "after idle {\n  set ::t::v 1; \n  namespace delete ::t\n}\nnamespace eval t {\n  vwait ::t::v\n}\n", numBytes=91, flags=0)
    at /home/dgp/fossil/tclbisect/generic/tclBasic.c:4872
#7  0x0000000000428b12 in Tcl_FSEvalFileEx (interp=0x7b0e80, pathPtr=0x7aed00, 
    encodingName=0x0) at /home/dgp/fossil/tclbisect/generic/tclIOUtil.c:1753
#8  0x000000000042e637 in Tcl_Main (argc=-1, argv=0x7fffffffdc58, 
    appInitProc=0x40f4fe <Tcl_AppInit>)
    at /home/dgp/fossil/tclbisect/generic/tclMain.c:355
#9  0x000000000040f4f7 in main (argc=2, argv=0x7fffffffdc48)
    at /home/dgp/fossil/tclbisect/unix/tclAppInit.c:80

(gdb) print *iPtr
Cannot access memory at address 0x1007b0e80
dahplain2016-06-15 23:16:27
I think I know the cause of the crash.

1) vwait ::test::foo waits for foo to be set.
2) When ::test::foo is set, vwait exits its loop
3) namespace teardown still sees ::test::foo as having a trace on it and invokes trace handlers
4) vwait's VwaitVarProc is called a second time for the variable UNSET trace event however it's out of the Tcl_VwaitObjCmd function by now and this function passes the address of a local variable named "done" to Tcl_TraceVar2 thereby smashing the stack and eventually leading to a crash.

Tcl_UntraceVar2 isn't unsetting the trace in Tcl_VwaitObjCmd.  I took out the TCL_TRACE_UNSETS flags and the crash went away.
dgpplain2016-06-16 13:09:08
Yes, that's it.

When Tcl_UntraceVar() is called, the variable
can no longer be found since the namespace has
been deleted -- even thought its final teardown
and variable destruction has not yet been carried out.
So the Trace is not undone.

Vwait is counting on a successful removal of the
trace for proper functioning.

Here's a related deadlock problem:

after idle {namespace delete ::t}
namespace eval ::t {vwait ::t::v}

vwait waits for the unset of ::t::v
which is held up because of an active
[namespace eval], but that is waiting
for the [vwait] to complete.

Common factor through all of this is
the delayed destruction of namespace
variables.  This is not by accident.
Things are the way they are because they
solved earlier problems and fixed earlier
bugs.
dgpplain2016-06-16 14:27:15
Branch bug-16828b3744 contains a patch that fixes
the demo scripts at the time the bug was exposed.
(Appears the problem was lurking before that).

Still need to merge it forward, as the same 
patch isn't effective on the tip of core-8-6-branch.
dgpplain2016-06-16 15:59:59
The re-appearing segfault is again an exposure
of the problem, not the cause of it.  It's a 
matter of whether the memory corruption of writing
to *donePtr causes crashing damage or not as to
whether we segfault.  The error is still present
if luck leads to no crash.

So the patch doesn't (really) fix the submitted
demo script.  Because the additional Tcl_UntraceVar()
call doesn't untrace the variable.  The variable name
'continue' doesn't resolve, so the Var isn't found so
it's trace cannot be found and removed.  Since it's
not removed, the unset trace fires later when *donePtr
writing is damaging.
dgpplain2016-06-16 16:13:27
Didn't resolve because the patch stupidly copied
the constraint to global resolution where it no
longer was proper.
dgpplain2016-06-16 17:17:13
Correct fix now on all branches.
Log in (anonymous is fine) to add a comment.