Tcl Source Code

View Ticket
Login
Ticket UUID: 16828b3744521541660af48501fa06e63e564ad3
Title: TclOO segmentation fault
Type: Bug Created on: 2016-06-14 20:08:02
Submitter: anonymous Assigned to: dgp
Subsystem: 45. Parsing and Eval Severity: Important
Priority: 5 Medium Last modified: 2016-06-16 17:17:13
Status: Closed Closed by: dgp
Resolution: Fixed Closed on: 2016-06-16 17:17:13
Version: 8.6
Description:

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

User Comments:
dah added on 2016-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
}

dgp added on 2016-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.

dgp added on 2016-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

dah added on 2016-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.

dgp added on 2016-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.

dgp added on 2016-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.

dgp added on 2016-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.

dgp added on 2016-06-16 16:13:27:
Didn't resolve because the patch stupidly copied
the constraint to global resolution where it no
longer was proper.

dgp added on 2016-06-16 17:17:13:
Correct fix now on all branches.