Artifacts Associated With Ticket 3154ea2759
Ticket change [41d6102128] (rid 146633) by anonymous on 2016-05-04 14:12:05:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
Here is the code: proc __BEGIN {} { ::tcl::unsupported::assemble { beginCatch @badLabel push error push testing invokeStk 2 pop push 0 jump @okLabel label @badLabel push 1; # should be pushReturnCode label @okLabel endCatch pop beginCatch @badLabel2 push error push testing invokeStk 2 pop push 0 jump @okLabel2 label @badLabel2 push 1; # should be pushReturnCode label @okLabel2 endCatch pop beginCatch @badLabel3 push error push testing invokeStk 2 pop push 0 jump @okLabel3 label @badLabel3 push 1; # should be pushReturnCode label @okLabel3 endCatch pop beginCatch @badLabel4 push error push testing invokeStk 2 pop push 0 jump @okLabel4 label @badLabel4 push 1; # should be pushReturnCode label @okLabel4 endCatch pop beginCatch @badLabel5 push error push testing invokeStk 2 pop push 0 jump @okLabel5 label @badLabel5 push 1; # should be pushReturnCode label @okLabel5 endCatch pop beginCatch @badLabel6 push error push testing invokeStk 2 pop push 0 jump @okLabel6 label @badLabel6 push 1; # should be pushReturnCode label @okLabel6 endCatch pop }} __BEGIN When I run this code, I get core dump: $ ~/tcl8.6.5/bin/tclsh8.6 /tmp/add.tcl alloc: invalid block: 0xb2dde0: 77 0 Aborted (core dumped) If the number of beginCatch/endCatch pairs is less than 6, the program succeeds. - foundin initialized to: "8.6.5"
- is_private initialized to: "0"
- login: "anonymous"
- priority initialized to: "5 Medium"
- private_contact initialized to: "1fa85a7a8cf201549ab887413ec59508d86baa00"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "anonymous"
- subsystem initialized to: "47. Bytecode Compiler"
- title initialized to: "Repeat catch six times and get core dump"
- type initialized to: "Bug"
Ticket change [2bb992d20f] (rid 146634) by dgp on 2016-05-04 15:21:16:
- assignee changed to: "dkf"
- login: "dgp"
- mimetype: "text/plain"
- priority changed to: "7 High"
Ticket change [b8f8f9fb50] (rid 146636) by dkf on 2016-05-05 09:12:47:
- icomment:
This code can trigger the crash even without executing <code>__BEGIN</code>; just compiling it is enough. This means that you can cause the crash with: <pre> # procedure definition from above... tcl::unsupported::disassemble proc __BEGIN </pre>
- login: "dkf"
- mimetype: "text/html"
- priority changed to: "8"
- severity changed to: "Severe"
- username: "dkf"
- icomment:
Ticket change [ecf946ff3b] (rid 146637) by dkf on 2016-05-05 09:17:04:
- icomment:
Based on: <blockquote><tt>If the number of beginCatch/endCatch pairs is less than 6, the program succeeds.</blockquote> I'd guess that the problem is something holding onto a pointer into the list of catch contexts over the point where the list is reallocated into a larger buffer. I have not confirmed this yet.
- login: "dkf"
- mimetype: "text/html"
- username: "dkf"
- icomment:
Ticket change [12b048a0ec] (rid 146638) by dkf on 2016-05-05 09:31:58:
- icomment:
Doesn't crash with mem-debugging enabled (!!!) but with just symbols I get this trace: <pre> (gdb) bt […] #4 0x0000000100158e76 in Tcl_Panic (…) at /Users/dkf/Documents/software/tcl/generic/tclPanic.c:160 #5 0x000000010017d977 in Ptr2Block (ptr=0x100884010 "\001") at /Users/dkf/Documents/software/tcl/generic/tclThreadAlloc.c:842 #6 0x000000010017cf19 in TclpFree (ptr=0x100884010 "\001") at /Users/dkf/Documents/software/tcl/generic/tclThreadAlloc.c:408 #7 0x00000001000d0dff in TclFreeCompileEnv (envPtr=0x7fff5fbfe750) at /Users/dkf/Documents/software/tcl/generic/tclCompile.c:1649 #8 0x00000001000cff91 in TclSetByteCodeFromAny (interp=0x10082da10, objPtr=0x10088ae10, hookProc=0, clientData=0x0) at /Users/dkf/Documents/software/tcl/generic/tclCompile.c:878 #9 0x000000010016a502 in TclProcCompileProc (interp=0x10082da10, procPtr=0x102034c90, bodyPtr=0x10088ae10, nsPtr=0x100821c10, description=0x1001d89a3 "body of proc", procName=0x1020223d0 "__BEGIN") at /Users/dkf/Documents/software/tcl/generic/tclProc.c:2044 #10 0x0000000100169dcf in TclPushProcCallFrame (clientData=0x102034c90, interp=0x10082da10, objc=1, objv=0x10083b0b0, isLambda=0) at /Users/dkf/Documents/software/tcl/generic/tclProc.c:1601 #11 0x0000000100169ea2 in TclNRInterpProc (clientData=0x102034c90, interp=0x10082da10, objc=1, objv=0x10083b0b0) at /Users/dkf/Documents/software/tcl/generic/tclProc.c:1673 #12 0x0000000100025a33 in Dispatch (data=0x10088af08, interp=0x10082da10, result=0) at /Users/dkf/Documents/software/tcl/generic/tclBasic.c:4360 #13 0x0000000100025ac0 in TclNRRunCallbacks (interp=0x10082da10, result=0, rootPtr=0x0) at /Users/dkf/Documents/software/tcl/generic/tclBasic.c:4393 #14 0x00000001000286b6 in TclEvalObjEx (interp=0x10082da10, objPtr=0x10082da10, flags=131072, invoker=0x0, word=0) at /Users/dkf/Documents/software/tcl/generic/tclBasic.c:5959 #15 0x000000010002864c in Tcl_EvalObjEx (interp=0x10082da10, objPtr=0x82da1000000000, flags=131072) at /Users/dkf/Documents/software/tcl/generic/tclBasic.c:5940 #16 0x000000010011b0e3 in Tcl_RecordAndEvalObj (interp=0x10082da10, cmdPtr=0x10088a690, flags=131072) at /Users/dkf/Documents/software/tcl/generic/tclHistory.c:190 #17 0x000000010014c653 in Tcl_MainEx (argc=-1, argv=0x7fff5fbffa30, appInitProc=0x1000029ff <Tcl_AppInit>, interp=0x10082da10) at /Users/dkf/Documents/software/tcl/generic/tclMain.c:537 #18 0x00000001000029f8 in main (argc=1, argv=0x7fff5fbffa28) at /Users/dkf/Documents/software/tcl/unix/tclAppInit.c:84 </pre>
- login: "dkf"
- mimetype: "text/html"
- username: "dkf"
- icomment:
Ticket change [ee2b6fd675] (rid 146639) by aspect on 2016-05-05 12:44:31:
- icomment:
The memory corruption appears to be taking place at [0], when it finds a -1 in the catchIndices array and uses that to write outside envArrayPtr, trashing the Block data. Putting instrumentation code [1] at that point it triggers several times (apparently harmlessly?) before the crash. I don't know if this block (and the similar one a few lines below) need to guard against indexing -1, or if it's a sign the array is carelessly updated somewhere else. [0]: http://core.tcl.tk/tcl/artifact/819418a0da027c20?ln=3985-3986 [1]: if(catchIndices[catchDepth] == -1) { fprintf(stderr, "catchIndices@%p[%d] == -1 on %d!\n", catchIndices, catchDepth, __LINE__); fflush(stderr); } - login: "aspect"
- mimetype: "text/plain"
- icomment:
Ticket change [90492cafa2] (rid 146640) by dkf on 2016-05-05 14:39:18:
- icomment:
Which indicates that the reason nobody detected it before was because they were getting lucky; the code was probably just scribbling over somewhere that didn't matter. <p> Why is the <tt>catchDepth</tt> wrong?
- login: "dkf"
- mimetype: "text/html"
- username: "dkf"
- icomment:
Ticket change [9fe20132eb] (rid 146641) by dgp on 2016-05-06 15:28:02:
- icomment:
Let me just observe that StackFreshCatches() does really weird things with its catchDepth argument. Help us, kbk, you're our only hope!
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [fe2fab164f] (rid 146642) by dgp on 2016-05-06 15:28:41:
- assignee changed to: "kbk"
- login: "dgp"
- mimetype: "text/plain"
Ticket change [d38bd9c793] (rid 146645) by dgp on 2016-05-06 16:09:13:
- icomment:
If nothing better comes along, a fix is now on the branch bug-3154ea2759. Feels too much like a workaround for my taste, but I don't pretend I follow all the housekeeping that's in play here. FWIW, the reason 6 is the magic number is that's what takes us out of static storage for Exception Ranges, and dynamic storage apparently leads to array bound violations writing where it's dangerous. Array bound violations from the static storage likely just scribbled on the high end of the static storage for literal values, which would only matter if there were 59 or 60 of them, no more and no less.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [231eeb5e3e] (rid 146648) by dkf on 2016-05-07 16:23:02:
- icomment:
I have a similar change applied to my private branch. The real problem is why the state is wrong in the first place.
- login: "dkf"
- mimetype: "text/html"
- username: "dkf"
- icomment:
Ticket change [d9d5f7e2a9] (rid 146649) by dkf on 2016-05-07 19:59:16:
- icomment:
Looking at this code (rewritten to be a bit clearer about basic blocks): <pre> beginCatch @badLabel push error push testing invokeStk 2 pop push 0 jump @okLabel label @badLabel push 1; # should be pushReturnCode jump @okLabel label @okLabel endCatch pop </pre> Is it just me or does the code get to the <tt>endCatch</tt> with the exception stack in an inconsistent state? (The analyser doesn't know that <tt>error testing</tt> will always throw.) If the <tt>invokeStk</tt> doesn't throw, the jump to <tt>@okLabel</tt> will be done with exception stack having depth 1. If the <tt>invokeStk</tt> <i>does</i> throw, the implicit jump to <tt>@okLabel</tt> will be effectively done with exception stack having depth 0. Hence the crunch. This code ought to refuse to compile. <p> Assuming I'm analysing this all right. - login: "dkf"
- mimetype: "text/html"
- username: "dkf"
- icomment:
Ticket change [206fb190b2] (rid 146650) by dkf on 2016-05-08 16:48:50:
- icomment:
Hah! I'm wrong. Taking the exception doesn't pop the exception stack.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [172ce716aa] (rid 147012) by dgp on 2016-06-20 21:47:26:
- icomment: "status?"
- login: "dgp"
- mimetype: "text/plain"
Ticket change [03334108e2] (rid 147051) by aspect on 2016-06-22 13:27:43:
- icomment:
seems like kbk already snuck this into trunk .. http://core.tcl.tk/tcl/timeline?r=bug-3154ea2759 Looking at the code again (without understanding much!), this seems to be the right thing. The second loop in UnstackExpiredCatches already includes such a guard, and can clearly set catches[i]=NULL, catchDepth[i]=-1 for items not at the end of the array. StackFreshCatches also has the guard and LookForFreshCatches has a stronger condition. This all argues that NULL/-1 in the middle of the array is legit, UEC has been missing the guard now in trunk since it was born, and six exception ranges really is a lot! I suspect StackFreshCatches only has catchDepth as an argument out of symmetry with UnstackExpiredCatches, which actually needs it. Some tidying up could be in order, but I think the bug is satisfactorily resolved.
- login: "aspect"
- mimetype: "text/plain"
- icomment:
Ticket change [dc4b2739ab] (rid 147056) by dkf on 2016-06-22 14:57:07:
- closedate changed to: "2457562.12299742"
- closer changed to: "dkf"
- icomment:
Since kbk's merged it, we can just close this. Don't know if the fix is perfect, but it stops the crashes.
- login: "dkf"
- mimetype: "text/html"
- resolution changed to: "Fixed"
- status changed to: "Closed"
- username: "dkf"