Tcl Source Code

View Ticket
Login
Ticket UUID: bdd91c7e431d8d9d448e5ce04cc02849c901e6f8
Title: tclsh crashes in [interp delete]
Type: Bug Created on: 2013-09-12 15:43:54
Submitter: azazel Assigned to: dgp
Subsystem: 20. [interp] Severity: Severe
Priority: 5 Medium Last modified: 2013-09-13 14:15:52
Status: Closed Closed by: dgp
Resolution: Fixed Closed on: 2013-09-13 14:15:52
Version: 8.5.14
Description:
I've compiled 8.5.14 as follows:

  ./configure --enable-shared --enable-symbols --prefix=/tmp/tcl8.5.14 && make && make install

Running the following code:

  $ cat ~/tmp/test/test.tcl
  set i [interp create]

  interp eval $i {

    for { set i 1000000 } { $i < 1250000 } { incr i } {

      lappend data $i $i

    }

    lappend l {*}$data

    array set a $data

    set d [dict create {*}$data]

  }

  interp delete $i

causes a seg-fault:

  $ /tmp/tcl8.5.14/bin/tclsh8.5 ~/tmp/test/test.tcl
  Segmentation fault (core dumped)

when deleting the slave interpreter:

  (gdb) bt
  #0  0x00007f0fa6c7c040 in TclObjCallVarTraces [...]
  #1  0x00007f0fa6c897dd in DeleteArray [...]
  #2  0x00007f0fa6c857f8 in UnsetVarStruct [...]
  #3  0x00007f0fa6c89393 in TclDeleteNamespaceVars [...]
  #4  0x00007f0fa6c46a63 in TclTeardownNamespace [...]
  #5  0x00007f0fa6b9eecc in DeleteInterpProc [...]
  #6  0x00007f0fa6c62b31 in Tcl_EventuallyFree [...]
  #7  0x00007f0fa6b9edd8 in Tcl_DeleteInterp [...]
  #8  0x00007f0fa6c23831 in SlaveObjCmdDeleteProc [...]
  #9  0x00007f0fa6ba0fc1 in Tcl_DeleteCommandFromToken [...]
  #10 0x00007f0fa6c206c1 in Tcl_InterpObjCmd [...]
  #11 0x00007f0fa6ba2374 in TclEvalObjvInternal [...]
  #12 0x00007f0fa6ba3872 in TclEvalEx [...]
  #13 0x00007f0fa6ba2bec in Tcl_EvalEx [...]
  #14 0x00007f0fa6c3bf3d in Tcl_FSEvalFileEx [...]
  #15 0x00007f0fa6c44f75 in Tcl_Main [...]
  #16 0x00000000004008c3 in main [...]

Running it under valgrind reveals the following:

  Invalid write of size 8
     at 0x4ED287D: TclExecuteByteCode (tclExecute.c:2408)
     by 0x4ED09F0: TclCompEvalObj (tclExecute.c:1537)
     by 0x4E6D966: TclEvalObjEx (tclBasic.c:5260)
     by 0x4EECA27: SlaveEval (tclInterp.c:2611)
     by 0x4EE977E: Tcl_InterpObjCmd (tclInterp.c:789)
     by 0x4E6B383: TclEvalObjvInternal (tclBasic.c:3693)
     by 0x4E6C881: TclEvalEx (tclBasic.c:4392)
     by 0x4E6BBFB: Tcl_EvalEx (tclBasic.c:4049)
     by 0x4F04F4C: Tcl_FSEvalFileEx (tclIOUtil.c:1853)
     by 0x4F0DF84: Tcl_Main (tclMain.c:439)
     by 0x4008D2: main (tclAppInit.c:85)
   Address 0x5b23fe8 is 104 bytes inside a block of size 16,040 free'd
     at 0x4C2A68C: free (vg_replace_malloc.c:446)
     by 0x4E668C3: TclpFree (tclAlloc.c:723)
     by 0x4E73611: Tcl_Free (tclCkalloc.c:1221)
     by 0x4ECF88A: DeleteExecStack (tclExecute.c:772)
     by 0x4ECFE8A: GrowEvaluationStack (tclExecute.c:1000)
     by 0x4ED2525: TclExecuteByteCode (tclExecute.c:2304)
     by 0x4ED09F0: TclCompEvalObj (tclExecute.c:1537)
     by 0x4E6D966: TclEvalObjEx (tclBasic.c:5260)
     by 0x4EECA27: SlaveEval (tclInterp.c:2611)
     by 0x4EE977E: Tcl_InterpObjCmd (tclInterp.c:789)
     by 0x4E6B383: TclEvalObjvInternal (tclBasic.c:3693)
     by 0x4E6C881: TclEvalEx (tclBasic.c:4392)

  Invalid read of size 8
     at 0x4ED2996: TclExecuteByteCode (tclExecute.c:2422)
     by 0x4ED09F0: TclCompEvalObj (tclExecute.c:1537)
     by 0x4E6D966: TclEvalObjEx (tclBasic.c:5260)
     by 0x4EECA27: SlaveEval (tclInterp.c:2611)
     by 0x4EE977E: Tcl_InterpObjCmd (tclInterp.c:789)
     by 0x4E6B383: TclEvalObjvInternal (tclBasic.c:3693)
     by 0x4E6C881: TclEvalEx (tclBasic.c:4392)
     by 0x4E6BBFB: Tcl_EvalEx (tclBasic.c:4049)
     by 0x4F04F4C: Tcl_FSEvalFileEx (tclIOUtil.c:1853)
     by 0x4F0DF84: Tcl_Main (tclMain.c:439)
     by 0x4008D2: main (tclAppInit.c:85)
   Address 0x5b23fd8 is 88 bytes inside a block of size 16,040 free'd
     at 0x4C2A68C: free (vg_replace_malloc.c:446)
     by 0x4E668C3: TclpFree (tclAlloc.c:723)
     by 0x4E73611: Tcl_Free (tclCkalloc.c:1221)
     by 0x4ECF88A: DeleteExecStack (tclExecute.c:772)
     by 0x4ECFE8A: GrowEvaluationStack (tclExecute.c:1000)
     by 0x4ED2525: TclExecuteByteCode (tclExecute.c:2304)
     by 0x4ED09F0: TclCompEvalObj (tclExecute.c:1537)
     by 0x4E6D966: TclEvalObjEx (tclBasic.c:5260)
     by 0x4EECA27: SlaveEval (tclInterp.c:2611)
     by 0x4EE977E: Tcl_InterpObjCmd (tclInterp.c:789)
     by 0x4E6B383: TclEvalObjvInternal (tclBasic.c:3693)
     by 0x4E6C881: TclEvalEx (tclBasic.c:4392)

Afaics, the problem lies in generic/tclExecute.c, ll. 2304ff.:

	moved = (GrowEvaluationStack(iPtr->execEnvPtr, length, 1) - 1)
		- (Tcl_Obj **) initCatchTop;

	if (moved) {
	    /*
	     * Change the global data to point to the new stack.
	     */

	    initCatchTop += moved;
	    catchTop += moved;
	    initTosPtr += moved;
	    tosPtr += moved;
	    esPtr = iPtr->execEnvPtr->execStackPtr;
	}

Note that if the stack has been moved, initCatchTop and catchTop are adjusted,
but bcFramePtr is not, which results in the invalid accesses at ll. 2408ff.:

	    bcFramePtr->data.tebc.pc = (char *) pc;
	    iPtr->cmdFramePtr = bcFramePtr;
	    if (iPtr->flags & INTERP_DEBUG_FRAME) {
		TclArgumentBCEnter((Tcl_Interp *) iPtr, objv, objc,
			codePtr, bcFramePtr, pc - codePtr->codeStart);
	    }
	    DECACHE_STACK_INFO();
	    result = TclEvalObjvInternal(interp, objc, objv,
		    /* call from TEBC */(char *) -1, -1, 0);
	    CACHE_STACK_INFO();
	    if (iPtr->flags & INTERP_DEBUG_FRAME) {
		TclArgumentBCRelease((Tcl_Interp *) iPtr, objv, objc,
			codePtr, pc - codePtr->codeStart);
	    }
	    iPtr->cmdFramePtr = iPtr->cmdFramePtr->nextPtr;

Adjusting bcFramePtr:

  $ diff -uw generic/tclExecute.c.orig generic/tclExecute.c
  --- generic/tclExecute.c.orig   2013-09-12 16:39:13.339001366 +0100
  +++ generic/tclExecute.c        2013-09-12 16:39:18.383001702 +0100
  @@ -2311,6 +2311,7 @@

	      initCatchTop += moved;
	      catchTop += moved;
  +           bcFramePtr = (CmdFrame *) (initCatchTop + codePtr->maxExceptDepth + 1);
	      initTosPtr += moved;
	      tosPtr += moved;
	      esPtr = iPtr->execEnvPtr->execStackPtr;

appears to fix the problem.
User Comments:
jan.nijtmans added on 2013-09-13 09:46:30:
Appropriate for 8.5.15?

jan.nijtmans added on 2013-09-13 11:57:07:

Suggested fix committed in branch: [c910b7341a]


dgp added on 2013-09-13 12:18:04:
I'd like to see it reproduced.  I haven't yet been able
to do that here.  I'd like to have miguel comment too,
if we can manage that.

All signs are this is a good bug catch and fix, and assuming
we can get the i's dotted and t's crossed in time, yes I
want known segfaults fixed in 8.5.15.

azazel added on 2013-09-13 12:37:32:
The initial seg-fault cropped up on my Debian testing box.  Just tried on RHEL6 and got this:

  $ /space/azazel/tcl8.5.14/bin/tclsh8.5 interp_crash.tcl
  *** glibc detected *** /space/azazel/tcl8.5.14/bin/tclsh8.5: double free or corruption (!prev): 0x00000000017e0400 ***
  ======= Backtrace: =========
  /lib64/libc.so.6[0x34ef476126]
  /lib64/libc.so.6[0x34ef478c53]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(TclpFree+0x18)[0x7fe3d827ba70]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(Tcl_Free+0x18)[0x7fe3d8288c9a]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(+0x123793)[0x7fe3d836b793]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(Tcl_DeleteHashTable+0x9a)[0x7fe3d82fddce]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(+0x12207a)[0x7fe3d836a07a]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(+0x11de7b)[0x7fe3d8365e7b]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(+0x121b16)[0x7fe3d8369b16]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(TclTeardownNamespace+0x21)[0x7fe3d832672c]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(+0x350fa)[0x7fe3d827d0fa]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(Tcl_EventuallyFree+0xa4)[0x7fe3d8342b11]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(Tcl_DeleteInterp+0x6b)[0x7fe3d827cfdf]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(+0xbb0d0)[0x7fe3d83030d0]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(Tcl_DeleteCommandFromToken+0x1ac)[0x7fe3d827f324]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(+0xb7f7f)[0x7fe3d82fff7f]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(+0x38726)[0x7fe3d8280726]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(+0x39cb1)[0x7fe3d8281cb1]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(Tcl_EvalEx+0x44)[0x7fe3d828100a]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(Tcl_FSEvalFileEx+0x3dd)[0x7fe3d831b9ff]
  /space/azazel/tcl8.5.14/lib/libtcl8.5.so(Tcl_Main+0x480)[0x7fe3d8324bb9]
  /space/azazel/tcl8.5.14/bin/tclsh8.5(main+0x27)[0x40082b]
  /lib64/libc.so.6(__libc_start_main+0xfd)[0x34ef41ecdd]
  /space/azazel/tcl8.5.14/bin/tclsh8.5[0x400749]
  ======= Memory map: ========
  00400000-00401000 r-xp 00000000 08:06 29843884                           /space/azazel/tcl8.5.14/bin/tclsh8.5
  00600000-00601000 rw-p 00000000 08:06 29843884                           /space/azazel/tcl8.5.14/bin/tclsh8.5
  017b8000-05188000 rw-p 00000000 00:00 0                                  [heap]
  34ef000000-34ef020000 r-xp 00000000 08:01 154627                         /lib64/ld-2.12.so
  34ef21f000-34ef220000 r--p 0001f000 08:01 154627                         /lib64/ld-2.12.so
  34ef220000-34ef221000 rw-p 00020000 08:01 154627                         /lib64/ld-2.12.so
  34ef221000-34ef222000 rw-p 00000000 00:00 0
  34ef400000-34ef58a000 r-xp 00000000 08:01 154958                         /lib64/libc-2.12.so
  34ef58a000-34ef789000 ---p 0018a000 08:01 154958                         /lib64/libc-2.12.so
  34ef789000-34ef78d000 r--p 00189000 08:01 154958                         /lib64/libc-2.12.so
  34ef78d000-34ef78e000 rw-p 0018d000 08:01 154958                         /lib64/libc-2.12.so
  34ef78e000-34ef793000 rw-p 00000000 00:00 0
  34ef800000-34ef802000 r-xp 00000000 08:01 131373                         /lib64/libdl-2.12.so
  34ef802000-34efa02000 ---p 00002000 08:01 131373                         /lib64/libdl-2.12.so
  34efa02000-34efa03000 r--p 00002000 08:01 131373                         /lib64/libdl-2.12.so
  34efa03000-34efa04000 rw-p 00003000 08:01 131373                         /lib64/libdl-2.12.so
  34f0800000-34f0883000 r-xp 00000000 08:01 157415                         /lib64/libm-2.12.so
  34f0883000-34f0a82000 ---p 00083000 08:01 157415                         /lib64/libm-2.12.so
  34f0a82000-34f0a83000 r--p 00082000 08:01 157415                         /lib64/libm-2.12.so
  34f0a83000-34f0a84000 rw-p 00083000 08:01 157415                         /lib64/libm-2.12.so
  34f1c00000-34f1c16000 r-xp 00000000 08:01 157414                         /lib64/libgcc_s-4.4.7-20120601.so.1
  34f1c16000-34f1e15000 ---p 00016000 08:01 157414                         /lib64/libgcc_s-4.4.7-20120601.so.1
  34f1e15000-34f1e16000 rw-p 00015000 08:01 157414                         /lib64/libgcc_s-4.4.7-20120601.so.1
  7fe3d0be9000-7fe3d1d8c000 rw-p 00000000 00:00 0
  7fe3d2360000-7fe3d2361000 rw-p 00000000 00:00 0
  7fe3d2361000-7fe3d239d000 r--s 00000000 08:05 518                        /var/db/nscd/passwd
  7fe3d239d000-7fe3d822e000 r--p 00000000 08:01 25357                      /usr/lib/locale/locale-archive
  7fe3d822e000-7fe3d8232000 rw-p 00000000 00:00 0
  7fe3d8248000-7fe3d83aa000 r-xp 00000000 08:06 29843883                   /space/azazel/tcl8.5.14/lib/libtcl8.5.so
  7fe3d83aa000-7fe3d85a9000 ---p 00162000 08:06 29843883                   /space/azazel/tcl8.5.14/lib/libtcl8.5.so
  7fe3d85a9000-7fe3d85b5000 rw-p 00161000 08:06 29843883                   /space/azazel/tcl8.5.14/lib/libtcl8.5.so
  7fe3d85b5000-7fe3d85b6000 rw-p 00000000 00:00 0
  7ffff539b000-7ffff53b0000 rw-p 00000000 00:00 0                          [stack]
  7ffff53ff000-7ffff5400000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  Aborted (core dumped)

Valgrind showed the same invalid accesses and applying the same patch appeared to fix the problem.

dgp added on 2013-09-13 12:54:26:
I've reproduced it now.  The difference between
startup script and interactive eval matters.

Checking the fix next...

dgp added on 2013-09-13 13:01:56:
The fix is confirmed effective.  I'll merge it in
for inclusion in 8.5.15.

Anyone tried 8.6 yet?

dgp added on 2013-09-13 13:33:03:
The demo script does not crash tclsh8.6 and
the code appears to have changed to use macros
so the same sort of bug cannot happen there.

dgp added on 2013-09-13 14:15:52:
Fix ready to go out in 8.5.15.