Skip to content
Tcl Source Code History Of Ticket 82521bfb6734f891dd
Login

History Of Ticket 82521bfb6734f891dd

Artifacts Associated With Ticket 82521bfb6734f891dd

  1. Ticket change [01f7bd1b90] (rid 140046) by anonymous on 2014-09-26 16:19:08:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. comment initialized to:
      I haven't been able to reduce this to a minimal test case, but it is reliably reproducible on our systems.  Prior to commit [af06c0fbf8] the code works, after that it segfaults (most of the time) or throws an error of the form "can't set "set�Rl": upvar refers to variable in deleted namespace".  The reported variable name differs and appears to be the result of random memory corruption.  Sometimes the error message refers to a deleted array rather than namespace.  The error always seems to be thrown at the same line, which doesn't set any variables:
      
      if {[string compare $dateopened ""] != 0 || $openrequest} {
      
      The code runs in NaviServer (4.99.6) and is part of a 200k line codebase, and since it appears to be failing as a result of prior state corruption it is proving difficult to reduce to a minimal test case.  The core dumps don't paint a very consistent picture, but often it appears to be in TEBCresume, under the doStoreVarDirect label, the line Tcl_DecrRefCount(valuePtr).  valuePtr is often 0x600000002, which is invalid.
      
      I bisected the commit history between 8.6.2 (bad) and 8.6.1 (good).  Commit [eabc1b8699] doesn't produce the segfault or errors, [af06c0fbf8] does.
      
      Running on Ubuntu 12.04 64 bit, Tcl and NaviServer both 64 bit.
      
    5. foundin initialized to: "8.6.2"
    6. is_private initialized to: "0"
    7. login: "anonymous"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "54152f94cb1628c0c148b4b5708ace53216227b0"
    10. resolution initialized to: "None"
    11. severity initialized to: "Critical"
    12. status initialized to: "Open"
    13. submitter initialized to: "anonymous"
    14. subsystem initialized to: "47. Bytecode Compiler"
    15. title initialized to: "Segfault from commit af06c0fbf8 onwards"
    16. type initialized to: "Bug"
  2. Ticket change [dd9bb37154] (rid 140047) by dgp on 2014-09-26 16:37:10:

    1. assignee changed to: "msofer"
    2. login: "dgp"
    3. mimetype: "text/plain"
  3. Ticket change [7f4c0f6fb7] (rid 140048) by msofer on 2014-09-26 18:09:25:

    1. icomment:
      What would really really help is if you could compile Tcl with the -DPURIFY flag, and then run the thing under valgrind until it crashes, as that setup is sometimes bettle able to pinpoint the troule spot.
      
      In the meantime I'll take a deeper look at that commit.
      
    2. login: "msofer"
    3. mimetype: "text/plain"
  4. Ticket change [2a9160d1d3] (rid 140049) by anonymous on 2014-09-26 19:21:06:

    1. icomment:
      Built commit [af06c0fbf8] with -DPURIFY and ran it under valgrind, the only thing it complains about was the same read that was causing the segfault in the core dumps I looked at:
      
      ==19808== Thread 8:
      ==19808== Invalid read of size 4
      ==19808==    at 0x5C034D1: TEBCresume (tclExecute.c:3415)
      ==19808==    by 0x5B2B8AE: TclNRRunCallbacks (tclBasic.c:4392)
      ==19808==    by 0x5B2B036: Tcl_EvalObjv (tclBasic.c:4123)
      ==19808==    by 0x5B2D7B0: TclEvalEx (tclBasic.c:5261)
      ==19808==    by 0x5B2CB64: Tcl_EvalEx (tclBasic.c:4926)
      ==19808==    by 0x4E84D8A: Ns_TclEvalCallback (tclcallbacks.c:169)
      ==19808==    by 0x4E92DDA: NsTclRequestProc (tclrequest.c:435)
      ==19808==    by 0x4E75708: Ns_ConnRunRequest (op.c:291)
      ==19808==    by 0x4E788C3: NsConnThread (queue.c:1469)
      ==19808==    by 0x58DFDCB: NsThreadMain (thread.c:219)
      ==19808==    by 0x58E0328: ThreadMain (pthread.c:782)
      ==19808==    by 0x6124E99: start_thread (pthread_create.c:308)
      ==19808==  Address 0x600000002 is not stack'd, malloc'd or (recently) free'd
      ==19808== 
      [26/Sep/2014:12:02:31][19808.11a2f700][-conn:rl:0] Fatal: received fatal signal 11
      ==19808== 
      ==19808== HEAP SUMMARY:
      ==19808==     in use at exit: 129,354,243 bytes in 5,503 blocks
      ==19808==   total heap usage: 9,889 allocs, 4,386 frees, 223,181,036 bytes allocated
      ==19808== 
      ==19808== LEAK SUMMARY:
      ==19808==    definitely lost: 60 bytes in 1 blocks
      ==19808==    indirectly lost: 240 bytes in 10 blocks
      ==19808==      possibly lost: 127,203,735 bytes in 5,201 blocks
      ==19808==    still reachable: 2,150,208 bytes in 291 blocks
      ==19808==         suppressed: 0 bytes in 0 blocks
      ==19808== Rerun with --leak-check=full to see details of leaked memory
      ==19808== 
      ==19808== For counts of detected and suppressed errors, rerun with: -v
      ==19808== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
      
      
      For comparison, I did the same with the parent commit, which previously wasn't segfaulting (tried several runs, with three tests each).  Run under valgrind it now complains of the same invalid read.  I'll attempt the bisect again, but it will take some time - it is glacially slow under valgrind.
      
    2. login: "anonymous"
    3. mimetype: "text/plain"
  5. Ticket change [5aa90ee005] (rid 140050) by dgp on 2014-09-26 22:12:22:

    1. icomment:
      In checkin [af06c0fbf8] line 3415 of tclExecute.c is
      
          case INST_APPEND_SCALAR1:
      
      I don't think I see any read at all there, let alone
      an invalid one of size 4.
      
      Anyone else puzzled by this?
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  6. Ticket change [9c48c1e0f7] (rid 140052) by anonymous on 2014-09-27 09:55:55:

    1. icomment:
      <p>Something is clearly wrong with my test setup, my apologies.  In the core dumps the line that consistently segfaults is:
      
      <pre>
      #6  0x00007fa525912b51 in TEBCresume (data=0x24ea478, interp=0x2954910, result=0) at /home/cyan/local/src/tcl8.6.2/generic/tclExecute.c:3415
      3415                TclDecrRefCount(valuePtr);
      (gdb) l
      3410             */
      3411
      3412        doStoreVarDirect:
      3413            valuePtr = varPtr->value.objPtr;
      3414            if (valuePtr != NULL) {
      <b>3415                TclDecrRefCount(valuePtr);</b>
      3416            }
      3417            objResultPtr = OBJ_AT_TOS;
      3418            varPtr->value.objPtr = objResultPtr;
      3419    #ifndef TCL_COMPILE_DEBUG
      </pre>
      
      <p>These line numbers don't match the sources I was building from.  Curiously moving to different commits and building from them did consistently affect the segfault behaviour (and version number reporting).  I'll have to spend some time constructing a less convoluted the test setup before proceeding.
      
      <p>I'll append a new comment with the details once I'm confident that the results make sense.  Sorry for wasting time on red herrings.
      
    2. login: "anonymous"
    3. mimetype: "text/html"
  7. Ticket change [df840f7b04] (rid 140053) by msofer on 2014-09-27 12:05:21:

    1. icomment:
      This is obviously a case of mem corruption, so that the state of things at crash time has a high chance of being meaningless. We have to find the spot where the corruption happens, not what its effects are ...
      
      The bug possibly existed before [af06c0fbf8], and that checkin just made it trigger a crash whereas before it was inocuous - things that sometimes happen with eg write-after-free bugs: if the mem was freed and still free when overwritten, nothing happens in general - but it was reallocated for reuse, crash! What valgrind shows below doesn't seem to be quite this scenario. But it is obvious that the bug was present before that checkin, only without the ensuing crash.
      
      As naviserver has plenty of C code touching Tcl internals, it is not yet clear to me that the problem is in Tcl and not naviserver.
      
    2. login: "msofer"
    3. mimetype: "text/plain"
  8. Ticket change [619da0395c] (rid 140054) by msofer on 2014-09-27 16:55:37:

    1. icomment:
      Re-reading your last comment, an idea that popped to mind is: are you sure that every part of your setup is running with the same version of tcl that you used when compiling? When multiple tcl versions are available on the machine, it may happen that you compile against one and run against another one. Also that different parts or extensions expect different version. Pay special attention to extensions using internal headers (xotcl, naviserver, whatever ...).
      
    2. login: "msofer"
    3. mimetype: "text/plain"
  9. Ticket change [bfe275f212] (rid 140055) by anonymous on 2014-09-27 17:18:29:

    1. icomment:
      There were some problems with that, so I've rebuild everything from scratch to be isolated from the system now and confirmed that it's loading the correct libs, scripts, etc (using strace -f -eopen and checking all the paths).
      
      Then I bisected again, this time with -DPURIFY and --enable-symbols=mem, and reached a different commit ([cddbfc3081]) that reliably segfaulted or crashed (this time with "trying to free a TclObj created in a different thread").  Valgrind on that commit produced quite a lot of memory errors on that version and nothing on it's parent.  But I get the feeling I'm chasing a Heisenbug.
      
      I noticed that small changes to the proc that was hitting the segfault were affecting the outcome, even on code paths that weren't run.  So I tried dumping  [tcl::unsupported::disassemble proc] just after it was defined.  For versions that segfaulted when run, the disassemble would also segfault, so I'm trying to reduce the code down to the minimum necessary to produce the segfault on disassembly.
      
      The strange thing about this is that, although it has all the classic signs of a memory corruption issue, it's always and only this proc that segfaults.  It's running in production serving around 3.5 million hits daily on 9 servers, and nearly every terrible thing you could think of is being done by some part of the code.
      
    2. login: "anonymous"
    3. mimetype: "text/plain"
  10. Ticket change [356deadfae] (rid 140056) by msofer on 2014-09-27 17:54:19:

    1. icomment:
      OMG - what was the author of [cddbfc3081] thinking? (I'm allowed to ask this, as it happens to have been me :P)
      
      I do now believe that THAT commit is very bad. Prior to convincing myself and removing it from trunk, could I ask you to remove it's effects nd confirm that your problem is fixed? The easiest way to do that is possibly to edit you sources, replacing the definition of TclClearNumConversion with
      
      #define TclClearNumConversion(envPtr)
      
    2. login: "msofer"
    3. mimetype: "text/plain"
  11. Ticket change [9983534f7f] (rid 140057) by anonymous on 2014-09-27 18:51:22:

    1. icomment:
      Unfortunately I've lost data that let me exercise the bug on the production system, but before I did I managed to trim the code down to the following, which segfaults in Tcl 8.6.2 (outside of NaviServer, no other packages loaded), and not in 8.6.1:  (sorry for the obfuscation, I couldn't risk posting this code here where google can find it, which could expose security issues)
      
      namespace import -force ::tcl::mathop::ne
      
      proc foo {} {
      	global v5
      	global v6
      
      	if {[p1 $v1 s2 "" $v2 $v3]} {
      		return [join $s2 \n]
      	}
      
      	set v10 [p2 s1 s3 "x[p3 $v1]"]
      
      	if {$s1 ne ""} {
      		set v11 "x$s1 $v1"
      		p4 s8 $v11
      		return y$v1
      	}
      
      	if {$v10 eq ""} {
      		return "z$v1 a"
      	}
      
      	set v8 [p5 s4 $v10 s5]
      	set v9 [p5 s4 $v10 s6]
      
      	if {$v2} {
      		append v7 "c"
      		p2 s1 s10 "d$v8 e"
      		if {[ne $s1 ""]} {p4 s8 "u$s1 f$v8"}
      
      		append v7 "g"
      		p2 s1 s10 "h[p6 s9 s11]i$v8"
      		if {[ne $s1 ""]} {p4 s8 "u$s1 i$v8"}
      
      		append v7 "j"
      		p2 s1 s10 "x1$v8"
      		if {[ne $s1 ""]} {p4 s8 "u$s1 x2$v1"}
      
      		append v7 "k"
      		p2 s1 s10 "x3$v8 x4"
      		if {[ne $s1 ""]} {p4 s8 "u$s1 x5$v8"}
      
      		append v7 "l"
      		p2 s1 s10 "x6$v8"
      		if {[ne $s1 ""]} {p4 s8 "u$s1 x7$v8"}
      
      		append v7 "m"
      		p2 s1 s10 "x8$v8 x9"
      		if {[ne $s1 ""]} {p4 s8 "u$s1 x10$v8"}
      
      		if {[string match x11 $v9]} {
      			append v7 "n$v9 o"
      			p7 $v9
      
      			append v7 "p"
      			p2 s1 s10 "q[p3 $v9]r"
      			if {[ne $s1 ""]} {p4 s8 "u$s1 s$v9"}
      		}
      	}
      
      	if {$v4} {
      		append v7 "t$v9 r"
      
      		p2 s1 s10 "x12[p3 $v9]x13"
      		if {[ne $s1 ""] && [ne [p8 $s1] "x14"]} {p4 s8 "x15$s1"}
      
      		append v7 "s"
      		p2 s1 s10 "x16$v8 x17"
      		if {[ne $s1 ""]} {p4 s8 "u$s1 t$v8"}
      	}
      
      	set v9 0
      
      	set v10 [p2 s1 s3 "x18$v8"]
      
      	if {$v10 ne ""} {
      		set v11 [p5 s4 $v10 v11]
      
      		if {$v11 ne "" || $v9} {}
      	}
      }
      
      puts [tcl::unsupported::disassemble proc foo]
      
    2. login: "anonymous"
    3. mimetype: "text/plain"
  12. Ticket change [1a15b7d369] (rid 140067) by msofer on 2014-09-27 21:41:23:

    1. closedate changed to: "2456928.40374089"
    2. closer changed to: "msofer"
    3. icomment:
      Thank you for finding the bug and for that script, it helped me confirm the hypothesis. I reverted the stupid commit [cddbfc3081] from trunk, the test script doesn't fail anymore.
      
      Please test trunk with your full app, and reopen this bug if this didn't fix the problem. Independently of the results of that test, this did fix at least one bug.
      
    4. login: "msofer"
    5. mimetype: "text/x-fossil-wiki"
    6. priority changed to: "9 Immediate"
    7. status changed to: "Closed"
  13. Ticket change [60383d9468] (rid 140130) by anonymous on 2014-10-01 09:54:54:

    1. closedate changed to: "2456931.91312571"
    2. closer changed to: "anonymous"
    3. icomment:
      I've run the trunk version from Sunday [956d1be41b] in production for a few days now, no more segfaults, so I can confirm that it fixes the problem I was seeing.
      
      Thanks :)
      
    4. login: "anonymous"
    5. mimetype: "text/plain"