Skip to content

Prevent invalid IL from branch trampolines - #2067

Merged
Bertk merged 7 commits into
coverlet-coverage:masterfrom
Bertk:fix-release-instrumentation
Nov 1, 2026
Merged

Bertk merged 7 commits into
coverlet-coverage:masterfrom
Bertk:fix-release-instrumentation

Conversation

@Bertk

@Bertk Bertk commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

Detect unsafe exception-handler boundaries and fall back to inline instrumentation. Preserve original TryStart boundaries to avoid invalid loop and try/finally IL.

Add issue #2058 regression coverage, sample control-flow methods, and JIT-preparation validation with diagnostic IL output.

Bertk added 3 commits November 1, 2026 09:34
Detect unsafe exception-handler boundaries and fall back to inline instrumentation. Preserve original `TryStart` boundaries to avoid invalid loop and `try/finally` IL.

Add issue coverlet-coverage#2058 regression coverage, sample control-flow methods, and JIT-preparation validation with diagnostic IL output.
Update exception-handler boundary rewriting so `TryStart` follows inserted branch-target instructions, preserving intra-try back-edges in optimized `do/while` loops. Add Issue 2058 release-instrumentation coverage and a `try/finally` loop regression sample.
Add ILVerify 10.0.14 as a local tool and recommend it for IL investigations. Track instructions immediately preceding exception-handler `TryStart` offsets and skip unsafe taken-branch counter injection when trampolines are unavailable, preserving inline fallback for other cases.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical instrumentation defect can redirect loop back-edges outside a try region without leave, producing invalid IL and requiring coordinated TryStart and branch-target handling plus a Release JIT-preparation regression test.

1 open issue
What changed in this PR

This PR addresses issue #2058, where coverage instrumentation can produce invalid IL for optimized, never-returning loops containing try/finally or lock statements.

Changes:

  • Disables branch trampolines for open-ended exception regions, using inline instrumentation instead.
  • Leaves original TryStart boundaries unchanged.
  • Adds three regression samples and JIT-preparation checks with diagnostic IL output.
File Description
test/​coverlet.core.coverage.tests/​Samples/​Instrumentation.Issue2058.cs Adds infinite-loop samples with conditionals, try/finally, and locks.
test/​coverlet.core.coverage.tests/​Coverage/​CoverageTests.Issue2058.cs Checks instrumented methods can be JIT-compiled without executing the loops.
src/​coverlet.core/​Instrumentation/​Instrumenter.cs Adds the trampoline safety guard and changes exception-boundary handling.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The guard skips registration of the taken branch and its hit counter, reducing the coverage denominator and potentially reporting 100% branch coverage without exercising both outcomes.

1 open issue

Review effort: Balanced

Use local branch trampolines when standard trampolines are unavailable before exception-handler boundaries. Refactor branch candidate registration, track local trampoline redirections, and preserve branch hit tracking across netstandard2.0.

Extend Issue 2058 coverage tests to verify both branch paths and filter recorded branch entries with LINQ.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Forward-branch trampoline insertion shifts the mutable instruction collection while the loop cursor remains unchanged, causing instructions to be revisited and risking duplicate-key exceptions or incomplete method instrumentation.

1 open issue
1 resolved since last review

Review effort: Balanced

Process the pre-instrumentation instruction snapshot with a stable index
instead of traversing the mutable instruction list. Remove manual index
adjustments so injected instructions cannot alter iteration or cause original
instructions to be skipped or processed incorrectly. Preserve target mapping
and instruction-target replacement after instrumentation.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because branch instrumentation around exception regions can produce invalid IL and incorrectly report unexecuted paths as covered.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This was referenced Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InvalidProgramException in Release: a never-returning loop with an if directly before a try/finally

2 participants