Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2014-10-03
20:22 • Closed ticket [bc1a96407a]: segfault in compiled array set of array-traced var... artifact: 3a339208ce user: dgp
20:20
fix [bc1a96407a] check-in: d7030fb605 user: dgp tags: trunk
2014-10-02
21:40 • Ticket [bc1a96407a] segfault in compiled array set of array-traced variable</i... artifact: 047df43938 user: dgp
18:48 • Ticket [bc1a96407a]: 3 changes artifact: c494ade6f5 user: dgp
17:44 • Ticket [bc1a96407a]: 3 changes artifact: bf09f2be53 user: dgp
17:43
[bc1a96407a] Partial solution should avoid crash, but may lead to wrong behavior. check-in: 6c9efd94aa user: dgp tags: bug-bc1a96407a
17:10 • Ticket [bc1a96407a] segfault in compiled array set of array-traced variable</i... artifact: 1271f5de85 user: dgp
2014-09-09
20:10 • Ticket [bc1a96407a]: 4 changes artifact: dcacc92591 user: dkf
12:53 • Ticket [bc1a96407a]: 3 changes artifact: d1612c4556 user: dgp
10:18 • Ticket [bc1a96407a]: 3 changes artifact: 4bf0aa4a28 user: dgp
03:54 • Ticket [bc1a96407a]: 3 changes artifact: a2934d40bb user: msofer
01:15 • Ticket [bc1a96407a]: 3 changes artifact: 56542a4f24 user: msofer
2014-09-08
17:17 • Ticket [bc1a96407a]: 3 changes artifact: 7569dda356 user: dgp
16:18 • Ticket [bc1a96407a]: 3 changes artifact: 92934630e5 user: dgp
16:14 • Ticket [bc1a96407a]: 3 changes artifact: 6253c5ccce user: dgp
16:09 • Ticket [bc1a96407a]: 4 changes artifact: c18c4eb26e user: dgp
15:58 • Ticket [bc1a96407a]: 3 changes artifact: 00380512bb user: dgp
15:43 • Ticket [bc1a96407a]: 4 changes artifact: 9852238d0a user: dgp
15:30 • Ticket [bc1a96407a]: 3 changes artifact: 26f57d4574 user: dgp
15:21 • Ticket [bc1a96407a]: 4 changes artifact: fce7986f5d user: dgp
15:13 • Ticket [bc1a96407a]: 4 changes artifact: 62852abecc user: msofer
15:01 • New ticket [bc1a96407a]. artifact: f72c917598 user: anonymous

segfault in compiled [array set] of array-traced variable

Closed / Fixedbc1a96407a7f73024c2238a747b428ec1145ae19Created 12 years agoUpdated 12 years ago
Descriptionplain2014-09-08 15:01:55
This piece of code running in 64Bit ends in Segmentation fault (core dumped) in Oracle Linux 6.4.

proc dispatcher {chan} {
    if {[eof $chan]} {
    } else {
        set line [gets $chan]
        array set ::guggus  [list a 1 b 2]
    }
}
fileevent stdin readable [list  ::dispatcher stdin]
vwait ::guggus
parray ::guggus
exit

--------------------
Tcl 8.6.2 64Bit Segmentation fault (core dumped)
Tcl 8.6.2 32Bit it works
Referenced by2
msoferplain2014-09-08 15:13:05
Segfault confirmed, valgrind sees the crash as

==17360== Invalid read of size 8
==17360==    at 0x56D1D8: TclObjCallVarTraces (tclTrace.c:2514)
==17360==    by 0x576DB1: TclPtrSetVar (tclVar.c:1985)

This is positively weird: no variables are traced in this script.
dgpplain2014-09-08 15:21:17
Confirmed:

Program received signal SIGSEGV, Segmentation fault.
0x000000000057a965 in TclObjCallVarTraces (iPtr=0x824648, arrayPtr=0x905d08,
    varPtr=0x8ffdc8, part1Ptr=0x0, part2Ptr=0x9066d8, flags=32,
    leaveErrMsg=512, index=2)
    at /home/dgp/fossil/tcl-only/generic/tclTrace.c:2514
2514        part1 = TclGetString(part1Ptr);
(gdb) bt
#0  0x000000000057a965 in TclObjCallVarTraces (iPtr=0x824648,
    arrayPtr=0x905d08, varPtr=0x8ffdc8, part1Ptr=0x0, part2Ptr=0x9066d8,
    flags=32, leaveErrMsg=512, index=2)
    at /home/dgp/fossil/tcl-only/generic/tclTrace.c:2514
#1  0x000000000058451f in TclPtrSetVar (interp=0x824648, varPtr=0x8ffdc8,
    arrayPtr=0x905d08, part1Ptr=0x0, part2Ptr=0x9066d8, newValuePtr=0x902588,
    flags=512, index=2) at /home/dgp/fossil/tcl-only/generic/tclVar.c:1985
#2  0x00000000004e8507 in TEBCresume (data=0x9067d0, interp=0x824648, result=0)
    at /home/dgp/fossil/tcl-only/generic/tclExecute.c:3576
#3  0x000000000041497f in TclNRRunCallbacks (interp=0x824648, result=0,
    rootPtr=0x907cb8) at /home/dgp/fossil/tcl-only/generic/tclBasic.c:4390
#4  0x0000000000417129 in TclEvalObjEx (interp=0x824648, objPtr=0x905d08,
    flags=131072, invoker=0x0, word=0)
    at /home/dgp/fossil/tcl-only/generic/tclBasic.c:5956
#5  0x00000000004170cd in Tcl_EvalObjEx (interp=0x824648, objPtr=0x905d08,
    flags=131072) at /home/dgp/fossil/tcl-only/generic/tclBasic.c:5937
#6  0x000000000052b569 in TclChannelEventScriptInvoker (clientData=0x8ef378,
    mask=2) at /home/dgp/fossil/tcl-only/generic/tclIO.c:8608
#7  0x000000000052ae08 in Tcl_NotifyChannel (channel=0x8f0578, mask=2)
    at /home/dgp/fossil/tcl-only/generic/tclIO.c:8107
#8  0x000000000059dd36 in FileHandlerEventProc (evPtr=0x902018, flags=-3)
    at /home/dgp/fossil/tcl-only/unix/tclUnixNotfy.c:765
#9  0x000000000054c63e in Tcl_ServiceEvent (flags=-3)
    at /home/dgp/fossil/tcl-only/generic/tclNotify.c:670
#10 0x000000000054ca6d in Tcl_DoOneEvent (flags=-3)
    at /home/dgp/fossil/tcl-only/generic/tclNotify.c:967
#11 0x00000000004de29d in Tcl_VwaitObjCmd (clientData=0x0, interp=0x824648,
    objc=2, objv=0x827b70) at /home/dgp/fossil/tcl-only/generic/tclEvent.c:1408
dgpplain2014-09-08 15:30:10
[vwait] is powered by a variable trace.
dgpplain2014-09-08 15:43:31
Broken by

http://core.tcl.tk/tcl/info/93a75ad1c4
dgpplain2014-09-08 15:58:13
Bug originates in 

http://core.tcl.tk/tcl/info/88629f23ac
dgpplain2014-09-08 16:09:56
Simpler demo:

after 0 [list apply {{} {array set ::foo [list a 1 b 2]}}]
vwait foo

Changes to bytecode execution of [array set] have broken
array traces.
dgpplain2014-09-08 16:14:22
Simpler still:

trace add variable foo array {#}
apply {{} {array set ::foo {a 1}}}
dgp Ticket [bc1a96407a]: 3 changes [History diff]2014-09-08 16:18:25
dgpplain2014-09-08 17:17:28
This looks to be hard.  The revised [array set] bytecode
is creating a nameless local variable that's an upvar link
to a named variable in the same local frame.  When traces
are in place, a NULL value is passed to the tracing machinery
for the name of the traced variable, and trace cannot accept
that.  The trace mechanism fundamentally requires that the
traced variable has a name.  (it's going to pass that name
on to the trace handler procs).

In this situation, it *might* be good enough to pass the
name of the link target, since its in the same frame.  I
don't quickly see how to achieve that, though.
msoferplain2014-09-09 01:15:21
Something is wrong: when tracing a variable that is upvared, normally it is the link TARGET that gets the trace. Witness:

% proc foo {} {
upvar 1 a b
trace add variable b write {puts hit;#}
set ::a 1
set b 1
}
% foo
hit
hit
1
% set a 2
hit
2

So this may be a problem with the way vwait creates the trace?
msoferplain2014-09-09 03:54:47
Another hint tht the core dislikes traces on upvar SOURCEs, and that [vwait] should better respect that:

% proc foo {} {
      trace add variable b write {puts hit;#}
      upvar 1 a b
      set b 1
 }
% foo
variable "b" has traces: can't use for upvar
dgpplain2014-09-09 10:18:02
The problem is that a traced variable has no name.

    upvar 0 ::a <noname>

Using the actual upvar command that doesn't happen
and all is ok:

% proc report {a e o} {                       
    set msg "$o on $a"
    if {$e ne {}} {append msg ($e)}
    puts $msg
}
% proc foo {} {
    upvar 0 ::tcl_platform x
    trace add variable x array report
    array set ::tcl_platform {mmmm donuts}
    array set x {where coffee}
}
% foo
array on ::tcl_platform
array on x
dgpplain2014-09-09 12:53:37
There's partial handling of this already in place.
When TclObjCallVarTraces() notices it got NULL for
part1Ptr, it "calls" localName (really a macro) to
pull a name out of local variable data kept by a
frame (an LVT?).  Trouble in this case is that also
fails, since the local temp variable truly does not
have a name, on purpose.

Traces have never been implemented to tolerate that.
Gotta have a name.  That's the problem.
dkfhtml2014-09-09 20:10:08

The code generated in this case is (removing wholly irrelevant bits):

  Command 5: "array set ::guggus  [list a 1 b 2]"...
    (30) startCommand +86 1 	# next cmd at pc 116
    (39) push1 3 	# "::guggus"
    (41) push1 4 	# "0"
    (43) reverse 2 
    (48) upvar %v2 	# temp var 2
    (53) pop 
  Command 6: "list a 1 b 2"...
    (54) startCommand +14 1 	# next cmd at pc 68
    (63) push1 5 	# "a 1 b 2"
    (65) dup 
    (66) listLength 
    (67) pop 
    (68) dup 
    (69) listLength 
    (70) push1 6 	# "1"
    (72) bitand 
    (73) jumpFalse1 +15 	# pc 88
    (75) push1 7 	# "list must have an even number of element"...
    (77) push1 8 	# "-errorcode {TCL ARGUMENT FORMAT}"
    (79) returnImm +1 0 
    (88) arrayExistsImm 2 
    (93) jumpTrue1 +7 	# pc 100
    (95) arrayMakeImm 2 
    (100) foreach_start 0 
		[data=[%v1633771873], loop=%v18446744073709551609
		 it%v1633771873	[%v3, %v4]]
    (105) loadScalar1 %v3 	# temp var 3
    (107) loadScalar1 %v4 	# temp var 4
    (109) storeArray1 %v2 	# temp var 2
    (111) pop 
    (112) foreach_step 
    (113) foreach_end 
    (114) push1 1 	# ""
I do not remember using upvar when writing the array set compiler. But be that as it may, if we fix this by generating the loop with storeArrayStk, we need to be careful when changing this (particularly the code that generates the highlighted lines) to ensure that we only run the code to generate the array name once; that'll have to be the value stored in the temporary variable.

Can we make the solution with nameless aliases work instead? Please? It's so much easier to code correctly.

dgpplain2014-10-02 17:10:41
Though the details are not fully clear (really big app),
Rolf Ade reports enough clues on the chat to make pretty
clear he's suffering segfaults due to the same cause.

If an elegant fix is not in the works, then we ought to
at least do the NULL check and avoid the crash.
dgpplain2014-10-02 17:44:20
see bug-bc1a96407a branch for a partial fix for review.
dgpplain2014-10-02 18:48:19
I think this problem is related.  If not, move to another ticket:

% apply {{} {set name foo(bar); array set $name {baz bash}}}
Segmentation fault: 11
dgpplain2014-10-02 21:40:16
New commit to that branch appears to be an effective fix.
Not sure how much abuse I'm committing.  msofer please
review.
dgpplain2014-10-03 20:22:15
Fix merged to trunk.

Only concern is whether any trouble can come from
replacing an anonymous temporary variable with
a temporary variable that lies about its name, claiming
a namespace-qualified name instead (which I believe
cannot conflict with any other local varname).
Log in (anonymous is fine) to add a comment.