Skip to content

perf: csharp func_start exponential backtracking on adjacent [...] (still fuses a roslyn file → 0 functions) #3320

Closed

Description

@squid-protocol

Follow-up to #3182 / PR #3317 (which fixed the Branch C lookahead ReDoS). This is the second, still-open csharp func_start backtracking trigger.

Impact / severity: priority: medium (not just speed)

Mechanism

A [...] run at a line start has three derivations:

  1. the attribute shield ^[ \t]*(?:\[[^\]]{0,250}\][ \t\n]*){0,5}
  2. single-char tokens in the return-type loop: [a-zA-Z0-9_<>\[\]?.*] contains [ and ]
  3. the token alternative \[[^\]]{0,80}\]

When no identifier + ( follows, the engine explores every combination before failing. That's exponential in adjacent brackets, repeated for each {0,5} shield count and up to {1,10} tokens. Worst observed: roslyn expected-output text [GetA][Get0][G1][operator]1True in code_stream costs 29s for one line-start attempt. Removing the attribute shield alone cuts an [InlineData] block ~6× (0.342s → 0.058s).

Repro: func_start.match(code_stream, <line start>) on line ~13791 of the file above, or stacked [InlineData(1, 2)] lines.

Fix options (why this isn't a drop-in like #3317)

  • Atomic groups / possessive quantifiers ((?>...), *+) would kill the ambiguity with minimal semantic change, but need Python ≥ 3.11. requires-python = ">=3.9" and CI still tests 3.9/3.10 on all three OSes. This is the cleanest route if we raise the floor to 3.11.
  • On 3.9: emulate atomicity with (?=(...))\1, or disambiguate (drop [/]/</> from the single-char class so brackets go only through their group alternatives). Both change accepted strings in edge cases, e.g. 3-level space-free generics A<B<C<D>>> or unbalanced x[. They need a parity policy: measure the function-set delta on the C# corpus (roslyn/runtime/PowerShell/crucible) and accept or reject it.

Acceptance

  • UserDefinedCompoundAssignmentOperatorsTests.cs under the fuse, with functions extracted.
  • Match-set delta on the C# corpus reported (zero, or explicitly accepted).
  • ReDoS regression tests for the [InlineData] stack and the [GetA][Get0]... shape.
  • Golden crucible both legs.

🤖 Generated with Claude Code

Activity

  1. squid-protocol commented on Sep 23, 2026

    @squid-protocol
    OwnerAuthor

    Preferred fix route depends on #3322 (raise the Python floor to 3.11 for atomic groups / possessive quantifiers). Community input in #3323.

  2. added 3 commits that reference this issue on Oct 12, 2026
  3. added
    scope:infra
    on Oct 16, 2026
  4. squid-protocol commented on Oct 17, 2026

    @squid-protocol
    OwnerAuthor

    Acceptance met. #4999 made C# func_start linear (UserDefinedCompoundAssignmentOperatorsTests.cs: 60s timeout/0 functions -> 1264 matches in 0.1s), reported the match-set delta (crucible 0; estates 1829 phantom matches lost, 33 gained), added the ReDoS regression tests (InlineData stack, [GetA][Get0] shape) and ran the golden crucible with no drift on both legs. #5356 profiled the residual Mode B brace slicer: no engine defect (linear; the 75s was match volume) and added a slicer guard test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancepriority: mediumscope:infra

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions