Skip to content
Tcl Source Code History Of Ticket 7d47ba7d94
Login

History Of Ticket 7d47ba7d94

Artifacts Associated With Ticket 7d47ba7d94

  1. Ticket change [31993579eb] (rid 143529) by tgl on 2015-10-17 03:38:02:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. comment initialized to:
      There are various ways to drive the regexp code into stack overflow.  An example is that
      
      regexp {(^$)+} {z}
      
      will cause a stack overrun in markreachable() in 8.6.4.  (On my x86_64 machine, if I set "ulimit -s 1024" it takes about 4 minutes to get to this point, but it will get there.)  I believe this is driven by the problem discussed in http://core.tcl.tk/tcl/tktview?name=8f245009b0 which I plan to submit a fix for, but the point that concerns me right now is that there are multiple places that recurse to a depth limited only by the size of the regexp NFA, and if you don't have lots of stack space you are going to get a SIGSEGV on stack overflow.
      
      The Postgres project has decided to deal with this hazard by uniformly installing stack overflow checks in all recursive functions in the regex code; see
      http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=b63fc28776c5d2efdb4de326ad0f0b5b88f82220
      
      I would like to submit a Tcl-adapted version of that patch, but I do not know what implementation technology would be considered acceptable, so the point of this bug report is to ask.
      
      I see that you guys previously noted the possibility of stack overrun in duptraverse() and fixed it with a hard-wired recursion depth count limit.  That answer doesn't seem too appealing to me: it complicates the function's API and it's very unclear what number to use for the limit.  So propagating the same methodology into a dozen other places is not attractive.
      
      The technology Postgres is using depends on measuring the distance from a function's local variables to a stack base pointer established at execution startup.  I do not see how we can use that exact approach in Tcl, since Tcl is fundamentally a library not an application and can't run at execution start; but I wonder if it would be okay to decree that we'll allow a maximum stack depth of so-and-so-many kilobytes measured from the entry to regcomp() or regexec() respectively.  That could be mechanized by taking the address of the "struct vars" struct as being the measurement reference.
      
      Comments or better ideas appreciated.
      
    5. foundin initialized to: "8.6.4"
    6. is_private initialized to: "0"
    7. login: "tgl"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "26864b65f1c6ce8bda86f87ed9c554e19ac0390d"
    10. resolution initialized to: "None"
    11. severity initialized to: "Important"
    12. status initialized to: "Open"
    13. submitter initialized to: "tgl"
    14. subsystem initialized to: "43. Regexp"
    15. title initialized to: "Stack overruns in regexp code"
    16. type initialized to: "Bug"
  2. Ticket change [041721364b] (rid 143531) by tgl on 2015-10-17 04:18:43:

    1. icomment:
      Note: open tickets 1905562 and 267b7e2334 both appear related to this.
      
    2. login: "tgl"
    3. mimetype: "text/plain"
  3. Ticket change [1756e75034] (rid 143568) by dgp on 2015-10-19 15:35:29:

    1. icomment:
      
      I don't have much to add.  Just want to confirm your instinct
      that the hard limit we stuck into Tcl 8.6 was a mistake that
      you do not want to repeat.
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  4. Ticket change [7b3e61535a] (rid 143610) by dgp on 2015-10-21 14:20:53:

    1. icomment:
      
      I brought over some of this, but since the stack depth
      test doesn't port, I just punted and created a
      STACK_TOO_DEEP() that always returns false, so
      stack checking is a no-op.  Room for improvement.
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  5. Ticket change [3e8a7045f3] (rid 143621) by tgl on 2015-10-21 17:04:26:

    1. icomment:
      OK, I'll submit a patch in this ticket to make it do something real, but probably not for a week or two.
      
      Is this code checked into mainline yet?
      
    2. login: "tgl"
    3. mimetype: "text/plain"
  6. Ticket change [a1745c694b] (rid 257532) by serhiy.storchaka on 2026-10-10 20:48:01:

    1. foundin changed to: "8.6.18, 9.0.5, 9.1.0"
    2. icomment:
      The <tt>(^$)+</tt> example is long fixed, but stack overflow was still easy to trigger in 8.6, 9.0 and 9.1: parentheses nested 40000 deep crashed in <tt>parse()</tt>, and lookahead constraints nested only 600 deep crashed in the <tt>checkLAConstraint()</tt> recursion, because each level kept a 16 KB small DFA on the stack. <tt>STACK_TOO_DEEP()</tt> was still defined as 0.
      
      Proposed fix in branch [/timeline?r=regexp-stack-depth|regexp-stack-depth], following your proposal: <tt>STACK_TOO_DEEP()</tt> limits the stack used by one compile or execution to <tt>REG_MAX_STACK_DEPTH</tt> (1 MB by default), measured from a local variable of <tt>compile()</tt> or <tt>exec()</tt>, and it is checked in the same places as in PostgreSQL. Lookahead DFAs are cached per execution, as in PostgreSQL's <tt>getladfa()</tt>, instead of being built on the stack. Too deep nesting now fails with "regular expression is too complex". Tests reg-33.32 to 33.35.
      
    3. login: "serhiy.storchaka"
    4. mimetype: "text/x-fossil-wiki"
  7. Ticket change [3ea17d5f13] (rid 257594) by serhiy.storchaka on 2026-10-11 10:09:04:

    1. icomment:
      A fixed limit cannot protect threads with smaller stacks: with a 1 MB stack, the branch crashed in <tt>duptraverse()</tt>. Now <tt>STACK_TOO_DEEP()</tt> also uses the real stack limit of the current thread where the system reports it (new internal <tt>TclpGetCStackLimit()</tt>: pthread_getattr_np, pthread_attr_get_np, pthread_get_stackaddr_np, pthread_stackseg_np, thr_stksegment, VirtualQuery), with <tt>REG_MAX_STACK_DEPTH</tt> as a cap. New test stack-4.1 runs deep REs with a 256 KB stack. Tested on Linux, Windows, FreeBSD, DragonFly, NetBSD, OpenBSD and OpenIndiana.
      
    2. login: "serhiy.storchaka"
    3. mimetype: "text/x-fossil-wiki"
  8. Ticket change [899feddc87] (rid 259347) by jan.nijtmans on 2026-10-19 21:08:23:

    1. assignee changed to: "jan.nijtmans"
    2. icomment:
      Now merged to [4a966b9df4|trunk]
      
      In addition, tested on MacOS. CI build is clean.
      
      I'm reluctant to merge further to core-9-0-branch for now, since Tcl 9.0.5 is coming soon.
      
    3. login: "jan.nijtmans"
    4. mimetype: "text/x-fossil-wiki"
    5. resolution changed to: "Fixed"
    6. status changed to: "Pending"
  9. Ticket change [dcd2721495] (rid 260717) by serhiy.storchaka on 2026-10-28 11:04:20:

    1. icomment:
      On Alpine Linux (musl), stack-4.1 segfaults: <tt>TclpGetCStackLimit()</tt> returns NULL there, so the 1 MB default is used. Fixed on branch [/timeline?r=musl-stack-limit|musl-stack-limit].
      
    2. login: "serhiy.storchaka"
    3. mimetype: "text/x-fossil-wiki"
  10. Ticket change [21e00ce766] (rid 260742) by jan.nijtmans on 2026-10-28 13:53:40:

    1. icomment:
      > On Alpine Linux (musl), stack-4.1 segfaults
      
      Thanks!  This part now merged to trunk too.
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  11. Ticket change [55c20fc863] (rid 261037) by dkf on 2026-11-01 16:57:43:

    1. closedate changed to: "2461346.2067539003"
    2. closer changed to: "dkf"
    3. login: "dkf"
    4. mimetype: "text/plain"
    5. status changed to: "Closed"
  12. Ticket change [a5c3dd4c0f] (rid 261601) by serhiy.storchaka on 2026-11-13 19:26:13:

    1. closer changed to: "nobody"
    2. icomment:
      The stack check misfires under AddressSanitizer with its fake stack (the default in GCC 15): addresses of local variables are then on the heap, so the computed depth is meaningless, and even loading a package fails with "regular expression is too complex". Proposed fix in branch [/timeline?r=regexp-stack-check-asan|regexp-stack-check-asan]: use the frame address with GCC and Clang.
      
    3. login: "serhiy.storchaka"
    4. mimetype: "text/x-fossil-wiki"
    5. resolution changed to: "None"
    6. status changed to: "Open"
  13. Ticket change [7eb572b882] (rid 261713) by chrstphrchvz on 2026-11-15 01:26:03:

    1. icomment:
      Can the fix for this ticket instead make use of <tt>TclGetCStackPtr()</tt> (the existing fix for [7371b6270b])? I believe <tt>REG_STACK_ADDR()</tt> might be duplicating it.
      
    2. login: "chrstphrchvz"
    3. mimetype: "text/x-fossil-wiki"
  14. Ticket change [dafe13f3fb] (rid 261731) by serhiy.storchaka on 2026-11-15 14:30:08:

    1. icomment:
      It is the same technique (<tt>__builtin_frame_address(0)</tt>), but calling <tt>TclGetCStackPtr()</tt> in the check makes regexp compilation about 5% slower, since the check runs at every recursion level. I'd rather keep the macro inline, and add <tt>_AddressOfReturnAddress()</tt> for MSVC as <tt>TclGetCStackPtr()</tt> does.
      
    2. login: "serhiy.storchaka"
    3. mimetype: "text/x-fossil-wiki"
  15. Ticket change [a3b05b5380] (rid 261876) by jan.nijtmans on 2026-11-18 16:05:12:

    1. icomment:
      > I'm reluctant to merge further to core-9-0-branch for now, since Tcl 9.0.5 is coming soon
      
      Well, I was right about this reluctancy: two follow-up commits were needed to make it fully right in various situations. But now the code has been on trunk some time, without further indications of any trouble, it's time to backport.
      
      So, backported now to core-9-0-branch. Serhiy, if you want to backport further to 8.6, please go ahead! +1. This would be nice for Tcl 9.8.19.
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
    4. resolution changed to: "Fixed"
    5. status changed to: "Pending"
  16. Ticket change [4c3dea3ce2] (rid 261877) by jan.nijtmans on 2026-11-18 16:06:02:

    1. icomment:
      > This would be nice for Tcl 9.8.19
      
      That should have been Tcl 8.6.19
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  17. Ticket change [0be96ffbb5] (rid 261912) by serhiy.storchaka on 2026-11-18 18:27:58:

    1. icomment:
      Backported to core-8-6-branch: [e720024d74]. 8.6 has no <tt>TCL_NOINLINE</tt> in tcl.h, so regguts.h defines it locally.
      
    2. login: "serhiy.storchaka"
    3. mimetype: "text/x-fossil-wiki"
  18. Ticket change [adb199c0ef] (rid 261916) by serhiy.storchaka on 2026-11-18 18:32:47:

    1. closedate changed to: "2461363.272776770872"
    2. closer changed to: "serhiy.storchaka"
    3. login: "serhiy.storchaka"
    4. resolution changed to: "Fixed"
    5. status changed to: "Closed"