TclExecuteByteCode assert fail after {*}longlist
Closed / Fixed2415422fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 18 years ago
Descriptionplain2008-12-11 01:06:44
The following code
proc nop {} {}
proc crash {} {
foreach val [list {*}[lrepeat 1781 x]] {
nop
}
}
crash
results in an assertion failure:
tclsh: /home/users/td26063/build/tcl8.6a3/unix/../generic/tclExecute.c:2047: TclExecuteByteCode: Assertion `(((Interp *) interp)->cmdFramePtr == bcFramePtr)' failed.
with backtrace:
#0 0x0048d7a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1 0x00128825 in raise () from /lib/tls/libc.so.6
#2 0x0012a289 in abort () from /lib/tls/libc.so.6
#3 0x00121da1 in __assert_fail () from /lib/tls/libc.so.6
#4 0x00885756 in TclExecuteByteCode (interp=0x80688e0, codePtr=0x808c230)
at /home/users/td26063/build/tcl8.6a3/unix/../generic/tclExecute.c:2047
#5 0x0082fa2c in NRCallTEBC (data=0x807dd2c, interp=0x80688e0, result=0)
at /home/users/td26063/build/tcl8.6a3/unix/../generic/tclBasic.c:4292
#6 0x0082f789 in TclNRRunCallbacks (interp=0x80688e0, result=0, rootPtr=0x0,
tebcCall=0)
at /home/users/td26063/build/tcl8.6a3/unix/../generic/tclBasic.c:4208
#7 0x0082f3de in Tcl_EvalObjv (interp=0x80688e0, objc=1, objv=0x80691b0,
flags=2097152)
at /home/users/td26063/build/tcl8.6a3/unix/../generic/tclBasic.c:3991
#8 0x00830dd3 in TclEvalEx (interp=0x80688e0,
script=0x8080960 "proc nop {} {}\nproc crash {} {\n foreach val [list {*}[lrepeat 1781 x]] {\n nop\n }\n}\ncrash\n", numBytes=102, flags=0,
line=7)
at /home/users/td26063/build/tcl8.6a3/unix/../generic/tclBasic.c:5085
#9 0x0083050e in Tcl_EvalEx (interp=0x80688e0,
script=0x8080960 "proc nop {} {}\nproc crash {} {\n foreach val [list {*}[lrepeat 1781 x]] {\n nop\n }\n}\ncrash\n", numBytes=102, flags=0)
at /home/users/td26063/build/tcl8.6a3/unix/../generic/tclBasic.c:4786
#10 0x008b81f0 in Tcl_FSEvalFileEx (interp=0x80688e0, pathPtr=0x807e4c0,
encodingName=0x0)
at /home/users/td26063/build/tcl8.6a3/unix/../generic/tclIOUtil.c:1757
#11 0x008c03a6 in Tcl_Main (argc=-1, argv=0xbfe96a2c,
appInitProc=0x804868b <Tcl_AppInit>)
at /home/users/td26063/build/tcl8.6a3/unix/../generic/tclMain.c:442
#12 0x0804867e in main (argc=2, argv=0xbfe96a24)
at /home/users/td26063/build/tcl8.6a3/unix/../unix/tclAppInit.c:87
Tcl doesn't crash when the repeat count is reduced to 1780 or less. Tcl DOES crash when "x" is replaced with "", so the string length doesn't seem to have anything to do with it.
Tcl doesn't crash when nop's definition is changed to "proc nop {args} {}", since the bytecode compiler optimizes that to a true no-op. Tcl also doesn't crash when nop is removed from the loop, even if it is replaced with "puts moo" or any other combination of command (not proc!) invocations.
Tcl doesn't crash when the loop is changed to "foreach val [lrepeat 1781 x] {nop}", so it is critical that {*} expansion take place.
Tcl doesn't crash when the loop is replaced with some other structure, for example:
proc nop {} {return 1}
proc crash {} {
if {[llength [list {*}[lrepeat 1781 ""]]]} {
nop
}
}
crash
so it appears that [foreach] also has a hand in this. Tcl DOES crash when additional control structures are put around the call to nop, for example:
proc nop {} {}
proc crash {} {
foreach val [list {*}[lrepeat 1781 ""]] {
foreach foo {1 2 3} {nop}
}
}
crash
The number 1781 may be magic only for my particular computer and architecture. Here's the output of uname -a:
Linux ARL020474LX 2.6.9-78.0.5.ELsmp #1 SMP Wed Oct 8 07:12:03 EDT 2008 i686 i686 i386 GNU/Linux
The system has two 2.8GHz Pentium 4 CPUs and one gigabyte of RAM.
msofer Ticket [2415422fff]: 1 change
[History diff]2008-12-11 08:24:55
ferrieuxplain2008-12-11 23:58:43
Hum, cannot repro on:
Linux lat6466.rd.francetelecom.fr 2.6.9-67.0.22.ELsmp #1 SMP Wed Jul 23 17:30:51 EDT 2008 i686 i686 i386 GNU/Linux
I tried both CFLAGS_DEBUG and CFLAGS_OPTIMIZE.
Here is the disassembled [crash]:
% ::tcl::unsupported::dis proc crash
ByteCode 0x0x9ff9060, refCt 1, epoch 4, interp 0x0x9fdf0f0 (epoch 4)
Source "\n if {[llength [list {*}[lrepeat 1781 \"\"]]]} {\n "
Cmds 5, src 65, inst 30, litObjs 5, aux 0, stkDepth 4, code/src 0.00
Proc 0x0xa011058, refCt 1, args 0, compiled locals 0
Commands 5:
1: pc 0-28, src 4-63 2: pc 0-17, src 9-43
3: pc 0-16, src 18-42 4: pc 3-10, src 27-41
5: pc 21-24, src 56-58
Command 1: "if {[llength [list {*}[lrepeat 1781 \"\"]]]} {\n nop"
Command 2: "llength [list {*}[lrepeat 1781 \"\"]]"
Command 3: "list {*}[lrepeat 1781 \"\"]"
(0) expandStart
(1) push1 0 # "list"
Command 4: "lrepeat 1781 \"\""
(3) push1 1 # "lrepeat"
(5) push1 2 # "1781"
(7) push1 3 # ""
(9) invokeStk1 3
(11) expandStkTop 2
(16) invokeExpanded
(17) listLength
(18) tryCvtToNumeric
(19) jumpFalse1 +8 # pc 27
Command 5: "nop"
(21) push1 4 # "nop"
(23) invokeStk1 1
(25) jump1 +4 # pc 29
(27) push1 3 # ""
(29) done
ferrieuxplain2008-12-12 06:48:06
FWIW, I found that the above code takes higher values to crash when typed interactively, but crashes for the critical value 1781 when sourced.
ferrieuxplain2008-12-12 19:54:24
I did a twin-gdb-session with one crashing an one non-crashing values.
The divergence occurs on return from GrowEvaluationStack at tclExecute.c:2621.
In the normal case, moved==0; but in the crashing case, "moved" is nonzero and hence the following "if" block is executed.
moved = GrowEvaluationStack(iPtr->execEnvPtr, length, 1)
- (Tcl_Obj **) bottomPtr;
if (moved) {
/*
* Change the global data to point to the new stack.
*/
bottomPtr = (BottomData *) (((Tcl_Obj **)bottomPtr) + moved);
initCatchTop += moved;
catchTop += moved;
initTosPtr += moved;
tosPtr += moved;
esPtr = iPtr->execEnvPtr->execStackPtr;
}
I'm sure Miguel will fix that instantly ;-)
andygothplain2008-12-12 23:47:31
Alexandre, thank you very much for all the research you've been doing. Twin GDB? I will have to keep that technique in mind!
msoferplain2008-12-13 00:56:04
Thanks for the analysis! I don't know about "instantly" (give me the weekend), but this gives me an extremely clear idea of exactly where I fscked up
dgpplain2008-12-19 04:43:41
Appears that tests basic-48.4.0
and nre-8.1 are the only ones
in the test suite that covers the
stanza of code AF has identified.
In both tests, the very long command
created by {*} expansion comes last
in the bytecode compiled script.
dgpplain2008-12-19 04:54:48
Same script is fine in Tcl 8.5.6b1, so I believe the flaw is in the new NRE code, and not the GES machinery.
dgpplain2008-12-19 05:15:54
Adding the line: bcFramePtr += moved; to that stanza to follow the pattern of the other pointers is enough to stop the assertion failure. But then we just move on to a segfault: 2801 bcFramePtr->data.tebc.pc = (char *) pc; (gdb) print *bcFramePtr Cannot access memory at address 0x971fd80
dgpplain2008-12-19 06:00:46
Ok, so making changes that make the assertion pass leads to a segfault. New theory: the assertion is simply mistaken. It asserts something that is often, but not always true. Disable it. That removes the reported trouble. Committing as the solution for 8.6b1. Leaving the report open for the real NRE expert to review when he returns to full activity.
msoferplain2009-03-21 13:58:09
* generic/tclExecute.c: proper fix for [Bug 2415422]. Reenabled * tests/nre.test: the failing assertion that was disabled on 2008-12-18: the assertion is correct, the fault was in the management of expansions. The memory move was being badly mismanaged (my bad), it worked by sheer chance. Fixed.
msoferplain2009-03-21 13:58:10
allow_comments - 1
Log in (anonymous is fine) to add a comment.