Skip to content
Tcl Source Code History Of Ticket 3154ea2759
Login

History Of Ticket 3154ea2759

Artifacts Associated With Ticket 3154ea2759

  1. Ticket change [41d6102128] (rid 146633) by anonymous on 2016-05-04 14:12:05:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. 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.
      
    5. foundin initialized to: "8.6.5"
    6. is_private initialized to: "0"
    7. login: "anonymous"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "1fa85a7a8cf201549ab887413ec59508d86baa00"
    10. resolution initialized to: "None"
    11. severity initialized to: "Minor"
    12. status initialized to: "Open"
    13. submitter initialized to: "anonymous"
    14. subsystem initialized to: "47. Bytecode Compiler"
    15. title initialized to: "Repeat catch six times and get core dump"
    16. type initialized to: "Bug"
  2. Ticket change [2bb992d20f] (rid 146634) by dgp on 2016-05-04 15:21:16:

    1. assignee changed to: "dkf"
    2. login: "dgp"
    3. mimetype: "text/plain"
    4. priority changed to: "7 High"
  3. Ticket change [b8f8f9fb50] (rid 146636) by dkf on 2016-05-05 09:12:47:

    1. 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>
      
    2. login: "dkf"
    3. mimetype: "text/html"
    4. priority changed to: "8"
    5. severity changed to: "Severe"
    6. username: "dkf"
  4. Ticket change [ecf946ff3b] (rid 146637) by dkf on 2016-05-05 09:17:04:

    1. 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.
      
    2. login: "dkf"
    3. mimetype: "text/html"
    4. username: "dkf"
  5. Ticket change [12b048a0ec] (rid 146638) by dkf on 2016-05-05 09:31:58:

    1. 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>
      
    2. login: "dkf"
    3. mimetype: "text/html"
    4. username: "dkf"
  6. Ticket change [ee2b6fd675] (rid 146639) by aspect on 2016-05-05 12:44:31:

    1. 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);              
          }
      
    2. login: "aspect"
    3. mimetype: "text/plain"
  7. Ticket change [90492cafa2] (rid 146640) by dkf on 2016-05-05 14:39:18:

    1. 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?
      
    2. login: "dkf"
    3. mimetype: "text/html"
    4. username: "dkf"
  8. Ticket change [9fe20132eb] (rid 146641) by dgp on 2016-05-06 15:28:02:

    1. icomment:
      
      Let me just observe that StackFreshCatches() does
      really weird things with its catchDepth argument.
      
      Help us, kbk, you're our only hope!
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  9. Ticket change [fe2fab164f] (rid 146642) by dgp on 2016-05-06 15:28:41:

    1. assignee changed to: "kbk"
    2. login: "dgp"
    3. mimetype: "text/plain"
  10. Ticket change [d38bd9c793] (rid 146645) by dgp on 2016-05-06 16:09:13:

    1. 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.
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  11. Ticket change [231eeb5e3e] (rid 146648) by dkf on 2016-05-07 16:23:02:

    1. icomment:
      I have a similar change applied to my private branch. The real problem is why the state is wrong in the first place.
      
    2. login: "dkf"
    3. mimetype: "text/html"
    4. username: "dkf"
  12. Ticket change [d9d5f7e2a9] (rid 146649) by dkf on 2016-05-07 19:59:16:

    1. 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.
      
    2. login: "dkf"
    3. mimetype: "text/html"
    4. username: "dkf"
  13. Ticket change [206fb190b2] (rid 146650) by dkf on 2016-05-08 16:48:50:

    1. icomment:
      Hah! I'm wrong. Taking the exception doesn't pop the exception stack.
      
    2. login: "dkf"
    3. mimetype: "text/plain"
    4. username: "dkf"
  14. Ticket change [172ce716aa] (rid 147012) by dgp on 2016-06-20 21:47:26:

    1. icomment: "status?"
    2. login: "dgp"
    3. mimetype: "text/plain"
  15. Ticket change [03334108e2] (rid 147051) by aspect on 2016-06-22 13:27:43:

    1. 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.
      
    2. login: "aspect"
    3. mimetype: "text/plain"
  16. Ticket change [dc4b2739ab] (rid 147056) by dkf on 2016-06-22 14:57:07:

    1. closedate changed to: "2457562.12299742"
    2. closer changed to: "dkf"
    3. icomment:
      Since kbk's merged it, we can just close this. Don't know if the fix is perfect, but it stops the crashes.
      
    4. login: "dkf"
    5. mimetype: "text/html"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"
    8. username: "dkf"