Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-06-22
14:57 • Closed ticket [3154ea2759]: Repeat catch six times and get core dump plus 7 ... artifact: dc4b2739ab user: dkf
13:27 • Ticket [3154ea2759]: 3 changes artifact: 03334108e2 user: aspect
2016-06-20
21:47 • Ticket [3154ea2759]: 3 changes artifact: 172ce716aa user: dgp
2016-05-08
16:48 • Ticket [3154ea2759]: 4 changes artifact: 206fb190b2 user: dkf
2016-05-07
19:59 • Ticket [3154ea2759]: 4 changes artifact: d9d5f7e2a9 user: dkf
16:23 • Ticket [3154ea2759]: 4 changes artifact: 231eeb5e3e user: dkf
2016-05-06
16:09 • Ticket [3154ea2759]: 3 changes artifact: d38bd9c793 user: dgp
15:28 • Ticket [3154ea2759]: 3 changes artifact: fe2fab164f user: dgp
15:28 • Ticket [3154ea2759]: 3 changes artifact: 9fe20132eb user: dgp
2016-05-05
14:39 • Ticket [3154ea2759]: 4 changes artifact: 90492cafa2 user: dkf
12:44 • Ticket [3154ea2759]: 3 changes artifact: ee2b6fd675 user: aspect
09:31 • Ticket [3154ea2759]: 4 changes artifact: 12b048a0ec user: dkf
09:17 • Ticket [3154ea2759]: 4 changes artifact: ecf946ff3b user: dkf
09:12 • Ticket [3154ea2759]: 6 changes artifact: b8f8f9fb50 user: dkf
2016-05-04
15:21 • Ticket [3154ea2759]: 4 changes artifact: 2bb992d20f user: dgp
14:12 • New ticket [3154ea2759]. artifact: 41d6102128 user: anonymous

Repeat catch six times and get core dump

Closed / Fixed3154ea27591de16257a7b8b8d361514b004e527fCreated 11 years agoUpdated 10 years ago
Descriptionplain2016-05-04 14:12:05
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.
dgp Ticket [3154ea2759]: 4 changes [History diff]2016-05-04 15:21:16
dkfhtml2016-05-05 09:12:47

This code can trigger the crash even without executing __BEGIN; just compiling it is enough. This means that you can cause the crash with:

# procedure definition from above...
tcl::unsupported::disassemble proc __BEGIN

dkfhtml2016-05-05 09:17:04

Based on:

If the number of beginCatch/endCatch pairs is less than 6, the program succeeds.
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.

dkfhtml2016-05-05 09:31:58

Doesn't crash with mem-debugging enabled (!!!) but with just symbols I get this trace:

(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

aspectplain2016-05-05 12:44:31
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);              
    }
dkfhtml2016-05-05 14:39:18

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.

Why is the catchDepth wrong?

dgpplain2016-05-06 15:28:02
Let me just observe that StackFreshCatches() does
really weird things with its catchDepth argument.

Help us, kbk, you're our only hope!
dgp Ticket [3154ea2759]: 3 changes [History diff]2016-05-06 15:28:41
dgpplain2016-05-06 16:09:13
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.
dkfhtml2016-05-07 16:23:02

I have a similar change applied to my private branch. The real problem is why the state is wrong in the first place.

dkfhtml2016-05-07 19:59:16

Looking at this code (rewritten to be a bit clearer about basic blocks):

        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
Is it just me or does the code get to the endCatch with the exception stack in an inconsistent state? (The analyser doesn't know that error testing will always throw.) If the invokeStk doesn't throw, the jump to @okLabel will be done with exception stack having depth 1. If the invokeStk does throw, the implicit jump to @okLabel will be effectively done with exception stack having depth 0. Hence the crunch. This code ought to refuse to compile.

Assuming I'm analysing this all right.

dkfplain2016-05-08 16:48:50
Hah! I'm wrong. Taking the exception doesn't pop the exception stack.
dgpplain2016-06-20 21:47:26
status?
aspectplain2016-06-22 13:27:43
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.
dkfhtml2016-06-22 14:57:07

Since kbk's merged it, we can just close this. Don't know if the fix is perfect, but it stops the crashes.

Log in (anonymous is fine) to add a comment.