Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2013-02-05
03:49 • Closed ticket [3602706fff]: Segfault on tclsh startup plus 9 other changes artifact: 39a382810b user: dgp
01:40 • Ticket [3602706fff]: 4 changes artifact: f4de84bb20 user: dgp
01:01 • Ticket [3602706fff]: 4 changes artifact: a66baec770 user: dgp
00:15 • Ticket [3602706fff]: 4 changes artifact: 1be7327bd7 user: dgp
2013-02-04
23:54 • Ticket [3602706fff]: 6 changes artifact: d65608221e user: dgp
21:31 • Ticket [3602706fff]: 2 changes artifact: 2e62fa9d01 user: nijtmans
2013-02-01
16:49 • Ticket [3602706fff]: 4 changes artifact: 96f21b8d81 user: nijtmans
15:44 • Ticket [3602706fff]: 4 changes artifact: 331a5f7515 user: nijtmans
08:24 • Ticket [3602706fff]: 4 changes artifact: 3533347550 user: dkf
00:03 • Ticket [3602706fff]: 4 changes artifact: b87a91cdef user: dgp
2013-01-31
23:43 • Ticket [3602706fff]: 4 changes artifact: df576dcc4e user: dgp
22:59 • Ticket [3602706fff]: 4 changes artifact: 77319e5b29 user: nijtmans
22:49 • Ticket [3602706fff]: 4 changes artifact: d117246b82 user: dgp
22:24 • Ticket [3602706fff]: 4 changes artifact: cf4e460600 user: nijtmans
21:59 • Ticket [3602706fff]: 4 changes artifact: 555b897555 user: dgp
16:30 • Ticket [3602706fff]: 4 changes artifact: 84f545f409 user: nijtmans
16:27 • Ticket [3602706fff]: 4 changes artifact: b5fb5ff3fd user: dkf
16:22 • Ticket [3602706fff]: 4 changes artifact: 0563e1c00a user: nijtmans
16:21 • Ticket [3602706fff]: 4 changes artifact: 0612f763ba user: dkf
16:15 • Ticket [3602706fff]: 4 changes artifact: da93910cbf user: tombert
13:40 • Ticket [3602706fff]: 4 changes artifact: e3993b2e8d user: tombert
04:50 • Ticket [3602706fff]: 4 changes artifact: 5c2b418af9 user: nijtmans
04:31 • Ticket [3602706fff]: 5 changes artifact: ded7b43df3 user: andreas_kupries
2013-01-30
21:23 • New ticket [3602706fff]. artifact: 8147007645 user: tombert

Segfault on tclsh startup

Closed / Fixed3602706fffffffffffffffffffffffffffffffffCreated 14 years agoUpdated 14 years ago
Descriptionplain2013-01-30 21:23:22
Somewhere between the checkins from 2012-12-07 till 2013-01-30 a bug was introduced.
There is no need to run a script, it crashes by simply starting the tclsh. Sidenote: I also updated tcl 8.6 and there is no crash.
Happens in Windows 7, MinGw, gcc 4.6.2.

Here is the output of gdb:

Starting program: E:\CM.git\tcltk85\debug\bin\tclsh85g.exe
[New Thread 9456.0x1ec8]

Program received signal SIGSEGV, Segmentation fault.
TclThreadAllocObj () at ./../generic/tclThreadAlloc.c:570
570         cachePtr->firstObjPtr = objPtr->internalRep.otherValuePtr;


----------------------------------
thx for your support
andreas_kupriesplain2013-01-31 04:31:04
Thomas, is possible for you to 'fossil bisect' the issue until you have the exact revision which causes the crash?

I rmemeber we had recent issues with CPUID detection and code.
Are you by chance on a 64bit platform?

From the naming of the tclsh I deduce that you have a debug build of some kind, is that correct ?

If yes, does the crash happen for a non-debug build also ?
nijtmansplain2013-01-31 04:50:25
I suspect this is a dup of 3601260

Does the following branch have the problem as well?
   <http://core.tcl.tk/tcl/info/10b2640e48>

I wasn't able to create a strack-trace.

Regards,
        Jan Nijtmans
tombertplain2013-01-31 13:40:45
It happens in the release version as well.
Ok - I will "fossil bisect" into it ...
tombertplain2013-01-31 16:15:21
2013-01-13 23:15:10 8aca9a8e961fb172 BAD
2013-01-13 09:04:10 4c2509336a9c2b2c GOOD CURRENT
dkfplain2013-01-31 16:21:59
In the context of the 8aca9a8e961fb172 commit, do we have a backtrace at the point of the crash?
Are any problems hit in an --enable-symbols=mem build?
nijtmansplain2013-01-31 16:22:13
When bisecting Bug #3601260, I came to exactly the same
commit which started the problem! Looks they are really dups,
even though the effect is different with different mingw
compiler versions.
dkfplain2013-01-31 16:27:13
The trigger for this crash is not on a codepath that my machine seems to exercise (for whatever reason); I can't hunt it directly. I *SUSPECT* that it is an unfixed location in the Win-specific code, but I have no true justification for that other than a hunch.
nijtmansplain2013-01-31 16:30:40
Looking at the changes in [8aca9a8e961fb172], I suspect
that it is related to refCounting objects. The segfault
occurs in the thread allocator, where objPtr point
to invalid memory, so somewhere the
Tcl_IncRefCount/Tcl_DecrRefCount's don't match.
That's my current theory. (this might be
in win-specific code, indeed, which is
fixed in trunk)

The easiest fix would be to simply
revert [8aca9a8e961fb172] on
core-8-5-branch and then merge-mark
to trunk (so the improvement is kept there)
That gives some time to investigate this,
without everyone (like me) being hindered by this.
dgpplain2013-01-31 21:59:28
Opened new branch bug-3602706 for
finding and fixing the bug.  Please confirm
it demonstrates the problem.  It Works For Me.
nijtmansplain2013-01-31 22:24:21
Yes it has the bug. I also checked my theory that this is a refCount bug, by
uncommenting all Tcl_DecrRefCount() calls (which makes all tests pass)
and then restoring them one by one until the tests fail again.

It turns out the the refCount goes wrong with the part2Ptr in the
function Tcl_GetVar2Ex. Minimal fix is in the branch now. I'm
not 100% sure this is the right fix, but it works!
dgpplain2013-01-31 22:49:16
Thanks!  Will examine.
nijtmansplain2013-01-31 22:59:54
It looks like we must thank dkf that trunk didn't have the crash. In
the following commit he added a lot of IncrRefCount/DecrRefCount
pairs on "trunk":
<http://core.tcl.tk/tcl/info/d91c86d0da>
Those were never backported to core-8-5-branch.
dgpplain2013-01-31 23:43:17
That suggests the problem lies in callers
of Tcl_GetVar2() which differs between
the two branches, bypassing the suspected
broken Tcl_GetVar2Ex() on the trunk, but
hitting it on 8.5.

Grep says that tclWinInit.c calls Tcl_GetVar2()
to set some vars in the ::env array.
dgpplain2013-02-01 00:03:31
The only other places Tcl_GetVar2 is called
is in the testing command [testset2] which is
hardly used at all, and in write traces on ::env
which is generic code.  There ought to be a way
to use that to demo the trouble on all platforms
on this bug branch.
dkfplain2013-02-01 08:24:46
FWIW, that commit was (partially) about reducing the depth of callframes associated with the legacy variable interfaces, keeping things simpler in backtraces and *maybe* reducing costs.

I wasn't trying to armour against someone else's bugs. :-)
nijtmansplain2013-02-01 15:44:31
I think I got it. The call that goes wrong is (tclWinInit.c):
    ptr = Tcl_GetVar2(interp, "env", "HOME", TCL_GLOBAL_ONLY);
but only when $env(HOME) does not exist. My $HOME env
variable was set while in Thomas' environment it wasn't,
that explains the difference in effect. My env.test test
cases were failing because they clean up the env array
and then start up a new tclsh (which doesn't have
$HOME --> crash). This explains everything!

The stack trace looks like (manuallly reconstructed)
  Tcl_GetVar2
  Tcl_GetVar2Ex
  Tcl_ObjGetVar2
  TclObjLookupVarEx(.../*createPart1*/ 0, /*createPart2*/ 1, ...)

Why is the createPart2 parameter 1???? That's the bug, is it?? Or
is it needed for read traces on the array???? The
effect is that a new hash entry is created for the env(HOME) key,
so its refCount is incremented, and (at the end of
TclObjLookupVarEx) it's decremented again. Callers
of Tcl_ObjGetVar2 don't expect that, do they?

Anyway, changing the parameter from 1 to 0 makes
all test-cases pass. (Done now on the branch).

Please evaluate! Tcl 8.6 has the same bug, but
because of dkf's already mentioned commit it
isn't noticed there.

There are more TclObjLookupVarEx calls with
createPart2=1, those should be checked too!
nijtmansplain2013-02-01 16:49:52
In tclParse.c, I see the following code:
....
arrayIndex = Tcl_GetObjResult(interp);
Tcl_IncrRefCount(arrayIndex);
....
varName = Tcl_NewStringObj(tokenPtr[1].start,
tokenPtr[1].size);
appendObj = Tcl_ObjGetVar2(interp, varName, arrayIndex,
TCL_LEAVE_ERR_MSG);
Tcl_DecrRefCount(varName);
....
    if (arrayIndex != NULL) {
Tcl_DecrRefCount(arrayIndex);
    }

So, for 'arrayIndex' the refCount is incremented before the
Tcl_ObjGetVar2 call, but for 'varName' it is not! For
"novem" it might be better to let Tcl_ObjGetVar2 clean up
part1Ptr and part2Ptr if the refCount is 0. That would
allow to simplify the above code to:
....
arrayIndex = Tcl_GetObjResult(interp);
/* Tcl_IncrRefCount(arrayIndex); No longer necessary */
....
varName = Tcl_NewStringObj(tokenPtr[1].start,
tokenPtr[1].size);
appendObj = Tcl_ObjGetVar2(interp, varName, arrayIndex,
TCL_LEAVE_ERR_MSG);
/* Tcl_DecrRefCount(varName); No longer necessary */
....
    /* if (arrayIndex != NULL) {
Tcl_DecrRefCount(arrayIndex);
    } No longer necessary */
nijtmans Ticket [3602706fff]: 2 changes [History diff]2013-02-04 21:31:17
dgpplain2013-02-04 23:54:08
Returning to the scene of the crime, I've
extended the mig-review branch to add the
refcount housekeeping and comment that
got missed or botched the first time.
dgpplain2013-02-05 00:15:39
New forked tip of the bug-3602706 branch
cherrypicks those fixes where they might be
conveniently tested.

Would be better to add cross-platform tests
that demonstrate the bug, but for now I'll
beg for a test run from the Windows crowd.
dgpplain2013-02-05 01:01:45
AArrgh.  Botched it again.  Please stand by.
dgpplain2013-02-05 01:40:46
Ok, fixed right this time, and a cross-platform test.
dgpplain2013-02-05 03:49:41
allow_comments - 1

8.5 and trunk branches updated with fix.
Log in (anonymous is fine) to add a comment.