Skip to content
Tcl Source Code History Of Ticket e533296a92
Login

History Of Ticket e533296a92

Artifacts Associated With Ticket e533296a92

  1. Ticket change [13e3b667e9] (rid 255741) by venkat on 2026-08-26 00:26:57:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-plain"
    4. comment initialized to:
      Running this script causes tcl to die with a segmentation violation.  (This on MacOS from my own build)
      
      proc probe {s} {
          info frame -1
          catch {apply {{s} {eval $s}} $s}
      }
      probe {catch {probe {error X}}; probe {}}
      puts survived
      
    5. foundin initialized to: "933d99c5a7a36abd6746e7c45c873853af42a95a"
    6. is_private initialized to: "0"
    7. login: "venkat"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "1fb8c275493b1f1dcf8db46d6d541794a011947f"
    10. resolution initialized to: "None"
    11. severity initialized to: "Severe"
    12. status initialized to: "Open"
    13. submitter initialized to: "venkat"
    14. subsystem initialized to: "22. [proc] and [uplevel]"
    15. title initialized to: "SegFault on [info frame] on apply"
    16. type initialized to: "Bug"
  2. Ticket change [bc4a8692a9] (rid 255742) by oehhar on 2026-08-26 12:16:36:

    1. icomment:
      Good catch!
      Confirmed on 8.6.18 and 9.0.4 on MS-Windows.
      
      Thanks,
      Harald
      
    2. login: "oehhar"
    3. mimetype: "text/x-fossil-wiki"
    4. username: "oehhar"
  3. Ticket change [739e830331] (rid 255749) by chw on 2026-08-26 19:48:18:

    1. icomment:
      Seems be present even in 8.5. This is my wildly guessed fix (diff against trunk):
      
      ---snip---
      Index: generic/tclCmdIL.c
      ==================================================================
      --- generic/tclCmdIL.c
      +++ generic/tclCmdIL.c
      @@ -1292,13 +1292,11 @@
           case TCL_LOCATION_BC: {
       	/*
       	 * Execution of bytecode. Talk to the BC engine to fill out the frame.
       	 */
       
      -	CmdFrame *fPtr = (CmdFrame *)TclStackAlloc(interp, sizeof(CmdFrame));
      -
      -	*fPtr = *framePtr;
      +	CmdFrame f = *framePtr, *fPtr = &f;
       
       	/*
       	 * Note:
       	 * Type BC => f.data.eval.path	  is not used.
       	 *	      f.data.tebc.codePtr is used instead.
      @@ -1328,11 +1326,10 @@
       
       	ADD_PAIR("cmd", TclGetSourceFromFrame(fPtr, 0, NULL));
       	if (fPtr->cmdObj && framePtr->cmdObj == NULL) {
       	    needsFree = lc - 1;
       	}
      -	TclStackFree(interp, fPtr);
       	break;
           }
       
           case TCL_LOCATION_SOURCE:
       	/*
      
      ---snip---
      
    2. login: "chw"
    3. mimetype: "text/plain"
  4. Ticket change [c6095cb925] (rid 255750) by sebres on 2026-08-26 20:13:19:

    1. assignee changed to: "sebres"
    2. icomment:
      I don't think it'd change something. I'll take a look - my fork doesn't segfault almost since scratch (8.5).
      
    3. login: "sebres"
    4. mimetype: "text/x-fossil-wiki"
    5. priority changed to: "7 High"
    6. severity changed to: "Important"
  5. Ticket change [b03e329620] (rid 255758) by sebres on 2026-08-26 21:53:23:

    1. icomment:
      I rebased it to 8.5 (initially based on that), but my original "fixed" variant was much more complex (and it is hardly possible to back-port)... 
      
      Branch [/vdiff?from=05af4fe48a638308&to=bug-e533296a92d89044-segfault|bug-e533296a92d89044-segfault] contains the simplest fix, but the question still remains - why is a such procPtr (with cmd after apply is done) in the call-frames stack yet at that time point?
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  6. Ticket change [596c514514] (rid 255759) by sebres on 2026-08-26 21:54:00:

    1. foundin changed to: "8.5+"
    2. login: "sebres"
    3. mimetype: "text/plain"
    4. resolution changed to: "Fixed"
    5. status changed to: "Pending"
  7. Ticket change [6db76f97b4] (rid 255761) by apnadkarni on 2026-08-27 03:31:55:

    1. icomment:
      Sergey, I feel the fix proposed will solve this issue but there is a deeper, more serious problem as your question about procPtr implies. What I see is that the test script invokes `TclInfoFrame` (via `InfoFrameCmd`) three times. On the third invocation, debugger shows on entry to `TclInfoFrame`, the following values:
      
      ```
      CmdFrame *  framePtr 0x0000020cb5b10ef8
      CallFrame * framePtr->framePtr 0x0000020cb5b10db0
      Proc *      framePtr->framePtr->procPtr 0x0000020cb5ce4b08
      Command *   framePtr->framePtr->procPtr->cmdPtr 0x0000020cb5b110b0
      ```
      
      Note the last value in particular. Then the line [fptr = TclStackAlloc](https://core.tcl-lang.org/tcl/file?ci=9ec5286ed2fe765c&name=generic%2FtclCmdIL.c&ln=1299) returns 0x0000020cb5b110a0. Since sizeof(*fptr) is 88, this allocation overlaps the memory referenced by framePtr->framePtr->procPtr->cmdPtr. Consequently, this [line](https://core.tcl-lang.org/tcl/file?ci=9ec5286ed2fe765c&name=generic%2FtclExecute.c&ln=9762) overwrites `framePtr->framePtr->procPtr->cmdPtr->hPtr` causing the crash. But in the meanwhile plenty of other fields are also corrupted, this is merely the one hitting a crash first.
      
      It seems to me that some code path is releasing memory allocated by TclStackAlloc too early, resulting in that memory being overwritten while still in use. I don't understand the runtime frame management enough so you might be able to tell if that is the case and where it is happening much quicker than I could.
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  8. Ticket change [289f7d0449] (rid 255762) by apnadkarni on 2026-08-27 03:34:54:

    1. icomment:
      BTW, I think Christian's suggestion will also only hide the issue since it avoids use of `TclStackAlloc` but does not answer why some previous TclStackAlloc seems to be premptively freed.
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  9. Ticket change [4b357466b4] (rid 255763) by chw on 2026-08-27 05:02:58:

    1. icomment:
      Ashok, indeed, my first shot did pure error hiding.
      Based on sebreas idea, a more gentle guess for 8.6 is here:
      
      https://androwish.org/home/info/b22c637978b91e3a
      
    2. login: "chw"
    3. mimetype: "text/plain"
  10. Ticket change [3a6da0e4eb] (rid 255764) by sebres on 2026-08-27 10:23:36:

    1. icomment:
      I think setting `hPtr = NULL` before freeing memory is a dirty workaround, because it'd imply use after free (worse enough), as well as that the memory behind `hPtr` remains unchanged. It is bad idea.
      
      As for Ashok's analyze, the mentioned overwrite looks like an after effect to me -the cmd pointer used in Tcl_ApplyObjCmd for procPtr is reserved in program stack and without the reset (leaving Tcl_ApplyObjCmd) a usage after free is possible there too. But as I said before, I also think that this would require further investigation. Unfortunately I cannot backport correct fix, because my TEBC uses completely different cmd-location approach (without scriptCLLocPtr and co) - the way how it gets "compiled" is simply different.
      
    2. login: "sebres"
    3. mimetype: "text/x-markdown"
  11. Ticket change [99aec5853d] (rid 255765) by sebres on 2026-08-27 12:42:38:

    1. icomment:
      Well, independently to the question of procPtr of finalized lambda in frames stack, the situation where procPtr->cmdPtr may be NULL is normal and expected, see [/artifact?name=c778eb691b0dc621&ln=2480-2499|for example SetLambdaFromAny].
      
      Thus the fix of me is basically correct (it simply restores the situation before the lambda invocation), so I'd merge it if no objections follow.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  12. Ticket change [2678c921cf] (rid 255766) by sebres on 2026-08-27 14:17:31:

    1. icomment:
      OK, found and fixed it ultimately: this going to nested invocation of lambda, where by return from child to parent call, the info of lambda that got overwritten within child stack gets broken (or in my previous fix - get lost due to reset). Amend [a7a8993cca8e49c9] avoids this reset of lambda info in frames stack by nested invocations (reserves this info as long as it will be evaluated), so fill this info and reset the artificial command only once (at first/parent invocation); test now adjusted to cover it (lambda shall be in the last frame-info too).
      This makes it also a bit faster by nested invocations of lambda.
      
      However I'd admit that for performance reasons, one could rewrite this to initialize procPtr->cmdPtr only once by first invocation (as long as the lamda exists), but this would expect another clean-up and referring mechanism.<br/>
      At least it'd explain why invocation of lambdas were always faster in my fork.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  13. Ticket change [d3877c2665] (rid 255778) by jan.nijtmans on 2026-08-27 15:03:56:

    1. icomment:
      I put chw's proposal in a [152c3d8cd63c99da|branch] (based on 8.6, since I don't care about 8.5 any more ...., and without setting a field to NULL before a ckfree). This way, the two solutions (sebres' and chw's) can be compared.
      
      It appears to work for me. Can you (@sebres and @chw) come to an agreement which one is best?
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  14. Ticket change [36f050c3aa] (rid 255783) by sebres on 2026-08-27 18:09:57:

    1. icomment:
      I backported original idea in [4ca6eb5618ce2003] and better test to this branch (as new branch) which is NRE capable now, but... as already said...
      
        *  I guess we can optimize the solution (no matter 8.5- or NRE-based)
        *  the idea of preventive creation of artificial lambda information in order to provide ExtraFrameInfo (used by [info frame] only) is a bit strange, IMHO and such info (e. g. with lambdaPtr) can be provided on demand and retrieved inside [info frame];
        *  I don't like the approach to share mem-block from TclStackAlloc across NRE-trampoline (if yieldto would change the NRE stack, e. g. I cannot exclude out of sequence errors by TclStackFree);
      
      I'll try to provide better one (or backport some part from my fork).
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  15. Ticket change [c14e6d73b0] (rid 255789) by sebres on 2026-08-27 21:19:41:

    1. icomment:
      Branch [/timeline?r=bug-e533296a92-sebres-optimized|bug-e533296a92-sebres-optimized] is currently pure experimental (even tests would probably fail), because there is no proper clean-up/reference model for lambda-commands to nsPtr (so they are not dereferenced and apply doesn't detect yet if namespace get deleted).
      
      But the idea is very interesting in my opinion...
      
      And because that unneeded interim alloc/free of `cmdPtr` is removed in `apply` now, the branch has a bit better performance:
      <code><pre>
          % timerate -calibrate {}
          % timerate { apply {{} {}} }
          - 0.223638 µs/# 3764760 # 4471519 #/sec 841.942 net-ms
          + 0.188577 µs/# 4337271 # 5302877 #/sec 817.909 net-ms
      </pre></code>
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  16. Ticket change [7d0ee01f5d] (rid 255792) by sebres on 2026-08-27 22:09:32:

    1. icomment:
      OK, OO is also affected by similar issue (old value of cmdPtr left in `procPtr->cmdPtr`), just it is not covered now in tests.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  17. Ticket change [9568d3a7e2] (rid 255798) by jan.nijtmans on 2026-08-28 08:18:50:

    1. icomment:
      Thanks for doing all this! My conclusion: let's not rush this in Tcl 9.1b1. Since it's a long-existing problem without anyone noticing it, it doesn't have 'High' priority IMHO.
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
    4. priority changed to: "5 Medium"
  18. Ticket change [f87566b069] (rid 255799) by apnadkarni on 2026-08-28 10:07:35:

    1. icomment:
      @sebres, @chw - seems reasonable to me at the level of my competency.
      
      Agree with Jan that not worth risking putting in 9.1b1.
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  19. Ticket change [9d1ae4a543] (rid 255873) by sebres on 2026-08-31 21:44:27:

    1. closedate changed to: "2461284.4058732987"
    2. closer changed to: "sebres"
    3. icomment:
      Thus fixed with mixed variant of me and Christian in 8.6 ([107a8d20dd3eac4a])...
      
      The optimized variant is also ready and can be merged to trunk as suggested by Jan and Ashok... I have no time currently and there seems to be some bug in 9.x right now, see [4937c309f4ce3657], so I merged and adjusted 8.6 only to 9.0 (blind, so wait for the tests).
      
      Thus close as fixed.<br/>
      However as already said I still think the tclOO may be also affected by this issue (the artificial command is not restored after method invocation, so may fail by nested calls). @dkf, would you like to take a look at it?
      
    4. login: "sebres"
    5. mimetype: "text/x-fossil-wiki"
    6. status changed to: "Closed"
  20. Ticket change [20f94f9068] (rid 255874) by jan.nijtmans on 2026-09-01 07:20:48:

    1. closedate changed to: "2461284.8061210765"
    2. closer changed to: "jan.nijtmans"
    3. icomment:
      
      This happens with --enable-symbols builds now (seen on both Linux and MacOS):
      
      regexp.test
      Test file error: tcltest: /home/runner/work/tcl/tcl/generic/tclProc.c:2759: ApplyNR2: Assertion `procPtr->cmdPtr == data[0]' failed.
      
      So, I don't consider it fixed yet :-(
      
    4. login: "jan.nijtmans"
    5. mimetype: "text/x-fossil-wiki"
  21. Ticket change [5e51e47a3c] (rid 255875) by sebres on 2026-09-01 09:55:41:

    1. closedate changed to: "2461284.9136798726"
    2. closer changed to: "sebres"
    3. icomment:
      Hmm... I cannot reproduce it on 8.6, so it looks like something with merge to 9.0 (there were some conflicts) or the fix is not suitable for 9.x (I didn't tested it in 9.x, because of build-bug I mentioned before).
      
    4. login: "sebres"
    5. mimetype: "text/x-fossil-wiki"
  22. Ticket change [3a2a435428] (rid 255876) by sebres on 2026-09-01 10:07:46:

    1. closedate changed to: "2461284.9220641088"
    2. icomment:
      OK, it seems to be not suitable indeed (but I guess 8.6 may get it also some days and it is simply retarded for some reason) - test regexp-27.8 seems to destroy the lambda representation (shimmer to list due to lindex), so it is typical use after free, this time in NRE-callback... It'd be hardly solvable this way, so instead I tend to merge my optimized branch (which is not affected by that issue due to proper refcounting and clean-up in TclProcCleanupProc).
      
    3. login: "sebres"
    4. mimetype: "text/x-fossil-wiki"
  23. Ticket change [db80054ef9] (rid 255883) by sebres on 2026-09-01 11:05:51:

    1. closedate changed to: "2461284.9624042823"
    2. icomment:
      Yep, 8.6 was affected too (see new test apply-9.4)...<br/>
      Shall be fixed now in 8.6 [e76affb84c9e89ae] and 9.0 [0449bde0a12fbad6].
      
      By the way, can someone take a look to the 9.x bug [4937c309f4ce3657] (and fix build with disabled zipfs)?
      
    3. login: "sebres"
    4. mimetype: "text/x-fossil-wiki"
  24. Ticket change [4a8aae4ee3] (rid 255885) by apnadkarni on 2026-09-01 11:45:31:

    1. closedate changed to: "2461284.989945903"
    2. closer changed to: "apnadkarni"
    3. icomment:
      Sergey, GH actions show an [assertion failure](https://github.com/tcltk/tcl/actions/runs/33479135279/job/99764635253#step:9:152) in the latest run with --enable-symbols. Looks like it might be related to your changes. Could you take a look?
      
    4. login: "apnadkarni"
    5. mimetype: "text/x-markdown"
  25. Ticket change [b7ccad5024] (rid 255887) by sebres on 2026-09-01 13:26:24:

    1. closedate changed to: "2461285.060000822"
    2. closer changed to: "sebres"
    3. icomment:
      Yes, it was exactly the error Jan meant previously in [20f94f9068ad1e72].
      
      This must be fixed now by merge of my optimized branch, which was not affected by the issue... Any attempt to handle it with NRE (or directly in call-stack) are doomed to fail with more "use after free" scenarios, because shimmering could remove the lambda representation and cmdPtr stored there shall not be restored, unless we'd get another one in followed nested call, so one'd rather need proper refcounting for that).
      
    4. login: "sebres"
    5. mimetype: "text/x-fossil-wiki"