Repository navigation
Prevent invalid IL from branch trampolines - #2067
Conversation
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.
There was a problem hiding this comment.
🟡 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
TryStartboundaries 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
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.
There was a problem hiding this comment.
🟡 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.
There was a problem hiding this comment.
🔵 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


Detect unsafe exception-handler boundaries and fall back to inline instrumentation. Preserve original
TryStartboundaries to avoid invalid loop andtry/finallyIL.Add issue #2058 regression coverage, sample control-flow methods, and JIT-preparation validation with diagnostic IL output.